Skip to content

Commit

Permalink
adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LasNikas committed May 27, 2024
1 parent b12bc5e commit a3adf1d
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 215 deletions.
2 changes: 1 addition & 1 deletion src/schemes/boundary/open_boundary/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function Base.show(io::IO, system::OpenBoundarySPHSystem)
@nospecialize system # reduce precompilation time

print(io, "OpenBoundarySPHSystem{", ndims(system), "}(")
print(io, system.boundary_zone)
print(io, type2string(system.boundary_zone))
print(io, ") with ", nparticles(system), " particles")
end

Expand Down
221 changes: 221 additions & 0 deletions test/schemes/boundary/open_boundary/boundary_zone.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
@testset verbose=true "Boundary Zone" begin
@testset verbose=true "Boundary Zone 2D" begin
particle_spacing = 0.2
open_boundary_layers = 4

plane_points_1 = [[0.0, 0.0], [0.5, -0.5], [1.0, 0.5]]
plane_points_2 = [[0.0, 1.0], [0.2, 2.0], [2.3, 0.5]]

@testset "Points $(i)" for i in eachindex(plane_points_1)
point_1 = plane_points_1[i]
point_2 = plane_points_2[i]

plane_size = point_2 - point_1

flow_directions = [
normalize([-plane_size[2], plane_size[1]]),
-normalize([-plane_size[2], plane_size[1]])
]

@testset "Flow Direction $(j)" for j in eachindex(flow_directions)
inflow = InFlow(; plane=(point_1, point_2), particle_spacing,
flow_direction=flow_directions[j], density=1.0,
open_boundary_layers)
outflow = OutFlow(; plane=(point_1, point_2), particle_spacing,
flow_direction=flow_directions[j], density=1.0,
open_boundary_layers)

boundary_zones = [
inflow,
outflow
]

@testset "$boundary_zone" for boundary_zone in boundary_zones
zone_width = open_boundary_layers *
boundary_zone.initial_condition.particle_spacing
sign_ = (boundary_zone isa InFlow) ? -1 : 1

@test plane_points_1[i] == boundary_zone.zone_origin
@test plane_points_2[i] - boundary_zone.zone_origin ==
boundary_zone.spanning_set[2]
@test sign_ * flow_directions[j]
normalize(boundary_zone.spanning_set[1])
@test zone_width norm(boundary_zone.spanning_set[1])
end
end
end
end

@testset verbose=true "Boundary Zone 3D" begin
particle_spacing = 0.05
open_boundary_layers = 4

plane_points_1 = [
[0.0, 0.0, 0.0],
[0.3113730847835541, 0.19079485535621643, -0.440864622592926]
]
plane_points_2 = [
[1.0, 0.0, 0.0],
[-0.10468611121177673, 0.252103328704834, -0.44965094327926636]
]
plane_points_3 = [
[0.0, 1.0, 0.0],
[0.3113730847835541, 0.25057315826416016, -0.02374829351902008]
]

@testset "Points $(i)" for i in eachindex(plane_points_1)
point_1 = plane_points_1[i]
point_2 = plane_points_2[i]
point_3 = plane_points_3[i]

edge1 = point_2 - point_1
edge2 = point_3 - point_1

flow_directions = [
normalize(cross(edge1, edge2)),
-normalize(cross(edge1, edge2))
]

@testset "Flow Direction $(j)" for j in eachindex(flow_directions)
inflow = InFlow(; plane=(point_1, point_2, point_3), particle_spacing,
flow_direction=flow_directions[j], density=1.0,
open_boundary_layers)
outflow = OutFlow(; plane=(point_1, point_2, point_3), particle_spacing,
flow_direction=flow_directions[j], density=1.0,
open_boundary_layers)

boundary_zones = [
inflow,
outflow
]

@testset "$boundary_zone" for boundary_zone in boundary_zones
zone_width = open_boundary_layers *
boundary_zone.initial_condition.particle_spacing
sign_ = (boundary_zone isa InFlow) ? -1 : 1

@test plane_points_1[i] == boundary_zone.zone_origin
@test plane_points_2[i] - boundary_zone.zone_origin ==
boundary_zone.spanning_set[2]
@test plane_points_3[i] - boundary_zone.zone_origin ==
boundary_zone.spanning_set[3]
@test sign_ * flow_directions[j]
normalize(boundary_zone.spanning_set[1])
@test zone_width norm(boundary_zone.spanning_set[1])
end
end
end
end

@testset verbose=true "Particle In Boundary Zone 2D" begin
plane_points = [[-0.2, -0.5], [0.3, 0.6]]
plane_size = plane_points[2] - plane_points[1]

