def dRootdx_1DNR(x, Cons): D = Cons["D"] tau = Cons["tau"] S2 = Cons["S2"] DYe = Cons["DYe"] Ye = DYe / D q = tau / D r = S2 / (D**2) W_guess_m2 = 1 - r / (x**2) W_guess = np.sqrt(1 / W_guess_m2) rho_guess = D / W_guess if rho_guess < (10**log_rho_array[0]): W_guess = D / (10**log_rho_array[0]) W_guess_m2 = 1 / (W_guess**2) eps_guess = -1 + (x * (1 - (W_guess**2)) / W_guess) + W_guess * (1 + q) T_guess, its = TInversionDriver(eps_guess, rho_guess, Ye) press_guess = EOS_pressFromPrim(T_guess, rho_guess, Ye) h_guess = 1 + eps_guess + press_guess / rho_guess dhdx = dhdx_1DNR(x, r, q, D, W_guess, T_guess, rho_guess, Ye, press_guess, eps_guess) dWdx = dWdx_1DNR(x, r) return 1 - W_guess * dhdx - h_guess * dWdx
def NR_TFromEpsRhoYe(eps_target,rho,Ye,max_its=100,rel_diff=1e-14,abs_diff=1e-15,T_guess=None,NR_fac=1.0): if T_guess == None: T_guess = 1.0 it=0 done=False T_current = T_guess eps_current = EOS_pressFromPrim(T_current,rho,Ye) T_record = [] T_record.append(T_current) success = False while done == False: depsdT = -EOS_dEdTFromPrim(T_current,rho,Ye) root = eps_target-eps_current delta_T = root/depsdT T_current = T_current - NR_fac*delta_T if T_current<10**log_T_array[0]: T_current = 10**log_T_array[0] elif T_current>10**log_T_array[-1]: T_current = 10**log_T_array[-1] eps_current = EOS_epsFromPrim(T_current,rho,Ye) T_record.append(T_current) it+=1 #if it>20 and it%20==0: #T_current = np.average(np.array(T_record)[it-10:it]) if it>=max_its: done = True success = False T_current = np.average(np.array(T_record)[-20:]) if np.abs(eps_target-eps_current)<abs_diff: done = True success = True #print("Absolute convergence in " + str(it) + " iterations") elif RelDiff(eps_target,eps_current)<rel_diff: done = True success = True #print("Relative convergence in " + str(it) + " iterations") return T_current, success, it
def GetWzTGuess(Cons_target, guesses): D = Cons_target["D"] DYe = Cons_target["DYe"] Ye_found = DYe / D if guesses == None: W_guess = 1.0 T_guess = 1.0 else: vsq_guess = guesses.vsq W_guess = 1 / np.sqrt(1 - vsq_guess) T_guess = guesses.T rho_guess = D / W_guess press_guess = EOS_pressFromPrim(T_guess, rho_guess, Ye_found) eps_guess = EOS_epsFromPrim(T_guess, rho_guess, Ye_found) h_guess = 1 + eps_guess + press_guess / rho_guess z_guess = rho_guess * h_guess * (W_guess**2) WzT_guess = {"W": W_guess, "z": z_guess, "T": T_guess} return WzT_guess
def GuessFromTargets(targets, min_frac=np.ones(3) * 0.98, max_frac=np.ones(3) * 1.02): guesses = Targets() rho_target = targets.rho rand = min_frac[0] + (max_frac[0] - min_frac[0]) * random() guesses.rho = rho_target * rand vsq_target = targets.vsq rand = min_frac[1] + (max_frac[1] - min_frac[1]) * random() guesses.vsq = vsq_target * rand guesses.W = 1 / (np.sqrt(1 - guesses.vsq)) T_target = targets.T rand = min_frac[2] + (max_frac[2] - min_frac[2]) * random() guesses.T = T_target * rand guesses.press = EOS_pressFromPrim(guesses.T, guesses.rho, targets.Ye) guesses.eps = EOS_epsFromPrim(guesses.T, guesses.rho, targets.Ye) guesses.h = 1 + guesses.eps + guesses.press / guesses.rho guesses.z = guesses.h * guesses.rho * (guesses.W**2) return guesses
def ConvergenceTest(targets, WzT, Cons_target, printTable=True): W_target = targets.W T_target = targets.T h_target = targets.h rho_target = targets.rho press_target = targets.press eps_target = targets.eps v1_target = targets.v1 v2_target = targets.v2 v3_target = targets.v3 Ye_found = Cons_target["DYe"] / Cons_target["D"] rho_found = Cons_target["D"] / WzT["W"] T_found = WzT["T"] v1_found = Cons_target["S_1"] / WzT["z"] v2_found = Cons_target["S_2"] / WzT["z"] v3_found = Cons_target["S_3"] / WzT["z"] press_found = EOS_pressFromPrim(T_found, rho_found, Ye_found) eps_found = EOS_epsFromPrim(T_found, rho_found, Ye_found) gubs, Cons_found = GetInitialGuess(rho=rho_found, velocity=np.array( [v1_found, v2_found, v3_found]), T=T_found, Ye=Ye_found) cons_errors = np.array([ RelDiff(Cons_target["D"], Cons_found["D"]), RelDiff(Cons_target["DYe"], Cons_found["DYe"]), RelDiff(Cons_target["tau"], Cons_found["tau"]), RelDiff(Cons_target["S2"], Cons_found["S2"]) ]) errors = np.array([ 2 * (rho_found - rho_target) / (rho_found + rho_target), 2 * (T_found - T_target) / (T_found + T_target), 2 * (v1_found - v1_target) / (v1_found + v1_target), 2 * (v2_found - v2_target) / (v2_found + v2_target), 2 * (v3_found - v3_target) / (v3_found + v3_target), 2 * (press_found - press_target) / (press_found + press_target), 2 * (eps_found - eps_target) / (eps_found + eps_target) ]) prim_err = np.sqrt(np.sum(errors**2) / len(errors)) con_err = np.max(np.abs(cons_errors)) if printTable: print("Primitive Vars") print("var target found relative_error") print("rho: " + table_string(rho_target) + table_string(rho_found) + table_string( np.abs(2 * (rho_found - rho_target) / (rho_found + rho_target)))) print("T: " + table_string(T_target) + table_string(T_found) + table_string( np.abs(2 * (T_found - T_target) / (T_found + T_target)))) print("v1: " + table_string(v1_target) + table_string(v1_found) + table_string( np.abs(2 * (v1_found - v1_target) / (v1_found + v1_target)))) print("v2: " + table_string(v2_target) + table_string(v2_found) + table_string( np.abs(2 * (v2_found - v2_target) / (v2_found + v2_target)))) print("v3: " + table_string(v3_target) + table_string(v3_found) + table_string( np.abs(2 * (v3_found - v3_target) / (v3_found + v3_target)))) print("press: " + table_string(press_target) + table_string(press_found) + table_string( np.abs(2 * (press_found - press_target) / (press_found + press_target)))) print("eps: " + table_string(eps_target) + table_string(eps_found) + table_string( np.abs(2 * (eps_found - eps_target) / (eps_found + eps_target)))) print("") print("Conservative Vars") print("var target found relative_error") print("D: " + table_string(Cons_target["D"]) + table_string(Cons_found["D"]) + table_string(cons_errors[0])) print("DYe: " + table_string(Cons_target["DYe"]) + table_string(Cons_found["DYe"]) + table_string(cons_errors[1])) print("tau: " + table_string(Cons_target["tau"]) + table_string(Cons_found["tau"]) + table_string(cons_errors[2])) print("S2: " + table_string(Cons_target["S2"]) + table_string(Cons_found["S2"]) + table_string(cons_errors[3])) return prim_err, con_err
def GetInitialGuess(rho=None, velocity=[], T=None, Ye=None): targets = Targets() if rho: targets.rho = rho else: ### Density ### log_rho_min = -11 log_rho_max = -3 alpha = 2 beta = 7 log_rho = log_rho_min + ((1 - betavariate(alpha, beta)) * (log_rho_max - log_rho_min)) targets.rho = 10**log_rho if velocity != []: targets.v1 = velocity[0] targets.v2 = velocity[1] targets.v3 = velocity[2] else: ### Velocity ### alpha = 2 beta = 3 speed = 0.5 * betavariate(alpha, beta) theta = 2 * np.pi * random() phi = np.arccos(-1.0 + 2 * random()) targets.v1 = speed * np.cos(theta) * np.sin(phi) targets.v2 = speed * np.sin(theta) * np.sin(phi) targets.v3 = speed * np.cos(phi) if T: targets.T = T else: ### Temperature ### log_T_min = log_T_array[0] + 0.5 log_T_max = log_T_array[-1] - 0.5 alpha = 2 beta = 7 log_T = log_T_min + (betavariate(alpha, beta) * (log_T_max - log_T_min)) targets.T = 10**log_T if Ye: targets.Ye = Ye else: ### Ye ### Ye_min = Ye_array[2] Ye_max = Ye_array[-3] alpha = 2 beta = 11 targets.Ye = Ye_min + (betavariate(alpha, beta) * (Ye_max - Ye_min)) ### Primitive ### targets.vsq = np.linalg.norm([targets.v1, targets.v2, targets.v3])**2 targets.W = 1 / (np.sqrt(1 - targets.vsq)) targets.press = EOS_pressFromPrim(targets.T, targets.rho, targets.Ye) targets.eps = EOS_epsFromPrim(targets.T, targets.rho, targets.Ye) targets.h = 1 + targets.eps + targets.press / targets.rho targets.z = targets.h * targets.rho * (targets.W**2) ##### Conserved ##### D = targets.rho * targets.W S_1 = (targets.rho * targets.h) * (targets.W**2) * targets.v1 S_2 = (targets.rho * targets.h) * (targets.W**2) * targets.v2 S_3 = (targets.rho * targets.h) * (targets.W**2) * targets.v3 tau = (targets.rho * targets.h) * (targets.W**2) - targets.press - D DYe = D * targets.Ye S2 = ((targets.rho * targets.h)**2) * (targets.W**4) * (targets.vsq) Cons_target = { "D": D, "S_1": S_1, "S_2": S_2, "S_3": S_3, "tau": tau, "DYe": DYe, "S2": S2 } return targets, Cons_target
return guesses ##### Primitives ##### rho_target = 2.8358299540575783e-09 v1_target = 0.1 v2_target = -0.2 v3_target = 0.15 Ye_target = 0.315 T_target = 4.166582513489608 vsq_target = np.linalg.norm([v1_target, v2_target, v3_target])**2 W_target = 1 / (np.sqrt(1 - vsq_target)) press_target = EOS_pressFromPrim(T_target, rho_target, Ye_target) eps_target = EOS_epsFromPrim(T_target, rho_target, Ye_target) h_target = 1 + eps_target + press_target / rho_target ##### Conserved ##### D = rho_target * W_target S_1 = (rho_target * h_target) * (W_target**2) * v1_target S_2 = (rho_target * h_target) * (W_target**2) * v2_target S_3 = (rho_target * h_target) * (W_target**2) * v3_target tau = (rho_target * h_target) * (W_target**2) - press_target - D DYe = D * Ye_target S2 = ((rho_target * h_target)**2) * (W_target**4) * (v2_target) Cons_target = { "D": D, "S_1": S_1,