Skip to content

Commit

Permalink
append to existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-24 committed Jan 10, 2024
1 parent ecc656e commit c6ffa2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 13 additions & 1 deletion nachos/bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
22 changes: 20 additions & 2 deletions nachos/core/baking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Check failure on line 21 in nachos/core/baking.py

View workflow job for this annotation

GitHub Actions / build (3.9)

not the same geometries: atomic symbols are different

Check failure on line 21 in nachos/core/baking.py

View workflow job for this annotation

GitHub Actions / build (3.10)

not the same geometries: atomic symbols are different

Check failure on line 21 in nachos/core/baking.py

View workflow job for this annotation

GitHub Actions / build (3.11)

not the same geometries: atomic symbols are different

Check failure on line 21 in nachos/core/baking.py

View workflow job for this annotation

GitHub Actions / build (3.12)

not the same geometries: atomic symbols are different


class Baker:
"""Baker class to finally perform the numerical differentiation
Expand All @@ -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):
Expand All @@ -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!')

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c6ffa2c

Please sign in to comment.