-
Notifications
You must be signed in to change notification settings - Fork 7
/
lattice.cpp
359 lines (313 loc) · 11.5 KB
/
lattice.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "lattice.h"
#include "stdlib.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
#include "math.h"
#include <list>
#include <map>
#include "common.h"
/* -----------------------------------------------------------------------------
* constructor does nothing
* -------------------------------------------------------------------------- */
lattice::lattice()
{
initialized = 0;
perp_x = perp_y = perp_z = 0;
nlayer = nucell = ntype = 0;
noct = ntetra = 0;
name = NULL;
attyp = NULL;
atpos = NULL;
layer = NULL;
numlayer = NULL;
h = NULL;
memory = new Memory;
}
/* -----------------------------------------------------------------------------
* deconstructor destroy any allocated memory
* -------------------------------------------------------------------------- */
lattice::~lattice()
{
if (atpos) memory->destroy(atpos);
if (attyp) memory->destroy(attyp);
if (name ) memory->destroy(name);
if (layer) memory->destroy(layer);
if (numlayer) memory->destroy(numlayer);
if (h ) memory->destroy(h);
delete memory;
}
/* -----------------------------------------------------------------------------
* Display lattice basic info
* -------------------------------------------------------------------------- */
void lattice::display()
{
if (!initialized) return;
setup();
printf("\n"); for (int i = 0; i < 7; ++i) printf("====");
printf(" Lattice Info "); for (int i = 0; i < 7; ++i) printf("===="); printf("\n");
printf("Lattice name........................: %s\n", name);
printf("Number of atom per unit cell........: %d\n", nucell);
printf("Number of atomic types per unit cell: %d\n", ntype);
printf("Number of z-layers in each unit cell: %d\n", nlayer);
printf("Number of atoms in each layer......:");
for (int i = 0; i < nlayer; ++i) printf(" %d", numlayer[i]); printf("\n");
printf("Expected height above each layer....:\n");
for (int i = 0; i < nlayer; ++i){
printf(" %g", h[i]);
if (i%5 == 4 || i == nlayer-1) printf("\n");
}
for (int i = 0; i < 14; ++i) printf("-----");
printf("\nLattice vectors:\n");
for (int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j) printf("%lf ", latvec[i][j]*alat);
printf("\n");
}
for (int i = 0; i < 14; ++i) printf("-----");
printf("\nBasis (Fractional coordinate & type):\n");
for (int i = 0; i < nucell; ++i){
printf("%lf %lf %lf %d\n", atpos[i][0], atpos[i][1], atpos[i][2], attyp[i]);
}
for (int i = 0; i < 14; ++i) printf("====="); printf("\n\n");
}
/* -----------------------------------------------------------------------------
* Method to re-orient the lattice, hope to follow the rules of LAMMPS
* -------------------------------------------------------------------------- */
void lattice::OrientLattice()
{
double LV[3][3], box[3], oldy[3];
box[0] = box[1] = box[2] = 0.;
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j){
LV[i][j] = latvec[i][j];
box[i] += latvec[i][j]*latvec[i][j];
latvec[i][j] = 0.;
}
box[i] = sqrt(box[i]);
}
// new A
latvec[0][0] = box[0];
// new B
for (int i = 0; i < 3; ++i) latvec[1][0] += LV[0][i]*LV[1][i]/box[0];
latvec[1][1] = sqrt(box[1]*box[1]-latvec[1][0]*latvec[1][0]);
// new C
for (int i = 0; i < 3; ++i) latvec[2][0] += LV[0][i]*LV[2][i]/box[0];
for (int i = 0; i < 3; ++i) oldy[i] = LV[1][i] - latvec[1][0]*LV[0][i]/box[0];
double yl=0.;
for (int i = 0; i < 3; ++i) yl += oldy[i]*oldy[i];
latvec[2][1] = (oldy[0]*LV[2][0] + oldy[1]*LV[2][1] + oldy[2]*LV[2][2])/sqrt(yl);
latvec[2][2] = sqrt(box[2]*box[2]-latvec[2][0]*latvec[2][0]-latvec[2][1]*latvec[2][1]);
return;
}
/* -----------------------------------------------------------------------------
* Method to count # of words in a string, without destroying the string
* -------------------------------------------------------------------------- */
int lattice::count_words(const char *line)
{
int n = strlen(line) + 1;
char *copy;
memory->create(copy, n, "copy");
strcpy(copy,line);
char *ptr;
if (ptr = strchr(copy,'#')) *ptr = '\0';
if (strtok(copy," \t\n\r\f") == NULL) {
memory->sfree(copy);
return 0;
}
n = 1;
while (strtok(NULL," \t\n\r\f")) n++;
memory->sfree(copy);
return n;
}
/* -----------------------------------------------------------------------------
* Method to setup the dependent parameters
* -------------------------------------------------------------------------- */
void lattice::setup()
{
if (initialized == 0) return;
// sort atom according to their types
for (int i = 0; i < nucell; ++i){
for (int j = i+1; j < nucell; ++j){
if (attyp[i] > attyp[j]){
int idum = attyp[i];
attyp[i] = attyp[j];
attyp[j] = idum;
for (int idim = 0; idim < 3; ++idim){
double fdum = atpos[i][idim];
atpos[i][idim] = atpos[j][idim];
atpos[j][idim] = fdum;
}
}
}
}
// get the layer ID
std::list<double> zlist;
std::list<double>::iterator it;
std::map<double,int> zmap;
zlist.clear();
for (int i = 0; i < nucell; ++i) zlist.push_back(atpos[i][2]);
zlist.sort(); zlist.unique();
memory->create(layer, nucell, "lattice:setup:layers");
int il = 0;
for (it = zlist.begin(); it != zlist.end(); ++it) zmap[*it] = il++;
for (int i = 0; i < nucell; ++i) layer[i] = zmap[atpos[i][2]];
nlayer = (int) zmap.size();
zlist.clear(); zmap.clear();
// get the height above each layer
if (h) memory->destroy(h);
if (numlayer) memory->destroy(numlayer);
memory->create(h, nlayer, "lattice->setup:h");
memory->create(numlayer, nlayer, "lattice->setup:numlayer");
for (int i = 0; i < nlayer; ++i) numlayer[i] = 0;
for (int i = 0; i < nucell; ++i) numlayer[layer[i]]++;
int i0 = 0, i1 =0;
double pos0, pos1;
for (int i = 0; i < nucell; ++i) if (layer[i] == 0) {i0 = i; break;}
pos0 = 0.;
for (int i = 0; i < 3; ++i) pos0 += atpos[i0][i] * latvec[i][2];
for (int il = 1; il < nlayer; ++il){
for (int i = 0; i < nucell; ++i) if (layer[i] == il) {i1 = i; break;}
pos1 = 0.;
for (int i = 0; i < 3; ++i) pos1 += atpos[i1][i] * latvec[i][2];
h[il-1] = (pos1 - pos0)*alat;
pos0 = pos1;
}
pos1 = 0.;
for (int i = 0; i < 3; ++i) pos1 += (atpos[i0][i]+double(i/2)) * latvec[i][2];
h[nlayer-1] = (pos1-pos0)*alat;
// get the length of each vector
lx = sqrt(DotProd(latvec[0], latvec[0]))*alat;
ly = sqrt(DotProd(latvec[1], latvec[1]))*alat;
lz = sqrt(DotProd(latvec[2], latvec[2]))*alat;
// get the norm vector of each pair
double Nxy[3], Nxz[3], Nyz[3];
Cross(latvec[0], latvec[1], Nxy);
Cross(latvec[0], latvec[2], Nxz);
Cross(latvec[1], latvec[2], Nyz);
double Sxy = sqrt(DotProd(&Nxy[0],&Nxy[0]));
double Sxz = sqrt(DotProd(&Nxz[0],&Nxz[0]));
double Syz = sqrt(DotProd(&Nyz[0],&Nyz[0]));
double vola = DotProd(&Nxy[0], latvec[2])*alat;
hx = vola/Syz;
hy = vola/Sxz;
hz = vola/Sxy;
// check if one basic vector is perpendicular to the remaing two
double AdotB = DotProd(latvec[0], latvec[1]);
double AdotC = DotProd(latvec[0], latvec[2]);
double BdotC = DotProd(latvec[1], latvec[2]);
if ( fabs(AdotB) + fabs(AdotC) <= NearZERO ) perp_x = 1;
if ( fabs(AdotB) + fabs(BdotC) <= NearZERO ) perp_y = 1;
if ( fabs(AdotC) + fabs(BdotC) <= NearZERO ) perp_z = 1;
return;
}
/* -----------------------------------------------------------------------------
* Method to rotate the lattice. angles are in unit of 2*pi
* -------------------------------------------------------------------------- */
void lattice::RotateLattice(double *angles, double rotated[][3])
{
const double tPI = 8.*atan(1.);
double cosa = cos(angles[0]*tPI);
double sina = sin(angles[0]*tPI);
double cosb = cos(angles[1]*tPI);
double sinb = sin(angles[1]*tPI);
double cosg = cos(angles[2]*tPI);
double sing = sin(angles[2]*tPI);
double rot[3][3];
rot[0][0] = cosb*cosg;
rot[0][1] = cosb*sing;
rot[0][2] = -sinb;
rot[1][0] = sina * sinb * cosg - cosa * sing;
rot[1][1] = sina * sinb * sing + cosa * cosg;
rot[1][2] = sina * cosb;
rot[2][0] = cosa * sinb * cosg + sina * sing;
rot[2][1] = cosa * sinb * sing - sina * cosg;
rot[2][2] = cosa * cosb;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) rotated[i][j] = 0.;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k) rotated[i][j] += rot[i][k] * latvec[k][j];
return;
}
/* -----------------------------------------------------------------------------
* Method to rotate the lattice. angles are in unit of 2*pi
* -------------------------------------------------------------------------- */
void lattice::RotateLattice(double *angles)
{
const double tPI = 8.*atan(1.);
double cosa = cos(angles[0]*tPI);
double sina = sin(angles[0]*tPI);
double cosb = cos(angles[1]*tPI);
double sinb = sin(angles[1]*tPI);
double cosg = cos(angles[2]*tPI);
double sing = sin(angles[2]*tPI);
double rot[3][3];
rot[0][0] = cosb*cosg;
rot[0][1] = cosb*sing;
rot[0][2] = -sinb;
rot[1][0] = sina * sinb * cosg - cosa * sing;
rot[1][1] = sina * sinb * sing + cosa * cosg;
rot[1][2] = sina * cosb;
rot[2][0] = cosa * sinb * cosg + sina * sing;
rot[2][1] = cosa * sinb * sing - sina * cosg;
rot[2][2] = cosa * cosb;
double rotated[3][3];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) rotated[i][j] = 0.;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k) rotated[i][j] += rot[i][k] * latvec[k][j];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) latvec[i][j] = rotated[i][j];
return;
}
/* -----------------------------------------------------------------------------
* Private method to get the cross product of two vectors; C[3] = A[3] X B[3]
* -------------------------------------------------------------------------- */
void lattice::Cross(double *A, double *B, double *C)
{
C[0] = A[1] * B[2] - A[2] * B[1];
C[1] = A[2] * B[0] - A[0] * B[2];
C[2] = A[0] * B[1] - A[1] * B[0];
return;
}
/* -----------------------------------------------------------------------------
* Private method to get the dot product of two vectors of dim 3
* -------------------------------------------------------------------------- */
double lattice::DotProd(double *A, double *B)
{
return A[0]*B[0] + A[1]*B[1] + A[2]*B[2];
}
/* -----------------------------------------------------------------------------
* read a floating point value from a string
* generate an error if not a legitimate floating point value
* called by various commands to check validity of their arguments
* -------------------------------------------------------------------------- */
double lattice::numeric(char *str)
{
int n = strlen(str);
for (int i = 0; i < n; ++i) {
if (isdigit(str[i])) continue;
if (str[i] == '-' || str[i] == '+' || str[i] == '.') continue;
if (str[i] == 'e' || str[i] == 'E') continue;
printf("ERROR: wrong input (%s) when a float is expected!\n\n", str);
exit(1);
}
return atof(str);
}
/* -----------------------------------------------------------------------------
* read an integer value from a string
* generate an error if not a legitimate integer value
* called by various commands to check validity of their arguments
* -------------------------------------------------------------------------- */
int lattice::inumeric(char *str)
{
int n = strlen(str);
for (int i = 0; i < n; i++) {
if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue;
printf("ERROR: wrong input (%s) when an integer is expected!\n\n", str);
exit(1);
}
return atoi(str);
}
/*----------------------------------------------------------------------------*/