flow_direction = normalize([-plane_size[2], plane_size[1]])

inflow = InFlow(; plane=plane_points, particle_spacing=0.1,
flow_direction, density=1.0, open_boundary_layers=4)
outflow = OutFlow(; plane=plane_points, particle_spacing=0.1,
flow_direction, density=1.0, open_boundary_layers=4)

boundary_zones = [
inflow,
outflow
]

@testset "$(nameof(typeof(boundary_zone)))" for boundary_zone in boundary_zones
perturb_ = boundary_zone isa InFlow ? eps() : -eps()

point_3 = boundary_zone.spanning_set[1] + boundary_zone.zone_origin

query_points = Dict(
"Behind" => ([-1.0, -1.0], false),
"Before" => ([2.0, 2.0], false),
"On Point 1" => (plane_points[1], true),
"On Point 2" => (plane_points[2], true),
"On Point 3" => (point_3, true),
"Closely On Point 1" => (plane_points[1] .+ perturb_ * flow_direction,
false))

TrixiParticles.@autoinfiltrate

@testset "$k" for k in keys(query_points)
(particle_position, evaluation) = query_points[k]

@test evaluation == boundary_zone(particle_position)
end
end
end

@testset verbose=true "Particle In Boundary Zone 3D" begin
point1 = [-0.2, -0.5, 0.0]
point2 = [0.3, 0.5, 0.0]
point3 = [0.13111173850909402, -0.665555869254547, 0.0]

flow_direction = normalize(cross(point2 - point1, point3 - point1))

inflow = InFlow(; plane=[point1, point2, point3], particle_spacing=0.1,
flow_direction, density=1.0, open_boundary_layers=4)
outflow = OutFlow(; plane=[point1, point2, point3], particle_spacing=0.1,
flow_direction, density=1.0, open_boundary_layers=4)

boundary_zones = [
inflow,
outflow
]

@testset "$(nameof(typeof(boundary_zone)))" for boundary_zone in boundary_zones
perturb_ = boundary_zone isa InFlow ? eps() : -eps()
query_points = Dict(
"Behind" => ([-1.0, -1.0, 1.2], false),
"Before" => ([2.0, 2.0, -1.2], false),
"On Point 1" => (point1, true),
"On Point 2" => (point2, true),
"On Point 3" => (point3, true),
"On Point 4" => (boundary_zone.spanning_set[1] + boundary_zone.zone_origin,
true),
"Closely On Point 1" => (point1 .+ perturb_ * flow_direction, false))

@testset "$k" for k in keys(query_points)
(particle_position, evaluation) = query_points[k]

@test evaluation == boundary_zone(particle_position)
end
end
end

@testset verbose=true "Illegal Inputs" begin
no_rectangular_plane = [[0.2, 0.3, -0.5], [-1.0, 1.5, 0.2], [-0.2, 2.0, -0.5]]
flow_direction = [0.0, 0.0, 1.0]

error_str = "the provided points do not span a rectangular plane"

@test_throws ArgumentError(error_str) InFlow(; plane=no_rectangular_plane,
particle_spacing=0.1,
flow_direction, density=1.0,
open_boundary_layers=2)
@test_throws ArgumentError(error_str) OutFlow(; plane=no_rectangular_plane,
particle_spacing=0.1,
flow_direction, density=1.0,
open_boundary_layers=2)

rectangular_plane = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
flow_direction = [0.0, 1.0, 0.0]

error_str = "flow direction and normal vector of " *
"inflow-plane do not correspond"

@test_throws ArgumentError(error_str) InFlow(; plane=rectangular_plane,
particle_spacing=0.1,
flow_direction, density=1.0,
open_boundary_layers=2)

error_str = "flow direction and normal vector of " *
"outflow-plane do not correspond"

@test_throws ArgumentError(error_str) OutFlow(; plane=rectangular_plane,
particle_spacing=0.1,
flow_direction, density=1.0,
open_boundary_layers=2)
end
end
65 changes: 36 additions & 29 deletions test/schemes/boundary/open_boundary/characteristic_variables.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@testset verbose=true "Characteristic Variables" begin
particle_spacing = 0.1
boundary_zones = [InFlow(), OutFlow()]

# Number of boundary particles in the influence of fluid particles
influenced_particles = [20, 52, 26]
Expand All @@ -19,29 +18,37 @@
reference_density = (pos, t) -> 1000.0 * t

# Plane points of open boundary
point1s = [[0.0, 0.0], [0.5, -0.5], [1.0, 0.5]]
point2s = [[0.0, 1.0], [0.2, 2.0], [2.3, 0.5]]

