Skip to content

Commit

Permalink
Fix softening bug (#154)
Browse files Browse the repository at this point in the history
* improve softening testing

* fix bug and improve args handling
  • Loading branch information
albert-de-montserrat authored Jan 24, 2024
1 parent b7d2750 commit 7bca8f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/Softening/Softening.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ struct LinearSoftening{T} <: AbstractSoftening
lo::T
max_value::T
min_value::T
damage::T
slope::T

function LinearSoftening(min_value::T, max_value::T, lo::T, hi::T) where T
damage = max_value + (max_value - min_value) / (hi - lo)
new{T}(hi, lo, max_value, min_value, damage)
slope = (max_value - min_value) / (hi - lo)
new{T}(hi, lo, max_value, min_value, slope)
end
end

LinearSoftening(min_max_values::NTuple{2, T}, lo_hi::NTuple{2, T}) where T = LinearSoftening(min_max_values..., lo_hi...)

function (softening::LinearSoftening)(max_value, softening_var::T) where T
@inline (softening::LinearSoftening)(args::Vararg{Any, N}) where N = softening(promote(args...)...)

@inline function (softening::LinearSoftening)(max_value::T, softening_var::T) where T

softening_var softening.hi && return softening.min_value
softening_var softening.lo && return T(max_value)
softening_var softening.lo && return max_value

return softening_var * softening.damage
return softening.min_value + (one(T) - softening_var) * softening.slope
end
22 changes: 16 additions & 6 deletions test/test_Softening.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ using GeoParams, Test
@test x === soft(x, rand())

# Test LinearSoftening
min_v, max_v = 15e0, 30e0
min_v, max_v = rand()*15, (rand()+1)*15
lo, hi = 0.0, 1.0

@test LinearSoftening(min_v, max_v, lo, hi) === LinearSoftening((min_v, max_v), (lo, hi))

soft_ϕ = LinearSoftening(min_v, max_v, lo, hi)

@test soft_ϕ(30, 1) == min_v
@test soft_ϕ(30, 0) == max_v
@test soft_ϕ(30, 0.5) == 0.5 * (min_v + max_v)
@test soft_ϕ(max_v, 1) == min_v
@test soft_ϕ(max_v, 0) == max_v
@test soft_ϕ(max_v, 0.5) 0.5 * (min_v + max_v)

min_v, max_v = 20e0, 20e0
soft_ϕ = LinearSoftening(min_v, max_v, lo, hi)

@test soft_ϕ(max_v, 1) == 20e0
@test soft_ϕ(max_v, 0) == 20e0
@test soft_ϕ(max_v, 0.5) == 20e0

# test Drucker-Prager with softening
min_v, max_v = 15e0, 30e0
lo, hi = 0.0, 1.0
soft_ϕ = LinearSoftening(min_v, max_v, lo, hi)

τII = 20e6
P = 1e6
args = (P=P, τII=τII)
Expand Down Expand Up @@ -51,5 +62,4 @@ using GeoParams, Test
@test compute_yieldfunction(p2, args) 1.95e7
@test compute_yieldfunction(p3, args) 1.974118095489748e7

end

end

0 comments on commit 7bca8f7

Please sign in to comment.