kawin.solver
kawin.kawin.solver.Solver
DESolver
class DESolver(self, iterator = rk4Iterator, defaultDT = 0.1, minDtFrac = 1e-8, maxDtFrac = 1):
Generic class for ODE/PDE solvers
Generalization - coupled ODEs or PDEs (bunch of coupled ODEs) can be stated as dX/dt = f(X, t)
Parameters
----------
iterator : Iterator function
Defines what iteration scheme to use
defaultDt : float (defaults to 0.1)
Default time increment if no function is implement to estimate a good time increment
minDtFrac : float (defaults to 1e-8)
Minimum time step as a fraction of simulation time
maxDtFrac : float (defaults to 1)
Maximum time step as a fraction of simulation time
def DESolver.setFunctions(self, preProcess = None, postProcess = None, printHeader = None, printStatus = None):
Sets functions before solving
If any of these are not defined, then the corresponding function will be the default defined here
def DESolver.setdXdtFunctions(self, f, correctdXdt, getDt, flattenX, unflattenX):
def DESolver.defaultDtFunc(self, dXdt):
Returns the default time increment
def DESolver.defaultPreProcess(self):
Default pre-processing function before an iteration
def DESolver.defaultPostProcess(self, currTime, X_new):
Default post-processing function after an iteration
def DESolver.defaultPrintHeader(self):
Default print function before solving
def DESolver.defaultPrintStatus(self, iteration, modeltime, simTimeElapsed):
Default print function for when n iterations passed and verbose is true
def DESolver.correctdXdtNotImplemented(self, dt, x, dXdt):
Default function to correct dXdt
def DESolver.flattenXNotImplemented(self, X):
Default flattenX function, which assumes X is in the correct format
def DESolver.unflattenXNotImplemented(self, X_flat, X_ref):
Default unflattenX function which assumes X is in the correct format
def DESolver._getdXdt(self, t, x, getDt = False):
Wrapper around getdXdt which will handle the following:
Handle flattening/unfalttening the x and dx/dt arrays
Calculate dt if not supplied
The API for the iterator will be that all arrays are 1D np.arrays where operators will be trivial
Parameters
----------
t : float
Time
x : 1D np.array
Model values
getDt : bool
Will calculate dt if True
def DESolver._updateX(self, x, dxdt, dt):
Helper function that hides the correctdXdt function
The API for the iterator will be that all arrays are 1D np.arrays where operators will be trivial
Parameters
----------
x : 1D np.array
Model values
dxdt : 1D np.array
Derivatives at x
dt : float
Time step
def DESolver.solve(self, t0, X0, tf, verbose = False, vIt = 10):
Solves dX/dt over a time increment
This will be the main function that a model will use
Steps during each iteration
1. Print status if vIt iterations passed
2. preProcess
3. Iterate
4. Update current time
5. postProcess
Parameters
----------
f : function
dX/dt - function taking in time and returning dX/dt
t0 : float
Starting time
X0 : list of arrays
X at time t
tf : float
Final time
verbose: bool (defaults to False)
Whether to print status
vIt : integer (defaults to 10)
Number of iterations to print status
kawin.kawin.solver.Iterators
def explicitEulerIterator(f, t, X_old, updateX):
Explicit euler iteration scheme
Defined by:
dXdt = f(t, X_n)
X_n+1 = X_n + f(t, X_n) * dt
Parameters
----------
f : function
dX/dt - function taking in time and X and returning dX/dt
t : float
Current time
X_old : list of arrays
X at time t
updateX : function
Helper function to handle any correction to dxdt
Takes in X_old, dxdt, dt and returns X_new
Returns
-------
X_new : unformatted list of floats
New values of X in format of X_old
dt : float
Time step
def rk4Iterator(f, t, X_old, updateX):
4th order Runga Kutta iteration scheme
Defined by:
k1 = f(t, X_n)
k2 = f(t + dt/2, X_n + k1 * dt/2)
k3 = f(t + dt/2, X_n + k2 * dt/2)
k4 = f(t + dt, X_n, k3 * dt)
X_n+1 = X_n + 1/6 * (k1 + 2*k2 + 2*k3 + k4) * dt
Parameters
----------
f : function
dX/dt - function taking in time and X and returning dX/dt
t : float
Current time
X_old : list of arrays
X at time t
updateX : function
Helper function to handle any correction to dxdt
Takes in X_old, dxdt, dt and returns X_new
Returns
-------
X_new : unformatted list of floats
New values of X in format of X_old
dt : float
Time step, important if modified from dtfunc