n2p2 - A neural network potential package
nnp-predict.cpp File Reference
#include "Atom.h"
#include "Prediction.h"
#include "Structure.h"
#include "utility.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
Include dependency graph for nnp-predict.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 30 of file nnp-predict.cpp.

31{
32 bool structureInfo = false;
33
34 if (argc != 2)
35 {
36 cout << "USAGE: " << argv[0] << " <info>\n"
37 << " <info> ... Write structure information for debugging "
38 "to \"structure.out (0/1)\".\n"
39 << " Execute in directory with these NNP files present:\n"
40 << " - input.data (structure file)\n"
41 << " - input.nn (NNP settings)\n"
42 << " - scaling.data (symmetry function scaling data)\n"
43 << " - \"weights.%%03d.data\" (weights files)\n";
44 return 1;
45 }
46
47 structureInfo = (bool)atoi(argv[1]);
48
49 ofstream logFile;
50 logFile.open("nnp-predict.log");
51 Prediction prediction;
53 prediction.setup();
54 prediction.log << "\n";
55 prediction.log << "*** PREDICTION **************************"
56 "**************************************\n";
57 prediction.log << "\n";
58 prediction.log << "Reading structure file...\n";
59 prediction.readStructureFromFile("input.data");
60 Structure& s = prediction.structure;
61 prediction.log << strpr("Structure contains %d atoms (%d elements).\n",
63 prediction.log << "Calculating NNP prediction...\n";
64 prediction.predict();
65 prediction.log << "\n";
66 prediction.logEwaldCutoffs();
67 prediction.log << "-----------------------------------------"
68 "--------------------------------------\n";
69 prediction.log << strpr("NNP total energy: %16.8E\n",
70 prediction.structure.energy);
71 if (prediction.getNnpType() == Mode::NNPType::HDNNP_4G)
72 {
73 prediction.log << strpr("NNP electrostatic energy: %16.8E\n",
74 prediction.structure.energyElec);
75 prediction.log << "\n";
76 prediction.log << "NNP charges:\n";
77 for (auto const& a : s.atoms)
78 {
79 prediction.log << strpr("%10zu %2s %16.8E\n",
80 a.index + 1,
81 prediction.elementMap[a.element].c_str(),
82 a.charge);
83 }
84 prediction.log << strpr("NNP total charge: %16.8E (ref: %16.8E)\n",
85 s.charge, s.chargeRef);
86 }
87 prediction.log << "\n";
88 prediction.log << "NNP forces:\n";
89 for (vector<Atom>::const_iterator it = s.atoms.begin();
90 it != s.atoms.end(); ++it)
91 {
92 prediction.log << strpr("%10zu %2s %16.8E %16.8E %16.8E\n",
93 it->index + 1,
94 prediction.elementMap[it->element].c_str(),
95 it->element,
96 it->f[0],
97 it->f[1],
98 it->f[2]);
99 }
100 prediction.log << "-----------------------------------------"
101 "--------------------------------------\n";
102 prediction.log << "Writing output files...\n";
103 prediction.log << " - energy.out\n";
104 ofstream file;
105 file.open("energy.out");
106
107 // File header.
108 vector<string> title;
109 vector<string> colName;
110 vector<string> colInfo;
111 vector<size_t> colSize;
112 title.push_back("Energy comparison.");
113 colSize.push_back(10);
114 colName.push_back("conf");
115 colInfo.push_back("Configuration index (starting with 1).");
116 colSize.push_back(10);
117 colName.push_back("natoms");
118 colInfo.push_back("Number of atoms in configuration.");
119 colSize.push_back(24);
120 colName.push_back("Eref");
121 colInfo.push_back("Reference potential energy.");
122 colSize.push_back(24);
123 colName.push_back("Ennp");
124 colInfo.push_back("Potential energy predicted by NNP.");
125 colSize.push_back(24);
126 colName.push_back("Ediff");
127 colInfo.push_back("Difference in energy per atom between reference and "
128 "NNP prediction.");
129 colSize.push_back(24);
130 colName.push_back("E_offset");
131 colInfo.push_back("Sum of atomic offset energies "
132 "(included in column Ennp).");
134 createFileHeader(title, colSize, colName, colInfo));
135
136 file << strpr("%10zu %10zu %24.16E %24.16E %24.16E %24.16E\n",
137 s.index + 1,
138 s.numAtoms,
139 s.energyRef,
140 s.energy,
141 (s.energyRef - s.energy) / s.numAtoms,
142 prediction.getEnergyOffset(s));
143 file.close();
144
145 prediction.log << " - nnatoms.out\n";
146 file.open("nnatoms.out");
147
148 // File header.
149 title.clear();
150 colName.clear();
151 colInfo.clear();
152 colSize.clear();
153 title.push_back("Energy contributions calculated from NNP.");
154 colSize.push_back(10);
155 colName.push_back("conf");
156 colInfo.push_back("Configuration index (starting with 1).");
157 colSize.push_back(10);
158 colName.push_back("index");
159 colInfo.push_back("Atom index (starting with 1).");
160 colSize.push_back(3);
161 colName.push_back("Z");
162 colInfo.push_back("Nuclear charge of atom.");
163 colSize.push_back(24);
164 colName.push_back("Qref");
165 colInfo.push_back("Reference atomic charge.");
166 colSize.push_back(24);
167 colName.push_back("Qnnp");
168 colInfo.push_back("NNP atomic charge.");
169 colSize.push_back(24);
170 colName.push_back("Eref_atom");
171 colInfo.push_back("Reference atomic energy contribution.");
172 colSize.push_back(24);
173 colName.push_back("Ennp_atom");
174 colInfo.push_back("Atomic energy contribution (physical units, no mean "
175 "or offset energy added).");
177 createFileHeader(title, colSize, colName, colInfo));
178
179 for (vector<Atom>::const_iterator it = s.atoms.begin();
180 it != s.atoms.end(); ++it)
181 {
182 file << strpr("%10zu %10zu %3zu %24.16E %24.16E %24.16E %24.16E\n",
183 s.index + 1,
184 it->index + 1,
185 prediction.elementMap.atomicNumber(it->element),
186 it->chargeRef,
187 it->charge,
188 0.0,
189 it->energy);
190 }
191 file.close();
192
193 prediction.log << " - nnforces.out\n";
194 file.open("nnforces.out");
195
196 // File header.
197 title.clear();
198 colName.clear();
199 colInfo.clear();
200 colSize.clear();
201 title.push_back("Atomic force comparison (ordered by atom index).");
202 colSize.push_back(10);
203 colName.push_back("conf");
204 colInfo.push_back("Configuration index (starting with 1).");
205 colSize.push_back(10);
206 colName.push_back("index");
207 colInfo.push_back("Atom index (starting with 1).");
208 colSize.push_back(24);
209 colName.push_back("fxRef");
210 colInfo.push_back("Reference force in x direction.");
211 colSize.push_back(24);
212 colName.push_back("fyRef");
213 colInfo.push_back("Reference force in y direction.");
214 colSize.push_back(24);
215 colName.push_back("fzRef");
216 colInfo.push_back("Reference force in z direction.");
217 colSize.push_back(24);
218 colName.push_back("fx");
219 colInfo.push_back("Force in x direction.");
220 colSize.push_back(24);
221 colName.push_back("fy");
222 colInfo.push_back("Force in y direction.");
223 colSize.push_back(24);
224 colName.push_back("fz");
225 colInfo.push_back("Force in z direction.");
227 createFileHeader(title, colSize, colName, colInfo));
228
229 for (vector<Atom>::const_iterator it = s.atoms.begin();
230 it != s.atoms.end(); ++it)
231 {
232 file << strpr("%10zu %10zu %24.16E %24.16E %24.16E %24.16E %24.16E "
233 "%24.16E\n",
234 s.index + 1,
235 it->index + 1,
236 it->fRef[0],
237 it->fRef[1],
238 it->fRef[2],
239 it->f[0],
240 it->f[1],
241 it->f[2]);
242 }
243 file.close();
244
245 prediction.log << "Writing structure with NNP prediction "
246 "to \"output.data\".\n";
247 file.open("output.data");
248 prediction.structure.writeToFile(&file, false);
249 file.close();
250
251 if (structureInfo)
252 {
253 prediction.log << "Writing detailed structure information to "
254 "\"structure.out\".\n";
255 file.open("structure.out");
256 vector<string> info = prediction.structure.info();
257 appendLinesToFile(file, info);
258 file.close();
259 }
260
261 prediction.log << "Finished.\n";
262 prediction.log << "*****************************************"
263 "**************************************\n";
264 logFile.close();
265
266 return 0;
267}
std::size_t atomicNumber(std::size_t index) const
Get atomic number from element index.
Definition: ElementMap.h:145
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
NNPType getNnpType() const
Getter for Mode::nnpType.
Definition: Mode.h:668
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:591
void logEwaldCutoffs()
Logs Ewald params whenever they change.
Definition: Mode.cpp:2149
Log log
Global log file.
Definition: Mode.h:593
double getEnergyOffset(Structure const &structure) const
Get atomic energy offset for given structure.
Definition: Mode.cpp:2056
void readStructureFromFile(std::string const &fileName="input.data")
Definition: Prediction.cpp:48
Structure structure
Definition: Prediction.h:39
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
vector< string > createFileHeader(vector< string > const &title, vector< size_t > const &colSize, vector< string > const &colName, vector< string > const &colInfo, char const &commentChar)
Definition: utility.cpp:111
void appendLinesToFile(ofstream &file, vector< string > const lines)
Append multiple lines of strings to open file stream.
Definition: utility.cpp:225
ofstream logFile
Definition: nnp-cutoff.cpp:29
Storage for one atomic configuration.
Definition: Structure.h:39
void writeToFile(std::string const fileName="output.data", bool const ref=true, bool const append=false) const
Write configuration to file.
Definition: Structure.cpp:1449
double charge
Charge determined by neural network potential.
Definition: Structure.h:96
std::size_t index
Index number of this structure.
Definition: Structure.h:73
double chargeRef
Reference charge.
Definition: Structure.h:98
double energyElec
Electrostatics part of the potential energy predicted by NNP.
Definition: Structure.h:91
double energy
Potential energy determined by neural network.
Definition: Structure.h:83
double energyRef
Reference potential energy.
Definition: Structure.h:85
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:75
std::size_t numElements
Global number of elements (all structures).
Definition: Structure.h:77
std::vector< Atom > atoms
Vector of all atoms in this structure.
Definition: Structure.h:122
std::vector< std::string > info() const
Get structure information as a vector of strings.
Definition: Structure.cpp:1641

References nnp::appendLinesToFile(), nnp::ElementMap::atomicNumber(), nnp::Structure::atoms, nnp::Structure::charge, nnp::Structure::chargeRef, nnp::createFileHeader(), nnp::Mode::elementMap, nnp::Structure::energy, nnp::Structure::energyElec, nnp::Structure::energyRef, nnp::Mode::getEnergyOffset(), nnp::Mode::getNnpType(), nnp::Structure::index, nnp::Structure::info(), nnp::Mode::log, nnp::Mode::logEwaldCutoffs(), logFile, nnp::Structure::numAtoms, nnp::Structure::numElements, nnp::Prediction::predict(), nnp::Prediction::readStructureFromFile(), nnp::Log::registerStreamPointer(), nnp::Prediction::setup(), nnp::strpr(), nnp::Prediction::structure, and nnp::Structure::writeToFile().

Here is the call graph for this function: