From 93d3a9ead4a5da039d6d8ee01b72e4e2b342e34b Mon Sep 17 00:00:00 2001 From: njzjz-bot Date: Thu, 16 Jul 2026 11:59:26 +0800 Subject: [PATCH] fix(pwmat): use selected frame cell for coordinates Fractionalize each atom.config frame with that frame's lattice instead of always using frame zero. Add a changing-cell regression; the prior single-frame writer tests could not expose the frame-index mismatch. Coding-Agent: Codex Codex-Version: codex-cli 0.144.4 Model: gpt-5.6-sol Reasoning-Effort: xhigh --- dpdata/formats/pwmat/atomconfig.py | 4 +++- tests/test_pwmat_config_dump.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/dpdata/formats/pwmat/atomconfig.py b/dpdata/formats/pwmat/atomconfig.py index 11677b0ef..e0742728d 100644 --- a/dpdata/formats/pwmat/atomconfig.py +++ b/dpdata/formats/pwmat/atomconfig.py @@ -82,7 +82,9 @@ def from_system_data(system, f_idx=0, skip_zeros=True): atomic_numbers.append(ELEMENTS.index(ii) + 1) posi_list = [] for jj, ii in zip(atomic_numbers, posis): - ii = np.matmul(ii, np.linalg.inv(system["cells"][0])) + # Fractional coordinates must be computed in the same frame's cell; + # frame zero is only valid for a fixed-cell trajectory. + ii = np.matmul(ii, np.linalg.inv(system["cells"][f_idx])) posi_list.append("%d %15.10f %15.10f %15.10f 1 1 1" % (jj, ii[0], ii[1], ii[2])) # noqa: UP031 for kk in range(len(posi_list)): min = kk diff --git a/tests/test_pwmat_config_dump.py b/tests/test_pwmat_config_dump.py index e4d5a5a8e..79ba8cd94 100644 --- a/tests/test_pwmat_config_dump.py +++ b/tests/test_pwmat_config_dump.py @@ -3,9 +3,11 @@ import os import unittest +import numpy as np from pwmat.config_ref_oh import Testconfigoh import dpdata +from dpdata.formats.pwmat.atomconfig import from_system_data def myfilecmp(test, f0, f1): @@ -58,5 +60,18 @@ def test_dump_pwmat_type_map(self): myfilecmp(self, "atom.config.tmp.1", "atom.config.tmp.2") +class TestAtomconfigFrameCell(unittest.TestCase): + def test_fractional_coordinates_use_selected_frame_cell(self): + system = { + "atom_names": ["H"], + "atom_numbs": [1], + "atom_types": np.array([0]), + "cells": np.array([np.eye(3), np.eye(3) * 2.0]), + "coords": np.array([[[1.0, 0.0, 0.0]], [[1.0, 0.0, 0.0]]]), + } + output = from_system_data(system, f_idx=1) + self.assertIn("0.5000000000 0.0000000000 0.0000000000", output) + + if __name__ == "__main__": unittest.main()