Skip to content

Commit a9b4f32

Browse files
author
LasNikas
committed
Merge branch 'main' into interpolation-gpu
2 parents 3371c98 + 618bba5 commit a9b4f32

File tree

7 files changed

+86
-22
lines changed

7 files changed

+86
-22
lines changed

.github/workflows/SpellCheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ jobs:
1010
- name: Checkout Actions Repository
1111
uses: actions/checkout@v4
1212
- name: Check spelling
13-
uses: crate-ci/typos@v1.31.2
13+
uses: crate-ci/typos@v1.32.0

src/callbacks/particle_shifting.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function particle_shifting!(integrator)
3232
v_ode, u_ode = integrator.u.x
3333
dt = integrator.dt
3434
# Internal cache vector, which is safe to use as temporary array
35-
u_cache = first(get_tmp_cache(integrator))
35+
vu_cache = first(get_tmp_cache(integrator))
3636

3737
# Update quantities that are stored in the systems. These quantities (e.g. pressure)
3838
# still have the values from the last stage of the previous step if not updated here.
@@ -41,7 +41,7 @@ function particle_shifting!(integrator)
4141
@trixi_timeit timer() "particle shifting" foreach_system(semi) do system
4242
u = wrap_u(u_ode, system, semi)
4343
v = wrap_v(v_ode, system, semi)
44-
particle_shifting!(u, v, system, v_ode, u_ode, semi, u_cache, dt)
44+
particle_shifting!(u, v, system, v_ode, u_ode, semi, vu_cache, dt)
4545
end
4646

4747
# Tell OrdinaryDiffEq that `u` has been modified
@@ -55,14 +55,18 @@ function particle_shifting!(u, v, system, v_ode, u_ode, semi, u_cache, dt)
5555
end
5656

5757
function particle_shifting!(u, v, system::FluidSystem, v_ode, u_ode, semi,
58-
u_cache, dt)
58+
vu_cache, dt)
5959
# Wrap the cache vector to an NDIMS x NPARTICLES matrix.
6060
# We need this buffer because we cannot safely update `u` while iterating over it.
61+
_, u_cache = vu_cache.x
6162
delta_r = wrap_u(u_cache, system, semi)
6263
set_zero!(delta_r)
6364

64-
v_max = maximum(particle -> norm(current_velocity(v, system, particle)),
65-
eachparticle(system))
65+
# This has similar performance to `maximum(..., eachparticle(system))`,
66+
# but is GPU-compatible.
67+
v_max = maximum(x -> sqrt(dot(x, x)),
68+
reinterpret(reshape, SVector{ndims(system), eltype(v)},
69+
current_velocity(v, system)))
6670

6771
# TODO this needs to be adapted to multi-resolution.
6872
# Section 3.2 explains what else needs to be changed.
@@ -88,7 +92,7 @@ function particle_shifting!(u, v, system::FluidSystem, v_ode, u_ode, semi,
8892
grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle)
8993

9094
# According to p. 29 below Eq. 9
91-
R = 0.2
95+
R = 2 // 10
9296
n = 4
9397

9498
# Eq. 7 in Sun et al. (2017).

src/schemes/fluid/weakly_compressible_sph/system.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ end
247247

248248
system_correction(system::WeaklyCompressibleSPHSystem) = system.correction
249249

250+
@inline function current_velocity(v, system::WeaklyCompressibleSPHSystem)
251+
return current_velocity(v, system.density_calculator, system)
252+
end
253+
254+
@inline function current_velocity(v, ::SummationDensity,
255+
system::WeaklyCompressibleSPHSystem)
256+
# When using `SummationDensity`, `v` contains only the velocity
257+
return v
258+
end
259+
260+
@inline function current_velocity(v, ::ContinuityDensity,
261+
system::WeaklyCompressibleSPHSystem)
262+
# When using `ContinuityDensity`, the velocity is stored
263+
# in the first `ndims(system)` rows of `v`.
264+
return view(v, 1:ndims(system), :)
265+
end
266+
250267
@inline function current_density(v, system::WeaklyCompressibleSPHSystem)
251268
return current_density(v, system.density_calculator, system)
252269
end

test/examples/gpu.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ end
136136
parallelization_backend=Main.parallelization_backend)
137137

138138
# Note that this simulation only takes 42 time steps on the CPU.
139-
# Due to https://github.com/JuliaGPU/Metal.jl/issues/549, it doesn't work on Metal.
139+
# TODO This takes 43 time steps on Metal.
140+
# Maybe related to https://github.com/JuliaGPU/Metal.jl/issues/549
140141
trixi_include_changeprecision(Float32, @__MODULE__,
141142
joinpath(examples_dir(), "fluid",
142143
"dam_break_3d.jl"),
143144
tspan=(0.0f0, 0.1f0),
144145
fluid_particle_spacing=0.1,
145146
semi=semi_fullgrid,
146-
maxiters=42)
147+
maxiters=43)
147148
@test sol.retcode == ReturnCode.Success
148149
backend = TrixiParticles.KernelAbstractions.get_backend(sol.u[end].x[1])
149150
@test backend == Main.parallelization_backend

test/validation/validation.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,33 @@
8383
# Verify number of plots
8484
@test length(axs_edac[1].scene.plots) >= 2
8585
end
86+
87+
@trixi_testset "TGV_2D" begin
88+
@trixi_test_nowarn trixi_include(@__MODULE__,
89+
joinpath(validation_dir(),
90+
"taylor_green_vortex_2d",
91+
"validation_taylor_green_vortex_2d.jl"),
92+
tspan=(0.0, 0.01)) [
93+
r"WARNING: Method definition pressure_function.*\n",
94+
r"WARNING: Method definition initial_pressure_function.*\n",
95+
r"WARNING: Method definition velocity_function.*\n",
96+
r"WARNING: Method definition initial_velocity_function.*\n"
97+
]
98+
@test sol.retcode == ReturnCode.Success
99+
@test count_rhs_allocations(sol, semi) == 0
100+
end
101+
102+
@trixi_testset "LDC_2D" begin
103+
@trixi_test_nowarn trixi_include(@__MODULE__,
104+
joinpath(validation_dir(),
105+
"lid_driven_cavity_2d",
106+
"validation_lid_driven_cavity_2d.jl"),
107+
tspan=(0.0, 0.02), dt=0.01,
108+
SENSOR_CAPTURE_TIME=0.01) [
109+
r"WARNING: Method definition lid_movement_function.*\n",
110+
r"WARNING: Method definition is_moving.*\n"
111+
]
112+
@test sol.retcode == ReturnCode.Success
113+
@test count_rhs_allocations(sol, semi) == 0
114+
end
86115
end

validation/lid_driven_cavity_2d/validation_lid_driven_cavity_2d.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ using TrixiParticles
22

33
# ==========================================================================================
44
# ==== Resolution
5-
particle_spacings = [0.02, 0.01, 0.005]
5+
# The paper provides reference data for particle spacings particle_spacings = [0.02, 0.01, 0.005]
6+
particle_spacing = 0.02
67

78
# ==========================================================================================
89
# ==== Experiment Setup
910
tspan = (0.0, 25.0)
1011
reynolds_numbers = [100.0, 1000.0, 10_000.0]
1112

12-
interpolated_velocity(v, u, t, system) = nothing
13+
const SENSOR_CAPTURE_TIME = 24.8
14+
const CAPTURE_STARTED = Ref(false)
1315

14-
function interpolated_velocity(v, u, t, system::TrixiParticles.FluidSystem)
15-
if t < 24.8
16+
interpolated_velocity(system, v, u, semi, t) = nothing
17+
18+
function interpolated_velocity(system::TrixiParticles.FluidSystem, v, u, semi, t)
19+
if t < SENSOR_CAPTURE_TIME
1620
return nothing
1721
end
1822

@@ -32,7 +36,7 @@ function interpolated_velocity(v, u, t, system::TrixiParticles.FluidSystem)
3236

3337
file = joinpath(output_directory, "interpolated_velocities.csv")
3438

35-
if isfile(file)
39+
if CAPTURE_STARTED[]
3640
data = TrixiParticles.CSV.read(file, TrixiParticles.DataFrame)
3741
vy_y_ = (data.vy_y .+ vy_y)
3842
vy_x_ = (data.vy_x .+ vy_x)
@@ -50,12 +54,13 @@ function interpolated_velocity(v, u, t, system::TrixiParticles.FluidSystem)
5054
counter=1, vy_y=vy_y, vy_x=vy_x, vx_y=vx_y, vx_x=vx_x)
5155

5256
TrixiParticles.CSV.write(output_directory * "/interpolated_velocities.csv", df)
57+
CAPTURE_STARTED[] = true
5358
end
5459

5560
return nothing
5661
end
5762

58-
for particle_spacing in particle_spacings, reynolds_number in reynolds_numbers,
63+
for reynolds_number in reynolds_numbers,
5964
density_calculator in [SummationDensity(), ContinuityDensity()], wcsph in [false, true]
6065
n_particles_xy = round(Int, 1.0 / particle_spacing)
6166

@@ -68,8 +73,9 @@ for particle_spacing in particle_spacings, reynolds_number in reynolds_numbers,
6873
name_density_calculator,
6974
"validation_run_lid_driven_cavity_2d_nparticles_$(n_particles_xy)x$(n_particles_xy)_Re_$Re")
7075

71-
saving_callback = SolutionSavingCallback(dt=0.1, output_directory=output_directory)
76+
saving_callback = SolutionSavingCallback(dt=0.02, output_directory=output_directory)
7277

78+
CAPTURE_STARTED[] = false
7379
pp_callback = PostprocessCallback(; dt=0.02,
7480
interpolated_velocity=interpolated_velocity,
7581
filename="interpolated_velocities",

validation/taylor_green_vortex_2d/validation_taylor_green_vortex_2d.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ using TrixiParticles
22

33
# ==========================================================================================
44
# ==== Resolution
5-
particle_spacings = [0.02, 0.01, 0.005]
5+
# The paper provides reference data for particle spacings particle_spacings = [0.02, 0.01, 0.005]
6+
particle_spacing = 0.02
67

78
# ==========================================================================================
89
# ==== Experiment Setup
@@ -12,10 +13,13 @@ reynolds_number = 100.0
1213
density_calculators = [ContinuityDensity(), SummationDensity()]
1314
perturb_coordinates = [false, true]
1415

15-
function compute_l1v_error(v, u, t, system)
16+
function compute_l1v_error(system, v_ode, u_ode, semi, t)
1617
v_analytical_avg = 0.0
1718
v_avg = 0.0
1819

20+
v = TrixiParticles.wrap_v(v_ode, system, semi)
21+
u = TrixiParticles.wrap_u(u_ode, system, semi)
22+
1923
for particle in TrixiParticles.eachparticle(system)
2024
position = TrixiParticles.current_coords(u, system, particle)
2125

@@ -32,11 +36,14 @@ function compute_l1v_error(v, u, t, system)
3236
return v_avg /= v_analytical_avg
3337
end
3438

35-
function compute_l1p_error(v, u, t, system)
39+
function compute_l1p_error(system, v_ode, u_ode, semi, t)
3640
p_max_exact = 0.0
3741

3842
L1p = 0.0
3943

44+
v = TrixiParticles.wrap_v(v_ode, system, semi)
45+
u = TrixiParticles.wrap_u(u_ode, system, semi)
46+
4047
for particle in TrixiParticles.eachparticle(system)
4148
position = TrixiParticles.current_coords(u, system, particle)
4249

@@ -57,14 +64,14 @@ end
5764

5865
# The pressure plotted in the paper is the difference of the local pressure minus
5966
# the average of the pressure of all particles.
60-
function diff_p_loc_p_avg(v, u, t, system)
61-
p_avg_tot = avg_pressure(v, u, t, system)
67+
function diff_p_loc_p_avg(system, v, u, semi, t)
68+
p_avg_tot = avg_pressure(system, v, u, semi, t)
6269

6370
return v[end, :] .- p_avg_tot
6471
end
6572

6673
for density_calculator in density_calculators, perturbation in perturb_coordinates,
67-
particle_spacing in particle_spacings, wcsph in [false, true]
74+
wcsph in [false, true]
6875
n_particles_xy = round(Int, 1.0 / particle_spacing)
6976

7077
name_density_calculator = density_calculator isa SummationDensity ?

0 commit comments

Comments
 (0)