n2p2 - A neural network potential package
EwaldTruncKolafaOptEta.cpp
Go to the documentation of this file.
1//
2// Created by philipp on 2/17/23.
3//
4
6#include <cmath>
7#include <algorithm>
8
9using namespace std;
10
11namespace nnp
12{
13 // Ratio of computing times for one real space and k space iteration.
14 double constexpr TrOverTk = 3.676;
15
17 const EwaldGlobalSettings &settings,
18 const EwaldStructureData &sData, EwaldParameters &params)
19 {
20 size_t N = sData.getNumAtoms();
21 double V = sData.getVolume();
22 newCutoffs = (volume != V || numAtoms != N);
23 if (!newCutoffs) return;
25
26 volume = V;
27 numAtoms = N;
28 prec = settings.precision;
29 fourPiEps = settings.fourPiEps;
30
31 calculateC(settings.maxCharge);
33
34 eta = std::max(eta,settings.maxQSigma);
35 params.eta = eta;
36 params.rCut = calculateRCut();
37 params.kCut = calculateKCut();
38 }
40 {
41 bool answer = newCutoffsWerePublished;
43 return answer;
44 }
45
47 {
48 double constexpr acceptError = 1.e-10;
49
50 // Initial approximation
51 double eta0 = pow(1 / TrOverTk * pow(volume, 2.0)
52 / pow(2 * M_PI, 3.0), 1.0 / 6.0);
53
54 // Selfconsistent calculation of eta
55 eta = eta0;
56 double oldEta;
57 double relError = 1.0;
58 s = 1.0;
59 while (relError > acceptError)
60 {
61 calculateS();
62 oldEta = eta;
63 eta = eta0 * pow((1 + 1 / (2 * pow(s, 2))), 1.0 / 6.0);
64 relError = abs(oldEta - eta) / eta;
65 }
66 }
68 {
69 return sqrt(2)*eta*s;
70 }
71
73 {
74 return sqrt(2)*s / eta;
75 }
76
78 {
79 double constexpr acceptError = 1.e-10;
80 double relXError = 1.0;
81 double relYError = 1.0;
82 double y = prec / C * sqrt(eta/2);
83
84 if (s <= 0.0) s = 0.5;
85
86 double step;
87 while (relXError > acceptError || relYError > acceptError)
88 {
89 step = 2*s / (4*pow(s,2.0) + 1)
90 * (1 - sqrt(s) / exp(-pow(s,2.0)) * y);
91
92 // If s would become negative, try smaller start value.
93 if (s <= -step)
94 {
95 s /= 2;
96 step = 1.0;
97 continue;
98 }
99 s += step;
100 relYError = abs((exp(-pow(s,2.0)) / sqrt(s) - y) / y);
101 relXError = abs(step/s);
102 }
103 }
104
106 {
107 C = pow(2, 3.0/4) * pow(qMax,2.0) * sqrt(numAtoms/volume) / fourPiEps;
108 }
109} // nnp
double getVolume() const
Definition: IEwaldTrunc.h:30
std::size_t getNumAtoms() const
Definition: IEwaldTrunc.h:31
void calculateC(double const qMax)
void calculateParameters(EwaldGlobalSettings const &settings, EwaldStructureData const &sData, EwaldParameters &params) override
Definition: Atom.h:29
double constexpr TrOverTk
double fourPiEps
Multiplicative constant .
Definition: IEwaldTrunc.h:19
double rCut
Cutoff in real space.
Definition: IEwaldTrunc.h:42
double eta
Width of the gaussian screening charges.
Definition: IEwaldTrunc.h:40
double kCut
Cutoff in reciprocal space.
Definition: IEwaldTrunc.h:44