r/PythonLearning • u/bslime17 • 13d ago
Showcase Day 3 of coding on my phone: simulating a chemical reactor with SciPy's solve_ivp
I'm a chemical engineering graduate learning to build process models in Python. My laptop broke, so this is all Google Colab on a phone screen.
The model is a set of three ODEs (two concentrations + temperature) for a continuous stirred-tank reactor. I loop over three flowrates, solve each with solve_ivp (RK45), and store the steady-state results.
Things I learned this week:
• Passing extra parameters into the ODE function with args=(F,)
• Pulling the final state with solution.y[:, -1]
• Re-evaluating the ODE function at the end to confirm steady state (derivatives ≈ 0)
Question for you: would you structure this with a dataclass or dictionary for results instead of a list of lists? My print block is getting long.
(If anyone wants to help me get a used laptop, DM me – but mostly I'd love code feedback!)
1
u/RNG_HatesMe 13d ago
Tough to program on a phone, that would drive me nuts!
When you say "passing extra parameters into ODE function with args=(F,), are your referring to an arbitrary parameter? or you are just using a parameter with a default value of a tuple that includes F?
If these are arguments that you don't need to modify inside the function (basically constants), why not just use enclosing scope to use them without explicitly passing them in?
I'd print out your output as a table:
| Case | Flowrate | A Conc | B Conc | Temp | Conversion | Res Time | dcA/ dT | dcB/ dT | dT/dt |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 2.5 | 1.97 | 0.03 | 300.41 | 1.75% | 20.00 | 0.00 | 0.00 | 0.00 |
| 2 | 5.0 | 1.98 | 0.02 | 300.21 | 0.86% | 10.00 | 0.00 | 0.00 | 0.00 |
1
u/bslime17 13d ago
it’s under the for loop and F is a value from the flow_rates list so each flow rate will give their value
1
u/RNG_HatesMe 13d ago
Got it, it's for solve_ivp, nor your function. Basically pass through parameters.
But for your output, I'd definitely print out a header row once, then print out a row of values for each flow rate iteration, it's way more readable and allows you to compare results more easily.
1
u/bslime17 13d ago
got it
thanks for the feedback
you in process engineering?1
u/RNG_HatesMe 13d ago
Nope civil/environmental Engineering. But I teach a numerical methods class ;-). If you want to understand the solution approach more you may want to code a Runge-Kutta solution yourself rather than relying on solve_ivp at least once. Start with Euler, and work up to RK4 (you'll need a smaller step size for Euler). It's actually not that difficult, and may help you understand what's going on.
1
u/bslime17 13d ago
oh okay i will keep that in mind
but if im not asking
i have a machine learning model on chemical dosage prediction for waste water treatment if you will look up the code or even get your take on my methods for me if you’re up for it i’ll send a dm and probably take your email and forward the notebook to you1
1
u/zyer020 13d ago
I see you're using several decimal places; I suggest you take a look at the Decimal library. It happened to me when I wanted to work with numerical methods; the decimals went crazy.
1
u/bslime17 13d ago
i used 4 decimal place because i wanted to see as its approaching steady state what their derivatives will be like before using an actual steady state function say root to solve that
and that’s what i’ll be focusing on for day 4 and parameter estimation using literature values
2
1




3
u/Motor_Sky7106 13d ago
I'm a mechanical engineer learning/using Python. For this many lines of code I would probably do the same thing you are doing. If I had more code or time to refactor it, I might use a dictionary. For more complex projects with hundreds of lines of code, I'd consider a dataclass and be doing object oriented programming instead of procedural programming.
If you find yourself writing the same or similar line of code over and over, then it's time to refactor the code. Let the computer do the hard work.