Skip to content

Commit c963096

Browse files
Merge pull request #44 from Geode-solutions/fracture_app
feat(Xnode): restore x_node managment
2 parents b832e33 + f8c9b0a commit c963096

21 files changed

Lines changed: 598 additions & 671 deletions

File tree

bindings/python/src/common.hpp

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

24+
#pragma once
25+
2426
#include <pybind11/operators.h>
2527
#include <pybind11/pybind11.h>
2628
#include <pybind11/stl.h>
@@ -30,51 +32,49 @@
3032
#include <absl/container/inlined_vector.h>
3133
#include <absl/types/span.h>
3234

33-
namespace pybind11
35+
namespace pybind11::detail
3436
{
35-
namespace detail
37+
38+
template < typename Type >
39+
struct type_caster< absl::FixedArray< Type > >
40+
: list_caster< absl::FixedArray< Type >, Type >
3641
{
37-
template < typename Type >
38-
struct type_caster< absl::FixedArray< Type > >
39-
: list_caster< absl::FixedArray< Type >, Type >
40-
{
41-
};
42+
};
4243

43-
template < typename Type, size_t dimension >
44-
struct type_caster< absl::InlinedVector< Type, dimension > >
45-
: list_caster< absl::InlinedVector< Type, dimension >, Type >
46-
{
47-
};
44+
template < typename Type, size_t dimension >
45+
struct type_caster< absl::InlinedVector< Type, dimension > >
46+
: list_caster< absl::InlinedVector< Type, dimension >, Type >
47+
{
48+
};
4849

49-
template < typename Type >
50-
struct type_caster< absl::Span< Type > >
51-
: list_caster< absl::Span< Type >, Type >
52-
{
53-
using value_conv = make_caster< Type >;
50+
template < typename Type >
51+
struct type_caster< absl::Span< Type > >
52+
: list_caster< absl::Span< Type >, Type >
53+
{
54+
using value_conv = make_caster< Type >;
5455

55-
bool load( handle src, bool convert )
56+
bool load( handle src, bool convert )
57+
{
58+
cpp_.clear();
59+
auto s = reinterpret_borrow< sequence >( src );
60+
cpp_.reserve( s.size() );
61+
for( auto it : s )
5662
{
57-
cpp_.clear();
58-
auto s = reinterpret_borrow< sequence >( src );
59-
cpp_.reserve( s.size() );
60-
for( auto it : s )
61-
{
62-
value_conv conv;
63-
if( !conv.load( it, convert ) )
64-
return false;
65-
cpp_.push_back( cast_op< Type&& >( std::move( conv ) ) );
66-
}
67-
this->value = absl::MakeConstSpan( cpp_ );
68-
return true;
63+
value_conv conv;
64+
if( !conv.load( it, convert ) )
65+
return false;
66+
cpp_.push_back( cast_op< Type&& >( std::move( conv ) ) );
6967
}
68+
this->value = absl::MakeConstSpan( cpp_ );
69+
return true;
70+
}
7071

71-
std::vector< typename std::remove_const< Type >::type > cpp_;
72-
};
72+
std::vector< typename std::remove_const< Type >::type > cpp_;
73+
};
7374

74-
template < typename Key, typename Value >
75-
struct type_caster< absl::flat_hash_map< Key, Value > >
76-
: map_caster< absl::flat_hash_map< Key, Value >, Key, Value >
77-
{
78-
};
79-
} // namespace detail
80-
} // namespace pybind11
75+
template < typename Key, typename Value >
76+
struct type_caster< absl::flat_hash_map< Key, Value > >
77+
: map_caster< absl::flat_hash_map< Key, Value >, Key, Value >
78+
{
79+
};
80+
} // namespace pybind11::detail

