n2p2 - A neural network potential package
nnp-select.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 "Log.h"
18#include "utility.h"
19#include <cstddef> // std::size_t
20#include <cstdlib> // srand, rand
21#include <cstring> // strcmp
22#include <iostream> // std::cout
23#include <fstream> // std::ofstream
24
25using namespace std;
26using namespace nnp;
27
28int main(int argc, char* argv[])
29{
30 if (argc < 3)
31 {
32 cout << "USAGE: " << argv[0] << " <mode> <arg1 <arg2>>\n"
33 << " <mode> ... Choose selection mode (random/interval).\n"
34 << " Arguments for mode \"random\":\n"
35 << " <arg1> ... Ratio of selected structures "
36 << "(1.0 equals 100 %).\n"
37 << " <arg2> ... Seed for random number generator "
38 << "(integer).\n"
39 << " Arguments for mode \"interval\":\n"
40 << " <arg1> ... Select structures in this interval "
41 << "(integer).\n"
42 << " Execute in directory with these NNP files present:\n"
43 << " - input.data (structure file)\n";
44 return 1;
45 }
46
47 bool mode = false; // 0 = random, 1 = interval.
48 bool writeStructure = false;
49 size_t countStructures = 0;
50 size_t countSelected = 0;
51 size_t interval = 10;
52 int seed = 10;
53 double ratio = 1.0;
54
55 Log log;
56 ifstream inputFile;
57 ofstream outputFile;
58 ofstream rejectFile;
59 ofstream logFile;
60
61 logFile.open("nnp-select.log");
63
64 log << "\n";
65 log << "*** NNP-SELECT **************************"
66 "**************************************\n";
67 log << "\n";
68
69 if (strcmp(argv[1], "random") == 0)
70 {
71 mode = false;
72 if (argc != 4)
73 {
74 throw invalid_argument("ERROR: Wrong number of arguments.\n");
75 }
76 ratio = atof(argv[2]);
77 seed = atoi(argv[3]);
78 srand(seed);
79 log << strpr("Selecting randomly %.2f %% of all structures.\n",
80 ratio * 100.0);
81 log << strpr("Random number generator seed: %d.\n", seed);
82 }
83 else if (strcmp(argv[1], "interval") == 0)
84 {
85 mode = true;
86 if (argc != 3)
87 {
88 throw invalid_argument("ERROR: Wrong number of arguments.\n");
89 }
90 interval = (size_t)atoi(argv[2]);
91 log << strpr("Selecting every %d structure.\n", interval);
92 }
93 else
94 {
95 throw invalid_argument("ERROR: Unknown selection mode.\n");
96 }
97
98 log << "*****************************************"
99 "**************************************\n";
100
101 inputFile.open("input.data");
102 outputFile.open("output.data");
103 rejectFile.open("reject.data");
104 string line;
105 while (getline(inputFile, line))
106 {
107 if (split(reduce(line)).at(0) == "begin")
108 {
109 if (mode)
110 {
111 if (countStructures % interval == 0) writeStructure = true;
112 else writeStructure = false;
113 }
114 else
115 {
116 if ((double)rand() / RAND_MAX <= ratio) writeStructure = true;
117 else writeStructure = false;
118 }
119 countStructures++;
120 if (writeStructure)
121 {
122 log << strpr("Structure %7d selected.\n", countStructures);
123 countSelected++;
124 }
125 }
126 if (writeStructure)
127 {
128 outputFile << line << '\n';
129 }
130 else
131 {
132 rejectFile << line << '\n';
133 }
134 }
135 inputFile.close();
136 outputFile.close();
137 rejectFile.close();
138
139 log << "*****************************************"
140 "**************************************\n";
141 log << strpr("Total structures : %7d\n", countStructures);
142 log << strpr("Selected structures : %7d\n", countSelected);
143 log << strpr("Selected structures percentage: %.3f %%\n",
144 countSelected / (double)countStructures * 100.0);
145 log << "*****************************************"
146 "**************************************\n";
147 logFile.close();
148
149 return 0;
150}
Logging class for library output.
Definition: Log.h:34
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
Definition: Atom.h:29
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
int main(int argc, char *argv[])
Definition: nnp-select.cpp:28