@testset "$boundary_zone" for boundary_zone in boundary_zones
@testset "Points $(i)" for i in eachindex(point1s)
n_influenced = influenced_particles[i]

plane_points = [point1s[i], point2s[i]]

plane_size = plane_points[2] - plane_points[1]
flow_directions = [
normalize([-plane_size[2], plane_size[1]]),
-normalize([-plane_size[2], plane_size[1]]),
plane_points_1 = [[0.0, 0.0], [0.5, -0.5], [1.0, 0.5]]
plane_points_2 = [[0.0, 1.0], [0.2, 2.0], [2.3, 0.5]]

@testset "Points $(i)" for i in eachindex(plane_points_1)
n_influenced = influenced_particles[i]

plane_points = [plane_points_1[i], plane_points_2[i]]

plane_size = plane_points[2] - plane_points[1]
flow_directions = [
normalize([-plane_size[2], plane_size[1]]),
-normalize([-plane_size[2], plane_size[1]]),
]

@testset "Flow Direction $(j)" for j in eachindex(flow_directions)
flow_direction = flow_directions[j]
inflow = InFlow(; plane=plane_points, particle_spacing, density,
flow_direction, open_boundary_layers)
outflow = OutFlow(; plane=plane_points, particle_spacing, density,
flow_direction, open_boundary_layers)

boundary_zones = [
inflow,
outflow,
]

@testset "Flow Direction $(j)" for j in eachindex(flow_directions)
flow_direction = flow_directions[j]

inlet_system = OpenBoundarySPHSystem(plane_points, boundary_zone,
sound_speed; flow_direction,
particle_spacing, open_boundary_layers,
density, reference_velocity,
reference_pressure, reference_density)
@testset "$(nameof(typeof(boundary_zone)))" for boundary_zone in boundary_zones
boundary_system = OpenBoundarySPHSystem(boundary_zone, sound_speed;
reference_velocity,
reference_pressure,
reference_density)

sign_ = (boundary_zone isa InFlow) ? 1 : -1
fluid = extrude_geometry(plane_points; particle_spacing, n_extrude=4,
Expand All @@ -52,13 +59,13 @@
density_calculator=ContinuityDensity(),
smoothing_length, sound_speed)

semi = Semidiscretization(fluid_system, inlet_system)
semi = Semidiscretization(fluid_system, boundary_system)

ode = semidiscretize(semi, (0.0, 5.0))

v0_ode, u0_ode = ode.u0.x
v = TrixiParticles.wrap_v(v0_ode, inlet_system, semi)
u = TrixiParticles.wrap_u(u0_ode, inlet_system, semi)
v = TrixiParticles.wrap_v(v0_ode, boundary_system, semi)
u = TrixiParticles.wrap_u(u0_ode, boundary_system, semi)

# ==== Characteristic Variables
# `J1 = -sound_speed^2 * (rho - rho_ref) + (p - p_ref)`
Expand All @@ -82,9 +89,9 @@
# First evaluation
# Particles not influenced by the fluid have zero values
t1 = 2.0
TrixiParticles.evaluate_characteristics!(inlet_system,
TrixiParticles.evaluate_characteristics!(boundary_system,
v, u, v0_ode, u0_ode, semi, t1)
evaluated_vars1 = inlet_system.characteristics
evaluated_vars1 = boundary_system.characteristics

if boundary_zone isa InFlow
@test all(isapprox.(evaluated_vars1[1, :], 0.0))
Expand All @@ -102,9 +109,9 @@
# Second evaluation
# Particles not influenced by the fluid have previous values
t2 = 3.0
TrixiParticles.evaluate_characteristics!(inlet_system,
TrixiParticles.evaluate_characteristics!(boundary_system,
v, u, v0_ode, u0_ode, semi, t2)
evaluated_vars2 = inlet_system.characteristics
evaluated_vars2 = boundary_system.characteristics

if boundary_zone isa InFlow
@test all(isapprox.(evaluated_vars2[1, :], 0.0))
Expand Down
2 changes: 2 additions & 0 deletions test/schemes/boundary/open_boundary/open_boundary.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include("characteristic_variables.jl")
include("boundary_zone.jl")
2 changes: 1 addition & 1 deletion test/schemes/schemes.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include("solid/total_lagrangian_sph/total_lagrangian_sph.jl")
include("boundary/dummy_particles/dummy_particles.jl")
include("boundary/monaghan_kajtar/monaghan_kajtar.jl")
include("boundary/open_boundary/characteristic_variables.jl")
include("boundary/open_boundary/open_boundary.jl")
include("fluid/fluid.jl")
Loading

0 comments on commit a3adf1d

Please sign in to comment.