Guest User

Untitled

a guest
Feb 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. '''
  2. Forward method to solve 1D reaction-diffusion equation:
  3. u_t = D * u_xx + alpha * u
  4.  
  5. with Dirichlet boundary conditions u(x0,t) = 0, u(xL,t) = 0
  6. and initial condition u(x,0) = 4*x - 4*x**2
  7. '''
  8.  
  9.  
  10. import numpy as np
  11. from scipy import sparse
  12. from mpl_toolkits.mplot3d import Axes3D
  13. import matplotlib.pyplot as plt
  14. from matplotlib import cm
  15. import math
  16.  
  17.  
  18. M = 40 # GRID POINTS on space interval
  19. N = 70 # GRID POINTS on time interval
  20.  
  21. x0 = 0.0
  22. xL = 1.0
  23.  
  24. # ----- Spatial discretization step -----
  25. dx = (xL - x0)/(M - 1)
  26.  
  27. t0 = 0.0
  28. tF = 0.2
  29.  
  30. # ----- Time step -----
  31. dt = (tF - t0)/(N - 1)
  32.  
  33. D = 0.1 # Diffusion coefficient
  34. alpha = -3 # Reaction rate
  35.  
  36. r = (dt*D)/pow(dx,2)
  37. s = dt*alpha
  38.  
  39. print("r =", r)
  40.  
  41. # ----- Creates grids -----
  42. xspan = np.linspace(x0, xL, M)
  43. tspan = np.linspace(t0, tF, N)
  44.  
  45. # ----- Initializes matrix solution U -----
  46. U = np.zeros((M, N))
  47.  
  48. # ----- Initial condition -----
  49. U[:,0] = 4*xspan - 4*xspan**2
  50. #U[:,0] = 0.0
  51.  
  52. # ----- Dirichlet Boundary Conditions -----
  53. U[0,:] = 0.0
  54. U[-1,:] = 0.0 #same as U[L,:] = U[M-1,:] = 0... -1 gets the last entry
  55.  
  56. # ----- Equation (15.8) in Lecture 15 -----
  57. for k in range(0, N-1):
  58. for i in range(1, M-1):
  59. U[i, k+1] = r*U[i-1, k] + (1-2*r+s)*U[i,k] + r*U[i+1,k]
  60.  
  61. X, T = np.meshgrid(tspan, xspan)
  62.  
  63. fig = plt.figure()
  64. ax = fig.gca(projection='3d')
  65.  
  66. surf = ax.plot_surface(X, T, U, cmap=cm.coolwarm,
  67. linewidth=0, antialiased=False)
  68.  
  69. ax.set_xticks([0, 0.05, 0.1, 0.15, 0.2])
  70.  
  71. ax.set_xlabel('Space')
  72. ax.set_ylabel('Time')
  73. ax.set_zlabel('U')
  74. plt.tight_layout()
  75. plt.show()
  76.  
  77. import numpy as np
  78. from scipy import sparse
  79. from mpl_toolkits.mplot3d import Axes3D
  80. import matplotlib.pyplot as plt
  81. from matplotlib import cm
  82. import math
  83.  
  84. def oneDendriticBranchModel(tauV, E, lambd, i_app, i_app_location, j_input, tauR):
  85.  
  86. M = 41 # GRID POINTS on space interval
  87. N = 41 # GRID POINTS on time interval
  88.  
  89. x0 = 0.0
  90. xL = 20.0
  91.  
  92. # ----- Spatial discretization step -----
  93. dx = (xL - x0)/(M - 1)
  94.  
  95. t0 = 0.0
  96. tF = 20.0
  97.  
  98. # ----- Time step -----
  99. dt = (tF - t0)/(N - 1)
  100.  
  101. D = 1 # Diffusion coefficient
  102. alpha = 1 # Reaction rate
  103.  
  104. #r = (dt*D)/pow(dx,2)
  105. #s = dt*alpha
  106. r = dt/tauV
  107. s = 1 - r - ((2*(pow(lambd,2))*dt)/(tauV*pow(dx,2)))
  108. t = (((pow(lambd,2))*dt)/(tauV*pow(dx,2)))
  109. indicator = 0
  110.  
  111. print("r =", r)
  112.  
  113. # ----- Creates grids -----
  114. xspan = np.linspace(x0, xL, M)
  115. tspan = np.linspace(t0, tF, N)
  116.  
  117. # ----- Initializes matrix solution V -----
  118. V = np.zeros((M, N))
  119.  
  120. # ----- Initial condition -----
  121. V[:,0] = 0
  122.  
  123. # ----- Dirichlet Boundary Conditions -----
  124. V[0,:] = 0.0
  125. V[-1,:] = 0.0 #same as V[L,:] = V[M-1,:] = 0... -1 gets the last entry
  126.  
  127. # ----- Equation (15.8) in Lecture 15 -----
  128. for j in range(0, N-1):
  129. for i in range(1, M-1):
  130.  
  131. if i == i_app_location and j >= j_input and j <= j_input + tauR:
  132. indicator = 1
  133. else:
  134. indicator = 0
  135.  
  136. V[i,j+1] = r*E + s*V[i,j] + t*V[i+1,j] + r*V[i-1,j] + r*i_app*indicator
  137.  
  138. X, T = np.meshgrid(tspan, xspan)
  139.  
  140. fig = plt.figure()
  141. ax = fig.gca(projection='3d')
  142.  
  143. surf = ax.plot_surface(X, T, V, cmap=cm.coolwarm,
  144. linewidth=0, antialiased=False)
  145.  
  146. #ax.set_xticks([0, 0.05, 0.1, 0.15, 0.2])
  147.  
  148. ax.set_xlabel('Space')
  149. ax.set_ylabel('Time')
  150. ax.set_zlabel('V')
  151. plt.tight_layout()
  152. plt.show()
  153.  
  154. oneDendriticBranchModel(1.0,0.0,1.0,2.0,1.0,1.0,2.0) #have to be float or we lose precision...
Add Comment
Please, Sign In to add comment