Skip to content

Commit

Permalink
Merge pull request #241 from SCM-NV/template
Browse files Browse the repository at this point in the history
ENH: Add the new `pressure` generic property to `cp2k_mm`
  • Loading branch information
BvB93 committed Feb 4, 2021
2 parents f79f5b4 + f252571 commit a1111ba
Show file tree
Hide file tree
Showing 9 changed files with 155,950 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/qmflows/cp2k_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ def _raise_df_exc_seq(columns: Sequence[str], prm_map: MappingSequence, ex: Exce
pass
elif msg.startswith('Shape of passed values is '):
pass
elif msg.startswith('setting an array element with a sequence'):
pass
else:
raise ex # an Exception was raised due to some other unforseen reason

Expand Down
7 changes: 7 additions & 0 deletions src/qmflows/data/dictionaries/propertiesCP2KMM.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ lattice:
column_start: 2
column_stop: 11
row_start: 1
pressure:
file_ext: out
function: extract_line_values
kwargs:
pattern: MD| Pressure
pos: 3
parser: generic_parsers
7 changes: 4 additions & 3 deletions src/qmflows/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Parsers API."""

from .generic_parsers import (awk_file, extract_line_value)
from .generic_parsers import (awk_file, extract_line_value, extract_line_values)
from .xyzParser import (parse_string_xyz, readXYZ,
manyXYZ, string_to_plams_Molecule)

__all__ = [
'awk_file', 'extract_line_value', 'parse_string_xyz', 'readXYZ', 'manyXYZ',
"string_to_plams_Molecule"]
'awk_file', 'extract_line_value', 'extract_line_values', 'parse_string_xyz',
'readXYZ', 'manyXYZ', 'string_to_plams_Molecule',
]
26 changes: 24 additions & 2 deletions src/qmflows/parsers/generic_parsers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Use the AWK command line to read output files."""

__all__ = ['awk_file', 'extract_line_value']
__all__ = ['awk_file', 'extract_line_value', 'extract_line_values']

import subprocess
from typing import Union, List, Optional
from typing import Union, List, Optional, Any

import numpy as np
from pyparsing import OneOrMore, SkipTo, Suppress

from .parser import parse_file
Expand Down Expand Up @@ -72,3 +73,24 @@ def extract_line_value(file_name: PathLike,
last_line = properties[-1].split()

return float(last_line[pos])


def extract_line_values(
file_name: PathLike,
pattern: Optional[str] = None,
pos: int = 0,
start: Optional[int] = None,
stop: Optional[int] = None,
step: Optional[int] = None,
dtype: Any = np.float64,
) -> np.ndarray:
"""Get multiply field records from a file.
Search for lines containing `pattern` and return all line
containing that value in the range defined by `start`, `stop` and `step`.
:returns: value at position `pos` in all found lines, containing pattern.
"""
parse_Line = OneOrMore(Suppress(SkipTo(pattern)) + SkipTo('\n'))
properties = parse_file(parse_Line, file_name)
iterator = (i.split()[pos] for i in properties[start:stop:step])
return np.fromiter(iterator, dtype=dtype)
4 changes: 2 additions & 2 deletions src/qmflows/parsers/orca_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ def read_column_orbitals(lines: Sequence[str]) -> Tuple[np.ndarray, np.ndarray]:
0H 1px 0.002631 0.023125 0.025515 -0.010664 0.005733 0.008004
0H 1py -0.001684 -0.022031 -0.006086 -0.022644 -0.013760 0.008735
"""
energies = np.array(lines[1].split(), dtype=np.float)
energies = np.array(lines[1].split(), dtype=np.float64)

coefficients = np.array(
[z.split()[2:] for z in lines[4:]], dtype=np.float)
[z.split()[2:] for z in lines[4:]], dtype=np.float64)

return energies, coefficients
21 changes: 16 additions & 5 deletions test/test_cp2k_mm_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def mock_runner(mocker_instance: MockFixture,
return run_mocked


def test_cp2k_singlepoint_mock(mocker) -> None:
def test_cp2k_singlepoint_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
s = SETTINGS.copy()
s.specific.cp2k += geometry.specific.cp2k_mm
Expand All @@ -79,7 +79,7 @@ def test_cp2k_singlepoint_mock(mocker) -> None:
assertion.isclose(result.energy, ref, rel_tol=10**-4)


def test_c2pk_opt_mock(mocker) -> None:
def test_c2pk_opt_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
s = SETTINGS.copy()
s.specific.cp2k += singlepoint.specific.cp2k_mm
Expand All @@ -105,7 +105,7 @@ def test_c2pk_opt_mock(mocker) -> None:
assertion.le(r_mean, 0.1)


def test_c2pk_freq_mock(mocker) -> None:
def test_c2pk_freq_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
s = SETTINGS.copy()
s.specific.cp2k += freq.specific.cp2k_mm
Expand All @@ -127,7 +127,7 @@ def test_c2pk_freq_mock(mocker) -> None:
assertion.isclose(H, H_ref, rel_tol=0.1)


def test_c2pk_md_mock(mocker) -> None:
def test_c2pk_md_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
s = SETTINGS.copy()
s.specific.cp2k += md.specific.cp2k_mm
Expand All @@ -141,7 +141,7 @@ def test_c2pk_md_mock(mocker) -> None:
assertion.isfile(result.results['cp2k-1_1000.restart'])


def test_c2pk_cell_opt_mock(mocker) -> None:
def test_c2pk_cell_opt_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
mol = Molecule(PATH / 'cspbbr3_3d.xyz')

Expand Down Expand Up @@ -183,3 +183,14 @@ def test_c2pk_cell_opt_mock(mocker) -> None:
np.testing.assert_allclose(result.coordinates, ref_coordinates)
np.testing.assert_allclose(result.forces, ref_forces)
np.testing.assert_allclose(result.lattice, ref_lattice)


def test_c2pk_npt_mock(mocker: MockFixture) -> None:
"""Mock a call to CP2K."""
s = Settings()
job = cp2k_mm(s, None)
run_mocked = mock_runner(mocker, settings=s, jobname="cp2k_mm_npt")
result = run_mocked(job)

ref_pressure = np.load(PATH / 'pressure.npy')
np.testing.assert_allclose(result.pressure, ref_pressure)

0 comments on commit a1111ba

Please sign in to comment.