Skip to content

Commit bcad127

Browse files
authored
Merge pull request #7 from Geode-solutions/gibbs_energy
feat(Energy Terms): set up energy term to build gibbs energy.
2 parents 53e1183 + aeadd66 commit bcad127

21 files changed

Lines changed: 1108 additions & 64 deletions

commitlint.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
extends: ["@commitlint/config-angular"],
3+
rules: {
4+
"scope-empty": [2, "never"],
5+
"subject-empty": [2, "never"],
6+
"subject-max-length": [0],
7+
"body-leading-blank": [0],
8+
"footer-leading-blank": [0],
9+
"header-max-length": [0],
10+
"scope-case": [0],
11+
"subject-case": [0],
12+
"subject-full-stop": [0],
13+
"type-case": [0],
14+
"type-empty": [0],
15+
},
16+
}

include/geode/stochastic/configuration/configuration.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ namespace geode
3838
void remove_object( index_t idx );
3939
index_t size() const;
4040

41+
/**
42+
* @brief Bounds-checked access to the marked object at index @p idx.
43+
* Wrapper for std::vector::at().
44+
* @throws std::out_of_range if idx is out of range.
45+
*/
4146
const MarkedObject< Geometry >& object( index_t idx ) const;
47+
/**
48+
* @brief Unchecked access to the marked object at index @p idx.
49+
* Wrapper for std::vector::operator[]().
50+
*/
51+
const MarkedObject< Geometry >& operator[]( index_t idx ) const;
4252

4353
std::vector< index_t > object_ids_with_mark( const Mark& mark );
4454

include/geode/stochastic/configuration/marked_object.hpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#include <geode/stochastic/common.hpp>
2929

