diff --git a/src/flux.jl b/src/flux.jl index 7bda06af..6e3beb94 100644 --- a/src/flux.jl +++ b/src/flux.jl @@ -67,6 +67,10 @@ end return @inbounds X[tpfa.right] - X[tpfa.left] end +@inline function gradient(X::AbstractMatrix, i, tpfa::TPFA) + return @inbounds X[i, tpfa.right] - X[i, tpfa.left] +end + pressure_gradient(state, disc) = gradient(state.Pressure, disc) @inline function upwind(upw::SPU, F, q) diff --git a/src/forces/bc.jl b/src/forces/bc.jl index 7e3935d7..1b4bef46 100644 --- a/src/forces/bc.jl +++ b/src/forces/bc.jl @@ -128,3 +128,90 @@ function apply_flow_bc!(acc, q, bc, model::SimulationModel{<:Any, T}, state, tim end end end + +function Jutul.vectorization_length(bc::FlowBoundaryCondition, variant) + if variant == :all + n = 4 # pressure, temperature, T_flow, T_thermal + f = bc.fractional_flow + if !isnothing(f) + n += length(f) + end + if !isnothing(bc.density) + n += 1 + end + return n + elseif variant == :control + return 1 + else + error("Variant $variant not supported") + end +end + +function Jutul.vectorize_force!(v, bc::FlowBoundaryCondition, variant) + names = [] + if variant == :all + v[1] = bc.pressure + push!(names, :pressure) + v[2] = bc.temperature + push!(names, :temperature) + v[3] = bc.trans_flow + push!(names, :trans_flow) + v[4] = bc.trans_thermal + push!(names, :trans_thermal) + offset = 4 + f = bc.fractional_flow + if !isnothing(f) + for (i, f_i) in enumerate(f) + offset += 1 + v[offset] = f_i + push!(names, Symbol("fractional_flow$i")) + end + end + if !isnothing(bc.density) + offset += 1 + v[offset] = bc.density + push!(names, :density) + end + elseif variant == :control + v[1] = bc.pressure + push!(names, :pressure) + else + error("Variant $variant not supported") + end + return (names = names, ) +end + +function Jutul.devectorize_force(bc::FlowBoundaryCondition, X, meta, variant) + p = X[1] + T = bc.temperature + trans_flow = bc.trans_flow + trans_thermal = bc.trans_thermal + f = bc.fractional_flow + ρ = bc.density + if variant == :all + T = X[2] + trans_flow = X[3] + trans_thermal = X[4] + + offset = 4 + if !isnothing(f) + f = X[(offset+1):(offset+length(f))] + end + if !isnothing(ρ) + ρ = X[end] + end + elseif variant == :control + # DO nothing + else + error("Variant $variant not supported") + end + return FlowBoundaryCondition( + bc.cell, + p, + T, + trans_flow, + trans_thermal, + f, + ρ + ) +end diff --git a/src/forces/sources.jl b/src/forces/sources.jl index 721c84d1..69e88bdd 100644 --- a/src/forces/sources.jl +++ b/src/forces/sources.jl @@ -39,3 +39,53 @@ function Jutul.subforce(s::AbstractVector{S}, model) where S<:SourceTerm return s[keep] end +function Jutul.vectorization_length(src::SourceTerm, variant) + n = 1 + if variant == :all + f = src.fractional_flow + if !isnothing(f) + n += length(f) + end + elseif variant == :control + # Do nothing + else + error("Variant $variant not supported") + end + return n +end + +function Jutul.vectorize_force!(v, src::SourceTerm, variant) + v[1] = src.value + names = [:value] + if variant == :all + offset = 1 + f = src.fractional_flow + if !isnothing(f) + for (i, f_i) in enumerate(f) + offset += 1 + v[offset] = f_i + push!(names, Symbol("fractional_flow$i")) + end + end + elseif variant == :control + # Do nothing + else + error("Variant $variant not supported") + end + return (names = names, ) +end + +function Jutul.devectorize_force(src::SourceTerm, X, meta, variant) + val = X[1] + f = src.fractional_flow + if variant == :all + if !isnothing(f) + f = tuple(X[2:end]...) + end + elseif variant == :control + # Do nothing + else + error("Variant $variant not supported") + end + return SourceTerm(src.cell, val, f, src.type) +end