4 ######################################################################################################################
5 # This file contains a class to solve for the exact solution of the Riemann Problem for the one dimensional Euler
6 # equations with stiffened gas equation of state
8 # Author: Michael Ndjinga
10 # Description : Translated from C++ package developped by Murray Cutforth
11 #######################################################################################################################
13 from math import pow, fabs, sqrt
15 def stiffenedgas_e (rho, p, gamma, pinf):
16 return (p+gamma*pinf)/(rho*(gamma-1));
18 def stiffenedgas_h (rho, p, gamma, pinf):
19 return gamma*(p+pinf)/(rho*(gamma-1));
21 def p_to_e_StiffenedGaz(p, rho, gamma, pinf):
22 e_field = (p + gamma*pinf) / (gamma - 1.) / rho
25 class exact_rs_stiffenedgas :
27 def __init__(self, gamma_L, gamma_R, pinf_L, pinf_R, tol=1.e-6, max_iter=100):
29 self.MAX_NB_ITER = max_iter
31 self.gamma_L = gamma_L
32 self.gamma_R = gamma_R
50 # Functions used to generate exact solutions to Riemann problems
52 def solve_RP (self, W_L, W_R):
53 assert len(W_L) == 3, "Left state should have three components (rho, u p)"
54 assert len(W_R) == 3, "Right state should have three components (rho, u p)"
55 assert W_L[0] >= 0.0, "Left density should be positive"
56 assert W_R[0] >= 0.0, "Right density should be positive"
57 # assert W_L[2] >= 0.0 # Since stiffened gases will often exhibit p<0..
58 # assert W_R[2] >= 0.0 #
61 print("Solving Riemann problem for left state W_L=", W_L, ", and right state W_R=",W_R)
65 self.P_STAR = self.find_p_star_newtonraphson(W_L[0], W_L[1], W_L[2], W_R[0], W_R[1], W_R[2])
70 self.S_STAR = 0.5*(W_L[1]+W_R[1]) + 0.5*(self.f(self.P_STAR,W_R[0],W_R[2],self.gamma_R,self.pinf_R) - self.f(self.P_STAR,W_L[0],W_L[2],self.gamma_L,self.pinf_L))
73 # Solution now depends on character of 1st and 3rd waves
75 if (self.P_STAR > W_L[2]):
78 self.rho_star_L = W_L[0]*((2.0*self.gamma_L*self.pinf_L + (self.gamma_L+1.0)*self.P_STAR + (self.gamma_L-1.0)*W_L[2])/(2.0*(W_L[2] + self.gamma_L*self.pinf_L) + (self.gamma_L-1.0)*self.P_STAR + (self.gamma_L-1.0)*W_L[2]))
79 self.S_L = W_L[1] - (self.Q_K(self.P_STAR,W_L[0],W_L[2],self.gamma_L,self.pinf_L)/W_L[0])
83 self.rho_star_L = W_L[0]*pow((self.P_STAR + self.pinf_L)/(W_L[2] + self.pinf_L), 1.0/self.gamma_L)
85 a_L = self.a(W_L[0], W_L[2], self.gamma_L, self.pinf_L)
86 a_star_L = a_L*pow((self.P_STAR + self.pinf_L)/(W_L[2] + self.pinf_L), (self.gamma_L-1.0)/(2.0*self.gamma_L))
88 self.S_HL = W_L[1] - a_L
89 self.S_TL = self.S_STAR - a_star_L
91 if (self.P_STAR > W_R[2]):
94 self.rho_star_R = W_R[0]*((2.0*self.gamma_R*self.pinf_R + (self.gamma_R+1.0)*self.P_STAR + (self.gamma_R-1.0)*W_R[2])/(2.0*(W_R[2] + self.gamma_R*self.pinf_R) + (self.gamma_R-1.0)*self.P_STAR + (self.gamma_R-1.0)*W_R[2]))
96 self.S_R = W_R[1] + (self.Q_K(self.P_STAR,W_R[0],W_R[2],self.gamma_R,self.pinf_R)/W_R[0])
100 self.rho_star_R = W_R[0]*pow((self.P_STAR + self.pinf_R)/(W_R[2] + self.pinf_R), 1.0/self.gamma_R)
102 a_R = self.a(W_R[0],W_R[2],self.gamma_R, self.pinf_R)
103 a_star_R = a_R*pow((self.P_STAR + self.pinf_R)/(W_R[2] + self.pinf_R), (self.gamma_R-1.0)/(2.0*self.gamma_R))
105 self.S_HR = W_R[1] + a_R
106 self.S_TR = self.S_STAR + a_star_R
108 def sample_solution (self, W_L, W_R, S):
111 # Find appropriate part of solution and return primitives
113 if (S < self.S_STAR):
114 # To the left of the contact
116 if (self.P_STAR > W_L[2]):
122 W[0] = self.rho_star_L
132 W[0] = self.rho_star_L
136 self.set_left_rarefaction_fan_state(W_L, S, W)
138 # To the right of the contact
140 if (self.P_STAR > W_R[2]):
146 W[0] = self.rho_star_R
156 W[0] = self.rho_star_R
160 self.set_right_rarefaction_fan_state(W_R, S, W)
164 # Functions used to solve for p_star iteratively
166 def find_p_star_newtonraphson (self, rho_L, u_L, p_L, rho_R, u_R, p_R ):
168 # First we set the initial guess for p_star using a simple mean-value approximation
170 p_star_next = 0.5*(p_L+p_R)
174 # Now use the Newton-Raphson algorithm
176 while True:#conversion of do ... while by while True... if (...) break
179 p_star_next = p_star - self.total_pressure_function(p_star,rho_L,u_L,p_L,rho_R,u_R,p_R)/self.total_pressure_function_deriv(p_star,rho_L,p_L,rho_R,p_R)
181 p_star_next = max(p_star_next, self.TOL)
185 if not ((fabs(p_star_next - p_star)/(0.5*(p_star+p_star_next)) > self.TOL) and n < self.MAX_NB_ITER):
188 if (n == self.MAX_NB_ITER):
189 raise ValueError("!!!!!!!!!!Newton algorithm did not converge. Increase tolerance or maximum number of time steps. Current values : tol=" + str(self.TOL) + ", max_iter=" + str(self.MAX_NB_ITER) )
190 #p_star_next = 0.5*(p_L+p_R)
194 def total_pressure_function (self, p_star, rho_L, u_L, p_L, rho_R, u_R, p_R ):
196 return self.f(p_star, rho_L, p_L, self.gamma_L, self.pinf_L) + self.f(p_star, rho_R, p_R, self.gamma_R, self.pinf_R) + u_R - u_L
198 def total_pressure_function_deriv (self, p_star, rho_L, p_L, rho_R, p_R ):
200 return self.f_deriv (p_star, rho_L, p_L, self.gamma_L, self.pinf_L) + self.f_deriv (p_star, rho_R, p_R, self.gamma_R, self.pinf_R)
203 def f (self, p_star, rho, p, gamma, pinf):
206 return (p_star - p)/self.Q_K(p_star, rho, p, gamma, pinf)
210 return (2.0*self.a(rho,p,gamma,pinf)/(gamma-1.0))*(pow((p_star + pinf)/(p + pinf), (gamma-1.0)/(2.0*gamma)) - 1.0)
213 def f_deriv (self, p_star, rho, p, gamma, pinf):
214 A = 2.0/((gamma+1.0)*rho)
215 B = (p+pinf)*(gamma-1.0)/(gamma+1.0)
219 return sqrt(A/(B+p_star+pinf))*(1.0 - ((p_star-p)/(2.0*(B+p_star+pinf))))
223 return (1.0/(rho*self.a(rho,p,gamma,pinf)))*pow((p_star+pinf)/(p+pinf), -(gamma+1.0)/(2.0*gamma))
227 # Functions to find the state inside a rarefaction fan
229 def set_left_rarefaction_fan_state (self, W_L, S, W):
230 a_L = self.a(W_L[0],W_L[2],self.gamma_L,self.pinf_L)
231 W[0] = W_L[0]*pow((2.0/(self.gamma_L+1.0)) + ((self.gamma_L-1.0)/(a_L*(self.gamma_L+1.0)))*(W_L[1] - S), 2.0/(self.gamma_L - 1.0))
232 W[1] = (2.0/(self.gamma_L+1.0))*(a_L + S + ((self.gamma_L-1.0)/2.0)*W_L[1])
233 W[2] = (W_L[2] + self.pinf_L)*pow((2.0/(self.gamma_L+1.0)) + ((self.gamma_L-1.0)/(a_L*(self.gamma_L+1.0)))*(W_L[1] - S), (2.0*self.gamma_L)/(self.gamma_L-1.0)) - self.pinf_L
235 def set_right_rarefaction_fan_state (self, W_R, S, W):
236 a_R = self.a(W_R[0],W_R[2],self.gamma_R,self.pinf_R)
237 W[0] = W_R[0]*pow((2.0/(self.gamma_R+1.0)) - ((self.gamma_R-1.0)/(a_R*(self.gamma_R+1.0)))*(W_R[1] - S), 2.0/(self.gamma_R - 1.0))
238 W[1] = (2.0/(self.gamma_R+1.0))*(- a_R + S + ((self.gamma_R-1.0)/2.0)*W_R[1])
239 W[2] = (W_R[2] + self.pinf_R)*pow((2.0/(self.gamma_R+1.0)) - ((self.gamma_R-1.0)/(a_R*(self.gamma_R+1.0)))*(W_R[1] - S), (2.0*self.gamma_R)/(self.gamma_R-1.0)) - self.pinf_R
245 def Q_K (self, p_star, rho, p, gamma, pinf):
246 A = 2.0/((gamma+1.0)*rho)
247 B = (p+pinf)*(gamma-1.0)/(gamma+1.0)
248 return sqrt((p_star+pinf+B)/A)
252 # Equation of state functions
254 def a (self, rho, p, gamma, pinf):#sound speed
255 return sqrt(gamma*((p+pinf)/rho))
257 #Determine the solution value at position x and time t
258 def rho_u_p_solution (initialLeftState, initialRightState, x, t, gamma, pinf, offset=0):
259 RS = exact_rs_stiffenedgas(gamma, gamma, pinf, pinf);
260 RS.solve_RP(initialLeftState, initialRightState);
261 return RS.sample_solution(initialLeftState, initialRightState, (x - offset)/t);