30+
#include <geode/geometry/basic_objects/segment.hpp>
31+
#include <geode/geometry/bounding_box.hpp>
32+
#include <geode/geometry/point.hpp>
33+
3034
namespace geode
3135
{
3236
FORWARD_DECLARATION_DIMENSION_CLASS( Point );
@@ -60,8 +64,38 @@ namespace geode
6064
const std::optional< Mark >& mark() const;
6165
bool has_mark() const;
6266

63-
decltype( auto ) bounding_box() const;
64-
decltype( auto ) barycenter() const;
67+
auto bounding_box() const
68+
{
69+
if constexpr( std::is_same_v< Geometry, Point2D > )
70+
{
71+
geode::BoundingBox< 2 > box;
72+
box.add_point( geometry_ );
73+
return box;
74+
}
75+
else if constexpr( std::is_same_v< Geometry, Point3D > )
76+
{
77+
geode::BoundingBox< 3 > box;
78+
box.add_point( geometry_ );
79+
return box;
80+
}
81+
else
82+
{
83+
return geometry_.bounding_box();
84+
}
85+
}
86+
87+
auto barycenter() const
88+
{
89+
if constexpr( std::is_same_v< Geometry, Point2D >
90+
|| std::is_same_v< Geometry, Point3D > )
91+
{
92+
return geometry_;
93+
}
94+
else
95+
{
96+
return geometry_.barycenter();
97+
}
98+
}
6599

66100
private:
67101
Geometry geometry_;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
26+
#include <geode/geometry/distance.hpp>
27+
#include <geode/geometry/point.hpp>
28+
29+
#include <geode/stochastic/configuration/marked_object.hpp>
30+
31+
namespace geode
32+
{
33+
struct DistanceCutoffInteraction
34+
{
35+
DistanceCutoffInteraction( double distance )
36+
: cutoff_distance( distance )
37+
{
38+
}
39+
40+
template < typename Geometry >
41+
double operator()( const MarkedObject< Geometry >& object1,
42+
const MarkedObject< Geometry >& object2 ) const
43+
{
44+
auto dist = geode::point_point_distance(
45+
object1.barycenter(), object2.barycenter() );
46+
return dist <= cutoff_distance ? 1.0 : 0.0;
47+
}
48+
49+
double cutoff_distance{ GLOBAL_EPSILON };
50+
};
51+
} // namespace geode
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
#pragma once
24+
25+
#include <geode/stochastic/common.hpp>
26+
27+
#include <geode/stochastic/configuration/configuration.hpp>
28+
29+
namespace
30+
{
31+
struct NegLogParam
32+
{
33+
explicit NegLogParam( double param )
34+
{
35+
OPENGEODE_EXCEPTION( param >= 0.,
36+
"[Gibbs energy term] - The model parameter "
37+
"cannot be smaller than 0." );
38+
if( param >= geode::GLOBAL_EPSILON )
39+
{
40+
value = -std::log( param );
41+
}
42+
}
43+
44+
double scale( double multiplier ) const
45+
{
46+
if( value )
47+
{
48+
return value.value() * multiplier;
49+
}
50+
return ( multiplier > 0 )
51+
? std::numeric_limits< double >::infinity()
52+
: 0.0;
53+
}
54+
55+
double param() const
56+
{
57+
if( value )
58+
{
59+
return std::exp( -value.value() );
60+
}
61+
return 0.;
62+
}
63+
std::optional< double > value;
64+
};
65+
} // namespace
66+
67+
namespace geode
68+
{
69+
template < typename Geometry >
70+
class EnergyTerm
71+
{
72+
public:
73+
explicit EnergyTerm( double parameter )
74+
: neg_log_parameter_( parameter )
75+
{
76+
}
77+
78+
virtual ~EnergyTerm() = default;
79+
80+
double parameter() const
81+
{
82+
return neg_log_parameter_.param();
83+
}
84+
85+
virtual double log_total(
86+
const Configuration< Geometry >& state ) const = 0;
87+
88+
virtual double log_delta_add( const Configuration< Geometry >& state,
89+
const MarkedObject< Geometry >& sample ) const = 0;
90+
91+
virtual double log_delta_remove( const Configuration< Geometry >& state,
92+
index_t sample_id ) const = 0;
93+
94+
virtual double log_delta_change( const Configuration< Geometry >& state,
95+
index_t old_sample_id,
96+
const MarkedObject< Geometry >& new_sample ) const = 0;
97+
98+
virtual double statistic(
99+
const Configuration< Geometry >& state ) const = 0;
100+
101+
protected:
102+
NegLogParam neg_log_parameter_;
103+
};
104+
} // namespace geode
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
#pragma once
24+
25+
#include <geode/stochastic/configuration/configuration.hpp>
26+
27+
#include <geode/stochastic/sampling/mcmc/models/components/energy_term.hpp>
28+
29+
namespace geode
30+
{
31+
template < typename Geometry >
32+
class IntensityTerm : public EnergyTerm< Geometry >
33+
{
34+
public:
35+
explicit IntensityTerm( double lambda )
36+
: EnergyTerm< Geometry >( lambda )
37+
{
38+
}
39+
40+
double log_total( const Configuration< Geometry >& state ) const final
41+
{
42+
const auto n = static_cast< double >( number_of_objects( state ) );
43+
return this->neg_log_parameter_.scale( n );
44+
}
45+
46+
double log_delta_add( const Configuration< Geometry >& state,
47+
const MarkedObject< Geometry >& sample ) const final
48+
{
49+
return this->neg_log_parameter_.scale( 1. );
50+
}
51+
52+
double log_delta_remove( const Configuration< Geometry >& state,
53+
index_t sample_id ) const final
54+
{
55+
return this->neg_log_parameter_.scale( -1. );
56+
}
57+
58+
double log_delta_change( const Configuration< Geometry >& state,
59+
index_t old_sample_id,
60+
const MarkedObject< Geometry >& new_sample ) const final
61+
{
62+
return 0.0;
63+
}
64+
65+
double statistic( const Configuration< Geometry >& state ) const final
66+
{
67+
return static_cast< double >( number_of_objects( state ) );
68+
}
69+
70+
private:
71+
index_t number_of_objects(
72+
const Configuration< Geometry >& state ) const
73+
{
74+
return state.size();
75+
}
76+
};
77+
} // namespace geode

0 commit comments

Comments
 (0)