I would like to solve the Lotka-Volterra equations for a range of test parameters.
I would also like to append the results to a dictionary to latter create a pandas dataFrame and output the results as a .csv
.
I have the following code:
import numpy as np
from scipy import integrate
import pandas as pd
import itertools
import multiprocessing as mtp
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.01)
alphas = np.arange(0.9, 1.4, 0.01)
paramlist = list(itertools.product(alphas, betas))
columns = {"time": t}
def function(params):
alpha, beta = params
res = integrate.odeint(derivative, X0, t, args = (alpha, beta, delta, gamma))
x, y = res.T
print(f"alpha: {alpha}, beta: {beta}")
columns[str(alpha)+'+'+ str(beta)+'_x'] = x
columns[str(alpha)+'+'+ str(beta)+'_y'] = y
nProcess = 4
with mtp.Pool(processes= nProcess) as pool:
pool.map(function, paramlist)
df = pd.DataFrame(columns)
df.to_csv("test.csv", sep='\t')
However, I am not able to get the results in the columns
dictionary.
How can I get the results. Additionlly, is there anyway to sort the appends based on e.g., beta
? Or will this kill the parallel efficiency?
Best Regards