Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SympyProblem slow while using large coords #27

Open
mulderdj opened this issue Jan 5, 2022 · 0 comments
Open

SympyProblem slow while using large coords #27

mulderdj opened this issue Jan 5, 2022 · 0 comments

Comments

@mulderdj
Copy link

mulderdj commented Jan 5, 2022

While using coords I have noticed that the time to create SympyProblem increases enormously with larger number of coords (see example below). Up to 40 it does not take too long (<2sec). But after 50, it takes up to minutes, which is unacceptable for me as I would like to use at least 100 coords. I am not sure if this is a bug related to sunode or putting to much stress on sympy...

Nonetheless, thank you in advance for looking in to it.

import numpy as np
import sunode
import sympy as sym
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def I_func(I0_e, μ, dz, c_list):
    I_list = sym.symarray('I', len(c_list))
    for n,c in enumerate(c_list):
        if n == 0:
            I_list[n] = I0_e * sym.exp(-μ * dz * c)
        else:
            I_list[n] =  I_list[n-1] * sym.exp(-μ * dz * c)
    return I_list

def R_func(Φ,I,μ,c,dz):
    return - (Φ * I * (1 - sym.exp(-μ * dz * c)) / dz)

R_func_v = np.vectorize(R_func)

# Define the right hand side of the ode
def rhs_sympy(t, c, p):
    I_list = I_func(p.I0_e,p.μ,p.dz,c.A)
    R_list = R_func_v(p.Φ,I_list,p.μ,c.A,p.dz)
    return {
        'A' : R_list
    }
dz = 1e-3
z_list = np.arange(0,0.01,dz)

coords = {
    'z_idx': pd.Index(np.arange(len(z_list)))
    }

params = {
    'Φ': (),
    'μ': (),
    'I0_e': (),
    'dz': (),
    }

c0 = {
    'A' : 'z_idx'
    }

print('Number of coords is:', len(z_list))
Number of coords is: 10
%%time
ode = sunode.symode.SympyProblem(
    params, 
    c0, 
    rhs_sympy, 
    derivative_params=[], 
    coords=coords
    )
CPU times: user 17.3 ms, sys: 0 ns, total: 17.3 ms
Wall time: 16.7 ms
solver = sunode.solver.Solver(ode)
solver.set_params_dict({
    'Φ': 0.65,
    'μ': 7675,
    'I0_e': 0.0038,
    'dz': dz,
    })

tvals = np.linspace(0, 1., 1001)
c0 = np.full(ode.n_states,0.07)

out = solver.make_output_buffers(tvals)
%%time 

solver.solve(0, tvals, c0, out)
CPU times: user 4.28 ms, sys: 0 ns, total: 4.28 ms
Wall time: 2.95 ms
plt.figure()
ax = plt.axes()
plt.plot(tvals,out[:,:]);
ax.set_xlabel('t [s]')
ax.set_ylabel('C$_{A}$')
ax.set_xscale('log')

output_6_0

for n in [2,5,10,20,30,40,50,75]:
    df = 0.01
    dz = df/n
    z_list = np.arange(0,df,dz)
    coords = {'z_idx': pd.Index(np.arange(len(z_list)))}
    print('Number of coords is:', len(z_list))
    %time ode = sunode.symode.SympyProblem(params, c0, rhs_sympy, derivative_params=[], coords=coords)
Number of coords is: 2
CPU times: user 14.3 ms, sys: 0 ns, total: 14.3 ms
Wall time: 13.3 ms
Number of coords is: 5
CPU times: user 30.2 ms, sys: 0 ns, total: 30.2 ms
Wall time: 28.5 ms
Number of coords is: 10
CPU times: user 71.6 ms, sys: 1.88 ms, total: 73.4 ms
Wall time: 73 ms
Number of coords is: 20
CPU times: user 279 ms, sys: 0 ns, total: 279 ms
Wall time: 278 ms
Number of coords is: 30
CPU times: user 670 ms, sys: 0 ns, total: 670 ms
Wall time: 670 ms
Number of coords is: 40
CPU times: user 1.81 s, sys: 0 ns, total: 1.81 s
Wall time: 1.81 s
Number of coords is: 50
CPU times: user 40.2 s, sys: 17 ms, total: 40.2 s
Wall time: 40.2 s
Number of coords is: 75
CPU times: user 5min 4s, sys: 0 ns, total: 5min 4s
Wall time: 5min 4s
from sinfo import sinfo
sinfo()
-----
matplotlib  3.5.1
numpy       1.21.5
pandas      1.3.5
sinfo       0.3.1
sunode      0.2.1
sympy       1.9
-----
IPython             7.30.1
jupyter_client      7.1.0
jupyter_core        4.9.1
jupyterlab          3.2.5
notebook            6.4.6
-----
Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:53) [GCC 9.4.0]
Linux-5.10.74.3-microsoft-standard-WSL2-x86_64-with-debian-bullseye-sid
12 logical CPU cores, x86_64
-----
Session information updated at 2022-01-05 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant