Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PhantomDump.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
21#include "shambase/string.hpp"
22#include <unordered_map>
23#include <array>
24#include <cstdlib>
25#include <optional>
26#include <stdexcept>
27#include <string>
28#include <utility>
29#include <vector>
30
31namespace shammodels::sph {
32
40 template<class T>
46 std::vector<std::pair<std::string, T>> entries;
47
55
62 void add(std::string s, T val) {
63 s = shambase::format("{:16s}", s);
64 entries.push_back({s, val});
65 }
66
72 void write(shambase::FortranIOFile &phfile);
73
80 inline std::optional<T> fetch(std::string s) const {
81 std::optional<T> ret = {};
82
83 for (auto [key, val] : entries) {
84 if (key == s) {
85 ret = val;
86 }
87 }
88
89 return ret;
90 }
91
99 template<class Tb>
100 inline void fetch_multiple(std::vector<Tb> &vec, std::string s) {
101 for (auto [key, val] : entries) {
102 if (key == s) {
103 vec.push_back(val);
104 }
105 }
106 }
107
111 void print_state();
112
113 template<class Tconv>
114 inline void add_to_map(std::unordered_map<std::string, Tconv> &map) {
115 for (auto [key, val] : entries) {
116 map[key] = val;
117 }
118 }
119 };
120
129 template<class T>
131
135 std::string tag;
136
140 std::vector<T> vals;
141
150
157 void write(shambase::FortranIOFile &phfile, i64 tot_count);
158
165 template<class Tb>
166 void fill_vec(std::string field_name, std::vector<Tb> &vec) {
167 if (tag == field_name) {
168 for (T a : vals) {
169 vec.push_back(a);
170 }
171 }
172 }
173
177 void print_state();
178 };
179
188
190 using fort_real = f64;
192 using fort_int = int;
193
195 std::vector<PhantomDumpBlockArray<fort_int>> blocks_fort_int;
197 std::vector<PhantomDumpBlockArray<i8>> blocks_i8;
199 std::vector<PhantomDumpBlockArray<i16>> blocks_i16;
201 std::vector<PhantomDumpBlockArray<i32>> blocks_i32;
203 std::vector<PhantomDumpBlockArray<i64>> blocks_i64;
205 std::vector<PhantomDumpBlockArray<fort_real>> blocks_fort_real;
207 std::vector<PhantomDumpBlockArray<f32>> blocks_f32;
209 std::vector<PhantomDumpBlockArray<f64>> blocks_f64;
210
217 u64 get_ref_fort_int(std::string s);
218
225 u64 get_ref_i8(std::string s);
226
233 u64 get_ref_i16(std::string s);
234
241 u64 get_ref_i32(std::string s);
242
249 u64 get_ref_i64(std::string s);
250
257 u64 get_ref_fort_real(std::string s);
258
265 u64 get_ref_f32(std::string s);
266
273 u64 get_ref_f64(std::string s);
274
278 void print_state();
279
289 shambase::FortranIOFile &phfile, i64 tot_count, std::array<i32, 8> numarray);
290
298 void write(shambase::FortranIOFile &phfile, i64 tot_count, std::array<i32, 8> numarray);
299
306 template<class T>
307 void fill_vec(std::string field_name, std::vector<T> &vec) {
308
309 field_name = shambase::format("{:16s}", field_name);
310
311 for (auto &tmp : blocks_fort_int) {
312 tmp.fill_vec(field_name, vec);
313 }
314 for (auto &tmp : blocks_i8) {
315 tmp.fill_vec(field_name, vec);
316 }
317 for (auto &tmp : blocks_i16) {
318 tmp.fill_vec(field_name, vec);
319 }
320 for (auto &tmp : blocks_i32) {
321 tmp.fill_vec(field_name, vec);
322 }
323 for (auto &tmp : blocks_i64) {
324 tmp.fill_vec(field_name, vec);
325 }
326 for (auto &tmp : blocks_fort_real) {
327 tmp.fill_vec(field_name, vec);
328 }
329 for (auto &tmp : blocks_f32) {
330 tmp.fill_vec(field_name, vec);
331 }
332 for (auto &tmp : blocks_f64) {
333 tmp.fill_vec(field_name, vec);
334 }
335 }
336 };
337
347 struct PhantomDump {
348
350 using fort_real = f64;
352 using fort_int = int;
353
365 std::string fileid;
366
369 i1 = 60769;
370 i2 = 60878;
371 i3 = 690706;
372 r1 = i2;
373 }
374
381 if (i1 != 60769) {
383 }
384 if (i2 != 60878) {
386 }
387 if (i3 != 690706) {
389 }
390 if (r1 != i2) {
392 }
393 }
394
412 std::vector<PhantomDumpBlock> blocks;
413
420
428
436 inline bool has_header_entry(std::string s) const {
437
438 s = shambase::format("{:16s}", s);
439
440 if (auto tmp = table_header_fort_int.fetch(s); tmp) {
441 return true;
442 }
443 if (auto tmp = table_header_i8.fetch(s); tmp) {
444 return true;
445 }
446 if (auto tmp = table_header_i16.fetch(s); tmp) {
447 return true;
448 }
449 if (auto tmp = table_header_i32.fetch(s); tmp) {
450 return true;
451 }
452 if (auto tmp = table_header_i64.fetch(s); tmp) {
453 return true;
454 }
455 if (auto tmp = table_header_fort_real.fetch(s); tmp) {
456 return true;
457 }
458 if (auto tmp = table_header_f32.fetch(s); tmp) {
459 return true;
460 }
461 if (auto tmp = table_header_f64.fetch(s); tmp) {
462 return true;
463 }
464
465 return false;
466 }
467
477 template<class T>
478 inline T read_header_float(std::string s) const {
479
480 s = shambase::format("{:16s}", s);
481
482 if (auto tmp = table_header_fort_real.fetch(s); tmp) {
483 return *tmp;
484 }
485 if (auto tmp = table_header_f32.fetch(s); tmp) {
486 return *tmp;
487 }
488 if (auto tmp = table_header_f64.fetch(s); tmp) {
489 return *tmp;
490 }
491
493 "the entry cannot be found : " + s);
494
495 return {};
496 }
497
505 template<class T>
506 inline std::vector<T> read_header_floats(std::string s) {
507 std::vector<T> vec{};
508
509 s = shambase::format("{:16s}", s);
510
514
515 return vec;
516 }
517
527 template<class T>
528 inline T read_header_int(std::string s) const {
529
530 s = shambase::format("{:16s}", s);
531
532 if (auto tmp = table_header_fort_int.fetch(s); tmp) {
533 return *tmp;
534 }
535 if (auto tmp = table_header_i8.fetch(s); tmp) {
536 return *tmp;
537 }
538 if (auto tmp = table_header_i16.fetch(s); tmp) {
539 return *tmp;
540 }
541 if (auto tmp = table_header_i32.fetch(s); tmp) {
542 return *tmp;
543 }
544 if (auto tmp = table_header_i64.fetch(s); tmp) {
545 return *tmp;
546 }
547
548 throw shambase::make_except_with_loc<std::runtime_error>("the entry cannot be found");
549
550 return {};
551 }
552
561 template<class T>
562 inline std::vector<T> read_header_ints(std::string s) {
563 std::vector<T> vec{};
564
565 s = shambase::format("{:16s}", s);
566
572
573 return vec;
574 }
575
577 void print_state();
578 };
579
581 bool compare_phantom_dumps(PhantomDump &dump1, PhantomDump &dump2);
582
583} // namespace shammodels::sph
double f64
Alias for double.
std::uint64_t u64
64 bit unsigned integer
std::int64_t i64
64 bit integer
Class for reading and writing Fortran-style binary files.
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the sph model
bool compare_phantom_dumps(PhantomDump &dump1, PhantomDump &dump2)
Compare two phantom dumps and report offenses.
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.
void fill_vec(std::string field_name, std::vector< Tb > &vec)
Fills a vector with the values of a given field name.
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.
u64 get_ref_i16(std::string s)
Gets the index of a block of type i16 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.
u64 get_ref_fort_int(std::string s)
Gets the index of a block of type fort_int with the given name.
void write(shambase::FortranIOFile &phfile, i64 tot_count, std::array< i32, 8 > numarray)
Writes a block to a file.
void fill_vec(std::string field_name, std::vector< T > &vec)
Fills a vector with the values of a given field name.
u64 get_ref_fort_real(std::string s)
Gets the index of a block of type fort_real with the given name.
f64 fort_real
The type for Phantom's real type.
std::vector< PhantomDumpBlockArray< i32 > > blocks_i32
The blocks of values of type i32.
int fort_int
The type for Phantom's integer type.
u64 get_ref_i64(std::string s)
Gets the index of a block of type i64 with the given name.
u64 get_ref_f64(std::string s)
Gets the index of a block of type f64 with the given name.
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.
u64 get_ref_i8(std::string s)
Gets the index of a block of type i8 with the given name.
u64 get_ref_i32(std::string s)
Gets the index of a block of type i32 with the given name.
Phantom dump table header for a specific type.
std::optional< T > fetch(std::string s) const
Fetches the value of a given entry from the header.
void add(std::string s, T val)
Adds an entry to the header.
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 fetch_multiple(std::vector< Tb > &vec, std::string s)
Fetches the values of all entries with a given name from the header into the supplied vector.
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.
std::vector< T > read_header_floats(std::string s)
Reads multiple float values from the table headers based on the given string.
PhantomDumpTableHeader< i16 > table_header_i16
Table header for signed 16-bit integer data.
int fort_int
Integer type used in the phantom dump format.
fort_int i3
Magic number used in the phantom dump format.
T read_header_float(std::string s) const
Retrieves a floating-point value from the table headers.
void override_magic_number()
Overrides the magic numbers used in the PhantomDump struct.
void check_magic_numbers()
Checks if the magic numbers in the PhantomDump struct match the expected values.
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.
bool has_header_entry(std::string s) const
Checks if a given string is present in any of the table headers.
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.
T read_header_int(std::string s) const
Retrieves an integer value from the table headers.
f64 fort_real
Floating-point type used in the phantom dump format.
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.
std::vector< T > read_header_ints(std::string s)
Retrieves multiple integer values from the table headers based on the given string.
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.