Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SodTube.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#include "shamphys/SodTube.hpp"
18#include "shammath/solve.hpp"
19#include <cmath>
20#include <iostream>
21
22f64 shamphys::SodTube::soundspeed(f64 P, f64 rho) { return std::sqrt(gamma * P / rho); };
23
24shamphys::SodTube::SodTube(f64 _gamma, f64 _rho_1, f64 _P_1, f64 _rho_5, f64 _P_5)
25 : gamma(_gamma), rho_1(_rho_1), P_1(_P_1), rho_5(_rho_5), P_5(_P_5) {
26 c_1 = soundspeed(P_1, rho_1);
27 c_5 = soundspeed(P_5, rho_5);
28
29 if (P_5 > P_1) {
30 throw "not correct";
31 }
32}
33
34f64 shamphys::SodTube::solve_P_4() {
35
36 auto shock_tube_function = [=](f64 P_4) {
37 f64 z = (P_4 / P_5 - 1.);
38
39 f64 gm1 = gamma - 1.0;
40 f64 gp1 = gamma + 1.0;
41 f64 g2 = 2.0 * gamma;
42
43 f64 fact1 = gm1 / g2 * (c_5 / c_1) * z / std::sqrt(1. + gp1 / g2 * z);
44 f64 fact = std::pow(1.0 - fact1, g2 / gm1);
45
46 return P_1 * fact - P_4;
47 };
48
49 auto df = [=](f64 P_4) {
50 return shammath::derivative_upwind<f64>(P_4, 1e-6, shock_tube_function);
51 };
52
53 return shammath::newton_rhaphson<f64>(shock_tube_function, df, 1e-6, P_1);
54}
55
56auto shamphys::SodTube::get_value(f64 t, f64 x) -> field_val {
57
58 f64 P_4 = solve_P_4();
59
60 f64 z = (P_4 / P_5 - 1.);
61
62 f64 gm1 = gamma - 1.0;
63 f64 gp1 = gamma + 1.0;
64 f64 gmfact1 = 0.5 * gm1 / gamma;
65 f64 gmfact2 = 0.5 * gp1 / gamma;
66
67 f64 fact = std::sqrt(1. + gmfact2 * z);
68
69 f64 vx_4 = c_5 * z / (gamma * fact);
70 f64 rho_4 = rho_5 * (1. + gmfact2 * z) / (1. + gmfact1 * z);
71
72 // shock speed
73
74 f64 w = c_5 * fact;
75
76 // compute values at foot of rarefaction
77 f64 P_3 = P_4;
78 f64 vx_3 = vx_4;
79 f64 rho_3 = rho_1 * std::pow(P_3 / P_1, 1. / gamma);
80
81 // compute positions
82 f64 c3 = soundspeed(P_3, rho_3);
83
84 f64 xhd, xft, xcd, xsh;
85
86 f64 xi = 0.;
87 xsh = xi + w * t;
88 xcd = xi + vx_3 * t;
89 xft = xi + (vx_3 - c3) * t;
90 xhd = xi - c_1 * t;
91
92 /*
93 1 : Head of rarefaction
94 2 : RAREFACTION
95 3 : Foot of rarefaction,
96 4 : Contact discontinuity,
97 5 : Shock,
98 */
99
100 f64 rho, p, vx;
101
102 if (x < xhd) {
103 rho = rho_1;
104 p = P_1;
105 vx = vx_1;
106 } else if (x < xft) {
107 vx = 2. / gp1 * (c_1 + (x - xi) / t);
108 f64 locfact = 1. - 0.5 * gm1 * vx / c_1;
109 rho = rho_1 * std::pow(locfact, 2. / gm1);
110 p = P_1 * std::pow(locfact, 2. * gamma / gm1);
111 } else if (x < xcd) {
112 rho = rho_3;
113 p = P_3;
114 vx = vx_3;
115
116 } else if (x < xsh) {
117 rho = rho_4;
118 p = P_4;
119 vx = vx_4;
120
121 } else {
122 rho = rho_5;
123 p = P_5;
124 vx = vx_5;
125 }
126
127 field_val ret;
128 ret.P = p;
129 ret.vx = vx;
130 ret.rho = rho;
131 return ret;
132
133 return {};
134}
constexpr const char * soundspeed
Sound speed c_s (derived from EOS)
double f64
Alias for double.