Skip to content

Commit

Permalink
Merge pull request #108 from bcube-project/dev_rotation
Browse files Browse the repository at this point in the history
New affine transformation `Rotation`
  • Loading branch information
bmxam authored Jun 17, 2024
2 parents d41bd31 + c612471 commit 25de50f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Bcube.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import .LazyOperators:
include("utils.jl")

include("./mesh/transformation.jl")
export Translation
export Translation, Rotation

include("./mesh/boundary_condition.jl")
export BoundaryCondition, PeriodicBCType
Expand Down
4 changes: 2 additions & 2 deletions src/mesh/domain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function _compute_periodicity(mesh, labels1, labels2, A, tol = 1e-9)
nbnd2 = length(bndfaces2)

# Allocate result
bnd_f2n1 = [f2n[iface] for iface in bndfaces2]
bnd_f2n2 = [f2n[iface] for iface in bndfaces2]
bnd_f2n1 = [zero(f2n[iface]) for iface in bndfaces1]
bnd_f2n2 = [zero(f2n[iface]) for iface in bndfaces2]
bnd_f2c = zeros(Int, nbnd2, 2) # Adjacent cells for each bnd face
bnd_ftypes = Array{AbstractEntityType}(undef, nbnd2)

Expand Down
9 changes: 6 additions & 3 deletions src/mesh/gmsh_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1240,12 +1240,15 @@ function gen_cylinder_shell_mesh(
# Mesh settings
if transfinite
_nθ = round(Int, nθ / 3)
map(
foreach(
line -> gmsh.model.geo.mesh.setTransfiniteCurve(line, _nθ),
(AOB1, BOC1, COA1, AOB2, BOC2, COA2),
)
map(line -> gmsh.model.geo.mesh.setTransfiniteCurve(line, nz), (A1A2, B1B2, C1C2))
map(surf -> gmsh.model.geo.mesh.setTransfiniteSurface(surf), surfs)
foreach(
line -> gmsh.model.geo.mesh.setTransfiniteCurve(line, nz),
(A1A2, B1B2, C1C2),
)
foreach(gmsh.model.geo.mesh.setTransfiniteSurface, surfs)
end
recombine && map(surf -> gmsh.model.geo.mesh.setRecombine(2, surf), surfs)

Expand Down
29 changes: 29 additions & 0 deletions src/mesh/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,32 @@ struct Translation{T} <: AbstractAffineTransformation
end
(t::Translation)(x) = x .+ t.vec
Base.inv(t::Translation) = Translation(-t.vec)

struct Rotation{T} <: AbstractAffineTransformation
A::T
end

"""
Rotation(u::AbstractVector, θ::Number)
Return a `Rotation` operator build from a given
rotation axis `u` and an angle of rotation `θ`.
# Example :
```julia
julia> rot = Rotation([0,0,1], π/4);
julia> x = [3.0,0,0];
julia> x_rot_ref = 3√(2)/2 .*[1,1,0] # expected result
julia> all(rot(x) .≈ x_rot_ref)
true
```
"""
function Rotation(u::AbstractVector, θ::Number)
Rotation((normalize(u), sincos(θ)))
end

function (r::Rotation)(x)
u, (sinθ, cosθ) = r.A
rx = cosθ * x + (1 - cosθ) * (u x) * u + sinθ * cross(u, x)
return rx
end
15 changes: 15 additions & 0 deletions test/mesh/test_transformation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "Transformation" begin
@testset "Translation" begin
u = [2, 0]
T = Translation(u)
x = [3, 1]
@test all(T(x) .== [5, 1])
end

@testset "Rotation" begin
rot = Rotation([0, 0, 1], π / 4)
x = [3.0, 0, 0]
x_rot_ref = 3(2) / 2 .* [1, 1, 0]
@test all(rot(x) .≈ x_rot_ref)
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ fname2sum = Dict(r[2] => r[1] for r in eachrow(f))
custom_include("./test_utils.jl")
custom_include("./mesh/test_entity.jl")
custom_include("./mesh/test_connectivity.jl")
custom_include("./mesh/test_transformation.jl")
custom_include("./mesh/test_mesh.jl")
custom_include("./mesh/test_mesh_generator.jl")
custom_include("./mesh/test_gmsh.jl")
Expand Down

0 comments on commit 25de50f

Please sign in to comment.