Skip to content

Commit

Permalink
Merge pull request #8 from pierre-24/append_data
Browse files Browse the repository at this point in the history
Append data to existing file, if any
  • Loading branch information
pierre-24 authored Jan 10, 2024
2 parents ecc656e + f474157 commit efb6fac
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/use.rst
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ The output depends on the value of ``-V``, which can be:
There is no way to change this behavior.
+ By default, the program also include the base tensors calculated in the process.
The ``-S`` option prevents this (that may be useful in the case of electric field differentiation)
+ If you want to add results to existing ``molecule_nd.h5`` file, you can use the ```--append`` option.
+ Projection over normal mode of all the geometrical derivatives is requested via the ``-p`` option, but you can also request that the cartesian hessian used to do so is different, with the ``-H`` option (which accepts FCHK and dalton archives with cartesian hessian in it as argument).


Expand Down
15 changes: 14 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,17 @@ def main():
storage = files.ComputationalResults(recipe, directory=recipe_directory)
storage.read(args.data)

original_cf = None
if args.append:
with open(args.output) as f:
try:
original_cf = ChemistryDataFile()
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')


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
10 changes: 7 additions & 3 deletions tests/tests_nachos_bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def test_bake_gaussian_F(self):

storage = files.ComputationalResults(r, directory=directory)
storage.read(storage_path)
baker = baking.Baker(r, storage, directory=directory)

# with energy:
baker = baking.Baker(r, storage, directory=directory)
cf_with_energy = baker.bake(only=[(derivatives.Derivative(), 3)])

self.assertIn('F', cf_with_energy.derivatives)
Expand All @@ -64,6 +64,7 @@ def test_bake_gaussian_F(self):
electrical_derivatives['FFF']['static'], cf_with_energy.derivatives['FFF']['static'])

# with mu:
baker = baking.Baker(r, storage, directory=directory)
cf_with_mu = baker.bake(only=[(derivatives.Derivative('F'), 2)])

self.assertIn('FF', cf_with_mu.derivatives)
Expand All @@ -79,6 +80,7 @@ def test_bake_gaussian_F(self):
delta=.01)

# with alpha:
baker = baking.Baker(r, storage, directory=directory)
cf_with_alpha = baker.bake(only=[(derivatives.Derivative('FF'), 1)])

self.assertIn('FFF', cf_with_alpha.derivatives)
Expand All @@ -89,6 +91,7 @@ def test_bake_gaussian_F(self):
cf_with_alpha.derivatives['FFF']['static'])

# dynamic
baker = baking.Baker(r, storage, directory=directory)
cf_with_alpha = baker.bake(only=[(derivatives.Derivative('dD'), 1)])

self.assertIn('dDF', cf_with_alpha.derivatives)
Expand Down Expand Up @@ -159,9 +162,8 @@ def test_bake_gaussian_G(self):

geometrical_derivatives = fchk.property('geometrical_derivatives')

baker = baking.Baker(r, storage, directory=directory)

# with energy:
baker = baking.Baker(r, storage, directory=directory)
cf_with_energy = baker.bake(only=[(derivatives.Derivative(), 2)])

self.assertIn('G', cf_with_energy.derivatives)
Expand All @@ -175,6 +177,7 @@ def test_bake_gaussian_G(self):
geometrical_derivatives['GG'], cf_with_energy.derivatives['GG'])

# try with all
baker = baking.Baker(r, storage, directory=directory)
cf = baker.bake()

self.assertIn('G', cf.derivatives)
Expand Down Expand Up @@ -212,6 +215,7 @@ def test_bake_gaussian_G(self):
math.fabs(ph[i, i]), mwh.frequencies[i] ** 2, places=5)

# now, bake and steal results from zero field
baker = baking.Baker(r, storage, directory=directory)
cf_with_copy = baker.bake(only=[(derivatives.Derivative(), 0)], copy_zero_field_basis=True)

# gradient and hessian ...
Expand Down

0 comments on commit efb6fac

Please sign in to comment.