Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Checks: >
-readability-use-anyofallof,
-readability-redundant-access-specifiers,
-readability-convert-member-functions-to-static,
-cppcoreguidelines-avoid-const-or-ref-data-members
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-pro-bounds-constant-array-index

CheckOptions:
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
Expand Down Expand Up @@ -63,5 +64,9 @@ CheckOptions:
value: lower_case
- key: readability-function-cognitive-complexity.Threshold
value: 10
- key: readability-function-cognitive-complexity.IgnoreMacros
value: true
- key: readability-function-size.ParameterThreshold
value: 4
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
value: true
2 changes: 1 addition & 1 deletion bindings/python/src/stochastic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
add_geode_python_binding(
NAME "py_stochastic"
SOURCES
"sampling/mcmc/helpers/fracture_simulation_runner.hpp"
# "sampling/mcmc/helpers/fracture_simulation_runner.hpp"
"sampling/mcmc/helpers/simulation_monitor.hpp"
"sampling/mcmc/helpers/simulation_printer.hpp"
"sampling/mcmc/simulation_runner.hpp"
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/src/stochastic/stochastic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "sampling/direct/double_sampler.hpp"

#include "sampling/mcmc/helpers/fracture_simulation_runner.hpp"
// #include "sampling/mcmc/helpers/fracture_simulation_runner.hpp"
#include "sampling/mcmc/helpers/simulation_monitor.hpp"
#include "sampling/mcmc/helpers/simulation_printer.hpp"
#include "sampling/mcmc/simulation_runner.hpp"
Expand All @@ -50,5 +50,5 @@ PYBIND11_MODULE( opengeode_stochastic_py_stochastic, module )
geode::define_simulation_monitor( module );
geode::define_simulation_printer( module );
geode::define_simulation_runner( module );
geode::define_fracture_simulation( module );
// geode::define_fracture_simulation( module );
}
10 changes: 5 additions & 5 deletions bindings/python/tests/stochastic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

add_geode_python_test(
SOURCE "test-py-mh-fractures.py"
DEPENDENCIES
${PROJECT_NAME}::py_stochastic
)
#add_geode_python_test(
# SOURCE "test-py-mh-fractures.py"
# DEPENDENCIES
# ${PROJECT_NAME}::py_stochastic
#)
51 changes: 51 additions & 0 deletions include/geode/stochastic/inference/statistic_monitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once
#include <geode/basic/common.hpp>
#include <geode/basic/uuid.hpp>

#include <geode/stochastic/common.hpp>

namespace geode
{

class StatMonitor

Check warning on line 10 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:10:11 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: means_, m2_
{
public:
void add_realization(
const absl::flat_hash_map< uuid, double >& values )

Check failure on line 14 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:14:25 [clang-diagnostic-error]

no template named 'flat_hash_map' in namespace 'absl'
{
++count_;
for( const auto& [term_uuid, value] : values )
{
auto& mean = means_[term_uuid];
auto& m2 = m2_[term_uuid]; // somme des carrés

double delta = value - mean;
mean += delta / count_;
double delta2 = value - mean;
m2 += delta * delta2;
}
}

const index_t statiscal_count() const

Check warning on line 29 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:29:9 [readability-const-return-type]

return type 'const index_t' (aka 'const unsigned int') is 'const'-qualified at the top level, which may reduce code readability without improving const correctness

Check warning on line 29 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:29:9 [modernize-use-nodiscard]

function 'statiscal_count' should be marked [[nodiscard]]

Check warning on line 29 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:29:9 [clang-diagnostic-ignored-qualifiers]

'const' type qualifier on return type has no effect
{
return count_;
}

double mean( const uuid& id ) const

Check warning on line 34 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:34:34 [readability-identifier-length]

parameter name 'id' is too short, expected at least 3 characters

Check warning on line 34 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:34:9 [modernize-use-nodiscard]

function 'mean' should be marked [[nodiscard]]
{
return means_.at( id );
}

double variance( const uuid& id ) const

Check warning on line 39 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:39:38 [readability-identifier-length]

parameter name 'id' is too short, expected at least 3 characters

Check warning on line 39 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:39:9 [modernize-use-nodiscard]

function 'variance' should be marked [[nodiscard]]
{
if( count_ < 2 )

Check warning on line 41 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:41:29 [google-readability-braces-around-statements]

statement should be inside braces
return 0.0;
return m2_.at( id ) / ( count_ - 1 );
}

private:
absl::flat_hash_map< uuid, double > means_;

Check warning on line 47 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:47:45 [clang-diagnostic-unused-private-field]

private field 'means_' is not used

Check failure on line 47 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:47:15 [clang-diagnostic-error]

no template named 'flat_hash_map' in namespace 'absl'
absl::flat_hash_map< uuid, double > m2_;

Check failure on line 48 in include/geode/stochastic/inference/statistic_monitor.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_monitor.hpp:48:15 [clang-diagnostic-error]

no template named 'flat_hash_map' in namespace 'absl'
index_t count_{ 0 };
};
} // namespace geode
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,31 @@
*/
#pragma once

