Esempio n. 1
0
def test_force_guo(ForceType, device):
    dtype = torch.double
    lattice = Lattice(D2Q9, dtype=dtype, device=device)
    flow = PoiseuilleFlow2D(resolution=10,
                            reynolds_number=1,
                            mach_number=0.02,
                            lattice=lattice,
                            initialize_with_zeros=True)
    force = ForceType(lattice,
                      tau=flow.units.relaxation_parameter_lu,
                      acceleration=flow.units.convert_acceleration_to_lu(
                          flow.acceleration))
    collision = BGKCollision(lattice,
                             tau=flow.units.relaxation_parameter_lu,
                             force=force)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    simulation.step(500)
    # compare with reference solution
    u_sim = flow.units.convert_velocity_to_pu(
        lattice.convert_to_numpy(lattice.u(simulation.f)))
    _, u_ref = flow.analytic_solution(flow.grid)
    fluidnodes = np.where(np.logical_not(flow.boundaries[0].mask.cpu()))
    assert u_ref[0].max() == pytest.approx(u_sim[0].max(), rel=0.005)
    assert u_ref[0][fluidnodes] == pytest.approx(u_sim[0][fluidnodes],
                                                 rel=None,
                                                 abs=0.005 * u_ref[0].max())
Esempio n. 2
0
def test_initialize_fneq(Case, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, device, dtype)
    if Case == TaylorGreenVortex3D:
        lattice = Lattice(D3Q27, dtype=dtype, device=device)
    flow = Case(resolution=16, reynolds_number=1000, mach_number=0.01, lattice=lattice)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation_neq = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming)

    pre_rho = lattice.rho(simulation_neq.f)
    pre_u = lattice.u(simulation_neq.f)

    simulation_neq.initialize_f_neq()

    post_rho = lattice.rho(simulation_neq.f)
    post_u = lattice.u(simulation_neq.f)
    assert(torch.allclose(pre_rho,post_rho,1e-6))
    assert(torch.allclose(pre_u,post_u))

    if Case == TaylorGreenVortex2D:
        error_reporter_neq = ErrorReporter(lattice, flow, interval=1, out=None)
        error_reporter_eq = ErrorReporter(lattice, flow, interval=1, out=None)
        simulation_eq = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming)
        simulation_neq.reporters.append(error_reporter_neq)
        simulation_eq.reporters.append(error_reporter_eq)

        simulation_neq.step(10)
        simulation_eq.step(10)
        error_u, error_p = np.mean(np.abs(error_reporter_neq.out), axis=0).tolist()
        error_u_eq, error_p_eq = np.mean(np.abs(error_reporter_eq.out), axis=0).tolist()

        assert(error_u < error_u_eq)
Esempio n. 3
0
def test_HDF5Reporter(tmpdir):
    lattice = Lattice(D2Q9, "cpu")
    flow = TaylorGreenVortex2D(resolution=16,
                               reynolds_number=10,
                               mach_number=0.05,
                               lattice=lattice)
    collision = BGKCollision(lattice=lattice,
                             tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice=lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    hdf5_reporter = HDF5Reporter(flow=flow,
                                 collision=collision,
                                 interval=1,
                                 filebase=tmpdir / "output")
    simulation.reporters.append(hdf5_reporter)
    simulation.step(3)
    assert os.path.isfile(tmpdir / "output.h5")

    dataset_train = LettuceDataset(filebase=tmpdir / "output.h5", target=True)
    train_loader = torch.utils.data.DataLoader(dataset_train, shuffle=False)
    print(dataset_train)
    for (f, target, idx) in train_loader:
        assert idx in (0, 1, 2)
        assert f.shape == (1, 9, 16, 16)
        assert target.shape == (1, 9, 16, 16)
Esempio n. 4
0
def test_obstacle(stencil, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(stencil, dtype=dtype, device=device)

    if stencil is D2Q9:
        mask = np.zeros([20, 10])
        mask[3:6, 3:6] = 1
        flow = Obstacle2D(20, 10, 100, 0.1, lattice=lattice, char_length_lu=3)
    if stencil is D3Q27:
        mask = np.zeros([20, 10, 5])
        mask[3:6, 3:6, :] = 1
        flow = Obstacle3D(20,
                          10,
                          5,
                          100,
                          0.1,
                          lattice=lattice,
                          char_length_lu=3)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    flow.mask = mask != 0
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    simulation.step(2)
Esempio n. 5
0
def test_flow_3d(IncompressibleFlow, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D3Q27, dtype=dtype, device=device)
    flow = IncompressibleFlow(16, 1, 0.05, lattice=lattice)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming)
    simulation.step(1)
Esempio n. 6
0
def test_save_and_load(dtype_device, tmpdir):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, device, dtype)
    flow = TaylorGreenVortex2D(resolution=16, reynolds_number=10, mach_number=0.05, lattice=lattice)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming)
    simulation.step(10)
    simulation.save_checkpoint(tmpdir / "checkpoint.pic")
    simulation2 = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming)
    simulation2.load_checkpoint(tmpdir / "checkpoint.pic")
    assert lattice.convert_to_numpy(simulation2.f) == pytest.approx(lattice.convert_to_numpy(simulation.f))
