n2p2 - A neural network potential package
EwaldTruncKolafaFixR.cpp
Go to the documentation of this file.
1//
2// Created by philipp on 2/17/23.
3//
4
6#include <stdexcept>
7
8using namespace std;
9
10namespace nnp
11{
13 EwaldStructureData const& sData,
14 EwaldParameters &params)
15 {
16 size_t N = sData.getNumAtoms();
17 double V = sData.getVolume();
18
19 newCutoffs = (V != volume || N != numAtoms);
20 if (!newCutoffs) return;
22
23 volume = V;
24 fourPiEps = settings.fourPiEps;
25 numAtoms = N;
26 double rCut = params.rCut;
27 if (rCut == 0.0)
28 throw std::runtime_error("ERROR: Real space cutoff of Ewald "
29 "summation cannot be zero.");
30
31 calculateC(settings.maxCharge);
32 params.eta = calculateEta(rCut, settings.precision);
33 params.kCut = calculateKCut(params.eta, settings.precision);
34 return;
35 }
37 {
38 bool answer = newCutoffsWerePublished;
40 return answer;
41 }
43 EwaldGlobalSettings const& settings,
44 EwaldParameters const& params) const
45 {
46 return params.eta >= settings.maxQSigma;
47 }
48
49 double EwaldTruncKolafaFixR::calculateEta(double const rCut,
50 double const prec) const
51 {
52 double x = sqrt(rCut/2) * prec / C;
53 if (x >= 1.0)
54 throw runtime_error("ERROR: Bad Ewald precision settings, try a "
55 "smaller value.");
56 return rCut / sqrt(-2 * log(x));
57 }
58
59 double EwaldTruncKolafaFixR::calculateKCut(double const eta,
60 double const prec) const
61 {
62 double constexpr acceptError = 1.e-10;
63 double relXError = 1.0;
64 double relYError = 1.0;
65 double y = eta * prec / (sqrt(2) * C);
66
67 double kCut = 1.0/eta;
68 double kCutOld;
69 while (relXError > acceptError || relYError > acceptError)
70 {
71 kCutOld = kCut;
72 kCut -= 2 * kCut / (1 + 2*pow(kCut * eta,2.0))
73 * (-1 + sqrt(kCut) * exp(pow(kCut * eta,2.0)/2) * y);
74 // If kCut becomes negative, start with smaller value.
75 if (kCut <= 0)
76 {
77 kCut = kCutOld / 2;
78 continue;
79 }
80
81 relXError = abs((kCut - kCutOld) / kCut);
82 relYError = abs((1/sqrt(kCut)
83 * exp(-pow(kCut * eta,2.0) / 2) - y) / y);
84 }
85 return kCut;
86 }
87
88 void EwaldTruncKolafaFixR::calculateC(const double qMax)
89 {
90 C = 2 * pow(qMax,2.0) * sqrt(numAtoms/volume) / fourPiEps;
91 }
92
93} // nnp
double getVolume() const
Definition: IEwaldTrunc.h:30
std::size_t getNumAtoms() const
Definition: IEwaldTrunc.h:31
void calculateC(double const qMax)
double calculateEta(double const rCut, double const prec) const
double calculateKCut(double const eta, double const prec) const
void calculateParameters(EwaldGlobalSettings const &settings, EwaldStructureData const &sData, EwaldParameters &params) override
bool isEstimateReliable(EwaldGlobalSettings const &settings, EwaldParameters const &params) const override
Definition: Atom.h:29
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