From 731885f6c1c16d32625005066348d6f2f16302c2 Mon Sep 17 00:00:00 2001 From: OutisLi Date: Sat, 30 Aug 2025 17:53:44 +0800 Subject: [PATCH 1/4] do not output spin info for lmp format --- dpdata/lammps/lmp.py | 6 +++--- dpdata/plugins/lammps.py | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/dpdata/lammps/lmp.py b/dpdata/lammps/lmp.py index e259aa5c..33cddb41 100644 --- a/dpdata/lammps/lmp.py +++ b/dpdata/lammps/lmp.py @@ -484,7 +484,7 @@ def rotate_to_lower_triangle( return cell, coord -def from_system_data(system, f_idx=0): +def from_system_data(system, f_idx=0, output_spins=False): ret = "" ret += "\n" natoms = sum(system["atom_numbs"]) @@ -529,7 +529,7 @@ def from_system_data(system, f_idx=0): + "\n" ) # noqa: UP031 - if "spins" in system: + if "spins" in system and output_spins: coord_fmt = ( coord_fmt.strip("\n") + " " @@ -544,7 +544,7 @@ def from_system_data(system, f_idx=0): ) # noqa: UP031 spins_norm = np.linalg.norm(system["spins"][f_idx], axis=1) for ii in range(natoms): - if "spins" in system: + if "spins" in system and output_spins: if spins_norm[ii] != 0: ret += coord_fmt % ( ii + 1, diff --git a/dpdata/plugins/lammps.py b/dpdata/plugins/lammps.py index b00d4ff0..8ee5ebc2 100644 --- a/dpdata/plugins/lammps.py +++ b/dpdata/plugins/lammps.py @@ -108,7 +108,7 @@ def from_system( register_charge(data) return data - def to_system(self, data, file_name: FileType, frame_idx=0, **kwargs): + def to_system(self, data, file_name: FileType, frame_idx=0, output_spins=False, **kwargs): """Dump the system in lammps data format. Parameters @@ -119,11 +119,14 @@ def to_system(self, data, file_name: FileType, frame_idx=0, **kwargs): The output file name frame_idx : int The index of the frame to dump + output_spins : bool, optional + Whether to output spin information columns. Default is False. + When True, outputs additional columns for spin direction and magnitude. **kwargs : dict other parameters """ assert frame_idx < len(data["coords"]) - w_str = dpdata.lammps.lmp.from_system_data(data, frame_idx) + w_str = dpdata.lammps.lmp.from_system_data(data, frame_idx, output_spins=output_spins) with open_file(file_name, "w") as fp: fp.write(w_str) From 081f3fe4b3d67e365c839114db143d894b40540d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 09:56:21 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/plugins/lammps.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dpdata/plugins/lammps.py b/dpdata/plugins/lammps.py index 8ee5ebc2..1f887647 100644 --- a/dpdata/plugins/lammps.py +++ b/dpdata/plugins/lammps.py @@ -108,7 +108,9 @@ def from_system( register_charge(data) return data - def to_system(self, data, file_name: FileType, frame_idx=0, output_spins=False, **kwargs): + def to_system( + self, data, file_name: FileType, frame_idx=0, output_spins=False, **kwargs + ): """Dump the system in lammps data format. Parameters @@ -126,7 +128,9 @@ def to_system(self, data, file_name: FileType, frame_idx=0, output_spins=False, other parameters """ assert frame_idx < len(data["coords"]) - w_str = dpdata.lammps.lmp.from_system_data(data, frame_idx, output_spins=output_spins) + w_str = dpdata.lammps.lmp.from_system_data( + data, frame_idx, output_spins=output_spins + ) with open_file(file_name, "w") as fp: fp.write(w_str) From 58d682543a6c79e945ecaa57ad85ad2aba8208be Mon Sep 17 00:00:00 2001 From: OutisLi Date: Tue, 9 Sep 2025 15:26:58 +0800 Subject: [PATCH 3/4] fix unit test --- dpdata/plugins/lammps.py | 6 ++---- tests/test_lammps_spin.py | 8 ++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/dpdata/plugins/lammps.py b/dpdata/plugins/lammps.py index 1f887647..ede501c4 100644 --- a/dpdata/plugins/lammps.py +++ b/dpdata/plugins/lammps.py @@ -109,7 +109,7 @@ def from_system( return data def to_system( - self, data, file_name: FileType, frame_idx=0, output_spins=False, **kwargs + self, data, file_name: FileType, frame_idx=0, **kwargs ): """Dump the system in lammps data format. @@ -121,13 +121,11 @@ def to_system( The output file name frame_idx : int The index of the frame to dump - output_spins : bool, optional - Whether to output spin information columns. Default is False. - When True, outputs additional columns for spin direction and magnitude. **kwargs : dict other parameters """ assert frame_idx < len(data["coords"]) + output_spins = bool(kwargs.pop("output_spins", False)) w_str = dpdata.lammps.lmp.from_system_data( data, frame_idx, output_spins=output_spins ) diff --git a/tests/test_lammps_spin.py b/tests/test_lammps_spin.py index d3d58920..bf2471ec 100644 --- a/tests/test_lammps_spin.py +++ b/tests/test_lammps_spin.py @@ -48,8 +48,8 @@ def setUp(self): def tearDown(self): pass # if os.path.isfile(self.lmp_coord_name):os.remove(self.lmp_coord_name) - def test_dump_input(self): - self.tmp_system.to("lammps/lmp", self.lmp_coord_name) + def test_dump_input(self, output_spins=False): + self.tmp_system.to("lammps/lmp", self.lmp_coord_name, output_spins=output_spins) self.assertTrue(os.path.isfile(self.lmp_coord_name)) with open(self.lmp_coord_name) as f: c = f.read() @@ -58,9 +58,9 @@ def test_dump_input(self): 2 2 1.2621856000 0.7018028000 0.5513885000 0.0000000000 0.8000000000 0.6000000000 5.0000000000""" self.assertTrue(coord_ref in c) - def test_dump_input_zero_spin(self): + def test_dump_input_zero_spin(self, output_spins=True): self.tmp_system.data["spins"] = [[[0, 0, 0], [0, 0, 0]]] - self.tmp_system.to("lammps/lmp", self.lmp_coord_name) + self.tmp_system.to("lammps/lmp", self.lmp_coord_name, output_spins=output_spins) self.assertTrue(os.path.isfile(self.lmp_coord_name)) with open(self.lmp_coord_name) as f: c = f.read() From 5589f809df77c9ae287853810a319db6a981afa2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Sep 2025 07:28:35 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/plugins/lammps.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dpdata/plugins/lammps.py b/dpdata/plugins/lammps.py index ede501c4..0db58b80 100644 --- a/dpdata/plugins/lammps.py +++ b/dpdata/plugins/lammps.py @@ -108,9 +108,7 @@ def from_system( register_charge(data) return data - def to_system( - self, data, file_name: FileType, frame_idx=0, **kwargs - ): + def to_system(self, data, file_name: FileType, frame_idx=0, **kwargs): """Dump the system in lammps data format. Parameters