0

help, please - I can't understand my own code! lol I'm fairly new at python and after many trials and errors, I got my code to work, but there is one particular part of it I don't understand.

In the code below, I'm solving a fairly basic ODE through scipy's odeint-function. My goal is then to build on this blue-print for more complicated systems.

My question(s): How could I call the method .reaction_rate_simple without any arguments and without the closing parenthesis? What does this mean in python? Should I use a static method here somewhere?

If anyone has any feedback on this - maybe this is a crappy piece of code and there's a better way of solving it!

I am very thankful for any response and help!

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

class batch_reator:

    def __init__(self, C_init, volume_reactor, time_init, time_end):
        self.C_init = C_init
        self.volume = volume_reactor
        self.time_init = time_init
        self.time_end = time_end
        self.operation_time = (time_end - time_init)
    
    def reaction_rate_simple(self, concentration_t, t, stoch_factor, order, rate_constant):
        reaction_rate = stoch_factor * rate_constant * (concentration_t ** order)
        return reaction_rate

    def equations_system(self, kinetics):
        dCdt = kinetics
        return dCdt


C_init = 200
time_init, time_end = 0, 1000 
rate_constant, volume_reactor, order, stoch_factor = 0.0001, 10, 1, -1
time_span = np.linspace(time_init, time_end, 100)
 
Batch_basic = batch_reator(C_init, volume_reactor, time_init, time_end)       
kinetics = Batch_basic.reaction_rate_simple

sol = odeint(Batch_basic.equations_system(kinetics), Batch_basic.C_init, time_span, args=(stoch_factor, order, rate_constant))

plt.plot(time_span, sol)
plt.show() 
J Faller
  • 5
  • 1

1 Answers1

0

I assume you are referring to the line

kinetics = Batch_basic.reaction_rate_simple

You are not calling it, you are saving the method as a variable and then passing that method to equations_system(...), which simply returns it. I am not familiar with odeint, but according to the documentation, it accepts a callable, which is what you are giving it.

In python functions, lambdas, classes are all callable and can be assigned to variable and passed to functions and called as needed.

In this particular case the callback definition from the odeint docs say

func callable(y, t, …) or callable(t, y, …) Computes the derivative of y at t. If the signature is callable(t, y, ...), then the argument tfirst must be set True.

So the first two arguments are passed in by odeint, and the other three are coming from the arguments specified by you.

saquintes
  • 1,074
  • 3
  • 11
  • Yes, I was referring to this exact line! I had never seen a method being saved as a variable before, which is why I didn't quite get it. Thank you! I'm still confused about the line after that - where does the variable `kinetics`, which is holding the method `reaction_rate_simple` get all its arguments from? 3 are coming from `args` and I assume the other two are "ignored" when using odeint... Anyway, this is a different question I can try to find out! Thank you very much for taking the time to help me, I appreciate it! – J Faller Dec 03 '20 at 18:09
  • Updated the answer to address that piece as well. – saquintes Dec 03 '20 at 20:08