Skip to content

Commit

Permalink
Fix dam break with corrections (#235)
Browse files Browse the repository at this point in the history
* Fix dam break with corrections

* Add error check for `trixi_include`

* Fix tests

* Add test for `trixi_include`

* Fix typo
  • Loading branch information
efaulhaber authored Oct 5, 2023
1 parent 9bf1da6 commit 2769ebf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
6 changes: 3 additions & 3 deletions examples/fluid/dam_break_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using OrdinaryDiffEq

# Constants
gravity = 9.81
athmospheric_pressure = 100000.0
atmospheric_pressure = 100000.0
fluid_density = 1000.0

# Simulation settings
Expand Down Expand Up @@ -51,8 +51,8 @@ move_wall(tank, tank.fluid_size[1])
# ==== Fluid
sound_speed = 20 * sqrt(gravity * initial_fluid_size[2])

state_equation = StateEquationCole(sound_speed, 7, fluid_density, athmospheric_pressure,
background_pressure=athmospheric_pressure)
state_equation = StateEquationCole(sound_speed, 7, fluid_density, atmospheric_pressure,
background_pressure=atmospheric_pressure)

smoothing_kernel = SchoenbergCubicSplineKernel{2}()

Expand Down
14 changes: 12 additions & 2 deletions examples/fluid/dam_break_2d_corrections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,33 @@ density_calculator_dict = Dict(
)

trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"),
particle_spacing=particle_spacing, smoothing_length=smoothing_length,
fluid_particle_spacing=particle_spacing, smoothing_length=smoothing_length,
boundary_density_calculator=ContinuityDensity(),
fluid_density_calculator=ContinuityDensity(),
correction=Nothing(), use_reinit=true,
relaxation_step_file_prefix="relaxation_continuity_reinit",
simulation_step_file_prefix="continuity_reinit",
relaxation_tspan=relaxation_tspan, simulation_tspan=simulation_tspan)

# Clip negative pressure to be able to use `SummationDensity`
state_equation = StateEquationCole(sound_speed, 7, fluid_density, atmospheric_pressure,
background_pressure=atmospheric_pressure,
clip_negative_pressure=true)

for correction_name in keys(correction_dict)
local fluid_density_calculator = density_calculator_dict[correction_name]
local correction = correction_dict[correction_name]

println("="^100)
println("fluid/dam_break_2d.jl with ", correction_name)

trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"),
particle_spacing=particle_spacing, smoothing_length=smoothing_length,
fluid_particle_spacing=particle_spacing,
smoothing_length=smoothing_length,
boundary_density_calculator=boundary_density_calculator,
fluid_density_calculator=fluid_density_calculator,
correction=correction, use_reinit=false,
state_equation=state_equation,
relaxation_step_file_prefix="relaxation_$(correction_name)",
simulation_step_file_prefix="$(correction_name)",
relaxation_tspan=relaxation_tspan, simulation_tspan=simulation_tspan)
Expand Down
18 changes: 17 additions & 1 deletion src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ julia> redirect_stdout(devnull) do
```
"""
function trixi_include(mod::Module, elixir::AbstractString; kwargs...)
# Check that all kwargs exist as assignments
code = read(elixir, String)
expr = Meta.parse("begin \n$code \nend")

for (key, val) in kwargs
# This will throw an error when `key` is not found
find_assignment(expr, key)
end

# Include `elixir` and replace assignments
Base.include(ex -> replace_assignments(insert_maxiters(ex); kwargs...), mod, elixir)
end

Expand Down Expand Up @@ -237,20 +247,26 @@ end
function find_assignment(expr, destination)
# declare result to be able to assign to it in the closure
local result
found = false

# find explicit and keyword assignments
walkexpr(expr) do x
if x isa Expr
if (x.head === Symbol("=") || x.head === :kw) &&
x.args[1] === Symbol(destination)
result = x.args[2]
found = true
# dump(x)
end
end
return x
end

result
if !found
throw(ArgumentError("assignment $destination not found in expression"))
end

return result
end

"""
Expand Down
1 change: 0 additions & 1 deletion test/examples/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
@test_nowarn trixi_include(@__MODULE__,
joinpath(examples_dir(), "fsi",
"dam_break_gate_2d.jl"),
tspan_relaxing=(0.0, 2.0),
tspan=(0.0, 0.4),
dtmax=1e-3)
@test sol.retcode == ReturnCode.Success
Expand Down
2 changes: 2 additions & 0 deletions test/general/general.jl
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# `util.jl` is added here to avoid confusion with `test_util.jl`
include("util.jl")
include("initial_condition.jl")
23 changes: 23 additions & 0 deletions test/general/util.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@testset verbose=true "trixi_include" begin
example = """
x = 4
"""

filename = tempname()
try
open(filename, "w") do file
write(file, example)
end

@test_nowarn trixi_include(filename)
@test @isdefined x
@test x == 4

@test_nowarn trixi_include(filename, x=7)
@test x == 7

@test_throws "assignment y not found in expression" trixi_include(filename, y=3)
finally
rm(filename, force=true)
end
end

0 comments on commit 2769ebf

Please sign in to comment.