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";
45 observers = {})
const final {
49 bool silentCalculator = taskSettings.extract(
"silent_stdout_calculator",
true);
50 std::shared_ptr<Core::Calculator> calc;
53 calc = copyCalculator(systems, _input.front(),
name());
54 Utils::CalculationRoutines::setLog(*calc,
true,
true, !silentCalculator);
57 if (calc->getStructure()->size() == 1) {
58 throw std::runtime_error(
"Cannot calculate NT1 optimization for monoatomic systems.");
63 auto optimizer = std::make_unique<Utils::NtOptimizer>(*calc);
65 bool stopOnError = stopOnErrorExtraction(taskSettings);
67 auto settings = optimizer->getSettings();
68 settings.merge(taskSettings);
69 if (!settings.valid()) {
70 settings.throwIncorrectSettings();
72 optimizer->setSettings(settings);
81 using Writer = Utils::XyzStreamHandler;
82 const std::string& outputSystem = ((!_output.empty()) ? _output[0] : _input[0]);
83 boost::filesystem::path dir(outputSystem);
84 boost::filesystem::create_directory(dir);
85 boost::filesystem::path trjfile(outputSystem +
".nt.trj.xyz");
86 std::ofstream trajectory((dir / trjfile).
string(), std::ofstream::out);
87 double oldEnergy = 0.0;
88 Eigen::VectorXd oldParams;
89 auto func = [&](
const int& cycle,
const double& energy,
const Eigen::VectorXd& params) {
90 if (oldParams.size() != params.size()) {
94 printf(
"%7s %16s %16s %16s %16s\n",
"Cycle",
"Energy",
"Energy Diff.",
"Step RMS",
"Max. Step");
96 auto diff = (params - oldParams).eval();
97 printf(
"%7d %+16.9f %+16.9f %+16.9f %+16.9f\n", cycle, energy, energy - oldEnergy,
98 sqrt(diff.squaredNorm() / diff.size()), diff.cwiseAbs().maxCoeff());
101 auto structure = calc->getStructure();
102 Writer::write(trajectory, *structure, std::to_string(energy));
104 optimizer->addObserver(func);
105 auto cout = _logger->output;
108 auto customObservers = [&calc, &observers](
const int& cycle,
const double& ,
const Eigen::VectorXd& ) {
109 for (
auto& observer : observers) {
110 auto atoms = calc->getStructure();
111 Utils::Results& results = calc->results();
112 observer(cycle, *atoms, results,
"nt1_scan");
115 optimizer->addObserver(customObservers);
118 auto structure = calc->getStructure();
121 cycles = optimizer->optimize(*structure, *_logger);
126 boost::filesystem::path failedPath(outputSystem +
".nt.failed.xyz");
127 std::ofstream failedFile((dir / failedPath).
string(), std::ofstream::out);
128 Writer::write(failedFile, *structure);
134 std::string noGuess =
"No transition state guess was found in Newton Trajectory scan";
135 size_t found = boost::current_exception_diagnostic_information().find(noGuess);
136 if (found == std::string::npos) {
137 boost::filesystem::path failedPath(outputSystem +
".nt.failed.xyz");
138 std::ofstream failedFile((dir / failedPath).
string(), std::ofstream::out);
139 Writer::write(failedFile, *structure);
150 <<
" Found TS guess after " << cycles <<
" iterations." <<
Core::Log::endl
154 systems[outputSystem] = calc;
155 boost::filesystem::path xyzfile(outputSystem +
".xyz");
156 std::ofstream xyz((dir / xyzfile).
string(), std::ofstream::out);
157 Writer::write(xyz, *(calc->getStructure()));
167 #endif // READUCT_NTOPTIMIZATIONTASK_H_
Definition: NtOptimizationTask.h:27
void warningIfMultipleOutputsGiven() const
Warn if more than one output system was specified.
Definition: Task.h:107
bool run(SystemsMap &systems, Utils::UniversalSettings::ValueCollection taskSettings, bool testRunOnly=false, std::vector< std::function< void(const int &, const Utils::AtomCollection &, const Utils::Results &, const std::string &)>> observers={}) const final
Executes the actual task represented by this class.
Definition: NtOptimizationTask.h:43
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:85
void warningIfMultipleInputsGiven() const
Warn if more than one input system was specified.
Definition: Task.h:98
const std::vector< std::string > & output() const
Getter for the names of the output systems generated by this task.
Definition: Task.h:92
The base class for all tasks in Readuct.
Definition: Task.h:29
std::string name() const override
Getter for the tasks name.
Definition: NtOptimizationTask.h:39