n2p2 - A neural network potential package
EwaldSetup.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include "EwaldSetup.h"
20#include "EwaldTruncJackson.h"
21#include "utility.h"
22#include <stdexcept> // std::runtime_error
23#include <memory> // std::make_unique
24
25using namespace nnp;
26using namespace std;
27
28
29EwaldSetup::EwaldSetup() : params ( ),
30 truncMethod(EWALDTruncMethod::JACKSON_CATLOW),
31 GlobSett ( ),
32 truncImpl (nullptr )
33{
34}
35
36void EwaldSetup::readFromArgs(vector<string> const& args)
37{
38 bool argConsistent = false;
39 if (truncMethod ==
41 argConsistent = (args.size() == 1);
42 else
43 argConsistent = (args.size() >= 1 && args.size() <= 3);
44
45 if (!argConsistent)
46 throw runtime_error("ERROR: Number of arguments for ewald_prec is "
47 "not consistent with "
48 "ewald_truncation_error_method.");
49
50 GlobSett.precision = atof(args.at(0).c_str());
51 if (GlobSett.precision <= 0.0)
52 throw runtime_error("ERROR: Ewald precision must be positive.");
53
55 {
56 truncImpl.reset(new EwaldTruncJackson{});
57 }
58 else
59 {
60 if (args.size() >= 2)
61 GlobSett.maxCharge = atof(args.at(1).c_str());
62 if (args.size() == 2)
63 {
65 }
66 if (args.size() == 3)
67 {
68 params.rCut = atof(args.at(2).c_str());
70 }
71 }
72}
73
74void EwaldSetup::toNormalizedUnits(double const convEnergy,
75 double const convLength)
76{
78 {
79 // in KOLAFA_PERRAM method precision has unit of a force.
80 GlobSett.precision *= convEnergy / convLength;
81 GlobSett.fourPiEps /= convLength * convEnergy;
82 // GlobSett.maxQSigma is already normalized
83 params = params.toNormalizedUnits(convLength);
84 // Other variables are already normalized.
85 }
86}
87
88void EwaldSetup::calculateParameters(double const volume, size_t const numAtoms)
89{
90 if (truncImpl == nullptr)
91 throw runtime_error("ERROR: Undefined Ewald truncation method.");
92 EwaldStructureData sData{volume, numAtoms};
93 truncImpl->calculateParameters(GlobSett, sData, params);
94}
95void EwaldSetup::logEwaldCutoffs(Log& log, double const lengthConversion) const
96{
98 {
99 EwaldParameters ewaldParams = params.toPhysicalUnits(lengthConversion);
100 log << "-----------------------------------------"
101 "--------------------------------------\n";
102 log << "Ewald parameters for this structure changed:\n";
103 log << strpr("Real space cutoff: %16.8E\n",
104 ewaldParams.rCut);
105 log << strpr("Reciprocal space cutoff: %16.8E\n",
106 ewaldParams.kCut);
107 log << strpr("Ewald screening parameter: %16.8E\n",
108 ewaldParams.eta);
109 if (!isEstimateReliable())
110 log << strpr("WARNING: Ewald truncation error estimate may not be "
111 "reliable, better compare it\n"
112 " with high accuracy settings.\n");
113 }
114}
116{
117 return truncImpl->publishedNewCutoffs();
118}
120{
121 return truncImpl->isEstimateReliable(GlobSett, params);
122}
EwaldParameters params
Definition: EwaldSetup.h:39
void toNormalizedUnits(double const convEnergy, double const convLength)
Convert cutoff parameters to normalized units.
Definition: EwaldSetup.cpp:74
EWALDTruncMethod truncMethod
Method for determining real and k space cutoffs.
Definition: EwaldSetup.h:79
void calculateParameters(double const newVolume, size_t const newNumAtoms)
Compute eta, rCut and kCut.
Definition: EwaldSetup.cpp:88
std::unique_ptr< IEwaldTrunc > truncImpl
Definition: EwaldSetup.h:82
bool publishedNewCutoffs() const
Definition: EwaldSetup.cpp:115
void logEwaldCutoffs(Log &log, double const lengthConversion) const
Use after Ewald summation!
Definition: EwaldSetup.cpp:95
EwaldGlobalSettings GlobSett
Definition: EwaldSetup.h:80
bool isEstimateReliable() const
Definition: EwaldSetup.cpp:119
void readFromArgs(std::vector< std::string > const &args)
Setup parameters from argument vector.
Definition: EwaldSetup.cpp:36
Logging class for library output.
Definition: Log.h:34
Definition: Atom.h:29
EWALDTruncMethod
Definition: EwaldSetup.h:29
@ KOLAFA_PERRAM
Method 1: Optimized in n2p2 (DOI: 10.1080/08927029208049126).
@ JACKSON_CATLOW
Method 0: Used by RuNNer (DOI: 10.1080/08927028808080944).
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
double fourPiEps
Multiplicative constant .
Definition: IEwaldTrunc.h:19
EwaldParameters toPhysicalUnits(double const convLength) const
Definition: IEwaldTrunc.h:51
double rCut
Cutoff in real space.
Definition: IEwaldTrunc.h:42
double eta
Width of the gaussian screening charges.
Definition: IEwaldTrunc.h:40
EwaldParameters toNormalizedUnits(double const convLength) const
Definition: IEwaldTrunc.h:57
double kCut
Cutoff in reciprocal space.
Definition: IEwaldTrunc.h:44