def wcblast(use_petsc=False, iplot=False, htmlplot=False, outdir='./_output', solver_type='classic'): """ Solve the Euler equations of compressible fluid dynamics. This example involves a pair of interacting shock waves. The conserved quantities are density, momentum density, and total energy density. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() import riemann solver.rp = riemann.rp1_euler_with_efix solver.num_waves = 3 solver.bc_lower[0] = pyclaw.BC.wall solver.bc_upper[0] = pyclaw.BC.wall # Initialize domain mx = 500 x = pyclaw.Dimension('x', 0.0, 1.0, mx) domain = pyclaw.Domain([x]) num_eqn = 3 state = pyclaw.State(domain, num_eqn) state.problem_data['gamma'] = gamma state.problem_data['gamma1'] = gamma1 state.q[0, :] = 1. state.q[1, :] = 0. x = state.grid.x.centers state.q[2, :] = ((x < 0.1) * 1.e3 + (0.1 <= x) * (x < 0.9) * 1.e-2 + (0.9 <= x) * 1.e2) / gamma1 solver.limiters = 4 claw = pyclaw.Controller() claw.tfinal = 0.038 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.num_output_times = 10 claw.outdir = outdir # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir) return claw.solution.q
def burgers(use_petsc=0,kernel_language='Fortran',iplot=0,htmlplot=0,outdir='./_output',solver_type='classic'): """ Example python script for solving the 1d Burgers equation. """ import numpy as np if use_petsc: import petclaw as pyclaw else: import pyclaw #=========================================================================== # Setup solver and solver parameters #=========================================================================== if solver_type=='sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() solver.limiters = pyclaw.limiters.tvd.vanleer solver.kernel_language = kernel_language if kernel_language=='Python': solver.set_riemann_solver('burgers') elif kernel_language=='Fortran': import riemann solver.rp = riemann.rp1_burgers solver.num_waves = 1 solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic #=========================================================================== # Initialize domain and then initialize the solution associated to the domain #=========================================================================== x = pyclaw.Dimension('x',0.0,1.0,500) domain = pyclaw.Domain(x) num_eqn = 1 state = pyclaw.State(domain,num_eqn) grid = state.grid xc=grid.x.centers state.q[0,:] = np.sin(np.pi*2*xc) + 0.50 state.problem_data['efix']=True #=========================================================================== # Setup controller and controller parameters. Then solve the problem #=========================================================================== claw = pyclaw.Controller() claw.tfinal =0.5 claw.solution = pyclaw.Solution(state,domain) claw.solver = solver claw.outdir = outdir status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def kpp(use_petsc=False, iplot=False, htmlplot=False, outdir='./_output', solver_type='classic'): """ Example python script for solving the 2d KPP equations. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() else: solver = pyclaw.ClawSolver2D() import riemann solver.rp = riemann.rp2_kpp solver.num_waves = 1 solver.bc_lower[0] = pyclaw.BC.extrap solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.extrap solver.bc_upper[1] = pyclaw.BC.extrap # Initialize domain mx = 200 my = 200 x = pyclaw.Dimension('x', -2.0, 2.0, mx) y = pyclaw.Dimension('y', -2.0, 2.0, my) domain = pyclaw.Domain([x, y]) num_eqn = 1 state = pyclaw.State(domain, num_eqn) qinit(state) solver.dimensional_split = 1 solver.cfl_max = 1.0 solver.cfl_desired = 0.9 solver.num_waves = 2 solver.limiters = pyclaw.limiters.tvd.minmod claw = pyclaw.Controller() claw.tfinal = 1.0 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.num_output_times = 10 # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def advection(kernel_language='Python', iplot=False, htmlplot=False, use_petsc=False, solver_type='classic', outdir='./_output'): """ Example python script for solving the 1d advection equation. """ import numpy as np if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() solver.kernel_language = kernel_language from riemann import rp_advection solver.num_waves = rp_advection.num_waves if solver.kernel_language == 'Python': solver.rp = rp_advection.rp_advection_1d else: import riemann solver.rp = riemann.rp1_advection solver.bc_lower[0] = 2 solver.bc_upper[0] = 2 x = pyclaw.Dimension('x', 0.0, 1.0, 100) domain = pyclaw.Domain(x) num_eqn = 1 state = pyclaw.State(domain, num_eqn) state.problem_data['u'] = 1. grid = state.grid xc = grid.x.centers beta = 100 gamma = 0 x0 = 0.75 state.q[0, :] = np.exp(-beta * (xc - x0)**2) * np.cos(gamma * (xc - x0)) claw = pyclaw.Controller() claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir claw.tfinal = 1.0 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def vc_advection(use_petsc=False, solver_type='classic', kernel_language='Python', iplot=False, htmlplot=False, outdir='./_output'): if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() solver.kernel_language = kernel_language from riemann import rp_vc_advection solver.num_waves = rp_vc_advection.num_waves if solver.kernel_language == 'Python': solver.rp = rp_vc_advection.rp_vc_advection_1d solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = 2 solver.bc_upper[0] = 2 solver.aux_bc_lower[0] = 2 solver.aux_bc_upper[0] = 2 xlower = 0.0 xupper = 1.0 mx = 100 x = pyclaw.Dimension('x', xlower, xupper, mx) domain = pyclaw.Domain(x) num_aux = 1 num_eqn = 1 state = pyclaw.State(domain, num_eqn, num_aux) qinit(state) auxinit(state) claw = pyclaw.Controller() claw.outdir = outdir claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.tfinal = 1.0 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def vc_advection(use_petsc=False,solver_type='classic',kernel_language='Python',iplot=False,htmlplot=False,outdir='./_output'): if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type=='sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() import riemann solver.num_waves = riemann.rp_vc_advection.num_waves solver.kernel_language = kernel_language if solver.kernel_language=='Python': solver.rp = riemann.rp_vc_advection.rp_vc_advection_1d elif solver.kernel_language=='Fortran': raise NotImplementedError('The 1D variable coefficient advection Riemann solver has not yet been ported.') solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = 2 solver.bc_upper[0] = 2 solver.aux_bc_lower[0] = 2 solver.aux_bc_upper[0] = 2 xlower=0.0; xupper=1.0; mx=100 x = pyclaw.Dimension('x',xlower,xupper,mx) domain = pyclaw.Domain(x) num_aux=1 num_eqn = 1 state = pyclaw.State(domain,num_eqn,num_aux) qinit(state) auxinit(state) claw = pyclaw.Controller() claw.outdir = outdir claw.solution = pyclaw.Solution(state,domain) claw.solver = solver claw.tfinal = 1.0 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def read_petsc(solution, frame, path='./', file_prefix='claw', read_aux=False, options={}): r""" Read in pickles and PETSc data files representing the solution :Input: - *solution* - (:class:`~pyclaw.solution.Solution`) Solution object to read the data into. - *frame* - (int) Frame number to be read in - *path* - (string) Path to the current directory of the file - *file_prefix* - (string) Prefix of the files to be read in. ``default = 'fort'`` - *read_aux* (bool) Whether or not an auxiliary file will try to be read in. ``default = False`` - *options* - (dict) Optional argument dictionary, see `PETScIO Option Table`_ .. _`PETScIO Option Table`: format : one of 'ascii' or 'binary' """ # Option parsing option_defaults = {'format': 'binary'} for k in option_defaults.iterkeys(): if options.has_key(k): pass else: options[k] = option_defaults[k] pickle_filename = os.path.join( path, '%s.pkl' % file_prefix) + str(frame).zfill(4) viewer_filename = os.path.join( path, '%s.ptc' % file_prefix) + str(frame).zfill(4) aux_viewer_filename = os.path.join(path, '%s_aux.ptc' % file_prefix) if frame < 0: # Don't construct file names with negative frameno values. raise IOError("Frame " + str(frame) + " does not exist ***") pickle_file = open(pickle_filename, 'rb') # this dictionary is mostly holding debugging information, only nstates is needed # most of this information is explicitly saved in the individual patches value_dict = pickle.load(pickle_file) nstates = value_dict['nstates'] num_dim = value_dict['num_dim'] num_aux = value_dict['num_aux'] num_eqn = value_dict['num_eqn'] # now set up the PETSc viewer if options['format'] == 'ascii': viewer = PETSc.Viewer().createASCII(viewer_filename, PETSc.Viewer.Mode.READ) if read_aux: aux_viewer = PETSc.Viewer().createASCII(aux_viewer_filename, PETSc.Viewer.Mode.READ) elif options['format'] == 'binary': if hasattr(PETSc.Viewer, 'createMPIIO'): viewer = PETSc.Viewer().createMPIIO(viewer_filename, PETSc.Viewer.Mode.READ) else: viewer = PETSc.Viewer().createBinary(viewer_filename, PETSc.Viewer.Mode.READ) if read_aux: if os.path.exists(aux_viewer_filename): if hasattr(PETSc.Viewer, 'createMPIIO'): aux_viewer = PETSc.Viewer().createMPIIO( aux_viewer_filename, PETSc.Viewer.Mode.READ) else: aux_viewer = PETSc.Viewer().createBinary( aux_viewer_filename, PETSc.Viewer.Mode.READ) else: from warnings import warn aux_file_path = os.path.join(path, aux_viewer_filename) warn('read_aux=True but aux file %s does not exist' % aux_file_path) read_aux = False else: raise IOError('format type %s not supported' % options['format']) patches = [] for m in xrange(nstates): patch_dict = pickle.load(pickle_file) level = patch_dict['level'] names = patch_dict['names'] lower = patch_dict['lower'] n = patch_dict['num_cells'] d = patch_dict['delta'] import petclaw dimensions = [] for i in xrange(num_dim): dimensions.append( #pyclaw.solution.Dimension(names[i],lower[i],lower[i] + n[i]*d[i],n[i])) petclaw.Dimension(names[i], lower[i], lower[i] + n[i] * d[i], n[i])) #patch = pyclaw.solution.Patch(dimensions) patch = petclaw.Patch(dimensions) patch.level = level #state = pyclaw.state.State(patch) state = petclaw.State(patch, num_eqn, num_aux) ## state.t = value_dict['t'] state.problem_data = value_dict['problem_data'] # DA View/Load is broken in Petsc-3.1.8, we can load/view the DA if needed in petsc-3.2 # state.q_da.load(viewer) state.gqVec.load(viewer) if read_aux: state.gauxVec.load(aux_viewer) solution.states.append(state) patches.append(state.patch) solution.domain = petclaw.geometry.Domain(patches) pickle_file.close() viewer.destroy() if read_aux: aux_viewer.destroy()
def acoustics(use_petsc=False,kernel_language='Fortran',solver_type='classic',iplot=False,htmlplot=False,outdir='./_output',weno_order=5): """ This example solves the 1-dimensional acoustics equations in a homogeneous medium. """ from numpy import sqrt, exp, cos from riemann import rp1_acoustics #================================================================= # Import the appropriate classes, depending on the options passed #================================================================= if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type=='classic': solver = pyclaw.ClawSolver1D() elif solver_type=='sharpclaw': solver = pyclaw.SharpClawSolver1D() solver.weno_order=weno_order else: raise Exception('Unrecognized value of solver_type.') #======================================================================== # Instantiate the solver and define the system of equations to be solved #======================================================================== solver.kernel_language=kernel_language from riemann import rp_acoustics solver.num_waves=rp_acoustics.num_waves if kernel_language=='Python': solver.rp = rp_acoustics.rp_acoustics_1d else: from riemann import rp1_acoustics solver.rp = rp1_acoustics solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic #======================================================================== # Instantiate the domain and set the boundary conditions #======================================================================== x = pyclaw.Dimension('x',0.0,1.0,100) domain = pyclaw.Domain(x) num_eqn = 2 state = pyclaw.State(domain,num_eqn) #======================================================================== # Set problem-specific variables #======================================================================== rho = 1.0 bulk = 1.0 state.problem_data['rho']=rho state.problem_data['bulk']=bulk state.problem_data['zz']=sqrt(rho*bulk) state.problem_data['cc']=sqrt(bulk/rho) #======================================================================== # Set the initial condition #======================================================================== xc=domain.grid.x.centers beta=100; gamma=0; x0=0.75 state.q[0,:] = exp(-beta * (xc-x0)**2) * cos(gamma * (xc - x0)) state.q[1,:] = 0. #======================================================================== # Set up the controller object #======================================================================== claw = pyclaw.Controller() claw.solution = pyclaw.Solution(state,domain) claw.solver = solver claw.outdir = outdir claw.tfinal = 1.0 # Solve status = claw.run() # Plot results if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def advection2D(iplot=False, use_petsc=False, htmlplot=False, outdir='./_output', solver_type='classic'): """ Example python script for solving the 2d advection equation. """ #=========================================================================== # Import libraries #=========================================================================== if use_petsc: import petclaw as pyclaw else: import pyclaw #=========================================================================== # Setup solver and solver parameters #=========================================================================== if solver_type == 'classic': solver = pyclaw.ClawSolver2D() solver.dimensional_split = 1 solver.limiters = pyclaw.limiters.tvd.vanleer elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() import riemann solver.rp = riemann.rp2_advection solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic solver.bc_lower[1] = pyclaw.BC.periodic solver.bc_upper[1] = pyclaw.BC.periodic solver.num_waves = 1 solver.cfl_max = 1.0 solver.cfl_desired = 0.9 #=========================================================================== # Initialize domain, then initialize the solution associated to the domain and # finally initialize aux array #=========================================================================== # Domain: mx = 50 my = 50 x = pyclaw.Dimension('x', 0.0, 1.0, mx) y = pyclaw.Dimension('y', 0.0, 1.0, my) domain = pyclaw.Domain([x, y]) num_eqn = 1 state = pyclaw.State(domain, num_eqn) state.problem_data['u'] = 0.5 # Parameters (global auxiliary variables) state.problem_data['v'] = 1.0 # Initial solution # ================ qinit(state) # This function is defined above #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.tfinal = 2.0 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir #=========================================================================== # Solve the problem #=========================================================================== status = claw.run() #=========================================================================== # Plot results #=========================================================================== if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def psystem2D(use_petsc=False, solver_type='classic', iplot=False, htmlplot=False): """ Solve the p-system in 2D with variable coefficients """ if use_petsc: import petclaw as pyclaw else: import pyclaw #################################### ######### MAIN PARAMETERS ########## #################################### # Domain x_lower = 0.25 x_upper = 20.25 y_lower = 0.25 y_upper = 20.25 # cells per layer Ng = 10 mx = (x_upper - x_lower) * Ng my = (y_upper - y_lower) * Ng # Initial condition parameters A = 10. x0 = 0.25 # Center of initial perturbation y0 = 0.25 # Center of initial perturbation varx = 0.5 vary = 0.5 # Width of initial perturbation # Boundary conditions bc_x_lower = pyclaw.BC.wall bc_x_upper = pyclaw.BC.extrap bc_y_lower = pyclaw.BC.wall bc_y_upper = pyclaw.BC.extrap # Turning off 1st half of the domain. Useful in rect domains turnZero_half_2D = 0 #flag t_turnZero = 50 # Regarding time tfinal = 20.0 num_output_times = 10 t0 = 0.0 # restart options restart_from_frame = None solver = pyclaw.ClawSolver2D() #solver = pyclaw.SharpClawSolver2D() import riemann solver.rp = riemann.rp2_psystem solver.num_waves = 2 solver.limiters = pyclaw.limiters.tvd.superbee solver.bc_lower[0] = bc_x_lower solver.bc_upper[0] = bc_x_upper solver.bc_lower[1] = bc_y_lower solver.bc_upper[1] = bc_y_upper solver.aux_bc_lower[0] = bc_x_lower solver.aux_bc_upper[0] = bc_x_upper solver.aux_bc_lower[1] = bc_y_lower solver.aux_bc_upper[1] = bc_y_upper solver.fwave = True solver.cfl_max = 0.9 solver.cfl_desired = 0.8 solver.before_step = b4step solver.dimensional_split = False #controller claw = pyclaw.Controller() claw.tfinal = tfinal claw.solver = solver if restart_from_frame is not None: claw.solution = pyclaw.Solution(restart_from_frame, format='petsc', read_aux=False) claw.solution.state.mp = 1 grid = claw.solution.domain.grid claw.solution.state.aux = setaux(grid.x.centers, grid.y.centers) claw.num_output_times = num_output_times - restart_from_frame claw.start_frame = restart_from_frame else: #################################### #################################### #################################### #Creation of Domain x = pyclaw.Dimension('x', x_lower, x_upper, mx) y = pyclaw.Dimension('y', y_lower, y_upper, my) domain = pyclaw.Domain([x, y]) num_eqn = 3 num_aux = 4 state = pyclaw.State(domain, num_eqn, num_aux) state.mF = 3 state.t = t0 #Set global parameters state.problem_data = {} state.problem_data['turnZero_half_2D'] = turnZero_half_2D state.problem_data['t_turnZero'] = t_turnZero state.mp = 1 grid = state.grid state.aux = setaux(grid.x.centers, grid.y.centers) #Initial condition qinit(state, A, x0, y0, varx, vary) claw.solution = pyclaw.Solution(state, domain) claw.num_output_times = num_output_times #claw.p_function = p_function claw.compute_F = compute_F grid.add_gauges([[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]]) solver.compute_gauge_values = gauge_pfunction claw.write_aux_init = False #Solve status = claw.run() #strain=claw.frames[claw.num_output_times].state.gqVec.getArray().reshape([grid.num_cells[0],grid.num_cells[1],num_eqn])[:,:,0] #return strain if iplot: pyclaw.plot.interactive_plot() if htmlplot: pyclaw.plot.html_plot()
def shockbubble(use_petsc=False,iplot=False,htmlplot=False,outdir='./_output',solver_type='classic'): """ Solve the Euler equations of compressible fluid dynamics. This example involves a bubble of dense gas that is impacted by a shock. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type=='sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.dq_src=dq_Euler_radial else: solver = pyclaw.ClawSolver2D() solver.dim_split = 0 solver.order_trans = 2 solver.limiters = [4,4,4,4,2] solver.step_src=step_Euler_radial solver.mwaves = 5 solver.bc_lower[0]=pyclaw.BC.custom solver.bc_upper[0]=pyclaw.BC.outflow solver.bc_lower[1]=pyclaw.BC.reflecting solver.bc_upper[1]=pyclaw.BC.outflow #Aux variable in ghost cells doesn't matter solver.aux_bc_lower[0]=pyclaw.BC.outflow solver.aux_bc_upper[0]=pyclaw.BC.outflow solver.aux_bc_lower[1]=pyclaw.BC.outflow solver.aux_bc_upper[1]=pyclaw.BC.outflow # Initialize grid mx=320; my=80 x = pyclaw.Dimension('x',0.0,2.0,mx) y = pyclaw.Dimension('y',0.0,0.5,my) grid = pyclaw.Grid([x,y]) meqn = 5 maux=8 state = pyclaw.State(grid,meqn,maux) state.aux_global['gamma']= gamma state.aux_global['gamma1']= gamma1 qinit(state) auxinit(state) solver.user_bc_lower=shockbc claw = pyclaw.Controller() claw.tfinal = 0.75 claw.solution = pyclaw.Solution(state) claw.solver = solver claw.nout = 10 claw.outdir = outdir # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir) return claw.solution.q
def shallow2D(use_petsc=False, iplot=0, htmlplot=False, outdir='./_output', solver_type='classic'): #=========================================================================== # Import libraries #=========================================================================== import numpy as np if use_petsc: import petclaw as pyclaw else: import pyclaw #=========================================================================== # Setup solver and solver parameters #=========================================================================== if solver_type == 'classic': solver = pyclaw.ClawSolver2D() elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.num_waves = 3 solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = pyclaw.BC.extrap solver.bc_upper[0] = pyclaw.BC.wall solver.bc_lower[1] = pyclaw.BC.extrap solver.bc_upper[1] = pyclaw.BC.wall solver.dimensional_split = 1 #=========================================================================== # Initialize domain and state, then initialize the solution associated to the # state and finally initialize aux array #=========================================================================== # Domain: xlower = -2.5 xupper = 2.5 mx = 150 ylower = -2.5 yupper = 2.5 my = 150 x = pyclaw.Dimension('x', xlower, xupper, mx) y = pyclaw.Dimension('y', ylower, yupper, my) domain = pyclaw.Domain([x, y]) num_eqn = 3 # Number of equations state = pyclaw.State(domain, num_eqn) grav = 1.0 # Parameter (global auxiliary variable) state.problem_data['grav'] = grav # Initial solution # ================ # Riemann states of the dam break problem damRadius = 0.5 hl = 2. ul = 0. vl = 0. hr = 1. ur = 0. vr = 0. qinit(state, hl, ul, vl, hr, ur, vl, damRadius) # This function is defined above #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.tfinal = 2.5 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir claw.num_output_times = 10 #=========================================================================== # Solve the problem #=========================================================================== status = claw.run() #=========================================================================== # Plot results #=========================================================================== if iplot: plot.interactive_plot(outdir=outdir, format=claw.output_format) if htmlplot: plot.html_plot(outdir=outdir, format=claw.output_format)
def advection_annulus(use_petsc=False, iplot=0, htmlplot=False, outdir='./_output', solver_type='classic'): #=========================================================================== # Import libraries #=========================================================================== if use_petsc: import petclaw as pyclaw else: import pyclaw #=========================================================================== # Setup solver and solver parameters #=========================================================================== if solver_type == 'classic': solver = pyclaw.ClawSolver2D() elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.bc_lower[0] = pyclaw.BC.extrap solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.periodic solver.bc_upper[1] = pyclaw.BC.periodic solver.aux_bc_lower[0] = pyclaw.BC.custom solver.aux_bc_upper[0] = pyclaw.BC.custom solver.user_aux_bc_lower = velocities_lower solver.user_aux_bc_upper = velocities_upper solver.aux_bc_lower[1] = pyclaw.BC.periodic solver.aux_bc_upper[1] = pyclaw.BC.periodic solver.num_waves = 1 solver.dimensional_split = 0 solver.transverse_waves = 2 solver.order = 2 solver.dt_initial = 0.1 solver.cfl_max = 0.5 solver.cfl_desired = 0.2 solver.limiters = pyclaw.limiters.tvd.vanleer #=========================================================================== # Initialize domain and state, then initialize the solution associated to the # state and finally initialize aux array #=========================================================================== # Domain: xlower = 0.2 xupper = 1.0 mx = 40 ylower = 0.0 yupper = np.pi * 2.0 my = 120 x = pyclaw.Dimension('x', xlower, xupper, mx) y = pyclaw.Dimension('y', ylower, yupper, my) domain = pyclaw.Domain([x, y]) domain.grid.mapc2p = mapc2p_annulus # Override default_mapc2p function implemented in geometry.py # State: num_eqn = 1 # Number of equations state = pyclaw.State(domain, num_eqn) # Set initial solution # ==================== qinit(state, mx, my) # This function is defined above # Set auxiliary array # =================== state.aux = setaux(state, mx, my) # This function is defined above state.index_capa = 2 #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.keep_copy = False claw.output_style = 1 claw.num_output_times = 10 claw.tfinal = 1.0 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir #=========================================================================== # Solve the problem #=========================================================================== status = claw.run() #=========================================================================== # Plot results #=========================================================================== if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def acoustics(use_petsc=False, kernel_language='Fortran', solver_type='classic', iplot=False, htmlplot=False, outdir='./_output', weno_order=5): """ This example solves the 1-dimensional acoustics equations in a homogeneous medium. """ import numpy as np #================================================================= # Import the appropriate classes, depending on the options passed #================================================================= if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver1D() elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() solver.weno_order = weno_order else: raise Exception('Unrecognized value of solver_type.') #======================================================================== # Instantiate the solver and define the system of equations to be solved #======================================================================== solver.kernel_language = kernel_language from riemann import rp_acoustics solver.num_waves = rp_acoustics.num_waves if kernel_language == 'Python': solver.rp = rp_acoustics.rp_acoustics_1d solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = pyclaw.BC.wall solver.bc_upper[0] = pyclaw.BC.wall solver.cfl_desired = 1.0 solver.cfl_max = 1.0 #======================================================================== # Instantiate the grid and set the boundary conditions #======================================================================== x = pyclaw.Dimension('x', 0.0, 1.0, 200) grid = pyclaw.Grid(x) num_eqn = 2 state = pyclaw.State(grid, num_eqn) #======================================================================== # Set problem-specific variables #======================================================================== rho = 1.0 bulk = 1.0 state.problem_data['rho'] = rho state.problem_data['bulk'] = bulk state.problem_data['zz'] = np.sqrt(rho * bulk) state.problem_data['cc'] = np.sqrt(bulk / rho) #======================================================================== # Set the initial condition #======================================================================== xc = grid.x.center state.q[0, :] = np.cos(2 * np.pi * xc) state.q[1, :] = 0. #======================================================================== # Set up the controller object #======================================================================== claw = pyclaw.Controller() claw.solution = pyclaw.Solution(state) claw.solver = solver claw.outdir = outdir claw.num_output_times = 40 claw.tfinal = 2.0 # Solve status = claw.run() # Plot results if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def stegoton(use_petsc=0, kernel_language='Fortran', solver_type='classic', iplot=0, htmlplot=0, outdir='./_output'): """ Stegoton problem. Nonlinear elasticity in periodic medium. See LeVeque & Yong (2003). $$\\epsilon_t - u_x = 0$$ $$\\rho(x) u_t - \\sigma(\\epsilon,x)_x = 0$$ """ vary_Z = False if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() else: solver = pyclaw.ClawSolver1D() solver.kernel_language = kernel_language if kernel_language == 'Python': solver.set_riemann_solver('nonlinear_elasticity') elif kernel_language == 'Fortran': import riemann solver.rp = riemann.rp1_nonlinear_elasticity_fwave solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic #Use the same BCs for the aux array solver.aux_bc_lower = solver.bc_lower solver.aux_bc_upper = solver.bc_lower xlower = 0.0 xupper = 600.0 cellsperlayer = 6 mx = int(round(xupper - xlower)) * cellsperlayer x = pyclaw.Dimension('x', xlower, xupper, mx) domain = pyclaw.Domain(x) num_eqn = 2 state = pyclaw.State(domain, num_eqn) #Set global parameters alpha = 0.5 KA = 1.0 KB = 4.0 rhoA = 1.0 rhoB = 4.0 state.problem_data = {} state.problem_data['t1'] = 10.0 state.problem_data['tw1'] = 10.0 state.problem_data['a1'] = 0.0 state.problem_data['alpha'] = alpha state.problem_data['KA'] = KA state.problem_data['KB'] = KB state.problem_data['rhoA'] = rhoA state.problem_data['rhoB'] = rhoB state.problem_data['trtime'] = 250.0 state.problem_data['trdone'] = False #Initialize q and aux xc = state.grid.x.centers state.aux = setaux(xc, rhoB, KB, rhoA, KA, alpha, solver.aux_bc_lower[0], xupper=xupper) qinit(state, ic=2, a2=1.0, xupper=xupper) tfinal = 500. num_output_times = 10 solver.max_steps = 5000000 solver.fwave = True solver.before_step = b4step solver.user_bc_lower = moving_wall_bc solver.user_bc_upper = zero_bc solver.num_waves = 2 if solver_type == 'sharpclaw': solver.lim_type = 2 solver.char_decomp = 0 claw = pyclaw.Controller() claw.keep_copy = False claw.output_style = 1 claw.num_output_times = num_output_times claw.tfinal = tfinal claw.solution = pyclaw.Solution(state, domain) claw.solver = solver if vary_Z == True: #Zvalues = [1.0,1.2,1.4,1.6,1.8,2.0,2.2,2.4,2.6,2.8,3.0,3.5,4.0] Zvalues = [3.5, 4.0] #a2values= [0.9766161130681, 1.0888194560100042, 1.1601786315361329, 1.20973731651806, 1.2462158254919984] for ii, Z in enumerate(Zvalues): a2 = 1.0 #a2values[ii] KB = Z rhoB = Z state.problem_data['KB'] = KB state.problem_data['rhoB'] = rhoB state.problem_data['trdone'] = False state.aux = setaux(xc, rhoB, KB, rhoA, KA, alpha, bc_lower, xupper=xupper) patch.x.bc_lower = 2 patch.x.bc_upper = 2 state.t = 0.0 qinit(state, ic=2, a2=a2) init_solution = Solution(state, domain) claw.solution = init_solution claw.solution.t = 0.0 claw.tfinal = tfinal claw.outdir = './_output_Z' + str(Z) + '_' + str(cellsperlayer) status = claw.run() else: # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir)
def shallow1D(use_petsc=False, kernel_language='Fortran', iplot=False, htmlplot=False, outdir='./_output', solver_type='classic'): #=========================================================================== # Import libraries #=========================================================================== import numpy as np if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver1D() solver.limiters = pyclaw.limiters.tvd.vanleer elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() #=========================================================================== # Setup solver and solver parameters #=========================================================================== solver.num_waves = 2 solver.kernel_language = kernel_language if kernel_language == 'Python': solver.set_riemann_solver('shallow_roe') solver.problem_data['g'] = 1.0 solver.problem_data['efix'] = False elif kernel_language == 'Fortran': import riemann solver.rp = riemann.rp1_shallow_roe_with_efix solver.bc_lower[0] = pyclaw.BC.extrap solver.bc_upper[0] = pyclaw.BC.extrap #=========================================================================== # Initialize domain and then initialize the solution associated to the domain #=========================================================================== xlower = -5.0 xupper = 5.0 mx = 500 x = pyclaw.Dimension('x', xlower, xupper, mx) domain = pyclaw.Domain(x) num_eqn = 2 state = pyclaw.State(domain, num_eqn) # Parameters state.problem_data['grav'] = 1.0 xc = state.grid.x.centers IC = '2-shock' x0 = 0. if IC == 'dam-break': hl = 3. ul = 0. hr = 1. ur = 0. state.q[0, :] = hl * (xc <= x0) + hr * (xc > x0) state.q[1, :] = hl * ul * (xc <= x0) + hr * ur * (xc > x0) elif IC == '2-shock': hl = 1. ul = 1. hr = 1. ur = -1. state.q[0, :] = hl * (xc <= x0) + hr * (xc > x0) state.q[1, :] = hl * ul * (xc <= x0) + hr * ur * (xc > x0) elif IC == 'perturbation': eps = 0.1 state.q[0, :] = 1.0 + eps * np.exp(-(xc - x0)**2 / 0.5) state.q[1, :] = 0. #=========================================================================== # Setup controller and controller paramters #=========================================================================== claw = pyclaw.Controller() claw.keep_copy = True claw.tfinal = 2.0 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir #=========================================================================== # Solve the problem #=========================================================================== status = claw.run() #=========================================================================== # Plot results #=========================================================================== if iplot: pyclaw.plot.interactive_plot(outdir=outdir, file_format=claw.output_format) if htmlplot: pyclaw.plot.html_plot(outdir=outdir, file_format=claw.output_format)
def shockbubble(use_petsc=False, iplot=False, htmlplot=False, outdir='./_output', solver_type='classic'): """ Solve the Euler equations of compressible fluid dynamics. This example involves a bubble of dense gas that is impacted by a shock. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.dq_src = dq_Euler_radial solver.weno_order = 5 solver.lim_type = 2 else: solver = pyclaw.ClawSolver2D() solver.dimensional_split = 0 solver.transverse_waves = 2 solver.limiters = [4, 4, 4, 4, 2] solver.step_source = step_Euler_radial solver.num_waves = 5 solver.bc_lower[0] = pyclaw.BC.custom solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.wall solver.bc_upper[1] = pyclaw.BC.extrap #Aux variable in ghost cells doesn't matter solver.aux_bc_lower[0] = pyclaw.BC.extrap solver.aux_bc_upper[0] = pyclaw.BC.extrap solver.aux_bc_lower[1] = pyclaw.BC.extrap solver.aux_bc_upper[1] = pyclaw.BC.extrap # Initialize domain mx = 160 my = 40 x = pyclaw.Dimension('x', 0.0, 2.0, mx) y = pyclaw.Dimension('y', 0.0, 0.5, my) domain = pyclaw.Domain([x, y]) num_eqn = 5 num_aux = 1 state = pyclaw.State(domain, num_eqn, num_aux) state.problem_data['gamma'] = gamma state.problem_data['gamma1'] = gamma1 qinit(state) auxinit(state) solver.user_bc_lower = shockbc claw = pyclaw.Controller() claw.tfinal = 0.75 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.num_output_times = 10 claw.outdir = outdir # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir) if iplot: pyclaw.plot.interactive_plot(outdir=outdir) return claw.solution.q
def acoustics2D(use_petsc=False, kernel_language='Fortran', iplot=False, htmlplot=False, solver_type='classic', outdir='./_output', num_output_times=10): """ Example python script for solving the 2d acoustics equations. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver2D() solver.dimensional_split = 1 solver.num_waves = 2 solver.limiters = [4] * solver.num_waves elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.num_waves = 2 solver.cfl_max = 0.5 solver.cfl_desired = 0.45 solver.bc_lower[0] = pyclaw.BC.extrap solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.extrap solver.bc_upper[1] = pyclaw.BC.extrap # Initialize domain mx = 100 my = 100 x = pyclaw.Dimension('x', -1.0, 1.0, mx) y = pyclaw.Dimension('y', -1.0, 1.0, my) domain = pyclaw.Domain([x, y]) num_eqn = 3 state = pyclaw.State(domain, num_eqn) rho = 1.0 bulk = 4.0 cc = np.sqrt(bulk / rho) zz = rho * cc state.problem_data['rho'] = rho state.problem_data['bulk'] = bulk state.problem_data['zz'] = zz state.problem_data['cc'] = cc tfinal = 0.12 qinit(state) initial_solution = pyclaw.Solution(state, domain) solver.dt_initial = np.min( domain.delta) / state.problem_data['cc'] * solver.cfl_desired claw = pyclaw.Controller() claw.keep_copy = True # The output format MUST be set to petsc! claw.tfinal = tfinal claw.solution = initial_solution claw.solver = solver claw.outdir = outdir claw.num_output_times = num_output_times # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot() if iplot: pyclaw.plot.interactive_plot() if use_petsc: grid = domain.grid pressure = claw.frames[ claw.num_output_times].state.gqVec.getArray().reshape( [state.num_eqn, grid.num_cells[0], grid.num_cells[1]], order='F')[0, :, :] else: pressure = claw.frames[claw.num_output_times].state.q[0, :, :] return pressure
def acoustics(use_petsc=True, kernel_language='Fortran', solver_type='classic', iplot=False, htmlplot=False, outdir='./_output', weno_order=5): import numpy as np """ 1D acoustics example. """ output_format = None #Suppress output to make tests faster if use_petsc: import petclaw as pyclaw else: #Pure pyclaw import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver1D() elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver1D() solver.weno_order = weno_order else: raise Exception('Unrecognized value of solver_type.') # Initialize patches and solution x = pyclaw.Dimension('x', 0.0, 1.0, 100) domain = pyclaw.Domain(x) num_eqn = 2 state = pyclaw.State(domain, num_eqn) rho = 1.0 bulk = 1.0 state.problem_data['rho'] = rho state.problem_data['bulk'] = bulk state.problem_data['zz'] = np.sqrt(rho * bulk) state.problem_data['cc'] = np.sqrt(rho / bulk) grid = state.grid xc = grid.x.centers beta = 100 gamma = 0 x0 = 0.75 state.q[0, :] = np.exp(-beta * (xc - x0)**2) * np.cos(gamma * (xc - x0)) state.q[1, :] = 0. init_solution = pyclaw.Solution(state, domain) solver.num_waves = 2 solver.kernel_language = kernel_language if kernel_language == 'Python': from riemann import rp_acoustics solver.rp = rp_acoustics.rp_acoustics_1d solver.limiters = [4] * solver.num_waves solver.dt_initial = grid.delta[0] / state.problem_data['cc'] * 0.1 solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic claw = pyclaw.Controller() claw.keep_copy = True claw.num_output_times = 5 claw.output_format = output_format claw.outdir = outdir claw.tfinal = 1.0 claw.solution = init_solution claw.solver = solver # Solve status = claw.run() #This test is set up so that the waves pass through the domain #exactly once, and the final solution should be equal to the #initial condition. Here we output the 1-norm of their difference. if use_petsc == True: q0 = claw.frames[0].state.gqVec.getArray().reshape([-1]) qfinal = claw.frames[ claw.num_output_times].state.gqVec.getArray().reshape([-1]) else: q0 = claw.frames[0].state.q.reshape([-1]) qfinal = claw.frames[claw.num_output_times].state.q.reshape([-1]) dx = claw.solution.domain.delta[0] return dx * np.sum(np.abs(qfinal - q0))
def shockbubble(use_petsc=False,iplot=False,htmlplot=False): """ Solve the Euler equations of compressible fluid dynamics. This example involves a bubble of dense gas that is impacted by a shock. """ if use_petsc: import petclaw as pyclaw else: import pyclaw # Initialize domain mx=160; my=40 x = pyclaw.Dimension('x',0.0,2.0,mx) y = pyclaw.Dimension('y',0.0,0.5,my) domain = pyclaw.Domain([x,y]) num_eqn = 5 num_aux=1 state = pyclaw.State(domain,num_eqn,num_aux) state.problem_data['gamma']= gamma state.problem_data['gamma1']= gamma1 tfinal = 0.2 qinit(state) auxinit(state) initial_solution = pyclaw.Solution(state,domain) solver = pyclaw.ClawSolver2D() solver.cfl_max = 0.5 solver.cfl_desired = 0.45 solver.num_waves = 5 solver.limiters = [4,4,4,4,2] solver.dt_initial=0.005 solver.user_bc_lower=shockbc solver.step_source=euler_rad_src solver.source_split = 1 solver.bc_lower[0]=pyclaw.BC.custom solver.bc_upper[0]=pyclaw.BC.extrap solver.bc_lower[1]=pyclaw.BC.wall solver.bc_upper[1]=pyclaw.BC.extrap #Aux variable in ghost cells doesn't matter solver.aux_bc_lower[0]=pyclaw.BC.extrap solver.aux_bc_upper[0]=pyclaw.BC.extrap solver.aux_bc_lower[1]=pyclaw.BC.extrap solver.aux_bc_upper[1]=pyclaw.BC.extrap claw = pyclaw.Controller() claw.keep_copy = True # The output format MUST be set to petsc! claw.tfinal = tfinal claw.solution = initial_solution claw.solver = solver claw.num_output_times = 1 # Solve status = claw.run() if htmlplot: pyclaw.plot.html_plot(file_format=claw.output_format) if iplot: pyclaw.plot.interactive_plot(file_format=claw.output_format) if use_petsc: grid = claw.solution.domain.grid density=claw.frames[claw.num_output_times].state.gqVec.getArray().reshape([state.num_eqn,grid.num_cells[0],grid.num_cells[1]],order='F')[0,:,:] else: density=claw.frames[claw.num_output_times].state.q[0,:,:] return density
def acoustics2D(iplot=False, htmlplot=False, use_petsc=False, outdir='./_output', solver_type='classic'): """ Example python script for solving the 2d acoustics equations. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver2D() solver.dimensional_split = False elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.num_waves = 2 solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = pyclaw.BC.wall solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.wall solver.bc_upper[1] = pyclaw.BC.extrap # Initialize domain mx = 100 my = 100 x = pyclaw.Dimension('x', -1.0, 1.0, mx) y = pyclaw.Dimension('y', -1.0, 1.0, my) domain = pyclaw.Domain([x, y]) num_eqn = 3 state = pyclaw.State(domain, num_eqn) rho = 1.0 bulk = 4.0 cc = np.sqrt(bulk / rho) zz = rho * cc state.problem_data['rho'] = rho state.problem_data['bulk'] = bulk state.problem_data['zz'] = zz state.problem_data['cc'] = cc grid = state.grid Y, X = np.meshgrid(grid.y.centers, grid.x.centers) r = np.sqrt(X**2 + Y**2) width = 0.2 state.q[0, :, :] = (np.abs(r - 0.5) <= width) * (1. + np.cos(np.pi * (r - 0.5) / width)) state.q[1, :, :] = 0. state.q[2, :, :] = 0. claw = pyclaw.Controller() claw.keep_copy = True claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir # Solve claw.tfinal = 0.6 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir, file_format=claw.output_format) if iplot: pyclaw.plot.interactive_plot(outdir=outdir, file_format=claw.output_format) if use_petsc: pressure = claw.frames[ claw.num_output_times].state.gqVec.getArray().reshape( [grid.num_cells[0], grid.num_cells[1], state.num_eqn])[:, :, 0] else: pressure = claw.frames[claw.num_output_times].state.q[:, :, 0] return pressure
def acoustics3D(iplot=False, htmlplot=False, use_petsc=False, outdir='./_output', solver_type='classic', test='hom'): """ Example python script for solving the 3d acoustics equations. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver3D() else: raise Exception('Unrecognized solver_type.') import riemann solver.rp = riemann.rp3_vc_acoustics solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic solver.bc_lower[1] = pyclaw.BC.periodic solver.bc_upper[1] = pyclaw.BC.periodic solver.bc_lower[2] = pyclaw.BC.periodic solver.bc_upper[2] = pyclaw.BC.periodic solver.aux_bc_lower[0] = pyclaw.BC.periodic solver.aux_bc_upper[0] = pyclaw.BC.periodic solver.aux_bc_lower[1] = pyclaw.BC.periodic solver.aux_bc_upper[1] = pyclaw.BC.periodic solver.aux_bc_lower[2] = pyclaw.BC.periodic solver.aux_bc_upper[2] = pyclaw.BC.periodic if test == 'hom': solver.dimensional_split = True mx = 256 my = 4 mz = 4 zr = 1.0 # Impedance in right half cr = 1.0 # Sound speed in right half elif test == 'het': solver.dimensional_split = False solver.bc_lower[0] = pyclaw.BC.wall solver.bc_lower[1] = pyclaw.BC.wall solver.bc_lower[2] = pyclaw.BC.wall solver.aux_bc_lower[0] = pyclaw.BC.wall solver.aux_bc_lower[1] = pyclaw.BC.wall solver.aux_bc_lower[2] = pyclaw.BC.wall mx = 30 my = 30 mz = 30 zr = 2.0 # Impedance in right half cr = 2.0 # Sound speed in right half solver.num_waves = 2 solver.limiters = pyclaw.limiters.tvd.MC # Initialize domain x = pyclaw.Dimension('x', -1.0, 1.0, mx) y = pyclaw.Dimension('y', -1.0, 1.0, my) z = pyclaw.Dimension('z', -1.0, 1.0, mz) domain = pyclaw.Domain([x, y, z]) num_eqn = 4 num_aux = 2 # density, sound speed state = pyclaw.State(domain, num_eqn, num_aux) zl = 1.0 # Impedance in left half cl = 1.0 # Sound speed in left half grid = state.grid grid.compute_c_centers() X, Y, Z = grid._c_centers state.aux[0, :, :, :] = zl * (X < 0.) + zr * (X >= 0.) # Impedance state.aux[1, :, :, :] = cl * (X < 0.) + cr * (X >= 0.) # Sound speed x0 = -0.5 y0 = 0. z0 = 0. if test == 'hom': r = np.sqrt((X - x0)**2) width = 0.2 state.q[0, :, :, :] = (np.abs(r) <= width) * (1. + np.cos(np.pi * (r) / width)) elif test == 'het': r = np.sqrt((X - x0)**2 + (Y - y0)**2 + (Z - z0)**2) width = 0.1 state.q[0, :, :, :] = (np.abs(r - 0.3) <= width) * ( 1. + np.cos(np.pi * (r - 0.3) / width)) state.q[1, :, :, :] = 0. state.q[2, :, :, :] = 0. state.q[3, :, :, :] = 0. claw = pyclaw.Controller() claw.keep_copy = True claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir # Solve claw.tfinal = 2.0 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir, format=claw.output_format) if iplot: pyclaw.plot.interactive_plot(outdir=outdir, format=claw.output_format) #if use_petsc: # pinitial=claw.frames[0].state.gqVec.getArray().reshape([state.num_eqn,grid.num_cells[0],grid.num_cells[1],grid.num_cells[2]],order='F')[0,:,:,mz/2] # pfinal=claw.frames[10].state.gqVec.getArray().reshape([state.num_eqn,grid.num_cells[0],grid.num_cells[1],grid.num_cells[2]],order='F')[0,:,:,mz/2] #else: # pinitial=claw.frames[0].state.q[0,:,:,mz/2] # pfinal=claw.frames[10].state.q[0,:,:,mz/2] #import matplotlib.pyplot as plt #for i in range(claw.num_output_times): # plt.pcolor(claw.frames[i].state.q[0,:,:,mz/2]) # plt.figure() #plt.show() if use_petsc: pinitial = claw.frames[0].state.gqVec.getArray().reshape( [ state.num_eqn, grid.num_cells[0], grid.num_cells[1], grid.num_cells[2] ], order='F')[0, :, :, :].reshape(-1) pmiddle = claw.frames[claw.num_output_times / 2].state.gqVec.getArray().reshape( [ state.num_eqn, grid.num_cells[0], grid.num_cells[1], grid.num_cells[2] ], order='F')[0, :, :, :].reshape(-1) pfinal = claw.frames[ claw.num_output_times].state.gqVec.getArray().reshape([ state.num_eqn, grid.num_cells[0], grid.num_cells[1], grid.num_cells[2] ])[0, :, :, :].reshape(-1) else: pinitial = claw.frames[0].state.q[0, :, :, :].reshape(-1) pmiddle = claw.frames[3].state.q[0, :, :, :].reshape(-1) pfinal = claw.frames[claw.num_output_times].state.q[ 0, :, :, :].reshape(-1) final_difference = np.prod(grid.delta) * np.linalg.norm(pfinal - pinitial, ord=1) middle_difference = np.prod(grid.delta) * np.linalg.norm( pmiddle - pinitial, ord=1) if test == 'hom': return final_difference elif test == 'het': return pfinal
def acoustics2D(iplot=False, htmlplot=False, use_petsc=False, outdir='./_output', solver_type='classic'): """ Example python script for solving the 2d acoustics equations. """ if use_petsc: import petclaw as pyclaw else: import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver2D() elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D() solver.dimensional_split = False solver.num_waves = 2 solver.limiters = pyclaw.limiters.tvd.MC solver.bc_lower[0] = pyclaw.BC.wall solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.wall solver.bc_upper[1] = pyclaw.BC.extrap solver.aux_bc_lower[0] = pyclaw.BC.wall solver.aux_bc_upper[0] = pyclaw.BC.extrap solver.aux_bc_lower[1] = pyclaw.BC.wall solver.aux_bc_upper[1] = pyclaw.BC.extrap # Initialize domain mx = 200 my = 200 x = pyclaw.Dimension('x', -1.0, 1.0, mx) y = pyclaw.Dimension('y', -1.0, 1.0, my) domain = pyclaw.Domain([x, y]) num_eqn = 3 num_aux = 2 # density, sound speed state = pyclaw.State(domain, num_eqn, num_aux) # Cell centers coordinates grid = state.grid Y, X = np.meshgrid(grid.y.centers, grid.x.centers) # Set aux arrays rhol = 4.0 rhor = 1.0 bulkl = 4.0 bulkr = 4.0 cl = np.sqrt(bulkl / rhol) cr = np.sqrt(bulkr / rhor) state.aux[0, :, :] = rhol * (X < 0.) + rhor * (X >= 0.) # Density state.aux[1, :, :] = cl * (X < 0.) + cr * (X >= 0.) # Sound speed # Set initial condition x0 = -0.5 y0 = 0. r = np.sqrt((X - x0)**2 + (Y - y0)**2) width = 0.1 rad = 0.25 state.q[0, :, :] = (np.abs(r - rad) <= width) * (1. + np.cos(np.pi * (r - rad) / width)) state.q[1, :, :] = 0. state.q[2, :, :] = 0. claw = pyclaw.Controller() claw.keep_copy = True claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir claw.num_output_times = 20 # Solve claw.tfinal = 0.6 status = claw.run() if htmlplot: pyclaw.plot.html_plot(outdir=outdir, file_format=claw.output_format) if iplot: pyclaw.plot.interactive_plot(outdir=outdir, file_format=claw.output_format) if use_petsc: pressure = claw.frames[ claw.num_output_times].state.gqVec.getArray().reshape( [grid.num_cells[0], grid.num_cells[1], state.num_eqn])[:, :, 0] else: pressure = claw.frames[claw.num_output_times].state.q[:, :, 0] return pressure