Skip to content

Commit

Permalink
ENH: Allow the generic cell_parameters keyword to take any array-li…
Browse files Browse the repository at this point in the history
…ke object (#238)
  • Loading branch information
BvB93 committed Dec 18, 2020
1 parent c22d0bb commit 6c2d83f
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/qmflows/packages/cp2k_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

import os
from os.path import join
from typing import Any, Union, Tuple, ClassVar, Type
from typing import Any, Union, Iterable, ClassVar, Type
from warnings import warn

import numpy as np
from scm import plams

from .packages import Package, Result, parse_output_warnings, load_properties
Expand Down Expand Up @@ -157,21 +158,21 @@ def write_cell_parameters(s: Settings, value: Any,
&END CELL
"""
def fun(xs: Tuple[Any, Any, Any]) -> str:
def fun(xs: Iterable[Any]) -> str:
return '{:.2f} {:.2f} {:.2f}'.format(*xs)

if not isinstance(value, list):
abc = [value] * 3
abc_cell = f' [angstrom] {fun(abc)}'
s.specific.cp2k.force_eval.subsys.cell.ABC = abc_cell
elif isinstance(value[0], list):
a, b, c = value
ar = np.asarray(value, dtype=np.float64)
if ar.ndim == 0:
abc = f' [angstrom] {fun(np.repeat(ar, 3))}'
s.specific.cp2k.force_eval.subsys.cell.ABC = abc
elif ar.ndim == 1:
abc = f' [angstrom] {fun(ar)}'
s.specific.cp2k.force_eval.subsys.cell.ABC = abc
elif ar.ndim == 2:
a, b, c = ar
s.specific.cp2k.force_eval.subsys.cell.A = fun(a)
s.specific.cp2k.force_eval.subsys.cell.B = fun(b)
s.specific.cp2k.force_eval.subsys.cell.C = fun(c)
elif isinstance(value, list):
abc = f' [angstrom] {fun(value)}'
s.specific.cp2k.force_eval.subsys.cell.ABC = abc
else:
raise RuntimeError(
f"cell parameter:{value!r}\nformat not recognized")
Expand Down

0 comments on commit 6c2d83f

Please sign in to comment.