Loading...
Searching...
No Matches
XTBPot.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
14#include <cmath>
15#include <cstddef>
16#include <vector>
17
18// Conversion factors
19// const double angstromToBohr = 1.8897261349925714;
20// const double hartreeToEV = 27.21138386;
21// const double hartreeBohr_to_eVA = 14.399645472115932;
24
25// pointer to number of atoms, pointer to array of positions
26// pointer to array of forces, pointer to internal energy
27// address to supercell size
28void XTBPot::force(long N, const double *R, const int *atomicNrs, double *F,
29 double *U, double *variance, const double *box) {
30 variance = nullptr;
31 int intN = static_cast<int>(N);
32 const bool periodicity[3]{std::abs(box[0]) > 1e-8, std::abs(box[4]) > 1e-8,
33 std::abs(box[8]) > 1e-8};
34 double box_bohr[3 * 3];
35
36 // Allocate memory for converted positions
37 std::vector<double> R_bohr(3 * N);
38
39 // Convert positions from Angstrom to Bohr
40 for (long idx = 0; idx < 3 * N; ++idx) {
41 R_bohr[idx] = R[idx] / BOHR;
42 }
43 for (long idx = 0; idx < 9; ++idx) {
44 box_bohr[idx] = box[idx] / BOHR;
45 }
46
47 if (!initialized) {
48 // First call: Create the molecule and load the Hamiltonian
49 mol = xtb_newMolecule(env, &intN, atomicNrs, R_bohr.data(), &total_charge,
50 &uhf, box_bohr, periodicity);
51
52 switch (xtb_paramset) {
54 xtb_loadGFNFF(env, mol, calc, nullptr);
55 break;
57 xtb_loadGFN0xTB(env, mol, calc, nullptr);
58 break;
60 xtb_loadGFN1xTB(env, mol, calc, nullptr);
61 break;
63 xtb_loadGFN2xTB(env, mol, calc, nullptr);
64 break;
65 }
66
70 initialized = true;
71 } else {
72 // Subsequent calls: Only update coordinates and lattice
73 xtb_updateMolecule(env, mol, R_bohr.data(), box_bohr);
74 }
75
77
78 // Check for SCF convergence or internal xTB errors
79 if (xtb_checkEnvironment(env) != 0) {
80 char err_msg[512];
81 xtb_getError(env, err_msg, nullptr);
82 throw std::runtime_error(std::string("xTB Error: ") + err_msg);
83 }
84
87
88 // Convert Hartree/Bohr to eV/Angstrom
89 for (long i = 0; i < 3 * N; ++i) {
90 F[i] *= -1.0 * (HARTREE / BOHR);
91 }
92 *U *= HARTREE;
93
94 // xtb's virial is dE/dε in Hartree, column-major. sigma = virial / V.
95 double virial[9] = {};
96 xtb_getVirial(env, res, virial);
97 const double volume = std::abs(Matrix3d::Map(box).determinant());
98 stress_.setZero();
99 haveStress_ = volume > 0.0;
100 if (haveStress_) {
101 const double scale = HARTREE / volume;
102 for (int col = 0; col < 3; ++col) {
103 for (int row = 0; row < 3; ++row) {
104 const double vij =
105 0.5 * (virial[row + 3 * col] + virial[col + 3 * row]);
106 stress_(row, col) = vij * scale;
107 }
108 }
109 }
110 counter++;
111}
const double HARTREE
const double BOHR
double xtb_acc
Definition XTBPot.h:123
xtb_TEnvironment env
Definition XTBPot.h:117
double xtb_electronic_temperature
Definition XTBPot.h:124
bool haveStress_
Definition XTBPot.h:132
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
Definition XTBPot.cpp:28
xtb_TMolecule mol
Definition XTBPot.h:119
bool initialized
Definition XTBPot.h:130
double total_charge
Definition XTBPot.h:126
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
XTB_API_ENTRY void XTB_API_CALL xtb_getGradient(xtb_TEnvironment, xtb_TResults, double *) XTB_API_SUFFIX__VERSION_2_0_0
Query singlepoint results object for gradient in Hartree / Bohr.
XTB_API_ENTRY xtb_TMolecule XTB_API_CALL xtb_newMolecule(xtb_TEnvironment, const int *, const int *, const double *, const double *, const int *, const double *, const bool *) XTB_API_SUFFIX__VERSION_2_0_0
Create new molecular structure data (quantities in Bohr).
XTB_API_ENTRY void XTB_API_CALL xtb_getEnergy(xtb_TEnvironment, xtb_TResults, double *) XTB_API_SUFFIX__VERSION_2_0_0
Query singlepoint results object for energy in Hartree.
XTB_API_ENTRY void XTB_API_CALL xtb_loadGFN0xTB(xtb_TEnvironment, xtb_TMolecule, xtb_TCalculator, char *) XTB_API_SUFFIX__VERSION_2_0_0
Load GFN0-xTB calculator.
XTB_API_ENTRY int XTB_API_CALL xtb_checkEnvironment(xtb_TEnvironment) XTB_API_SUFFIX__VERSION_2_0_0
Check current status of calculation environment.
XTB_API_ENTRY void XTB_API_CALL xtb_updateMolecule(xtb_TEnvironment, xtb_TMolecule, const double *, const double *) XTB_API_SUFFIX__VERSION_2_0_0
Update coordinates and lattice parameters (quantities in Bohr).
XTB_API_ENTRY void XTB_API_CALL xtb_setElectronicTemp(xtb_TEnvironment, xtb_TCalculator, double) XTB_API_SUFFIX__VERSION_2_0_0
Set electronic temperature for level filling in tight binding calculators in K.
XTB_API_ENTRY void XTB_API_CALL xtb_loadGFNFF(xtb_TEnvironment, xtb_TMolecule, xtb_TCalculator, char *) XTB_API_SUFFIX__VERSION_2_0_0
Load GFN-FF calculator.
XTB_API_ENTRY void XTB_API_CALL xtb_loadGFN1xTB(xtb_TEnvironment, xtb_TMolecule, xtb_TCalculator, char *) XTB_API_SUFFIX__VERSION_2_0_0
Load GFN1-xTB calculator.
XTB_API_ENTRY void XTB_API_CALL xtb_setAccuracy(xtb_TEnvironment, xtb_TCalculator, double) XTB_API_SUFFIX__VERSION_2_0_0
Set numerical accuracy of calculator in the range of 1000 to 0.0001.
XTB_API_ENTRY void XTB_API_CALL xtb_setMaxIter(xtb_TEnvironment, xtb_TCalculator, int) XTB_API_SUFFIX__VERSION_2_0_0
Set maximum number of iterations for self-consistent TB calculators.
XTB_API_ENTRY void XTB_API_CALL xtb_loadGFN2xTB(xtb_TEnvironment, xtb_TMolecule, xtb_TCalculator, char *) XTB_API_SUFFIX__VERSION_2_0_0
Load GFN2-xTB calculator.
XTB_API_ENTRY void XTB_API_CALL xtb_getVirial(xtb_TEnvironment, xtb_TResults, double *) XTB_API_SUFFIX__VERSION_2_0_0
Query singlepoint results object for virial in Hartree.
XTB_API_ENTRY void XTB_API_CALL xtb_singlepoint(xtb_TEnvironment, xtb_TMolecule, xtb_TCalculator, xtb_TResults) XTB_API_SUFFIX__VERSION_2_0_0
Perform singlepoint calculation.
XTB_API_ENTRY void XTB_API_CALL xtb_getError(xtb_TEnvironment, char *, const int *) XTB_API_SUFFIX__VERSION_2_0_0
Return and empty error stack.