def __init__(self, domain_config): solver = pyclaw.ClawSolver2D(riemann.euler_4wave_2D) solver.all_bcs = pyclaw.BC.periodic xmin, xmax = domain_config.x_range ymin, ymax = domain_config.y_range nx, ny = domain_config.num_cells domain = pyclaw.Domain([xmin, ymin], [xmax, ymax], [nx, ny]) solution = pyclaw.Solution(num_eqn, domain) solution.problem_data["gamma"] = 2.0 solver.dimensional_split = False solver.transverse_waves = 2 solver.step_source = q_src claw = pyclaw.Controller() claw.solution = solution claw.solver = solver claw.output_format = "ascii" claw.outdir = "./_output" self._solver = solver self._domain = domain self._solution = solution self._claw = claw
def setup(): from clawpack import pyclaw from clawpack.pyclaw.examples.advection_reaction_2d import advection_2d solver = pyclaw.ClawSolver2D(advection_2d) # Use dimensional splitting since no transverse solver is defined solver.dimensional_split = 1 solver.all_bcs = pyclaw.BC.extrap 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 domain = pyclaw.Domain((0., 0.), (1., 1.), (100, 100)) solver.num_eqn = 2 solver.num_waves = 1 num_aux = 2 state = pyclaw.State(domain, solver.num_eqn, num_aux) Xe, Ye = domain.grid.p_nodes Xc, Yc = domain.grid.p_centers dx, dy = domain.grid.delta # Edge velocities # u(x_(i-1/2),y_j) state.aux[0, :, :] = -(psi(Xe[:-1, 1:], Ye[:-1, 1:]) - psi(Xe[:-1, :-1], Ye[:-1, :-1])) / dy # v(x_i,y_(j-1/2)) state.aux[1, :, :] = (psi(Xe[1:, :-1], Ye[1:, :-1]) - psi(Xe[:-1, :-1], Ye[:-1, :-1])) / dx solver.before_step = set_velocities solver.step_source = source_step solver.source_split = 1 state.q[0, :, :] = (Xc <= 0.5) state.q[1, :, :] = (Yc <= 0.5) claw = pyclaw.Controller() claw.tfinal = t_period claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.keep_copy = True claw.setplot = setplot return claw
def setup(kernel_language='Fortran', solver_type='classic', use_petsc=False, outdir='./_output'): solver = pyclaw.ClawSolver2D(riemann.shallow_bathymetry_fwave_2D) solver.dimensional_split = 1 # No transverse solver available solver.bc_lower[0] = pyclaw.BC.custom solver.user_bc_lower = wave_maker_bc solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.wall solver.bc_upper[1] = pyclaw.BC.wall 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 my = 20 mx = 4 * my x = pyclaw.Dimension(0., 4., mx, name='x') y = pyclaw.Dimension(0, 1, my, name='y') domain = pyclaw.Domain([x, y]) state = pyclaw.State(domain, num_eqn, num_aux=1) X, Y = state.p_centers state.aux[0, :, :] = bathymetry(X, Y) state.q[depth, :, :] = 1. - state.aux[0, :, :] state.q[x_momentum, :, :] = 0. state.q[y_momentum, :, :] = 0. state.problem_data['grav'] = 1.0 state.problem_data['dry_tolerance'] = 1.e-3 state.problem_data['sea_level'] = 0. claw = pyclaw.Controller() claw.tfinal = 10 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.num_output_times = 40 claw.setplot = setplot claw.keep_copy = True return claw
def setup(use_petsc=False, solver_type='classic', outdir='./_output', disable_output=False): if use_petsc: raise Exception( "petclaw does not currently support mapped grids (go bug Lisandro who promised to implement them)" ) if solver_type != 'classic': raise Exception( "Only Classic-style solvers (solver_type='classic') are supported on mapped grids" ) solver = pyclaw.ClawSolver2D(riemann.shallow_sphere_2D) solver.fmod = classic2 # Set boundary conditions # ======================= solver.bc_lower[0] = pyclaw.BC.periodic solver.bc_upper[0] = pyclaw.BC.periodic solver.bc_lower[1] = pyclaw.BC.custom # Custom BC for sphere solver.bc_upper[1] = pyclaw.BC.custom # Custom BC for sphere solver.user_bc_lower = qbc_lower_y solver.user_bc_upper = qbc_upper_y # Auxiliary array solver.aux_bc_lower[0] = pyclaw.BC.periodic solver.aux_bc_upper[0] = pyclaw.BC.periodic solver.aux_bc_lower[1] = pyclaw.BC.custom # Custom BC for sphere solver.aux_bc_upper[1] = pyclaw.BC.custom # Custom BC for sphere solver.user_aux_bc_lower = auxbc_lower_y solver.user_aux_bc_upper = auxbc_upper_y # Dimensional splitting ? # ======================= solver.dimensional_split = 0 # Transverse increment waves and transverse correction waves are computed # and propagated. # ======================================================================= solver.transverse_waves = 2 # Use source splitting method # =========================== solver.source_split = 2 # Set source function # =================== solver.step_source = fortran_src_wrapper # Set the limiter for the waves # ============================= solver.limiters = pyclaw.limiters.tvd.MC #=========================================================================== # Initialize domain and state, then initialize the solution associated to the # state and finally initialize aux array #=========================================================================== # Domain: xlower = -3.0 xupper = 1.0 mx = 40 ylower = -1.0 yupper = 1.0 my = 20 # Check whether or not the even number of cells are used in in both # directions. If odd numbers are used a message is print at screen and the # simulation is interrupted. if (mx % 2 != 0 or my % 2 != 0): message = 'Please, use even numbers of cells in both direction. ' \ 'Only even numbers allow to impose correctly the boundary ' \ 'conditions!' raise ValueError(message) x = pyclaw.Dimension(xlower, xupper, mx, name='x') y = pyclaw.Dimension(ylower, yupper, my, name='y') domain = pyclaw.Domain([x, y]) dx = domain.grid.delta[0] dy = domain.grid.delta[1] # Define some parameters used in Fortran common blocks solver.fmod.comxyt.dxcom = dx solver.fmod.comxyt.dycom = dy solver.fmod.sw.g = 11489.57219 solver.rp.comxyt.dxcom = dx solver.rp.comxyt.dycom = dy solver.rp.sw.g = 11489.57219 # Define state object # =================== num_aux = 16 # Number of auxiliary variables state = pyclaw.State(domain, solver.num_eqn, num_aux) # Override default mapc2p function # ================================ state.grid.mapc2p = mapc2p_sphere_vectorized # Set auxiliary variables # ======================= # Get lower left corner coordinates xlower, ylower = state.grid.lower[0], state.grid.lower[1] num_ghost = 2 auxtmp = np.ndarray(shape=(num_aux, mx + 2 * num_ghost, my + 2 * num_ghost), dtype=float, order='F') auxtmp = problem.setaux(mx, my, num_ghost, mx, my, xlower, ylower, dx, dy, auxtmp, Rsphere) state.aux[:, :, :] = auxtmp[:, num_ghost:-num_ghost, num_ghost:-num_ghost] # Set index for capa state.index_capa = 0 # Set initial conditions # ====================== # 1) Call fortran function qtmp = np.ndarray(shape=(solver.num_eqn, mx + 2 * num_ghost, my + 2 * num_ghost), dtype=float, order='F') qtmp = problem.qinit(mx, my, num_ghost, mx, my, xlower, ylower, dx, dy, qtmp, auxtmp, Rsphere) state.q[:, :, :] = qtmp[:, num_ghost:-num_ghost, num_ghost:-num_ghost] # 2) call python function define above #qinit(state,mx,my) #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.keep_copy = True if disable_output: claw.output_format = None claw.output_style = 1 claw.num_output_times = 10 claw.tfinal = 10 claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.outdir = outdir return claw
#!/usr/bin/env python # encoding: utf-8 """ Solve the Euler equations of compressible fluid dynamics. """ from clawpack import pyclaw from clawpack import riemann solver = pyclaw.ClawSolver2D(riemann.rp2_euler_4wave) solver.all_bcs = pyclaw.BC.extrap domain = pyclaw.Domain([0., 0.], [1., 1.], [100, 100]) solution = pyclaw.Solution(solver.num_eqn, domain) gamma = 1.4 solution.problem_data['gamma'] = gamma # Set initial data xx, yy = domain.grid.p_centers l = xx < 0.5 r = xx >= 0.5 b = yy < 0.5 t = yy >= 0.5 solution.q[0, ...] = 2. * l * t + 1. * l * b + 1. * r * t + 3. * r * b solution.q[1, ...] = 0.75 * t - 0.75 * b solution.q[2, ...] = 0.5 * l - 0.5 * r solution.q[3, ...] = 0.5 * solution.q[0, ...] * ( solution.q[1, ...]**2 + solution.q[2, ...]**2) + 1. / (gamma - 1.) #solver.evolve_to_time(solution,tend=0.3) claw = pyclaw.Controller() claw.tfinal = 0.3
def plot_frame(frame): import matplotlib.pyplot as plt q = frame.q x, y = frame.state.grid.c_centers plt.pcolormesh(x, y, q[density, ...]) def plot_results(): from clawpack.visclaw import iplot ip = iplot.Iplot(load_frame, plot_frame) ip.plotloop() solver = pyclaw.ClawSolver2D(riemann.euler_4wave_2D) solver.all_bcs = pyclaw.BC.extrap domain = pyclaw.Domain([0., 0.], [1., 1.], [200, 200]) solution = pyclaw.Solution(num_eqn, domain) gamma = 1.4 solution.problem_data['gamma'] = gamma # Set initial data xx, yy = domain.grid.p_centers l = xx < 0.5 r = xx >= 0.5 b = yy < 0.5 t = yy >= 0.5 solution.q[density, ...] = 2. * l * t + 1. * l * b + 1. * r * t + 3. * r * b solution.q[x_momentum, ...] = 0.75 * t - 0.75 * b
def inclusion(): from clawpack import pyclaw from clawpack import riemann solver = pyclaw.ClawSolver2D(riemann.vc_elasticity_2D) solver.dimensional_split = False solver.transverse_waves = 2 solver.limiters = pyclaw.limiters.tvd.MC mx = 200 my = 100 num_aux = 7 domain = pyclaw.Domain((0., 0.), (2., 1.), (mx, my)) state = pyclaw.State(domain, solver.num_eqn, num_aux) solution = pyclaw.Solution(state, domain) solver.bc_lower[0] = pyclaw.BC.custom solver.user_bc_lower = moving_wall_bc solver.bc_upper[0] = pyclaw.BC.extrap solver.bc_lower[1] = pyclaw.BC.periodic # No stress solver.bc_upper[1] = pyclaw.BC.periodic # No stress 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 rho1 = 1.0 lam1 = 200. mu1 = 100. rho2 = 1.0 lam2 = 2.0 mu2 = 1.0 # set aux arrays # aux[0,i,j] = density rho in (i,j) cell # aux[1,i,j] = lambda in (i,j) cell # aux[2,i,j] = mu in (i,j) cell # aux[3,i,j] = cp in (i,j) cell # aux[4,i,j] = cs in (i,j) cell # aux[5,i,j] = xdisp in (i,j) cell # aux[6,i,j] = ydisp in (i,j) cell xx, yy = domain.grid.p_centers inbar = (0.5 < xx) * (xx < 1.5) * (0.4 < yy) * (yy < 0.6) outbar = 1 - inbar aux = state.aux aux[0, :, :] = rho1 * inbar + rho2 * outbar aux[1, :, :] = lam1 * inbar + lam2 * outbar aux[2, :, :] = mu1 * inbar + mu2 * outbar bulk = aux[1, :, :] + 2. * aux[2, :, :] aux[3, :, :] = np.sqrt(bulk / aux[0, :, :]) aux[4, :, :] = np.sqrt(aux[2, :, :] / aux[0, :, :]) aux[5, :, :] = 0. aux[6, :, :] = 0. # set initial condition state.q[:, :, :] = 0. claw = pyclaw.Controller() claw.solver = solver claw.solution = solution claw.num_output_times = 20 claw.tfinal = 0.5 claw.setplot = setplot return claw
Disable all loggers (quiet runs) """ root_logger = logging.getLogger() root_logger.disabled = True from clawpack.riemann import advection_1D fsolver_1D = pyclaw.ClawSolver1D(advection_1D) fsolver_1D.kernel_language = 'Fortran' from clawpack.riemann import advection_1D_py pysolver_1D = pyclaw.ClawSolver1D(advection_1D_py.advection_1D) pysolver_1D.kernel_language = 'Python' from clawpack.riemann import shallow_roe_with_efix_2D fsolver_2D = pyclaw.ClawSolver2D(shallow_roe_with_efix_2D) fsolver_2D.kernel_language = 'Fortran' solvers_1D = { 'current_fortran' : fsolver_1D, 'current_python' : pysolver_1D } solvers_2D = { 'current_fortran' : fsolver_2D } new_solvers_1D = {} # Here we try and bring in "experimental" solvers for comparison # If we can't bring in the solver, complain and move on...
def setup(num_cells=500, tfinal=30, solver_type='classic', num_output_times=150): from clawpack import riemann from clawpack import pyclaw if solver_type == 'classic': solver = pyclaw.ClawSolver2D(riemann.sw_aug_2D) solver.step_source = bed_friction solver.dimensional_split = True solver.limiters = pyclaw.limiters.tvd.minmod solver.cfl_max = 0.45 solver.cfl_desired = 0.3 elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D(riemann.sw_aug_2D) solver.bc_lower[0] = pyclaw.BC.custom solver.user_bc_lower = wave_maker_bc 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.extrap solver.aux_bc_upper[0] = pyclaw.BC.extrap solver.aux_bc_lower[1] = pyclaw.BC.periodic solver.aux_bc_upper[1] = pyclaw.BC.periodic solver.fwave = True # Domain: xlower = -0.0 xupper = 40. ylower = -0.5 yupper = 0.5 mx = num_cells my = 2 x = pyclaw.Dimension(xlower, xupper, mx, name='x') y = pyclaw.Dimension(ylower, yupper, my, name='y') domain = pyclaw.Domain([x, y]) num_aux = 1 state = pyclaw.State(domain, solver.num_eqn, num_aux) state.aux[:, :, :] = bathymetry(state.p_centers[0]) state.problem_data['grav'] = 9.810 # Gravitational force state.problem_data['t1'] = 50.0 # Stop generating waves after this time state.problem_data['amp'] = 0.3 # Amplitude of incoming waves qinit(state) #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.tfinal = tfinal claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.num_output_times = num_output_times claw.keep_copy = True claw.output_format = None return claw
def advect_field(u, v, qfunc, qkws={}, nx=200, ny=50, dx=500., dy=500., t_out=np.arange(0, 3601, 5*60), sharp=True): """ Run a 2D advection calculation via clawpack. """ is_vc = isinstance(u, (np.ndarray, )) if is_vc: rp = riemann.vc_advection_2D else: rp = riemann.advection_2D if sharp: solver = pyclaw.SharpClawSolver2D(rp) else: solver = pyclaw.ClawSolver2D(rp) solver.limiters = pyclaw.limiters.tvd.vanleer 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 if is_vc: 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 # Register domain x = pyclaw.Dimension(0, dx*nx, nx, name='x') y = pyclaw.Dimension(0, dy*ny, ny, name='y') domain = pyclaw.Domain([x, y]) x1d = domain.grid.x.centers y1d = domain.grid.y.centers xx = domain.grid.c_centers[0] yy = domain.grid.c_centers[1] num_eqn = 1 state = pyclaw.State(domain, num_eqn) if is_vc: state.aux[0, ...] = u state.aux[1, ...] = v else: state.problem_data['u'] = u # m/s state.problem_data['v'] = v # m/s q = qfunc(xx, yy, **qkws) state.q[0, ...] = q claw = pyclaw.Controller() claw.solution = pyclaw.Solution(state, domain) claw.solver = solver claw.keep_copy = True claw.tfinal = t_out[-1] claw.out_times = t_out claw.num_output_times = len(t_out) - 1 claw.run() times = claw.out_times tracers = [f.q.squeeze() for f in claw.frames] tracers = np.asarray(tracers) if not is_vc: u = u*np.ones_like(xx) v = v*np.ones_like(xx) print(tracers.shape, u.shape, xx.shape, x1d.shape) ds = xr.Dataset( {'q': (('time', 'x', 'y'), tracers), 'u': (('x', 'y'), u), 'v': (('x', 'y'), v)}, {'time': times, 'x': x1d, 'y': y1d}, ) ds['time'].attrs.update({'long_name': 'time', 'units': 'seconds since 2000-01-01 0:0:0'}) ds['x'].attrs.update({'long_name': 'x-coordinate', 'units': 'm'}) ds['y'].attrs.update({'long_name': 'y-coordinate', 'units': 'm'}) ds['u'].attrs.update({'long_name': 'zonal wind', 'units': 'm/s'}) ds['v'].attrs.update({'long_name': 'meridional wind', 'units': 'm/s'}) ds.attrs.update({ 'Conventions': 'CF-1.7' }) return ds
def sw_eqns(use_petsc=True, outdir='./_output', solver_type='classic', file_prefix=None, # Refinement refn=4, # goal: refn=4 -> Nx=128, Ny=128 # General parameters for simulatiuon # final_time=400.0, nDOut=400, restart_from_frame=None, # about initial condition A=0.05, sig2=2, mwl=0.75, # about the bathymetry bathymetry_type=0, #0: old bathymetry, 1: new bathymetry, 2: channel with inclined walls # about friction friction=False, friction_coeff=0.01, #switch to Periodic BCs time_to_switch_BCs = 25.0): #=========================================================================== # Import libraries #=========================================================================== from clawpack import riemann if use_petsc: import clawpack.petclaw as pyclaw else: from clawpack import pyclaw #=========================================================================== # Setup solver and solver parameters #=========================================================================== if solver_type == 'classic': solver = pyclaw.ClawSolver2D(riemann.sw_aug_2D) #solver = pyclaw.ClawSolver2D(riemann.shallow_bathymetry_fwave_2D) solver.limiters = pyclaw.limiters.tvd.minmod solver.dimensional_split=True elif solver_type == 'sharpclaw': solver = pyclaw.SharpClawSolver2D(riemann.sw_aug_2D) solver.bc_lower[0] = pyclaw.BC.wall solver.bc_upper[0] = pyclaw.BC.extrap solver.aux_bc_lower[0] = pyclaw.BC.extrap solver.aux_bc_upper[0] = pyclaw.BC.extrap if bathymetry_type==2: solver.bc_lower[1] = pyclaw.BC.wall solver.bc_upper[1] = pyclaw.BC.wall solver.aux_bc_lower[1] = pyclaw.BC.wall solver.aux_bc_upper[1] = pyclaw.BC.wall else: solver.bc_lower[1] = pyclaw.BC.periodic solver.bc_upper[1] = pyclaw.BC.periodic solver.aux_bc_lower[1] = pyclaw.BC.periodic solver.aux_bc_upper[1] = pyclaw.BC.periodic # solver.cfl_max = 0.25 solver.cfl_desired = 0.2 solver.fwave = True solver.before_step = switch_to_periodic_BCs #=========================================================================== # Set up controller and controller parameters #=========================================================================== claw = pyclaw.Controller() claw.tfinal = final_time claw.num_output_times = nDOut claw.solver = solver claw.outdir = outdir claw.keep_copy = False # Mesh resolution Nx = 8 * (2**refn) Ny = 8 * (2**refn) # FRICTION # if friction: solver.step_source = step_friction solver.source_split = 1 # if restart_from_frame is not None: claw.solution = pyclaw.Solution(restart_from_frame, file_format='petsc',read_aux=False,file_prefix=file_prefix) grid = claw.solution.domain.grid claw.solution.state.aux = bathymetry(grid.x.centers,grid.y.centers,bathymetry_type=bathymetry_type) claw.num_output_times = claw.num_output_times - restart_from_frame claw.start_frame = restart_from_frame claw.solution.state.problem_data['time_to_switch_BCs'] = time_to_switch_BCs else: # Domain: xlower = 0.; xupper = Lx if bathymetry_type==2: ylower = 0.; yupper = 1.0 else: ylower = -Ly/2.; yupper = Ly/2. # mx=int((xupper-xlower)*Nx) my=int((yupper-ylower)*Ny) x = pyclaw.Dimension(xlower,xupper,mx,name='x') y = pyclaw.Dimension(ylower,yupper,my,name='y') domain = pyclaw.Domain([x,y]) num_aux = 1 state = pyclaw.State(domain,solver.num_eqn,num_aux) state.aux = bathymetry(state.grid.x.centers,state.grid.y.centers,bathymetry_type=bathymetry_type) state.problem_data['grav'] = 9.8 state.problem_data['cf'] = friction_coeff claw.solution = pyclaw.Solution(state,domain) qinit(state,A,sig2,mwl) state.problem_data['time_to_switch_BCs'] = time_to_switch_BCs # run status = claw.run()
def fplot(frame_number): frame = claw.frames[frame_number] density = frame.q[0, :, :] (vx, vy) = np.gradient(density) vs = np.sqrt(vx**2 + vy**2) im.set_data(vs.T) #print im return im, claw = pyclaw.Controller() claw.tfinal = 0.6 claw.num_output_times = 40 riemann_solver = riemann.euler_4wave_2D claw.solver = pyclaw.ClawSolver2D(riemann_solver) claw.solver.all_bcs = pyclaw.BC.extrap grid_size = (300, 300) domain = pyclaw.Domain((0., 0.), (1., 1.), grid_size) claw.solution = pyclaw.Solution(claw.solver.num_eqn, domain) gam = 1.4 claw.solution.problem_data['gamma'] = gam # Set initial data q = claw.solution.q xx, yy = domain.grid.p_centers l = xx < 0.5 r = xx >= 0.5 b = yy < 0.5