diff --git a/cp2kdata/block_parser/stress.py b/cp2kdata/block_parser/stress.py index 7fcbba6..49e9b5a 100644 --- a/cp2kdata/block_parser/stress.py +++ b/cp2kdata/block_parser/stress.py @@ -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\[(?PGPa|bar)\] \n \s+X\s+Y\s+Z\s*\n \s+X @@ -19,7 +21,7 @@ \s+(?P[\s-]\d+\.\d+) \s+(?P[\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\[(?PGPa|bar)\]\s*\n \s+STRESS\|\s+x\s+y\s+z\s*\n \s+STRESS\|\s+x \s+(?P[\s-]\d+\.\d+E[\+\-]\d\d) @@ -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) diff --git a/tests/test_stress.py b/tests/test_stress.py new file mode 100644 index 0000000..32fc343 --- /dev/null +++ b/tests/test_stress.py @@ -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)