Debian Codegraph @

avogadro/calc/energycalculator.h

raw symbols SCIP
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CALC_ENERGYCALCULATOR_H
7 #define AVOGADRO_CALC_ENERGYCALCULATOR_H
8
9 #include "avogadrocalcexport.h"
10
11 #include <avogadro/core/constraint.h>
12 #include <avogadro/core/molecule.h>
13 #include <avogadro/core/variantmap.h>
14 #include <avogadro/core/vector.h>
15
16 namespace Avogadro {
17 namespace Core {
18 class Molecule;
19 }
20
21 namespace Calc {
22
23 constexpr Real KCAL_TO_KJ = 4.184;
24
25 class AVOGADROCALC_EXPORT EnergyCalculator
26 {
27 public:
28 EnergyCalculator() = default;
29 virtual ~EnergyCalculator() = default;
30
31 /**
32 * Create a new instance of the model. Ownership passes to the
33 * caller.
34 */
35 virtual EnergyCalculator* newInstance() const = 0;
36
37 /**
38 * @return a unique identifier for this calculator.
39 */
40 virtual std::string identifier() const = 0;
41
42 /**
43 * @return A short translatable name for this method (e.g., MMFF94, UFF, etc.)
44 */
45 virtual std::string name() const = 0;
46
47 /**
48 * @return a description of the method
49 */
50 virtual std::string description() const = 0;
51
52 /**
53 * @brief Return a JSON object string describing optional user-editable
54 * settings for this model. Empty string means no user options.
55 */
56 virtual std::string userOptions() const { return std::string(); }
57
58 /**
59 * @brief Set user-selected options serialized as a JSON object string.
60 * @return True on success.
61 */
62 virtual bool setUserOptions([[maybe_unused]] const std::string& optionsJson)
63 {
64 return true;
65 }
66
67 /**
68 * @brief Indicate if your method only treats a subset of elements
69 * @return an element mask corresponding to the defined subset
70 */
71 virtual Core::Molecule::ElementMask elements() const = 0;
72
73 /**
74 * @brief Indicate if your method can handle unit cells
75 * @return true if unit cells are supported
76 */
77 virtual bool acceptsUnitCell() const { return false; }
78
79 /**
80 * @brief Indicate if your method can handle ions
81 * Many methods only treat neutral systems, either
82 * a neutral molecule or a neutral unit cell.
83 *
84 * @return true if ions are supported
85 */
86 virtual bool acceptsIons() const { return false; }
87
88 /**
89 * @brief Indicate if your method can handle radicals
90 * Most methods only treat closed-shell molecules.
91 * @return true if radicals are supported
92 */
93 virtual bool acceptsRadicals() const { return false; }
94
95 /**
96 * Calculate the energy for this method.
97 */
98 virtual Real value(const Eigen::VectorXd& x) = 0;
99
100 /**
101 * Calculate the gradients for this method, defaulting to numerical
102 * finite-difference methods
103 */
104 virtual void gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad);
105
106 /**
107 * Calculate numerical gradients for this method.
108 */
109 void finiteGradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad,
110 int accuracy = 0);
111
112 /**
113 * Evaluate the energy and optionally gradients in one call.
114 *
115 * Derived classes with efficient fused implementations should override this.
116 */
117 virtual Real evaluate(const Eigen::VectorXd& x,
118 Eigen::VectorXd* grad = nullptr);
119
120 /**
121 * Calculate the Hessian matrix for this method, defaulting to numerical
122 * finite-difference methods.
123 */
124 virtual void hessian(const Eigen::VectorXd& x, Eigen::MatrixXd& hess);
125
126 /**
127 * Calculate numerical Hessian for this method.
128 */
129 void finiteHessian(const Eigen::VectorXd& x, Eigen::MatrixXd& hess,
130 int accuracy = 0);
131
132 /**
133 * Called to 'clean' gradients @param grad (e.g., for constraints)
134 */
135 void cleanGradients(Eigen::VectorXd& grad);
136
137 /**
138 * Called to get the energies for the current set of constraints.
139 * which should be added to the value() method for real energies
140 * in derived classes
141 * @return the sum of the constraint energies
142 */
143 Real constraintEnergies(const Eigen::VectorXd& x);
144
145 /**
146 * Called to get the gradients for the current set of constraints.
147 * which should be added to the gradient() method in derived classes.
148 * @param x the current coordinates
149 * @param grad the gradient vector to be updated with constraint gradients
150 */
151 void constraintGradients(const Eigen::VectorXd& x, Eigen::VectorXd& grad);
152
153 /**
154 * Called to get the constraints for this method.
155 * @return the constraints for this method
156 */
157 std::vector<Core::Constraint> constraints() const;
158
159 // Set the constraints for this method
160 void setConstraints(const std::vector<Core::Constraint>& constraints);
161
162 /**
163 * Called to update the "frozen" mask (e.g., during editing)
164 */
165 void setMask(Eigen::VectorXd mask) { m_mask = mask; }
166
167 /**
168 * @return the frozen atoms mask
169 */
170 Eigen::VectorXd mask() const { return m_mask; }
171
172 /**
173 * Called when the current molecule changes.
174 */
175 virtual void setMolecule(Core::Molecule* mol) = 0;
176
177 protected:
178 /**
179 * @brief Append an error to the error string for the model.
180 * @param errorString The error to be added.
181 * @param newLine Add a new line after the error string?
182 */
183 void appendError(const std::string& errorString, bool newLine = true) const;
184
185 Eigen::VectorXd m_mask; // optimize or frozen atom mask
186 // Separate the constraints into different types
187 // for speed and convenience.
188 std::vector<Core::Constraint> m_distanceConstraints;
189 std::vector<Core::Constraint> m_angleConstraints;
190 std::vector<Core::Constraint> m_torsionConstraints;
191 std::vector<Core::Constraint> m_outOfPlaneConstraints;
192
193 private:
194 mutable std::string m_error;
195 };
196
197 } // end namespace Calc
198 } // end namespace Avogadro
199
200 #endif // AVOGADRO_CALC_ENERGYCALCULATOR_H