ivp.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. def dfdy(t,y,system):
  23. dfdy=system.M(t).dot(y)+system.u(t)
  24. return dfdy
  25. def jacobi(t,y,system):
  26. return system.M(t)
  27. #SE post calculation
  28. def dfdyS(t,S,system):
  29. #unwrap S to NxM where M is number of parameters
  30. mS=numpy.reshape(S,(system.n,system.m))
  31. mOut=system.M(t).dot(mS)+system.fS(t)
  32. return numpy.ravel(mOut)
  33. def jacobiSE(t,S,system):
  34. N=system.n*(system.m)
  35. fJ=numpy.zeros((N,N))
  36. #print('fJ shape {}'.format(fJ.shape))
  37. for i in range(system.m):
  38. fJ[i*system.n:(i+1)*system.n,i*system.n:(i+1)*system.n]=system.M(t)
  39. return fJ
  40. #SE simultaeneous calculation
  41. def dfdySFull(t,S,system):
  42. #unwrap S to NxM where M is number of parameters
  43. mS=numpy.reshape(S,(system.n,system.m+1))
  44. #system.fS(y,t) is NxM matrix where M are parameters
  45. y=mS[:,0]
  46. mOut=system.M(t).dot(mS)+system.fSY(y,t)
  47. try:
  48. system.iPrint+=1
  49. except AttributeError:
  50. system.iPrint=0
  51. if system.iPrint>1000:
  52. print('At t={:.2f}'.format(t),flush=True)
  53. system.iPrint=0
  54. return numpy.ravel(mOut)
  55. def jacobiSEFull(t,S,system):
  56. N=system.n*(system.m+1)
  57. fJ=numpy.zeros((N,N))
  58. #print('fJ shape {}'.format(fJ.shape))
  59. for i in range(system.m+1):
  60. fJ[i*system.n:(i+1)*system.n,i*system.n:(i+1)*system.n]=system.M(t)
  61. return fJ
  62. def solveSimultaneous(model,tmax,atol,rtol,method='LSODA',t0=0,y0=numpy.array([]), Sin=numpy.array([])):
  63. S1=numpy.zeros((model.n,model.m+1))
  64. if Sin.size!=0:
  65. S1[:,1:]=Sin
  66. if y0.size==0:
  67. y0=numpy.zeros(model.n)
  68. #set initial condition
  69. S1[:,0]=y0
  70. S1=S1.ravel()
  71. sol=scipy.integrate.solve_ivp(dfdySFull,[t0, tmax],S1, args=(model,), jac=jacobiSEFull,
  72. method=method, atol=atol, rtol=rtol)
  73. t=sol.t
  74. sFull=numpy.reshape(numpy.transpose(sol.y),(len(t),model.n,model.m+1))
  75. s1=sFull[:,:,1:]
  76. ysol=sFull[:,:,0]
  77. se=model.calculateUncertainty(ysol,s1)
  78. print('Done simultaneous LSODA SE')
  79. return t,ysol,se,s1
  80. def solveSequential(model,tmax,atol,rtol,method='LSODA',t0=0,y0=numpy.array([]), S1=numpy.array([])):
  81. if y0.size==0:
  82. y0=numpy.zeros(model.n)
  83. model.iPrint=0
  84. solIVP=scipy.integrate.solve_ivp(dfdy,[t0, tmax],y0, args=(model,), jac=jacobi,
  85. method=method, atol=atol, rtol=rtol)
  86. #y is n x nt (odeint nt x n)
  87. sol=numpy.transpose(solIVP.y)
  88. t=solIVP.t
  89. print('shape (y) {}'.format(sol.shape))
  90. model.setY(t,sol)
  91. if S1.size==0:
  92. S1=numpy.zeros((model.n,model.m))
  93. S1=S1.ravel()
  94. solIVPSE=scipy.integrate.solve_ivp(dfdyS,[0, tmax],S1, args=(model,), jac=jacobiSE,
  95. method=method, atol=atol, rtol=rtol)
  96. sraw=numpy.reshape(numpy.transpose(solIVPSE.y),(len(solIVPSE.t),model.n,model.m))
  97. #interpolate on t
  98. s1=numpy.zeros((len(t),model.n,model.m))
  99. for i in range(model.n):
  100. for j in range(model.m):
  101. tck = scipy.interpolate.splrep(solIVPSE.t, sraw[:,i,j], s=0)
  102. s1[:,i,j]=scipy.interpolate.splev(t, tck, der=0)
  103. se=model.calculateUncertainty(sol,s1)
  104. return t,sol,se,s1
  105. def solveSimultaneousOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]), Sin=numpy.array([])):
  106. t = numpy.linspace(t0,tmax, nt)
  107. if y0.size==0:
  108. y0=numpy.zeros(model.n)
  109. S1=numpy.zeros((model.n,model.m+1))
  110. if Sin.size!=0:
  111. S1[:,1:]=Sin
  112. #set initial condition
  113. S1[:,0]=y0
  114. S1=S1.ravel()
  115. solSE1=scipy.integrate.odeint(dfdySFull, S1, t, args=(model,),Dfun=jacobiSEFull,tfirst=True)
  116. sFull=numpy.reshape(solSE1,(len(t),model.n,model.m+1))
  117. s1=sFull[:,:,1:]
  118. sol=sFull[:,:,0]
  119. se=sys.calculateUncertainty(sol,s1)
  120. print('Done simultaneous SE')
  121. return t,sol,se,s1
  122. def solveSequentialOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]),S1=numpy.array([])):
  123. t = numpy.linspace(t0,tmax, nt)
  124. if y0.size==0:
  125. y0=numpy.zeros(sys.n)
  126. sol = scipy.integrate.odeint(dfdy, y0=y0, t=t, args=(model,),Dfun=jacobi,tfirst=True)
  127. print('shape (y) {}'.format(sol.shape))
  128. model.setY(t,sol)
  129. if S1.size==0:
  130. S1=numpy.zeros((model.n,model.m))
  131. S1=S1.ravel()
  132. solSE=scipy.integrate.odeint(dfdyS, S1, t, args=(model,),Dfun=jacobiSE,tfirst=True)
  133. s1=numpy.reshape(solSE,(len(t),model.n,model.m))
  134. se=model.calculateUncertainty(sol,s1)
  135. print('Done sequential SE')
  136. return t,sol,se,s1