preprocess.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import numpy as np
  2. from collections import namedtuple
  3. from scipy.linalg import block_diag
  4. def shape(x, p):
  5. """generate p shape functions and its first order derivative
  6. at the given location x. x can be an array"""
  7. A = np.array([np.linspace(-1, 1, p)]).T**np.arange(p)
  8. C = np.linalg.inv(A).T
  9. x = np.array([x]).T
  10. shp = C.dot((x**np.arange(p)).T)
  11. shpx = C[:, 1::1].dot((x**np.arange(p - 1) * np.arange(1, p)).T)
  12. return shp, shpx
  13. def forcing(x):
  14. f = 1
  15. return f
  16. def boundaryCondition(case):
  17. if case == 0:
  18. # primal problem
  19. bc = [0, 0]
  20. if case == 1:
  21. # adjoint problem
  22. bc = [0, 1]
  23. return bc
  24. class discretization(object):
  25. """Given the problem statement and current mesh,
  26. construct the discretization matrices"""
  27. def __init__(self, coeff, mesh, enrich=None):
  28. self.mesh = mesh
  29. self.coeff = coeff
  30. self.enrich = enrich
  31. # the following init are for the sake of simplicity
  32. self.numBasisFuncs = coeff.pOrder + 1
  33. self.tau_pos = coeff.TAUPLUS
  34. self.tau_neg = coeff.TAUMINUS
  35. self.conv = coeff.CONVECTION
  36. self.kappa = coeff.DIFFUSION
  37. self.numEle = len(mesh) - 1
  38. self.dist = self.distGen()
  39. # shape function and gauss quadrature
  40. self.gqOrder = 5 * self.numBasisFuncs
  41. self.xi, self.wi = np.polynomial.legendre.leggauss(self.gqOrder)
  42. self.shp, self.shpx = shape(self.xi, self.numBasisFuncs)
  43. # enrich the space if the enrich argument is given
  44. if enrich is not None:
  45. self.shpEnrich, self.shpxEnrich = shape(
  46. self.xi, self.numBasisFuncs + enrich)
  47. def distGen(self):
  48. dist = np.zeros(self.numEle)
  49. for i in range(1, self.numEle + 1):
  50. dist[i - 1] = self.mesh[i] - self.mesh[i - 1]
  51. return dist
  52. def matGroup(self):
  53. lhsMat = self.lhsGen()
  54. F, L, R = lhsMat.F, lhsMat.L, lhsMat.R
  55. eleMat = self.eleGen()
  56. A, B, BonU, D = eleMat.A, eleMat.B, eleMat.BonU, eleMat.D
  57. faceMat = self.interfaceGen()
  58. C, E, G, H = faceMat.C, faceMat.E, faceMat.G, faceMat.H
  59. matGroup = namedtuple(
  60. 'matGroup', ['A', 'B', 'BonU', 'C', 'D', 'E', 'F', 'G', 'H', 'L', 'R'])
  61. return matGroup(A, B, BonU, C, D, E, F, G, H, L, R)
  62. def lhsGen(self):
  63. """Generate matrices associated with left hand side"""
  64. if self.enrich is not None:
  65. # when enrich is given
  66. # provide matrices used in the DWR residual calculation
  67. numBasisFuncs = self.numBasisFuncs + self.enrich
  68. shp = self.shpEnrich
  69. else:
  70. numBasisFuncs = self.numBasisFuncs
  71. shp = self.shp
  72. # forcing vector F
  73. F = np.zeros(numBasisFuncs * self.numEle)
  74. for i in range(1, self.numEle + 1):
  75. f = self.dist[i - 1] / 2 \
  76. * shp.dot(self.wi * forcing(self.mesh[i - 1] +
  77. 1 / 2 * (1 + self.xi) *
  78. self.dist[i - 1]))
  79. F[(i - 1) * numBasisFuncs:i * numBasisFuncs] = f
  80. F[0] += (self.conv + self.tau_pos) * boundaryCondition(0)[0]
  81. F[-1] += (-self.conv + self.tau_neg) * boundaryCondition(0)[1]
  82. # L, easy in 1d
  83. L = np.zeros(self.numEle - 1)
  84. # R, easy in 1d
  85. R = np.zeros(numBasisFuncs * self.numEle)
  86. R[0] = boundaryCondition(0)[0]
  87. R[-1] = -boundaryCondition(0)[1]
  88. lhsMat = namedtuple('lhsMat', ['F', 'L', 'R'])
  89. return lhsMat(F, L, R)
  90. def eleGen(self):
  91. """Generate matrices associated with elements"""
  92. if self.enrich is not None:
  93. numBasisFuncsEnrich = self.numBasisFuncs + self.enrich
  94. shpEnrich = self.shpEnrich
  95. shpxEnrich = self.shpxEnrich
  96. b = (self.shpx.T * np.ones((self.gqOrder, self.numBasisFuncs))
  97. ).T.dot(np.diag(self.wi).dot(shpEnrich.T))
  98. # BonQ is only used in calculating DWR residual
  99. BonQ = block_diag(*[b] * (self.numEle)).T
  100. else:
  101. numBasisFuncsEnrich = self.numBasisFuncs
  102. shpEnrich = self.shp
  103. shpxEnrich = self.shpx
  104. BonQ = None
  105. a = 1 / self.coeff.DIFFUSION * \
  106. shpEnrich.dot(np.diag(self.wi).dot(self.shp.T))
  107. A = np.repeat(self.dist, self.numBasisFuncs) / \
  108. 2 * block_diag(*[a] * (self.numEle))
  109. b = (shpxEnrich.T * np.ones((self.gqOrder, numBasisFuncsEnrich))
  110. ).T.dot(np.diag(self.wi).dot(self.shp.T))
  111. B = block_diag(*[b] * (self.numEle))
  112. d = self.coeff.REACTION * \
  113. shpEnrich.dot(np.diag(self.wi).dot(self.shp.T))
  114. # assemble D
  115. dFace = np.zeros((numBasisFuncsEnrich, self.numBasisFuncs))
  116. dFace[0, 0] = self.tau_pos
  117. dFace[-1, -1] = self.tau_neg
  118. dConv = -self.conv * (shpxEnrich.T * np.ones((self.gqOrder,
  119. numBasisFuncsEnrich)))\
  120. .T.dot(np.diag(self.wi).dot(self.shp.T))
  121. D = np.repeat(self.dist, self.numBasisFuncs) / 2 * \
  122. block_diag(*[d] * (self.numEle)) + \
  123. block_diag(*[dFace] * (self.numEle)) +\
  124. block_diag(*[dConv] * (self.numEle))
  125. eleMat = namedtuple('eleMat', ['A', 'B', 'BonU', 'D'])
  126. return eleMat(A, B, BonQ, D)
  127. def interfaceGen(self):
  128. """Generate matrices associated with interfaces"""
  129. if self.enrich is not None:
  130. # when enrich is given
  131. # provide matrices used in the DWR residual calculation
  132. numBasisFuncs = self.numBasisFuncs + self.enrich
  133. else:
  134. numBasisFuncs = self.numBasisFuncs
  135. tau_pos, tau_neg, conv = self.tau_pos, self.tau_neg, self.conv
  136. # elemental h
  137. h = np.zeros((2, 2))
  138. h[0, 0], h[-1, -1] = -conv - tau_pos, conv - tau_neg
  139. # mappinng matrix
  140. map_h = np.zeros((2, self.numEle), dtype=int)
  141. map_h[:, 0] = np.arange(2)
  142. for i in np.arange(1, self.numEle):
  143. map_h[:, i] = np.arange(
  144. map_h[2 - 1, i - 1], map_h[2 - 1, i - 1] + 2)
  145. # assemble H and eliminate boundaries
  146. H = np.zeros((self.numEle + 1, self.numEle + 1))
  147. for i in range(self.numEle):
  148. for j in range(2):
  149. m = map_h[j, i]
  150. for k in range(2):
  151. n = map_h[k, i]
  152. H[m, n] += h[j, k]
  153. H = H[1:self.numEle][:, 1:self.numEle]
  154. # elemental e
  155. e = np.zeros((numBasisFuncs, 2))
  156. e[0, 0], e[-1, -1] = -conv - tau_pos, conv - tau_neg
  157. # mapping matrix
  158. map_e_x = np.arange(numBasisFuncs * self.numEle,
  159. dtype=int).reshape(self.numEle,
  160. numBasisFuncs).T
  161. map_e_y = map_h
  162. # assemble global E
  163. E = np.zeros((numBasisFuncs * self.numEle, self.numEle + 1))
  164. for i in range(self.numEle):
  165. for j in range(numBasisFuncs):
  166. m = map_e_x[j, i]
  167. for k in range(2):
  168. n = map_e_y[k, i]
  169. E[m, n] += e[j, k]
  170. E = E[:, 1:self.numEle]
  171. # elemental c
  172. c = np.zeros((numBasisFuncs, 2))
  173. c[0, 0], c[-1, -1] = -1, 1
  174. # assemble global C
  175. C = np.zeros((numBasisFuncs * self.numEle, self.numEle + 1))
  176. for i in range(self.numEle):
  177. for j in range(numBasisFuncs):
  178. m = map_e_x[j, i]
  179. for k in range(2):
  180. n = map_e_y[k, i]
  181. C[m, n] += c[j, k]
  182. C = C[:, 1:self.numEle]
  183. # elemental g
  184. g = np.zeros((2, numBasisFuncs))
  185. g[0, 0], g[-1, -1] = tau_pos, tau_neg
  186. # mapping matrix
  187. map_g_x = map_h
  188. map_g_y = np.arange(numBasisFuncs * self.numEle,
  189. dtype=int).reshape(self.numEle,
  190. numBasisFuncs).T
  191. # assemble global G
  192. G = np.zeros((self.numEle + 1, numBasisFuncs * self.numEle))
  193. for i in range(self.numEle):
  194. for j in range(2):
  195. m = map_g_x[j, i]
  196. for k in range(numBasisFuncs):
  197. n = map_g_y[k, i]
  198. G[m, n] += g[j, k]
  199. G = G[1:self.numEle, :]
  200. faceMat = namedtuple('faceMat', ['C', 'E', 'G', 'H'])
  201. return faceMat(C, E, G, H)