Source code for iodata.inputs.common
# IODATA is an input and output module for quantum chemistry.
# Copyright (C) 2011-2019 The IODATA Development Team
#
# This file is part of IODATA.
#
# IODATA is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# IODATA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
# --
"""Utilities for writing input files."""
from collections.abc import Callable
from typing import TextIO
import attrs
import numpy as np
from ..iodata import IOData
__all__ = ("write_input_base",)
[docs]
def write_input_base(
fh: TextIO, data: IOData, template: str, atom_line: Callable, user_fields: dict
):
"""Generate a dictionary with fields to replace in the template."""
# Convert IOData instance to dict for formatting.
fields = attrs.asdict(data, recurse=False)
# Set general defaults.
title = data.title if data.title is not None else "Input Generated by IOData"
# The templates need the title on a single line, e.g. a blank line ends a section
# in a Gaussian input and the line of an ORCA input starts with a comment character.
fields["title"] = " ".join(line.strip() for line in title.splitlines() if line.strip())
# Convert spin polarization to multiplicity.
fields["spinmult"] = int(abs(np.round(data.spinpol))) + 1 if data.spinpol is not None else 1
fields["charge"] = int(data.charge) if data.charge is not None else 0
# User- or format-specific fields have priority.
fields.update(user_fields)
# Generate geometry.
geometry = [atom_line(data, iatom) for iatom in range(data.natom)]
fields["geometry"] = "\n".join(geometry)
print(template.format(**fields), file=fh)