ivp.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import numpy
  2. import scipy.integrate
  3. #scipy solve_ivp interface using system of n equations
  4. #infrastructure for sensitivity analysis of y with respect
  5. #to model parameters p also integrated
  6. #
  7. #General form of equation:
  8. # dy/dt=M*y+u
  9. #where y is an n-vector and M is nxn Jacobi matrix
  10. #
  11. #implementation of system has to provide attributes:
  12. # - n, number of variables
  13. # - m, number of parameters
  14. # - M(t), nxn, a time dependent Jacobi matrix,
  15. # - u(t), 1xn, a time dependent source term
  16. # - fS(t) mxn, a time dependent array of derivatives
  17. # fS(t)[p,:] = d(M*y)/dp (t)
  18. # where y is internal interpolated y-only ivp solution
  19. # - fSY(t,y), mxn, a time dependent array of derivatives:
  20. # fS(t)[p,:] = d(M*y)/dp (t)
  21. # where current y is supplied at each step
  22. # - Su(t), mxn, a (time dependent) array of partial derivatives:
  23. # Su(t)[p,i] = du_i/dp (t)
  24. def dfdy(t,y,system):
  25. dfdy=system.M(t).dot(y)+system.u(t)
  26. return dfdy
  27. def jacobi(t,y,system):
  28. return system.M(t)
  29. #SE post calculation
  30. def dfdyS(t,S,system):
  31. #unwrap S to NxM where M is number of parameters
  32. mS=numpy.reshape(S,(system.n,system.m))
  33. mOut=system.M(t).dot(mS)+system.fS(t)
  34. return numpy.ravel(mOut)
  35. def jacobiSE(t,S,system):
  36. N=system.n*(system.m)
  37. fJ=numpy.zeros((N,N))
  38. #print('fJ shape {}'.format(fJ.shape))
  39. for i in range(system.m):
  40. fJ[i*system.n:(i+1)*system.n,i*system.n:(i+1)*system.n]=system.M(t)
  41. return fJ
  42. #SE simultaeneous calculation
  43. def dfdySFull(t,S,system):
  44. #unwrap S to NxM where M is number of parameters
  45. mS=numpy.reshape(S,(system.n,system.m+1))
  46. #system.fS(y,t) is NxM matrix where M are parameters
  47. y=mS[:,0]
  48. mOut=system.M(t,y).dot(mS)+system.fSY(y,t)
  49. #add u derivatives
  50. mOut[:,1:]+=numpy.transpose(system.Su(t))
  51. try:
  52. system.iPrint+=1
  53. except AttributeError:
  54. system.iPrint=0
  55. if system.iPrint>1000:
  56. print('At t={:.2f}'.format(t),flush=True)
  57. system.iPrint=0
  58. return numpy.ravel(mOut)
  59. def jacobiSEFull(t,S,system):
  60. return system.jacobiFull(t)
  61. def solveSimultaneous(model,tmax,atol,rtol,method='LSODA',nt=201,\
  62. t0=0,y0=numpy.array([]), sIn=numpy.array([])):
  63. t_eval=numpy.linspace(t0,tmax,nt)
  64. s0=numpy.zeros((model.n,model.m+1))
  65. if sIn.size!=0:
  66. s0[:,1:]=sIn
  67. if y0.size==0:
  68. y0=numpy.zeros(model.n)
  69. #set initial condition
  70. s0[:,0]=y0
  71. s0=s0.ravel()
  72. sol=scipy.integrate.solve_ivp(dfdySFull,[t0, tmax],s0, args=(model,),\
  73. jac=jacobiSEFull, method=method, \
  74. atol=atol, rtol=rtol, t_eval=t_eval)
  75. t=sol.t
  76. sFull=numpy.reshape(numpy.transpose(sol.y),(len(t),model.n,model.m+1))
  77. s1=sFull[:,:,1:]
  78. ysol=sFull[:,:,0]
  79. se=model.calculateUncertainty(s1)
  80. print('Done simultaneous LSODA SE')
  81. return t,ysol,se,s1
  82. def solveSequential(model,tmax,atol,rtol,method='LSODA',nt=201,\
  83. t0=0,y0=numpy.array([]), sIn=numpy.array([])):
  84. t_eval=numpy.linspace(t0,tmax,nt)
  85. if y0.size==0:
  86. y0=numpy.zeros(model.n)
  87. solIVP=scipy.integrate.solve_ivp(dfdy,[t0, tmax],y0, args=(model,), jac=jacobi,
  88. method=method, atol=atol, rtol=rtol, t_eval=t_eval)
  89. #y is n x nt (odeint nt x n)
  90. sol=numpy.transpose(solIVP.y)
  91. t=solIVP.t
  92. print('shape (y) {}'.format(sol.shape))
  93. model.setY(t,sol)
  94. s0=numpy.zeros((model.n,model.m))
  95. if sIn.size==0:
  96. s0=sIn
  97. s0=s0.ravel()
  98. solIVPSE=scipy.integrate.solve_ivp(dfdyS,[0, tmax],s0, args=(model,), jac=jacobiSE,
  99. method=method, atol=atol, rtol=rtol,t_eval=t_eval)
  100. sraw=numpy.reshape(numpy.transpose(solIVPSE.y),(len(solIVPSE.t),model.n,model.m))
  101. #interpolate on t
  102. s1=numpy.zeros((len(t),model.n,model.m))
  103. for i in range(model.n):
  104. for j in range(model.m):
  105. tck = scipy.interpolate.splrep(solIVPSE.t, sraw[:,i,j], s=0)
  106. s1[:,i,j]=scipy.interpolate.splev(t, tck, der=0)
  107. se=model.calculateUncertainty(s1)
  108. return t,sol,se,s1
  109. def solveSimultaneousOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]), sIn=numpy.array([])):
  110. t = numpy.linspace(t0,tmax, nt)
  111. if y0.size==0:
  112. y0=numpy.zeros(model.n)
  113. s0=numpy.zeros((model.n,model.m+1))
  114. if sIn.size!=0:
  115. s0[:,1:]=sIn
  116. #set initial condition
  117. s0[:,0]=y0
  118. s0=s0.ravel()
  119. solSE1=scipy.integrate.odeint(dfdySFull, s0, t, args=(model,),Dfun=jacobiSEFull,tfirst=True)
  120. sFull=numpy.reshape(solSE1,(len(t),model.n,model.m+1))
  121. s1=sFull[:,:,1:]
  122. sol=sFull[:,:,0]
  123. se=sys.calculateUncertainty(s1)
  124. print('Done simultaneous SE')
  125. return t,sol,se,s1
  126. def solveSequentialOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]),sIn=numpy.array([])):
  127. t = numpy.linspace(t0,tmax, nt)
  128. if y0.size==0:
  129. y0=numpy.zeros(sys.n)
  130. sol = scipy.integrate.odeint(dfdy, y0=y0, t=t, args=(model,),Dfun=jacobi,tfirst=True)
  131. print('shape (y) {}'.format(sol.shape))
  132. model.setY(t,sol)
  133. s0=numpy.zeros((model.n,model.m))
  134. if sIn.size==0:
  135. s0=sIn
  136. s0=s0.ravel()
  137. solSE=scipy.integrate.odeint(dfdyS, s0, t, args=(model,),Dfun=jacobiSE,tfirst=True)
  138. s1=numpy.reshape(solSE,(len(t),model.n,model.m))
  139. se=model.calculateUncertainty(s1)
  140. print('Done sequential SE')
  141. return t,sol,se,s1