def test_is_asymmetric_1d(): g = patch.Grid1d(4, ng=0) a = g.scratch_array() a[:] = [-1, -2, 2, 1] assert a.is_asymmetric()
def test_bcs_1d(): myg = patch.Grid1d(4, ng=2, xmax=1.0) myd = patch.CellCenterData1d(myg, dtype=np.int) bco = bnd.BC(xlb="outflow", xrb="outflow") myd.register_var("outflow", bco) bcp = bnd.BC(xlb="periodic", xrb="periodic") myd.register_var("periodic", bcp) bcre = bnd.BC(xlb="reflect-even", xrb="reflect-even") myd.register_var("reflect-even", bcre) bcro = bnd.BC(xlb="reflect-odd", xrb="reflect-odd") myd.register_var("reflect-odd", bcro) myd.create() a = myd.get_var("outflow") a.v()[:] = np.array([1, 2, 3, 4], dtype=int) b = myd.get_var("periodic") c = myd.get_var("reflect-even") d = myd.get_var("reflect-odd") b[:] = a[:] c[:] = a[:] d[:] = a[:] myd.fill_BC("outflow") # left ghost assert a[myg.ilo - 1] == 1 # right ghost assert a[myg.ihi + 1] == 4 myd.fill_BC("periodic") # x-boundaries assert b[myg.ilo - 1] == b[myg.ihi] assert b[myg.ilo] == b[myg.ihi + 1] myd.fill_BC("reflect-even") # left -- we'll check 2 ghost cells here # left assert_array_equal(c[myg.ilo:myg.ilo + 2], c[myg.ilo - 2:myg.ilo][::-1]) # right assert_array_equal(c[myg.ihi - 1:myg.ihi + 1], c[myg.ihi + 1:myg.ihi + 3][::-1]) myd.fill_BC("reflect-odd") # left -- we'll check 2 ghost cells here # left assert_array_equal(d[myg.ilo:myg.ilo + 2], -1.0 * d[myg.ilo - 2:myg.ilo][::-1]) # right assert_array_equal(d[myg.ihi - 1:myg.ihi + 1], -1.0 * d[myg.ihi + 1:myg.ihi + 3][::-1])
def test_indexer_1d(): g = patch.Grid1d(3, ng=2) a = g.scratch_array() a[:] = np.arange(g.qx) assert_array_equal(a.v(), np.array([2., 3., 4.])) assert_array_equal(a.ip(1), np.array([3., 4., 5.])) assert_array_equal(a.ip(-1), np.array([1., 2., 3.]))
def setup_method(self): """ this is run before each test """ nx = 8 self.g = patch.Grid1d(nx, ng=2, xmax=1.0) self.d = patch.CellCenterData1d(self.g, dtype=np.int) bco = bnd.BC1d(xlb="outflow", xrb="outflow") self.d.register_var("a", bco) self.d.register_var("b", bco) self.d.create()
def grid_setup_1d(rp, ng=1): nx = rp.get_param("mesh.nx") try: xmin = rp.get_param("mesh.xmin") except KeyError: xmin = 0.0 msg.warning("mesh.xmin not set, defaulting to 0.0") try: xmax = rp.get_param("mesh.xmax") except KeyError: xmax = 1.0 msg.warning("mesh.xmax not set, defaulting to 1.0") my_grid = patch.Grid1d(nx, xmin=xmin, xmax=xmax, ng=ng) return my_grid
def test_write_read_1d(): myg = patch.Grid1d(8, ng=2, xmax=1.0) myd = patch.CellCenterData1d(myg) bco = bnd.BC(xlb="outflow", xrb="outflow") myd.register_var("a", bco) myd.create() a = myd.get_var("a") a.v()[:] = np.arange(8) myd.write("io_test_1d") # now read it in nd = io.read_1d("io_test_1d") anew = nd.get_var("a") assert_array_equal(anew.v(), a.v())
def read_1d(filename): """read an HDF5 file and recreate the simulation object that holds the data and state of the simulation. """ if not filename.endswith(".h5"): filename += ".h5" with h5py.File(filename, "r") as f: # read the simulation information -- this only exists if the # file was created as a simulation object try: solver_name = f.attrs["solver"] problem_name = f.attrs["problem"] t = f.attrs["time"] n = f.attrs["nsteps"] except KeyError: # this was just a patch written out solver_name = None # read in the grid info and create our grid grid = f["grid"].attrs myg = patch.Grid1d(grid["nx"], ng=grid["ng"], xmin=grid["xmin"], xmax=grid["xmax"]) # sometimes problems define custom BCs -- at the moment, we # are going to assume that these always map to BC.user. We # need to read these in now, since the variable creation # requires it. custom_bcs = read_bcs(f) if custom_bcs is not None: if solver_name in [ "compressible_fv4", "compressible_rk", "compressible_sdc" ]: bc_solver = "compressible" else: bc_solver = solver_name bcmod = importlib.import_module("{}.{}".format(bc_solver, "BC")) for name in custom_bcs: bnd.define_bc(name, bcmod.user, is_solid=custom_bcs[name]) # read in the variable info -- start by getting the names gs = f["state"] names = [] for n in gs: names.append(n) # create the CellCenterData1d object myd = patch.CellCenterData1d(myg) for n in names: grp = gs[n] bc = bnd.BC1d(xlb=grp.attrs["xlb"], xrb=grp.attrs["xrb"]) myd.register_var(n, bc) myd.create() # auxillary data for k in f["aux"].attrs: myd.set_aux(k, f["aux"].attrs[k]) # restore the variable data for n in names: grp = gs[n] data = grp["data"] v = myd.get_var(n) v.v()[:] = data[:] # restore the particle data try: gparticles = f["particles"] particle_data = gparticles["particle_positions"] init_data = gparticles["init_particle_positions"] my_particles = particles.Particles(myd, None, len(particle_data), "array", particle_data, init_data) except KeyError: my_particles = None if solver_name is not None: solver = importlib.import_module(solver_name) sim = solver.Simulation(solver_name, problem_name, None) sim.n = n sim.cc_data = myd sim.cc_data.t = t sim.particles = my_particles sim.read_extras(f) if solver_name is not None: return sim return myd
def test_equality(self): g2 = patch.Grid1d(5, ng=1) assert g2 != self.g
def setup_method(self): """ this is run before each test """ self.g = patch.Grid1d(6, ng=2, xmax=1.5)