n2p2 - A neural network potential package
nnp-scaling.cpp File Reference
#include "Dataset.h"
#include "mpi-extra.h"
#include "utility.h"
#include <mpi.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
Include dependency graph for nnp-scaling.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-scaling.cpp.

31{
32 bool useForces = false;
33 int numProcs = 0;
34 int myRank = 0;
35 long memory = 0;
36 size_t count = 0;
37 ofstream myLog;
38
39 if (argc != 2)
40 {
41 cout << "USAGE: " << argv[0] << " <nbins>\n"
42 << " <nbins> ... Number of symmetry function"
43 " histogram bins.\n"
44 << " Execute in directory with these NNP files present:\n"
45 << " - input.data (structure file)\n"
46 << " - input.nn (NNP settings)\n";
47 return 1;
48 }
49
50 size_t numBins = (size_t)atoi(argv[1]);
51
52 MPI_Init(&argc, &argv);
53 MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
54 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
55
56 Dataset dataset;
57 myLog.open(strpr("nnp-scaling.log.%04d", myRank).c_str());
58 if (myRank != 0) dataset.log.writeToStdout = false;
59 dataset.log.registerStreamPointer(&myLog);
60 dataset.setupMPI();
61 dataset.initialize();
62 dataset.loadSettingsFile();
63 // TODO: is there a better way than setting initial_hardness true? stage is
64 // not accessible here.
65 dataset.setupGeneric("", true);
67 dataset.setupSymmetryFunctionStatistics(true, false, false, false);
69 dataset.distributeStructures(true);
70 if (dataset.useNormalization()) dataset.toNormalizedUnits();
71 useForces = dataset.settingsKeywordExists("use_short_forces");
72
73 dataset.log << "\n";
74 dataset.log << "*** CALCULATING SYMMETRY FUNCTIONS ******"
75 "**************************************\n";
76 dataset.log << "\n";
77
78 dataset.log << "Check the log files of all (!) MPI processes"
79 " for warnings in this section!\n";
80
81 for (vector<Structure>::iterator it = dataset.structures.begin();
82 it != dataset.structures.end(); ++it)
83 {
84 it->calculateNeighborList(dataset.getMaxCutoffRadius());
85#ifdef N2P2_NO_SF_GROUPS
86 dataset.calculateSymmetryFunctions((*it), useForces);
87#else
88 dataset.calculateSymmetryFunctionGroups((*it), useForces);
89#endif
90 memory += dataset.calculateBufferSize((*it));
91 count++;
92 // Clear unneccessary memory (neighbor list and others), leave only
93 // numNeighbors and symmetry functions (G) for histogram calculation.
94 // Don't use these structures after these operations unless you know
95 // what you do!
96 for (vector<Atom>::iterator it2 = it->atoms.begin();
97 it2 != it->atoms.end(); ++it2)
98 {
99 it2->numNeighborsUnique = 0;
100 it2->neighborsUnique.clear();
101 vector<size_t>(it2->neighborsUnique).swap(it2->neighborsUnique);
102
103 it2->numNeighborsPerElement.clear();
104 vector<size_t>(it2->numNeighborsPerElement).swap(
105 it2->numNeighborsPerElement);
106
107 it2->dEdG.clear();
108 vector<double>(it2->dEdG).swap(it2->dEdG);
109
110#ifdef N2P2_FULL_SFD_MEMORY
111 it2->dGdxia.clear();
112 vector<double>(it2->dGdxia).swap(it2->dGdxia);
113#endif
114
115 it2->dGdr.clear();
116 vector<Vec3D>(it2->dGdr).swap(it2->dGdr);
117
118 // Leave this for the histogram.
119 //it2->numNeighbors = 0;
120 it2->neighbors.clear();
121 vector<Atom::Neighbor>(it2->neighbors).swap(it2->neighbors);
122 }
123 }
124 dataset.log << "*****************************************"
125 "**************************************\n";
126
129 dataset.writeNeighborHistogram();
130 dataset.writeSymmetryFunctionHistograms(numBins);
132
133 dataset.log << "\n";
134 dataset.log << "*** MEMORY USAGE ESTIMATION *************"
135 "**************************************\n";
136 dataset.log << "\n";
137 dataset.log << "Estimated memory usage for training (keyword"
138 " \"memorize_symfunc_results\":\n";
139 if (useForces)
140 {
141 dataset.log << "Valid for training of energies and forces.\n";
142 }
143 else
144 {
145 dataset.log << "Valid for training of energies only.\n";
146 }
147 dataset.log << strpr("Memory for local structures : "
148 "%15ld bytes (%.2f MiB = %.2f GiB).\n",
149 memory,
150 memory / 1024. / 1024.,
151 memory / 1024. / 1024. / 1024.);
152 MPI_Allreduce(MPI_IN_PLACE, &memory, 1, MPI_LONG , MPI_SUM, MPI_COMM_WORLD);
153 MPI_Allreduce(MPI_IN_PLACE, &count , 1, MPI_SIZE_T, MPI_SUM, MPI_COMM_WORLD);
154 dataset.log << strpr("Memory for all structures : "
155 "%15ld bytes (%.2f MiB = %.2f GiB).\n",
156 memory,
157 memory / 1024. / 1024.,
158 memory / 1024. / 1024. / 1024.);
159 dataset.log << strpr("Average memory per structure : "
160 "%15.0f bytes (%.2f MiB).\n",
161 memory / (double)count,
162 memory / (double)count / 1024. / 1024.);
163 dataset.log << "*****************************************"
164 "**************************************\n";
165
166 myLog.close();
167
168 MPI_Finalize();
169
170 return 0;
171}
Collect and process large data sets.
Definition: Dataset.h:35
void writeSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Write symmetry function scaling values to file.
Definition: Dataset.cpp:1004
void collectSymmetryFunctionStatistics()
Collect symmetry function statistics from all processors.
Definition: Dataset.cpp:974
int distributeStructures(bool randomize, bool excludeRank0=false, std::string const &fileName="input.data")
Read data file and distribute structures among processors.
Definition: Dataset.cpp:724
int calculateBufferSize(Structure const &structure) const
Calculate buffer size required to communicate structure via MPI.
Definition: Dataset.cpp:165
void setupMPI()
Initialize MPI with MPI_COMM_WORLD.
Definition: Dataset.cpp:52
std::vector< Structure > structures
All structures in this dataset.
Definition: Dataset.h:195
void writeSymmetryFunctionFile(std::string fileName="function.data")
Write symmetry function legacy file ("function.data").
Definition: Dataset.cpp:1255
void writeSymmetryFunctionHistograms(std::size_t numBins, std::string fileNameFormat="sf.%03zu.%04zu.histo")
Calculate and write symmetry function histograms.
Definition: Dataset.cpp:1103
std::size_t writeNeighborHistogram(std::string const &fileNameHisto="neighbors.histo", std::string const &fileNameStructure="neighbors.out")
Calculate and write neighbor histogram and per-structure statistics.
Definition: Dataset.cpp:1320
void setupRandomNumberGenerator()
Initialize random number generator.
Definition: Dataset.cpp:110
void toNormalizedUnits()
Switch all structures to normalized units.
Definition: Dataset.cpp:952
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
bool writeToStdout
Turn on/off output to stdout.
Definition: Log.h:85
void initialize()
Write welcome message with version information.
Definition: Mode.cpp:55
void setupGeneric(std::string const &nnpDir="", bool skipNormalize=false, bool initialHardness=false)
Combine multiple setup routines and provide a basic NNP setup.
Definition: Mode.cpp:212
double getMaxCutoffRadius() const
Getter for Mode::maxCutoffRadius.
Definition: Mode.h:693
bool useNormalization() const
Check if normalization is enabled.
Definition: Mode.h:703
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
Definition: Mode.cpp:1561
void setupSymmetryFunctionScalingNone()
Set up "empy" symmetry function scaling.
Definition: Mode.cpp:692
bool settingsKeywordExists(std::string const &keyword) const
Check if keyword was found in settings file.
Definition: Mode.cpp:2193
Log log
Global log file.
Definition: Mode.h:593
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
Definition: Mode.cpp:1480
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
Definition: Mode.cpp:1103
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition: Mode.cpp:161
#define MPI_SIZE_T
Definition: mpi-extra.h:22
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90

References nnp::Dataset::calculateBufferSize(), nnp::Mode::calculateSymmetryFunctionGroups(), nnp::Mode::calculateSymmetryFunctions(), nnp::Dataset::collectSymmetryFunctionStatistics(), nnp::Dataset::distributeStructures(), nnp::Mode::getMaxCutoffRadius(), nnp::Mode::initialize(), nnp::Mode::loadSettingsFile(), nnp::Mode::log, MPI_SIZE_T, nnp::Log::registerStreamPointer(), nnp::Mode::settingsKeywordExists(), nnp::Mode::setupGeneric(), nnp::Dataset::setupMPI(), nnp::Dataset::setupRandomNumberGenerator(), nnp::Mode::setupSymmetryFunctionScalingNone(), nnp::Mode::setupSymmetryFunctionStatistics(), nnp::strpr(), nnp::Dataset::structures, nnp::Dataset::toNormalizedUnits(), nnp::Mode::useNormalization(), nnp::Dataset::writeNeighborHistogram(), nnp::Dataset::writeSymmetryFunctionFile(), nnp::Dataset::writeSymmetryFunctionHistograms(), nnp::Dataset::writeSymmetryFunctionScaling(), and nnp::Log::writeToStdout.

Here is the call graph for this function: