Skip to content
Open
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
9 changes: 7 additions & 2 deletions cp2kdata/block_parser/stress.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import regex as re
import numpy as np

BAR_TO_GPA = 1e-4

STRESS_RE = re.compile(
r"""
(\sSTRESS\sTENSOR\s\[GPa\]
(\sSTRESS\sTENSOR\s\[(?P<unit>GPa|bar)\]
\n
\s+X\s+Y\s+Z\s*\n
\s+X
Expand All @@ -19,7 +21,7 @@
\s+(?P<zy>[\s-]\d+\.\d+)
\s+(?P<zz>[\s-]\d+\.\d+)\n
|# or another pattern used in v8.1
\s+STRESS\|\sAnalytical\sstress\stensor\s\[GPa\]\s*\n
\s+STRESS\|\sAnalytical\sstress\stensor\s\[(?P<unit>GPa|bar)\]\s*\n
\s+STRESS\|\s+x\s+y\s+z\s*\n
\s+STRESS\|\s+x
\s+(?P<xx>[\s-]\d+\.\d+E[\+\-]\d\d)
Expand Down Expand Up @@ -47,6 +49,9 @@ def parse_stress_tensor_list(output_file):
[match["yx"], match["yy"], match["yz"]],
[match["zx"], match["zy"], match["zz"]]
]
if match["unit"] == "bar":
stress_tensor = np.asarray(stress_tensor, dtype=float)
stress_tensor *= BAR_TO_GPA
stress_tensor_list.append(stress_tensor)
if stress_tensor_list:
return np.array(stress_tensor_list, dtype=float)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_stress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np

from cp2kdata.block_parser.stress import parse_stress_tensor_list


def test_parse_cp2k_2025_stress_in_bar():
output = """
STRESS| Analytical stress tensor [GPa]
STRESS| x y z
STRESS| x -1.00000000000E+00 2.00000000000E+00 3.00000000000E+00
STRESS| y 4.00000000000E+00 -5.00000000000E+00 6.00000000000E+00
STRESS| z 7.00000000000E+00 8.00000000000E+00 -9.00000000000E+00
STRESS| Analytical stress tensor [bar]
STRESS| x y z
STRESS| x -1.00000000000E+04 2.00000000000E+04 3.00000000000E+04
STRESS| y 4.00000000000E+04 -5.00000000000E+04 6.00000000000E+04
STRESS| z 7.00000000000E+04 8.00000000000E+04 -9.00000000000E+04
"""
expected = np.array(
[
[-1.0, 2.0, 3.0],
[4.0, -5.0, 6.0],
[7.0, 8.0, -9.0],
]
)

stresses = parse_stress_tensor_list(output)

assert stresses.shape == (2, 3, 3)
np.testing.assert_allclose(stresses[0], expected)
np.testing.assert_allclose(stresses[1], expected)