How to integrate python class attributes #163
Replies: 3 comments 7 replies
-
I think it might be worth dragging James Rosindell into this discussion - it was an area he had some great thoughts about in the original VR meeting. And Jaideep! More broadly, I'm not quite sure I understand the problem:
Bear in mind that ODEs are witchcraft to me.... |
Beta Was this translation helpful? Give feedback.
-
Thought about/discussed my difficulty a bit more so hopefully this statement of the problem will be a bit more clear. In essence the odeint(function, y0, t, k)
As the initial condition The trouble I'm having is in working out how to fit this into the object based approach I have used for defining I can see how you can setup I've though of a number of ways to potentially resolve this problem:
Option 2 feels like the most plausible solution to me. But I'm quite worried that it would result in a monstrously large class, so wonder if there is a smarter approach here. @dalonsoa @alexdewar let me know if you have any thoughts on this. Cheers! |
Beta Was this translation helpful? Give feedback.
-
The documentation suggests using This might be a stupid question, but are you doing separate integrations or are they all combined somehow? |
Beta Was this translation helpful? Give feedback.
-
I've started taking a look at how to integrate my model over time. The thing I'm having trouble getting my head around is how to use
python
's ODE solvers with dimensions that are stored as class attributes.I've been considering using
scipy
's builtinodeint
solver.Where
y0
is a vector of initial values,t
is the integration time span, andmodel
is the function to be integrated. My understanding is thatmodel
should have a form where the amount to update each dimension is returned as part of a vector.I understand how to set up simple cases like the above. However, in our case a lot of what we will be interested in integrating won't be supplied as a vector but as attributes of a class (which are stored as vectors).
I can write a function where all pool updates are calculated and returned.
This could in theory be integrated (by adding an extra vector argument
pools
to be integrated over). However,soil_carbon
andsoil_nitrogen
would not be updated between integration steps. The easy fix for this is to add a step that updates each set of pools before the update amount is returned.The above should definitely work if we were using a simple time step, i.e.$y_1 = y_0 + dt*\Delta y$ . However, the
scipy
integrators generally use more complex integration schemes (mainly Runga-Kutta methods), wherey
values are calculated for multiple time points (e.g. the midpoint) in order to improve the accuracy of the calculation. My concern is how to make sure that the attributes ofSoilCarbonPools
are not updated more or less times than they should be.Something I considered was recreating
SoilCarbonPools
for every time step, but this feels like it would be costly and should be avoided if possible.I was wondering if there was an established best practice for tackling this kind of problem? And if anyone had any suggestions about how to tackle this?
Beta Was this translation helpful? Give feedback.
All reactions