-
Notifications
You must be signed in to change notification settings - Fork 159
fix: Support CP2K 2025 output format for energy and forces (fixes #850) #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
newtontech
wants to merge
4
commits into
deepmodeling:master
Choose a base branch
from
newtontech:fix-cp2k-2025-format-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b067f01
fix: Support CP2K 2025 output format for energy and forces (fixes #850)
ac0d5ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 602a06f
test: add edge case tests for CP2K 2025 format parsing
7478a4c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| DBCSR| Multiplication driver XSMM | ||
| DBCSR| Multrec recursion limit 512 | ||
|
|
||
| **** **** ****** ** PROGRAM STARTED AT 2025-01-15 10:30:00.000 | ||
| ***** ** *** *** ** PROGRAM STARTED ON test-machine | ||
| ** **** ****** PROGRAM STARTED BY test | ||
| ***** ** ** ** ** PROGRAM PROCESS ID 12345 | ||
| **** ** ******* ** PROGRAM STARTED IN /test | ||
|
|
||
| CP2K| version string: CP2K version 2025.1 | ||
| CP2K| source code revision number: git:abc123 | ||
|
|
||
| GLOBAL| Force Environment number 1 | ||
| GLOBAL| Run type ENERGY_FORCE | ||
| GLOBAL| Global print level MEDIUM | ||
| GLOBAL| Total number of message passing processes 1 | ||
|
|
||
| MEMORY| system memory details [Kb] | ||
|
|
||
| CELL| Vector a [angstrom]: 5.000 0.000 0.000 |a| = 5.000 | ||
| CELL| Vector b [angstrom]: 0.000 5.000 0.000 |b| = 5.000 | ||
| CELL| Vector c [angstrom]: 0.000 0.000 5.000 |c| = 5.000 | ||
|
|
||
| ATOMIC KIND INFORMATION | ||
|
|
||
| 1. Atomic kind: H Number of atoms: 2 | ||
|
|
||
| MOLECULE KIND INFORMATION | ||
|
|
||
| TOTAL NUMBERS AND MAXIMUM NUMBERS | ||
|
|
||
| Total number of - Atomic kinds: 1 | ||
| - Atoms: 2 | ||
|
|
||
|
|
||
| SCF WAVEFUNCTION OPTIMIZATION | ||
|
|
||
| Step Update method Time Convergence Total energy Change | ||
| ------------------------------------------------------------------------------ | ||
|
|
||
| 1 OT DIIS 0.80E-01 0.1 0.00000001 -7.3641902645877 -7.36E+00 | ||
|
|
||
| *** SCF run converged in 1 steps *** | ||
|
|
||
|
|
||
| ******************************************************************************* | ||
| MODULE QUICKSTEP: ATOMIC COORDINATES IN angstrom | ||
|
|
||
| Atom Kind Element X Y Z Z(eff) Mass | ||
|
|
||
| 1 1 H 1 0.000000 0.000000 0.000000 1.00 1.0079 | ||
| 2 1 H 1 0.760000 0.000000 0.000000 1.00 1.0079 | ||
|
|
||
| ENERGY| Total FORCE_EVAL ( QS ) energy [hartree] -7.364190264587725 | ||
|
|
||
| FORCES| Atomic forces [hartree/bohr] | ||
|
|
||
| FORCES| Atom x y z |f| | ||
|
|
||
| FORCES| 1 -5.73440344E-02 2.95274914E-02 -1.50988167E-02 6.62433792E-02 | ||
| FORCES| 2 7.92269287E-02 3.84670665E-02 -3.41478833E-02 9.44600412E-02 | ||
|
|
||
| STRESS TENSOR [GPa] | ||
|
|
||
| X Y Z | ||
| X 0.12345678 0.00000000 0.00000000 | ||
| Y 0.00000000 0.12345678 0.00000000 | ||
| Z 0.00000000 0.00000000 0.12345678 | ||
|
|
||
| ------------------------------------------------------------------------------- | ||
| - -\n - T I M I N G - | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
| **** **** ****** ** PROGRAM ENDED AT 2025-01-15 10:30:05.000 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 0 | ||
| 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| from comp_sys import CompLabeledSys | ||
| from context import dpdata | ||
|
|
||
|
|
||
| class TestCp2k2025Output(unittest.TestCase, CompLabeledSys): | ||
| """Test CP2K 2025 output format parsing.""" | ||
|
|
||
| def setUp(self): | ||
| self.system_1 = dpdata.LabeledSystem( | ||
| "cp2k/cp2k_2025_output/cp2k_2025_output", fmt="cp2k/output" | ||
| ) | ||
| self.system_2 = dpdata.LabeledSystem( | ||
| "cp2k/cp2k_2025_output/deepmd", fmt="deepmd/npy" | ||
| ) | ||
| self.places = 6 | ||
| self.e_places = 6 | ||
| self.f_places = 6 | ||
| self.v_places = 4 | ||
|
|
||
| def test_energy_extraction(self): | ||
| """Test that energy is correctly extracted from CP2K 2025 format.""" | ||
| # Energy should be -7.364190264587725 hartree | ||
| # Using dpdata's conversion factor: -200.3898256786414 eV | ||
| expected_energy = -200.3898256786414 | ||
| self.assertAlmostEqual( | ||
| self.system_1.data["energies"][0], expected_energy, places=5 | ||
| ) | ||
|
|
||
| def test_forces_extraction(self): | ||
| """Test that forces are correctly extracted from CP2K 2025 format.""" | ||
| # Forces should be converted from hartree/bohr to eV/angstrom | ||
| self.assertEqual(self.system_1.data["forces"].shape, (1, 2, 3)) | ||
| # Check first atom force x-component | ||
| self.assertAlmostEqual( | ||
| self.system_1.data["forces"][0][0][0], -2.94874881, places=5 | ||
| ) | ||
|
|
||
|
|
||
| class TestCp2k2023FormatStillWorks(unittest.TestCase, CompLabeledSys): | ||
| """Test that CP2K 2023 format still works (regression test).""" | ||
|
|
||
| def setUp(self): | ||
| self.system_1 = dpdata.LabeledSystem( | ||
| "cp2k/cp2k_normal_output/cp2k_output", fmt="cp2k/output" | ||
| ) | ||
| self.system_2 = dpdata.LabeledSystem( | ||
| "cp2k/cp2k_normal_output/deepmd", fmt="deepmd/npy" | ||
| ) | ||
| self.places = 6 | ||
| self.e_places = 6 | ||
| self.f_places = 6 | ||
| self.v_places = 4 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential uninitialized variable in fallback path.
If
"[hartree]"is not found inpartsand the fallback loop doesn't find any numeric value,energywill not be set for this line, but the code continues silently. While the assertion on line 491 will eventually catch this, the error message won't be helpful for debugging malformed CP2K 2025 output.Consider adding explicit error handling:
Proposed fix
if is_cp2k_2025: # Find the energy value after "[hartree]" parts = ii.split() try: hartree_idx = parts.index("[hartree]") energy = parts[hartree_idx + 1] except (ValueError, IndexError): # Fallback: try to find energy value in the line for part in reversed(parts): try: float(part) energy = part break except ValueError: continue + else: + # Loop completed without finding energy + raise RuntimeError(f"Cannot parse energy from CP2K 2025 line: {ii}")🤖 Prompt for AI Agents