Ejemplo n.º 1
0
def euler3d(kernel_language='Fortran',solver_type='classic',\
            use_petsc=False,outdir='./_output',\
            output_format='hdf5',file_prefix='equil',disable_output=False,\
            mx=mxyz[0],my=mxyz[1],mz=mxyz[2],\
            tfinal=64.0,num_output_times=1):

    if use_petsc:
        import clawpack.petclaw as pyclaw
    else:
        from clawpack import pyclaw

    if solver_type=='classic':
        solver = pyclaw.ClawSolver3D()
        solver.dimensional_split = True
        solver.limiters = pyclaw.limiters.tvd.minmod
        solver.num_ghost = 2
        solver.order = 2
        solver.fwave = True
    elif solver_type=='sharpclaw':
        solver = pyclaw.SharpClawSolver3D()
    else:
        raise Exception('Unrecognized solver_type.')

    import logging
    solver.logger.setLevel(logging.DEBUG)

    import euler_3d_gmap
    solver.rp = euler_3d_gmap
    solver.num_eqn = 5
    solver.num_waves = 3
    solver.cfl_max = 0.6
    solver.cfl_desired = 0.5
    solver.dt_initial = 1.e-0
    solver.max_steps = 10000

    # Initialize Domain
    x = pyclaw.Dimension(0.0,1.0,mx,name='x')
    y = pyclaw.Dimension(0.0,1.0,my,name='y')
    z = pyclaw.Dimension(0.0,1.0,mz,name='z')
    domain = pyclaw.Domain([x,y,z])

    num_aux = 15
    state = pyclaw.State(domain,solver.num_eqn,num_aux)
    state.problem_data['gamma']=gamma
    state.problem_data['g_r'] = gR
    state.problem_data['gravity'] = gravityTerm
    state.problem_data['gravityflux'] = gravityEflux

    # Grids
    mbc = solver.num_ghost
    grid = state.grid

    # Computational Grid Sizes
    dxc = domain.grid.delta[0]
    dyc = domain.grid.delta[1]
    dzc = domain.grid.delta[2]
    pmx, pmy, pmz = grid.num_cells[0], grid.num_cells[1], grid.num_cells[2]

    # Computational Grid Centers and Edges
    centers = grid.c_centers # centers (Comp.)
    centersBC = grid.c_centers_with_ghost(mbc) # centers w Ghost (Comp.)
    edgesBC = grid.c_edges_with_ghost(mbc) # edges w Ghost (Comp.)

    # Grid Centers Without Boundary Cells (1D Slice) - Comp. and Phys.
    xcc = grid.x.centers # x centers (Comp.)
    ycc = grid.y.centers # y centers (Comp.)
    zcc = grid.z.centers # z centers (Comp.)
    xcp,ycp,zcp = mg.mapc2pwrapper(xcc,ycc,zcc,pmz,xyzMin,xyzMax,mapType)

    # Grid Centers Without Boundary Cells (3D Arrays)
    Xcc,Ycc,Zcc = centers[0][:][:][:],centers[1][:][:][:],centers[2][:][:][:]
    Xcp,Ycp,Zcp = mg.mapc2pwrapper(Xcc,Ycc,Zcc,pmz,xyzMin,xyzMax,mapType)
    Xcp = np.reshape(Xcp,[pmx,pmy,pmz],order='F') # x centers (Phys.)
    Ycp = np.reshape(Ycp,[pmx,pmy,pmz],order='F') # y centers (Phys.)
    Zcp = np.reshape(Zcp,[pmx,pmy,pmz],order='F') # z centers (Phys.)

    # Grid Edges With Boundary Cells (1D Slice along z)- Comp. and Phys.
    xecZ = edgesBC[0][0][0][:] # x edges along z (Comp.)
    yecZ = edgesBC[1][0][0][:] # y edges along z (Comp.)
    zecZ = edgesBC[2][0][0][:] # z edges along z (Comp.)
    xepZ,yepZ,zepZ = mg.mapc2pwrapper(xecZ,yecZ,zecZ,pmz,xyzMin,xyzMax,mapType)

    # Grid Centers With Boundary Cells (1D Slice along z) - Comp. and Phys.
    global zcpZ
    xccZ = centersBC[0][0][0][:] # x centers along z (Comp.)
    yccZ = centersBC[1][0][0][:] # y centers along z (Comp.)
    zccZ = centersBC[2][0][0][:] # z centers along z (Comp.)
    xcpZ,ycpZ,zcpZ = mg.mapc2pwrapper(xccZ,yccZ,zccZ,pmz,xyzMin,xyzMax,mapType)
 
    if np.sqrt(xepZ[0]**2+yepZ[0]**2+zepZ[0]**2) <= 0:
        print("WARNING: z may go below Earth's surface"," zepZ: ",zepZ[0:10])

    # Create vectors for 1D pressure and density column with boundary cells
    mz0 = pmz+2*mbc
    global p0, rho0, Mavg
    p0 = np.zeros([mz0],dtype='float',order='F')
    rho0 = np.zeros([mz0],dtype='float',order='F')
    Mavg = np.zeros([mz0],dtype='float',order='F')

    # Set the equilibrium pressure such that dp/dz = -rho*gR
    p0,rho0,Mavg = setEquilibriumAtmosphere(p0,rho0,Mavg)

    # Modify the equilibrium such that dp/dz = -rho*gR is held numerically
    p0 = modifyEquilibriumAtmosphere(zepZ,p0,rho0)

    # Set the auxiliary variables
    xlower,ylower,zlower = edgesBC[0][0][0][0],edgesBC[1][0][0][0],edgesBC[2][0][0][0]
    dxc,dyc,dzc = domain.grid.delta[0],domain.grid.delta[1],domain.grid.delta[2]
        
    global auxtmp
    auxtmp = np.zeros([num_aux,pmx+2*mbc,pmy+2*mbc,pmz+2*mbc],dtype='float',order='F')
    auxtmp = mg.setauxiliaryvariables(num_aux,mbc,pmx,pmy,pmz,xlower,ylower,zlower,dxc,dyc,dzc,xyzMin,xyzMax,mapType)
    state.aux[:,:,:,:] = auxtmp[:,mbc:-mbc,mbc:-mbc,mbc:-mbc]

    # Set Index for Capcaity Function in state.aux (Python 0-based)
    state.index_capa = 12 

    # Set the state variables (Initial Conditions)

    # Initialize p,T,velSqrd
    p = np.zeros([pmx,pmy,pmz],dtype='float',order='F')
    T = np.zeros([pmx,pmy,pmz],dtype='float',order='F')
    velSqrd = np.zeros([pmx,pmy,pmz],dtype='float',order='F')

    # Density
    for i in range(pmx):
        for j in range(pmy):
            # NEEDS TO BE FIXED WHEN MPI SLICES NORMAL TO Z
            state.q[0,i,j,:] = rho0[mbc:pmz+mbc]
    
    # Momentum
    state.q[1,:,:,:] = 0. # x-momentum (rho*u)
    state.q[2,:,:,:] = 0. # y-momentum (rho*v)
    state.q[3,:,:,:] = 0. # z-momentum (rho*w)

    # Velocity Squared (u**2+v**2+w**2)
    velSqrd[:,:,:] = (state.q[1,:,:,:]**2+state.q[2,:,:,:]**2 + state.q[3,:,:,:]**2)/state.q[0,:,:,:]**2

    # Energy
    for i in range(pmx):
        for j in range(pmy):
            # NEEDS TO BE FIXED WHEN MPI SLICES NORMAL TO Z
            p[i,j,:] = p0[mbc:pmz+mbc]
    state.q[4,:,:,:] = p/gamma1 + 0.5*state.q[0,:,:,:]*velSqrd + state.q[0,:,:,:]*(gR)*Zcp[:,:,:]*gFlux

    # Add Temperature Perturbation
    T = p/state.q[0,:,:,:]
    L = np.sqrt((Xcp-xSphere)**2+(Ycp-ySphere)**2+(Zcp-zSphere)**2)
    for i in range(pmx):
        for j in range(pmy):
            for k in range(pmz):
                if L[i,j,k] <= rSphere:
                    mu = Mavg[k+mbc]/nAvogadro
                    T[i,j,k] += TSphere*(kBoltzmann/mu)*(1.0-L[i,j,k]/rSphere)
                    p[i,j,k] = T[i,j,k]*state.q[0,i,j,k]
    state.q[4,:,:,:] = p/gamma1 + 0.5*state.q[0,:,:,:]*velSqrd + state.q[0,:,:,:]*(gR)*Zcp[:,:,:]*gFlux # energy (e)

    # Setup Boundary Conditions

    # X - Boundary Conditions
    solver.bc_lower[0] = pyclaw.BC.extrap
    solver.bc_upper[0] = pyclaw.BC.extrap

    # Y - Boundary Conditions
    solver.bc_lower[1] = pyclaw.BC.extrap
    solver.bc_upper[1] = pyclaw.BC.extrap

    # Z - Boundary Conditions
    solver.bc_lower[2] = pyclaw.BC.custom
    solver.bc_upper[2] = pyclaw.BC.custom
    solver.user_bc_lower = customBCLowerZ
    solver.user_bc_upper = customBCUpperZ

    # Aux - Boundary Conditions
    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
    solver.aux_bc_lower[2] = pyclaw.BC.custom
    solver.aux_bc_upper[2] = pyclaw.BC.custom
    solver.user_aux_bc_lower = customAuxBCLowerZ
    solver.user_aux_bc_upper = customAuxBCUpperZ

    # Solver Parameters
    claw = pyclaw.Controller()
    claw.verbosity = 4
    claw.solution = pyclaw.Solution(state,domain)
    claw.solver = solver
    claw.output_format = output_format
    claw.output_file_prefix = file_prefix
    claw.keep_copy = False
    if disable_output:
        claw.output_format = None
    claw.tfinal = tfinal
    claw.num_output_times = num_output_times
    claw.outdir = outdir
    #state.mp = 1
    #claw.compute_p = outputDensity

    return claw
