Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
crystalLattice.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19
22#include "shambackends/vec.hpp"
23#include "shamcomm/logs.hpp"
26#include <array>
27#include <functional>
28#include <utility>
29#include <vector>
30
31namespace shammath {
32
33 class LatticeError : public std::exception {
34 public:
35 explicit LatticeError(const char *message) : msg_(message) {}
36
37 explicit LatticeError(const std::string &message) : msg_(message) {}
38
39 ~LatticeError() noexcept override = default;
40
41 [[nodiscard]] const char *what() const noexcept override { return msg_.c_str(); }
42
43 protected:
44 std::string msg_;
45 };
46
47 namespace iterator_utils {
48
55 template<class Tvec, class Lattice>
56 class LatticeIterator {
57 using Tscal = shambase::VecComponent<Tvec>;
58 static constexpr u32 dim = 3;
59
60 Tscal dr;
61 std::array<i32, dim> coord_min;
62
63 std::array<size_t, dim> coord_delta;
64 size_t current_idx;
65 size_t max_coord;
66
67 bool done = false;
68
69 public:
70 LatticeIterator(
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),
73 coord_delta({
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]),
77 }) {
78
79 // must check for all axis otherwise we loop forever
80 for (int ax = 0; ax < dim; ax++) {
81 if (coord_min[ax] == coord_max[ax]) {
82 done = true;
83 }
84 }
85
86 max_coord = coord_delta[0] * coord_delta[1] * coord_delta[2];
87 }
88
89 inline bool is_done() { return done; }
90
91 inline Tvec next() {
92
93 // std::array<i32, 3> current = {
94 // coord_min[0] + i32(current_idx / (coord_delta[1] * coord_delta[2])),
95 // coord_min[1] + i32((current_idx / coord_delta[2]) % coord_delta[1]),
96 // coord_min[2] + i32(current_idx % coord_delta[2]),
97 // };
98
99 // current index of the particle
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]))),
104 };
105
106 // find the physical position of this particle
107 Tvec ret = Lattice::generator(dr, current);
108
109 if (!done) {
110 current_idx++;
111 }
112 if (current_idx >= max_coord) {
113 done = true;
114 }
115
116 return ret;
117 }
118
119 inline std::vector<Tvec> next_n(u64 nmax) {
120 std::vector<Tvec> ret{};
121 for (u64 i = 0; i < nmax; i++) {
122 if (done) {
123 break;
124 }
125 ret.push_back(next());
126 }
127 shamlog_debug_ln("Lattice iterator", "next_n final idx", current_idx);
128 return ret;
129 }
130
131 inline void skip(u64 n) {
132 if (!done) {
133 current_idx += n;
134 }
135 if (current_idx >= max_coord) {
136 done = true;
137 }
138 shamlog_debug_ln("Lattice iterator", "skip final idx", current_idx);
139 }
140 };
141
149 template<class Tvec, class Lattice>
150 class LatticeIteratorDiscontinuous {
151 using Tscal = shambase::VecComponent<Tvec>;
152 static constexpr u32 dim = 3;
153
154 Tscal dr;
155
156 std::array<std::vector<size_t>, dim> remapped_indices;
157
158 std::array<size_t, dim> coord_delta;
159 size_t max_coord;
160
161 bool done = false;
162
163 public:
164 size_t current_idx;
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]),
171 }) {
172
173 // must check for all axis otherwise we loop forever
174 for (int ax = 0; ax < dim; ax++) {
175 if (coord_min[ax] == coord_max[ax]) {
176 done = true;
177 }
178 }
179
180 max_coord = coord_delta[0] * coord_delta[1] * coord_delta[2];
181
182 for (int ax = 0; ax < dim; ax++) {
183 DiscontinuousIterator<i32> it(coord_min[ax], coord_max[ax]);
184 while (!it.is_done()) {
185 remapped_indices[ax].push_back(it.next());
186 }
187 }
188 }
189
190 inline bool is_done() { return done; }
191
192 inline Tvec next() {
193
194 // std::array<i32, 3> current = {
195 // coord_min[0] + i32(current_idx / (coord_delta[1] * coord_delta[2])),
196 // coord_min[1] + i32((current_idx / coord_delta[2]) % coord_delta[1]),
197 // coord_min[2] + i32(current_idx % coord_delta[2]),
198 // };
199
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]))),
204 };
205
206 current[0] = remapped_indices[0][current[0]];
207 current[1] = remapped_indices[1][current[1]];
208 current[2] = remapped_indices[2][current[2]];
209
210 Tvec ret = Lattice::generator(dr, current);
211
212 if (!done) {
213 current_idx++;
214 }
215 if (current_idx >= max_coord) {
216 done = true;
217 }
218
219 return ret;
220 }
221
222 inline std::vector<Tvec> next_n(u64 nmax) {
223 std::vector<Tvec> ret{};
224 for (u64 i = 0; i < nmax; i++) {
225 if (done) {
226 break;
227 }
228 ret.push_back(next());
229 }
230 shamlog_debug_ln("Discontinuous iterator", "next_n final idx", current_idx);
231 return ret;
232 }
233
234 inline void skip(u64 n) {
235 if (!done) {
236 current_idx += n;
237 }
238 if (current_idx >= max_coord) {
239 done = true;
240 }
241 shamlog_debug_ln("Discontinuous iterator", "skip final idx", current_idx);
242 }
243 };
244
245 } // namespace iterator_utils
246
252 template<class Tvec>
254 public:
255 static constexpr u32 dim = 3;
256
257 static_assert(
258 dim == shambase::VectorProperties<Tvec>::dimension, "this lattice exist only in dim 3");
259
260 using Tscal = shambase::VecComponent<Tvec>;
261
269 static inline constexpr Tvec generator(Tscal dr, std::array<i32, dim> coord) noexcept {
270
271 i32 i = coord[0];
272 i32 j = coord[1];
273 i32 k = coord[2];
274
275 Tvec r_a
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};
279
280 return dr * r_a;
281 }
282
291 constexpr static bool can_make_periodic_box(
292 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
293 if (coord_max[0] - coord_min[0] < 2) {
294 return false;
295 }
296
297 if ((coord_max[1] + coord_min[1]) % 2 != 0) {
298 return false;
299 }
300
301 if ((coord_max[2] + coord_min[2]) % 2 != 0) {
302 return false;
303 }
304
305 return true;
306 }
307
317 static inline constexpr CoordRange<Tvec> get_periodic_box(
318 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
319 Tscal xmin, xmax, ymin, ymax, zmin, zmax;
320
321 xmin = 2 * coord_min[0];
322 xmax = 2 * coord_max[0];
323
324 ymin = sycl::sqrt(3.) * coord_min[1];
325 ymax = sycl::sqrt(3.) * coord_max[1];
326
327 zmin = 2 * sycl::sqrt(6.) * coord_min[2] / 3;
328 zmax = 2 * sycl::sqrt(6.) * coord_max[2] / 3;
329
330 if (!can_make_periodic_box(coord_min, coord_max)) {
331 throw LatticeError(
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");
335 }
336
337 return {Tvec{xmin, ymin, zmin} * dr, Tvec{xmax, ymax, zmax} * dr};
338 }
339
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) {
342
343 Tvec coord_min;
344 Tvec coord_max;
345
346 coord_min[0] = box_min[0] / 2.;
347 coord_max[0] = box_max[0] / 2.;
348
349 coord_min[1] = box_min[1] / sycl::sqrt(3.);
350 coord_max[1] = box_max[1] / sycl::sqrt(3.);
351
352 coord_min[2] = box_min[2] / (2 * sycl::sqrt(6.) / 3);
353 coord_max[2] = box_max[2] / (2 * sycl::sqrt(6.) / 3);
354
355 coord_min /= dr;
356 coord_max /= dr;
357
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};
362
363 return {ret_coord_min, ret_coord_max};
364 }
365
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;
378
379 ret_coord_min[0] = coord_min[0];
380 ret_coord_min[1] = coord_min[1];
381 ret_coord_min[2] = coord_min[2];
382
383 ret_coord_max[0] = coord_max[0];
384 ret_coord_max[1] = coord_max[1];
385 ret_coord_max[2] = coord_max[2];
386
387 if (coord_max[0] - coord_min[0] < 2) {
388 ret_coord_max[0]++;
389 }
390
391 if ((coord_max[1] + coord_min[1]) % 2 != 0) {
392 ret_coord_max[1]++;
393 }
394
395 if ((coord_max[2] + coord_min[2]) % 2 != 0) {
396 ret_coord_max[2]++;
397 }
398
399 return {ret_coord_min, ret_coord_max};
400 }
401
404
408
409 inline static std::tuple<Tvec, Tvec> get_ideal_hcp_box(
410 Tscal dr, std::tuple<Tvec, Tvec> box) {
411
412 auto [bmin, bmax] = box;
413
414 auto [idxs_min, idxs_max] = LatticeHCP::get_box_index_bounds(dr, bmin, bmax);
415
416 auto [idxs_min_per, idxs_max_per]
417 = LatticeHCP::nearest_periodic_box_indices(idxs_min, idxs_max);
418
420 = LatticeHCP::get_periodic_box(dr, idxs_min_per, idxs_max_per);
421
422 return {ret.lower, ret.upper};
423 }
424 };
425
440 template<class Tvec>
442 public:
443 static constexpr u32 dim = 3;
444
445 static_assert(
446 dim == shambase::VectorProperties<Tvec>::dimension, "this lattice exist only in dim 3");
447
448 using Tscal = shambase::VecComponent<Tvec>;
449
451 static constexpr i32 z_period = 3;
452
454 static inline constexpr i32 pmod(i32 a, i32 b) noexcept { return ((a % b) + b) % b; }
455
466 static inline constexpr Tvec generator(Tscal dr, std::array<i32, dim> coord) noexcept {
467
468 i32 i = coord[0];
469 i32 j = coord[1];
470 i32 k = coord[2];
471
472 i32 s = pmod(k, z_period); // layer type : 0 -> A, 1 -> B, 2 -> C
473
474 Tvec r_a = {
475 2 * i + pmod(j + s, 2), sycl::sqrt(3.) * (j + s / 3.), 2 * sycl::sqrt(6.) * k / 3};
476
477 return dr * r_a;
478 }
479
488 constexpr static bool can_make_periodic_box(
489 std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
490 if (coord_max[0] - coord_min[0] < 2) {
491 return false;
492 }
493
494 // check for empty/inverted span
495 if (coord_max[1] <= coord_min[1] || coord_max[2] <= coord_min[2]) {
496 return false;
497 }
498
499 if (pmod(coord_max[1] - coord_min[1], 2) != 0) {
500 return false;
501 }
502
503 if (pmod(coord_max[2] - coord_min[2], z_period) != 0) {
504 return false;
505 }
506
507 return true;
508 }
509
519 static inline constexpr CoordRange<Tvec> get_periodic_box(
520 Tscal dr, std::array<i32, dim> coord_min, std::array<i32, dim> coord_max) {
521 Tscal xmin, xmax, ymin, ymax, zmin, zmax;
522
523 xmin = 2 * coord_min[0];
524 xmax = 2 * coord_max[0];
525
526 ymin = sycl::sqrt(3.) * coord_min[1];
527 ymax = sycl::sqrt(3.) * coord_max[1];
528
529 zmin = 2 * sycl::sqrt(6.) * coord_min[2] / 3;
530 zmax = 2 * sycl::sqrt(6.) * coord_max[2] / 3;
531
532 if (!can_make_periodic_box(coord_min, coord_max)) {
533 throw LatticeError(
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");
537 }
538
539 return {Tvec{xmin, ymin, zmin} * dr, Tvec{xmax, ymax, zmax} * dr};
540 }
541
546 static inline constexpr std::pair<std::array<i32, dim>, std::array<i32, dim>>
547 get_box_index_bounds(Tscal dr, Tvec box_min, Tvec box_max) {
548
549 Tvec coord_min;
550 Tvec coord_max;
551
552 coord_min[0] = box_min[0] / 2.;
553 coord_max[0] = box_max[0] / 2.;
554
555 coord_min[1] = box_min[1] / sycl::sqrt(3.);
556 coord_max[1] = box_max[1] / sycl::sqrt(3.);
557
558 coord_min[2] = box_min[2] / (2 * sycl::sqrt(6.) / 3);
559 coord_max[2] = box_max[2] / (2 * sycl::sqrt(6.) / 3);
560
561 coord_min /= dr;
562 coord_max /= dr;
563
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};
568
569 return {ret_coord_min, ret_coord_max};
570 }
571
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;
585
586 if (ret_coord_max[0] - ret_coord_min[0] < 2) {
587 ret_coord_max[0] = ret_coord_min[0] + 2;
588 }
589
590 if (pmod(ret_coord_max[1] - ret_coord_min[1], 2) != 0) {
591 ret_coord_max[1]++;
592 }
593
594 ret_coord_max[2]
595 += pmod(z_period - pmod(ret_coord_max[2] - ret_coord_min[2], z_period), z_period);
596
597 return {ret_coord_min, ret_coord_max};
598 }
599
602
606
616 */
617 static inline constexpr Tvec get_box_dim(Tscal dr, u32 xcnt, u32 ycnt, u32 zcnt) {
619 = get_periodic_box(dr, {0, 0, 0}, {i32(xcnt), i32(ycnt), i32(zcnt)});
620 return box.upper - box.lower;
621 }
622
625 */
626 inline static std::tuple<Tvec, Tvec> get_ideal_fcc_box(
627 Tscal dr, std::tuple<Tvec, Tvec> box) {
628
629 auto [bmin, bmax] = box;
630
631 auto [idxs_min, idxs_max] = LatticeFCC::get_box_index_bounds(dr, bmin, bmax);
632
633 auto [idxs_min_per, idxs_max_per]
634 = LatticeFCC::nearest_periodic_box_indices(idxs_min, idxs_max);
635
637 = LatticeFCC::get_periodic_box(dr, idxs_min_per, idxs_max_per);
638
639 return {ret.lower, ret.upper};
640 }
641 };
642
648 template<class Tvec>
650 public:
651 static constexpr u32 dim = 3;
652
653 static_assert(
654 dim == shambase::VectorProperties<Tvec>::dimension, "this lattice exist only in dim 3");
655
656 using Tscal = shambase::VecComponent<Tvec>;
657
665 static inline constexpr Tvec generator(Tscal dr, std::array<i32, dim> coord) noexcept {
666
667 i32 i = coord[0];
668 i32 j = coord[1];
669 i32 k = coord[2];
670
671 Tvec r_a = {i, j, k};
672
673 return dr * r_a;
674 }
675
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) {
678
679 Tvec coord_min;
680 Tvec coord_max;
681
682 coord_min[0] = box_min[0];
683 coord_max[0] = box_max[0];
684
685 coord_min[1] = box_min[1];
686 coord_max[1] = box_max[1];
687
688 coord_min[2] = box_min[2];
689 coord_max[2] = box_max[2];
690
691 coord_min /= dr;
692 coord_max /= dr;
693
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};
698
699 return {ret_coord_min, ret_coord_max};
700 }
701
704
708 };
709
710} // namespace shammath
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
Definition AABB.hpp:26