NEML2 1.4.0
Loading...
Searching...
No Matches
crystallography.cxx
1// Copyright 2023, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#include "neml2/models/crystallography/crystallography.h"
26
27#include "neml2/tensors/Transformable.h"
28#include "neml2/tensors/tensors.h"
29
30using namespace torch::indexing;
31
32namespace neml2
33{
34namespace crystallography
35{
36namespace crystal_symmetry_operators
37{
38const torch::Tensor
39tetragonal(const torch::TensorOptions & options)
40{
41 return torch::tensor({{o, z, z, z},
42 {z, z, o, z},
43 {z, o, z, z},
44 {z, z, z, o},
45 {a, z, z, -a},
46 {a, z, z, a},
47 {z, a, a, z},
48 {z, -a, a, z}},
49 options);
50}
51
52const torch::Tensor
53hexagonal(const torch::TensorOptions & options)
54{
55 return torch::tensor({{o, z, z, z},
56 {-h, z, z, b},
57 {h, z, z, b},
58 {b, z, z, -h},
59 {z, z, z, o},
60 {b, z, z, h},
61 {z, -h, b, z},
62 {z, o, z, z},
63 {z, h, b, z},
64 {z, b, h, z},
65 {z, z, o, z},
66 {z, b, -h, z}},
67 options);
68}
69
70const torch::Tensor
71cubic(const torch::TensorOptions & options)
72{
73 return torch::tensor({{o, z, z, z}, {h, h, h, h}, {-h, h, h, h}, {h, -h, h, h},
74 {h, h, -h, h}, {-h, -h, -h, h}, {h, -h, -h, h}, {-h, -h, h, h},
75 {-h, h, -h, h}, {z, z, o, z}, {z, z, z, o}, {z, o, z, z},
76 {z, -a, z, a}, {z, a, z, a}, {a, z, a, z}, {a, z, -a, z},
77 {z, z, -a, a}, {a, a, z, z}, {a, -a, z, z}, {z, z, a, a},
78 {z, -a, a, z}, {a, z, z, -a}, {z, a, a, z}, {a, z, z, a}},
79 options);
80}
81} // namespace crystal_symmetry_operators
82
83R2
84symmetry_operations_from_orbifold(std::string orbifold, const torch::TensorOptions & options)
85{
86 if (orbifold == "432")
87 {
89 }
90 else if (orbifold == "23")
91 {
93 Quaternion(crystal_symmetry_operators::cubic(options).index({Slice(0, 12)})));
94 }
95 else if (orbifold == "622")
96 {
98 }
99 else if (orbifold == "32")
100 {
102 torch::cat({crystal_symmetry_operators::hexagonal(options).index({Slice(0, 3)}),
103 crystal_symmetry_operators::hexagonal(options).index({Slice(9, 12)})})));
104 }
105 else if (orbifold == "6")
106 {
109 }
110 else if (orbifold == "3")
111 {
114 }
115 else if (orbifold == "42")
116 {
118 }
119 else if (orbifold == "4")
120 {
122 torch::cat({crystal_symmetry_operators::tetragonal(options).index({Slice(0, 1)}),
123 crystal_symmetry_operators::tetragonal(options).index({Slice(3, 6)})})));
124 }
125 else if (orbifold == "222")
126 {
129 }
130 else if (orbifold == "2")
131 {
134 }
135 else if (orbifold == "1")
136 {
139 }
140 else
141 {
142 throw NEMLException("Unknown crystal class " + orbifold);
143 }
144}
145
146Vec
148{
149 neml_assert_dbg(inp.batch_dim() == 0);
150 // Batched tensor with all possible answers
151 auto options = ops * inp;
152 // I think we have to go one by one...
153 // Slightly annoying that while Vec and torch::Tensor are convertible a
154 // list of Vecs aren't convertable into a TensorList
155 std::vector<torch::Tensor> unique{torch::Tensor(options.batch_index({0}))};
156 Vec unique_vecs = Vec(torch::stack(unique));
157 for (TorchSize i = 1; i < options.batch_sizes()[0]; i++)
158 {
159 auto vi = options.batch_index({i});
160 // Compares list of vectors to vector to figure out if any are the same
161 auto same = [](const torch::Tensor & a, const torch::Tensor & b)
162 { return torch::any(torch::all(torch::isclose(a, b), 1)); };
164 {
165 unique.push_back(torch::Tensor(vi));
166 unique_vecs = Vec(torch::stack(unique));
167 }
168 }
169 // Get the batch of all possible answers
170 return unique_vecs;
171}
172
173} // namespace crystallography
174} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition error.h:33
A batched quaternion.
Definition Quaternion.h:44
A basic R2.
Definition R2.h:42
The (logical) vector.
Definition Vec.h:42
constexpr Real o
Definition crystallography.h:39
constexpr Real z
Definition crystallography.h:40
constexpr Real a
Definition crystallography.h:36
const torch::Tensor hexagonal(const torch::TensorOptions &options)
hexagonal symmetry operators
Definition crystallography.cxx:53
constexpr Real h
Definition crystallography.h:38
constexpr Real b
Definition crystallography.h:37
const torch::Tensor tetragonal(const torch::TensorOptions &options)
tetragonal symmetry operators
Definition crystallography.cxx:39
const torch::Tensor cubic(const torch::TensorOptions &options)
cubic symmetry operators
Definition crystallography.cxx:71
Vec unique_bidirectional(const R2 &ops, const Vec &inp)
Helper to return all symmetrically-equivalent directions from a cartesian vector.
Definition crystallography.cxx:147
R2 symmetry_operations_from_orbifold(std::string orbifold, const torch::TensorOptions &options)
Helper function to return the symmetry operators given the Orbifold notation.
Definition crystallography.cxx:84
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
R2 transform_from_quaternion(const Quaternion &q)
Construct from quaternions, useful for comparison to old NEML.
Definition Transformable.cxx:33