Beispiel #1
0
def dump(ndim, path, quantity):

    origin = [0] * ndim

    coarseLayout = gridlayout.GridLayout(
        Box([0] * ndim, [39] * ndim), origin=origin, dl=[0.2] * ndim
    )

    fineLayout = gridlayout.GridLayout(
        Box([18] * ndim, [37] * ndim), origin=origin, dl=[0.1] * ndim
    )

    domainSize = coarseLayout.box.shape * coarseLayout.dl

    coarseCoords, fineCoords, is_primal = [], [], []

    for direction in directions[:ndim]:
        coarseCoords += [coarseLayout.yeeCoordsFor(quantity, direction)]
        fineCoords += [fineLayout.yeeCoordsFor(quantity, direction)]
        is_primal += [gridlayout.yee_element_is_primal(quantity, direction)]

    lower = [10] * ndim
    upper = [15 + primal for primal in is_primal]
    coarseBox = Box(lower, np.asarray(upper) - 1)

    for fn_idx, function in enumerate(functionList):
        fine = function(fineCoords, domainSize)
        coarse = function(coarseCoords, domainSize)
        afterCoarse = np.copy(coarse)

        coarseField = FieldData(coarseLayout, quantity, data=coarse)
        fineField   = FieldData(fineLayout, quantity, data=fine)
        coarsen(quantity, coarseField, fineField, coarseBox, fine, afterCoarse)

        if fn_idx == 0:
            fineData = np.copy(fine)
            coarseData = np.copy(coarse)
            afterCoarseData = np.copy(afterCoarse)
        else:
            fineData = np.vstack((fineData, fine))
            coarseData = np.vstack((coarseData, coarse))
            afterCoarseData = np.vstack((afterCoarseData, afterCoarse))

    file_datas = {
        f"{quantity}_fine_original{ndim}d.txt": fineData,
        f"{quantity}_coarse_original{ndim}d.txt": coarseData,
        f"{quantity}_coarse_linear_coarsed_{ndim}d.txt": afterCoarseData,
    }

    for filename, data in file_datas.items():
        np.savetxt(os.path.join(path, filename), data, delimiter=" ")
Beispiel #2
0
def refine(field, **kwargs):
    """
      optional kwargs
        data : for overriding the field dataset so you don't have to make a temporary copy of a field just to have a different dataset
    """
    assert isinstance(field, FieldData)
    kwarg_keys = ["data"]
    assert any([key in kwarg_keys for key in list(kwargs.keys())])

    refinement_ratio = 2
    primal_directions = field.primal_directions()

    data = kwargs.get("data", field.dataset[:])
    if "data" in kwargs:
        assert data.shape == field.dataset.shape

    fine_box    = boxm.refine(field.box, refinement_ratio)
    fine_layout = GridLayout(fine_box, field.origin, field.layout.dl/refinement_ratio, interp_order=field.layout.interp_order)

    assert field.box.ndim == 1
    fine_data = np.zeros(fine_box.shape + primal_directions + (field.ghosts_nbr[0] * 2))

    ghostX = field.ghosts_nbr[0]
    assert ghostX > 0

    cadence = 2

    if primal_directions[0]:
        fine_data[ghostX:-ghostX:cadence] = data[ghostX:-ghostX] # coarse primal on top of fine
        fine_data[ghostX + 1:-(ghostX - 1):cadence] = 0.5*data[ghostX:-ghostX] + 0.5*data[ghostX + 1:-(ghostX - 1)]
    else:
        fine_data[ghostX:-(ghostX + 1):cadence] = 0.25*data[ghostX - 1:-(ghostX + 1)] + 0.75*data[ghostX:-ghostX]
        fine_data[ghostX + 1:-(ghostX - 1):cadence] = 0.25*data[ghostX + 1:-(ghostX - 1)] + 0.75*data[ghostX:-ghostX]

    return FieldData(fine_layout, field.field_name, data=fine_data)