Esempio n. 7
0
def test_readme():
    """Whenever you have to change this test, the example in README.rst has to change, too.
    Note differences in the device + number of steps.
    """

    import torch
    from lettuce import BGKCollision, StandardStreaming, Lattice, D2Q9, TaylorGreenVortex2D, Simulation

    device = "cpu"
    dtype = torch.float32

    lattice = Lattice(D2Q9, device, dtype)
    flow = TaylorGreenVortex2D(resolution=256,
                               reynolds_number=10,
                               mach_number=0.05,
                               lattice=lattice)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    mlups = simulation.step(num_steps=1)

    print("Performance in MLUPS:", mlups)
Esempio n. 8
0
def convergence(ctx, init_f_neq):
    """Use Taylor Green 2D for convergence test in diffusive scaling."""
    device, dtype = ctx.obj['device'], ctx.obj['dtype']
    lattice = Lattice(D2Q9, device, dtype)
    error_u_old = None
    error_p_old = None
    print(("{:>15} " * 5).format("resolution", "error (u)", "order (u)",
                                 "error (p)", "order (p)"))

    for i in range(4, 9):
        resolution = 2**i
        mach_number = 8 / resolution

        # Simulation
        flow = TaylorGreenVortex2D(resolution=resolution,
                                   reynolds_number=10000,
                                   mach_number=mach_number,
                                   lattice=lattice)
        collision = BGKCollision(lattice,
                                 tau=flow.units.relaxation_parameter_lu)
        streaming = StandardStreaming(lattice)
        simulation = Simulation(flow=flow,
                                lattice=lattice,
                                collision=collision,
                                streaming=streaming)
        if (init_f_neq):
            simulation.initialize_f_neq()
        error_reporter = ErrorReporter(lattice, flow, interval=1, out=None)
        simulation.reporters.append(error_reporter)
        for _ in range(10 * resolution):
            simulation.step(1)
        error_u, error_p = np.mean(np.abs(error_reporter.out), axis=0).tolist()
        factor_u = 0 if error_u_old is None else error_u_old / error_u
        factor_p = 0 if error_p_old is None else error_p_old / error_p
        error_u_old = error_u
        error_p_old = error_p
        print("{:15} {:15.2e} {:15.1f} {:15.2e} {:15.1f}".format(
            resolution, error_u, factor_u / 2, error_p, factor_p / 2))
    if factor_u / 2 < 1.9:
        print("Velocity convergence order < 2.")
    if factor_p / 2 < 0.9:
        print("Velocity convergence order < 1.")
    if factor_u / 2 < 1.9 or factor_p / 2 < 0.9:
        sys.exit(1)
    else:
        return 0
Esempio n. 9
0
def test_generic_reporters(Reporter, Case, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, dtype=dtype, device=device)
    flow = Case(64, 10000, 0.05, lattice=lattice)
    if Case == TaylorGreenVortex3D:
        lattice = Lattice(D3Q27, dtype=dtype, device=device)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    kinE_reporter = Reporter(lattice, flow, interval=1, out=None)
    simulation.reporters.append(kinE_reporter)
    simulation.step(2)
    assert (np.asarray(kinE_reporter.out)[1, 1] == pytest.approx(np.asarray(
        kinE_reporter.out)[0, 1],
                                                                 rel=0.05))
Esempio n. 10
0
def test_vtk_reporter_no_mask(tmpdir):
    lattice = Lattice(D2Q9, "cpu")
    flow = TaylorGreenVortex2D(resolution=16,
                               reynolds_number=10,
                               mach_number=0.05,
                               lattice=lattice)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    vtk_reporter = VTKReporter(lattice,
                               flow,
                               interval=1,
                               filename_base=tmpdir / "output")
    simulation.reporters.append(vtk_reporter)
    simulation.step(2)
    assert os.path.isfile(tmpdir / "output_00000000.vtr")
    assert os.path.isfile(tmpdir / "output_00000001.vtr")
