7 #ifndef READUCT_NTOPTIMIZATIONTASK_H_
8 #define READUCT_NTOPTIMIZATIONTASK_H_
17 #include <boost/exception/diagnostic_information.hpp>
18 #include <boost/filesystem.hpp>
36 :
Task(std::move(input), std::move(output), std::move(logger)) {
39 std::string
name()
const override {
40 return "NT1 Optimization";
47 bool silentCalculator = taskSettings.extract(
"silent_stdout_calculator",
true);
48 std::shared_ptr<Core::Calculator> calc;
51 calc = copyCalculator(systems, _input.front(),
name());
52 Utils::CalculationRoutines::setLog(*calc,
true,
true, !silentCalculator);
55 if (calc->getStructure()->size() == 1) {
56 throw std::runtime_error(
"Cannot calculate NT1 optimization for monoatomic systems.");
61 auto optimizer = std::make_unique<Utils::NtOptimizer>(*calc);
63 bool stopOnError = stopOnErrorExtraction(taskSettings);
65 auto settings = optimizer->getSettings();
66 settings.merge(taskSettings);
67 if (!settings.valid()) {
68 settings.throwIncorrectSettings();
70 optimizer->setSettings(settings);
80 const std::string& outputSystem = ((!_output.empty()) ? _output[0] : _input[0]);
81 boost::filesystem::path dir(outputSystem);
82 boost::filesystem::create_directory(dir);
83 boost::filesystem::path trjfile(outputSystem +
".nt.trj.xyz");
84 std::ofstream trajectory((dir / trjfile).
string(), std::ofstream::out);
85 double oldEnergy = 0.0;
86 Eigen::VectorXd oldParams;
87 auto func = [&](
const int& cycle,
const double& energy,
const Eigen::VectorXd& params) {
88 if (oldParams.size() != params.size()) {
92 printf(
"%7s %16s %16s %16s %16s\n",
"Cycle",
"Energy",
"Energy Diff.",
"Step RMS",
"Max. Step");
94 auto diff = (params - oldParams).eval();
95 printf(
"%7d %+16.9f %+16.9f %+16.9f %+16.9f\n", cycle, energy, energy - oldEnergy,
96 sqrt(diff.squaredNorm() / diff.size()), diff.cwiseAbs().maxCoeff());
99 auto structure = calc->getStructure();
100 Writer::write(trajectory, *structure, std::to_string(energy));
102 optimizer->addObserver(func);
103 auto cout = _logger->output;
106 auto structure = calc->getStructure();
109 cycles = optimizer->optimize(*structure, *_logger);
118 std::string noGuess =
"No transition state guess was found in Newton Trajectory scan";
119 size_t found = boost::current_exception_diagnostic_information().find(noGuess);
120 if (found == std::string::npos) {
130 <<
" Found TS guess after " << cycles <<
" iterations." <<
Core::Log::endl
134 systems[outputSystem] = calc;
135 boost::filesystem::path xyzfile(outputSystem +
".xyz");
136 std::ofstream xyz((dir / xyzfile).
string(), std::ofstream::out);
137 Writer::write(xyz, *(calc->getStructure()));
147 #endif // READUCT_NTOPTIMIZATIONTASK_H_
Definition: NtOptimizationTask.h:27
void warningIfMultipleOutputsGiven() const
Warn if more than one output system was specified.
Definition: Task.h:99
static std::ostream & endl(std::ostream &os)
NtOptimizationTask(std::vector< std::string > input, std::vector< std::string > output, std::shared_ptr< Core::Log > logger=nullptr)
Construct a new NtOptimizationTask.
Definition: NtOptimizationTask.h:35
const std::vector< std::string > & input() const
Getter for the expected names of the input systems.
Definition: Task.h:77
bool run(SystemsMap &systems, Utils::UniversalSettings::ValueCollection taskSettings, bool testRunOnly=false) const final
Executes the actual task represented by this class.
Definition: NtOptimizationTask.h:43
void warningIfMultipleInputsGiven() const
Warn if more than one input system was specified.
Definition: Task.h:90
const std::vector< std::string > & output() const
Getter for the names of the output systems generated by this task.
Definition: Task.h:84
The base class for all tasks in Readuct.
Definition: Task.h:28
std::string name() const override
Getter for the tasks name.
Definition: NtOptimizationTask.h:39