Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions python/metatomic_torch/metatomic/torch/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,19 +931,19 @@ def _convert_systems_units(
) -> List[System]:
if model_length_unit == "" or system_length_unit == "":
# no conversion for positions/cell/NL
conversion = 1.0
length_conversion = 1.0
else:
conversion = unit_conversion_factor(
system_length_unit,
model_length_unit,
length_conversion = unit_conversion_factor(
from_unit=system_length_unit,
to_unit=model_length_unit,
)

new_systems: List[System] = []
for system in systems:
new_system = System(
types=system.types,
positions=conversion * system.positions,
cell=conversion * system.cell,
positions=length_conversion * system.positions,
cell=length_conversion * system.cell,
pbc=system.pbc,
)

Expand All @@ -953,7 +953,7 @@ def _convert_systems_units(
new_system.add_neighbor_list(
request,
TensorBlock(
values=conversion * neighbors.values,
values=length_conversion * neighbors.values,
samples=neighbors.samples,
components=neighbors.components,
properties=neighbors.properties,
Expand Down
41 changes: 41 additions & 0 deletions python/metatomic_torch/tests/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
load_model_extensions,
read_model_metadata,
)
from metatomic.torch.model import _convert_systems_units


class MinimalModel(torch.nn.Module):
Expand Down Expand Up @@ -694,3 +695,43 @@ def test_not_requested_output(system):

# make sure it does not crash with check_consistency=False
atomistic([system], evaluation_options, check_consistency=False)


def test_systems_unit_conversion(system):
requested_inputs = {
"masses": ModelOutput(
quantity="mass",
unit="kg",
per_atom=True,
),
}
mass_block = TensorBlock(
values=torch.tensor([[1.0]], dtype=torch.float64),
samples=Labels("atom", torch.tensor([[0]])),
components=[],
properties=Labels("mass", torch.tensor([[0]])),
)
mass_tensor = TensorMap(Labels("atom", torch.tensor([[0]])), [mass_block])
mass_tensor.set_info("unit", "u")
mass_tensor.set_info("quantity", "mass")
system.add_data("masses", mass_tensor)
systems = [system, system]
converted_systems = _convert_systems_units(
systems, "angstrom", "nm", requested_inputs
)

# The systems are the same, so the converted systems should be the same as well
assert torch.allclose(
converted_systems[0].positions, converted_systems[1].positions
)
assert torch.allclose(
converted_systems[0].get_data("masses").block().values,
converted_systems[1].get_data("masses").block().values,
)

# To check if the conversion was correct
assert torch.allclose(converted_systems[0].positions, systems[0].positions * 1e-1)
assert torch.allclose(
converted_systems[0].get_data("masses").block().values,
systems[0].get_data("masses").block().values * 1.660539e-27,
)
Loading