#include <geode/stochastic/common.hpp>

#include <geode/stochastic/models/energy_terms/single_object_term.hpp>
#include <absl/container/flat_hash_map.h>
#include <geode/stochastic/inference/statistic_monitor.hpp>
#include <geode/stochastic/inference/target_statistic.hpp>

namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( OwnerSegment );
ALIAS_2D( OwnerSegment );
} // namespace geode

namespace geode
{
class opengeode_stochastic_stochastic_api IntensityTerm
: public SingleObjectTerm< OwnerSegment2D,
std::function< double( const OwnerSegment2D&,
const SpatialDomain< OwnerSegment2D::dim >& ) > >
template < typename ObjectType >
class StatisticObjective
{
public:
explicit IntensityTerm( std::string_view name,
double lambda,
std::vector< uuid > targeted_set_ids,
double caracteristic_length,
const SpatialDomain< OwnerSegment2D::dim >& domain );
double compute_loss( const StatMonitor& monitor,
const std::vector< TargetStatistic >& targets ) const
{
double loss = 0.0;

for( const auto& target : targets )
{
const double mean = monitor.mean( target.term_id );
const double diff = mean - target.value;

loss += diff * diff;
}

return loss;
}
};
} // namespace geode
54 changes: 54 additions & 0 deletions include/geode/stochastic/inference/statistic_validator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*
*/
#pragma once

#include <absl/container/flat_hash_map.h>

#include <geode/stochastic/inference/statistic_monitor.hpp>
#include <geode/stochastic/inference/target_statistic.hpp>

namespace geode
{
template < typename ObjectType >
class StatisticsValidator
{
public:
void check( const StatisticsMonitor& monitor,

Check failure on line 36 in include/geode/stochastic/inference/statistic_validator.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_validator.hpp:36:27 [clang-diagnostic-error]

unknown type name 'StatisticsMonitor'; did you mean 'StatisticsValidator'?
const std::vector< TargetStatistic >& targets ) const
{
for( const auto& target : targets )
{
const double mean = monitor.mean( target.term_id );

Check failure on line 41 in include/geode/stochastic/inference/statistic_validator.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/statistic_validator.hpp:41:45 [clang-diagnostic-error]

no member named 'mean' in 'StatisticsValidator<ObjectType>'
const double rel_error = std::abs( mean - target.value )
/ ( std::abs( target.value ) + 1e-12 );
// OPENGEODE_EXCEPTION( rel_error < t.tolerance,
// "[StatisticsValidator] Failure for term ",
// t.term_id.string(), "\n mean = ",
// mean,
// "\n target = ", target.value, "\n error
// = ", rel_error,
// "\n tol = ", target.tolerance );
}
}
};
} // namespace geode
33 changes: 33 additions & 0 deletions include/geode/stochastic/inference/target_statistic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*
*/
#pragma once

namespace geode
{
struct TargetStatistic
{
geode::uuid term_id;

Check failure on line 29 in include/geode/stochastic/inference/target_statistic.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/stochastic/inference/target_statistic.hpp:29:16 [clang-diagnostic-error]

no type named 'uuid' in namespace 'geode'
double value;
double tolerance{ 0.05 };
};
} // namespace geode
106 changes: 39 additions & 67 deletions include/geode/stochastic/models/energy_term_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,30 @@ namespace geode

EnergyTermCollection& operator=( EnergyTermCollection&& ) = default;

[[nodiscard]] uuid add_energy_term(
std::shared_ptr< EnergyTerm< ObjectType > > term )
uuid add_energy_term(
std::unique_ptr< EnergyTerm< ObjectType > >&& term )
{
const uuid term_id = term->id();
energy_terms_.emplace( term_id, term );
for( const uuid& set_id : term->targeted_set_ids() )
{
set_to_terms_[set_id].push_back( term );
}
return term_id;
}

