ivp.py 4.7 KB

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