Beispiel #1
0
    def initialize(self):
        """
        Initialize the grid and variables for advection and set the initial
        conditions for the chosen problem.
        """

        my_grid = grid_setup_1d(self.rp, ng=4)

        # create the variables
        my_data = patch.CellCenterData1d(my_grid)
        bc = bc_setup_1d(self.rp)[0]
        my_data.register_var("density", bc)
        my_data.create()

        self.cc_data = my_data

        if self.rp.get_param("particles.do_particles") == 1:
            n_particles = self.rp.get_param("particles.n_particles")
            particle_generator = self.rp.get_param(
                "particles.particle_generator")
            self.particles = particles.Particles(self.cc_data, bc, n_particles,
                                                 particle_generator)

        # now set the initial conditions for the problem
        problem = importlib.import_module("advection_1d.problems.{}".format(
            self.problem_name))
        problem.init_data(self.cc_data, self.rp)
Beispiel #2
0
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])
Beispiel #3
0
    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()
Beispiel #4
0
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())
Beispiel #5
0
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