Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate rectangular shape and tank #305

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/fluid/rectangular_tank_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ state_equation = StateEquationCole(sound_speed, 7, fluid_density, atmospheric_pr

tank = RectangularTank(fluid_particle_spacing, initial_fluid_size, tank_size, fluid_density,
n_layers=boundary_layers, spacing_ratio=spacing_ratio,
acceleration=(0.0, -gravity), state_equation=state_equation)
acceleration=(0.0, -gravity), state_equation=state_equation, rot_angle=π/6)

# ==========================================================================================
# ==== Fluid
Expand Down
17 changes: 16 additions & 1 deletion src/setups/rectangular_shape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function RectangularShape(particle_spacing, n_particles_per_dimension,
min_coordinates, density;
init_velocity=ntuple(_ -> 0.0, length(n_particles_per_dimension)),
acceleration=nothing, state_equation=nothing,
pressure=0.0, tlsph=false,
pressure=0.0, tlsph=false, rot_angle=0,
loop_order=nothing)
if particle_spacing < eps()
throw(ArgumentError("`particle_spacing` needs to be positive and larger than $(eps())"))
Expand All @@ -76,6 +76,21 @@ function RectangularShape(particle_spacing, n_particles_per_dimension,
coordinates = rectangular_shape_coords(particle_spacing, n_particles_per_dimension,
min_coordinates, tlsph=tlsph,
loop_order=loop_order)

if rot_angle != 0
# Set coordinates to origin
coordinates .-= min_coordinates

# Rotate each vector
for particle in axes(coordinates, 2)
particle_position = extract_svector(coordinates, Val(NDIMS), particle)
coordinates[:, particle] = rot_matrix(rot_angle, Val(NDIMS)) * particle_position
end

# Sift coordinates back
coordinates .+= min_coordinates
end

velocities = init_velocity .* ones(ELTYPE, size(coordinates))

# Allow zero acceleration with state equation, but interpret `nothing` acceleration
Expand Down
22 changes: 19 additions & 3 deletions src/setups/rectangular_tank.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@doc raw"""
RectangularTank(particle_spacing, fluid_size, tank_size, fluid_density;
n_layers=1, spacing_ratio=1.0,
n_layers=1, spacing_ratio=1.0, min_coordinates=zeros(length(fluid_size))
init_velocity=zeros(length(fluid_size)),
boundary_density=fluid_density,
faces=Tuple(trues(2 * length(fluid_size))))
Expand All @@ -18,6 +18,7 @@ Rectangular tank filled with a fluid to set up dam-break-style simulations.
- `spacing_ratio`: Ratio of `particle_spacing` to boundary particle spacing.
A value of 2 means that the boundary particle spacing will be
half the fluid particle spacing.
- `min_coordinates`: Coordinates of the corner in negative coordinate directions.
- `init_velocity`: The initial velocity of each fluid particle as `(x, y)` (or `(x, y, z)` in 3D).
- `boundary_density`: Density of each boundary particle (by default set to the rest density)
- `faces`: By default all faces are generated. Set faces by passing a
Expand Down Expand Up @@ -58,8 +59,9 @@ struct RectangularTank{NDIMS, NDIMSt2, ELTYPE <: Real}

function RectangularTank(particle_spacing, fluid_size, tank_size, fluid_density;
pressure=0.0, n_layers=1, spacing_ratio=1.0,
min_coordinates=zeros(length(fluid_size)),
init_velocity=zeros(length(fluid_size)),
boundary_density=fluid_density,
boundary_density=fluid_density, rot_angle=0,
faces=Tuple(trues(2 * length(fluid_size))),
acceleration=nothing, state_equation=nothing)
NDIMS = length(fluid_size)
Expand Down Expand Up @@ -112,13 +114,27 @@ struct RectangularTank{NDIMS, NDIMSt2, ELTYPE <: Real}

fluid = RectangularShape(particle_spacing, n_particles_per_dim, zeros(NDIMS),
fluid_density, init_velocity=init_velocity,
pressure=pressure,
pressure=pressure,rot_angle=rot_angle,
acceleration=acceleration, state_equation=state_equation)

boundary = InitialCondition(boundary_coordinates, boundary_velocities,
boundary_masses, boundary_densities,
particle_spacing=boundary_spacing)

if rot_angle != 0
# Rotate each boundary particle vector
for particle in axes(boundary.coordinates, 2)
particle_position = extract_svector(boundary.coordinates, boundary,
particle)
boundary.coordinates[:, particle] = rot_matrix(rot_angle, Val(NDIMS)) *
particle_position
end
end

# Move the tank corner in the negative coordinate directions to the desired position.
fluid.coordinates .+= min_coordinates
boundary.coordinates .+= min_coordinates

return new{NDIMS, 2 * NDIMS, ELTYPE}(fluid, boundary, fluid_size_, tank_size_,
faces, face_indices,
particle_spacing, spacing_ratio, n_layers,
Expand Down
4 changes: 4 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,7 @@ end
function type2string(type)
return string(nameof(typeof(type)))
end

rot_matrix(θ, NDIMS::Val{1}) = sign(θ) == π ? -1 : 1
rot_matrix(θ, NDIMS::Val{2}) = SMatrix{2, 2}(cos(θ), -sin(θ), sin(θ), cos(θ))
LasNikas marked this conversation as resolved.
Show resolved Hide resolved
rot_matrix(θ, NDIMS::Val{3}) = SMatrix{3, 3}(cos(θ), -sin(θ), 0, sin(θ), cos(θ), 0, 0, 0, 1)
57 changes: 35 additions & 22 deletions test/setups/rectangular_tank.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@testset "Coordinates" begin
particle_spacings = [0.1, 0.2]
spacing_ratios = [1, 3]
min_coordinates = [(0.0, 0.0), (-0.3, 2.0)]

expected_fluid_coords = [
[0.05 0.15 0.25 0.35 0.45 0.05 0.15 0.25 0.35 0.45 0.05 0.15 0.25 0.35 0.45 0.05 0.15 0.25 0.35 0.45;
Expand All @@ -33,17 +34,22 @@
],
]

@testset "Particle Spacing: $(particle_spacings[i])" for i in eachindex(particle_spacings)
@testset "Spacing Ratio: $(spacing_ratios[j])" for j in eachindex(spacing_ratios)
tank = RectangularTank(particle_spacings[i],
(water_width, water_height),
(tank_width, tank_height),
water_density,
spacing_ratio=spacing_ratios[j])

@test isapprox(tank.fluid.coordinates, expected_fluid_coords[i])
@test isapprox(tank.boundary.coordinates,
expected_bound_coords[i][j])
@testset "Move Tank: $(min_coordinates[i])" for i in eachindex(min_coordinates)
@testset "Particle Spacing: $(particle_spacings[j])" for j in eachindex(particle_spacings)
@testset "Spacing Ratio: $(spacing_ratios[k])" for k in eachindex(spacing_ratios)
tank = RectangularTank(particle_spacings[j],
(water_width, water_height),
(tank_width, tank_height),
water_density,
spacing_ratio=spacing_ratios[k],
min_coordinates=min_coordinates[i])
expected_fluid_coords_ = copy(expected_fluid_coords[j])
expected_bound_coords_ = copy(expected_bound_coords[j][k])
expected_fluid_coords_ .+= min_coordinates[i]
expected_bound_coords_ .+= min_coordinates[i]
@test isapprox(tank.fluid.coordinates, expected_fluid_coords_)
@test isapprox(tank.boundary.coordinates, expected_bound_coords_)
end
end
end
end
Expand Down Expand Up @@ -267,6 +273,7 @@ end
@testset "Coordinates" begin
particle_spacings = [0.1, 0.2]
spacing_ratios = [1, 3]
min_coordinates = [(0.0, 0.0, 0.0), (-0.3, 2.0, -0.5)]

expected_fluid_coords = [
[0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35 0.05 0.15 0.25 0.35;
Expand Down Expand Up @@ -296,17 +303,23 @@ end
],
]

@testset "Particle Spacing: $(particle_spacings[i])" for i in eachindex(particle_spacings)
@testset "Spacing Ratio: $(spacing_ratios[j])" for j in eachindex(spacing_ratios)
tank = RectangularTank(particle_spacings[i],
(water_width, water_height, water_depth),
(tank_width, tank_height, tank_depth),
water_density,
spacing_ratio=spacing_ratios[j])

@test isapprox(tank.fluid.coordinates, expected_fluid_coords[i])
@test isapprox(tank.boundary.coordinates,
expected_bound_coords[i][j])
@testset "Move Tank: $(min_coordinates[i])" for i in eachindex(min_coordinates)
@testset "Particle Spacing: $(particle_spacings[j])" for j in eachindex(particle_spacings)
@testset "Spacing Ratio: $(spacing_ratios[k])" for k in eachindex(spacing_ratios)
tank = RectangularTank(particle_spacings[j],
(water_width, water_height, water_depth),
(tank_width, tank_height, tank_depth),
water_density,
spacing_ratio=spacing_ratios[k],
min_coordinates=min_coordinates[i])
expected_fluid_coords_ = copy(expected_fluid_coords[j])
expected_bound_coords_ = copy(expected_bound_coords[j][k])
expected_fluid_coords_ .+= min_coordinates[i]
expected_bound_coords_ .+= min_coordinates[i]

@test isapprox(tank.fluid.coordinates, expected_fluid_coords_)
@test isapprox(tank.boundary.coordinates, expected_bound_coords_)
end
end
end
end
Expand Down
Loading