Loading...
Searching...
No Matches
Potential.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/EonLogger.h"
13#include <cctype>
14#include <csignal>
15#include <ctime>
16#include <limits>
17#include <memory>
18#include <span>
19#include <stdexcept>
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "eon/HelperFunctions.h"
25#include "eon/Parameters.h"
26#include "eon/Potential.h"
27#include "eon/Runtime.h"
28
29#ifdef WITH_CATLEARN
31#endif
32
33#ifdef WITH_GPRD
35#endif
36
40#include "rgpot/EMT/EMTPot.hpp"
41#include "rgpot/LennardJones/LJClusterPot.hpp"
42#include "rgpot/LennardJones/LJPot.hpp"
43#include "rgpot/Morse/MorsePot.hpp"
44#include "rgpot/ZBL/ZBLPot.hpp"
45#ifdef RGPOT_HAS_DFTD3
46#include "rgpot/D3Pot/D3Pot.hpp"
47#endif
48#ifdef RGPOT_HAS_DFTD4
49#include "rgpot/D4Pot/D4Pot.hpp"
50#endif
51#ifdef RGPOT_HAS_EXPR
52#include "rgpot/ExprPot/ExprPot.hpp"
53#endif
54#include "rgpot/MOPACPot/MOPACPot.hpp"
55#include "rgpot/fortran/FortranPots.hpp"
56#ifndef IS_WINDOWS
58#ifdef WITH_RGPOT
60#endif
61#endif
62
63// Fortran potentials: always compiled, loaded at runtime via dlopen
64
65#ifdef EMBED_PYTHON
66#ifdef WITH_ASE_POT
68#endif
69#endif
70
71#ifdef EONMPI
73#endif
74
76
77#ifndef _WIN32
78#ifdef WITH_VASP
80#endif
81#endif
82
83#ifdef WITH_AMS
86#endif
87
88#ifdef WITH_ASE_ORCA
90#endif
91
92#ifdef WITH_ASE_NWCHEM
94#endif
95
96#ifdef WITH_METATOMIC
98#endif
99
100#ifdef WITH_WATER
101#include "rgpot/Water/WaterPots.hpp"
102#endif
103
104// Should respect Fortran availability
105
106#ifdef WITH_XTB
107#include "rgpot/XTBPot/XTBPot.hpp"
108#endif
109
110#include <cmath>
111#include <limits>
112#include <stdexcept>
113
114std::tuple<double, AtomMatrix> eonc::Potential::get_ef(const AtomMatrix &pos,
115 const VectorXi &atmnrs,
116 const Matrix3d &box) {
117 double energy{std::numeric_limits<double>::infinity()};
118 long nAtoms = static_cast<long>(pos.rows());
119 AtomMatrix forces{MatrixXd::Zero(nAtoms, 3)};
120 double var{0}; // no variance for true potentials
121 const auto n = static_cast<size_t>(nAtoms);
122 this->force(std::span<const double>(pos.data(), n * 3),
123 std::span<const int>(atmnrs.data(), n),
124 std::span<double>(forces.data(), n * 3), &energy, &var,
125 std::span<const double>(box.data(), 9));
127 if (!std::isfinite(energy) || !forces.allFinite()) {
128 throw std::runtime_error("Potential::get_ef: non-finite energy or forces");
129 }
130
131 return std::make_tuple(energy, forces);
132}
133
134namespace eonc::helpers {
135namespace {
136
137std::string lower_copy(std::string s) {
138 for (char &c : s) {
139 c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
140 }
141 return s;
142}
143
144// A pair config with [Potential] pair_cutoff and pair_switch_width applied.
145// A cutoff of 0 keeps the config's own. An rgpot older than the C^2 switch
146// has no switch_width member; it accepts only width 0.
147template <class Config> Config pair_config(const Parameters &params) {
148 Config cfg{};
149 const double cutoff = params.potential_options().pairCutoff;
150 if (!(cutoff >= 0.0) || !std::isfinite(cutoff)) {
151 throw std::runtime_error(
152 "[Potential] pair_cutoff must be a finite length, or 0 for the "
153 "potential's own cutoff");
154 }
155 if (cutoff > 0.0) {
156 cfg.cutoff = cutoff;
157 }
158 const double width = params.potential_options().pairSwitchWidth;
159 if constexpr (requires { cfg.switch_width; }) {
160 cfg.switch_width = width;
161 } else if (width != 0.0) {
162 throw std::runtime_error(
163 "[Potential] pair_switch_width needs an rgpot whose pair configs "
164 "carry switch_width");
165 }
166 return cfg;
167}
168
169#ifdef RGPOT_HAS_EXPR
170#ifdef RGPOT_HAS_DFTD3
171rgpot::D3Damping d3_damping_from_params(const Parameters &params) {
172 rgpot::D3Damping damp = rgpot::D3Damping::BJ;
173 if (lower_copy(params.dftd_options().d3_damping) == "zero") {
174 damp = rgpot::D3Damping::Zero;
175 }
176 return damp;
177}
178#endif
179
180std::unique_ptr<rgpot::PotentialBase> make_expr_term(const std::string &raw,
181 const Parameters &params) {
182 const std::string name = lower_copy(raw);
183 if (name == "lj") {
184 return std::make_unique<rgpot::LJPot>(pair_config<rgpot::LJConfig>(params));
185 }
186 if (name == "ljcluster") {
187 return std::make_unique<rgpot::LJClusterPot>(
188 pair_config<rgpot::LJClusterConfig>(params));
189 }
190 if (name == "morse" || name == "morse_pt") {
191 return std::make_unique<rgpot::MorsePot>(
192 pair_config<rgpot::MorseConfig>(params));
193 }
194 if (name == "zbl") {
195 return std::make_unique<rgpot::ZBLPot>(rgpot::ZBLConfig{
196 .cut_inner = params.zbl_options().cut_inner,
197 .cut_global = params.zbl_options().cut_global,
198 });
199 }
200#ifdef RGPOT_HAS_DFTD3
201 if (name == "d3" || name == "dftd3") {
202 return std::make_unique<rgpot::D3Pot>(rgpot::D3Config{
203 .damping = d3_damping_from_params(params),
204 .functional = params.dftd_options().functional,
205 .atm = params.dftd_options().atm,
206 });
207 }
208#endif
209#ifdef RGPOT_HAS_DFTD4
210 if (name == "d4" || name == "dftd4") {
211 return std::make_unique<rgpot::D4Pot>(rgpot::D4Config{
212 .functional = params.dftd_options().functional,
213 .charge = params.dftd_options().d4_charge,
214 .atm = params.dftd_options().atm,
215 });
216 }
217#endif
218 if (name == "mopac") {
219 return std::make_unique<rgpot::MOPACPot>(rgpot::MOPACPot::Config{
220 .charge = params.mopac_options().charge,
221 .spin = params.mopac_options().spin,
222 .model = params.mopac_options().model,
223 .engine_path = params.mopac_options().engine_path,
224 });
225 }
226 throw std::runtime_error(
227 "ExprPot unknown term '" + raw +
228 "' (lj, ljcluster, morse, zbl, d3/dftd3, d4/dftd4, mopac)");
229}
230
231std::vector<rgpot::ExprPot::Term> parse_expr_terms(const Parameters &params) {
232 std::vector<rgpot::ExprPot::Term> terms;
233 std::string buf = params.expr_options().terms;
234 std::string token;
235 auto flush = [&]() {
236 while (!token.empty() &&
237 std::isspace(static_cast<unsigned char>(token.front()))) {
238 token.erase(token.begin());
239 }
240 while (!token.empty() &&
241 std::isspace(static_cast<unsigned char>(token.back()))) {
242 token.pop_back();
243 }
244 if (!token.empty()) {
245 terms.emplace_back(token, make_expr_term(token, params));
246 token.clear();
247 }
248 };
249 for (char c : buf) {
250 if (c == ',') {
251 flush();
252 } else {
253 token.push_back(c);
254 }
255 }
256 flush();
257 if (terms.empty()) {
258 throw std::runtime_error(
259 "ExprPot needs [ExprPot] terms (comma-separated names used in "
260 "expression)");
261 }
262 return terms;
263}
264#endif
265
266} // namespace
267
268std::unique_ptr<Potential> makePotential(const Parameters &params) {
269 return makePotential(params.potential_options().potential, params);
270}
271std::shared_ptr<Potential> makeImagePotential(const Potential &pot,
272 const Parameters &params) {
273 PotentialConstructionScope scope(pot.registry());
274 if (auto cloned = pot.clonePotential()) {
275 return cloned;
276 }
277 return sharePotential(makePotential(params));
278}
279std::unique_ptr<Potential>
280makePotential(PotType ptype, const Parameters &params, Runtime &runtime) {
281 PotentialConstructionScope scope(runtime.pots());
283 return makePotential(ptype, params);
284}
285std::unique_ptr<Potential> makePotential(PotType ptype,
286 const Parameters &params) {
287 // Inject config-file path before any potential constructor runs.
288 // Called on every code path including Job::Job which uses this overload.
289 PluginLoader::instance().add_config_paths(
291 switch (ptype) {
292 // TODO: Every potential must know their own type
293 case PotType::EMT: {
295 PotType::EMT, params,
296 rgpot::EMTConfig{.rasmussen = params.potential_options().EMTRasmussen});
297 break;
298 }
299 case PotType::EXT_POT: {
300 return (std::make_unique<ExtPot>(params));
301 break;
302 }
303 case PotType::LJ: {
305 pair_config<rgpot::LJConfig>(params));
306 break;
307 }
308 case PotType::LJCLUSTER: {
310 PotType::LJCLUSTER, params,
311 pair_config<rgpot::LJClusterConfig>(params));
312 break;
313 }
314 case PotType::MORSE_PT: {
316 pair_config<rgpot::MorseConfig>(params));
317 break;
318 }
319#ifdef CUH2_POT
320 case PotType::CUH2: {
322 break;
323 }
324#endif
325#ifdef WITH_WATER
326 case PotType::TIP4P: {
328 rgpot::WaterConfig{});
329 break;
330 }
331 case PotType::SPCE: {
333 rgpot::WaterConfig{});
334 break;
335 }
336 case PotType::TIP4P_PT: {
338 rgpot::WaterConfig{});
339 break;
340 }
341#endif
342#ifdef WITH_FORTRAN
343 case PotType::TIP4P_H: {
345 params);
346 break;
347 }
348#endif
349 // Fortran potentials: always available, loaded at runtime via dlopen
350 case PotType::EAM_AL: {
352 params);
353 break;
354 }
355 case PotType::EDIP: {
357 break;
358 }
359 case PotType::FEHE: {
361 break;
362 }
363 case PotType::LENOSKY_SI: {
365 params);
366 break;
367 }
368 case PotType::SW_SI: {
370 break;
371 }
372 case PotType::TERSOFF_SI: {
374 params);
375 break;
376 }
377#ifndef _WIN32
378#ifdef WITH_VASP
379 case PotType::VASP: {
380 return (std::make_unique<VASP>(params));
381 break;
382 }
383#endif
384#endif
385 case PotType::LAMMPS: {
386 return std::make_unique<LAMMPSPot>(params);
387 }
388#ifdef EONMPI
389 case PotType::MPI: {
390 return (std::make_unique<MPIPot>(params));
391 break;
392 }
393#endif
394#ifdef EMBED_PYTHON
395#ifdef WITH_ASE_POT
396 case PotType::ASE_POT: {
397 return (std::make_unique<ASE>(params));
398 break;
399 }
400#endif
401#endif
402#ifdef WITH_AMS
403 case PotType::AMS: {
404 return (std::make_unique<AMS>(params));
405 break;
406 }
407 case PotType::AMS_IO: {
408 return (std::make_unique<AMS_IO>(params));
409 break;
410 }
411#endif
412#ifdef WITH_CATLEARN
413 case PotType::CatLearn: {
414 return (std::make_unique<CatLearnPot>(params));
415 break;
416 }
417#endif
418// TODO: Handle Fortran interaction
419#ifdef WITH_XTB
420 case PotType::XTB: {
421 const auto &o = params.xtb_options();
422 rgpot::GFNMethod method = rgpot::GFNMethod::GFN2xTB;
423 if (o.paramset == "GFNFF") {
424 method = rgpot::GFNMethod::GFNFF;
425 } else if (o.paramset == "GFN0xTB") {
426 method = rgpot::GFNMethod::GFN0xTB;
427 } else if (o.paramset == "GFN1xTB") {
428 method = rgpot::GFNMethod::GFN1xTB;
429 } else if (o.paramset == "GFN2xTB") {
430 method = rgpot::GFNMethod::GFN2xTB;
431 } else {
432 throw std::runtime_error(
433 "Parameter set for XTB must be one of GFNFF, GFN0xTB, GFN1xTB or "
434 "GFN2xTB.\n");
435 }
437 PotType::XTB, params,
438 rgpot::XTBConfig{
439 .method = method,
440 .accuracy = o.acc,
441 .electronic_temperature = o.elec_temperature,
442 .max_iterations = static_cast<int>(o.maxiter),
443 .charge = o.charge,
444 .uhf = o.uhf,
445 });
446 break;
447 }
448#endif
449#ifdef WITH_ASE_ORCA
450 case PotType::ASE_ORCA: {
451 return (std::make_unique<ASEOrcaPot>(params));
452 break;
453 }
454#endif
455#ifdef WITH_ASE_NWCHEM
456 case PotType::ASE_NWCHEM: {
457 return (std::make_unique<ASENwchemPot>(params));
458 break;
459 }
460#endif
461#ifdef WITH_METATOMIC
462 case PotType::METATOMIC: {
463 return (std::make_unique<MetatomicPotential>(params));
464 break;
465 }
466#endif
467#ifdef RGPOT_HAS_DFTD3
468 case PotType::DFTD3: {
469 rgpot::D3Damping damp = rgpot::D3Damping::BJ;
470 std::string dname = params.dftd_options().d3_damping;
471 for (char &c : dname) {
472 c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
473 }
474 if (dname == "zero") {
475 damp = rgpot::D3Damping::Zero;
476 }
478 PotType::DFTD3, params,
479 rgpot::D3Config{.damping = damp,
480 .functional = params.dftd_options().functional,
481 .atm = params.dftd_options().atm});
482 break;
483 }
484#endif
485#ifdef RGPOT_HAS_DFTD4
486 case PotType::DFTD4: {
488 PotType::DFTD4, params,
489 rgpot::D4Config{.functional = params.dftd_options().functional,
490 .charge = params.dftd_options().d4_charge,
491 .atm = params.dftd_options().atm});
492 break;
493 }
494#endif
495 case PotType::ZBL: {
497 PotType::ZBL, params,
498 rgpot::ZBLConfig{
499 .cut_inner = params.zbl_options().cut_inner,
500 .cut_global = params.zbl_options().cut_global,
501 });
502 break;
503 }
504#ifndef IS_WINDOWS
506 return (std::make_unique<SocketNWChemPot>(params));
507 break;
508 }
509#endif
510#ifdef WITH_RGPOT
511 case PotType::RGPOT: {
512 return (std::make_unique<RgpotPot>(params));
513 break;
514 }
515#endif
516 case PotType::MOPAC: {
518 PotType::MOPAC, params,
519 rgpot::MOPACPot::Config{
520 .charge = params.mopac_options().charge,
521 .spin = params.mopac_options().spin,
522 .model = params.mopac_options().model,
523 .engine_path = params.mopac_options().engine_path,
524 });
525 break;
526 }
527#ifdef RGPOT_HAS_EXPR
528 case PotType::EXPR: {
529 if (params.expr_options().expression.empty()) {
530 throw std::runtime_error(
531 "ExprPot needs [ExprPot] expression, e.g. 0.5*lj + d3");
532 }
533 rgpot::ExprPot expr(params.expr_options().expression,
534 parse_expr_terms(params));
535 return std::make_unique<RgpotAdapter<rgpot::ExprPot>>(PotType::EXPR, params,
536 std::move(expr));
537 break;
538 }
539#endif
540 default:
541 EONC_LOG_ERROR("No known potential could be constructed from {}",
542 magic_enum::enum_name(ptype));
543 eonc::log::get()->flush_log();
544 throw std::runtime_error("Terminating");
545 break;
546 }
547}
548
549} // namespace eonc::helpers
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
#define EONC_LOG_ERROR(...)
Definition EonLogger.h:328
std::unique_ptr< eonc::Potential > makeRgpotDefault(eonc::PotType ptype, const eonc::Parameters &params)
Factory arm helper for kernels whose parameters are fixed tabulated data with no eOn-side configurati...
std::unique_ptr< eonc::Potential > makeRgpot(eonc::PotType ptype, const eonc::Parameters &params, const Cfg &cfg)
Factory arm helper: construct the kernel from its config and wrap it.
const expr_options_t & expr_options() const
const potential_options_t & potential_options() const
const zbl_options_t & zbl_options() const
const mopac_options_t & mopac_options() const
const dftd_options_t & dftd_options() const
const expr_options_t & expr_options() const
const xtb_options_t & xtb_options() const
const potential_options_t & potential_options() const
const zbl_options_t & zbl_options() const
const mopac_options_t & mopac_options() const
const dftd_options_t & dftd_options() const
void add_config_paths(const std::string &colon_paths) override
Inject search paths from the eOn config file.
static PluginLoader & instance()
Thread-safe singleton accessor (Meyer's pattern).
RAII: Potential default construction records on this registry instead of PotRegistry::get().
Definition Potential.h:33
virtual void force(long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box)=0
std::tuple< double, AtomMatrix > get_ef(const AtomMatrix &pos, const VectorXi &atmnrs, const Matrix3d &box)
void recordForceCall() noexcept
Charge one evaluated geometry to this potential and its owning registry.
Move-only composition root for process resources (dlopen loaders and the potential registry).
Definition Runtime.h:27
PotRegistry & pots() noexcept
Definition Runtime.cpp:33
PluginLoader & plugins() noexcept
Definition Runtime.cpp:39
std::shared_ptr< Potential > makeImagePotential(const Potential &pot, const Parameters &params)
A separate instance of pot for one image of a band, dimer or pair of minimizations: a clone when the ...
std::unique_ptr< Potential > makePotential(const Parameters &params)
Exclusive ownership.
std::shared_ptr< Potential > sharePotential(std::unique_ptr< Potential > pot)
Multi-owner boundary: one instance observed by several Matters, images, or a binding.
Definition Potential.h:292
quill::Logger * get() noexcept
Get or create the default "combi" logger.
Definition EonLogger.h:111
double pairSwitchWidth
Width (Angstrom) of the C^2 quintic switch that takes the lj, ljcluster and morse_pt pair terms to ze...
double pairCutoff
Cutoff (Angstrom) of the lj, ljcluster and morse_pt pair terms.