From 4457c3c8d17fb46637043ccb428cf38868b07f7e Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 11:36:10 +0100 Subject: [PATCH 01/10] fix(EnergyTerm): make a term hard-core only for a zero parameter A parameter gamma below GLOBAL_EPSILON was a hard-core: a density of one fracture per km2 given in m-2 (1e-6) meant no fracture at all. Only gamma = 0 is now a hard-core; any finite theta is accepted by set_thetas, however negative: the energy is computed in log, so a tiny gamma is stable. A weak but positive repulsion no longer makes an attraction integrable. The observation term stays a hard-core when all the objects are observed up to the rounding of lambda * A. Co-Authored-By: Claude Opus 5.5 --- .../models/energy_terms/energy_term.hpp | 9 ++- include/geode/stochastic/models/model.hpp | 12 +-- .../applications/observed_intensity.cpp | 12 ++- src/geode/stochastic/models/model.cpp | 5 +- .../applications/test-observed-intensity.cpp | 29 +++++++ .../energy_terms/test-pairwise-term.cpp | 16 ++-- .../models/test-model-integrability.cpp | 15 ++++ .../models/test-model-parameters.cpp | 80 ++++++++++++++++++- 8 files changed, 150 insertions(+), 28 deletions(-) diff --git a/include/geode/stochastic/models/energy_terms/energy_term.hpp b/include/geode/stochastic/models/energy_terms/energy_term.hpp index 6ec27e8..5b6730a 100644 --- a/include/geode/stochastic/models/energy_terms/energy_term.hpp +++ b/include/geode/stochastic/models/energy_terms/energy_term.hpp @@ -50,9 +50,10 @@ namespace geode public: /// gamma is the parameter of the term, in the user space: the energy - /// is -log( gamma ) times the energy statistic. gamma below - /// GLOBAL_EPSILON makes the term a hard-core constraint: any - /// configuration with a positive energy statistic is forbidden. + /// is -log( gamma ) times the energy statistic. gamma = 0 makes the + /// term a hard-core constraint: any configuration with a positive + /// energy statistic is forbidden. Any positive gamma, however small, + /// is an ordinary term (the parameters are in physical units). explicit EnergyTerm( std::string_view name, double gamma, std::vector< uuid >&& impacted_set_ids, @@ -63,7 +64,7 @@ namespace geode OpenGeodeStochasticStochasticException::check_exception( gamma >= 0., nullptr, OpenGeodeException::TYPE::data, "[EnergyTerm] Model parameter cannot be negative." ); - hard_core_ = gamma < GLOBAL_EPSILON; + hard_core_ = gamma == 0.; theta_ = hard_core_ ? -std::numeric_limits< double >::infinity() : std::log( gamma ); absl::c_sort( impacted_set_ids_ ); diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 1b1d6b1..1d6d9aa 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -145,9 +145,9 @@ namespace geode * Changes the parameters of all the terms, given in the order of the * terms (see thetas()). All of them are checked before any is * written: if one is refused, the model is unchanged. A hard-core term - * keeps -infinity; any other term needs a finite theta, at least - * log( GLOBAL_EPSILON ) (lower parameters make a hard-core at - * construction), and at most 0 if its attraction is not integrable + * keeps -infinity; any other term needs a finite theta (however + * negative: the parameters are in physical units), at most 0 if its + * attraction is not integrable * (see EnergyTerm::attraction_allowed). * The model is read-only during a simulation: change it between two * simulations, never while a sampler walks in another thread. @@ -345,12 +345,6 @@ namespace geode std::isfinite( theta ), nullptr, OpenGeodeException::TYPE::data, "[Model] Term '", name, "': the parameter theta must be finite, not ", theta, "." ); - OpenGeodeStochasticStochasticException::check_exception( - theta >= std::log( GLOBAL_EPSILON ), nullptr, - OpenGeodeException::TYPE::data, "[Model] Term '", name, - "': theta = ", theta, " is lower than log( GLOBAL_EPSILON ) = ", - std::log( GLOBAL_EPSILON ), - ", which would make it a hard-core at construction." ); OpenGeodeStochasticStochasticException::check_exception( theta <= 0. || energy_term.attraction_allowed(), nullptr, OpenGeodeException::TYPE::data, "[Model] Attractive term '", diff --git a/src/geode/stochastic/applications/observed_intensity.cpp b/src/geode/stochastic/applications/observed_intensity.cpp index 7619279..c651ce0 100644 --- a/src/geode/stochastic/applications/observed_intensity.cpp +++ b/src/geode/stochastic/applications/observed_intensity.cpp @@ -151,8 +151,16 @@ namespace geode set.name, "': lambda = ", lambda, " gives ", nb_expected, " objects expected to touch the domain, fewer than its ", nb_fixed, " fixed objects touching it: lambda is underestimated." ); - const auto observed_ratio = - nb_expected > 0. ? std::min( nb_fixed / nb_expected, 1. ) : 0.; + // all observed up to the rounding of lambda * A: exactly 0 left, so + // that the observation term is a hard-core (only gamma = 0 is one) + constexpr double ROUNDING = 1e-12; + double observed_ratio{ 0. }; + if( nb_expected > 0. ) + { + observed_ratio = nb_fixed < nb_expected * ( 1. - ROUNDING ) + ? nb_fixed / nb_expected + : 1.; + } Logger::info( "[IntensityScope] Object set '", set.name, "': ", nb_fixed, " fixed objects touch the domain, out of ", nb_expected, diff --git a/src/geode/stochastic/models/model.cpp b/src/geode/stochastic/models/model.cpp index e40979d..3207b1e 100644 --- a/src/geode/stochastic/models/model.cpp +++ b/src/geode/stochastic/models/model.cpp @@ -49,8 +49,9 @@ namespace bool is_hard_core( const geode::PairwiseTermConfig& term ) { - // gamma below epsilon is handled as 0 by the energy scale - if( term.gamma >= geode::GLOBAL_EPSILON ) + // only gamma = 0 is a hard-core: a weak repulsion does not bound the + // number of objects + if( term.gamma != 0. ) { return false; } diff --git a/tests/stochastic/applications/test-observed-intensity.cpp b/tests/stochastic/applications/test-observed-intensity.cpp index 503bdf9..9485b95 100644 --- a/tests/stochastic/applications/test-observed-intensity.cpp +++ b/tests/stochastic/applications/test-observed-intensity.cpp @@ -281,6 +281,34 @@ namespace geode::Logger::info( "--> SUCCESS!" ); } + // lambda * |D| = 0.07 * 100 = 7.000000000000001 with 7 fixed points: all + // the objects are observed, despite the rounding of lambda * |D|. + void test_all_objects_observed_with_rounding() + { + geode::Logger::info( "TEST - ALL THE OBJECTS OBSERVED WITH ROUNDING" ); + geode::PoissonProcessBuilder< geode::Point2D > poisson; + poisson.set_domain( square_domain( 10., 2. ) ); + // NOLINTNEXTLINE(*-magic-numbers) + auto& set_config = poisson.add_set( + "set_A", 0.07, std::nullopt, geode::IntensityScope::all_objects ); + // NOLINTNEXTLINE(*-magic-numbers) + for( const auto i : geode::Range{ 7 } ) + { + set_config.fixed_objects.emplace_back( + geode::Point2D{ { 0.5 + i, 5. } } ); + } + const auto context = poisson.build_simulation_context(); + const auto& terms = context.model->terms(); + const auto& observation = + terms.get( terms.get_term_uuid( "set_A_observation" ) ); + geode::OpenGeodeStochasticStochasticException::test( + observation.is_hard_core(), + "[ObservedIntensity] all observed up to rounding: the observation " + "term should be a hard-core, theta = ", + observation.theta() ); + geode::Logger::info( "--> SUCCESS!" ); + } + // lambda * |D| = 5 < 10 fixed points void test_underestimated_density() { @@ -515,6 +543,7 @@ int main() test_touching_domain_area(); test_underestimated_density(); test_all_objects_observed(); + test_all_objects_observed_with_rounding(); test_points_all_objects(); test_fractures_all_objects(); test_three_observation_cases(); diff --git a/tests/stochastic/models/energy_terms/test-pairwise-term.cpp b/tests/stochastic/models/energy_terms/test-pairwise-term.cpp index 9988527..3978e8a 100644 --- a/tests/stochastic/models/energy_terms/test-pairwise-term.cpp +++ b/tests/stochastic/models/energy_terms/test-pairwise-term.cpp @@ -139,7 +139,7 @@ void test_pairwise_term_zero_gamma( // --- energy double total = term->energy( pattern ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( total ), - "[PairwiseTerm] energy with gammadelta_energy_add( pattern, p4_ref ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( delta ), "[PairwiseTerm] delta_energy_add with " - "gammadelta_energy_add( pattern, buffer_ref ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( delta ), "[PairwiseTerm] delta_energy_add buffer with " - "gammadelta_energy_change( pattern, obj_id, p4_ref ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( delta ), "[PairwiseTerm] delta_energy_change VOI->VOI with " - "gammadelta_energy_change( pattern, old_buffer, p4_ref ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( delta ), "[PairwiseTerm] delta_energy_change buffer->VOI with " - "gammadelta_energy_change( pattern, obj_id, buffer_ref ); geode::OpenGeodeStochasticStochasticException::test( std::isinf( delta ), "[PairwiseTerm] delta_energy_change VOI->buffer with " - "gammadelta_energy_remove( pattern, obj_id ); geode::OpenGeodeStochasticStochasticException::test( delta == 0, "[PairwiseTerm] delta_energy_remove VOI with " - "gammaobserved_statistic( pattern ); @@ -315,7 +315,7 @@ int main() pw_interaction_cfg.gamma = 2.0; test_pairwise_term( pw_interaction_cfg, pattern, domain ); - pw_interaction_cfg.gamma = 0.9999 * geode::GLOBAL_EPSILON; + pw_interaction_cfg.gamma = 0.; test_pairwise_term_zero_gamma( pw_interaction_cfg, pattern, domain ); test_segment_intersection_statistic(); diff --git a/tests/stochastic/models/test-model-integrability.cpp b/tests/stochastic/models/test-model-integrability.cpp index 661fcb1..5b3aae9 100644 --- a/tests/stochastic/models/test-model-integrability.cpp +++ b/tests/stochastic/models/test-model-integrability.cpp @@ -99,6 +99,20 @@ namespace "integrable" ); } + void test_attractive_model_with_tiny_repulsion() + { + // NOLINTBEGIN(*-magic-numbers) + geode::ModelConfig config; + config.terms.emplace_back( + pairwise_term( "attraction", { { "A", "A" } }, 2., 1. ) ); + // a weak but positive repulsion does not bound the number of objects + config.terms.emplace_back( + pairwise_term( "spacing", { { "A", "A" } }, 1e-7, 0.1 ) ); + // NOLINTEND(*-magic-numbers) + check( config, false, + "attractive model with a tiny repulsion should not be integrable" ); + } + void test_attractive_model_with_hard_core() { // NOLINTBEGIN(*-magic-numbers) @@ -153,6 +167,7 @@ int main() test_repulsive_model(); test_attractive_model_without_hard_core(); test_attractive_model_with_zero_hard_core(); + test_attractive_model_with_tiny_repulsion(); test_attractive_model_with_hard_core(); test_attractive_inter_set_model(); diff --git a/tests/stochastic/models/test-model-parameters.cpp b/tests/stochastic/models/test-model-parameters.cpp index 9aeb69a..cf973d3 100644 --- a/tests/stochastic/models/test-model-parameters.cpp +++ b/tests/stochastic/models/test-model-parameters.cpp @@ -281,7 +281,6 @@ namespace message ); }; const auto nan = std::numeric_limits< double >::quiet_NaN(); - const auto too_small = std::log( geode::GLOBAL_EPSILON ) - 1.; check_refused( throws( [&model, &initial] { model.set_thetas( absl::MakeConstSpan( initial.data(), initial.size() - 1 ) ); @@ -295,7 +294,7 @@ namespace model.set_theta( 1, 0. ); } ), "set_theta on a hard-core" ); - for( const auto theta : { nan, INF, -INF, too_small } ) + for( const auto theta : { nan, INF, -INF } ) { check_refused( throws( [&model, &with, theta] { model.set_thetas( with( 0, theta ) ); @@ -341,8 +340,11 @@ namespace // NOLINTBEGIN(*-magic-numbers) model.set_theta( 2, 0.4 ); model.set_theta( 0, 700. ); + // far below log( 1e-6 ): a tiny density, not a hard-core + model.set_theta( 4, -700. ); geode::OpenGeodeStochasticStochasticException::test( - model.thetas()[2] == 0.4 && model.thetas()[0] == 700., + model.thetas()[2] == 0.4 && model.thetas()[0] == 700. + && model.thetas()[4] == -700., "[ModelParameters] valid parameters not set" ); const auto energy = model.energy( *context.object_sets ); geode::OpenGeodeStochasticStochasticException::test( @@ -551,6 +553,76 @@ namespace // NOLINTEND(*-magic-numbers) geode::Logger::info( "--> SUCCESS!" ); } + + /// A tiny but positive gamma (e.g. a density of 1 per km2 in m-2) is an + /// ordinary term, not a hard-core. + void test_tiny_gamma_is_not_hard_core() + { + geode::Logger::info( "TEST - TINY GAMMA IS NOT A HARD-CORE" ); + auto gammas = point_gammas(); + // NOLINTNEXTLINE(*-magic-numbers) + gammas[4] = 1e-7; + auto context = + geode::build_simulation_context( point_config( gammas ) ); + const auto& term = *context.model->terms().energy_terms()[4]; + geode::OpenGeodeStochasticStochasticException::test( + !term.is_hard_core() && term.theta() == std::log( gammas[4] ), + "[ModelParameters] gamma = 1e-7 should be an ordinary term" ); + geode::Logger::info( "--> SUCCESS!" ); + } + + /// Tiny gammas on a pairwise and a single object term: the energy deltas + /// match the energy variations and no energy is NaN along a walk. + void test_stability_with_tiny_gammas() + { + geode::Logger::info( "TEST - STABILITY WITH TINY GAMMAS" ); + // NOLINTNEXTLINE(*-magic-numbers) + for( const auto tiny : { 1e-12, 1e-300 } ) + { + auto gammas = point_gammas(); + gammas[3] = tiny; + gammas[4] = tiny; + auto context = + geode::build_simulation_context( point_config( gammas ) ); + const auto& model = *context.model; + auto& state = *context.object_sets; + geode::RandomEngine engine; + engine.set_seed( "@model-parameters-tiny@" ); + // NOLINTNEXTLINE(*-magic-numbers) + for( const auto operation : geode::Range{ 200 } ) + { + geode_unused( operation ); + const auto set_index = random_index( engine, 2 ); + const auto set_id = + state.get_set_uuid( set_index == 0 ? "A" : "B" ); + const auto before = model.energy( state ); + auto object = context.set_samplers[set_index]->sample( engine ); + const auto delta = model.delta_energy_add( state, + geode::ObjectRef< geode::Point2D >{ object, set_id } ); + state.add_object( std::move( object ), set_id, false ); + const auto after = model.energy( state ); + geode::OpenGeodeStochasticStochasticException::test( + !std::isnan( after ) && !std::isnan( delta ), + "[ModelParameters] NaN energy with gamma = ", tiny ); + if( std::isfinite( before ) && std::isfinite( after ) ) + { + geode::OpenGeodeStochasticStochasticException::test( + std::fabs( after - before - delta ) + < 1e-9 * ( 1. + std::fabs( after ) ), + "[ModelParameters] delta inconsistent with gamma = ", + tiny ); + } + } + // NOLINTNEXTLINE(*-magic-numbers) + context.mh_sampler->walk( state, engine, 2000 ); + geode::OpenGeodeStochasticStochasticException::test( + !std::isnan( model.energy( state ) ), + "[ModelParameters] NaN energy after a walk with gamma = ", + tiny ); + } + geode::Logger::info( "--> SUCCESS!" ); + } + } // namespace int main() @@ -563,6 +635,8 @@ int main() test_mh_sees_new_parameters(); test_rejections(); test_accepted(); + test_tiny_gamma_is_not_hard_core(); + test_stability_with_tiny_gammas(); test_fixed_objects_violating_hard_core(); test_fracture_parameters_match_rebuilt_model(); return 0; From 3f59c6e31908b51c12045903648104f434efd26b Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 11:38:53 +0100 Subject: [PATCH 02/10] fix(Fractures): refuse a length term on an unbounded length law The energy weight of a fracture in a length term is its whole length. With an unbounded length law (no max_value, or Gaussian), a parameter above 1, the usual one to favor long fractures, gives a non integrable model. The simulation context now refuses such a length term; the bounded length law rule is shared with the buffer size check. Co-Authored-By: Claude Opus 5.5 --- .../stochastic/test-py-simulation-errors.py | 8 +++ .../segment_set_sampler.hpp | 5 ++ .../mcmc/helpers/simulation_context.hpp | 54 +++++++++++++++ .../segment_set_sampler.cpp | 14 ++-- .../applications/test-fractures.cpp | 65 +++++++++++++++++++ 5 files changed, 141 insertions(+), 5 deletions(-) diff --git a/bindings/python/tests/stochastic/test-py-simulation-errors.py b/bindings/python/tests/stochastic/test-py-simulation-errors.py index b9a9e62..f0255b3 100644 --- a/bindings/python/tests/stochastic/test-py-simulation-errors.py +++ b/bindings/python/tests/stochastic/test-py-simulation-errors.py @@ -155,6 +155,14 @@ def test_builder_errors(): builder, fset = small_builder() fset.dynamics = stochastic.ObjectSetDynamicsConfig(birth_ratio=0.0, death_ratio=0.0, change_ratio=0.0) raises(og.OpenGeodeDataError, lambda: builder.build_simulation_runner(), "No move at all") + # a length term needs a bounded length law: the energy weight of a + # fracture is its whole length + builder, fset = small_builder() + fset.sampler.length = stochastic.DoubleDistributionDescription( + "TruncatedLogNormal", mean=0.0, standard_deviation=0.5, min_value=0.5 + ) + builder.add_intensity("fset", 0.5) + raises(og.OpenGeodeDataError, lambda: builder.build_simulation_runner(), "Length term on an unbounded law") print("test_builder_errors: OK") diff --git a/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp b/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp index 9ec7cdc..c7b9ceb 100644 --- a/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp +++ b/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp @@ -90,4 +90,9 @@ namespace geode double rotation_probability_; }; + /// True if the length law has a maximal value (a Gaussian has none): + /// segments never exceed it. + [[nodiscard]] opengeode_stochastic_stochastic_api bool + is_bounded_length_law( + const DoubleSampler::DistributionDescription& length ); } // namespace geode \ No newline at end of file diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index 05a87fc..cde8f63 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -25,6 +25,8 @@ #include +#include + #include #include @@ -100,11 +102,63 @@ namespace geode geode::ModelConfig model; }; + namespace detail + { + /// The energy weight of a segment in a length term is its whole + /// length: with an unbounded length law, a parameter above 1 (the usual + /// one, favoring long fractures) gives a non integrable model. + template < typename ObjectType > + void check_length_terms( + const SimulationContextConfig< ObjectType >& config ) + { + if constexpr( std::is_same_v< ObjectType, OwnerSegment2D > ) + { + for( const auto& term_config : config.model.terms ) + { + const auto* term = + std::get_if< SingleObjectTermConfig >( &term_config ); + if( term == nullptr + || !std::holds_alternative< + SegmentLengthInsideBoxFeatureConfig >( + term->object_feature ) ) + { + continue; + } + for( const auto& set : config.sets ) + { + if( absl::c_linear_search( + term->object_set_names, set.name ) ) + { + OpenGeodeStochasticStochasticException:: + check_exception( + is_bounded_length_law( set.sampler.length ), + nullptr, OpenGeodeException::TYPE::data, + "[SimulationContext] Length term '", + term->term_name, "' on object set '", + set.name, + "': its length law is not bounded (no " + "max_value, or Gaussian). The energy " + "weight of a segment is its whole length, " + "so the model is not integrable for a " + "parameter above 1. Give the length law a " + "max_value." ); + } + } + } + } + else + { + geode_unused( config ); + } + } + } // namespace detail + template < typename ObjectType > [[nodiscard]] geode::SimulationContext< ObjectType > build_simulation_context( const SimulationContextConfig< ObjectType >& config ) { + detail::check_length_terms( config ); geode::SimulationContext< ObjectType > context; // ------------------------- diff --git a/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp b/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp index 84275e8..dfd4211 100644 --- a/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp +++ b/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp @@ -40,11 +40,7 @@ namespace void check_buffer_size( const geode::SpatialDomain< 2 >& domain, const geode::DoubleSampler::DistributionDescription& length ) { - const auto bounded = - length.max_value.has_value() - && length.distribution_type - != geode::Gaussian::distribution_type_static(); - if( !bounded ) + if( !geode::is_bounded_length_law( length ) ) { geode::Logger::warning( "[UniformSegmentSetSampler] Unbounded " "length distribution: segments longer than " @@ -73,6 +69,14 @@ namespace namespace geode { + bool is_bounded_length_law( + const DoubleSampler::DistributionDescription& length ) + { + return length.max_value.has_value() + && length.distribution_type + != Gaussian::distribution_type_static(); + } + UniformSegmentSetSampler::UniformSegmentSetSampler( const SpatialDomain< 2 >& domain, const ObjectSamplerConfig< OwnerSegment2D >& config ) diff --git a/tests/stochastic/applications/test-fractures.cpp b/tests/stochastic/applications/test-fractures.cpp index 86bbd31..b156187 100644 --- a/tests/stochastic/applications/test-fractures.cpp +++ b/tests/stochastic/applications/test-fractures.cpp @@ -324,6 +324,70 @@ namespace geode::Logger::info( "--> SUCCESS!" ); } + geode::FractureProcessBuilder length_term_builder( + const geode::DoubleSampler::DistributionDescription& length ) + { + // NOLINTBEGIN(*-magic-numbers) + geode::FractureProcessBuilder fractures; + geode::SpatialDomainConfig< 2 > domain; + domain.min_point = geode::Point2D{ { 0., 0. } }; + domain.max_point = geode::Point2D{ { 10., 10. } }; + domain.buffer_size = 5.; + fractures.set_domain( domain ); + auto& fset = fractures.add_fracture_set( "fset", 0.1 ); + fset.sampler.length = length; + fset.sampler.azimuth.distribution_type = + geode::UniformClosed< double >::distribution_type_static(); + fset.sampler.azimuth.min_value = 0.; + fset.sampler.azimuth.max_value = 180.; + fractures.add_intensity( "fset", 0.5 ); + // NOLINTEND(*-magic-numbers) + return fractures; + } + + /// The energy weight of a fracture in a length term is its whole length: + /// with an unbounded length law, a length term parameter above 1 (the + /// usual one, favoring long fractures) gives a non integrable model. + void test_length_term_needs_bounded_length_law() + { + geode::Logger::info( "TEST - LENGTH TERM NEEDS A BOUNDED LENGTH LAW" ); + // NOLINTBEGIN(*-magic-numbers) + geode::DoubleSampler::DistributionDescription log_normal; + log_normal.distribution_type = + geode::TruncatedLogNormal::distribution_type_static(); + log_normal.mean = 0.; + log_normal.standard_deviation = 0.5; + log_normal.min_value = 0.5; + bool rejected{ false }; + try + { + const auto context = + length_term_builder( log_normal ).build_simulation_context(); + } + catch( const geode::OpenGeodeException& ) + { + rejected = true; + } + geode::OpenGeodeStochasticStochasticException::test( rejected, + "[Fractures] length term on an unbounded length law accepted" ); + + log_normal.max_value = 4.; + auto bounded = + length_term_builder( log_normal ).build_simulation_context(); + // favoring long fractures: gamma > 1 + bounded.model->set_theta( 1, 1. ); + + geode::DoubleSampler::DistributionDescription power_law; + power_law.distribution_type = + geode::TruncatedPowerLaw::distribution_type_static(); + power_law.alpha = 2.; + power_law.min_value = 0.5; + power_law.max_value = 4.; + const auto power_law_context = + length_term_builder( power_law ).build_simulation_context(); + // NOLINTEND(*-magic-numbers) + geode::Logger::info( "--> SUCCESS!" ); + } } // namespace int main() @@ -338,6 +402,7 @@ int main() test_observed_fractures(); test_two_fracture_sets(); test_attractive_x_node_is_rejected(); + test_length_term_needs_bounded_length_law(); return 0; } catch( ... ) From 36cd7733cd01699b8bd5458e21933738f83a2f19 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 11:41:21 +0100 Subject: [PATCH 03/10] fix(Model): stop the energy sum at the first infinite term A violated hard-core forbids the configuration whatever the other terms. The energy and its deltas now stop at the first +inf term: with fixed objects violating a hard-core and a term overflowing to -inf, the energy was NaN. The finite sums are unchanged. Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/models/model.hpp | 10 +++++++++- tests/stochastic/models/test-model-parameters.cpp | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 1d6d9aa..2a62cbd 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -309,7 +309,15 @@ namespace geode double sum = 0.0; for( const auto& term : terms_collection_.energy_terms() ) { - sum += term_value( *term ); + const auto value = term_value( *term ); + // a violated hard-core forbids the configuration, whatever + // the other terms: stopping here avoids +inf - inf = NaN when + // another term overflows + if( value == std::numeric_limits< double >::infinity() ) + { + return value; + } + sum += value; } return sum; } diff --git a/tests/stochastic/models/test-model-parameters.cpp b/tests/stochastic/models/test-model-parameters.cpp index cf973d3..98ab824 100644 --- a/tests/stochastic/models/test-model-parameters.cpp +++ b/tests/stochastic/models/test-model-parameters.cpp @@ -431,6 +431,16 @@ namespace geode::OpenGeodeStochasticStochasticException::test( model.thetas() == thetas, "[ModelParameters] fracture parameters not set" ); + // a huge density parameter overflows -theta * statistic to -inf: + // with the violated hard-core (+inf), the energy must stay +inf + // NOLINTNEXTLINE(*-magic-numbers) + model.set_theta( 0, 1e308 ); + const auto overflow_energy = model.energy( state ); + geode::OpenGeodeStochasticStochasticException::test( + overflow_energy == INF, + "[ModelParameters] energy with a violated hard-core and an " + "overflowing term should be +inf, not ", + overflow_energy ); geode::Logger::info( "--> SUCCESS!" ); } From 3d89536c00d361ca0f7f37704f95d20430309d40 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 11:48:25 +0100 Subject: [PATCH 04/10] fix(Fractures): document the bounded length law of a length term add_intensity documents, in C++ and Python, that the length law of the set must be bounded, and the refusal speaks of gamma > 1 (theta > 0). The set_thetas documentation no longer says that the unbounded length law is not refused. Tests: a Gaussian length law is refused even with a max_value, a set without length term keeps an unbounded law, and a birth crossing a fixed fracture stays +inf with an overflowing term. Co-Authored-By: Claude Opus 5.5 --- .../src/stochastic/applications/fractures.hpp | 6 +++- .../stochastic/applications/fractures.hpp | 4 +++ include/geode/stochastic/models/model.hpp | 14 ++++---- .../mcmc/helpers/simulation_context.hpp | 7 ++-- .../applications/test-fractures.cpp | 33 ++++++++++++++++++- .../models/test-model-parameters.cpp | 10 ++++++ 6 files changed, 63 insertions(+), 11 deletions(-) diff --git a/bindings/python/src/stochastic/applications/fractures.hpp b/bindings/python/src/stochastic/applications/fractures.hpp index 7bae11e..e827996 100644 --- a/bindings/python/src/stochastic/applications/fractures.hpp +++ b/bindings/python/src/stochastic/applications/fractures.hpp @@ -106,7 +106,11 @@ namespace geode "expected_total_length, if given, is the expected mean of " "the statistic of the term (the total length of the " "fractures inside the domain). The set is looked up when the " - "simulation is built." ) + "simulation is built; its length law must then be bounded (a " + "max_value, not Gaussian), otherwise OpenGeodeDataError is " + "raised: the energy weight of a fracture is its whole length, " + "and p21 > 1, which favors long fractures, is not integrable " + "with an unbounded law." ) .def( "add_minimal_spacing", &FractureProcessBuilder::add_minimal_spacing, pybind11::arg( "set_name" ), pybind11::arg( "minimal_spacing" ), diff --git a/include/geode/stochastic/applications/fractures.hpp b/include/geode/stochastic/applications/fractures.hpp index f7ce61c..7a24b27 100644 --- a/include/geode/stochastic/applications/fractures.hpp +++ b/include/geode/stochastic/applications/fractures.hpp @@ -79,6 +79,10 @@ namespace geode IntensityScope scope = IntensityScope::free_objects ); /// Intensity term (fracture length per unit area) on a fracture set. + /// The length law of the set must be bounded (a max_value, not + /// Gaussian): the energy weight of a fracture is its whole length, and + /// p21 > 1, which favors long fractures, is otherwise not integrable. + /// Checked when the simulation is built. void add_intensity( std::string_view set_name, double p21, std::optional< double > expected_total_length = std::nullopt ); diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 2a62cbd..5c8c5f3 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -151,9 +151,10 @@ namespace geode * (see EnergyTerm::attraction_allowed). * The model is read-only during a simulation: change it between two * simulations, never while a sampler walks in another thread. - * Known limit: the energy weight of a segment length term is the whole - * length, so a positive theta with an unbounded length law (no - * max_value) gives a non integrable model. It is not refused here. + * The energy weight of a segment length term is the whole length: a + * positive theta needs a bounded length law. build_simulation_context + * refuses a length term on an unbounded law; a model built outside a + * simulation context is not checked. */ void set_thetas( absl::Span< const double > thetas ) { @@ -310,9 +311,10 @@ namespace geode for( const auto& term : terms_collection_.energy_terms() ) { const auto value = term_value( *term ); - // a violated hard-core forbids the configuration, whatever - // the other terms: stopping here avoids +inf - inf = NaN when - // another term overflows + // an infinite energy (a violated hard-core, or an ordinary + // term overflowing) forbids the configuration whatever the + // other terms: stopping here avoids +inf - inf = NaN when + // another term overflows to -inf if( value == std::numeric_limits< double >::infinity() ) { return value; diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index cde8f63..f9514b1 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -105,8 +105,8 @@ namespace geode namespace detail { /// The energy weight of a segment in a length term is its whole - /// length: with an unbounded length law, a parameter above 1 (the usual - /// one, favoring long fractures) gives a non integrable model. + /// length: with an unbounded length law, gamma > 1 (theta > 0, the + /// usual one, favoring long fractures) gives a non integrable model. template < typename ObjectType > void check_length_terms( const SimulationContextConfig< ObjectType >& config ) @@ -140,7 +140,8 @@ namespace geode "max_value, or Gaussian). The energy " "weight of a segment is its whole length, " "so the model is not integrable for a " - "parameter above 1. Give the length law a " + "parameter gamma > 1 (theta > 0). Give the " + "length law a " "max_value." ); } } diff --git a/tests/stochastic/applications/test-fractures.cpp b/tests/stochastic/applications/test-fractures.cpp index b156187..bb7a6b7 100644 --- a/tests/stochastic/applications/test-fractures.cpp +++ b/tests/stochastic/applications/test-fractures.cpp @@ -346,7 +346,7 @@ namespace } /// The energy weight of a fracture in a length term is its whole length: - /// with an unbounded length law, a length term parameter above 1 (the + /// with an unbounded length law, a length term gamma > 1 (theta > 0, the /// usual one, favoring long fractures) gives a non integrable model. void test_length_term_needs_bounded_length_law() { @@ -385,6 +385,37 @@ namespace power_law.max_value = 4.; const auto power_law_context = length_term_builder( power_law ).build_simulation_context(); + + // a Gaussian is unbounded, even with a max_value + geode::DoubleSampler::DistributionDescription gaussian; + gaussian.distribution_type = + geode::Gaussian::distribution_type_static(); + gaussian.mean = 2.; + gaussian.standard_deviation = 0.5; + gaussian.max_value = 4.; + rejected = false; + try + { + const auto context = + length_term_builder( gaussian ).build_simulation_context(); + } + catch( const geode::OpenGeodeException& ) + { + rejected = true; + } + geode::OpenGeodeStochasticStochasticException::test( rejected, + "[Fractures] length term on a Gaussian length law accepted" ); + + // a set without length term keeps its unbounded law (warning only) + log_normal.max_value.reset(); + auto fractures = length_term_builder( power_law ); + auto& other = fractures.add_fracture_set( "other", 0.1 ); + other.sampler.length = log_normal; + other.sampler.azimuth.distribution_type = + geode::UniformClosed< double >::distribution_type_static(); + other.sampler.azimuth.min_value = 0.; + other.sampler.azimuth.max_value = 180.; + const auto other_context = fractures.build_simulation_context(); // NOLINTEND(*-magic-numbers) geode::Logger::info( "--> SUCCESS!" ); } diff --git a/tests/stochastic/models/test-model-parameters.cpp b/tests/stochastic/models/test-model-parameters.cpp index 98ab824..0fd7b00 100644 --- a/tests/stochastic/models/test-model-parameters.cpp +++ b/tests/stochastic/models/test-model-parameters.cpp @@ -441,6 +441,16 @@ namespace "[ModelParameters] energy with a violated hard-core and an " "overflowing term should be +inf, not ", overflow_energy ); + // a birth crossing a fixed fracture: +inf, whatever the overflow + const geode::OwnerSegment2D crossing{ geode::Point2D{ { 4.5, 4. } }, + geode::Point2D{ { 4.5, 6. } } }; + const auto overflow_delta = model.delta_energy_add( state, + geode::ObjectRef< geode::OwnerSegment2D >{ crossing, set_id } ); + geode::OpenGeodeStochasticStochasticException::test( + overflow_delta == INF, + "[ModelParameters] birth crossing a fixed fracture with an " + "overflowing term should be +inf, not ", + overflow_delta ); geode::Logger::info( "--> SUCCESS!" ); } From 92ff00dc446cc9846d89ab130bd4c1b74747239a Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 17:40:16 +0100 Subject: [PATCH 05/10] fix(Model): check the length laws with the model integrability The integrability of the simulated model depends on the model config and on the laws of the set samplers, which define the reference measure. The length term rule becomes check_model_integrability on a SimulationContextConfig: it applies the rules of the model config and checks that each length term has a bounded length law. build_simulation_context calls it. Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/models/model.hpp | 2 + .../mcmc/helpers/simulation_context.hpp | 85 +++++++++---------- .../models/test-model-integrability.cpp | 55 ++++++++++++ 3 files changed, 99 insertions(+), 43 deletions(-) diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 5c8c5f3..ca65231 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -88,6 +88,8 @@ namespace geode /// intra-set hard-core (gamma = 0) at a strictly positive distance. /// Without it, the Strauss model with gamma > 1 is not integrable /// (Baddeley, Rubak and Turner, 2015). + /// The rules which need the laws of the set samplers (length terms) are + /// checked by check_model_integrability on a SimulationContextConfig. opengeode_stochastic_stochastic_api void check_model_integrability( const ModelConfig& config ); diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index f9514b1..a77c30d 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -102,64 +102,63 @@ namespace geode geode::ModelConfig model; }; - namespace detail + /*! + * Throws if the simulated model is not integrable. The simulated density + * is exp( -energy ) with respect to the laws of the set samplers, so its + * integrability depends on the model config and on these laws: + * - the rules of the model config (see check_model_integrability on + * ModelConfig): an attraction requires hard-cores; + * - a length term requires a bounded length law (a max_value, not + * Gaussian) on each of its sets: the energy weight of a segment is its + * whole length, so gamma > 1 (theta > 0), the usual value to favor long + * fractures, is not integrable with an unbounded law. Once checked, any + * parameter of the length term is integrable: the laws do not change. + */ + template < typename ObjectType > + void check_model_integrability( + const SimulationContextConfig< ObjectType >& config ) { - /// The energy weight of a segment in a length term is its whole - /// length: with an unbounded length law, gamma > 1 (theta > 0, the - /// usual one, favoring long fractures) gives a non integrable model. - template < typename ObjectType > - void check_length_terms( - const SimulationContextConfig< ObjectType >& config ) + check_model_integrability( config.model ); + if constexpr( std::is_same_v< ObjectType, OwnerSegment2D > ) { - if constexpr( std::is_same_v< ObjectType, OwnerSegment2D > ) + for( const auto& term_config : config.model.terms ) { - for( const auto& term_config : config.model.terms ) + const auto* term = + std::get_if< SingleObjectTermConfig >( &term_config ); + if( term == nullptr + || !std::holds_alternative< + SegmentLengthInsideBoxFeatureConfig >( + term->object_feature ) ) { - const auto* term = - std::get_if< SingleObjectTermConfig >( &term_config ); - if( term == nullptr - || !std::holds_alternative< - SegmentLengthInsideBoxFeatureConfig >( - term->object_feature ) ) - { - continue; - } - for( const auto& set : config.sets ) + continue; + } + for( const auto& set : config.sets ) + { + if( absl::c_linear_search( + term->object_set_names, set.name ) ) { - if( absl::c_linear_search( - term->object_set_names, set.name ) ) - { - OpenGeodeStochasticStochasticException:: - check_exception( - is_bounded_length_law( set.sampler.length ), - nullptr, OpenGeodeException::TYPE::data, - "[SimulationContext] Length term '", - term->term_name, "' on object set '", - set.name, - "': its length law is not bounded (no " - "max_value, or Gaussian). The energy " - "weight of a segment is its whole length, " - "so the model is not integrable for a " - "parameter gamma > 1 (theta > 0). Give the " - "length law a " - "max_value." ); - } + OpenGeodeStochasticStochasticException::check_exception( + is_bounded_length_law( set.sampler.length ), + nullptr, OpenGeodeException::TYPE::data, + "[Model] Length term '", term->term_name, + "' on object set '", set.name, + "': its length law is not bounded (no max_value, " + "or Gaussian). The energy weight of a segment is " + "its whole length, so the model is not integrable " + "for a parameter gamma > 1 (theta > 0). Give the " + "length law a max_value." ); } } } - else - { - geode_unused( config ); - } } - } // namespace detail + } template < typename ObjectType > [[nodiscard]] geode::SimulationContext< ObjectType > build_simulation_context( const SimulationContextConfig< ObjectType >& config ) { - detail::check_length_terms( config ); + check_model_integrability( config ); geode::SimulationContext< ObjectType > context; // ------------------------- diff --git a/tests/stochastic/models/test-model-integrability.cpp b/tests/stochastic/models/test-model-integrability.cpp index 5b3aae9..27057df 100644 --- a/tests/stochastic/models/test-model-integrability.cpp +++ b/tests/stochastic/models/test-model-integrability.cpp @@ -22,6 +22,7 @@ */ #include +#include namespace { @@ -155,6 +156,59 @@ namespace "inter-set hard-core should not make the model integrable" ); // NOLINTEND(*-magic-numbers) } + bool is_integrable( + const geode::SimulationContextConfig< geode::OwnerSegment2D >& config ) + { + try + { + geode::check_model_integrability( config ); + return true; + } + catch( const geode::OpenGeodeException& ) + { + return false; + } + } + + /// Integrability of a simulated model: the rules of the model config and + /// the length laws of the sets, which the model config does not know. + void test_simulation_context_integrability() + { + // NOLINTBEGIN(*-magic-numbers) + geode::SimulationContextConfig< geode::OwnerSegment2D > config; + auto& set = config.add_set( "A" ); + set.sampler.length.distribution_type = + geode::TruncatedLogNormal::distribution_type_static(); + set.sampler.length.mean = 0.; + set.sampler.length.standard_deviation = 0.5; + set.sampler.length.min_value = 0.5; + geode::SingleObjectTermConfig length_term; + length_term.term_name = "A_p21"; + length_term.object_set_names = { "A" }; + length_term.lambda = 2.; + length_term.object_feature = + geode::SegmentLengthInsideBoxFeatureConfig{ 1. }; + config.model.terms.emplace_back( length_term ); + geode::OpenGeodeStochasticStochasticException::test( + !is_integrable( config ), + "[ModelIntegrability] length term on an unbounded length law " + "should not be integrable" ); + + set.sampler.length.max_value = 4.; + geode::OpenGeodeStochasticStochasticException::test( + is_integrable( config ), + "[ModelIntegrability] length term on a bounded length law should " + "be integrable" ); + + // the rules of the model config are applied too + config.model.terms.emplace_back( + pairwise_term( "attraction", { { "A", "A" } }, 2., 1. ) ); + // NOLINTEND(*-magic-numbers) + geode::OpenGeodeStochasticStochasticException::test( + !is_integrable( config ), + "[ModelIntegrability] attraction without hard-core should not be " + "integrable in a simulation context" ); + } } // namespace int main() @@ -170,6 +224,7 @@ int main() test_attractive_model_with_tiny_repulsion(); test_attractive_model_with_hard_core(); test_attractive_inter_set_model(); + test_simulation_context_integrability(); geode::Logger::info( "--> SUCCESS!" ); return 0; From 9e7ddbd6e700929970de60b9a03cab6843ba201b Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 19:21:38 +0100 Subject: [PATCH 06/10] fix(SimulationContext): check the length laws in their own function check_model_integrability keeps a single meaning: the rules of the model config. The length term rule, which needs the laws of the set samplers, becomes check_bounded_length_laws on a SimulationContextConfig, called by build_simulation_context; it no longer reapplies the model config rules, already checked when the model is built. Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/models/model.hpp | 4 +-- .../mcmc/helpers/simulation_context.hpp | 31 ++++++++++--------- .../models/test-model-integrability.cpp | 23 +++++--------- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index ca65231..95b9398 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -88,8 +88,8 @@ namespace geode /// intra-set hard-core (gamma = 0) at a strictly positive distance. /// Without it, the Strauss model with gamma > 1 is not integrable /// (Baddeley, Rubak and Turner, 2015). - /// The rules which need the laws of the set samplers (length terms) are - /// checked by check_model_integrability on a SimulationContextConfig. + /// The rule which needs the laws of the set samplers (length terms) is + /// checked by check_bounded_length_laws on a SimulationContextConfig. opengeode_stochastic_stochastic_api void check_model_integrability( const ModelConfig& config ); diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index a77c30d..8e9b6ef 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -103,22 +103,19 @@ namespace geode }; /*! - * Throws if the simulated model is not integrable. The simulated density - * is exp( -energy ) with respect to the laws of the set samplers, so its - * integrability depends on the model config and on these laws: - * - the rules of the model config (see check_model_integrability on - * ModelConfig): an attraction requires hard-cores; - * - a length term requires a bounded length law (a max_value, not - * Gaussian) on each of its sets: the energy weight of a segment is its - * whole length, so gamma > 1 (theta > 0), the usual value to favor long - * fractures, is not integrable with an unbounded law. Once checked, any - * parameter of the length term is integrable: the laws do not change. + * Throws if a length term (SegmentLengthInsideBoxFeature) is on an object + * set whose length law is not bounded (no max_value, or Gaussian). The + * energy weight of a segment is its whole length: with an unbounded law, + * gamma > 1 (theta > 0), the usual value to favor long fractures, gives a + * non integrable model. This rule needs the laws of the set samplers, + * which the model config does not know (see check_model_integrability for + * the rules of the model config). Once checked, any parameter of the + * length term is integrable: the laws do not change. */ template < typename ObjectType > - void check_model_integrability( + void check_bounded_length_laws( const SimulationContextConfig< ObjectType >& config ) { - check_model_integrability( config.model ); if constexpr( std::is_same_v< ObjectType, OwnerSegment2D > ) { for( const auto& term_config : config.model.terms ) @@ -140,8 +137,8 @@ namespace geode OpenGeodeStochasticStochasticException::check_exception( is_bounded_length_law( set.sampler.length ), nullptr, OpenGeodeException::TYPE::data, - "[Model] Length term '", term->term_name, - "' on object set '", set.name, + "[SimulationContext] Length term '", + term->term_name, "' on object set '", set.name, "': its length law is not bounded (no max_value, " "or Gaussian). The energy weight of a segment is " "its whole length, so the model is not integrable " @@ -151,6 +148,10 @@ namespace geode } } } + else + { + geode_unused( config ); + } } template < typename ObjectType > @@ -158,7 +159,7 @@ namespace geode build_simulation_context( const SimulationContextConfig< ObjectType >& config ) { - check_model_integrability( config ); + check_bounded_length_laws( config ); geode::SimulationContext< ObjectType > context; // ------------------------- diff --git a/tests/stochastic/models/test-model-integrability.cpp b/tests/stochastic/models/test-model-integrability.cpp index 27057df..6e466c2 100644 --- a/tests/stochastic/models/test-model-integrability.cpp +++ b/tests/stochastic/models/test-model-integrability.cpp @@ -156,12 +156,12 @@ namespace "inter-set hard-core should not make the model integrable" ); // NOLINTEND(*-magic-numbers) } - bool is_integrable( + bool has_bounded_length_laws( const geode::SimulationContextConfig< geode::OwnerSegment2D >& config ) { try { - geode::check_model_integrability( config ); + geode::check_bounded_length_laws( config ); return true; } catch( const geode::OpenGeodeException& ) @@ -170,9 +170,9 @@ namespace } } - /// Integrability of a simulated model: the rules of the model config and - /// the length laws of the sets, which the model config does not know. - void test_simulation_context_integrability() + /// A length term needs a bounded length law on its sets, which the model + /// config does not know. + void test_length_term_needs_bounded_length_law() { // NOLINTBEGIN(*-magic-numbers) geode::SimulationContextConfig< geode::OwnerSegment2D > config; @@ -190,24 +190,17 @@ namespace geode::SegmentLengthInsideBoxFeatureConfig{ 1. }; config.model.terms.emplace_back( length_term ); geode::OpenGeodeStochasticStochasticException::test( - !is_integrable( config ), + !has_bounded_length_laws( config ), "[ModelIntegrability] length term on an unbounded length law " "should not be integrable" ); set.sampler.length.max_value = 4.; geode::OpenGeodeStochasticStochasticException::test( - is_integrable( config ), + has_bounded_length_laws( config ), "[ModelIntegrability] length term on a bounded length law should " "be integrable" ); - // the rules of the model config are applied too - config.model.terms.emplace_back( - pairwise_term( "attraction", { { "A", "A" } }, 2., 1. ) ); // NOLINTEND(*-magic-numbers) - geode::OpenGeodeStochasticStochasticException::test( - !is_integrable( config ), - "[ModelIntegrability] attraction without hard-core should not be " - "integrable in a simulation context" ); } } // namespace @@ -224,7 +217,7 @@ int main() test_attractive_model_with_tiny_repulsion(); test_attractive_model_with_hard_core(); test_attractive_inter_set_model(); - test_simulation_context_integrability(); + test_length_term_needs_bounded_length_law(); geode::Logger::info( "--> SUCCESS!" ); return 0; From 34f6d8b4e564f426ba461291d5d715ecdbca77c0 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 19:33:40 +0100 Subject: [PATCH 07/10] fix(ObservedIntensity): compare the numbers of objects with a dimensionless tolerance DIMENSIONLESS_EPSILON (1e-6) is the tolerance for the quantities without unit: ratios, weights, exponents, numbers of objects. GLOBAL_EPSILON is a length tolerance and depends on the units: it stays for lengths only. The observation term uses a single rule on the number of unobserved objects lambda * A - F: below -epsilon lambda is underestimated, within epsilon all the objects are observed (hard-core), above the observation is partial. The proposal weights, the von Mises concentration and the power law exponent use the new constant, with the same value. Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/common.hpp | 6 +++ .../mcmc/proposal/proposal_kernel.hpp | 2 +- .../applications/observed_intensity.cpp | 19 +++---- .../stochastic/sampling/random_engine.cpp | 4 +- .../applications/test-observed-intensity.cpp | 51 +++++++++++-------- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/include/geode/stochastic/common.hpp b/include/geode/stochastic/common.hpp index 675026b..401d375 100644 --- a/include/geode/stochastic/common.hpp +++ b/include/geode/stochastic/common.hpp @@ -39,4 +39,10 @@ namespace geode static constexpr double LOG_PROB_INVALID = -std::numeric_limits< double >::infinity(); + + /// Tolerance for the quantities without unit: ratios, weights, exponents, + /// numbers of objects. GLOBAL_EPSILON is a length tolerance (it depends on + /// the units): use it for lengths and distances only. Never use either as + /// a threshold on a model parameter, which is in physical units. + static constexpr double DIMENSIONLESS_EPSILON{ 1E-6 }; } // namespace geode \ No newline at end of file diff --git a/include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp b/include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp index 090b114..c64f34e 100644 --- a/include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp +++ b/include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp @@ -138,7 +138,7 @@ namespace geode // Ensure total > 0 OpenGeodeStochasticStochasticException::check_exception( - total > GLOBAL_EPSILON, nullptr, + total > DIMENSIONLESS_EPSILON, nullptr, OpenGeodeException::TYPE::internal, "[ProposalKernel] - The probability of every moves is zero." ); diff --git a/src/geode/stochastic/applications/observed_intensity.cpp b/src/geode/stochastic/applications/observed_intensity.cpp index c651ce0..6358b83 100644 --- a/src/geode/stochastic/applications/observed_intensity.cpp +++ b/src/geode/stochastic/applications/observed_intensity.cpp @@ -145,22 +145,19 @@ namespace geode } const auto nb_expected = lambda * touching_domain_area( *domain, set.sampler ); + // numbers of objects: an absolute tolerance, independent of the units + const auto nb_unobserved = nb_expected - nb_fixed; OpenGeodeStochasticStochasticException::check_exception( - nb_expected - nb_fixed >= -GLOBAL_EPSILON, nullptr, + nb_unobserved >= -DIMENSIONLESS_EPSILON, nullptr, OpenGeodeException::TYPE::data, "[IntensityScope] Object set '", set.name, "': lambda = ", lambda, " gives ", nb_expected, " objects expected to touch the domain, fewer than its ", nb_fixed, " fixed objects touching it: lambda is underestimated." ); - // all observed up to the rounding of lambda * A: exactly 0 left, so - // that the observation term is a hard-core (only gamma = 0 is one) - constexpr double ROUNDING = 1e-12; - double observed_ratio{ 0. }; - if( nb_expected > 0. ) - { - observed_ratio = nb_fixed < nb_expected * ( 1. - ROUNDING ) - ? nb_fixed / nb_expected - : 1.; - } + // all observed up to the tolerance (e.g. the rounding of lambda * A): + // exactly 0 left, so that the observation term is a hard-core (only + // gamma = 0 is one) + const auto observed_ratio = + nb_unobserved > DIMENSIONLESS_EPSILON ? nb_fixed / nb_expected : 1.; Logger::info( "[IntensityScope] Object set '", set.name, "': ", nb_fixed, " fixed objects touch the domain, out of ", nb_expected, diff --git a/src/geode/stochastic/sampling/random_engine.cpp b/src/geode/stochastic/sampling/random_engine.cpp index 0cc8d75..5721d94 100644 --- a/src/geode/stochastic/sampling/random_engine.cpp +++ b/src/geode/stochastic/sampling/random_engine.cpp @@ -264,7 +264,7 @@ namespace geode // Uniform approximation for very small concentration (nearly // uniform) - if( law.concentration < geode::GLOBAL_EPSILON ) + if( law.concentration < geode::DIMENSIONLESS_EPSILON ) { UniformClosedOpen< double > uniform_dist; uniform_dist.max_value = 2.0 * M_PI; @@ -354,7 +354,7 @@ namespace geode // [xmin, xmax], written with the ratio x / xmin so that no power // of xmin or xmax overflows. is_valid() ensures that xmax is // finite when alpha <= 1. - if( std::fabs( law.alpha - 1.0 ) <= geode::GLOBAL_EPSILON + if( std::fabs( law.alpha - 1.0 ) <= geode::DIMENSIONLESS_EPSILON && std::isfinite( xmax ) ) { // bounded by the finite xmax: cannot overflow diff --git a/tests/stochastic/applications/test-observed-intensity.cpp b/tests/stochastic/applications/test-observed-intensity.cpp index 9485b95..9deba78 100644 --- a/tests/stochastic/applications/test-observed-intensity.cpp +++ b/tests/stochastic/applications/test-observed-intensity.cpp @@ -281,31 +281,38 @@ namespace geode::Logger::info( "--> SUCCESS!" ); } - // lambda * |D| = 0.07 * 100 = 7.000000000000001 with 7 fixed points: all - // the objects are observed, despite the rounding of lambda * |D|. - void test_all_objects_observed_with_rounding() + // lambda * |D| = 7 + gap with 7 fixed points: within DIMENSIONLESS_EPSILON + // of an object, all the objects are observed (rounding of lambda * |D|, + // e.g. 0.07 * 100 = 7.000000000000001, or a gap of a fraction of object). + void test_all_objects_observed_up_to_tolerance() { - geode::Logger::info( "TEST - ALL THE OBJECTS OBSERVED WITH ROUNDING" ); - geode::PoissonProcessBuilder< geode::Point2D > poisson; - poisson.set_domain( square_domain( 10., 2. ) ); - // NOLINTNEXTLINE(*-magic-numbers) - auto& set_config = poisson.add_set( - "set_A", 0.07, std::nullopt, geode::IntensityScope::all_objects ); + geode::Logger::info( + "TEST - ALL THE OBJECTS OBSERVED UP TO THE TOLERANCE" ); // NOLINTNEXTLINE(*-magic-numbers) - for( const auto i : geode::Range{ 7 } ) + for( const auto lambda : { 0.07, ( 7. + 5e-7 ) / 100. } ) { - set_config.fixed_objects.emplace_back( - geode::Point2D{ { 0.5 + i, 5. } } ); + geode::PoissonProcessBuilder< geode::Point2D > poisson; + poisson.set_domain( square_domain( 10., 2. ) ); + auto& set_config = poisson.add_set( "set_A", lambda, std::nullopt, + geode::IntensityScope::all_objects ); + // NOLINTNEXTLINE(*-magic-numbers) + for( const auto i : geode::Range{ 7 } ) + { + set_config.fixed_objects.emplace_back( + geode::Point2D{ { 0.5 + i, 5. } } ); + } + const auto context = poisson.build_simulation_context(); + const auto& terms = context.model->terms(); + const auto& observation = + terms.get( terms.get_term_uuid( "set_A_observation" ) ); + geode::OpenGeodeStochasticStochasticException::test( + observation.is_hard_core(), + "[ObservedIntensity] all observed up to the tolerance (lambda " + "= ", + lambda, + "): the observation term should be a hard-core, theta = ", + observation.theta() ); } - const auto context = poisson.build_simulation_context(); - const auto& terms = context.model->terms(); - const auto& observation = - terms.get( terms.get_term_uuid( "set_A_observation" ) ); - geode::OpenGeodeStochasticStochasticException::test( - observation.is_hard_core(), - "[ObservedIntensity] all observed up to rounding: the observation " - "term should be a hard-core, theta = ", - observation.theta() ); geode::Logger::info( "--> SUCCESS!" ); } @@ -543,7 +550,7 @@ int main() test_touching_domain_area(); test_underestimated_density(); test_all_objects_observed(); - test_all_objects_observed_with_rounding(); + test_all_objects_observed_up_to_tolerance(); test_points_all_objects(); test_fractures_all_objects(); test_three_observation_cases(); From 47b9612d5bb4cbf26e7a02d5d5b4c571eb18cdb2 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 19:43:32 +0100 Subject: [PATCH 08/10] fix(Model): name the attraction rule and document the integrability check_model_integrability only checked one rule: an attractive pairwise term needs intra-set hard-cores. It becomes check_attractions_have_hard_cores, symmetric with check_bounded_length_laws. model.hpp documents the integrability of the model in one place, with references: the reference measure, why a repulsive model is integrable, the two sources of non integrability and where each one is checked, at construction and in set_thetas. Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/models/model.hpp | 50 +++++++++++++++---- .../mcmc/helpers/simulation_context.hpp | 12 ++--- src/geode/stochastic/models/model.cpp | 2 +- .../models/test-model-integrability.cpp | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 95b9398..1df46c7 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -83,14 +83,42 @@ namespace geode std::vector< EnergyTermConfig > terms; }; - /// Throws if the model is not well defined (not integrable): an attractive - /// pairwise term (gamma > 1) requires each of its object sets to have an - /// intra-set hard-core (gamma = 0) at a strictly positive distance. - /// Without it, the Strauss model with gamma > 1 is not integrable - /// (Baddeley, Rubak and Turner, 2015). - /// The rule which needs the laws of the set samplers (length terms) is - /// checked by check_bounded_length_laws on a SimulationContextConfig. - opengeode_stochastic_stochastic_api void check_model_integrability( + /*! + * Integrability of the model. + * The simulated density, exp( -energy ) = exp( sum theta_i s_i ), is + * defined with respect to the laws of the set samplers (the reference + * Poisson process of independent objects). It must be integrable, + * otherwise the model is not defined and the Metropolis-Hastings chain + * diverges (Moller and Waagepetersen, 2004, Statistical Inference and + * Simulation for Spatial Point Processes, Chapman & Hall/CRC). + * A repulsive or neutral model (all theta <= 0) is always integrable: its + * density is bounded by that of the reference process. Each source of + * non integrability has its own check: + * 1. An attractive pairwise term (gamma > 1, theta > 0) lets the objects + * pile up without bound: the Strauss model with gamma > 1 is not + * integrable (Kelly and Ripley, 1976, A note on Strauss's model for + * clustering, Biometrika 63(2), 357-360; Baddeley, Rubak and Turner, + * 2015, Spatial Point Patterns: Methodology and Applications with R, + * Chapman & Hall/CRC). An intra-set hard-core at a strictly + * positive distance bounds the number of objects of each set in the + * bounded extended domain, which makes the attraction integrable: + * check_attractions_have_hard_cores, at construction. + * 2. A length term with theta > 0 weights a segment by exp( theta L / l ): + * integrable only if the length law is bounded (otherwise + * E[ exp( theta L / l ) ] may diverge, e.g. for a log-normal or a power + * law): check_bounded_length_laws, on a SimulationContextConfig since it + * needs the laws of the set samplers. + * 3. Model::set_thetas and set_theta check each new theta against the + * same rules (EnergyTerm::attraction_allowed for the rule 1; the length + * laws of the rule 2 do not change after construction). + * Single object terms only change the intensity: any finite theta keeps + * them integrable, given the rule 2. + */ + + /// Throws if an attractive pairwise term (gamma > 1) has an object set + /// without an intra-set hard-core (gamma = 0) at a strictly positive + /// distance (rule 1 of the model integrability above). + opengeode_stochastic_stochastic_api void check_attractions_have_hard_cores( const ModelConfig& config ); namespace detail @@ -99,7 +127,7 @@ namespace geode /// than 1 (theta > 0) and keep the model integrable: always for a /// single object term, and for a pairwise term only if all its object /// sets have an intra-set hard-core at a strictly positive distance - /// (same rule as check_model_integrability). + /// (same rule as check_attractions_have_hard_cores). [[nodiscard]] opengeode_stochastic_stochastic_api std::vector< bool > attraction_allowed( const ModelConfig& config ); } // namespace detail @@ -114,12 +142,12 @@ namespace geode ~Model() = default; /// Builds the terms of the config. Throws if the model is not - /// integrable (see check_model_integrability). + /// integrable (see check_attractions_have_hard_cores). Model( const ModelConfig& config, const ObjectSets< ObjectType >& object_sets, const SpatialDomain< ObjectType::dim >& domain ) { - check_model_integrability( config ); + check_attractions_have_hard_cores( config ); const auto attraction = detail::attraction_allowed( config ); for( const auto term : Indices{ config.terms } ) { diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index 8e9b6ef..2179c98 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -104,12 +104,12 @@ namespace geode /*! * Throws if a length term (SegmentLengthInsideBoxFeature) is on an object - * set whose length law is not bounded (no max_value, or Gaussian). The - * energy weight of a segment is its whole length: with an unbounded law, - * gamma > 1 (theta > 0), the usual value to favor long fractures, gives a - * non integrable model. This rule needs the laws of the set samplers, - * which the model config does not know (see check_model_integrability for - * the rules of the model config). Once checked, any parameter of the + * set whose length law is not bounded (no max_value, or Gaussian): rule 2 + * of the model integrability (see model.hpp). The energy weight of a + * segment is its whole length: with an unbounded law, gamma > 1 + * (theta > 0), the usual value to favor long fractures, gives a non + * integrable model. This rule needs the laws of the set samplers, which + * the model config does not know. Once checked, any parameter of the * length term is integrable: the laws do not change. */ template < typename ObjectType > diff --git a/src/geode/stochastic/models/model.cpp b/src/geode/stochastic/models/model.cpp index 3207b1e..2ee4729 100644 --- a/src/geode/stochastic/models/model.cpp +++ b/src/geode/stochastic/models/model.cpp @@ -107,7 +107,7 @@ namespace namespace geode { - void check_model_integrability( const ModelConfig& config ) + void check_attractions_have_hard_cores( const ModelConfig& config ) { const auto hard_cores = hard_core_sets( config ); for( const auto& term_cfg : config.terms ) diff --git a/tests/stochastic/models/test-model-integrability.cpp b/tests/stochastic/models/test-model-integrability.cpp index 6e466c2..fa9f69a 100644 --- a/tests/stochastic/models/test-model-integrability.cpp +++ b/tests/stochastic/models/test-model-integrability.cpp @@ -44,7 +44,7 @@ namespace { try { - geode::check_model_integrability( config ); + geode::check_attractions_have_hard_cores( config ); return true; } catch( const geode::OpenGeodeException& ) From 6902f0b8b91c5759e18a98ab5d92bc3b8c4745b9 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 19:47:59 +0100 Subject: [PATCH 09/10] fix(Model): shorten the integrability documentation Co-Authored-By: Claude Opus 5.5 --- include/geode/stochastic/models/model.hpp | 40 +++++++---------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 1df46c7..867e2ff 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -84,35 +84,17 @@ namespace geode }; /*! - * Integrability of the model. - * The simulated density, exp( -energy ) = exp( sum theta_i s_i ), is - * defined with respect to the laws of the set samplers (the reference - * Poisson process of independent objects). It must be integrable, - * otherwise the model is not defined and the Metropolis-Hastings chain - * diverges (Moller and Waagepetersen, 2004, Statistical Inference and - * Simulation for Spatial Point Processes, Chapman & Hall/CRC). - * A repulsive or neutral model (all theta <= 0) is always integrable: its - * density is bounded by that of the reference process. Each source of - * non integrability has its own check: - * 1. An attractive pairwise term (gamma > 1, theta > 0) lets the objects - * pile up without bound: the Strauss model with gamma > 1 is not - * integrable (Kelly and Ripley, 1976, A note on Strauss's model for - * clustering, Biometrika 63(2), 357-360; Baddeley, Rubak and Turner, - * 2015, Spatial Point Patterns: Methodology and Applications with R, - * Chapman & Hall/CRC). An intra-set hard-core at a strictly - * positive distance bounds the number of objects of each set in the - * bounded extended domain, which makes the attraction integrable: - * check_attractions_have_hard_cores, at construction. - * 2. A length term with theta > 0 weights a segment by exp( theta L / l ): - * integrable only if the length law is bounded (otherwise - * E[ exp( theta L / l ) ] may diverge, e.g. for a log-normal or a power - * law): check_bounded_length_laws, on a SimulationContextConfig since it - * needs the laws of the set samplers. - * 3. Model::set_thetas and set_theta check each new theta against the - * same rules (EnergyTerm::attraction_allowed for the rule 1; the length - * laws of the rule 2 do not change after construction). - * Single object terms only change the intensity: any finite theta keeps - * them integrable, given the rule 2. + * Integrability of the model: exp( sum theta_i s_i ) must be integrable + * with respect to the laws of the set samplers (Moller and Waagepetersen, + * 2004). The statistics are non negative, so theta <= 0 is always + * integrable. The sources of non integrability and their checks: + * 1. attractive pairwise term (theta > 0) without intra-set hard-cores + * (Kelly and Ripley, 1976; Baddeley, Rubak and Turner, 2015): + * check_attractions_have_hard_cores; + * 2. length term with theta > 0 on an unbounded length law: + * check_bounded_length_laws (needs the sampler laws); + * 3. Model::set_thetas checks each new theta against rule 1; the laws of + * rule 2 do not change after construction. */ /// Throws if an attractive pairwise term (gamma > 1) has an object set From 6559b0f10c9647c8c09034c1000008e08b01e1ba Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Mon, 30 Nov 2026 21:01:55 +0100 Subject: [PATCH 10/10] fix(SimulationContext): forbid only the attraction of a length term on an unbounded law A length term on an unbounded length law was refused whatever its parameter, although p21 <= 1 (theta <= 0) is integrable. The rule now goes through the attraction flag of the term, like the hard-core rule of the pairwise terms: build_simulation_context calls the new Model::forbid_attraction on such a term, so theta > 0 is refused when the simulation is built and by set_thetas afterwards, and theta <= 0 is accepted. The segment specific rule moves out of the generic header. A length law with an infinite max_value is no longer seen as bounded. Tests: term indices named in the model parameter tests, literal tiny parameters instead of GLOBAL_EPSILON. Co-Authored-By: Claude Opus 5.5 --- .../src/stochastic/applications/fractures.hpp | 10 +- .../stochastic/test-py-simulation-errors.py | 8 +- .../stochastic/applications/fractures.hpp | 9 +- .../models/energy_terms/energy_term.hpp | 3 +- include/geode/stochastic/models/model.hpp | 64 +++++++--- .../segment_set_sampler.hpp | 4 +- .../mcmc/helpers/simulation_context.hpp | 72 ++++------- src/geode/stochastic/CMakeLists.txt | 1 + .../segment_set_sampler.cpp | 3 + .../mcmc/helpers/simulation_context.cpp | 72 +++++++++++ .../applications/test-fractures.cpp | 117 ++++++++++++------ .../energy_terms/test-pairwise-term.cpp | 2 +- .../test-single-object-term-count.cpp | 4 +- .../test-single-object-term-length.cpp | 2 +- .../models/test-model-integrability.cpp | 29 ++--- .../models/test-model-parameters.cpp | 30 +++-- 16 files changed, 277 insertions(+), 153 deletions(-) create mode 100644 src/geode/stochastic/sampling/mcmc/helpers/simulation_context.cpp diff --git a/bindings/python/src/stochastic/applications/fractures.hpp b/bindings/python/src/stochastic/applications/fractures.hpp index e827996..6ce9d70 100644 --- a/bindings/python/src/stochastic/applications/fractures.hpp +++ b/bindings/python/src/stochastic/applications/fractures.hpp @@ -106,11 +106,11 @@ namespace geode "expected_total_length, if given, is the expected mean of " "the statistic of the term (the total length of the " "fractures inside the domain). The set is looked up when the " - "simulation is built; its length law must then be bounded (a " - "max_value, not Gaussian), otherwise OpenGeodeDataError is " - "raised: the energy weight of a fracture is its whole length, " - "and p21 > 1, which favors long fractures, is not integrable " - "with an unbounded law." ) + "simulation is built. p21 > 1, which favors long fractures, " + "needs a bounded length law on the set (a finite max_value, " + "not Gaussian), otherwise OpenGeodeDataError is raised: the " + "energy weight of a fracture is its whole length, so the model " + "is not integrable with an unbounded law." ) .def( "add_minimal_spacing", &FractureProcessBuilder::add_minimal_spacing, pybind11::arg( "set_name" ), pybind11::arg( "minimal_spacing" ), diff --git a/bindings/python/tests/stochastic/test-py-simulation-errors.py b/bindings/python/tests/stochastic/test-py-simulation-errors.py index f0255b3..1bafae2 100644 --- a/bindings/python/tests/stochastic/test-py-simulation-errors.py +++ b/bindings/python/tests/stochastic/test-py-simulation-errors.py @@ -155,14 +155,14 @@ def test_builder_errors(): builder, fset = small_builder() fset.dynamics = stochastic.ObjectSetDynamicsConfig(birth_ratio=0.0, death_ratio=0.0, change_ratio=0.0) raises(og.OpenGeodeDataError, lambda: builder.build_simulation_runner(), "No move at all") - # a length term needs a bounded length law: the energy weight of a - # fracture is its whole length + # p21 > 1 needs a bounded length law: the energy weight of a fracture is + # its whole length builder, fset = small_builder() fset.sampler.length = stochastic.DoubleDistributionDescription( "TruncatedLogNormal", mean=0.0, standard_deviation=0.5, min_value=0.5 ) - builder.add_intensity("fset", 0.5) - raises(og.OpenGeodeDataError, lambda: builder.build_simulation_runner(), "Length term on an unbounded law") + builder.add_intensity("fset", 2.0) + raises(og.OpenGeodeDataError, lambda: builder.build_simulation_runner(), "p21 > 1 on an unbounded law") print("test_builder_errors: OK") diff --git a/include/geode/stochastic/applications/fractures.hpp b/include/geode/stochastic/applications/fractures.hpp index 7a24b27..fdc923e 100644 --- a/include/geode/stochastic/applications/fractures.hpp +++ b/include/geode/stochastic/applications/fractures.hpp @@ -79,10 +79,11 @@ namespace geode IntensityScope scope = IntensityScope::free_objects ); /// Intensity term (fracture length per unit area) on a fracture set. - /// The length law of the set must be bounded (a max_value, not - /// Gaussian): the energy weight of a fracture is its whole length, and - /// p21 > 1, which favors long fractures, is otherwise not integrable. - /// Checked when the simulation is built. + /// p21 > 1, which favors long fractures, needs a bounded length law + /// on the set (a finite max_value, not Gaussian): the energy weight of + /// a fracture is its whole length, so the model is otherwise not + /// integrable. Checked when the simulation is built, and by + /// Model::set_thetas afterwards. void add_intensity( std::string_view set_name, double p21, std::optional< double > expected_total_length = std::nullopt ); diff --git a/include/geode/stochastic/models/energy_terms/energy_term.hpp b/include/geode/stochastic/models/energy_terms/energy_term.hpp index 5b6730a..83c87be 100644 --- a/include/geode/stochastic/models/energy_terms/energy_term.hpp +++ b/include/geode/stochastic/models/energy_terms/energy_term.hpp @@ -92,7 +92,8 @@ namespace geode /// True if theta may be positive (gamma > 1) and keep the model /// integrable. Set by the model, which knows the other terms (see - /// detail::attraction_allowed); false for a term outside a model. + /// detail::attraction_allowed and Model::forbid_attraction); false for + /// a term outside a model. [[nodiscard]] bool attraction_allowed() const { return attraction_allowed_; diff --git a/include/geode/stochastic/models/model.hpp b/include/geode/stochastic/models/model.hpp index 867e2ff..c221fd7 100644 --- a/include/geode/stochastic/models/model.hpp +++ b/include/geode/stochastic/models/model.hpp @@ -92,9 +92,10 @@ namespace geode * (Kelly and Ripley, 1976; Baddeley, Rubak and Turner, 2015): * check_attractions_have_hard_cores; * 2. length term with theta > 0 on an unbounded length law: - * check_bounded_length_laws (needs the sampler laws); - * 3. Model::set_thetas checks each new theta against rule 1; the laws of - * rule 2 do not change after construction. + * build_simulation_context, which knows the sampler laws, calls + * Model::forbid_attraction on such a term; + * 3. Model::set_thetas checks each new theta against both rules (see + * EnergyTerm::attraction_allowed). */ /// Throws if an attractive pairwise term (gamma > 1) has an object set @@ -106,10 +107,11 @@ namespace geode namespace detail { /// For each term of the config, true if its parameter may be greater - /// than 1 (theta > 0) and keep the model integrable: always for a - /// single object term, and for a pairwise term only if all its object - /// sets have an intra-set hard-core at a strictly positive distance - /// (same rule as check_attractions_have_hard_cores). + /// than 1 (theta > 0) and keep the model integrable as far as the + /// config tells: always for a single object term (rule 2, which needs + /// the sampler laws, is applied by Model::forbid_attraction), and for + /// a pairwise term only if all its object sets have an intra-set + /// hard-core at a strictly positive distance (rule 1). [[nodiscard]] opengeode_stochastic_stochastic_api std::vector< bool > attraction_allowed( const ModelConfig& config ); } // namespace detail @@ -163,10 +165,6 @@ namespace geode * (see EnergyTerm::attraction_allowed). * The model is read-only during a simulation: change it between two * simulations, never while a sampler walks in another thread. - * The energy weight of a segment length term is the whole length: a - * positive theta needs a bounded length law. build_simulation_context - * refuses a length term on an unbounded law; a model built outside a - * simulation context is not checked. */ void set_thetas( absl::Span< const double > thetas ) { @@ -185,6 +183,28 @@ namespace geode } } + /*! + * Forbids a positive theta on a term: its attraction is not + * integrable because of the laws of the set samplers, which the model + * config does not know (rule 2 of the model integrability: a length + * term on an unbounded length law). build_simulation_context calls + * it; a model built otherwise must call it itself. Throws, and leaves + * the term unchanged, if its theta is already positive. + */ + void forbid_attraction( index_t term ) + { + OpenGeodeStochasticStochasticException::check_exception( + term < nb_terms(), nullptr, OpenGeodeException::TYPE::data, + "[Model] Unknown term index ", term, " (", nb_terms(), + " terms)." ); + auto& energy_term = *terms_collection_.energy_terms()[term]; + if( !energy_term.is_hard_core() ) + { + check_attraction( energy_term, energy_term.theta(), false ); + } + energy_term.allow_attraction( false ); + } + /// Changes the parameter of one term, with the rules of set_thetas. /// A hard-core term cannot be changed. void set_theta( index_t term, double theta ) @@ -367,12 +387,24 @@ namespace geode std::isfinite( theta ), nullptr, OpenGeodeException::TYPE::data, "[Model] Term '", name, "': the parameter theta must be finite, not ", theta, "." ); + check_attraction( + energy_term, theta, energy_term.attraction_allowed() ); + } + + static void check_attraction( + const EnergyTerm< ObjectType >& energy_term, + double theta, + bool allowed ) + { OpenGeodeStochasticStochasticException::check_exception( - theta <= 0. || energy_term.attraction_allowed(), nullptr, - OpenGeodeException::TYPE::data, "[Model] Attractive term '", - name, "' (theta = ", theta, - " > 0) is not integrable: its object sets require a hard-core " - "interaction (gamma = 0) at a strictly positive distance." ); + theta <= 0. || allowed, nullptr, OpenGeodeException::TYPE::data, + "[Model] Attractive term '", + energy_term.name().value_or( "unnamed" ), "' (theta = ", theta, + " > 0, gamma > 1) is not integrable: a pairwise term requires " + "a hard-core interaction (gamma = 0) at a strictly positive " + "distance on each of its object sets, a length term a bounded " + "length law (a finite max_value, not Gaussian) on each of its " + "object sets." ); } private: diff --git a/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp b/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp index c7b9ceb..787539a 100644 --- a/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp +++ b/include/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.hpp @@ -90,8 +90,8 @@ namespace geode double rotation_probability_; }; - /// True if the length law has a maximal value (a Gaussian has none): - /// segments never exceed it. + /// True if the length law has a finite maximal value (a Gaussian has + /// none, even with a max_value): segments never exceed it. [[nodiscard]] opengeode_stochastic_stochastic_api bool is_bounded_length_law( const DoubleSampler::DistributionDescription& length ); diff --git a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp index 2179c98..dbac0a8 100644 --- a/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp +++ b/include/geode/stochastic/sampling/mcmc/helpers/simulation_context.hpp @@ -25,8 +25,6 @@ #include -#include - #include #include @@ -102,64 +100,34 @@ namespace geode geode::ModelConfig model; }; - /*! - * Throws if a length term (SegmentLengthInsideBoxFeature) is on an object - * set whose length law is not bounded (no max_value, or Gaussian): rule 2 - * of the model integrability (see model.hpp). The energy weight of a - * segment is its whole length: with an unbounded law, gamma > 1 - * (theta > 0), the usual value to favor long fractures, gives a non - * integrable model. This rule needs the laws of the set samplers, which - * the model config does not know. Once checked, any parameter of the - * length term is integrable: the laws do not change. - */ - template < typename ObjectType > - void check_bounded_length_laws( - const SimulationContextConfig< ObjectType >& config ) + namespace detail { - if constexpr( std::is_same_v< ObjectType, OwnerSegment2D > ) - { - for( const auto& term_config : config.model.terms ) - { - const auto* term = - std::get_if< SingleObjectTermConfig >( &term_config ); - if( term == nullptr - || !std::holds_alternative< - SegmentLengthInsideBoxFeatureConfig >( - term->object_feature ) ) - { - continue; - } - for( const auto& set : config.sets ) - { - if( absl::c_linear_search( - term->object_set_names, set.name ) ) - { - OpenGeodeStochasticStochasticException::check_exception( - is_bounded_length_law( set.sampler.length ), - nullptr, OpenGeodeException::TYPE::data, - "[SimulationContext] Length term '", - term->term_name, "' on object set '", set.name, - "': its length law is not bounded (no max_value, " - "or Gaussian). The energy weight of a segment is " - "its whole length, so the model is not integrable " - "for a parameter gamma > 1 (theta > 0). Give the " - "length law a max_value." ); - } - } - } - } - else + /// Indices of the terms whose attraction (theta > 0) the laws of the + /// set samplers make non integrable (rule 2 of the model + /// integrability, see model.hpp). None for these object types. + template < typename ObjectType > + [[nodiscard]] std::vector< index_t > attraction_forbidden_by_laws( + const SimulationContextConfig< ObjectType >& config ) { geode_unused( config ); + return {}; } - } + + /// Segments: the length terms (SegmentLengthInsideBoxFeature) on an + /// object set whose length law is not bounded (see + /// is_bounded_length_law). The energy weight of a segment is its + /// whole length: with an unbounded law, theta > 0 (gamma > 1, which + /// favors long fractures) gives a non integrable model. + [[nodiscard]] opengeode_stochastic_stochastic_api std::vector< index_t > + attraction_forbidden_by_laws( + const SimulationContextConfig< OwnerSegment2D >& config ); + } // namespace detail template < typename ObjectType > [[nodiscard]] geode::SimulationContext< ObjectType > build_simulation_context( const SimulationContextConfig< ObjectType >& config ) { - check_bounded_length_laws( config ); geode::SimulationContext< ObjectType > context; // ------------------------- @@ -193,6 +161,10 @@ namespace geode // ------------------------- context.model = geode::build_model< ObjectType >( config.model, *context.object_sets, *context.domain ); + for( const auto term : detail::attraction_forbidden_by_laws( config ) ) + { + context.model->forbid_attraction( term ); + } // ------------------------- // MH sampler diff --git a/src/geode/stochastic/CMakeLists.txt b/src/geode/stochastic/CMakeLists.txt index 93959e0..7a278be 100644 --- a/src/geode/stochastic/CMakeLists.txt +++ b/src/geode/stochastic/CMakeLists.txt @@ -41,6 +41,7 @@ add_geode_library( "models/energy_terms/energy_term_builder.cpp" "models/model.cpp" "sampling/random_engine.cpp" + "sampling/mcmc/helpers/simulation_context.cpp" "common.cpp" PUBLIC_HEADERS "applications/fractures.hpp" diff --git a/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp b/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp index dfd4211..761938d 100644 --- a/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp +++ b/src/geode/stochastic/sampling/direct/object_set_sampler/segment_set_sampler.cpp @@ -23,6 +23,8 @@ #include +#include + #include #include #include @@ -73,6 +75,7 @@ namespace geode const DoubleSampler::DistributionDescription& length ) { return length.max_value.has_value() + && std::isfinite( length.max_value.value() ) && length.distribution_type != Gaussian::distribution_type_static(); } diff --git a/src/geode/stochastic/sampling/mcmc/helpers/simulation_context.cpp b/src/geode/stochastic/sampling/mcmc/helpers/simulation_context.cpp new file mode 100644 index 0000000..7e803fb --- /dev/null +++ b/src/geode/stochastic/sampling/mcmc/helpers/simulation_context.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +#include + +namespace +{ + bool is_length_term( const geode::EnergyTermConfig& term_config ) + { + const auto* term = + std::get_if< geode::SingleObjectTermConfig >( &term_config ); + return term != nullptr + && std::holds_alternative< + geode::SegmentLengthInsideBoxFeatureConfig >( + term->object_feature ); + } +} // namespace + +namespace geode +{ + namespace detail + { + std::vector< index_t > attraction_forbidden_by_laws( + const SimulationContextConfig< OwnerSegment2D >& config ) + { + std::vector< index_t > terms; + for( const auto term : Indices{ config.model.terms } ) + { + const auto& term_config = config.model.terms[term]; + if( !is_length_term( term_config ) ) + { + continue; + } + const auto& set_names = + std::get< SingleObjectTermConfig >( term_config ) + .object_set_names; + const auto unbounded = absl::c_any_of( + config.sets, [&set_names]( const auto& set ) { + return absl::c_linear_search( set_names, set.name ) + && !is_bounded_length_law( set.sampler.length ); + } ); + if( unbounded ) + { + terms.push_back( term ); + } + } + return terms; + } + } // namespace detail +} // namespace geode diff --git a/tests/stochastic/applications/test-fractures.cpp b/tests/stochastic/applications/test-fractures.cpp index bb7a6b7..64c2e65 100644 --- a/tests/stochastic/applications/test-fractures.cpp +++ b/tests/stochastic/applications/test-fractures.cpp @@ -325,7 +325,8 @@ namespace geode::Logger::info( "--> SUCCESS!" ); } geode::FractureProcessBuilder length_term_builder( - const geode::DoubleSampler::DistributionDescription& length ) + const geode::DoubleSampler::DistributionDescription& length, + double p21 ) { // NOLINTBEGIN(*-magic-numbers) geode::FractureProcessBuilder fractures; @@ -340,42 +341,92 @@ namespace geode::UniformClosed< double >::distribution_type_static(); fset.sampler.azimuth.min_value = 0.; fset.sampler.azimuth.max_value = 180.; - fractures.add_intensity( "fset", 0.5 ); + fractures.add_intensity( "fset", p21 ); // NOLINTEND(*-magic-numbers) return fractures; } + bool build_throws( const geode::FractureProcessBuilder& fractures ) + { + try + { + const auto context = fractures.build_simulation_context(); + return false; + } + catch( const geode::OpenGeodeException& ) + { + return true; + } + } + + bool set_theta_throws( geode::Model< geode::OwnerSegment2D >& model, + geode::index_t term, + double theta ) + { + try + { + model.set_theta( term, theta ); + return false; + } + catch( const geode::OpenGeodeException& ) + { + return true; + } + } + + geode::index_t length_term_index( + const geode::Model< geode::OwnerSegment2D >& model ) + { + return model.term_index( model.terms().get_term_uuid( "fset_p21" ) ); + } + /// The energy weight of a fracture in a length term is its whole length: - /// with an unbounded length law, a length term gamma > 1 (theta > 0, the - /// usual one, favoring long fractures) gives a non integrable model. + /// with an unbounded length law, p21 > 1 (theta > 0, favoring long + /// fractures) gives a non integrable model; p21 <= 1 stays integrable. void test_length_term_needs_bounded_length_law() { geode::Logger::info( "TEST - LENGTH TERM NEEDS A BOUNDED LENGTH LAW" ); // NOLINTBEGIN(*-magic-numbers) + constexpr double ATTRACTIVE_P21{ 2. }; + constexpr double REPULSIVE_P21{ 0.5 }; geode::DoubleSampler::DistributionDescription log_normal; log_normal.distribution_type = geode::TruncatedLogNormal::distribution_type_static(); log_normal.mean = 0.; log_normal.standard_deviation = 0.5; log_normal.min_value = 0.5; - bool rejected{ false }; - try - { - const auto context = - length_term_builder( log_normal ).build_simulation_context(); - } - catch( const geode::OpenGeodeException& ) - { - rejected = true; - } - geode::OpenGeodeStochasticStochasticException::test( rejected, - "[Fractures] length term on an unbounded length law accepted" ); + geode::OpenGeodeStochasticStochasticException::test( + build_throws( length_term_builder( log_normal, ATTRACTIVE_P21 ) ), + "[Fractures] p21 > 1 on an unbounded length law accepted" ); + + // p21 <= 1 on an unbounded law is integrable, but cannot become > 1 + auto repulsive = length_term_builder( log_normal, REPULSIVE_P21 ) + .build_simulation_context(); + const auto unbounded_term = length_term_index( *repulsive.model ); + geode::OpenGeodeStochasticStochasticException::test( + set_theta_throws( *repulsive.model, unbounded_term, 1. ), + "[Fractures] set_theta > 0 on an unbounded length law accepted" ); + geode::OpenGeodeStochasticStochasticException::test( + !set_theta_throws( *repulsive.model, unbounded_term, -1. ), + "[Fractures] set_theta < 0 on an unbounded length law refused" ); + + // an infinite max_value is no bound + log_normal.max_value = std::numeric_limits< double >::infinity(); + geode::OpenGeodeStochasticStochasticException::test( + !geode::is_bounded_length_law( log_normal ), + "[Fractures] a length law with an infinite max_value is bounded" ); + geode::OpenGeodeStochasticStochasticException::test( + build_throws( length_term_builder( log_normal, ATTRACTIVE_P21 ) ), + "[Fractures] p21 > 1 on a length law with an infinite max_value " + "accepted" ); log_normal.max_value = 4.; - auto bounded = - length_term_builder( log_normal ).build_simulation_context(); - // favoring long fractures: gamma > 1 - bounded.model->set_theta( 1, 1. ); + auto bounded = length_term_builder( log_normal, ATTRACTIVE_P21 ) + .build_simulation_context(); + geode::OpenGeodeStochasticStochasticException::test( + !set_theta_throws( + *bounded.model, length_term_index( *bounded.model ), 1. ), + "[Fractures] set_theta > 0 on a bounded length law refused" ); geode::DoubleSampler::DistributionDescription power_law; power_law.distribution_type = @@ -383,8 +434,9 @@ namespace power_law.alpha = 2.; power_law.min_value = 0.5; power_law.max_value = 4.; - const auto power_law_context = - length_term_builder( power_law ).build_simulation_context(); + geode::OpenGeodeStochasticStochasticException::test( + !build_throws( length_term_builder( power_law, ATTRACTIVE_P21 ) ), + "[Fractures] p21 > 1 on a bounded power law refused" ); // a Gaussian is unbounded, even with a max_value geode::DoubleSampler::DistributionDescription gaussian; @@ -393,29 +445,22 @@ namespace gaussian.mean = 2.; gaussian.standard_deviation = 0.5; gaussian.max_value = 4.; - rejected = false; - try - { - const auto context = - length_term_builder( gaussian ).build_simulation_context(); - } - catch( const geode::OpenGeodeException& ) - { - rejected = true; - } - geode::OpenGeodeStochasticStochasticException::test( rejected, - "[Fractures] length term on a Gaussian length law accepted" ); + geode::OpenGeodeStochasticStochasticException::test( + build_throws( length_term_builder( gaussian, ATTRACTIVE_P21 ) ), + "[Fractures] p21 > 1 on a Gaussian length law accepted" ); // a set without length term keeps its unbounded law (warning only) log_normal.max_value.reset(); - auto fractures = length_term_builder( power_law ); + auto fractures = length_term_builder( power_law, ATTRACTIVE_P21 ); auto& other = fractures.add_fracture_set( "other", 0.1 ); other.sampler.length = log_normal; other.sampler.azimuth.distribution_type = geode::UniformClosed< double >::distribution_type_static(); other.sampler.azimuth.min_value = 0.; other.sampler.azimuth.max_value = 180.; - const auto other_context = fractures.build_simulation_context(); + geode::OpenGeodeStochasticStochasticException::test( + !build_throws( fractures ), + "[Fractures] unbounded law on a set without length term refused" ); // NOLINTEND(*-magic-numbers) geode::Logger::info( "--> SUCCESS!" ); } diff --git a/tests/stochastic/models/energy_terms/test-pairwise-term.cpp b/tests/stochastic/models/energy_terms/test-pairwise-term.cpp index 3978e8a..28d04e9 100644 --- a/tests/stochastic/models/energy_terms/test-pairwise-term.cpp +++ b/tests/stochastic/models/energy_terms/test-pairwise-term.cpp @@ -310,7 +310,7 @@ int main() test_pairwise_term( pw_interaction_cfg, pattern, domain ); - pw_interaction_cfg.gamma = geode::GLOBAL_EPSILON; + pw_interaction_cfg.gamma = 1e-6; test_pairwise_term( pw_interaction_cfg, pattern, domain ); pw_interaction_cfg.gamma = 2.0; test_pairwise_term( pw_interaction_cfg, pattern, domain ); diff --git a/tests/stochastic/models/energy_terms/test-single-object-term-count.cpp b/tests/stochastic/models/energy_terms/test-single-object-term-count.cpp index 2907a6b..16cadb7 100644 --- a/tests/stochastic/models/energy_terms/test-single-object-term-count.cpp +++ b/tests/stochastic/models/energy_terms/test-single-object-term-count.cpp @@ -152,7 +152,7 @@ int main() // Test different lambda values including near-zero run_density_test( term_config, pattern, domain ); - term_config.lambda = geode::GLOBAL_EPSILON; + term_config.lambda = 1e-6; run_density_test( term_config, pattern, domain ); term_config.lambda = 100.0021165; run_density_test( term_config, pattern, domain ); @@ -175,7 +175,7 @@ int main() term_config.term_name = "zero"; term_config.object_set_names = { set_name }; - term_config.lambda = -geode::GLOBAL_EPSILON; + term_config.lambda = -1e-6; term_config.object_feature = geode::ObjectCountFeatureConfig{}; auto term = geode::build_energy_term< geode::Point2D >( diff --git a/tests/stochastic/models/energy_terms/test-single-object-term-length.cpp b/tests/stochastic/models/energy_terms/test-single-object-term-length.cpp index 76307aa..a58fbc2 100644 --- a/tests/stochastic/models/energy_terms/test-single-object-term-length.cpp +++ b/tests/stochastic/models/energy_terms/test-single-object-term-length.cpp @@ -181,7 +181,7 @@ int main() term_config.object_feature = object_length; run_intensity_test( term_config, pattern, domain, L0 ); - term_config.lambda = geode::GLOBAL_EPSILON; + term_config.lambda = 1e-6; run_intensity_test( term_config, pattern, domain, L0 ); term_config.lambda = 50.0; run_intensity_test( term_config, pattern, domain, L0 ); diff --git a/tests/stochastic/models/test-model-integrability.cpp b/tests/stochastic/models/test-model-integrability.cpp index fa9f69a..7b7c9fe 100644 --- a/tests/stochastic/models/test-model-integrability.cpp +++ b/tests/stochastic/models/test-model-integrability.cpp @@ -156,22 +156,9 @@ namespace "inter-set hard-core should not make the model integrable" ); // NOLINTEND(*-magic-numbers) } - bool has_bounded_length_laws( - const geode::SimulationContextConfig< geode::OwnerSegment2D >& config ) - { - try - { - geode::check_bounded_length_laws( config ); - return true; - } - catch( const geode::OpenGeodeException& ) - { - return false; - } - } - /// A length term needs a bounded length law on its sets, which the model - /// config does not know. + /// A length term on an unbounded length law cannot be attractive: the + /// model config does not know the laws, the simulation context does. void test_length_term_needs_bounded_length_law() { // NOLINTBEGIN(*-magic-numbers) @@ -182,6 +169,8 @@ namespace set.sampler.length.mean = 0.; set.sampler.length.standard_deviation = 0.5; set.sampler.length.min_value = 0.5; + config.model.terms.emplace_back( + pairwise_term( "spacing", { { "A", "A" } }, 0., 0.1 ) ); geode::SingleObjectTermConfig length_term; length_term.term_name = "A_p21"; length_term.object_set_names = { "A" }; @@ -190,16 +179,16 @@ namespace geode::SegmentLengthInsideBoxFeatureConfig{ 1. }; config.model.terms.emplace_back( length_term ); geode::OpenGeodeStochasticStochasticException::test( - !has_bounded_length_laws( config ), + geode::detail::attraction_forbidden_by_laws( config ) + == std::vector< geode::index_t >{ 1 }, "[ModelIntegrability] length term on an unbounded length law " - "should not be integrable" ); + "should not allow attraction" ); set.sampler.length.max_value = 4.; geode::OpenGeodeStochasticStochasticException::test( - has_bounded_length_laws( config ), + geode::detail::attraction_forbidden_by_laws( config ).empty(), "[ModelIntegrability] length term on a bounded length law should " - "be integrable" ); - + "allow attraction" ); // NOLINTEND(*-magic-numbers) } } // namespace diff --git a/tests/stochastic/models/test-model-parameters.cpp b/tests/stochastic/models/test-model-parameters.cpp index 0fd7b00..e965c26 100644 --- a/tests/stochastic/models/test-model-parameters.cpp +++ b/tests/stochastic/models/test-model-parameters.cpp @@ -79,6 +79,11 @@ namespace /// Terms: density A, hard-core (A,A), attraction (A,A), repulsion (A,B), /// density B. A has a hard-core: its attraction is integrable; B has none. + constexpr geode::index_t DENSITY_A{ 0 }; + constexpr geode::index_t ATTRACTION_A{ 2 }; + constexpr geode::index_t REPULSION_AB{ 3 }; + constexpr geode::index_t DENSITY_B{ 4 }; + std::vector< double > point_gammas() { // NOLINTNEXTLINE(*-magic-numbers) @@ -338,13 +343,14 @@ namespace geode::build_simulation_context( point_config( point_gammas() ) ); auto& model = *context.model; // NOLINTBEGIN(*-magic-numbers) - model.set_theta( 2, 0.4 ); - model.set_theta( 0, 700. ); + model.set_theta( ATTRACTION_A, 0.4 ); + model.set_theta( DENSITY_A, 700. ); // far below log( 1e-6 ): a tiny density, not a hard-core - model.set_theta( 4, -700. ); + model.set_theta( DENSITY_B, -700. ); geode::OpenGeodeStochasticStochasticException::test( - model.thetas()[2] == 0.4 && model.thetas()[0] == 700. - && model.thetas()[4] == -700., + model.thetas()[ATTRACTION_A] == 0.4 + && model.thetas()[DENSITY_A] == 700. + && model.thetas()[DENSITY_B] == -700., "[ModelParameters] valid parameters not set" ); const auto energy = model.energy( *context.object_sets ); geode::OpenGeodeStochasticStochasticException::test( @@ -434,7 +440,8 @@ namespace // a huge density parameter overflows -theta * statistic to -inf: // with the violated hard-core (+inf), the energy must stay +inf // NOLINTNEXTLINE(*-magic-numbers) - model.set_theta( 0, 1e308 ); + model.set_theta( + model.term_index( model.terms().get_term_uuid( "A_p20" ) ), 1e308 ); const auto overflow_energy = model.energy( state ); geode::OpenGeodeStochasticStochasticException::test( overflow_energy == INF, @@ -581,12 +588,13 @@ namespace geode::Logger::info( "TEST - TINY GAMMA IS NOT A HARD-CORE" ); auto gammas = point_gammas(); // NOLINTNEXTLINE(*-magic-numbers) - gammas[4] = 1e-7; + gammas[DENSITY_B] = 1e-7; auto context = geode::build_simulation_context( point_config( gammas ) ); - const auto& term = *context.model->terms().energy_terms()[4]; + const auto& term = *context.model->terms().energy_terms()[DENSITY_B]; geode::OpenGeodeStochasticStochasticException::test( - !term.is_hard_core() && term.theta() == std::log( gammas[4] ), + !term.is_hard_core() + && term.theta() == std::log( gammas[DENSITY_B] ), "[ModelParameters] gamma = 1e-7 should be an ordinary term" ); geode::Logger::info( "--> SUCCESS!" ); } @@ -600,8 +608,8 @@ namespace for( const auto tiny : { 1e-12, 1e-300 } ) { auto gammas = point_gammas(); - gammas[3] = tiny; - gammas[4] = tiny; + gammas[REPULSION_AB] = tiny; + gammas[DENSITY_B] = tiny; auto context = geode::build_simulation_context( point_config( gammas ) ); const auto& model = *context.model;