Exemplo n.º 1
0
def test_initialize_fneq(Case, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(D2Q9, device, dtype)
    if "3D" in Case.__name__:
        lattice = Lattice(D3Q27, dtype=dtype, device=device)
    flow = Case(resolution=40,
                reynolds_number=1000,
                mach_number=0.1,
                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)
    pre_ke = lattice.incompressible_energy(simulation_neq.f)

    simulation_neq.initialize_f_neq()

    post_rho = lattice.rho(simulation_neq.f)
    post_u = lattice.u(simulation_neq.f)
    post_ke = lattice.incompressible_energy(simulation_neq.f)
    tol = 1e-6
    assert torch.allclose(pre_rho, post_rho, rtol=0.0, atol=tol)
    assert torch.allclose(pre_u, post_u, rtol=0.0, atol=tol)
    assert torch.allclose(pre_ke, post_ke, rtol=0.0, atol=tol)

    if Case is 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)
Exemplo n.º 2
0
def test_divergence(stencil, dtype_device):
    dtype, device = dtype_device
    lattice = Lattice(stencil, dtype=dtype, device=device)
    flow = DecayingTurbulence(50, 1, 0.05, lattice=lattice, ic_energy=0.5)
    collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu)
    streaming = StandardStreaming(lattice)
    simulation = Simulation(flow=flow,
                            lattice=lattice,
                            collision=collision,
                            streaming=streaming)
    ekin = flow.units.convert_incompressible_energy_to_pu(
        torch.sum(lattice.incompressible_energy(
            simulation.f))) * flow.units.convert_length_to_pu(1.0)**lattice.D

    u0 = flow.units.convert_velocity_to_pu(lattice.u(simulation.f)[0])
    u1 = flow.units.convert_velocity_to_pu(lattice.u(simulation.f)[1])
    dx = flow.units.convert_length_to_pu(1.0)
    grad_u0 = torch_gradient(u0, dx=dx, order=6).cpu().numpy()
    grad_u1 = torch_gradient(u1, dx=dx, order=6).cpu().numpy()
    divergence = np.sum(grad_u0[0] + grad_u1[1])

    if lattice.D == 3:
        u2 = flow.units.convert_velocity_to_pu(lattice.u(simulation.f)[2])
        grad_u2 = torch_gradient(u2, dx=dx, order=6).cpu().numpy()
        divergence += np.sum(grad_u2[2])
    assert (flow.ic_energy == pytest.approx(lattice.convert_to_numpy(ekin),
                                            rel=1))
    assert (0 == pytest.approx(divergence, abs=2e-3))