Beispiel #1
0
def test_grid_fine_to_coarse_2d():
    lattice = Lattice(D2Q9, 'cpu', dtype=torch.double)
    # streaming = StandardStreaming(lattice)

    flow_f = TaylorGreenVortex2D(40, 1600, 0.15, lattice)
    collision_f = BGKCollision(lattice,
                               tau=flow_f.units.relaxation_parameter_lu)
    sim_f = Simulation(flow_f, lattice, collision_f, streaming=None)

    flow_c = TaylorGreenVortex2D(20, 1600, 0.15, lattice)
    collision_c = BGKCollision(lattice,
                               tau=flow_c.units.relaxation_parameter_lu)
    sim_c = Simulation(flow_c, lattice, collision_c, streaming=None)

    f_c = grid_fine_to_coarse(lattice, sim_f.f,
                              flow_f.units.relaxation_parameter_lu,
                              flow_c.units.relaxation_parameter_lu)

    p_init, u_init = flow_c.initial_solution(flow_c.grid)
    rho_init = lattice.convert_to_tensor(
        flow_c.units.convert_pressure_pu_to_density_lu(p_init))
    u_init = lattice.convert_to_tensor(
        flow_c.units.convert_velocity_to_lu(u_init))
    shear_c_init = lattice.shear_tensor(sim_c.f)
    shear_c = lattice.shear_tensor(f_c)

    assert torch.isclose(lattice.u(f_c), u_init).all()
    assert torch.isclose(lattice.rho(f_c), rho_init).all()
    assert torch.isclose(f_c, sim_c.f).all()
    assert torch.isclose(shear_c_init, shear_c).all()
Beispiel #2
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)
Beispiel #3
0
def test_initialization(dtype_device, use_jacobi):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, device, dtype)
    flow = TaylorGreenVortex2D(resolution=24,
                               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)
    # set initial pressure to 0 everywhere
    p, u = flow.initial_solution(flow.grid)
    u0 = lattice.convert_to_tensor(flow.units.convert_velocity_to_lu(u))
    rho0 = lattice.convert_to_tensor(np.ones_like(u0[0, ...].cpu()))
    simulation.f = lattice.equilibrium(rho0, u0)
    if use_jacobi:
        simulation.initialize_pressure(1000, 1e-6)
        num_iterations = 0
    else:
        num_iterations = simulation.initialize(500, 1e-3)
    piter = lattice.convert_to_numpy(
        flow.units.convert_density_lu_to_pressure_pu(lattice.rho(
            simulation.f)))
    # assert that pressure is converged up to 0.05 (max p
    assert piter == pytest.approx(p, rel=0.0, abs=5e-2)
    assert num_iterations < 500
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)
Beispiel #5
0
def test_write_vtk(tmpdir):
    lattice = Lattice(D2Q9, "cpu")
    flow = TaylorGreenVortex2D(resolution=16,
                               reynolds_number=10,
                               mach_number=0.05,
                               lattice=lattice)
    p, u = flow.initial_solution(flow.grid)
    point_dict = {"p": p[0, ..., None]}
    write_vtk(point_dict, id=1, filename_base=tmpdir / "output")
    assert os.path.isfile(tmpdir / "output_00000001.vtr")
Beispiel #6
0
def test_write_image(tmpdir):
    pytest.skip("matplotlib not working")
    lattice = Lattice(D2Q9, "cpu")
    flow = TaylorGreenVortex2D(resolution=16,
                               reynolds_number=10,
                               mach_number=0.05,
                               lattice=lattice)
    p, u = flow.initial_solution(flow.grid)
    write_image(tmpdir / "p.png", p[0])
    print(tmpdir / "p.png")
    assert os.path.isfile(tmpdir / "p.png")
Beispiel #7
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))
Beispiel #8
0
def test_torch_gradient_2d(order):
    lattice = Lattice(D2Q9, device='cpu', )
    flow = TaylorGreenVortex2D(resolution=100, reynolds_number=1, mach_number=0.05, lattice=lattice)
    grid = flow.grid
    p, u = flow.initial_solution(grid)
    dx = flow.units.convert_length_to_pu(1.0)
    u0_grad = torch_gradient(lattice.convert_to_tensor(u[0]), dx=dx, order=order).numpy()
    u0_grad_np = np.array(np.gradient(u[0], dx))
    u0_grad_analytic = np.array([
        -np.sin(grid[0]) * np.sin(grid[1]),
        np.cos(grid[0]) * np.cos(grid[1]),
    ])
    assert (u0_grad_analytic == pytest.approx(u0_grad, rel=0.0, abs=1e-3))
    assert (u0_grad_np[:, 2:-2, 2:-2] == pytest.approx(u0_grad[:, 2:-2, 2:-2], rel=0.0, abs=1e-3))
Beispiel #9
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
Beispiel #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")
Beispiel #11
0
def test_torch_gradient_2d(order):
    lattice = Lattice(
        D2Q9,
        device='cpu',
    )
    flow = TaylorGreenVortex2D(resolution=100,
                               reynolds_number=1,
                               mach_number=0.05,
                               lattice=lattice)
    grid = flow.grid
    p, u = flow.initial_solution(grid)
    dx = grid[0][0][1] - grid[0][0][0]
    u0_grad = torch_gradient(lattice.convert_to_tensor(u[0]),
                             dx=dx,
                             order=order).numpy()
    u0_grad_analytic = np.array([
        -np.sin(grid[0]) * np.sin(grid[1]),
        np.cos(grid[0]) * np.cos(grid[1]),
    ])
    assert (u0_grad_analytic[0, 1, :] == pytest.approx(u0_grad[0, 1, :],
                                                       rel=2))