Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"C_Cpp.default.cppStandard": "c++11",
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.includePath": [
"${workspaceFolder}/include",
"${workspaceFolder}/build/include",
"${workspaceFolder:OpenGeode}/include",
"${workspaceFolder:OpenGeode}/build/opengeode/include",
"${workspaceFolder:OpenGeode}/build/third_party/abseil/install/include",
"${workspaceFolder:OpenGeode}/build/third_party/bitsery/install/include",
Expand All @@ -14,6 +15,7 @@
"${workspaceFolder}/include",
"${workspaceFolder}/src",
"${workspaceFolder}/build/include",
"${workspaceFolder:OpenGeode}/include",
"${workspaceFolder:OpenGeode}/build/opengeode/include",
"${workspaceFolder:OpenGeode}/build/third_party/abseil/install/include",
"${workspaceFolder:OpenGeode}/build/third_party/bitsery/install/include",
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ option(OPENGEODE_STOCHASTIC_WITH_PYTHON "Compile Python bindings" OFF)
# Get MyModule dependencies
find_package(OpenGeode REQUIRED CONFIG)

install(
FILES include/geode/stochastic/project.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/geode/stochastic
COMPONENT public
)

# ------------------------------------------------------------------------------------------------
# Configure the OpenGeode-Stochastic libraries
add_subdirectory(src/geode/stochastic)
Expand Down
6 changes: 4 additions & 2 deletions bindings/python/src/stochastic/stochastic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
PYBIND11_MODULE( opengeode_stochastic_py_stochastic, module )
{
module.doc() = "OpenGeode-Stochastic Python binding";
pybind11::class_< geode::StochasticLibrary >( module, "StochasticLibrary" )
.def( "initialize", &geode::StochasticLibrary::initialize );
pybind11::class_< geode::OpenGeodeStochasticStochasticLibrary >(
module, "OpenGeodeStochasticStochasticLibrary" )
.def( "initialize",
&geode::OpenGeodeStochasticStochasticLibrary::initialize );

geode::define_spatial_domain( module );

Expand Down
2 changes: 1 addition & 1 deletion bindings/python/stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

from opengeode_stochastic_py_stochastic import *

StochasticLibrary.initialize()
OpenGeodeStochasticStochasticLibrary.initialize()
6 changes: 5 additions & 1 deletion include/geode/stochastic/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@

#pragma once

#include <limits>

#include <geode/basic/common.hpp>
#include <geode/basic/library.hpp>
#include <geode/basic/logger.hpp>

#include <geode/stochastic/opengeode_stochastic_stochastic_export.hpp>
#include <geode/stochastic/project.hpp>

namespace geode
{
OPENGEODE_LIBRARY( opengeode_stochastic_stochastic_api, Stochastic );
OPENGEODE_LIBRARY(
opengeode_stochastic_stochastic_api, OpenGeodeStochastic, Stochastic );

static constexpr double LOG_PROB_INVALID =
-std::numeric_limits< double >::infinity();
Expand Down
35 changes: 35 additions & 0 deletions include/geode/stochastic/project.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 <geode/basic/assert.hpp>

namespace geode
{
class OpenGeodeStochasticException : public OpenGeodeException
{
protected:
using OpenGeodeException::OpenGeodeException;
};
} // namespace geode
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ namespace geode
: ObjectSetSampler< Point< dimension > >{}, domain_( domain )
{
auto volume = domain_.extended_n_volume();
OPENGEODE_EXCEPTION( volume != 0.,
OpenGeodeStochasticStochasticException::check_exception(
volume != 0., nullptr, OpenGeodeException::TYPE::data,
"[PointSetSampler] - Undefined Extended Bounding "
"Box (volume ==0)." );
this->log_pdf_ = -std::log( volume );
step_move_ = define_step_for_move();
OPENGEODE_EXCEPTION( step_move_ > 0.,
OpenGeodeStochasticStochasticException::check_exception(
step_move_ > 0., nullptr, OpenGeodeException::TYPE::data,
"[PointSetSampler] - Undefined step length for move (value == ",
step_move_, ")." );
}
Expand Down Expand Up @@ -73,10 +75,10 @@ namespace geode
new_point =
PointUniformSampler::sample< dimension >( engine, ball );
}
throw OpenGeodeException(
absl::StrCat( "[PointSampler] - Cannot find a point in the "
"extended domain" ) );
return obj;
throw OpenGeodeStochasticStochasticException{ nullptr,
OpenGeodeException::TYPE::internal,
"[PointSampler] - Cannot find a point in the "
"extended domain" };
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace geode
azimuth_{ azimuth }
{
auto volume = domain_.extended_n_volume();
OPENGEODE_EXCEPTION( volume != 0.,
OpenGeodeStochasticStochasticException::check_exception(
volume != 0., nullptr, OpenGeodeException::TYPE::data,
"[SegmentSetSampler] - Undefined Extended Bounding "
"Box (volume ==0)." );
this->log_pdf_ = -std::log( volume );
Expand Down Expand Up @@ -83,8 +84,9 @@ namespace geode
}
new_point = PointUniformSampler::sample< 2 >( engine, ball );
}
throw OpenGeodeException( absl::StrCat(
"[SegmentSetSampler] - Cannot find a point in the box" ) );
throw OpenGeodeStochasticStochasticException{ nullptr,
OpenGeodeException::TYPE::internal,
"[SegmentSetSampler] - Cannot find a point in the box" };
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ namespace geode::detail
{
explicit EnergyScale( double param )
{
OPENGEODE_EXCEPTION( param >= 0.,
"[Gibbs energy term] - The model parameter "
"cannot be negative." );
OpenGeodeStochasticStochasticException::check_exception(
param >= 0., nullptr, OpenGeodeException::TYPE::data,
"[Gibbs energy term] - The model parameter cannot be "
"negative." );

if( param >= geode::GLOBAL_EPSILON )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ namespace geode
const uuid& term_id ) const
{
auto term_it = energy_terms_.find( term_id );
OPENGEODE_EXCEPTION( term_it != energy_terms_.end(),
absl::StrCat( "[EnergyTermCollection] Unknown energy term: ",
term_id.string() ) );
OpenGeodeStochasticStochasticException::check_exception(
term_it != energy_terms_.end(), nullptr,
OpenGeodeException::TYPE::data,
"[EnergyTermCollection] Unknown energy term: ",
term_id.string() );
return *term_it->second;
}

Expand All @@ -95,7 +97,9 @@ namespace geode
terms_for_set( const uuid& set_id ) const
{
const auto it = set_to_terms_.find( set_id );
OPENGEODE_EXCEPTION( it != set_to_terms_.end(),
OpenGeodeStochasticStochasticException::check_exception(
it != set_to_terms_.end(), nullptr,
OpenGeodeException::TYPE::data,
"[EnergyTermCollection] - Object Subset (", set_id.string(),
") does not have any energy term." );
return it->second;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ namespace geode

void add_x_node_monitoring( double beta_x_node )
{
OPENGEODE_EXCEPTION( beta_x_node <= 1.0 && beta_x_node >= 0.,
OpenGeodeStochasticStochasticException::check_exception(
beta_x_node <= 1.0 && beta_x_node >= 0., nullptr,
OpenGeodeException::TYPE::data,
"[FractureSimulationRunner] x node should be inhibitated, "
"please provise a value in [0., 1.]." );
beta_x_node_ = beta_x_node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ namespace geode

void add_realization( const std::vector< double >& values )
{
OPENGEODE_EXCEPTION( values.size() == means_.size(),
OpenGeodeStochasticStochasticException::check_exception(
values.size() == means_.size(), nullptr,
OpenGeodeException::TYPE::data,
"[StatisticsMonitor] - Mismatch between realization size and "
"expected number of statistics." );
++count_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,20 @@ namespace geode
fs::path file_path{ std::string( path_filename ) };

if( !file_path.has_parent_path() )
{
file_path = fs::current_path() / file_path;

}
if( file_path.has_parent_path() )
{
fs::create_directories( file_path.parent_path() );

}
std::ofstream file( file_path, mode );
if( !file.is_open() )
throw geode::OpenGeodeException(
"Cannot open file: " + file_path.string() );

{
throw geode::OpenGeodeStochasticStochasticException{ nullptr,
OpenGeodeException::TYPE::data,
"Cannot open file: " + file_path.string() };
}
return file;
}
const std::string& create_statistics_file(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ namespace geode
: gibbs_energy_{ energy_term_collection },
proposal_kernel_( std::move( proposal_kernel ) )
{
OPENGEODE_ASSERT(
proposal_kernel_ != nullptr, "[MH] null proposal kernel" );
OpenGeodeStochasticStochasticException::check_exception(
proposal_kernel_ != nullptr, nullptr,
OpenGeodeException::TYPE::data, "[MH] null proposal kernel" );
}

StepResult< ObjectType > step(
Expand Down Expand Up @@ -106,34 +107,33 @@ namespace geode

void set_beta( double b )
{
OPENGEODE_EXCEPTION( b >= 0.0, "[MH] beta must be >= 0" );
OpenGeodeStochasticStochasticException::check_exception( b >= 0.0,
nullptr, OpenGeodeException::TYPE::data,
"[MH] beta must be >= 0" );
if( b == 0 )
{
geode::Logger::info(
Logger::info(
"[Metropolis Hastings] - beta == 0 all move will be "
"accepted - Uniform sampling." );
}
if( b < 1 )
{
geode::Logger::info(
Logger::info(
"[Metropolis Hastings] - beta < 1 moves that increase "
"energy are "
"more likely to be accepted - Hot system introduce "
"randomness for exploration." );
"energy are more likely to be accepted - Hot system "
"introduce randomness for exploration." );
}
if( b == 1 )
{
geode::Logger::info( "[Metropolis Hastings] - beta == 1 "
"default choice no temperature "
"- only consider energy." );
Logger::info( "[Metropolis Hastings] - beta == 1 default "
"choice no temperature - only consider energy." );
}
if( b > 1 )
{
geode::Logger::info(
"[Metropolis Hastings] - beta > 1 moves that increase "
"energy are "
"less likely to be accepted - Cold system to ensure "
"convergence but may find local minimum randomness." );
Logger::info( "[Metropolis Hastings] - beta > 1 moves that "
"increase energy are less likely to be accepted "
"- Cold system to ensure convergence but may "
"find local minimum randomness." );
}
beta_ = b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ namespace geode
double change_ratio )
{
const auto total_ratio = birth_ratio + death_ratio;
OPENGEODE_EXCEPTION(
total_ratio > 0., "BIRTH+DEATH ratio must be positive" );
OpenGeodeStochasticStochasticException::check_exception(
total_ratio > 0., nullptr, OpenGeodeException::TYPE::data,
"BIRTH+DEATH ratio must be positive" );

const auto p_birth = birth_ratio / total_ratio;
kernel.add_move(
Expand Down Expand Up @@ -77,7 +78,8 @@ namespace geode
double death_prob )
{
auto birth_death_prob = birth_prob + death_prob;
OPENGEODE_EXCEPTION( birth_death_prob < 1.,
OpenGeodeStochasticStochasticException::check_exception(
birth_death_prob < 1., nullptr, OpenGeodeException::TYPE::data,
"[Proposal Kernel] - changes should be allowed." );
auto kernel = std::make_unique< ProposalKernel< ObjectType > >();
kernel->add_move(
Expand Down
19 changes: 13 additions & 6 deletions include/geode/stochastic/sampling/mcmc/proposal/moves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ namespace geode

double transition_probability() const
{
OPENGEODE_EXCEPTION( std::isfinite( log_forward_prob )
&& std::isfinite( log_backward_prob ),
OpenGeodeStochasticStochasticException::check_exception(
std::isfinite( log_forward_prob )
&& std::isfinite( log_backward_prob ),
nullptr, OpenGeodeException::TYPE::data,
"[Proposal Probabilities] Non-finite proposal "
"log-probabilities" );
return log_backward_prob - log_forward_prob;
Expand Down Expand Up @@ -80,9 +82,10 @@ namespace geode
case MoveType::Change:
return "Change";
default:
throw OpenGeodeException(
throw OpenGeodeStochasticStochasticException{ nullptr,
OpenGeodeException::TYPE::data,
"[MoveResult] -Move result type should always be "
"defined." );
"defined." };
return "Unknown";
}
}
Expand Down Expand Up @@ -113,7 +116,9 @@ namespace geode
double proportion_weight )
: sampler_( sampler ), proportion_weight_{ proportion_weight }
{
OPENGEODE_EXCEPTION( proportion_weight_ > 0.,
OpenGeodeStochasticStochasticException::check_exception(
proportion_weight_ > 0., nullptr,
OpenGeodeException::TYPE::data,
"[Move] - the weight factor for a move should be in higher "
"than 0. (here = ",
proportion_weight_, ")" );
Expand Down Expand Up @@ -182,7 +187,9 @@ namespace geode

void initialize_probability( double probability ) override
{
OPENGEODE_EXCEPTION( birth_ratio_ > 0. && birth_ratio_ < 1.,
OpenGeodeStochasticStochasticException::check_exception(
birth_ratio_ > 0. && birth_ratio_ < 1., nullptr,
OpenGeodeException::TYPE::data,
"[BirthDeathMove]-the ratio of birth over mover should be in "
"]0,1[. (here = ",
birth_ratio_, ")" );
Expand Down
Loading
Loading