# 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."""fromtypingimportCallable,TextIOimportattrsimportnumpyasnpfrom..iodataimportIOData__all__=("write_input_base",)
[docs]defwrite_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.fields["title"]=data.titleifdata.titleisnotNoneelse"Input Generated by IOData"# Convert spin polarization to multiplicity.fields["spinmult"]=int(abs(np.round(data.spinpol)))+1ifdata.spinpolisnotNoneelse1fields["charge"]=int(data.charge)ifdata.chargeisnotNoneelse0# User- or format-specific fields have priority.fields.update(user_fields)# Generate geometry.geometry=[atom_line(data,iatom)foriatominrange(data.natom)]fields["geometry"]="\n".join(geometry)print(template.format(**fields),file=fh)