Skip to content

Commit

Permalink
Merge pull request #20 from bpuchala/2.X_matrix_rep
Browse files Browse the repository at this point in the history
Add SymOp.matrix_rep to get matrix representation
  • Loading branch information
bpuchala authored Oct 19, 2023
2 parents bd8d2f6 + 8d9bd30 commit a7a54a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python/src/xtal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,46 @@ PYBIND11_MODULE(_xtal, m) {
)pbdoc")
.def("matrix", &xtal::get_matrix,
"Returns the transformation matrix value.")
.def(
"matrix_rep",
[](xtal::SymOp const &op, std::string key) -> Eigen::MatrixXd {
Eigen::MatrixXd M;
try {
AnisoValTraits traits(key);
M = traits.symop_to_matrix(get_matrix(op), get_translation(op),
get_time_reversal(op));
} catch (std::exception &e) {
std::stringstream msg;
msg << "Error getting matrix rep: CASM does not know how to "
"transform the property '"
<< key << "'.";
throw std::runtime_error(msg.str());
}
return M;
},
R"pbdoc(
Returns the matrix representation of a symmetry operation for transforming \
properties
Parameters
----------
key: str
The name of the CASM-supported property to be transformed.
Returns
-------
matrix_rep : numpy.ndarray[numpy.float64[m, m]]
The matrix representation for transforming properties. The matrix is
square, with dimension equal to the standard dimension of the specified
property. For example, `m=3` for `key="disp"`, and `m=6` for
`key="Hstrain"`. Local properties, such as `"disp"`, stored as columns of
array `local_values`, can then be transformed using
``matrix_rep @ local_values``. Global properties, such as `"Hstrain"`,
stored as array `global_values` with a single column, can similarly be
transformed using ``matrix_rep @ global_values``.
)pbdoc",
py::arg("key"))
.def("translation", &xtal::get_translation,
"Returns the translation value.")
.def("time_reversal", &xtal::get_time_reversal,
Expand Down
12 changes: 12 additions & 0 deletions python/tests/test_symop.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def test_SymOp_mul_properties():
assert "disp" in transformed_properties
assert transformed_properties["disp"].shape == (3, 4)

matrix_rep = op.matrix_rep("disp")
transformed_disp = matrix_rep @ disp
assert np.allclose(transformed_disp, transformed_properties["disp"])

# check 1d array - accepted, but returned as (6,1) array
Hstrain = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
assert Hstrain.shape == (6,)
Expand All @@ -138,6 +142,10 @@ def test_SymOp_mul_properties():
assert "Hstrain" in transformed_properties
assert transformed_properties["Hstrain"].shape == (6, 1)

matrix_rep = op.matrix_rep("Hstrain")
transformed_Hstrain = matrix_rep @ Hstrain.reshape(-1, 1)
assert np.allclose(transformed_Hstrain, transformed_properties["Hstrain"])

# check 2d column array - accepted, stays as (6,1) array
Hstrain = np.array([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6]]).transpose()
assert Hstrain.shape == (6, 1)
Expand All @@ -149,6 +157,10 @@ def test_SymOp_mul_properties():
assert "Hstrain" in transformed_properties
assert transformed_properties["Hstrain"].shape == (6, 1)

matrix_rep = op.matrix_rep("Hstrain")
transformed_Hstrain = matrix_rep @ Hstrain
assert np.allclose(transformed_Hstrain, transformed_properties["Hstrain"])


def test_SymOp_mul_lattice(tetragonal_lattice):
R = np.array(
Expand Down

0 comments on commit a7a54a0

Please sign in to comment.