@mat3ra/made 2026.8.7-0 → 2026.8.13-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +2 -2
- package/dist/js/Material.d.ts +219 -0
- package/dist/js/Material.js +457 -0
- package/dist/js/basis/basis.d.ts +14 -9
- package/dist/js/basis/basis.js +15 -13
- package/dist/js/basis/constrained_basis.d.ts +9 -7
- package/dist/js/basis/constrained_basis.js +4 -4
- package/dist/js/generated/MaterialSchemaMixin.d.ts +5 -0
- package/dist/js/generated/MaterialSchemaMixin.js +69 -0
- package/dist/js/lattice/lattice.d.ts +4 -3
- package/dist/js/lattice/lattice.js +1 -2
- package/dist/js/made.d.ts +12 -12
- package/dist/js/made.js +38 -5
- package/dist/js/parsers/espresso.d.ts +2 -3
- package/dist/js/parsers/espresso.js +0 -1
- package/dist/js/parsers/native_format_parsers.d.ts +9 -8
- package/dist/js/parsers/native_format_parsers.js +6 -6
- package/dist/js/parsers/parsers.d.ts +6 -6
- package/dist/js/parsers/poscar.d.ts +5 -4
- package/dist/js/parsers/poscar.js +3 -2
- package/dist/js/parsers/xyz.d.ts +3 -5
- package/dist/js/parsers/xyz.js +3 -4
- package/dist/js/parsers/xyz_combinatorial_basis.js +3 -1
- package/dist/js/tools/basis.d.ts +1 -1
- package/dist/js/tools/index.d.ts +7 -7
- package/dist/js/tools/material.d.ts +1 -1
- package/dist/js/tools/material.js +8 -7
- package/dist/js/tools/supercell.d.ts +4 -5
- package/dist/js/tools/supercell.js +2 -4
- package/dist/js/tools/surface.d.ts +4 -4
- package/dist/js/tools/surface.js +2 -7
- package/package.json +6 -6
- package/scripts/generate-mixins.ts +28 -0
- package/src/js/Material.ts +618 -0
- package/src/js/basis/basis.ts +38 -25
- package/src/js/basis/constrained_basis.ts +12 -11
- package/src/js/basis/coordinates.ts +0 -1
- package/src/js/cell/cell.ts +0 -1
- package/src/js/generated/MaterialSchemaMixin.ts +79 -0
- package/src/js/lattice/lattice.ts +6 -4
- package/src/js/lattice/reciprocal/lattice_reciprocal.ts +19 -6
- package/src/js/made.ts +1 -1
- package/src/js/parsers/espresso.ts +2 -3
- package/src/js/parsers/native_format_parsers.ts +9 -8
- package/src/js/parsers/poscar.ts +12 -6
- package/src/js/parsers/xyz.ts +9 -6
- package/src/js/parsers/xyz_combinatorial_basis.ts +5 -2
- package/src/js/tools/basis.ts +0 -1
- package/src/js/tools/cell.ts +0 -1
- package/src/js/tools/material.ts +13 -11
- package/src/js/tools/supercell.ts +8 -7
- package/src/js/tools/surface.ts +7 -17
- package/src/py/mat3ra/made/material.py +6 -5
- package/tests/fixtures/si-standata.json +2 -2
- package/tests/js/basis/basis.ts +8 -8
- package/tests/js/fixtures.ts +12 -9
- package/tests/js/material/material.ts +161 -0
- package/tests/js/parsers/espresso.ts +2 -5
- package/tests/js/parsers/poscar.ts +2 -2
- package/tests/js/register-esse-schemas.cjs +10 -0
- package/tests/js/tools/supercell.ts +2 -2
- package/tests/js/tools/surface.ts +6 -10
- package/dist/js/material.d.ts +0 -169
- package/dist/js/material.js +0 -362
- package/src/js/material.ts +0 -458
- package/tests/js/material.test.ts +0 -42
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { InMemoryEntity } from "@mat3ra/code/dist/js/entity";
|
|
2
|
+
import { EntityError } from "@mat3ra/code/dist/js/entity/in_memory";
|
|
3
|
+
import {
|
|
4
|
+
type Defaultable,
|
|
5
|
+
defaultableEntityMixin,
|
|
6
|
+
} from "@mat3ra/code/dist/js/entity/mixins/DefaultableMixin";
|
|
7
|
+
import {
|
|
8
|
+
type HasMetadata,
|
|
9
|
+
hasMetadataMixin,
|
|
10
|
+
} from "@mat3ra/code/dist/js/entity/mixins/HasMetadataMixin";
|
|
11
|
+
import {
|
|
12
|
+
type NamedEntity,
|
|
13
|
+
namedEntityMixin,
|
|
14
|
+
} from "@mat3ra/code/dist/js/entity/mixins/NamedEntityMixin";
|
|
15
|
+
import { clone, deepClone } from "@mat3ra/code/dist/js/utils/clone";
|
|
16
|
+
import JSONSchemasInterface from "@mat3ra/esse/dist/js/esse/JSONSchemasInterface";
|
|
17
|
+
import type { JSONSchema } from "@mat3ra/esse/dist/js/esse/utils";
|
|
18
|
+
import type {
|
|
19
|
+
AtomicConstraintsSchema,
|
|
20
|
+
BasisSchema,
|
|
21
|
+
ConsistencyCheck,
|
|
22
|
+
DerivedPropertiesSchema,
|
|
23
|
+
FileSourceSchema,
|
|
24
|
+
InChIRepresentationSchema,
|
|
25
|
+
LatticeSchema,
|
|
26
|
+
MaterialEnhancedHashedSchema,
|
|
27
|
+
MaterialEnhancedSchema,
|
|
28
|
+
MaterialHashedSchema,
|
|
29
|
+
MaterialSchema,
|
|
30
|
+
} from "@mat3ra/esse/dist/js/types";
|
|
31
|
+
import CryptoJS from "crypto-js";
|
|
32
|
+
|
|
33
|
+
import { type BasisConfig } from "./basis/basis";
|
|
34
|
+
import { type ConstrainedBasisConfig, ConstrainedBasis } from "./basis/constrained_basis";
|
|
35
|
+
import {
|
|
36
|
+
isConventionalCellSameAsPrimitiveForLatticeType,
|
|
37
|
+
PRIMITIVE_TO_CONVENTIONAL_CELL_LATTICE_TYPES,
|
|
38
|
+
PRIMITIVE_TO_CONVENTIONAL_CELL_MULTIPLIERS,
|
|
39
|
+
} from "./cell/conventional_cell";
|
|
40
|
+
import { Constraint } from "./constraints/constraints";
|
|
41
|
+
import { type MaterialSchemaMixin, materialSchemaMixin } from "./generated/MaterialSchemaMixin";
|
|
42
|
+
import { Lattice } from "./lattice/lattice";
|
|
43
|
+
import parsers from "./parsers/parsers";
|
|
44
|
+
import supercellTools from "./tools/supercell";
|
|
45
|
+
|
|
46
|
+
/** ESSE `$id` values for material schema variants. */
|
|
47
|
+
const MATERIAL_SCHEMA_IDS = {
|
|
48
|
+
pure: "material",
|
|
49
|
+
enhanced: "material-enhanced",
|
|
50
|
+
hashed: "material-hashed",
|
|
51
|
+
enhancedHashed: "material-enhanced-hashed",
|
|
52
|
+
} as const;
|
|
53
|
+
|
|
54
|
+
function parseConstrainedBasis(
|
|
55
|
+
textOrObject: string | BasisConfig | ConstrainedBasisConfig,
|
|
56
|
+
format?: "xyz",
|
|
57
|
+
units?: BasisSchema["units"],
|
|
58
|
+
): ConstrainedBasisConfig {
|
|
59
|
+
if (typeof textOrObject === "string") {
|
|
60
|
+
if (format !== "xyz") {
|
|
61
|
+
throw new Error("Invalid format");
|
|
62
|
+
}
|
|
63
|
+
return parsers.xyz.toBasisConfig(textOrObject, units);
|
|
64
|
+
}
|
|
65
|
+
if ("constraints" in textOrObject) {
|
|
66
|
+
return textOrObject;
|
|
67
|
+
}
|
|
68
|
+
return { ...textOrObject, constraints: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Bundle of material JSON-schema variants used by {@link Material} projections.
|
|
75
|
+
* Web-app passes extended schemas here so `toJSON*` return types stay precise.
|
|
76
|
+
*/
|
|
77
|
+
export type MaterialSchemaMap = {
|
|
78
|
+
pure: MaterialSchema;
|
|
79
|
+
enhanced: MaterialEnhancedSchema;
|
|
80
|
+
hashed: MaterialHashedSchema;
|
|
81
|
+
enhancedHashed: MaterialEnhancedHashedSchema;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Default ESSE schema map (no web-app extensions). */
|
|
85
|
+
export type DefaultMaterialSchemas = MaterialSchemaMap;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Constructor config: constraints/hash/metadata optional — normalized in the constructor
|
|
89
|
+
* (`parseConstrainedBasis` + {@link Material.updateHash}; `metadata` defaults to `{}`).
|
|
90
|
+
*/
|
|
91
|
+
export type MaterialConfig<S extends MaterialEnhancedHashedSchema = MaterialEnhancedHashedSchema> =
|
|
92
|
+
Omit<PartialBy<S, "name" | "metadata" | "hash" | "scaledHash">, "basis"> & {
|
|
93
|
+
basis: MaterialSchema["basis"] | MaterialEnhancedSchema["basis"];
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const defaultMaterialConfig: MaterialEnhancedSchema = {
|
|
97
|
+
name: "Silicon FCC",
|
|
98
|
+
basis: {
|
|
99
|
+
elements: [
|
|
100
|
+
{
|
|
101
|
+
id: 0,
|
|
102
|
+
value: "Si",
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: 1,
|
|
106
|
+
value: "Si",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
coordinates: [
|
|
110
|
+
{
|
|
111
|
+
id: 0,
|
|
112
|
+
value: [0.0, 0.0, 0.0],
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: 1,
|
|
116
|
+
value: [0.25, 0.25, 0.25],
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
units: "crystal",
|
|
120
|
+
constraints: [],
|
|
121
|
+
},
|
|
122
|
+
lattice: {
|
|
123
|
+
// Primitive cell for Diamond FCC Silicon at ambient conditions
|
|
124
|
+
type: "FCC",
|
|
125
|
+
a: 3.867,
|
|
126
|
+
b: 3.867,
|
|
127
|
+
c: 3.867,
|
|
128
|
+
alpha: 60,
|
|
129
|
+
beta: 60,
|
|
130
|
+
gamma: 60,
|
|
131
|
+
units: {
|
|
132
|
+
length: "angstrom",
|
|
133
|
+
angle: "degree",
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
metadata: {},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
interface BaseMaterial<S extends MaterialEnhancedHashedSchema = MaterialEnhancedHashedSchema>
|
|
140
|
+
extends MaterialSchemaMixin,
|
|
141
|
+
NamedEntity,
|
|
142
|
+
Defaultable,
|
|
143
|
+
Required<HasMetadata<S["metadata"]>> {}
|
|
144
|
+
|
|
145
|
+
class BaseMaterial<
|
|
146
|
+
S extends MaterialEnhancedHashedSchema = MaterialEnhancedHashedSchema,
|
|
147
|
+
> extends InMemoryEntity<S> {}
|
|
148
|
+
|
|
149
|
+
materialSchemaMixin(BaseMaterial.prototype);
|
|
150
|
+
namedEntityMixin(BaseMaterial.prototype);
|
|
151
|
+
defaultableEntityMixin(BaseMaterial);
|
|
152
|
+
hasMetadataMixin(BaseMaterial.prototype);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Unified material. Pass extended schemas via {@link MaterialSchemaMap} so each
|
|
156
|
+
* `toJSON*` returns the web-app (or other host) schema type.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* type WebappSchemas = {
|
|
161
|
+
* pure: MaterialSchema;
|
|
162
|
+
* enhanced: WebappMaterialEnhancedSchema;
|
|
163
|
+
* hashed: WebappMaterialHashedSchema;
|
|
164
|
+
* enhancedHashed: WebappMaterialEnhancedSchema;
|
|
165
|
+
* };
|
|
166
|
+
* class CoreMaterial extends Material<WebappSchemas> {}
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
class Material<Schemas extends MaterialSchemaMap = DefaultMaterialSchemas> extends BaseMaterial<
|
|
170
|
+
Schemas["enhancedHashed"]
|
|
171
|
+
> {
|
|
172
|
+
declare static createDefault: () => Material<MaterialSchemaMap>;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Schema used by {@link InMemoryEntity.clean} / {@link toJSON}.
|
|
176
|
+
* Defaults to enhanced+hashed; subclasses / web-app Core* may override.
|
|
177
|
+
*/
|
|
178
|
+
static get jsonSchema(): JSONSchema {
|
|
179
|
+
return this.jsonSchemaEnhancedHashed;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Schema for {@link toJSONPure} — override in web-app if the base material schema is extended. */
|
|
183
|
+
static get jsonSchemaPure(): JSONSchema {
|
|
184
|
+
return JSONSchemasInterface.getRequiredSchemaById(MATERIAL_SCHEMA_IDS.pure);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Schema for {@link toJSONEnhanced}. */
|
|
188
|
+
static get jsonSchemaEnhanced(): JSONSchema {
|
|
189
|
+
return JSONSchemasInterface.getRequiredSchemaById(MATERIAL_SCHEMA_IDS.enhanced);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Schema for {@link toJSONHashed}. */
|
|
193
|
+
static get jsonSchemaHashed(): JSONSchema {
|
|
194
|
+
return JSONSchemasInterface.getRequiredSchemaById(MATERIAL_SCHEMA_IDS.hashed);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Schema for {@link toJSONEnhancedHashed}. */
|
|
198
|
+
static get jsonSchemaEnhancedHashed(): JSONSchema {
|
|
199
|
+
return JSONSchemasInterface.getRequiredSchemaById(MATERIAL_SCHEMA_IDS.enhancedHashed);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
static get defaultConfig(): MaterialEnhancedSchema {
|
|
203
|
+
return defaultMaterialConfig;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static constructMaterialFileSource(
|
|
207
|
+
fileName: string,
|
|
208
|
+
fileContent: string,
|
|
209
|
+
fileExtension: string,
|
|
210
|
+
): FileSourceSchema {
|
|
211
|
+
return {
|
|
212
|
+
extension: fileExtension,
|
|
213
|
+
filename: fileName,
|
|
214
|
+
text: fileContent,
|
|
215
|
+
hash: CryptoJS.MD5(fileContent).toString(),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @param config - Partial entity input. `basis.constraints` / `hash` / `metadata` may be omitted;
|
|
221
|
+
* constraints and hash are filled here; missing `metadata` becomes `{}`.
|
|
222
|
+
* `NoInfer` keeps `Schemas` from being inferred from the config object literal.
|
|
223
|
+
*/
|
|
224
|
+
constructor(config: NoInfer<MaterialConfig>) {
|
|
225
|
+
const basis = parseConstrainedBasis(config.basis);
|
|
226
|
+
|
|
227
|
+
super({
|
|
228
|
+
...config,
|
|
229
|
+
basis,
|
|
230
|
+
name: config.name ?? config.formula ?? "",
|
|
231
|
+
metadata: config.metadata ?? {},
|
|
232
|
+
hash: config.hash ?? "",
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
this.formula = config.formula || this.getBasis().formula;
|
|
236
|
+
this.name = this.name || this.formula;
|
|
237
|
+
this.updateHash();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
get hash(): Schemas["enhancedHashed"]["hash"] {
|
|
241
|
+
return this.requiredProp("hash");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
set hash(value: Schemas["enhancedHashed"]["hash"]) {
|
|
245
|
+
this.setProp("hash", value);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get scaledHash(): Schemas["enhancedHashed"]["scaledHash"] {
|
|
249
|
+
return this.prop("scaledHash");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
set scaledHash(value: Schemas["enhancedHashed"]["scaledHash"]) {
|
|
253
|
+
this.setProp("scaledHash", value);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Recompute and store {@link hash} from the current basis/lattice (or InChI if non-periodic).
|
|
258
|
+
* When non-periodic but InChI is not derived yet (e.g. designer toggle before butler), use
|
|
259
|
+
* geometric hash so setters do not throw — same practical UX as main before auto-updateHash.
|
|
260
|
+
*/
|
|
261
|
+
updateHash(): void {
|
|
262
|
+
const hasInchi = Boolean(this.getDerivedPropertyByName("inchi"));
|
|
263
|
+
this.hash =
|
|
264
|
+
this.isNonPeriodic && !hasInchi
|
|
265
|
+
? this.calculateHash("", false, true)
|
|
266
|
+
: this.calculateHash();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Override schema-mixin accessors so basis/lattice changes keep hash in sync.
|
|
270
|
+
get basis(): Schemas["enhancedHashed"]["basis"] {
|
|
271
|
+
return this.requiredProp("basis");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
set basis(value: Schemas["enhancedHashed"]["basis"]) {
|
|
275
|
+
this.setProp("basis", value);
|
|
276
|
+
this.updateHash();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
get lattice(): Schemas["enhancedHashed"]["lattice"] {
|
|
280
|
+
return this.requiredProp("lattice");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
set lattice(value: Schemas["enhancedHashed"]["lattice"]) {
|
|
284
|
+
this.setProp("lattice", value);
|
|
285
|
+
this.updateHash();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
updateFormula() {
|
|
289
|
+
const basis = this.getBasis();
|
|
290
|
+
this.formula = basis.formula;
|
|
291
|
+
this.unitCellFormula = basis.unitCellFormula;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @summary Returns the specific derived property (as specified by name) for a material.
|
|
296
|
+
*/
|
|
297
|
+
getDerivedPropertyByName(name: string) {
|
|
298
|
+
return this.getDerivedProperties().find((x) => x.name === name);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @summary Returns the derived properties array for a material.
|
|
303
|
+
*/
|
|
304
|
+
getDerivedProperties(): DerivedPropertiesSchema {
|
|
305
|
+
return this.derivedProperties ?? [];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
unsetFileProps() {
|
|
309
|
+
this.unsetProp("src");
|
|
310
|
+
this.unsetProp("icsdId");
|
|
311
|
+
this.unsetProp("external");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
setBasis(basis: BasisConfig | ConstrainedBasisConfig): void;
|
|
315
|
+
|
|
316
|
+
setBasis(basis: string, format: "xyz", units?: BasisSchema["units"]): void;
|
|
317
|
+
|
|
318
|
+
setBasis(
|
|
319
|
+
textOrObject: string | BasisConfig | ConstrainedBasisConfig,
|
|
320
|
+
format?: "xyz",
|
|
321
|
+
units?: BasisSchema["units"],
|
|
322
|
+
) {
|
|
323
|
+
this.basis = parseConstrainedBasis(textOrObject, format, units);
|
|
324
|
+
this.unsetFileProps();
|
|
325
|
+
this.updateFormula();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private setBasisConstraints(constraints: Constraint[]) {
|
|
329
|
+
this.setBasis({
|
|
330
|
+
...this.basis,
|
|
331
|
+
constraints: constraints.map((constraint) => ({
|
|
332
|
+
id: constraint.id,
|
|
333
|
+
value: constraint.value,
|
|
334
|
+
})),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
setBasisConstraintsFromArrayOfObjects(constraints: AtomicConstraintsSchema) {
|
|
339
|
+
this.setBasisConstraints(
|
|
340
|
+
constraints.map((constraint) =>
|
|
341
|
+
Constraint.fromValueAndId(constraint.value, constraint.id),
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
getBasis(): ConstrainedBasis {
|
|
347
|
+
return new ConstrainedBasis({
|
|
348
|
+
...parseConstrainedBasis(this.basis),
|
|
349
|
+
cell: this.getLattice().vectors,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
setLattice(lattice: LatticeSchema) {
|
|
354
|
+
const basis = this.getBasis();
|
|
355
|
+
const originalIsInCrystalUnits = basis.isInCrystalUnits;
|
|
356
|
+
|
|
357
|
+
basis.toCartesian();
|
|
358
|
+
basis.cell = new Lattice(lattice).vectors;
|
|
359
|
+
|
|
360
|
+
if (originalIsInCrystalUnits) {
|
|
361
|
+
basis.toCrystal();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
this.basis = basis.toJSON();
|
|
365
|
+
this.lattice = lattice;
|
|
366
|
+
|
|
367
|
+
this.unsetFileProps();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
getLattice() {
|
|
371
|
+
return new Lattice(this.lattice);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* High-level access to unique elements from material instead of basis.
|
|
376
|
+
*/
|
|
377
|
+
get uniqueElements() {
|
|
378
|
+
return this.getBasis().uniqueElements;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Returns the inchi string from the derivedProperties for a non-periodic material, or throws an error if the
|
|
383
|
+
* inchi cannot be found.
|
|
384
|
+
* @returns {String}
|
|
385
|
+
*/
|
|
386
|
+
getInchiStringForHash(): string {
|
|
387
|
+
const inchi = this.getDerivedPropertyByName("inchi");
|
|
388
|
+
if (inchi) {
|
|
389
|
+
return (inchi as InChIRepresentationSchema).value;
|
|
390
|
+
}
|
|
391
|
+
throw new Error("Hash cannot be created. Missing InChI string in derivedProperties");
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Calculates hash from basis and lattice. Algorithm expects the following:
|
|
396
|
+
* - asserts lattice units to be angstrom
|
|
397
|
+
* - asserts basis units to be crystal
|
|
398
|
+
* - asserts basis coordinates and lattice measurements are rounded to hash precision
|
|
399
|
+
* - forms strings for lattice and basis
|
|
400
|
+
* - creates MD5 hash from basisStr + latticeStr + salt
|
|
401
|
+
* @param salt Salt for hashing, empty string by default.
|
|
402
|
+
* @param isScaled Whether to scale the lattice parameter 'a' to 1.
|
|
403
|
+
*/
|
|
404
|
+
calculateHash(salt = "", isScaled = false, bypassNonPeriodicCheck = false): string {
|
|
405
|
+
let message;
|
|
406
|
+
if (!this.isNonPeriodic || bypassNonPeriodicCheck) {
|
|
407
|
+
message =
|
|
408
|
+
this.getBasis().hashString +
|
|
409
|
+
"#" +
|
|
410
|
+
this.getLattice().getHashString(isScaled) +
|
|
411
|
+
"#" +
|
|
412
|
+
salt;
|
|
413
|
+
} else {
|
|
414
|
+
message = this.getInchiStringForHash();
|
|
415
|
+
}
|
|
416
|
+
return CryptoJS.MD5(message).toString();
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Converts basis to crystal/fractional coordinates.
|
|
421
|
+
*/
|
|
422
|
+
toCrystal() {
|
|
423
|
+
this.setBasis(this.getBasis().toCrystal().toJSON());
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Converts current material's basis coordinates to cartesian.
|
|
428
|
+
* No changes if coordinates already cartesian.
|
|
429
|
+
*/
|
|
430
|
+
toCartesian() {
|
|
431
|
+
this.setBasis(this.getBasis().toCartesian().toJSON());
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Returns material's basis in XYZ format (includes atomic constraints when present).
|
|
436
|
+
*/
|
|
437
|
+
getBasisAsXyz(fractional = false): string {
|
|
438
|
+
return parsers.xyz.fromMaterial(this.toJSONEnhanced(), fractional);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Returns material in Quantum Espresso output format:
|
|
443
|
+
* ```
|
|
444
|
+
* CELL_PARAMETERS (angstroms)
|
|
445
|
+
* -0.543131284 -0.000000000 0.543131284
|
|
446
|
+
* -0.000000000 0.543131284 0.543131284
|
|
447
|
+
* -0.543131284 0.543131284 0.000000000
|
|
448
|
+
*
|
|
449
|
+
* ATOMIC_POSITIONS (crystal)
|
|
450
|
+
* Si 0.000000000 0.000000000 -0.000000000
|
|
451
|
+
* Si 0.250000000 0.250000000 0.250000000
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
getAsQEFormat(): string {
|
|
455
|
+
return parsers.espresso.toEspressoFormat(this.toJSONPure());
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Returns material in POSCAR format. Pass `true` to ignore original poscar source and re-serialize.
|
|
460
|
+
*/
|
|
461
|
+
getAsPOSCAR(ignoreOriginal = false, omitConstraints = false): string {
|
|
462
|
+
// By default return original source if exists
|
|
463
|
+
if (this.src?.extension === "poscar" && !ignoreOriginal) {
|
|
464
|
+
return this.src.text;
|
|
465
|
+
}
|
|
466
|
+
return parsers.poscar.toPoscar(this.toJSONEnhanced(), omitConstraints);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Returns a copy of the material with conventional cell constructed instead of primitive.
|
|
471
|
+
*/
|
|
472
|
+
getACopyWithConventionalCell(): this {
|
|
473
|
+
const material = this.clone();
|
|
474
|
+
|
|
475
|
+
const lattice = this.getLattice();
|
|
476
|
+
|
|
477
|
+
// if conventional and primitive cells are the same => return a copy.
|
|
478
|
+
if (isConventionalCellSameAsPrimitiveForLatticeType(lattice.type)) {
|
|
479
|
+
return material;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const conventionalSupercellMatrix =
|
|
483
|
+
PRIMITIVE_TO_CONVENTIONAL_CELL_MULTIPLIERS[lattice.type];
|
|
484
|
+
const conventionalLatticeType = PRIMITIVE_TO_CONVENTIONAL_CELL_LATTICE_TYPES[lattice.type];
|
|
485
|
+
const config = supercellTools.generateConfig(this, conventionalSupercellMatrix);
|
|
486
|
+
|
|
487
|
+
config.lattice.type = conventionalLatticeType;
|
|
488
|
+
config.name = `${this.name} - conventional cell`;
|
|
489
|
+
|
|
490
|
+
return this.clone(config);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* @summary a series of checks for the material and returns an array of results in ConsistencyChecks format.
|
|
495
|
+
* @returns Array of checks results
|
|
496
|
+
*/
|
|
497
|
+
getConsistencyChecks(): ConsistencyCheck[] {
|
|
498
|
+
const basisChecks = this.getBasisConsistencyChecks();
|
|
499
|
+
|
|
500
|
+
// any other Material checks can be added here
|
|
501
|
+
|
|
502
|
+
return basisChecks;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* @summary a series of checks for the material's basis and returns an array of results in ConsistencyChecks format.
|
|
507
|
+
* @returns Array of checks results
|
|
508
|
+
*/
|
|
509
|
+
getBasisConsistencyChecks(): ConsistencyCheck[] {
|
|
510
|
+
const checks: ConsistencyCheck[] = [];
|
|
511
|
+
const limit = 1000;
|
|
512
|
+
const basis = this.getBasis();
|
|
513
|
+
|
|
514
|
+
if (basis.elements.length < limit) {
|
|
515
|
+
const overlappingAtomsGroups = basis.getOverlappingAtoms();
|
|
516
|
+
overlappingAtomsGroups.forEach(({ id1, id2, element1, element2 }) => {
|
|
517
|
+
checks.push(
|
|
518
|
+
{
|
|
519
|
+
key: `basis.coordinates.${id1}`,
|
|
520
|
+
name: "atomsOverlap",
|
|
521
|
+
severity: "warning",
|
|
522
|
+
message: `Atom ${element1} is too close to ${element2} at position ${
|
|
523
|
+
id2 + 1
|
|
524
|
+
}`,
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
key: `basis.coordinates.${id2}`,
|
|
528
|
+
name: "atomsOverlap",
|
|
529
|
+
severity: "warning",
|
|
530
|
+
message: `Atom ${element2} is too close to ${element1} at position ${
|
|
531
|
+
id1 + 1
|
|
532
|
+
}`,
|
|
533
|
+
},
|
|
534
|
+
);
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return checks;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Full material JSON: constrained basis + hash fields from live getters.
|
|
543
|
+
* Variants below AJV-clean this payload against the matching ESSE schema.
|
|
544
|
+
* Builds from `_json` (not `super.toJSON`) so we do not pre-clean against
|
|
545
|
+
* {@link Material.jsonSchema} before projecting to a narrower schema.
|
|
546
|
+
*/
|
|
547
|
+
private getFullJSON(): Schemas["enhancedHashed"] {
|
|
548
|
+
const fullJSON = {
|
|
549
|
+
...clone(this._json),
|
|
550
|
+
lattice: this.getLattice().toJSON(),
|
|
551
|
+
basis: this.getBasis().toJSON(),
|
|
552
|
+
isNonPeriodic: this.isNonPeriodic,
|
|
553
|
+
hash: this.hash,
|
|
554
|
+
} as Schemas["enhancedHashed"];
|
|
555
|
+
if (this.scaledHash !== undefined) {
|
|
556
|
+
fullJSON.scaledHash = this.scaledHash;
|
|
557
|
+
}
|
|
558
|
+
return fullJSON;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Clone {@link getFullJSON} and validate/clean against `jsonSchema` via AJV
|
|
563
|
+
* (same path as {@link InMemoryEntity.validateData} / {@link InMemoryEntity.clean}).
|
|
564
|
+
*/
|
|
565
|
+
private cleanFullJSONAgainstSchema(jsonSchema: JSONSchema): object {
|
|
566
|
+
try {
|
|
567
|
+
return (this.constructor as typeof Material).validateData(
|
|
568
|
+
deepClone(this.getFullJSON()),
|
|
569
|
+
true,
|
|
570
|
+
jsonSchema,
|
|
571
|
+
);
|
|
572
|
+
} catch (err) {
|
|
573
|
+
// validateData throws EntityError with only the code as message — log details for DevTools.
|
|
574
|
+
if (err instanceof EntityError) {
|
|
575
|
+
console.error("Material.toJSON validation failed", {
|
|
576
|
+
code: err.code,
|
|
577
|
+
error: err.details?.error,
|
|
578
|
+
json: err.details?.json,
|
|
579
|
+
schema: err.details?.schema,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
toJSONPure(): Schemas["pure"] {
|
|
587
|
+
return this.cleanFullJSONAgainstSchema(
|
|
588
|
+
(this.constructor as typeof Material).jsonSchemaPure,
|
|
589
|
+
) as Schemas["pure"];
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
toJSONEnhanced(): Schemas["enhanced"] {
|
|
593
|
+
return this.cleanFullJSONAgainstSchema(
|
|
594
|
+
(this.constructor as typeof Material).jsonSchemaEnhanced,
|
|
595
|
+
) as Schemas["enhanced"];
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
toJSONHashed(): Schemas["hashed"] {
|
|
599
|
+
return this.cleanFullJSONAgainstSchema(
|
|
600
|
+
(this.constructor as typeof Material).jsonSchemaHashed,
|
|
601
|
+
) as Schemas["hashed"];
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
toJSONEnhancedHashed(): Schemas["enhancedHashed"] {
|
|
605
|
+
return this.cleanFullJSONAgainstSchema(
|
|
606
|
+
(this.constructor as typeof Material).jsonSchemaEnhancedHashed,
|
|
607
|
+
) as Schemas["enhancedHashed"];
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
toJSON(): Schemas["enhancedHashed"] {
|
|
611
|
+
// Same payload as toJSONEnhancedHashed when jsonSchema is the enhanced-hashed default.
|
|
612
|
+
return this.cleanFullJSONAgainstSchema(
|
|
613
|
+
(this.constructor as typeof Material).jsonSchema,
|
|
614
|
+
) as Schemas["enhancedHashed"];
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export default Material;
|