-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multi-GPU support and mlups computation
- Loading branch information
1 parent
a05441f
commit 71c952b
Showing
13 changed files
with
161 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import xlb | ||
from xlb.compute_backends import ComputeBackends | ||
from xlb.precision_policy import Fp32Fp32 | ||
|
||
from xlb.solver import IncompressibleNavierStokes | ||
from xlb.grid import Grid | ||
from xlb.operator.macroscopic import Macroscopic | ||
from xlb.operator.equilibrium import QuadraticEquilibrium | ||
from xlb.utils import save_fields_vtk, save_image | ||
|
||
xlb.init( | ||
precision_policy=Fp32Fp32, | ||
compute_backend=ComputeBackends.JAX, | ||
velocity_set=xlb.velocity_set.D2Q9, | ||
) | ||
|
||
grid_shape = (1000, 1000) | ||
grid = Grid.create(grid_shape) | ||
|
||
|
||
def initializer(): | ||
rho = grid.create_field(cardinality=1) + 1.0 | ||
u = grid.create_field(cardinality=2) | ||
|
||
circle_center = (grid_shape[0] // 2, grid_shape[1] // 2) | ||
circle_radius = 10 | ||
|
||
for x in range(grid_shape[0]): | ||
for y in range(grid_shape[1]): | ||
if (x - circle_center[0]) ** 2 + ( | ||
y - circle_center[1] | ||
) ** 2 <= circle_radius**2: | ||
rho = rho.at[0, x, y].add(0.001) | ||
|
||
func_eq = QuadraticEquilibrium() | ||
f_eq = func_eq(rho, u) | ||
|
||
return f_eq | ||
|
||
|
||
f = initializer() | ||
|
||
compute_macro = Macroscopic() | ||
|
||
solver = IncompressibleNavierStokes(grid, omega=1.0) | ||
|
||
|
||
def perform_io(f, step): | ||
rho, u = compute_macro(f) | ||
fields = {"rho": rho[0], "u_x": u[0], "u_y": u[1]} | ||
save_fields_vtk(fields, step) | ||
save_image(rho[0], step) | ||
print(f"Step {step + 1} complete") | ||
|
||
|
||
num_steps = 1000 | ||
io_rate = 100 | ||
for step in range(num_steps): | ||
f = solver.step(f, timestep=step) | ||
|
||
if step % io_rate == 0: | ||
perform_io(f, step) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import xlb | ||
import time | ||
import jax | ||
import argparse | ||
from xlb.compute_backends import ComputeBackends | ||
from xlb.precision_policy import Fp32Fp32 | ||
from xlb.operator.initializer import EquilibriumInitializer | ||
|
||
from xlb.solver import IncompressibleNavierStokes | ||
from xlb.grid import Grid | ||
|
||
parser = argparse.ArgumentParser( | ||
description="MLUPS for 3D Lattice Boltzmann Method Simulation (BGK)" | ||
) | ||
parser.add_argument("cube_edge", type=int, help="Length of the edge of the cubic grid") | ||
parser.add_argument("num_steps", type=int, help="Timestep for the simulation") | ||
|
||
args = parser.parse_args() | ||
|
||
cube_edge = args.cube_edge | ||
num_steps = args.num_steps | ||
|
||
|
||
xlb.init( | ||
precision_policy=Fp32Fp32, | ||
compute_backend=ComputeBackends.JAX, | ||
velocity_set=xlb.velocity_set.D3Q19, | ||
) | ||
|
||
grid_shape = (cube_edge, cube_edge, cube_edge) | ||
grid = Grid.create(grid_shape) | ||
|
||
f = grid.create_field(cardinality=19, callback=EquilibriumInitializer(grid)) | ||
|
||
solver = IncompressibleNavierStokes(grid, omega=1.0) | ||
|
||
# Ahead-of-Time Compilation to remove JIT overhead | ||
|
||
|
||
if xlb.current_backend() == ComputeBackends.JAX: | ||
lowered = jax.jit(solver.step).lower(f, timestep=0) | ||
solver_step_compiled = lowered.compile() | ||
|
||
start_time = time.time() | ||
|
||
for step in range(num_steps): | ||
f = solver_step_compiled(f, timestep=step) | ||
|
||
end_time = time.time() | ||
total_lattice_updates = cube_edge**3 * num_steps | ||
total_time_seconds = end_time - start_time | ||
mlups = (total_lattice_updates / total_time_seconds) / 1e6 | ||
print(f"MLUPS: {mlups}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .utils import downsample_field, save_image, save_fields_vtk, save_BCs_vtk, rotate_geometry, voxelize_stl, axangle2mat |
Oops, something went wrong.