Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Phantom2Shamrock.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
22
23namespace shammodels::sph {
24
25 template<class Tvec>
27
28 EOSConfig<Tvec> cfg{};
29
30 i64 ieos = phdump.read_header_int<i64>("ieos");
31
32 shamlog_debug_ln("PhantomDump", "read ieos :", ieos);
33
34 if (ieos == 1) {
35 f64 cs;
36 phdump::eos1_load(phdump, cs);
37 cfg.set_isothermal(cs);
38 } else if (ieos == 2) {
39 f64 gamma;
40 phdump::eos2_load(phdump, gamma);
41 cfg.set_adiabatic(gamma);
42 } else if (ieos == 3) {
43 f64 cs0, q, r0;
44 phdump::eos3_load(phdump, cs0, q, r0);
45 cfg.set_locally_isothermalLP07(cs0, q, r0);
46 } else if (ieos == 13) {
47 f64 cs0, q, r0;
48 phdump::eos13_load(phdump, cs0, q, r0);
49 // u32_max implies all sinks
50 cfg.set_locally_isothermalFA2014_extended(cs0, q, r0, u32_max);
51 } else if (ieos == 14) {
52 f64 cs0, q, r0;
53 phdump::eos14_load(phdump, cs0, q, r0);
54 cfg.set_locally_isothermalFA2014_extended(cs0, q, r0, 2);
55 } else {
56 const std::string msg = "loading phantom ieos=" + std::to_string(ieos)
57 + " is not implemented in shamrock";
58 if (bypass_error) {
59 logger::warn_ln("SPH", msg);
60 } else {
62 }
63 }
64
65 return cfg;
66 }
67
68 template<class Tvec>
70 EOSConfig<Tvec> &cfg, PhantomDump &dump, bool bypass_error) {
71
72 using EOS_Isothermal = typename EOSConfig<Tvec>::Isothermal;
73 using EOS_Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
74 using EOS_LocallyIsothermal = typename EOSConfig<Tvec>::LocallyIsothermal;
75 using EOS_LocallyIsothermalLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
76 using EOS_LocallyIsothermalFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
77 using EOS_LocallyIsothermalFA2014Extended =
79
80 if (EOS_Isothermal *eos_config = std::get_if<EOS_Isothermal>(&cfg.config)) {
81 phdump::eos1_write(dump, eos_config->cs);
82 } else if (EOS_Adiabatic *eos_config = std::get_if<EOS_Adiabatic>(&cfg.config)) {
83 phdump::eos2_write(dump, eos_config->gamma);
84 } else if (
85 EOS_LocallyIsothermalLP07 *eos_config
86 = std::get_if<EOS_LocallyIsothermalLP07>(&cfg.config)) {
87 phdump::eos3_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
88 } else if (
89 EOS_LocallyIsothermalFA2014Extended *eos_config
90 = std::get_if<EOS_LocallyIsothermalFA2014Extended>(&cfg.config)) {
91
92 if (eos_config->n_sinks == u32_max) {
93 phdump::eos13_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
94 } else if (eos_config->n_sinks == 2) {
95 phdump::eos14_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
96 } else {
97 const std::string msg
98 = "Phantom only support all or 2 sinks for this EOS configuration";
99 if (bypass_error) {
100 logger::warn_ln("SPH", msg);
101 } else {
103 }
104 }
105 } else {
106 const std::string msg
107 = "The current shamrock EOS is not implemented in phantom dump conversion";
108 if (bypass_error) {
109 logger::warn_ln("SPH", msg);
110 } else {
112 }
113 }
114 }
115
117 template EOSConfig<f32_3> get_shamrock_eosconfig<f32_3>(PhantomDump &phdump, bool bypass_error);
119 template EOSConfig<f64_3> get_shamrock_eosconfig<f64_3>(PhantomDump &phdump, bool bypass_error);
120
123 EOSConfig<f32_3> &cfg, PhantomDump &dump, bool bypass_error);
126 EOSConfig<f64_3> &cfg, PhantomDump &dump, bool bypass_error);
127
128} // namespace shammodels::sph
129
130namespace shammodels::sph {
131
132 template<class Tvec>
134 AVConfig<Tvec> cfg{};
135
136 cfg.set_varying_cd10(0, 1, 0.1, phdump.read_header_float<f64>("alphau"), 2);
137
138 return cfg;
139 }
140
142 template AVConfig<f32_3> get_shamrock_avconfig<f32_3>(PhantomDump &phdump);
144 template AVConfig<f64_3> get_shamrock_avconfig<f64_3>(PhantomDump &phdump);
145
146 template<class Tscal>
148
149 f64 udist = phdump.read_header_float<f64>("udist");
150 f64 umass = phdump.read_header_float<f64>("umass");
151 f64 utime = phdump.read_header_float<f64>("utime");
152 f64 umagfd = phdump.read_header_float<f64>("umagfd");
153
155 utime, udist, umass
156 // unit_current = 1 ,
157 // unit_temperature = 1 ,
158 // unit_qte = 1 ,
159 // unit_lumint = 1
160 );
161 }
162
163 template<class Tscal>
165 std::optional<shamunits::UnitSystem<Tscal>> &units, PhantomDump &dump, bool bypass_error) {
166
167 if (units) {
168 dump.table_header_f64.add("udist", units->m_inv);
169 dump.table_header_f64.add("umass", units->kg_inv);
170 dump.table_header_f64.add("utime", units->s_inv);
171
172 f64 umass = units->template to<shamunits::units::kg>();
173 f64 utime = units->template to<shamunits::units::s>();
174 f64 udist = units->template to<shamunits::units::m>();
175
176 shamunits::Constants<Tscal> ctes{*units};
177 f64 ccst = ctes.c();
178 f64 ucharge = sqrt(umass * udist / (4. * shambase::constants::pi<f64> /*mu_0 in cgs*/));
179
180 f64 umagfd = umass / (utime * ucharge);
181
182 dump.table_header_f64.add("umagfd", umagfd);
183 } else {
184 logger::warn_ln("SPH", "no units are set, defaulting to SI");
185
186 dump.table_header_f64.add("udist", 1);
187 dump.table_header_f64.add("umass", 1);
188 dump.table_header_f64.add("utime", 1);
189 dump.table_header_f64.add("umagfd", 3.54491);
190 }
191 }
192
194 template shamunits::UnitSystem<f32> get_shamrock_units<f32>(PhantomDump &phdump);
196 template shamunits::UnitSystem<f64> get_shamrock_units<f64>(PhantomDump &phdump);
197
198 template void write_shamrock_units_in_phantom_dump<f64>(
199 std::optional<shamunits::UnitSystem<f64>> &units, PhantomDump &dump, bool bypass_error);
200} // namespace shammodels::sph
201
202namespace shammodels::sph {
203 template<class Tvec>
205 BCConfig<Tvec> cfg;
206 // xmin, xmax, y... z... are in the header only in periodic mode in phantom
207 if (phdump.has_header_entry("xmin")) {
208 logger::raw_ln("Setting periodic boundaries from phantmdump");
209 cfg.set_periodic();
210 } else {
211 logger::raw_ln("Setting free boundaries from phantmdump");
212 cfg.set_free();
213 }
214 return cfg;
215 }
216
217 template<class Tvec>
218 void write_shamrock_boundaries_in_phantom_dump(
219 BCConfig<Tvec> &cfg,
220 std::tuple<Tvec, Tvec> box_size,
221 PhantomDump &dump,
222 bool bypass_error) {
223
224 auto [bmin, bmax] = box_size;
225 using SolverConfigBC = BCConfig<Tvec>;
226 using SolverBCFree = typename SolverConfigBC::Free;
227 using SolverBCPeriodic = typename SolverConfigBC::Periodic;
228 using SolverBCShearingPeriodic = typename SolverConfigBC::ShearingPeriodic;
229
230 // boundary condition selections
231 if (SolverBCFree *c = std::get_if<SolverBCFree>(&cfg.config)) {
232 // do nothing
233 } else if (SolverBCPeriodic *c = std::get_if<SolverBCPeriodic>(&cfg.config)) {
234 dump.table_header_fort_real.add("xmin", bmin.x());
235 dump.table_header_fort_real.add("xmax", bmax.x());
236 dump.table_header_fort_real.add("ymin", bmin.y());
237 dump.table_header_fort_real.add("ymax", bmax.x());
238 dump.table_header_fort_real.add("zmin", bmin.z());
239 dump.table_header_fort_real.add("zmax", bmax.x());
240 } else if (
241 SolverBCShearingPeriodic *c = std::get_if<SolverBCShearingPeriodic>(&cfg.config)) {
242 std::string err_msg
243 = "Phantom does not support shearing periodic boundaries but your are "
244 "making a phantom dump with them, set bypass_error_check=True to ignore";
245
246 if (!bypass_error) {
247 throw std::runtime_error(err_msg);
248 } else {
249 logger::warn_ln("PhantomDump", err_msg);
250 }
251 }
252 }
253
255 template BCConfig<f32_3> get_shamrock_boundary_config<f32_3>(PhantomDump &phdump);
257 template BCConfig<f64_3> get_shamrock_boundary_config<f64_3>(PhantomDump &phdump);
258
260 template void write_shamrock_boundaries_in_phantom_dump<f32_3>(
261 BCConfig<f32_3> &cfg,
262 std::tuple<f32_3, f32_3> box_size,
263 PhantomDump &dump,
264 bool bypass_error);
266 template void write_shamrock_boundaries_in_phantom_dump<f64_3>(
267 BCConfig<f64_3> &cfg,
268 std::tuple<f64_3, f64_3> box_size,
269 PhantomDump &dump,
270 bool bypass_error);
271} // namespace shammodels::sph
double f64
Alias for double.
std::int64_t i64
64 bit integer
Defines a unit system.
Class holding the value of numerous constants generated from the following source.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
namespace for the sph model
BCConfig< Tvec > get_shamrock_boundary_config(PhantomDump &phdump)
Generate an Shamrock boundary configuration from a PhantomDump object.
AVConfig< Tvec > get_shamrock_avconfig(PhantomDump &phdump)
Generate an Shamrock artificial viscosity configuration from a PhantomDump object.
template EOSConfig< f64_3 > get_shamrock_eosconfig< f64_3 >(PhantomDump &phdump, bool bypass_error)
explicit instanciation for f64_3
EOSConfig< Tvec > get_shamrock_eosconfig(PhantomDump &phdump, bool bypass_error)
Generate a Shamrock EOS configuration from a PhantomDump object.
template void write_shamrock_eos_in_phantom_dump< f64_3 >(EOSConfig< f64_3 > &cfg, PhantomDump &dump, bool bypass_error)
explicit instanciation for f64_3
void write_shamrock_units_in_phantom_dump(std::optional< shamunits::UnitSystem< Tscal > > &units, PhantomDump &dump, bool bypass_error)
Write shamrock units config into the phantom dump.
template EOSConfig< f32_3 > get_shamrock_eosconfig< f32_3 >(PhantomDump &phdump, bool bypass_error)
explicit instanciation for f32_3
shamunits::UnitSystem< Tscal > get_shamrock_units(PhantomDump &phdump)
Get the shamrock units object.
void write_shamrock_eos_in_phantom_dump(EOSConfig< Tvec > &cfg, PhantomDump &dump, bool bypass_error)
Write the eos config to th phantom dump header.
template void write_shamrock_eos_in_phantom_dump< f32_3 >(EOSConfig< f32_3 > &cfg, PhantomDump &dump, bool bypass_error)
explicit instanciation for f32_3
constexpr u32 u32_max
u32 max value
Locally isothermal equation of state configuration.
Definition EOSConfig.hpp:61
Configuration struct for the equation of state used in the hydrodynamic models.
Definition EOSConfig.hpp:42
Variant config
Current EOS configuration.
Definition EOSConfig.hpp:89
Configuration for the Artificial Viscosity (AV)
Definition AVConfig.hpp:189
void set_varying_cd10(Tscal alpha_min, Tscal alpha_max, Tscal sigma_decay, Tscal alpha_u, Tscal beta_AV)
Sets the configuration to use a varying Cullen & Dehnen 2010 artificial viscosity.
Definition AVConfig.hpp:220
Boundary conditions configuration.
Definition BCConfig.hpp:40
void set_periodic()
Set the boundary condition to periodic boundaries.
Definition BCConfig.hpp:101
void set_free()
Set the boundary condition to free boundaries.
Definition BCConfig.hpp:98
void add(std::string s, T val)
Adds an entry to the header.
Class representing a Phantom dump file.
T read_header_float(std::string s) const
Retrieves a floating-point value from the table headers.
bool has_header_entry(std::string s) const
Checks if a given string is present in any of the table headers.
T read_header_int(std::string s) const
Retrieves an integer value from the table headers.
PhantomDumpTableHeader< f64 > table_header_f64
Table header for 64-bit floating-point data.
Configuration struct for adiabatic equation of state.
Configuration struct for isothermal equation of state.
Configuration struct for the locally isothermal equation of state extended from Farris 2014 to includ...
Configuration struct for the locally isothermal equation of state from Farris 2014.
Configuration struct for the locally isothermal equation of state from Lodato Price 2007.
Physical constants.
constexpr T c()
get c in the current unit system units (m.s-1)