This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Adds a property string to ase_to_tensormap #55
Open
bananenpampe
wants to merge
6
commits into
lab-cosmo:main
Choose a base branch
from
bananenpampe:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
180372d
adds a property string to ase_to_tensormap
bananenpampe 5f957fd
add whitespace
bananenpampe 75442be
line too long
bananenpampe 7b46ac8
changeing stress to info dict
bananenpampe a31ed02
Adds the possibility to get energies and forces
bananenpampe 08c47db
Update src/equisolve/utils/convert.py
bananenpampe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
import ase | ||
import numpy as np | ||
import pytest | ||
from ase.calculators.calculator import Calculator, all_changes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be removed? |
||
from ase.stress import voigt_6_to_full_3x3_stress | ||
from numpy.testing import assert_equal | ||
|
||
from equisolve.utils import ase_to_tensormap, properties_to_tensormap | ||
|
@@ -35,7 +37,9 @@ def forces(self): | |
|
||
@pytest.fixture | ||
def stress(self): | ||
return [i for i in self.rng.random([self.n_strucs, 3, 3])] | ||
return [ | ||
voigt_6_to_full_3x3_stress(i) for i in self.rng.random([self.n_strucs, 6]) | ||
] | ||
|
||
def test_ase_to_tensormap(self, energies, forces, stress): | ||
frames = [] | ||
|
@@ -61,6 +65,67 @@ def test_ase_to_tensormap(self, energies, forces, stress): | |
block.gradient("cell").values, -np.array(stress).reshape(-1, 3, 3, 1) | ||
) | ||
|
||
def test_ase_to_tensormap_w_calculator(self, energies, forces, stress): | ||
class CustomCalculator(Calculator): | ||
implemented_properties = ("energy", "forces", "stress") | ||
|
||
def __init__(self, energy, forces, stress, **kwargs): | ||
Calculator.__init__(self, **kwargs) | ||
self.energy = energy # Predefined potential energy | ||
self.forces = forces # Predefined forces | ||
self.stress = stress # Predefined stress | ||
|
||
def calculate( | ||
self, atoms=None, properties=("energy"), system_changes=all_changes | ||
): | ||
super().calculate(atoms, properties, system_changes) | ||
|
||
self.results["energy"] = self.energy | ||
self.results["forces"] = self.forces | ||
self.results["stress"] = self.stress | ||
|
||
frames = [] | ||
for i in range(len(energies)): | ||
frame = ase.Atoms(self.n_atoms * "H") | ||
frame.calc = CustomCalculator(energies[i], forces[i], stress[i]) | ||
frame.info["energy"] = energies[i] | ||
frame.arrays["forces"] = forces[i] | ||
frame.info["stress"] = stress[i] | ||
frames.append(frame) | ||
|
||
property_tm = ase_to_tensormap(frames, "energy", "forces", "stress") | ||
|
||
# Use `[0]` function without parameters to check that TensorMap | ||
# only has one block. | ||
block = property_tm[0] | ||
|
||
assert_equal(block.values, np.array(energies).reshape(-1, 1)) | ||
assert_equal( | ||
block.gradient("positions").values, | ||
-np.concatenate(forces, axis=0).reshape(-1, 3, 1), | ||
) | ||
assert_equal( | ||
block.gradient("cell").values, -np.array(stress).reshape(-1, 3, 3, 1) | ||
) | ||
|
||
property_tm = ase_to_tensormap(frames) | ||
block = property_tm[0] | ||
|
||
assert_equal(block.values, np.array(energies).reshape(-1, 1)) | ||
assert_equal( | ||
block.gradient("positions").values, | ||
-np.concatenate(forces, axis=0).reshape(-1, 3, 1), | ||
) | ||
|
||
print(block.gradient("cell").values.shape) | ||
print(np.array(stress).reshape(-1, 3, 3, 1).shape) | ||
|
||
print(block.gradient("cell").values + np.array(stress).reshape(-1, 3, 3, 1)) | ||
|
||
assert_equal( | ||
block.gradient("cell").values, -np.array(stress).reshape(-1, 3, 3, 1) | ||
) | ||
|
||
def test_properties_to_tensormap(self, energies, forces, stress): | ||
property_tm = properties_to_tensormap(energies, forces, stress) | ||
block = property_tm[0] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this also needs a check if energy is present right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhh, honestly I wanted this to fail since having gradients available but not the property, seems a bit weird to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, forgot that this just extracts the property