-
Couldn't load subscription status.
- Fork 6
Refactor cache #42
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
Refactor cache #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,14 +48,18 @@ end | |
| Base.zero(::Type{PlasticState{dim,T,M}}) where {dim,T,M} = PlasticState(zero(SymmetricTensor{2,dim,T,M}), zero(T), zero(SymmetricTensor{2,dim,T,M}), zero(T)) | ||
| initial_material_state(::Plastic) = zero(PlasticState{3,Float64,6}) | ||
|
|
||
| struct PlasticCache{T<:OnceDifferentiable} <: AbstractCache | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be |
||
| nlsolve_cache::T | ||
| end | ||
|
|
||
| function get_cache(m::Plastic) | ||
| state = initial_material_state(m) | ||
| # it doesn't actually matter which state and strain step we use here, | ||
| # f is overwritten in the constitutive driver before being used. | ||
| f(r_vector, x_vector) = vector_residual!(((x)->MaterialModels.residuals(x, m, state, zero(SymmetricTensor{2,3}))), r_vector, x_vector, m) | ||
| v_cache = Vector{Float64}(undef, get_n_scalar_equations(m)) | ||
| cache = NLsolve.OnceDifferentiable(f, v_cache, v_cache; autodiff = :forward) | ||
| return cache | ||
| return PlasticCache(cache) | ||
| end | ||
|
|
||
| # constitutive driver operates in 3D, so these can always be 3D | ||
|
|
@@ -122,28 +126,30 @@ See [NLsolve documentation](https://github.com/JuliaNLSolvers/NLsolve.jl#common- | |
| function material_response(m::Plastic, ε::SymmetricTensor{2,3,T,6}, state::PlasticState{3}, | ||
| Δt=nothing; cache=get_cache(m), options::Dict{Symbol, Any} = Dict{Symbol, Any}()) where T | ||
|
|
||
| nlsolve_cache = cache.nlsolve_cache | ||
|
|
||
| σ_trial = m.Eᵉ ⊡ (ε - state.εᵖ) | ||
| Φ = sqrt(3/2)*norm(dev(σ_trial-state.α)) - m.σ_y - state.κ | ||
|
|
||
| if Φ <= 0 | ||
| return σ_trial, m.Eᵉ, state | ||
| else | ||
| # set the current residual function that depends only on the variables | ||
| # cache.f = (r_vector, x_vector) -> vector_residual!(((x)->residuals(x,m,state,Δε)), r_vector, x_vector, m) | ||
| # nlsolve_cache.f = (r_vector, x_vector) -> vector_residual!(((x)->residuals(x,m,state,Δε)), r_vector, x_vector, m) | ||
| f(r_vector, x_vector) = vector_residual!(((x)->residuals(x,m,state,ε)), r_vector, x_vector, m) | ||
| update_cache!(cache, f) | ||
| update_cache!(nlsolve_cache, f) | ||
| # initial guess | ||
| x0 = ResidualsPlastic(σ_trial, state.κ, state.α, state.μ) | ||
| # convert initial guess to vector | ||
| tomandel!(cache.x_f, x0) | ||
| tomandel!(nlsolve_cache.x_f, x0) | ||
| # solve for variables x | ||
| nlsolve_options = get(options, :nlsolve_params, Dict{Symbol, Any}(:method=>:newton)) | ||
| haskey(nlsolve_options, :method) || merge!(nlsolve_options, Dict{Symbol, Any}(:method=>:newton)) # set newton if the user did not supply another method | ||
| result = NLsolve.nlsolve(cache, cache.x_f; nlsolve_options...) | ||
| result = NLsolve.nlsolve(nlsolve_cache, nlsolve_cache.x_f; nlsolve_options...) | ||
| if result.f_converged | ||
| x = frommandel(ResidualsPlastic, result.zero::Vector{T}) | ||
| εᵖ = state.εᵖ + x.μ*(3/(2*sqrt(3/2)*norm(dev(x.σ-x.α)))*dev(x.σ-x.α)) | ||
| dRdx = cache.DF | ||
| dRdx = nlsolve_cache.DF | ||
| inv_J_σσ = frommandel(SymmetricTensor{4,3}, inv(dRdx)) | ||
| ∂σ∂ε = inv_J_σσ ⊡ m.Eᵉ | ||
| return x.σ, ∂σ∂ε, PlasticState(εᵖ, x.κ, x.α, x.μ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually prefer being explicit with the return statements (and I think we pretty much are in the rest of the package).