Reaktoro  v2.9.4
A unified framework for modeling chemically reactive systems
Param.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2022 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <limits>
22 
23 // Reaktoro includes
24 #include <Reaktoro/Common/Exception.hpp>
25 #include <Reaktoro/Common/NumberTraits.hpp>
26 #include <Reaktoro/Common/Types.hpp>
27 
28 namespace Reaktoro {
29 
31 class Param
32 {
33 public:
35  Param();
36 
38  Param(const real& val);
39 
41  Param(double val);
42 
44  Param(const String& id, const real& val);
45 
47  auto clone() const -> Param;
48 
50  auto assign(const Param& other) -> Param&;
51 
53  auto value(const real& val) -> Param&;
54 
56  auto value() const -> const real&;
57 
59  auto value() -> real&;
60 
62  auto id(String id) -> Param&;
63 
65  auto id() const -> const String&;
66 
68  auto lowerbound(double val) -> Param&;
69 
71  auto lowerbound() const -> double;
72 
74  auto upperbound(double val) -> Param&;
75 
77  auto upperbound() const -> double;
78 
80  auto isconst(bool val) -> Param&;
81 
83  auto isconst() const -> bool;
84 
86  auto operator=(double val) -> Param&;
87 
89  auto operator=(const real& val) -> Param&;
90 
92  operator const real&() const;
93 
95  operator real&();
96 
98  operator double() const;
99 
101  static auto Constant(const real& val) -> Param;
102 
103 private:
104  struct Impl;
105 
106  SharedPtr<Impl> pimpl;
107 };
108 
109 } // namespace Reaktoro
110 
111 //======================================================================
112 // CODE BELOW NEEDED FOR ARITHMETIC OPERATIONS INVOLVING PARAM
113 //======================================================================
114 
115 namespace Reaktoro {
116 
117 inline auto operator+(const Param& p) { return p.value(); }
118 inline auto operator-(const Param& p) { return -p.value(); }
119 
120 inline auto operator+(const Param& p, const Param& q) { return p.value() + q.value(); }
121 inline auto operator-(const Param& p, const Param& q) { return p.value() - q.value(); }
122 inline auto operator*(const Param& p, const Param& q) { return p.value() * q.value(); }
123 inline auto operator/(const Param& p, const Param& q) { return p.value() / q.value(); }
124 
125 template<typename T, Requires<isNumeric<T>> = true> auto operator+(const Param& p, const T& x) { return p.value() + x; }
126 template<typename T, Requires<isNumeric<T>> = true> auto operator-(const Param& p, const T& x) { return p.value() - x; }
127 template<typename T, Requires<isNumeric<T>> = true> auto operator*(const Param& p, const T& x) { return p.value() * x; }
128 template<typename T, Requires<isNumeric<T>> = true> auto operator/(const Param& p, const T& x) { return p.value() / x; }
129 
130 template<typename T, Requires<isNumeric<T>> = true> auto operator+(const T& x, const Param& p) { return x + p.value(); }
131 template<typename T, Requires<isNumeric<T>> = true> auto operator-(const T& x, const Param& p) { return x - p.value(); }
132 template<typename T, Requires<isNumeric<T>> = true> auto operator*(const T& x, const Param& p) { return x * p.value(); }
133 template<typename T, Requires<isNumeric<T>> = true> auto operator/(const T& x, const Param& p) { return x / p.value(); }
134 
135 } // namespace Reaktoro
136 
137 //======================================================================
138 // CODE BELOW NEEDED FOR COMPARISON OPERATIONS INVOLVING PARAM
139 //======================================================================
140 
141 namespace Reaktoro {
142 
143 inline auto operator==(const Param& p, const Param& q) { return p.value() == q.value(); }
144 inline auto operator!=(const Param& p, const Param& q) { return p.value() != q.value(); }
145 inline auto operator< (const Param& p, const Param& q) { return p.value() < q.value(); }
146 inline auto operator> (const Param& p, const Param& q) { return p.value() > q.value(); }
147 inline auto operator<=(const Param& p, const Param& q) { return p.value() <= q.value(); }
148 inline auto operator>=(const Param& p, const Param& q) { return p.value() >= q.value(); }
149 
150 template<typename T, Requires<isNumeric<T>> = true> auto operator==(const Param& p, const T& x) { return p.value() == x; }
151 template<typename T, Requires<isNumeric<T>> = true> auto operator!=(const Param& p, const T& x) { return p.value() != x; }
152 template<typename T, Requires<isNumeric<T>> = true> auto operator< (const Param& p, const T& x) { return p.value() < x; }
153 template<typename T, Requires<isNumeric<T>> = true> auto operator> (const Param& p, const T& x) { return p.value() > x; }
154 template<typename T, Requires<isNumeric<T>> = true> auto operator<=(const Param& p, const T& x) { return p.value() <= x; }
155 template<typename T, Requires<isNumeric<T>> = true> auto operator>=(const Param& p, const T& x) { return p.value() >= x; }
156 
157 template<typename T, Requires<isNumeric<T>> = true> auto operator==(const T& x, const Param& p) { return x == p.value(); }
158 template<typename T, Requires<isNumeric<T>> = true> auto operator!=(const T& x, const Param& p) { return x != p.value(); }
159 template<typename T, Requires<isNumeric<T>> = true> auto operator< (const T& x, const Param& p) { return x < p.value(); }
160 template<typename T, Requires<isNumeric<T>> = true> auto operator> (const T& x, const Param& p) { return x > p.value(); }
161 template<typename T, Requires<isNumeric<T>> = true> auto operator<=(const T& x, const Param& p) { return x <= p.value(); }
162 template<typename T, Requires<isNumeric<T>> = true> auto operator>=(const T& x, const Param& p) { return x >= p.value(); }
163 
164 } // namespace Reaktoro
165 
166 //======================================================================
167 // CODE BELOW NEEDED FOR MEMOIZATION TECHNIQUE INVOLVING PARAM
168 //======================================================================
169 
170 namespace Reaktoro {
171 
172 template<typename T>
173 struct MemoizationTraits;
174 
176 template<>
178 {
179  using CacheType = real;
180 };
181 
182 } // namespace Reaktoro
183 
184 //======================================================================
185 // CODE BELOW NEEDED FOR AUTOMATIC DIFFERENTIATION INVOLVING PARAM
186 //======================================================================
187 
188 namespace Reaktoro {
189 
190 template<size_t order, typename U>
191 auto seed(Param& param, U&& seedval)
192 {
193  autodiff::detail::seed<order>(param.value(), seedval);
194 }
195 
196 } // namespace Reaktoro
197 
198 namespace autodiff {
199 namespace detail {
200 
202 template<>
203 struct NumberTraits<Reaktoro::Param>
204 {
206  using NumericType = double;
207 
209  static constexpr auto Order = 1;
210 };
211 
212 } // namespace autodiff
213 } // namespace detail
A type used to represent the value of a parameter and its lower and upper bounds.
Definition: Param.hpp:32
auto value() const -> const real &
Return the value of the parameter.
Param(const real &val)
Construct a Param object with given value.
Param()
Construct a default Param object.
auto value(const real &val) -> Param &
Set the value of the parameter.
auto clone() const -> Param
Return a deep copy of this Param object.
auto isconst() const -> bool
Return true if the parameter is constant.
auto assign(const Param &other) -> Param &
Assign the attributes of another Param object to this.
auto lowerbound() const -> double
Return the lower bound of the parameter.
auto upperbound() const -> double
Return the upper bound of the parameter.
static auto Constant(const real &val) -> Param
Return a Param object that represents a constant parameter.
Param(const String &id, const real &val)
Construct a Param object with identifier id and value val.
Param(double val)
Construct a Param object with given value.
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto operator<(const ChemicalFormula &lhs, const ChemicalFormula &rhs) -> bool
Compare two ChemicalFormula objects for less than.
auto operator/(const Eigen::MatrixBase< DerivedLHS > &lhs, const Eigen::MatrixBase< DerivedRHS > &rhs) -> decltype(lhs.cwiseQuotient(rhs))
Return the component-wise division of two matrices.
Definition: Matrix.hpp:762
std::string String
Convenient alias for std::string.
Definition: Types.hpp:52
auto operator-(const typename Derived::Scalar &scalar, const Eigen::MatrixBase< Derived > &mat) -> decltype((scalar - mat.array()).matrix())
Return the component-wise division of two matrices.
Definition: Matrix.hpp:786
autodiff::real real
The number type used throughout the library.
Definition: Real.hpp:26
auto operator+(const typename Derived::Scalar &scalar, const Eigen::MatrixBase< Derived > &mat) -> decltype((scalar+mat.array()).matrix())
Return the component-wise division of two matrices.
Definition: Matrix.hpp:774
auto operator!=(const ElementalComposition &l, const ElementalComposition &r) -> bool
Return true if two ElementalComposition objects are different.
auto operator==(const ChemicalFormula &lhs, const ChemicalFormula &rhs) -> bool
Compare two ChemicalFormula objects for equality.
std::shared_ptr< T > SharedPtr
Convenient alias for std::shared_ptr<T>.
Definition: Types.hpp:106
Used to enable function arguments of a type T to be cached in a memoized version of the function.
Definition: Memoization.hpp:30
double NumericType
The underlying floating point type of Param.
Definition: Param.hpp:206