From c6ffa2c646833fc9b36349d4b4ecf97da893ad68 Mon Sep 17 00:00:00 2001 From: Pierre Beaujean Date: Wed, 10 Jan 2024 14:44:32 +0100 Subject: [PATCH] append to existing file --- nachos/bake.py | 14 +++++++++++++- nachos/core/baking.py | 22 ++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/nachos/bake.py b/nachos/bake.py index d14eb2d..bdf7c6e 100644 --- a/nachos/bake.py +++ b/nachos/bake.py @@ -7,6 +7,7 @@ from qcip_tools import derivatives_g, derivatives from qcip_tools.chemistry_files import helpers, PropertyNotDefined, PropertyNotPresent +from qcip_tools.chemistry_files.chemistry_datafile import ChemistryDataFile, BadChemistryDataFile import nachos from nachos.core import files, baking @@ -110,6 +111,9 @@ def get_arguments_parser(): type=treat_romberg_arg, help='Bypass detection and force a value in the triangle. Must be of the form `k;m`.') + arguments_parser.add_argument( + '-a', '--append', action='store_true', help='Append to existing H5 file') + return arguments_parser @@ -144,8 +148,16 @@ def main(): storage = files.ComputationalResults(recipe, directory=recipe_directory) storage.read(args.data) + original_cf = ChemistryDataFile() + if args.append: + with open(args.output) as f: + try: + original_cf.read(f) + except BadChemistryDataFile as e: + return exit_failure('Cannot append data to `{}`: {}'.format(args.output, e)) + # go and bake - baker = baking.Baker(recipe, storage, directory=recipe_directory) + baker = baking.Baker(recipe, storage, directory=recipe_directory, original_cf=original_cf) only = None if args.only: diff --git a/nachos/core/baking.py b/nachos/core/baking.py index 0d3835b..bb43987 100644 --- a/nachos/core/baking.py +++ b/nachos/core/baking.py @@ -13,6 +13,14 @@ class BadBaking(Exception): pass +def _equal_molecules_or_raise(mol1, mol2): + """Compare two geometries + """ + + if [a.symbol for a in mol1] != [a.symbol for a in mol2]: + raise BadBaking('not the same geometries: atomic symbols are different') + + class Baker: """Baker class to finally perform the numerical differentiation @@ -22,9 +30,11 @@ class Baker: :type storage: nachos.core.files.ComputationalResults :param directory: working directory :type directory: str + :param original_cf: Originak chemistry file to append to + :type original_cf: qcip_tools.chemistry_files.chemistry_datafile.ChemistryDataFile """ - def __init__(self, recipe, storage, directory='.'): + def __init__(self, recipe, storage, directory='.', original_cf=None): self.recipe = recipe if not os.path.isdir(directory): @@ -33,6 +43,14 @@ def __init__(self, recipe, storage, directory='.'): self.directory = directory self.storage = storage + if original_cf is not None: + _equal_molecules_or_raise(self.recipe.geometry, original_cf.molecule) + self.original_cf = original_cf + else: + self.original_cf = chemistry_datafile.ChemistryDataFile.from_molecule( + self.recipe.geometry, 'nachos ND result' + ) + if self.storage.check() != ([], []): raise BadBaking('The storage (h5 file) does not fulfill the recipe!') @@ -73,7 +91,7 @@ def bake(self, only=None, out=sys.stdout, verbosity_level=0, copy_zero_field_bas raise BadBaking('no differentiation requested!') bases.sort(key=lambda a: a[1], reverse=True) - f = chemistry_datafile.ChemistryDataFile.from_molecule(self.recipe.geometry, 'nachos ND result') + f = self.original_cf dof = 3 * len(self.recipe.geometry) if copy_zero_field_basis: