Skip to content

Commit d7791ab

Browse files
committed
cli/workchains: add more options, parse referenced structure file
1 parent 195cc45 commit d7791ab

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

aiida_cp2k/cli/utils/cp2k_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@
1515
"--structure",
1616
type=types.DataParamType(sub_classes=("aiida.data:structure",)),
1717
help="StructureData node.")
18+
19+
MAX_NUM_MACHINES = OverridableOption('-m',
20+
'--max-num-machines',
21+
type=int,
22+
default=1,
23+
show_default=True,
24+
help='The maximum number of machines (nodes) to use for the calculations.')
25+
26+
MAX_WALLCLOCK_SECONDS = OverridableOption('-w',
27+
'--max-wallclock-seconds',
28+
type=int,
29+
default=1800,
30+
show_default=True,
31+
help='the maximum wallclock time in seconds to set for the calculations.')

aiida_cp2k/cli/workflows/base.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
@click.option('-m', '--description', type=str)
2020
@cp2k_options.STRUCTURE()
2121
@cp2k_options.DAEMON()
22+
@cp2k_options.MAX_NUM_MACHINES()
23+
@cp2k_options.MAX_WALLCLOCK_SECONDS()
2224
@decorators.with_dbenv()
23-
def cmd_launch_workflow(code, daemon, cp2k_input_file, label, description, structure):
25+
def cmd_launch_workflow(code, daemon, cp2k_input_file, label, description, structure, max_num_machines,
26+
max_wallclock_seconds):
2427
"""Run a CP2K calculation with a given input file through AiiDA"""
28+
# pylint: disable=too-many-arguments,too-many-branches,too-many-statements
2529

2630
from aiida.orm import Dict
2731
from aiida.plugins import WorkflowFactory
32+
from aiida.orm.nodes.data.structure import StructureData
2833
from cp2k_input_tools.parser import CP2KInputParserSimplified
2934

3035
parser = CP2KInputParserSimplified(key_trafo=str.upper,
@@ -33,33 +38,62 @@ def cmd_launch_workflow(code, daemon, cp2k_input_file, label, description, struc
3338
level_reduction_blacklist=["KIND"])
3439
tree = parser.parse(cp2k_input_file)
3540

36-
builder = WorkflowFactory('cp2k.base').get_builder()
41+
try:
42+
tree["FORCE_EVAL"]["SUBSYS"].pop("COORD")
43+
structure_in_config = True
44+
except KeyError:
45+
structure_in_config = False
46+
47+
try:
48+
coord_file_from_config = tree["FORCE_EVAL"]["SUBSYS"]["TOPOLOGY"].pop("COORD_FILE_NAME")
49+
except KeyError:
50+
coord_file_from_config = None
3751

52+
builder = WorkflowFactory('cp2k.base').get_builder()
3853
builder.cp2k.code = code
3954

4055
if structure:
4156
builder.cp2k.structure = structure
4257

43-
try:
44-
tree["FORCE_EVAL"]["SUBSYS"]["TOPOLOGY"].pop("COORD_FILE_NAME")
45-
echo.echo_info("The explicitly given structure overrides the structure referenced in the input file")
46-
except KeyError:
47-
pass
58+
if coord_file_from_config:
59+
echo.echo_info(
60+
"The explicitly given structure will override the structure file referenced in the input file")
4861

49-
else:
62+
if structure_in_config:
63+
echo.echo_info("The explicitly given structure will override the structure in the input file")
64+
65+
elif structure_in_config:
5066
try:
51-
with open(cp2k_input_file.name, "r") as fhandle:
52-
builder.cp2k.structure = structure_from_cp2k_inp(fhandle)
67+
cp2k_input_file.seek(0)
68+
builder.cp2k.structure = structure_from_cp2k_inp(cp2k_input_file)
5369
builder.cp2k.structure.store()
5470
echo.echo("Created StructureData<{}> from file {}]\n".format(builder.cp2k.structure.pk,
5571
cp2k_input_file.name))
5672
except ValueError as err:
5773
echo.echo_critical(str(err))
5874

75+
elif coord_file_from_config:
76+
try:
77+
import ase.io
78+
except ImportError:
79+
echo.echo_critical('You have not installed the package ase. \nYou can install it with: pip install ase')
80+
81+
try:
82+
asecell = ase.io.read(coord_file_from_config)
83+
builder.cp2k.structure = StructureData(ase=asecell)
84+
builder.cp2k.structure.store()
85+
echo.echo("Created StructureData<{}> from file {}]\n".format(builder.cp2k.structure.pk,
86+
coord_file_from_config))
87+
except ValueError as err:
88+
echo.echo_critical(str(err))
89+
else:
90+
echo.echo_critical("No structure found/referenced in the input file and not set explicitly")
91+
5992
builder.cp2k.metadata.options = {
6093
"resources": {
61-
"num_machines": 1,
94+
"num_machines": max_num_machines,
6295
},
96+
'max_wallclock_seconds': int(max_wallclock_seconds),
6397
}
6498
builder.cp2k.parameters = Dict(dict=tree)
6599

0 commit comments

Comments
 (0)