33 class LatticeError :
public std::exception {
35 explicit LatticeError(
const char *message) : msg_(message) {}
37 explicit LatticeError(
const std::string &message) : msg_(message) {}
39 ~LatticeError()
noexcept override =
default;
41 [[nodiscard]]
const char *what()
const noexcept override {
return msg_.c_str(); }
47 namespace iterator_utils {
55 template<
class Tvec,
class Lattice>
56 class LatticeIterator {
57 using Tscal = shambase::VecComponent<Tvec>;
58 static constexpr u32 dim = 3;
61 std::array<i32, dim> coord_min;
63 std::array<size_t, dim> coord_delta;
71 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max)
72 : dr(dr), coord_min(coord_min), current_idx(0),
74 size_t(coord_max[0] - coord_min[0]),
75 size_t(coord_max[1] - coord_min[1]),
76 size_t(coord_max[2] - coord_min[2]),
80 for (
int ax = 0; ax < dim; ax++) {
81 if (coord_min[ax] == coord_max[ax]) {
86 max_coord = coord_delta[0] * coord_delta[1] * coord_delta[2];
89 inline bool is_done() {
return done; }
100 std::array<i32, 3> current = {
101 coord_min[0] +
i32(current_idx % coord_delta[0]),
102 coord_min[1] +
i32((current_idx / coord_delta[0]) % coord_delta[1]),
103 coord_min[2] +
i32((current_idx / (coord_delta[0] * coord_delta[1]))),
107 Tvec ret = Lattice::generator(dr, current);
112 if (current_idx >= max_coord) {
119 inline std::vector<Tvec> next_n(
u64 nmax) {
120 std::vector<Tvec> ret{};
121 for (
u64 i = 0; i < nmax; i++) {
125 ret.push_back(next());
127 shamlog_debug_ln(
"Lattice iterator",
"next_n final idx", current_idx);
131 inline void skip(
u64 n) {
135 if (current_idx >= max_coord) {
138 shamlog_debug_ln(
"Lattice iterator",
"skip final idx", current_idx);
149 template<
class Tvec,
class Lattice>
150 class LatticeIteratorDiscontinuous {
151 using Tscal = shambase::VecComponent<Tvec>;
152 static constexpr u32 dim = 3;
156 std::array<std::vector<size_t>, dim> remapped_indices;
158 std::array<size_t, dim> coord_delta;
165 LatticeIteratorDiscontinuous(
166 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max)
167 : dr(dr), current_idx(0), coord_delta({
168 size_t(coord_max[0] - coord_min[0]),
169 size_t(coord_max[1] - coord_min[1]),
170 size_t(coord_max[2] - coord_min[2]),
174 for (
int ax = 0; ax < dim; ax++) {
175 if (coord_min[ax] == coord_max[ax]) {
180 max_coord = coord_delta[0] * coord_delta[1] * coord_delta[2];
182 for (
int ax = 0; ax < dim; ax++) {
184 while (!it.is_done()) {
185 remapped_indices[ax].push_back(it.next());
190 inline bool is_done() {
return done; }
200 std::array<i32, 3> current = {
201 i32(current_idx % coord_delta[0]),
202 i32((current_idx / coord_delta[0]) % coord_delta[1]),
203 i32((current_idx / (coord_delta[0] * coord_delta[1]))),
206 current[0] = remapped_indices[0][current[0]];
207 current[1] = remapped_indices[1][current[1]];
208 current[2] = remapped_indices[2][current[2]];
210 Tvec ret = Lattice::generator(dr, current);
215 if (current_idx >= max_coord) {
222 inline std::vector<Tvec> next_n(
u64 nmax) {
223 std::vector<Tvec> ret{};
224 for (
u64 i = 0; i < nmax; i++) {
228 ret.push_back(next());
230 shamlog_debug_ln(
"Discontinuous iterator",
"next_n final idx", current_idx);
234 inline void skip(
u64 n) {
238 if (current_idx >= max_coord) {
241 shamlog_debug_ln(
"Discontinuous iterator",
"skip final idx", current_idx);
255 static constexpr u32 dim = 3;
258 dim == shambase::VectorProperties<Tvec>::dimension,
"this lattice exist only in dim 3");
260 using Tscal = shambase::VecComponent<Tvec>;
269 static inline constexpr Tvec
generator(Tscal dr, std::array<i32, dim> coord)
noexcept {
276 = {2 * i + (sycl::abs(j + k) % 2),
277 sycl::sqrt(3.) * (j + (1. / 3.) * (sycl::abs(k) % 2)),
278 2 * sycl::sqrt(6.) * k / 3};
292 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
293 if (coord_max[0] - coord_min[0] < 2) {
297 if ((coord_max[1] + coord_min[1]) % 2 != 0) {
301 if ((coord_max[2] + coord_min[2]) % 2 != 0) {
318 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
319 Tscal xmin, xmax, ymin, ymax, zmin, zmax;
321 xmin = 2 * coord_min[0];
322 xmax = 2 * coord_max[0];
324 ymin = sycl::sqrt(3.) * coord_min[1];
325 ymax = sycl::sqrt(3.) * coord_max[1];
327 zmin = 2 * sycl::sqrt(6.) * coord_min[2] / 3;
328 zmax = 2 * sycl::sqrt(6.) * coord_max[2] / 3;
332 "x axis count should be greater than 1\n"
333 "y axis count should be even\n"
334 "z axis count should be even");
337 return {Tvec{xmin, ymin, zmin} * dr, Tvec{xmax, ymax, zmax} * dr};
340 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
341 get_box_index_bounds(Tscal dr, Tvec box_min, Tvec box_max) {
346 coord_min[0] = box_min[0] / 2.;
347 coord_max[0] = box_max[0] / 2.;
349 coord_min[1] = box_min[1] / sycl::sqrt(3.);
350 coord_max[1] = box_max[1] / sycl::sqrt(3.);
352 coord_min[2] = box_min[2] / (2 * sycl::sqrt(6.) / 3);
353 coord_max[2] = box_max[2] / (2 * sycl::sqrt(6.) / 3);
358 std::array<i32, 3> ret_coord_min
359 = {
i32(coord_min.x()) - 1,
i32(coord_min.y()) - 1,
i32(coord_min.z()) - 1};
360 std::array<i32, 3> ret_coord_max
361 = {
i32(coord_max.x()) + 1,
i32(coord_max.y()) + 1,
i32(coord_max.z()) + 1};
363 return {ret_coord_min, ret_coord_max};
373 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
375 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
376 std::array<i32, dim> ret_coord_min;
377 std::array<i32, dim> ret_coord_max;
379 ret_coord_min[0] = coord_min[0];
380 ret_coord_min[1] = coord_min[1];
381 ret_coord_min[2] = coord_min[2];
383 ret_coord_max[0] = coord_max[0];
384 ret_coord_max[1] = coord_max[1];
385 ret_coord_max[2] = coord_max[2];
387 if (coord_max[0] - coord_min[0] < 2) {
391 if ((coord_max[1] + coord_min[1]) % 2 != 0) {
395 if ((coord_max[2] + coord_min[2]) % 2 != 0) {
399 return {ret_coord_min, ret_coord_max};
409 inline static std::tuple<Tvec, Tvec> get_ideal_hcp_box(
410 Tscal dr, std::tuple<Tvec, Tvec> box) {
412 auto [bmin, bmax] = box;
414 auto [idxs_min, idxs_max] = LatticeHCP::get_box_index_bounds(dr, bmin, bmax);
416 auto [idxs_min_per, idxs_max_per]
422 return {ret.lower, ret.upper};
443 static constexpr u32 dim = 3;
446 dim == shambase::VectorProperties<Tvec>::dimension,
"this lattice exist only in dim 3");
448 using Tscal = shambase::VecComponent<Tvec>;
454 static inline constexpr i32 pmod(
i32 a,
i32 b)
noexcept {
return ((a % b) + b) % b; }
466 static inline constexpr Tvec
generator(Tscal dr, std::array<i32, dim> coord)
noexcept {
475 2 * i +
pmod(j + s, 2), sycl::sqrt(3.) * (j + s / 3.), 2 * sycl::sqrt(6.) * k / 3};
489 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
490 if (coord_max[0] - coord_min[0] < 2) {
495 if (coord_max[1] <= coord_min[1] || coord_max[2] <= coord_min[2]) {
499 if (
pmod(coord_max[1] - coord_min[1], 2) != 0) {
520 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
521 Tscal xmin, xmax, ymin, ymax, zmin, zmax;
523 xmin = 2 * coord_min[0];
524 xmax = 2 * coord_max[0];
526 ymin = sycl::sqrt(3.) * coord_min[1];
527 ymax = sycl::sqrt(3.) * coord_max[1];
529 zmin = 2 * sycl::sqrt(6.) * coord_min[2] / 3;
530 zmax = 2 * sycl::sqrt(6.) * coord_max[2] / 3;
534 "x axis count should be greater than 1\n"
535 "y axis count should be positive and even\n"
536 "z axis count should be a positive multiple of 3");
539 return {Tvec{xmin, ymin, zmin} * dr, Tvec{xmax, ymax, zmax} * dr};
546 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
552 coord_min[0] = box_min[0] / 2.;
553 coord_max[0] = box_max[0] / 2.;
555 coord_min[1] = box_min[1] / sycl::sqrt(3.);
556 coord_max[1] = box_max[1] / sycl::sqrt(3.);
558 coord_min[2] = box_min[2] / (2 * sycl::sqrt(6.) / 3);
559 coord_max[2] = box_max[2] / (2 * sycl::sqrt(6.) / 3);
564 std::array<i32, 3> ret_coord_min
565 = {
i32(coord_min.x()) - 1,
i32(coord_min.y()) - 1,
i32(coord_min.z()) - 1};
566 std::array<i32, 3> ret_coord_max
567 = {
i32(coord_max.x()) + 1,
i32(coord_max.y()) + 1,
i32(coord_max.z()) + 1};
569 return {ret_coord_min, ret_coord_max};
580 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
582 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
583 std::array<i32, dim> ret_coord_min = coord_min;
584 std::array<i32, dim> ret_coord_max = coord_max;
586 if (ret_coord_max[0] - ret_coord_min[0] < 2) {
587 ret_coord_max[0] = ret_coord_min[0] + 2;
590 if (
pmod(ret_coord_max[1] - ret_coord_min[1], 2) != 0) {
597 return {ret_coord_min, ret_coord_max};
620 return box.upper - box.lower;
627 Tscal dr, std::tuple<Tvec, Tvec> box) {
629 auto [bmin, bmax] = box;
633 auto [idxs_min_per, idxs_max_per]
639 return {ret.lower, ret.upper};
651 static constexpr u32 dim = 3;
654 dim == shambase::VectorProperties<Tvec>::dimension,
"this lattice exist only in dim 3");
656 using Tscal = shambase::VecComponent<Tvec>;
665 static inline constexpr Tvec
generator(Tscal dr, std::array<i32, dim> coord)
noexcept {
671 Tvec r_a = {i, j, k};
676 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
677 get_box_index_bounds(Tscal dr, Tvec box_min, Tvec box_max) {
682 coord_min[0] = box_min[0];
683 coord_max[0] = box_max[0];
685 coord_min[1] = box_min[1];
686 coord_max[1] = box_max[1];
688 coord_min[2] = box_min[2];
689 coord_max[2] = box_max[2];
694 std::array<i32, 3> ret_coord_min
695 = {
i32(coord_min.x()) - 1,
i32(coord_min.y()) - 1,
i32(coord_min.z()) - 1};
696 std::array<i32, 3> ret_coord_max
697 = {
i32(coord_max.x()) + 1,
i32(coord_max.y()) + 1,
i32(coord_max.z()) + 1};
699 return {ret_coord_min, ret_coord_max};
header for PatchData related function and declaration
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
Discontinuous Iterator will iterate over every values in an integer set in the most discontinuous way...
utility for generating Cubic crystal lattices
static constexpr Tvec generator(Tscal dr, std::array< i32, dim > coord) noexcept
generate a Cubic lattice centered on (0,0,0)
iterator_utils::LatticeIteratorDiscontinuous< Tvec, LatticeCubic > IteratorDiscontinuous
Iterator utility to generate the lattice in a discontinuous order.
iterator_utils::LatticeIterator< Tvec, LatticeCubic > Iterator
Iterator utility to generate the lattice.
utility for generating FCC (face-centered cubic) crystal lattices
static constexpr std::pair< std::array< i32, dim >, std::array< i32, dim > > nearest_periodic_box_indices(std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
get the nearest integer triplets bound that gives a periodic box (by only increasing the upper bound)
static constexpr std::pair< std::array< i32, dim >, std::array< i32, dim > > get_box_index_bounds(Tscal dr, Tvec box_min, Tvec box_max)
Get the lattice integer coordinates bounds covering the given box (with a one cell margin on each sid...
static constexpr i32 z_period
Number of layers after which the stacking sequence repeats along z.
iterator_utils::LatticeIteratorDiscontinuous< Tvec, LatticeFCC > IteratorDiscontinuous
Iterator utility to generate the lattice in a discontinuous order.
static constexpr bool can_make_periodic_box(std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
check if the given lattice coordinates bounds can make a periodic box
static constexpr CoordRange< Tvec > get_periodic_box(Tscal dr, std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
Get the periodic box corresponding to integer lattice coordinates this function will throw if the coo...
static constexpr Tvec get_box_dim(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt)
Get the box size for a FCC lattice of xcnt * ycnt * zcnt lattice cells this function will throw if th...
iterator_utils::LatticeIterator< Tvec, LatticeFCC > Iterator
Iterator utility to generate the lattice.
static std::tuple< Tvec, Tvec > get_ideal_fcc_box(Tscal dr, std::tuple< Tvec, Tvec > box)
Get the smallest periodic FCC box containing the given box.
static constexpr Tvec generator(Tscal dr, std::array< i32, dim > coord) noexcept
generate a FCC lattice centered on (0,0,0)
static constexpr i32 pmod(i32 a, i32 b) noexcept
Positive modulo (C++ % keeps the sign of the dividend).
utility for generating HCP crystal lattices
static constexpr bool can_make_periodic_box(std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
check if the given lattice coordinates bounds can make a periodic box
static constexpr Tvec generator(Tscal dr, std::array< i32, dim > coord) noexcept
generate a HCP lattice centered on (0,0,0)
iterator_utils::LatticeIteratorDiscontinuous< Tvec, LatticeHCP > IteratorDiscontinuous
Iterator utility to generate the lattice in a discontinuous order.
iterator_utils::LatticeIterator< Tvec, LatticeHCP > Iterator
Iterator utility to generate the lattice.
static constexpr std::pair< std::array< i32, dim >, std::array< i32, dim > > nearest_periodic_box_indices(std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
get the nearest integer triplets bound that gives a periodic box
static constexpr CoordRange< Tvec > get_periodic_box(Tscal dr, std::array< i32, dim > coord_min, std::array< i32, dim > coord_max)
Get the periodic box corresponding to integer lattice coordinates this function will throw if the coo...
Iterator going through a lattice coordinates range in a discontinuous order (bit-reversed,...
Iterator going through a lattice coordinates range in order.
namespace for math utility