Esempio n. 11
0
def test_equilibrium_pressure_outlet(dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, dtype=dtype, device=device)

    class MyObstacle(Obstacle2D):
        @property
        def boundaries(self, *args):
            x, y = self.grid
            return [
                EquilibriumBoundaryPU(
                    np.abs(x) < 1e-6, self.units.lattice, self.units,
                    np.array([self.units.characteristic_velocity_pu, 0])),
                EquilibriumOutletP(self.units.lattice, [0, -1]),
                EquilibriumOutletP(self.units.lattice, [0, 1]),
                EquilibriumOutletP(self.units.lattice, [1, 0]),
                BounceBackBoundary(self.mask, self.units.lattice)
            ]

    flow = MyObstacle(30,
                      30,
                      reynolds_number=10,
                      mach_number=0.1,
                      lattice=lattice,
                      char_length_lu=10)
    mask = np.zeros_like(flow.grid[0], dtype=np.bool)
    mask[10:20, 10:20] = 1
    flow.mask = mask
    simulation = Simulation(
        flow, lattice,
        RegularizedCollision(lattice, flow.units.relaxation_parameter_lu),
        StandardStreaming(lattice))
    simulation.step(20)
    rho = lattice.rho(simulation.f)
    u = lattice.u(simulation.f)
    feq = lattice.equilibrium(torch.ones_like(rho), u)
    p = flow.units.convert_density_lu_to_pressure_pu(rho)
    zeros = torch.zeros_like(p[0, -1, :])
    assert torch.allclose(zeros, p[:, -1, :], rtol=0, atol=1e-4)
    assert torch.allclose(zeros, p[:, :, 0], rtol=0, atol=1e-4)
    assert torch.allclose(zeros, p[:, :, -1], rtol=0, atol=1e-4)
    assert torch.allclose(feq[:, -1, 1:-1], feq[:, -2, 1:-1])
Esempio n. 12
0
def test_generic_reporters(Observable, Case, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, dtype=dtype, device=device)
    flow = Case(32, 10000, 0.05, lattice=lattice)
    if Case == TaylorGreenVortex3D:
        lattice = Lattice(D3Q27, dtype=dtype, device=device)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    reporter = ObservableReporter(Observable(lattice, flow),
                                  interval=1,
                                  out=None)
    simulation.reporters.append(reporter)
    simulation.step(2)
    values = np.asarray(reporter.out)
    if Observable is EnergySpectrum:
        assert values[1, 2:] == pytest.approx(values[0, 2:],
                                              rel=0.0,
                                              abs=values[0, 2:].sum() / 10)
    else:
        assert values[1, 2] == pytest.approx(values[0, 2], rel=0.05)
Esempio n. 13
0
def benchmark(ctx, steps, resolution, profile_out, flow, vtk_out):
    """Run a short simulation and print performance in MLUPS.
    """
    # start profiling
    if profile_out:
        profile = cProfile.Profile()
        profile.enable()

    # setup and run simulation
    device, dtype = ctx.obj['device'], ctx.obj['dtype']
    flow_class, stencil = flow_by_name[flow]
    lattice = Lattice(stencil, device, dtype)
    flow = flow_class(resolution=resolution,
                      reynolds_number=1,
                      mach_number=0.05,
                      lattice=lattice)
    force = Guo(lattice,
                tau=flow.units.relaxation_parameter_lu,
                acceleration=flow.units.convert_acceleration_to_lu(
                    flow.force)) if hasattr(flow, "acceleration") else None
    collision = BGKCollision(lattice,
                             tau=flow.units.relaxation_parameter_lu,
                             force=force)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    if vtk_out:
        simulation.reporters.append(VTKReporter(lattice, flow, interval=10))
    mlups = simulation.step(num_steps=steps)

    # write profiling output
    if profile_out:
        profile.disable()
        stats = pstats.Stats(profile)
        stats.sort_stats('cumulative')
        stats.print_stats()
        profile.dump_stats(profile_out)
        click.echo(f"Saved profiling information to {profile_out}.")

    click.echo("Finished {} steps in {} bit precision. MLUPS: {:10.2f}".format(
        steps,
        str(dtype).replace("torch.float", ""), mlups))
    return 0
Esempio n. 14
0
                                 out=None)
energy_f = ObservableReporter(IncompressibleKineticEnergy(lattice, flow_fine),
                              interval=postplotinterval * 2,
                              out=None)
spectrum_f = ObservableReporter(EnergySpectrum(lattice, flow_fine),
                                interval=postplotinterval * 2,
                                out=None)
f_reporter = FReporter(2 * data_interval)
simulation.reporters.append(f_reporter)
simulation.reporters.append(enstrophy_f)
simulation.reporters.append(energy_f)
simulation.reporters.append(spectrum_f)

simulation.initialize_pressure()
simulation.initialize_f_neq()
_ = simulation.step(2 * n_steps)

########################################################################################################################
##### Make Data Pairs
########################################################################################################################
print('Make Data Pairs and shuffle training data')
data = torch.stack(f_reporter.fs).cpu()
f_reporter.fs = []

inputs = data[0:-1]
outputs = data[1:]

f_init = inputs[nr_init].clone()
f_output = outputs[-1].clone()

shuffle = torch.randperm(inputs.shape[0])