Beispiel #3
0
def build_patch_datas(domain_box, boxes, **kwargs):
    quantities = kwargs["quantities"]
    origin = kwargs["origin"]
    interp_order = kwargs["interp_order"]
    domain_size = kwargs["domain_size"]
    cell_width = kwargs["cell_width"]
    refinement_ratio = kwargs["refinement_ratio"]

    skip_particles = False
    if "particles" in quantities:
        del quantities[quantities.index("particles")]
    else:
        skip_particles = True

    domain_layout = GridLayout(domain_box, origin, cell_width, interp_order)

    coarse_particles = Particles(box=domain_box)

    # copy domain particles and put them in ghost cells
    particle_ghost_nbr = domain_layout.particleGhostNbr(interp_order)
    box_extend = particle_ghost_nbr - 1

    upper_slct_box = Box(domain_box.upper - box_extend, domain_box.upper)
    lower_slct_box = Box(domain_box.lower, domain_box.lower + box_extend)

    if not skip_particles:
        coarse_particles = Particles(box=domain_box)
        upper_cell_particles = coarse_particles.select(upper_slct_box)
        lower_cell_particles = coarse_particles.select(lower_slct_box)

        coarse_particles.add(
            upper_cell_particles.shift_icell(-domain_box.shape()))
        coarse_particles.add(
            lower_cell_particles.shift_icell(domain_box.shape()))

    patch_datas = {}

    for ilvl, lvl_box in boxes.items():

        lvl_cell_width = cell_width / (refinement_ratio**ilvl)

        if not skip_particles:
            if ilvl == 0:
                lvl_particles = coarse_particles
            else:
                level_domain_box = boxm.refine(domain_box, refinement_ratio)
                lvl_ghost_domain_box = boxm.grow(
                    level_domain_box,
                    domain_layout.particleGhostNbr(interp_order))
                lvl_particles = Particles(box=lvl_ghost_domain_box)

        if ilvl not in patch_datas:
            patch_datas[ilvl] = []

        for box in lvl_box:

            ghost_box = boxm.grow(box, 5)
            origin = box.lower * lvl_cell_width
            layout = GridLayout(box, origin, lvl_cell_width, interp_order)

            datas = {
                qty: globals()[qty](ghost_box, layout, domain_size)
                for qty in quantities
            }

            if not skip_particles:
                datas["particles"] = lvl_particles.select(ghost_box)

            boxed_patch_datas = {}
            for qty_name, data in datas.items():
                if qty_name == 'particles':
                    pdata = ParticleData(layout, data, "pop_name")
                else:
                    pdata = FieldData(layout, qty_name, data)

                boxed_patch_datas[qty_name] = pdata

            patch_datas[ilvl].append(boxed_patch_datas)

    return patch_datas
Beispiel #4
0
def refine(field, **kwargs):
    """
    optional kwargs
      data : for overriding the field dataset so you don't have to make a temporary copy of a field just to have a different dataset
    """
    assert isinstance(field, FieldData)
    kwarg_keys = ["data"]
    assert any([key in kwarg_keys for key in list(kwargs.keys())])

    refinement_ratio = 2
    primal_directions = field.primal_directions()

    data = kwargs.get("data", field.dataset[:])
    if "data" in kwargs:
        assert data.shape == field.dataset.shape

    fine_layout = GridLayout(
        boxm.refine(field.box, refinement_ratio),
        field.origin,
        field.layout.dl / refinement_ratio,
        interp_order=field.layout.interp_order,
    )

    # fine box has extra ghosts for padding against coarser such that at the normal number of ghosts are filled
    fine_box = boxm.shrink(boxm.refine(field.ghost_box, refinement_ratio),
                           field.ghosts_nbr)
    fine_data = np.zeros(fine_box.shape + primal_directions +
                         (field.ghosts_nbr * 2))

    ghostX = field.ghosts_nbr[0]
    assert ghostX > 1

    cadence = 2
    assert cadence == refinement_ratio

    gX = 2  # level 0 ghost buffer from edge of normal coarse data box
    rgX = 4  # level 1 ghost buffer from edge of extra large fine data box

    if field.box.ndim == 1:
        if primal_directions[0]:
            # coarse primal on top of fine
            fine_data[rgX:-rgX:cadence] = data[gX:-gX]
            fine_data[rgX +
                      1:-(rgX - 1):cadence] = (0.5 * data[gX:-gX] +
                                               0.5 * data[gX + 1:-(gX - 1)])
        else:
            fine_data[rgX:-(rgX + 1):cadence] = (
                0.25 * data[gX - 1:-(gX + 1)] + 0.75 * data[gX:-gX])
            fine_data[rgX +
                      1:-(rgX - 1):cadence] = (0.25 * data[gX + 1:-(gX - 1)] +
                                               0.75 * data[gX:-gX])

    if fine_box.ndim > 1:
        assert field.ghosts_nbr[1] > 1
        gY = 2
        rgY = 4
        cad = cadence

    if fine_box.ndim == 2:
        if all(primal_directions):
            fine_data[rgX:-rgX:cad, rgY:-rgY:cad] = data[gX:-gX, gY:-gY]
            fine_data[rgX + 1:-(rgX - 1):cad,
                      rgY:-rgY:cad] = (0.5 * data[gX:-gX, gY:-gY] +
                                       0.5 * data[gX + 1:-(gX - 1), gY:-gY])
            fine_data[rgX:-rgX:cad, rgY + 1:-(rgY - 1):cad] = (
                0.5 * data[gX:-gX, gY:-gY] +
                0.5 * data[gX:-gX, gY + 1:-(gY - 1)])
            fine_data[rgX + 1:-(rgX - 1):cad, rgY + 1:-(rgY - 1):cad] = (
                0.25 * data[gX:-gX, gY:-gY] +
                0.25 * data[gX + 1:-(gX - 1), gY:-gY] +
                0.25 * data[gX:-gX, gY + 1:-(gY - 1)] +
                0.25 * data[gX + 1:-(gX - 1), gY + 1:-(gY - 1)])

        elif primal_directions[0] and not primal_directions[1]:
            fine_data[rgX:-rgX:cad,
                      rgY:-rgY:cad] = (0.25 * data[gX:-gX, gY - 1:-(gY + 1)] +
                                       0.75 * data[gX:-gX, gY:-gY])
            fine_data[rgX + 1:-(rgX - 1):cad, rgY:-rgY:cad] = 0.75 * (
                0.5 * data[gX + 1:-(gX - 1), gY:-gY] +
                0.5 * data[gX:-gX, gY:-gY]) + 0.25 * (
                    0.5 * data[gX + 1:-(gX - 1), gY - 1:-(gY + 1)] +
                    0.5 * data[gX:-gX, gY - 1:-(gY + 1)])
            fine_data[rgX:-rgX:cad, rgY + 1:-(rgY - 1):cad] = (
                0.25 * data[gX:-gX, gY + 1:-(gY - 1)] +
                0.75 * data[gX:-gX, gY:-gY])
            fine_data[rgX + 1:-(rgX - 1):cad,
                      rgY + 1:-(rgY - 1):cad] = 0.75 * (
                          0.5 * data[gX + 1:-(gX - 1), gY:-gY] +
                          0.5 * data[gX:-gX, gY:-gY]) + 0.25 * (
                              0.5 * data[gX + 1:-(gX - 1), gY + 1:-(gY - 1)] +
                              0.5 * data[gX:-gX, gY + 1:-(gY - 1)])

        elif not primal_directions[0] and primal_directions[1]:
            fine_data[rgX:-rgX:cad,
                      rgY:-rgY:cad] = (0.25 * data[gX - 1:-(gX + 1), gY:-gY] +
                                       0.75 * data[gX:-gX, gY:-gY])
            fine_data[rgX + 1:-(rgX - 1):cad,
                      rgY:-rgY:cad] = (0.25 * data[gX + 1:-(gX - 1), gY:-gY] +
                                       0.75 * data[gX:-gX, gY:-gY])
            fine_data[rgX:-rgX:cad, rgY + 1:-(rgY - 1):cad] = 0.5 * (
                0.25 * data[gX - 1:-(gX + 1), gY:-gY] +
                0.75 * data[gX:-gX, gY:-gY]) + 0.5 * (
                    0.25 * data[gX - 1:-(gX + 1), gY + 1:-(gY - 1)] +
                    0.75 * data[gX:-gX, gY + 1:-(gY - 1)])
            fine_data[rgX + 1:-(rgX - 1):cad, rgY + 1:-(rgY - 1):cad] = 0.5 * (
                0.25 * data[gX + 1:-(gX - 1), gY:-gY] +
                0.75 * data[gX:-gX, gY:-gY]) + 0.5 * (
                    0.25 * data[gX + 1:-(gX - 1), gY + 1:-(gY - 1)] +
                    0.75 * data[gX:-gX, gY + 1:-(gY - 1)])

        elif not any(primal_directions):
            fine_data[rgX:-rgX:cad, rgY:-rgY:cad] = 0.75 * (
                0.25 * data[gX - 1:-(gX + 1), gY:-gY] +
                0.75 * data[gX:-gX, gY:-gY]) + 0.25 * (
                    0.25 * data[gX - 1:-(gX + 1), gY - 1:-(gY + 1)] +
                    0.75 * data[gX:-gX, gY - 1:-(gY + 1)])
            fine_data[rgX + 1:-(rgX - 1):cad, rgY:-rgY:cad] = 0.75 * (
                0.25 * data[gX + 1:-(gX - 1), gY:-gY] +
                0.75 * data[gX:-gX, gY:-gY]) + 0.25 * (
                    0.25 * data[gX + 1:-(gX - 1), gY - 1:-(gY + 1)] +
                    0.75 * data[gX:-gX, gY - 1:-(gY + 1)])
            fine_data[rgX:-rgX:cad, rgY + 1:-(rgY - 1):cad] = 0.75 * (
                0.25 * data[gX - 1:-(gX + 1), gY:-gY] +
                0.75 * data[gX:-gX, gY:-gY]) + 0.25 * (
                    0.25 * data[gX - 1:-(gX + 1), gY + 1:-(gY - 1)] +
                    0.75 * data[gX:-gX, gY + 1:-(gY - 1)])
            fine_data[rgX + 1:-(rgX - 1):cad,
                      rgY + 1:-(rgY - 1):cad] = 0.75 * (
                          0.25 * data[gX + 1:-(gX - 1), gY:-gY] +
                          0.75 * data[gX:-gX, gY:-gY]) + 0.25 * (
                              0.25 * data[gX + 1:-(gX - 1), gY + 1:-(gY - 1)] +
                              0.75 * data[gX:-gX, gY + 1:-(gY - 1)])

    # the fine data is ghost_nbr larger than expected to include ghost values from the coarser, so we drop the outer values
    if field.box.ndim == 1:
        fine_data = fine_data[field.ghosts_nbr[0]:-field.ghosts_nbr[0]]
    elif field.box.ndim == 2:
        fine_data = fine_data[field.ghosts_nbr[0]:-field.ghosts_nbr[0],
                              field.ghosts_nbr[1]:-field.ghosts_nbr[1], ]
    elif field.box.ndim == 3:
        fine_data = fine_data[field.ghosts_nbr[0]:-field.ghosts_nbr[0],
                              field.ghosts_nbr[1]:-field.ghosts_nbr[1],
                              field.ghosts_nbr[2]:-field.ghosts_nbr[2], ]
    return FieldData(fine_layout, field.field_name, data=fine_data)