[[nodiscard]] bool remove_energy_term( const uuid& term_id )
{
auto term_it = energy_terms_.find( term_id );
if( term_it == energy_terms_.end() )
{
return false;
}

auto term = term_it->second;

for( const uuid& set_id : term->targeted_set_ids() )
{
auto vec_it = set_to_terms_.find( set_id );
if( vec_it == set_to_terms_.end() )
{
continue;
}

auto& vec = vec_it->second;
vec.erase(
std::remove( vec.begin(), vec.end(), term ), vec.end() );

if( vec.empty() )
{
set_to_terms_.erase( vec_it );
}
}

energy_terms_.erase( term_it );
return true;
}

void clear()
{
energy_terms_.clear();
set_to_terms_.clear();
auto term_idx = energy_terms_.size();

const auto term_name = term->name();
OPENGEODE_EXCEPTION( term_name.has_value(),
absl::StrCat( "[EnergyTermCollection]- Energy Term name is not "
"defined." ) );
const auto term_uuid = term->id();
auto [it, inserted_uuid] =
name_to_uuid_.emplace( term_name.value(), term_uuid );
OPENGEODE_EXCEPTION( inserted_uuid,
absl::StrCat( "[EnergyTermCollection]- Energy Term named ",
term_name.value(), " already exists." ) );

auto [it2, inserted_index] =
uuid_to_index_.emplace( term_uuid, term_idx );
OPENGEODE_EXCEPTION( inserted_index,
absl::StrCat( "[EnergyTermCollection]- Energy Term ",
term_uuid.string(), " already exists." ) );

energy_terms_.emplace_back( std::move( term ) );
return term_uuid;
}

[[nodiscard]] index_t size() const
Expand All @@ -76,49 +52,45 @@ namespace geode
[[nodiscard]] const EnergyTerm< ObjectType >& get(
const uuid& term_id ) const
{
auto term_it = energy_terms_.find( term_id );
OPENGEODE_EXCEPTION( term_it != energy_terms_.end(),
auto term_it = uuid_to_index_.find( term_id );
OPENGEODE_EXCEPTION( term_it != uuid_to_index_.end(),
absl::StrCat( "[EnergyTermCollection] Unknown energy term: ",
term_id.string() ) );
return *term_it->second;
return *energy_terms_[term_it->second];
}

[[nodiscard]] const absl::flat_hash_map< uuid,
std::shared_ptr< EnergyTerm< ObjectType > > >&
all_terms() const
[[nodiscard]] uuid get_term_uuid( std::string_view name ) const
{
return energy_terms_;
auto uuid_it = name_to_uuid_.find( name );
OPENGEODE_EXCEPTION( uuid_it != name_to_uuid_.end(),
absl::StrCat(
"[EnergyTermCollection] Unknown energy term: ", name ) );
return uuid_it->second;
}

[[nodiscard]] const std::vector<
std::shared_ptr< EnergyTerm< ObjectType > > >&
terms_for_set( const uuid& set_id ) const
std::unique_ptr< EnergyTerm< ObjectType > > >&
energy_terms() const
{
const auto it = set_to_terms_.find( set_id );
OPENGEODE_EXCEPTION( it != set_to_terms_.end(),
"[EnergyTermCollection] - Object Subset (", set_id.string(),
") does not have any energy term." );
return it->second;
return energy_terms_;
}

[[nodiscard]] std::string string() const
{
auto message = absl::StrCat(
"EnergyTermCollection: ", energy_terms_.size(), " terms:" );
for( const auto& [id, term] : energy_terms_ )
for( const auto& term : energy_terms_ )
{
absl::StrAppend( &message, "\n\t --> ", term->string() );
}
return message;
}

private:
absl::flat_hash_map< uuid, std::shared_ptr< EnergyTerm< ObjectType > > >
absl::flat_hash_map< std::string, uuid > name_to_uuid_;
absl::flat_hash_map< uuid, index_t > uuid_to_index_;
std::vector< std::unique_ptr< EnergyTerm< ObjectType > > >
energy_terms_;

absl::flat_hash_map< uuid,
std::vector< std::shared_ptr< EnergyTerm< ObjectType > > > >
set_to_terms_;
};

} // namespace geode
Loading
Loading