Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
#)
80 changes: 24 additions & 56 deletions include/geode/stochastic/models/energy_term_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,35 @@ namespace geode
EnergyTermCollection& operator=( EnergyTermCollection&& ) = default;

[[nodiscard]] uuid add_energy_term(
std::shared_ptr< EnergyTerm< ObjectType > > 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;
const uuid term_uuid = term->id();
auto term_idx = energy_terms_.size();
energy_terms_.emplace_back( std::move( term ) );
energy_terms_map_.emplace( term_uuid, term_idx );
return term_uuid;
}

[[nodiscard]] bool remove_energy_term( const uuid& term_id )
{
auto term_it = energy_terms_.find( term_id );
if( term_it == energy_terms_.end() )
auto term_it = energy_terms_map_.find( term_id );
if( term_it == energy_terms_map_.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 );
index_t idx = term_it->second;
index_t last = energy_terms_.size() - 1;
std::swap( energy_terms_[idx], energy_terms_[last] );
energy_terms_map_[energy_terms_[idx]->id()] = idx;
energy_terms_.pop_back();
energy_terms_map_.erase( term_it );
return true;
}

void clear()
{
energy_terms_.clear();
set_to_terms_.clear();
energy_terms_map_.clear();
}

[[nodiscard]] index_t size() const
Expand All @@ -76,49 +58,35 @@ 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 = energy_terms_map_.find( term_id );
OPENGEODE_EXCEPTION( term_it != energy_terms_map_.end(),
absl::StrCat( "[EnergyTermCollection] Unknown energy term: ",
term_id.string() ) );
return *term_it->second;
}

[[nodiscard]] const absl::flat_hash_map< uuid,
std::shared_ptr< EnergyTerm< ObjectType > > >&
all_terms() const
{
return energy_terms_;
return *energy_terms_[term_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 > > >
std::vector< std::unique_ptr< EnergyTerm< ObjectType > > >
energy_terms_;

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

} // namespace geode
25 changes: 5 additions & 20 deletions include/geode/stochastic/models/energy_terms/density_term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,24 @@
*/
#pragma once

#include <geode/stochastic/spatial/object_sets.hpp>

#include <geode/stochastic/models/energy_terms/single_object_term.hpp>
#include <geode/stochastic/spatial/single_object_features/single_object_feature.hpp>

namespace geode
{
template < typename ObjectType >
class DensityTerm : public SingleObjectTerm< ObjectType,
std::function< double( const ObjectType&,
const SpatialDomain< ObjectType::dim >& ) > >
class DensityTerm : public SingleObjectTerm< ObjectType >
{
public:
explicit DensityTerm( std::string_view name,
double lambda,
std::vector< uuid > targeted_set_ids,
const SpatialDomain< ObjectType::dim >& domain )
: SingleObjectTerm< ObjectType,
std::function< double( const ObjectType&,
const SpatialDomain< ObjectType::dim >& ) > >(
name,
: SingleObjectTerm< ObjectType >( name,
lambda,
std::move( targeted_set_ids ),
1.0, // scale by domain area to get density per unit
[]( const ObjectType& obj,
const SpatialDomain< ObjectType::dim >& spatial_domain ) {
if( SpatialDomainChecker< ObjectType >::
is_anchored_in_domain( spatial_domain, obj ) )
{
return 1.0;
}
return 0.0;
}, // contribution = 1 anchoredin domain
domain )
domain,
std::make_unique< ObjectInDomainFeature< ObjectType > >() )
{
}
};
Expand Down
44 changes: 21 additions & 23 deletions include/geode/stochastic/models/energy_terms/energy_term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ namespace geode
public:
explicit EnergyTerm( std::string_view name,
double param,
std::vector< uuid >&& targeted_set_ids,
std::vector< uuid >&& impacted_set_ids,
const SpatialDomain< ObjectType::dim >& domain )
: energy_scale_{ param },
targeted_set_ids_{ std::move( targeted_set_ids ) },
impacted_set_ids_{ std::move( impacted_set_ids ) },
domain_( domain )
{
std::sort( targeted_set_ids_.begin(), targeted_set_ids_.end() );
std::sort( impacted_set_ids_.begin(), impacted_set_ids_.end() );
Comment thread
francoisbonneau marked this conversation as resolved.
Outdated
impacted_set_ids_.erase( std::unique( impacted_set_ids_.begin(),
impacted_set_ids_.end() ),
impacted_set_ids_.end() );
impacted_set_ids_.shrink_to_fit();
IdentifierBuilder builder( *this );
builder.set_name( name );
}
Expand All @@ -107,9 +111,9 @@ namespace geode
return energy_scale_.parameter();
}

[[nodiscard]] const std::vector< uuid >& targeted_set_ids() const
[[nodiscard]] const std::vector< uuid >& impacted_set_ids() const
{
return targeted_set_ids_;
return impacted_set_ids_;
}

/// Energy contribution for a given statistic multiplier
Expand Down Expand Up @@ -143,51 +147,45 @@ namespace geode
absl::StrCat( "Term : ", name().value_or( id().string() ),
"; uuid: ", id().string(),
" parameter value: ", energy_scale_.parameter(),
" applyied on ", targeted_set_ids_.size(),
" applyied on ", impacted_set_ids_.size(),
" object subsets -->" );
for( const auto& set_id : targeted_set_ids_ )
for( const auto& set_id : impacted_set_ids_ )
{
absl::StrAppend( &message, "\t", set_id.string() );
}
return message;
}

protected:
[[nodiscard]] bool is_targeted_set( const uuid& set_id ) const
[[nodiscard]] bool is_impacted_set( const uuid& set_id ) const
{
return std::binary_search(
targeted_set_ids_.begin(), targeted_set_ids_.end(), set_id );
impacted_set_ids_.begin(), impacted_set_ids_.end(), set_id );
}

[[nodiscard]] const SpatialDomain< ObjectType::dim >& domain() const
{
return domain_;
}

// bool intersects_domain( const ObjectType& obj ) const
// {
// return SpatialDomainChecker< ObjectType
// >::intersects_domain(
// domain_, obj );
// }

template < typename Func >
void for_each_targeted_object(
const ObjectSets< ObjectType >& state, Func&& do_apply ) const
void for_each_object_in_sets( const ObjectSets< ObjectType >& state,
const std::vector< uuid >& set_ids,
Func&& do_apply ) const
{
for( const auto& targeted_set_id : targeted_set_ids_ )
for( const auto& set_id : set_ids )
{
for( const auto set_id :
state.get_objects_in_set( targeted_set_id ) )
for( const auto object_ids :
state.get_objects_in_set( set_id ) )
{
std::forward< Func >( do_apply )( set_id );
std::forward< Func >( do_apply )( object_ids );
}
}
}

private:
detail::EnergyScale energy_scale_;
std::vector< uuid > targeted_set_ids_;
std::vector< uuid > impacted_set_ids_;
const SpatialDomain< ObjectType::dim >& domain_;
};
} // namespace geode
Loading
Loading