Hello to the community,
I have started to learn python for about 2 months, and currently would like to see if it is possible to solve ODE's in parallel in python.
I have this code for solving in serial the Lotka-Volterra equations.
import numpy as np
from scipy import integrate
import pandas as pd
def derivative(X, t, alpha, beta, delta, gamma):
x, y = X
dotx = x * (alpha - beta * y)
doty = y * (-delta + gamma * x)
return np.array([dotx, doty])
delta = 1.
gamma = 1.
x0 = 4.
y0 = 2.
Nt = 1000
tmax = 30.
t = np.linspace(0.,tmax, Nt)
X0 = [x0, y0]
betas = np.arange(0.9, 1.4, 0.1)
alphas = np.arange(0.9, 1.4, 0.1)
df = pd.DataFrame({"time": t})
for beta, i in zip(betas, range(len(betas))):
for alpha, j in zip(alphas, range(len(alphas))):
print("solving for: \n alpha: %s \n beta: %s \n" %(alpha, beta))
res = integrate.odeint(derivative, X0, t, args = (alpha,beta, delta, gamma))
x, y = res.T
df = pd.concat([df, pd.DataFrame({str(alpha)+'+'+ str(beta)+'_x' : x})], axis=1)
df = pd.concat([df, pd.DataFrame({str(alpha)+'+'+ str(beta)+'_y' : y})], axis=1)
I have a parameter range of alphas
and betas
. Currently, they are small so this runs without a problem.
But if I have vectors of one order of magnitude higher betas = np.arange(0.9, 1.4, 0.01)
the code will take quite some time to complete.
I would like to know if it is possible to parallelize this. Split the alphas
and betas
vectors to different processors, solve everything and put it into a pandas dataframe to have create a .csv
file.
Best Regards