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))
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())
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_energy_spectrum(tmpdir, Flow): lattice = Lattice(D2Q9, device='cpu') if Flow == TaylorGreenVortex3D or Flow == 'DecayingTurbulence3D': lattice = Lattice(D3Q27, device='cpu') if Flow == 'DecayingTurbulence2D' or Flow == 'DecayingTurbulence3D': Flow = DecayingTurbulence flow = Flow(resolution=20, reynolds_number=1600, mach_number=0.01, lattice=lattice) collision = BGKCollision(lattice, tau=flow.units.relaxation_parameter_lu) streaming = StandardStreaming(lattice) simulation = Simulation(flow=flow, lattice=lattice, collision=collision, streaming=streaming) spectrum = lattice.convert_to_numpy( EnergySpectrum(lattice, flow)(simulation.f)) energy = IncompressibleKineticEnergy(lattice, flow)(simulation.f).item() if Flow == DecayingTurbulence: # check that the reported spectrum agrees with the spectrum used for initialization ek_ref, _ = flow.energy_spectrum assert (spectrum == pytest.approx(ek_ref, rel=0.0, abs=0.1)) if Flow == TaylorGreenVortex2D or Flow == TaylorGreenVortex3D: # check that flow has only one mode ek_max = sorted(spectrum, reverse=True) assert ek_max[0] * 1e-5 > ek_max[1] assert (energy == pytest.approx(np.sum(spectrum), rel=0.1, abs=0.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))
energy_data.append([[other_model.__name__], [energy_r.out]]) enstrophy_data.append([[other_model.__name__], [enstrophy_r.out]]) spectrum_data.append([[other_model.__name__], [np.array(spectrum_r.out)[-1, 2:]]]) f_image.append([[other_model.__name__], [f_reporter_r.fs]]) enstrophy_r.out = [] energy_r.out = [] spectrum_r.out = [] energy_c_data = torch.zeros(len(data)) enstrophy_c_data = torch.zeros(len(data)) for i in range(len(data)): energy_c_data[i] = energy(data[i].cuda()) enstrophy_c_data[i] = enstrophy(data[i].cuda()) energy_c_data = lattice.convert_to_numpy(energy_c_data) enstrophy_c_data = lattice.convert_to_numpy(enstrophy_c_data) ######################################################################################################################## ##### Plotting ######################################################################################################################## plt.rc('font', family='serif') plt.rc('font', size=10) plt.rcParams['text.latex.preview'] = True rat = .5 columnWidth = 3.5 plt.rcParams['lines.linewidth'] = 1 plt.rcParams['lines.markeredgewidth'] = 1 plt.rcParams['lines.markersize'] = 3 plt.rcParams['lines.linestyle'] = '-' plt.rcParams['markers.fillstyle'] = 'full'