Skip to content

Commit

Permalink
Add an example run for an advanced parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
yakutovicha committed Jan 28, 2025
1 parent c30932f commit 6c56102
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions examples/single_calculations/example_geopt_advanced_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
###############################################################################
# Copyright (c), The AiiDA-CP2K authors. #
# SPDX-License-Identifier: MIT #
# AiiDA-CP2K is hosted on GitHub at https://github.com/aiidateam/aiida-cp2k #
# For further information on the license, see the LICENSE.txt file. #
###############################################################################
"""Run DFT geometry optimization."""

import os
import sys

import ase.io
import click
from aiida.common import NotExistent
from aiida.engine import run
from aiida.orm import Dict, SinglefileData, load_code
from aiida.plugins import DataFactory

StructureData = DataFactory("core.structure")


def example_geopt(cp2k_code):
"""Run DFT geometry optimization."""

print("Testing CP2K GEO_OPT on H2O (DFT)...")

thisdir = os.path.dirname(os.path.realpath(__file__))

# Structure.
structure = StructureData(
ase=ase.io.read(os.path.join(thisdir, "..", "files", "h2.xyz"))
)

# Basis set.
basis_file = SinglefileData(
file=os.path.join(thisdir, "..", "files", "BASIS_MOLOPT")
)

# Pseudopotentials.
pseudo_file = SinglefileData(
file=os.path.join(thisdir, "..", "files", "GTH_POTENTIALS")
)

# Parameters.
parameters = Dict(
{
"GLOBAL": {
"RUN_TYPE": "GEO_OPT",
},
"FORCE_EVAL": {
"METHOD": "Quickstep",
"DFT": {
"BASIS_SET_FILE_NAME": "BASIS_MOLOPT",
"POTENTIAL_FILE_NAME": "GTH_POTENTIALS",
"QS": {
"EPS_DEFAULT": 1.0e-12,
"WF_INTERPOLATION": "ps",
"EXTRAPOLATION_ORDER": 3,
},
"MGRID": {
"NGRIDS": 4,
"CUTOFF": 280,
"REL_CUTOFF": 30,
},
"XC": {
"XC_FUNCTIONAL": {
"_": "PBE",
},
},
"POISSON": {
"PERIODIC": "none",
"PSOLVER": "MT",
},
},
"SUBSYS": {
"KIND": [
{
"_": "O",
"BASIS_SET": "DZVP-MOLOPT-SR-GTH",
"POTENTIAL": "GTH-PBE-q6",
},
{
"_": "H",
"BASIS_SET": "DZVP-MOLOPT-SR-GTH",
"POTENTIAL": "GTH-PBE-q1",
},
],
},
},
}
)

# Construct process builder.
builder = cp2k_code.get_builder()
builder.structure = structure
builder.parameters = parameters
builder.code = cp2k_code
builder.file = {
"basis": basis_file,
"pseudo": pseudo_file,
}
builder.metadata.options.resources = {
"num_machines": 1,
"num_mpiprocs_per_machine": 1,
}
builder.metadata.options.max_wallclock_seconds = 1 * 3 * 60
builder.metadata.options.parser_name = "cp2k_advanced_parser"

print("Submitted calculation...")
calc = run(builder)

# Check walltime not exceeded.
assert calc["output_parameters"]["exceeded_walltime"] is False

# Check energy.
expected_energy = -1.17212345935
if abs(calc["output_parameters"]["energy"] - expected_energy) < 1e-10:
print("OK, energy has the expected value.")
else:
print("ERROR!")
print(f"Expected energy value: {expected_energy}")
print(f"Actual energy value: {calc['output_parameters']['energy']}")
sys.exit(3)

# Check geometry.
expected_dist = 0.732594809575
dist = calc["output_structure"].get_ase().get_distance(0, 1)
if abs(dist - expected_dist) < 1e-7:
print("OK, H-H distance has the expected value.")
else:
print("ERROR!")
print(f"Expected dist value: {expected_dist}")
print(f"Actual dist value: {dist}")
sys.exit(3)

# Check motion step information.
assert calc['output_parameters']['motion_step_info']['step'] == [0, 1, 2], "ERROR: motion step info is incorrect"
assert calc['output_parameters']['motion_step_info']['cell_a_angs'] == [4.0, 4.0, 4.0], "ERROR: motion step info is incorrect"

@click.command("cli")
@click.argument("codelabel")
def cli(codelabel):
"""Click interface."""
try:
code = load_code(codelabel)
except NotExistent:
print(f"The code '{codelabel}' does not exist.")
sys.exit(1)
example_geopt(code)


if __name__ == "__main__":
cli()

0 comments on commit 6c56102

Please sign in to comment.