diff --git a/src/flow_coupler_types.jl b/src/flow_coupler_types.jl index aeb66f1..2816f47 100644 --- a/src/flow_coupler_types.jl +++ b/src/flow_coupler_types.jl @@ -44,17 +44,21 @@ struct FlashedMixture2Phase{T, A<:AbstractVector{T}, E} critical_distance::Float64 flash_cond::@NamedTuple{p::Float64, T::Float64, z::E} flash_stability::StabilityReport - function FlashedMixture2Phase(state::PhaseState2Phase, K::K_t, V::V_t, liquid, vapor; - vec_type = Vector{V_t}, + function FlashedMixture2Phase(state::PhaseState2Phase, K::K_t, V::V_t, + liquid::FlashedPhase{V_t, A}, vapor::FlashedPhase{V_t, A}; + vec_type = A, critical_distance = NaN, cond = missing, stability_report = StabilityReport() - ) where {V_t, K_t} + ) where {V_t, K_t, A} + # The phase vector type is part of the result type. Infer it from the + # phases instead of using the keyword value as a type parameter. + vec_type === A || throw(ArgumentError("vec_type must match the phase vectors")) if ismissing(cond) z0 = convert(K_t, fill(NaN, length(K))) cond = (p = NaN, T = NaN, z = z0) end - new{V_t, vec_type, K_t}(state, K, V, liquid, vapor, critical_distance, cond, stability_report) + new{V_t, A, K_t}(state, K, V, liquid, vapor, critical_distance, cond, stability_report) end end diff --git a/test/runtests.jl b/test/runtests.jl index 0c5c80e..b7c4334 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -235,6 +235,33 @@ using ForwardDiff @test dZ == 1.0 end +@testset "Static flashed mixture allocation" begin + function allocated_static_flash(V) + x = SVector(0.8*one(V), 0.2*one(V)) + y = SVector(0.1*one(V), 0.9*one(V)) + K = SVector(0.125, 4.5) + cond = (p = 1.0, T = 273.15, z = SVector(0.5, 0.5)) + allocated_bytes = @allocated flashed = FlashedMixture2Phase( + MultiComponentFlash.two_phase_lv, K, V, x, y, + one(V), one(V), NaN, cond) + return allocated_bytes, flashed + end + + # The flash is stored once per cell, so even a small per-result allocation + # produces substantial GC traffic on reservoir grids. + observed_bytes = Ref{Int}(0) + for _ in 1:2 + ForwardDiff.derivative(0.4) do V + observed_bytes[], flashed = allocated_static_flash(V) + @test flashed.V == V + return flashed.V + end + end + if VERSION >= v"1.12" + @test observed_bytes[] == 0 + end +end + @testset "Rachford-Rice derivatives" begin N = 25 for z_light in range(0.0, 1.0, length = N)