Skip to content

Commit

Permalink
add rotation (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored Jan 19, 2017
1 parent d914af7 commit 7d0b7ba
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/man/other_operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ where ``\lambda_i`` are the eigenvalues and ``\mathbf{v}_i`` are the correspondi
eig
```

## Rotations

```@docs
rotate
```

## Special operations

For computing a special dot product between two vectors $\mathbf{a}$ and $\mathbf{b}$ with a fourth order symmetric tensor $\mathbf{C}$ such that $a_k C_{ikjl} b_l$ there is `dotdot(a, C, b)`. This function is useful because it is the expression for the tangent matrix in continuum mechanics when the displacements are approximated by scalar shape functions.
Expand Down
2 changes: 2 additions & 0 deletions src/ContMechTensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export minortranspose, majortranspose, isminorsymmetric, ismajorsymmetric
export tdot, dott, dotdot
export hessian#, gradient
export basevec, eᵢ
export rotate


@deprecate extract_components(tensor) Array(tensor)

Expand Down
39 changes: 39 additions & 0 deletions src/math_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,42 @@ julia> trace(dev(A))
$Tt($exp)
end
end

# http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation/
"""
Rotate a three dimensional vector `x` around another vector `u` a total of `θ` radians.
```julia
rotate(x::Vec{3}, u::Vec{3}, θ::Number)
```
**Example:**
```jldoctest
julia> x = Vec{3}((0.0, 0.0, 1.0))
3-element ContMechTensors.Tensor{1,3,Float64,3}:
0.0
0.0
1.0
julia> u = Vec{3}((0.0, 1.0, 0.0))
3-element ContMechTensors.Tensor{1,3,Float64,3}:
0.0
1.0
0.0
julia> rotate(x, u, π/2)
3-element ContMechTensors.Tensor{1,3,Float64,3}:
1.0
0.0
6.12323e-17
```
"""
function rotate(x::Vec{3}, u::Vec{3}, θ::Number)
ux = u x
= u u
c = cos(θ)
s = sin(θ)
(u * ux * (1 - c) +* x * c + sqrt(u²) * (u × x) * s) /
end

18 changes: 18 additions & 0 deletions test/test_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,23 @@ end # of testset
AAT = Tensor{4, dim, T}((i,j,k,l) -> AA_sym[i,l,k,j])
@test AAT (b a) dotdot(a, AA_sym, b)
end # of testset

@testset "rotation" begin
x = eᵢ(Vec{3}, 1)
y = eᵢ(Vec{3}, 2)
z = eᵢ(Vec{3}, 3)

@test rotate(z, z, rand()) z
@test rotate(2*z, y, π/2) 2*x
@test rotate(3*z, y, π) -3*z
@test rotate(x+y+z, z, π/4) Vec{3}((0.0,2,1.0))

a = rand(Vec{3})
b = rand(Vec{3})
@test rotate(a, b, 0) a
@test rotate(a, b, π) rotate(a, b, -π)
@test rotate(a, b, π/2) rotate(a, -b, -π/2)
end # of testset

end
end # of testset

0 comments on commit 7d0b7ba

Please sign in to comment.