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

Transform (rotate, scale and translate) initial conditions #583

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export BoundaryMovement
export examples_dir, validation_dir, trixi_include
export trixi2vtk
export RectangularTank, RectangularShape, SphereShape
export VoxelSphere, RoundSphere, reset_wall!, extrude_geometry
export VoxelSphere, RoundSphere, reset_wall!, extrude_geometry, transform!
export SourceTermDamping
export ShepardKernelCorrection, KernelCorrection, AkinciFreeSurfaceCorrection,
GradientCorrection, BlendedGradientCorrection, MixedKernelGradientCorrection
Expand Down
41 changes: 41 additions & 0 deletions src/general/initial_condition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,43 @@ end

Base.intersect(initial_condition::InitialCondition) = initial_condition

function transform!(initial_condition::InitialCondition;
rotate=nothing, translate=nothing, scale=nothing)
(; coordinates) = initial_condition

NDIMS = ndims(initial_condition)

if !isnothing(rotate)
if isa(rotate, Real)
rot = rot_matrix(rotate, Val(NDIMS))
for particle in axes(coordinates, 2)
particle_position = extract_svector(coordinates, Val(NDIMS), particle)
coordinates[:, particle] = rot * particle_position
end
else
throw(ArgumentError("`rotate` must be of type `Real`"))
end
end

if !isnothing(scale)
if isa(scale, Real)
coordinates .*= scale
else
throw(ArgumentError("`scale` must be of type `Real`"))
end
end

if !isnothing(translate)
if length(translate) == NDIMS
coordinates .+= translate
else
throw(ArgumentError("`translate` must be of length $NDIMS for a $NDIMS-D problem"))
end
end

return initial_condition
end

# Find particles in `coords1` that are closer than `max_distance` to any particle in `coords2`
function find_too_close_particles(coords1, coords2, max_distance)
NDIMS = size(coords1, 1)
Expand All @@ -306,3 +343,7 @@ function find_too_close_particles(coords1, coords2, max_distance)

return result
end

rot_matrix(θ, NDIMS::Val{1}) = 0 < θ <= π ? -1 : 1
rot_matrix(θ, NDIMS::Val{2}) = @SMatrix [cos(θ) -sin(θ); sin(θ) cos(θ)]
rot_matrix(θ, NDIMS::Val{3}) = @SMatrix [cos(θ) -sin(θ) 0; sin(θ) cos(θ) 0; 0 0 1]
Loading