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
16
21
22namespace shammodels::sph {
23
24 template<class Tvec>
26
27 EOSConfig<Tvec> cfg{};
28
29 i64 ieos = phdump.read_header_int<i64>("ieos");
30
31 shamlog_debug_ln("PhantomDump", "read ieos :", ieos);
32
33 if (ieos == 1) {
34 f64 cs;
35 phdump::eos1_load(phdump, cs);
36 cfg.set_isothermal(cs);
37 } else if (ieos == 2) {
38 f64 gamma;
39 phdump::eos2_load(phdump, gamma);
40 cfg.set_adiabatic(gamma);
41 } else if (ieos == 3) {
42 f64 cs0, q, r0;
43 phdump::eos3_load(phdump, cs0, q, r0);
44 cfg.set_locally_isothermalLP07(cs0, q, r0);
45 } else if (ieos == 6) {
46 f64 cs0, q, r0;
47 i32 isink;
48 phdump::eos6_load(phdump, cs0, q, r0, isink);
49 // TODO: implement a locally isothermal eos centred on a sink in shamrock
51 "SPH",
52 "phantom ieos=6 (locally isothermal centred on sink",
53 isink,
54 ") is not implemented in shamrock, falling back to ieos=3 (locally isothermal "
55 "centred on the origin). This is only correct if the sink stays at the origin.");
56 cfg.set_locally_isothermalLP07(cs0, q, r0);
57 } else if (ieos == 13) {
58 f64 cs0, q, r0;
59 phdump::eos13_load(phdump, cs0, q, r0);
60 // u32_max implies all sinks
61 cfg.set_locally_isothermalFA2014_extended(cs0, q, r0, u32_max);
62 } else if (ieos == 14) {
63 f64 cs0, q, r0;
64 phdump::eos14_load(phdump, cs0, q, r0);
65 cfg.set_locally_isothermalFA2014_extended(cs0, q, r0, 2);
66 } else {
67 const std::string msg = "loading phantom ieos=" + std::to_string(ieos)
68 + " is not implemented in shamrock";
69 if (bypass_error) {
70 logger::warn_ln("SPH", msg);
71 } else {
73 }
74 }
75
76 return cfg;
77 }
78
79 template<class Tvec>
81 EOSConfig<Tvec> &cfg, PhantomDump &dump, bool bypass_error) {
82
83 using EOS_Isothermal = typename EOSConfig<Tvec>::Isothermal;
84 using EOS_Adiabatic = typename EOSConfig<Tvec>::Adiabatic;
85 using EOS_LocallyIsothermal = typename EOSConfig<Tvec>::LocallyIsothermal;
86 using EOS_LocallyIsothermalLP07 = typename EOSConfig<Tvec>::LocallyIsothermalLP07;
87 using EOS_LocallyIsothermalFA2014 = typename EOSConfig<Tvec>::LocallyIsothermalFA2014;
88 using EOS_LocallyIsothermalFA2014Extended =
90
91 if (EOS_Isothermal *eos_config = std::get_if<EOS_Isothermal>(&cfg.config)) {
92 phdump::eos1_write(dump, eos_config->cs);
93 } else if (EOS_Adiabatic *eos_config = std::get_if<EOS_Adiabatic>(&cfg.config)) {
94 phdump::eos2_write(dump, eos_config->gamma);
95 } else if (
96 EOS_LocallyIsothermalLP07 *eos_config
97 = std::get_if<EOS_LocallyIsothermalLP07>(&cfg.config)) {
98 phdump::eos3_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
99 } else if (
100 EOS_LocallyIsothermalFA2014Extended *eos_config
101 = std::get_if<EOS_LocallyIsothermalFA2014Extended>(&cfg.config)) {
102
103 if (eos_config->n_sinks == u32_max) {
104 phdump::eos13_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
105 } else if (eos_config->n_sinks == 2) {
106 phdump::eos14_write(dump, eos_config->cs0, eos_config->q, eos_config->r0);
107 } else {
108 const std::string msg
109 = "Phantom only support all or 2 sinks for this EOS configuration";
110 if (bypass_error) {
111 logger::warn_ln("SPH", msg);
112 } else {
114 }
115 }
116 } else {
117 const std::string msg
118 = "The current shamrock EOS is not implemented in phantom dump conversion";
119 if (bypass_error) {
120 logger::warn_ln("SPH", msg);
121 } else {
123 }
124 }
125 }
126
128 template EOSConfig<f32_3> get_shamrock_eosconfig<f32_3>(PhantomDump &phdump, bool bypass_error);
130 template EOSConfig<f64_3> get_shamrock_eosconfig<f64_3>(PhantomDump &phdump, bool bypass_error);
131
134 EOSConfig<f32_3> &cfg, PhantomDump &dump, bool bypass_error);
137 EOSConfig<f64_3> &cfg, PhantomDump &dump, bool bypass_error);
138
139} // namespace shammodels::sph
140
141namespace shammodels::sph {
142
143 template<class Tvec>
145 AVConfig<Tvec> cfg{};
146
147 cfg.set_varying_cd10(0, 1, 0.1, phdump.read_header_float<f64>("alphau"), 2);
148
149 return cfg;
150 }
151
156
157 namespace {
158 // Phantom stores its unit headers (udist, umass, utime, umagfd) in cgs,
159 // while shamunits::UnitSystem is built from SI base units.
160 constexpr f64 m_to_cm = 1e2;
161 constexpr f64 kg_to_g = 1e3;
162
163 // truncated value of pi used by phantom (physcon.f90), using it makes umagfd bit-identical
164 // to the one phantom writes
165 constexpr f64 phantom_pi = 3.1415926536;
166
168 f64 get_phantom_umagfd(f64 udist, f64 umass, f64 utime) {
169 f64 ucharge = sycl::sqrt(umass * udist / (4. * phantom_pi));
170 return umass / (utime * ucharge);
171 }
172 } // namespace
173
174 template<class Tscal>
176
177 f64 udist = phdump.read_header_float<f64>("udist");
178 f64 umass = phdump.read_header_float<f64>("umass");
179 f64 utime = phdump.read_header_float<f64>("utime");
180 f64 umagfd = phdump.read_header_float<f64>("umagfd");
181
183 utime, udist / m_to_cm, umass / kg_to_g
184 // unit_current = 1 ,
185 // unit_temperature = 1 ,
186 // unit_qte = 1 ,
187 // unit_lumint = 1
188 );
189 }
190
191 template<class Tscal>
193 std::optional<shamunits::UnitSystem<Tscal>> &units, PhantomDump &dump, bool bypass_error) {
194
195 // code units expressed in SI (s, m, kg), SI being the code units if none are set
196 f64 utime_si = 1;
197 f64 udist_si = 1;
198 f64 umass_si = 1;
199
200 if (units) {
201 utime_si = units->s_inv;
202 udist_si = units->m_inv;
203 umass_si = units->kg_inv;
204 } else {
205 logger::warn_ln("SPH", "no units are set, defaulting to SI");
206 }
207
208 f64 udist = udist_si * m_to_cm;
209 f64 umass = umass_si * kg_to_g;
210 f64 utime = utime_si;
211
212 dump.table_header_f64.add("udist", udist);
213 dump.table_header_f64.add("umass", umass);
214 dump.table_header_f64.add("utime", utime);
215 dump.table_header_f64.add("umagfd", get_phantom_umagfd(udist, umass, utime));
216 }
217
222
224 std::optional<shamunits::UnitSystem<f64>> &units, PhantomDump &dump, bool bypass_error);
225} // namespace shammodels::sph
226
227namespace shammodels::sph {
228 template<class Tvec>
230 BCConfig<Tvec> cfg;
231 // xmin, xmax, y... z... are in the header only in periodic mode in phantom
232 if (phdump.has_header_entry("xmin")) {
233 logger::raw_ln("Setting periodic boundaries from phantmdump");
234 cfg.set_periodic();
235 } else {
236 logger::raw_ln("Setting free boundaries from phantmdump");
237 cfg.set_free();
238 }
239 return cfg;
240 }
241
242 template<class Tvec>
243 void write_shamrock_boundaries_in_phantom_dump(
244 BCConfig<Tvec> &cfg,
245 std::tuple<Tvec, Tvec> box_size,
246 PhantomDump &dump,
247 bool bypass_error) {
248
249 auto [bmin, bmax] = box_size;
250 using SolverConfigBC = BCConfig<Tvec>;
251 using SolverBCFree = typename SolverConfigBC::Free;
252 using SolverBCPeriodic = typename SolverConfigBC::Periodic;
253 using SolverBCShearingPeriodic = typename SolverConfigBC::ShearingPeriodic;
254
255 // boundary condition selections
256 if (SolverBCFree *c = std::get_if<SolverBCFree>(&cfg.config)) {
257 // do nothing
258 } else if (SolverBCPeriodic *c = std::get_if<SolverBCPeriodic>(&cfg.config)) {
259 dump.table_header_fort_real.add("xmin", bmin.x());
260 dump.table_header_fort_real.add("xmax", bmax.x());
261 dump.table_header_fort_real.add("ymin", bmin.y());
262 dump.table_header_fort_real.add("ymax", bmax.y());
263 dump.table_header_fort_real.add("zmin", bmin.z());
264 dump.table_header_fort_real.add("zmax", bmax.z());
265 } else if (
266 SolverBCShearingPeriodic *c = std::get_if<SolverBCShearingPeriodic>(&cfg.config)) {
267 std::string err_msg
268 = "Phantom does not support shearing periodic boundaries but your are "
269 "making a phantom dump with them, set bypass_error_check=True to ignore";
270
271 if (!bypass_error) {
272 throw std::runtime_error(err_msg);
273 } else {
274 logger::warn_ln("PhantomDump", err_msg);
275 }
276 }
277 }
278
283
285 template void write_shamrock_boundaries_in_phantom_dump<f32_3>(
286 BCConfig<f32_3> &cfg,
287 std::tuple<f32_3, f32_3> box_size,
288 PhantomDump &dump,
289 bool bypass_error);
291 template void write_shamrock_boundaries_in_phantom_dump<f64_3>(
292 BCConfig<f64_3> &cfg,
293 std::tuple<f64_3, f64_3> box_size,
294 PhantomDump &dump,
295 bool bypass_error);
296} // namespace shammodels::sph
void eos2_write(PhantomDump &dump, const f64 &gamma)
Write the EOS2 to the phantom dump.
void eos1_write(PhantomDump &dump, const f64 &cs)
Write the EOS1 to the phantom dump.
void eos2_load(const PhantomDump &dump, f64 &gamma)
Load the EOS2 from the phantom dump.
void eos13_write(PhantomDump &dump, const f64 &cs0, const f64 &q, const f64 &r0)
Write the EOS13 to the phantom dump.
void eos3_load(const PhantomDump &dump, f64 &cs0, f64 &q, f64 &r0)
Load the EOS3 from the phantom dump.
void eos13_load(const PhantomDump &dump, f64 &cs0, f64 &q, f64 &r0)
Load the EOS13 from the phantom dump.
void eos14_write(PhantomDump &dump, const f64 &cs0, const f64 &q, const f64 &r0)
Write the EOS14 to the phantom dump.
void eos14_load(const PhantomDump &dump, f64 &cs0, f64 &q, f64 &r0)
Load the EOS14 from the phantom dump.
void eos3_write(PhantomDump &dump, const f64 &cs0, const f64 &q, const f64 &r0)
Write the EOS3 to the phantom dump.
void eos1_load(const PhantomDump &dump, f64 &cs)
Load the EOS1 from the phantom dump.
void eos6_load(const PhantomDump &dump, f64 &cs0, f64 &q, f64 &r0, i32 &isink)
Load the EOS6 from the phantom dump.
double f64
Alias for double.
std::int64_t i64
64 bit integer
std::int32_t i32
32 bit integer
A unit system, defined by the SI value of its base units.
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
void raw_ln(Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:89
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
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
void set_isothermal(Tscal cs)
Set the EOS configuration to an isothermal equation of state.
Definition EOSConfig.hpp:98
Variant config
Current EOS configuration.
Definition EOSConfig.hpp:91
shamphys::EOS_Config_Isothermal< Tscal > Isothermal
Isothermal equation of state configuration.
Definition EOSConfig.hpp:58
shamphys::EOS_Config_LocallyIsothermalDisc_Farris2014< Tscal > LocallyIsothermalFA2014
Locally isothermal equation of state configuration from Lodato Price 2007.
Definition EOSConfig.hpp:67
void set_adiabatic(Tscal gamma)
Set the EOS configuration to an adiabatic equation of state.
shamphys::EOS_Config_LocallyIsothermal_LP07< Tscal > LocallyIsothermalLP07
Locally isothermal equation of state configuration from Lodato Price 2007.
Definition EOSConfig.hpp:64
shamphys::EOS_Config_LocallyIsothermalDisc_ExtendedFarris2014< Tscal > LocallyIsothermalFA2014Extended
Locally isothermal equation of state configuration from Farris 2014 extended to q !...
Definition EOSConfig.hpp:70
void set_locally_isothermalLP07(Tscal cs0, Tscal q, Tscal r0)
Set the EOS configuration to a locally isothermal equation of state (Lodato Price 2007).
shamphys::EOS_Config_Adiabatic< Tscal > Adiabatic
Adiabatic equation of state configuration.
Definition EOSConfig.hpp:52
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.
PhantomDumpTableHeader< f64 > table_header_f64
Table header for 64-bit floating-point data.