-
Notifications
You must be signed in to change notification settings - Fork 3
Add from_matrix class method to Box. #81
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
Changes from 1 commit
6dfb4a7
553a041
f41e5ce
b530570
c9ef348
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,55 @@ def cast(cls, value): | |
| else: | ||
| raise TypeError(f"Unable to cast boxlike object with shape {v.shape}") | ||
|
|
||
| @classmethod | ||
| def from_matrix(cls, low, matrix): | ||
| """Create a Box from low and matrix. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| low : list | ||
| Origin of the box. | ||
| matrix : :class:`numpy.ndarray` | ||
| Upper triangular matrix in LAMMPS style: | ||
| [[lx, xy, xz], | ||
| [0, ly, yz], | ||
| [0, 0, lz]] | ||
|
|
||
| Returns | ||
| ------- | ||
| :class:`Box` | ||
| A simulation box. | ||
|
|
||
| Raises | ||
| ------ | ||
| TypeError | ||
| If `low` is not length 3. | ||
| TypeError | ||
| If `matrix` is not a 3x3 array. | ||
| ValueError | ||
| If `matrix` is not upper triangular. | ||
|
|
||
| """ | ||
| low = numpy.array(low, dtype=float) | ||
| arr = numpy.array(matrix, dtype=float) | ||
|
|
||
| if low.shape != (3,): | ||
| raise TypeError("Low must be a 3-tuple") | ||
| if arr.shape != (3, 3): | ||
| raise TypeError("Box matrix must be a 3x3 array") | ||
| if arr[1, 0] != 0 or arr[2, 0] != 0 or arr[2, 1] != 0: | ||
| raise ValueError("Box matrix must be upper triangular") | ||
|
|
||
| # Extract diagonal elements for box lengths | ||
| lx, ly, lz = arr[0, 0], arr[1, 1], arr[2, 2] | ||
| high = low + [lx, ly, lz] | ||
|
|
||
| # Extract tilt factors | ||
| xy, xz, yz = arr[0, 1], arr[0, 2], arr[1, 2] | ||
| tilt = [xy, xz, yz] if numpy.any([xy, xz, yz]) else None | ||
|
||
|
|
||
| return cls(low, high, tilt) | ||
|
|
||
| @property | ||
| def low(self): | ||
| """:class:`numpy.ndarray`: Box low.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.