diff --git a/core/src/utils.jl b/core/src/utils.jl index 155de2b70..65cb8372c 100644 --- a/core/src/utils.jl +++ b/core/src/utils.jl @@ -906,13 +906,13 @@ end Function that goes smoothly from 0 to 1 in the interval [0,threshold], and is constant outside this interval. """ -function reduction_factor(x::Real, threshold::Real)::Real +function reduction_factor(x::T, threshold::Real)::T where {T <: Real} return if x < 0 - 0.0 + zero(T) elseif x < threshold x_scaled = x / threshold (-2 * x_scaled + 3) * x_scaled^2 else - 1.0 + one(T) end end diff --git a/core/test/utils.jl b/core/test/utils.jl index a0ae96bd7..0544cbe99 100644 --- a/core/test/utils.jl +++ b/core/test/utils.jl @@ -206,3 +206,13 @@ end @test isempty(fv) @test length(fv) == 0 end + +@testset "reduction_factor" begin + @test Ribasim.reduction_factor(-2.0, 2.0) === 0.0 + @test Ribasim.reduction_factor(0.0f0, 2.0) === 0.0f0 + @test Ribasim.reduction_factor(0.0, 2.0) === 0.0 + @test Ribasim.reduction_factor(1.0f0, 2.0) === 0.5f0 + @test Ribasim.reduction_factor(1.0, 2.0) === 0.5 + @test Ribasim.reduction_factor(3.0f0, 2.0) === 1.0f0 + @test Ribasim.reduction_factor(3.0, 2.0) === 1.0 +end