-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save Q vectors in the output file #634
Open
MBartkowiakSTFC
wants to merge
5
commits into
protos
Choose a base branch
from
maciej/qvector-npt-smearing
base: protos
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
08dabaf
Standardise the Q to HKL conversion
MBartkowiakSTFC d161aa0
Add q-vector writeout to output files
MBartkowiakSTFC 92cbf2c
Improve visualisation of data arrays with no axes
MBartkowiakSTFC 9bed8b0
Remove slash and pipe character from vector group names
MBartkowiakSTFC dcbd3f6
Remove debug print statements
MBartkowiakSTFC 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
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
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
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
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 |
---|---|---|
|
@@ -14,12 +14,19 @@ | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
import abc | ||
from typing import TYPE_CHECKING | ||
|
||
import numpy as np | ||
|
||
from MDANSE.Core.Error import Error | ||
from MDANSE.Framework.Configurable import Configurable | ||
from MDANSE.Core.SubclassFactory import SubclassFactory | ||
from MDANSE.MLogging import LOG | ||
|
||
if TYPE_CHECKING: | ||
from MDANSE.MolecularDynamics.UnitCell import UnitCell | ||
from MDANSE.Framework.OutputVariables.IOutputVariable import OutputData | ||
|
||
|
||
class QVectorsError(Error): | ||
pass | ||
|
@@ -54,3 +61,90 @@ def generate(self) -> bool: | |
|
||
def setStatus(self, status): | ||
self._status = status | ||
|
||
@classmethod | ||
def qvectors_to_hkl( | ||
self, vector_array: np.array, unit_cell: "UnitCell" | ||
) -> np.ndarray: | ||
"""Using a unit cell definition, recalculates an array | ||
of q vectors to an equivalent array of HKL Miller indices. | ||
|
||
Parameters | ||
---------- | ||
vector_array : np.array | ||
a (3,N) array of scattering vectors | ||
unit_cell : UnitCell | ||
an instance of UnitCell class describing the simulation box | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
A (3,N) array of HKL values (Miller indices) | ||
""" | ||
return np.dot(unit_cell.direct, vector_array) / (2 * np.pi) | ||
|
||
@classmethod | ||
def hkl_to_qvectors(self, hkls: np.array, unit_cell: "UnitCell") -> np.ndarray: | ||
"""Converts an array of HKL values to scattering vectors | ||
based on the definition of a unit cell. | ||
|
||
Parameters | ||
---------- | ||
hkls : np.array | ||
A (3,N) array of HKL values (Miller indices) | ||
unit_cell : UnitCell | ||
An instance of UnitCell class describing the simulation box shape | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
a (3, N) array of Q vectors (scattering vectors) | ||
""" | ||
return 2 * np.pi * np.dot(unit_cell.inverse, hkls) | ||
|
||
def write_vectors_to_file(self, output_data: "OutputData"): | ||
"""Writes a summary of the generated vectors to the output | ||
file using an OutputData class instance. | ||
|
||
Parameters | ||
---------- | ||
output_data : OutputData | ||
An object managing the writeout to one or many output files | ||
""" | ||
q_values = [float(x) for x in self._configuration["q_vectors"].keys()] | ||
output_data.add( | ||
"vector_generator_q", | ||
"LineOutputVariable", | ||
q_values, | ||
units="1/nm", | ||
) | ||
qvector_lengths = [ | ||
self._configuration["q_vectors"][q]["q_vectors"].shape[1] for q in q_values | ||
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. Given you refer to
|
||
] | ||
qarray_maxlength = np.max(qvector_lengths) | ||
output_data.add( | ||
"vector_generator_qvector_array", | ||
"VolumeOutputVariable", | ||
(len(q_values), 3, qarray_maxlength), | ||
units="1/nm", | ||
) | ||
output_data["vector_generator_qvector_array"][:] = 0.0 | ||
for nq, q in enumerate(q_values): | ||
output_data["vector_generator_qvector_array"][ | ||
nq, :, : qvector_lengths[nq] | ||
] = self._configuration["q_vectors"][q]["q_vectors"] | ||
try: | ||
hkl_arrays = [self._configuration["q_vectors"][q]["hkls"] for q in q_values] | ||
except KeyError: | ||
return | ||
output_data.add( | ||
"vector_generator_hkl_array", | ||
"VolumeOutputVariable", | ||
(len(q_values), 3, qarray_maxlength), | ||
units="au", | ||
) | ||
output_data["vector_generator_hkl_array"][:] = 0.0 | ||
for nq, q in enumerate(q_values): | ||
output_data["vector_generator_hkl_array"][nq, :, : qvector_lengths[nq]] = ( | ||
hkl_arrays[nq] | ||
) |
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -43,27 +43,27 @@ def _generate(self): | |||||||||
if self._configuration["seed"]["value"] != 0: | ||||||||||
np.random.seed(self._configuration["seed"]["value"]) | ||||||||||
random.seed(self._configuration["seed"]["value"]) | ||||||||||
|
||||||||||
qMax = ( | ||||||||||
self._configuration["shells"]["last"] | ||||||||||
+ 0.5 * self._configuration["width"]["value"] | ||||||||||
) | ||||||||||
|
||||||||||
hklMax = ( | ||||||||||
np.ceil([qMax / np.sqrt(np.sum(v**2)) for v in self._inverseUnitCell.T]) + 1 | ||||||||||
) | ||||||||||
hklMax = np.ceil(self.qvectors_to_hkl(qMax * np.eye(3), self._unit_cell)) + 1 | ||||||||||
|
||||||||||
vects = np.mgrid[ | ||||||||||
-hklMax[0] : hklMax[0] + 1, | ||||||||||
-hklMax[1] : hklMax[1] + 1, | ||||||||||
-hklMax[2] : hklMax[2] + 1, | ||||||||||
hkl_vects = np.mgrid[ | ||||||||||
-hklMax[0, 0] : hklMax[0, 0] + 1, | ||||||||||
-hklMax[1, 1] : hklMax[1, 1] + 1, | ||||||||||
-hklMax[2, 2] : hklMax[2, 2] + 1, | ||||||||||
] | ||||||||||
|
||||||||||
vects = vects.reshape( | ||||||||||
3, int(2 * hklMax[0] + 1) * int(2 * hklMax[1] + 1) * int(2 * hklMax[2] + 1) | ||||||||||
hkl_vects = hkl_vects.reshape( | ||||||||||
3, | ||||||||||
int(2 * hklMax[0, 0] + 1) | ||||||||||
* int(2 * hklMax[1, 1] + 1) | ||||||||||
* int(2 * hklMax[2, 2] + 1), | ||||||||||
Comment on lines
+61
to
+63
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.
Suggested change
|
||||||||||
) | ||||||||||
|
||||||||||
vects = np.dot(self._inverseUnitCell, vects) | ||||||||||
vects = self.hkl_to_qvectors(hkl_vects, self._unit_cell) | ||||||||||
|
||||||||||
dists2 = np.sum(vects**2, axis=0) | ||||||||||
|
||||||||||
|
@@ -96,11 +96,8 @@ def _generate(self): | |||||||||
self._configuration["q_vectors"][q]["q_vectors"] = vects[:, hits] | ||||||||||
self._configuration["q_vectors"][q]["n_q_vectors"] = n | ||||||||||
self._configuration["q_vectors"][q]["q"] = q | ||||||||||
self._configuration["q_vectors"][q]["hkls"] = np.rint( | ||||||||||
np.dot( | ||||||||||
self._directUnitCell, | ||||||||||
self._configuration["q_vectors"][q]["q_vectors"], | ||||||||||
) | ||||||||||
self._configuration["q_vectors"][q]["hkls"] = self.qvectors_to_hkl( | ||||||||||
vects[:, hits], self._unit_cell | ||||||||||
) | ||||||||||
|
||||||||||
if self._status is not None: | ||||||||||
|
Oops, something went wrong.
Oops, something went wrong.
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.
Keys is redundant.