diff --git a/src/Domain.jl b/src/Domain.jl index fcf0eee1..82d2e05e 100644 --- a/src/Domain.jl +++ b/src/Domain.jl @@ -10,13 +10,13 @@ using ReverseDiff abstract type AbstractDomain end export AbstractDomain -abstract type AbstractConstantKDomain <: AbstractDomain end +abstract type AbstractConstantKDomain <: AbstractDomain end export AbstractConstantKDomain abstract type AbstractVariableKDomain <: AbstractDomain end export AbstractVariableKDomain -mutable struct ConstantTPDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I<:Integer, Q<:AbstractArray} <: AbstractConstantKDomain +mutable struct ConstantTPDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q<:AbstractArray} <: AbstractConstantKDomain phase::N indexes::Q #assumed to be in ascending order parameterindexes::Q @@ -39,21 +39,21 @@ mutable struct ConstantTPDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I thermovariabledict::Dict{String,Int64} end -function ConstantTPDomain(;phase::E2,initialconds::Dict{X,X2},constantspecies::Array{X3,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {E<:Real,E2<:AbstractPhase,Q<:AbstractInterface,W<:Real,X,X2,X3} +function ConstantTPDomain(; phase::E2, initialconds::Dict{X,X2}, constantspecies::Array{X3,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {E<:Real,E2<:AbstractPhase,Q<:AbstractInterface,W<:Real,X,X2,X3} #set conditions and initialconditions T = 0.0 P = 0.0 - y0 = zeros(length(phase.species)+1) + y0 = zeros(length(phase.species) + 1) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" P = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" y0[ind] = val end end @@ -65,40 +65,40 @@ function ConstantTPDomain(;phase::E2,initialconds::Dict{X,X2},constantspecies::A N = sum(ns) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] - Gs = calcgibbs(phase,T) + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + Gs = calcgibbs(phase, T) if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent mu = phase.solvent.mu(T) else mu = 0.0 end if phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(phase.species,:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(phase.species, :diffusion)] else diffs = Array{typeof(T),1}() end - C = P/(R*T) - V = N*R*T/P + C = P / (R * T) + V = N * R * T / P y0[end] = V - kfs,krevs = getkfkrevs(phase,T,P,C,N,ns,Gs,diffs,V,0.0) + kfs, krevs = getkfkrevs(phase, T, P, C, N, ns, Gs, diffs, V, 0.0) kfsp = deepcopy(kfs) for ind in efficiencyinds kfsp[ind] = 1.0 end - p = vcat(deepcopy(Gs),kfsp) + p = vcat(deepcopy(Gs), kfsp) if sparse - jacobian=spzeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = spzeros(typeof(T), length(phase.species), length(phase.species)) else - jacobian=zeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) end rxnarray = getreactionindices(phase) - return ConstantTPDomain(phase,[1,length(phase.species),length(phase.species)+1],[1,length(phase.species)+length(phase.reactions)],constspcinds, - T,P,kfs,krevs,efficiencyinds,Gs,rxnarray,mu,diffs,jacobian,sensitivity,false,MVector(false),MVector(0.0),p, Dict("V"=>length(phase.species)+1)), y0, p + return ConstantTPDomain(phase, [1, length(phase.species), length(phase.species) + 1], [1, length(phase.species) + length(phase.reactions)], constspcinds, + T, P, kfs, krevs, efficiencyinds, Gs, rxnarray, mu, diffs, jacobian, sensitivity, false, MVector(false), MVector(0.0), p, Dict("V" => length(phase.species) + 1)), y0, p end export ConstantTPDomain @@ -117,8 +117,8 @@ struct ConstantVDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ConstantVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {E,X,X2,Z<:IdealGas,Q<:AbstractInterface} +function ConstantVDomain(; phase::Z, initialconds::Dict{X,E}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {E,X,X2,Z<:IdealGas,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 @@ -126,7 +126,7 @@ function ConstantVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arra V = 0.0 ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -134,39 +134,39 @@ function ConstantVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arra elseif key == "V" V = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end @assert V != 0.0 || (T != 0.0 && P != 0.0) N = sum(ns) if V == 0.0 - V = N*R*T/P + V = N * R * T / P elseif T == 0.0 - T = P*V/(R*N) + T = P * V / (R * N) elseif P == 0.0 - P = N*R*T/V + P = N * R * T / V else throw(error("ConstantVDomain overspecified with T,P and V")) end - y0 = vcat(ns,T,P) - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + y0 = vcat(ns, T, P) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) else - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) end rxnarray = getreactionindices(phase) - return ConstantVDomain(phase,[1,length(phase.species),length(phase.species)+1,length(phase.species)+2],[1,length(phase.species)+length(phase.reactions)],constspcinds, - V,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict("T"=>length(phase.species)+1,"P"=>length(phase.species)+2)), y0, p + return ConstantVDomain(phase, [1, length(phase.species), length(phase.species) + 1, length(phase.species) + 2], [1, length(phase.species) + length(phase.reactions)], constspcinds, + V, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict("T" => length(phase.species) + 1, "P" => length(phase.species) + 2)), y0, p end export ConstantVDomain @@ -185,8 +185,8 @@ struct ConstantPDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ConstantPDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {E,X,X2,Z<:IdealGas,Q<:AbstractInterface} +function ConstantPDomain(; phase::Z, initialconds::Dict{X,E}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {E,X,X2,Z<:IdealGas,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 @@ -194,7 +194,7 @@ function ConstantPDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arra V = 0.0 ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -202,39 +202,39 @@ function ConstantPDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arra elseif key == "V" V = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end @assert P != 0.0 || (T != 0.0 && V != 0.0) N = sum(ns) if P == 0.0 - P = N*R*T/V + P = N * R * T / V elseif T == 0.0 - T = P*V/(R*N) + T = P * V / (R * N) elseif V == 0.0 - V = N*R*T/P + V = N * R * T / P else throw(error("ConstantPDomain overspecified with T,P and V")) end - y0 = vcat(ns,T,V) - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + y0 = vcat(ns, T, V) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) else - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) end rxnarray = getreactionindices(phase) - return ConstantPDomain(phase,[1,length(phase.species),length(phase.species)+1,length(phase.species)+2],[1,length(phase.species)+length(phase.reactions)],constspcinds, - P,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict("T"=>length(phase.species)+1,"V"=>length(phase.species)+2)), y0, p + return ConstantPDomain(phase, [1, length(phase.species), length(phase.species) + 1, length(phase.species) + 2], [1, length(phase.species) + length(phase.reactions)], constspcinds, + P, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict("T" => length(phase.species) + 1, "V" => length(phase.species) + 2)), y0, p end export ConstantPDomain @@ -254,8 +254,8 @@ struct ParametrizedTPDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Inte p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ParametrizedTPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {X,X2,Z<:IdealGas,Q<:AbstractInterface} +function ParametrizedTPDomain(; phase::Z, initialconds::Dict{X,Any}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {X,X2,Z<:IdealGas,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 @@ -264,7 +264,7 @@ function ParametrizedTPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecie ts = 0.0 ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -274,48 +274,48 @@ function ParametrizedTPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecie elseif key == "ts" ts = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind) <: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end @assert V != 0.0 || (T != 0.0 && P != 0.0) - if isa(T,AbstractArray) - Tfcn = getspline(ts,T) - elseif isa(T,Function) + if isa(T, AbstractArray) + Tfcn = getspline(ts, T) + elseif isa(T, Function) Tfcn = T else throw(error("ParametrizedTPDomain must take \"T\" as a function or if an array of times for \"ts\" is supplied as an array of volumes")) end - if isa(P,AbstractArray) - Pfcn = getspline(ts,P) - elseif isa(P,Function) + if isa(P, AbstractArray) + Pfcn = getspline(ts, P) + elseif isa(P, Function) Pfcn = P else throw(error("ParametrizedTPDomain must take \"P\" as a function or if an array of times for \"ts\" is supplied as an array of volumes")) end N = sum(ns) - V = N*R*Tfcn(0.0)/Pfcn(0.0) - y0 = zeros(length(phase.species)+1) + V = N * R * Tfcn(0.0) / Pfcn(0.0) + y0 = zeros(length(phase.species) + 1) y0[1:length(phase.species)] = ns y0[length(phase.species)+1] = V - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(V),length(phase.species)+1,length(phase.species)+1) + jacobian = zeros(typeof(V), length(phase.species) + 1, length(phase.species) + 1) else - jacobian=zeros(typeof(V),length(phase.species)+1,length(phase.species)+1) + jacobian = zeros(typeof(V), length(phase.species) + 1, length(phase.species) + 1) end rxnarray = getreactionindices(phase) - return ParametrizedTPDomain(phase,[1,length(phase.species),length(phase.species)+1],[1,length(phase.species)+length(phase.reactions)],constspcinds, - Tfcn,Pfcn,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict("V"=>length(phase.species)+1)), y0, p + return ParametrizedTPDomain(phase, [1, length(phase.species), length(phase.species) + 1], [1, length(phase.species) + length(phase.reactions)], constspcinds, + Tfcn, Pfcn, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict("V" => length(phase.species) + 1)), y0, p end export ParametrizedTPDomain @@ -334,8 +334,8 @@ struct ParametrizedVDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integ p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ParametrizedVDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {X,X2,E<:Real,Z<:IdealGas,Q<:AbstractInterface} +function ParametrizedVDomain(; phase::Z, initialconds::Dict{X,Any}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {X,X2,E<:Real,Z<:IdealGas,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 @@ -345,7 +345,7 @@ function ParametrizedVDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] @assert "V" in keys(initialconds) - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -355,44 +355,44 @@ function ParametrizedVDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies elseif key == "ts" ts = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end - @assert isa(V,Function) || isa(V,AbstractArray) - if isa(V,AbstractArray) - Vfcn = getspline(ts,V) - elseif isa(V,Function) + @assert isa(V, Function) || isa(V, AbstractArray) + if isa(V, AbstractArray) + Vfcn = getspline(ts, V) + elseif isa(V, Function) Vfcn = V else throw(error("ParametrizedVDomain must take \"V\" as a function or if an array of times for \"ts\" is supplied as an array of volumes")) end N = sum(ns) if T == 0.0 - T = P*Vfcn(0.0)/(R*N) + T = P * Vfcn(0.0) / (R * N) elseif P == 0.0 - P = N*R*T/Vfcn(0.0) + P = N * R * T / Vfcn(0.0) else - ns *= (P*Vfcn(0.0)/(R*T))/sum(ns) #automatically scale down moles if pressure specified + ns *= (P * Vfcn(0.0) / (R * T)) / sum(ns) #automatically scale down moles if pressure specified end - y0 = vcat(ns,T,P) - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + y0 = vcat(ns, T, P) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) else - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) end rxnarray = getreactionindices(phase) - return ParametrizedVDomain(phase,[1,length(phase.species),length(phase.species)+1,length(phase.species)+2],[1,length(phase.species)+length(phase.reactions)],constspcinds, - Vfcn,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict("T"=>length(phase.species)+1,"P"=>length(phase.species)+2)), y0, p + return ParametrizedVDomain(phase, [1, length(phase.species), length(phase.species) + 1, length(phase.species) + 2], [1, length(phase.species) + length(phase.reactions)], constspcinds, + Vfcn, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict("T" => length(phase.species) + 1, "P" => length(phase.species) + 2)), y0, p end export ParametrizedVDomain @@ -411,8 +411,8 @@ struct ParametrizedPDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integ p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ParametrizedPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {X,X2,E<:Real,Z<:IdealGas,Q<:AbstractInterface} +function ParametrizedPDomain(; phase::Z, initialconds::Dict{X,Any}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {X,X2,E<:Real,Z<:IdealGas,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 @@ -422,7 +422,7 @@ function ParametrizedPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] @assert "P" in keys(initialconds) - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -432,48 +432,48 @@ function ParametrizedPDomain(;phase::Z,initialconds::Dict{X,Any},constantspecies elseif key == "ts" ts = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end - @assert isa(P,Function) || isa(P,AbstractArray) - if isa(P,AbstractArray) - Pfcn = getspline(ts,P) - elseif isa(P,Function) + @assert isa(P, Function) || isa(P, AbstractArray) + if isa(P, AbstractArray) + Pfcn = getspline(ts, P) + elseif isa(P, Function) Pfcn = P else throw(error("ParametrizedPDomain must take \"P\" as a function or if an array of times for \"ts\" is supplied as an array of volumes")) end N = sum(ns) if T == 0.0 - T = Pfcn(0.0)*V/(R*N) + T = Pfcn(0.0) * V / (R * N) elseif V == 0.0 - V = N*R*T/Pfcn(0.0) + V = N * R * T / Pfcn(0.0) else - ns *= (Pfcn(0.0)*V/(R*T))/sum(ns) #automatically scale down moles if volume specified + ns *= (Pfcn(0.0) * V / (R * T)) / sum(ns) #automatically scale down moles if volume specified end - y0 = vcat(ns,T,V) - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + y0 = vcat(ns, T, V) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) else - jacobian=zeros(typeof(T),length(phase.species)+2,length(phase.species)+2) + jacobian = zeros(typeof(T), length(phase.species) + 2, length(phase.species) + 2) end rxnarray = getreactionindices(phase) - return ParametrizedPDomain(phase,[1,length(phase.species),length(phase.species)+1,length(phase.species)+2],[1,length(phase.species)+length(phase.reactions)],constspcinds, - Pfcn,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict("T"=>length(phase.species)+1,"V"=>length(phase.species)+2)), y0, p + return ParametrizedPDomain(phase, [1, length(phase.species), length(phase.species) + 1, length(phase.species) + 2], [1, length(phase.species) + length(phase.reactions)], constspcinds, + Pfcn, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict("T" => length(phase.species) + 1, "V" => length(phase.species) + 2)), y0, p end export ParametrizedPDomain -mutable struct ConstantTVDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I<:Integer, Q<:AbstractArray} <: AbstractConstantKDomain +mutable struct ConstantTVDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q<:AbstractArray} <: AbstractConstantKDomain phase::N indexes::Q #assumed to be in ascending order parameterindexes::Q @@ -497,8 +497,8 @@ mutable struct ConstantTVDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ConstantTVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Array{X2,1}=Array{String,1}(), - sparse=false,sensitivity=false) where {E,X,X2, Z<:AbstractPhase,Q<:AbstractInterface,W<:Real} +function ConstantTVDomain(; phase::Z, initialconds::Dict{X,E}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse=false, sensitivity=false) where {E,X,X2,Z<:AbstractPhase,Q<:AbstractInterface,W<:Real} #set conditions and initialconditions T = 0.0 V = 0.0 @@ -506,7 +506,7 @@ function ConstantTVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arr phi = 0.0 y0 = zeros(length(phase.species)) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -516,8 +516,8 @@ function ConstantTVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arr elseif key == "Phi" phi = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" y0[ind] = val end end @@ -527,36 +527,36 @@ function ConstantTVDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Arr N = sum(ns) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] - Gs = calcgibbs(phase,T) + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + Gs = calcgibbs(phase, T) if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent mu = phase.solvent.mu(T) else mu = 0.0 end if phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(phase.species,:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(phase.species, :diffusion)] else diffs = Array{Float64,1}() end P = 1.0e8 #essentiallly assuming this is a liquid - C = N/V - kfs,krevs = getkfkrevs(phase,T,P,C,N,ns,Gs,diffs,V,phi) - kfsnondiff = getkfs(phase,T,P,C,ns,V,phi) - p = vcat(Gs,kfsnondiff) + C = N / V + kfs, krevs = getkfkrevs(phase, T, P, C, N, ns, Gs, diffs, V, phi) + kfsnondiff = getkfs(phase, T, P, C, ns, V, phi) + p = vcat(Gs, kfsnondiff) if sparse - jacobian=zeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) else - jacobian=zeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) end rxnarray = getreactionindices(phase) - return ConstantTVDomain(phase,[1,length(phase.species)],[1,length(phase.species)+length(phase.reactions)],constspcinds, - T,V,phi,kfs,krevs,kfsnondiff,efficiencyinds,Gs,rxnarray,mu,diffs,jacobian,sensitivity,false,MVector(false),MVector(0.0),p,Dict{String,Int64}()), y0, p + return ConstantTVDomain(phase, [1, length(phase.species)], [1, length(phase.species) + length(phase.reactions)], constspcinds, + T, V, phi, kfs, krevs, kfsnondiff, efficiencyinds, Gs, rxnarray, mu, diffs, jacobian, sensitivity, false, MVector(false), MVector(0.0), p, Dict{String,Int64}()), y0, p end export ConstantTVDomain @@ -577,8 +577,8 @@ struct ParametrizedTConstantVDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ParametrizedTConstantVDomain(;phase::IdealDiluteSolution,initialconds::Dict{X,X3},constantspecies::Array{X2,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {X,X2,X3,Q<:AbstractInterface} +function ParametrizedTConstantVDomain(; phase::IdealDiluteSolution, initialconds::Dict{X,X3}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {X,X2,X3,Q<:AbstractInterface} #set conditions and initialconditions T = 0.0 P = 1.0e8 #essentiallly assuming this is a liquid @@ -588,7 +588,7 @@ function ParametrizedTConstantVDomain(;phase::IdealDiluteSolution,initialconds:: ns = zeros(length(phase.species)) spnames = [x.name for x in phase.species] @assert "V" in keys(initialconds) - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "P" @@ -600,14 +600,14 @@ function ParametrizedTConstantVDomain(;phase::IdealDiluteSolution,initialconds:: elseif key == "Phi" phi = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" ns[ind] = val end end - if isa(T,AbstractArray) - Tfcn = getspline(ts,T) - elseif isa(T,Function) + if isa(T, AbstractArray) + Tfcn = getspline(ts, T) + elseif isa(T, Function) Tfcn = T else throw(error("ParametrizedTConstantVDomain must take \"T\" as a function or if an array of times for \"ts\" is supplied as an array of volumes")) @@ -615,26 +615,26 @@ function ParametrizedTConstantVDomain(;phase::IdealDiluteSolution,initialconds:: N = sum(ns) y0 = zeros(length(phase.species)) y0[1:length(phase.species)] = ns - p = vcat(zeros(length(phase.species)),ones(length(phase.reactions))) + p = vcat(zeros(length(phase.species)), ones(length(phase.reactions))) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] if sparse - jacobian=zeros(typeof(V),length(phase.species)+1,length(phase.species)+1) + jacobian = zeros(typeof(V), length(phase.species) + 1, length(phase.species) + 1) else - jacobian=zeros(typeof(V),length(phase.species)+1,length(phase.species)+1) + jacobian = zeros(typeof(V), length(phase.species) + 1, length(phase.species) + 1) end rxnarray = getreactionindices(phase) - return ParametrizedTConstantVDomain(phase,[1,length(phase.species)],[1,length(phase.species)+length(phase.reactions)],constspcinds, - Tfcn,V,phi,efficiencyinds,rxnarray,jacobian,sensitivity,MVector(false),MVector(0.0),p,Dict{String,Int64}()), y0, p + return ParametrizedTConstantVDomain(phase, [1, length(phase.species)], [1, length(phase.species) + length(phase.reactions)], constspcinds, + Tfcn, V, phi, efficiencyinds, rxnarray, jacobian, sensitivity, MVector(false), MVector(0.0), p, Dict{String,Int64}()), y0, p end export ParametrizedTConstantVDomain -mutable struct ConstantTAPhiDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real, I<:Integer, Q<:AbstractArray} <: AbstractConstantKDomain +mutable struct ConstantTAPhiDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q<:AbstractArray} <: AbstractConstantKDomain phase::N indexes::Q #assumed to be in ascending order parameterindexes::Q @@ -657,15 +657,15 @@ mutable struct ConstantTAPhiDomain{N<:AbstractPhase,S<:Integer,W<:Real, W2<:Real p::Array{W,1} thermovariabledict::Dict{String,Int64} end -function ConstantTAPhiDomain(;phase::E2,initialconds::Dict{X,X2},constantspecies::Array{X3,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false,stationary::Bool=false) where {E<:Real,E2<:AbstractPhase,W<:Real,X,X2,X3} +function ConstantTAPhiDomain(; phase::E2, initialconds::Dict{X,X2}, constantspecies::Array{X3,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false, stationary::Bool=false) where {E<:Real,E2<:AbstractPhase,W<:Real,X,X2,X3} #set conditions and initialconditions T = 0.0 A = 0.0 phi = 0.0 #default 0.0 y0 = zeros(length(phase.species)) spnames = [x.name for x in phase.species] - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "A" @@ -673,8 +673,8 @@ function ConstantTAPhiDomain(;phase::E2,initialconds::Dict{X,X2},constantspecies elseif key == "Phi" phi = val else - ind = findfirst(isequal(key),spnames) - @assert typeof(ind)<: Integer "$key not found in species list: $spnames" + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" y0[ind] = val end end @@ -685,29 +685,29 @@ function ConstantTAPhiDomain(;phase::E2,initialconds::Dict{X,X2},constantspecies N = sum(ns) if length(constantspecies) > 0 - spcnames = getfield.(phase.species,:name) - constspcinds = [findfirst(isequal(k),spcnames) for k in constantspecies] + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] - Gs = calcgibbs(phase,T) + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + Gs = calcgibbs(phase, T) if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent mu = phase.solvent.mu(T) else mu = 0.0 end C = 0.0 #this currently shouldn't matter here, on a surface you shouldn't have pdep - kfs,krevs = getkfkrevs(phase,T,0.0,C,N,ns,Gs,[],A,phi) - p = vcat(Gs,kfs) + kfs, krevs = getkfkrevs(phase, T, 0.0, C, N, ns, Gs, [], A, phi) + p = vcat(Gs, kfs) if sparse - jacobian=spzeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = spzeros(typeof(T), length(phase.species), length(phase.species)) else - jacobian=zeros(typeof(T),length(phase.species),length(phase.species)) + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) end rxnarray = getreactionindices(phase) - return ConstantTAPhiDomain(phase,[1,length(phase.species)],[1,length(phase.species)+length(phase.reactions)],constspcinds, - T,A,phi,kfs,krevs,efficiencyinds,Gs,rxnarray,mu,Array{Float64,1}(),jacobian,sensitivity,false,MVector(false),MVector(0.0),p,Dict{String,Int64}()), y0, p + return ConstantTAPhiDomain(phase, [1, length(phase.species)], [1, length(phase.species) + length(phase.reactions)], constspcinds, + T, A, phi, kfs, krevs, efficiencyinds, Gs, rxnarray, mu, Array{Float64,1}(), jacobian, sensitivity, false, MVector(false), MVector(0.0), p, Dict{String,Int64}()), y0, p end export ConstantTAPhiDomain @@ -719,7 +719,7 @@ the liquid phase reactants encapsulated in the film. mass: the mass of the solid part in the swollen film rho: the density of the solid part in the swollen film """ -mutable struct FragmentBasedConstantTrhoDomain{N<:AbstractPhase, S<:Integer, W<:Real, W2<:Real, I<:Integer, Q<:AbstractArray} <: AbstractConstantKDomain +mutable struct FragmentBasedConstantTrhoDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q<:AbstractArray} <: AbstractConstantKDomain phase::N indexes::Q #assumed to be in ascending order parameterindexes::Q @@ -744,8 +744,8 @@ mutable struct FragmentBasedConstantTrhoDomain{N<:AbstractPhase, S<:Integer, W<: thermovariabledict::Dict{String,Int64} end -function FragmentBasedConstantTrhoDomain(;phase::Z,initialconds::Dict{X,E},constantspecies::Array{X4,1}=Array{String,1}(), - sparse::Bool=false,sensitivity::Bool=false) where {X,E,X1,E1,X3,X4,Z<:AbstractPhase} +function FragmentBasedConstantTrhoDomain(; phase::Z, initialconds::Dict{X,E}, constantspecies::Array{X4,1}=Array{String,1}(), + sparse::Bool=false, sensitivity::Bool=false) where {X,E,X1,E1,X3,X4,Z<:AbstractPhase} T = 0.0 rho = 0.0 @@ -755,9 +755,9 @@ function FragmentBasedConstantTrhoDomain(;phase::Z,initialconds::Dict{X,E},const fragmentnames = getfield.(getphasespecies(phase), :name) - y0 = zeros(length(fragmentnames)+1) #track mass + y0 = zeros(length(fragmentnames) + 1) #track mass - for (key,val) in initialconds + for (key, val) in initialconds if key == "T" T = val elseif key == "A" @@ -768,8 +768,8 @@ function FragmentBasedConstantTrhoDomain(;phase::Z,initialconds::Dict{X,E},const mass = val y0[end] = val else - ind = findfirst(isequal(key),fragmentnames) - @assert typeof(ind)<: Integer "$key not found in fragment list: $fragmentnames" + ind = findfirst(isequal(key), fragmentnames) + @assert typeof(ind) <: Integer "$key not found in fragment list: $fragmentnames" y0[ind] = val end end @@ -780,1276 +780,1446 @@ function FragmentBasedConstantTrhoDomain(;phase::Z,initialconds::Dict{X,E},const ns = y0[1:end-1] N = sum(ns) - V = mass/rho + V = mass / rho if length(constantspecies) > 0 for spc in constantspecies @assert spc in fragmentnames "$spc is not in the fragment list: $fragmentnames" end - constspcinds = [findfirst(isequal(k),fragmentnames) for k in constantspecies] + constspcinds = [findfirst(isequal(k), fragmentnames) for k in constantspecies] else constspcinds = Array{Int64,1}() end - efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics)<:AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] - Gs = calcgibbs(phase,T) + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + Gs = calcgibbs(phase, T) if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent mu = phase.solvent.mu(T) else mu = 0.0 end if phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(getphasespecies(phase),:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(getphasespecies(phase), :diffusion)] else diffs = Array{Float64,1}() end - C = N/V - kfs,krevs = getkfkrevs(phase,T,P,C,N,ns,Gs,diffs,V,0.0) - kfsnondiff = getkfs(phase,T,P,C,ns,V,0.0) + C = N / V + kfs, krevs = getkfkrevs(phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + kfsnondiff = getkfs(phase, T, P, C, ns, V, 0.0) - p = vcat(Gs,kfsnondiff) + p = vcat(Gs, kfsnondiff) if sparse - jacobian=zeros(typeof(T),length(getphasespecies(phase)),length(getphasespecies(phase))) + jacobian = zeros(typeof(T), length(getphasespecies(phase)), length(getphasespecies(phase))) else - jacobian=zeros(typeof(T),length(getphasespecies(phase)),length(getphasespecies(phase))) + jacobian = zeros(typeof(T), length(getphasespecies(phase)), length(getphasespecies(phase))) end rxnarray = getreactionindices(phase) - return FragmentBasedConstantTrhoDomain(phase,[1,length(fragmentnames),length(fragmentnames)+1],[1,length(phase.species)+length(phase.reactions)],constspcinds, - T,rho,A,kfs,krevs,kfsnondiff,efficiencyinds,Gs,rxnarray,mu,diffs,jacobian,sensitivity,false,MVector(false),MVector(0.0),p,Dict("mass"=>length(fragmentnames)+1)), y0, p + return FragmentBasedConstantTrhoDomain(phase, [1, length(fragmentnames), length(fragmentnames) + 1], [1, length(phase.species) + length(phase.reactions)], constspcinds, + T, rho, A, kfs, krevs, kfsnondiff, efficiencyinds, Gs, rxnarray, mu, diffs, jacobian, sensitivity, false, MVector(false), MVector(0.0), p, Dict("mass" => length(fragmentnames) + 1)), y0, p end export FragmentBasedConstantTrhoDomain -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W3=SciMLBase.NullParameters()) where {W3<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:Array{Float64,1},Q} #no parameter input +mutable struct ConstantTLiqFilmDomain{N<:AbstractPhase,S<:Integer,W<:Real,W2<:Real,I<:Integer,Q<:AbstractArray} <: AbstractConstantKDomain + phase::N + indexes::Q #assumed to be in ascending order + parameterindexes::Q + constantspeciesinds::Array{S,1} + T::W + epsilon::W + phi::W + kfs::Array{W,1} + krevs::Array{W,1} + kfsnondiff::Array{W,1} + efficiencyinds::Array{I,1} + Gs::Array{W,1} + rxnarray::Array{Int64,2} + mu::W + diffusivity::Array{W,1} + jacobian::Array{W,2} + sensitivity::Bool + alternativepformat::Bool + jacuptodate::MArray{Tuple{1},Bool,1,1} + t::MArray{Tuple{1},W2,1,1} + p::Array{W,1} + thermovariabledict::Dict{String,Int64} +end + +function ConstantTLiqFilmDomain(; phase::Z, initialconds::Dict{X,E}, constantspecies::Array{X2,1}=Array{String,1}(), + sparse=false, sensitivity=false) where {E,X,X2,Z<:AbstractPhase,Q<:AbstractInterface,W<:Real} + #set conditions and initialconditions + T = 0.0 + V = 0.0 + P = 1.0e8 + phi = 0.0 + epsilon = 0.0 + y0 = zeros(length(phase.species) + 1) + spnames = [x.name for x in phase.species] + for (key, val) in initialconds + if key == "T" + T = val + elseif key == "P" + throw(error("ConstantTLiqFilmDomain cannot specify P")) + elseif key == "V" + V = val + y0[end] = val + elseif key == "epsilon" + epsilon = val + else + ind = findfirst(isequal(key), spnames) + @assert typeof(ind) <: Integer "$key not found in species list: $spnames" + y0[ind] = val + end + end + @assert T != 0.0 + @assert V != 0.0 + @assert epsilon != 0.0 + + ns = y0[1:end-1] + N = sum(ns) + + if length(constantspecies) > 0 + spcnames = getfield.(phase.species, :name) + constspcinds = [findfirst(isequal(k), spcnames) for k in constantspecies] + else + constspcinds = Array{Int64,1}() + end + efficiencyinds = [rxn.index for rxn in phase.reactions if typeof(rxn.kinetics) <: AbstractFalloffRate && length(rxn.kinetics.efficiencies) > 0] + Gs = calcgibbs(phase, T) + if :solvent in fieldnames(typeof(phase)) && typeof(phase.solvent) != EmptySolvent + mu = phase.solvent.mu(T) + else + mu = 0.0 + end + if phase.diffusionlimited + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(phase.species, :diffusion)] + else + diffs = Array{Float64,1}() + end + P = 1.0e8 #essentiallly assuming this is a liquid + C = N / V + kfs, krevs = getkfkrevs(phase, T, P, C, N, ns, Gs, diffs, V, phi) + kfsnondiff = getkfs(phase, T, P, C, ns, V, phi) + p = vcat(Gs, kfsnondiff) + if sparse + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) + else + jacobian = zeros(typeof(T), length(phase.species), length(phase.species)) + end + rxnarray = getreactionindices(phase) + return ConstantTLiqFilmDomain(phase, [1, length(phase.species), length(phase.species) + 1], [1, length(phase.species) + length(phase.reactions)], constspcinds, + T, epsilon, phi, kfs, krevs, kfsnondiff, efficiencyinds, Gs, rxnarray, mu, diffs, jacobian, sensitivity, false, MVector(false), MVector(0.0), p, Dict{String,Int64}(["V" => length(phase.species) + 1])), y0, p +end + +export ConstantTLiqFilmDomain + +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W3=SciMLBase.NullParameters()) where {W3<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:Array{Float64,1},Q} #no parameter input ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V for ind in d.efficiencyinds #efficiency related rates may have changed - d.kfs[ind],d.krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,d.Gs,d.diffusivity,V,0.0) + d.kfs[ind], d.krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, d.Gs, d.diffusivity, V, 0.0) end - return ns,cs,d.T,d.P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:Array{Float64,1},Q<:Float64} #uses parameter input +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:Array{Float64,1},Q<:Float64} #uses parameter input ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V if !d.alternativepformat @views kfps = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - @views nothermochg= d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nothermochg = d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] else - kfps = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - nothermochg= d.Gs == d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfps = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + nothermochg = d.Gs == d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] end @views nokfchg = count(d.kfs .!= kfps) <= length(d.efficiencyinds) && all(kfps[d.efficiencyinds] .== 1.0) if nothermochg && nokfchg for ind in d.efficiencyinds #efficiency related rates may have changed - d.kfs[ind],d.krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,d.Gs,d.diffusivity,V,0.0;f=kfps[ind]) + d.kfs[ind], d.krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; f=kfps[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 elseif nothermochg d.kfs = kfps for ind in d.efficiencyinds #efficiency related rates may have changed - d.kfs[ind],d.krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,d.Gs,d.diffusivity,V,0.0;f=kfps[ind]) + d.kfs[ind], d.krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; f=kfps[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 else #need to handle thermo changes d.kfs .= kfps if !d.alternativepformat d.Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] else - d.Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] end - krevs = getkfkrevs(d.phase,d.T,d.P,C,N,ns,d.Gs,d.diffusivity,V,0.0;kfs=d.kfs)[2] + krevs = getkfkrevs(d.phase, d.T, d.P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; kfs=d.kfs)[2] for ind in d.efficiencyinds #efficiency related rates may have changed - d.kfs[ind],d.krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,d.Gs,d.diffusivity,V,0.0;f=kfps[ind]) + d.kfs[ind], d.krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; f=kfps[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::Array{W3,1},t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,W3<:ForwardDiff.Dual,Q} #Autodiff y +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::Array{W3,1}, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,W3<:ForwardDiff.Dual,Q} #Autodiff y ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V if !d.alternativepformat - kfs = convert(typeof(y),p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + kfs = convert(typeof(y), p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] else - kfs = convert(typeof(y),d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = convert(typeof(y), d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] end - krevs = convert(typeof(y),getkfkrevs(d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfs)[2]) + krevs = convert(typeof(y), getkfkrevs(d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfs)[2]) for ind in d.efficiencyinds #efficiency related rates may have changed - kfs[ind],krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;f=kfs[ind]) + kfs[ind], krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; f=kfs[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J,Q} #Autodiff p +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J,Q} #Autodiff p ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V if !d.alternativepformat kfs = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] else - kfs = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] end - krevs = getkfkrevs(d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfs)[2] + krevs = getkfkrevs(d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfs)[2] for ind in d.efficiencyinds #efficiency related rates may have changed - kfs[ind],krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;f=kfs[ind]) + kfs[ind], krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; f=kfs[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},Q} #Autodiff p +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},Q} #Autodiff p ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V - kfs = similar(y,length(d.phase.reactions)) - krevs = similar(y,length(d.phase.reactions)) + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V + kfs = similar(y, length(d.phase.reactions)) + krevs = similar(y, length(d.phase.reactions)) if !d.alternativepformat kfs .= p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] else - kfs .= d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs .= d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] end - krevs .= getkfkrevs(d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfs)[2] + krevs .= getkfkrevs(d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfs)[2] for ind in d.efficiencyinds #efficiency related rates may have changed - kfs[ind],krevs[ind] = getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;f=kfs[ind]) + kfs[ind], krevs[ind] = getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; f=kfs[ind]) end - return ns,cs,d.T,d.P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, d.P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},W<:IdealGas,Y<:Integer,J,Q} #Tracker/reversediff +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},W<:IdealGas,Y<:Integer,J,Q} #Tracker/reversediff ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0)[1]*p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] + kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0)[1] * p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0)[1]*d.p[length(d.phase.species)+ind]*p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0)[1] * d.p[length(d.phase.species)+ind] * p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] end - krevs = getkfkrevs(d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfs)[2] - return ns,cs,d.T,d.P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + krevs = getkfkrevs(d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfs)[2] + return ns, cs, d.T, d.P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},W<:IdealGas,Y<:Integer,J<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},Q} #Tracker/reversediff +@inline function calcthermo(d::ConstantTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},W<:IdealGas,Y<:Integer,J<:Union{ReverseDiff.TrackedArray,Tracker.TrackedArray},Q} #Tracker/reversediff ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = d.P*V/(R*d.T) - cs = ns./V - C = N/V + N = d.P * V / (R * d.T) + cs = ns ./ V + C = N / V if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0)[1]*p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] + kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0)[1] * p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind],d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0)[1]*d.p[length(d.phase.species)+ind]*p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = [ind in d.efficiencyinds ? getkfkrev(d.phase.reactions[ind], d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0)[1] * d.p[length(d.phase.species)+ind] * p[d.parameterindexes[1]-1+length(d.phase.species)+ind] : p[d.parameterindexes[1]-1+length(d.phase.species)+ind] for ind in 1:length(d.phase.reactions)] end - krevs = getkfkrevs(d.phase,d.T,d.P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfs)[2] - return ns,cs,d.T,d.P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + krevs = getkfkrevs(d.phase, d.T, d.P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfs)[2] + return ns, cs, d.T, d.P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*d.V/(R*T) - cs = ns./d.V - C = N/d.V + N = P * d.V / (R * T) + cs = ns ./ d.V + C = N / d.V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) Cvave = 0.0 - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,d.V,0.0) - return ns,cs,T,P,d.V,C,N,0.0,kfs,krevs,Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, d.V, 0.0) + return ns, cs, T, P, d.V, C, N, 0.0, kfs, krevs, Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*d.V/(R*T) - cs = ns./d.V - C = N/d.V + N = P * d.V / (R * T) + cs = ns ./ d.V + C = N / d.V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) Cvave = 0.0 - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @views @fastmath hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @views @fastmath hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,d.V,0.0) - return @views @fastmath ns,cs,T,P,d.V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, d.V, 0.0) + return @views @fastmath ns, cs, T, P, d.V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*d.V/(R*T) - cs = ns./d.V - C = N/d.V + N = P * d.V / (R * T) + cs = ns ./ d.V + C = N / d.V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) Cvave = 0.0 - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,d.V,0.0) - return @views @fastmath ns,cs,T,P,d.V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, d.V, 0.0) + return @views @fastmath ns, cs, T, P, d.V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ConstantPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = d.P*V/(R*T) - cs = ns./V - C = N/V + N = d.P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,d.P,C,N,ns,Gs,diffs,V,0.0) - return ns,cs,T,d.P,V,C,N,0.0,kfs,krevs,Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, d.P, C, N, ns, Gs, diffs, V, 0.0) + return ns, cs, T, d.P, V, C, N, 0.0, kfs, krevs, Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ConstantPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = d.P*V/(R*T) - cs = ns./V - C = N/V + N = d.P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,d.P,C,N,ns,Gs,diffs,V,0.0) + kfs, krevs = getkfkrevs(d.phase, T, d.P, C, N, ns, Gs, diffs, V, 0.0) if p != SciMLBase.NullParameters() - return @views @fastmath ns,cs,T,d.P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + return @views @fastmath ns, cs, T, d.P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 else - return ns,cs,T,d.P,V,C,N,0.0,kfs,krevs,Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + return ns, cs, T, d.P, V, C, N, 0.0, kfs, krevs, Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end end -@inline function calcthermo(d::ConstantPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = d.P*V/(R*T) - cs = ns./V - C = N/V + N = d.P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,d.P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,d.P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, d.P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, d.P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} V = d.V(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return ns,cs,T,P,V,C,N,0.0,kfs,krevs,Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return ns, cs, T, P, V, C, N, 0.0, kfs, krevs, Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} V = d.V(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} V = d.V(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] P = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Us = (hdivRT.-1.0)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Us = (hdivRT .- 1.0) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Us,Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Us, Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return ns,cs,T,P,V,C,N,0.0,kfs,krevs,Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return ns, cs, T, P, V, C, N, 0.0, kfs, krevs, Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] T = y[d.indexes[3]] V = y[d.indexes[4]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Hs = zeros(length(d.phase.species)) - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Hs = hdivRT.*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Hs = hdivRT .* (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Hs,Array{Float64,1}(),Gs,diffs,Cvave,cpdivR,0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Hs, Array{Float64,1}(), Gs, diffs, Cvave, cpdivR, 0.0 end -@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} V = d.V T = d.T(t) ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./V - C = N/V + cs = ns ./ V + C = N / V P = 1.0e8 #liquid phase Gs = zeros(length(d.phase.species)) mu = d.phase.solvent.mu(T) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) if d.phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,d.phi) - return ns,cs,T,P,V,C,N,mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, d.phi) + return ns, cs, T, P, V, C, N, mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} V = d.V T = d.T(t) ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./V - C = N/V + cs = ns ./ V + C = N / V P = 1.0e8 #liquid phase Gs = zeros(length(d.phase.species)) mu = d.phase.solvent.mu(T) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) if d.phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,d.phi) - return @views @fastmath ns,cs,T,P,V,C,N,mu,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),d.phi + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, d.phi) + return @views @fastmath ns, cs, T, P, V, C, N, mu, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTConstantVDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} V = d.V T = d.T(t) ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./V - C = N/V + cs = ns ./ V + C = N / V P = 1.0e8 #liquid phase Gs = zeros(length(d.phase.species)) mu = d.phase.solvent.mu(T) - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) if d.phase.diffusionlimited - diffs = [x(T=T,mu=mu,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=mu, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,d.phi) - return @views @fastmath ns,cs,T,P,V,C,N,mu,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),d.phi + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, d.phi) + return @views @fastmath ns, cs, T, P, V, C, N, mu, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ParametrizedTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:SciMLBase.NullParameters,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} T = d.T(t) @assert T < 10000.0 P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return ns,cs,T,P,V,C,N,0.0,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return ns, cs, T, P, V, C, N, 0.0, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ParametrizedTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2<:Array{Float64,1},W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} T = d.T(t) @assert T < 10000.0 P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT .+= p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ParametrizedTPDomain{W,Y},y::J,t::Q,p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ParametrizedTPDomain{W,Y}, y::J, t::Q, p::W2=SciMLBase.NullParameters()) where {W2,W<:IdealGas,Y<:Integer,J<:AbstractArray,Q} T = d.T(t) @assert T < 10000.0 P = d.P(t) ns = y[d.indexes[1]:d.indexes[2]] V = y[d.indexes[3]] - N = P*V/(R*T) - cs = ns./V - C = N/V + N = P * V / (R * T) + cs = ns ./ V + C = N / V Gs = zeros(length(d.phase.species)) Us = zeros(length(d.phase.species)) - cpdivR,hdivRT1,sdivR = calcHSCpdless(d.phase.vecthermo,T) - @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)]./(R*T) - @fastmath Gs = (hdivRT.-sdivR)*(R*T) - @fastmath Cvave = dot(cpdivR,ns) - @fastmath Cvave *= R/N + cpdivR, hdivRT1, sdivR = calcHSCpdless(d.phase.vecthermo, T) + @fastmath @views hdivRT = hdivRT1 .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] ./ (R * T) + @fastmath Gs = (hdivRT .- sdivR) * (R * T) + @fastmath Cvave = dot(cpdivR, ns) + @fastmath Cvave *= R / N @fastmath Cvave -= R if d.phase.diffusionlimited - diffs = [x(T=T,mu=0.0,P=P) for x in getfield.(d.phase.species,:diffusion)] + diffs = [x(T=T, mu=0.0, P=P) for x in getfield.(d.phase.species, :diffusion)] else diffs = Array{Float64,1}() end - kfs,krevs = getkfkrevs(d.phase,T,P,C,N,ns,Gs,diffs,V,0.0) - return @views @fastmath ns,cs,T,P,V,C,N,0.0,kfs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],krevs.*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)],Array{Float64,1}(),Array{Float64,1}(),Gs,diffs,0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, T, P, C, N, ns, Gs, diffs, V, 0.0) + return @views @fastmath ns, cs, T, P, V, C, N, 0.0, kfs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], krevs .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(kfs)], Array{Float64,1}(), Array{Float64,1}(), Gs, diffs, 0.0, Array{Float64,1}(), 0.0 end -@inline function calcthermo(d::ConstantTVDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantTVDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.V - C = N/d.V + cs = ns ./ d.V + C = N / d.V P = 1.0e8 - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ConstantTVDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealDiluteSolution,Y<:Integer,J<:Array{Float64,1},Q} +@inline function calcthermo(d::ConstantTVDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealDiluteSolution,Y<:Integer,J<:Array{Float64,1},Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.V - C = N/d.V + cs = ns ./ d.V + C = N / d.V P = 1.0e8 if !d.alternativepformat @views nothermochg = d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] @views nokfchg = d.kfsnondiff == p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] if nothermochg && nokfchg - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi elseif nothermochg d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi else d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] d.Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end else - @views nothermochg = d.Gs == d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - @views nokfchg = d.kfsnondiff == d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + @views nothermochg = d.Gs == d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nokfchg = d.kfsnondiff == d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] if nothermochg && nokfchg - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi elseif nothermochg - d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi else - d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.Gs .= d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.Gs .= d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end end end -@inline function calcthermo(d::ConstantTVDomain{W,Y},y::Array{W2,1},t::Q,p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff y +@inline function calcthermo(d::ConstantTVDomain{W,Y}, y::Array{W2,1}, t::Q, p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff y ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.V - C = N/d.V + cs = ns ./ d.V + C = N / d.V P = 1.0e8 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = convert(typeof(y),p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + kfsnondiff = convert(typeof(y), p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = convert(typeof(y),d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = convert(typeof(y), d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) end - kfs,krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,d.V,d.phi;kfs=kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, d.V, d.phi; kfs=kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ConstantTVDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff p +@inline function calcthermo(d::ConstantTVDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff p ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.V - C = N/d.V + cs = ns ./ d.V + C = N / d.V P = 1.0e8 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] end - kfs,krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,d.V,d.phi;kfs=kfsnondiff) - return ns,cs,d.T,P,d.V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, d.V, d.phi; kfs=kfsnondiff) + return ns, cs, d.T, P, d.V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ConstantTAPhiDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} +@inline function calcthermo(d::ConstantTAPhiDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.A - C = N/d.A + cs = ns ./ d.A + C = N / d.A P = 0.0 - return ns,cs,d.T,P,d.A,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),d.Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.A, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), d.Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ConstantTAPhiDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealSurface,Y<:Integer,J<:Array{Float64,1},Q} +@inline function calcthermo(d::ConstantTAPhiDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealSurface,Y<:Integer,J<:Array{Float64,1},Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.A - C = N/d.A + cs = ns ./ d.A + C = N / d.A P = 0.0 if !d.alternativepformat @views nothermochg = d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] if nothermochg - return ns,cs,d.T,P,d.A,C,N,d.mu,p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)],d.krevs,Array{Float64,1}(),Array{Float64,1}(),d.Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.A, C, N, d.mu, p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)], d.krevs, Array{Float64,1}(), Array{Float64,1}(), d.Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi else d.kfs = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] d.Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfs)[2] - return ns,cs,d.T,P,d.A,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),d.Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfs)[2] + return ns, cs, d.T, P, d.A, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), d.Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end else - @views nothermochg = d.Gs == d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nothermochg = d.Gs == d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] if nothermochg - return ns,cs,d.T,P,d.A,C,N,d.mu,d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)],d.krevs,Array{Float64,1}(),Array{Float64,1}(),d.Gs,0.0,Array{Float64,1}(),d.phi + return ns, cs, d.T, P, d.A, C, N, d.mu, d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)], d.krevs, Array{Float64,1}(), Array{Float64,1}(), d.Gs, 0.0, Array{Float64,1}(), d.phi else - d.kfs = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,d.V,d.phi;kfs=d.kfs)[2] - return ns,cs,d.T,P,d.A,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),d.Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + d.kfs = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, d.V, d.phi; kfs=d.kfs)[2] + return ns, cs, d.T, P, d.A, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), d.Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end end end -@inline function calcthermo(d::ConstantTAPhiDomain{W,Y},y::Array{W2,1},t::Q,p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} #autodiff y +@inline function calcthermo(d::ConstantTAPhiDomain{W,Y}, y::Array{W2,1}, t::Q, p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} #autodiff y ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.A - C = N/d.A + cs = ns ./ d.A + C = N / d.A P = 0.0 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = convert(typeof(y),p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + kfs = convert(typeof(y), p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = convert(typeof(y),d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = convert(typeof(y), d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) end - krevs = convert(typeof(y),getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,d.A,d.phi;kfs=kfs)[2]) - return ns,cs,d.T,P,d.A,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + krevs = convert(typeof(y), getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, d.A, d.phi; kfs=kfs)[2]) + return ns, cs, d.T, P, d.A, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -@inline function calcthermo(d::ConstantTAPhiDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} #autodiff p +@inline function calcthermo(d::ConstantTAPhiDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2,W<:IdealSurface,Y<:Integer,J<:AbstractArray,Q} #autodiff p ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) - cs = ns./d.A - C = N/d.A + cs = ns ./ d.A + C = N / d.A P = 0.0 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] kfs = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfs = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfs = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] end - krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,d.A,d.phi;kfs=kfs)[2] - return ns,cs,d.T,P,d.A,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Gs,Array{Float64,1}(),0.0,Array{Float64,1}(),d.phi + krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, d.A, d.phi; kfs=kfs)[2] + return ns, cs, d.T, P, d.A, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Gs, Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end -function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} +function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:SciMLBase.NullParameters,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) mass = y[d.indexes[3]] - V = mass/d.rho - cs = ns./V - C = N/V + V = mass / d.rho + cs = ns ./ V + C = N / V P = 1.0e8 - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:FragmentBasedIdealFilm,Y<:Integer,J<:Array{Float64,1},Q} +function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:FragmentBasedIdealFilm,Y<:Integer,J<:Array{Float64,1},Q} ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) mass = y[d.indexes[3]] - V = mass/d.rho - cs = ns./V - C = N/V + V = mass / d.rho + cs = ns ./ V + C = N / V P = 1.0e8 if !d.alternativepformat @views nothermochg = d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] @views nokfchg = d.kfsnondiff == p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] if nothermochg && nokfchg - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 elseif nothermochg d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,V,0.0;kfs=d.kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 else d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] d.Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,V,0.0;kfs=d.kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end else - @views nothermochg = d.Gs == d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - @views nokfchg = d.kfsnondiff == d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + @views nothermochg = d.Gs == d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nokfchg = d.kfsnondiff == d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] if nothermochg && nokfchg - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 elseif nothermochg - d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,V,0.0;kfs=d.kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 else - d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] - d.Gs .= d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - d.kfs,d.krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,d.Gs,d.diffusivity,V,0.0;kfs=d.kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,d.kfs,d.krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.Gs .= d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, 0.0; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end end end -function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y},y::Array{W2,1},t::Q,p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} #autodiff y +function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y}, y::Array{W2,1}, t::Q, p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} #autodiff y ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) mass = y[d.indexes[3]] - V = mass/d.rho - cs = ns./V - C = N/V + V = mass / d.rho + cs = ns ./ V + C = N / V P = 1.0e8 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = convert(typeof(y),p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + kfsnondiff = convert(typeof(y), p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = convert(typeof(y),d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = convert(typeof(y), d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) end - kfs,krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 end -function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y},y::J,t::Q,p::Q2=SciMLBase.NullParameters()) where {Q2,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} #autodiff p +function calcthermo(d::FragmentBasedConstantTrhoDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2,W<:FragmentBasedIdealFilm,Y<:Integer,J<:AbstractArray,Q} #autodiff p ns = y[d.indexes[1]:d.indexes[2]] N = sum(ns) mass = y[d.indexes[3]] - V = mass/d.rho - cs = ns./V - C = N/V + V = mass / d.rho + cs = ns ./ V + C = N / V + P = 1.0e8 + if !d.alternativepformat + Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + else + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + end + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, V, 0.0; kfs=kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), 0.0 +end + +@inline function calcthermo(d::ConstantTLiqFilmDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2<:Array{Float64,1},W<:IdealDiluteSolution,Y<:Integer,J<:Array{Float64,1},Q} + ns = y[d.indexes[1]:d.indexes[2]] + V = y[d.indexes[3]] + N = sum(ns) + cs = ns ./ V + C = N / V + P = 1.0e8 + if !d.alternativepformat + @views nothermochg = d.Gs == p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nokfchg = d.kfsnondiff == p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + if nothermochg && nokfchg + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + elseif nothermochg + d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + else + d.kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + end + else + @views nothermochg = d.Gs == d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + @views nokfchg = d.kfsnondiff == d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + if nothermochg && nokfchg + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + elseif nothermochg + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + else + d.kfsnondiff .= d.p[length(d.phase.species)+1:length(d.phase.species)+length(d.phase.reactions)] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + d.Gs .= d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + d.kfs, d.krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, d.Gs, d.diffusivity, V, d.phi; kfs=d.kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, d.kfs, d.krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi + end + end + +end + +@inline function calcthermo(d::ConstantTLiqFilmDomain{W,Y}, y::Array{W2,1}, t::Q, p::Q2=SciMLBase.NullParameters()) where {W2<:ForwardDiff.Dual,Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff y + ns = y[d.indexes[1]:d.indexes[2]] + V = y[d.indexes[3]] + N = sum(ns) + cs = ns ./ V + C = N / V + P = 1.0e8 + if !d.alternativepformat + Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = convert(typeof(y), p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + else + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = convert(typeof(y), d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)]) + end + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, V, d.phi; kfs=kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi +end + +@inline function calcthermo(d::ConstantTLiqFilmDomain{W,Y}, y::J, t::Q, p::Q2=SciMLBase.NullParameters()) where {Q2,W<:IdealDiluteSolution,Y<:Integer,J<:AbstractArray,Q} #autodiff p + ns = y[d.indexes[1]:d.indexes[2]] + V = y[d.indexes[3]] + N = sum(ns) + cs = ns ./ V + C = N / V P = 1.0e8 if !d.alternativepformat Gs = p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] kfsnondiff = p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] else - Gs = d.p[1:length(d.phase.species)].+p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] - kfsnondiff = d.p[length(d.phase.species)+1:end].*p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] + Gs = d.p[1:length(d.phase.species)] .+ p[d.parameterindexes[1]-1+1:d.parameterindexes[1]-1+length(d.phase.species)] + kfsnondiff = d.p[length(d.phase.species)+1:end] .* p[d.parameterindexes[1]-1+length(d.phase.species)+1:d.parameterindexes[1]-1+length(d.phase.species)+length(d.phase.reactions)] end - kfs,krevs = getkfkrevs(d.phase,d.T,P,C,N,ns,Gs,d.diffusivity,V,0.0;kfs=kfsnondiff) - return ns,cs,d.T,P,V,C,N,d.mu,kfs,krevs,Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),Array{Float64,1}(),0.0,Array{Float64,1}(),0.0 + kfs, krevs = getkfkrevs(d.phase, d.T, P, C, N, ns, Gs, d.diffusivity, V, d.phi; kfs=kfsnondiff) + return ns, cs, d.T, P, V, C, N, d.mu, kfs, krevs, Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), Array{Float64,1}(), 0.0, Array{Float64,1}(), d.phi end export calcthermo -@inline function calcdomainderivatives!(d::Q,dydt::Z7,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Array{Z,1},Hs::Array{Z11,1},V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z8) where {Q<:AbstractDomain,Z12,Z11,Z10,Z9,Z8<:Real,Z7,W<:IdealGas,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} +@inline function calcdomainderivatives!(d::Q, dydt::Z7, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Array{Z,1}, Hs::Array{Z11,1}, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z8) where {Q<:AbstractDomain,Z12,Z11,Z10,Z9,Z8<:Real,Z7,W<:IdealGas,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*inter.F(t) - elseif isa(inter,Outlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.F(t).*ns./N - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,T) - kHs = map.(inter.kHs,T) - evap = kLAs.*ns - cond = kLAs.*inter.molefractions.*inter.P./kHs*V + if isa(inter, Inlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* inter.F(t) + elseif isa(inter, Outlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.F(t) .* ns ./ N + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, T) + kHs = map.(inter.kHs, T) + evap = kLAs .* ns + cond = kLAs .* inter.molefractions .* inter.P ./ kHs * V dydt[d.indexes[1]:d.indexes[2]] .-= (evap .- cond) - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V end end end -@inline function calcdomainderivatives!(d::Q,dydt::Z7,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Array{Z,1},Hs::Array{Z11,1},V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z8) where {Q<:ConstantTPDomain,Z12,Z11,Z10,Z9,Z8<:Real,Z7,W<:IdealGas,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} - @views @fastmath @inbounds dydt[d.indexes[3]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/P +@inline function calcdomainderivatives!(d::Q, dydt::Z7, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Array{Z,1}, Hs::Array{Z11,1}, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z8) where {Q<:ConstantTPDomain,Z12,Z11,Z10,Z9,Z8<:Real,Z7,W<:IdealGas,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} + @views @fastmath @inbounds dydt[d.indexes[3]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / P for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*inter.F(t) - dydt[d.indexes[3]] += inter.F(t)*R*T/P - elseif isa(inter,Outlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.F(t).*ns./N - dydt[d.indexes[3]] -= inter.F(t)*R*T/P - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + if isa(inter, Inlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* inter.F(t) + dydt[d.indexes[3]] += inter.F(t) * R * T / P + elseif isa(inter, Outlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.F(t) .* ns ./ N + dydt[d.indexes[3]] -= inter.F(t) * R * T / P + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs net_evap = evap .- cond dydt[d.indexes[1]:d.indexes[2]] .+= net_evap - dydt[d.indexes[3]] += sum(net_evap)*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs + dydt[d.indexes[3]] += sum(net_evap) * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs dydt[d.indexes[3]] += inter.Vin(t) - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V dydt[d.indexes[3]] -= inter.Vout(t) end end for inter in interfaces - if isa(inter,VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else + if isa(inter, VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else @inbounds dVdt = dydt[d.indexes[3]] - @inbounds flow = P*dVdt/(R*T) - @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns/N + @inbounds flow = P * dVdt / (R * T) + @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns / N @inbounds dydt[d.indexes[3]] -= dVdt end end end -@inline function calcdomainderivatives!(d::ConstantVDomain{W,Y},dydt::K,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Z,Hs::Z11,V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z7) where {Z12,Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} - @views @fastmath @inbounds dydt[d.indexes[3]] = -dot(Us,dydt[d.indexes[1]:d.indexes[2]])/(N*Cvave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/V + P/T*dydt[d.indexes[3]] +@inline function calcdomainderivatives!(d::ConstantVDomain{W,Y}, dydt::K, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Z, Hs::Z11, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z7) where {Z12,Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} + @views @fastmath @inbounds dydt[d.indexes[3]] = -dot(Us, dydt[d.indexes[1]:d.indexes[2]]) / (N * Cvave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / V + P / T * dydt[d.indexes[3]] for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain + if isa(inter, Inlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*flow - dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* flow + dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/V + P/T*dTdt - elseif isa(inter,Outlet) && d == inter.domain + dydt[d.indexes[4]] += flow * R * T / V + P / T * dTdt + elseif isa(inter, Outlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .-= flow.*ns./N - dTdt = (P*V/N*flow)/(N*Cvave) + dydt[d.indexes[1]:d.indexes[2]] .-= flow .* ns ./ N + dTdt = (P * V / N * flow) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= flow*R*T/V + P/T*dTdt - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + dydt[d.indexes[4]] -= flow * R * T / V + P / T * dTdt + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs dydt[d.indexes[1]:d.indexes[2]] .+= (evap .- cond) flow = sum(evap) - dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/V + P/T*dTdt + dydt[d.indexes[4]] += flow * R * T / V + P / T * dTdt flow = sum(cond) - dTdt = (P*V/N*flow)/(N*Cvave) + dTdt = (P * V / N * flow) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= flow*R*T/V + P/T*dTdt - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs - dTdt = inter.Vin(t)*(inter.Hpervolume - dot(Us,ns)/V)/(N*Cvave) + dydt[d.indexes[4]] -= flow * R * T / V + P / T * dTdt + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs + dTdt = inter.Vin(t) * (inter.Hpervolume - dot(Us, ns) / V) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += inter.Vin(t)*P/V + P/T*dTdt - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V - dTdt = (P*inter.Vout(t))/(N*Cvave) + dydt[d.indexes[4]] += inter.Vin(t) * P / V + P / T * dTdt + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V + dTdt = (P * inter.Vout(t)) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= inter.Vout(t)*P/V + P/T*dTdt + dydt[d.indexes[4]] -= inter.Vout(t) * P / V + P / T * dTdt end end end -@inline function calcdomainderivatives!(d::ConstantPDomain{W,Y},dydt::K,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Z,Hs::Z11,V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z7) where {Z12,Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} - @fastmath Cpave = Cvave+R - @views @fastmath @inbounds dydt[d.indexes[3]] = -dot(Hs,dydt[d.indexes[1]:d.indexes[2]])/(N*Cpave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/P + dydt[d.indexes[3]]*V/T +@inline function calcdomainderivatives!(d::ConstantPDomain{W,Y}, dydt::K, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Z, Hs::Z11, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z7) where {Z12,Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5} + @fastmath Cpave = Cvave + R + @views @fastmath @inbounds dydt[d.indexes[3]] = -dot(Hs, dydt[d.indexes[1]:d.indexes[2]]) / (N * Cpave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / P + dydt[d.indexes[3]] * V / T for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain + if isa(inter, Inlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*flow - dTdt = flow*(inter.H - dot(Hs,ns)/N)/(N*Cpave) + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* flow + dTdt = flow * (inter.H - dot(Hs, ns) / N) / (N * Cpave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/P + dTdt*V/T - elseif isa(inter,Outlet) && d == inter.domain + dydt[d.indexes[4]] += flow * R * T / P + dTdt * V / T + elseif isa(inter, Outlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .-= flow.*ns./N - dydt[d.indexes[4]] -= flow*R*T/P - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + dydt[d.indexes[1]:d.indexes[2]] .-= flow .* ns ./ N + dydt[d.indexes[4]] -= flow * R * T / P + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs dydt[d.indexes[1]:d.indexes[2]] .+= (evap .- cond) flow = sum(evap) - dTdt = flow*(inter.H - dot(Hs,ns)/N)/(N*Cpave) + dTdt = flow * (inter.H - dot(Hs, ns) / N) / (N * Cpave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/P + dTdt*V/T + dydt[d.indexes[4]] += flow * R * T / P + dTdt * V / T flow = sum(cond) - dydt[d.indexes[4]] -= flow*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs - dTdt = inter.Vin(t)*(inter.Hpervolume - dot(Hs,ns)/V)/(N*Cpave) + dydt[d.indexes[4]] -= flow * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs + dTdt = inter.Vin(t) * (inter.Hpervolume - dot(Hs, ns) / V) / (N * Cpave) dydt[d.indexes[3]] += dTdt dydt[d.indexes[4]] += inter.Vin(t) - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V dydt[d.indexes[4]] -= inter.Vout(t) end end for inter in interfaces - if isa(inter,VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else + if isa(inter, VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else @inbounds dVdt = dydt[d.indexes[4]] - @inbounds flow = P*dVdt/(R*T) - @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns/N + @inbounds flow = P * dVdt / (R * T) + @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns / N @inbounds dydt[d.indexes[4]] -= dVdt end end end -@inline function calcdomainderivatives!(d::ParametrizedTPDomain{W,Y},dydt::K,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Z,Hs::Z11,V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} - @views @fastmath @inbounds dydt[d.indexes[3]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/P + Calculus.derivative(d.T,t)*V/T - Calculus.derivative(d.P,t)*V/P +@inline function calcdomainderivatives!(d::ParametrizedTPDomain{W,Y}, dydt::K, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Z, Hs::Z11, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} + @views @fastmath @inbounds dydt[d.indexes[3]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / P + Calculus.derivative(d.T, t) * V / T - Calculus.derivative(d.P, t) * V / P for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain + if isa(inter, Inlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*flow - dydt[d.indexes[3]] += flow*R*T/P - elseif isa(inter,Outlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* flow + dydt[d.indexes[3]] += flow * R * T / P + elseif isa(inter, Outlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .-= flow*ns./N - dydt[d.indexes[3]] -= flow*R*T/P - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns ./ N + dydt[d.indexes[3]] -= flow * R * T / P + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs dydt[d.indexes[1]:d.indexes[2]] .+= (evap .- cond) flow = sum(evap) - dydt[d.indexes[3]] += flow*R*T/P + dydt[d.indexes[3]] += flow * R * T / P flow = sum(cond) - dydt[d.indexes[3]] -= flow*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs + dydt[d.indexes[3]] -= flow * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs dydt[d.indexes[3]] += inter.Vin(t) - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V dydt[d.indexes[3]] -= inter.Vout(t) end end for inter in interfaces - if isa(inter,VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else + if isa(inter, VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else @inbounds dVdt = dydt[d.indexes[3]] - @inbounds flow = P*dVdt/(R*T) - @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns/N + @inbounds flow = P * dVdt / (R * T) + @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns / N @inbounds dydt[d.indexes[3]] -= dVdt end end end -@inline function calcdomainderivatives!(d::ParametrizedVDomain{W,Y},dydt::K,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Z,Hs::Z11,V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} - dVdt = Calculus.derivative(d.V,t) - @views @fastmath @inbounds dydt[d.indexes[3]] = (-dot(Us,dydt[d.indexes[1]:d.indexes[2]])-P*dVdt)/(N*Cvave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/V + dydt[d.indexes[3]]*P/T - P/V*dVdt +@inline function calcdomainderivatives!(d::ParametrizedVDomain{W,Y}, dydt::K, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Z, Hs::Z11, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} + dVdt = Calculus.derivative(d.V, t) + @views @fastmath @inbounds dydt[d.indexes[3]] = (-dot(Us, dydt[d.indexes[1]:d.indexes[2]]) - P * dVdt) / (N * Cvave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / V + dydt[d.indexes[3]] * P / T - P / V * dVdt for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain + if isa(inter, Inlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*flow - dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* flow + dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/V + dTdt*P/T - elseif isa(inter,Outlet) && d == inter.domain + dydt[d.indexes[4]] += flow * R * T / V + dTdt * P / T + elseif isa(inter, Outlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .-= flow*ns./N - dTdt = (P*V/N*flow)/(N*Cvave) + dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns ./ N + dTdt = (P * V / N * flow) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= flow*R*T/V + dTdt*P/T - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + dydt[d.indexes[4]] -= flow * R * T / V + dTdt * P / T + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs dydt[d.indexes[1]:d.indexes[2]] .+= (evap .- cond) flow = sum(evap) - dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/V + dTdt*P/T + dydt[d.indexes[4]] += flow * R * T / V + dTdt * P / T flow = sum(cond) - dTdt = (P*V/N*flow)/(N*Cvave) + dTdt = (P * V / N * flow) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= flow*R*T/V + dTdt*P/T - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs - dTdt = inter.Vin(t)*(inter.Hpervolume - dot(Us,ns)/V)/(N*Cvave) + dydt[d.indexes[4]] -= flow * R * T / V + dTdt * P / T + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs + dTdt = inter.Vin(t) * (inter.Hpervolume - dot(Us, ns) / V) / (N * Cvave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += inter.Vin(t)*P/V + dTdt*P/T - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V - dTdt = (P*inter.Vout(t))/(N*Cvave) + dydt[d.indexes[4]] += inter.Vin(t) * P / V + dTdt * P / T + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V + dTdt = (P * inter.Vout(t)) / (N * Cvave) dydt[d.indexes[3]] -= dTdt - dydt[d.indexes[4]] -= inter.Vout(t)*P/V + dTdt*P/T + dydt[d.indexes[4]] -= inter.Vout(t) * P / V + dTdt * P / T end end end -@inline function calcdomainderivatives!(d::ParametrizedPDomain{W,Y},dydt::K,interfaces::Z12;t::Z10,T::Z4,P::Z9,Us::Z,Hs::Z11,V::Z2,C::Z3,ns::Z5,N::Z6,Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} - @fastmath Cpave = Cvave+R - dPdt = Calculus.derivative(d.P,t) - @views @fastmath @inbounds dydt[d.indexes[3]] = (-dot(Hs,dydt[d.indexes[1]:d.indexes[2]])+V*dPdt)/(N*Cpave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]])*R*T/P + dydt[d.indexes[3]]*V/T - dPdt*V/P +@inline function calcdomainderivatives!(d::ParametrizedPDomain{W,Y}, dydt::K, interfaces::Z12; t::Z10, T::Z4, P::Z9, Us::Z, Hs::Z11, V::Z2, C::Z3, ns::Z5, N::Z6, Cvave::Z7) where {Z11,Z10,Z9,W<:IdealGas,Z7,K,Y<:Integer,Z6,Z,Z2,Z3,Z4,Z5,Z12} + @fastmath Cpave = Cvave + R + dPdt = Calculus.derivative(d.P, t) + @views @fastmath @inbounds dydt[d.indexes[3]] = (-dot(Hs, dydt[d.indexes[1]:d.indexes[2]]) + V * dPdt) / (N * Cpave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds dydt[d.indexes[4]] = sum(dydt[d.indexes[1]:d.indexes[2]]) * R * T / P + dydt[d.indexes[3]] * V / T - dPdt * V / P for ind in d.constantspeciesinds #make dydt zero for constant species @inbounds dydt[ind] = 0.0 end for inter in interfaces - if isa(inter,Inlet) && d == inter.domain + if isa(inter, Inlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .+= inter.y.*flow - dTdt = flow*(inter.H - dot(Hs,ns)/N)/(N*Cpave) + dydt[d.indexes[1]:d.indexes[2]] .+= inter.y .* flow + dTdt = flow * (inter.H - dot(Hs, ns) / N) / (N * Cpave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/P + dTdt*V/T - elseif isa(inter,Outlet) && d == inter.domain + dydt[d.indexes[4]] += flow * R * T / P + dTdt * V / T + elseif isa(inter, Outlet) && d == inter.domain flow = inter.F(t) - dydt[d.indexes[1]:d.indexes[2]] .-= flow.*ns./N - dydt[d.indexes[4]] -= flow*R*T/P - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && d == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + dydt[d.indexes[1]:d.indexes[2]] .-= flow .* ns ./ N + dydt[d.indexes[4]] -= flow * R * T / P + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && d == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs dydt[d.indexes[1]:d.indexes[2]] .+= (evap .- cond) flow = sum(evap) - dTdt = flow*(inter.H - dot(Hs,ns)/N)/(N*Cpave) + dTdt = flow * (inter.H - dot(Hs, ns) / N) / (N * Cpave) dydt[d.indexes[3]] += dTdt - dydt[d.indexes[4]] += flow*R*T/P + dTdt*V/T + dydt[d.indexes[4]] += flow * R * T / P + dTdt * V / T flow = sum(cond) - dydt[d.indexes[1]:d.indexes[2]] .-= flow.*ns./N - dydt[d.indexes[4]] -= flow*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t)*inter.cs - dTdt = inter.Vin(t)*(inter.Hpervolume - dot(Hs,ns)/V)/(N*Cpave) + dydt[d.indexes[1]:d.indexes[2]] .-= flow .* ns ./ N + dydt[d.indexes[4]] -= flow * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .+= inter.Vin(t) * inter.cs + dTdt = inter.Vin(t) * (inter.Hpervolume - dot(Hs, ns) / V) / (N * Cpave) dydt[d.indexes[3]] += dTdt dydt[d.indexes[4]] += inter.Vin(t) - elseif isa(inter,VolumetricFlowRateOutlet) && d == inter.domain - dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t)*ns/V + elseif isa(inter, VolumetricFlowRateOutlet) && d == inter.domain + dydt[d.indexes[1]:d.indexes[2]] .-= inter.Vout(t) * ns / V dydt[d.indexes[4]] -= inter.Vout(t) end end for inter in interfaces - if isa(inter,VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else + if isa(inter, VolumeMaintainingOutlet) && d == inter.domain #VolumeMaintainingOutlet has to be evaluated after dVdt has been modified by everything else @inbounds dVdt = dydt[d.indexes[4]] - @inbounds flow = P*dVdt/(R*T) - @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns/N + @inbounds flow = P * dVdt / (R * T) + @views @inbounds dydt[d.indexes[1]:d.indexes[2]] .-= flow * ns / N @inbounds dydt[d.indexes[4]] -= dVdt end end end export calcdomainderivatives! -@inline function jacobianyefficiencyderiv!(jac::S,domain::Union{ConstantTPDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedPDomain},kinetics::AbstractFalloffRate,efficiencies::Dict{Int64,Float64},rxnarray::Array{Int64,2},rxnind::Int64,Vind::Int64,cs::Array{Float64,1},T::Float64,V::Float64,C::Float64,Ceff::Float64,Kc::Float64) where {S<:AbstractArray} - dkdCeff = _calcdkdCeff(kinetics,T,Ceff) - for (spcind,efficiencyval) in efficiencies - @fastmath dkdni = dkdCeff*(efficiencyval/V) - _jacobianykderiv!(jac,spcind,dkdni,dkdni/Kc,rxnarray,rxnind,cs,V) +@inline function jacobianyefficiencyderiv!(jac::S, domain::Union{ConstantTPDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedPDomain}, kinetics::AbstractFalloffRate, efficiencies::Dict{Int64,Float64}, rxnarray::Array{Int64,2}, rxnind::Int64, Vind::Int64, cs::Array{Float64,1}, T::Float64, V::Float64, C::Float64, Ceff::Float64, Kc::Float64) where {S<:AbstractArray} + dkdCeff = _calcdkdCeff(kinetics, T, Ceff) + for (spcind, efficiencyval) in efficiencies + @fastmath dkdni = dkdCeff * (efficiencyval / V) + _jacobianykderiv!(jac, spcind, dkdni, dkdni / Kc, rxnarray, rxnind, cs, V) end - @fastmath dkdV = dkdCeff*(C-Ceff)/V - _jacobianykderiv!(jac,Vind,dkdV,dkdV/Kc,rxnarray,rxnind,cs,V) + @fastmath dkdV = dkdCeff * (C - Ceff) / V + _jacobianykderiv!(jac, Vind, dkdV, dkdV / Kc, rxnarray, rxnind, cs, V) end -@inline function jacobianyefficiencyderiv!(jac::S,domain::Union{ConstantVDomain,ParametrizedVDomain,ConstantTVDomain,ParametrizedTConstantVDomain},kinetics::AbstractFalloffRate,efficiencies::Dict{Int64,Float64},rxnarray::AbstractArray,rxnind::Int64,Vind::Int64,cs::Array{Float64,1},T::Float64,V::Float64,C::Float64,Ceff::Float64,Kc::Float64) where {S<:AbstractArray} - dkdCeff = _calcdkdCeff(kinetics,T,Ceff) - for (spcind,efficiencyval) in efficiencies - @fastmath dkdni = dkdCeff*(efficiencyval/V) - _jacobianykderiv!(jac,spcind,dkdni,dkdni/Kc,rxnarray,rxnind,cs,V) +@inline function jacobianyefficiencyderiv!(jac::S, domain::Union{ConstantVDomain,ParametrizedVDomain,ConstantTVDomain,ParametrizedTConstantVDomain}, kinetics::AbstractFalloffRate, efficiencies::Dict{Int64,Float64}, rxnarray::AbstractArray, rxnind::Int64, Vind::Int64, cs::Array{Float64,1}, T::Float64, V::Float64, C::Float64, Ceff::Float64, Kc::Float64) where {S<:AbstractArray} + dkdCeff = _calcdkdCeff(kinetics, T, Ceff) + for (spcind, efficiencyval) in efficiencies + @fastmath dkdni = dkdCeff * (efficiencyval / V) + _jacobianykderiv!(jac, spcind, dkdni, dkdni / Kc, rxnarray, rxnind, cs, V) end end -@inline function jacobianynsderiv!(jac::S,domain::Union{ConstantTPDomain,ParametrizedTPDomain},rxnarray::Array{Int64,2},efficiencyinds::Array{I,1},cs::Array{Float64,1},kfs::Array{Float64,1},krevs::Array{Float64,1},T::Float64,V::Float64,C::Float64) where {S<:AbstractArray,I<:Integer} +@inline function jacobianynsderiv!(jac::S, domain::Union{ConstantTPDomain,ParametrizedTPDomain}, rxnarray::Array{Int64,2}, efficiencyinds::Array{I,1}, cs::Array{Float64,1}, kfs::Array{Float64,1}, krevs::Array{Float64,1}, T::Float64, V::Float64, C::Float64) where {S<:AbstractArray,I<:Integer} @simd for rxnind = 1:size(rxnarray)[2] - @inbounds _jacobianynswrtns!(jac,rxnarray,rxnind,cs,kfs[rxnind],krevs[rxnind]) - @inbounds _jacobianynswrtV!(jac,domain.indexes[3],rxnarray,rxnind,cs,kfs[rxnind],krevs[rxnind]) + @inbounds _jacobianynswrtns!(jac, rxnarray, rxnind, cs, kfs[rxnind], krevs[rxnind]) + @inbounds _jacobianynswrtV!(jac, domain.indexes[3], rxnarray, rxnind, cs, kfs[rxnind], krevs[rxnind]) if rxnind in efficiencyinds - @inbounds @fastmath Kc = kfs[rxnind]/krevs[rxnind] - @inbounds @fastmath Ceff = C + sum(cs[i]*val for (i,val) in domain.phase.reactions[rxnind].kinetics.efficiencies) - @inbounds jacobianyefficiencyderiv!(jac,domain,domain.phase.reactions[rxnind].kinetics,domain.phase.reactions[rxnind].kinetics.efficiencies,rxnarray,rxnind,domain.indexes[3],cs,T,V,C,Ceff,Kc) + @inbounds @fastmath Kc = kfs[rxnind] / krevs[rxnind] + @inbounds @fastmath Ceff = C + sum(cs[i] * val for (i, val) in domain.phase.reactions[rxnind].kinetics.efficiencies) + @inbounds jacobianyefficiencyderiv!(jac, domain, domain.phase.reactions[rxnind].kinetics, domain.phase.reactions[rxnind].kinetics.efficiencies, rxnarray, rxnind, domain.indexes[3], cs, T, V, C, Ceff, Kc) end end end -@inline function jacobianynsderiv!(jac::S,domain::Union{ConstantVDomain,ParametrizedVDomain,ConstantTVDomain,ConstantTAPhiDomain,ParametrizedTConstantVDomain},rxnarray::Array{Int64,2},efficiencyinds::Array{I,1},cs::Array{Float64,1},kfs::Array{Float64,1},krevs::Array{Float64,1},T::Float64,V::Float64,C::Float64) where {S<:AbstractArray,I<:Integer} +@inline function jacobianynsderiv!(jac::S, domain::Union{ConstantVDomain,ParametrizedVDomain,ConstantTVDomain,ConstantTAPhiDomain,ParametrizedTConstantVDomain}, rxnarray::Array{Int64,2}, efficiencyinds::Array{I,1}, cs::Array{Float64,1}, kfs::Array{Float64,1}, krevs::Array{Float64,1}, T::Float64, V::Float64, C::Float64) where {S<:AbstractArray,I<:Integer} @simd for rxnind = 1:size(rxnarray)[2] - @inbounds _jacobianynswrtns!(jac,rxnarray,rxnind,cs,kfs[rxnind],krevs[rxnind]) + @inbounds _jacobianynswrtns!(jac, rxnarray, rxnind, cs, kfs[rxnind], krevs[rxnind]) if rxnind in efficiencyinds - @inbounds @fastmath Kc = kfs[rxnind]/krevs[rxnind] - @inbounds @fastmath Ceff = C + sum(cs[i]*val for (i,val) in domain.phase.reactions[rxnind].kinetics.efficiencies) - @inbounds jacobianyefficiencyderiv!(jac,domain,domain.phase.reactions[rxnind].kinetics,domain.phase.reactions[rxnind].kinetics.efficiencies,rxnarray,rxnind,0,cs,T,V,C,Ceff,Kc) + @inbounds @fastmath Kc = kfs[rxnind] / krevs[rxnind] + @inbounds @fastmath Ceff = C + sum(cs[i] * val for (i, val) in domain.phase.reactions[rxnind].kinetics.efficiencies) + @inbounds jacobianyefficiencyderiv!(jac, domain, domain.phase.reactions[rxnind].kinetics, domain.phase.reactions[rxnind].kinetics.efficiencies, rxnarray, rxnind, 0, cs, T, V, C, Ceff, Kc) end end end -@inline function jacobianynsderiv!(jac::S,domain::Union{ConstantPDomain,ParametrizedPDomain},rxnarray::Array{Int64,2},efficiencyinds::Array{I,1},cs::Array{Float64,1},kfs::Array{Float64,1},krevs::Array{Float64,1},T::Float64,V::Float64,C::Float64) where {S<:AbstractArray,I<:Integer} +@inline function jacobianynsderiv!(jac::S, domain::Union{ConstantPDomain,ParametrizedPDomain}, rxnarray::Array{Int64,2}, efficiencyinds::Array{I,1}, cs::Array{Float64,1}, kfs::Array{Float64,1}, krevs::Array{Float64,1}, T::Float64, V::Float64, C::Float64) where {S<:AbstractArray,I<:Integer} @simd for rxnind = 1:size(rxnarray)[2] - @inbounds _jacobianynswrtns!(jac,rxnarray,rxnind,cs,kfs[rxnind],krevs[rxnind]) - @inbounds _jacobianynswrtV!(jac,domain.indexes[4],rxnarray,rxnind,cs,kfs[rxnind],krevs[rxnind]) + @inbounds _jacobianynswrtns!(jac, rxnarray, rxnind, cs, kfs[rxnind], krevs[rxnind]) + @inbounds _jacobianynswrtV!(jac, domain.indexes[4], rxnarray, rxnind, cs, kfs[rxnind], krevs[rxnind]) if rxnind in efficiencyinds - @inbounds @fastmath Kc = kfs[rxnind]/krevs[rxnind] - @inbounds @fastmath Ceff = C + sum(cs[i]*val for (i,val) in domain.phase.reactions[rxnind].kinetics.efficiencies) - @inbounds jacobianyefficiencyderiv!(jac,domain,domain.phase.reactions[rxnind].kinetics,domain.phase.reactions[rxnind].kinetics.efficiencies,rxnarray,rxnind,domain.indexes[4],cs,T,V,C,Ceff,Kc) + @inbounds @fastmath Kc = kfs[rxnind] / krevs[rxnind] + @inbounds @fastmath Ceff = C + sum(cs[i] * val for (i, val) in domain.phase.reactions[rxnind].kinetics.efficiencies) + @inbounds jacobianyefficiencyderiv!(jac, domain, domain.phase.reactions[rxnind].kinetics, domain.phase.reactions[rxnind].kinetics.efficiencies, rxnarray, rxnind, domain.indexes[4], cs, T, V, C, Ceff, Kc) end end end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantTPDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantTPDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) # dV/dt = sum(dn/dt)*R*T/P # d/dni(dV/dt) = sum(d/dni(dn/dt))*R*T/P # d/dV(dV/dt) = sum(d/dV(dn/dt))*R*T/P @simd for i in domain.indexes[1]:domain.indexes[2] - @views @inbounds @fastmath jac[domain.indexes[3],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/P + @views @inbounds @fastmath jac[domain.indexes[3], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / P end - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[3]] = sum(jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]])*R*T/P + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[3]] = sum(jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]]) * R * T / P @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dV/dt += inter.F(t)*R*T/P # d/dni(dn/dt) += 0 @@ -2058,7 +2228,7 @@ end # d/dV(dV/dt) += 0 nothing - elseif isa(inter,Outlet) && domain == inter.domain + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t).*ns./N = inter.F(t).*ns*(RT/PV) # dV/dt -= inter.F(t)*R*T/P # d/dni(dni/dt) -= inter.F(t)/N @@ -2068,12 +2238,12 @@ end flow = inter.F(t) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N + @inbounds @fastmath jac[i, i] -= flow / N end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]] .-= -flow.*ns/(N*V) - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]] .-= -flow .* ns / (N * V) + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) # evaporation # dn/dt .+= kLAs.*inter.V.*inter.cs @@ -2092,14 +2262,14 @@ end # -= d/dni(dni/dt)*R*T/P # d/dV(dV/dt) -= sum(d/dV(dn/dt))*R*T/P @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath jac[domain.indexes[3],i] -= ddnidnidt*R*T/P + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath jac[domain.indexes[3], i] -= ddnidnidt * R * T / P end - ddVdndt = kLAs.*inter.V.*cs*(-1/V)*R*T./kHs - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]] .-= ddVdndt - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[3]] -= sum(ddVdndt)*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + ddVdndt = kLAs .* inter.V .* cs * (-1 / V) * R * T ./ kHs + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]] .-= ddVdndt + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[3]] -= sum(ddVdndt) * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt += inter.Vin(t)*inter.cs # dV/dt += inter.Vin(t) # d/dn(dn/dt) += 0 @@ -2108,7 +2278,7 @@ end # d/dV(dV/dt) += 0 nothing - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dV/dt -= inter.Vout(t) # d/dni(dni/dt) -= inter.Vout(t)/V @@ -2117,19 +2287,19 @@ end # d/dV(dV/dt) -= 0 @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]] .-= -inter.Vout(t)*ns/(V*V) + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]] .-= -inter.Vout(t) * ns / (V * V) end end end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantVDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantVDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V # dT/dt = -dot(Us,dn/dt)/(N*Cvave) @@ -2140,17 +2310,17 @@ end # Note: d/dni(Cvave) = cpdivR[i]*R/N # d/dni(dP/dt) = sum(d/dni(dn/dt))*R*T/V + P/T*d/dni(dT/dt) for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @views @inbounds @fastmath jac[domain.indexes[3],i] = -dot(Us,jac[domain.indexes[1]:domain.indexes[2],i])/(N*Cvave)+dot(Us,dydt[domain.indexes[1]:domain.indexes[2]])/(N*Cvave*Cvave)*dCvavedni - @views @inbounds @fastmath jac[domain.indexes[4],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/V + P/T*jac[domain.indexes[3],i] + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @views @inbounds @fastmath jac[domain.indexes[3], i] = -dot(Us, jac[domain.indexes[1]:domain.indexes[2], i]) / (N * Cvave) + dot(Us, dydt[domain.indexes[1]:domain.indexes[2]]) / (N * Cvave * Cvave) * dCvavedni + @views @inbounds @fastmath jac[domain.indexes[4], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / V + P / T * jac[domain.indexes[3], i] end @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dTdt += inter.F(t)*(inter.H - dot(Us,ns)/N)/(N*Cvave) # dP/dt += inter.F(t)*R*T/V + P/T*dTdt @@ -2161,14 +2331,14 @@ end # Note: d/dni(Cvave) = cpdivR[i]*R/N # d/dni(dP/dt) += P/T*d/dni(dTdt) flow = inter.F(t) - @fastmath dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + @fastmath dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Us[i]/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += P/T*ddnidTdt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Us[i] / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += P / T * ddnidTdt end - elseif isa(inter,Outlet) && domain == inter.domain + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t).*ns./N # dT/dt -= (P*V/N*inter.F(t))/(N*Cvave) # dP/dt -= inter.F(t)*R*T/V + P/T*dT/dt @@ -2180,19 +2350,19 @@ end # -= -dT/dt * (dCvavedni/Cvave) # d/dni(dP/dt) -= P/T*d/dni(dT/dt) flow = inter.F(t) - @fastmath dTdt = (P*V/N*flow)/(N*Cvave) + @fastmath dTdt = (P * V / N * flow) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = -dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= P/T*ddnidTdt + @inbounds @fastmath jac[i, i] -= flow / N + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = -dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= P / T * ddnidTdt end - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs # evaporation # dn/dt .+= kLAs.*inter.V.*inter.cs @@ -2206,12 +2376,12 @@ end # += sum(kLAs.*inter.V.*inter.cs)*(-Us[i]/N)/(N*Cvave) - dT/dt * (dCvavedni/Cvave) # d/dni(dP/dt) += P/T*d/dni(dT/dt) flow = sum(evap) - @fastmath dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + @fastmath dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Us[i]/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += P/T*ddnidTdt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Us[i] / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += P / T * ddnidTdt end # condensation @@ -2228,16 +2398,16 @@ end # d/dni(dP/dt) -= kLAs[i]*inter.V/V*R*T/kHs[i]*R*T/V + P/T*d/dni(dT/dt) # -= d/dni(dni/dt)*R*T/V + P/T*d/dni(dT/dt) flow = sum(cond) - @fastmath dTdt = (P*V/N*flow)/(N*Cvave) + @fastmath dTdt = (P * V / N * flow) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = (P*V/N*ddnidnidt)/(N*Cvave) - dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= ddnidnidt*R*T/V + P/T*ddnidTdt + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = (P * V / N * ddnidnidt) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= ddnidnidt * R * T / V + P / T * ddnidTdt end - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt += inter.Vin(t)*inter.cs # dT/dt += inter.Vin(t)*(inter.Hpervolume - dot(Us,ns)/V)/(N*Cvave) # dP/dt += inter.Vin(t)*P/V + P/T*dTdt @@ -2247,15 +2417,15 @@ end # Note: Cvave = sum(ns.*cpdivR)*R/N - R # Note: d/dni(Cvave) = cpdivR[i]*R/N # d/dni(dP/dt) += P/T * d/dni(dT/dt) - @fastmath dTdt = (inter.Vin(t)*(inter.Hpervolume - dot(Us,ns)/V))/(N*Cvave) + @fastmath dTdt = (inter.Vin(t) * (inter.Hpervolume - dot(Us, ns) / V)) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = inter.Vin(t)*(-Us[i]/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += P/T*ddnidTdt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = inter.Vin(t) * (-Us[i] / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += P / T * ddnidTdt end - - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dT/dt -= (P*inter.Vout(t))/(N*Cvave) # dP/dt -= inter.Vout(t)*P/V + P/T*dTdt @@ -2266,29 +2436,29 @@ end # Note: d/dni(Cvave) = cpdivR[i]*R/N # -= -dT/dt * (dCvavedni/Cvave) # d/dni(dP/dt) -= P/T * d/dni(dT/dt) - @fastmath dTdt = (P*inter.Vout(t))/(N*Cvave) + @fastmath dTdt = (P * inter.Vout(t)) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = -dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= P/T*ddnidTdt + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = -dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= P / T * ddnidTdt end end end - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[3],T,colorvec) - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[4],P,colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[3], T, colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[4], P, colorvec) return jac end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantPDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ConstantPDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V # dT/dt = -dot(Hs,dn/dt)/(N*Cpave) @@ -2306,22 +2476,22 @@ end # = -dot(Hs,d/dV(dn/dt))/(N*Cpave) # d/dni(dV/dt) = sum(d/dni(dn/dt))*R*T/P + d/dni(dT/dt)*V/T # d/dV(dV/dt) = sum(d/dV(dn/dt))*R*T/P + d/dV(dT/dt)*V/T + dT/dt/T - @fastmath Cpave = Cvave+R - @views @inbounds @fastmath dTdt = -dot(Hs,dydt[domain.indexes[1]:domain.indexes[2]])/(N*Cpave) + @fastmath Cpave = Cvave + R + @views @inbounds @fastmath dTdt = -dot(Hs, dydt[domain.indexes[1]:domain.indexes[2]]) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @views @inbounds @fastmath jac[domain.indexes[3],i] = -dot(Hs,jac[domain.indexes[1]:domain.indexes[2],i])/(N*Cpave)-dTdt*(dCpavedni/Cpave) - @views @inbounds @fastmath jac[domain.indexes[4],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/P + V/T*jac[domain.indexes[3],i] + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @views @inbounds @fastmath jac[domain.indexes[3], i] = -dot(Hs, jac[domain.indexes[1]:domain.indexes[2], i]) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @views @inbounds @fastmath jac[domain.indexes[4], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / P + V / T * jac[domain.indexes[3], i] end - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[4]] = -dot(Hs,jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]])/(N*Cpave) - @views @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] = sum(jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]])*R*T/P + dTdt/T + V/T*jac[domain.indexes[3],domain.indexes[4]] + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[4]] = -dot(Hs, jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]]) / (N * Cpave) + @views @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] = sum(jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]]) * R * T / P + dTdt / T + V / T * jac[domain.indexes[3], domain.indexes[4]] @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dT/dt += inter.F(t)*(inter.H - dot(Hs,ns)/N)/(N*Cpave) # dV/dt += inter.F(t)*R*T/P + dT/dt*V/T @@ -2339,18 +2509,18 @@ end # d/dni(dV/dt) += d/dni(dT/dt)*V/T # d/dV(dV/dt) += d/dV(dT/dt)*V/T flow = inter.F(t) - @fastmath H = dot(Hs,ns)/N - @fastmath dTdt = flow*(inter.H - H)/(N*Cpave) + @fastmath H = dot(Hs, ns) / N + @fastmath dTdt = flow * (inter.H - H) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Hs[i]/N)/(N*Cpave)-dTdt*(dCpavedni/Cpave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += V/T*ddnidTdt + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Hs[i] / N) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += V / T * ddnidTdt end - @fastmath ddVdTdt = flow*H/V/(N*Cpave) - @inbounds jac[domain.indexes[3],domain.indexes[4]] += ddVdTdt - @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] += V/T*ddVdTdt - elseif isa(inter,Outlet) && domain == inter.domain + @fastmath ddVdTdt = flow * H / V / (N * Cpave) + @inbounds jac[domain.indexes[3], domain.indexes[4]] += ddVdTdt + @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] += V / T * ddVdTdt + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t).*ns./N # dT/dt -= 0 # dV/dt -= inter.F(t)*R*T/P @@ -2364,14 +2534,14 @@ end # d/dV(dV/dt) -= 0 flow = inter.F(t) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N + @inbounds @fastmath jac[i, i] -= flow / N end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= -flow*ns/N/V - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= -flow * ns / N / V + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + # evaporation # dn/dt .+= kLAs.*inter.V.*inter.cs # dT/dt += sum(kLAs.*inter.V.*inter.cs)*(inter.H - dot(Hs,ns)/N)/(N*Cpave) @@ -2393,17 +2563,17 @@ end # d/dni(dV/dt) += d/dni(dT/dt)*V/T # d/dV(dV/dt) += d/dV(dT/dt)*V/T + dT/dt/T flow = sum(evap) - @fastmath H = dot(Hs,ns)/N - @fastmath dTdt = flow*(inter.H - H)/(N*Cpave) + @fastmath H = dot(Hs, ns) / N + @fastmath dTdt = flow * (inter.H - H) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Hs[i]/N)/(N*Cpave)-dTdt*(dCpavedni/Cpave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += V/T*ddnidTdt + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Hs[i] / N) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += V / T * ddnidTdt end - @fastmath ddVdTdt = flow*H/V/(N*Cpave) - @inbounds jac[domain.indexes[3],domain.indexes[4]] += ddVdTdt - @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] += dTdt/T + V/T*ddVdTdt + @fastmath ddVdTdt = flow * H / V / (N * Cpave) + @inbounds jac[domain.indexes[3], domain.indexes[4]] += ddVdTdt + @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] += dTdt / T + V / T * ddVdTdt # condensation # dn/dt .-= kLAs.*inter.V.*cs*R*T./kHs @@ -2417,14 +2587,14 @@ end # -= ddnidnidt*R*T/P # d/dV(dV/dt) -= sum(d/dV(dn/dt))*R*T/P @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath jac[domain.indexes[4],i] -= ddnidnidt*R*T/P + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath jac[domain.indexes[4], i] -= ddnidnidt * R * T / P end - ddVdndt = kLAs.*inter.V.*cs*(-1/V)*R*T./kHs - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= ddVdndt - @views @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] .-= sum(ddVdndt)*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + ddVdndt = kLAs .* inter.V .* cs * (-1 / V) * R * T ./ kHs + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= ddVdndt + @views @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] .-= sum(ddVdndt) * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt .+= inter.Vin(t)*inter.cs # dT/dt += inter.Vin(t)*(inter.Hpervolume - dot(Hs,ns)/V)/(N*Cpave) # dV/dt += inter.Vin(t) @@ -2437,10 +2607,10 @@ end # = inter.Vin(t)*(dot(Hs, ns)/V^2)/(N*Cpave) - dT/dt/(N*Cpave) * ((P/RT)*Cpave - (PV/RT)*Cpave/V) # = 0 @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds jac[domain.indexes[3],i] += inter.Vin(t)*(-Hs[i]/N)/(N*Cpave) - dTdt*(dCpavedni/Cpave) + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds jac[domain.indexes[3], i] += inter.Vin(t) * (-Hs[i] / N) / (N * Cpave) - dTdt * (dCpavedni / Cpave) end - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dT/dt -= 0 # dV/dt -= inter.Vout(t) @@ -2451,36 +2621,36 @@ end # d/dni(dV/dt) -= 0 # d/dV(dV/dt) -= 0 @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= -inter.Vout(t)/(V*V)*ns + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= -inter.Vout(t) / (V * V) * ns end end - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[3],T,colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[3], T, colorvec) return jac end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedTPDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedTPDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) # dV/dt = sum(dn/dt)*R*T/P + Calculus.derivative(d.T,t)*V/T - Calculus.derivative(d.P,t)*V/P # d/dni(dV/dt) = sum(d/dni(dn/dt))*R*T/P # d/dV(dV/dt) = sum(d/dV(dn/dt))*R*T/P + Calculus.derivative(d.T,t)/T - Calculus.derivative(d.P,t)/P @simd for i in domain.indexes[1]:domain.indexes[2] - @views @inbounds @fastmath jac[domain.indexes[3],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/P + @views @inbounds @fastmath jac[domain.indexes[3], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / P end - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[3]] = sum(jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]])*R*T/P + Calculus.derivative(domain.T,t)/T - Calculus.derivative(domain.P,t)/P + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[3]] = sum(jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]]) * R * T / P + Calculus.derivative(domain.T, t) / T - Calculus.derivative(domain.P, t) / P @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dV/dt += inter.F(t)*R*T/P # d/dni(dni/dt) += 0 @@ -2488,8 +2658,8 @@ end # d/dni(dV/dt) += 0 # d/dV(dV/dt) += 0 nothing - - elseif isa(inter,Outlet) && domain == inter.domain + + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t)*ns./N # dV/dt -= inter.F(t)*R*T/P # d/dni(dni/dt) -= inter.F(t)/N @@ -2501,12 +2671,12 @@ end # d/dV(dV/dt) -= 0 flow = inter.F(t) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N + @inbounds @fastmath jac[i, i] -= flow / N end - @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]] .-= -flow*ns/N/V - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) + @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]] .-= -flow * ns / N / V + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) # evaporaiton # dn/dt .+= kLAs.*inter.V.*inter.cs @@ -2525,18 +2695,18 @@ end # -= d/dni(dni/dt)*R*T/P # d/dV(dV/dt) -= sum(d/dV(dn/dt))*R*T/P @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath jac[domain.indexes[3],i] -= ddnidnidt*R*T/P + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath jac[domain.indexes[3], i] -= ddnidnidt * R * T / P end - ddVdndt = kLAs.*inter.V.*cs.*(-1/V)*R*T./kHs - @views @inbounds @fastmath jac[domain.indexs[1]:domain.indexes[2],domain.indexes[3]] .-= ddVdndt - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[3]] -= sum(ddVdndt)*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + ddVdndt = kLAs .* inter.V .* cs .* (-1 / V) * R * T ./ kHs + @views @inbounds @fastmath jac[domain.indexs[1]:domain.indexes[2], domain.indexes[3]] .-= ddVdndt + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[3]] -= sum(ddVdndt) * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt .+= inter.Vin(t)*inter.cs # dV/dt += inter.Vin(t) nothing - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dV/dt -= inter.Vout(t) # d/dni(dni/dt) -= inter.Vout(t)/V @@ -2544,21 +2714,21 @@ end # d/dni(dV/dt) -= 0 # d/dV(dV/dt) -= 0 @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[3]] .-= -inter.Vout(t)*ns/(V*V) + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[3]] .-= -inter.Vout(t) * ns / (V * V) end end return jac end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedVDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedVDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V # dT/dt = (-dot(Us,dn/dt)-P*dV/dt(t))/(N*Cvave) @@ -2569,19 +2739,19 @@ end # Note: d/dni(Cvave) = cpdivR[i]*R/N # = -dot(Us, d/dni(dn/dt))/(N*Cvave) - dT/dt * (d/dni(Cvave)/Cvave) # d/dni(dP/dt) = sum(d/dni(dn/dt))*R*T/V + d/dni(dT/dt)*P/T - dVdt = Calculus.derivative(domain.V,t) + dVdt = Calculus.derivative(domain.V, t) @simd for i in domain.indexes[1]:domain.indexes[2] - @fastmath @inbounds dCvavedni = cpdivR[i]*R/N - @views @inbounds @fastmath jac[domain.indexes[3],i] = -dot(Us,jac[domain.indexes[1]:domain.indexes[2],i])/(N*Cvave)-(-dot(Us,dydt[domain.indexes[1]:domain.indexes[2]])-P*dVdt)/(N*Cvave*Cvave)*dCvavedni - @views @inbounds @fastmath jac[domain.indexes[4],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/V + P/T*jac[domain.indexes[3],i] + @fastmath @inbounds dCvavedni = cpdivR[i] * R / N + @views @inbounds @fastmath jac[domain.indexes[3], i] = -dot(Us, jac[domain.indexes[1]:domain.indexes[2], i]) / (N * Cvave) - (-dot(Us, dydt[domain.indexes[1]:domain.indexes[2]]) - P * dVdt) / (N * Cvave * Cvave) * dCvavedni + @views @inbounds @fastmath jac[domain.indexes[4], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / V + P / T * jac[domain.indexes[3], i] end @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dT/dt += inter.F(t)*(inter.H - dot(Us,ns)/N)/(N*Cvave) # dP/dt += inter.F(t)*R*T/V + dT/dt*P/T @@ -2592,14 +2762,14 @@ end # += inter.F(t)*(-Us[i]/N)/(N*Cvave) + dT/dt * (-1/Cvave) * d/dni(Cvave) # d/dni(dP/dt) += d/dni(dT/dt)*P/T flow = inter.F(t) - @fastmath dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + @fastmath dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Us[i]/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += P/T*ddnidTdt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Us[i] / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += P / T * ddnidTdt end - elseif isa(inter,Outlet) && domain == inter.domain + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t)*ns./N # dT/dt -= (P*V/N*inter.F(t))/(N*Cvave) # dP/dt -= inter.F(t)*R*T/V + dT/dt*P/T @@ -2608,20 +2778,20 @@ end # -= dT/dt * (-1/Cvave) * d/dni(Cvave) # d/dni(dP/dt) -= d/dni(dT/dt)*P/T flow = inter.F(t) - @fastmath dTdt = (P*V/N*flow)/(N*Cvave) + @fastmath dTdt = (P * V / N * flow) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = -dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= P/T*ddnidTdt + @inbounds @fastmath jac[i, i] -= flow / N + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = -dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= P / T * ddnidTdt end - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - cond = kLAs.*inter.V.*cs*R*T./kHs - + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + cond = kLAs .* inter.V .* cs * R * T ./ kHs + # evaporation # dn/dt .+= kLAs.*inter.V.*inter.cs # dT/dt += sum(kLAs.*inter.V.*inter.cs)*(inter.H - dot(Us,ns)/N)/(N*Cvave) @@ -2631,12 +2801,12 @@ end # += sum(kLAs.*inter.V.*inter.cs)*(-Us[i]/N)/(N*Cvave) + dT/dt * (-1/Cvave) * d/dni(Cvave) # d/dni(dP/dt) += d/dni(dT/dt)*P/T flow = sum(evap) - @fastmath dTdt = flow*(inter.H - dot(Us,ns)/N)/(N*Cvave) + @fastmath dTdt = flow * (inter.H - dot(Us, ns) / N) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Us[i]/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += P/T*ddnidTdt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Us[i] / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += P / T * ddnidTdt end # condensation @@ -2648,16 +2818,16 @@ end # d/dni(dP/dt) -= kLAs[i]*inter.V/V*R*T/kHs[i]*R*T/V + P/T*ddnidTdt # -= d/dni(dni/dt)*R*T/V + P/T*ddnidTdt flow = sum(cond) - @fastmath dTdt = (P*V/N*flow)/(N*Cvave) + @fastmath dTdt = (P * V / N * flow) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = (P*V/N*ddnidnidt)/(N*Cvave) - dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= ddnidnidt*R*T/V + P/T*ddnidTdt + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = (P * V / N * ddnidnidt) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= ddnidnidt * R * T / V + P / T * ddnidTdt end - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt .+= inter.Vin(t)*inter.cs # dT/dt += inter.Vin(t)*(inter.Hpervolume - dot(Us,ns)/V)/(N*Cvave) # dP/dt += inter.Vin(t)*P/V + dT/dt*P/T @@ -2669,13 +2839,13 @@ end # Note: dCvave/dni = cpdivR[i]*R/N # d/dni(dP/dt) += d/dni(dT/dt)*P/T @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = (inter.Vout*P/N)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - ddnidTdt = inter.Vin(t)*(-Us[i]/V)/(N*Cvave)-dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += ddnidTdt*P/T + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = (inter.Vout * P / N) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + ddnidTdt = inter.Vin(t) * (-Us[i] / V) / (N * Cvave) - dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += ddnidTdt * P / T end - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dT/dt -= (P*inter.Vout(t))/(N*Cvave) # dP/dt -= inter.Vout(t)*P/V + dT/dt*P/T @@ -2684,29 +2854,29 @@ end # -= dT/dt *(-1/(Cvave)) d/dni(Cvave) # -= - dT/dt * (dCvavedni/Cvave) # d/dni(dP/dt) -= P/T * d/dni(dT/dt) - @fastmath dTdt = (P*inter.Vout(t))/(N*Cvave) + @fastmath dTdt = (P * inter.Vout(t)) / (N * Cvave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V - @inbounds @fastmath dCvavedni = cpdivR[i]*R/N - @fastmath ddnidTdt = -dTdt*(dCvavedni/Cvave) - @inbounds jac[domain.indexes[3],i] -= ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] -= P/T*ddnidTdt + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V + @inbounds @fastmath dCvavedni = cpdivR[i] * R / N + @fastmath ddnidTdt = -dTdt * (dCvavedni / Cvave) + @inbounds jac[domain.indexes[3], i] -= ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] -= P / T * ddnidTdt end end end - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[3],T,colorvec) - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[4],P,colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[3], T, colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[4], P, colorvec) return jac end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedPDomain} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:ParametrizedPDomain} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V # dT/dt = (-dot(Hs,dn/dt)+V*dPdt(t))/(N*Cpave) @@ -2717,23 +2887,23 @@ end # = (-dot(Hs,d/dV(dn/dt)) + dPdt(t))/(N*Cpave) + dT/dt * (-1/(N*Cpave)) # d/dni(dV/dt) = sum(d/dni(dn/dt))*R*T/P + V/T*d/dni(dT/dt) # d/dV(dV/dt) = sum(d/dV(dn/dt))*R*T/P + d/dV(dT/dt)*V/T + dT/dt/T - dPdt(t)/P - @fastmath Cpave = Cvave+R - dPdt = Calculus.derivative(domain.P,t) - @views @inbounds @fastmath dTdt = (-dot(Hs,dydt[domain.indexes[1]:domain.indexes[2]])+V*dPdt)/(N*Cpave) + @fastmath Cpave = Cvave + R + dPdt = Calculus.derivative(domain.P, t) + @views @inbounds @fastmath dTdt = (-dot(Hs, dydt[domain.indexes[1]:domain.indexes[2]]) + V * dPdt) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @views @inbounds @fastmath jac[domain.indexes[3],i] = -dot(Hs,jac[domain.indexes[1]:domain.indexes[2],i])/(N*Cpave)-dTdt*(dCpavedni/Cpave) - @views @inbounds @fastmath jac[domain.indexes[4],i] = sum(jac[domain.indexes[1]:domain.indexes[2],i])*R*T/P + V/T*jac[domain.indexes[3],i] + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @views @inbounds @fastmath jac[domain.indexes[3], i] = -dot(Hs, jac[domain.indexes[1]:domain.indexes[2], i]) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @views @inbounds @fastmath jac[domain.indexes[4], i] = sum(jac[domain.indexes[1]:domain.indexes[2], i]) * R * T / P + V / T * jac[domain.indexes[3], i] end - @views @inbounds @fastmath jac[domain.indexes[3],domain.indexes[4]] = (-dot(Hs,jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]])+dPdt)/(N*Cpave) - @views @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] = sum(jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]])*R*T/P + dTdt/T + V/T*jac[domain.indexes[3],domain.indexes[4]]-dPdt/P + @views @inbounds @fastmath jac[domain.indexes[3], domain.indexes[4]] = (-dot(Hs, jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]]) + dPdt) / (N * Cpave) + @views @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] = sum(jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]]) * R * T / P + dTdt / T + V / T * jac[domain.indexes[3], domain.indexes[4]] - dPdt / P @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain # dn/dt .+= inter.y.*inter.F(t) # dT/dt += inter.F(t)*(inter.H - dot(Hs,ns)/N)/(N*Cpave) # dV/dt += inter.F(t)*R*T/P + dT/dt*V/T @@ -2750,18 +2920,18 @@ end # d/dni(dV/dt) += V/T*d/dni(dT/dt) # d/dV(dV/dt) += d/dV(dT/dt)*V/T + dT/dt/T flow = inter.F(t) - @fastmath H = dot(Hs,ns)/N - @fastmath dTdt = flow*(inter.H - H)/(N*Cpave) + @fastmath H = dot(Hs, ns) / N + @fastmath dTdt = flow * (inter.H - H) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Hs[i]/N)/(N*Cpave)-dTdt*(dCpavedni/Cpave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += V/T*ddnidTdt + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Hs[i] / N) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += V / T * ddnidTdt end - @fastmath ddVdTdt = flow*H/V/(N*Cpave) - @inbounds jac[domain.indexes[3],domain.indexes[4]] += ddVdTdt - @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] += dTdt/T + V/T*ddVdTdt - elseif isa(inter,Outlet) && domain == inter.domain + @fastmath ddVdTdt = flow * H / V / (N * Cpave) + @inbounds jac[domain.indexes[3], domain.indexes[4]] += ddVdTdt + @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] += dTdt / T + V / T * ddVdTdt + elseif isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t).*ns./N # dT/dt -= 0 # dV/dt -= inter.F(t)*R*T/P @@ -2775,14 +2945,14 @@ end # d/dV(dV/dt) -= 0 flow = inter.F(t) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N + @inbounds @fastmath jac[i, i] -= flow / N end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= -flow*ns/N/V - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,inter.T) - kHs = map.(inter.kHs,inter.T) - evap = kLAs.*inter.V.*inter.cs - + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= -flow * ns / N / V + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, inter.T) + kHs = map.(inter.kHs, inter.T) + evap = kLAs .* inter.V .* inter.cs + # evaporation # dn/dt .+= kLAs.*inter.V.*inter.cs # dT/dt += sum(kLAs.*inter.V.*inter.cs)*(inter.H - dot(Hs,ns)/N)/(N*Cpave) @@ -2802,17 +2972,17 @@ end # d/dni(dV/dt) += V/T*d/dni(dT/dt) # d/dV(dV/dt) += d/dV(dT/dt)*V/T + dT/dt/T flow = sum(evap) - @fastmath H = dot(Hs,ns)/N - @fastmath dTdt = flow*(inter.H - H)/(N*Cpave) + @fastmath H = dot(Hs, ns) / N + @fastmath dTdt = flow * (inter.H - H) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds @fastmath ddnidTdt = flow*(-Hs[i]/N)/(N*Cpave) - dTdt*(dCpavedni/Cpave) - @inbounds jac[domain.indexes[3],i] += ddnidTdt - @inbounds @fastmath jac[domain.indexes[4],i] += V/T*ddnidTdt + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds @fastmath ddnidTdt = flow * (-Hs[i] / N) / (N * Cpave) - dTdt * (dCpavedni / Cpave) + @inbounds jac[domain.indexes[3], i] += ddnidTdt + @inbounds @fastmath jac[domain.indexes[4], i] += V / T * ddnidTdt end - @fastmath ddVdTdt = flow*H/V/(N*Cpave) - @inbounds jac[domain.indexes[3],domain.indexes[4]] += ddVdTdt - @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] += dTdt/T + V/T*ddVdTdt + @fastmath ddVdTdt = flow * H / V / (N * Cpave) + @inbounds jac[domain.indexes[3], domain.indexes[4]] += ddVdTdt + @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] += dTdt / T + V / T * ddVdTdt # condensation # dn/dt .-= kLAs.*inter.V.*cs*R*T./kHs @@ -2826,14 +2996,14 @@ end # -= d/dni(dni/dt)*R*T/P # d/dV(dV/dt) -= sum(d/dV(dn/dt))*R*T/P @simd for i in domain.indexes[1]:domain.indexes[2] - ddnidnidt = kLAs[i]*inter.V/V*R*T/kHs[i] - @inbounds @fastmath jac[i,i] -= ddnidnidt - @inbounds @fastmath jac[domain.indexes[4],i] -= ddnidnidt*R*T/P + ddnidnidt = kLAs[i] * inter.V / V * R * T / kHs[i] + @inbounds @fastmath jac[i, i] -= ddnidnidt + @inbounds @fastmath jac[domain.indexes[4], i] -= ddnidnidt * R * T / P end - ddVdndt = kLAs.*inter.V.*cs.*(-1/V)*R*T./kHs - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= ddVdndt - @inbounds @fastmath jac[domain.indexes[4],domain.indexes[4]] -= sum(ddVdndt)*R*T/P - elseif isa(inter,VolumetricFlowRateInlet) && domain == inter.domain + ddVdndt = kLAs .* inter.V .* cs .* (-1 / V) * R * T ./ kHs + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= ddVdndt + @inbounds @fastmath jac[domain.indexes[4], domain.indexes[4]] -= sum(ddVdndt) * R * T / P + elseif isa(inter, VolumetricFlowRateInlet) && domain == inter.domain # dn/dt .+= inter.Vin(t)*inter.cs # dT/dt += inter.Vin(t)*(inter.Hpervolume - dot(Hs,ns)/V)/(N*Cpave) # dV/dt += inter.Vin(t) @@ -2849,12 +3019,12 @@ end # Note: d/dV(Cpave) = dot(cpdivR,ns)*R*(RT/PV)*(-1/V) = -Cpave/V # = inter.Vin(t)*(dot(Hs,ns)/V^2)/(N*Cpave) - dT/dt/(N*Cpave) * (P/RT*Cpave + PV/RT(-Cpave/V)) # = 0 - dTdt = inter.Vin(t)*(inter.Hpervolume - dot(Hs,ns)/V)/(N*Cpave) + dTdt = inter.Vin(t) * (inter.Hpervolume - dot(Hs, ns) / V) / (N * Cpave) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath dCpavedni = cpdivR[i]*R/N - @inbounds @fastmath jac[domain.indexes[3],i] += inter.Vin(t)*(-Hs[i]/V)/(N*Cpave) - dTdt*(dCpavedni/Cpave) + @inbounds @fastmath dCpavedni = cpdivR[i] * R / N + @inbounds @fastmath jac[domain.indexes[3], i] += inter.Vin(t) * (-Hs[i] / V) / (N * Cpave) - dTdt * (dCpavedni / Cpave) end - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # dT/dt -= 0 # dV/dt -= inter.Vout(t) @@ -2865,54 +3035,54 @@ end # d/dni(dV/dt) -= 0 # d/dV(dV/dt) -= 0 @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V end - @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2],domain.indexes[4]] .-= -inter.Vout(t)/(V*V)*ns + @views @inbounds @fastmath jac[domain.indexes[1]:domain.indexes[2], domain.indexes[4]] .-= -inter.Vout(t) / (V * V) * ns end end - @inbounds jacobianytherm!(jac,y,p,t,domain,interfaces,domain.indexes[3],T,colorvec) + @inbounds jacobianytherm!(jac, y, p, t, domain, interfaces, domain.indexes[3], T, colorvec) return jac end -@inline function jacobiany!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain,ConstantTAPhiDomain,ParametrizedTConstantVDomain}} - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,phi = calcthermo(domain,y,t,p) - jacobianynsderiv!(jac,domain,domain.rxnarray,domain.efficiencyinds,cs,kfs,krevs,T,V,C) +@inline function jacobiany!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain,ConstantTAPhiDomain,ParametrizedTConstantVDomain}} + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, phi = calcthermo(domain, y, t, p) + jacobianynsderiv!(jac, domain, domain.rxnarray, domain.efficiencyinds, cs, kfs, krevs, T, V, C) @simd for ind in domain.constantspeciesinds - @inbounds jac[ind,:] .= 0. + @inbounds jac[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Outlet) && domain == inter.domain + if isa(inter, Outlet) && domain == inter.domain # dn/dt .-= inter.F(t).*ns./N # Note N = sum(ns) for liquid phase # d/dni(dni/dt) -= inter.F(t)/N - inter.F(t)*ns[i]/(N*N) # d/dnj(dni/dt) -= -inter.F(t)*ns[i]/(N*N) flow = inter.F(t) @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= flow/N - @inbounds @fastmath jac[i,:] .+= flow*ns[i]/(N*N) + @inbounds @fastmath jac[i, i] -= flow / N + @inbounds @fastmath jac[i, :] .+= flow * ns[i] / (N * N) end - elseif isa(inter,kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain - kLAs = map.(inter.kLAs,T) + elseif isa(inter, kLAkHCondensationEvaporationWithReservoir) && domain == inter.domain + kLAs = map.(inter.kLAs, T) # evaporation # dn/dt .-= kLAs.*ns # d/dni(dni/dt) -= kLAs[i] @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= kLAs[i] + @inbounds @fastmath jac[i, i] -= kLAs[i] end # condensation # dn/dt .+= kLAs.*inter.molefractions*inter.P/kHs*V nothing - elseif isa(inter,VolumetricFlowRateOutlet) && domain == inter.domain + elseif isa(inter, VolumetricFlowRateOutlet) && domain == inter.domain # dn/dt .-= inter.Vout(t)*ns/V # d/dni(dni/dt) -= inter.Vout(t)/V @simd for i in domain.indexes[1]:domain.indexes[2] - @inbounds @fastmath jac[i,i] -= inter.Vout(t)/V + @inbounds @fastmath jac[i, i] -= inter.Vout(t) / V end end end @@ -2922,107 +3092,107 @@ end export jacobiany! -@inline function jacobianpnsderiv!(jacp::Q,y::U,p::W,t::Z,domain::D,rxnarray::Array{Int64,2},cs::Array{Float64,1},T::Float64,V::Float64,kfs::Array{Float64,1},krevs::Array{Float64,1},Nspcs::Int64,Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTPDomain,ConstantTAPhiDomain}} - @fastmath RTinv = 1.0/(R*T) +@inline function jacobianpnsderiv!(jacp::Q, y::U, p::W, t::Z, domain::D, rxnarray::Array{Int64,2}, cs::Array{Float64,1}, T::Float64, V::Float64, kfs::Array{Float64,1}, krevs::Array{Float64,1}, Nspcs::Int64, Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTPDomain,ConstantTAPhiDomain}} + @fastmath RTinv = 1.0 / (R * T) @simd for rxnind = 1:Nrxns if rxnind in domain.efficiencyinds - if @inbounds rxnarray[2,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]] - elseif @inbounds rxnarray[3,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - elseif @inbounds rxnarray[4,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] + if @inbounds rxnarray[2, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] + elseif @inbounds rxnarray[3, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + elseif @inbounds rxnarray[4, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] else - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] end - if @inbounds rxnarray[6,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]] - elseif @inbounds rxnarray[7,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - elseif @inbounds rxnarray[8,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] + if @inbounds rxnarray[6, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] + elseif @inbounds rxnarray[7, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + elseif @inbounds rxnarray[8, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] else - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] end else - if @inbounds rxnarray[2,rxnind] == 0 - @inbounds fderiv = cs[rxnarray[1,rxnind]] - elseif @inbounds rxnarray[3,rxnind] == 0 - @fastmath @inbounds fderiv = cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - elseif @inbounds rxnarray[4,rxnind] == 0 - @fastmath @inbounds fderiv = cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] + if @inbounds rxnarray[2, rxnind] == 0 + @inbounds fderiv = cs[rxnarray[1, rxnind]] + elseif @inbounds rxnarray[3, rxnind] == 0 + @fastmath @inbounds fderiv = cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + elseif @inbounds rxnarray[4, rxnind] == 0 + @fastmath @inbounds fderiv = cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] else - @fastmath @inbounds fderiv = cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] + @fastmath @inbounds fderiv = cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] end - if @inbounds rxnarray[6,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]/kfs[rxnind]*cs[rxnarray[5,rxnind]] - elseif @inbounds rxnarray[7,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]/kfs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - elseif @inbounds rxnarray[8,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]/kfs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] + if @inbounds rxnarray[6, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] / kfs[rxnind] * cs[rxnarray[5, rxnind]] + elseif @inbounds rxnarray[7, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] / kfs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + elseif @inbounds rxnarray[8, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] / kfs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] else - @fastmath @inbounds rderiv = krevs[rxnind]/kfs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] + @fastmath @inbounds rderiv = krevs[rxnind] / kfs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] end end - flux = fderiv-rderiv - _spreadreactantpartials!(jacp,flux,rxnarray,rxnind,Nspcs+rxnind) - _spreadproductpartials!(jacp,-flux,rxnarray,rxnind,Nspcs+rxnind) - - @fastmath @inbounds gderiv = rderiv*kfs[rxnind]*RTinv - - @inbounds jacp[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[1,rxnind]) - if @inbounds rxnarray[2,rxnind] !== 0 - @inbounds jacp[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[2,rxnind]) - if @inbounds rxnarray[3,rxnind] !== 0 - @inbounds jacp[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[3,rxnind]) - if @inbounds rxnarray[4,rxnind] !== 0 - @inbounds jacp[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[4,rxnind]) + flux = fderiv - rderiv + _spreadreactantpartials!(jacp, flux, rxnarray, rxnind, Nspcs + rxnind) + _spreadproductpartials!(jacp, -flux, rxnarray, rxnind, Nspcs + rxnind) + + @fastmath @inbounds gderiv = rderiv * kfs[rxnind] * RTinv + + @inbounds jacp[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[1, rxnind]) + if @inbounds rxnarray[2, rxnind] !== 0 + @inbounds jacp[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[2, rxnind]) + if @inbounds rxnarray[3, rxnind] !== 0 + @inbounds jacp[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[3, rxnind]) + if @inbounds rxnarray[4, rxnind] !== 0 + @inbounds jacp[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[4, rxnind]) end end end - @inbounds jacp[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[5,rxnind]) - if @inbounds rxnarray[6,rxnind] !== 0 - @inbounds jacp[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[6,rxnind]) - if @inbounds rxnarray[7,rxnind] !== 0 - @inbounds jacp[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[7,rxnind]) - if @inbounds rxnarray[8,rxnind] !== 0 - @inbounds jacp[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[8,rxnind]) + @inbounds jacp[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[5, rxnind]) + if @inbounds rxnarray[6, rxnind] !== 0 + @inbounds jacp[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[6, rxnind]) + if @inbounds rxnarray[7, rxnind] !== 0 + @inbounds jacp[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[7, rxnind]) + if @inbounds rxnarray[8, rxnind] !== 0 + @inbounds jacp[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[8, rxnind]) end end end @@ -3031,85 +3201,85 @@ export jacobiany! jacp .*= V end -@inline function jacobianpnsderiv!(jacp::Q,y::U,p::W,t::Z,domain::D,rxnarray::Array{Int64,2},cs::Array{Float64,1},T::Float64,V::Float64,kfs::Array{Float64,1},krevs::Array{Float64,1},Nspcs::Int64,Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantVDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedVDomain,ParametrizedPDomain,ParametrizedTConstantVDomain}} - @fastmath RTinv = 1.0/(R*T) +@inline function jacobianpnsderiv!(jacp::Q, y::U, p::W, t::Z, domain::D, rxnarray::Array{Int64,2}, cs::Array{Float64,1}, T::Float64, V::Float64, kfs::Array{Float64,1}, krevs::Array{Float64,1}, Nspcs::Int64, Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantVDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedVDomain,ParametrizedPDomain,ParametrizedTConstantVDomain}} + @fastmath RTinv = 1.0 / (R * T) @simd for rxnind = 1:Nrxns - if @inbounds rxnarray[2,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]] - elseif @inbounds rxnarray[3,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - elseif @inbounds rxnarray[4,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] + if @inbounds rxnarray[2, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] + elseif @inbounds rxnarray[3, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + elseif @inbounds rxnarray[4, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] else - @fastmath @inbounds fderiv = kfs[rxnind]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] + @fastmath @inbounds fderiv = kfs[rxnind] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] end - if @inbounds rxnarray[6,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]] - elseif @inbounds rxnarray[7,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - elseif @inbounds rxnarray[8,rxnind] == 0 - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] + if @inbounds rxnarray[6, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] + elseif @inbounds rxnarray[7, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + elseif @inbounds rxnarray[8, rxnind] == 0 + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] else - @fastmath @inbounds rderiv = krevs[rxnind]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] + @fastmath @inbounds rderiv = krevs[rxnind] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] end - @fastmath flux = fderiv-rderiv - _spreadreactantpartials!(jacp,flux,rxnarray,rxnind,Nspcs+rxnind) - _spreadproductpartials!(jacp,-flux,rxnarray,rxnind,Nspcs+rxnind) - - @fastmath gderiv = rderiv*RTinv - - @inbounds jacp[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[1,rxnind]) - if @inbounds rxnarray[2,rxnind] !== 0 - @inbounds jacp[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[2,rxnind]) - if @inbounds rxnarray[3,rxnind] !== 0 - @inbounds jacp[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[3,rxnind]) - if @inbounds rxnarray[4,rxnind] !== 0 - @inbounds jacp[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[4,rxnind]) + @fastmath flux = fderiv - rderiv + _spreadreactantpartials!(jacp, flux, rxnarray, rxnind, Nspcs + rxnind) + _spreadproductpartials!(jacp, -flux, rxnarray, rxnind, Nspcs + rxnind) + + @fastmath gderiv = rderiv * RTinv + + @inbounds jacp[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[1, rxnind]) + if @inbounds rxnarray[2, rxnind] !== 0 + @inbounds jacp[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[2, rxnind]) + if @inbounds rxnarray[3, rxnind] !== 0 + @inbounds jacp[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[3, rxnind]) + if @inbounds rxnarray[4, rxnind] !== 0 + @inbounds jacp[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[4, rxnind]) end end end - @inbounds jacp[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[5,rxnind]) - if @inbounds rxnarray[6,rxnind] !== 0 - @inbounds jacp[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[6,rxnind]) - if @inbounds rxnarray[7,rxnind] !== 0 - @inbounds jacp[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[7,rxnind]) - if @inbounds rxnarray[8,rxnind] !== 0 - @inbounds jacp[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[8,rxnind]) + @inbounds jacp[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[5, rxnind]) + if @inbounds rxnarray[6, rxnind] !== 0 + @inbounds jacp[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[6, rxnind]) + if @inbounds rxnarray[7, rxnind] !== 0 + @inbounds jacp[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[7, rxnind]) + if @inbounds rxnarray[8, rxnind] !== 0 + @inbounds jacp[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[8, rxnind]) end end end @@ -3117,85 +3287,85 @@ end jacp .*= V end -@inline function jacobianpnsderiv!(jacp::Q,y::U,p::W,t::Z,domain::D,rxnarray::Array{Int64,2},cs::Array{Float64,1},T::Float64,V::Float64,kfs::Array{Float64,1},krevs::Array{Float64,1},Nspcs::Int64,Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain}} - @fastmath RTinv = 1.0/(R*T) +@inline function jacobianpnsderiv!(jacp::Q, y::U, p::W, t::Z, domain::D, rxnarray::Array{Int64,2}, cs::Array{Float64,1}, T::Float64, V::Float64, kfs::Array{Float64,1}, krevs::Array{Float64,1}, Nspcs::Int64, Nrxns::Int64) where {Q3<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain}} + @fastmath RTinv = 1.0 / (R * T) @simd for rxnind = 1:Nrxns - if @inbounds rxnarray[2,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*kfs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[1,rxnind]] - elseif @inbounds rxnarray[3,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*kfs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - elseif rxnarray[4,rxnind] == 0 - @fastmath @inbounds fderiv = kfs[rxnind]*kfs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] + if @inbounds rxnarray[2, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * kfs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[1, rxnind]] + elseif @inbounds rxnarray[3, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * kfs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + elseif rxnarray[4, rxnind] == 0 + @fastmath @inbounds fderiv = kfs[rxnind] * kfs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] else - @fastmath @inbounds fderiv = kfs[rxnind]*kfs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] + @fastmath @inbounds fderiv = kfs[rxnind] * kfs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] end - if @inbounds rxnarray[6,rxnind] == 0 - @fastmath @inbounds rderiv = kfs[rxnind]*krevs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[5,rxnind]] - elseif @inbounds rxnarray[7,rxnind] == 0 - @fastmath @inbounds rderiv = kfs[rxnind]*krevs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - elseif @inbounds rxnarray[8,rxnind] == 0 - @fastmath @inbounds rderiv = kfs[rxnind]*krevs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] + if @inbounds rxnarray[6, rxnind] == 0 + @fastmath @inbounds rderiv = kfs[rxnind] * krevs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[5, rxnind]] + elseif @inbounds rxnarray[7, rxnind] == 0 + @fastmath @inbounds rderiv = kfs[rxnind] * krevs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + elseif @inbounds rxnarray[8, rxnind] == 0 + @fastmath @inbounds rderiv = kfs[rxnind] * krevs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] else - @fastmath @inbounds rderiv = kfs[rxnind]*krevs[rxnind]/(p[Nspcs+rxnind]*p[Nspcs+rxnind])*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] + @fastmath @inbounds rderiv = kfs[rxnind] * krevs[rxnind] / (p[Nspcs+rxnind] * p[Nspcs+rxnind]) * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] end - @fastmath flux = fderiv-rderiv - _spreadreactantpartials!(jacp,flux,rxnarray,rxnind,Nspcs+rxnind) - _spreadproductpartials!(jacp,-flux,rxnarray,rxnind,Nspcs+rxnind) - - @fastmath @inbounds gderiv = rderiv*(p[Nspcs+rxnind]*p[Nspcs+rxnind])/kfs[rxnind]*RTinv - - @inbounds jacp[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[1,rxnind]) - if @inbounds rxnarray[2,rxnind] !== 0 - @inbounds jacp[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[2,rxnind]) - if @inbounds rxnarray[3,rxnind] !== 0 - @inbounds jacp[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[3,rxnind]) - if @inbounds rxnarray[4,rxnind] !== 0 - @inbounds jacp[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[3,rxnind]] -= gderiv - @inbounds jacp[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[3,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds jacp[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= gderiv - @inbounds _spreadreactantpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[4,rxnind]) + @fastmath flux = fderiv - rderiv + _spreadreactantpartials!(jacp, flux, rxnarray, rxnind, Nspcs + rxnind) + _spreadproductpartials!(jacp, -flux, rxnarray, rxnind, Nspcs + rxnind) + + @fastmath @inbounds gderiv = rderiv * (p[Nspcs+rxnind] * p[Nspcs+rxnind]) / kfs[rxnind] * RTinv + + @inbounds jacp[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[1, rxnind]) + if @inbounds rxnarray[2, rxnind] !== 0 + @inbounds jacp[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[2, rxnind]) + if @inbounds rxnarray[3, rxnind] !== 0 + @inbounds jacp[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[3, rxnind]) + if @inbounds rxnarray[4, rxnind] !== 0 + @inbounds jacp[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[3, rxnind]] -= gderiv + @inbounds jacp[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[3, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds jacp[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= gderiv + @inbounds _spreadreactantpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[4, rxnind]) end end end - @inbounds jacp[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[5,rxnind]) - if @inbounds rxnarray[6,rxnind] !== 0 - @inbounds jacp[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[6,rxnind]) - if @inbounds rxnarray[7,rxnind] !== 0 - @inbounds jacp[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[7,rxnind]) - if @inbounds rxnarray[8,rxnind] !== 0 - @inbounds jacp[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[7,rxnind]] -= gderiv - @inbounds jacp[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[7,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds jacp[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= gderiv - @inbounds _spreadproductpartials!(jacp,gderiv,rxnarray,rxnind,rxnarray[8,rxnind]) + @inbounds jacp[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[5, rxnind]) + if @inbounds rxnarray[6, rxnind] !== 0 + @inbounds jacp[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[6, rxnind]) + if @inbounds rxnarray[7, rxnind] !== 0 + @inbounds jacp[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[7, rxnind]) + if @inbounds rxnarray[8, rxnind] !== 0 + @inbounds jacp[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[7, rxnind]] -= gderiv + @inbounds jacp[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[7, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds jacp[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= gderiv + @inbounds _spreadproductpartials!(jacp, gderiv, rxnarray, rxnind, rxnarray[8, rxnind]) end end end @@ -3203,114 +3373,114 @@ end jacp .*= V end -function jacobianp!(jacp::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTPDomain,ParametrizedTPDomain}} - jacp.=0.0 - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) +function jacobianp!(jacp::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTPDomain,ParametrizedTPDomain}} + jacp .= 0.0 + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) - Nspcs=length(cs) + Nspcs = length(cs) Nrxns = size(domain.rxnarray)[2] - jacobianpnsderiv!(jacp,y,p,t,domain,domain.rxnarray,cs,T,V,kfs,krevs,Nspcs,Nrxns) + jacobianpnsderiv!(jacp, y, p, t, domain, domain.rxnarray, cs, T, V, kfs, krevs, Nspcs, Nrxns) @simd for i in 1:length(p) - @views @inbounds @fastmath jacp[domain.indexes[3],i] = sum(jacp[domain.indexes[1]:domain.indexes[2],i])*R*T/P + @views @inbounds @fastmath jacp[domain.indexes[3], i] = sum(jacp[domain.indexes[1]:domain.indexes[2], i]) * R * T / P end @simd for ind in domain.constantspeciesinds - @inbounds jacp[ind,:] .= 0. + @inbounds jacp[ind, :] .= 0.0 end end -function jacobianp!(jacp::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantVDomain,ParametrizedVDomain}} - jacp.=0.0 - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) +function jacobianp!(jacp::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantVDomain,ParametrizedVDomain}} + jacp .= 0.0 + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) Nspcs = length(cs) Nrxns = size(domain.rxnarray)[2] dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V - jacobianpnsderiv!(jacp,y,p,t,domain,domain.rxnarray,cs,T,V,kfs,krevs,Nspcs,Nrxns) + jacobianpnsderiv!(jacp, y, p, t, domain, domain.rxnarray, cs, T, V, kfs, krevs, Nspcs, Nrxns) @simd for i in 1:Nspcs - @views @fastmath @inbounds jacp[domain.indexes[3],i] = -(dot(Us,jacp[domain.indexes[1]:domain.indexes[2],i])+dydt[i])/(N*Cvave) - @views @fastmath @inbounds jacp[domain.indexes[4],i] = sum(jacp[domain.indexes[1]:domain.indexes[2],i])*R*T/V + P/T*jacp[domain.indexes[3],i] + @views @fastmath @inbounds jacp[domain.indexes[3], i] = -(dot(Us, jacp[domain.indexes[1]:domain.indexes[2], i]) + dydt[i]) / (N * Cvave) + @views @fastmath @inbounds jacp[domain.indexes[4], i] = sum(jacp[domain.indexes[1]:domain.indexes[2], i]) * R * T / V + P / T * jacp[domain.indexes[3], i] end @simd for i in Nspcs+1:Nspcs+Nrxns - @views @fastmath @inbounds jacp[domain.indexes[3],i] = -dot(Us,jacp[domain.indexes[1]:domain.indexes[2],i])/(N*Cvave) - @views @fastmath @inbounds jacp[domain.indexes[4],i] = sum(jacp[domain.indexes[1]:domain.indexes[2],i])*R*T/V + P/T*jacp[domain.indexes[3],i] + @views @fastmath @inbounds jacp[domain.indexes[3], i] = -dot(Us, jacp[domain.indexes[1]:domain.indexes[2], i]) / (N * Cvave) + @views @fastmath @inbounds jacp[domain.indexes[4], i] = sum(jacp[domain.indexes[1]:domain.indexes[2], i]) * R * T / V + P / T * jacp[domain.indexes[3], i] end @simd for ind in domain.constantspeciesinds - @inbounds jacp[ind,:] .= 0. + @inbounds jacp[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain flow = inter.F(t) @simd for i in 1:Nspcs - ddGidTdt = flow*(-ns[i]/N)/(N*Cvave) - jacp[domain.indexes[3],i] += ddGidTdt - jacp[domain.indexes[4],i] += P/T*ddGidTdt + ddGidTdt = flow * (-ns[i] / N) / (N * Cvave) + jacp[domain.indexes[3], i] += ddGidTdt + jacp[domain.indexes[4], i] += P / T * ddGidTdt end end end end -function jacobianp!(jacp::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantPDomain,ParametrizedPDomain}} - jacp.=0.0 - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) +function jacobianp!(jacp::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantPDomain,ParametrizedPDomain}} + jacp .= 0.0 + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) Nspcs = length(cs) Nrxns = size(domain.rxnarray)[2] dydt = zeros(size(y)) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V - jacobianpnsderiv!(jacp,y,p,t,domain,domain.rxnarray,cs,T,V,kfs,krevs,Nspcs,Nrxns) + jacobianpnsderiv!(jacp, y, p, t, domain, domain.rxnarray, cs, T, V, kfs, krevs, Nspcs, Nrxns) - @fastmath Cpave = Cvave+R + @fastmath Cpave = Cvave + R @simd for i in 1:Nspcs - @views @fastmath @inbounds jacp[domain.indexes[3],i] = -(dot(Hs,jacp[domain.indexes[1]:domain.indexes[2],i])+dydt[i])/(N*Cpave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds jacp[domain.indexes[4],i] = sum(jacp[domain.indexes[1]:domain.indexes[2],i])*R*T/P + jacp[domain.indexes[3],i]*V/T + @views @fastmath @inbounds jacp[domain.indexes[3], i] = -(dot(Hs, jacp[domain.indexes[1]:domain.indexes[2], i]) + dydt[i]) / (N * Cpave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds jacp[domain.indexes[4], i] = sum(jacp[domain.indexes[1]:domain.indexes[2], i]) * R * T / P + jacp[domain.indexes[3], i] * V / T end @simd for i in Nspcs+1:Nspcs+Nrxns - @views @fastmath @inbounds jacp[domain.indexes[3],i] = -(dot(Hs,jacp[domain.indexes[1]:domain.indexes[2],i]))/(N*Cpave) #divide by V to cancel ωV to ω - @views @fastmath @inbounds jacp[domain.indexes[4],i] = sum(jacp[domain.indexes[1]:domain.indexes[2],i])*R*T/P + jacp[domain.indexes[3],i]*V/T + @views @fastmath @inbounds jacp[domain.indexes[3], i] = -(dot(Hs, jacp[domain.indexes[1]:domain.indexes[2], i])) / (N * Cpave) #divide by V to cancel ωV to ω + @views @fastmath @inbounds jacp[domain.indexes[4], i] = sum(jacp[domain.indexes[1]:domain.indexes[2], i]) * R * T / P + jacp[domain.indexes[3], i] * V / T end @simd for ind in domain.constantspeciesinds - @inbounds jacp[ind,:] .= 0. + @inbounds jacp[ind, :] .= 0.0 end @simd for inter in interfaces - if isa(inter,Inlet) && domain == inter.domain + if isa(inter, Inlet) && domain == inter.domain flow = inter.F(t) for i in 1:Nspcs - ddGidTdt = flow*(- ns[i]/N)/(N*Cpave) - jacp[domain.indexes[3],i] += ddGidTdt - jacp[domain.indexes[4],i] += ddGidTdt*V/T + ddGidTdt = flow * (-ns[i] / N) / (N * Cpave) + jacp[domain.indexes[3], i] += ddGidTdt + jacp[domain.indexes[4], i] += ddGidTdt * V / T end end end end -function jacobianp!(jacp::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain,ParametrizedTConstantVDomain,ConstantTAPhiDomain}} - jacp.=0.0 - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) +function jacobianp!(jacp::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:Union{ConstantTVDomain,ParametrizedTConstantVDomain,ConstantTAPhiDomain}} + jacp .= 0.0 + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR = calcthermo(domain, y, t, p) Nspcs = length(cs) Nrxns = size(domain.rxnarray)[2] - jacobianpnsderiv!(jacp,y,p,t,domain,domain.rxnarray,cs,T,V,kfs,krevs,Nspcs,Nrxns) + jacobianpnsderiv!(jacp, y, p, t, domain, domain.rxnarray, cs, T, V, kfs, krevs, Nspcs, Nrxns) @simd for ind in domain.constantspeciesinds - @inbounds jacp[ind,:] .= 0. + @inbounds jacp[ind, :] .= 0.0 end end @@ -3321,26 +3491,26 @@ function getreactionindices(ig::Q) where {Q<:AbstractPhase} end export getreactionindices -@inline function getsensspcsrxns(domain::D,ind::Int64) where {D<:AbstractDomain} +@inline function getsensspcsrxns(domain::D, ind::Int64) where {D<:AbstractDomain} sensspcinds = Array{Int64,1}() sensrxninds = Array{Int64,1}() for rxnind in 1:size(domain.rxnarray)[2] - if ind in @inbounds @view domain.rxnarray[:,rxnind] - for spcind in @inbounds @view domain.rxnarray[:,rxnind] + if ind in @inbounds @view domain.rxnarray[:, rxnind] + for spcind in @inbounds @view domain.rxnarray[:, rxnind] if !(spcind in sensspcinds) && (spcind !== 0) - push!(sensspcinds,spcind) + push!(sensspcinds, spcind) end end - push!(sensrxninds,rxnind) + push!(sensrxninds, rxnind) end end - sensrxns = Array{ElementaryReaction,1}(undef,length(sensrxninds)) - sensspcs = Array{Species,1}(undef,length(sensspcinds)) - sensspcnames = Array{String,1}(undef,length(sensspcinds)) - senstooriginspcind = Array{Int64,1}(undef,length(sensspcinds)) - senstooriginrxnind = Array{Int64,1}(undef,length(sensrxninds)) - for (i,spcind) in enumerate(sensspcinds) + sensrxns = Array{ElementaryReaction,1}(undef, length(sensrxninds)) + sensspcs = Array{Species,1}(undef, length(sensspcinds)) + sensspcnames = Array{String,1}(undef, length(sensspcinds)) + senstooriginspcind = Array{Int64,1}(undef, length(sensspcinds)) + senstooriginrxnind = Array{Int64,1}(undef, length(sensrxninds)) + for (i, spcind) in enumerate(sensspcinds) spc = getphasespecies(domain.phase)[spcind] sensspcnames[i] = spc.name @inbounds sensspcs[i] = Species( @@ -3369,16 +3539,16 @@ export getreactionindices reactants = Array{Species,1}() reactantinds = Array{Int64,1}() @simd for reactant in rxn.reactants - ind = findfirst(isequal(reactant.name),sensspcnames) - @inbounds push!(reactants,sensspcs[ind]) - push!(reactantinds,ind) + ind = findfirst(isequal(reactant.name), sensspcnames) + @inbounds push!(reactants, sensspcs[ind]) + push!(reactantinds, ind) end products = Array{Species,1}() productinds = Array{Int64,1}() @simd for product in rxn.products - ind = findfirst(isequal(product.name),sensspcnames) - @inbounds push!(products,sensspcs[ind]) - push!(productinds,ind) + ind = findfirst(isequal(product.name), sensspcnames) + @inbounds push!(products, sensspcs[ind]) + push!(productinds, ind) end @inbounds sensrxns[i] = ElementaryReaction( @@ -3394,32 +3564,32 @@ export getreactionindices @inbounds senstooriginrxnind[i] = rxnind end - return sensspcs,sensrxns,sensspcnames,senstooriginspcind,senstooriginrxnind + return sensspcs, sensrxns, sensspcnames, senstooriginspcind, senstooriginrxnind end -@inline function getsensdomain(domain::D,ind::Int64) where {D<:AbstractDomain} +@inline function getsensdomain(domain::D, ind::Int64) where {D<:AbstractDomain} - sensspcs,sensrxns,sensspcnames,senstooriginspcind,senstooriginrxnind = getsensspcsrxns(domain,ind) + sensspcs, sensrxns, sensspcnames, senstooriginspcind, senstooriginrxnind = getsensspcsrxns(domain, ind) initialconds = Dict{String,Float64}() for fn in fieldnames(typeof(domain)) if fn in (:T, :P, :V) - initialconds["$fn"] = getfield(domain,fn) + initialconds["$fn"] = getfield(domain, fn) end end - d = split(repr(typeof(domain)),"{")[1] + d = split(repr(typeof(domain)), "{")[1] - if occursin(".",d) - d = Symbol(split(d,".")[2]) + if occursin(".", d) + d = Symbol(split(d, ".")[2]) else d = Symbol(d) end - if isa(domain.phase,IdealGas) - return eval(d)(phase=IdealGas(sensspcs,sensrxns,name="phase"),initialconds=initialconds)[1],sensspcnames,senstooriginspcind,senstooriginrxnind + if isa(domain.phase, IdealGas) + return eval(d)(phase=IdealGas(sensspcs, sensrxns, name="phase"), initialconds=initialconds)[1], sensspcnames, senstooriginspcind, senstooriginrxnind else - return eval(d)(phase=IdealDiluteSolution(sensspcs,sensrxns,domain.phase.solvent,name="phase"),initialconds=initialconds)[1],sensspcnames,senstooriginspcind,senstooriginrxnind + return eval(d)(phase=IdealDiluteSolution(sensspcs, sensrxns, domain.phase.solvent, name="phase"), initialconds=initialconds)[1], sensspcnames, senstooriginspcind, senstooriginrxnind end end diff --git a/src/Interface.jl b/src/Interface.jl index b4dd8727..466022ab 100644 --- a/src/Interface.jl +++ b/src/Interface.jl @@ -34,36 +34,36 @@ struct ReactiveInternalInterface{T,B,C,C2,N,Q<:AbstractReaction,X} <: AbstractRe reversibililty::Array{Bool,1} forwardability::Array{Bool,1} end -function ReactiveInternalInterface(domain1,domain2,reactions,A) - vectuple,vecinds,otherrxns,otherrxninds,posinds = getveckinetics(reactions) - rxns = vcat(reactions[vecinds],reactions[otherrxninds]) - rxns = [ElementaryReaction(index=i,reactants=rxn.reactants,reactantinds=rxn.reactantinds,products=rxn.products, - productinds=rxn.productinds,kinetics=rxn.kinetics,radicalchange=rxn.radicalchange,reversible=rxn.reversible,forwardable=rxn.forwardable,pairs=rxn.pairs) for (i,rxn) in enumerate(rxns)] - rxnarray = getinterfacereactioninds(domain1,domain2,rxns) - M,Nrp1,Nrp2 = getstoichmatrix(domain1,domain2,reactions) - reversibility = Array{Bool,1}(getfield.(rxns,:reversible)) - forwardability = Array{Bool,1}(getfield.(rxns,:forwardable)) - return ReactiveInternalInterface(domain1,domain2, - rxns,vectuple,posinds,rxnarray,M,Nrp1,Nrp2,A,[1,length(reactions)], - [0,1],ones(length(rxns)),reversibility,forwardability),ones(length(rxns)) +function ReactiveInternalInterface(domain1, domain2, reactions, A) + vectuple, vecinds, otherrxns, otherrxninds, posinds = getveckinetics(reactions) + rxns = vcat(reactions[vecinds], reactions[otherrxninds]) + rxns = [ElementaryReaction(index=i, reactants=rxn.reactants, reactantinds=rxn.reactantinds, products=rxn.products, + productinds=rxn.productinds, kinetics=rxn.kinetics, radicalchange=rxn.radicalchange, reversible=rxn.reversible, forwardable=rxn.forwardable, pairs=rxn.pairs) for (i, rxn) in enumerate(rxns)] + rxnarray = getinterfacereactioninds(domain1, domain2, rxns) + M, Nrp1, Nrp2 = getstoichmatrix(domain1, domain2, reactions) + reversibility = Array{Bool,1}(getfield.(rxns, :reversible)) + forwardability = Array{Bool,1}(getfield.(rxns, :forwardable)) + return ReactiveInternalInterface(domain1, domain2, + rxns, vectuple, posinds, rxnarray, M, Nrp1, Nrp2, A, [1, length(reactions)], + [0, 1], ones(length(rxns)), reversibility, forwardability), ones(length(rxns)) end export ReactiveInternalInterface -function getkfskrevs(ri::ReactiveInternalInterface,T1,T2,phi1,phi2,Gs1,Gs2,cstot::Array{Q,1}) where {Q} - kfs = getkfs(ri,T1,0.0,0.0,Array{Q,1}(),ri.A,phi1) - Kc = getKc.(ri.reactions,ri.domain1.phase,ri.domain2.phase,Ref(Gs1),Ref(Gs2),T1,phi1) - krevs = kfs./Kc - return kfs,krevs +function getkfskrevs(ri::ReactiveInternalInterface, T1, T2, phi1, phi2, Gs1, Gs2, cstot::Array{Q,1}) where {Q} + kfs = getkfs(ri, T1, 0.0, 0.0, Array{Q,1}(), ri.A, phi1) + Kc = getKc.(ri.reactions, ri.domain1.phase, ri.domain2.phase, Ref(Gs1), Ref(Gs2), T1, phi1) + krevs = kfs ./ Kc + return kfs, krevs end -function evaluate(ri::ReactiveInternalInterface,dydt,domains,T1,T2,phi1,phi2,Gs1,Gs2,cstot,p::W) where {W<:SciMLBase.NullParameters} - kfs,krevs = getkfskrevs(ri,T1,T2,phi1,phi2,Gs1,Gs2,cstot) - addreactionratecontributions!(dydt,ri.rxnarray,cstot,kfs,krevs,ri.A) +function evaluate(ri::ReactiveInternalInterface, dydt, domains, T1, T2, phi1, phi2, Gs1, Gs2, cstot, p::W) where {W<:SciMLBase.NullParameters} + kfs, krevs = getkfskrevs(ri, T1, T2, phi1, phi2, Gs1, Gs2, cstot) + addreactionratecontributions!(dydt, ri.rxnarray, cstot, kfs, krevs, ri.A) end -function evaluate(ri::ReactiveInternalInterface,dydt,domains,T1,T2,phi1,phi2,Gs1,Gs2,cstot,p) - kfs,krevs = getkfskrevs(ri,T1,T2,phi1,phi2,Gs1,Gs2,cstot) - addreactionratecontributions!(dydt,ri.rxnarray,cstot,kfs.*p[ri.parameterindexes[1]:ri.parameterindexes[2]],krevs.*p[ri.parameterindexes[1]:ri.parameterindexes[2]],ri.A) +function evaluate(ri::ReactiveInternalInterface, dydt, domains, T1, T2, phi1, phi2, Gs1, Gs2, cstot, p) + kfs, krevs = getkfskrevs(ri, T1, T2, phi1, phi2, Gs1, Gs2, cstot) + addreactionratecontributions!(dydt, ri.rxnarray, cstot, kfs .* p[ri.parameterindexes[1]:ri.parameterindexes[2]], krevs .* p[ri.parameterindexes[1]:ri.parameterindexes[2]], ri.A) end export evaluate @@ -86,130 +86,130 @@ struct ReactiveInternalInterfaceConstantTPhi{J,N,B,B2,B3,C,C2,Q<:AbstractReactio reversibility::Array{Bool,1} forwardability::Array{Bool,1} end -function ReactiveInternalInterfaceConstantTPhi(domain1,domain2,reactions,T,A,phi=0.0) - @assert domain1.T == domain2.T - reactions = upgradekinetics(reactions,domain1,domain2) - rxnarray = getinterfacereactioninds(domain1,domain2,reactions) - kfs = getkf.(reactions,nothing,T,0.0,0.0,Ref([]),A,phi) - Kc = getKc.(reactions,domain1.phase,domain2.phase,Ref(domain1.Gs),Ref(domain2.Gs),T,phi) - krevs = kfs./Kc - M,Nrp1,Nrp2 = getstoichmatrix(domain1,domain2,reactions) - reversibility = Array{Bool,1}(getfield.(reactions,:reversible)) - forwardability = Array{Bool,1}(getfield.(reactions,:forwardable)) - if isa(reactions,Vector{Any}) - reactions = convert(Vector{ElementaryReaction},reactions) +function ReactiveInternalInterfaceConstantTPhi(domain1, domain2, reactions, T, A, phi=0.0) + @assert domain1.T == domain2.T + reactions = upgradekinetics(reactions, domain1, domain2) + rxnarray = getinterfacereactioninds(domain1, domain2, reactions) + kfs = getkf.(reactions, nothing, T, 0.0, 0.0, Ref([]), A, phi) + Kc = getKc.(reactions, domain1.phase, domain2.phase, Ref(domain1.Gs), Ref(domain2.Gs), T, phi) + krevs = kfs ./ Kc + M, Nrp1, Nrp2 = getstoichmatrix(domain1, domain2, reactions) + reversibility = Array{Bool,1}(getfield.(reactions, :reversible)) + forwardability = Array{Bool,1}(getfield.(reactions, :forwardable)) + if isa(reactions, Vector{Any}) + reactions = convert(Vector{ElementaryReaction}, reactions) end - if isa(kfs,Vector{Any}) - kfs = convert(Vector{Float64},kfs) + if isa(kfs, Vector{Any}) + kfs = convert(Vector{Float64}, kfs) end - return ReactiveInternalInterfaceConstantTPhi(domain1,domain2,reactions, - rxnarray,M,Nrp1,Nrp2,kfs,krevs,T,A,[1,length(reactions)], - [0,1],kfs[1:end],reversibility,forwardability),kfs[1:end] + return ReactiveInternalInterfaceConstantTPhi(domain1, domain2, reactions, + rxnarray, M, Nrp1, Nrp2, kfs, krevs, T, A, [1, length(reactions)], + [0, 1], kfs[1:end], reversibility, forwardability), kfs[1:end] end export ReactiveInternalInterfaceConstantTPhi -function getkfskrevs(ri::ReactiveInternalInterfaceConstantTPhi,T1,T2,phi1,phi2,Gs1,Gs2,cstot) - return ri.kfs,ri.krevs +function getkfskrevs(ri::ReactiveInternalInterfaceConstantTPhi, T1, T2, phi1, phi2, Gs1, Gs2, cstot) + return ri.kfs, ri.krevs end -function evaluate(ri::ReactiveInternalInterfaceConstantTPhi,dydt,domains,T1,T2,phi1,phi2,Gs1,Gs2,cstot,p::W) where {W<:SciMLBase.NullParameters} - addreactionratecontributions!(dydt,ri.rxnarray,cstot,ri.kfs,ri.krevs,ri.A) +function evaluate(ri::ReactiveInternalInterfaceConstantTPhi, dydt, domains, T1, T2, phi1, phi2, Gs1, Gs2, cstot, p::W) where {W<:SciMLBase.NullParameters} + addreactionratecontributions!(dydt, ri.rxnarray, cstot, ri.kfs, ri.krevs, ri.A) end -function evaluate(ri::ReactiveInternalInterfaceConstantTPhi,dydt,domains,T1,T2,phi1,phi2,Gs1,Gs2,cstot,p) +function evaluate(ri::ReactiveInternalInterfaceConstantTPhi, dydt, domains, T1, T2, phi1, phi2, Gs1, Gs2, cstot, p) if p[ri.parameterindexes[1]:ri.parameterindexes[2]] == ri.kfs kfs = ri.kfs - else + else kfs = p[ri.parameterindexes[1]:ri.parameterindexes[2]] end if length(Gs1) == 0 || length(Gs2) == 0 || (all(Gs1 .== ri.domain1.Gs) && all(Gs2 .== ri.domain2.Gs)) krevs = ri.krevs else - Kc = getKcs(ri,T1,Gs1,Gs2) - krevs = kfs./Kc + Kc = getKcs(ri, T1, Gs1, Gs2) + krevs = kfs ./ Kc end - addreactionratecontributions!(dydt,ri.rxnarray,cstot,kfs,krevs,ri.A) + addreactionratecontributions!(dydt, ri.rxnarray, cstot, kfs, krevs, ri.A) end export evaluate """ construct the stochiometric matrix for the reactions crossing both domains and the reaction molecule # change """ -function getstoichmatrix(domain1,domain2,rxns) - M = spzeros(length(rxns),length(domain1.phase.species)+length(domain2.phase.species)) +function getstoichmatrix(domain1, domain2, rxns) + M = spzeros(length(rxns), length(domain1.phase.species) + length(domain2.phase.species)) Nrp1 = zeros(length(rxns)) Nrp2 = zeros(length(rxns)) N1 = length(domain1.phase.species) spcs1 = domain1.phase.species spcs2 = domain2.phase.species - for (i,rxn) in enumerate(rxns) + for (i, rxn) in enumerate(rxns) Nrp1[i] = Float64(length([x for x in rxn.products if x in spcs1]) - length([x for x in rxn.reactants if x in spcs1])) Nrp2[i] = Float64(length([x for x in rxn.products if x in spcs2]) - length([x for x in rxn.reactants if x in spcs2])) - for (j,r) in enumerate(rxn.reactants) + for (j, r) in enumerate(rxn.reactants) isfirst = true - ind = findfirst(isequal(r),domain1.phase.species) + ind = findfirst(isequal(r), domain1.phase.species) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - M[i,isfirst ? ind : ind+N1] += 1 + M[i, isfirst ? ind : ind + N1] += 1 end - for (j,r) in enumerate(rxn.products) + for (j, r) in enumerate(rxn.products) isfirst = true - ind = findfirst(isequal(r),domain1.phase.species) + ind = findfirst(isequal(r), domain1.phase.species) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - M[i,isfirst ? ind : ind+N1] -= 1 + M[i, isfirst ? ind : ind + N1] -= 1 end end - return M,Nrp1,Nrp2 + return M, Nrp1, Nrp2 end -function getinterfacereactioninds(domain1,domain2,reactions) - indices = zeros(Int64,(8,length(reactions))) +function getinterfacereactioninds(domain1, domain2, reactions) + indices = zeros(Int64, (8, length(reactions))) N1 = length(domain1.phase.species) - for (i,rxn) in enumerate(reactions) - for (j,r) in enumerate(rxn.reactants) + for (i, rxn) in enumerate(reactions) + for (j, r) in enumerate(rxn.reactants) isfirst = true - ind = findfirst(isequal(r),domain1.phase.species) + ind = findfirst(isequal(r), domain1.phase.species) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - indices[j,i] = isfirst ? ind : ind+N1 + indices[j, i] = isfirst ? ind : ind + N1 end - for (j,r) in enumerate(rxn.products) + for (j, r) in enumerate(rxn.products) isfirst = true - ind = findfirst(isequal(r),domain1.phase.species) + ind = findfirst(isequal(r), domain1.phase.species) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - indices[j+4,i] = isfirst ? ind : ind+N1 + indices[j+4, i] = isfirst ? ind : ind + N1 end end return indices end -function upgradekinetics(rxns,domain1,domain2) - domain1surf = hasproperty(domain1.phase,:sitedensity) - domain2surf = hasproperty(domain2.phase,:sitedensity) +function upgradekinetics(rxns, domain1, domain2) + domain1surf = hasproperty(domain1.phase, :sitedensity) + domain2surf = hasproperty(domain2.phase, :sitedensity) @assert !(domain1surf && domain2surf) if domain1surf surfdomain = domain1 elseif domain2surf surfdomain = domain2 end - newrxns = Array{ElementaryReaction,1}(undef,length(rxns)) - for (i,rxn) in enumerate(rxns) - if isa(rxn.kinetics,StickingCoefficient) + newrxns = Array{ElementaryReaction,1}(undef, length(rxns)) + for (i, rxn) in enumerate(rxns) + if isa(rxn.kinetics, StickingCoefficient) spc = [spc for spc in rxn.reactants if !(spc in surfdomain.phase.species)] @assert length(spc) == 1 - kin = stickingcoefficient2arrhenius(rxn.kinetics,surfdomain.phase.sitedensity,length(rxn.reactants)-1,spc[1].molecularweight) - newrxns[i] = ElementaryReaction(index=rxn.index,reactants=rxn.reactants,reactantinds=rxn.reactantinds,products=rxn.products, - productinds=rxn.productinds,kinetics=kin,radicalchange=rxn.radicalchange,reversible=rxn.reversible,forwardable=rxn.forwardable,pairs=rxn.pairs) + kin = stickingcoefficient2arrhenius(rxn.kinetics, surfdomain.phase.sitedensity, length(rxn.reactants) - 1, spc[1].molecularweight) + newrxns[i] = ElementaryReaction(index=rxn.index, reactants=rxn.reactants, reactantinds=rxn.reactantinds, products=rxn.products, + productinds=rxn.productinds, kinetics=kin, radicalchange=rxn.radicalchange, reversible=rxn.reversible, forwardable=rxn.forwardable, pairs=rxn.pairs) else newrxns[i] = rxn end @@ -217,18 +217,18 @@ function upgradekinetics(rxns,domain1,domain2) return [rxn for rxn in newrxns] end -function stickingcoefficient2arrhenius(sc,sitedensity,N,mw;Tmin=300.0,Tmax=2000.0) - mass = mw/Na - ksc(T) = sc(T)/sitedensity^N*sqrt(kB*T/(2.0*pi*mass)) - Ts = Array{Float64,1}(Tmin:10:Tmax); +function stickingcoefficient2arrhenius(sc, sitedensity, N, mw; Tmin=300.0, Tmax=2000.0) + mass = mw / Na + ksc(T) = sc(T) / sitedensity^N * sqrt(kB * T / (2.0 * pi * mass)) + Ts = Array{Float64,1}(Tmin:10:Tmax) kscvals = ksc.(Ts) - k(T,p) = log.(abs(p[1]).*T.^p[2].*exp.(-p[3]./(R.*T))) - p0 = [sc.A/sitedensity*sqrt(kB*1000.0/(2.0*pi*mass)),0.5,sc.Ea] - fit = curve_fit(k,Ts,log.(kscvals),p0;x_tol=1e-18) + k(T, p) = log.(abs(p[1]) .* T .^ p[2] .* exp.(-p[3] ./ (R .* T))) + p0 = [sc.A / sitedensity * sqrt(kB * 1000.0 / (2.0 * pi * mass)), 0.5, sc.Ea] + fit = curve_fit(k, Ts, log.(kscvals), p0; x_tol=1e-18) @assert fit.converged p = fit.param p[1] = abs(p[1]) - return Arrhenius(;A=p[1],n=p[2],Ea=p[3]) + return Arrhenius(; A=p[1], n=p[2], Ea=p[3]) end struct Inlet{Q<:Real,S,V<:AbstractArray,U<:Real,X<:Real,FF<:Function} <: AbstractBoundaryInterface @@ -240,27 +240,27 @@ struct Inlet{Q<:Real,S,V<:AbstractArray,U<:Real,X<:Real,FF<:Function} <: Abstrac H::Q end -function Inlet(domain::V,conddict::Dict{X1,X},F::FF) where {V,X1,X,B<:Real,FF<:Function} +function Inlet(domain::V, conddict::Dict{X1,X}, F::FF) where {V,X1,X,B<:Real,FF<:Function} T = 0.0 P = 0.0 - y = makespcsvector(domain.phase,conddict) - yout = y./sum(y) + y = makespcsvector(domain.phase, conddict) + yout = y ./ sum(y) - if haskey(conddict,"T") + if haskey(conddict, "T") T = conddict["T"] end - if haskey(conddict,"P") + if haskey(conddict, "P") P = conddict["P"] end - if haskey(conddict,"Hin") + if haskey(conddict, "Hin") H = conddict["Hin"] else @assert T != 0.0 && P != 0.0 "T and P need to be provided if Hin is not provided for Inlet" - H = dot(getEnthalpy.(getfield.(domain.phase.species,:thermo),T),yout) + H = dot(getEnthalpy.(getfield.(domain.phase.species, :thermo), T), yout) end - return Inlet(domain,yout,F,T,P,H) + return Inlet(domain, yout, F, T, P, H) end export Inlet @@ -291,28 +291,28 @@ struct kLAkHCondensationEvaporationWithReservoir{S,V1<:AbstractArray,V2<:Real,V3 kHs::V8 end -function kLAkHCondensationEvaporationWithReservoir(domain::D,conddict::Dict{X1,X}) where {D,X1,X} - y = makespcsvector(domain.phase,conddict) - molefractions = y./sum(y) +function kLAkHCondensationEvaporationWithReservoir(domain::D, conddict::Dict{X1,X}) where {D,X1,X} + y = makespcsvector(domain.phase, conddict) + molefractions = y ./ sum(y) T = conddict["T"] - H = dot(getEnthalpy.(getfield.(domain.phase.species,:thermo),T),molefractions) - kLAs = [T -> kLA(T=T) for kLA in getfield.(domain.phase.species,:liquidvolumetricmasstransfercoefficient)] - kHs = [T -> kH(T=T) for kH in getfield.(domain.phase.species,:henrylawconstant)] + H = dot(getEnthalpy.(getfield.(domain.phase.species, :thermo), T), molefractions) + kLAs = [T -> kLA(T=T) for kLA in getfield.(domain.phase.species, :liquidvolumetricmasstransfercoefficient)] + kHs = [T -> kH(T=T) for kH in getfield.(domain.phase.species, :henrylawconstant)] - if isa(domain.phase,IdealDiluteSolution) - if !haskey(conddict,"P") + if isa(domain.phase, IdealDiluteSolution) + if !haskey(conddict, "P") @error "P needs to be specified for the vapor resevoir over the liquid phase domain" end P = conddict["P"] - V = sum(y)*R*T/P - return kLAkHCondensationEvaporationWithReservoir(domain,molefractions,T,P,V,Array{Float64,1}(),H,kLAs,kHs) - elseif isa(domain.phase,IdealGas) - if !haskey(conddict,"V") + V = sum(y) * R * T / P + return kLAkHCondensationEvaporationWithReservoir(domain, molefractions, T, P, V, Array{Float64,1}(), H, kLAs, kHs) + elseif isa(domain.phase, IdealGas) + if !haskey(conddict, "V") @error "V needs to be specified for the liquid resevoir under the gas phase domain" end V = conddict["V"] - cs = y./V - return kLAkHCondensationEvaporationWithReservoir(domain,Array{Float64,1}(),T,1e8,V,cs,H,kLAs,kHs) + cs = y ./ V + return kLAkHCondensationEvaporationWithReservoir(domain, Array{Float64,1}(), T, 1e8, V, cs, H, kLAs, kHs) end end @@ -338,26 +338,26 @@ VolumetricFlowRateInlet adds a volumetric flow rate inlet to a domain. conddict should provide the species composition in concentration, T, and P. Hin is the inlet enthalpy per mol (J/mol) and is optional, if not provided, it will be calculated from T and P. """ -function VolumetricFlowRateInlet(domain::V,conddict::Dict{X1,X},Vin::FF) where {V,X1,X,FF<:Function} +function VolumetricFlowRateInlet(domain::V, conddict::Dict{X1,X}, Vin::FF) where {V,X1,X,FF<:Function} T = 0.0 P = 0.0 - cs = makespcsvector(domain.phase,conddict) + cs = makespcsvector(domain.phase, conddict) - if haskey(conddict,"T") + if haskey(conddict, "T") T = conddict["T"] end - if haskey(conddict,"P") + if haskey(conddict, "P") P = conddict["P"] end - if haskey(conddict,"Hin") - Hpervolume = conddict["Hin"]/sum(cs) # convert to J/m3 + if haskey(conddict, "Hin") + Hpervolume = conddict["Hin"] / sum(cs) # convert to J/m3 else @assert T != 0.0 && P != 0.0 "T and P need to be provided if Hin is not provided for Inlet" - Hpervolume = dot(getEnthalpy.(getfield.(domain.phase.species,:thermo),T),cs) # J/m3 + Hpervolume = dot(getEnthalpy.(getfield.(domain.phase.species, :thermo), T), cs) # J/m3 end - return VolumetricFlowRateInlet(domain,cs,Vin,T,P,Hpervolume) + return VolumetricFlowRateInlet(domain, cs, Vin, T, P, Hpervolume) end export VolumetricFlowRateInlet @@ -385,27 +385,27 @@ struct VaporLiquidMassTransferInternalInterfaceConstantT{D1,D2,B} <: AbstractInt p::Array{Float64,1} end -function VaporLiquidMassTransferInternalInterfaceConstantT(domaingas,domainliq,ignoremasstransferspcnames) - @assert isa(domaingas.phase,IdealGas) - @assert isa(domainliq.phase,IdealDiluteSolution) - @assert getfield.(domaingas.phase.species,:name) == getfield.(domainliq.phase.species,:name) +function VaporLiquidMassTransferInternalInterfaceConstantT(domaingas, domainliq, ignoremasstransferspcnames) + @assert isa(domaingas.phase, IdealGas) + @assert isa(domainliq.phase, IdealDiluteSolution) + @assert getfield.(domaingas.phase.species, :name) == getfield.(domainliq.phase.species, :name) T = domainliq.T phase = domainliq.phase - ignoremasstransferspcinds = getinterfaceignoremasstransferspcinds(domaingas,domainliq,ignoremasstransferspcnames) - kLAs = [kLA(T=T) for kLA in getfield.(phase.species,:liquidvolumetricmasstransfercoefficient)] - kHs = [kH(T=T) for kH in getfield.(phase.species,:henrylawconstant)] - return VaporLiquidMassTransferInternalInterfaceConstantT(domaingas,domainliq,ignoremasstransferspcnames,ignoremasstransferspcinds,kLAs,kHs,[1,length(domainliq.phase.species)],[0,0],ones(length(domainliq.phase.species))),ones(length(domainliq.phase.species)) + ignoremasstransferspcinds = getinterfaceignoremasstransferspcinds(domaingas, domainliq, ignoremasstransferspcnames) + kLAs = [kLA(T=T) for kLA in getfield.(phase.species, :liquidvolumetricmasstransfercoefficient)] + kHs = [kH(T=T) for kH in getfield.(phase.species, :henrylawconstant)] + return VaporLiquidMassTransferInternalInterfaceConstantT(domaingas, domainliq, ignoremasstransferspcnames, ignoremasstransferspcinds, kLAs, kHs, [1, length(domainliq.phase.species)], [0, 0], ones(length(domainliq.phase.species))), ones(length(domainliq.phase.species)) end export VaporLiquidMassTransferInternalInterfaceConstantT -function getkLAkHs(vl::VaporLiquidMassTransferInternalInterfaceConstantT,Tgas,Tliq) +function getkLAkHs(vl::VaporLiquidMassTransferInternalInterfaceConstantT, Tgas, Tliq) return vl.kLAs, vl.kHs end -function evaluate(vl::VaporLiquidMassTransferInternalInterfaceConstantT,dydt,Vgas,Vliq,Tgas,Tliq,N1,N2,P1,P2,Cvave1,Cvave2,ns1,ns2,Us1,Us2,cstot,p) - kLAs, kHs = getkLAkHs(vl,Tgas,Tliq) - @views @inbounds @fastmath evap = kLAs*Vliq.*cstot[vl.domainliq.indexes[1]:vl.domainliq.indexes[2]] #evap_i = kLA_i * Vliq * cliq_i - @views @inbounds @fastmath cond = kLAs*Vliq.*cstot[vl.domaingas.indexes[1]:vl.domaingas.indexes[2]]*R*Tgas./kHs #cond_i = kLA_i * Vliq * Pgas_i / kH_i, Pgas_i = cgas_i * R * Tgas +function evaluate(vl::VaporLiquidMassTransferInternalInterfaceConstantT, dydt, Vgas, Vliq, Tgas, Tliq, N1, N2, P1, P2, Cvave1, Cvave2, ns1, ns2, Us1, Us2, cstot, p) + kLAs, kHs = getkLAkHs(vl, Tgas, Tliq) + @views @inbounds @fastmath evap = kLAs * Vliq .* cstot[vl.domainliq.indexes[1]:vl.domainliq.indexes[2]] #evap_i = kLA_i * Vliq * cliq_i + @views @inbounds @fastmath cond = kLAs * Vliq .* cstot[vl.domaingas.indexes[1]:vl.domaingas.indexes[2]] * R * Tgas ./ kHs #cond_i = kLA_i * Vliq * Pgas_i / kH_i, Pgas_i = cgas_i * R * Tgas netevap = (evap .- cond) @simd for ind in vl.ignoremasstransferspcinds @inbounds netevap[ind] = 0.0 @@ -415,12 +415,12 @@ function evaluate(vl::VaporLiquidMassTransferInternalInterfaceConstantT,dydt,Vga end export evaluate -function getinterfaceignoremasstransferspcinds(domaingas,domainliq,ignoremasstransferspcnames) - indices = zeros(Int64,length(ignoremasstransferspcnames)) - spcnamesliq = getfield.(domainliq.phase.species,:name) - for (i,name) in enumerate(ignoremasstransferspcnames) - indliq = findfirst(isequal(name),spcnamesliq) - indices[i] = domainliq.indexes[1]-1+indliq +function getinterfaceignoremasstransferspcinds(domaingas, domainliq, ignoremasstransferspcnames) + indices = zeros(Int64, length(ignoremasstransferspcnames)) + spcnamesliq = getfield.(domainliq.phase.species, :name) + for (i, name) in enumerate(ignoremasstransferspcnames) + indliq = findfirst(isequal(name), spcnamesliq) + indices[i] = domainliq.indexes[1] - 1 + indliq end return indices end @@ -445,79 +445,79 @@ struct FragmentBasedReactiveFilmGrowthInterfaceConstantT{D1,D2,Q<:AbstractReacti Mws::Array{Float64,1} end -function FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm,domain2,reactions) - @assert isa(domainfilm.phase,FragmentBasedIdealFilm) +function FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm, domain2, reactions) + @assert isa(domainfilm.phase, FragmentBasedIdealFilm) @assert domainfilm.T == domain2.T T = domainfilm.T - rxnarray, fragmentbasedrxnarray = getfragmentbasedinterfacereactioninds(domainfilm,domain2,reactions) - - kfs = getkf.(reactions,nothing,T,0.0,0.0,Ref([]),0.0,0.0) - Kc = getKc.(reactions,domainfilm.phase,domain2.phase,Ref(domainfilm.Gs),Ref(domain2.Gs),T,0.0) - krevs = kfs./Kc + rxnarray, fragmentbasedrxnarray = getfragmentbasedinterfacereactioninds(domainfilm, domain2, reactions) + + kfs = getkf.(reactions, nothing, T, 0.0, 0.0, Ref([]), 0.0, 0.0) + Kc = getKc.(reactions, domainfilm.phase, domain2.phase, Ref(domainfilm.Gs), Ref(domain2.Gs), T, 0.0) + krevs = kfs ./ Kc - M,Nrp1,Nrp2 = getstoichmatrix(domainfilm,domain2,reactions) - reversibility = Array{Bool,1}(getfield.(reactions,:reversible)) - forwardability = Array{Bool,1}(getfield.(reactions,:forwardable)) + M, Nrp1, Nrp2 = getstoichmatrix(domainfilm, domain2, reactions) + reversibility = Array{Bool,1}(getfield.(reactions, :reversible)) + forwardability = Array{Bool,1}(getfield.(reactions, :forwardable)) - Mws = vcat(getfield.(domainfilm.phase.fragments,:molecularweight),getfield.(domain2.phase.species,:molecularweight)) - return FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm,domain2,reactions,rxnarray,fragmentbasedrxnarray,M,Nrp1,Nrp2,kfs,krevs,T,[1,length(reactions)], - [0,1],kfs[1:end],reversibility,forwardability,Mws),kfs[1:end] + Mws = vcat(getfield.(domainfilm.phase.fragments, :molecularweight), getfield.(domain2.phase.species, :molecularweight)) + return FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm, domain2, reactions, rxnarray, fragmentbasedrxnarray, M, Nrp1, Nrp2, kfs, krevs, T, [1, length(reactions)], + [0, 1], kfs[1:end], reversibility, forwardability, Mws), kfs[1:end] end export FragmentBasedReactiveFilmGrowthInterfaceConstantT -function getfragmentbasedinterfacereactioninds(domainfilm,domain2,reactions) +function getfragmentbasedinterfacereactioninds(domainfilm, domain2, reactions) # find maximum number of species in fragment-based reaction and names of fragment-based species maxnumfragmentbasedreacprod = 0 for rxn in reactions numfragmentbasedreac = length(rxn.fragmentbasedreactants) numfragmentbasedprod = length(rxn.fragmentbasedproducts) - maxnumfragmentbasedreacprod = max(maxnumfragmentbasedreacprod,numfragmentbasedreac,numfragmentbasedprod) + maxnumfragmentbasedreacprod = max(maxnumfragmentbasedreacprod, numfragmentbasedreac, numfragmentbasedprod) end - fragmentbasedrxnarray = zeros(Int64,(maxnumfragmentbasedreacprod*2,length(reactions))) - rxnarray = zeros(Int64, (8,length(reactions))) + fragmentbasedrxnarray = zeros(Int64, (maxnumfragmentbasedreacprod * 2, length(reactions))) + rxnarray = zeros(Int64, (8, length(reactions))) N1 = length(domainfilm.phase.fragments) - for (i,rxn) in enumerate(reactions) - for (j,r) in enumerate(rxn.reactants) + for (i, rxn) in enumerate(reactions) + for (j, r) in enumerate(rxn.reactants) if !r.isfragmentintermediate isfirst = true - ind = findfirst(isequal(r),domainfilm.phase.fragments) + ind = findfirst(isequal(r), domainfilm.phase.fragments) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - rxnarray[j,i] = isfirst ? ind : ind+N1 + rxnarray[j, i] = isfirst ? ind : ind + N1 end end - for (j,r) in enumerate(rxn.fragmentbasedreactants) + for (j, r) in enumerate(rxn.fragmentbasedreactants) isfirst = true - ind = findfirst(isequal(r),domainfilm.phase.fragments) + ind = findfirst(isequal(r), domainfilm.phase.fragments) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - fragmentbasedrxnarray[j,i] = isfirst ? ind : ind+N1 + fragmentbasedrxnarray[j, i] = isfirst ? ind : ind + N1 end - for (j,r) in enumerate(rxn.products) + for (j, r) in enumerate(rxn.products) if !r.isfragmentintermediate isfirst = true - ind = findfirst(isequal(r),domainfilm.phase.fragments) + ind = findfirst(isequal(r), domainfilm.phase.fragments) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - rxnarray[j+4,i] = isfirst ? ind : ind+N1 + rxnarray[j+4, i] = isfirst ? ind : ind + N1 end end - for (j,r) in enumerate(rxn.fragmentbasedproducts) + for (j, r) in enumerate(rxn.fragmentbasedproducts) isfirst = true - ind = findfirst(isequal(r),domainfilm.phase.fragments) + ind = findfirst(isequal(r), domainfilm.phase.fragments) if ind === nothing isfirst = false - ind = findfirst(isequal(r),domain2.phase.species) + ind = findfirst(isequal(r), domain2.phase.species) end - fragmentbasedrxnarray[j+maxnumfragmentbasedreacprod,i] = isfirst ? ind : ind+N1 + fragmentbasedrxnarray[j+maxnumfragmentbasedreacprod, i] = isfirst ? ind : ind + N1 end end return rxnarray, fragmentbasedrxnarray @@ -526,12 +526,16 @@ end export getfragmentbasedinterfacereactioninds function getkfskrevs(ri::FragmentBasedReactiveFilmGrowthInterfaceConstantT) - return ri.kfs,ri.krevs + return ri.kfs, ri.krevs end -function evaluate(ri::FragmentBasedReactiveFilmGrowthInterfaceConstantT,dydt,domainfilm,Vfilm,cstot) +function evaluate(ri::FragmentBasedReactiveFilmGrowthInterfaceConstantT, dydt, Vfilm, cstot) kfs, krevs = getkfskrevs(ri) - addreactionratecontributions!(dydt,ri.fragmentbasedrxnarray,ri.rxnarray,cstot,kfs,krevs,Vfilm,domainfilm.indexes[3],ri.Mws,domainfilm.indexes[1]:domainfilm.indexes[2]) + addreactionratecontributions!(dydt, ri.fragmentbasedrxnarray, ri.rxnarray, cstot, kfs, krevs, Vfilm, ri.domainfilm.indexes[3], ri.Mws, ri.domainfilm.indexes[1]:ri.domainfilm.indexes[2]) + if hasproperty(ri.domain2, :epsilon) + epsilon = ri.domain2.epsilon + dydt[ri.domain2.thermovariabledict["V"]] = dydt[ri.domainfilm.indexes[3]] / ri.domainfilm.rho / (1 - epsilon) * epsilon + end end export evaluate \ No newline at end of file diff --git a/src/Reactor.jl b/src/Reactor.jl index 8cebfeb7..549e9ead 100644 --- a/src/Reactor.jl +++ b/src/Reactor.jl @@ -27,70 +27,70 @@ struct Reactor{D,G1,G2,Q,F1,F2,F3} <: AbstractReactor precsjulia::F3 #function to calculate preconditioner for Julia solvers end -function Reactor(domain::T,y0::Array{T1,1},tspan::Tuple,interfaces::Z=[];p::X=SciMLBase.NullParameters(),forwardsensitivities=false,forwarddiff=false,modelingtoolkit=false,tau=1e-3) where {T<:AbstractDomain,T1<:Real,Z<:AbstractArray,X} - dydt(dy::X,y::T,p::V,t::Q) where {X,T,Q,V} = dydtreactor!(dy,y,t,domain,interfaces,p=p) - jacy!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobiany!(J,y,p,t,domain,interfaces,nothing) - jacyforwarddiff!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J,y,p,t,domain,interfaces,nothing) - jacp!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianp!(J,y,p,t,domain,interfaces,nothing) - jacpforwarddiff!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J,y,p,t,domain,interfaces,nothing) - - psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64, Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64, Int64}}, tau::Float64) +function Reactor(domain::T, y0::Array{T1,1}, tspan::Tuple, interfaces::Z=[]; p::X=SciMLBase.NullParameters(), forwardsensitivities=false, forwarddiff=false, modelingtoolkit=false, tau=1e-3) where {T<:AbstractDomain,T1<:Real,Z<:AbstractArray,X} + dydt(dy::X, y::T, p::V, t::Q) where {X,T,Q,V} = dydtreactor!(dy, y, t, domain, interfaces, p=p) + jacy!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobiany!(J, y, p, t, domain, interfaces, nothing) + jacyforwarddiff!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J, y, p, t, domain, interfaces, nothing) + jacp!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianp!(J, y, p, t, domain, interfaces, nothing) + jacpforwarddiff!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J, y, p, t, domain, interfaces, nothing) + + psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64,Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64,Int64}}, tau::Float64) precsundials(z::T1, r::T2, p::T3, t::T4, y::T5, fy::T6, gamma::T7, delta::T8, lr::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsundials(z, r, p, t, y, fy, gamma, delta, lr, preccache) - precsjulia(W::T1,du::T2,u::T3,p::T4,t::T5,newW::T6,Plprev::T7,Prprev::T8,solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W,du,u,p,t,newW,Plprev,Prprev,solverdata,tau) - + precsjulia(W::T1, du::T2, u::T3, p::T4, t::T5, newW::T6, Plprev::T7, Prprev::T8, solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W, du, u, p, t, newW, Plprev, Prprev, solverdata, tau) + # determine worst sparsity y0length = length(y0) - J = spzeros(y0length,y0length) - jacyforwarddiff!(J,NaN*ones(y0length),p,0.0) + J = spzeros(y0length, y0length) + jacyforwarddiff!(J, NaN * ones(y0length), p, 0.0) @. J.nzval = 1.0 - sparsity = 1.0 - length(J.nzval)/(y0length*y0length) + sparsity = 1.0 - length(J.nzval) / (y0length * y0length) # preconditioner caches for Sundials solver - W = spzeros(y0length,y0length) - jacyforwarddiff!(W,y0,p,0.0) - @. W.nzval = -1.0*W.nzval + W = spzeros(y0length, y0length) + jacyforwarddiff!(W, y0, p, 0.0) + @. W.nzval = -1.0 * W.nzval idxs = diagind(W) @inbounds @views @. W[idxs] = W[idxs] + 1 - prectmp = ilu(W, τ = tau) + prectmp = ilu(W, τ=tau) preccache = Ref(prectmp) - + if (forwardsensitivities || !forwarddiff) && domain isa Union{ConstantTPDomain,ConstantVDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedVDomain,ParametrizedPDomain,ConstantTVDomain,ParametrizedTConstantVDomain,ConstantTAPhiDomain} if !forwardsensitivities - odefcn = ODEFunction(dydt;jac=jacy!,paramjac=jacp!) + odefcn = ODEFunction(dydt; jac=jacy!, paramjac=jacp!) else - odefcn = ODEFunction(dydt;paramjac=jacp!) + odefcn = ODEFunction(dydt; paramjac=jacp!) end else - odefcn = ODEFunction(dydt;jac=jacyforwarddiff!,paramjac=jacpforwarddiff!,jac_prototype=float.(J)) #jac_prototype is not needed/used for Sundials solvers but maybe needed for Julia solvers + odefcn = ODEFunction(dydt; jac=jacyforwarddiff!, paramjac=jacpforwarddiff!, jac_prototype=float.(J)) #jac_prototype is not needed/used for Sundials solvers but maybe needed for Julia solvers end if forwardsensitivities - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES) else - ode = ODEProblem(odefcn,y0,tspan,p) + ode = ODEProblem(odefcn, y0, tspan, p) if sparsity > 0.8 #empirical threshold to use preconditioner - recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES,prec=precsundials,psetup=psetupsundials,prec_side=1) + recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES, prec=precsundials, psetup=psetupsundials, prec_side=1) else - recsolver = Sundials.CVODE_BDF() + recsolver = Sundials.CVODE_BDF() end end if modelingtoolkit sys = modelingtoolkitize(ode) jac = eval(ModelingToolkit.generate_jacobian(sys)[2]) if (forwardsensitivities || !forwarddiff) && domain isa Union{ConstantTPDomain,ConstantVDomain,ConstantPDomain,ParametrizedTPDomain,ParametrizedVDomain,ParametrizedPDomain,ConstantTVDomain,ParametrizedTConstantVDomain,ConstantTAPhiDomain} - odefcn = ODEFunction(dydt;jac=jac,paramjac=jacp!) - else - odefcn = ODEFunction(dydt;jac=jac,paramjac=jacpforwarddiff!) + odefcn = ODEFunction(dydt; jac=jac, paramjac=jacp!) + else + odefcn = ODEFunction(dydt; jac=jac, paramjac=jacpforwarddiff!) end if forwardsensitivities - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) else - ode = ODEProblem(odefcn,y0,tspan,p) + ode = ODEProblem(odefcn, y0, tspan, p) end end - return Reactor(domain,interfaces,y0,tspan,p,ode,recsolver,forwardsensitivities,forwarddiff,modelingtoolkit,tau,precsundials,psetupsundials,precsjulia) + return Reactor(domain, interfaces, y0, tspan, p, ode, recsolver, forwardsensitivities, forwarddiff, modelingtoolkit, tau, precsundials, psetupsundials, precsjulia) end -function Reactor(domains::T,y0s::W1,tspan::W2,interfaces::Z=Tuple(),ps::X=SciMLBase.NullParameters();forwardsensitivities=false,modelingtoolkit=false,tau=1e-3) where {T<:Tuple,W1<:Tuple,Z,X,W2} +function Reactor(domains::T, y0s::W1, tspan::W2, interfaces::Z=Tuple(), ps::X=SciMLBase.NullParameters(); forwardsensitivities=false, modelingtoolkit=false, tau=1e-3) where {T<:Tuple,W1<:Tuple,Z,X,W2} #adjust indexing y0 = zeros(sum(length(y) for y in y0s)) Nvars = 0 @@ -99,23 +99,23 @@ function Reactor(domains::T,y0s::W1,tspan::W2,interfaces::Z=Tuple(),ps::X=SciMLB end n = Nvars k = 1 - for (j,domain) in enumerate(domains) + for (j, domain) in enumerate(domains) Nspcs = length(getphasespecies(domain.phase)) Ntherm = length(domain.indexes) - 2 for i = 1:8, j = 1:size(domain.rxnarray)[2] - if domain.rxnarray[i,j] != 0 - domain.rxnarray[i,j] += k-1 + if domain.rxnarray[i, j] != 0 + domain.rxnarray[i, j] += k - 1 end end for i in 1:length(domain.constantspeciesinds) - domain.constantspeciesinds[i] += k-1 + domain.constantspeciesinds[i] += k - 1 end domain.indexes[1] = k k += Nspcs - domain.indexes[2] = k-1 + domain.indexes[2] = k - 1 y0[domain.indexes[1]:domain.indexes[2]] = y0s[j][1:Nspcs] for m = 3:2+Ntherm - for (therm,ind) in domain.thermovariabledict + for (therm, ind) in domain.thermovariabledict if ind == domain.indexes[m] domain.thermovariabledict[therm] = n end @@ -125,25 +125,25 @@ function Reactor(domains::T,y0s::W1,tspan::W2,interfaces::Z=Tuple(),ps::X=SciMLB n -= 1 end end - + p = Array{Float64,1}() phases = [] phaseinds = Array{Tuple,1}() - for (i,domain) in enumerate(domains) + for (i, domain) in enumerate(domains) if domain.phase in phases - ind = findfirst(phases.==domain.phase) + ind = findfirst(phases .== domain.phase) domain.parameterindexes[1] = phaseinds[ind][1] domain.parameterindexes[2] = phaseinds[ind][2] - ds = [domains[ind] for ind in findall(x->x.phase==domain.phase,domains)] - if any(.!isa.(ds,AbstractConstantKDomain)) && any(isa.(ds,AbstractConstantKDomain)) #handle different p formats for the same phase + ds = [domains[ind] for ind in findall(x -> x.phase == domain.phase, domains)] + if any(.!isa.(ds, AbstractConstantKDomain)) && any(isa.(ds, AbstractConstantKDomain)) #handle different p formats for the same phase for d in ds - if !isa(d,AbstractConstantKDomain) + if !isa(d, AbstractConstantKDomain) p[domain.parameterindexes[1]:domain.parameterindexes[2]] .= d.p break end end for d in ds - if isa(d,AbstractConstantKDomain) + if isa(d, AbstractConstantKDomain) if forwardsensitivities == true #error if running into sensitivity bug error("forward sensitivities is not supported for domain combinations that share rate coefficients, but treat them differently ex: ConstantTPDomain and ConstantVDomain") end @@ -151,98 +151,98 @@ function Reactor(domains::T,y0s::W1,tspan::W2,interfaces::Z=Tuple(),ps::X=SciMLB end end end - else - domain.parameterindexes[1] = length(p)+1 - domain.parameterindexes[2] = length(p)+length(ps[i]) - push!(phaseinds,(length(p)+1,length(p)+length(ps[i]))) - push!(phases,domain.phase) - p = vcat(p,ps[i]) + else + domain.parameterindexes[1] = length(p) + 1 + domain.parameterindexes[2] = length(p) + length(ps[i]) + push!(phaseinds, (length(p) + 1, length(p) + length(ps[i]))) + push!(phases, domain.phase) + p = vcat(p, ps[i]) end end - - for (i,inter) in enumerate(interfaces) + + for (i, inter) in enumerate(interfaces) if isa(inter, FragmentBasedReactiveFilmGrowthInterfaceConstantT) - indfilm = findfirst(isequal(inter.domainfilm),domains) - ind2 = findfirst(isequal(inter.domain2),domains) + indfilm = findfirst(isequal(inter.domainfilm), domains) + ind2 = findfirst(isequal(inter.domain2), domains) inter.domaininds[1] = indfilm inter.domaininds[2] = ind2 - inter.parameterindexes[1] = length(p)+1 - inter.parameterindexes[2] = length(p)+length(ps[i+length(domains)]) - rxnarray, fragmentbasedrxnarray = getfragmentbasedinterfacereactioninds(inter.domainfilm,inter.domain2,inter.reactions) + inter.parameterindexes[1] = length(p) + 1 + inter.parameterindexes[2] = length(p) + length(ps[i+length(domains)]) + rxnarray, fragmentbasedrxnarray = getfragmentbasedinterfacereactioninds(inter.domainfilm, inter.domain2, inter.reactions) inter.rxnarray .= rxnarray inter.fragmentbasedrxnarray .= fragmentbasedrxnarray - p = vcat(p,ps[i+length(domains)]) + p = vcat(p, ps[i+length(domains)]) elseif isa(inter, AbstractReactiveInternalInterface) - ind1 = findfirst(isequal(inter.domain1),domains) - ind2 = findfirst(isequal(inter.domain2),domains) + ind1 = findfirst(isequal(inter.domain1), domains) + ind2 = findfirst(isequal(inter.domain2), domains) inter.domaininds[1] = ind1 inter.domaininds[2] = ind2 - inter.parameterindexes[1] = length(p)+1 - inter.parameterindexes[2] = length(p)+length(ps[i+length(domains)]) - inter.rxnarray .= getinterfacereactioninds(inter.domain1,inter.domain2,inter.reactions) - p = vcat(p,ps[i+length(domains)]) + inter.parameterindexes[1] = length(p) + 1 + inter.parameterindexes[2] = length(p) + length(ps[i+length(domains)]) + inter.rxnarray .= getinterfacereactioninds(inter.domain1, inter.domain2, inter.reactions) + p = vcat(p, ps[i+length(domains)]) elseif isa(inter, VaporLiquidMassTransferInternalInterfaceConstantT) - indgas = findfirst(isequal(inter.domaingas),domains) - indliq = findfirst(isequal(inter.domainliq),domains) + indgas = findfirst(isequal(inter.domaingas), domains) + indliq = findfirst(isequal(inter.domainliq), domains) inter.domaininds[1] = indgas inter.domaininds[2] = indliq - inter.parameterindexes[1] = length(p)+1 - inter.parameterindexes[2] = length(p)+length(ps[i+length(domains)]) - inter.ignoremasstransferspcinds .= getinterfaceignoremasstransferspcinds(inter.domaingas,inter.domainliq,inter.ignoremasstransferspcnames) - p = vcat(p,ps[i+length(domains)]) + inter.parameterindexes[1] = length(p) + 1 + inter.parameterindexes[2] = length(p) + length(ps[i+length(domains)]) + inter.ignoremasstransferspcinds .= getinterfaceignoremasstransferspcinds(inter.domaingas, inter.domainliq, inter.ignoremasstransferspcnames) + p = vcat(p, ps[i+length(domains)]) end end - - dydt(dy::X,y::T,p::V,t::Q) where {X,T,Q,V} = dydtreactor!(dy,y,t,domains,interfaces,p=p) - jacy!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J,y,p,t,domains,interfaces,nothing) - jacp!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J,y,p,t,domains,interfaces,nothing) - psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64, Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64, Int64}}, tau::Float64) + dydt(dy::X, y::T, p::V, t::Q) where {X,T,Q,V} = dydtreactor!(dy, y, t, domains, interfaces, p=p) + jacy!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J, y, p, t, domains, interfaces, nothing) + jacp!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J, y, p, t, domains, interfaces, nothing) + + psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64,Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64,Int64}}, tau::Float64) precsundials(z::T1, r::T2, p::T3, t::T4, y::T5, fy::T6, gamma::T7, delta::T8, lr::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsundials(z, r, p, t, y, fy, gamma, delta, lr, preccache) - precsjulia(W::T1,du::T2,u::T3,p::T4,t::T5,newW::T6,Plprev::T7,Prprev::T8,solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W,du,u,p,t,newW,Plprev,Prprev,solverdata,tau) - + precsjulia(W::T1, du::T2, u::T3, p::T4, t::T5, newW::T6, Plprev::T7, Prprev::T8, solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W, du, u, p, t, newW, Plprev, Prprev, solverdata, tau) + # determine worst sparsity y0length = length(y0) - J = spzeros(y0length,y0length) - jacy!(J,NaN*ones(y0length),p,0.0) + J = spzeros(y0length, y0length) + jacy!(J, NaN * ones(y0length), p, 0.0) @. J.nzval = 1.0 - sparsity = 1.0 - length(J.nzval)/(y0length*y0length) + sparsity = 1.0 - length(J.nzval) / (y0length * y0length) # preconditioner caches for Sundials solver - W = spzeros(y0length,y0length) - jacy!(W,y0,p,0.0) - @. W.nzval = -1.0*W.nzval + W = spzeros(y0length, y0length) + jacy!(W, y0, p, 0.0) + @. W.nzval = -1.0 * W.nzval idxs = diagind(W) @inbounds @views @. W[idxs] = W[idxs] + 1 - prectmp = ilu(W, τ = tau) + prectmp = ilu(W, τ=tau) preccache = Ref(prectmp) - + if forwardsensitivities - odefcn = ODEFunction(dydt;paramjac=jacp!) - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + odefcn = ODEFunction(dydt; paramjac=jacp!) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES) if modelingtoolkit sys = modelingtoolkitize(ode) jac = eval(ModelingToolkit.generate_jacobian(sys)[2]) - odefcn = ODEFunction(dydt;jac=jac,paramjac=jacp!) - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + odefcn = ODEFunction(dydt; jac=jac, paramjac=jacp!) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) end else - odefcn = ODEFunction(dydt;jac=jacy!,paramjac=jacp!,jac_prototype=float.(J)) - ode = ODEProblem(odefcn,y0,tspan,p) + odefcn = ODEFunction(dydt; jac=jacy!, paramjac=jacp!, jac_prototype=float.(J)) + ode = ODEProblem(odefcn, y0, tspan, p) if sparsity > 0.8 #empirical threshold to use preconditioner - recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES,prec=precsundials,psetup=psetupsundials,prec_side=1) + recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES, prec=precsundials, psetup=psetupsundials, prec_side=1) else - recsolver = Sundials.CVODE_BDF() + recsolver = Sundials.CVODE_BDF() end if modelingtoolkit sys = modelingtoolkitize(ode) jac = eval(ModelingToolkit.generate_jacobian(sys)[2]) - odefcn = ODEFunction(dydt;jac=jac,paramjac=jacp!) - ode = ODEProblem(odefcn,y0,tspan,p) + odefcn = ODEFunction(dydt; jac=jac, paramjac=jacp!) + ode = ODEProblem(odefcn, y0, tspan, p) end end - return Reactor(domains,interfaces,y0,tspan,p,ode,recsolver,forwardsensitivities,false,modelingtoolkit,tau,precsundials,psetupsundials,precsjulia),y0,p + return Reactor(domains, interfaces, y0, tspan, p, ode, recsolver, forwardsensitivities, false, modelingtoolkit, tau, precsundials, psetupsundials, precsjulia), y0, p end struct ReducedModelCache{X1,X2,X3} @@ -252,72 +252,72 @@ struct ReducedModelCache{X1,X2,X3} end #Not generating reducedmodelmappings within Reactor object as generating qssc! can take a while for large mechanisms so we only want to do it once -function Reactor(domain::T,y0unlumped::Array{W1,1},tspan::Tuple,reducedmodelmappings::ReducedModelMappings,interfaces::Z=[];p::X=SciMLBase.NullParameters(),forwardsensitivities=false,forwarddiff=false,modelingtoolkit=false,tau=1e-3,chunk_size=9) where {T<:AbstractDomain,W1<:Real,Z<:AbstractArray,X,F<:Function} - dydt(dy::X,y::T,p::V,t::Q) where {X,T,Q,V} = dydtreactor!(dy,y,t,domain,interfaces,reducedmodelmappings,reducedmodelcache,p=p) - jacy!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J,y,p,t,domain,interfaces,reducedmodelmappings,reducedmodelcache) - jacp!(J::Q2,y::T,p::V,t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J,y,p,t,domain,interfaces,reducedmodelmappings,reducedmodelcache) +function Reactor(domain::T, y0unlumped::Array{W1,1}, tspan::Tuple, reducedmodelmappings::ReducedModelMappings, interfaces::Z=[]; p::X=SciMLBase.NullParameters(), forwardsensitivities=false, forwarddiff=false, modelingtoolkit=false, tau=1e-3, chunk_size=9) where {T<:AbstractDomain,W1<:Real,Z<:AbstractArray,X,F<:Function} + dydt(dy::X, y::T, p::V, t::Q) where {X,T,Q,V} = dydtreactor!(dy, y, t, domain, interfaces, reducedmodelmappings, reducedmodelcache, p=p) + jacy!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianyforwarddiff!(J, y, p, t, domain, interfaces, reducedmodelmappings, reducedmodelcache) + jacp!(J::Q2, y::T, p::V, t::Q) where {Q2,T,Q,V} = jacobianpforwarddiff!(J, y, p, t, domain, interfaces, reducedmodelmappings, reducedmodelcache) #y0 in Y space - y0 = zeros(length(reducedmodelmappings.reducedindexes)+length(reducedmodelmappings.lumpedgroupmapping)+length(domain.thermovariabledict)) + y0 = zeros(length(reducedmodelmappings.reducedindexes) + length(reducedmodelmappings.lumpedgroupmapping) + length(domain.thermovariabledict)) @inbounds @views y0[1:end-length(domain.thermovariabledict)-length(reducedmodelmappings.lumpedgroupmapping)] .= y0unlumped[reducedmodelmappings.reducedindexes] - for (i,group) in enumerate(reducedmodelmappings.lumpedgroupmapping) - for (index,weight) in group + for (i, group) in enumerate(reducedmodelmappings.lumpedgroupmapping) + for (index, weight) in group @fastmath @inbounds y0[length(reducedmodelmappings.reducedindexes)+i] += y0unlumped[index] end end @inbounds @views y0[end-length(domain.thermovariabledict)+1:end] .= y0unlumped[end-length(domain.thermovariabledict)+1:end] #initialize cache - ylength = length(y0)+length(reducedmodelmappings.qssindexes)-length(reducedmodelmappings.lumpedgroupmapping)+length(reducedmodelmappings.lumpedindexes) - yunlumped = dualcache(zeros(ylength),chunk_size) - dydtunlumped = dualcache(zeros(ylength),chunk_size) - qssc = dualcache(zeros(length(reducedmodelmappings.qssindexes)),chunk_size) - reducedmodelcache = ReducedModelCache(yunlumped,dydtunlumped,qssc) + ylength = length(y0) + length(reducedmodelmappings.qssindexes) - length(reducedmodelmappings.lumpedgroupmapping) + length(reducedmodelmappings.lumpedindexes) + yunlumped = dualcache(zeros(ylength), chunk_size) + dydtunlumped = dualcache(zeros(ylength), chunk_size) + qssc = dualcache(zeros(length(reducedmodelmappings.qssindexes)), chunk_size) + reducedmodelcache = ReducedModelCache(yunlumped, dydtunlumped, qssc) - psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64, Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64, Int64}}, tau::Float64) + psetupsundials(p::T1, t::T2, u::T3, du::T4, jok::Bool, jcurPtr::T5, gamma::T6) where {T1,T2,T3,T4,T5,T6} = _psetupsundials(p, t, u, du, jok, jcurPtr, gamma, jacy!, W::SparseMatrixCSC{Float64,Int64}, preccache::Base.RefValue{IncompleteLU.ILUFactorization{Float64,Int64}}, tau::Float64) precsundials(z::T1, r::T2, p::T3, t::T4, y::T5, fy::T6, gamma::T7, delta::T8, lr::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsundials(z, r, p, t, y, fy, gamma, delta, lr, preccache) - precsjulia(W::T1,du::T2,u::T3,p::T4,t::T5,newW::T6,Plprev::T7,Prprev::T8,solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W,du,u,p,t,newW,Plprev,Prprev,solverdata,tau) - + precsjulia(W::T1, du::T2, u::T3, p::T4, t::T5, newW::T6, Plprev::T7, Prprev::T8, solverdata::T9) where {T1,T2,T3,T4,T5,T6,T7,T8,T9} = _precsjulia(W, du, u, p, t, newW, Plprev, Prprev, solverdata, tau) + # determine worst sparsity y0length = length(y0) - J = spzeros(y0length,y0length) - jacy!(J,NaN*ones(y0length),p,0.0) + J = spzeros(y0length, y0length) + jacy!(J, NaN * ones(y0length), p, 0.0) @. J.nzval = 1.0 - sparsity = 1.0 - length(J.nzval)/(y0length*y0length) + sparsity = 1.0 - length(J.nzval) / (y0length * y0length) # preconditioner caches for Sundials solver - W = spzeros(y0length,y0length) - jacy!(W,y0,p,0.0) - @. W.nzval = -1.0*W.nzval + W = spzeros(y0length, y0length) + jacy!(W, y0, p, 0.0) + @. W.nzval = -1.0 * W.nzval idxs = diagind(W) @inbounds @views @. W[idxs] = W[idxs] + 1 - prectmp = ilu(W, τ = tau) + prectmp = ilu(W, τ=tau) preccache = Ref(prectmp) - - odefcn = ODEFunction(dydt;jac=jacy!,paramjac=jacp!,jac_prototype=float.(J)) #jac_prototype is not needed/used for Sundials solvers but maybe needed for Julia solvers + + odefcn = ODEFunction(dydt; jac=jacy!, paramjac=jacp!, jac_prototype=float.(J)) #jac_prototype is not needed/used for Sundials solvers but maybe needed for Julia solvers if forwardsensitivities - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES) else - ode = ODEProblem(odefcn,y0,tspan,p) + ode = ODEProblem(odefcn, y0, tspan, p) if sparsity > 0.8 #empirical threshold to use preconditioner - recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES,prec=precsundials,psetup=psetupsundials,prec_side=1) + recsolver = Sundials.CVODE_BDF(linear_solver=:GMRES, prec=precsundials, psetup=psetupsundials, prec_side=1) else - recsolver = Sundials.CVODE_BDF() + recsolver = Sundials.CVODE_BDF() end end if modelingtoolkit sys = modelingtoolkitize(ode) jac = eval(ModelingToolkit.generate_jacobian(sys)[2]) - odefcn = ODEFunction(dydt;jac=jac,paramjac=jacp!) + odefcn = ODEFunction(dydt; jac=jac, paramjac=jacp!) if forwardsensitivities - ode = ODEForwardSensitivityProblem(odefcn,y0,tspan,p) + ode = ODEForwardSensitivityProblem(odefcn, y0, tspan, p) else - ode = ODEProblem(odefcn,y0,tspan,p) + ode = ODEProblem(odefcn, y0, tspan, p) end end - return Reactor(domain,interfaces,y0,tspan,p,ode,recsolver,forwardsensitivities,forwarddiff,modelingtoolkit,tau,precsundials,psetupsundials,precsjulia) + return Reactor(domain, interfaces, y0, tspan, p, ode, recsolver, forwardsensitivities, forwarddiff, modelingtoolkit, tau, precsundials, psetupsundials, precsjulia) end export Reactor @@ -335,16 +335,16 @@ export Reactor """ if jok @. W = 0.0 - jac!(W,u,p,t) + jac!(W, u, p, t) jcurPtr[] = true # W = I - gamma*J - @. W.nzval = -gamma*W.nzval + @. W.nzval = -gamma * W.nzval idxs = diagind(W) @inbounds @views @. W[idxs] = W[idxs] + 1 # Build preconditioner on W - preccache[] = ilu(W, τ = tau) + preccache[] = ilu(W, τ=tau) end nothing end @@ -361,9 +361,9 @@ end lr: a flag for whether lr=1 (left) or lr=2 (right) preconditioning preccache: preconditioner cache """ - ldiv!(z,preccache[],r) + ldiv!(z, preccache[], r) end -@inline function _precsjulia(W::T1,du::T2,u::T3,p::T4,t::T5,newW::T6,Plprev::T7,Prprev::T8,solverdata::T9,tau::T10) where {T1,T2,T3,T4,T5,T6,T7,T8,T9,T10} +@inline function _precsjulia(W::T1, du::T2, u::T3, p::T4, t::T5, newW::T6, Plprev::T7, Prprev::T8, solverdata::T9, tau::T10) where {T1,T2,T3,T4,T5,T6,T7,T8,T9,T10} """ Update preconditioner when Jacobian needs to be updated for Julia solvers. Credit to tutorial of DifferentialEquations.jl. W: I - gamma*J or I/gamma - J depending on the algorithm. @@ -380,200 +380,200 @@ end solverdata: Optional extra data the solvers can give to the precs function. Solver-dependent and subject to change. """ if newW === nothing || newW - Pl = ilu(convert(AbstractMatrix,W), τ = tau) + Pl = ilu(convert(AbstractMatrix, W), τ=tau) else Pl = Plprev end - Pl,nothing + Pl, nothing end -@inline function getrate(rxn::T,cs::Array{W,1},kfs::Array{Q,1},krevs::Array{Q,1}) where {T<:AbstractReaction,Q,W<:Real} +@inline function getrate(rxn::T, cs::Array{W,1}, kfs::Array{Q,1}, krevs::Array{Q,1}) where {T<:AbstractReaction,Q,W<:Real} Nreact = length(rxn.reactantinds) Nprod = length(rxn.productinds) R = 0.0 if Nreact == 1 - @fastmath @inbounds R += kfs[rxn.index]*cs[rxn.reactantinds[1]] + @fastmath @inbounds R += kfs[rxn.index] * cs[rxn.reactantinds[1]] elseif Nreact == 2 - @fastmath @inbounds R += kfs[rxn.index]*cs[rxn.reactantinds[1]]*cs[rxn.reactantinds[2]] + @fastmath @inbounds R += kfs[rxn.index] * cs[rxn.reactantinds[1]] * cs[rxn.reactantinds[2]] elseif Nreact == 3 - @fastmath @inbounds R += kfs[rxn.index]*cs[rxn.reactantinds[1]]*cs[rxn.reactantinds[2]]*cs[rxn.reactantinds[3]] + @fastmath @inbounds R += kfs[rxn.index] * cs[rxn.reactantinds[1]] * cs[rxn.reactantinds[2]] * cs[rxn.reactantinds[3]] elseif Nreact == 4 - @fastmath @inbounds R += kfs[rxn.index]*cs[rxn.reactantinds[1]]*cs[rxn.reactantinds[2]]*cs[rxn.reactantinds[3]]*cs[rxn.reactantinds[4]] + @fastmath @inbounds R += kfs[rxn.index] * cs[rxn.reactantinds[1]] * cs[rxn.reactantinds[2]] * cs[rxn.reactantinds[3]] * cs[rxn.reactantinds[4]] end if Nprod == 1 - @fastmath @inbounds R -= krevs[rxn.index]*cs[rxn.productinds[1]] + @fastmath @inbounds R -= krevs[rxn.index] * cs[rxn.productinds[1]] elseif Nprod == 2 - @fastmath @inbounds R -= krevs[rxn.index]*cs[rxn.productinds[1]]*cs[rxn.productinds[2]] + @fastmath @inbounds R -= krevs[rxn.index] * cs[rxn.productinds[1]] * cs[rxn.productinds[2]] elseif Nprod == 3 - @fastmath @inbounds R -= krevs[rxn.index]*cs[rxn.productinds[1]]*cs[rxn.productinds[2]]*cs[rxn.productinds[3]] + @fastmath @inbounds R -= krevs[rxn.index] * cs[rxn.productinds[1]] * cs[rxn.productinds[2]] * cs[rxn.productinds[3]] elseif Nprod == 4 - @fastmath @inbounds R -= krevs[rxn.index]*cs[rxn.productinds[1]]*cs[rxn.productinds[2]]*cs[rxn.productinds[3]]*cs[rxn.productinds[4]] + @fastmath @inbounds R -= krevs[rxn.index] * cs[rxn.productinds[1]] * cs[rxn.productinds[2]] * cs[rxn.productinds[3]] * cs[rxn.productinds[4]] end return R end export getrate -@inline function getrates(rarray,cs,kfs,krevs) +@inline function getrates(rarray, cs, kfs, krevs) rts = zeros(length(kfs)) for i = 1:length(rts) - if @inbounds rarray[2,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]] - elseif @inbounds rarray[3,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]] - elseif @inbounds rarray[4,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]] + if @inbounds rarray[2, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] + elseif @inbounds rarray[3, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] + elseif @inbounds rarray[4, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] else - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]]*cs[rarray[4,i]] + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] * cs[rarray[4, i]] end - if @inbounds rarray[6,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]] - elseif @inbounds rarray[7,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]] - elseif @inbounds rarray[8,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]] + if @inbounds rarray[6, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] + elseif @inbounds rarray[7, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] + elseif @inbounds rarray[8, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] else - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]]*cs[rarray[8,i]] + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] * cs[rarray[8, i]] end @fastmath R = fR - rR - + rts[i] = R end return rts end export getrates -@inline function addreactionratecontributions!(dydt::Q,rarray::Array{W2,2},cs::W,kfs::Z,krevs::Y) where {Q,Z,Y,T,W,W2} +@inline function addreactionratecontributions!(dydt::Q, rarray::Array{W2,2}, cs::W, kfs::Z, krevs::Y) where {Q,Z,Y,T,W,W2} @inbounds @simd for i = 1:size(rarray)[2] - if @inbounds rarray[2,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]] - elseif @inbounds rarray[3,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]] - elseif @inbounds rarray[4,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]] + if @inbounds rarray[2, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] + elseif @inbounds rarray[3, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] + elseif @inbounds rarray[4, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] else - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]]*cs[rarray[4,i]] + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] * cs[rarray[4, i]] end - if @inbounds rarray[6,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]] - elseif @inbounds rarray[7,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]] - elseif @inbounds rarray[8,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]] + if @inbounds rarray[6, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] + elseif @inbounds rarray[7, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] + elseif @inbounds rarray[8, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] else - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]]*cs[rarray[8,i]] + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] * cs[rarray[8, i]] end @fastmath R = fR - rR - @inbounds @fastmath dydt[rarray[1,i]] -= R - if @inbounds rarray[2,i] != 0 - @inbounds @fastmath dydt[rarray[2,i]] -= R - if @inbounds rarray[3,i] != 0 - @inbounds @fastmath dydt[rarray[3,i]] -= R - if @inbounds rarray[4,i] != 0 - @inbounds @fastmath dydt[rarray[4,i]] -= R + @inbounds @fastmath dydt[rarray[1, i]] -= R + if @inbounds rarray[2, i] != 0 + @inbounds @fastmath dydt[rarray[2, i]] -= R + if @inbounds rarray[3, i] != 0 + @inbounds @fastmath dydt[rarray[3, i]] -= R + if @inbounds rarray[4, i] != 0 + @inbounds @fastmath dydt[rarray[4, i]] -= R end end end - @inbounds @fastmath dydt[rarray[5,i]] += R - if @inbounds rarray[6,i] != 0 - @inbounds @fastmath dydt[rarray[6,i]] += R - if @inbounds rarray[7,i] != 0 - @inbounds @fastmath dydt[rarray[7,i]] += R - if @inbounds rarray[8,i] != 0 - @inbounds @fastmath dydt[rarray[8 ,i]] += R + @inbounds @fastmath dydt[rarray[5, i]] += R + if @inbounds rarray[6, i] != 0 + @inbounds @fastmath dydt[rarray[6, i]] += R + if @inbounds rarray[7, i] != 0 + @inbounds @fastmath dydt[rarray[7, i]] += R + if @inbounds rarray[8, i] != 0 + @inbounds @fastmath dydt[rarray[8, i]] += R end end end end end -function addreactionratecontributions!(dydt::Q,fragmentbasedrxnarray::Array{Int64,2},rxnarray::Array{Int64,2},cs::W,kfs::Z,krevs::Y,V::Q1,massindex::Int64,Mws::Array{Float64,1},fragmentindexes::Q2) where {Q,Z,Y,T,W,W2,Q1,Q2} +function addreactionratecontributions!(dydt::Q, fragmentbasedrxnarray::Array{Int64,2}, rxnarray::Array{Int64,2}, cs::W, kfs::Z, krevs::Y, V::Q1, massindex::Int64, Mws::Array{Float64,1}, fragmentindexes::Q2) where {Q,Z,Y,T,W,W2,Q1,Q2} numfragmentbasedreacprod, numrxns = size(fragmentbasedrxnarray) - half = Int(numfragmentbasedreacprod/2) + half = Int(numfragmentbasedreacprod / 2) for i = 1:numrxns fR = 0 - if rxnarray[1,i] != 0 - fR = kfs[i]*cs[rxnarray[1,i]] + if rxnarray[1, i] != 0 + fR = kfs[i] * cs[rxnarray[1, i]] for j = 2:4 - if rxnarray[j,i] != 0 - @fastmath fR *= cs[rxnarray[j,i]] + if rxnarray[j, i] != 0 + @fastmath fR *= cs[rxnarray[j, i]] end end end rR = 0 - if rxnarray[5,i] !=0 - rR = krevs[i]*cs[rxnarray[5,i]] + if rxnarray[5, i] != 0 + rR = krevs[i] * cs[rxnarray[5, i]] for j = 6:8 - if rxnarray[j,i] != 0 - @fastmath rR *= cs[rxnarray[j,i]] + if rxnarray[j, i] != 0 + @fastmath rR *= cs[rxnarray[j, i]] end end end - @fastmath R = (fR - rR)*V + @fastmath R = (fR - rR) * V for j = 1:half - if fragmentbasedrxnarray[j,i] != 0 - @fastmath dydt[fragmentbasedrxnarray[j,i]] -= R - if !(fragmentbasedrxnarray[j,i] in fragmentindexes) - dydt[massindex] += R * Mws[fragmentbasedrxnarray[j,i]] + if fragmentbasedrxnarray[j, i] != 0 + @fastmath dydt[fragmentbasedrxnarray[j, i]] -= R + if !(fragmentbasedrxnarray[j, i] in fragmentindexes) + dydt[massindex] += R * Mws[fragmentbasedrxnarray[j, i]] end end end for j = half+1:numfragmentbasedreacprod - if fragmentbasedrxnarray[j,i] != 0 - @fastmath dydt[fragmentbasedrxnarray[j,i]] += R - if !(fragmentbasedrxnarray[j,i] in fragmentindexes) - dydt[massindex] -= R * Mws[fragmentbasedrxnarray[j,i]] + if fragmentbasedrxnarray[j, i] != 0 + @fastmath dydt[fragmentbasedrxnarray[j, i]] += R + if !(fragmentbasedrxnarray[j, i] in fragmentindexes) + dydt[massindex] -= R * Mws[fragmentbasedrxnarray[j, i]] end end end end end -@inline function addreactionratecontributions!(dydt::Q,rarray::Array{W2,2},cs::W,kfs::Z,krevs::Y,V) where {Q,Z,Y,T,W,W2} +@inline function addreactionratecontributions!(dydt::Q, rarray::Array{W2,2}, cs::W, kfs::Z, krevs::Y, V) where {Q,Z,Y,T,W,W2} @inbounds @simd for i = 1:size(rarray)[2] - if @inbounds rarray[2,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]] - elseif @inbounds rarray[3,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]] - elseif @inbounds rarray[4,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]] + if @inbounds rarray[2, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] + elseif @inbounds rarray[3, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] + elseif @inbounds rarray[4, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] else - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]]*cs[rarray[4,i]] + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] * cs[rarray[4, i]] end - if @inbounds rarray[6,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]] - elseif @inbounds rarray[7,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]] - elseif @inbounds rarray[8,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]] + if @inbounds rarray[6, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] + elseif @inbounds rarray[7, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] + elseif @inbounds rarray[8, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] else - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]]*cs[rarray[8,i]] + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] * cs[rarray[8, i]] end - @fastmath R = (fR - rR)*V - @inbounds @fastmath dydt[rarray[1,i]] -= R - if @inbounds rarray[2,i] != 0 - @inbounds @fastmath dydt[rarray[2,i]] -= R - if @inbounds rarray[3,i] != 0 - @inbounds @fastmath dydt[rarray[3,i]] -= R - if @inbounds rarray[4,i] != 0 - @inbounds @fastmath dydt[rarray[4,i]] -= R + @fastmath R = (fR - rR) * V + @inbounds @fastmath dydt[rarray[1, i]] -= R + if @inbounds rarray[2, i] != 0 + @inbounds @fastmath dydt[rarray[2, i]] -= R + if @inbounds rarray[3, i] != 0 + @inbounds @fastmath dydt[rarray[3, i]] -= R + if @inbounds rarray[4, i] != 0 + @inbounds @fastmath dydt[rarray[4, i]] -= R end end end - @inbounds @fastmath dydt[rarray[5,i]] += R - if @inbounds rarray[6,i] != 0 - @inbounds @fastmath dydt[rarray[6,i]] += R - if @inbounds rarray[7,i] != 0 - @inbounds @fastmath dydt[rarray[7,i]] += R - if @inbounds rarray[8,i] != 0 - @inbounds @fastmath dydt[rarray[8,i]] += R + @inbounds @fastmath dydt[rarray[5, i]] += R + if @inbounds rarray[6, i] != 0 + @inbounds @fastmath dydt[rarray[6, i]] += R + if @inbounds rarray[7, i] != 0 + @inbounds @fastmath dydt[rarray[7, i]] += R + if @inbounds rarray[8, i] != 0 + @inbounds @fastmath dydt[rarray[8, i]] += R end end end @@ -581,178 +581,168 @@ end end export addreactionratecontributions! -@inline function addreactionratecontributionsforwardreverse!(dydt::Q,rarray::Array{W2,2},cs::W,kfs::Z,krevs::Y,V) where {Q,Z,Y,T,W,W2} +@inline function addreactionratecontributionsforwardreverse!(dydt::Q, rarray::Array{W2,2}, cs::W, kfs::Z, krevs::Y, V) where {Q,Z,Y,T,W,W2} frts = zeros(length(kfs)) rrts = zeros(length(kfs)) rts = zeros(length(kfs)) @inbounds for i = 1:size(rarray)[2] - if @inbounds rarray[2,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]] - elseif @inbounds rarray[3,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]] - elseif @inbounds rarray[4,i] == 0 - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]] + if @inbounds rarray[2, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] + elseif @inbounds rarray[3, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] + elseif @inbounds rarray[4, i] == 0 + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] else - @inbounds @fastmath fR = kfs[i]*cs[rarray[1,i]]*cs[rarray[2,i]]*cs[rarray[3,i]]*cs[rarray[4,i]] + @inbounds @fastmath fR = kfs[i] * cs[rarray[1, i]] * cs[rarray[2, i]] * cs[rarray[3, i]] * cs[rarray[4, i]] end - if @inbounds rarray[6,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]] - elseif @inbounds rarray[7,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]] - elseif @inbounds rarray[8,i] == 0 - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]] + if @inbounds rarray[6, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] + elseif @inbounds rarray[7, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] + elseif @inbounds rarray[8, i] == 0 + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] else - @inbounds @fastmath rR = krevs[i]*cs[rarray[5,i]]*cs[rarray[6,i]]*cs[rarray[7,i]]*cs[rarray[8,i]] + @inbounds @fastmath rR = krevs[i] * cs[rarray[5, i]] * cs[rarray[6, i]] * cs[rarray[7, i]] * cs[rarray[8, i]] end - @inbounds @fastmath frts[i] = fR*V - @inbounds @fastmath rrts[i] = rR*V + @inbounds @fastmath frts[i] = fR * V + @inbounds @fastmath rrts[i] = rR * V @inbounds @fastmath R = frts[i] - rrts[i] @inbounds rts[i] = R - @inbounds @fastmath dydt[rarray[1,i]] -= R - if @inbounds rarray[2,i] != 0 - @inbounds @fastmath dydt[rarray[2,i]] -= R - if @inbounds rarray[3,i] != 0 - @inbounds @fastmath dydt[rarray[3,i]] -= R - if @inbounds rarray[4,i] != 0 - @inbounds @fastmath dydt[rarray[4,i]] -= R + @inbounds @fastmath dydt[rarray[1, i]] -= R + if @inbounds rarray[2, i] != 0 + @inbounds @fastmath dydt[rarray[2, i]] -= R + if @inbounds rarray[3, i] != 0 + @inbounds @fastmath dydt[rarray[3, i]] -= R + if @inbounds rarray[4, i] != 0 + @inbounds @fastmath dydt[rarray[4, i]] -= R end end end - @inbounds @fastmath dydt[rarray[5,i]] += R - if @inbounds rarray[6,i] != 0 - @inbounds @fastmath dydt[rarray[6,i]] += R - if @inbounds rarray[7,i] != 0 - @inbounds @fastmath dydt[rarray[7,i]] += R - if @inbounds rarray[8,i] != 0 - @inbounds @fastmath dydt[rarray[8,i]] += R + @inbounds @fastmath dydt[rarray[5, i]] += R + if @inbounds rarray[6, i] != 0 + @inbounds @fastmath dydt[rarray[6, i]] += R + if @inbounds rarray[7, i] != 0 + @inbounds @fastmath dydt[rarray[7, i]] += R + if @inbounds rarray[8, i] != 0 + @inbounds @fastmath dydt[rarray[8, i]] += R end end end end - return rts,frts,rrts + return rts, frts, rrts end export addreactionratecontributionsforwardreverse! -@inline function dydtreactor!(dydt::RC,y::U,t::Z,domain::Q,interfaces::B;p::RV=SciMLBase.NullParameters(),sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:AbstractDomain} - dydt .= 0.0 - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR,phi = calcthermo(domain,y,t,p) - addreactionratecontributions!(dydt,domain.rxnarray,cs,kfs,krevs) - dydt .*= V - calcdomainderivatives!(domain,dydt,interfaces;t=t,T=T,P=P,Us=Us,Hs=Hs,V=V,C=C,ns=ns,N=N,Cvave=Cvave) - return dydt -end - -function dydtreactor!(dydt::RC,y::U,t::Z,domain::FragmentBasedConstantTrhoDomain{W,Y},interfaces::B;p::RV=SciMLBase.NullParameters(),sensitivity::Bool=true) where {RC,RV,B<:AbstractArray,Z<:Real,U,J<:Integer,W<:IdealDiluteSolution,Y<:Integer} +@inline function dydtreactor!(dydt::RC, y::U, t::Z, domain::Q, interfaces::B; p::RV=SciMLBase.NullParameters(), sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:AbstractDomain} dydt .= 0.0 - massindex = domain.indexes[3] - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR = calcthermo(domain,y,t,p) - addreactionratecontributions!(dydt,domain.fragmentbasedrxnarray,domain.rxnarray,cs,kfs,krevs,massindex,domain.Mws,domain.fragmentindexes) + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR, phi = calcthermo(domain, y, t, p) + addreactionratecontributions!(dydt, domain.rxnarray, cs, kfs, krevs) dydt .*= V - calcdomainderivatives!(domain,dydt,interfaces;t=t,T=T,P=P,Us=Us,Hs=Hs,V=V,C=C,ns=ns,N=N,Cvave=Cvave) + calcdomainderivatives!(domain, dydt, interfaces; t=t, T=T, P=P, Us=Us, Hs=Hs, V=V, C=C, ns=ns, N=N, Cvave=Cvave) return dydt end -@inline function dydtreactor!(dydt::RC,y::U,t::Z,domains::Q,interfaces::B;p::RV=SciMLBase.NullParameters(),sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:Tuple} +@inline function dydtreactor!(dydt::RC, y::U, t::Z, domains::Q, interfaces::B; p::RV=SciMLBase.NullParameters(), sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:Tuple} cstot = similar(y) cstot .= 0.0 dydt .= 0.0 domain = domains[1] - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR,phi = calcthermo(domain,y,t,p) - vns = Array{Any,1}(undef,length(domains)) + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR, phi = calcthermo(domain, y, t, p) + vns = Array{Any,1}(undef, length(domains)) vns[1] = ns - vcs = Array{Any,1}(undef,length(domains)) + vcs = Array{Any,1}(undef, length(domains)) vcs[1] = cs cstot[domain.indexes[1]:domain.indexes[2]] = cs - vT = Array{Any,1}(undef,length(domains)) + vT = Array{Any,1}(undef, length(domains)) vT[1] = T - vP = Array{Any,1}(undef,length(domains)) + vP = Array{Any,1}(undef, length(domains)) vP[1] = P - vV = Array{Any,1}(undef,length(domains)) + vV = Array{Any,1}(undef, length(domains)) vV[1] = V - vC = Array{Any,1}(undef,length(domains)) + vC = Array{Any,1}(undef, length(domains)) vC[1] = C - vN = Array{Any,1}(undef,length(domains)) + vN = Array{Any,1}(undef, length(domains)) vN[1] = N - vmu = Array{Any,1}(undef,length(domains)) + vmu = Array{Any,1}(undef, length(domains)) vmu[1] = mu - vkfs = Array{Any,1}(undef,length(domains)) + vkfs = Array{Any,1}(undef, length(domains)) vkfs[1] = kfs - vkrevs = Array{Any,1}(undef,length(domains)) + vkrevs = Array{Any,1}(undef, length(domains)) vkrevs[1] = krevs - vHs = Array{Any,1}(undef,length(domains)) + vHs = Array{Any,1}(undef, length(domains)) vHs[1] = Hs - vUs = Array{Any,1}(undef,length(domains)) + vUs = Array{Any,1}(undef, length(domains)) vUs[1] = Us - vGs = Array{Any,1}(undef,length(domains)) + vGs = Array{Any,1}(undef, length(domains)) vGs[1] = Gs - vdiffs = Array{Any,1}(undef,length(domains)) + vdiffs = Array{Any,1}(undef, length(domains)) vdiffs[1] = diffs - vCvave = Array{Any,1}(undef,length(domains)) + vCvave = Array{Any,1}(undef, length(domains)) vCvave[1] = Cvave - vcpdivR = Array{Any,1}(undef,length(domains)) + vcpdivR = Array{Any,1}(undef, length(domains)) vcpdivR[1] = cpdivR - vphi = Array{Any,1}(undef,length(domains)) + vphi = Array{Any,1}(undef, length(domains)) vphi[1] = phi - addreactionratecontributions!(dydt,domain.rxnarray,cstot,kfs,krevs) + addreactionratecontributions!(dydt, domain.rxnarray, cstot, kfs, krevs) @views dydt[domain.indexes[1]:domain.indexes[2]] .*= V - for (i,domain) in enumerate(@views domains[2:end]) + for (i, domain) in enumerate(@views domains[2:end]) k = i + 1 - vns[k],vcs[k],vT[k],vP[k],vV[k],vC[k],vN[k],vmu[k],vkfs[k],vkrevs[k],vHs[k],vUs[k],vGs[k],vdiffs[k],vCvave[k],vcpdivR[k],vphi[k] = calcthermo(domain,y,t,p) + vns[k], vcs[k], vT[k], vP[k], vV[k], vC[k], vN[k], vmu[k], vkfs[k], vkrevs[k], vHs[k], vUs[k], vGs[k], vdiffs[k], vCvave[k], vcpdivR[k], vphi[k] = calcthermo(domain, y, t, p) cstot[domain.indexes[1]:domain.indexes[2]] .= vcs[k] - addreactionratecontributions!(dydt,domain.rxnarray,cstot,vkfs[k],vkrevs[k]) + addreactionratecontributions!(dydt, domain.rxnarray, cstot, vkfs[k], vkrevs[k]) @views dydt[domain.indexes[1]:domain.indexes[2]] .*= vV[k] end - for (i,inter) in enumerate(interfaces) - if isa(inter,FragmentBasedReactiveFilmGrowthInterfaceConstantT) - evaluate(inter,dydt,domains[inter.domaininds[1]],vV[inter.domaininds[1]],cstot) - elseif isa(inter,AbstractReactiveInternalInterface) - evaluate(inter,dydt,domains,vT[inter.domaininds[1]],vT[inter.domaininds[2]],vphi[inter.domaininds[1]],vphi[inter.domaininds[2]],vGs[inter.domaininds[1]],vGs[inter.domaininds[2]],cstot,p) - elseif isa(inter,VaporLiquidMassTransferInternalInterfaceConstantT) - evaluate(inter,dydt,vV[inter.domaininds[1]],vV[inter.domaininds[2]],vT[inter.domaininds[1]],vT[inter.domaininds[2]],vN[inter.domaininds[1]],vN[inter.domaininds[2]],vP[inter.domaininds[1]],vP[inter.domaininds[2]],vCvave[inter.domaininds[1]],vCvave[inter.domaininds[2]],vns[inter.domaininds[1]],vns[inter.domaininds[2]],vUs[inter.domaininds[1]],vUs[inter.domaininds[2]],cstot,p) + for (i, inter) in enumerate(interfaces) + if isa(inter, FragmentBasedReactiveFilmGrowthInterfaceConstantT) + evaluate(inter, dydt, vV[inter.domaininds[1]], cstot) + elseif isa(inter, AbstractReactiveInternalInterface) + evaluate(inter, dydt, domains, vT[inter.domaininds[1]], vT[inter.domaininds[2]], vphi[inter.domaininds[1]], vphi[inter.domaininds[2]], vGs[inter.domaininds[1]], vGs[inter.domaininds[2]], cstot, p) + elseif isa(inter, VaporLiquidMassTransferInternalInterfaceConstantT) + evaluate(inter, dydt, vV[inter.domaininds[1]], vV[inter.domaininds[2]], vT[inter.domaininds[1]], vT[inter.domaininds[2]], vN[inter.domaininds[1]], vN[inter.domaininds[2]], vP[inter.domaininds[1]], vP[inter.domaininds[2]], vCvave[inter.domaininds[1]], vCvave[inter.domaininds[2]], vns[inter.domaininds[1]], vns[inter.domaininds[2]], vUs[inter.domaininds[1]], vUs[inter.domaininds[2]], cstot, p) end end - for (i,domain) in enumerate(domains) - calcdomainderivatives!(domain,dydt,interfaces;t=t,T=vT[i],P=vP[i],Us=vUs[i],Hs=vHs[i],V=vV[i],C=vC[i],ns=vns[i],N=vN[i],Cvave=vCvave[i]) + for (i, domain) in enumerate(domains) + calcdomainderivatives!(domain, dydt, interfaces; t=t, T=vT[i], P=vP[i], Us=vUs[i], Hs=vHs[i], V=vV[i], C=vC[i], ns=vns[i], N=vN[i], Cvave=vCvave[i]) end return dydt end -function dydtreactor!(dydt::RC,y::U,t::Z,domain::Q,interfaces::B,reducedmodelmappings::ReducedModelMappings,reducedmodelcache::ReducedModelCache;p::RV=SciMLBase.NullParameters(),sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:AbstractDomain} +function dydtreactor!(dydt::RC, y::U, t::Z, domain::Q, interfaces::B, reducedmodelmappings::ReducedModelMappings, reducedmodelcache::ReducedModelCache; p::RV=SciMLBase.NullParameters(), sensitivity::Bool=true) where {RC,RV,B,Z,U,Q<:AbstractDomain} dydt .= 0.0 - yunlumped = get_tmp(reducedmodelcache.yunlumped,first(y)*t) - dydtunlumped = get_tmp(reducedmodelcache.dydtunlumped,first(y)*t) - qssc = get_tmp(reducedmodelcache.qssc,first(y)*t) + yunlumped = get_tmp(reducedmodelcache.yunlumped, first(y) * t) + dydtunlumped = get_tmp(reducedmodelcache.dydtunlumped, first(y) * t) + qssc = get_tmp(reducedmodelcache.qssc, first(y) * t) yunlumped .= 0.0 dydtunlumped .= 0.0 qssc .= 0.0 - #unlump y to Z space + #unlump y to Z space @inbounds @views yunlumped[reducedmodelmappings.reducedindexes] .= y[1:length(reducedmodelmappings.reducedindexes)] - for (i,group) in enumerate(reducedmodelmappings.lumpedgroupmapping) - for (index,weight) in group + for (i, group) in enumerate(reducedmodelmappings.lumpedgroupmapping) + for (index, weight) in group @inbounds yunlumped[index] = weight * y[length(reducedmodelmappings.reducedindexes)+i] end end @inbounds @views yunlumped[end-length(domain.thermovariabledict)+1:end] .= y[end-length(domain.thermovariabledict)+1:end] - ns,cs,T,P,V,C,N,mu,kfs,krevs,Hs,Us,Gs,diffs,Cvave,cpdivR,phi = calcthermo(domain,yunlumped,t,p) + ns, cs, T, P, V, C, N, mu, kfs, krevs, Hs, Us, Gs, diffs, Cvave, cpdivR, phi = calcthermo(domain, yunlumped, t, p) #calculate qss species concentration - reducedmodelmappings.qssc!(qssc,cs,kfs,krevs) + reducedmodelmappings.qssc!(qssc, cs, kfs, krevs) @inbounds yunlumped[reducedmodelmappings.qssindexes] .= qssc .* V @views ns .= yunlumped[domain.indexes[1]:domain.indexes[2]] - cs .= ns./V + cs .= ns ./ V - addreactionratecontributions!(dydtunlumped,domain.rxnarray,cs,kfs,krevs) + addreactionratecontributions!(dydtunlumped, domain.rxnarray, cs, kfs, krevs) @fastmath dydtunlumped .*= V - calcdomainderivatives!(domain,dydtunlumped,interfaces;t=t,T=T,P=P,Us=Us,Hs=Hs,V=V,C=C,ns=ns,N=N,Cvave=Cvave) + calcdomainderivatives!(domain, dydtunlumped, interfaces; t=t, T=T, P=P, Us=Us, Hs=Hs, V=V, C=C, ns=ns, N=N, Cvave=Cvave) #lump dydt back to Y space @inbounds @views dydt[1:length(reducedmodelmappings.reducedindexes)] .= dydtunlumped[reducedmodelmappings.reducedindexes] - for (i,group) in enumerate(reducedmodelmappings.lumpedgroupmapping) - for (index,weight) in group + for (i, group) in enumerate(reducedmodelmappings.lumpedgroupmapping) + for (index, weight) in group @fastmath @inbounds dydt[length(reducedmodelmappings.reducedindexes)+i] += dydtunlumped[index] end end @@ -761,33 +751,33 @@ function dydtreactor!(dydt::RC,y::U,t::Z,domain::Q,interfaces::B,reducedmodelmap end export dydtreactor! -function jacobianyforwarddiff!(J::Q,y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - f(dy::X,y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy,y,t,domain,interfaces;p=p,sensitivity=false) - ForwardDiff.jacobian!(J,f,zeros(size(y)),y) +function jacobianyforwarddiff!(J::Q, y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + f(dy::X, y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy, y, t, domain, interfaces; p=p, sensitivity=false) + ForwardDiff.jacobian!(J, f, zeros(size(y)), y) end export jacobianyforwarddiff! -function jacobianyforwarddiff(y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - J = zeros(length(y),length(y)) - jacobianyforwarddiff!(J,y,p,t,domain,interfaces,colorvec) +function jacobianyforwarddiff(y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + J = zeros(length(y), length(y)) + jacobianyforwarddiff!(J, y, p, t, domain, interfaces, colorvec) return J end export jacobianyforwarddiff -function jacobianyforwarddiff!(J::Q,y::U,p::W,t::Z,domains::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} - f(dy::X,y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy,y,t,domains,interfaces;p=p,sensitivity=false) - ForwardDiff.jacobian!(J,f,zeros(size(y)),y) +function jacobianyforwarddiff!(J::Q, y::U, p::W, t::Z, domains::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} + f(dy::X, y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy, y, t, domains, interfaces; p=p, sensitivity=false) + ForwardDiff.jacobian!(J, f, zeros(size(y)), y) end -function jacobianyforwarddiff(y::U,p::W,t::Z,domains::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} - J = zeros(length(y),length(y)) - jacobianyforwarddiff!(J,y,p,t,domains,interfaces,colorvec) +function jacobianyforwarddiff(y::U, p::W, t::Z, domains::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} + J = zeros(length(y), length(y)) + jacobianyforwarddiff!(J, y, p, t, domains, interfaces, colorvec) return J end -function jacobianyforwarddiff!(J::Q,y::U,p::W,t::Z,domain::V,interfaces::Q3,reducedmodelmappings::ReducedModelMappings,reducedmodelcache::ReducedModelCache,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - f(dy::X,y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy,y,t,domain,interfaces,reducedmodelmappings,reducedmodelcache;p=p,sensitivity=false) - ForwardDiff.jacobian!(J,f,zeros(size(y)),y) +function jacobianyforwarddiff!(J::Q, y::U, p::W, t::Z, domain::V, interfaces::Q3, reducedmodelmappings::ReducedModelMappings, reducedmodelcache::ReducedModelCache, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + f(dy::X, y::Array{T,1}) where {T<:Real,X} = dydtreactor!(dy, y, t, domain, interfaces, reducedmodelmappings, reducedmodelcache; p=p, sensitivity=false) + ForwardDiff.jacobian!(J, f, zeros(size(y)), y) end # function jacobiany!(J::Q,y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2<:AbstractArray,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} @@ -800,40 +790,40 @@ end # end # export jacobiany! -function jacobianpforwarddiff!(J::Q,y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - function f(dy::X,p::Array{T,1}) where {X,T<:Real} - dydtreactor!(dy,y,t,domain,interfaces;p=p,sensitivity=false) +function jacobianpforwarddiff!(J::Q, y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + function f(dy::X, p::Array{T,1}) where {X,T<:Real} + dydtreactor!(dy, y, t, domain, interfaces; p=p, sensitivity=false) end dy = zeros(length(y)) - ForwardDiff.jacobian!(J,f,dy,p) + ForwardDiff.jacobian!(J, f, dy, p) end -function jacobianpforwarddiff!(J::Q,y::U,p::W,t::Z,domains::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} - function f(dy::X,p::Array{T,1}) where {X,T<:Real} - dydtreactor!(dy,y,t,domains,interfaces;p=p,sensitivity=false) +function jacobianpforwarddiff!(J::Q, y::U, p::W, t::Z, domains::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} + function f(dy::X, p::Array{T,1}) where {X,T<:Real} + dydtreactor!(dy, y, t, domains, interfaces; p=p, sensitivity=false) end dy = zeros(length(y)) - ForwardDiff.jacobian!(J,f,dy,p) + ForwardDiff.jacobian!(J, f, dy, p) end -function jacobianpforwarddiff!(J::Q,y::U,p::W,t::Z,domains::V,reducedmodelmappings::ReducedModelMappings,reducedmodelcache::ReducedModelCache,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} - function f(dy::X,p::Array{T,1}) where {X,T<:Real} - dydtreactor!(dy,y,t,domains,interfaces,reducedmodelmapping,reducedmodelcache;p=p,sensitivity=false) +function jacobianpforwarddiff!(J::Q, y::U, p::W, t::Z, domains::V, reducedmodelmappings::ReducedModelMappings, reducedmodelcache::ReducedModelCache, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} + function f(dy::X, p::Array{T,1}) where {X,T<:Real} + dydtreactor!(dy, y, t, domains, interfaces, reducedmodelmapping, reducedmodelcache; p=p, sensitivity=false) end dy = zeros(length(y)) - ForwardDiff.jacobian!(J,f,dy,p) + ForwardDiff.jacobian!(J, f, dy, p) end export jacobianpforwarddiff! -function jacobianpforwarddiff(y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - J = zeros(length(y),length(getphasespecies(domain.phase))+length(domain.phase.reactions)) - jacobianpforwarddiff!(J,y,p,t,domain,interfaces,colorvec) +function jacobianpforwarddiff(y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + J = zeros(length(y), length(getphasespecies(domain.phase)) + length(domain.phase.reactions)) + jacobianpforwarddiff!(J, y, p, t, domain, interfaces, colorvec) end -function jacobianpforwarddiff(y::U,p::W,t::Z,domains::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} - J = zeros(length(y),length(getphasespecies(domains.phase))+length(domains.phase.reactions)) - jacobianpforwarddiff!(J,y,p,t,domains,interfaces,colorvec) +function jacobianpforwarddiff(y::U, p::W, t::Z, domains::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,V<:Tuple} + J = zeros(length(y), length(getphasespecies(domains.phase)) + length(domains.phase.reactions)) + jacobianpforwarddiff!(J, y, p, t, domains, interfaces, colorvec) end export jacobianpforwarddiff @@ -850,623 +840,623 @@ export jacobianpforwarddiff # jacobianp!(domain;cs=cs,V=V,T=T,Us=Us,Cvave=Cvave,N=N,kfs=kfs,krevs=krevs,wV=wV,ratederiv=J) # end -function jacobiany(y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - J = zeros(length(y),length(y)) - jacobiany!(J,y,p,t,domain,interfaces,colorvec) +function jacobiany(y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + J = zeros(length(y), length(y)) + jacobiany!(J, y, p, t, domain, interfaces, colorvec) return J end export jacobiany -function jacobianp(y::U,p::W,t::Z,domain::V,interfaces::Q3,colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} - J = zeros(length(y),length(getphasespecies(domain.phase))+length(domain.phase.reactions)) - jacobianp!(J,y,p,t,domain,interfaces,colorvec) +function jacobianp(y::U, p::W, t::Z, domain::V, interfaces::Q3, colorvec::Q2=nothing) where {Q3,Q2,U<:AbstractArray,W,Z<:Real,V<:AbstractDomain} + J = zeros(length(y), length(getphasespecies(domain.phase)) + length(domain.phase.reactions)) + jacobianp!(J, y, p, t, domain, interfaces, colorvec) return J end export jacobianp -@inline function _spreadreactantpartials!(jac::S,deriv::Float64,rxnarray::Array{Int64,2},rxnind::Int64,ind::Int64) where {S<:AbstractArray} - @inbounds jac[rxnarray[5,rxnind],ind] += deriv - if @inbounds rxnarray[6,rxnind] !== 0 - @inbounds jac[rxnarray[6,rxnind],ind] += deriv - if @inbounds rxnarray[7,rxnind] !== 0 - @inbounds jac[rxnarray[7,rxnind],ind] += deriv - if @inbounds rxnarray[8,rxnind] !== 0 - @inbounds jac[rxnarray[8,rxnind],ind] += deriv +@inline function _spreadreactantpartials!(jac::S, deriv::Float64, rxnarray::Array{Int64,2}, rxnind::Int64, ind::Int64) where {S<:AbstractArray} + @inbounds jac[rxnarray[5, rxnind], ind] += deriv + if @inbounds rxnarray[6, rxnind] !== 0 + @inbounds jac[rxnarray[6, rxnind], ind] += deriv + if @inbounds rxnarray[7, rxnind] !== 0 + @inbounds jac[rxnarray[7, rxnind], ind] += deriv + if @inbounds rxnarray[8, rxnind] !== 0 + @inbounds jac[rxnarray[8, rxnind], ind] += deriv end end end end -@inline function _spreadproductpartials!(jac::S,deriv::Float64,rxnarray::Array{Int64,2},rxnind::Int64,ind::Int64) where {S<:AbstractArray} - @inbounds jac[rxnarray[1,rxnind],ind] += deriv - if @inbounds rxnarray[2,rxnind] !== 0 - @inbounds jac[rxnarray[2,rxnind],ind] += deriv - if @inbounds rxnarray[3,rxnind] !== 0 - @inbounds jac[rxnarray[3,rxnind],ind] += deriv - if @inbounds rxnarray[4,rxnind] !== 0 - @inbounds jac[rxnarray[4,rxnind],ind] += deriv +@inline function _spreadproductpartials!(jac::S, deriv::Float64, rxnarray::Array{Int64,2}, rxnind::Int64, ind::Int64) where {S<:AbstractArray} + @inbounds jac[rxnarray[1, rxnind], ind] += deriv + if @inbounds rxnarray[2, rxnind] !== 0 + @inbounds jac[rxnarray[2, rxnind], ind] += deriv + if @inbounds rxnarray[3, rxnind] !== 0 + @inbounds jac[rxnarray[3, rxnind], ind] += deriv + if @inbounds rxnarray[4, rxnind] !== 0 + @inbounds jac[rxnarray[4, rxnind], ind] += deriv end end end end -@inline function _jacobianynswrtns!(jac::S,rxnarray::Array{Int64,2},rxnind::Int64,cs::Array{Float64,1},kf::Float64,krev::Float64) where {S<:AbstractArray} - k=kf - if rxnarray[2,rxnind] == 0 +@inline function _jacobianynswrtns!(jac::S, rxnarray::Array{Int64,2}, rxnind::Int64, cs::Array{Float64,1}, kf::Float64, krev::Float64) where {S<:AbstractArray} + k = kf + if rxnarray[2, rxnind] == 0 deriv = k - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - elseif rxnarray[3,rxnind] == 0 - if rxnarray[1,rxnind] == rxnarray[2,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + elseif rxnarray[3, rxnind] == 0 + if rxnarray[1, rxnind] == rxnarray[2, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) end - elseif rxnarray[4,rxnind] == 0 - if rxnarray[1,rxnind]==rxnarray[2,rxnind] && rxnarray[1,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 3.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[2,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - elseif rxnarray[2,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) + elseif rxnarray[4, rxnind] == 0 + if rxnarray[1, rxnind] == rxnarray[2, rxnind] && rxnarray[1, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 3.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[2, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + elseif rxnarray[2, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) end else - if rxnarray[1,rxnind]==rxnarray[2,rxnind] && rxnarray[1,rxnind]==rxnarray[3,rxnind] && rxnarray[1,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = 4.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 4.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[2,rxnind] && rxnarray[1,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[4,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[3,rxnind] && rxnarray[1,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[2,rxnind] && rxnarray[1,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - elseif rxnarray[2,rxnind]==rxnarray[3,rxnind] && rxnarray[2,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= 3.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= 3.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[2,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[4,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[4,rxnind]) - elseif rxnarray[1,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - elseif rxnarray[2,rxnind]==rxnarray[3,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= 2.0*dderiv - @inbounds jac[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[4,rxnind]) - elseif rxnarray[2,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= 2.0*dderiv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - elseif rxnarray[3,rxnind]==rxnarray[4,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= dderiv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= 2.0*deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) + if rxnarray[1, rxnind] == rxnarray[2, rxnind] && rxnarray[1, rxnind] == rxnarray[3, rxnind] && rxnarray[1, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = 4.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 4.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[2, rxnind] && rxnarray[1, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[4, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[3, rxnind] && rxnarray[1, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[2, rxnind] && rxnarray[1, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + elseif rxnarray[2, rxnind] == rxnarray[3, rxnind] && rxnarray[2, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= 3.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= 3.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[2, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[4, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[4, rxnind]) + elseif rxnarray[1, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + elseif rxnarray[2, rxnind] == rxnarray[3, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= 2.0 * dderiv + @inbounds jac[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[4, rxnind]) + elseif rxnarray[2, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= 2.0 * dderiv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + elseif rxnarray[3, rxnind] == rxnarray[4, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= dderiv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= 2.0 * deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[1,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[1,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[2,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[2,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[3,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[3,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[2,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[3,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds jac[rxnarray[4,rxnind],rxnarray[4,rxnind]] -= deriv - @inbounds _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,rxnarray[4,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[1, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[1, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[2, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[2, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[3, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[3, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[2, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[3, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds jac[rxnarray[4, rxnind], rxnarray[4, rxnind]] -= deriv + @inbounds _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, rxnarray[4, rxnind]) end end - k=krev - if rxnarray[6,rxnind] == 0 + k = krev + if rxnarray[6, rxnind] == 0 deriv = k - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - elseif rxnarray[7,rxnind] == 0 - if rxnarray[5,rxnind] == rxnarray[6,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + elseif rxnarray[7, rxnind] == 0 + if rxnarray[5, rxnind] == rxnarray[6, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) end - elseif rxnarray[8,rxnind] == 0 - if rxnarray[5,rxnind]==rxnarray[6,rxnind] && rxnarray[5,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 3.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[6,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - elseif rxnarray[6,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) + elseif rxnarray[8, rxnind] == 0 + if rxnarray[5, rxnind] == rxnarray[6, rxnind] && rxnarray[5, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 3.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[6, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + elseif rxnarray[6, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) end else - if rxnarray[5,rxnind]==rxnarray[6,rxnind] && rxnarray[5,rxnind]==rxnarray[7,rxnind] && rxnarray[5,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = 4.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 4.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[6,rxnind] && rxnarray[5,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[8,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[7,rxnind] && rxnarray[5,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[6,rxnind] && rxnarray[5,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= 3.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - elseif rxnarray[6,rxnind]==rxnarray[7,rxnind] && rxnarray[6,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= 3.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = 3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= 3.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[6,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[8,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[8,rxnind]) - elseif rxnarray[5,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - elseif rxnarray[6,rxnind]==rxnarray[7,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[8,rxnind]) - elseif rxnarray[6,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= 2.0*deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - elseif rxnarray[7,rxnind]==rxnarray[8,rxnind] - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = 2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= 2.0*deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) + if rxnarray[5, rxnind] == rxnarray[6, rxnind] && rxnarray[5, rxnind] == rxnarray[7, rxnind] && rxnarray[5, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = 4.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 4.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[6, rxnind] && rxnarray[5, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[8, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[7, rxnind] && rxnarray[5, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[6, rxnind] && rxnarray[5, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= 3.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + elseif rxnarray[6, rxnind] == rxnarray[7, rxnind] && rxnarray[6, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= 3.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = 3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= 3.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[6, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[8, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[8, rxnind]) + elseif rxnarray[5, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + elseif rxnarray[6, rxnind] == rxnarray[7, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[8, rxnind]) + elseif rxnarray[6, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= 2.0 * deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + elseif rxnarray[7, rxnind] == rxnarray[8, rxnind] + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = 2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= 2.0 * deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) else - @inbounds @fastmath deriv = k*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[5,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[5,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[6,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[6,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[7,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[7,rxnind]) - @inbounds @fastmath deriv = k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[6,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[7,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds jac[rxnarray[8,rxnind],rxnarray[8,rxnind]] -= deriv - @inbounds _spreadproductpartials!(jac,deriv,rxnarray,rxnind,rxnarray[8,rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[5, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[5, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[6, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[6, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[7, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[7, rxnind]) + @inbounds @fastmath deriv = k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[6, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[7, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds jac[rxnarray[8, rxnind], rxnarray[8, rxnind]] -= deriv + @inbounds _spreadproductpartials!(jac, deriv, rxnarray, rxnind, rxnarray[8, rxnind]) end end end -@inline function _jacobianynswrtV!(jac::S,Vind::Int64,rxnarray::Array{Int64,2},rxnind::Int64,cs::Array{Float64,1},kf::Float64,krev::Float64) where {S<:AbstractArray} - k=kf - if rxnarray[2,rxnind]==0 +@inline function _jacobianynswrtV!(jac::S, Vind::Int64, rxnarray::Array{Int64,2}, rxnind::Int64, cs::Array{Float64,1}, kf::Float64, krev::Float64) where {S<:AbstractArray} + k = kf + if rxnarray[2, rxnind] == 0 nothing - elseif rxnarray[3,rxnind]== 0 - @inbounds @fastmath deriv = -k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]] - @inbounds jac[rxnarray[1,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[2,rxnind],Vind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,Vind) - elseif rxnarray[4,rxnind]== 0 - @inbounds @fastmath deriv = -2.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]] - @inbounds jac[rxnarray[1,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[2,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[3,rxnind],Vind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,Vind) + elseif rxnarray[3, rxnind] == 0 + @inbounds @fastmath deriv = -k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] + @inbounds jac[rxnarray[1, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[2, rxnind], Vind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, Vind) + elseif rxnarray[4, rxnind] == 0 + @inbounds @fastmath deriv = -2.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] + @inbounds jac[rxnarray[1, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[2, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[3, rxnind], Vind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, Vind) else - @inbounds @fastmath deriv = -3.0*k*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]] - @inbounds jac[rxnarray[1,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[2,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[3,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[4,rxnind],Vind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,Vind) + @inbounds @fastmath deriv = -3.0 * k * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] + @inbounds jac[rxnarray[1, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[2, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[3, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[4, rxnind], Vind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, Vind) end - k=krev - if rxnarray[6,rxnind]==0 + k = krev + if rxnarray[6, rxnind] == 0 nothing - elseif rxnarray[7,rxnind] == 0 - @inbounds @fastmath deriv = -k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]] - @inbounds jac[rxnarray[5,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[6,rxnind],Vind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,Vind) - elseif rxnarray[8,rxnind] == 0 - @inbounds @fastmath deriv = -2.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]] - @inbounds jac[rxnarray[5,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[6,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[7,rxnind],Vind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,Vind) + elseif rxnarray[7, rxnind] == 0 + @inbounds @fastmath deriv = -k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] + @inbounds jac[rxnarray[5, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[6, rxnind], Vind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, Vind) + elseif rxnarray[8, rxnind] == 0 + @inbounds @fastmath deriv = -2.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] + @inbounds jac[rxnarray[5, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[6, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[7, rxnind], Vind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, Vind) else - @inbounds @fastmath deriv = -3.0*k*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]] - @inbounds jac[rxnarray[5,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[6,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[7,rxnind],Vind] -= deriv - @inbounds jac[rxnarray[8,rxnind],Vind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,Vind) + @inbounds @fastmath deriv = -3.0 * k * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] + @inbounds jac[rxnarray[5, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[6, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[7, rxnind], Vind] -= deriv + @inbounds jac[rxnarray[8, rxnind], Vind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, Vind) end end """ This function calculates the ns partials in jacobiany involving k derivatives. dkdx is either dkdni and dkdV. x is either ni or V. """ -@inline function _jacobianykderiv!(jac::S,xind::Int64,dkfdx::Float64,dkrevdx::Float64,rxnarray::Array{Int64,2},rxnind::Int64,cs::Array{Float64,1},V::Float64) where {S<:AbstractArray} +@inline function _jacobianykderiv!(jac::S, xind::Int64, dkfdx::Float64, dkrevdx::Float64, rxnarray::Array{Int64,2}, rxnind::Int64, cs::Array{Float64,1}, V::Float64) where {S<:AbstractArray} dkdx = dkfdx - if rxnarray[2,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[1,rxnind]]*V - @inbounds jac[rxnarray[1,rxnind],xind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,xind) - elseif rxnarray[3,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*V - @inbounds jac[rxnarray[1,rxnind],xind] -= deriv - @inbounds jac[rxnarray[2,rxnind],xind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,xind) - elseif rxnarray[4,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*V - @inbounds jac[rxnarray[1,rxnind],xind] -= deriv - @inbounds jac[rxnarray[2,rxnind],xind] -= deriv - @inbounds jac[rxnarray[3,rxnind],xind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,xind) + if rxnarray[2, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[1, rxnind]] * V + @inbounds jac[rxnarray[1, rxnind], xind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, xind) + elseif rxnarray[3, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * V + @inbounds jac[rxnarray[1, rxnind], xind] -= deriv + @inbounds jac[rxnarray[2, rxnind], xind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, xind) + elseif rxnarray[4, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * V + @inbounds jac[rxnarray[1, rxnind], xind] -= deriv + @inbounds jac[rxnarray[2, rxnind], xind] -= deriv + @inbounds jac[rxnarray[3, rxnind], xind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, xind) else - @inbounds @fastmath deriv = dkdx*cs[rxnarray[1,rxnind]]*cs[rxnarray[2,rxnind]]*cs[rxnarray[3,rxnind]]*cs[rxnarray[4,rxnind]]*V - @inbounds jac[rxnarray[1,rxnind],xind] -= deriv - @inbounds jac[rxnarray[2,rxnind],xind] -= deriv - @inbounds jac[rxnarray[3,rxnind],xind] -= deriv - @inbounds jac[rxnarray[4,rxnind],xind] -= deriv - _spreadreactantpartials!(jac,deriv,rxnarray,rxnind,xind) + @inbounds @fastmath deriv = dkdx * cs[rxnarray[1, rxnind]] * cs[rxnarray[2, rxnind]] * cs[rxnarray[3, rxnind]] * cs[rxnarray[4, rxnind]] * V + @inbounds jac[rxnarray[1, rxnind], xind] -= deriv + @inbounds jac[rxnarray[2, rxnind], xind] -= deriv + @inbounds jac[rxnarray[3, rxnind], xind] -= deriv + @inbounds jac[rxnarray[4, rxnind], xind] -= deriv + _spreadreactantpartials!(jac, deriv, rxnarray, rxnind, xind) end dkdx = dkrevdx - if rxnarray[6,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[5,rxnind]]*V - @inbounds jac[rxnarray[5,rxnind],xind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,xind) - elseif rxnarray[7,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*V - @inbounds jac[rxnarray[5,rxnind],xind] -= deriv - @inbounds jac[rxnarray[6,rxnind],xind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,xind) - elseif rxnarray[8,rxnind] == 0 - @inbounds @fastmath deriv = dkdx*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*V - @inbounds jac[rxnarray[5,rxnind],xind] -= deriv - @inbounds jac[rxnarray[6,rxnind],xind] -= deriv - @inbounds jac[rxnarray[7,rxnind],xind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,xind) + if rxnarray[6, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[5, rxnind]] * V + @inbounds jac[rxnarray[5, rxnind], xind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, xind) + elseif rxnarray[7, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * V + @inbounds jac[rxnarray[5, rxnind], xind] -= deriv + @inbounds jac[rxnarray[6, rxnind], xind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, xind) + elseif rxnarray[8, rxnind] == 0 + @inbounds @fastmath deriv = dkdx * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * V + @inbounds jac[rxnarray[5, rxnind], xind] -= deriv + @inbounds jac[rxnarray[6, rxnind], xind] -= deriv + @inbounds jac[rxnarray[7, rxnind], xind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, xind) else - @inbounds @fastmath deriv = dkdx*cs[rxnarray[5,rxnind]]*cs[rxnarray[6,rxnind]]*cs[rxnarray[7,rxnind]]*cs[rxnarray[8,rxnind]]*V - @inbounds jac[rxnarray[5,rxnind],xind] -= deriv - @inbounds jac[rxnarray[6,rxnind],xind] -= deriv - @inbounds jac[rxnarray[7,rxnind],xind] -= deriv - @inbounds jac[rxnarray[8,rxnind],xind] -= deriv - _spreadproductpartials!(jac,deriv,rxnarray,rxnind,xind) + @inbounds @fastmath deriv = dkdx * cs[rxnarray[5, rxnind]] * cs[rxnarray[6, rxnind]] * cs[rxnarray[7, rxnind]] * cs[rxnarray[8, rxnind]] * V + @inbounds jac[rxnarray[5, rxnind], xind] -= deriv + @inbounds jac[rxnarray[6, rxnind], xind] -= deriv + @inbounds jac[rxnarray[7, rxnind], xind] -= deriv + @inbounds jac[rxnarray[8, rxnind], xind] -= deriv + _spreadproductpartials!(jac, deriv, rxnarray, rxnind, xind) end end -function _dydttherm(dy::X,x::T,y::Q,p::W,t::Z,domain::D,interfaces::Q3,ind::T1) where {X,Q3<:AbstractArray,T<:Real,Q<:AbstractArray,Z<:Real,D<:AbstractDomain,T1<:Integer,W} - v = [ i != ind ? convert(typeof(x),z) : x for (i,z) in enumerate(y)] - return dydtreactor!(dy,v,t,domain,interfaces;p=p,sensitivity=false) +function _dydttherm(dy::X, x::T, y::Q, p::W, t::Z, domain::D, interfaces::Q3, ind::T1) where {X,Q3<:AbstractArray,T<:Real,Q<:AbstractArray,Z<:Real,D<:AbstractDomain,T1<:Integer,W} + v = [i != ind ? convert(typeof(x), z) : x for (i, z) in enumerate(y)] + return dydtreactor!(dy, v, t, domain, interfaces; p=p, sensitivity=false) end -function jacobianytherm!(jac::Q,y::U,p::W,t::Z,domain::D,interfaces::Q3,ind::I,x::F,colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:AbstractDomain,I<:Int64,F<:Float64} - f(dy::X,x::Y) where {Y<:Real,X} = _dydttherm(dy,x,y,p,t,domain,interfaces,ind) - jac[:,ind] = ForwardDiff.derivative(f,zeros(size(y)),x) +function jacobianytherm!(jac::Q, y::U, p::W, t::Z, domain::D, interfaces::Q3, ind::I, x::F, colorvec::Q2=nothing) where {Q3<:AbstractArray,Q2,Q<:AbstractArray,U<:AbstractArray,W,Z<:Real,D<:AbstractDomain,I<:Int64,F<:Float64} + f(dy::X, x::Y) where {Y<:Real,X} = _dydttherm(dy, x, y, p, t, domain, interfaces, ind) + jac[:, ind] = ForwardDiff.derivative(f, zeros(size(y)), x) end # function jacobianp!(d::W;cs::Q,V::Y,T::Y2,Us::Z3,Cvave::Z4,N::Z5,kfs::Z,krevs::X,wV::Q2,ratederiv::Q3) where {Q3,W<:Union{ConstantTPDomain,ConstantTVDomain},Z4<:Real,Z5<:Real,Z3<:AbstractArray,Q2<:AbstractArray,Q<:AbstractArray,Y2<:Real,Y<:Real,Z<:AbstractArray,X<:AbstractArray} diff --git a/src/Simulation.jl b/src/Simulation.jl index 95668fa7..f80f614d 100644 --- a/src/Simulation.jl +++ b/src/Simulation.jl @@ -273,7 +273,7 @@ function rops(ssys::SystemSimulation, t) vCvave = Array{Any,1}(undef, length(domains)) vphi = Array{Any,1}(undef, length(domains)) ropmat = spzeros(Nrxns, Nspcs) - start = 1 + start = 0 for (k, sim) in enumerate(ssys.sims) vns[k], vcs[k], vT[k], vP[k], vV[k], vC[k], vN[k], vmu[k], vkfs[k], vkrevs[k], vHs[k], vUs[k], vGs[k], vdiffs[k], vCvave[k], vphi[k] = calcthermo(sim.domain, ssys.sol(t), t) cstot[sim.domain.indexes[1]:sim.domain.indexes[2]] = vcs[k] @@ -283,7 +283,7 @@ function rops(ssys::SystemSimulation, t) for inter in ssys.interfaces if inter isa FragmentBasedReactiveFilmGrowthInterfaceConstantT kfs, krevs = getkfskrevs(inter) - rops!(ropmat, inter.rxnarray, inter.fragmentbasedrxnarray, cstot, kfs, krevs, vV[inter.domaininds[1]], start) + rops!(ropmat, nothing, inter.rxnarray, inter.fragmentbasedrxnarray, cstot, kfs, krevs, vV[inter.domaininds[1]], inter.Mws, inter.domainfilm.indexes[1]:inter.domainfilm.indexes[2], start) start += length(kfs) elseif hasproperty(inter, :reactions) kfs, krevs = getkfskrevs(inter, vT[inter.domaininds[1]], vT[inter.domaininds[2]], vphi[inter.domaininds[1]], vphi[inter.domaininds[2]], vGs[inter.domaininds[1]], vGs[inter.domaininds[2]], cstot) @@ -294,6 +294,59 @@ function rops(ssys::SystemSimulation, t) return ropmat end +function massrops(ssys::SystemSimulation, t) + domains = getfield.(ssys.sims, :domain) + @assert any([domain isa FragmentBasedConstantTrhoDomain for domain in domains]) "massrops only works for FragmentBasedConstantTrhoDomain" + + Nrxns = sum([length(sim.domain.phase.reactions) for sim in ssys.sims]) + sum([length(inter.reactions) for inter in ssys.interfaces if hasproperty(inter, :reactions)]) + Nspcs = sum([length(getphasespecies(sim.domain.phase)) for sim in ssys.sims]) + cstot = zeros(Nspcs) + vns = Array{Any,1}(undef, length(domains)) + vcs = Array{Any,1}(undef, length(domains)) + vT = Array{Any,1}(undef, length(domains)) + vP = Array{Any,1}(undef, length(domains)) + vV = Array{Any,1}(undef, length(domains)) + vC = Array{Any,1}(undef, length(domains)) + vN = Array{Any,1}(undef, length(domains)) + vmu = Array{Any,1}(undef, length(domains)) + vkfs = Array{Any,1}(undef, length(domains)) + vkrevs = Array{Any,1}(undef, length(domains)) + vHs = Array{Any,1}(undef, length(domains)) + vUs = Array{Any,1}(undef, length(domains)) + vGs = Array{Any,1}(undef, length(domains)) + vdiffs = Array{Any,1}(undef, length(domains)) + vCvave = Array{Any,1}(undef, length(domains)) + vphi = Array{Any,1}(undef, length(domains)) + if any([domain isa FragmentBasedConstantTrhoDomain for domain in domains]) + ropmat = spzeros(Nrxns, Nspcs + 1) + else + ropmat = spzeros(Nrxns, Nspcs) + end + massropvec = spzeros(Nrxns, 1) + ropmat = spzeros(Nrxns, Nspcs) + start = 0 + for (k, sim) in enumerate(ssys.sims) + vns[k], vcs[k], vT[k], vP[k], vV[k], vC[k], vN[k], vmu[k], vkfs[k], vkrevs[k], vHs[k], vUs[k], vGs[k], vdiffs[k], vCvave[k], vphi[k] = calcthermo(sim.domain, ssys.sol(t), t) + cstot[sim.domain.indexes[1]:sim.domain.indexes[2]] = vcs[k] + rops!(ropmat, sim.domain.rxnarray, cstot, vkfs[k], vkrevs[k], vV[k], start) + start += length(vkfs[k]) + end + for inter in ssys.interfaces + if inter isa FragmentBasedReactiveFilmGrowthInterfaceConstantT + kfs, krevs = getkfskrevs(inter) + rops!(ropmat, massropvec, inter.rxnarray, inter.fragmentbasedrxnarray, cstot, kfs, krevs, vV[inter.domaininds[1]], inter.Mws, inter.domainfilm.indexes[1]:inter.domainfilm.indexes[2], start) + start += length(kfs) + elseif hasproperty(inter, :reactions) + kfs, krevs = getkfskrevs(inter, vT[inter.domaininds[1]], vT[inter.domaininds[2]], vphi[inter.domaininds[1]], vphi[inter.domaininds[2]], vGs[inter.domaininds[1]], vGs[inter.domaininds[2]], cstot) + rops!(ropmat, inter.rxnarray, cstot, kfs, krevs, inter.A, start) + start += length(kfs) + end + end + return ropmat, massropvec +end + +export massrops + """ calculates the rates of production/loss at a given time point for a single species this outputs a sparse vector of length num reactions containing the production/loss @@ -420,7 +473,7 @@ function rops!(ropvec, rarray::Array{Int64,2}, cs, kfs, krevs, V, start, ind) end -function rops!(ropmat, rarray::Array{Int64,2}, fragmentbasedrxnarray::Array{Int64,2}, cs, kfs, krevs, V, start) +function rops!(ropmat, massropvec, rarray::Array{Int64,2}, fragmentbasedrxnarray::Array{Int64,2}, cs, kfs, krevs, V, Mws, fragmentindexes, start) numfragmentbasedreacprod, numrxns = size(fragmentbasedrxnarray) half = Int(numfragmentbasedreacprod / 2) for i = 1:length(kfs) @@ -428,13 +481,23 @@ function rops!(ropmat, rarray::Array{Int64,2}, fragmentbasedrxnarray::Array{Int6 for j = 1:half if fragmentbasedrxnarray[j, i] != 0 - @fastmath ropmat[fragmentbasedrxnarray[j, i]] -= R + @fastmath ropmat[i+start, fragmentbasedrxnarray[j, i]] -= R + if !(fragmentbasedrxnarray[j, i] in fragmentindexes) + if !(massropvec == nothing) + @fastmath @inbounds massropvec[i+start] -= R * Mws[fragmentbasedrxnarray[j, i]] + end + end end end for j = half+1:numfragmentbasedreacprod if fragmentbasedrxnarray[j, i] != 0 - @fastmath ropmat[fragmentbasedrxnarray[j, i]] += R + @fastmath ropmat[i+start, fragmentbasedrxnarray[j, i]] += R + if !(fragmentbasedrxnarray[j, i] in fragmentindexes) + if !(massropvec == nothing) + @fastmath @inbounds massropvec[i+start] += R * Mws[fragmentbasedrxnarray[j, i]] + end + end end end end @@ -539,19 +602,19 @@ function getadjointsensitivities(bsol::Simulation, target::String, solver; sensa if length(bsol.domain.p) <= pethane if target in ["T", "V", "P", "mass"] || !isempty(bsol.interfaces) - du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=g, dgdu_continuous=dgdu, - dgdp_continuous=dgdp, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) + du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=g, dgdu_continuous=dgdu, + dgdp_continuous=dgdp, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) else - du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=sensg, dgdu_continuous=dsensgdu, - dgdp_continuous=dsensgdp, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) + du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=sensg, dgdu_continuous=dsensgdu, + dgdp_continuous=dsensgdp, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) end else if target in ["T", "V", "P", "mass"] || !isempty(bsol.interfaces) - du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=g, dgdu_continuous=gdurevdiff, - dgdp_continuous=dgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) + du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=g, dgdu_continuous=gdurevdiff, + dgdp_continuous=dgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) else du0, dpadj = adjoint_sensitivities(bsol.sol, solver; g=sensg, dgdu_continuous=dsensgdurevdiff, - dgdp_continuous=dsensgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) + dgdp_continuous=dsensgdprevdiff, sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) end end if normalize @@ -562,7 +625,7 @@ function getadjointsensitivities(bsol::Simulation, target::String, solver; sensa dpadj ./= bsol.sol(bsol.sol.t[end])[ind] end end - return dpadj::LinearAlgebra.Adjoint{Float64, Vector{Float64}} + return dpadj::LinearAlgebra.Adjoint{Float64,Vector{Float64}} end function getadjointsensitivities(syssim::Q, bsol::W3, target::String, solver::W; sensalg::W2=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)), @@ -597,7 +660,7 @@ function getadjointsensitivities(syssim::Q, bsol::W3, target::String, solver::W; dgdu(out, y, p, t) = ForwardDiff.gradient!(out, y -> g(y, p, t), y) dgdp(out, y, p, t) = ForwardDiff.gradient!(out, p -> g(y, p, t), p) du0, dpadj = adjoint_sensitivities(syssim.sol, solver; g=g, dgdu_continuous=dgdu, dgdp_continuous=dgdp, - sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) + sensealg=sensalg, abstol=abstol, reltol=reltol, kwargs...) if normalize for domain in domains dpadj[domain.parameterindexes[1]+length(domain.phase.species):domain.parameterindexes[2]] .*= syssim.p[domain.parameterindexes[1]+length(domain.phase.species):domain.parameterindexes[2]] @@ -606,7 +669,7 @@ function getadjointsensitivities(syssim::Q, bsol::W3, target::String, solver::W; dpadj ./= bsol.sol(bsol.sol.t[end])[ind] end end - return dpadj::LinearAlgebra.Adjoint{Float64, Vector{Float64}} + return dpadj::LinearAlgebra.Adjoint{Float64,Vector{Float64}} end export getadjointsensitivities diff --git a/src/TestReactors.jl b/src/TestReactors.jl index dc20d76f..7277ec7a 100644 --- a/src/TestReactors.jl +++ b/src/TestReactors.jl @@ -6,60 +6,60 @@ using DataFrames @testset "Test Reactors" begin -phaseDict = readinput("../src/testing/liquid_phase.rms") -spcs = phaseDict["phase"]["Species"]; #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] -rxns = phaseDict["phase"]["Reactions"]; -solv = phaseDict["Solvents"][1]; -liq = IdealDiluteSolution(spcs,rxns,solv;name="phase",diffusionlimited=true) #Define the phase (how species thermodynamic and kinetic properties calculated) - -@testset "Test liquid phase Constant T Constant V reactor simulation" begin -#Constant T and V Ideal Dilute Liquid -initialconds = Dict(["T"=>450.0,"V"=>1.0e-6*1e6,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) #Set simulation Initial Temp and Pressure -domain,y0,p = ConstantTVDomain(phase=liq,initialconds=initialconds,constantspecies=["oxygen"]) #Define the domain (encodes how system thermodynamic properties calculated) -react = Reactor(domain,y0,(0.0,140000.01);p=p) #Create the reactor object - -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-8); #solve the ode associated with the reactor - -spcnames = getfield.(liq.species,:name) -octaneind = findfirst(isequal("octane"),spcnames) -y = sol(32977.61568) -@test y[octaneind]/sum(y) ≈ 0.461599061 rtol=3e-2 #from RMG simulator I believe the slight difference is due to better calculation of diffusion limits in RMS - -#analytic jacobian vs. ForwardDiff jacobian -t=32977.61568; -y=sol(t) -ja=jacobiany(y,p,t,domain,[],nothing); -j=jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; - -@testset "Test liquid phase Constant T Constant V reactor with volumetric flow rate inlet and outlet simulation" begin - #Constant T and V Ideal Dilute Liquid - initialconds = Dict(["T"=>450.0,"V"=>1.0e-6*1e6,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) #Set simulation Initial Temp and Pressure - domain,y0,p = ConstantTVDomain(phase=liq,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - - inlet = VolumetricFlowRateInlet(domain,Dict(["T"=>450.0,"P"=>1.e5,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]),x->1.0) #set the inlet flow rate and conditions - outlet = VolumetricFlowRateOutlet(domain,x->1.0) #set the outlet flow rate - interfaces = [inlet,outlet] - react = Reactor(domain,y0,(0.0,140000.01),interfaces;p=p) #Create the reactor object - - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-8); #solve the ode associated with the reactor - - #analytic jacobian vs. ForwardDiff jacobian - t=32977.61568; - y=sol(t) - ja=jacobiany(y,p,t,domain,[],nothing); - j=jacobianyforwarddiff(y,p,t,domain,[],nothing); - @test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - - jpa=jacobianp(y,p,t,domain,[],nothing); - jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); - @test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; + phaseDict = readinput("../src/testing/liquid_phase.rms") + spcs = phaseDict["phase"]["Species"] #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] + rxns = phaseDict["phase"]["Reactions"] + solv = phaseDict["Solvents"][1] + liq = IdealDiluteSolution(spcs, rxns, solv; name="phase", diffusionlimited=true) #Define the phase (how species thermodynamic and kinetic properties calculated) + + @testset "Test liquid phase Constant T Constant V reactor simulation" begin + #Constant T and V Ideal Dilute Liquid + initialconds = Dict(["T" => 450.0, "V" => 1.0e-6 * 1e6, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantTVDomain(phase=liq, initialconds=initialconds, constantspecies=["oxygen"]) #Define the domain (encodes how system thermodynamic properties calculated) + react = Reactor(domain, y0, (0.0, 140000.01); p=p) #Create the reactor object + + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-8) #solve the ode associated with the reactor + + spcnames = getfield.(liq.species, :name) + octaneind = findfirst(isequal("octane"), spcnames) + y = sol(32977.61568) + @test y[octaneind] / sum(y) ≈ 0.461599061 rtol = 3e-2 #from RMG simulator I believe the slight difference is due to better calculation of diffusion limits in RMS + + #analytic jacobian vs. ForwardDiff jacobian + t = 32977.61568 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end + + @testset "Test liquid phase Constant T Constant V reactor with volumetric flow rate inlet and outlet simulation" begin + #Constant T and V Ideal Dilute Liquid + initialconds = Dict(["T" => 450.0, "V" => 1.0e-6 * 1e6, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantTVDomain(phase=liq, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + inlet = VolumetricFlowRateInlet(domain, Dict(["T" => 450.0, "P" => 1.e5, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]), x -> 1.0) #set the inlet flow rate and conditions + outlet = VolumetricFlowRateOutlet(domain, x -> 1.0) #set the outlet flow rate + interfaces = [inlet, outlet] + react = Reactor(domain, y0, (0.0, 140000.01), interfaces; p=p) #Create the reactor object + + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-8) #solve the ode associated with the reactor + + #analytic jacobian vs. ForwardDiff jacobian + t = 32977.61568 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end @testset "Test liquid phase Constant T Constant V reactor simulation with kLAkHCondensationEvaporationWithReservoir" begin phaseDict = readinput("../src/testing/constantkLAkH.rms") @@ -68,35 +68,35 @@ end; rxns = phaseDict["phase"]["Reactions"] solvent = phaseDict["Solvents"][1] - liq = IdealDiluteSolution(spcs,rxns,solvent;name="phase",diffusionlimited=true) + liq = IdealDiluteSolution(spcs, rxns, solvent; name="phase", diffusionlimited=true) - initialconds = Dict(["T"=>450.0,"V"=>1.0e-6*1e6,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) - domain,y0,p = ConstantTVDomain(phase=liq,initialconds=initialconds) - conds = Dict(["T"=>450.0,"P"=>1.e5,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) - interfaces = [kLAkHCondensationEvaporationWithReservoir(domain,conds)] - react = Reactor(domain,y0,(0.0,140000.01),interfaces;p=p) + initialconds = Dict(["T" => 450.0, "V" => 1.0e-6 * 1e6, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) + domain, y0, p = ConstantTVDomain(phase=liq, initialconds=initialconds) + conds = Dict(["T" => 450.0, "P" => 1.e5, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) + interfaces = [kLAkHCondensationEvaporationWithReservoir(domain, conds)] + react = Reactor(domain, y0, (0.0, 140000.01), interfaces; p=p) - sol1 = solve(react.ode,react.recommendedsolver,abstol=1e-18,reltol=1e-6); + sol1 = solve(react.ode, react.recommendedsolver, abstol=1e-18, reltol=1e-6) phaseDict = readinput("../src/testing/TdependentkLAkH.rms") spcs = phaseDict["phase"]["Species"] rxns = phaseDict["phase"]["Reactions"] solvent = phaseDict["Solvents"][1] - liq = IdealDiluteSolution(spcs,rxns,solvent;name="phase",diffusionlimited=true) - - initialconds = Dict(["T"=>450.0,"V"=>1.0e-6*1e6,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) - domain,y0,p = ConstantTVDomain(phase=liq,initialconds=initialconds) - conds = Dict(["T"=>450.0,"P"=>1.e5,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) - interfaces = [kLAkHCondensationEvaporationWithReservoir(domain,conds)] - react = Reactor(domain,y0,(0.0,140000.01),interfaces;p=p) #Create the reactor object - - sol2 = solve(react.ode,react.recommendedsolver,abstol=1e-18,reltol=1e-6); - - spcnames = getfield.(liq.species,:name) - octaneind = findfirst(isequal("octane"),spcnames) + liq = IdealDiluteSolution(spcs, rxns, solvent; name="phase", diffusionlimited=true) + + initialconds = Dict(["T" => 450.0, "V" => 1.0e-6 * 1e6, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) + domain, y0, p = ConstantTVDomain(phase=liq, initialconds=initialconds) + conds = Dict(["T" => 450.0, "P" => 1.e5, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) + interfaces = [kLAkHCondensationEvaporationWithReservoir(domain, conds)] + react = Reactor(domain, y0, (0.0, 140000.01), interfaces; p=p) #Create the reactor object + + sol2 = solve(react.ode, react.recommendedsolver, abstol=1e-18, reltol=1e-6) + + spcnames = getfield.(liq.species, :name) + octaneind = findfirst(isequal("octane"), spcnames) @test sol1(32977.61568) ≈ sol2(32977.61568) - end; + end @testset "Test vapor-liquid phase multi-domain reactor simulation with VaporLiquidMassTransferInternalInterfaceConstantT and VolumeMaintainingOutlet interface" begin input_file = "../src/testing/TdependentkLAkH.rms" @@ -106,538 +106,539 @@ end; solvent = phaseDict["Solvents"][1] gasspcs = liqspcs gasrxns = [] - liqspcnames = getfield.(liqspcs,:name) - gasspcnames = getfield.(gasspcs,:name) + liqspcnames = getfield.(liqspcs, :name) + gasspcnames = getfield.(gasspcs, :name) - gas = IdealGas(gasspcs,gasrxns;name="gas"); - liq = IdealDiluteSolution(liqspcs,liqrxns,solvent;name="liq",diffusionlimited=true) + gas = IdealGas(gasspcs, gasrxns; name="gas") + liq = IdealDiluteSolution(liqspcs, liqrxns, solvent; name="liq", diffusionlimited=true) Vliq = 1.0 Vgas = 1.0 T = 25 + 273.15 octaneconc = 6478 - tf = 3600*24 + tf = 3600 * 24 - initialconds = Dict("octane"=>octaneconc*Vliq,"T"=>T,"V"=>Vliq) - domainliq,y0liq,pliq = ConstantTVDomain(phase=liq,initialconds=initialconds); + initialconds = Dict("octane" => octaneconc * Vliq, "T" => T, "V" => Vliq) + domainliq, y0liq, pliq = ConstantTVDomain(phase=liq, initialconds=initialconds) P = 101300.0 - N2N = P*Vgas/R/T*0.8 - oxygenN = P*Vgas/R/T*0.2 - initialconds = Dict("N2"=>N2N,"oxygen"=>oxygenN,"T"=>T,"P"=>P) - domaingas,y0gas,pgas = ConstantTPDomain(phase=gas,initialconds=initialconds); - initialconds = Dict("N2"=>N2N,"oxygen"=>oxygenN,"T"=>T,"P"=>P) - inletgas = Inlet(domaingas,initialconds,x->42) + N2N = P * Vgas / R / T * 0.8 + oxygenN = P * Vgas / R / T * 0.2 + initialconds = Dict("N2" => N2N, "oxygen" => oxygenN, "T" => T, "P" => P) + domaingas, y0gas, pgas = ConstantTPDomain(phase=gas, initialconds=initialconds) + initialconds = Dict("N2" => N2N, "oxygen" => oxygenN, "T" => T, "P" => P) + inletgas = Inlet(domaingas, initialconds, x -> 42) outletgas = VolumeMaintainingOutlet(domaingas) ignoremasstransferspcnames = ["octane"] #only allowing octane to be consumed via reactions, to avoid the liquid phase to dry out completely - vl,pinter = VaporLiquidMassTransferInternalInterfaceConstantT(domaingas,domainliq,ignoremasstransferspcnames); + vl, pinter = VaporLiquidMassTransferInternalInterfaceConstantT(domaingas, domainliq, ignoremasstransferspcnames) - domains = (domainliq,domaingas) - interfaces = [vl,inletgas,outletgas] - react,y0,p = Reactor(domains,(y0liq,y0gas),(0.0,tf),interfaces,(pliq,pgas,pinter)); - sol = solve(react.ode,react.recommendedsolver,abstol=1e-18,reltol=1e-6); + domains = (domainliq, domaingas) + interfaces = [vl, inletgas, outletgas] + react, y0, p = Reactor(domains, (y0liq, y0gas), (0.0, tf), interfaces, (pliq, pgas, pinter)) + sol = solve(react.ode, react.recommendedsolver, abstol=1e-18, reltol=1e-6) name = "oxygen" - ind = findfirst(x->x==name,liqspcnames) - @test sol(sol.t[end])[ind] ≈ 0.11758959354431776 rtol=1e-5 #test there are oxygen dissolved into the liquid + ind = findfirst(x -> x == name, liqspcnames) + @test sol(sol.t[end])[ind] ≈ 0.11758959354431776 rtol = 1e-5 #test there are oxygen dissolved into the liquid end -@testset "Test liquid phase Parametrized T Constant V reactor jacobian" begin -#Parametrized T constant V Ideal Dilute Liquid -initialconds = Dict(["ts"=>[0.,600.,1200.],"T"=>[450.0,490.,500.],"V"=>1.0e-6*1e6,"octane"=>6.154e-3*1e6,"oxygen"=>4.953e-6*1e6]) -domain,y0,p = ParametrizedTConstantVDomain(phase=liq,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,140000.01),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-8); #solve the ode associated with the reactor - -#analytic jacobian vs. ForwardDiff jacobian -t=32977.61568; -y=sol(t) -ja=jacobiany(y,p,t,domain,[],nothing); -j=jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; - -@testset "Test ConstantTAPhi Reactor Simulation" begin - phaseDict = readinput("../src/testing/ORR.rms") - spcs = phaseDict["phase"]["Species"]; #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] - rxns = phaseDict["phase"]["Reactions"]; - AdivV = 1.0 - is = IdealSurface(spcs,rxns,2.486e-5;name="surf"); - initialconds = Dict(["T"=>298.0,"A"=>1.0,"Phi"=>-0.5,"A*"=>0.5,"B*"=>0.5,"OHA"=>0.0,"O2"=>0.25*AdivV,"H+"=>1.81e-6*AdivV,"H2O2aq"=>0.0*AdivV,"H2O"=>5.55e4*AdivV]) - domain,y0,p =ConstantTAPhiDomain(phase=is,initialconds=initialconds;sensitivity=false,constantspecies=["H+","O2","H2O2aq","H2O"]); - react = Reactor(domain,y0,(0.0,1e-5),p=p); - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-6); - sim = Simulation(sol,domain); - @test concentrations(sim,"OA",1e-6) ≈ 0.00013773350978007822 rtol=1e-5 - @test concentrations(sim,"A*",1e-6) ≈ 1.3559599927418817e-5 rtol=1e-5 - @test concentrations(sim,"OHA",1e-6) ≈ 0.4998487068452803 rtol=1e-5 -end - -#Use superminimal example to test -phaseDict = readinput("../src/testing/superminimal.rms") #load mechanism dictionary -spcs = phaseDict["phase"]["Species"]; #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] -rxns = phaseDict["phase"]["Reactions"]; -ig = IdealGas(spcs,rxns,name="phase") - -#Constant T and P Ideal Gas -@testset "Test constant T and P reactor simulation" begin - #Define the phase (how species thermodynamic and kinetic properties calculated) -initialconds = Dict(["T"=>1000.0,"P"=>1e5,"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp and Pressure -domain,y0,p = ConstantTPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,150.11094);p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor -sim = Simulation(sol,domain); -save(sim, "test.csv") -@test isfile("test.csv") - -spcnames = getfield.(ig.species,:name) -h2ind = findfirst(isequal("H2"),spcnames) -o2ind = findfirst(isequal("O2"),spcnames) -h2oind = findfirst(isequal("H2O"),spcnames) -y = sol(20.44002454) -N = sim.N(20.44002454) -@test y[h2ind]/N ≈ 0.412883111 rtol=1e-4 #from RMG simulator -@test y[o2ind]/N ≈ 0.200419093 rtol=1e-4 -@test y[h2oind]/N ≈ 0.386618602 rtol=1e-4 - -#analytic jacobian vs. ForwardDiff jacobian -t=20.44002454; -y=sol(t) -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) - -#sensitivities -dps = getadjointsensitivities(sim,"H2",CVODE_BDF(linear_solver=:GMRES);sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(true)),abstol=1e-12,reltol=1e-6) -react2 = Reactor(domain,y0,(0.0,150.11094);p=p,forwardsensitivities=true) -sol2 = solve(react2.ode,CVODE_BDF(linear_solver=:GMRES),abstol=1e-21,reltol=1e-7); #solve the ode associated with the reactor -sim2 = Simulation(sol2,domain) -solthreaded = threadedsensitivities(Reactor(domain,y0,(0.0,150.11094);p=p,forwardsensitivities=false); - odekwargs=Dict([:abstol=>1e-21,:reltol=>1e-7]),senskwargs=Dict([:abstol=>1e-21,:reltol=>1e-7])) -simthreaded = Simulation(solthreaded,domain) - -x,dp = extract_local_sensitivities(sol2,150.11094); -xth,dpth = extract_local_sensitivities(solthreaded,150.11094); - -ind = findfirst(isequal("H2"),sim2.names) -dpvs = [v[ind] for v in dp] -dpvsth = [v[ind] for v in dpth] -dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] -dpvsth[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] -dpvs ./= sol2(150.11094)[ind] -dpvsth ./= solthreaded(150.11094)[ind] - -rerr = (dpvs .- dps')./dpvs -rerrth = (dpvsth .- dps')./dpvsth -rerr = [isinf(x) ? 0.0 : x for x in rerr] -rerrth = [isinf(x) ? 0.0 : x for x in rerrth] -@test all((abs.(rerr) .> 1e-1).==false) -@test all((abs.(rerrth) .> 1e-1).==false) -end; - -#Constant T and P Ideal Gas -@testset "Test constant T and P reactor with interfaces simulation" begin - #Define the phase (how species thermodynamic and kinetic properties calculated) - initialconds = Dict(["T"=>1000.0,"P"=>1e5,"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp and Pressure - domain,y0,p = ConstantTPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - - interfaces = [Inlet(domain,Dict{String,Float64}("H2"=>0.67,"O2"=>0.33,"T"=>1000.0,"P"=>1e5),x->0.001), - Outlet(domain,x->0.001)] - - react = Reactor(domain,y0,(0.0,150.11094),interfaces;p=p) #Create the reactor object - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - sim = Simulation(sol,domain,interfaces); - - #analytic jacobian vs. ForwardDiff jacobian - t = 20.44002454; - y = sol(t) - ja = jacobiany(y,p,t,domain,interfaces,nothing); - j = jacobianyforwarddiff(y,p,t,domain,interfaces,nothing); - @test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - - jpa = jacobianp(y,p,t,domain,interfaces,nothing); - jp = jacobianpforwarddiff(y,p,t,domain,interfaces,nothing); - @test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) - - #sensitivities - dps = getadjointsensitivities(sim,"H2",CVODE_BDF(linear_solver=:GMRES);sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(true)),abstol=1e-12,reltol=1e-6) - react2 = Reactor(domain,y0,(0.0,150.11094),interfaces;p=p,forwardsensitivities=true) - sol2 = solve(react2.ode,CVODE_BDF(linear_solver=:GMRES),abstol=1e-21,reltol=1e-7); #solve the ode associated with the reactor - sim2 = Simulation(sol2,domain,interfaces) - - x,dp = extract_local_sensitivities(sol2,150.11094); - ind = findfirst(isequal("H2"),sim2.names) - dpvs = [v[ind] for v in dp] - dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] - dpvs ./= sol2(150.11094)[ind] - rerr = (dpvs .- dps')./dpvs - rerr = [isinf(x) ? 0.0 : x for x in rerr] - @test all((abs.(rerr) .> 1e-1).==false) - end; - -#Constant V adiabatic Ideal Gas -#uses superminimal.yml mechanism -@testset "Constant volume adiabatic reactor simulation" begin -initialconds = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp and Pressure -domain,y0,p = ConstantVDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,0.101),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - -ts = exp.(range(log(1e-15),length=10000,stop=log(0.1))) -IDT = ts[argmax(diff([sol(t)[end] for t in ts]))] #Ignition Delay Time based on argmax(dTdt(t)) - -@test IDT ≈ 0.038384723436228063 rtol=1e-5 #from Cantera simulation - -#analytic jacobian vs. ForwardDiff jacobian -t=0.01; -y = sol(t); -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) - -#sensitivities -react = Reactor(domain,y0,(0.0,0.02),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor -sim = Simulation(sol,domain) -dps = getadjointsensitivities(sim,"H2",CVODE_BDF();sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),abstol=1e-12,reltol=1e-6) -react2 = Reactor(domain,y0,(0.0,0.02);p=p,forwardsensitivities=true) -sol2 = solve(react2.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor -sim2 = Simulation(sol2,domain) - -x,dp = extract_local_sensitivities(sol2,0.02); -ind = findfirst(isequal("H2"),sim2.names) -dpvs = [v[ind] for v in dp] -dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] -dpvs ./= sol2(0.02)[ind] -rerr = (dpvs .- dps')./dpvs -rerr = [isinf(x) ? 0.0 : x for x in rerr] -@test all((abs.(rerr) .> 2e-1).==false) -end; - -#Constant P adiabatic Ideal Gas -#uses ethane.rms mechanism -@testset "Constant pressure adiabatic reactor simulation" begin - -phaseDict = readinput("../src/testing/ethane.rms") -spcs = phaseDict["phase"]["Species"] -rxns = phaseDict["phase"]["Reactions"] -ig = IdealGas(spcs,rxns,name="phase") - -initialconds = Dict(["T"=>1000.0,"P"=>2.0e5,"ethane"=>1.0,"Ar"=>1.0,"O2"=>3.5]) #Set simulation Initial Temp and Pressure -domain,y0,p = ConstantPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,1.0);p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor -sim = Simulation(sol,domain) - -ts = exp.(range(log(1e-15),length=10000,stop=log(0.2))) -IDT = ts[argmax(diff([sol(t)[end] for t in ts]))] #Ignition Delay Time based on argmax(dTdt(t)) - -@test IDT ≈ 0.07324954954380769 rtol=1e-5 - -#analytic jacobian vs. ForwardDiff jacobian -t=0.01; -y=sol(t) -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) - -#transitory sensitivities -ethaneind = findfirst(isequal("ethane"),sim.names) -(dSdt,tau) = transitorysensitivitiesfulltrapezoidal(sim,0.01) -dSdttrape = dSdt[ethaneind,:] -dSdttrapetrue = [2.9575870145096627e-7, 4.515275851956882e-11, 4.988521948240434e-11, -2.5799274590483593e-11, -3.4555131774031248e-12, 0.0, -2.1492670519414238e-7, 7.46732761666698e-9, 0.0, 0.0, -2.40138848721267e-7, 3.9219318929084755e-7, -1.4739312254182412e-8, 5.92445424297349e-7, -7.125209636476018e-8, -1.1352622702113613e-7, 7.946413524885843e-8, 6.705472353867515e-12, -4.557442966614505e-10, 7.418526742019254e-9, 8.438347173720314e-11, -1.2366741309434584e-10, -4.225417298096224e-13, 3.365005913661073e-11, -1.0695252156578608e-12, 2.0373377649853602e-16, -0.00044230108462490786, -1.0167340432033213e-8, -2.583036369059463e-13, 5.781731887238174e-15, 0.0, -4.334058036761624e-16, 0.0, 1.3168450957987063e-15, 1.1998309904180759e-6, -5.949349200507399e-6, 3.1444981902056855e-7, 1.8398290487996734e-5, 7.304780256981038e-8, 9.618451445619252e-8, 2.8366497171337888e-8, -1.364696143868719e-6, -8.810140643319245e-11, -3.962757443811278e-12, 5.027540595806555e-9, --0.00023939911435707088, -0.032852326043979294, -0.03798141508237119, -0.0033779935066780868, 7.105745741537494e-9, 1.412003505611414e-10, 1.0225614778472479e-8, 7.271674880756403e-10, 1.0603171573593312e-5, 6.554625893344657e-10, -0.0009646230043543857, 3.6697977742128043e-10, -2.6016005061672334e-8, 8.244482601003305e-10, 9.871392088202154e-8, 7.087374979644391e-8, 3.7494699026628695e-6, -0.0016360860266714356, 7.46796895065408e-10, -2.1377430063578516e-8, -1.5313395782779889e-6, --3.44814170913112e-8, -2.0205662027875063e-13, -6.167915597541991e-5, -1.0232310996433263e-8, 8.773160423979728e-13, -8.272836567208742e-12, -1.3849538738942805e-7, -2.8267415377402184e-7, -1.956160665062943e-9, 1.4249212654328193e-10, 1.4025863899596462e-13, 2.6192412351312244e-15, 6.209421075823885e-13, 7.563798137179707e-6, 1.0737707383411033e-17, 5.5720952223799324e-8, 6.707604770379481e-12, 4.083583009271183e-16, -4.9694659616325786e-11, 1.2353451155074604e-10, -2.9313710880962973e-9, 2.5209077489688276e-15, -3.605953179593259e-8, -9.262968242527441e-7, -5.267767400715836e-9, -2.0538584940509884e-12, 2.4582858956997566e-15, -7.055068170841879e-9, -1.4020276284380862e-6, -0.001988526129422606, 5.222914010991184e-6, -0.002389400035195657, 5.892619594146276e-9, -6.593316167800227e-12, 6.798280145273607e-14, -1.2498073552259318e-10, -9.721907208893787e-7, 1.8620274111689625e-10, 2.705888506809334e-7, 2.4326666876274853e-44, 9.630742927954911e-42, 2.4353199117753496e-16, 1.8705658712224538e-14, 1.5145954056505608e-19, -6.5851089037225425e-6, -8.587734090686863e-26, 2.8904881300280135e-9, 9.637715763640123e-14, -6.489237417275701e-12, 3.037266296308308e-10, 0.0016140972380429778, 5.6414646097585933e-5, 1.972981642283126e-11, 2.073029225006578e-8, 1.190672498122864e-7, 2.4661025783165292e-14, -2.8053580290286356e-9, 1.3198518652028483e-6, 2.5680388016794715e-15, 6.7963019095535e-14, -9.311987491323173e-10, 1.3580367145534529e-14, -4.143118184898953e-11, --1.605904738012102e-11, 3.72207891995559e-5, 4.849224291528469e-6, -0.0016554597624482278, -5.9196933275908064e-8, 1.73270139591348e-9, 8.521863840987097e-9, -2.4562040665363023e-6, -4.934842425177314e-6, -2.5768820349447168e-8, 3.4288679720876376e-7, -1.1635869304274178e-9, 3.650102637910092e-13, 2.568655079627211e-14, -8.203639185708453e-15, 1.3851394699508101e-14, 3.905955571296345e-12, 1.0508113259645755e-5, -3.7536060928465414e-7, 0.00023524067977726278, -0.03235148399206836, 3.843597225108039e-13, -5.452697163403704e-10, 9.281350122766843e-10, 9.181092007716816e-6, 1.1203368555573775e-5, -2.0769630397555456e-5, 3.495769120111611e-7, 1.4873642363083278e-7, 7.540628688138892e-13, 2.667344416697606e-12] -@test all((abs.(dSdttrape.-dSdttrapetrue) .> 1e-4.*abs.(dSdttrapetrue).+1e-16).==false) - -out = analyzespc(sim,"ethane",0.01) -s = getrxnanalysisstring(sim,out[1]) -@test s == "Analyzing ethane sensitivity to HO2+ethane<=>H2O2+C2H5 at a value of -0.0379814 \n\nKey branching for HO2 \nHO2+HO2<=>O2+H2O2 had a branching ratio of 0.632358 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.358444 \n\nKey branching for ethane \nOH+ethane<=>H2O+C2H5 had a branching ratio of 0.576145 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.328068 \nH+ethane<=>H2+C2H5 had a branching ratio of 0.0313989 \nO2+ethane<=>HO2+C2H5 had a branching ratio of 0.0291454 \nCH3+CH3<=>ethane had a branching ratio of 0.0165147 \nCH3+ethane<=>CH4+C2H5 had a branching ratio of 0.0145334 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\n\n" - -phaseDict = readinput("../src/testing/ethane.rms") -spcs = phaseDict["phase"]["Species"] -rxns = phaseDict["phase"]["Reactions"] -ig1 = IdealGas(spcs,[],name="phase1") -ig2 = IdealGas(spcs,rxns,name="phase2") - -initialcondsV1 = Dict(["T"=>1000.0,"P"=>2.0e5,"ethane"=>1.0,"Ar"=>1.0,"O2"=>3.5]) -domainV1,y0V1,pV1 = ConstantPDomain(phase=ig1,initialconds=initialcondsV1) #Define the domain (encodes how system thermodynamic properties calculated) -initialcondsV2 = Dict(["T"=>1000.0,"P"=>2.0e5,"ethane"=>1.0,"Ar"=>1.0,"O2"=>3.5]) -domainV2,y0V2,pV2 = ConstantPDomain(phase=ig2,initialconds=initialcondsV2) #Define the domain (encodes how system thermodynamic properties calculated) - -react,y0,p = Reactor((domainV1,domainV2),(y0V1,y0V2),(0.0,1.0),[],(pV1,pV2)); -sol = solve(react.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); -sysim = SystemSimulation(sol,(domainV1,domainV2),[],p); - -out = analyzespc(sysim.sims[2],"ethane",0.01) -s = getrxnanalysisstring(sysim.sims[2],out[1]) -@test s == "Analyzing ethane sensitivity to HO2+ethane<=>H2O2+C2H5 at a value of -0.0379814 \n\nKey branching for HO2 \nHO2+HO2<=>O2+H2O2 had a branching ratio of 0.632358 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.358444 \n\nKey branching for ethane \nOH+ethane<=>H2O+C2H5 had a branching ratio of 0.576145 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.328068 \nH+ethane<=>H2+C2H5 had a branching ratio of 0.0313989 \nO2+ethane<=>HO2+C2H5 had a branching ratio of 0.0291454 \nCH3+CH3<=>ethane had a branching ratio of 0.0165147 \nCH3+ethane<=>CH4+C2H5 had a branching ratio of 0.0145334 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\n\n" -end; - -#Parametrized T and P Ideal Gas -#uses superminimal.yml mechanism -@testset "Parametrized T and P reactor jacobian" begin -initialconds = Dict(["T"=>[1000.0,1400.0,1500.0],"ts"=>[0.,100.,200.],"P"=>[1.0e5,1.8e5,2.0e5],"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp/Pressure and Volume (function/array) -domain,y0,p = ParametrizedTPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,0.101),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - -#analytic jacobian vs. ForwardDiff jacobian -t=0.1; -y = sol(t); -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; - -#Parametrized V adiabatic Ideal Gas -#uses superminimal.yml mechanism -@testset "Parametrized volume adiabatic reactor jacobian" begin -initialconds = Dict(["T"=>1000.0,"ts"=>[0.,100.,200.],"V"=>[0.01,0.05,0.1],"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp/Pressure and Volume (function/array) -domain,y0,p = ParametrizedVDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,0.101),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - -#analytic jacobian vs. ForwardDiff jacobian -t=0.1; -y = sol(t); -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; - -#Parametrized P adiabatic Ideal Gas -#uses ethane.yml mechanism -@testset "Parametrized pressure adiabatic reactor jacobian" begin -initialconds = Dict(["T"=>1000.0,"ts"=>[0.,100.,200.],"P"=>[2.0e5,3.0e5,5.0e5],"H2"=>0.67,"O2"=>0.33]) -domain,y0,p = ParametrizedPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - -react = Reactor(domain,y0,(0.0,0.101),p=p) #Create the reactor object -sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - -#analytic jacobian vs. ForwardDiff jacobian -t=0.1; -y = sol(t); -ja=jacobiany(y,p,t,domain,[],nothing); -j = jacobianyforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(ja.-j) .> 1e-4.*abs.(j).+1e-16).==false) - -jpa=jacobianp(y,p,t,domain,[],nothing); -jp=jacobianpforwarddiff(y,p,t,domain,[],nothing); -@test all((abs.(jpa.-jp) .> 1e-4.*abs.(jp).+1e-16).==false) -end; - - - -@testset "Multi-domain ConstantV sensitivity analysis" begin - phaseDict = readinput("../src/testing/superminimal.rms") - spcs = phaseDict["phase"]["Species"] - rxns = phaseDict["phase"]["Reactions"] - ig = IdealGas(spcs,rxns,name="phase") - - initialcondsV = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainV,y0V,pV = ConstantVDomain(phase=ig,initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) - - reactV = Reactor(domainV,y0V,(0.0,0.037);p=pV) #Create the reactor object - solV = solve(reactV.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor - simV = Simulation(solV,domainV) - - initialcondsV1 = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainV1,y0V1,pV1 = ConstantVDomain(phase=ig,initialconds=initialcondsV1) #Define the domain (encodes how system thermodynamic properties calculated) - initialcondsV2 = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainV2,y0V2,pV2 = ConstantVDomain(phase=ig,initialconds=initialcondsV2) #Define the domain (encodes how system thermodynamic properties calculated) - - react,y0,p = Reactor((domainV1,domainV2),(y0V1,y0V2),(0.0,0.037),[],(pV1,pV2)); - sol = solve(react.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); - sysim = SystemSimulation(sol,(domainV1,domainV2),[],p); - - t = 0.03 - @test sol(t)[1:length(spcs)] ≈ solV(t)[1:end-2] rtol=1e-5 - @test sol(t)[length(spcs)+1:end-4] ≈ solV(t)[1:end-2] rtol=1e-5 - - dpsV = getadjointsensitivities(simV,"H2",CVODE_BDF();sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),abstol=1e-12,reltol=1e-6) - dps = getadjointsensitivities(sysim,sysim.sims[1],"H2",CVODE_BDF();sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)),abstol=1e-12,reltol=1e-6) - @test dpsV ≈ dps rtol=1e-4 -end; - -@testset "Multi-domain ConstantV and ConstantTP simulation" begin - phaseDict = readinput("../src/testing/superminimal.rms") - spcs = phaseDict["phase"]["Species"] + @testset "Test liquid phase Parametrized T Constant V reactor jacobian" begin + #Parametrized T constant V Ideal Dilute Liquid + initialconds = Dict(["ts" => [0.0, 600.0, 1200.0], "T" => [450.0, 490.0, 500.0], "V" => 1.0e-6 * 1e6, "octane" => 6.154e-3 * 1e6, "oxygen" => 4.953e-6 * 1e6]) + domain, y0, p = ParametrizedTConstantVDomain(phase=liq, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 140000.01), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-8) #solve the ode associated with the reactor + + #analytic jacobian vs. ForwardDiff jacobian + t = 32977.61568 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end + + @testset "Test ConstantTAPhi Reactor Simulation" begin + phaseDict = readinput("../src/testing/ORR.rms") + spcs = phaseDict["phase"]["Species"] #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] + rxns = phaseDict["phase"]["Reactions"] + AdivV = 1.0 + is = IdealSurface(spcs, rxns, 2.486e-5; name="surf") + initialconds = Dict(["T" => 298.0, "A" => 1.0, "Phi" => -0.5, "A*" => 0.5, "B*" => 0.5, "OHA" => 0.0, "O2" => 0.25 * AdivV, "H+" => 1.81e-6 * AdivV, "H2O2aq" => 0.0 * AdivV, "H2O" => 5.55e4 * AdivV]) + domain, y0, p = ConstantTAPhiDomain(phase=is, initialconds=initialconds; sensitivity=false, constantspecies=["H+", "O2", "H2O2aq", "H2O"]) + react = Reactor(domain, y0, (0.0, 1e-5), p=p) + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-6) + sim = Simulation(sol, domain) + @test concentrations(sim, "OA", 1e-6) ≈ 0.00013773350978007822 rtol = 1e-5 + @test concentrations(sim, "A*", 1e-6) ≈ 1.3559599927418817e-5 rtol = 1e-5 + @test concentrations(sim, "OHA", 1e-6) ≈ 0.4998487068452803 rtol = 1e-5 + end + + #Use superminimal example to test + phaseDict = readinput("../src/testing/superminimal.rms") #load mechanism dictionary + spcs = phaseDict["phase"]["Species"] #mechanism dictionaries index: phaseDict[phasename]["Species" or "Reactions"] rxns = phaseDict["phase"]["Reactions"] - ig = IdealGas(spcs,rxns,name="phase") - - initialcondsTP = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainTP,y0TP,pTP = ConstantTPDomain(phase=ig,initialconds=initialcondsTP) #Define the domain (encodes how system thermodynamic properties calculated) - - reactTP = Reactor(domainTP,y0TP,(0.0,0.04);p=pTP) #Create the reactor object - solTP = solve(reactTP.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor - - initialcondsV = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainV,y0V,pV = ConstantVDomain(phase=ig,initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) - - reactV = Reactor(domainV,y0V,(0.0,0.04);p=pV) #Create the reactor object - solV = solve(reactV.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); #solve the ode associated with the reactor - - initialcondsTP = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainTP,y0TP,pTP = ConstantTPDomain(phase=ig,initialconds=initialcondsTP) #Define the domain (encodes how system thermodynamic properties calculated) - initialcondsV = Dict(["T"=>1000.0,"P"=>10.0e5,"H2"=>0.67,"O2"=>0.33]) - domainV,y0V,pV = ConstantVDomain(phase=ig,initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) - - react,y0,p = Reactor((domainTP,domainV),(y0TP,y0V),(0.0,0.04),[],(pTP,pV)); - sol = solve(react.ode,CVODE_BDF(),abstol=1e-16,reltol=1e-6); - - t = 0.03 - @test sol(t)[1:length(spcs)] ≈ solTP(t)[1:end-1] rtol=1e-5 - print(length(sol(t)[length(spcs)+1:end-3])) - print(length(solV(t)[1:end-2])) - @test sol(t)[length(spcs)+1:end-3] ≈ solV(t)[1:end-2] rtol=1e-5 -end; - -@testset "Multi-domain Gas-Surface ConstantTP and ConstantTAPhi Simulation" begin - phaseDict = readinput("../src/testing/ch4o2cat.rms") - gasspcs = phaseDict["gas"]["Species"]; - gasrxns = phaseDict["gas"]["Reactions"]; - surfacespcs = phaseDict["surface"]["Species"] - surfacerxns = phaseDict["surface"]["Reactions"] - interfacerxns = phaseDict[Set(["gas","surface"])]["Reactions"]; - - ig = IdealGas(gasspcs,gasrxns;name="gas"); - cat = IdealSurface(surfacespcs,surfacerxns,2.486e-5;name="surface"); - - initialconds = Dict(["T"=>800.0,"P"=>1.0e5,"O2"=>0.2,"N2"=>0.7,"CH4"=>0.1]); - domaingas,y0gas,pgas = ConstantTPDomain(phase=ig,initialconds=initialconds,); - - V = 8.314*800.0/1.0e5 - A = 1.0e5*V - initialconds = Dict(["T"=>800.0,"A"=>A,"vacantX"=>cat.sitedensity*A]); - domaincat,y0cat,pcat = ConstantTAPhiDomain(phase=cat,initialconds=initialconds,); - - inter,pinter = ReactiveInternalInterfaceConstantTPhi(domaingas,domaincat,interfacerxns,800.0,A); - - react,y0,p = Reactor((domaingas,domaincat),(y0gas,y0cat),(0.0,0.1),(inter,),(pgas,pcat,pinter)); - - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-6); - - ssys = SystemSimulation(sol,(domaingas,domaincat,),(inter,),p); - - @test concentrations(ssys,"OX",0.5e-5) ≈ 8.033191655902819e-6 rtol=1e-5 - @test molefractions(ssys.sims[1],"H2O",0.5e-5) ≈ 0.10899527627867926 rtol=1e-5 -end; - -@testset "Multi-domain Gas-Surface ConstantV and ConstantTAPhi Simulation" begin - phaseDict = readinput("../src/testing/ch4o2cat.rms") - gasspcs = phaseDict["gas"]["Species"]; - gasrxns = phaseDict["gas"]["Reactions"]; - surfacespcs = phaseDict["surface"]["Species"] - surfacerxns = phaseDict["surface"]["Reactions"] - interfacerxns = phaseDict[Set(["gas","surface"])]["Reactions"]; - - ig = IdealGas(gasspcs,gasrxns;name="gas"); - cat = IdealSurface(surfacespcs,surfacerxns,2.486e-5;name="surface"); - - initialconds = Dict(["T"=>800.0,"P"=>1.0e5,"O2"=>0.2,"N2"=>0.7,"CH4"=>0.1]); - domaingas,y0gas,pgas = ConstantVDomain(phase=ig,initialconds=initialconds,); - - V = 8.314*800.0/1.0e5 - A = 1.0e5*V - initialconds = Dict(["T"=>800.0,"A"=>A,"vacantX"=>cat.sitedensity*A]); - domaincat,y0cat,pcat = ConstantTAPhiDomain(phase=cat,initialconds=initialconds,); - - inter,pinter = ReactiveInternalInterface(domaingas,domaincat,interfacerxns,A); - - react,y0,p = Reactor((domaingas,domaincat),(y0gas,y0cat),(0.0,0.1),(inter,),(pgas,pcat,pinter)); - - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-6); - - ssys = SystemSimulation(sol,(domaingas,domaincat,),(inter,),p); - - @test concentrations(ssys,"OX",0.5e-5) ≈ 1.9165723392283484e-5 rtol=1e-5 - @test molefractions(ssys.sims[1],"H2O",1e-3) ≈ 0.12732345278036702 rtol=1e-5 -end; - -@testset "Multi-domain Liquid-Film ConstantTV and FragmentBasedConstantTrhoDomain Simulation" begin - path = "../src/testing/minimal_fragment_based_film_growth_model.rms" - phaseDict = readinput(path); - liqspcs = phaseDict["liquid"]["Species"]; - liqrxns = phaseDict["liquid"]["Reactions"]; - filmspcs = phaseDict["film"]["Species"]; - filmrxns = phaseDict["film"]["Reactions"]; - interfacerxns = phaseDict[Set(["liquid","film"])]["Reactions"]; - solvent = phaseDict["Solvents"][1]; - - liq = IdealDiluteSolution(liqspcs,liqrxns,solvent;name="liquid",diffusionlimited=true); - film = FragmentBasedIdealFilm(filmspcs,filmrxns;name="film",diffusionlimited=false); - - initialconds = Dict(["T"=>298.0,"V"=>1.0,"1,3-BUTADIENE(L)"=>10000.0,"2-BUTENE(L)"=>1000.0, - "CYCLOPENTADIENE(L)"=>100.0]); - domainliq,y0liq,pliq = ConstantTVDomain(phase=liq,initialconds=initialconds,constantspecies=["1,3-BUTADIENE(L)","2-BUTENE(L)","CYCLOPENTADIENE(L)"]); - - initialconds = Dict(["T"=>298.0, "A"=>1.0, "rho"=>1.0, "mass"=>1.0, - "AR"=>0.01, "KR"=>0.01, "AH"=>40000.0, "CDB"=>10000.0, - ]); - domainfilm,y0film,pfilm = FragmentBasedConstantTrhoDomain(phase=film,initialconds=initialconds); - - inter,pinter = FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm,domainliq,interfacerxns); - - react,y0,p = Reactor((domainfilm,domainliq),(y0film,y0liq),(0.0,0.1),(inter,),(pfilm,pliq,pinter)); - - sol = solve(react.ode,react.recommendedsolver,abstol=1e-20,reltol=1e-6); - - ssys = SystemSimulation(sol,(domainfilm,domainliq,),(inter,),p); - - @test concentrations(ssys, "CDB", 0.05) ≈ 10000.322038151966 rtol=1e-5 - @test concentrations(ssys, "KR", 0.05) ≈ 1.008673416492296e-9 rtol=1e-5 -end - -@testset "Test Crash Analysis" begin - #Define the phase (how species thermodynamic and kinetic properties calculated) - phaseDict = readinput("../src/testing/superminimal_broken.rms") - spcs = phaseDict["phase"]["Species"]; - rxns = phaseDict["phase"]["Reactions"]; - ig = IdealGas(spcs,rxns;name="gas"); - initialconds = Dict(["T"=>1000.0,"P"=>1e5,"H2"=>0.67,"O2"=>0.33]) #Set simulation Initial Temp and Pressure - domain,y0,p = ConstantTPDomain(phase=ig,initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) - - react = Reactor(domain,y0,(0.0,150.11094);p=p) #Create the reactor object - sol = solve(react.ode,CVODE_BDF(),abstol=1e-20,reltol=1e-12); #solve the ode associated with the reactor - sim = Simulation(sol,domain,[],p); - - dmech = analyzecrash(sim) - @test length(dmech.rxns) == 1 - @test length(dmech.spcs) == 4 - @test dmech.rxns[1].index == 11 -end; + ig = IdealGas(spcs, rxns, name="phase") + + #Constant T and P Ideal Gas + @testset "Test constant T and P reactor simulation" begin + #Define the phase (how species thermodynamic and kinetic properties calculated) + initialconds = Dict(["T" => 1000.0, "P" => 1e5, "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantTPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 150.11094); p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + sim = Simulation(sol, domain) + save(sim, "test.csv") + @test isfile("test.csv") + + ropmat = rops(sim, "H2", 20.44002454) + @test ropmat[1] ≈ -5.304207075533127e-13 rtol = 1e-4 + + spcnames = getfield.(ig.species, :name) + h2ind = findfirst(isequal("H2"), spcnames) + o2ind = findfirst(isequal("O2"), spcnames) + h2oind = findfirst(isequal("H2O"), spcnames) + y = sol(20.44002454) + N = sim.N(20.44002454) + @test y[h2ind] / N ≈ 0.412883111 rtol = 1e-4 #from RMG simulator + @test y[o2ind] / N ≈ 0.200419093 rtol = 1e-4 + @test y[h2oind] / N ≈ 0.386618602 rtol = 1e-4 + + #analytic jacobian vs. ForwardDiff jacobian + t = 20.44002454 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + + #sensitivities + dps = getadjointsensitivities(sim, "H2", CVODE_BDF(linear_solver=:GMRES); sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(true)), abstol=1e-12, reltol=1e-6) + react2 = Reactor(domain, y0, (0.0, 150.11094); p=p, forwardsensitivities=true) + sol2 = solve(react2.ode, CVODE_BDF(linear_solver=:GMRES), abstol=1e-21, reltol=1e-7) #solve the ode associated with the reactor + sim2 = Simulation(sol2, domain) + solthreaded = threadedsensitivities(Reactor(domain, y0, (0.0, 150.11094); p=p, forwardsensitivities=false); + odekwargs=Dict([:abstol => 1e-21, :reltol => 1e-7]), senskwargs=Dict([:abstol => 1e-21, :reltol => 1e-7])) + simthreaded = Simulation(solthreaded, domain) + + x, dp = extract_local_sensitivities(sol2, 150.11094) + xth, dpth = extract_local_sensitivities(solthreaded, 150.11094) + + ind = findfirst(isequal("H2"), sim2.names) + dpvs = [v[ind] for v in dp] + dpvsth = [v[ind] for v in dpth] + dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] + dpvsth[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] + dpvs ./= sol2(150.11094)[ind] + dpvsth ./= solthreaded(150.11094)[ind] + + rerr = (dpvs .- dps') ./ dpvs + rerrth = (dpvsth .- dps') ./ dpvsth + rerr = [isinf(x) ? 0.0 : x for x in rerr] + rerrth = [isinf(x) ? 0.0 : x for x in rerrth] + @test all((abs.(rerr) .> 1e-1) .== false) + @test all((abs.(rerrth) .> 1e-1) .== false) + end + + #Constant T and P Ideal Gas + @testset "Test constant T and P reactor with interfaces simulation" begin + #Define the phase (how species thermodynamic and kinetic properties calculated) + initialconds = Dict(["T" => 1000.0, "P" => 1e5, "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantTPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + interfaces = [Inlet(domain, Dict{String,Float64}("H2" => 0.67, "O2" => 0.33, "T" => 1000.0, "P" => 1e5), x -> 0.001), + Outlet(domain, x -> 0.001)] + + react = Reactor(domain, y0, (0.0, 150.11094), interfaces; p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + sim = Simulation(sol, domain, interfaces) + + #analytic jacobian vs. ForwardDiff jacobian + t = 20.44002454 + y = sol(t) + ja = jacobiany(y, p, t, domain, interfaces, nothing) + j = jacobianyforwarddiff(y, p, t, domain, interfaces, nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, interfaces, nothing) + jp = jacobianpforwarddiff(y, p, t, domain, interfaces, nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + + #sensitivities + dps = getadjointsensitivities(sim, "H2", CVODE_BDF(linear_solver=:GMRES); sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(true)), abstol=1e-12, reltol=1e-6) + react2 = Reactor(domain, y0, (0.0, 150.11094), interfaces; p=p, forwardsensitivities=true) + sol2 = solve(react2.ode, CVODE_BDF(linear_solver=:GMRES), abstol=1e-21, reltol=1e-7) #solve the ode associated with the reactor + sim2 = Simulation(sol2, domain, interfaces) + + x, dp = extract_local_sensitivities(sol2, 150.11094) + ind = findfirst(isequal("H2"), sim2.names) + dpvs = [v[ind] for v in dp] + dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] + dpvs ./= sol2(150.11094)[ind] + rerr = (dpvs .- dps') ./ dpvs + rerr = [isinf(x) ? 0.0 : x for x in rerr] + @test all((abs.(rerr) .> 1e-1) .== false) + end + + #Constant V adiabatic Ideal Gas + #uses superminimal.yml mechanism + @testset "Constant volume adiabatic reactor simulation" begin + initialconds = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantVDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 0.101), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + + ts = exp.(range(log(1e-15), length=10000, stop=log(0.1))) + IDT = ts[argmax(diff([sol(t)[end] for t in ts]))] #Ignition Delay Time based on argmax(dTdt(t)) + + @test IDT ≈ 0.038384723436228063 rtol = 1e-5 #from Cantera simulation + + #analytic jacobian vs. ForwardDiff jacobian + t = 0.01 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + + #sensitivities + react = Reactor(domain, y0, (0.0, 0.02), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + sim = Simulation(sol, domain) + dps = getadjointsensitivities(sim, "H2", CVODE_BDF(); sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)), abstol=1e-12, reltol=1e-6) + react2 = Reactor(domain, y0, (0.0, 0.02); p=p, forwardsensitivities=true) + sol2 = solve(react2.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) #solve the ode associated with the reactor + sim2 = Simulation(sol2, domain) + + x, dp = extract_local_sensitivities(sol2, 0.02) + ind = findfirst(isequal("H2"), sim2.names) + dpvs = [v[ind] for v in dp] + dpvs[length(domain.phase.species)+1:end] .*= domain.p[length(domain.phase.species)+1:end] + dpvs ./= sol2(0.02)[ind] + rerr = (dpvs .- dps') ./ dpvs + rerr = [isinf(x) ? 0.0 : x for x in rerr] + @test all((abs.(rerr) .> 2e-1) .== false) + end + + #Constant P adiabatic Ideal Gas + #uses ethane.rms mechanism + @testset "Constant pressure adiabatic reactor simulation" begin + + phaseDict = readinput("../src/testing/ethane.rms") + spcs = phaseDict["phase"]["Species"] + rxns = phaseDict["phase"]["Reactions"] + ig = IdealGas(spcs, rxns, name="phase") + + initialconds = Dict(["T" => 1000.0, "P" => 2.0e5, "ethane" => 1.0, "Ar" => 1.0, "O2" => 3.5]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 1.0); p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) #solve the ode associated with the reactor + sim = Simulation(sol, domain) + + ts = exp.(range(log(1e-15), length=10000, stop=log(0.2))) + IDT = ts[argmax(diff([sol(t)[end] for t in ts]))] #Ignition Delay Time based on argmax(dTdt(t)) + + @test IDT ≈ 0.07324954954380769 rtol = 1e-5 + + #analytic jacobian vs. ForwardDiff jacobian + t = 0.01 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + + #transitory sensitivities + ethaneind = findfirst(isequal("ethane"), sim.names) + (dSdt, tau) = transitorysensitivitiesfulltrapezoidal(sim, 0.01) + dSdttrape = dSdt[ethaneind, :] + dSdttrapetrue = [2.9575870145096627e-7, 4.515275851956882e-11, 4.988521948240434e-11, -2.5799274590483593e-11, -3.4555131774031248e-12, 0.0, -2.1492670519414238e-7, 7.46732761666698e-9, 0.0, 0.0, -2.40138848721267e-7, 3.9219318929084755e-7, -1.4739312254182412e-8, 5.92445424297349e-7, -7.125209636476018e-8, -1.1352622702113613e-7, 7.946413524885843e-8, 6.705472353867515e-12, -4.557442966614505e-10, 7.418526742019254e-9, 8.438347173720314e-11, -1.2366741309434584e-10, -4.225417298096224e-13, 3.365005913661073e-11, -1.0695252156578608e-12, 2.0373377649853602e-16, -0.00044230108462490786, -1.0167340432033213e-8, -2.583036369059463e-13, 5.781731887238174e-15, 0.0, -4.334058036761624e-16, 0.0, 1.3168450957987063e-15, 1.1998309904180759e-6, -5.949349200507399e-6, 3.1444981902056855e-7, 1.8398290487996734e-5, 7.304780256981038e-8, 9.618451445619252e-8, 2.8366497171337888e-8, -1.364696143868719e-6, -8.810140643319245e-11, -3.962757443811278e-12, 5.027540595806555e-9, + -0.00023939911435707088, -0.032852326043979294, -0.03798141508237119, -0.0033779935066780868, 7.105745741537494e-9, 1.412003505611414e-10, 1.0225614778472479e-8, 7.271674880756403e-10, 1.0603171573593312e-5, 6.554625893344657e-10, -0.0009646230043543857, 3.6697977742128043e-10, -2.6016005061672334e-8, 8.244482601003305e-10, 9.871392088202154e-8, 7.087374979644391e-8, 3.7494699026628695e-6, -0.0016360860266714356, 7.46796895065408e-10, -2.1377430063578516e-8, -1.5313395782779889e-6, + -3.44814170913112e-8, -2.0205662027875063e-13, -6.167915597541991e-5, -1.0232310996433263e-8, 8.773160423979728e-13, -8.272836567208742e-12, -1.3849538738942805e-7, -2.8267415377402184e-7, -1.956160665062943e-9, 1.4249212654328193e-10, 1.4025863899596462e-13, 2.6192412351312244e-15, 6.209421075823885e-13, 7.563798137179707e-6, 1.0737707383411033e-17, 5.5720952223799324e-8, 6.707604770379481e-12, 4.083583009271183e-16, -4.9694659616325786e-11, 1.2353451155074604e-10, -2.9313710880962973e-9, 2.5209077489688276e-15, -3.605953179593259e-8, -9.262968242527441e-7, -5.267767400715836e-9, -2.0538584940509884e-12, 2.4582858956997566e-15, -7.055068170841879e-9, -1.4020276284380862e-6, -0.001988526129422606, 5.222914010991184e-6, -0.002389400035195657, 5.892619594146276e-9, -6.593316167800227e-12, 6.798280145273607e-14, -1.2498073552259318e-10, -9.721907208893787e-7, 1.8620274111689625e-10, 2.705888506809334e-7, 2.4326666876274853e-44, 9.630742927954911e-42, 2.4353199117753496e-16, 1.8705658712224538e-14, 1.5145954056505608e-19, -6.5851089037225425e-6, -8.587734090686863e-26, 2.8904881300280135e-9, 9.637715763640123e-14, -6.489237417275701e-12, 3.037266296308308e-10, 0.0016140972380429778, 5.6414646097585933e-5, 1.972981642283126e-11, 2.073029225006578e-8, 1.190672498122864e-7, 2.4661025783165292e-14, -2.8053580290286356e-9, 1.3198518652028483e-6, 2.5680388016794715e-15, 6.7963019095535e-14, -9.311987491323173e-10, 1.3580367145534529e-14, -4.143118184898953e-11, + -1.605904738012102e-11, 3.72207891995559e-5, 4.849224291528469e-6, -0.0016554597624482278, -5.9196933275908064e-8, 1.73270139591348e-9, 8.521863840987097e-9, -2.4562040665363023e-6, -4.934842425177314e-6, -2.5768820349447168e-8, 3.4288679720876376e-7, -1.1635869304274178e-9, 3.650102637910092e-13, 2.568655079627211e-14, -8.203639185708453e-15, 1.3851394699508101e-14, 3.905955571296345e-12, 1.0508113259645755e-5, -3.7536060928465414e-7, 0.00023524067977726278, -0.03235148399206836, 3.843597225108039e-13, -5.452697163403704e-10, 9.281350122766843e-10, 9.181092007716816e-6, 1.1203368555573775e-5, -2.0769630397555456e-5, 3.495769120111611e-7, 1.4873642363083278e-7, 7.540628688138892e-13, 2.667344416697606e-12] + @test all((abs.(dSdttrape .- dSdttrapetrue) .> 1e-4 .* abs.(dSdttrapetrue) .+ 1e-16) .== false) + + out = analyzespc(sim, "ethane", 0.01) + s = getrxnanalysisstring(sim, out[1]) + @test s == "Analyzing ethane sensitivity to HO2+ethane<=>H2O2+C2H5 at a value of -0.0379814 \n\nKey branching for HO2 \nHO2+HO2<=>O2+H2O2 had a branching ratio of 0.632358 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.358444 \n\nKey branching for ethane \nOH+ethane<=>H2O+C2H5 had a branching ratio of 0.576145 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.328068 \nH+ethane<=>H2+C2H5 had a branching ratio of 0.0313989 \nO2+ethane<=>HO2+C2H5 had a branching ratio of 0.0291454 \nCH3+CH3<=>ethane had a branching ratio of 0.0165147 \nCH3+ethane<=>CH4+C2H5 had a branching ratio of 0.0145334 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\n\n" + + phaseDict = readinput("../src/testing/ethane.rms") + spcs = phaseDict["phase"]["Species"] + rxns = phaseDict["phase"]["Reactions"] + ig1 = IdealGas(spcs, [], name="phase1") + ig2 = IdealGas(spcs, rxns, name="phase2") + + initialcondsV1 = Dict(["T" => 1000.0, "P" => 2.0e5, "ethane" => 1.0, "Ar" => 1.0, "O2" => 3.5]) + domainV1, y0V1, pV1 = ConstantPDomain(phase=ig1, initialconds=initialcondsV1) #Define the domain (encodes how system thermodynamic properties calculated) + initialcondsV2 = Dict(["T" => 1000.0, "P" => 2.0e5, "ethane" => 1.0, "Ar" => 1.0, "O2" => 3.5]) + domainV2, y0V2, pV2 = ConstantPDomain(phase=ig2, initialconds=initialcondsV2) #Define the domain (encodes how system thermodynamic properties calculated) + + react, y0, p = Reactor((domainV1, domainV2), (y0V1, y0V2), (0.0, 1.0), [], (pV1, pV2)) + sol = solve(react.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) + sysim = SystemSimulation(sol, (domainV1, domainV2), [], p) + + out = analyzespc(sysim.sims[2], "ethane", 0.01) + s = getrxnanalysisstring(sysim.sims[2], out[1]) + @test s == "Analyzing ethane sensitivity to HO2+ethane<=>H2O2+C2H5 at a value of -0.0379814 \n\nKey branching for HO2 \nHO2+HO2<=>O2+H2O2 had a branching ratio of 0.632358 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.358444 \n\nKey branching for ethane \nOH+ethane<=>H2O+C2H5 had a branching ratio of 0.576145 \nHO2+ethane<=>H2O2+C2H5 had a branching ratio of 0.328068 \nH+ethane<=>H2+C2H5 had a branching ratio of 0.0313989 \nO2+ethane<=>HO2+C2H5 had a branching ratio of 0.0291454 \nCH3+CH3<=>ethane had a branching ratio of 0.0165147 \nCH3+ethane<=>CH4+C2H5 had a branching ratio of 0.0145334 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\nAssociated key reaction path in ethane loss direction \nHO2+ethane<=>H2O2+C2H5 at path branching of 0.328068 \nHO2+C2H4<=>O2+C2H5 at path branching of 0.919341 \n\n\n" + end + + #Parametrized T and P Ideal Gas + #uses superminimal.yml mechanism + @testset "Parametrized T and P reactor jacobian" begin + initialconds = Dict(["T" => [1000.0, 1400.0, 1500.0], "ts" => [0.0, 100.0, 200.0], "P" => [1.0e5, 1.8e5, 2.0e5], "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp/Pressure and Volume (function/array) + domain, y0, p = ParametrizedTPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 0.101), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + + #analytic jacobian vs. ForwardDiff jacobian + t = 0.1 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end + + #Parametrized V adiabatic Ideal Gas + #uses superminimal.yml mechanism + @testset "Parametrized volume adiabatic reactor jacobian" begin + initialconds = Dict(["T" => 1000.0, "ts" => [0.0, 100.0, 200.0], "V" => [0.01, 0.05, 0.1], "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp/Pressure and Volume (function/array) + domain, y0, p = ParametrizedVDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 0.101), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + + #analytic jacobian vs. ForwardDiff jacobian + t = 0.1 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end + + #Parametrized P adiabatic Ideal Gas + #uses ethane.yml mechanism + @testset "Parametrized pressure adiabatic reactor jacobian" begin + initialconds = Dict(["T" => 1000.0, "ts" => [0.0, 100.0, 200.0], "P" => [2.0e5, 3.0e5, 5.0e5], "H2" => 0.67, "O2" => 0.33]) + domain, y0, p = ParametrizedPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 0.101), p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + + #analytic jacobian vs. ForwardDiff jacobian + t = 0.1 + y = sol(t) + ja = jacobiany(y, p, t, domain, [], nothing) + j = jacobianyforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(ja .- j) .> 1e-4 .* abs.(j) .+ 1e-16) .== false) + + jpa = jacobianp(y, p, t, domain, [], nothing) + jp = jacobianpforwarddiff(y, p, t, domain, [], nothing) + @test all((abs.(jpa .- jp) .> 1e-4 .* abs.(jp) .+ 1e-16) .== false) + end + + @testset "Multi-domain ConstantV sensitivity analysis" begin + phaseDict = readinput("../src/testing/superminimal.rms") + spcs = phaseDict["phase"]["Species"] + rxns = phaseDict["phase"]["Reactions"] + ig = IdealGas(spcs, rxns, name="phase") + + initialcondsV = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainV, y0V, pV = ConstantVDomain(phase=ig, initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) + + reactV = Reactor(domainV, y0V, (0.0, 0.037); p=pV) #Create the reactor object + solV = solve(reactV.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) #solve the ode associated with the reactor + simV = Simulation(solV, domainV) + + initialcondsV1 = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainV1, y0V1, pV1 = ConstantVDomain(phase=ig, initialconds=initialcondsV1) #Define the domain (encodes how system thermodynamic properties calculated) + initialcondsV2 = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainV2, y0V2, pV2 = ConstantVDomain(phase=ig, initialconds=initialcondsV2) #Define the domain (encodes how system thermodynamic properties calculated) + + react, y0, p = Reactor((domainV1, domainV2), (y0V1, y0V2), (0.0, 0.037), [], (pV1, pV2)) + sol = solve(react.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) + sysim = SystemSimulation(sol, (domainV1, domainV2), [], p) + + t = 0.03 + @test sol(t)[1:length(spcs)] ≈ solV(t)[1:end-2] rtol = 1e-5 + @test sol(t)[length(spcs)+1:end-4] ≈ solV(t)[1:end-2] rtol = 1e-5 + + dpsV = getadjointsensitivities(simV, "H2", CVODE_BDF(); sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)), abstol=1e-12, reltol=1e-6) + dps = getadjointsensitivities(sysim, sysim.sims[1], "H2", CVODE_BDF(); sensealg=InterpolatingAdjoint(autojacvec=ReverseDiffVJP(false)), abstol=1e-12, reltol=1e-6) + @test dpsV ≈ dps rtol = 1e-4 + end + + @testset "Multi-domain ConstantV and ConstantTP simulation" begin + phaseDict = readinput("../src/testing/superminimal.rms") + spcs = phaseDict["phase"]["Species"] + rxns = phaseDict["phase"]["Reactions"] + ig = IdealGas(spcs, rxns, name="phase") + + initialcondsTP = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainTP, y0TP, pTP = ConstantTPDomain(phase=ig, initialconds=initialcondsTP) #Define the domain (encodes how system thermodynamic properties calculated) + + reactTP = Reactor(domainTP, y0TP, (0.0, 0.04); p=pTP) #Create the reactor object + solTP = solve(reactTP.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) #solve the ode associated with the reactor + + initialcondsV = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainV, y0V, pV = ConstantVDomain(phase=ig, initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) + + reactV = Reactor(domainV, y0V, (0.0, 0.04); p=pV) #Create the reactor object + solV = solve(reactV.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) #solve the ode associated with the reactor + + initialcondsTP = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainTP, y0TP, pTP = ConstantTPDomain(phase=ig, initialconds=initialcondsTP) #Define the domain (encodes how system thermodynamic properties calculated) + initialcondsV = Dict(["T" => 1000.0, "P" => 10.0e5, "H2" => 0.67, "O2" => 0.33]) + domainV, y0V, pV = ConstantVDomain(phase=ig, initialconds=initialcondsV) #Define the domain (encodes how system thermodynamic properties calculated) + + react, y0, p = Reactor((domainTP, domainV), (y0TP, y0V), (0.0, 0.04), [], (pTP, pV)) + sol = solve(react.ode, CVODE_BDF(), abstol=1e-16, reltol=1e-6) + + t = 0.03 + @test sol(t)[1:length(spcs)] ≈ solTP(t)[1:end-1] rtol = 1e-5 + print(length(sol(t)[length(spcs)+1:end-3])) + print(length(solV(t)[1:end-2])) + @test sol(t)[length(spcs)+1:end-3] ≈ solV(t)[1:end-2] rtol = 1e-5 + end + + @testset "Multi-domain Gas-Surface ConstantTP and ConstantTAPhi Simulation" begin + phaseDict = readinput("../src/testing/ch4o2cat.rms") + gasspcs = phaseDict["gas"]["Species"] + gasrxns = phaseDict["gas"]["Reactions"] + surfacespcs = phaseDict["surface"]["Species"] + surfacerxns = phaseDict["surface"]["Reactions"] + interfacerxns = phaseDict[Set(["gas", "surface"])]["Reactions"] + + ig = IdealGas(gasspcs, gasrxns; name="gas") + cat = IdealSurface(surfacespcs, surfacerxns, 2.486e-5; name="surface") + + initialconds = Dict(["T" => 800.0, "P" => 1.0e5, "O2" => 0.2, "N2" => 0.7, "CH4" => 0.1]) + domaingas, y0gas, pgas = ConstantTPDomain(phase=ig, initialconds=initialconds,) + + V = 8.314 * 800.0 / 1.0e5 + A = 1.0e5 * V + initialconds = Dict(["T" => 800.0, "A" => A, "vacantX" => cat.sitedensity * A]) + domaincat, y0cat, pcat = ConstantTAPhiDomain(phase=cat, initialconds=initialconds,) + + inter, pinter = ReactiveInternalInterfaceConstantTPhi(domaingas, domaincat, interfacerxns, 800.0, A) + + react, y0, p = Reactor((domaingas, domaincat), (y0gas, y0cat), (0.0, 0.1), (inter,), (pgas, pcat, pinter)) + + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-6) + + ssys = SystemSimulation(sol, (domaingas, domaincat,), (inter,), p) + + @test concentrations(ssys, "OX", 0.5e-5) ≈ 8.033191655902819e-6 rtol = 1e-5 + @test molefractions(ssys.sims[1], "H2O", 0.5e-5) ≈ 0.10899527627867926 rtol = 1e-5 + end + + @testset "Multi-domain Gas-Surface ConstantV and ConstantTAPhi Simulation" begin + phaseDict = readinput("../src/testing/ch4o2cat.rms") + gasspcs = phaseDict["gas"]["Species"] + gasrxns = phaseDict["gas"]["Reactions"] + surfacespcs = phaseDict["surface"]["Species"] + surfacerxns = phaseDict["surface"]["Reactions"] + interfacerxns = phaseDict[Set(["gas", "surface"])]["Reactions"] + + ig = IdealGas(gasspcs, gasrxns; name="gas") + cat = IdealSurface(surfacespcs, surfacerxns, 2.486e-5; name="surface") + + initialconds = Dict(["T" => 800.0, "P" => 1.0e5, "O2" => 0.2, "N2" => 0.7, "CH4" => 0.1]) + domaingas, y0gas, pgas = ConstantVDomain(phase=ig, initialconds=initialconds,) + + V = 8.314 * 800.0 / 1.0e5 + A = 1.0e5 * V + initialconds = Dict(["T" => 800.0, "A" => A, "vacantX" => cat.sitedensity * A]) + domaincat, y0cat, pcat = ConstantTAPhiDomain(phase=cat, initialconds=initialconds,) + + inter, pinter = ReactiveInternalInterface(domaingas, domaincat, interfacerxns, A) + + react, y0, p = Reactor((domaingas, domaincat), (y0gas, y0cat), (0.0, 0.1), (inter,), (pgas, pcat, pinter)) + + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-6) + + ssys = SystemSimulation(sol, (domaingas, domaincat,), (inter,), p) + + @test concentrations(ssys, "OX", 0.5e-5) ≈ 1.9165723392283484e-5 rtol = 1e-5 + @test molefractions(ssys.sims[1], "H2O", 1e-3) ≈ 0.12732345278036702 rtol = 1e-5 + end + + @testset "Multi-domain Liquid-Film ConstantTV and FragmentBasedConstantTrhoDomain Simulation" begin + path = "../src/testing/minimal_fragment_based_film_growth_model.rms" + phaseDict = readinput(path) + liqspcs = phaseDict["liquid"]["Species"] + liqrxns = phaseDict["liquid"]["Reactions"] + filmspcs = phaseDict["film"]["Species"] + filmrxns = phaseDict["film"]["Reactions"] + interfacerxns = phaseDict[Set(["liquid", "film"])]["Reactions"] + solvent = phaseDict["Solvents"][1] + + liq = IdealDiluteSolution(liqspcs, liqrxns, solvent; name="liquid", diffusionlimited=true) + film = FragmentBasedIdealFilm(filmspcs, filmrxns; name="film", diffusionlimited=false) + + initialconds = Dict(["T" => 298.0, "V" => 1.0, "1,3-BUTADIENE(L)" => 10000.0, "2-BUTENE(L)" => 1000.0, + "CYCLOPENTADIENE(L)" => 100.0]) + domainliq, y0liq, pliq = ConstantTVDomain(phase=liq, initialconds=initialconds, constantspecies=["1,3-BUTADIENE(L)", "2-BUTENE(L)", "CYCLOPENTADIENE(L)"]) + + initialconds = Dict(["T" => 298.0, "A" => 1.0, "rho" => 1.0, "mass" => 1.0, + "AR" => 0.01, "KR" => 0.01, "AH" => 40000.0, "CDB" => 10000.0, + ]) + domainfilm, y0film, pfilm = FragmentBasedConstantTrhoDomain(phase=film, initialconds=initialconds) + + inter, pinter = FragmentBasedReactiveFilmGrowthInterfaceConstantT(domainfilm, domainliq, interfacerxns) + + react, y0, p = Reactor((domainfilm, domainliq), (y0film, y0liq), (0.0, 0.1), (inter,), (pfilm, pliq, pinter)) + + sol = solve(react.ode, react.recommendedsolver, abstol=1e-20, reltol=1e-6) + + ssys = SystemSimulation(sol, (domainfilm, domainliq,), (inter,), p) + + @test concentrations(ssys, "CDB", 0.05) ≈ 10000.322038151966 rtol = 1e-5 + @test concentrations(ssys, "KR", 0.05) ≈ 1.008673416492296e-9 rtol = 1e-5 + end + + @testset "Test Crash Analysis" begin + #Define the phase (how species thermodynamic and kinetic properties calculated) + phaseDict = readinput("../src/testing/superminimal_broken.rms") + spcs = phaseDict["phase"]["Species"] + rxns = phaseDict["phase"]["Reactions"] + ig = IdealGas(spcs, rxns; name="gas") + initialconds = Dict(["T" => 1000.0, "P" => 1e5, "H2" => 0.67, "O2" => 0.33]) #Set simulation Initial Temp and Pressure + domain, y0, p = ConstantTPDomain(phase=ig, initialconds=initialconds) #Define the domain (encodes how system thermodynamic properties calculated) + + react = Reactor(domain, y0, (0.0, 150.11094); p=p) #Create the reactor object + sol = solve(react.ode, CVODE_BDF(), abstol=1e-20, reltol=1e-12) #solve the ode associated with the reactor + sim = Simulation(sol, domain, [], p) + + dmech = analyzecrash(sim) + @test length(dmech.rxns) == 1 + @test length(dmech.spcs) == 4 + @test dmech.rxns[1].index == 11 + end end; \ No newline at end of file