예제 #1
0
파일: kpp.py 프로젝트: mparsani/pyclaw
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)
예제 #2
0
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)
예제 #3
0
파일: acoustics.py 프로젝트: LuluLiu/pyclaw
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
예제 #4
0
파일: acoustics.py 프로젝트: LuluLiu/pyclaw
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
예제 #5
0
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
예제 #6
0
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
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
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