Let us say I have a bunch of functions that I want to modify based on the stream type. Different density methods for liquid streams and gas streams etc. I create an abstract type “Substance” and define the stream as follows:
mutable struct ProcessStream{T, S<:AbstractSubstance} <: AbstractConnector{T}
temperature :: T
pressure :: T
components :: Vector{T}
end
here, T can be many different kinds of numbers (including Dual numbers because I’m trying to optimize this with ForwardDiff). The thing is, if I want to connect these four streams to a heat exchanger, I now have many parameters
mutable struct HeatExchanger{T,S1,S2,S3,S4} <: AbstractHeatExchanger{T}
LMTD::T
heating_inlet::ProcessStream{T,S1}
heating_outlet::ProcessStream{T,S2}
cooling_inlet::ProcessStream{T,S3}
cooling_outlet::ProcessStream{T,S4}
specs::HeatExchangerSpecs
end
This is a bit unwieldy when I’m trying to build models. It’s even worse when the heat exchanger is part of a distillation unit. In fact, I could see this as an example of a combinatorial explosion of types; every parametric type will need all the parameters of all the parametric types below, recursively. A clever workaround would be to make the type a field
mutable struct ProcessStream{T} <: AbstractConnector{T}
substance :: DataType
temperature :: T
pressure :: T
components :: Vector{T}
end
I could then dispatch functions based on substance like
get_density(s::ProcessStream) = get_density(s.substance, s)
function get_density(::Type{T}, s::ProcessStream) where T <: IdealGas
....
end
which results in a much simpler HeatExchanger object
mutable struct HeatExchanger{T} <: AbstractHeatExchanger{T}
LMTD::T
heating_inlet::ProcessStream{T}
heating_outlet::ProcessStream{T}
cooling_inlet::ProcessStream{T}
cooling_outlet::ProcessStream{T}
specs::HeatExchangerSpecs
end
Thus the number of parameters stays at a constant value of 1 instead of exploding exponentially. I have a feeling that the first method is correct, but the second method is so much more convenient. How much of a performance hit do you think I’d get if I need this inside an optimization?