n2p2 - A neural network potential package
nnp-convert.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 "ElementMap.h"
18#include "Log.h"
19#include "Structure.h"
20#include "utility.h"
21#include <algorithm>
22#include <cmath>
23#include <cstdlib>
24#include <iostream>
25#include <fstream>
26#include <map>
27#include <string>
28#include <utility>
29#include <vector>
30
31using namespace std;
32using namespace nnp;
33
34int main(int argc, char* argv[])
35{
36 if (argc < 3)
37 {
38 cout << "USAGE: " << argv[0] << " <format> <elem1 <elem2 ...>>\n"
39 << " <format> ... Structure file output format "
40 "(xyz/poscar).\n"
41 << " <elemN> .... Symbol for Nth element.\n"
42 << " Execute in directory with these NNP files present:\n"
43 << " - input.data (structure file)\n";
44 return 1;
45 }
46
47 ofstream logFile;
48 logFile.open("nnp-convert.log");
49 Log log;
51
52 log << "\n";
53 log << "*** NNP-CONVERT *************************"
54 "**************************************\n";
55 log << "\n";
56
57 string format = argv[1];
58 log << strpr("Requested file format : %s\n", format.c_str());
59 string outputFileName;
60 string outputFileNamePrefix;
61 if (format == "xyz")
62 {
63 outputFileName = "input.xyz";
64 log << strpr("Output file name : %s\n", outputFileName.c_str());
65 }
66 else if (format == "poscar")
67 {
68 outputFileNamePrefix = "POSCAR";
69 log << strpr("Output file name prefix: %s\n",
70 outputFileNamePrefix.c_str());
71 }
72 else
73 {
74 log << "ERROR: Unknown output file format.\n";
75 return 1;
76 }
77 size_t numElements = argc - 2;
78 log << strpr("Number of elements : %zu\n", numElements);
79 string elements;
80 elements += argv[2];
81 for (size_t i = 3; i < numElements + 2; ++i)
82 {
83 elements += " ";
84 elements += argv[i];
85 }
86 log << strpr("Element string : %s\n", elements.c_str());
87 log << "*****************************************"
88 "**************************************\n";
89
90 ElementMap elementMap;
91 elementMap.registerElements(elements);
92
93 ifstream inputFile;
94 inputFile.open("input.data");
95 Structure structure;
96 structure.setElementMap(elementMap);
97
98 size_t countStructures = 0;
99 ofstream outputFile;
100 if (format == "xyz")
101 {
102 outputFile.open(outputFileName.c_str());
103 }
104
105 while (inputFile.peek() != EOF)
106 {
107 structure.readFromFile(inputFile);
108 if (format == "xyz")
109 {
110 structure.writeToFileXyz(&outputFile);
111 }
112 else if (format == "poscar")
113 {
114 outputFileName = strpr("%s_%d",
115 outputFileNamePrefix.c_str(),
116 countStructures + 1);
117 outputFile.open(outputFileName.c_str());
118 structure.writeToFilePoscar(&outputFile, elements);
119 outputFile.close();
120 }
121 countStructures++;
122 log << strpr("Configuration %7zu: %7zu atoms\n",
123 countStructures,
124 structure.numAtoms);
125 structure.reset();
126 }
127
128 if (format == "xyz")
129 {
130 outputFile.close();
131 }
132
133 log << "*****************************************"
134 "**************************************\n";
135 logFile.close();
136
137 return 0;
138}
Contains element map.
Definition: ElementMap.h:30
std::size_t registerElements(std::string const &elementLine)
Extract all elements and store in element map.
Definition: ElementMap.cpp:36
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
int main(int argc, char *argv[])
Definition: nnp-convert.cpp:34
ofstream logFile
Definition: nnp-cutoff.cpp:29
Storage for one atomic configuration.
Definition: Structure.h:39
void setElementMap(ElementMap const &elementMap)
Set element map of structure.
Definition: Structure.cpp:71
void writeToFilePoscar(std::ofstream *const &file) const
Write configuration to POSCAR file.
Definition: Structure.cpp:1568
void writeToFileXyz(std::ofstream *const &file) const
Write configuration to xyz file.
Definition: Structure.cpp:1533
void readFromFile(std::string const fileName="input.data")
Read configuration from file.
Definition: Structure.cpp:97
void reset()
Reset everything but elementMap.
Definition: Structure.cpp:1319
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:75