n2p2 - A neural network potential package
nnp-prune.cpp File Reference
#include "Mode.h"
#include "utility.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdexcept>
Include dependency graph for nnp-prune.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 27 of file nnp-prune.cpp.

28{
29 double threshold;
30 string pruneMode;
31
32 if (!(argc == 3 || argc == 4))
33 {
34 cout << "USAGE: " << argv[0] << " <mode> <threshold> <<sensmode>>\n"
35 << " <mode> ........ Use scaling data \"range\" or "
36 "sensitivity analysis data \"sensitivity\".\n"
37 << " <threshold> ... Prune symmetry function lines in "
38 "settings file below this threshold.\n"
39 << " <sensmode> .... If <mode> is sensitivity, select mean "
40 "square average or maximum sensitivity (msa/max).\n"
41 << " Execute in directory with these NNP files present:\n"
42 << " - input.nn (NNP settings)\n"
43 << " - sensitivity.???.out (Data for sensitivity mode).\n";
44 return 1;
45 }
46
47 pruneMode = argv[1];
48 if (! (pruneMode == "range" || pruneMode == "sensitivity"))
49 {
50 throw invalid_argument("ERROR: Unknown pruning mode.\n");
51 }
52 threshold = atof(argv[2]);
53
54 ofstream logFile;
55 logFile.open("nnp-prune.log");
56 Mode mode;
58 mode.initialize();
59 mode.loadSettingsFile();
60 mode.setupGeneric();
61 if (pruneMode == "range")
62 {
64 }
65
66 mode.log << "\n";
67 mode.log << "*** PRUNE SYMMETRY FUNCTIONS ************"
68 "**************************************\n";
69 mode.log << "\n";
70
71 vector<size_t> prune;
72 if (pruneMode == "range")
73 {
74 mode.log << "Pruning symmetry functions according to their range.\n";
75 mode.log << strpr("Symmetry function threshold for pruning |Gmax - "
76 "Gmin| < %10.2E\n", threshold);
77 prune = mode.pruneSymmetryFunctionsRange(threshold);
78 mode.log << strpr("Number of symmetry functions to be pruned: %zu\n",
79 prune.size());
80 mode.writePrunedSettingsFile(prune, "output-prune-range.nn");
81 mode.log << "Pruned settings file written to: output-prune-range.nn\n";
82 }
83 else if (pruneMode == "sensitivity")
84 {
85 if (argc != 4)
86 {
87 throw invalid_argument("ERROR: Please select sensitivity mode.\n");
88 }
89 string sensMode = argv[3];
90 size_t column = 0;
91 if (sensMode == "msa")
92 {
93 column = 1;
94 mode.log << strpr("Pruning symmetry functions below mean square "
95 "average sensitivity threshold: %10.2E.\n",
96 threshold);
97 }
98 else if (sensMode == "max")
99 {
100 column = 2;
101 mode.log << strpr("Pruning symmetry functions below maximum "
102 "sensitivity threshold: %10.2E.\n",
103 threshold);
104 }
105 else
106 {
107 throw invalid_argument("ERROR: Unknown sensitivity mode.\n");
108 }
109 vector<vector<double> > sensitivity;
110 size_t numElements = mode.getNumElements();
111 vector<size_t> numSymmetryFunctions = mode.getNumSymmetryFunctions();
112 sensitivity.resize(numElements);
113 for (size_t i = 0; i < numElements; ++i)
114 {
115 ifstream sensFile;
116 string sensFileName = strpr("sensitivity.%03d.out",
117 mode.elementMap.atomicNumber(i));
118 sensFile.open(sensFileName.c_str());
119 string line;
120 vector<string> lines;
121 while (getline(sensFile, line))
122 {
123 if (line.at(0) != '#')
124 {
125 lines.push_back(line);
126 }
127 }
128 if (lines.size() != numSymmetryFunctions.at(i))
129 {
130 throw range_error(strpr("ERROR: Inconsistent sensistivity"
131 " data file \"%s\".\n",
132 sensFileName.c_str()));
133 }
134 for (size_t j = 0; j < numSymmetryFunctions.at(i); ++j)
135 {
136 vector<string> split = nnp::split(reduce(lines.at(j)));
137 double s = atof(split.at(column).c_str());
138 sensitivity.at(i).push_back(s);
139 }
140 sensFile.close();
141 }
142 prune = mode.pruneSymmetryFunctionsSensitivity(threshold, sensitivity);
143 mode.log << strpr("Number of symmetry functions to be pruned: %zu\n",
144 prune.size());
145 mode.writePrunedSettingsFile(prune, "output-prune-sensitivity.nn");
146 mode.log << "Pruned settings file written to: "
147 "output-prune-sensitivity.nn\n";
148 }
149
150 mode.log << "*****************************************"
151 "**************************************\n";
152 logFile.close();
153
154 return 0;
155}
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
Base class for all NNP applications.
Definition: Mode.h:87
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:591
void initialize()
Write welcome message with version information.
Definition: Mode.cpp:55
std::size_t getNumElements() const
Getter for Mode::numElements.
Definition: Mode.h:698
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
std::vector< std::size_t > pruneSymmetryFunctionsRange(double threshold)
Prune symmetry functions according to their range and write settings file.
Definition: Mode.cpp:2228
void writePrunedSettingsFile(std::vector< std::size_t > prune, std::string fileName="output.nn") const
Copy settings file but comment out lines provided.
Definition: Mode.cpp:2204
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
Definition: Mode.cpp:712
std::vector< std::size_t > getNumSymmetryFunctions() const
Get number of symmetry functions per element.
Definition: Mode.cpp:2180
Log log
Global log file.
Definition: Mode.h:593
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition: Mode.cpp:161
std::vector< std::size_t > pruneSymmetryFunctionsSensitivity(double threshold, std::vector< std::vector< double > > sensitivity)
Prune symmetry functions with sensitivity analysis data.
Definition: Mode.cpp:2249
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
vector< string > split(string const &input, char delimiter)
Split string at each delimiter.
Definition: utility.cpp:33
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
Definition: utility.cpp:60
ofstream logFile
Definition: nnp-cutoff.cpp:29

References nnp::ElementMap::atomicNumber(), nnp::Mode::elementMap, nnp::Mode::getNumElements(), nnp::Mode::getNumSymmetryFunctions(), nnp::Mode::initialize(), nnp::Mode::loadSettingsFile(), nnp::Mode::log, logFile, nnp::Mode::pruneSymmetryFunctionsRange(), nnp::Mode::pruneSymmetryFunctionsSensitivity(), nnp::reduce(), nnp::Log::registerStreamPointer(), nnp::Mode::setupGeneric(), nnp::Mode::setupSymmetryFunctionScaling(), nnp::split(), nnp::strpr(), and nnp::Mode::writePrunedSettingsFile().

Here is the call graph for this function: