Skip to content

Commit 44c1c02

Browse files
author
LasNikas
committed
add dvu_ode vector to custom quantities
1 parent 6b43656 commit 44c1c02

File tree

13 files changed

+63
-47
lines changed

13 files changed

+63
-47
lines changed

examples/fluid/hydrostatic_water_column_2d.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ semi = Semidiscretization(fluid_system, boundary_system,
7373
ode = semidiscretize(semi, tspan)
7474

7575
info_callback = InfoCallback(interval=50)
76-
saving_callback = SolutionSavingCallback(dt=0.02, prefix="")
76+
saving_callback = SolutionSavingCallback(dt=0.02, prefix="", dv=TrixiParticles.acceleration)
7777

7878
# This is to easily add a new callback with `trixi_include`
7979
extra_callback = nothing

src/TrixiParticles.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ using ReadVTK: ReadVTK
2323
using RecipesBase: RecipesBase, @series
2424
using Random: seed!
2525
using SciMLBase: CallbackSet, DiscreteCallback, DynamicalODEProblem, u_modified!,
26-
get_tmp_cache, set_proposed_dt!, ODESolution, ODEProblem, terminate!
26+
get_tmp_cache, set_proposed_dt!, ODESolution, ODEProblem, terminate!,
27+
get_du
2728
@reexport using StaticArrays: SVector
2829
using StaticArrays: @SMatrix, SMatrix, setindex
2930
using StrideArrays: PtrArray, StaticInt

src/callbacks/post_process.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ end
229229
# `affect!`
230230
function (pp::PostprocessCallback)(integrator)
231231
@trixi_timeit timer() "apply postprocess cb" begin
232+
dvu_ode = get_du(integrator)
233+
dv_ode, du_ode = dvu_ode.x
232234
vu_ode = integrator.u
233235
v_ode, u_ode = vu_ode.x
234236
semi = integrator.p
@@ -252,7 +254,7 @@ function (pp::PostprocessCallback)(integrator)
252254
system_index = system_indices(system, semi)
253255

254256
for (key, f) in pp.func
255-
result = custom_quantity(f, system, v_ode, u_ode, semi, t)
257+
result = custom_quantity(f, system, dv_ode, du_ode, v_ode, u_ode, semi, t)
256258
if result !== nothing
257259
add_entry!(pp, string(key), t, result, filenames[system_index])
258260
new_data = true

src/callbacks/solution_saving.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ function (solution_callback::SolutionSavingCallback)(integrator)
154154
(; interval, output_directory, custom_quantities, write_meta_data, git_hash,
155155
verbose, prefix, latest_saved_iter, max_coordinates) = solution_callback
156156

157+
dvu_ode = get_du(integrator)
157158
vu_ode = integrator.u
158159
semi = integrator.p
159160
iter = get_iter(interval, integrator)
@@ -173,7 +174,7 @@ function (solution_callback::SolutionSavingCallback)(integrator)
173174
println("Writing solution to $output_directory at t = $(integrator.t)")
174175
end
175176

176-
@trixi_timeit timer() "save solution" trixi2vtk(vu_ode, semi, integrator.t;
177+
@trixi_timeit timer() "save solution" trixi2vtk(dvu_ode, vu_ode, semi, integrator.t;
177178
iter, output_directory, prefix,
178179
write_meta_data, git_hash=git_hash[],
179180
max_coordinates, custom_quantities...)

src/general/custom_quantities.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Returns the total kinetic energy of all particles in a system.
55
"""
6-
function kinetic_energy(system, v_ode, u_ode, semi, t)
6+
function kinetic_energy(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
77
v = wrap_v(v_ode, system, semi)
88

99
# If `each_moving_particle` is empty (no moving particles), return zero
@@ -18,7 +18,7 @@ end
1818
1919
Returns the total mass of all particles in a system.
2020
"""
21-
function total_mass(system, v_ode, u_ode, semi, t)
21+
function total_mass(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
2222
return sum(eachparticle(system)) do particle
2323
return system.mass[particle]
2424
end
@@ -43,7 +43,7 @@ end
4343
4444
Returns the maximum pressure over all particles in a system.
4545
"""
46-
function max_pressure(system::FluidSystem, v_ode, u_ode, semi, t)
46+
function max_pressure(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
4747
v = wrap_v(v_ode, system, semi)
4848
return maximum(current_pressure(v, system))
4949
end
@@ -57,7 +57,7 @@ end
5757
5858
Returns the minimum pressure over all particles in a system.
5959
"""
60-
function min_pressure(system::FluidSystem, v_ode, u_ode, semi, t)
60+
function min_pressure(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
6161
v = wrap_v(v_ode, system, semi)
6262
return minimum(current_pressure(v, system))
6363
end
@@ -71,13 +71,13 @@ end
7171
7272
Returns the average pressure over all particles in a system.
7373
"""
74-
function avg_pressure(system::FluidSystem, v_ode, u_ode, semi, t)
74+
function avg_pressure(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
7575
v = wrap_v(v_ode, system, semi)
7676
sum_ = sum(current_pressure(v, system))
7777
return sum_ / nparticles(system)
7878
end
7979

80-
function avg_pressure(system, v_ode, u_ode, semi, t)
80+
function avg_pressure(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
8181
return NaN
8282
end
8383

@@ -86,12 +86,12 @@ end
8686
8787
Returns the maximum density over all particles in a system.
8888
"""
89-
function max_density(system::FluidSystem, v_ode, u_ode, semi, t)
89+
function max_density(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
9090
v = wrap_v(v_ode, system, semi)
9191
return maximum(current_density(v, system))
9292
end
9393

94-
function max_density(system, v_ode, u_ode, semi, t)
94+
function max_density(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
9595
return NaN
9696
end
9797

@@ -100,12 +100,12 @@ end
100100
101101
Returns the minimum density over all particles in a system.
102102
"""
103-
function min_density(system::FluidSystem, v_ode, u_ode, semi, t)
103+
function min_density(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
104104
v = wrap_v(v_ode, system, semi)
105105
return minimum(current_density(v, system))
106106
end
107107

108-
function min_density(system, v_ode, u_ode, semi, t)
108+
function min_density(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
109109
return NaN
110110
end
111111

@@ -114,7 +114,7 @@ end
114114
115115
Returns the average_density over all particles in a system.
116116
"""
117-
function avg_density(system::FluidSystem, v_ode, u_ode, semi, t)
117+
function avg_density(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi, t)
118118
v = wrap_v(v_ode, system, semi)
119119
sum_ = sum(current_density(v, system))
120120
return sum_ / nparticles(system)
@@ -123,3 +123,7 @@ end
123123
function avg_density(system, v_ode, u_ode, semi, t)
124124
return NaN
125125
end
126+
127+
function acceleration(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
128+
return wrap_v(dv_ode, system, semi)
129+
end

src/io/read_vtk.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ function vtk2trixi(file)
5757
@info "No '$field' field found in VTK file. Will be set to zero."
5858
end
5959
end
60-
return InitialCondition(; coordinates, particle_spacing=results["particle_spacing"],
60+
return InitialCondition(; coordinates,
61+
particle_spacing=first(results["particle_spacing"]),
6162
velocity=results["velocity"],
6263
mass=results["mass"],
6364
density=results["density"],

src/io/write_vtk.jl

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ trixi2vtk(sol.u[end], semi, 0.0, iter=1, my_custom_quantity=kinetic_energy)
4848
4949
```
5050
"""
51-
function trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix="",
52-
write_meta_data=true, git_hash=compute_git_hash(),
51+
function trixi2vtk(dvu_ode, vu_ode, semi, t; iter=nothing, output_directory="out",
52+
prefix="", write_meta_data=true, git_hash=compute_git_hash(),
5353
max_coordinates=Inf, custom_quantities...)
5454
(; systems) = semi
55-
v_ode, u_ode = vu_ode.x
5655

5756
# Update quantities that are stored in the systems. These quantities (e.g. pressure)
5857
# still have the values from the last stage of the previous step if not updated here.
5958
@trixi_timeit timer() "update systems" begin
59+
v_ode, u_ode = vu_ode.x
6060
# Don't create sub-timers here to avoid cluttering the timer output
6161
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t;
6262
update_from_callback=true)
@@ -68,24 +68,26 @@ function trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix
6868
system_index = system_indices(system, semi)
6969
periodic_box = get_neighborhood_search(system, semi).periodic_box
7070

71-
trixi2vtk(system, v_ode, u_ode, semi, t, periodic_box;
71+
trixi2vtk(system, dvu_ode, vu_ode, semi, t, periodic_box;
7272
system_name=filenames[system_index], output_directory, iter, prefix,
7373
write_meta_data, git_hash, max_coordinates, custom_quantities...)
7474
end
7575
end
7676

7777
# Convert data for a single TrixiParticle system to VTK format
78-
function trixi2vtk(system_, v_ode_, u_ode_, semi_, t, periodic_box; output_directory="out",
79-
prefix="", iter=nothing, system_name=vtkname(system_),
80-
write_meta_data=true, max_coordinates=Inf, git_hash=compute_git_hash(),
81-
custom_quantities...)
78+
function trixi2vtk(system_, dvu_ode_, vu_ode_, semi_, t, periodic_box;
79+
output_directory="out", prefix="", iter=nothing,
80+
system_name=vtkname(system_), write_meta_data=true, max_coordinates=Inf,
81+
git_hash=compute_git_hash(), custom_quantities...)
8282
mkpath(output_directory)
8383

8484
# Skip empty systems
8585
if nparticles(system_) == 0
8686
return
8787
end
8888

89+
v_ode_, u_ode_ = vu_ode_.x
90+
8991
# Transfer to CPU if data is on the GPU. Do nothing if already on CPU.
9092
v_ode, u_ode, system, semi = transfer2cpu(v_ode_, u_ode_, system_, semi_)
9193

@@ -137,8 +139,10 @@ function trixi2vtk(system_, v_ode_, u_ode_, semi_, t, periodic_box; output_direc
137139
end
138140

139141
# Extract custom quantities for this system
142+
# TODO: Check if any custom quantities and then adapt from gpu
143+
dv_ode, du_ode = dvu_ode_.x
140144
for (key, quantity) in custom_quantities
141-
value = custom_quantity(quantity, system, v_ode, u_ode, semi, t)
145+
value = custom_quantity(quantity, system, dv_ode, du_ode, v_ode, u_ode, semi, t)
142146
if value !== nothing
143147
vtk[string(key)] = value
144148
end
@@ -164,20 +168,21 @@ function transfer2cpu(v_, u_, system_, semi_)
164168
return v_, u_, system_, semi_
165169
end
166170

167-
function custom_quantity(quantity::AbstractArray, system, v_ode, u_ode, semi, t)
171+
function custom_quantity(quantity::AbstractArray, system, dv_ode, du_ode, v_ode, u_ode,
172+
semi, t)
168173
return quantity
169174
end
170175

171-
function custom_quantity(quantity, system, v_ode, u_ode, semi, t)
176+
function custom_quantity(quantity, system, dv_ode, du_ode, v_ode, u_ode, semi, t)
172177
# Check if `quantity` is a function of `system`, `v_ode`, `u_ode`, `semi` and `t`
173178
if !isempty(methods(quantity,
174-
(typeof(system), typeof(v_ode), typeof(u_ode),
175-
typeof(semi), typeof(t))))
176-
return quantity(system, v_ode, u_ode, semi, t)
179+
(typeof(system), typeof(dv_ode), typeof(du_ode), typeof(v_ode),
180+
typeof(u_ode), typeof(semi), typeof(t))))
181+
return quantity(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
177182
end
178183

179184
# Assume `quantity` is a function of `data`
180-
data = system_data(system, v_ode, u_ode, semi)
185+
data = system_data(system, dv_ode, du_ode, v_ode, u_ode, semi)
181186
return quantity(system, data, t)
182187
end
183188

@@ -301,12 +306,12 @@ function write2vtk!(vtk, v, u, t, system::FluidSystem; write_meta_data=true)
301306
grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle)
302307

303308
surface_tension[1:ndims(system),
304-
particle] .+= surface_tension_force(surface_tension_a,
305-
surface_tension_b,
306-
system, system, particle,
307-
neighbor, pos_diff,
308-
distance, rho_a, rho_b,
309-
grad_kernel)
309+
particle] .+= surface_tension_force(surface_tension_a,
310+
surface_tension_b,
311+
system, system, particle,
312+
neighbor, pos_diff,
313+
distance, rho_a, rho_b,
314+
grad_kernel)
310315
end
311316
vtk["surface_tension"] = surface_tension
312317

src/schemes/boundary/open_boundary/system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ end
499499
# When the neighbor is an open boundary system, just use the viscosity of the fluid `system` instead
500500
@inline viscosity_model(system, neighbor_system::OpenBoundarySPHSystem) = system.viscosity
501501

502-
function system_data(system::OpenBoundarySPHSystem, v_ode, u_ode, semi)
502+
function system_data(system::OpenBoundarySPHSystem, dv_ode, du_ode, v_ode, u_ode, semi)
503503
v = wrap_v(v_ode, system, semi)
504504
u = wrap_u(u_ode, system, semi)
505505

src/schemes/boundary/system.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,12 @@ function system_correction(system::BoundarySPHSystem{<:BoundaryModelDummyParticl
455455
return system.boundary_model.correction
456456
end
457457

458-
function system_data(system::BoundarySPHSystem, v_ode, u_ode, semi)
458+
function system_data(system::BoundarySPHSystem, dv_ode, du_ode, v_ode, u_ode, semi)
459459
v = wrap_v(v_ode, system, semi)
460460
u = wrap_u(u_ode, system, semi)
461461

462462
coordinates = current_coordinates(u, system)
463-
velocity = current_velocity(v, system)
463+
velocity = 0 # TODO this is broken
464464
density = current_density(v, system)
465465
pressure = current_pressure(v, system)
466466

@@ -471,7 +471,7 @@ function available_data(::BoundarySPHSystem)
471471
return (:coordinates, :velocity, :density, :pressure)
472472
end
473473

474-
function system_data(system::BoundaryDEMSystem, v_ode, u_ode, semi)
474+
function system_data(system::BoundaryDEMSystem, dv_ode, du_ode, v_ode, u_ode, semi)
475475
(; coordinates, radius, normal_stiffness) = system
476476

477477
return (; coordinates, radius, normal_stiffness)

src/schemes/fluid/fluid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ end
174174
return nothing
175175
end
176176

177-
function system_data(system::FluidSystem, v_ode, u_ode, semi)
177+
function system_data(system::FluidSystem, dv_ode, du_ode, v_ode, u_ode, semi)
178178
(; mass) = system
179179

180180
v = wrap_v(v_ode, system, semi)

0 commit comments

Comments
 (0)