def analytic_solution(gamma, npts): positions, regions, values = sod.solve(left_state=(8. / gamma, 8, 0), right_state=(1, 1, 0.), geometry=(-0.5, 0.5, 0.), t=0.2, gamma=gamma, npts=npts) return values
def analyticSolution(*args, **kwargs): """ A wrapper for sod.solve(). Requires the sod-shocktube package, available at https://github.com/ibackus/sod-shocktube See sod.solve() for documentation """ try: import sod except ImportError: print "sod-shocktube package must be installed. It can be found at"\ " https://github.com/ibackus/sod-shocktube" raise positions, regions, sol = sod.solve(*args, **kwargs) return positions, regions, sol
t = 0.007 Rs = 287.058 left_state = (1e5, 348.432, Rs, 0) right_state = (1e4, 278.746, Rs, 0) geometry = (-5, 5, 0) # left_state and right_state set pressure, density and u (velocity) # geometry sets left boundary on 0., right boundary on 1 and initial # position of the shock xi on 0.5 # t is the time evolution for which positions and states in tube should be # calculated # gamma denotes specific heat # note that gamma and npts are default parameters (1.4 and 500) in solve # function positions, regions, values = sod.solve(left_state=left_state, \ right_state=right_state, geometry=geometry, t=t, gamma=gamma, npts=npts, dustFrac=dustFrac) # Finally, let's plot solutions xex = values['x'] pex = values['p'] rho = values['rho'] uex = values['u'] Tex = pex / (rho * Rs) xnum, Tnum, unum, pnum= \ np.loadtxt("postProcessing/singleGraph/0.007/data_T_mag(U)_p.xy" \ , dtype=np.float, usecols=(0,1,2,3), unpack=True) ''' # Printing positions print('Positions:')
elif towatch == "energy": for i in range(Ny): uy = data['rhou_y'][i][i] / data['rho'][i][i] ux = data['rhou_x'][i][i] / data['rho'][i][i] Ek = (uy**2 + ux**2) / 2.0 qty[i] = data['rhoE'][i][i] / data['rho'][i][i] - Ek elif towatch == "rho": for i in range(Ny): qty[i] = data['rho'][i][i] if args.solver == 'sod': sys.path.insert(1, os.path.join(sys.path[0], 'sod')) from sod import solve positions, regions, values = solve(left_state=(1, 1, 0), right_state=(0.1, 0.125, 0.), geometry=(0., 1., 0.5), t=0.2, gamma=1.4, npts=Npoints) if args.solver == 'sedov': sys.path.insert(1, os.path.join(sys.path[0], 'sedov')) from sedov import solve if Ny == 1: values = solve(t=1.0, gamma=1.4, xpos=x, ndim=1) if Nx and Ny > 1: values = solve(t=1.0, gamma=1.4, xpos=x, ndim=2) if args.solver == 'noh': sys.path.insert(1, os.path.join(sys.path[0], 'noh')) from noh import solve if Nx == 1:
x = data[:, 0] rho = data[:, 1] u = data[:, 2] p = data[:, 3] mach = data[:, 4] figwidth = 15 figheight = 10 #LOAD ANALYTICAL DATA gamma = 1.4 npts = 1000 pleft = 1. / (gamma - 1) pright = 0.125 / (gamma - 1.) positions, regions, values = sod.solve(left_state=(pleft, 1, 0), right_state=(pright, 0.125, 0.), geometry=(-10., 10., 0.0), t=2.00, gamma=gamma, npts=npts) p_ana = values['p'] rho_ana = values['rho'] u_ana = values['u'] speedsound_ana = np.sqrt((gamma * p_ana) / (rho_ana)) mach_ana = u_ana / speedsound_ana #PLOTTING plt.subplots(figsize=(figwidth, figheight)) plt.subplot(4, 1, 1) plt.title("DENSITY", fontsize=20) plt.plot(x, rho, '-xb', label='Numerical') plt.plot(x, rho_ana, '-r', label='Analytical') plt.xticks([], [])
# try: scheme = f"{ds.attrs['scheme_flux_solver']}({ds.attrs['scheme_spatial_reconstruction']} {ds.attrs['scheme_limiter']})" # except Exception: # scheme = None t = 0.2 actual_time = ds.density.sel(time=t, method="nearest").time.data gamma = 1.4 npts = 500 # exact results positions, regions, values = sod.solve( left_state=(1, 1, 0), right_state=(0.1, 0.125, 0.0), geometry=(0.0, 1.0, 0.5), t=actual_time, gamma=gamma, npts=npts, ) p = values["p"] rho = values["rho"] u = values["u"] plt.figure(figsize=(12, 6)) ds.density.sel(time=t, method="nearest").plot(x="x", label="CATO Density") plt.plot(values["x"], rho, label="Exact Density") ds.x_velocity.sel(time=t, method="nearest").plot(x="x", label="CATO Velocity") plt.plot(values["x"], u, label="Exact Velocity")
left_state = (1, 1, 0) right_state = (0.1, 0.125, 0.0) # left_state and right_state set pressure, density and u (velocity) # geometry sets left boundary on 0., right boundary on 1 and initial # position of the shock xi on 0.5 # t is the time evolution for which positions and states in tube should be # calculated # gamma denotes specific heat # note that gamma and npts are default parameters (1.4 and 500) in solve # function positions, regions, values = sod.solve( left_state=left_state, right_state=right_state, geometry=(0.0, 1.0, 0.5), t=t, gamma=gamma, npts=npts, dustFrac=dustFrac, ) # Printing positions print("Positions:") for desc, vals in positions.items(): print("{0:10} : {1}".format(desc, vals)) # Printing p, rho and u for regions print("Regions:") for region, vals in sorted(regions.items()): print("{0:10} : {1}".format(region, vals)) # Finally, let's plot the solutions
cfl=0.5, integrator="rk2", flux="hllc", print_step=100, order=1, animation=False) rho, rhou, rhoE, rhoY = eqn.get_solution() rho, u, p, Y = eqn.get_solution_primvars() plt.plot(eqn.xc, rho, 'r--', lw=1, label="Density") plt.plot(eqn.xc, u, 'g--', lw=1, label="Velocity") plt.plot(eqn.xc, p, 'b--', lw=1, label="Pressure") if eqn.nscalar > 0: plt.plot(eqn.xc, rhoY, 'c--', lw=1, label="Y") # #plt.plot(eqn.xc, rhou, 'x-', lw=1) # #plt.plot(eqn.xc, rhoE, 'x-', lw=1) positions, regions, values = sod.solve(left_state=(1, 1, 0), right_state=(0.1, 0.125, 0.), geometry=(-0.5, 0.5, 0.0), t=tf, gamma=constants.gamma, npts=101) plt.plot(values['x'], values['rho'], 'r-', lw=2, label="Density") plt.plot(values['x'], values['u'], 'g-', lw=2, label="Velocity") plt.plot(values['x'], values['p'], 'b-', lw=2, label="Pressure") plt.show()
def compute(data_path, qty_name_list, solver): """ Compute L^2 error between a solution in path and exact Sod solution 1D. """ uid_to_coords = dict() start = timer() Nx, Ny, dx, dy = read_converter_1D(data_path, uid_to_coords) end = timer() print("Time to load coordinates converter of %s elements" % (Nx * Ny), "%s second(s)" % int((end - start))) # Call solver. #in values: ['energy', 'p', 'u', 'rho', 'rho_total', 'x'] start = timer() if solver == 'sod': if Nx != 1 and Ny != 1: return None from sod import solve positions, regions, values = solve(left_state=(1, 1, 0), right_state=(0.1, 0.125, 0.), geometry=(0., 1., 0.5), t=0.2, gamma=1.4, npts=Nx) if solver == 'sedov': from sedov import solve x = np.linspace(0., Nx * float(dx) * np.sqrt(2), Nx) if Ny==1: values = solve(t=1.0, gamma=1.4, xpos=x, ndim=1) if Nx and Ny>1: values = solve(t=1.0, gamma=1.4, xpos=x, ndim=2) if solver == 'noh': from noh import solve if Nx==1: values = solve(t=0.6, gamma=5./3., ndim=1,npts=Ny) if Ny==1: values = solve(t=0.6, gamma=5./3., ndim=1,npts=Nx) if Ny>1: values = solve(t=0.6, gamma=5./3., ndim=2,npts=Nx) # values['rho'] = 0*np.ones(Nx) end = timer() print("Time to compute exact solution of %s elements" % len(values['rho']), "%s second(s)" % int((end - start))) result = {'N' : Nx*Ny} # Must load rho in any case. start = timer() rho = np.zeros(Nx) counter = 0 with open(os.path.join(data_path, 'rho.dat'), 'r') as quantity_f: for line in quantity_f: if counter == Nx: break line_list = line.split(' ') pos = int(line_list[0]) if pos in uid_to_coords: coords = uid_to_coords[pos] rho[coords[0]] = float(line_list[1]) counter = counter + 1 end = timer() print("Time to load quantity rho of %s elements" % (Nx * Ny), "%s second(s)" % int((end - start))) if (solver == 'sedov' or solver == 'noh') and (Nx>1 and Ny>1): #2D Sedov or Noh result["rho"] = np.linalg.norm((rho - values["rho"]), ord=2) * min(float(dx),float(dy)) * np.sqrt(2) else: result["rho"] = np.linalg.norm((rho - values["rho"]), ord=2) * min(float(dx),float(dy)) ''' q_map = {"rhoE" : "energy", "rhou_x" : "u"} for q_name in q_map: # Load data start = timer() data = np.zeros((Nx, Ny)) read_quantity(data, data_path, uid_to_coords, q_name) if Ny == Nx: data_uy = np.zeros((Nx, Ny)) read_quantity(data_uy, data_path, uid_to_coords, "rhou_y") q = np.zeros(Nx) for i in range(Nx): uy = data_uy[i][i] / rho[i] ux = data[i][i] / rho[i] q[i] = np.sqrt((ux**2. + uy**2.) / 2.) else: q = data[:,0] / rho end = timer() print("Time to load quantity", q_name, "of %s elements" % (Nx * Ny), "%s second(s)" % int((end - start))) result[q_map[q_name]] = np.linalg.norm((q - values[q_map[q_name]]), ord=2) / Nx ''' return result