diff --git a/cpp/lammpsweb/lammpsweb.cpp b/cpp/lammpsweb/lammpsweb.cpp index 921c8f5d..707b5a41 100644 --- a/cpp/lammpsweb/lammpsweb.cpp +++ b/cpp/lammpsweb/lammpsweb.cpp @@ -366,6 +366,13 @@ std::string LAMMPSWeb::getLastCommand() { return lastCommand; } +std::string LAMMPSWeb::getUnits() { + if (!m_lmp || !m_lmp->update) { + return ""; + } + return std::string(m_lmp->update->unit_style); +} + int LAMMPSWeb::getTimesteps() { if (!m_lmp) { diff --git a/cpp/lammpsweb/lammpsweb.h b/cpp/lammpsweb/lammpsweb.h index 86239f15..0b44b7c2 100644 --- a/cpp/lammpsweb/lammpsweb.h +++ b/cpp/lammpsweb/lammpsweb.h @@ -59,6 +59,7 @@ class LAMMPSWeb Fix getFix(std::string name); std::vector getVariableNames(); Variable getVariable(std::string name); + std::string getUnits(); long getMemoryUsage(); // Pointer getters @@ -121,6 +122,7 @@ EMSCRIPTEN_BINDINGS(LAMMPSWeb) .function("getFixNames", &LAMMPSWeb::getFixNames) .function("getVariable", &LAMMPSWeb::getVariable) .function("getVariableNames", &LAMMPSWeb::getVariableNames) + .function("getUnits", &LAMMPSWeb::getUnits) .function("syncComputes", &LAMMPSWeb::syncComputes) .function("syncFixes", &LAMMPSWeb::syncFixes) .function("syncVariables", &LAMMPSWeb::syncVariables) diff --git a/src/components/SelectedAtomsInfo.tsx b/src/components/SelectedAtomsInfo.tsx index 4799f023..7e6ef80b 100644 --- a/src/components/SelectedAtomsInfo.tsx +++ b/src/components/SelectedAtomsInfo.tsx @@ -1,6 +1,7 @@ import { Button } from "antd"; import { Particles } from "omovi"; import { useMemo } from "react"; +import { useLammpsUnits } from "../hooks/useLammpsUnits"; interface SelectedAtomsInfoProps { selectedAtoms: Set; @@ -48,6 +49,8 @@ const SelectedAtomsInfo = ({ particles, onClearSelection, }: SelectedAtomsInfoProps) => { + const distanceUnit = useLammpsUnits(); + if (selectedAtoms.size === 0) { return null; } @@ -117,7 +120,7 @@ const SelectedAtomsInfo = ({ Geometry
- Distance: {calculateDistance(atomData[0].position, atomData[1].position).toFixed(3)} Å + Distance: {calculateDistance(atomData[0].position, atomData[1].position).toFixed(3)} {distanceUnit}
)} @@ -130,15 +133,15 @@ const SelectedAtomsInfo = ({
d({atomData[0].atomId}-{atomData[1].atomId}):{" "} - {calculateDistance(atomData[0].position, atomData[1].position).toFixed(3)} Å + {calculateDistance(atomData[0].position, atomData[1].position).toFixed(3)} {distanceUnit}
d({atomData[1].atomId}-{atomData[2].atomId}):{" "} - {calculateDistance(atomData[1].position, atomData[2].position).toFixed(3)} Å + {calculateDistance(atomData[1].position, atomData[2].position).toFixed(3)} {distanceUnit}
d({atomData[0].atomId}-{atomData[2].atomId}):{" "} - {calculateDistance(atomData[0].position, atomData[2].position).toFixed(3)} Å + {calculateDistance(atomData[0].position, atomData[2].position).toFixed(3)} {distanceUnit}
diff --git a/src/hooks/useLammpsUnits.ts b/src/hooks/useLammpsUnits.ts new file mode 100644 index 00000000..d27dff32 --- /dev/null +++ b/src/hooks/useLammpsUnits.ts @@ -0,0 +1,20 @@ +import { useMemo } from "react"; +import { useStoreState } from "./index"; +import { getDistanceUnitSymbol } from "../utils/parsers"; + +/** + * Hook to get the distance unit symbol based on the current LAMMPS unit system + * @returns The distance unit symbol (e.g., "Å", "σ", "m", "cm", etc.) + */ +export const useLammpsUnits = (): string => { + const lammps = useStoreState((state) => state.simulation.lammps); + + return useMemo(() => { + if (!lammps) { + return "Å"; // Default to Angstroms if LAMMPS is not available + } + + const unitStyle = lammps.getUnits(); + return getDistanceUnitSymbol(unitStyle); + }, [lammps]); +}; diff --git a/src/types.ts b/src/types.ts index a942c77d..eda0a070 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,7 @@ export type LammpsWeb = { getFixNames: () => CPPArray; getVariable: (name: string) => LMPModifier; getVariableNames: () => CPPArray; + getUnits: () => string; syncComputes: () => void; syncFixes: () => void; syncVariables: () => void; diff --git a/src/utils/parsers.ts b/src/utils/parsers.ts index 41505640..70e3961b 100644 --- a/src/utils/parsers.ts +++ b/src/utils/parsers.ts @@ -88,3 +88,35 @@ export const parseAtomSizeAndColor = (line: string) => { } }; +/** + * Gets the distance unit symbol for a given LAMMPS unit style + * @param unitStyle The LAMMPS unit style (e.g., "real", "metal", "lj", "si", etc.) + * @returns The distance unit symbol string + */ +export const getDistanceUnitSymbol = (unitStyle: string | undefined): string => { + if (!unitStyle) { + return "Å"; // Default to Angstroms if unit style is not found + } + + const style = unitStyle.toLowerCase(); + switch (style) { + case "lj": + return "σ"; + case "real": + case "metal": + return "Å"; + case "si": + return "m"; + case "cgs": + return "cm"; + case "electron": + return "a₀"; + case "micro": + return "μm"; + case "nano": + return "nm"; + default: + return "Å"; // Default to Angstroms for unknown unit styles + } +}; +