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)
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_grid_fine_to_coarse_3d(): lattice = Lattice(D3Q27, 'cpu', dtype=torch.double) flow_f = TaylorGreenVortex3D(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 = TaylorGreenVortex3D(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_c_init, u_c_init = flow_c.initial_solution(flow_c.grid) rho_c_init = flow_c.units.convert_pressure_pu_to_density_lu(p_c_init) u_c_init = flow_c.units.convert_velocity_to_lu(u_c_init) shear_c_init = lattice.shear_tensor(sim_c.f) shear_c = lattice.shear_tensor(f_c) assert np.isclose(lattice.u(f_c).cpu().numpy(), u_c_init).all() assert np.isclose(lattice.rho(f_c).cpu().numpy(), rho_c_init).all() assert torch.isclose(f_c, sim_c.f).all() assert torch.isclose(shear_c_init, shear_c).all()
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])
simulation_p = Simulation(flow=flow_coarse, lattice=lattice, collision=collision_coarse, streaming=streaming) simulation_p.flow.units.characteristic_velocity_pu = simulation.flow.units.characteristic_velocity_pu f_reporter_r = FReporter_coarse(data_interval) f_reporter_r.fs = [] f_reporter_r.ts = [] simulation_p.reporters.append(f_reporter_r) simulation_p.reporters.append(enstrophy_r) simulation_p.reporters.append(energy_r) simulation_p.reporters.append(spectrum_r) with torch.no_grad(): simulation_p.step(n_steps - nr_init * data_interval) error_rho = (loss_function(lattice.rho(f_output.cuda()), lattice.rho(simulation_p.f)).item()) error_u = (loss_function(lattice.u(f_output.cuda()), lattice.u(simulation_p.f)).item()) error_energy = (loss_function(energy(f_output.cuda()), energy(simulation_p.f)).item()) error_enstrophy = (loss_function(enstrophy(f_output.cuda()), enstrophy(simulation_p.f)).item()) print( f"{other_model.__name__:<20s} {np.mean(error_u):.4e} {np.mean(error_rho):.4e} {np.mean(error_energy):.4e} {np.mean(error_enstrophy):.4e}" ) energy_data.append([[other_model.__name__], [energy_r.out]]) enstrophy_data.append([[other_model.__name__], [enstrophy_r.out]]) spectrum_data.append([[other_model.__name__],