Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
KnutAM committed Dec 2, 2023
2 parents bbbef1f + 16cc109 commit 47a869e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/stressiterations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function reduced_material_response(stress_state::NoIterationState,
end

function reduced_material_response(stress_state::IterationState,
m::AbstractMaterial, ϵ::AbstractTensor, args...)
m::AbstractMaterial, ϵ::AbstractTensor, args::Vararg{Any,N}) where N

# Newton options
tol = get_tolerance(stress_state)
Expand Down
32 changes: 32 additions & 0 deletions test/performance.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Checks to ensure that e.g. calls are allocation-free
# If functions that shouldn't allocate do, this could be a
# sign of type-instability, would have caught e.g.
# https://github.com/KnutAM/MaterialModelsBase.jl/pull/10

function get_run_allocations(ss, m, ϵv, t = collect(range(0, 1, length(ϵv))))
state, cache, σv, ϵv_full = setup_run_timehistory(m, ϵv)
run_timehistory!(σv, ϵv_full, state, cache, ss, m, ϵv, t) # Compilation
return @allocated run_timehistory!(σv, ϵv_full, state, cache, ss, m, ϵv, t)
end

@testset "performance" begin
# Load cases
N = 100
load_cases = (
(UniaxialStress(),
collect((SymmetricTensor{2,1}((0.1*(i-1)/N,)) for i in 1:N+1))),
)
# Materials
G = 80.e3; K = 160.e3; η = 20.e3
linear_elastic = LinearElastic(G, K)
visco_elastic = ViscoElastic(linear_elastic, LinearElastic(G*0.5, K*0.5), η)
materials = (linear_elastic, visco_elastic)

for (stress_state, ϵv) in load_cases
for m in materials
allocs = get_run_allocations(stress_state, m, ϵv)
allocs > 0 && println(allocs, " allocations for m::", typeof(m), ", ss::", typeof(stress_state))
@test allocs == 0
end
end
end
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const MMB = MaterialModelsBase

include("TestMaterials.jl")
using .TestMaterials
include("utils4testing.jl")

include("stressiterations.jl")

include("differentiation.jl")

include("errors.jl")
include("errors.jl")
include("performance.jl")
39 changes: 0 additions & 39 deletions test/stressiterations.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
_getdim(::MaterialModelsBase.State1D) = 1
_getdim(::MaterialModelsBase.State2D) = 2
_getdim(::MaterialModelsBase.State3D) = 3
_getdim(::AbstractTensor{order,dim}) where {order,dim} = dim

tensortype(::SymmetricTensor) = SymmetricTensor
tensortype(::Tensor) = Tensor

# Create GeneralStressState from regular states to check compatibility
function MMB.GeneralStressState(::UniaxialStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->!(i==j==1))
return MMB.GeneralStressState(σ_ctrl, σ)
end
function MMB.GeneralStressState(::PlaneStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->(i==3 || j==3))
return MMB.GeneralStressState(σ_ctrl, σ)
end
function MMB.GeneralStressState(::UniaxialNormalStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->(i==j(2,3)))
return MMB.GeneralStressState(σ_ctrl, σ)
end

all_states = (
FullStressState(), UniaxialStrain(), PlaneStrain(),
UniaxialStress(), PlaneStress(), UniaxialNormalStress())
Expand All @@ -32,20 +7,6 @@ iter_mandel = ( ([2,3,4,5,6,7,8,9],[2,3,4,5,6]),
([2,3], [2,3]))


function run_timehistory(s::MMB.AbstractStressState, m::AbstractMaterial, ϵv::Vector{<:AbstractTensor}, t = collect(range(0,1;length=length(ϵ))))
state = initial_material_state(m)
cache = allocate_material_cache(m)
σv = eltype(ϵv)[]
ϵv_full = tensortype(first(ϵv))[]
local dσdϵ
for i in 2:length(ϵv)
σ, dσdϵ, state, ϵ_full = material_response(s, m, ϵv[i], state, t[i]-t[i-1], cache)
push!(σv, σ)
push!(ϵv_full, ϵ_full)
end
return σv, ϵv_full, dσdϵ
end

@testset "conversions" begin
# Test that
# 1) All conversions are invertible (convert back and fourth)
Expand Down
48 changes: 48 additions & 0 deletions test/utils4testing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
_getdim(::MaterialModelsBase.State1D) = 1
_getdim(::MaterialModelsBase.State2D) = 2
_getdim(::MaterialModelsBase.State3D) = 3
_getdim(::AbstractTensor{order,dim}) where {order,dim} = dim

tensortype(::SymmetricTensor) = SymmetricTensor
tensortype(::Tensor) = Tensor

# Create GeneralStressState from regular states to check compatibility
function MMB.GeneralStressState(::UniaxialStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->!(i==j==1))
return MMB.GeneralStressState(σ_ctrl, σ)
end
function MMB.GeneralStressState(::PlaneStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->(i==3 || j==3))
return MMB.GeneralStressState(σ_ctrl, σ)
end
function MMB.GeneralStressState(::UniaxialNormalStress, TT::Type{<:SecondOrderTensor{3}})
σ = zero(TT)
σ_ctrl = tensortype(σ){2,3,Bool}((i,j)->(i==j(2,3)))
return MMB.GeneralStressState(σ_ctrl, σ)
end

function run_timehistory!(σv::Vector, ϵv_full::Vector, state, cache, s, m, ϵv, t)
local dσdϵ
for i in 1:(length(ϵv)-1)
σ, dσdϵ, state, ϵ_full = material_response(s, m, ϵv[i+1], state, t[i+1]-t[i], cache)
σv[i] = σ
ϵv_full[i] = ϵ_full
end
return dσdϵ
end

function setup_run_timehistory(m::AbstractMaterial, ϵv::Vector{<:AbstractTensor})
state = initial_material_state(m)
cache = allocate_material_cache(m)
σv = zeros(eltype(ϵv), length(ϵv)-1)
ϵv_full = zeros(tensortype(first(ϵv)){2,3}, length(ϵv)-1)
return state, cache, σv, ϵv_full
end

function run_timehistory(s, m::AbstractMaterial, ϵv, t = collect(range(0, 1, length(ϵv))))
state, cache, σv, ϵv_full = setup_run_timehistory(m, ϵv)
dσdϵ = run_timehistory!(σv, ϵv_full, state, cache, s, m, ϵv, t)
return σv, ϵv_full, dσdϵ
end

0 comments on commit 47a869e

Please sign in to comment.