Scine::Readuct  6.0.0
This is the SCINE module ReaDuct.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
ElementaryStepOptimizer.h
Go to the documentation of this file.
1 
8 #ifndef READUCT_ELEMENTARYSTEPOPTIMIZATION_ELEMENTARYSTEPOPTIMIZER_H
9 #define READUCT_ELEMENTARYSTEPOPTIMIZATION_ELEMENTARYSTEPOPTIMIZER_H
10 
14 #include "ReactionProfile.h"
16 #include "TypeConverter.h"
19 #include "Utils/Settings.h"
22 #include <Eigen/Core>
23 #include <utility>
24 
25 namespace Scine {
26 namespace Readuct {
27 namespace ElementaryStepOptimization {
28 
34  public:
36  ElementaryStepOptimizerBase() = default;
37 
39  virtual ~ElementaryStepOptimizerBase() = default;
40 
46  virtual int optimize(Core::Log& log) = 0;
47 
53  virtual ReactionProfile& getReactionProfile() = 0;
54 
59  virtual void setSettings(const Utils::Settings& settings) = 0;
60 
65  virtual Utils::Settings getSettings() const = 0;
70  virtual const std::shared_ptr<Utils::Settings> getCalculatorSettings() const = 0;
77 };
78 
84 template<class OptimizerType>
86  public:
93  explicit ElementaryStepOptimizer(Core::Calculator& calculator, ReactionProfile initialReactionProfile)
94  : _calculator(calculator), _profile(std::move(initialReactionProfile)){};
95 
100  virtual void setSettings(const Utils::Settings& settings) override {
101  check.applySettings(settings);
102  optimizer.applySettings(settings);
103  numberEquidistantPoints = settings.getInt("num_integration_points");
104  };
105 
110  virtual Utils::Settings getSettings() const override {
112  settings.modifyInt("num_integration_points", numberEquidistantPoints);
113  return settings;
114  };
115 
120  const std::shared_ptr<Utils::Settings> getCalculatorSettings() const override {
121  return std::make_shared<Utils::Settings>(_calculator.settings());
122  };
123 
129  int optimize(Core::Log& log) override {
130  // Preparations
131  EnergiesAndGradientsAlongSpline valuesAlongSpline;
132  const auto& elements = _profile.getMolecularSpline().getElements();
133  const auto structure = _calculator.getStructure();
134  if (!structure || structure->getElements() != elements) {
135  Utils::PositionCollection positions = Utils::PositionCollection::Zero(elements.size(), 3); // dummy positions
136  Utils::AtomCollection atoms(elements, positions);
137  _calculator.setStructure(atoms);
138  }
139 
140  // Get initial variable values and map to format required by update function
141  auto& spline = _profile.getMolecularSpline().getBSpline();
142  auto variables = TypeConverter::getInnerControlPointMatrix(spline);
143  Eigen::VectorXd variablesVector;
144  int nRows = variables.rows();
145  int nColumns = variables.cols();
146  variablesVector = Eigen::Map<const Eigen::VectorXd>(variables.data(), nRows * nColumns);
147 
148  // Create a profile calculator
149  RecurringProfileCalculator profileCalculator(_calculator, numberEquidistantPoints);
150 
151  // Define update function
152  auto const update = [&](const Eigen::VectorXd& parameters, double& value, Eigen::VectorXd& gradients) {
153  Eigen::MatrixXd variables;
154  variables = Eigen::Map<const Eigen::MatrixXd>(parameters.data(), nRows, nColumns);
155 
156  // Insert variables into Spline
157  TypeConverter::setInnerControlPoints(spline, variables);
158 
159  // Evaluate cost value
160  profileCalculator.calculateEnergiesAndGradients(spline);
161  auto valuesAlongSpline = profileCalculator.valuesAlongSpline();
163  costCalculator.calculateCost(spline, valuesAlongSpline);
164  value = costCalculator.getCost();
165 
166  // Evaluate gradients
167  Eigen::MatrixXd fullDerivatives = costCalculator.getCostDerivatives();
168  int numberInnerControlPoints = spline.controlPointCount() - 2;
169  Eigen::MatrixXd derivatives = fullDerivatives.middleRows(1, numberInnerControlPoints);
170  gradients = Eigen::Map<const Eigen::VectorXd>(derivatives.data(), nRows * nColumns);
171 
172  // Update reaction profile
173  _profile.getProfileEnergies() = ProfileEnergies{profileCalculator.getCoordinates(), profileCalculator.getEnergies()};
174  };
175 
176  // Optimize
177  auto cycles = optimizer.optimize(variablesVector, update, check, log);
178 
179  return cycles;
180  }
181 
188  return _profile;
189  }
190 
198  return check;
199  };
200 
202  OptimizerType optimizer;
203 
206 
208  int numberEquidistantPoints = numIntegrationPointsDefaultValue;
209 
210  private:
211  Core::Calculator& _calculator;
212  ReactionProfile _profile;
213 };
214 } // namespace ElementaryStepOptimization
215 } // namespace Readuct
216 } // namespace Scine
217 
218 #endif // READUCT_ELEMENTARYSTEPOPTIMIZER_H
virtual ~ElementaryStepOptimizerBase()=default
Virtual default destructor.
virtual void applySettings(const Settings &settings)=0
virtual const std::shared_ptr< Utils::Settings > getCalculatorSettings() const =0
Get the settings of the calculator used for the energy calculations during the optimization.
int optimize(Core::Log &log) override
Definition: ElementaryStepOptimizer.h:129
OptimizerType optimizer
The underlying optimizer (public in order to change its settings).
Definition: ElementaryStepOptimizer.h:199
virtual void setSettings(const Utils::Settings &settings)=0
Apply the given settings.
virtual std::unique_ptr< Utils::AtomCollection > getStructure() const =0
Utils::GradientBasedCheck getConvergenceCheck() const override
The underlying convergence check.
Definition: ElementaryStepOptimizer.h:197
A class to optimize reaction paths based on the B-Splines approach described in Alain Vaucher&#39;s Ph...
Definition: ElementaryStepOptimizer.h:85
The base class for all reaction path optimizers. The only purpose of this base class is to hide the t...
Definition: ElementaryStepOptimizer.h:33
virtual void setSettings(const Utils::Settings &settings) override
Apply the given settings.
Definition: ElementaryStepOptimizer.h:100
double getCost() const
Get the cost associated with a given spline (after it has been evaluated).
Definition: ReactionPathCostCalculator.h:98
const std::shared_ptr< Utils::Settings > getCalculatorSettings() const override
Get the settings of the calculator used for the energy calculations during the optimization.
Definition: ElementaryStepOptimizer.h:120
ElementaryStepOptimizer(Core::Calculator &calculator, ReactionProfile initialReactionProfile)
Construct a new TestOptimizer object.
Definition: ElementaryStepOptimizer.h:93
virtual Utils::Settings & settings()=0
virtual Utils::Settings getSettings() const override
Get the public settings as a Utils::Settings object.
Definition: ElementaryStepOptimizer.h:110
virtual ReactionProfile & getReactionProfile()=0
Get the current reaction profile (at the end of an optimization, this is the final reaction profile)...
int numberEquidistantPoints
The number of interpolation points on the spline.
Definition: ElementaryStepOptimizer.h:208
Settings for an ElementaryStepOptimizer.
Definition: ElementaryStepOptimizerSettings.h:29
ReactionProfile & getReactionProfile() override
Get the current reaction profile (at the end of an optimization, this is the final reaction profile)...
Definition: ElementaryStepOptimizer.h:187
Utils::GradientBasedCheck check
The underlying convergence check (public in order to change its settings).
Definition: ElementaryStepOptimizer.h:205
Eigen::MatrixXd getCostDerivatives() const
Get the cost derivatives associated with a given spline.
Definition: ReactionPathCostCalculator.h:102
virtual void setStructure(const Utils::AtomCollection &structure)=0
virtual int optimize(Core::Log &log)=0
Optimize an interpolated elementary step.
virtual Utils::Settings getSettings() const =0
Get the public settings as a Utils::Settings object.
virtual Utils::GradientBasedCheck getConvergenceCheck() const =0
The underlying convergence check.
void calculateCost(const Utils::BSplines::BSpline &spline, const EnergiesAndGradientsAlongSpline &energyValues)
Evaluate the cost associated with a given spline.
Definition: ReactionPathCostCalculator.h:93