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

Add tests for rotate_axes #20

Merged
merged 8 commits into from
Feb 16, 2023
4 changes: 2 additions & 2 deletions src/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ for S in (:StiffnessTensor, :ComplianceTensor)
@eval function rotate_axes(T::$S, Q::AbstractMatrix)
@assert isrotation(Q)
@einsum T′[i, j, k, l] := T[m, n, o, p] * Q[i, m] * Q[j, n] * Q[k, o] * Q[l, p]
return $S(T)
return $S(T)
end
end
for S in (:TensorStrain, :TensorStress)
@eval function rotate_axes(T::$S, Q::AbstractMatrix)
@assert isrotation(Q)
@einsum T′[i, j] := T[k, l] * Q[i, k] * Q[j, l]
return $S(T)
return $S(T)
end
end
for S in (:StiffnessMatrix, :ComplianceMatrix, :EngineeringStrain, :EngineeringStress)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ using Test
include("operations.jl")
include("invariants.jl")
include("axial.jl")
include("transform.jl")
end
110 changes: 110 additions & 0 deletions test/transform.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using LinearAlgebra: diag

@testset "Test example from notes (3.35)" begin
# TODO: See https://github.com/ahwillia/Einsum.jl/issues/43
for τ in (250,)
𝟘 = zero(τ)
σ = TensorStress([
𝟘 τ 𝟘
τ 𝟘 𝟘
𝟘 𝟘 𝟘
])
Q = [
1 1 0
-1 1 0
0 0 √2
] / √2
@test rotate_axes(σ, Q) ≈ TensorStress([
τ 𝟘 𝟘
𝟘 -τ 𝟘
𝟘 𝟘 𝟘
])
@test rotate_axes(EngineeringStress(σ), Q) ≈ EngineeringStress(τ, -τ, 0, 0, 0, 0)
end
end

@testset "Test homework 3 question 1" begin
σ = TensorStress([
0 0 0
0 245 0
0 0 0
])
Q = [
1 1 0
-1 1 0
0 0 √2
] / √2
@test rotate_axes(σ, Q)[1, 2] ≈ sin(2 * pi / 4) / 2 * σ[2, 2]
end

@testset "Test homework 3 question 2" begin
σ = TensorStress([
300 100 0
100 100 0
0 0 0
])
Q = θ -> [
cos(θ) sin(θ) 0
-sin(θ) cos(θ) 0
0 0 1
]
@testset "Determine the principal stresses" begin
@test rotate_axes(σ, Q(pi / 8)) ≈ TensorStress([
200+100 * √2 0 0
0 200-100 * √2 0
0 0 0
])
end
@testset "Determine the maximum in-plane shear" begin
@test rotate_axes(σ, Q(-pi / 8))[1, 2] == 100 * √2
@test rotate_axes(σ, Q(3pi / 8))[1, 2] == -100 * √2
end
end

@testset "Test question 5 in midterm 1, 2019" begin
σ₀ = 250
σ = TensorStress([
σ₀ 0 0
0 2σ₀ 0
0 0 0
])
Q = [
1/√2 -1/√2 0
1/√6 1/√6 -2/√6
1/√3 1/√3 1/√3
]
@test rotate_axes(σ, Q)[3, 1] ≈ -σ₀ / √6
end

@testset "Test question 6 in midterm 1, 2019" begin
σ₀ = 360
σ = TensorStress([
σ₀ σ₀/3 0
σ₀/3 σ₀ 0
0 0 0
])
Q = θ -> [
cos(θ) sin(θ) 0
-sin(θ) cos(θ) 0
0 0 1
]
@test rotate_axes(σ, Q(-pi / 4)) ≈ TensorStress(diagm([2σ₀ / 3, 4σ₀ / 3, 0]))
@test rotate_axes(σ, Q(pi / 4)) == TensorStress(diagm([4σ₀ / 3, 2σ₀ / 3, 0]))
@test principal_values(σ) ≈ [0, 2σ₀ / 3, 4σ₀ / 3]
end

@testset "Test using principal axes as rotation matrix" begin
ε = TensorStrain([
0.5 0.3 0.2
0.3 -0.2 -0.1
0.2 -0.1 0.1
])
evecs = principal_axes(ε)'
normal_strains = rotate_axes(ε, evecs)
@test norm(normal_strains - [
-0.370577 0 0
0 0.115308 0
0 0 0.655269
]) < 1e-6
@test principal_values(ε) ≈ diag(normal_strains)
end