ivp.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. return numpy.ravel(mOut)
  48. def jacobiSEFull(t,S,system):
  49. N=system.n*(system.m+1)
  50. fJ=numpy.zeros((N,N))
  51. #print('fJ shape {}'.format(fJ.shape))
  52. for i in range(system.m+1):
  53. fJ[i*system.n:(i+1)*system.n,i*system.n:(i+1)*system.n]=system.M(t)
  54. return fJ
  55. def solveSimultaneous(model,tmax,atol,rtol,method='LSODA',t0=0,y0=numpy.array([]), Sin=numpy.array([])):
  56. S1=numpy.zeros((model.n,model.m+1))
  57. if Sin.size!=0:
  58. S1[:,1:]=Sin
  59. if y0.size==0:
  60. y0=numpy.zeros(model.n)
  61. #set initial condition
  62. S1[:,0]=y0
  63. S1=S1.ravel()
  64. sol=scipy.integrate.solve_ivp(dfdySFull,[t0, tmax],S1, args=(model,), jac=jacobiSEFull,
  65. method=method, atol=atol, rtol=rtol)
  66. t=sol.t
  67. sFull=numpy.reshape(numpy.transpose(sol.y),(len(t),model.n,model.m+1))
  68. s1=sFull[:,:,1:]
  69. ysol=sFull[:,:,0]
  70. se=model.calculateUncertainty(ysol,s1)
  71. print('Done simultaneous LSODA SE')
  72. return t,ysol,se,s1
  73. def solveSequential(model,tmax,atol,rtol,method='LSODA',t0=0,y0=numpy.array([]), S1=numpy.array([])):
  74. if y0.size==0:
  75. y0=numpy.zeros(model.n)
  76. solIVP=scipy.integrate.solve_ivp(dfdy,[t0, tmax],y0, args=(model,), jac=jacobi,
  77. method=method, atol=atol, rtol=rtol)
  78. #y is n x nt (odeint nt x n)
  79. sol=numpy.transpose(solIVP.y)
  80. t=solIVP.t
  81. print('shape (y) {}'.format(sol.shape))
  82. model.setY(t,sol)
  83. if S1.size==0:
  84. S1=numpy.zeros((model.n,model.m))
  85. S1=S1.ravel()
  86. solIVPSE=scipy.integrate.solve_ivp(dfdyS,[0, tmax],S1, args=(model,), jac=jacobiSE,
  87. method=method, atol=atol, rtol=rtol)
  88. sraw=numpy.reshape(numpy.transpose(solIVPSE.y),(len(solIVPSE.t),model.n,model.m))
  89. #interpolate on t
  90. s1=numpy.zeros((len(t),model.n,model.m))
  91. for i in range(model.n):
  92. for j in range(model.m):
  93. tck = scipy.interpolate.splrep(solIVPSE.t, sraw[:,i,j], s=0)
  94. s1[:,i,j]=scipy.interpolate.splev(t, tck, der=0)
  95. se=model.calculateUncertainty(sol,s1)
  96. return t,sol,se,s1
  97. def solveSimultaneousOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]), Sin=numpy.array([])):
  98. t = numpy.linspace(t0,tmax, nt)
  99. if y0.size==0:
  100. y0=numpy.zeros(model.n)
  101. S1=numpy.zeros((model.n,model.m+1))
  102. if Sin.size!=0:
  103. S1[:,1:]=Sin
  104. #set initial condition
  105. S1[:,0]=y0
  106. S1=S1.ravel()
  107. solSE1=scipy.integrate.odeint(dfdySFull, S1, t, args=(model,),Dfun=jacobiSEFull,tfirst=True)
  108. sFull=numpy.reshape(solSE1,(len(t),model.n,model.m+1))
  109. s1=sFull[:,:,1:]
  110. sol=sFull[:,:,0]
  111. se=sys.calculateUncertainty(sol,s1)
  112. print('Done simultaneous SE')
  113. return t,sol,se,s1
  114. def solveSequentialOdeint(model,tmax,nt=201,t0=0,y0=numpy.array([]),S1=numpy.array([])):
  115. t = numpy.linspace(t0,tmax, nt)
  116. if y0.size==0:
  117. y0=numpy.zeros(sys.n)
  118. sol = scipy.integrate.odeint(dfdy, y0=y0, t=t, args=(model,),Dfun=jacobi,tfirst=True)
  119. print('shape (y) {}'.format(sol.shape))
  120. model.setY(t,sol)
  121. if S1.size==0:
  122. S1=numpy.zeros((model.n,model.m))
  123. S1=S1.ravel()
  124. solSE=scipy.integrate.odeint(dfdyS, S1, t, args=(model,),Dfun=jacobiSE,tfirst=True)
  125. s1=numpy.reshape(solSE,(len(t),model.n,model.m))
  126. se=model.calculateUncertainty(sol,s1)
  127. print('Done sequential SE')
  128. return t,sol,se,s1