-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlattice.js
228 lines (212 loc) · 7.3 KB
/
lattice.js
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
import lodash from "lodash";
import _ from "underscore";
import { Cell } from "../cell/cell";
import { primitiveCell } from "../cell/primitive_cell";
import { HASH_TOLERANCE } from "../constants";
import math from "../math";
import { LatticeBravais } from "./lattice_bravais";
import { LatticeVectors } from "./lattice_vectors";
import { LATTICE_TYPE, LATTICE_TYPE_CONFIGS, LATTICE_TYPE_EXTENDED } from "./types";
import { UnitCell } from "./unit_cell";
/**
* Scaling factor used to calculate the new lattice size for non-periodic systems.
* The scaling factor ensures that a non-periodic structure will have have a lattice greater than the structures size.
*/
export const nonPeriodicLatticeScalingFactor = 2.0;
/*
* Container class for crystal lattice and associated methods.
* Follows Bravais convention for lattice types and contains lattice vectors within.
* Units for lattice vector coordinates are "angstroms", and "degrees" for the corresponding angles.
*/
export class Lattice extends LatticeBravais {
/**
* Create a Lattice class from a config object.
* @param {Object} config - Config object. See LatticeVectors.fromBravais.
*/
constructor(config = {}) {
super(config);
this.vectors = LatticeVectors.fromBravais(config);
}
/**
* Create a Lattice class from a list of vectors.
* @param {Object} config - Config object. See LatticeBravais.fromVectors.
*/
static fromVectors(config) {
return new Lattice(LatticeBravais.fromVectors(config).toJSON());
}
/**
* Serialize class instance to JSON.
* @example As below:
{
"a" : 3.867,
"b" : 3.867,
"c" : 3.867,
"alpha" : 60,
"beta" : 60,
"gamma" : 60,
"units" : {
"length" : "angstrom",
"angle" : "degree"
},
"type" : "FCC",
"vectors" : {
"a" : [
3.34892,
0,
1.9335
],
"b" : [
1.116307,
3.157392,
1.9335
],
"c" : [
0,
0,
3.867
],
"alat" : 1,
"units" : "angstrom"
}
}
*/
toJSON(skipRounding = false) {
const round = skipRounding ? () => {} : Lattice._roundValue; // round values by default
return {
a: round(this.a),
b: round(this.b),
c: round(this.c),
alpha: round(this.alpha),
beta: round(this.beta),
gamma: round(this.gamma),
units: {
length: this.units.length,
angle: this.units.angle,
},
type: this.type,
vectors: this.vectors.toJSON(),
};
}
clone(extraContext) {
return new this.constructor({ ...this.toJSON(), ...extraContext });
}
/**
* Get lattice vectors as a nested array
* @return {Array[]}
*/
get vectorArrays() {
return this.vectors.vectorArrays;
}
get Cell() {
return new Cell(this.vectorArrays);
}
/**
* Get a short label for the type of the lattice, eg. "MCLC".
* @return {String}
*/
get typeLabel() {
return lodash.get(
LATTICE_TYPE_CONFIGS.find((c) => c.code === this.type),
"label",
"Unknown",
);
}
/**
* Get a short label for the extended type of the lattice, eg. "MCLC-5".
* @return {String}
*/
get typeExtended() {
const { a, b, c, alpha, beta, gamma, type } = this;
const cosAlpha = math.cos((alpha / 180) * math.PI);
switch (type) {
case LATTICE_TYPE.BCT:
return c < a ? LATTICE_TYPE_EXTENDED.BCT_1 : LATTICE_TYPE_EXTENDED.BCT_2;
case LATTICE_TYPE.ORCF:
if (1 / (a * a) >= 1 / (b * b) + 1 / (c * c)) {
return LATTICE_TYPE_EXTENDED.ORCF_1;
}
return LATTICE_TYPE_EXTENDED.ORCF_2;
case LATTICE_TYPE.RHL:
return cosAlpha > 0 ? LATTICE_TYPE_EXTENDED.RHL_1 : LATTICE_TYPE_EXTENDED.RHL_2;
case LATTICE_TYPE.MCLC:
if (gamma >= 90) {
// MCLC-1,2
return LATTICE_TYPE_EXTENDED.MCLC_1;
}
if ((b / c) * cosAlpha + ((b * b) / (a * a)) * (1 - cosAlpha * cosAlpha) <= 1) {
// MCLC-3,4
return LATTICE_TYPE_EXTENDED.MCLC_3;
}
return LATTICE_TYPE_EXTENDED.MCLC_5;
case LATTICE_TYPE.TRI:
if (alpha > 90 && beta > 90 && gamma >= 90) {
// TRI-1a,2a
return LATTICE_TYPE_EXTENDED.TRI_1a;
}
return LATTICE_TYPE_EXTENDED.TRI_1b;
default:
return type;
}
}
/**
* Calculate the volume of the lattice cell.
* @return {Number}
*/
get volume() {
return math.abs(math.det(this.vectorArrays));
}
/*
* Returns a "default" primitive lattice by type, with lattice parameters scaled by the length of "a",
* @param latticeConfig {Object} LatticeBravais config (see constructor)
*/
static getDefaultPrimitiveLatticeConfigByType(latticeConfig) {
const f_ = Lattice._roundValue;
// construct new primitive cell using lattice parameters and skip rounding the vectors
const pCell = primitiveCell(latticeConfig, true);
// create new lattice from primitive cell
const newLattice = { ...Lattice.fromVectorArrays(pCell, latticeConfig.type) };
// preserve the new type and scale back the lattice parameters
const k = latticeConfig.a / newLattice.a;
return Object.assign(newLattice, {
a: f_(newLattice.a * k),
b: f_(newLattice.b * k),
c: f_(newLattice.c * k),
alpha: f_(newLattice.alpha),
beta: f_(newLattice.beta),
gamma: f_(newLattice.gamma),
});
}
// TODO: remove
get unitCell() {
const vectors = _.flatten(this.vectorArrays);
vectors.push(this.units.length);
return new UnitCell(...vectors);
}
/**
* Returns a string further used for the calculation of an unique hash.
* @param {Boolean} isScaled - Whether to scale the vectors by the length of the first vector initially.
* @return {string}
*/
getHashString(isScaled = false) {
// lattice vectors must be measured in angstroms
const latticeInAngstroms = this;
const scaleK = isScaled ? latticeInAngstroms.a : 1;
const scaledLattice = {
...latticeInAngstroms,
a: latticeInAngstroms.a / scaleK,
b: latticeInAngstroms.b / scaleK,
c: latticeInAngstroms.c / scaleK,
};
// form lattice string
return `${[
scaledLattice.a,
scaledLattice.b,
scaledLattice.c,
scaledLattice.alpha,
scaledLattice.beta,
scaledLattice.gamma,
]
.map((x) => math.round(x, HASH_TOLERANCE))
.join(";")};`;
}
}