n2p2 - A neural network potential package
nnp-convert.cpp File Reference
#include "ElementMap.h"
#include "Log.h"
#include "Structure.h"
#include "utility.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <utility>
#include <vector>
Include dependency graph for nnp-convert.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 34 of file nnp-convert.cpp.

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
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
ofstream logFile
Definition: nnp-cutoff.cpp:29
Storage for one atomic configuration.
Definition: Structure.h:34
void setElementMap(ElementMap const &elementMap)
Set element map of structure.
Definition: Structure.cpp:60
void writeToFilePoscar(std::ofstream *const &file) const
Write configuration to POSCAR file.
Definition: Structure.cpp:778
void writeToFileXyz(std::ofstream *const &file) const
Write configuration to xyz file.
Definition: Structure.cpp:743
void readFromFile(std::string const fileName="input.data")
Read configuration from file.
Definition: Structure.cpp:86
void reset()
Reset everything but elementMap.
Definition: Structure.cpp:543
std::size_t numAtoms
Total number of atoms present in this structure.
Definition: Structure.h:68

References logFile, nnp::Structure::numAtoms, nnp::Structure::readFromFile(), nnp::ElementMap::registerElements(), nnp::Log::registerStreamPointer(), nnp::Structure::reset(), nnp::Structure::setElementMap(), nnp::strpr(), nnp::Structure::writeToFilePoscar(), and nnp::Structure::writeToFileXyz().

Here is the call graph for this function: