Skip to content

Commit 18b6007

Browse files
authored
Merge pull request #28 from Geode-solutions/determinist_objects
feat(DeterministicObject): add the possibility to constrained simulation with determininstic object.
2 parents 4adda92 + ff07c2a commit 18b6007

21 files changed

Lines changed: 345 additions & 146 deletions

File tree

bindings/python/src/stochastic/sampling/mcmc/helpers/fracture_simulation_runner.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ namespace geode
3636
.def_readwrite( "length", &FractureSetDescription::length )
3737
.def_readwrite( "azimuth", &FractureSetDescription::azimuth )
3838
.def_readwrite( "p20", &FractureSetDescription::p20 )
39+
.def( "add_observed_fracture",
40+
[]( FractureSetDescription& self, const geode::Point2D& a,
41+
const geode::Point2D& b ) {
42+
self.observed_fractures.push_back( { a, b } );
43+
} )
3944
.def_readwrite(
4045
"minimal_spacing", &FractureSetDescription::minimal_spacing )
4146
.def_readwrite(

bindings/python/tests/stochastic/test-py-mh-fractures.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_fracture_simulator():
4646
# --- Object set
4747
setA = stochastic.FractureSetDescription()
4848
setA.name = "A"
49+
setA.add_observed_fracture(og.Point2D([10.0, 10.0]), og.Point2D([20.0, 20.0]))
4950

5051
# length
5152
setA.length.distribution_type =stochastic.DistributionType("UniformClosed")

include/geode/stochastic/sampling/mcmc/helpers/fracture_simulation_runner.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <geode/stochastic/spatial/pairwise_interactions.hpp>
3535
#include <geode/stochastic/spatial/spatial_domain.hpp>
3636

37+
#include <geode/geometry/basic_objects/segment.hpp>
38+
3739
namespace geode
3840
{
3941
struct FractureSetDescription
@@ -45,6 +47,7 @@ namespace geode
4547

4648
// positionning
4749
double p20;
50+
std::vector< std::array< geode::Point2D, 2 > > observed_fractures;
4851
// double p21;
4952
double minimal_spacing{ 0. };
5053

@@ -96,6 +99,13 @@ namespace geode
9699
for( const auto& set_desc : set_descriptors_ )
97100
{
98101
const auto set_id = this->object_sets_.add_set( set_desc.name );
102+
for( const auto& fixed_object : set_desc.observed_fractures )
103+
{
104+
this->object_sets_.add_object(
105+
geode::OwnerSegment2D{
106+
fixed_object[0], fixed_object[1] },
107+
set_id, true );
108+
}
99109
name_to_uuid[set_desc.name] = set_id;
100110

101111
auto length_distribution =

include/geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ namespace geode
194194
[]( auto& state, auto& proposal ) {
195195
state.add_object(
196196
std::move( proposal.proposed_move.new_object.value() ),
197-
proposal.set_id );
197+
proposal.set_id, false );
198198
} );
199199
};
200200

@@ -207,7 +207,7 @@ namespace geode
207207
gibbs_energy_.delta_log_remove( state, old_object_id );
208208
return accept_or_reject( proposal, state, engine, delta_log_energy,
209209
[]( auto& state, auto& proposal ) {
210-
state.remove_object( proposal.old_object_id() );
210+
state.remove_free_object( proposal.old_object_id() );
211211
} );
212212
};
213213

@@ -221,7 +221,7 @@ namespace geode
221221
state, old_object_id, new_object );
222222
return accept_or_reject( proposal, state, engine, delta_log_energy,
223223
[]( auto& state, auto& proposal ) {
224-
state.update_object( proposal.old_object_id(),
224+
state.update_free_object( proposal.old_object_id(),
225225
std::move(
226226
proposal.proposed_move.new_object.value() ) );
227227
} );

include/geode/stochastic/sampling/mcmc/models/components/energy_term.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ namespace geode
175175
for( const auto& targeted_set_id : targeted_set_ids_ )
176176
{
177177
for( const auto id :
178-
geode::Range{ state.nb_objects_in_set( targeted_set_id ) } )
178+
state.get_objects_in_set( targeted_set_id ) )
179179
{
180-
do_apply( ObjectId{ id, targeted_set_id } );
180+
do_apply( id );
181181
}
182182
}
183183
}

include/geode/stochastic/sampling/mcmc/proposal/moves.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ namespace geode
138138
virtual std::string string() const = 0;
139139

