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_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))
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_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])
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__], [np.array(spectrum_r.out)[-1, 2:]]]) f_image.append([[other_model.__name__], [f_reporter_r.fs]])