Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
VTKDump.cpp
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
19#include "shamalgs/memory.hpp"
24
25namespace {
26
27 template<class Tvec>
28 shamrock::LegacyVtkWriter start_dump(PatchScheduler &sched, std::string dump_name) {
29 StackEntry stack_loc{};
30 shamrock::LegacyVtkWriter writer(dump_name, true, shamrock::UnstructuredGrid);
31
32 using namespace shamrock::patch;
33
34 u64 num_obj = sched.get_rank_count();
35
36 shamlog_debug_mpi_ln("gsph::vtk", "rank count =", num_obj);
37
38 const u32 ixyz = sched.pdl_old().get_field_idx<Tvec>(shammodels::gsph::names::common::xyz);
39 std::unique_ptr<sycl::buffer<Tvec>> pos = sched.rankgather_field<Tvec>(ixyz);
40
41 writer.write_points(pos, num_obj);
42
43 return writer;
44 }
45
47 StackEntry stack_loc{};
48
49 u64 num_obj = sched.get_rank_count();
50
51 using namespace shamrock::patch;
52
53 if (num_obj > 0) {
54 sycl::buffer<u64> idp(num_obj);
55
56 u64 ptr = 0;
57 sched.for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
58 using namespace shamalgs::memory;
59 using namespace shambase;
60
61 write_with_offset_into(
62 shamsys::instance::get_compute_queue(),
63 idp,
64 cur_p.id_patch,
65 ptr,
66 pdat.get_obj_cnt());
67
68 ptr += pdat.get_obj_cnt();
69 });
70
71 writer.write_field("patchid", idp, num_obj);
72 } else {
73 writer.write_field_no_buf<u64>("patchid");
74 }
75 }
76
78 StackEntry stack_loc{};
79
80 using namespace shamrock::patch;
81 u64 num_obj = sched.get_rank_count();
82
83 if (num_obj > 0) {
84 sycl::buffer<u32> idp(num_obj);
85
86 u64 ptr = 0;
87 sched.for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
88 using namespace shamalgs::memory;
89 using namespace shambase;
90
91 write_with_offset_into<u32>(
92 shamsys::instance::get_compute_queue(),
93 idp,
95 ptr,
96 pdat.get_obj_cnt());
97
98 ptr += pdat.get_obj_cnt();
99 });
100
101 writer.write_field("world_rank", idp, num_obj);
102 } else {
103 writer.write_field_no_buf<u32>("world_rank");
104 }
105 }
106
107 template<class T>
109 PatchScheduler &sched,
111 u32 field_idx,
112 std::string field_dump_name) {
113 StackEntry stack_loc{};
114
115 using namespace shamrock::patch;
116 u64 num_obj = sched.get_rank_count();
117
118 if (num_obj > 0) {
119 std::unique_ptr<sycl::buffer<T>> field_vals = sched.rankgather_field<T>(field_idx);
120
121 writer.write_field(field_dump_name, field_vals, num_obj);
122 } else {
123 writer.write_field_no_buf<T>(field_dump_name);
124 }
125 }
126
127} // anonymous namespace
128
129namespace shammodels::gsph::modules {
130
131 template<class Tvec, template<class> class SPHKernel>
132 void VTKDump<Tvec, SPHKernel>::do_dump(std::string filename, bool add_patch_world_id) {
133
134 StackEntry stack_loc{};
135
136 using namespace shamrock;
137 using namespace shamrock::patch;
138
139 using namespace gsph::names;
140
141 PatchDataLayerLayout &pdl = scheduler().pdl_old();
142 const u32 ivxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::vxyz);
143 const u32 iaxyz = pdl.get_field_idx<Tvec>(gsph::names::newtonian::axyz);
144 const u32 ihpart = pdl.get_field_idx<Tscal>(gsph::names::common::hpart);
145
146 // Thermodynamic fields from patchdata (computed during timestep, persistent across
147 // restarts)
148 const u32 idensity = pdl.get_field_idx<Tscal>(gsph::names::newtonian::density);
149 const u32 ipressure = pdl.get_field_idx<Tscal>(gsph::names::newtonian::pressure);
150 const u32 isoundspeed = pdl.get_field_idx<Tscal>(gsph::names::newtonian::soundspeed);
151
152 // Check for optional internal energy field
153 const bool has_uint = solver_config.has_field_uint();
154 const u32 iuint = has_uint ? pdl.get_field_idx<Tscal>(gsph::names::newtonian::uint) : 0;
155
156 shamrock::LegacyVtkWriter writer = start_dump<Tvec>(scheduler(), filename);
157 writer.add_point_data_section();
158
159 // Count fields to write
160 u32 fnum = 0;
161 if (add_patch_world_id) {
162 fnum += 2; // patchid and world_rank
163 }
164 fnum++; // h
165 fnum++; // v
166 fnum++; // a
167 fnum++; // rho
168 fnum++; // P
169 fnum++; // cs
170
171 if (has_uint) {
172 fnum++; // u
173 }
174
175 writer.add_field_data_section(fnum);
176
177 if (add_patch_world_id) {
178 vtk_dump_add_patch_id(scheduler(), writer);
179 vtk_dump_add_worldrank(scheduler(), writer);
180 }
181
182 vtk_dump_add_field<Tscal>(scheduler(), writer, ihpart, "h");
183 vtk_dump_add_field<Tvec>(scheduler(), writer, ivxyz, "v");
184 vtk_dump_add_field<Tvec>(scheduler(), writer, iaxyz, "a");
185
186 if (has_uint) {
187 vtk_dump_add_field<Tscal>(scheduler(), writer, iuint, "u");
188 }
189
190 // Read precomputed thermodynamic fields from patchdata
191 vtk_dump_add_field<Tscal>(scheduler(), writer, idensity, "rho");
192 vtk_dump_add_field<Tscal>(scheduler(), writer, ipressure, "P");
193 vtk_dump_add_field<Tscal>(scheduler(), writer, isoundspeed, "cs");
194 }
195
196} // namespace shammodels::gsph::modules
197
198// Explicit template instantiations
199using namespace shammath;
200
204
Constants for field names in GSPH solver, organized by physics mode.
constexpr const char * xyz
Position field (3D coordinates)
Header file describing a Node Instance.
void vtk_dump_add_field(PatchScheduler &sched, shamrock::LegacyVtkWriter &writer, u32 field_idx, const std::string &field_dump_name)
Add a data field to VTK dump.
shamrock::LegacyVtkWriter start_dump(PatchScheduler &sched, const std::string &dump_name)
Start a VTK dump by writing particle positions.
void vtk_dump_add_patch_id(PatchScheduler &sched, shamrock::LegacyVtkWriter &writer)
Add patch ID field to VTK dump.
void vtk_dump_add_worldrank(PatchScheduler &sched, shamrock::LegacyVtkWriter &writer)
Add world rank field to VTK dump.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
PatchDataLayer container class, the layout is described in patchdata_layout.
VTK dump module for GSPH solver.
memory manipulation algorithms
namespace for basic c++ utilities
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
namespace for math utility
Definition AABB.hpp:26
namespace for the main framework
Definition __init__.py:1
main include file for memory algorithms
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
Functions related to the MPI communicator.