matterviz 0.3.0 → 0.3.2
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/dist/FilePicker.svelte +37 -20
- package/dist/Icon.svelte +2 -2
- package/dist/MillerIndexInput.svelte +60 -0
- package/dist/MillerIndexInput.svelte.d.ts +7 -0
- package/dist/app.css +38 -2
- package/dist/brillouin/BrillouinZone.svelte +20 -62
- package/dist/brillouin/BrillouinZone.svelte.d.ts +1 -1
- package/dist/brillouin/BrillouinZoneExportPane.svelte +12 -20
- package/dist/brillouin/BrillouinZoneScene.svelte +2 -2
- package/dist/brillouin/BrillouinZoneScene.svelte.d.ts +1 -1
- package/dist/chempot-diagram/ChemPotDiagram.svelte +192 -0
- package/dist/chempot-diagram/ChemPotDiagram.svelte.d.ts +13 -0
- package/dist/chempot-diagram/ChemPotDiagram2D.svelte +677 -0
- package/dist/chempot-diagram/ChemPotDiagram2D.svelte.d.ts +16 -0
- package/dist/chempot-diagram/ChemPotDiagram3D.svelte +2688 -0
- package/dist/chempot-diagram/ChemPotDiagram3D.svelte.d.ts +16 -0
- package/dist/chempot-diagram/ChemPotScene3D.svelte +8 -0
- package/dist/chempot-diagram/ChemPotScene3D.svelte.d.ts +7 -0
- package/dist/chempot-diagram/color.d.ts +10 -0
- package/dist/chempot-diagram/color.js +33 -0
- package/dist/chempot-diagram/compute.d.ts +38 -0
- package/dist/chempot-diagram/compute.js +650 -0
- package/dist/chempot-diagram/index.d.ts +5 -0
- package/dist/chempot-diagram/index.js +5 -0
- package/dist/chempot-diagram/pointer.d.ts +16 -0
- package/dist/chempot-diagram/pointer.js +40 -0
- package/dist/chempot-diagram/temperature.d.ts +15 -0
- package/dist/chempot-diagram/temperature.js +37 -0
- package/dist/chempot-diagram/types.d.ts +83 -0
- package/dist/chempot-diagram/types.js +27 -0
- package/dist/colors/index.d.ts +3 -1
- package/dist/colors/index.js +4 -0
- package/dist/composition/BarChart.svelte +13 -22
- package/dist/composition/BubbleChart.svelte +5 -3
- package/dist/composition/FormulaFilter.svelte +770 -90
- package/dist/composition/FormulaFilter.svelte.d.ts +37 -1
- package/dist/composition/PieChart.svelte +43 -18
- package/dist/composition/PieChart.svelte.d.ts +1 -1
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -0
- package/dist/convex-hull/ConvexHull.svelte +14 -1
- package/dist/convex-hull/ConvexHull.svelte.d.ts +1 -1
- package/dist/convex-hull/ConvexHull2D.svelte +14 -45
- package/dist/convex-hull/ConvexHull2D.svelte.d.ts +1 -1
- package/dist/convex-hull/ConvexHull3D.svelte +396 -134
- package/dist/convex-hull/ConvexHull3D.svelte.d.ts +1 -1
- package/dist/convex-hull/ConvexHull4D.svelte +93 -42
- package/dist/convex-hull/ConvexHull4D.svelte.d.ts +1 -1
- package/dist/convex-hull/ConvexHullControls.svelte +94 -31
- package/dist/convex-hull/ConvexHullControls.svelte.d.ts +4 -2
- package/dist/convex-hull/ConvexHullStats.svelte +697 -128
- package/dist/convex-hull/ConvexHullStats.svelte.d.ts +6 -1
- package/dist/convex-hull/ConvexHullTooltip.svelte +1 -0
- package/dist/convex-hull/GasPressureControls.svelte +72 -38
- package/dist/convex-hull/GasPressureControls.svelte.d.ts +2 -1
- package/dist/convex-hull/TemperatureSlider.svelte +46 -19
- package/dist/convex-hull/TemperatureSlider.svelte.d.ts +2 -1
- package/dist/convex-hull/demo-temperature.d.ts +6 -0
- package/dist/convex-hull/demo-temperature.js +36 -0
- package/dist/convex-hull/gas-thermodynamics.js +16 -5
- package/dist/convex-hull/helpers.d.ts +7 -1
- package/dist/convex-hull/helpers.js +45 -15
- package/dist/convex-hull/index.d.ts +15 -1
- package/dist/convex-hull/index.js +1 -0
- package/dist/convex-hull/thermodynamics.d.ts +8 -21
- package/dist/convex-hull/thermodynamics.js +106 -17
- package/dist/convex-hull/types.d.ts +7 -0
- package/dist/convex-hull/types.js +11 -0
- package/dist/coordination/CoordinationBarPlot.svelte +29 -46
- package/dist/element/BohrAtom.svelte +1 -1
- package/dist/element/data.js +2 -14
- package/dist/element/data.json.gz +0 -0
- package/dist/element/index.d.ts +1 -1
- package/dist/element/index.js +1 -0
- package/dist/element/types.d.ts +1 -0
- package/dist/fermi-surface/FermiSurface.svelte +21 -65
- package/dist/fermi-surface/FermiSurface.svelte.d.ts +1 -1
- package/dist/fermi-surface/FermiSurfaceControls.svelte.d.ts +1 -1
- package/dist/fermi-surface/FermiSurfaceScene.svelte +1 -1
- package/dist/fermi-surface/FermiSurfaceScene.svelte.d.ts +1 -1
- package/dist/fermi-surface/compute.js +1 -21
- package/dist/fermi-surface/marching-cubes.d.ts +2 -13
- package/dist/fermi-surface/marching-cubes.js +2 -519
- package/dist/fermi-surface/parse.js +17 -23
- package/dist/heatmap-matrix/HeatmapMatrix.svelte +1273 -0
- package/dist/heatmap-matrix/HeatmapMatrix.svelte.d.ts +110 -0
- package/dist/heatmap-matrix/HeatmapMatrixControls.svelte +171 -0
- package/dist/heatmap-matrix/HeatmapMatrixControls.svelte.d.ts +31 -0
- package/dist/heatmap-matrix/index.d.ts +53 -0
- package/dist/heatmap-matrix/index.js +100 -0
- package/dist/heatmap-matrix/shared.d.ts +2 -0
- package/dist/heatmap-matrix/shared.js +4 -0
- package/dist/icons.d.ts +119 -0
- package/dist/icons.js +119 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +6 -1
- package/dist/io/export.js +15 -3
- package/dist/io/file-drop.d.ts +7 -0
- package/dist/io/file-drop.js +43 -0
- package/dist/io/index.d.ts +2 -2
- package/dist/io/index.js +2 -112
- package/dist/io/types.d.ts +1 -0
- package/dist/io/url-drop.d.ts +2 -0
- package/dist/io/url-drop.js +118 -0
- package/dist/isosurface/Isosurface.svelte +231 -0
- package/dist/isosurface/Isosurface.svelte.d.ts +8 -0
- package/dist/isosurface/IsosurfaceControls.svelte +273 -0
- package/dist/isosurface/IsosurfaceControls.svelte.d.ts +9 -0
- package/dist/isosurface/index.d.ts +5 -0
- package/dist/isosurface/index.js +6 -0
- package/dist/isosurface/parse.d.ts +6 -0
- package/dist/isosurface/parse.js +548 -0
- package/dist/isosurface/slice.d.ts +11 -0
- package/dist/isosurface/slice.js +145 -0
- package/dist/isosurface/types.d.ts +55 -0
- package/dist/isosurface/types.js +178 -0
- package/dist/labels.d.ts +2 -1
- package/dist/labels.js +1 -0
- package/dist/layout/InfoTag.svelte +62 -62
- package/dist/layout/SubpageGrid.svelte +74 -0
- package/dist/layout/SubpageGrid.svelte.d.ts +14 -0
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/json-tree/JsonNode.svelte +226 -53
- package/dist/layout/json-tree/JsonTree.svelte +425 -51
- package/dist/layout/json-tree/JsonTree.svelte.d.ts +1 -1
- package/dist/layout/json-tree/JsonValue.svelte +218 -97
- package/dist/layout/json-tree/types.d.ts +27 -2
- package/dist/layout/json-tree/utils.d.ts +14 -1
- package/dist/layout/json-tree/utils.js +254 -0
- package/dist/marching-cubes.d.ts +14 -0
- package/dist/marching-cubes.js +519 -0
- package/dist/math.d.ts +8 -0
- package/dist/math.js +374 -7
- package/dist/overlays/ContextMenu.svelte +3 -2
- package/dist/overlays/DraggablePane.svelte +163 -58
- package/dist/overlays/DraggablePane.svelte.d.ts +2 -0
- package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte +232 -77
- package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte.d.ts +6 -2
- package/dist/phase-diagram/PhaseDiagramControls.svelte +32 -11
- package/dist/phase-diagram/PhaseDiagramControls.svelte.d.ts +3 -2
- package/dist/phase-diagram/PhaseDiagramEditorPane.svelte +103 -0
- package/dist/phase-diagram/PhaseDiagramEditorPane.svelte.d.ts +15 -0
- package/dist/phase-diagram/PhaseDiagramExportPane.svelte +102 -95
- package/dist/phase-diagram/PhaseDiagramExportPane.svelte.d.ts +7 -0
- package/dist/phase-diagram/PhaseDiagramTooltip.svelte +100 -26
- package/dist/phase-diagram/PhaseDiagramTooltip.svelte.d.ts +6 -3
- package/dist/phase-diagram/index.d.ts +2 -0
- package/dist/phase-diagram/index.js +2 -0
- package/dist/phase-diagram/svg-to-diagram.d.ts +2 -0
- package/dist/phase-diagram/svg-to-diagram.js +865 -0
- package/dist/phase-diagram/types.d.ts +10 -0
- package/dist/phase-diagram/utils.d.ts +7 -4
- package/dist/phase-diagram/utils.js +149 -59
- package/dist/plot/AxisLabel.svelte +26 -0
- package/dist/plot/AxisLabel.svelte.d.ts +16 -0
- package/dist/plot/BarPlot.svelte +473 -228
- package/dist/plot/BarPlot.svelte.d.ts +3 -3
- package/dist/plot/BarPlotControls.svelte +3 -2
- package/dist/plot/BarPlotControls.svelte.d.ts +1 -1
- package/dist/plot/ColorBar.svelte +54 -54
- package/dist/plot/ColorBar.svelte.d.ts +1 -1
- package/dist/plot/ElementScatter.svelte +4 -3
- package/dist/plot/FillArea.svelte +4 -1
- package/dist/plot/Histogram.svelte +320 -230
- package/dist/plot/Histogram.svelte.d.ts +2 -2
- package/dist/plot/HistogramControls.svelte +29 -10
- package/dist/plot/HistogramControls.svelte.d.ts +6 -2
- package/dist/plot/InteractiveAxisLabel.svelte.d.ts +2 -2
- package/dist/plot/PlotControls.svelte +109 -27
- package/dist/plot/PlotControls.svelte.d.ts +1 -1
- package/dist/plot/PlotLegend.svelte +1 -1
- package/dist/plot/PortalSelect.svelte +2 -1
- package/dist/plot/ReferenceLine.svelte +2 -1
- package/dist/plot/ReferenceLine.svelte.d.ts +1 -0
- package/dist/plot/ReferencePlane.svelte +1 -3
- package/dist/plot/ScatterPlot.svelte +343 -209
- package/dist/plot/ScatterPlot.svelte.d.ts +3 -3
- package/dist/plot/ScatterPlot3D.svelte.d.ts +2 -2
- package/dist/plot/ScatterPlot3DControls.svelte +203 -250
- package/dist/plot/ScatterPlot3DScene.svelte +4 -7
- package/dist/plot/ScatterPlot3DScene.svelte.d.ts +2 -2
- package/dist/plot/ScatterPlotControls.svelte +95 -55
- package/dist/plot/ScatterPlotControls.svelte.d.ts +1 -1
- package/dist/plot/ZeroLines.svelte +44 -0
- package/dist/plot/ZeroLines.svelte.d.ts +32 -0
- package/dist/plot/ZoomRect.svelte +21 -0
- package/dist/plot/ZoomRect.svelte.d.ts +8 -0
- package/dist/plot/axis-utils.d.ts +1 -1
- package/dist/plot/data-cleaning.js +1 -5
- package/dist/plot/index.d.ts +6 -2
- package/dist/plot/index.js +6 -2
- package/dist/plot/interactions.d.ts +8 -10
- package/dist/plot/interactions.js +10 -19
- package/dist/plot/layout.d.ts +7 -1
- package/dist/plot/layout.js +12 -4
- package/dist/plot/reference-line.d.ts +4 -21
- package/dist/plot/reference-line.js +7 -81
- package/dist/plot/types.d.ts +42 -17
- package/dist/plot/types.js +10 -0
- package/dist/plot/utils/label-placement.js +14 -11
- package/dist/plot/utils.d.ts +1 -0
- package/dist/plot/utils.js +14 -0
- package/dist/rdf/RdfPlot.svelte +55 -66
- package/dist/rdf/RdfPlot.svelte.d.ts +1 -1
- package/dist/rdf/index.d.ts +1 -1
- package/dist/rdf/index.js +1 -1
- package/dist/settings.d.ts +5 -0
- package/dist/settings.js +37 -3
- package/dist/spectral/Bands.svelte +515 -143
- package/dist/spectral/Bands.svelte.d.ts +22 -2
- package/dist/spectral/helpers.d.ts +23 -1
- package/dist/spectral/helpers.js +65 -9
- package/dist/spectral/types.d.ts +2 -0
- package/dist/structure/AtomLegend.svelte +31 -10
- package/dist/structure/AtomLegend.svelte.d.ts +1 -1
- package/dist/structure/CellSelect.svelte +92 -22
- package/dist/structure/Lattice.svelte +2 -0
- package/dist/structure/Structure.svelte +716 -173
- package/dist/structure/Structure.svelte.d.ts +7 -2
- package/dist/structure/StructureControls.svelte +26 -14
- package/dist/structure/StructureControls.svelte.d.ts +5 -1
- package/dist/structure/StructureInfoPane.svelte +7 -1
- package/dist/structure/StructureScene.svelte +386 -95
- package/dist/structure/StructureScene.svelte.d.ts +15 -4
- package/dist/structure/atom-properties.d.ts +6 -2
- package/dist/structure/atom-properties.js +38 -25
- package/dist/structure/export.js +10 -7
- package/dist/structure/ferrox-wasm-types.d.ts +3 -2
- package/dist/structure/ferrox-wasm-types.js +0 -3
- package/dist/structure/ferrox-wasm.d.ts +3 -2
- package/dist/structure/ferrox-wasm.js +1 -2
- package/dist/structure/index.d.ts +7 -0
- package/dist/structure/index.js +22 -0
- package/dist/structure/parse.js +19 -16
- package/dist/structure/partial-occupancy.d.ts +25 -0
- package/dist/structure/partial-occupancy.js +102 -0
- package/dist/structure/validation.js +6 -3
- package/dist/symmetry/SymmetryStats.svelte +18 -4
- package/dist/symmetry/WyckoffTable.svelte +18 -10
- package/dist/symmetry/index.d.ts +7 -4
- package/dist/symmetry/index.js +83 -18
- package/dist/table/HeatmapTable.svelte +468 -69
- package/dist/table/HeatmapTable.svelte.d.ts +13 -1
- package/dist/table/ToggleMenu.svelte +291 -44
- package/dist/table/ToggleMenu.svelte.d.ts +4 -1
- package/dist/table/index.d.ts +3 -0
- package/dist/tooltip/index.d.ts +1 -1
- package/dist/tooltip/index.js +1 -0
- package/dist/trajectory/Trajectory.svelte +147 -145
- package/dist/trajectory/TrajectoryExportPane.svelte +13 -9
- package/dist/trajectory/TrajectoryExportPane.svelte.d.ts +1 -1
- package/dist/trajectory/constants.d.ts +6 -0
- package/dist/trajectory/constants.js +7 -0
- package/dist/trajectory/extract.js +3 -5
- package/dist/trajectory/format-detect.d.ts +9 -0
- package/dist/trajectory/format-detect.js +76 -0
- package/dist/trajectory/frame-reader.d.ts +17 -0
- package/dist/trajectory/frame-reader.js +339 -0
- package/dist/trajectory/helpers.d.ts +15 -0
- package/dist/trajectory/helpers.js +187 -0
- package/dist/trajectory/index.d.ts +1 -0
- package/dist/trajectory/index.js +11 -4
- package/dist/trajectory/parse/ase.d.ts +2 -0
- package/dist/trajectory/parse/ase.js +76 -0
- package/dist/trajectory/parse/hdf5.d.ts +2 -0
- package/dist/trajectory/parse/hdf5.js +121 -0
- package/dist/trajectory/parse/index.d.ts +12 -0
- package/dist/trajectory/parse/index.js +304 -0
- package/dist/trajectory/parse/lammps.d.ts +5 -0
- package/dist/trajectory/parse/lammps.js +169 -0
- package/dist/trajectory/parse/vasp.d.ts +2 -0
- package/dist/trajectory/parse/vasp.js +65 -0
- package/dist/trajectory/parse/xyz.d.ts +2 -0
- package/dist/trajectory/parse/xyz.js +109 -0
- package/dist/trajectory/types.d.ts +11 -0
- package/dist/trajectory/types.js +1 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +4 -0
- package/dist/xrd/XrdPlot.svelte +6 -4
- package/dist/xrd/calc-xrd.js +0 -1
- package/package.json +33 -23
- package/readme.md +4 -4
- package/dist/trajectory/parse.d.ts +0 -42
- package/dist/trajectory/parse.js +0 -1267
- /package/dist/element/{data.json.d.ts → data.json.gz.d.ts} +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// HDF5 trajectory parsing (torch-sim / generic format)
|
|
2
|
+
import { calc_lattice_params, transpose_3x3_matrix } from '../../math';
|
|
3
|
+
import * as h5wasm from 'h5wasm';
|
|
4
|
+
import { convert_atomic_numbers, create_trajectory_frame, validate_3x3_matrix, } from '../helpers';
|
|
5
|
+
const is_hdf5_dataset = (entity) => entity !== null && (`to_array` in entity && entity instanceof h5wasm.Dataset);
|
|
6
|
+
const is_hdf5_group = (entity) => entity !== null && (`keys` in entity && entity instanceof h5wasm.Group);
|
|
7
|
+
export async function parse_torch_sim_hdf5(buffer, filename) {
|
|
8
|
+
const { FS } = await h5wasm.ready;
|
|
9
|
+
const file_basename = filename?.split(`/`).at(-1)?.replace(/[^\w.-]/g, `_`) || `temp`;
|
|
10
|
+
const unique_suffix = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
11
|
+
const temp_filename = `${file_basename}-${unique_suffix}.h5`;
|
|
12
|
+
FS.writeFile(temp_filename, new Uint8Array(buffer));
|
|
13
|
+
let h5_file = null;
|
|
14
|
+
try {
|
|
15
|
+
h5_file = new h5wasm.File(temp_filename, `r`);
|
|
16
|
+
const found_paths = {};
|
|
17
|
+
let total_groups_found = 0;
|
|
18
|
+
const find_dataset = (names) => {
|
|
19
|
+
const discover = (parent, path = ``) => {
|
|
20
|
+
total_groups_found++;
|
|
21
|
+
for (const name of parent.keys()) {
|
|
22
|
+
const item = parent.get(name);
|
|
23
|
+
const full_path = path ? `${path}/${name}` : `/${name}`;
|
|
24
|
+
if (names.includes(name) && is_hdf5_dataset(item)) {
|
|
25
|
+
const found_name = names.find((n) => n === name);
|
|
26
|
+
if (found_name)
|
|
27
|
+
found_paths[found_name] = full_path;
|
|
28
|
+
return item;
|
|
29
|
+
}
|
|
30
|
+
if (is_hdf5_group(item)) {
|
|
31
|
+
const result = discover(item, full_path);
|
|
32
|
+
if (result)
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
};
|
|
38
|
+
return discover(h5_file);
|
|
39
|
+
};
|
|
40
|
+
const positions_data = find_dataset([`positions`, `coords`, `coordinates`])
|
|
41
|
+
?.to_array();
|
|
42
|
+
const atomic_numbers_data = find_dataset([`atomic_numbers`, `numbers`, `Z`, `species`])?.to_array();
|
|
43
|
+
const cells_data = find_dataset([`cell`, `cells`, `lattice`])?.to_array();
|
|
44
|
+
const energies_data = find_dataset([`potential_energy`, `energy`])?.to_array();
|
|
45
|
+
if (!positions_data || !atomic_numbers_data) {
|
|
46
|
+
const missing_datasets = [];
|
|
47
|
+
if (!positions_data) {
|
|
48
|
+
missing_datasets.push(`positions (tried: positions, coords, coordinates)`);
|
|
49
|
+
}
|
|
50
|
+
if (!atomic_numbers_data) {
|
|
51
|
+
missing_datasets.push(`atomic numbers (tried: atomic_numbers, numbers, Z, species)`);
|
|
52
|
+
}
|
|
53
|
+
const missing_str = missing_datasets.join(`, `);
|
|
54
|
+
const available_str = Array.from(h5_file.keys()).join(`, `);
|
|
55
|
+
throw new Error(`Missing required dataset(s) in HDF5 file: ${missing_str}. Available datasets: ${available_str}`);
|
|
56
|
+
}
|
|
57
|
+
const positions_are_frames = positions_data.length > 0 &&
|
|
58
|
+
positions_data.every((entry) => Array.isArray(entry) && entry.every((coord) => Array.isArray(coord)));
|
|
59
|
+
const positions = positions_are_frames
|
|
60
|
+
? positions_data
|
|
61
|
+
: [positions_data];
|
|
62
|
+
const atomic_numbers_are_frames = atomic_numbers_data.length > 0 &&
|
|
63
|
+
atomic_numbers_data.every((entry) => Array.isArray(entry));
|
|
64
|
+
const atomic_numbers = atomic_numbers_are_frames
|
|
65
|
+
? atomic_numbers_data
|
|
66
|
+
: [atomic_numbers_data];
|
|
67
|
+
const frames = positions.map((frame_pos, idx) => {
|
|
68
|
+
const frame_atomic_numbers = atomic_numbers[idx] || atomic_numbers[0];
|
|
69
|
+
const frame_elements = convert_atomic_numbers(frame_atomic_numbers);
|
|
70
|
+
const cell = cells_data?.[idx];
|
|
71
|
+
const lattice_mat = cell
|
|
72
|
+
? transpose_3x3_matrix(validate_3x3_matrix(cell))
|
|
73
|
+
: undefined;
|
|
74
|
+
const energy_entry = energies_data?.[idx];
|
|
75
|
+
const energy = Array.isArray(energy_entry) ? energy_entry[0] : energy_entry;
|
|
76
|
+
const metadata = {};
|
|
77
|
+
if (energy !== undefined)
|
|
78
|
+
metadata.energy = energy;
|
|
79
|
+
if (lattice_mat) {
|
|
80
|
+
metadata.volume = calc_lattice_params(lattice_mat).volume;
|
|
81
|
+
}
|
|
82
|
+
const pbc = lattice_mat ? [true, true, true] : [false, false, false];
|
|
83
|
+
return create_trajectory_frame(frame_pos, frame_elements, lattice_mat, pbc, idx, metadata);
|
|
84
|
+
});
|
|
85
|
+
const first_frame_elements = frames[0]?.structure.sites.map((site) => site.species[0].element) ??
|
|
86
|
+
[];
|
|
87
|
+
return {
|
|
88
|
+
frames,
|
|
89
|
+
metadata: {
|
|
90
|
+
source_format: `hdf5_trajectory`,
|
|
91
|
+
frame_count: frames.length,
|
|
92
|
+
num_atoms: first_frame_elements.length,
|
|
93
|
+
periodic_boundary_conditions: cells_data
|
|
94
|
+
? [true, true, true]
|
|
95
|
+
: [false, false, false],
|
|
96
|
+
element_counts: first_frame_elements.reduce((counts, element) => {
|
|
97
|
+
counts[element] = (counts[element] || 0) + 1;
|
|
98
|
+
return counts;
|
|
99
|
+
}, {}),
|
|
100
|
+
discovered_datasets: {
|
|
101
|
+
positions: found_paths.positions || found_paths.coords ||
|
|
102
|
+
found_paths.coordinates ||
|
|
103
|
+
`unknown`,
|
|
104
|
+
atomic_numbers: found_paths.atomic_numbers || found_paths.numbers ||
|
|
105
|
+
found_paths.Z || found_paths.species || `unknown`,
|
|
106
|
+
cells: found_paths.cell || found_paths.cells || found_paths.lattice,
|
|
107
|
+
energies: found_paths.potential_energy || found_paths.energy,
|
|
108
|
+
},
|
|
109
|
+
total_groups_found,
|
|
110
|
+
has_cell_info: Boolean(cells_data),
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
h5_file?.close();
|
|
116
|
+
try {
|
|
117
|
+
FS.unlink(temp_filename);
|
|
118
|
+
}
|
|
119
|
+
catch { /* temp file cleanup is best-effort */ }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { is_trajectory_file } from '../format-detect';
|
|
2
|
+
import { TrajFrameReader } from '../frame-reader';
|
|
3
|
+
import type { FrameLoader, ParseProgress, TrajectoryType } from '../index';
|
|
4
|
+
import type { AtomTypeMapping, LoadingOptions } from '../types';
|
|
5
|
+
export { INDEX_SAMPLE_RATE, LARGE_FILE_THRESHOLD, MAX_BIN_FILE_SIZE, MAX_METADATA_SIZE, MAX_SAFE_STRING_LENGTH, MAX_TEXT_FILE_SIZE, } from '../constants';
|
|
6
|
+
export type { AtomTypeMapping, LoadingOptions } from '../types';
|
|
7
|
+
export { is_trajectory_file, TrajFrameReader };
|
|
8
|
+
export declare function parse_trajectory_data(data: unknown, filename?: string, atom_type_mapping?: AtomTypeMapping): Promise<TrajectoryType>;
|
|
9
|
+
export declare function get_unsupported_format_message(filename: string, content: string): string | null;
|
|
10
|
+
export declare function parse_trajectory_async(data: ArrayBuffer | string, filename: string, on_progress?: (progress: ParseProgress) => void, options?: LoadingOptions): Promise<TrajectoryType>;
|
|
11
|
+
export declare function create_frame_loader(filename: string): FrameLoader;
|
|
12
|
+
export declare function load_binary_traj(resp: Response, type: string, fallback?: boolean): Promise<ArrayBuffer | string>;
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { is_binary } from '../../io/is-binary';
|
|
2
|
+
import * as math from '../../math';
|
|
3
|
+
import { parse_xyz } from '../../structure/parse';
|
|
4
|
+
import { INDEX_SAMPLE_RATE, LARGE_FILE_THRESHOLD } from '../constants';
|
|
5
|
+
import { FORMAT_PATTERNS, is_trajectory_file, strip_compression_extensions, } from '../format-detect';
|
|
6
|
+
import { TrajFrameReader } from '../frame-reader';
|
|
7
|
+
import { create_trajectory_frame, validate_3x3_matrix } from '../helpers';
|
|
8
|
+
import { parse_ase_trajectory } from './ase';
|
|
9
|
+
import { parse_torch_sim_hdf5 } from './hdf5';
|
|
10
|
+
import { parse_lammps_trajectory } from './lammps';
|
|
11
|
+
import { parse_vasp_xdatcar } from './vasp';
|
|
12
|
+
import { parse_xyz_trajectory } from './xyz';
|
|
13
|
+
const log_parse_debug = (message, error) => {
|
|
14
|
+
console.debug(message, error);
|
|
15
|
+
};
|
|
16
|
+
// Re-export constants and types for consumers
|
|
17
|
+
export { INDEX_SAMPLE_RATE, LARGE_FILE_THRESHOLD, MAX_BIN_FILE_SIZE, MAX_METADATA_SIZE, MAX_SAFE_STRING_LENGTH, MAX_TEXT_FILE_SIZE, } from '../constants';
|
|
18
|
+
export { is_trajectory_file, TrajFrameReader };
|
|
19
|
+
export async function parse_trajectory_data(data, filename, atom_type_mapping) {
|
|
20
|
+
if (data instanceof ArrayBuffer) {
|
|
21
|
+
if (FORMAT_PATTERNS.ase(data, filename))
|
|
22
|
+
return parse_ase_trajectory(data, filename);
|
|
23
|
+
if (FORMAT_PATTERNS.hdf5(data, filename)) {
|
|
24
|
+
return await parse_torch_sim_hdf5(data, filename);
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`Unsupported binary format${filename ? `: ${filename}` : ``}`);
|
|
27
|
+
}
|
|
28
|
+
if (typeof data === `string`) {
|
|
29
|
+
const content = data.trim();
|
|
30
|
+
if (FORMAT_PATTERNS.xyz_multi(content, filename))
|
|
31
|
+
return parse_xyz_trajectory(content);
|
|
32
|
+
if (FORMAT_PATTERNS.vasp(content, filename)) {
|
|
33
|
+
return parse_vasp_xdatcar(content, filename);
|
|
34
|
+
}
|
|
35
|
+
if (FORMAT_PATTERNS.lammpstrj(content, filename)) {
|
|
36
|
+
return parse_lammps_trajectory(content, filename, atom_type_mapping);
|
|
37
|
+
}
|
|
38
|
+
// Single XYZ fallback
|
|
39
|
+
if (filename?.toLowerCase().match(/\.(?:xyz|extxyz)$/)) {
|
|
40
|
+
try {
|
|
41
|
+
const structure = parse_xyz(content);
|
|
42
|
+
if (structure) {
|
|
43
|
+
return {
|
|
44
|
+
frames: [{ structure, step: 0, metadata: {} }],
|
|
45
|
+
metadata: { source_format: `single_xyz`, frame_count: 1 },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// Single-frame XYZ parsing failed, continue to JSON parsing.
|
|
51
|
+
log_parse_debug(`Single XYZ parse fallback failed for ${filename ?? `unknown file`}:`, error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
data = JSON.parse(content);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
log_parse_debug(`JSON parse failed for ${filename ?? `unknown file`}:`, error);
|
|
59
|
+
throw new Error(`Unsupported text format`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!data || typeof data !== `object`)
|
|
63
|
+
throw new Error(`Invalid data format`);
|
|
64
|
+
// Handle JSON formats
|
|
65
|
+
if (Array.isArray(data)) {
|
|
66
|
+
const frames = data.map((frame_data, idx) => {
|
|
67
|
+
const frame_obj = frame_data;
|
|
68
|
+
const frame_step = frame_obj.step;
|
|
69
|
+
return {
|
|
70
|
+
structure: (frame_obj.structure || frame_obj),
|
|
71
|
+
step: typeof frame_step === `number` ? frame_step : idx,
|
|
72
|
+
metadata: frame_obj.metadata || {},
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
return { frames, metadata: { source_format: `array`, frame_count: frames.length } };
|
|
76
|
+
}
|
|
77
|
+
const obj = data;
|
|
78
|
+
// Pymatgen format
|
|
79
|
+
if (obj[`@class`] === `Trajectory` && obj.species && obj.coords && obj.lattice) {
|
|
80
|
+
const species = obj.species;
|
|
81
|
+
const frame_elements = species.map((specie) => specie.element);
|
|
82
|
+
const coords = obj.coords;
|
|
83
|
+
const matrix = validate_3x3_matrix(obj.lattice);
|
|
84
|
+
const frame_properties = obj.frame_properties || [];
|
|
85
|
+
const frac_to_cart = math.create_frac_to_cart(matrix);
|
|
86
|
+
const frames = coords.map((frame_coords, idx) => {
|
|
87
|
+
const positions = frame_coords.map((abc) => frac_to_cart(abc));
|
|
88
|
+
// Process frame properties to extract numpy arrays
|
|
89
|
+
const raw_properties = frame_properties[idx] || {};
|
|
90
|
+
const processed_properties = {};
|
|
91
|
+
Object.entries(raw_properties).forEach(([key, value]) => {
|
|
92
|
+
if (value && typeof value === `object` &&
|
|
93
|
+
value[`@class`] === `array`) {
|
|
94
|
+
// Extract numpy array data
|
|
95
|
+
const array_obj = value;
|
|
96
|
+
processed_properties[key] = array_obj.data;
|
|
97
|
+
// Calculate force statistics for forces
|
|
98
|
+
if (key === `forces` && Array.isArray(array_obj.data)) {
|
|
99
|
+
const forces = array_obj.data;
|
|
100
|
+
const force_magnitudes = forces.map((force) => Math.hypot(...force));
|
|
101
|
+
if (force_magnitudes.length > 0) {
|
|
102
|
+
processed_properties.force_max = force_magnitudes.reduce((max_val, magnitude) => magnitude > max_val ? magnitude : max_val, force_magnitudes[0]);
|
|
103
|
+
processed_properties.force_norm = Math.sqrt(force_magnitudes.reduce((sum, f) => sum + f ** 2, 0) /
|
|
104
|
+
force_magnitudes.length);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Calculate stress statistics for stress tensor
|
|
108
|
+
if (key === `stress` && Array.isArray(array_obj.data)) {
|
|
109
|
+
const stress_tensor = array_obj.data;
|
|
110
|
+
if (!math.is_square_matrix(stress_tensor, 3)) {
|
|
111
|
+
console.warn(`Invalid stress tensor structure in frame ${idx}`);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
// Calculate stress components (diagonal elements represent normal stresses)
|
|
115
|
+
const normal_stresses = [
|
|
116
|
+
stress_tensor[0][0],
|
|
117
|
+
stress_tensor[1][1],
|
|
118
|
+
stress_tensor[2][2],
|
|
119
|
+
];
|
|
120
|
+
processed_properties.stress_max = Math.max(...normal_stresses.map(Math.abs));
|
|
121
|
+
// Calculate hydrostatic pressure (negative of mean normal stress)
|
|
122
|
+
processed_properties.pressure =
|
|
123
|
+
-(normal_stresses[0] + normal_stresses[1] + normal_stresses[2]) / 3;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
processed_properties[key] = value;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return create_trajectory_frame(positions, frame_elements, matrix, [true, true, true], idx, processed_properties);
|
|
132
|
+
});
|
|
133
|
+
return {
|
|
134
|
+
frames,
|
|
135
|
+
metadata: {
|
|
136
|
+
filename,
|
|
137
|
+
source_format: `pymatgen_trajectory`,
|
|
138
|
+
frame_count: frames.length,
|
|
139
|
+
species_list: [...new Set(species.map((specie) => specie.element))],
|
|
140
|
+
periodic_boundary_conditions: [true, true, true],
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
// Object with frames
|
|
145
|
+
if (Array.isArray(obj.frames)) {
|
|
146
|
+
const metadata = (obj.metadata ?? {});
|
|
147
|
+
return {
|
|
148
|
+
frames: obj.frames,
|
|
149
|
+
metadata: { ...metadata, source_format: `object_with_frames` },
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// Single structure
|
|
153
|
+
if (obj.sites) {
|
|
154
|
+
return {
|
|
155
|
+
frames: [{ structure: obj, step: 0, metadata: {} }],
|
|
156
|
+
metadata: { source_format: `single_structure`, frame_count: 1 },
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
throw new Error(`Unrecognized trajectory format`);
|
|
160
|
+
}
|
|
161
|
+
export function get_unsupported_format_message(filename, content) {
|
|
162
|
+
const lower = filename.toLowerCase();
|
|
163
|
+
// Check for unsupported compression formats first
|
|
164
|
+
const unsupported_compression = [
|
|
165
|
+
{ ext: `.bz2`, name: `BZ2` },
|
|
166
|
+
{ ext: `.xz`, name: `XZ` },
|
|
167
|
+
{ ext: `.zip`, name: `ZIP` },
|
|
168
|
+
];
|
|
169
|
+
for (const { ext, name } of unsupported_compression) {
|
|
170
|
+
if (lower.endsWith(ext)) {
|
|
171
|
+
return `🚫 ${name} compression not supported in browser\nPlease decompress the file first`;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// .dump files are LAMMPS binary dumps which require external tools to parse.
|
|
175
|
+
// .lammpstrj files are LAMMPS text-based trajectory files supported by parse_lammps_trajectory().
|
|
176
|
+
const formats = [
|
|
177
|
+
{ extensions: [`.dump`], name: `LAMMPS binary dump`, tool: `pymatgen` },
|
|
178
|
+
{ extensions: [`.nc`, `.netcdf`], name: `NetCDF`, tool: `MDAnalysis` },
|
|
179
|
+
{ extensions: [`.dcd`], name: `DCD`, tool: `MDAnalysis` },
|
|
180
|
+
];
|
|
181
|
+
for (const { extensions, name, tool } of formats) {
|
|
182
|
+
if (extensions.some((ext) => lower.endsWith(ext))) {
|
|
183
|
+
return `🚫 ${name} format not supported\nConvert with ${tool} first`;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return is_binary(content)
|
|
187
|
+
? `🚫 Binary format not supported${filename ? `: ${filename}` : ``}`
|
|
188
|
+
: null;
|
|
189
|
+
}
|
|
190
|
+
// Unified async parser with streaming support
|
|
191
|
+
export async function parse_trajectory_async(data, filename, on_progress, options = {}) {
|
|
192
|
+
const { use_indexing, index_sample_rate = INDEX_SAMPLE_RATE, extract_plot_metadata = true, atom_type_mapping, } = options;
|
|
193
|
+
const update_progress = (current, stage) => on_progress?.({ current, total: 100, stage });
|
|
194
|
+
try {
|
|
195
|
+
update_progress(0, `Detecting format...`);
|
|
196
|
+
const data_size = data instanceof ArrayBuffer
|
|
197
|
+
? data.byteLength
|
|
198
|
+
: new TextEncoder().encode(data).byteLength;
|
|
199
|
+
const is_large_file = data_size > LARGE_FILE_THRESHOLD;
|
|
200
|
+
const should_use_indexing = use_indexing ?? is_large_file;
|
|
201
|
+
if (is_large_file) {
|
|
202
|
+
update_progress(5, `Large file detected (${Math.round(data_size / 1024 / 1024)}MB)`);
|
|
203
|
+
}
|
|
204
|
+
// Use indexed loading for supported large files (including compressed names).
|
|
205
|
+
const base_filename = strip_compression_extensions(filename);
|
|
206
|
+
if (should_use_indexing && /\.(xyz|extxyz|traj)$/.test(base_filename)) {
|
|
207
|
+
return await parse_with_unified_loader(data, filename, {
|
|
208
|
+
index_sample_rate,
|
|
209
|
+
extract_plot_metadata,
|
|
210
|
+
}, on_progress);
|
|
211
|
+
}
|
|
212
|
+
// Fallback to direct parsing
|
|
213
|
+
update_progress(10, `Parsing trajectory...`);
|
|
214
|
+
const result = await parse_trajectory_data(data, filename, atom_type_mapping);
|
|
215
|
+
update_progress(100, `Complete`);
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
const error_message = error instanceof Error ? error.message : `Unknown error`;
|
|
220
|
+
update_progress(100, `Error: ${error_message}`);
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// Unified frame loading using new TrajFrameReader
|
|
225
|
+
async function parse_with_unified_loader(data, filename, options, on_progress) {
|
|
226
|
+
const { index_sample_rate, extract_plot_metadata } = options;
|
|
227
|
+
const loader = new TrajFrameReader(filename);
|
|
228
|
+
on_progress?.({ current: 10, total: 100, stage: `Counting frames...` });
|
|
229
|
+
const total_frames = await loader.get_total_frames(data);
|
|
230
|
+
on_progress?.({ current: 20, total: 100, stage: `Building frame index...` });
|
|
231
|
+
const frame_index = await loader.build_frame_index(data, index_sample_rate, (progress) => {
|
|
232
|
+
const adjusted = 20 + (progress.current / 100) * 30;
|
|
233
|
+
on_progress?.({
|
|
234
|
+
current: adjusted,
|
|
235
|
+
total: 100,
|
|
236
|
+
stage: `Building index: ${progress.stage}`,
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
on_progress?.({ current: 50, total: 100, stage: `Loading initial frames...` });
|
|
240
|
+
const initial_frame_count = Math.min(10, total_frames);
|
|
241
|
+
const frame_promises = Array.from({ length: initial_frame_count }, (_, idx) => loader.load_frame(data, idx));
|
|
242
|
+
const loaded_frames = await Promise.all(frame_promises);
|
|
243
|
+
const frames = loaded_frames.filter((frame) => frame !== null);
|
|
244
|
+
let plot_metadata;
|
|
245
|
+
if (extract_plot_metadata) {
|
|
246
|
+
on_progress?.({ current: 70, total: 100, stage: `Extracting plot metadata...` });
|
|
247
|
+
try {
|
|
248
|
+
plot_metadata = await loader.extract_plot_metadata(data, { sample_rate: 1 }, (progress) => {
|
|
249
|
+
const adjusted = 70 + (progress.current / 100) * 20;
|
|
250
|
+
on_progress?.({
|
|
251
|
+
current: adjusted,
|
|
252
|
+
total: 100,
|
|
253
|
+
stage: `Extracting: ${progress.stage}`,
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
console.warn(`Failed to extract plot metadata:`, error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const stage = `Ready: ${total_frames} frames indexed`;
|
|
262
|
+
on_progress?.({ current: 100, total: 100, stage });
|
|
263
|
+
return {
|
|
264
|
+
frames,
|
|
265
|
+
metadata: {
|
|
266
|
+
source_format: filename.toLowerCase().endsWith(`.traj`)
|
|
267
|
+
? `ase_trajectory`
|
|
268
|
+
: `xyz_trajectory`,
|
|
269
|
+
frame_count: total_frames,
|
|
270
|
+
},
|
|
271
|
+
total_frames,
|
|
272
|
+
indexed_frames: frame_index,
|
|
273
|
+
plot_metadata,
|
|
274
|
+
is_indexed: true,
|
|
275
|
+
frame_loader: loader,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
// Factory function for frame loader (simplified)
|
|
279
|
+
export function create_frame_loader(filename) {
|
|
280
|
+
if (!filename.toLowerCase().match(/\.(xyz|extxyz|traj)$/)) {
|
|
281
|
+
throw new Error(`Unsupported format for frame loading: ${filename}`);
|
|
282
|
+
}
|
|
283
|
+
return new TrajFrameReader(filename);
|
|
284
|
+
}
|
|
285
|
+
export async function load_binary_traj(resp, type, fallback = false) {
|
|
286
|
+
try {
|
|
287
|
+
// Read binary from a clone so the original can be used for text fallback
|
|
288
|
+
return await resp.clone().arrayBuffer();
|
|
289
|
+
}
|
|
290
|
+
catch (binary_error) {
|
|
291
|
+
if (fallback) {
|
|
292
|
+
console.warn(`Binary load failed for ${type}, using text fallback:`, binary_error);
|
|
293
|
+
try {
|
|
294
|
+
return await resp.text();
|
|
295
|
+
}
|
|
296
|
+
catch (text_error) {
|
|
297
|
+
const combined_error = new AggregateError([binary_error, text_error], `Failed to load ${type} as binary or text`);
|
|
298
|
+
console.error(`Binary and text fallback both failed for ${type}:`, combined_error);
|
|
299
|
+
throw combined_error;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
throw new Error(`Failed to load ${type} as binary`, { cause: binary_error });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import * as math from '../../math';
|
|
2
|
+
import type { TrajectoryType } from '../index';
|
|
3
|
+
import type { AtomTypeMapping } from '../types';
|
|
4
|
+
export declare function parse_lammps_box(box_lines: string[], is_triclinic: boolean): math.Matrix3x3 | null;
|
|
5
|
+
export declare function parse_lammps_trajectory(content: string, filename?: string, atom_type_mapping?: AtomTypeMapping): TrajectoryType;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { ELEM_SYMBOLS } from '../../labels';
|
|
2
|
+
import * as math from '../../math';
|
|
3
|
+
import { coerce_element_symbol, create_trajectory_frame } from '../helpers';
|
|
4
|
+
// Parse LAMMPS box bounds → lattice matrix. Handles orthogonal and triclinic boxes.
|
|
5
|
+
// Triclinic: converts bounding box to actual dims per https://docs.lammps.org/Howto_triclinic.html
|
|
6
|
+
// Lattice vectors: a=(lx,0,0), b=(xy,ly,0), c=(xz,yz,lz)
|
|
7
|
+
export function parse_lammps_box(box_lines, is_triclinic) {
|
|
8
|
+
if (box_lines.length !== 3)
|
|
9
|
+
return null;
|
|
10
|
+
const bounds = box_lines.map((line) => line.split(/\s+/).map(Number));
|
|
11
|
+
const min_cols = is_triclinic ? 3 : 2;
|
|
12
|
+
if (bounds.some((row) => row.length < min_cols || row.slice(0, min_cols).some(isNaN))) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (!is_triclinic) {
|
|
16
|
+
// Orthogonal: bounds = [lo, hi] per dimension
|
|
17
|
+
const [[lo_x, hi_x], [lo_y, hi_y], [lo_z, hi_z]] = bounds;
|
|
18
|
+
return [[hi_x - lo_x, 0, 0], [0, hi_y - lo_y, 0], [0, 0, hi_z - lo_z]];
|
|
19
|
+
}
|
|
20
|
+
// Triclinic: bounds = [lo_bound, hi_bound, tilt] with tilts xy, xz, yz
|
|
21
|
+
const [[xlo_b, xhi_b, xy], [ylo_b, yhi_b, xz], [zlo_b, zhi_b, yz]] = bounds;
|
|
22
|
+
const lx = (xhi_b - Math.max(0, xy, xz, xy + xz)) -
|
|
23
|
+
(xlo_b - Math.min(0, xy, xz, xy + xz));
|
|
24
|
+
const ly = (yhi_b - Math.max(0, yz)) - (ylo_b - Math.min(0, yz));
|
|
25
|
+
const lz = zhi_b - zlo_b;
|
|
26
|
+
return [[lx, 0, 0], [xy, ly, 0], [xz, yz, lz]];
|
|
27
|
+
}
|
|
28
|
+
// Parse LAMMPS trajectory (.lammpstrj). Atom types mapped to elements via atom_type_mapping
|
|
29
|
+
// or by default: 1→H, 2→He, etc. Supports orthogonal and triclinic simulation boxes.
|
|
30
|
+
export function parse_lammps_trajectory(content, filename, atom_type_mapping) {
|
|
31
|
+
const lines = content.trim().split(/\r?\n/);
|
|
32
|
+
const frames = [];
|
|
33
|
+
const atom_types_found = new Set();
|
|
34
|
+
let id_fallback_warning_emitted = false;
|
|
35
|
+
let idx = 0;
|
|
36
|
+
const read_line = () => lines[idx++]?.trim() ?? ``;
|
|
37
|
+
const peek_line = () => lines[idx]?.trim() ?? ``;
|
|
38
|
+
const skip_to = (prefix) => {
|
|
39
|
+
while (idx < lines.length && !peek_line().startsWith(prefix))
|
|
40
|
+
idx++;
|
|
41
|
+
return idx < lines.length;
|
|
42
|
+
};
|
|
43
|
+
// Helper to map atom type to element symbol
|
|
44
|
+
const get_element = (atom_type) => {
|
|
45
|
+
if (atom_type_mapping?.[atom_type])
|
|
46
|
+
return atom_type_mapping[atom_type];
|
|
47
|
+
return ELEM_SYMBOLS[Math.max(0, atom_type - 1) % ELEM_SYMBOLS.length];
|
|
48
|
+
};
|
|
49
|
+
while (idx < lines.length) {
|
|
50
|
+
if (!skip_to(`ITEM: TIMESTEP`))
|
|
51
|
+
break;
|
|
52
|
+
idx++;
|
|
53
|
+
const timestep = parseInt(read_line(), 10) || 0;
|
|
54
|
+
if (!skip_to(`ITEM: NUMBER OF ATOMS`))
|
|
55
|
+
break;
|
|
56
|
+
idx++;
|
|
57
|
+
const num_atoms = parseInt(read_line(), 10);
|
|
58
|
+
if (!num_atoms || num_atoms <= 0)
|
|
59
|
+
continue;
|
|
60
|
+
// BOX BOUNDS: orthogonal="pp pp pp", triclinic="xy xz yz pp pp pp"
|
|
61
|
+
if (!skip_to(`ITEM: BOX BOUNDS`))
|
|
62
|
+
break;
|
|
63
|
+
const box_header = read_line();
|
|
64
|
+
const is_triclinic = /BOX BOUNDS\s+xy\s+xz\s+yz/i.test(box_header);
|
|
65
|
+
const tokens = box_header.replace(`ITEM: BOX BOUNDS`, ``).trim().split(/\s+/).slice(-3);
|
|
66
|
+
const is_periodic = (tok) => tok.toLowerCase().startsWith(`p`);
|
|
67
|
+
const pbc = tokens.length === 3
|
|
68
|
+
? [is_periodic(tokens[0]), is_periodic(tokens[1]), is_periodic(tokens[2])]
|
|
69
|
+
: [true, true, true];
|
|
70
|
+
const lattice_matrix = parse_lammps_box([read_line(), read_line(), read_line()], is_triclinic);
|
|
71
|
+
if (!lattice_matrix)
|
|
72
|
+
continue;
|
|
73
|
+
// Find ITEM: ATOMS and parse column headers
|
|
74
|
+
if (!skip_to(`ITEM: ATOMS`))
|
|
75
|
+
break;
|
|
76
|
+
const cols = read_line().replace(`ITEM: ATOMS`, ``).trim().toLowerCase().split(/\s+/);
|
|
77
|
+
const col = Object.fromEntries(cols.map((name, col_idx) => [name, col_idx]));
|
|
78
|
+
// Determine position columns: prefer unwrapped (xu/yu/zu) > scaled (xs/ys/zs) > regular (x/y/z)
|
|
79
|
+
const pos_keys = [`xu`, `yu`, `zu`].every((key) => key in col)
|
|
80
|
+
? [`xu`, `yu`, `zu`]
|
|
81
|
+
: [`xs`, `ys`, `zs`].every((key) => key in col)
|
|
82
|
+
? [`xs`, `ys`, `zs`]
|
|
83
|
+
: [`x`, `y`, `z`];
|
|
84
|
+
const pos_cols = pos_keys.map((key) => col[key]);
|
|
85
|
+
// Atom identity: prefer numeric type, else explicit element symbol.
|
|
86
|
+
// Fallback to ID-based mapping only for legacy dumps; this can be inaccurate
|
|
87
|
+
// for large or non-element-like IDs, so prefer TYPE column when available.
|
|
88
|
+
const type_col = col.type;
|
|
89
|
+
const element_col = col.element;
|
|
90
|
+
const id_col = col.id;
|
|
91
|
+
const use_scaled = pos_keys[0] === `xs`;
|
|
92
|
+
const max_col_idx = Math.max(...pos_cols, type_col ?? -1, element_col ?? -1, id_col ?? -1);
|
|
93
|
+
if (pos_cols.some((col_idx) => col_idx === undefined))
|
|
94
|
+
continue;
|
|
95
|
+
if (type_col === undefined && element_col === undefined && id_col === undefined) {
|
|
96
|
+
console.warn(`Skipping LAMMPS frame at timestep ${timestep}: missing type/element/id column`);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
// Parse atom data
|
|
100
|
+
const positions = [];
|
|
101
|
+
const elements = [];
|
|
102
|
+
const frac_to_cart = use_scaled ? math.create_frac_to_cart(lattice_matrix) : null;
|
|
103
|
+
for (let atom = 0; atom < num_atoms && idx < lines.length; atom++) {
|
|
104
|
+
const parts = read_line().split(/\s+/);
|
|
105
|
+
const coords = pos_cols.map((col_idx) => parseFloat(parts[col_idx]));
|
|
106
|
+
if (coords.some(isNaN) || parts.length <= max_col_idx)
|
|
107
|
+
continue;
|
|
108
|
+
// Convert scaled coordinates to Cartesian if needed
|
|
109
|
+
const xyz = frac_to_cart ? frac_to_cart(coords) : coords;
|
|
110
|
+
let element_symbol;
|
|
111
|
+
if (type_col !== undefined) {
|
|
112
|
+
// Map atom type to element using custom mapping or default (type 1 -> H, etc.)
|
|
113
|
+
const atom_type = parseInt(parts[type_col], 10) || 1;
|
|
114
|
+
atom_types_found.add(atom_type);
|
|
115
|
+
element_symbol = get_element(atom_type);
|
|
116
|
+
}
|
|
117
|
+
else if (element_col !== undefined) {
|
|
118
|
+
const raw_symbol = parts[element_col];
|
|
119
|
+
if (!raw_symbol)
|
|
120
|
+
continue;
|
|
121
|
+
element_symbol = coerce_element_symbol(raw_symbol);
|
|
122
|
+
if (!element_symbol) {
|
|
123
|
+
console.warn(`Skipping LAMMPS atom with unknown element symbol "${raw_symbol}" at timestep ${timestep}`);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (id_col !== undefined) {
|
|
128
|
+
const atom_id = parseInt(parts[id_col], 10) || 1;
|
|
129
|
+
atom_types_found.add(atom_id);
|
|
130
|
+
if (!id_fallback_warning_emitted) {
|
|
131
|
+
console.warn(`LAMMPS parser fallback: mapping atom IDs to elements from ID column; this may be incorrect for large or sequential IDs. Prefer a TYPE column when available.`);
|
|
132
|
+
id_fallback_warning_emitted = true;
|
|
133
|
+
}
|
|
134
|
+
element_symbol = get_element(atom_id);
|
|
135
|
+
}
|
|
136
|
+
if (!element_symbol)
|
|
137
|
+
continue;
|
|
138
|
+
positions.push(xyz);
|
|
139
|
+
elements.push(element_symbol);
|
|
140
|
+
}
|
|
141
|
+
if (positions.length === elements.length && positions.length === num_atoms) {
|
|
142
|
+
const { volume } = math.calc_lattice_params(lattice_matrix);
|
|
143
|
+
frames.push(create_trajectory_frame(positions, elements, lattice_matrix, pbc, timestep, { volume, timestep }));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (frames.length === 0) {
|
|
147
|
+
throw new Error(`No valid frames found in LAMMPS trajectory`);
|
|
148
|
+
}
|
|
149
|
+
const first_frame = frames[0];
|
|
150
|
+
const element_counts = first_frame.structure.sites.reduce((counts, site) => {
|
|
151
|
+
const elem = site.species[0].element;
|
|
152
|
+
counts[elem] = (counts[elem] || 0) + 1;
|
|
153
|
+
return counts;
|
|
154
|
+
}, {});
|
|
155
|
+
return {
|
|
156
|
+
frames,
|
|
157
|
+
metadata: {
|
|
158
|
+
filename,
|
|
159
|
+
source_format: `lammps_trajectory`,
|
|
160
|
+
frame_count: frames.length,
|
|
161
|
+
total_atoms: first_frame.structure.sites.length,
|
|
162
|
+
periodic_boundary_conditions: (`lattice` in first_frame.structure)
|
|
163
|
+
? first_frame.structure.lattice.pbc
|
|
164
|
+
: [true, true, true],
|
|
165
|
+
atom_types: Array.from(atom_types_found).sort((a, b) => a - b),
|
|
166
|
+
element_counts,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|