How to pass an argument to a function defined within a module?

Hello! I need to solve two systems of differential equations that I defined within a module. The value of a parameter in the second system depends on an external function that is also defined in the same module and receives the solution of the first system as an argument. How can I pass the solution of the first system of differential equations to the function when solving the second system?

I leave an example of the code below:

using DifferentialEquations

module mm
      # Required libraries 
      using DifferentialEquations
      using ParameterizedFunctions

      # First ODE
      function odeFB(du, u, p, t)
          Ge, k1, k2, km2, k3, k4, km4, k5, km5, k6, Km, Vmax = p 
          du[1] = ((Vmax * Ge * u[1])/(Km + u[1])) - (k4 * u[1]) - (k2 * u[3] * u[1]) + (km2 * u[3]) + (km4 * u[2])
          du[2] = (k4 * u[1])- (k5 * u[2]) - (km4 * u[2]) + (km5 * u[3])
          du[3] =  - (k3 * u[3]) + (k5 * u[2]) - (km5 * u[3])
      end 
      
      # Second ODE 
      function odeNF(du, u, p, t)
          Ge, k1, k2, km2, k3, k4, km4, k5, km5, k6, Km, Vmax = p 
          du[1] = ((Vmax * Ge * u[1])/(Km + u[1])) - (k4 * u[1]) - (k2 * u[3] * u[1]) + (km2 * u[3]) + (km4 * u[2])
          du[2] = (k4 * f(t, solFBR))- (k5 * u[2]) - (km4 * u[2]) + (km5 * u[3])
          du[3] =  - (k3 * u[3]) + (k5 * u[2]) - (km5 * u[3])
      end 

      # External function to calculate k6 in the second ODE 
      function f(t, solFBR)
          if t > 0
              i = findfirst(times .>= t)
              return ((((solFBR[1,i] - solFBR[1,i-1]) / (solFBR.t[i] - solFBR.t[i-1])) * (t .- solFBR.t[i-1])) .+ solFBR[1,i-1])
          else
              return sol[1,1]
          end
      end
  end 

# Solve first system of ODEs
x0 = ones(3)
p = [24.150000000000002, 0.34, 5.3, 5.3, 4.0, 2.0, 2.0, 2.3, 2.3, NaN, 1100.0, 1400.0]
probFB = ODEProblem(mm.odeFB,x0,1000,p)

solFB = solve(probFB, reltol = 1e-12);

# Solve second system of ODEs
probNF = ODEProblem(mm.odeNF,x0,1000,p)
solNF = solve(probNF, reltol = 1e-12);

I am not 100% sure I understand your task, but here is my guess

  1. You could make solFB a parameter for the second ODE? That way it would appear (maybe last?) in the line
Ge, k1, k2, km2, k3, k4, km4, k5, km5, k6, Km, Vmax, solFB = p 

You could maybe also omit k6 in that set of parameters since you second line in said second ODE could be

k6 = f(t, solFB)

So for completeness, before your second solve call, do

# Was NaN k6? That's what I removed
p2 = [24.150000000000002, 0.34, 5.3, 5.3, 4.0, 2.0, 2.0, 2.3, 2.3, 1100.0, 1400.0, solFB]
probNF = ODEProblem(mm.odeNF,x0,1000,p2) # note the p2 here

It works great, thank you very much!

1 Like