Skip to content

Commit aa6475b

Browse files
LasNikasLasNikassvchbefaulhaber
authored
Revise boundary zones and allow bidirectional flow (trixi-framework#623)
* fuse `InFlow` and `OutFlow` to `BoundaryZone` * add docs * adapt tests * fix tests * apply formatter * add argument error * quick fix * implement suggestions * switch from symbol to types * extend words * fix 3D pipe flow * minor changes in docs --------- Co-authored-by: LasNikas <niklas.nehe@web.de> Co-authored-by: Sven Berger <berger.sven@gmail.com> Co-authored-by: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com>
1 parent a59b8cf commit aa6475b

File tree

12 files changed

+270
-283
lines changed

12 files changed

+270
-283
lines changed

.typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Shepard = "Shepard"
77
shepard = "shepard"
88
Strack = "Strack"
99
Lok = "Lok"
10+
PN = "PN"
1011
Typ = "Typ"

examples/fluid/pipe_flow_2d.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ function velocity_function2d(pos, t)
8686
end
8787

8888
plane_in = ([0.0, 0.0], [0.0, domain_size[2]])
89-
inflow = InFlow(; plane=plane_in, flow_direction,
90-
open_boundary_layers, density=fluid_density, particle_spacing)
89+
inflow = BoundaryZone(; plane=plane_in,
90+
plane_normal=flow_direction, open_boundary_layers,
91+
density=fluid_density, particle_spacing, boundary_type=InFlow())
9192

9293
open_boundary_in = OpenBoundarySPHSystem(inflow; fluid_system,
9394
boundary_model=BoundaryModelLastiwka(),
@@ -97,9 +98,9 @@ open_boundary_in = OpenBoundarySPHSystem(inflow; fluid_system,
9798
reference_velocity=velocity_function2d)
9899

99100
plane_out = ([domain_size[1], 0.0], [domain_size[1], domain_size[2]])
100-
outflow = OutFlow(; plane=plane_out,
101-
flow_direction, open_boundary_layers, density=fluid_density,
102-
particle_spacing)
101+
outflow = BoundaryZone(; plane=plane_out,
102+
plane_normal=-flow_direction, open_boundary_layers,
103+
density=fluid_density, particle_spacing, boundary_type=OutFlow())
103104

104105
open_boundary_out = OpenBoundarySPHSystem(outflow; fluid_system,
105106
boundary_model=BoundaryModelLastiwka(),

src/TrixiParticles.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ include("preprocessing/preprocessing.jl")
5858
export Semidiscretization, semidiscretize, restart_with!
5959
export InitialCondition
6060
export WeaklyCompressibleSPHSystem, EntropicallyDampedSPHSystem, TotalLagrangianSPHSystem,
61-
BoundarySPHSystem, DEMSystem, BoundaryDEMSystem, OpenBoundarySPHSystem, InFlow,
62-
OutFlow
61+
BoundarySPHSystem, DEMSystem, BoundaryDEMSystem, OpenBoundarySPHSystem
62+
export BoundaryZone, InFlow, OutFlow, BidirectionalFlow
6363
export InfoCallback, SolutionSavingCallback, DensityReinitializationCallback,
6464
PostprocessCallback, StepsizeCallback, UpdateCallback, SteadyStateReachedCallback
6565
export ContinuityDensity, SummationDensity

src/general/semidiscretization.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,13 @@ function check_configuration(system::TotalLagrangianSPHSystem, systems)
855855
"`ContinuityDensity` is not yet supported for a `TotalLagrangianSPHSystem`"))
856856
end
857857
end
858+
859+
function check_configuration(system::OpenBoundarySPHSystem, systems)
860+
(; boundary_model, boundary_zone) = system
861+
862+
if boundary_model isa BoundaryModelLastiwka &&
863+
boundary_zone isa BoundaryZone{BidirectionalFlow}
864+
throw(ArgumentError("`BoundaryModelLastiwka` needs a specific flow direction. " *
865+
"Please specify inflow and outflow."))
866+
end
867+
end

src/schemes/boundary/open_boundary/boundary_zones.jl

Lines changed: 114 additions & 186 deletions
Large diffs are not rendered by default.

src/schemes/boundary/open_boundary/method_of_characteristics.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
44
Boundary model for `OpenBoundarySPHSystem`.
55
This model uses the characteristic variables to propagate the appropriate values
6-
to the outlet or inlet and have been proposed by Lastiwka et al. (2009). For more information
7-
about the method see [description below](@ref method_of_characteristics).
6+
to the outlet or inlet and have been proposed by Lastiwka et al. (2009).
7+
It requires a specific flow direction to be passed to the [`BoundaryZone`](@ref).
8+
For more information about the method see [description below](@ref method_of_characteristics).
89
"""
910
struct BoundaryModelLastiwka end
1011

1112
# Called from update callback via `update_open_boundary_eachstep!`
1213
@inline function update_boundary_quantities!(system, boundary_model::BoundaryModelLastiwka,
1314
v, u, v_ode, u_ode, semi, t)
14-
(; density, pressure, cache, flow_direction,
15+
(; density, pressure, cache, boundary_zone,
1516
reference_velocity, reference_pressure, reference_density) = system
17+
(; flow_direction) = boundary_zone
1618

1719
sound_speed = system_sound_speed(system.fluid_system)
1820

@@ -125,8 +127,9 @@ end
125127

126128
function evaluate_characteristics!(system, neighbor_system::FluidSystem,
127129
v, u, v_ode, u_ode, semi, t)
128-
(; volume, cache, flow_direction, density, pressure,
130+
(; volume, cache, boundary_zone, density, pressure,
129131
reference_velocity, reference_pressure, reference_density) = system
132+
(; flow_direction) = boundary_zone
130133
(; characteristics) = cache
131134

132135
v_neighbor_system = wrap_v(v_ode, neighbor_system, semi)
@@ -175,15 +178,15 @@ function evaluate_characteristics!(system, neighbor_system::FluidSystem,
175178
return system
176179
end
177180

178-
@inline function prescribe_conditions!(characteristics, particle, ::OutFlow)
181+
@inline function prescribe_conditions!(characteristics, particle, ::BoundaryZone{OutFlow})
179182
# J3 is prescribed (i.e. determined from the exterior of the domain).
180183
# J1 and J2 is transmitted from the domain interior.
181184
characteristics[3, particle] = zero(eltype(characteristics))
182185

183186
return characteristics
184187
end
185188

186-
@inline function prescribe_conditions!(characteristics, particle, ::InFlow)
189+
@inline function prescribe_conditions!(characteristics, particle, ::BoundaryZone{InFlow})
187190
# Allow only J3 to propagate upstream to the boundary
188191
characteristics[1, particle] = zero(eltype(characteristics))
189192
characteristics[2, particle] = zero(eltype(characteristics))

src/schemes/boundary/open_boundary/system.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@doc raw"""
2-
OpenBoundarySPHSystem(boundary_zone::Union{InFlow, OutFlow};
2+
OpenBoundarySPHSystem(boundary_zone::BoundaryZone;
33
fluid_system::FluidSystem, buffer_size::Integer,
44
boundary_model,
55
reference_velocity=nothing,
@@ -9,7 +9,7 @@
99
Open boundary system for in- and outflow particles.
1010
1111
# Arguments
12-
- `boundary_zone`: Use [`InFlow`](@ref) for an inflow and [`OutFlow`](@ref) for an outflow boundary.
12+
- `boundary_zone`: See [`BoundaryZone`](@ref).
1313
1414
# Keywords
1515
- `fluid_system`: The corresponding fluid system
@@ -39,15 +39,14 @@ struct OpenBoundarySPHSystem{BM, BZ, NDIMS, ELTYPE <: Real, IC, FS, ARRAY1D, RV,
3939
volume :: ARRAY1D # Array{ELTYPE, 1}: [particle]
4040
pressure :: ARRAY1D # Array{ELTYPE, 1}: [particle]
4141
boundary_zone :: BZ
42-
flow_direction :: SVector{NDIMS, ELTYPE}
4342
reference_velocity :: RV
4443
reference_pressure :: RP
4544
reference_density :: RD
4645
buffer :: B
4746
update_callback_used :: Ref{Bool}
4847
cache :: C
4948

50-
function OpenBoundarySPHSystem(boundary_zone::Union{InFlow, OutFlow};
49+
function OpenBoundarySPHSystem(boundary_zone::BoundaryZone;
5150
fluid_system::FluidSystem,
5251
buffer_size::Integer, boundary_model,
5352
reference_velocity=nothing,
@@ -116,8 +115,6 @@ struct OpenBoundarySPHSystem{BM, BZ, NDIMS, ELTYPE <: Real, IC, FS, ARRAY1D, RV,
116115
reference_density_ = wrap_reference_function(reference_density, Val(NDIMS))
117116
end
118117

119-
flow_direction_ = boundary_zone.flow_direction
120-
121118
cache = create_cache_open_boundary(boundary_model, initial_condition)
122119

123120
return new{typeof(boundary_model), typeof(boundary_zone), NDIMS, ELTYPE,
@@ -126,7 +123,7 @@ struct OpenBoundarySPHSystem{BM, BZ, NDIMS, ELTYPE <: Real, IC, FS, ARRAY1D, RV,
126123
typeof(reference_density_), typeof(buffer),
127124
typeof(cache)}(initial_condition, fluid_system, boundary_model, mass,
128125
density, volume, pressure, boundary_zone,
129-
flow_direction_, reference_velocity_, reference_pressure_,
126+
reference_velocity_, reference_pressure_,
130127
reference_density_, buffer, false, cache)
131128
end
132129
end
@@ -143,12 +140,13 @@ end
143140

144141
timer_name(::OpenBoundarySPHSystem) = "open_boundary"
145142
vtkname(system::OpenBoundarySPHSystem) = "open_boundary"
143+
boundary_type_name(::BoundaryZone{ZT}) where {ZT} = string(nameof(ZT))
146144

147145
function Base.show(io::IO, system::OpenBoundarySPHSystem)
148146
@nospecialize system # reduce precompilation time
149147

150148
print(io, "OpenBoundarySPHSystem{", ndims(system), "}(")
151-
print(io, type2string(system.boundary_zone))
149+
print(io, boundary_type_name(system.boundary_zone))
152150
print(io, ") with ", nparticles(system), " particles")
153151
end
154152

@@ -163,8 +161,7 @@ function Base.show(io::IO, ::MIME"text/plain", system::OpenBoundarySPHSystem)
163161
summary_line(io, "#buffer_particles", system.buffer.buffer_size)
164162
summary_line(io, "fluid system", type2string(system.fluid_system))
165163
summary_line(io, "boundary model", type2string(system.boundary_model))
166-
summary_line(io, "boundary", type2string(system.boundary_zone))
167-
summary_line(io, "flow direction", system.flow_direction)
164+
summary_line(io, "boundary type", boundary_type_name(system.boundary_zone))
168165
summary_line(io, "prescribed velocity", type2string(system.reference_velocity))
169166
summary_line(io, "prescribed pressure", type2string(system.reference_pressure))
170167
summary_line(io, "prescribed density", type2string(system.reference_density))
@@ -258,7 +255,7 @@ end
258255

259256
# Outflow particle is outside the boundary zone
260257
@inline function convert_particle!(system::OpenBoundarySPHSystem, fluid_system,
261-
boundary_zone::OutFlow, particle, v, u,
258+
boundary_zone::BoundaryZone{OutFlow}, particle, v, u,
262259
v_fluid, u_fluid)
263260
deactivate_particle!(system, particle, u)
264261

@@ -267,7 +264,7 @@ end
267264

268265
# Inflow particle is outside the boundary zone
269266
@inline function convert_particle!(system::OpenBoundarySPHSystem, fluid_system,
270-
boundary_zone::InFlow, particle, v, u,
267+
boundary_zone::BoundaryZone{InFlow}, particle, v, u,
271268
v_fluid, u_fluid)
272269
(; spanning_set) = boundary_zone
273270

@@ -282,6 +279,31 @@ end
282279
return system
283280
end
284281

282+
# Buffer particle is outside the boundary zone
283+
@inline function convert_particle!(system::OpenBoundarySPHSystem, fluid_system,
284+
boundary_zone::BoundaryZone{BidirectionalFlow}, particle,
285+
v, u, v_fluid, u_fluid)
286+
relative_position = current_coords(u, system, particle) - boundary_zone.zone_origin
287+
288+
# Check if particle is in- or outside the fluid domain.
289+
# `plane_normal` is always pointing into the fluid domain.
290+
if signbit(dot(relative_position, boundary_zone.plane_normal))
291+
deactivate_particle!(system, particle, u)
292+
293+
return system
294+
end
295+
296+
# Activate a new particle in simulation domain
297+
transfer_particle!(fluid_system, system, particle, v_fluid, u_fluid, v, u)
298+
299+
# Reset position of boundary particle
300+
for dim in 1:ndims(system)
301+
u[dim, particle] = boundary_zone.plane_normal[dim]
302+
end
303+
304+
return system
305+
end
306+
285307
# Fluid particle is in boundary zone
286308
@inline function convert_particle!(fluid_system::FluidSystem, system,
287309
boundary_zone, particle, v, u, v_fluid, u_fluid)

src/visualization/write2vtk.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,8 @@ function write2vtk!(vtk, v, u, t, system::OpenBoundarySPHSystem; write_meta_data
345345
for particle in active_particles(system)]
346346

347347
if write_meta_data
348-
vtk["boundary_zone"] = type2string(system.boundary_zone)
348+
vtk["boundary_zone"] = type2string(first(typeof(system.boundary_zone).parameters))
349349
vtk["width"] = round(system.boundary_zone.zone_width, digits=3)
350-
vtk["flow_direction"] = system.flow_direction
351350
vtk["velocity_function"] = type2string(system.reference_velocity)
352351
vtk["pressure_function"] = type2string(system.reference_pressure)
353352
vtk["density_function"] = type2string(system.reference_density)

test/general/buffer.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Mock fluid system
33
struct FluidSystemMock3 <: TrixiParticles.FluidSystem{2, Nothing} end
44

5-
inflow = InFlow(; plane=([0.0, 0.0], [0.0, 1.0]), particle_spacing=0.2,
6-
open_boundary_layers=2, density=1.0, flow_direction=[1.0, 0.0])
7-
system = OpenBoundarySPHSystem(inflow; fluid_system=FluidSystemMock3(),
5+
zone = BoundaryZone(; plane=([0.0, 0.0], [0.0, 1.0]), particle_spacing=0.2,
6+
open_boundary_layers=2, density=1.0, plane_normal=[1.0, 0.0],
7+
boundary_type=InFlow())
8+
system = OpenBoundarySPHSystem(zone; fluid_system=FluidSystemMock3(),
89
reference_density=0.0, reference_pressure=0.0,
910
reference_velocity=[0, 0],
1011
boundary_model=BoundaryModelLastiwka(), buffer_size=0)
11-
system_buffer = OpenBoundarySPHSystem(inflow; buffer_size=5,
12+
system_buffer = OpenBoundarySPHSystem(zone; buffer_size=5,
1213
reference_density=0.0, reference_pressure=0.0,
1314
reference_velocity=[0, 0],
1415
boundary_model=BoundaryModelLastiwka(),

0 commit comments

Comments
 (0)