Ejemplo n.º 2
0
def euler3d(kernel_language='Fortran',solver_type='classic',\
            use_petsc=False,outdir='./_output',\
            output_format='hdf5',file_prefix='equil',disable_output=False,\
            mx=mxyz[0],my=mxyz[1],mz=mxyz[2],\
            tfinal=64.0,num_output_times=1):

    if use_petsc:
        import clawpack.petclaw as pyclaw
    else:
        from clawpack import pyclaw

    if solver_type == 'classic':
        solver = pyclaw.ClawSolver3D()
        solver.dimensional_split = True
        solver.limiters = pyclaw.limiters.tvd.minmod
        solver.num_ghost = 2
        solver.order = 2
        solver.fwave = True
    elif solver_type == 'sharpclaw':
        solver = pyclaw.SharpClawSolver3D()
    else:
        raise Exception('Unrecognized solver_type.')

    import logging
    solver.logger.setLevel(logging.DEBUG)

    import euler_3d_gmap
    solver.rp = euler_3d_gmap
    solver.num_eqn = 5
    solver.num_waves = 3
    solver.cfl_max = 0.6
    solver.cfl_desired = 0.5
    solver.dt_initial = 1.e-0
    solver.max_steps = 10000

    # Initialize Domain
    x = pyclaw.Dimension(0.0, 1.0, mx, name='x')
    y = pyclaw.Dimension(0.0, 1.0, my, name='y')
    z = pyclaw.Dimension(0.0, 1.0, mz, name='z')
    domain = pyclaw.Domain([x, y, z])

    num_aux = 15
    state = pyclaw.State(domain, solver.num_eqn, num_aux)
    state.problem_data['gamma'] = gamma
    state.problem_data['g_r'] = gR
    state.problem_data['gravity'] = gravityTerm
    state.problem_data['gravityflux'] = gravityEflux

    # Grids
    mbc = solver.num_ghost
    grid = state.grid

    # Computational Grid Sizes
    dxc = domain.grid.delta[0]
    dyc = domain.grid.delta[1]
    dzc = domain.grid.delta[2]
    pmx, pmy, pmz = grid.num_cells[0], grid.num_cells[1], grid.num_cells[2]

    # Computational Grid Centers and Edges
    centers = grid.c_centers  # centers (Comp.)
    centersBC = grid.c_centers_with_ghost(mbc)  # centers w Ghost (Comp.)
    edgesBC = grid.c_edges_with_ghost(mbc)  # edges w Ghost (Comp.)

    # Grid Centers Without Boundary Cells (1D Slice) - Comp. and Phys.
    xcc = grid.x.centers  # x centers (Comp.)
    ycc = grid.y.centers  # y centers (Comp.)
    zcc = grid.z.centers  # z centers (Comp.)
    xcp, ycp, zcp = mg.mapc2pwrapper(xcc, ycc, zcc, pmz, xyzMin, xyzMax,
                                     mapType)

    # Grid Centers Without Boundary Cells (3D Arrays)
    Xcc, Ycc, Zcc = centers[0][:][:][:], centers[1][:][:][:], centers[
        2][:][:][:]
    Xcp, Ycp, Zcp = mg.mapc2pwrapper(Xcc, Ycc, Zcc, pmz, xyzMin, xyzMax,
                                     mapType)
    Xcp = np.reshape(Xcp, [pmx, pmy, pmz], order='F')  # x centers (Phys.)
    Ycp = np.reshape(Ycp, [pmx, pmy, pmz], order='F')  # y centers (Phys.)
    Zcp = np.reshape(Zcp, [pmx, pmy, pmz], order='F')  # z centers (Phys.)

    # Grid Edges With Boundary Cells (1D Slice along z)- Comp. and Phys.
    xecZ = edgesBC[0][0][0][:]  # x edges along z (Comp.)
    yecZ = edgesBC[1][0][0][:]  # y edges along z (Comp.)
    zecZ = edgesBC[2][0][0][:]  # z edges along z (Comp.)
    xepZ, yepZ, zepZ = mg.mapc2pwrapper(xecZ, yecZ, zecZ, pmz, xyzMin, xyzMax,
                                        mapType)

    # Grid Centers With Boundary Cells (1D Slice along z) - Comp. and Phys.
    global zcpZ
    xccZ = centersBC[0][0][0][:]  # x centers along z (Comp.)
    yccZ = centersBC[1][0][0][:]  # y centers along z (Comp.)
    zccZ = centersBC[2][0][0][:]  # z centers along z (Comp.)
    xcpZ, ycpZ, zcpZ = mg.mapc2pwrapper(xccZ, yccZ, zccZ, pmz, xyzMin, xyzMax,
                                        mapType)

    if np.sqrt(xepZ[0]**2 + yepZ[0]**2 + zepZ[0]**2) <= 0:
        print "WARNING: z may go below Earth's surface", " zepZ: ", zepZ[0:10]

    # Create vectors for 1D pressure and density column with boundary cells
    mz0 = pmz + 2 * mbc
    global p0, rho0, Mavg
    p0 = np.zeros([mz0], dtype='float', order='F')
    rho0 = np.zeros([mz0], dtype='float', order='F')
    Mavg = np.zeros([mz0], dtype='float', order='F')

    # Set the equilibrium pressure such that dp/dz = -rho*gR
    p0, rho0, Mavg = setEquilibriumAtmosphere(p0, rho0, Mavg)

    # Modify the equilibrium such that dp/dz = -rho*gR is held numerically
    p0 = modifyEquilibriumAtmosphere(zepZ, p0, rho0)

    # Set the auxiliary variables
    xlower, ylower, zlower = edgesBC[0][0][0][0], edgesBC[1][0][0][0], edgesBC[
        2][0][0][0]
    dxc, dyc, dzc = domain.grid.delta[0], domain.grid.delta[
        1], domain.grid.delta[2]

    global auxtmp
    auxtmp = np.zeros([num_aux, pmx + 2 * mbc, pmy + 2 * mbc, pmz + 2 * mbc],
                      dtype='float',
                      order='F')
    auxtmp = mg.setauxiliaryvariables(num_aux, mbc, pmx, pmy, pmz, xlower,
                                      ylower, zlower, dxc, dyc, dzc, xyzMin,
                                      xyzMax, mapType)
    state.aux[:, :, :, :] = auxtmp[:, mbc:-mbc, mbc:-mbc, mbc:-mbc]

    # Set Index for Capcaity Function in state.aux (Python 0-based)
    state.index_capa = 12

    # Set the state variables (Initial Conditions)

    # Initialize p,T,velSqrd
    p = np.zeros([pmx, pmy, pmz], dtype='float', order='F')
    T = np.zeros([pmx, pmy, pmz], dtype='float', order='F')
    velSqrd = np.zeros([pmx, pmy, pmz], dtype='float', order='F')

    # Density
    for i in range(pmx):
        for j in range(pmy):
            # NEEDS TO BE FIXED WHEN MPI SLICES NORMAL TO Z
            state.q[0, i, j, :] = rho0[mbc:pmz + mbc]

    # Momentum
    state.q[1, :, :, :] = 0.  # x-momentum (rho*u)
    state.q[2, :, :, :] = 0.  # y-momentum (rho*v)
    state.q[3, :, :, :] = 0.  # z-momentum (rho*w)

    # Velocity Squared (u**2+v**2+w**2)
    velSqrd[:, :, :] = (state.q[1, :, :, :]**2 + state.q[2, :, :, :]**2 +
                        state.q[3, :, :, :]**2) / state.q[0, :, :, :]**2

    # Energy
    for i in range(pmx):
        for j in range(pmy):
            # NEEDS TO BE FIXED WHEN MPI SLICES NORMAL TO Z
            p[i, j, :] = p0[mbc:pmz + mbc]
    state.q[4, :, :, :] = p / gamma1 + 0.5 * state.q[
        0, :, :, :] * velSqrd + state.q[0, :, :, :] * (
            gR) * Zcp[:, :, :] * gFlux

    # Add Temperature Perturbation
    T = p / state.q[0, :, :, :]
    L = np.sqrt((Xcp - xSphere)**2 + (Ycp - ySphere)**2 + (Zcp - zSphere)**2)
    for i in range(pmx):
        for j in range(pmy):
            for k in range(pmz):
                if L[i, j, k] <= rSphere:
                    mu = Mavg[k + mbc] / nAvogadro
                    T[i, j, k] += TSphere * (kBoltzmann /
                                             mu) * (1.0 - L[i, j, k] / rSphere)
                    p[i, j, k] = T[i, j, k] * state.q[0, i, j, k]
    state.q[4, :, :, :] = p / gamma1 + 0.5 * state.q[
        0, :, :, :] * velSqrd + state.q[0, :, :, :] * (
            gR) * Zcp[:, :, :] * gFlux  # energy (e)

    # Setup Boundary Conditions

    # X - Boundary Conditions
    solver.bc_lower[0] = pyclaw.BC.extrap
    solver.bc_upper[0] = pyclaw.BC.extrap

    # Y - Boundary Conditions
    solver.bc_lower[1] = pyclaw.BC.extrap
    solver.bc_upper[1] = pyclaw.BC.extrap

    # Z - Boundary Conditions
    solver.bc_lower[2] = pyclaw.BC.custom
    solver.bc_upper[2] = pyclaw.BC.custom
    solver.user_bc_lower = customBCLowerZ
    solver.user_bc_upper = customBCUpperZ

    # Aux - Boundary Conditions
    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
    solver.aux_bc_lower[2] = pyclaw.BC.custom
    solver.aux_bc_upper[2] = pyclaw.BC.custom
    solver.user_aux_bc_lower = customAuxBCLowerZ
    solver.user_aux_bc_upper = customAuxBCUpperZ

    # Solver Parameters
    claw = pyclaw.Controller()
    claw.verbosity = 4
    claw.solution = pyclaw.Solution(state, domain)
    claw.solver = solver
    claw.output_format = output_format
    claw.output_file_prefix = file_prefix
    claw.keep_copy = False
    if disable_output:
        claw.output_format = None
    claw.tfinal = tfinal
    claw.num_output_times = num_output_times
    claw.outdir = outdir
    #state.mp = 1
    #claw.compute_p = outputDensity

    return claw
