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
0 commit comments