bindings/python/src/stochastic/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
add_geode_python_binding(
2222
NAME "py_stochastic"
2323
SOURCES
24-
# "sampling/mcmc/helpers/fracture_simulation_runner.hpp"
25-
"sampling/mcmc/helpers/simulation_monitor.hpp"
24+
"applications/fractures.hpp"
25+
"inference/statistics_tracker.hpp"
2626
"sampling/mcmc/helpers/simulation_printer.hpp"
2727
"sampling/mcmc/simulation_runner.hpp"
2828
"sampling/direct/double_sampler.hpp"
2929
"sampling/random_engine.hpp"
3030
"sampling/distributions.hpp"
3131
"spatial/spatial_domain.hpp"
32+
"spatial/object_sets.hpp"
3233
"stochastic.cpp"
3334
DEPENDENCIES
3435
${PROJECT_NAME}::stochastic
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright (c) 2019 - 2026 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+
#pragma once
24+
25+
#include "../../common.hpp"
26+
#include <geode/stochastic/applications/fractures.hpp>
27+
28+
namespace geode
29+
{
30+
void define_fracture_network_description( pybind11::module& module )
31+
{
32+
pybind11::class_< FractureSamplerConfig >( module,
33+
"FractureSamplerConfig",
34+
"Configuration of the stochastic fracture sampler." )
35+
.def( pybind11::init<>() )
36+
.def_readwrite( "move_ratio", &FractureSamplerConfig::move_ratio,
37+
"Relative probability of proposing a change move." )
38+
.def_readwrite( "length", &FractureSamplerConfig::length,
39+
"Distribution used to sample fracture lengths." )
40+
.def_readwrite( "azimuth", &FractureSamplerConfig::azimuth,
41+
"Distribution used to sample fracture orientations." );
42+
43+
pybind11::class_< FractureSetDescription >( module,
44+
"FractureSetDescription", "Description of a fracture family." )
45+
.def( pybind11::init<>() )
46+
47+
.def_readwrite( "name", &FractureSetDescription::fset_name,
48+
"Name of the fracture set." )
49+
50+
.def_readwrite( "sampler", &FractureSetDescription::sampler,
51+
"Sampling configuration used to generate fractures." )
52+
53+
.def_readwrite( "birth_ratio", &FractureSetDescription::birth_ratio,
54+
"Relative probability of birth moves." )
55+
56+
.def_readwrite( "death_ratio", &FractureSetDescription::death_ratio,
57+
"Relative probability of death moves." )
58+
59+
.def_readwrite( "change_ratio",
60+
&FractureSetDescription::change_ratio,
61+
"Relative probability of change moves." )
62+
63+
.def_readwrite( "p20", &FractureSetDescription::p20,
64+
"Target fracture density (number of fractures per unit area)." )
65+
66+
.def_readwrite( "expected_number",
67+
&FractureSetDescription::expected_number,
68+
"Expected number of fractures, if monitored." )
69+
70+
.def_readwrite( "p21", &FractureSetDescription::p21,
71+
"Target fracture intensity (total fracture length per unit "
72+
"area)." )
73+
74+
.def_readwrite( "expected_total_length",
75+
&FractureSetDescription::expected_total_length,
76+
"Expected cumulative fracture length, if monitored." )
77+
78+
.def(
79+
"add_observed_fracture",
80+
[]( FractureSetDescription& self, const geode::Point2D& start,
81+
const geode::Point2D& end ) {
82+
self.observed_fractures.push_back( { start, end } );
83+
},
84+
"Add a fixed observed fracture defined by two endpoints." )
85+
86+
.def_readwrite( "minimal_spacing",
87+
&FractureSetDescription::minimal_spacing,
88+
"Minimum allowed spacing between fractures." )
89+
90+
.def( "string", &FractureSetDescription::string,
91+
"Return a detailed textual description of the fracture set." )
92+
93+
.def( "__repr__", []( const FractureSetDescription& self ) {
94+
return "<FractureSetDescription name='" + self.fset_name + "'>";
95+
} );
96+
97+
pybind11::class_< FractureNetworkDescription >( module,
98+
"FractureNetworkDescription",
99+
"Description of a complete fracture network simulation." )
100+
101+
.def( pybind11::init<>() )
102+
103+
.def_readwrite( "name", &FractureNetworkDescription::fnet_name,
104+
"Name of the fracture network." )
105+
106+
.def_readwrite( "domain", &FractureNetworkDescription::domain,
107+
"Spatial simulation domain." )
108+
109+
.def_readwrite( "fracture_sets",
110+
&FractureNetworkDescription::fracture_sets,
111+
"List of fracture sets composing the network." )
112+
113+
.def( "add_fracture_set",
114+
&FractureNetworkDescription::add_fracture_set,
115+
pybind11::return_value_policy::reference_internal,
116+
"Create and return a new fracture set." )
117+
118+
.def( "add_x_node_monitoring",
119+
&FractureNetworkDescription::add_x_node_monitoring,
120+
pybind11::arg( "beta" ),
121+
"Enable monitoring of X-node interactions with the given "
122+
"weight." )
123+
124+
.def_readwrite( "expected_x_node",
125+
&FractureNetworkDescription::expected_x_node,
126+
"Expected number of X-nodes, if monitored." )
127+
128+
.def( "string", &FractureNetworkDescription::string,
129+
"Return a detailed textual description of the network." )
130+
131+
.def( "__repr__", []( const FractureNetworkDescription& self ) {
132+
return "<FractureNetworkDescription name='" + self.fnet_name
133+
+ "'>";
134+
} );
135+
136+
// module.def( "build_fractures_simulation_context",
137+
// &build_fractures_simulation_context, pybind11::arg(
138+
// "description" ), "Build a simulation context from a
139+
// fracture network description." );
140+
//
141+
// module.def( "build_fractures_targeted_stat",
142+
// &build_fractures_targeted_stat, pybind11::arg(
143+
// "description" ), "Create the target statistics associated
144+
// with a fracture network " "description." );
145+
146+
module.def( "build_fractures_simulation_runner",
147+
&build_fractures_simulation_runner, pybind11::arg( "description" ),
148+
"Create a ready-to-use simulation runner from a fracture network "
149+
"description." );
150+
}
151+
152+
} // namespace geode

bindings/python/src/stochastic/sampling/mcmc/helpers/simulation_monitor.hpp renamed to bindings/python/src/stochastic/inference/statistics_tracker.hpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,42 @@
2020
* SOFTWARE.
2121
*
2222
*/
23+
#pragma once
24+
25+
#include "../../common.hpp"
2326

2427
#include <geode/stochastic/inference/statistics_tracker.hpp>
2528

29+
#include <geode/geometry/basic_objects/segment.hpp>
30+
2631
namespace geode
2732
{
28-
void define_simulation_monitor( pybind11::module &module )
33+
34+
template < typename ObjectType >
35+
void define_statistics_tracker_impl(
36+
pybind11::module_& module, const std::string& typestr )
2937
{
30-
pybind11::class_< geode::StatisticsTracker >(
31-
module, "StatisticsTracker" )
32-
.def( pybind11::init< geode::index_t >(),
33-
pybind11::arg( "nb_energy_terms" ),
34-
"Create a StatisticsTracker for a given number of energy "
35-
"terms" )
36-
.def( "add_realization", &geode::StatisticsTracker::add_realization,
37-
pybind11::arg( "values" ),
38-
"Add a realization (vector of doubles) to update statistics" )
39-
.def( "statiscal_count", &geode::StatisticsTracker::statiscal_count,
40-
"Return the number of realizations added" )
41-
.def_property_readonly( "means", &geode::StatisticsTracker::means,
42-
"Return the computed mean values for each energy term" )
43-
.def_property_readonly( "variances",
44-
&geode::StatisticsTracker::variances,
45-
"Return the computed variances for each energy term" )
46-
.def( "__repr__", []( const geode::StatisticsTracker &self ) {
47-
return "<StatisticsTracker count="
48-
+ std::to_string( self.statiscal_count() ) + ">";
38+
using Tracker = geode::StatisticsTracker< ObjectType >;
39+
const auto pyclass_name = absl::StrCat( typestr, "StatisticsTracker" );
40+
41+
pybind11::class_< Tracker >( module, pyclass_name.c_str() )
42+
.def( "statiscal_count", &Tracker::statiscal_count )
43+
44+
.def(
45+
"means", &Tracker::means, pybind11::return_value_policy::copy )
46+
47+
.def( "variances", &Tracker::variances )
48+
49+
.def( "__repr__", []( const Tracker& tracker ) {
50+
return absl::StrCat( "StatisticsTracker(count=",
51+
tracker.statiscal_count(), ")" );
4952
} );
5053
}
54+
55+
void define_statistics_tracker( pybind11::module_& module )
56+
{
57+
define_statistics_tracker_impl< OwnerSegment2D >(
58+
module, "OwnerSegment2D" );
59+
}
60+
5161
} // namespace geode

bindings/python/src/stochastic/sampling/direct/double_sampler.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* SOFTWARE.
2121
*
2222
*/
23+
#pragma once
24+
25+
#include "../../../common.hpp"
2326

2427
#include <geode/stochastic/sampling/direct/double_sampler.hpp>
2528

@@ -54,9 +57,9 @@ namespace geode
5457
.def( "string", &DoubleSampler::DistributionDescription::string,
5558
"Return a detailed description of the Distribution law" )
5659
.def( "__repr__",
57-
[]( const DoubleSampler::DistributionDescription &d ) {
58-
return "<DoubleDistributionDescription name=" + d.name
59-
+ ">";
60+
[]( const DoubleSampler::DistributionDescription &dist_desc ) {
61+
return "<DoubleDistributionDescription name="
62+
+ dist_desc.name + ">";
6063
} );
6164

6265
// Bind Distribution variant

bindings/python/src/stochastic/sampling/distributions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* SOFTWARE.
2121
*
2222
*/
23+
#pragma once
24+
#include "../../common.hpp"
2325

2426
#include <geode/stochastic/sampling/distributions.hpp>
2527

0 commit comments

Comments
 (0)