Advertisement
Guest User

calciumregPython

a guest
Apr 21st, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import numpy as numpy
  2. from scipy import integrate
  3. from matplotlib.pylab import *
  4.  
  5. def calcium(t, x):
  6.     """
  7.    returns
  8.    """  
  9.     n = 1
  10.     #PTH constants
  11.     SetPoint_PTH_50 = 46.8 #mg/L
  12.     PTHmax = 127*10e-6  #mg/L
  13.     dpth = log(2)/(4.0/(60.0*24.0)) # /days
  14.     pth_mean = 35.8e-6 # mg/L
  15.    
  16.     #CTN constants
  17.     SetPoint_CT = 53.2 #mg/L
  18.     dctn = log(2)/(10.0/(24.0*60.0)) # /days
  19.     CTNmax = 1.0e-5 # mg/L
  20.    
  21.     #DHCC
  22.     DHCCmean = 2.163e-2 #mg/L
  23.     ddhhc = log(2)/(5.0/24.0) #/days
  24.    
  25.     #calcium equation constants
  26.     K_PTH = PTHmax*dpth #mg/L*day
  27.     K_CTN = CTNmax*dctn  #mg/L*day
  28.     K_DHCC = (DHCCmean*ddhhc)/pth_mean  #/day
  29.  
  30.     K1 = 1.6e6 #PTH
  31.     K2 = -18e6 #CTN
  32.     K3 = (9.24e-5)*K2 - 0.00165*K1 + 2323.62 #dthh
  33.     K4 = 10.0 #mg/L * day
  34.     K5 = 0.1 #mg/L*day
  35.    
  36.      
  37.     #Assign some variables for convenience of notation
  38.     ca = x[0]
  39.     pth = x[1] #mean PTH 35.8 pg/mL
  40.     dthh = x[2] #mean DTHH 51.5 nmol/L
  41.     ctn = x[3] #mean 2 ng/L, range UD to 10 ng/L
  42.  
  43.     #Algebraic Equations
  44.  
  45.     #pth
  46.     pthp = K_PTH * (1.0/(1.0 + (ca/SetPoint_PTH_50))) - dpth * pth
  47.  
  48.     #dhcc
  49.     dthp = K_DHCC*pth - ddhhc*dthh
  50.  
  51.     #ctn
  52.     ctnp = K_CTN * (ca/SetPoint_CT) / (1.0 + (ca/SetPoint_CT) ) - dctn * ctn
  53.  
  54.     #calcium
  55.     cap = K1*pth - K2*ctn + K3*dthh - ca*K4*((ctn/pth)/(1.0 + (ctn/pth))) - K5*ca
  56.  
  57.     n = len(x) # 4: implies we have 4 ODEs
  58.     xp = np.zeros((n,1))
  59.     xp[0] = cap
  60.     xp[1] = pthp
  61.     xp[2] = dthp
  62.     xp[3] = ctnp
  63.     return xp
  64.  
  65. # The ''driver'' that will integrate the ODE(s):
  66. if __name__ == '__main__':    
  67.    
  68.     # Start by specifying the integrator:
  69.     # use ''vode'' with "backward differentiation forumla"
  70.     r = integrate.ode(calcium).set_integrator('dopri5')
  71.  
  72.     #Set the time range
  73.     t_start = 0.0
  74.     t_final = 10.0
  75.     delta_t = 0.001
  76.  
  77.     #Number of time steps: 1 extra for initial condition
  78.     num_steps = np.floor((t_final - t_start)/delta_t) + 1
  79.  
  80.     #Set initial condition(s): for integrating variable and time!
  81.     ca_zero = 95 #mg/L
  82.     pth_zero = 35.8e-6 #mg/L
  83.     dthh_zero = 2.163e-2 #mg/L
  84.     ctn_zero = 2e-6 #mg/L
  85.     r.set_initial_value([ca_zero,pth_zero,dthh_zero,ctn_zero], t_start)
  86.  
  87.     #Additional Python step: create vectors to store trajectories
  88.     t = np.zeros((num_steps, 1))
  89.     ca = np.zeros((num_steps, 1))
  90.     pth = np.zeros((num_steps,1))
  91.     dthh = np.zeros((num_steps,1))
  92.     ctn = np.zeros((num_steps,1))
  93.  
  94.     t[0] = t_start
  95.     ca[0] = ca_zero
  96.     pth[0] = pth_zero
  97.     dthh[0] = dthh_zero
  98.     ctn[0] = ctn_zero
  99.  
  100.     #Integrate the ODE(s) across each delta_t timestep
  101.     k = 1
  102.     while r.successful() and k < num_steps:
  103.         r.integrate(r.t + delta_t)
  104.  
  105.         #Store the results to plot later
  106.         t[k] = r.t
  107.         ca[k] = r.y[0]
  108.         pth[k] = r.y[1]
  109.         dthh[k] = r.y[2]
  110.         ctn[k] = r.y[3]
  111.         k += 1
  112.     #All done! Plot the trajectories
  113.     fig = figure()
  114.     ax1 = subplot(411)
  115.     ax1.plot(t, ca)
  116.     ax1.legend(["ca"])
  117.     ax1.grid('on')    
  118.    
  119.     ax2 = subplot(412)
  120.     ax2.plot(t,pth)
  121.     ax2.grid('on')
  122.     ax2.legend(["pth"])
  123.    
  124.     ax3 = subplot(413)
  125.     ax3.plot(t,dthh)
  126.     ax3.grid('on')
  127.     ax3.legend(["dthh"])
  128.    
  129.     ax4 = subplot(414)
  130.     ax4.plot(t,ctn)
  131.     ax4.legend(["ctn"])
  132.     ax4.grid('on')
  133.     show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement