Skip to content

Commit

Permalink
GaussianCalculation: Do no limit kwargs for GaussianInput (#34)
Browse files Browse the repository at this point in the history
* Tests: Automate calculation test reference generation and checking

The reference file for the single `GaussianCalculation` test was
generated manually and read manually to compare to the one created by
running the calcjob plugin. This is replaced by using the fixture
`file_regression` which not only automates this, if the reference file
needs to change due to a change in the code, it can be automatically
generated by running the tests with the `--force-regen` flag.

* `GaussianCalculation`: Do no limit kwargs for `GaussianInput`

The `GaussianCalculation` is rendering the input file from the
`parameters` input dictionary through the `GaussianInput` utility from
`pymatgen`. However, it was hard-coding which keywords could be
specified through the parameters. For example, the `gen_basis` keyword
of `GaussianInput` was ignored. Here we simply expand the entire
parameters dictionary as it is provided through the inputs, after
setting some relevant defaults.
  • Loading branch information
sphuber authored Apr 5, 2023
1 parent 8bb77e0 commit e953c34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 64 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ repos:
rev: v4.4.0
hooks:
- id: end-of-file-fixer
exclude: ^.*data/
exclude: &exclude_files >
(?x)^(
.*data/|
aiida_gaussian/tests/.*.in$
)$
- id: trailing-whitespace
exclude: *exclude_files
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pycqa/isort
Expand Down
39 changes: 8 additions & 31 deletions aiida_gaussian/calculations/gaussian.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Gaussian input plugin."""

import pymatgen.io.gaussian as mgaus
from aiida.common import CalcInfo, CodeInfo

# from aiida.cmdline.utils import echo
from aiida.engine import CalcJob
from aiida.orm import Dict, Float, RemoteData
from aiida.plugins import DataFactory
from pymatgen.io.gaussian import GaussianInput

StructureData = DataFactory("core.structure")

Expand Down Expand Up @@ -213,32 +212,10 @@ def prepare_for_submission(self, folder):
return calcinfo

@classmethod
def _render_input_string_from_params(cls, param_dict, pmg_structure):
"""
Generate the Gaussian input file using pymatgen
In the following, the param_dict.get() method is used, which returns
`None` if the corresponding key is not specified, resulting in the
default value defined by the pymatgen library. For example, charge
and spin_multiplicity are then defined based on the pymatgen molecule
definition.
"""

inp = mgaus.GaussianInput(
pmg_structure,
title="input generated by the aiida-gaussian plugin",
charge=param_dict.get("charge"),
spin_multiplicity=param_dict.get(
"multiplicity", param_dict.get("spin_multiplicity")
),
functional=param_dict.get("functional"),
basis_set=param_dict.get("basis_set"),
route_parameters=param_dict.get("route_parameters"),
input_parameters=param_dict.get("input_parameters"),
link0_parameters=param_dict.get("link0_parameters"),
dieze_tag=param_dict.get(
"dieze_tag", "#N"
), # normal print level by default
)

return inp.to_string(cart_coords=True)
def _render_input_string_from_params(cls, parameters, pmg_structure):
"""Generate the Gaussian input file using pymatgen."""
parameters.setdefault("dieze_tag", "#N")
parameters.setdefault("spin_multiplicity", parameters.pop("multiplicity", None))
parameters["title"] = "input generated by the aiida-gaussian plugin"
gaussian_input = GaussianInput(pmg_structure, **parameters)
return gaussian_input.to_string(cart_coords=True)
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
""" Tests for gaussian plugin
"""

"""Tests for gaussian plugin."""
import os

from aiida.orm import Dict, StructureData
from aiida.plugins import CalculationFactory
from pymatgen.core import Molecule

from aiida_gaussian import tests
from aiida_gaussian.calculations.gaussian import GaussianCalculation


def test_gaussian(fixture_code):

def test_default(fixture_code, generate_calc_job, file_regression):
"""Test a default calculation for :class:`aiida_gaussian.calculations.gaussian.GaussianCalculation`."""
geometry_file = os.path.join(tests.TEST_DIR, "data", "ch4.xyz")
expected_inp_file = os.path.join(tests.TEST_DIR, "data", "gaussian_test.inp")

# structure
structure = StructureData(pymatgen_molecule=Molecule.from_file(geometry_file))

num_cores = 1
Expand Down Expand Up @@ -58,26 +53,6 @@ def test_gaussian(fixture_code):
},
}

# Prepare the fake calculation for submission in a "sandbox folder"

from aiida.common.folders import SandboxFolder
from aiida.engine.utils import instantiate_process
from aiida.manage.manager import get_manager

manager = get_manager()
runner = manager.get_runner()

process_class = CalculationFactory("gaussian")
process = instantiate_process(runner, process_class, **inputs)

sandbox_folder = SandboxFolder()

process.prepare_for_submission(sandbox_folder)

with sandbox_folder.open("aiida.inp") as handle:
input_written = handle.read()

with open(expected_inp_file) as f:
expected_inp = f.read()

assert input_written == expected_inp
tmp_path, _ = generate_calc_job(GaussianCalculation, inputs)
content_input_file = (tmp_path / GaussianCalculation.INPUT_FILE).read_text()
file_regression.check(content_input_file, encoding="utf-8", extension=".in")
File renamed without changes.

0 comments on commit e953c34

Please sign in to comment.