140140
protected:
141-
std::optional< geode::index_t > draw_a_sample_id(
141+
std::optional< geode::index_t > draw_a_free_sample_id(
142142
const ObjectSet< ObjectType >& set, RandomEngine& engine ) const
143143
{
144-
if( set.empty() )
144+
const auto max_obj_id = set.nb_free_objects();
145+
if( max_obj_id == 0 )
145146
{
146147
return std::nullopt;
147148
}
148-
const auto max_obj_id = set.size();
149149
geode::UniformClosed< index_t > uniform_closed_index_t;
150150
uniform_closed_index_t.min_value = 0;
151151
uniform_closed_index_t.max_value = max_obj_id - 1;
@@ -212,26 +212,27 @@ namespace geode
212212
birth.proposal_probabilities.log_forward_prob =
213213
log_p_birth_ + this->sampler_.log_pdf( new_obj );
214214
birth.proposal_probabilities.log_backward_prob =
215-
log_p_death_ - std::log( set.size() + 1.0 );
215+
log_p_death_ - std::log( set.nb_objects() + 1.0 );
216216
return birth;
217217
}
218218

219219
MoveResult< ObjectType > propose_death_move(
220220
const ObjectSet< ObjectType >& set, RandomEngine& engine ) const
221221
{
222222
MoveResult< ObjectType > death;
223-
death.old_object_id = this->draw_a_sample_id( set, engine );
223+
death.old_object_id = this->draw_a_free_sample_id( set, engine );
224224
if( !death.old_object_id.has_value() )
225225
{
226226
return death;
227227
}
228228
const auto cur_object_id = death.old_object_id.value();
229229
death.type = MoveType::Death;
230230
death.proposal_probabilities.log_forward_prob =
231-
log_p_death_ - std::log( set.size() );
231+
log_p_death_ - std::log( set.nb_free_objects() );
232232
death.proposal_probabilities.log_backward_prob =
233233
log_p_birth_
234-
+ this->sampler_.log_pdf( set.get_object( cur_object_id ) );
234+
+ this->sampler_.log_pdf(
235+
set.get_free_object( cur_object_id ) );
235236
return death;
236237
}
237238

@@ -256,14 +257,14 @@ namespace geode
256257
RandomEngine& engine ) const override
257258
{
258259
MoveResult< ObjectType > change;
259-
change.old_object_id = this->draw_a_sample_id( set, engine );
260+
change.old_object_id = this->draw_a_free_sample_id( set, engine );
260261
if( !change.old_object_id.has_value() )
261262
{
262263
return change;
263264
}
264265
change.type = MoveType::Change;
265266
const auto& object_to_change =
266-
set.get_object( change.old_object_id.value() );
267+
set.get_free_object( change.old_object_id.value() );
267268
change.new_object =
268269
this->sampler_.change( object_to_change, engine );
269270
change.proposal_probabilities.log_forward_prob =

include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ namespace geode
4646
{
4747
OPENGEODE_EXCEPTION( proposed_move.old_object_id.has_value(),
4848
"[Proposal] Proposal has no old_object_id" );
49-
return ObjectId{ proposed_move.old_object_id.value(), set_id };
49+
return ObjectId{ proposed_move.old_object_id.value(), false,
50+
set_id };
5051
};
5152

5253
std::string string() const

include/geode/stochastic/spatial/object_neighborhood.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ namespace geode
3535
struct ObjectId
3636
{
3737
index_t index;
38+
bool fixed;
3839
uuid set_id;
3940
bool operator==( const ObjectId& other ) const noexcept
4041
{
41-
return index == other.index && set_id == other.set_id;
42+
return index == other.index && fixed == other.fixed
43+
&& set_id == other.set_id;
4244
}
4345
bool operator!=( const ObjectId& other ) const noexcept
4446
{
45-
return index != other.index || set_id != other.set_id;
47+
return index != other.index || fixed != other.fixed
48+
|| set_id != other.set_id;
4649
}
4750
};
4851

include/geode/stochastic/spatial/object_set.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@ namespace geode
3737
ObjectSet& operator=( ObjectSet&& ) noexcept = default;
3838

3939
void set_name( std::string_view name );
40-
const Type& get_object( index_t index ) const;
40+
const Type& get_fixed_object( index_t index ) const;
41+
const Type& get_free_object( index_t index ) const;
4142

42-
index_t add_object( Type&& object );
43-
void update_object( index_t index, Type&& object );
44-
void remove_object( index_t index );
43+
index_t add_fixed_object( Type&& object );
44+
45+
index_t add_free_object( Type&& object );
46+
void update_free_object( index_t index, Type&& object );
47+
void remove_free_object( index_t index );
48+
49+
index_t nb_objects() const;
50+
index_t nb_fixed_objects() const;
51+
index_t nb_free_objects() const;
4552

46-
index_t size() const;
4753
bool empty() const;
4854

4955
std::string string() const;
5056

5157
private:
52-
std::vector< Type > objects_;
58+
std::vector< Type > fixed_objects_;
59+
std::vector< Type > free_objects_;
5360
};
5461
} // namespace geode

include/geode/stochastic/spatial/object_sets.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ namespace geode
5959
const ObjectSet< Type >& get_set( const uuid& set_id ) const;
6060
const Type& get_object( const ObjectId& object_id ) const;
6161
std::vector< ObjectId > get_all_object() const;
62+
std::vector< ObjectId > get_objects_in_set( const uuid& set_id ) const;
6263

6364
index_t nb_sets() const;
6465
index_t nb_objects_in_set( const uuid& set_id ) const;
6566
index_t nb_objects() const;
6667

6768
uuid add_set( std::string_view name );
68-
ObjectId add_object( Type&& object, const uuid& set_id );
69-
void update_object( const ObjectId& object_id, Type&& object );
70-
void remove_object( const ObjectId& object_id );
69+
ObjectId add_object( Type&& object, const uuid& set_id, bool fixed );
70+
void update_free_object( const ObjectId& object_id, Type&& object );
71+
void remove_free_object( const ObjectId& object_id );
7172

7273
// Object neighbor search by ObjectId (always excludes self)
7374
std::vector< ObjectId > neighbors( const ObjectId& object_id,
@@ -83,6 +84,8 @@ namespace geode
8384

8485
private:
8586
ObjectSet< Type >& get_set( const uuid& set_id );
87+
// void update_neighborhood_remove_context( const ObjectId& object_id
88+
// );
8689

8790
private:
8891
absl::flat_hash_map< uuid, ObjectSet< Type > > sets_;

0 commit comments

Comments
 (0)