diff --git a/src/transform.jl b/src/transform.jl index 8228acf..ea2a1ca 100644 --- a/src/transform.jl +++ b/src/transform.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index acbb52f..fcabdc1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,4 +6,5 @@ using Test include("operations.jl") include("invariants.jl") include("axial.jl") + include("transform.jl") end diff --git a/test/transform.jl b/test/transform.jl new file mode 100644 index 0000000..27c82da --- /dev/null +++ b/test/transform.jl @@ -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