Error when calculating RHS of reaction ode "no method matching Float64(::Num)"

I have some code that uses a function to calculate some changes in concentration, but I get an error of:

ERROR: LoadError: MethodError: no method matching Float64(::Num)
Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  (::Type{T})(::T) where T<:Number at boot.jl:760
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50

I have attached the code below:

using DifferentialEquations
@parameters t c0[1:4] Ke[1:2] kb[1:2] aw aw² aw³ ρ ζ ρζ ρζ² γ γ² T

# Calculate parameters
ρ = 0.592
ζ = 1.0
ρζ  = ρ*ζ
ρζ² = ρζ*ρζ
ρζ³ = ρζ*ρζ²

aw = 0.995
aw² = aw*aw
aw³ = aw*aw²

γ = 1.08
γ² = γ*γ

T = 590.0
# calculate equilibrium constants
Ke[01] = (1.0E-06)*10.0^(-4.098 + (-3245.2/T) + (2.2362E+05/(T^2)) + (-3.9984E+07/(T^3)) + (log10(ρ) * (13.957 + (-1262.3/T) + (8.5641E+05/(T^2)))) )
Ke[02] = 10^(28.6059+0.012078*T+(1573.21/T)-13.2258*log10(T))

# calculate backward rate constants
kb[01] = Ke[01]*ρζ²/γ²
kb[02] = Ke[02]*γ/ρζ


# set initial concentrations
c0 = [0.09897, 0.01186, 2.94e-5, 4.17e-8]

function oderhs(c,Ke,kb,aw,aw²,aw³,ρζ,ρζ²,ρζ³,γ,γ²)
      # rename c to their corresponding species
      H₃BO₃   = c[1]; H₄BO₄⁻ = c[2]; OH⁻ = c[3]; H⁺ = c[4];
      # rename Ke to their corresponding reactions
      Ke_iw1 = Ke[1]; Ke_ba1 = Ke[2];
      # rename kb to their corresponding reactions
      kb_iw1 = kb[1]; kb_ba1 = kb[2];
      # determine the rate of reaction for each reaction
      r_iw1 = kb_iw1*(H⁺*OH⁻ - Ke_iw1*ρζ²*aw/γ²)
      r_ba1 = kb_ba1*(H₄BO₄⁻ - H₃BO₃*OH⁻*Ke_ba1*γ/ρζ)
      dc = zeros(eltype(c),4)
      # calculate the change in species concentration
      dc[1] = r_ba1
      dc[2] = r_ba1
      dc[3] = r_iw1 + r_ba1
      dc[4] = r_iw1
      
      return dc
end

dc = oderhs(c0,Ke,kb,aw,aw²,aw³,ρζ,ρζ²,ρζ³,γ,γ²)

Welcome to Julia discourse! For future reference, please post the full stack trace along with the error message – it makes it easier for others to help you. :slight_smile:

Parameters are not for assigning to. The way they work is that you provide values for them during the construction of an ODEProblem from and ODESystem by providing a mapping from parameters to values. The error messages you are getting are b/c you have overwritten the parameters with literal numbers, and the final one that you listed is b/c you are trying to put a symbolic parameter into the array dc that expects literal numbers. Here’s an example for how the process works. (I assume you are getting @parameters from ModelingToolkit.)

If you haven’t already, check out Catalyst.jl for simulating chemical reactions.

Thanks!
What fixes the problem is changing
dc = zeros(eltype(c),4)
to
dc = zeros(Num,4)

given from:

https://stackoverflow.com/questions/68328599/error-when-calculating-rhs-of-ode-no-method-matching-float64num/68330792#68330792

Thank you for suggesting Catalyst.jl, but I haven’t had success coupling the results from it to my transport solver (thus the need to hard-code the reaction rates instead) :slight_smile:

1 Like

Open an issue?

1 Like

Done
https://github.com/SciML/Catalyst.jl/issues/346