-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvasp2abinit.cpp
202 lines (171 loc) · 5.11 KB
/
vasp2abinit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Created by Ryky Nelson Aug 2019
*/
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <string>
#include <sstream>
#include <iomanip>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <cstring>
#include <cstdlib>
#include "elements.h"
using namespace std;
using namespace Eigen;
using namespace boost::filesystem;
void Usage(string exe_name){
cerr << "please specify your POSCAR or CONTCAR;\n"
<< "e.g.: " << exe_name << " POSCAR\n";
}
void author(){
cout << "Program is created by Ryky Nelson.\n";
}
string GetEnv( const string & var ) {
const char * val = ::getenv( var.c_str() );
if ( val == 0 ) {
return "";
}
else {
return val;
}
}
int main(int argc, char* argv[]){
if (argc < 2 ) {
Usage(argv[0]);
return 1;
}
string flag(argv[1]);
if (flag == "-c" || flag == "-C") {
author();
return 0;
}
string vaspin = "";
vaspin = argv[1];
std::ifstream vaspFile( vaspin.c_str() );
if( !vaspFile || (vaspFile.peek() == std::ifstream::traits_type::eof()) ) {
cerr << "Unable to open " << vaspin << " or it is empty!" << endl;
return 0;
}
string line; stringstream ss;
string trash, sval;
double dval;
getline(vaspFile,line); // ignore the header
getline(vaspFile,line);
ss.str(line); ss.clear();
double acell;
ss >> acell;
// get the primitive vector from POSCAR
Matrix3d primVec = Matrix3d::Zero();
for (int idummy = 0; idummy < 3; ++idummy) { // the elements' order is kept!!!
getline(vaspFile,line);
ss.str(string()); ss.clear(); ss.str(line);
ss >> primVec(idummy,0) >> primVec(idummy,1) >> primVec(idummy,2);
}
double maxEl = primVec.array().abs().maxCoeff();
acell *= maxEl;
primVec /= maxEl;
// get the atoms and position-vectors from POSCAR
size_t natom(0), ntyp(0);
string atyp;
vector<string> atomType;
getline(vaspFile,line);
ss.str(string()); ss.clear(); ss.str(line);
while(ss>>atyp) atomType.push_back(atyp);
ntyp = atomType.size();
vector<size_t> numPerType;
getline(vaspFile,line);
ss.str(string()); ss.clear(); ss.str(line);
for(vector<string>::iterator it = atomType.begin(); it != atomType.end(); ++it) {
size_t tdummy; ss >> tdummy;
natom += tdummy;
numPerType.push_back(tdummy);
}
getline(vaspFile,line); // kind of coordinates: D or C, for now assuming D
vector<Vector3d> apos;
for(size_t idummy = 0; idummy < ntyp; ++idummy) {
size_t npTyp = numPerType[idummy];
for(size_t jdummy = 0; jdummy < npTyp; ++jdummy) {
getline(vaspFile,line);
ss.str(string()); ss.clear(); ss.str(line);
Vector3d pos;
ss >> pos(0) >> pos(1) >> pos(2);
apos.push_back(pos);
}
}
// print output
cout << "acell " << "3*" << acell << " angstrom\n";
// print rprim
cout << fixed << setprecision(12);
cout << "\nrprim\n" << primVec << endl
<< "\nnatom " << natom << "\n\nntypat " << ntyp << endl
<< "\ntypat ";
for(size_t idummy = 0; idummy < ntyp; ++idummy)
for(size_t jdummy = 0; jdummy < numPerType[idummy]; ++jdummy) cout << (idummy+1) << ' ';
cout << "\n\nznucl ";
for (vector<string>::iterator it = atomType.begin(); it != atomType.end(); ++it) {
int atomOfE = elementsMass.at(*it);
cout << atomOfE << ' ';
}
cout << endl << endl;
// print xred
cout << "xred\n" << fixed << setprecision(12);
size_t iterdummy(0);
for(size_t idummy = 0; idummy < ntyp; ++idummy) {
size_t npTyp = numPerType[idummy];
for(size_t jdummy = 0; jdummy < npTyp; ++jdummy) {
cout << ' ' << apos[iterdummy].transpose() << endl;
++iterdummy;
}
}
cout << "\necut XXECUTXX\n"
<< "\npawecutdg XXPAWECUTDGXX\n"
<< "\nnband XXNBNDXX\n";
cout << "\ntolvrs 1.0d-12\n"
<< "\nnstep 100\n"
<< "\nistwfk *1\n";
// string nspin("1");
string NSpin = GetEnv("NSPIN");
if ( NSpin.empty() ) NSpin = '1';
ss.str(string()); ss.clear(); ss.str(NSpin);
int nspin;
ss >> nspin;
cout << "\nnsppol " << nspin << endl;
if (nspin == 2) {
string magmom = GetEnv("MAGMOM");
ss.str(string()); ss.clear(); ss.str(magmom);
cout << "\nspinat\n" << fixed << setprecision(3);
size_t tdummy;
while(ss>>tdummy) {
double magmomV; ss >> magmomV;
for ( size_t idummy = 0; idummy < tdummy; ++idummy ) {
cout << " 0.000 0.000 " << magmomV << endl;
}
}
}
cout << "\noccopt 6\n";
// print K_POINTS {automatic}
cout << "\nngkpt ";
string nkptString;
int kptDensity(-1);
istringstream itmp;
if (argc > 2) itmp.str(argv[2]);
if ( !(itmp >> kptDensity) || ! itmp.eof()) kptDensity = -1;
if (kptDensity > 0){
int ngrid = kptDensity / natom;
int mult = pow(( ngrid
* primVec.row(0).norm()
* primVec.row(1).norm()
* primVec.row(2).norm() ), 1.0/3.0);
Array3i nkpt;
for (int ikpt = 0; ikpt < 3; ++ikpt) {
nkpt(ikpt) = round(mult / primVec.row(ikpt).norm());
if (nkpt(ikpt) == 0) nkpt(ikpt) = 1;
}
cout << nkpt.transpose() << endl;
}
else cout << "XXKPOINTXX\n";
cout << "\nshiftk 0.0 0.0 0.0\n";
return 0;
}