Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PhantomDump.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
18
21#include "shambase/sets.hpp"
23#include "shambase/string.hpp"
25#include "shamcomm/logs.hpp"
30#include <unordered_map>
31#include <string>
32#include <vector>
33
34template<class T>
43
44template<class T>
46 shambase::FortranIOFile &phfile, i64 tot_count) {
47 StackEntry stack_loc{};
48 phfile.write_fixed_string(tag, 16);
49 phfile.write_val_array(vals, tot_count);
50}
51
52template<class T>
54 logger::raw_ln("tag =", tag, "size =", vals.size());
55}
56
57template<class T>
60 StackEntry stack_loc{};
61
63
64 int nvars;
65
66 phfile.read(nvars);
67
68 if (nvars == 0) {
69 return tmp;
70 }
71
72 std::vector<std::string> tags;
73 phfile.read_string_array(tags, 16, nvars);
74
75 std::vector<T> vals;
76 phfile.read_val_array(vals, nvars);
77
78 for (u32 i = 0; i < nvars; i++) {
79 tmp.entries.push_back({tags[i], vals[i]});
80 }
81
82 return tmp;
83}
84
85template<class T>
87 StackEntry stack_loc{};
88
89 int nvars = entries.size();
90 phfile.write(nvars);
91
92 if (nvars == 0) {
93 return;
94 }
95
96 std::vector<std::string> tags;
97 std::vector<T> vals;
98 for (u32 i = 0; i < nvars; i++) {
99 auto [a, b] = entries[i];
100 tags.push_back(a);
101 vals.push_back(b);
102 }
103
104 phfile.write_string_array(tags, 16, nvars);
105 phfile.write_val_array(vals, nvars);
106}
107
108template<class T>
110
111 for (auto [key, val] : entries) {
112 logger::raw_ln(key, val);
113 }
114}
115
117
118 logger::raw_ln("--blocks_fort_int --");
119 for (auto b : blocks_fort_int) {
120 b.print_state();
121 }
122 logger::raw_ln("--blocks_i8 --");
123 for (auto b : blocks_i8) {
124 b.print_state();
125 }
126 logger::raw_ln("--blocks_i16 --");
127 for (auto b : blocks_i16) {
128 b.print_state();
129 }
130 logger::raw_ln("--blocks_i32 --");
131 for (auto b : blocks_i32) {
132 b.print_state();
133 }
134 logger::raw_ln("--blocks_i64 --");
135 for (auto b : blocks_i64) {
136 b.print_state();
137 }
138 logger::raw_ln("--blocks_fort_real--");
139 for (auto b : blocks_fort_real) {
140 b.print_state();
141 }
142 logger::raw_ln("--blocks_f32 --");
143 for (auto b : blocks_f32) {
144 b.print_state();
145 }
146 logger::raw_ln("--blocks_f64 --");
147 for (auto b : blocks_f64) {
148 b.print_state();
149 }
150}
151
153 shambase::FortranIOFile &phfile, i64 tot_count, std::array<i32, 8> numarray) {
154 PhantomDumpBlock block;
155
156 block.tot_count = tot_count;
157
158 for (u32 j = 0; j < numarray[0]; j++) {
159 block.blocks_fort_int.push_back(
161 }
162 for (u32 j = 0; j < numarray[1]; j++) {
163 block.blocks_i8.push_back(PhantomDumpBlockArray<i8>::from_file(phfile, block.tot_count));
164 }
165 for (u32 j = 0; j < numarray[2]; j++) {
166 block.blocks_i16.push_back(PhantomDumpBlockArray<i16>::from_file(phfile, block.tot_count));
167 }
168 for (u32 j = 0; j < numarray[3]; j++) {
169 block.blocks_i32.push_back(PhantomDumpBlockArray<i32>::from_file(phfile, block.tot_count));
170 }
171 for (u32 j = 0; j < numarray[4]; j++) {
172 block.blocks_i64.push_back(PhantomDumpBlockArray<i64>::from_file(phfile, block.tot_count));
173 }
174 for (u32 j = 0; j < numarray[5]; j++) {
175 block.blocks_fort_real.push_back(
177 }
178 for (u32 j = 0; j < numarray[6]; j++) {
179 block.blocks_f32.push_back(PhantomDumpBlockArray<f32>::from_file(phfile, block.tot_count));
180 }
181 for (u32 j = 0; j < numarray[7]; j++) {
182 block.blocks_f64.push_back(PhantomDumpBlockArray<f64>::from_file(phfile, block.tot_count));
183 }
184
185 return block;
186}
187
189 shambase::FortranIOFile &phfile, i64 tot_count, std::array<i32, 8> numarray) {
190 StackEntry stack_loc{};
191
192 for (u32 j = 0; j < numarray[0]; j++) {
193 blocks_fort_int[j].write(phfile, tot_count);
194 }
195 for (u32 j = 0; j < numarray[1]; j++) {
196 blocks_i8[j].write(phfile, tot_count);
197 }
198 for (u32 j = 0; j < numarray[2]; j++) {
199 blocks_i16[j].write(phfile, tot_count);
200 }
201 for (u32 j = 0; j < numarray[3]; j++) {
202 blocks_i32[j].write(phfile, tot_count);
203 }
204 for (u32 j = 0; j < numarray[4]; j++) {
205 blocks_i64[j].write(phfile, tot_count);
206 }
207 for (u32 j = 0; j < numarray[5]; j++) {
208 blocks_fort_real[j].write(phfile, tot_count);
209 }
210 for (u32 j = 0; j < numarray[6]; j++) {
211 blocks_f32[j].write(phfile, tot_count);
212 }
213 for (u32 j = 0; j < numarray[7]; j++) {
214 blocks_f64[j].write(phfile, tot_count);
215 }
216}
217
219
220 s = sham::format("{:16s}", s);
221 auto &blocks = blocks_fort_real;
222
223 for (u32 i = 0; i < blocks_fort_real.size(); i++) {
224 if (blocks_fort_real[i].tag == s) {
225 return i;
226 }
227 }
228
230 tmp.tag = s;
231 blocks_fort_real.push_back(std::move(tmp));
232
233 for (u32 i = 0; i < blocks_fort_real.size(); i++) {
234 if (blocks_fort_real[i].tag == s) {
235 return i;
236 }
237 }
238
239 return 0;
240}
241
243
244 s = sham::format("{:16s}", s);
245
246 auto &blocks = blocks_f32;
247
248 for (u32 i = 0; i < blocks_f32.size(); i++) {
249 if (blocks_f32[i].tag == s) {
250 return i;
251 }
252 }
253
255 tmp.tag = s;
256 blocks_f32.push_back(std::move(tmp));
257
258 for (u32 i = 0; i < blocks_f32.size(); i++) {
259 if (blocks_f32[i].tag == s) {
260 return i;
261 }
262 }
263
264 return 0;
265}
266
268 StackEntry stack_loc{};
269
271 phfile.write(i1, r1, i2, iversion, i3);
272
273 phfile.write_fixed_string(fileid, 100);
274
275 table_header_fort_int.write(phfile);
276 table_header_i8.write(phfile);
277 table_header_i16.write(phfile);
278 table_header_i32.write(phfile);
279 table_header_i64.write(phfile);
280 table_header_fort_real.write(phfile);
281 table_header_f32.write(phfile);
282 table_header_f64.write(phfile);
283
284 int nblocks = blocks.size();
285 phfile.write(nblocks);
286
287 std::vector<i64> block_tot_counts;
288 std::vector<std::array<i32, 8>> block_numarray;
289 for (u32 i = 0; i < nblocks; i++) {
290
291 i64 tot_count = blocks[i].tot_count;
292 std::array<i32, 8> counts
293 = {i32(blocks[i].blocks_fort_int.size()),
294 i32(blocks[i].blocks_i8.size()),
295 i32(blocks[i].blocks_i16.size()),
296 i32(blocks[i].blocks_i32.size()),
297 i32(blocks[i].blocks_i64.size()),
298 i32(blocks[i].blocks_fort_real.size()),
299 i32(blocks[i].blocks_f32.size()),
300 i32(blocks[i].blocks_f64.size())};
301
302 phfile.write(tot_count, counts);
303 block_tot_counts.push_back(tot_count);
304 block_numarray.push_back(counts);
305 }
306
307 for (u32 i = 0; i < nblocks; i++) {
308 blocks[i].write(phfile, block_tot_counts[i], block_numarray[i]);
309 }
310
311 return phfile;
312}
313
315 shambase::FortranIOFile &phfile) {
316 PhantomDump phdump;
317
318 // first line
319 //<4 bytes>i1,r1,i2,iversion,i3<4 bytes>
320 phfile.read(phdump.i1, phdump.r1, phdump.i2, phdump.iversion, phdump.i3);
321 phdump.check_magic_numbers();
322
323 // The second line contains a 100-character file identifier:
324 // <4 bytes>fileid<4 bytes>
325 phfile.read_fixed_string(phdump.fileid, 100);
326
327 // loop i=1,8
328 // <4 bytes>nvars<4 bytes>
329 // <4 bytes>tags(1:nvars)<4 bytes>
330 // <4 bytes>vals(1:nvals)<4 bytes>
331 // end loop
332 phdump.table_header_fort_int = PhantomDumpTableHeader<fort_int>::from_file(phfile);
333 phdump.table_header_i8 = PhantomDumpTableHeader<i8>::from_file(phfile);
334 phdump.table_header_i16 = PhantomDumpTableHeader<i16>::from_file(phfile);
335 phdump.table_header_i32 = PhantomDumpTableHeader<i32>::from_file(phfile);
336 phdump.table_header_i64 = PhantomDumpTableHeader<i64>::from_file(phfile);
337 phdump.table_header_fort_real = PhantomDumpTableHeader<fort_real>::from_file(phfile);
338 phdump.table_header_f32 = PhantomDumpTableHeader<f32>::from_file(phfile);
339 phdump.table_header_f64 = PhantomDumpTableHeader<f64>::from_file(phfile);
340
341 int nblocks;
342 phfile.read(nblocks);
343
344 std::vector<i64> block_tot_counts;
345 std::vector<std::array<i32, 8>> block_numarray;
346
347 for (u32 i = 0; i < nblocks; i++) {
348
349 i64 tot_count;
350 std::array<i32, 8> counts;
351
352 phfile.read(tot_count, counts);
353
354 block_tot_counts.push_back(tot_count);
355 block_numarray.push_back(counts);
356 }
357 for (u32 i = 0; i < nblocks; i++) {
358 phdump.blocks.push_back(
359 PhantomDumpBlock::from_file(phfile, block_tot_counts[i], block_numarray[i]));
360 }
361
362 if (!phfile.finished_read()) {
363 logger::warn_ln("[PhantomReader]", "some data was not read");
364 }
365
366 return phdump;
367}
368
370 logger::raw_ln("--- dump state ---");
371
372 logger::raw_ln("table_header_fort_int len =", table_header_fort_int.entries.size());
373 table_header_fort_int.print_state();
374 logger::raw_ln("table_header_i8 len =", table_header_i8.entries.size());
375 table_header_i8.print_state();
376 logger::raw_ln("table_header_i16 len =", table_header_i16.entries.size());
377 table_header_i16.print_state();
378 logger::raw_ln("table_header_i32 len =", table_header_i32.entries.size());
379 table_header_i32.print_state();
380 logger::raw_ln("table_header_i64 len =", table_header_i64.entries.size());
381 table_header_i64.print_state();
382 logger::raw_ln("table_header_fort_real len =", table_header_fort_real.entries.size());
383 table_header_fort_real.print_state();
384 logger::raw_ln("table_header_f32 len =", table_header_f32.entries.size());
385 table_header_f32.print_state();
386 logger::raw_ln("table_header_f64 len =", table_header_f64.entries.size());
387 table_header_f64.print_state();
388
389 for (u32 i = 0; i < blocks.size(); i++) {
390 logger::raw_ln("block ", i, ":");
391 blocks[i].print_state();
392 }
393 logger::raw_ln("------------------");
394}
395
397
398 u64 offenses = 0;
399
400 auto load_header = [](PhantomDump &dump) {
401 std::unordered_map<std::string, f64> header;
402
403 dump.table_header_fort_int.add_to_map(header);
404 dump.table_header_i8.add_to_map(header);
405 dump.table_header_i16.add_to_map(header);
406 dump.table_header_i32.add_to_map(header);
407 dump.table_header_i64.add_to_map(header);
408 dump.table_header_fort_real.add_to_map(header);
409 dump.table_header_f32.add_to_map(header);
410 dump.table_header_f64.add_to_map(header);
411 return header;
412 };
413
414 std::unordered_map<std::string, f64> header_ref = load_header(dump_ref);
415 std::unordered_map<std::string, f64> header_comp = load_header(dump_comp);
416
417 auto get_keys = [](std::unordered_map<std::string, f64> &map) {
418 std::set<std::string> ret;
419 for (auto [key, val] : map) {
420 ret.insert(key);
421 }
422 return ret;
423 };
424
425 std::set<std::string> header_key_ref = get_keys(header_ref);
426 std::set<std::string> header_key_comp = get_keys(header_comp);
427
428 std::vector<std::string> missing = {};
429 std::vector<std::string> matching = {};
430 std::vector<std::string> extra = {};
431
432 shambase::set_diff(header_key_comp, header_key_ref, missing, matching, extra);
433
434 for (std::string &missing_key : missing) {
436 "PhantomDump",
437 "The dump we are comparing against is missing the key",
438 missing_key,
439 "in the header");
440 offenses++;
441 }
442 for (std::string &matching_key : matching) {
443 if (header_ref[matching_key] != header_comp[matching_key]) {
445 "PhantomDump",
446 sham::format(
447 "Mismatch in the header key {}, ref={}, comp={}",
448 matching_key,
449 header_ref[matching_key],
450 header_comp[matching_key]));
451 offenses++;
452 }
453 }
454 for (std::string &extra_key : extra) {
456 "PhantomDump",
457 "The dump we are comparing against has the extra key",
458 extra_key,
459 "in the header");
460 offenses++;
461 }
462
463 logger::info_ln("PhantomDump", "This comparison reported", offenses, "offenses");
464
465 return offenses == 0;
466}
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int64_t i64
64 bit integer
std::int32_t i32
32 bit integer
Class for reading and writing Fortran-style binary files.
void write(Args &...args)
Write a list of arguments to the internal buffer.
void write_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Write a fixed-length string array to the buffer.
void write_val_array(std::vector< T > &vec, u32 val_count)
Write an array of values to the buffer.
void read_fixed_string(std::string &s, u32 len)
Read a fixed-length string from the buffer.
void read(Args &...args)
Read a list of arguments from the internal buffer.
void read_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Read a fixed-length string array from the buffer.
void read_val_array(std::vector< T > &vec, u32 val_count)
Read an array of values from the buffer.
void write_fixed_string(std::string &s, u32 len)
Write a fixed-length string to the buffer.
bool finished_read()
Check if the end of the file has been reached.
This header file contains utility functions related to exception handling in the code.
void set_diff(Container1 &c1, Container2 &ref, std::vector< T > &missing, std::vector< T > &matching, std::vector< T > &extra)
Compute the difference between two containers as three separate vectors.
Definition sets.hpp:41
bool compare_phantom_dumps(PhantomDump &dump1, PhantomDump &dump2)
Compare two phantom dumps and report offenses.
void raw_ln(Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:89
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
A helper class to represent a single block of data in a Phantom dump.
void write(shambase::FortranIOFile &phfile, i64 tot_count)
Writes a block to a file.
std::string tag
The tag of the block.
void print_state()
Prints the state of the block.
std::vector< T > vals
The values of the block.
static PhantomDumpBlockArray from_file(shambase::FortranIOFile &phfile, i64 tot_count)
Reads a block from a file.
A class to represent a single block of data in a Phantom dump.
std::vector< PhantomDumpBlockArray< fort_int > > blocks_fort_int
The blocks of values of type fort_int.
static PhantomDumpBlock from_file(shambase::FortranIOFile &phfile, i64 tot_count, std::array< i32, 8 > numarray)
Reads a block from a file.
u64 get_ref_f32(std::string s)
Gets the index of a block of type f32 with the given name.
std::vector< PhantomDumpBlockArray< i16 > > blocks_i16
The blocks of values of type i16.
std::vector< PhantomDumpBlockArray< i8 > > blocks_i8
The blocks of values of type i8.
std::vector< PhantomDumpBlockArray< f64 > > blocks_f64
The blocks of values of type f64.
std::vector< PhantomDumpBlockArray< i64 > > blocks_i64
The blocks of values of type i64.
void write(shambase::FortranIOFile &phfile, i64 tot_count, std::array< i32, 8 > numarray)
Writes a block to a file.
u64 get_ref_fort_real(std::string s)
Gets the index of a block of type fort_real with the given name.
std::vector< PhantomDumpBlockArray< i32 > > blocks_i32
The blocks of values of type i32.
i64 tot_count
The total number of values in the block.
std::vector< PhantomDumpBlockArray< fort_real > > blocks_fort_real
The blocks of values of type fort_real.
std::vector< PhantomDumpBlockArray< f32 > > blocks_f32
The blocks of values of type f32.
void print_state()
Prints the state of the block.
Phantom dump table header for a specific type.
static PhantomDumpTableHeader< T > from_file(shambase::FortranIOFile &phfile)
Reads the header from a Phantom dump file.
std::vector< std::pair< std::string, T > > entries
A vector of pairs containing the name and the value of each entry in the table.
void print_state()
Prints the state of the header.
void write(shambase::FortranIOFile &phfile)
Writes the header to a Phantom dump file.
Class representing a Phantom dump file.
PhantomDumpTableHeader< i16 > table_header_i16
Table header for signed 16-bit integer data.
fort_int i3
Magic number used in the phantom dump format.
PhantomDumpTableHeader< fort_int > table_header_fort_int
Table header for integer data.
std::string fileid
Magic number used in the phantom dump format.
fort_int i1
Magic number used in the phantom dump format.
PhantomDumpTableHeader< f32 > table_header_f32
Table header for 32-bit floating-point data.
PhantomDumpTableHeader< i64 > table_header_i64
Table header for signed 64-bit integer data.
PhantomDumpTableHeader< i32 > table_header_i32
Table header for signed 32-bit integer data.
static PhantomDump from_file(shambase::FortranIOFile &phfile)
Reads a Phantom dump file and returns a PhantomDump object.
shambase::FortranIOFile gen_file()
Generates a Phantom dump file from the current state of the object.
fort_int i2
Magic number used in the phantom dump format.
std::vector< PhantomDumpBlock > blocks
List of blocks in the Phantom dump file.
PhantomDumpTableHeader< fort_real > table_header_fort_real
Table header for floating-point data.
PhantomDumpTableHeader< f64 > table_header_f64
Table header for 64-bit floating-point data.
fort_int iversion
Magic number used in the phantom dump format.
void print_state()
Print current state of the data stored in the class.
PhantomDumpTableHeader< i8 > table_header_i8
Table header for signed 8-bit integer data.
fort_real r1
Magic number used in the phantom dump format.