Scine::Readuct  4.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  Utils::PositionCollection positions = Utils::PositionCollection::Zero(elements.size(), 3); // dummy positions
134  Utils::AtomCollection atoms(elements, positions);
135  _calculator.setStructure(atoms);
136 
137  // Get initial variable values and map to format required by update function
138  auto& spline = _profile.getMolecularSpline().getBSpline();
139  auto variables = TypeConverter::getInnerControlPointMatrix(spline);
140  Eigen::VectorXd variablesVector;
141  int nRows = variables.rows();
142  int nColumns = variables.cols();
143  variablesVector = Eigen::Map<const Eigen::VectorXd>(variables.data(), nRows * nColumns);
144 
145  // Create a profile calculator
146  RecurringProfileCalculator profileCalculator(_calculator, numberEquidistantPoints);
147 
148  // Define update function
149  auto const update = [&](const Eigen::VectorXd& parameters, double& value, Eigen::VectorXd& gradients) {
150  Eigen::MatrixXd variables;
151  variables = Eigen::Map<const Eigen::MatrixXd>(parameters.data(), nRows, nColumns);
152 
153  // Insert variables into Spline
154  TypeConverter::setInnerControlPoints(spline, variables);
155 
156  // Evaluate cost value
157  profileCalculator.calculateEnergiesAndGradients(spline);
158  auto valuesAlongSpline = profileCalculator.valuesAlongSpline();
160  costCalculator.calculateCost(spline, valuesAlongSpline);
161  value = costCalculator.getCost();
162 
163  // Evaluate gradients
164  Eigen::MatrixXd fullDerivatives = costCalculator.getCostDerivatives();
165  int numberInnerControlPoints = spline.controlPointCount() - 2;
166  Eigen::MatrixXd derivatives = fullDerivatives.middleRows(1, numberInnerControlPoints);
167  gradients = Eigen::Map<const Eigen::VectorXd>(derivatives.data(), nRows * nColumns);
168 
169  // Update reaction profile
170  _profile.getProfileEnergies() = ProfileEnergies{profileCalculator.getCoordinates(), profileCalculator.getEnergies()};
171  };
172 
173  // Optimize
174  auto cycles = optimizer.optimize(variablesVector, update, check, log);
175 
176  return cycles;
177  }
178 
185  return _profile;
186  }
187 
195  return check;
196  };
197 
199  OptimizerType optimizer;
200 
203 
205  int numberEquidistantPoints = numIntegrationPointsDefaultValue;
206 
207  private:
208  Core::Calculator& _calculator;
209  ReactionProfile _profile;
210 };
211 } // namespace ElementaryStepOptimization
212 } // namespace Readuct
213 } // namespace Scine
214 
215 #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:196
virtual void setSettings(const Utils::Settings &settings)=0
Apply the given settings.
Utils::GradientBasedCheck getConvergenceCheck() const override
The underlying convergence check.
Definition: ElementaryStepOptimizer.h:194
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:97
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:205
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:184
Utils::GradientBasedCheck check
The underlying convergence check (public in order to change its settings).
Definition: ElementaryStepOptimizer.h:202
Eigen::MatrixXd getCostDerivatives() const
Get the cost derivatives associated with a given spline.
Definition: ReactionPathCostCalculator.h:101
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:92