Skip to content

Commit 807defe

Browse files
authored
Merge pull request #23 from Geode-solutions/binding_python
feat(BindingAPI): exose necessary classes
2 parents d95135a + 3d95041 commit 807defe

29 files changed

Lines changed: 1058 additions & 459 deletions

bindings/python/src/stochastic/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@
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
34+
OpenGeode::basic
35+
OpenGeode::geometry
36+
OpenGeode::model
2837
)

bindings/python/src/stochastic/random_engine.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
} // namespace geode
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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/distributions.hpp>
25+
26+
namespace geode
27+
{
28+
29+
void define_distributions( pybind11::module& module )
30+
{
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== );
37+
38+
// UniformClosed<double>
39+
pybind11::class_< UniformClosed< double > >( module, "UniformClosed" )
40+
.def( pybind11::init<>() )
41+
.def_readwrite( "min_value", &UniformClosed< double >::min_value )
42+
.def_readwrite( "max_value", &UniformClosed< double >::max_value )
43+
.def( "is_valid", &UniformClosed< double >::is_valid )
44+
.def( "distribution_type_static",
45+
&UniformClosed< double >::distribution_type_static )
46+
.def( "distribution_type",
47+
&UniformClosed< double >::distribution_type )
48+
.def( "string", &UniformClosed< double >::string );
49+
50+
// UniformClosedOpen<double>
51+
pybind11::class_< UniformClosedOpen< double > >(
52+
module, "UniformClosedOpen" )
53+
.def( pybind11::init<>() )
54+
.def_readwrite(
55+
"min_value", &UniformClosedOpen< double >::min_value )
56+
.def_readwrite(
57+
"max_value", &UniformClosedOpen< double >::max_value )
58+
.def( "is_valid", &UniformClosedOpen< double >::is_valid )
59+
.def( "distribution_type_static",
60+
&UniformClosedOpen< double >::distribution_type_static )
61+
.def( "distribution_type",
62+
&UniformClosedOpen< double >::distribution_type )
63+
.def( "string", &UniformClosedOpen< double >::string );
64+
65+
// Gaussian
66+
pybind11::class_< Gaussian >( module, "Gaussian" )
67+
.def( pybind11::init<>() )
68+
.def_readwrite( "mean", &Gaussian::mean )
69+
.def_readwrite(
70+
"standard_deviation", &Gaussian::standard_deviation )
71+
.def( "is_valid", &Gaussian::is_valid )
72+
.def( "distribution_type_static",
73+
&Gaussian::distribution_type_static )
74+
.def( "distribution_type", &Gaussian::distribution_type )
75+
.def( "string", &Gaussian::string );
76+
77+
// TruncatedGaussian
78+
pybind11::class_< TruncatedGaussian >( module, "TruncatedGaussian" )
79+
.def( pybind11::init<>() )
80+
.def_readwrite( "mean", &TruncatedGaussian::mean )
81+
.def_readwrite(
82+
"standard_deviation", &TruncatedGaussian::standard_deviation )
83+
.def_readwrite( "min_value", &TruncatedGaussian::min_value )
84+
.def_readwrite( "max_value", &TruncatedGaussian::max_value )
85+
.def( "is_valid", &TruncatedGaussian::is_valid )
86+
.def( "distribution_type_static",
87+
&TruncatedGaussian::distribution_type_static )
88+
.def( "distribution_type", &TruncatedGaussian::distribution_type )
89+
.def( "string", &TruncatedGaussian::string );
90+
}
91+
92+
} // 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&,
73+
// index_t ) >( &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)