Loading...
Searching...
No Matches
XTBPot.h
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#pragma once
13
14#include "eon/Parameters.h"
15#include "eon/Potential.h"
16#include "units.hpp"
17#include "xtb.h"
18
19#include <iostream>
20
21class XTBPot final : public eonc::Potential {
22public:
23 // Functions
25 : eonc::Potential(eonc::PotType::XTB, p), xtb_acc{p.xtb_options().acc},
26 xtb_electronic_temperature{p.xtb_options().elec_temperature},
27 xtb_max_iter{p.xtb_options().maxiter},
28 total_charge{p.xtb_options().charge}, uhf{p.xtb_options().uhf} {
29 // Deprecated for packaging: prefer potential=RGPOT
30 // backend=xtb (dlopen libxtb_engine.so). Native -Dwith_xtb stays available.
31 std::cerr
32 << "WARNING: native XTBPot (-Dwith_xtb) is deprecated for packaging; "
33 "prefer potential=RGPOT with [RgpotPot] backend=xtb "
34 "(dlopen libxtb_engine.so; set RGPOT_XTB_ENGINE).\n";
35 counter = 0;
36 initialized = false;
39 // Release the default output unit to prevent Fortran NEWUNIT conflicts
40 // when multiple XTB environments coexist (e.g. per-image NEB potentials)
42 if (!env) {
43 throw std::runtime_error("Failed to create xtb environment");
44 }
46 if (!calc) {
48 throw std::runtime_error("Failed to create xtb calculator");
49 }
51 if (!calc) {
53 throw std::runtime_error("Failed to create xtb results");
54 }
55 // Unmarshal parameters
56 if (p.xtb_options().paramset == "GFNFF") {
58 } else if (p.xtb_options().paramset == "GFN0xTB") {
60 } else if (p.xtb_options().paramset == "GFN1xTB") {
62 } else if (p.xtb_options().paramset == "GFN2xTB") {
64 } else {
65 throw std::runtime_error("Parameter set for XTB must be one of GFNFF, "
66 "GFN0xTB, GFN1xTB or GFN2xTB.\n");
67 }
68 }
69
70 virtual ~XTBPot() {
71 if (res) {
73 }
74 if (calc) {
76 }
77 if (mol) {
79 }
80 if (env) {
83 }
84 QUILL_LOG_INFO(eonc::log::get(), "[XTB] called potential {} times",
85 counter++);
86 }
87
88 // Disable copy to prevent double-free of Fortran pointers
89 XTBPot(const XTBPot &) = delete;
90 XTBPot &operator=(const XTBPot &) = delete;
91
92 // To satisfy interface
93 void cleanMemory(void);
94
95 void force(long N, const double *R, const int *atomicNrs, double *F,
96 double *U, double *variance, const double *box) override;
97
100 [[nodiscard]] bool isThreadSafe() const noexcept override { return false; }
101
105 [[nodiscard]] bool needsPerImageInstance() const noexcept override {
106 return false;
107 }
108
109 [[nodiscard]] bool computesStress() const noexcept override {
110 return haveStress_;
111 }
112
113 [[nodiscard]] Matrix3d cauchyStress() const override { return stress_; }
114
115private:
120 xtb_TResults res = nullptr;
121
123 double xtb_acc;
126 double total_charge = 0.0;
127 int uhf = 0;
128
129 size_t counter;
131 Matrix3d stress_{Matrix3d::Zero()};
132 bool haveStress_{false};
133};
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
double xtb_acc
Definition XTBPot.h:123
xtb_TEnvironment env
Definition XTBPot.h:117
double xtb_electronic_temperature
Definition XTBPot.h:124
Matrix3d cauchyStress() const override
Cauchy stress in eV/Angstrom^3.
Definition XTBPot.h:113
bool isThreadSafe() const noexcept override
XTB Fortran library uses per-instance state (env/calc).
Definition XTBPot.h:100
virtual ~XTBPot()
Definition XTBPot.h:70
bool haveStress_
Definition XTBPot.h:132
GFNMethod
Definition XTBPot.h:116
bool computesStress() const noexcept override
True when force() leaves a Cauchy stress that cauchyStress() can read until the next force() on this ...
Definition XTBPot.h:109
XTBPot(const eonc::Parameters &p)
Definition XTBPot.h:24
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
Definition XTBPot.cpp:28
bool needsPerImageInstance() const noexcept override
XTB restart.f90 uses global Fortran file units that collide when multiple environments exist in one p...
Definition XTBPot.h:105
xtb_TMolecule mol
Definition XTBPot.h:119
bool initialized
Definition XTBPot.h:130
double total_charge
Definition XTBPot.h:126
XTBPot(const XTBPot &)=delete
size_t counter
Definition XTBPot.h:129
size_t xtb_max_iter
Definition XTBPot.h:125
xtb_TResults res
Definition XTBPot.h:120
int uhf
Definition XTBPot.h:127
Matrix3d stress_
Definition XTBPot.h:131
xtb_TCalculator calc
Definition XTBPot.h:118
GFNMethod xtb_paramset
Definition XTBPot.h:122
XTBPot & operator=(const XTBPot &)=delete
void cleanMemory(void)
const xtb_options_t & xtb_options() const
Potential(PotType a_ptype)
Production default: construction-scope registry, else PotRegistry::get().
quill::Logger * get() noexcept
Get or create the default "combi" logger.
Definition EonLogger.h:44
RAII resource manager for the ARTn C library with global synchronization.
Physical constants, unit conversion.
XTB_API_ENTRY xtb_TCalculator XTB_API_CALL xtb_newCalculator(void) XTB_API_SUFFIX__VERSION_2_0_0
Create new calculator object.
struct _xtb_TCalculator * xtb_TCalculator
Single point calculator class.
Definition xtb.h:61
struct _xtb_TEnvironment * xtb_TEnvironment
Calculation environment class.
Definition xtb.h:55
XTB_API_ENTRY void XTB_API_CALL xtb_delMolecule(xtb_TMolecule *) XTB_API_SUFFIX__VERSION_2_0_0
Delete molecular structure data.
XTB_API_ENTRY void XTB_API_CALL xtb_delResults(xtb_TResults *) XTB_API_SUFFIX__VERSION_2_0_0
Delete singlepoint results object.
struct _xtb_TMolecule * xtb_TMolecule
Molecular structure data class.
Definition xtb.h:58
XTB_API_ENTRY xtb_TEnvironment XTB_API_CALL xtb_newEnvironment(void) XTB_API_SUFFIX__VERSION_2_0_0
Create new xtb calculation environment object.
#define XTB_VERBOSITY_MUTED
Definition xtb.h:34
XTB_API_ENTRY void XTB_API_CALL xtb_setVerbosity(xtb_TEnvironment, int) XTB_API_SUFFIX__VERSION_2_0_0
Set verbosity of calculation output.
XTB_API_ENTRY void XTB_API_CALL xtb_releaseOutput(xtb_TEnvironment) XTB_API_SUFFIX__VERSION_2_0_0
Release output unit from this environment.
XTB_API_ENTRY void XTB_API_CALL xtb_delEnvironment(xtb_TEnvironment *) XTB_API_SUFFIX__VERSION_2_0_0
Delete a xtb calculation environment object.
XTB_API_ENTRY xtb_TResults XTB_API_CALL xtb_newResults(void) XTB_API_SUFFIX__VERSION_2_0_0
Create new singlepoint results object.
struct _xtb_TResults * xtb_TResults
Single point results class.
Definition xtb.h:64
XTB_API_ENTRY void XTB_API_CALL xtb_delCalculator(xtb_TCalculator *) XTB_API_SUFFIX__VERSION_2_0_0
Delete calculator object.