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.log << "-----------------------------------------"
67 "--------------------------------------\n";
68 prediction.log << strpr("NNP energy: %16.8E\n",
69 prediction.structure.energy);
70 prediction.log << "\n";
71 prediction.log << "NNP forces:\n";
72 for (vector<Atom>::const_iterator it = s.atoms.begin();
73 it != s.atoms.end(); ++it)
74 {
75 prediction.log << strpr("%10zu %2s %16.8E %16.8E %16.8E\n",
76 it->index + 1,
77 prediction.elementMap[it->element].c_str(),
78 it->element,
79 it->f[0],
80 it->f[1],
81 it->f[2]);
82 }
83 prediction.log << "-----------------------------------------"
84 "--------------------------------------\n";
85 prediction.log << "Writing output files...\n";
86 prediction.log << " - energy.out\n";
87 ofstream file;
88 file.open("energy.out");
89
90 // File header.
91 vector<string> title;
92 vector<string> colName;
93 vector<string> colInfo;
94 vector<size_t> colSize;
95 title.push_back("Energy comparison.");
96 colSize.push_back(16);
97 colName.push_back("Ennp");
98 colInfo.push_back("Potential energy predicted by NNP.");
99 colSize.push_back(16);
100 colName.push_back("Eref");
101 colInfo.push_back("Reference potential energy.");
102 colSize.push_back(16);
103 colName.push_back("Ediff");
104 colInfo.push_back("Difference between reference and NNP prediction.");
105 colSize.push_back(16);
106 colName.push_back("E_offset");
107 colInfo.push_back("Sum of atomic offset energies "
108 "(included in column Ennp).");
110 createFileHeader(title, colSize, colName, colInfo));
111
112 file << strpr("%16.8E %16.8E %16.8E %16.8E\n",
113 prediction.structure.energy,
114 prediction.structure.energyRef,
115 prediction.structure.energyRef - prediction.structure.energy,
116 prediction.getEnergyOffset(prediction.structure));
117 file.close();
118
119 prediction.log << " - nnatoms.out\n";
120 file.open("nnatoms.out");
121
122 // File header.
123 title.clear();
124 colName.clear();
125 colInfo.clear();
126 colSize.clear();
127 title.push_back("Energy contributions calculated from NNP.");
128 colSize.push_back(10);
129 colName.push_back("index");
130 colInfo.push_back("Atom index.");
131 colSize.push_back(2);
132 colName.push_back("e");
133 colInfo.push_back("Element of atom.");
134 colSize.push_back(16);
135 colName.push_back("charge");
136 colInfo.push_back("Atomic charge (not used).");
137 colSize.push_back(16);
138 colName.push_back("Ennp_atom");
139 colInfo.push_back("Atomic energy contribution (physical units, no mean "
140 "or offset energy added).");
142 createFileHeader(title, colSize, colName, colInfo));
143
144 for (vector<Atom>::const_iterator it = s.atoms.begin();
145 it != s.atoms.end(); ++it)
146 {
147 file << strpr("%10d %2s %16.8E %16.8E\n",
148 it->index,
149 prediction.elementMap[it->element].c_str(),
150 it->charge,
151 it->energy);
152 }
153 file.close();
154
155 prediction.log << " - nnforces.out\n";
156 file.open("nnforces.out");
157
158 // File header.
159 title.clear();
160 colName.clear();
161 colInfo.clear();
162 colSize.clear();
163 title.push_back("Atomic force comparison (ordered by atom index).");
164 colSize.push_back(16);
165 colName.push_back("fx");
166 colInfo.push_back("Force in x direction.");
167 colSize.push_back(16);
168 colName.push_back("fy");
169 colInfo.push_back("Force in y direction.");
170 colSize.push_back(16);
171 colName.push_back("fz");
172 colInfo.push_back("Force in z direction.");
173 colSize.push_back(16);
174 colName.push_back("fxRef");
175 colInfo.push_back("Reference force in x direction.");
176 colSize.push_back(16);
177 colName.push_back("fyRef");
178 colInfo.push_back("Reference force in y direction.");
179 colSize.push_back(16);
180 colName.push_back("fzRef");
181 colInfo.push_back("Reference force in z direction.");
182 colSize.push_back(16);
183 colName.push_back("fxDiff");
184 colInfo.push_back("Difference between reference and NNP force in x dir.");
185 colSize.push_back(16);
186 colName.push_back("fyDiff");
187 colInfo.push_back("Difference between reference and NNP force in y dir.");
188 colSize.push_back(16);
189 colName.push_back("fzDiff");
190 colInfo.push_back("Difference between reference and NNP force in z dir.");
192 createFileHeader(title, colSize, colName, colInfo));
193
194 for (vector<Atom>::const_iterator it = s.atoms.begin();
195 it != s.atoms.end(); ++it)
196 {
197 file << strpr("%16.8E %16.8E %16.8E %16.8E %16.8E "
198 "%16.8E %16.8E %16.8E %16.8E\n",
199 it->f[0],
200 it->f[1],
201 it->f[2],
202 it->fRef[0],
203 it->fRef[1],
204 it->fRef[2],
205 it->fRef[0] - it->f[0],
206 it->fRef[1] - it->f[1],
207 it->fRef[2] - it->f[2]);
208 }
209 file.close();
210
211 prediction.log << "Writing structure with NNP prediction "
212 "to \"output.data\".\n";
213 file.open("output.data");
214 prediction.structure.writeToFile(&file, false);
215 file.close();
216
217 if (structureInfo)
218 {
219 prediction.log << "Writing detailed structure information to "
220 "\"structure.out\".\n";
221 file.open("structure.out");
222 vector<string> info = prediction.structure.info();
223 appendLinesToFile(file, info);
224 file.close();
225 }
226
227 prediction.log << "Finished.\n";
228 prediction.log << "*****************************************"
229 "**************************************\n";
230 logFile.close();
231
232 return 0;
233}
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:508
Log log
Global log file.
Definition: Mode.h:510
double getEnergyOffset(Structure const &structure) const
Get atomic energy offset for given structure.
Definition: Mode.cpp:1499
void readStructureFromFile(std::string const &fileName="input.data")
Definition: Prediction.cpp:44
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:104
void appendLinesToFile(ofstream &file, vector< string > const lines)
Append multiple lines of strings to open file stream.
Definition: utility.cpp:218
ofstream logFile
Definition: nnp-cutoff.cpp:29
Storage for one atomic configuration.
Definition: Structure.h:34
void writeToFile(std::string const fileName="output.data", bool const ref=true, bool const append=false) const
Write configuration to file.
Definition: Structure.cpp:659
double energy
Potential energy determined by neural network.
Definition: Structure.h:76
double energyRef
Reference potential energy.
Definition: Structure.h:78
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:68
std::size_t numElements
Global number of elements (all structures).
Definition: Structure.h:70
std::vector< Atom > atoms
Vector of all atoms in this structure.
Definition: Structure.h:96
std::vector< std::string > info() const
Get structure information as a vector of strings.
Definition: Structure.cpp:851

References nnp::appendLinesToFile(), nnp::Structure::atoms, nnp::createFileHeader(), nnp::Mode::elementMap, nnp::Structure::energy, nnp::Structure::energyRef, nnp::Mode::getEnergyOffset(), nnp::Structure::info(), nnp::Mode::log, 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: