Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
GeneratorLattice.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 "shammath/AABB.hpp"
26#include <type_traits>
27
29
36 template<class Lattice>
38
40 template<class Tvec>
41 struct LatticeProperties<shammath::LatticeHCP<Tvec>> {
42 static constexpr const char *name = "GeneratorLatticeHCP";
43 static constexpr f64 default_init_h_factor = 1.7817974362806785; // 2^(5/6)
44 };
45
47 template<class Tvec>
48 struct LatticeProperties<shammath::LatticeFCC<Tvec>> {
49 static constexpr const char *name = "GeneratorLatticeFCC";
50 static constexpr f64 default_init_h_factor = 1.7817974362806785; // 2^(5/6)
51 };
52
54 template<class Tvec>
55 struct LatticeProperties<shammath::LatticeCubic<Tvec>> {
56 static constexpr const char *name = "GeneratorLatticeCubic";
57 static constexpr f64 default_init_h_factor = 1.0;
58 };
59
67 template<class Tvec, class Lattice, bool discontinuous = true>
68 class GeneratorLattice : public ISPHSetupNode {
69 using Tscal = shambase::VecComponent<Tvec>;
70 static constexpr u32 dim = shambase::VectorProperties<Tvec>::dimension;
71 using LatticeIter = std::conditional_t<
72 discontinuous,
73 typename Lattice::IteratorDiscontinuous,
74 typename Lattice::Iterator>;
75
76 ShamrockCtx &context;
77 Tscal dr;
79 Tscal init_h_factor;
80
81 LatticeIter generator;
82
83 public:
85 static constexpr Tscal default_init_h_factor
87
88 private:
89 static auto init_gen(Tscal dr, std::pair<Tvec, Tvec> box) {
90
91 auto [idxs_min, idxs_max] = Lattice::get_box_index_bounds(dr, box.first, box.second);
92 u32 idx_gen = 0;
93 return LatticeIter(dr, idxs_min, idxs_max);
94 };
95
96 public:
98 ShamrockCtx &context,
99 Tscal dr,
100 std::pair<Tvec, Tvec> box,
101 Tscal init_h_factor = default_init_h_factor)
102 : context(context), dr(dr), box(box), init_h_factor(init_h_factor),
103 generator(init_gen(dr, box)) {}
104
105 bool is_done() { return generator.is_done(); }
106
108 StackEntry stack_loc{};
109
110 using namespace shamrock::patch;
111 PatchScheduler &sched = shambase::get_check_ref(context.sched);
112
113 std::vector<Tvec> pos_data;
114
115 // Fill pos_data if the scheduler has some patchdata in this rank
116 if (!is_done()) {
117 u64 loc_gen_count = nmax;
118
119 auto gen_info = shamalgs::collective::fetch_view(loc_gen_count);
120
121 u64 skip_start = gen_info.head_offset;
122 u64 gen_cnt = loc_gen_count;
123 u64 skip_end = gen_info.total_byte_count - loc_gen_count - gen_info.head_offset;
124
125 shamlog_debug_ln(
126 get_name(),
127 "generate : ",
128 skip_start,
129 gen_cnt,
130 skip_end,
131 "total",
132 skip_start + gen_cnt + skip_end);
133
134 generator.skip(skip_start);
135 auto tmp = generator.next_n(gen_cnt);
136 generator.skip(skip_end);
137
138 for (Tvec r : tmp) {
139 if (Patch::is_in_patch_converted(r, box.lower, box.upper)) {
140 pos_data.push_back(r);
141 }
142 }
143 }
144
145 // Make a patchdata from pos_data
146 PatchDataLayer tmp(sched.get_layout_ptr_old());
147 if (!pos_data.empty()) {
148 tmp.resize(pos_data.size());
149 tmp.fields_raz();
150
151 {
152 u32 len = pos_data.size();
154 = tmp.get_field<Tvec>(sched.pdl_old().get_field_idx<Tvec>("xyz"));
155 // sycl::buffer<Tvec> buf(pos_data.data(), len);
156 f.override(pos_data, len);
157 }
158
159 {
161 = tmp.get_field<Tscal>(sched.pdl_old().get_field_idx<Tscal>("hpart"));
162 f.override(init_h_factor * dr);
163 }
164 }
165 return tmp;
166 }
167
170 };
171
172} // namespace shammodels::sph::modules
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
Setup node generating particles on a lattice.
std::string get_name()
Get the name of the node.
bool is_done()
This function return true if the setup is done.
shamrock::patch::PatchDataLayer next_n(u32 nmax)
This function generate patchdata with at most nmax per MPI ranks This function is always assumed as c...
ISPHSetupNode_Dot get_dot_subgraph()
Get a dot subgraph describing the node and its childrens (recursively).
This class is an interface that all SPH setup nodes must implement. It describe an operation associat...
PatchDataLayer container class, the layout is described in patchdata_layout.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
namespace for math utility
Definition AABB.hpp:26
namespace for the sph model modules
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Axis-Aligned bounding box.
Definition AABB.hpp:99
This struct is used to generate a dot graph of the setup tree.
Per-lattice properties of the generator (specialized per lattice).
static bool is_in_patch_converted(sycl::vec< T, 3 > val, sycl::vec< T, 3 > min_val, sycl::vec< T, 3 > max_val)
check if particle is in the asked range, given the output of @convert_coord
Definition Patch.hpp:210