Ejemplo n.º 3
0
        def visualize(self,
                      U,
                      title='',
                      legend=None,
                      filename=None,
                      block=True,
                      separate_colorbars=True):
            import os
            import matplotlib.pyplot as plt
            from matplotlib.colors import BoundaryNorm
            from matplotlib.ticker import MaxNLocator

            mx = self.claw.solution.state.q.shape[1]
            my = self.claw.solution.state.q.shape[2]
            mz = self.claw.solution.state.q.shape[3]

            Ur = np.reshape(
                U.to_numpy(),
                (U.to_numpy().shape[0], self.claw.solver.num_eqn, mx, my, mz),
                order='F')

            sys.path.append(os.getcwd())
            from mappedGrid import euler3d_mappedgrid as mg
            import hotsphere as hs

            # Atmosphere Data
            mbc = self.claw.solver.num_ghost
            print("mbc:", mbc)
            pmzBC = mz + 2 * mbc
            p0 = np.zeros([pmzBC], dtype='float', order='F')
            rho0 = np.zeros([pmzBC], dtype='float', order='F')
            Mavg = np.zeros([pmzBC], dtype='float', order='F')
            p0, rho0, Mavg = hs.setEquilibriumAtmosphere(p0, rho0, Mavg)

            # Create Grid
            xc, yc, zc = np.linspace(0.0, 1.0, mx), np.linspace(
                0.0, 1.0, my), np.linspace(0.0, 1.0, mz)
            Xc, Yc, Zc = np.meshgrid(xc, yc, zc, indexing='ij')
            Xp, Yp, Zp = mg.mapc2pwrapper(Xc, Yc, Zc, mz, hs.xyzMin, hs.xyzMax,
                                          hs.mapType)
            Xp, Yp, Zp = np.reshape(Xp, [mx, my, mz], order='F'), \
                         np.reshape(Yp, [mx, my, mz], order='F'), \
                         np.reshape(Zp, [mx, my, mz], order='F')

            print("Ur:", np.shape(Ur))
            index = np.size(Ur, 0) - 1

            # Conserved Quantities Advanced by Solver (Euler Gas Dynamics Equations)
            rho = Ur[index, 0, :, 79, :]  # Mass Density
            rhou = Ur[index, 1, :, 79, :]  # Momentum x-direction
            rhov = Ur[index, 2, :, 79, :]  # Momentum y-direction
            rhow = Ur[index, 3, :, 79, :]  # Momentum z-direction
            ene = Ur[index, 4, :, 79, :]  # Energy Density

            # Compute Derived Quantities
            velsq = (rhou**2 + rhov**2 + rhow**2) / rho**2  # velocity squared
            p = (hs.gamma - 1.0) * (ene - 0.5 * rho * velsq)  # gas pressure
            T = np.zeros([np.size(p, 0), np.size(p, 1)],
                         dtype='float',
                         order='F')
            for i in range(np.size(p, 0)):
                for k in range(np.size(p, 1)):
                    # Compute Temperature
                    T[i, k] = p[i, k] / (hs.nAvogadro / Mavg[k + mbc] *
                                         rho[i, k]) / hs.kBoltzmann
            LogT = np.log(T)  # Log Temperature
            LogRho = np.log(rho)  # Log Mass Density
            Vmag = np.sqrt(velsq)  # Velocity Magnitude
            cSound = np.sqrt(hs.gamma * p / rho)  # Sound Speed

            plt.figure()
            plt.title(r'PyClaw Finite Volume Solution')

            plt.subplot(221)
            plt.pcolormesh(Xp[:, 79, :],
                           Zp[:, 79, :],
                           LogRho,
                           cmap='jet',
                           vmin=-40,
                           vmax=-18,
                           shading='gouraud')
            plt.title('Log Mass Density')
            plt.colorbar()

            plt.subplot(222)
            plt.pcolormesh(Xp[:, 79, :],
                           Zp[:, 79, :],
                           LogT,
                           vmin=0.0,
                           vmax=10.0,
                           cmap='jet',
                           shading='gouraud')
            plt.title('Log Temperature')
            plt.colorbar()

            plt.subplot(223)
            plt.pcolormesh(Xp[:, 79, :],
                           Zp[:, 79, :],
                           Vmag,
                           cmap='jet',
                           vmin=0.,
                           vmax=5.e5,
                           shading='gouraud')
            plt.title('Velocity Magnitude')
            plt.colorbar()

            plt.subplot(224)
            plt.pcolormesh(Xp[:, 79, :],
                           Zp[:, 79, :],
                           cSound,
                           cmap='jet',
                           shading='gouraud')
            plt.title('Sound Speed, c_s')
            plt.colorbar()

            plt.show()
Ejemplo n.º 4
0
def hotsphere3D(kernel_language='Fortran',
                solver_type=mainSolver,
                use_petsc=False,
                outdir=outDir,
                output_format='hdf5',
                file_prefix=filePrefix,
                disable_output=False,
                mx=mxyz[0],
                my=mxyz[1],
                mz=mxyz[2],
                tfinal=timeFinal,
                num_output_times=numberOfOutputTimes):

    # Load PyClaw
    if mpiSize > 1 and not use_petsc:
        print("For MPI runs, use_petsc=True, exiting.")
        exit()

    if use_petsc:
        import clawpack.petclaw as pyclaw
    else:
        from clawpack import pyclaw

    # Solver Settings
    if solver_type == 'classic':
        import euler_3d_gmap
        solver = pyclaw.ClawSolver3D()
        solver.rp = euler_3d_gmap
        solver.num_eqn = neqn
        solver.num_waves = nwaves
        solver.dimensional_split = True
        solver.limiters = pyclaw.limiters.tvd.minmod
        solver.num_ghost = 2
        solver.order = 2
        solver.fwave = True
        solver.source_split = 0
        solver.before_step = None
        solver.step_source = None
        solver.dt_variable = True
        solver.cfl_max = 0.60
        solver.cfl_desired = 0.50
        solver.dt_initial = 1.e-4
        solver.max_steps = 50000
    else:
        raise Exception('Unrecognized solver_type')

    # Logging
    import logging
    solver.logger.setLevel(logging.DEBUG)
    solver.logger.info("PyClaw Solver: " + solver_type)

    # Domain
    x = pyclaw.Dimension(0.0, 1.0, mx, name='x')
    y = pyclaw.Dimension(0.0, 1.0, my, name='y')
    z = pyclaw.Dimension(0.0, 1.0, mz, name='z')
    domain = pyclaw.Domain([x, y, z])
    num_aux = 15
    state = pyclaw.State(domain, solver.num_eqn, num_aux)

    # Define variables passed to the Riemann solver via Fortran COMMON block
    state.problem_data['gamma'] = gamma
    state.problem_data['g_r'] = accelerationDueToGravity
    state.problem_data['gravity'] = gravityTerm
    state.problem_data['gravityflux'] = False

    # Grids
    mbc = solver.num_ghost
    grid = state.grid

    dxc, dyc, dzc = domain.grid.delta[0], domain.grid.delta[
        1], domain.grid.delta[2]
    pmx, pmy, pmz = grid.num_cells[0], grid.num_cells[1], grid.num_cells[2]
    pmxBC, pmyBC, pmzBC = pmx + 2 * mbc, pmy + 2 * mbc, pmz + 2 * mbc

    centers = grid.c_centers  # cell centers
    centersBC = grid.c_centers_with_ghost(mbc)
    nodesBC = grid.c_nodes_with_ghost(mbc)

    Xcc, Ycc, Zcc = centers[0][:][:][:], centers[1][:][:][:], centers[
        2][:][:][:]
    Xcp, Ycp, Zcp = mg.mapc2pwrapper(Xcc, Ycc, Zcc, pmz, xyzMin, xyzMax,
                                     mapType)
    Xcp = np.reshape(Xcp, [pmx, pmy, pmz], order='F')  # x centers (Phys.)
    Ycp = np.reshape(Ycp, [pmx, pmy, pmz], order='F')  # y centers (Phys.)
    Zcp = np.reshape(Zcp, [pmx, pmy, pmz], order='F')  # z centers (Phys.)

    # Grid nodes With Boundary Cells (1D Slice along z)- Comp. and Phys.
    xecZ = nodesBC[0][0][0][:]  # x nodes along z (Comp.)
    yecZ = nodesBC[1][0][0][:]  # y nodes along z (Comp.)
    zecZ = nodesBC[2][0][0][:]  # z nodes along z (Comp.)
    xepZ, yepZ, zepZ = mg.mapc2pwrapper(xecZ, yecZ, zecZ, pmz, xyzMin, xyzMax,
                                        mapType)

    # Grid Centers With Boundary Cells (1D Slice along z) - Comp. and Phys.
    global xcpZ, ycpZ, zcpZ
    xccZ = centersBC[0][0][0][:]  # x centers along z (Comp.)
    yccZ = centersBC[1][0][0][:]  # y centers along z (Comp.)
    zccZ = centersBC[2][0][0][:]  # z centers along z (Comp.)
    xcpZ, ycpZ, zcpZ = mg.mapc2pwrapper(xccZ, yccZ, zccZ, pmz, xyzMin, xyzMax,
                                        mapType)

    # Equilibrium Atmosphere
    global p0, rho0, Mavg
    p0 = np.zeros([pmzBC], dtype='float', order='F')
    rho0 = np.zeros([pmzBC], dtype='float', order='F')
    Mavg = np.zeros([pmzBC], dtype='float', order='F')
    p0, rho0, Mavg = setEquilibriumAtmosphere(
        p0, rho0,
        Mavg)  # Set the equilibrium pressure such that dp/dz = -rho*gR
    altEdgesAboveEarth = np.sqrt(
        xepZ**2 + yepZ**2 + zepZ**2
    ) - radiusOfEarth  # Modify the equilibrium such that dp/dz = -rho*gR is held numerically
    p0 = modifyEquilibriumAtmosphere(altEdgesAboveEarth, p0, rho0)

    # Aux Variables
    xlower, ylower, zlower = nodesBC[0][0][0][0], nodesBC[1][0][0][0], nodesBC[
        2][0][0][0]
    global auxtmp
    auxtmp = np.zeros([num_aux, pmx + 2 * mbc, pmy + 2 * mbc, pmz + 2 * mbc],
                      dtype='float',
                      order='F')
    auxtmp = mg.setauxiliaryvariables(num_aux, mbc, pmx, pmy, pmz, xlower,
                                      ylower, zlower, dxc, dyc, dzc, xyzMin,
                                      xyzMax, mapType)
    state.aux[:, :, :, :] = auxtmp[:, mbc:-mbc, mbc:-mbc, mbc:-mbc]

    # State Variables
    q = state.q
    p = np.zeros([pmx, pmy, pmz], dtype='float', order='F')
    T = np.zeros([pmx, pmy, pmz], dtype='float', order='F')
    for i in range(np.size(p, 0)):
        for j in range(np.size(p, 1)):
            p[i, j, :] = p0[mbc:-mbc]
            q[0, i, j, :] = rho0[mbc:-mbc]
    q[1, :, :, :] = 0.
    q[2, :, :, :] = 0.
    q[3, :, :, :] = 0.
    q[4, :, :, :] = p / gamma1

    # Add Perturbation
    T[:, :, :] = p / state.q[0, :, :, :]
    L = np.sqrt((Xcp - xCenter)**2 + (Ycp - yCenter)**2 + (Zcp - zCenter)**2)
    for i in range(pmx):
        for j in range(pmy):
            for k in range(pmz):
                if L[i, j, k] <= radiusOfHotSphere:
                    # mu = Mavg[k + mbc] / nAvogadro
                    # T[i,j,k] += 11.604e3*(kBoltzmann/mu)*(1.0-L[i,j,k]/radiusOfHotSphere)
                    # p[i,j,k] = T[i,j,k]*state.q[0,i,j,k]
                    # print("i:"+str(i)+" j:"+str(j)+" k:"+str(k))
                    p[i, j, k] += 11.604e3*kBoltzmann*nAvogadro/Mavg[k+mbc]*q[0, i, j, k]\
                        * (1.0-L[i, j, k]/radiusOfHotSphere)
    q[4, :, :, :] = p / gamma1

    # solver.logger.info("Temperature Min/Max: "+str(np.min(p*Mavg/nAvogadro/kBoltzmann))+"/"+str(np.max(p*Mavg/nAvogadro/kBoltzmann)) )
    # solver.logger.info("Temperature Min/Max: " + str(np.min(T)) + "/" + str(np.max(T)))

    # Index for Capacity function in state.aux (Python 0-based)
    state.index_capa = 12

    # Boundary Conditions
    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
    solver.bc_lower[2] = pyclaw.BC.custom
    solver.bc_upper[2] = pyclaw.BC.custom
    solver.user_bc_lower = customBCLowerZ
    solver.user_bc_upper = customBCUpperZ

    # Aux - Boundary Conditions
    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
    solver.aux_bc_lower[2] = pyclaw.BC.custom
    solver.aux_bc_upper[2] = pyclaw.BC.custom
    solver.user_aux_bc_lower = customAuxBCLowerZ
    solver.user_aux_bc_upper = customAuxBCUpperZ

    # Setup Controller
    claw = pyclaw.Controller()
    claw.verbosity = 4
    claw.solution = pyclaw.Solution(state, domain)
    claw.write_aux_init = True
    claw.solver = solver
    claw.output_format = output_format
    claw.output_file_prefix = file_prefix
    if not use_petsc: claw.output_options = {'compression': 'gzip'}
    claw.write_aux_always = False
    claw.keep_copy = True
    claw.tfinal = tfinal
    claw.num_output_times = num_output_times
    claw.outdir = outdir
    claw.output_style = output_style
    if output_style == 2:
        claw.out_times = out_times

    # Print output times
    if output_style == 1:
        outTimes = np.linspace(claw.solution.t, tfinal, num_output_times + 1)
    elif output_style == 2:
        outTimes = claw.out_times
    print("Planned output times: [" + " ".join(str(e) for e in outTimes) + "]")

    return PyClawModel(claw)