Skip to content

Commit 575063a

Browse files
feat(BindingAPI): exose necessary classes
1 parent d95135a commit 575063a

27 files changed

Lines changed: 923 additions & 379 deletions

bindings/python/src/stochastic/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
add_geode_python_binding(
2222
NAME "py_stochastic"
2323
SOURCES
24-
# "random_engine.cpp"
24+
"sampling/mcmc/helpers/fracture_simulation_runner.hpp"
25+
"sampling/mcmc/helpers/simulation_monitor.hpp"
26+
"sampling/mcmc/helpers/simulation_printer.hpp"
27+
"sampling/mcmc/helpers/simulation_runner.hpp"
28+
"sampling/direct/double_sampler.hpp"
29+
"sampling/random_engine.hpp"
30+
"sampling/distributions.hpp"
2531
"stochastic.cpp"
2632
DEPENDENCIES
2733
${PROJECT_NAME}::stochastic

bindings/python/src/stochastic/random_engine.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#include <geode/stochastic/sampling/direct/double_sampler.hpp>
25+
26+
namespace geode
27+
{
28+
void define_double_sampler( pybind11::module& module )
29+
{
30+
// Bind DistributionDescription
31+
pybind11::class_< DoubleSampler::DistributionDescription >(
32+
module, "DoubleDistributionDescription" )
33+
.def( pybind11::init<>() )
34+
.def_readwrite(
35+
"name", &DoubleSampler::DistributionDescription::name )
36+
.def_readwrite( "distribution_type",
37+
&DoubleSampler::DistributionDescription::distribution_type )
38+
.def_readwrite( "min_value",
39+
&DoubleSampler::DistributionDescription::min_value )
40+
.def_readwrite( "max_value",
41+
&DoubleSampler::DistributionDescription::max_value )
42+
.def_readwrite(
43+
"mean", &DoubleSampler::DistributionDescription::mean )
44+
.def_readwrite( "standard_deviation",
45+
&DoubleSampler::DistributionDescription::standard_deviation )
46+
.def( "string", &DoubleSampler::DistributionDescription::string,
47+
"Return a detailed description of the Distribution law" )
48+
.def( "__repr__",
49+
[]( const DoubleSampler::DistributionDescription& d ) {
50+
return "<DoubleDistributionDescription name=" + d.name
51+
+ ">";
52+
} );
53+
54+
// Bind Distribution variant
55+
pybind11::class_< DoubleSampler::Distribution >(
56+
module, "Distribution" );
57+
58+
// Bind DoubleSampler
59+
pybind11::class_< DoubleSampler >( module, "DoubleSampler" )
60+
.def_static( "create_distribution",
61+
&DoubleSampler::create_distribution, pybind11::arg( "desc" ),
62+
"Create a distribution from a description" )
63+
.def_static( "sample", &DoubleSampler::sample,
64+
pybind11::arg( "engine" ), pybind11::arg( "dist" ),
65+
"Sample a value from a distribution using a RandomEngine" );
66+
67+
// Optionally, expose the variant types
68+
pybind11::class_< UniformClosed< double > >(
69+
module, "UniformClosedDouble" )
70+
.def( pybind11::init<>() )
71+
.def_readwrite( "min_value", &UniformClosed< double >::min_value )
72+
.def_readwrite( "max_value", &UniformClosed< double >::max_value );
73+
74+
pybind11::class_< UniformClosedOpen< double > >(
75+
module, "UniformClosedOpenDouble" )
76+
.def( pybind11::init<>() )
77+
.def_readwrite(
78+
"min_value", &UniformClosedOpen< double >::min_value )
79+
.def_readwrite(
80+
"max_value", &UniformClosedOpen< double >::max_value );
81+
82+
pybind11::class_< Gaussian >( module, "Gaussian" )
83+
.def( pybind11::init<>() )
84+
.def_readwrite( "mean", &Gaussian::mean )
85+
.def_readwrite(
86+
"standard_deviation", &Gaussian::standard_deviation );
87+
88+
pybind11::class_< TruncatedGaussian >( module, "TruncatedGaussian" )
89+
.def( pybind11::init<>() )
90+
.def_readwrite( "mean", &TruncatedGaussian::mean )
91+
.def_readwrite(
92+
"standard_deviation", &TruncatedGaussian::standard_deviation )
93+
.def_readwrite( "min_value", &TruncatedGaussian::min_value )
94+
.def_readwrite( "max_value", &TruncatedGaussian::max_value );
95+
}
96+
} // namespace geode

src/geode/stochastic/hello_world.cpp renamed to bindings/python/src/stochastic/sampling/distributions.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@
2121
*
2222
*/
2323

24-
#include <geode/stochastic/hello_world.hpp>
25-
26-
#include <geode/basic/logger.hpp>
24+
#include <geode/stochastic/sampling/distributions.hpp>
2725

2826
namespace geode
2927
{
30-
bool hello_world()
28+
29+
void define_distributions( pybind11::module& module )
3130
{
32-
geode::Logger::info( "Hello Geode Stochastic World!" );
33-
return true;
31+
// DistributionType
32+
pybind11::class_< DistributionType >( module, "DistributionType" )
33+
.def( pybind11::init<>() )
34+
.def( pybind11::init< std::string >() )
35+
.def( "get", &DistributionType::get )
36+
.def( "matches", &DistributionType::operator== );
3437
}
38+
3539
} // namespace geode
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#include <geode/stochastic/sampling/mcmc/helpers/fracture_simulation_runner.hpp>
25+
26+
namespace geode
27+
{
28+
void define_fracture_simulation( pybind11::module& module )
29+
{
30+
using namespace geode;
31+
32+
pybind11::class_< FractureSetDescription >(
33+
module, "FractureSetDescription" )
34+
.def( pybind11::init<>() )
35+
.def_readwrite( "name", &FractureSetDescription::name )
36+
.def_readwrite( "length", &FractureSetDescription::length )
37+
.def_readwrite( "azimuth", &FractureSetDescription::azimuth )
38+
.def_readwrite( "p20", &FractureSetDescription::p20 )
39+
.def_readwrite(
40+
"minimal_spacing", &FractureSetDescription::minimal_spacing )
41+
.def_readwrite(
42+
"birth_ratio", &FractureSetDescription::birth_ratio )
43+
.def_readwrite(
44+
"death_ratio", &FractureSetDescription::death_ratio )
45+
.def_readwrite(
46+
"change_ratio", &FractureSetDescription::change_ratio )
47+
.def( "string", &FractureSetDescription::string,
48+
"Return a detailed description of the fracture set" )
49+
.def( "__repr__", []( const FractureSetDescription& self ) {
50+
return "<FractureSetDescription name='" + self.name + "'>";
51+
} );
52+
53+
pybind11::class_< FractureSimulationRunner,
54+
std::shared_ptr< FractureSimulationRunner > >(
55+
module, "FractureSimulationRunner" )
56+
.def( pybind11::init< const BoundingBox2D& >(),
57+
pybind11::arg( "box" ) )
58+
.def( "add_fracture_set_descriptor",
59+
&FractureSimulationRunner::add_fracture_set_descriptor,
60+
pybind11::arg( "descriptor" ),
61+
"Add a fracture set configuration to the simulation." )
62+
.def( "initialize", &FractureSimulationRunner::initialize,
63+
"Initialize internal samplers, energy terms, and proposal "
64+
"kernels." )
65+
.def( "check_statistics",
66+
&FractureSimulationRunner::check_statistics,
67+
pybind11::arg( "statistic_monitoring" ),
68+
"Check computed statistics after simulation." )
69+
// Explicit overload bindings
70+
.def( "run",
71+
static_cast< const ObjectSets< OwnerSegment2D >& (
72+
FractureSimulationRunner::*) ( RandomEngine&, index_t ) >(
73+
&FractureSimulationRunner::run ),
74+
pybind11::arg( "engine" ), pybind11::arg( "steps" ),
75+
"Run simulation for a fixed number of steps." )
76+
.def( "run",
77+
static_cast< StatisticsMonitor ( FractureSimulationRunner::* )(
78+
RandomEngine&, const SimulationConfigurator& ) >(
79+
&FractureSimulationRunner::run ),
80+
pybind11::arg( "engine" ), pybind11::arg( "config" ),
81+
"Run the simulation and return statistics monitoring "
82+
"results." );
83+
}
84+
} // namespace geode
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#include <geode/stochastic/sampling/mcmc/helpers/simulation_monitor.hpp>
25+
26+
namespace geode
27+
{
28+
void define_simulation_monitor( pybind11::module& module )
29+
{
30+
pybind11::class_< geode::StatisticsMonitor >(
31+
module, "StatisticsMonitor" )
32+
.def( pybind11::init< geode::index_t >(),
33+
pybind11::arg( "nb_energy_terms" ),
34+
"Create a StatisticsMonitor for a given number of energy "
35+
"terms" )
36+
.def( "add_realization", &geode::StatisticsMonitor::add_realization,
37+
pybind11::arg( "values" ),
38+
"Add a realization (vector of doubles) to update statistics" )
39+
.def( "statiscal_count", &geode::StatisticsMonitor::statiscal_count,
40+
"Return the number of realizations added" )
41+
.def_property_readonly( "means", &geode::StatisticsMonitor::means,
42+
"Return the computed mean values for each energy term" )
43+
.def_property_readonly( "variances",
44+
&geode::StatisticsMonitor::variances,
45+
"Return the computed variances for each energy term" )
46+
.def( "__repr__", []( const geode::StatisticsMonitor& self ) {
47+
return "<StatisticsMonitor count="
48+
+ std::to_string( self.statiscal_count() ) + ">";
49+
} );
50+
}
51+
} // namespace geode

0 commit comments

Comments
 (0)