discretization.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import numpy as np
  2. from numpy import sin, cos, pi
  3. from scipy.linalg import block_diag
  4. import matplotlib.pyplot as plt
  5. class hdpg1d(object):
  6. """
  7. 1d advection hdg solver outlined in 'an implicit HHDG method for
  8. confusion'. Test case: /tau = 1, convection only, linear and higher order.
  9. Please enter number of elements and polynomial order, i.e., HDG1d(10,2)
  10. """
  11. def __init__(self, numEle, numPolyOrder):
  12. self.numEle = numEle
  13. self.numBasisFuncs = numPolyOrder + 1
  14. self.tau_pos = 1e-6
  15. self.tau_neg = 1e-6
  16. self.c = 0
  17. self.kappa = 1e-6
  18. self.estError = []
  19. self.trueError = []
  20. def bc(self, case, t=None):
  21. # boundary condition
  22. if case == 0:
  23. # advection-diffusion
  24. bc = [0, 0]
  25. if case == 1:
  26. # simple convection
  27. # bc = np.sin(2*np.pi*t)
  28. # adjoint boundary
  29. bc = [0, 1]
  30. return bc
  31. def shape(self, x, p):
  32. """ evaluate shape functions at give locations"""
  33. # coeffient matrix
  34. A = np.array([np.linspace(-1, 1, p)]).T**np.arange(p)
  35. C = np.linalg.inv(A).T
  36. x = np.array([x]).T
  37. shp = C.dot((x**np.arange(p)).T)
  38. shpx = C[:, 1::1].dot((x**np.arange(p - 1) * np.arange(1, p)).T)
  39. return shp, shpx
  40. def forcing(self, x):
  41. # f = np.cos(2*np.pi*x)
  42. # f = 4*pi**2*sin(2*pi*x)
  43. f = 1
  44. return f
  45. def mesh(self, n_ele, index, x):
  46. """generate mesh"""
  47. # if n_ele < 1 or n_ele > self.numEle:
  48. # raise RuntimeError('Bad Element number')
  49. in_value = np.zeros(len(index))
  50. for i in np.arange(len(index)):
  51. in_value[i] = (x[index[i]] + x[index[i] - 1]) / 2
  52. x_c = np.sort(np.insert(x, 0, in_value))
  53. x_i = np.linspace(x_c[n_ele - 1], x_c[n_ele], num=self.numBasisFuncs)
  54. dx = x_c[n_ele] - x_c[n_ele - 1]
  55. return x_i, dx, x_c
  56. def exact(self, x):
  57. """solve the problem in an enriched space to simulate exact soltuion"""
  58. self.numEle = 1000
  59. self.numBasisFuncs = 3
  60. x = np.linspace(0, 1, self.numEle + 1)
  61. self.exactSol = self.solve_local([], x)
  62. def matrix_gen(self, index, x):
  63. n_ele = self.numEle
  64. # order of polynomial shape functions
  65. p = self.numBasisFuncs
  66. # order of gauss quadrature
  67. gorder = 2 * p
  68. # shape function and gauss quadrature
  69. xi, wi = np.polynomial.legendre.leggauss(gorder)
  70. shp, shpx = self.shape(xi, p)
  71. # ---------------------------------------------------------------------
  72. # advection constant
  73. con = self.c
  74. # diffusion constant
  75. kappa = self.kappa
  76. # number of nodes (solution U)
  77. n_ele = self.numEle + len(index)
  78. # elemental forcing vector
  79. F = np.zeros(p * n_ele)
  80. for i in range(1, n_ele + 1):
  81. x_i, dx_i, _ = self.mesh(i, index, x)
  82. f = dx_i / 2 * \
  83. shp.dot(wi * self.forcing(x[0] + 1 / 2 * (1 + xi) * dx_i))
  84. F[(i - 1) * p:(i - 1) * p + p] = f
  85. F[0] += (con + self.tau_pos) * self.bc(0)[0]
  86. F[-1] += (-con + self.tau_neg) * self.bc(0)[1]
  87. # elemental d
  88. d = shp.dot(np.diag(wi).dot(shp.T))
  89. # elemental a
  90. a = 1 / kappa * shp.dot(np.diag(wi).dot(shp.T))
  91. # elemental b
  92. b = (shpx.T * np.ones((gorder, p))).T.dot(np.diag(wi).dot(shp.T))
  93. # elemental h
  94. h = np.zeros((2, 2))
  95. h[0, 0], h[-1, -1] = -con - self.tau_pos, con - self.tau_neg
  96. # mappinng matrix
  97. map_h = np.zeros((2, n_ele), dtype=int)
  98. map_h[:, 0] = np.arange(2)
  99. for i in np.arange(1, n_ele):
  100. map_h[:, i] = np.arange(
  101. map_h[2 - 1, i - 1], map_h[2 - 1, i - 1] + 2)
  102. # assemble H and eliminate boundaries
  103. H = np.zeros((n_ele + 1, n_ele + 1))
  104. for i in range(n_ele):
  105. for j in range(2):
  106. m = map_h[j, i]
  107. for k in range(2):
  108. n = map_h[k, i]
  109. H[m, n] += h[j, k]
  110. H = H[1:n_ele][:, 1:n_ele]
  111. # elemental g
  112. g = np.zeros((2, p))
  113. g[0, 0], g[-1, -1] = self.tau_pos, self.tau_neg
  114. # mapping matrix
  115. map_g_x = map_h
  116. map_g_y = np.arange(p * n_ele, dtype=int).reshape(n_ele, p).T
  117. # assemble global G
  118. G = np.zeros((n_ele + 1, p * n_ele))
  119. for i in range(n_ele):
  120. for j in range(2):
  121. m = map_g_x[j, i]
  122. for k in range(p):
  123. n = map_g_y[k, i]
  124. G[m, n] += g[j, k]
  125. G = G[1:n_ele, :]
  126. # elemental e
  127. e = np.zeros((p, 2))
  128. e[0, 0], e[-1, -1] = -con - self.tau_pos, con - self.tau_neg
  129. # mapping matrix
  130. map_e_x = np.arange(p * n_ele, dtype=int).reshape(n_ele, p).T
  131. map_e_y = map_h
  132. # assemble global E
  133. E = np.zeros((p * n_ele, n_ele + 1))
  134. for i in range(n_ele):
  135. for j in range(p):
  136. m = map_e_x[j, i]
  137. for k in range(2):
  138. n = map_e_y[k, i]
  139. E[m, n] += e[j, k]
  140. E = E[:, 1:n_ele]
  141. # L, easy in 1d
  142. L = np.zeros(n_ele - 1)
  143. # elemental c
  144. c = np.zeros((p, 2))
  145. c[0, 0], c[-1, -1] = -1, 1
  146. # assemble global C
  147. C = np.zeros((p * n_ele, n_ele + 1))
  148. for i in range(n_ele):
  149. for j in range(p):
  150. m = map_e_x[j, i]
  151. for k in range(2):
  152. n = map_e_y[k, i]
  153. C[m, n] += c[j, k]
  154. C = C[:, 1:n_ele]
  155. # L, easy in 1d
  156. L = np.zeros(n_ele - 1)
  157. # R, easy in 1d
  158. R = np.zeros(p * n_ele)
  159. R[0] = self.bc(0)[0]
  160. R[-1] = -self.bc(0)[1]
  161. return d, m, E, G, H, F, L, a, b, C, R
  162. def solve_local(self, index, x):
  163. """ solve the 1d advection equation wit local HDG"""
  164. d, _, E, G, H, F, L, a, b, C, R = self.matrix_gen(index, x)
  165. # find dx
  166. dx = np.zeros(self.numEle + len(index))
  167. for i in range(1, self.numEle + len(index) + 1):
  168. x_i, dx_i, x_n = self.mesh(i, index, x)
  169. dx[i - 1] = dx_i
  170. # assemble global D
  171. bb = np.zeros((self.numBasisFuncs, self.numBasisFuncs))
  172. bb[0, 0] = self.tau_pos
  173. bb[-1, -1] = self.tau_neg
  174. D = np.repeat(dx, self.numBasisFuncs) / 2 * block_diag(*[d] * (
  175. self.numEle + len(index))) + block_diag(*[bb] * (self.numEle + len(index)))
  176. # assemble global A
  177. A = np.repeat(dx, self.numBasisFuncs) / 2 * block_diag(*
  178. [a] * (self.numEle + len(index)))
  179. # assemble global B
  180. B = block_diag(*[b] * (self.numEle + len(index)))
  181. # solve U and \lambda
  182. K = -np.concatenate((C.T, G), axis=1).dot(np.linalg.inv(
  183. np.bmat([[A, -B], [B.T, D]])).dot(np.concatenate((C, E)))) + H
  184. F_hat = np.array([L]).T - np.concatenate((C.T, G), axis=1).dot(np.linalg.inv(
  185. np.bmat([[A, -B], [B.T, D]]))).dot(np.array([np.concatenate((R, F))]).T)
  186. lamba = np.linalg.solve(K, F_hat)
  187. U = np.linalg.inv(np.bmat([[A, -B], [B.T, D]])).dot(
  188. np.array([np.concatenate((R, F))]).T - np.concatenate((C, E)).dot(lamba))
  189. return U, lamba
  190. def solve_adjoint(self, index, x, u, u_hat):
  191. self.numBasisFuncs = self.numBasisFuncs + 1
  192. d, _, E, G, H, F, L, a, b, C, R = self.matrix_gen(index, x)
  193. # add boundary
  194. F = np.zeros(len(F))
  195. R[-1] = -self.bc(1)[1]
  196. # find dx
  197. dx = np.zeros(self.numEle + len(index))
  198. for i in range(1, self.numEle + len(index) + 1):
  199. x_i, dx_i, x_n = self.mesh(i, index, x)
  200. dx[i - 1] = dx_i
  201. # assemble global D
  202. bb = np.zeros((self.numBasisFuncs, self.numBasisFuncs))
  203. bb[0, 0] = self.tau_pos
  204. bb[-1, -1] = self.tau_neg
  205. D = np.repeat(dx, self.numBasisFuncs) / 2 * block_diag(*[d] * (
  206. self.numEle + len(index))) + block_diag(*[bb] * (self.numEle + len(index)))
  207. # assemble global A
  208. A = np.repeat(dx, self.numBasisFuncs) / 2 * block_diag(*
  209. [a] * (self.numEle + len(index)))
  210. # assemble global B
  211. B = block_diag(*[b] * (self.numEle + len(index)))
  212. # # assemble global matrix LHS
  213. LHS = np.bmat([[A, -B, C], [B.T, D, E], [C.T, G, H]])
  214. # solve U and \lambda
  215. U = np.linalg.solve(LHS.T, np.concatenate((R, F, L)))
  216. return U[0:2 * self.numBasisFuncs * (self.numEle + len(index))], U[2 * self.numBasisFuncs * (self.numEle + len(index)):len(U)]
  217. def diffusion(self):
  218. """solve 1d convection with local HDG"""
  219. # begin and end time
  220. t, T = 0, 1
  221. # time marching step for diffusion equation
  222. dt = 1e-3
  223. d, m, E, G, H, F, L, a, b, C, R = self.matrix_gen()
  224. # add time derivatives to the space derivatives (both are
  225. # elmental-wise)
  226. d = d + 1 / dt * m
  227. # assemble global D
  228. D = block_diag(*[d] * self.numEle)
  229. # assemble global A
  230. A = block_diag(*[a] * self.numEle)
  231. # assemble global B
  232. B = block_diag(*[b] * self.numEle)
  233. # initial condition
  234. X = np.zeros(self.numBasisFuncs * self.numEle)
  235. for i in range(1, self.numEle + 1):
  236. x = self.mesh(i)
  237. X[(i - 1) * self.numBasisFuncs:(i - 1) *
  238. self.numBasisFuncs + self.numBasisFuncs] = x
  239. U = np.concatenate((pi * cos(pi * X), sin(pi * X)))
  240. # assemble M
  241. M = block_diag(*[1 / dt * m] * self.numEle)
  242. # time marching
  243. while t < T:
  244. # add boundary conditions
  245. F_dynamic = F + \
  246. M.dot(U[self.numEle * self.numBasisFuncs:2 *
  247. self.numEle * self.numBasisFuncs])
  248. # assemble global matrix LHS
  249. LHS = np.bmat([[A, -B, C], [B.T, D, E], [C.T, G, H]])
  250. # solve U and \lambda
  251. U = np.linalg.solve(LHS, np.concatenate((R, F_dynamic, L)))
  252. # plot solutions
  253. plt.clf()
  254. plt.plot(X, U[self.numEle * self.numBasisFuncs:2 *
  255. self.numEle * self.numBasisFuncs], '-r.')
  256. plt.plot(X, sin(pi * X) * np.exp(-pi**2 * t))
  257. plt.ylim([0, 1])
  258. plt.grid()
  259. plt.pause(1e-3)
  260. plt.close()
  261. print("Diffusion equation du/dt - du^2/d^2x = 0 with u_exact ="
  262. ' 6sin(pi*x)*exp(-pi^2*t).')
  263. plt.figure(1)
  264. plt.plot(X, U[self.numEle * self.numBasisFuncs:2 *
  265. self.numEle * self.numBasisFuncs], '-r.')
  266. plt.plot(X, sin(pi * X) * np.exp(-pi**2 * T))
  267. plt.xlabel('x')
  268. plt.ylabel('y')
  269. plt.legend(('Numberical', 'Exact'), loc='upper left')
  270. plt.title('Simple Diffusion Equation Solution at t = {}'.format(T))
  271. plt.grid()
  272. plt.savefig('diffusion', bbox_inches='tight')
  273. plt.show(block=False)
  274. return U
  275. def residual(self, U, hat_U, z, hat_z, dx, index, x_c):
  276. n_ele = self.numEle
  277. # order of polynomial shape functions
  278. p = self.numBasisFuncs
  279. p_l = p - 1
  280. # order of gauss quadrature
  281. gorder = 2 * p
  282. # shape function and gauss quadrature
  283. xi, wi = np.polynomial.legendre.leggauss(gorder)
  284. shp, shpx = self.shape(xi, p)
  285. shp_l, shpx_l = self.shape(xi, p_l)
  286. # ---------------------------------------------------------------------
  287. # advection constant
  288. con = self.c
  289. # diffusion constant
  290. kappa = self.kappa
  291. z_q, z_u, z_hat = np.zeros(self.numBasisFuncs * self.numEle), \
  292. np.zeros(self.numBasisFuncs *
  293. self.numEle), np.zeros(self.numEle - 1)
  294. q, u, lamba = np.zeros(p_l * self.numEle), \
  295. np.zeros(p_l * self.numEle), np.zeros(self.numEle - 1)
  296. for i in np.arange(self.numBasisFuncs * self.numEle):
  297. z_q[i] = z[i]
  298. z_u[i] = z[i + self.numBasisFuncs * self.numEle]
  299. for i in np.arange(p_l * self.numEle):
  300. q[i] = U[i]
  301. u[i] = U[i + p_l * self.numEle]
  302. for i in np.arange(self.numEle - 1):
  303. z_hat[i] = hat_z[i]
  304. # add boundary condtions to U_hat
  305. U_hat = np.zeros(self.numEle + 1)
  306. for i, x in enumerate(hat_U):
  307. U_hat[i + 1] = x
  308. U_hat[0] = self.bc(0)[0]
  309. U_hat[-1] = self.bc(0)[1]
  310. # L, easy in 1d
  311. L = np.zeros(n_ele + 1)
  312. # R, easy in 1d
  313. RR = np.zeros(p * n_ele)
  314. # elemental forcing vector
  315. F = np.zeros(p * n_ele)
  316. for i in range(1, n_ele + 1):
  317. f = dx[i - 1] / 2 * \
  318. shp.dot(
  319. wi * self.forcing(x_c[0] + 1 / 2 * (1 + xi) * dx[i - 1]))
  320. F[(i - 1) * p:(i - 1) * p + p] = f
  321. # elemental h
  322. h = np.zeros((2, 2))
  323. h[0, 0], h[-1, -1] = -con - self.tau_pos, con - self.tau_neg
  324. # mappinng matrix
  325. map_h = np.zeros((2, n_ele), dtype=int)
  326. map_h[:, 0] = np.arange(2)
  327. for i in np.arange(1, n_ele):
  328. map_h[:, i] = np.arange(
  329. map_h[2 - 1, i - 1], map_h[2 - 1, i - 1] + 2)
  330. # assemble H and eliminate boundaries
  331. H = np.zeros((n_ele + 1, n_ele + 1))
  332. for i in range(n_ele):
  333. for j in range(2):
  334. m = map_h[j, i]
  335. for k in range(2):
  336. n = map_h[k, i]
  337. H[m, n] += h[j, k]
  338. H = H[1:n_ele][:, 1:n_ele]
  339. # elemental g
  340. g = np.zeros((2, p_l))
  341. g[0, 0], g[-1, -1] = self.tau_pos, self.tau_neg
  342. # mapping matrix
  343. map_g_x = map_h
  344. map_g_y = np.arange(p_l * n_ele, dtype=int).reshape(n_ele, p_l).T
  345. # assemble global G
  346. G = np.zeros((n_ele + 1, p_l * n_ele))
  347. for i in range(n_ele):
  348. for j in range(2):
  349. m = map_g_x[j, i]
  350. for k in range(p_l):
  351. n = map_g_y[k, i]
  352. G[m, n] += g[j, k]
  353. G = G[1:n_ele, :]
  354. # elemental c
  355. c = np.zeros((p_l, 2))
  356. c[0, 0], c[-1, -1] = -1, 1
  357. # mapping matrix
  358. map_e_x = np.arange(p_l * n_ele, dtype=int).reshape(n_ele, p_l).T
  359. map_e_y = map_h
  360. # assemble global C
  361. C = np.zeros((p_l * n_ele, n_ele + 1))
  362. for i in range(n_ele):
  363. for j in range(p_l):
  364. m = map_e_x[j, i]
  365. for k in range(2):
  366. n = map_e_y[k, i]
  367. C[m, n] += c[j, k]
  368. C = C[:, 1:n_ele]
  369. # L, easy in 1d
  370. L = np.zeros(n_ele - 1)
  371. # residual vector
  372. R = np.zeros(self.numEle)
  373. for i in np.arange(self.numEle):
  374. a = dx[i] / 2 * 1 / kappa * \
  375. ((shp.T).T).dot(np.diag(wi).dot(shp_l.T))
  376. b = ((shpx.T) * np.ones((gorder, p))
  377. ).T.dot(np.diag(wi).dot(shp_l.T))
  378. b_t = ((shpx_l.T) * np.ones((gorder, p_l))
  379. ).T.dot(np.diag(wi).dot(shp.T))
  380. d = dx[i] / 2 * shp.dot(np.diag(wi).dot(shp_l.T))
  381. d[0, 0] += self.tau_pos
  382. d[-1, -1] += self.tau_neg
  383. h = np.zeros((2, 2))
  384. h[0, 0], h[-1, -1] = -con - self.tau_pos, con - self.tau_neg
  385. g = np.zeros((2, p_l))
  386. g[0, 0], g[-1, -1] = self.tau_pos, self.tau_neg
  387. e = np.zeros((p, 2))
  388. e[0, 0], e[-1, -1] = -con - self.tau_pos, con - self.tau_neg
  389. c = np.zeros((p, 2))
  390. c[0, 0], c[-1, -1] = -1, 1
  391. m = np.zeros((2, p_l))
  392. m[0, 0], m[-1, -1] = -1, 1
  393. # local error
  394. R[i] = (np.concatenate((a.dot(q[p_l * i:p_l * i + p_l]) + -b.dot(u[p_l * i:p_l * i + p_l]) + c.dot(U_hat[i:i + 2]),
  395. b_t.T.dot(q[p_l * i:p_l * i + p_l]) + d.dot(u[p_l * i:p_l * i + p_l]) + e.dot(U_hat[i:i + 2]))) - np.concatenate((RR[p * i:p * i + p], F[p * i:p * i + p]))).dot(1 - np.concatenate((z_q[p * i:p * i + p], z_u[p * i:p * i + p])))
  396. com_index = np.argsort(np.abs(R))
  397. # select \theta% elements with the large error
  398. theta = 0.15
  399. refine_index = com_index[int(self.numEle * (1 - theta)):len(R)]
  400. self.numBasisFuncs = self.numBasisFuncs - 1
  401. # global error
  402. R_g = (C.T.dot(q) + G.dot(u) + H.dot(U_hat[1:-1])).dot(1 - z_hat)
  403. return np.abs(np.sum(R) + np.sum(R_g)), refine_index + 1
  404. def adaptive(self):
  405. x = np.linspace(0, 1, self.numEle + 1)
  406. index = []
  407. U, hat_U = self.solve_local(index, x)
  408. U_adjoint, hat_adjoint = self.solve_adjoint(index, x, U, hat_U)
  409. X = np.zeros(self.numBasisFuncs * self.numEle)
  410. dx = np.zeros(self.numEle + len(index))
  411. for i in range(1, self.numEle + 1):
  412. x_i, dx_i, x_n = self.mesh(i, index, x)
  413. X[(i - 1) * self.numBasisFuncs:(i - 1) *
  414. self.numBasisFuncs + self.numBasisFuncs] = x_i
  415. dx[i - 1] = dx_i
  416. numAdaptive = 28
  417. trueError = np.zeros((2, numAdaptive))
  418. estError = np.zeros((2, numAdaptive))
  419. for i in np.arange(numAdaptive):
  420. est_error, index = self.residual(
  421. U, hat_U, U_adjoint, hat_adjoint, dx, index, x)
  422. index = index.tolist()
  423. U, hat_U = self.solve_local(index, x)
  424. U_adjoint, hat_adjoint = self.solve_adjoint(index, x, U, hat_U)
  425. self.numBasisFuncs = self.numBasisFuncs - 1
  426. X = np.zeros(self.numBasisFuncs * (self.numEle + len(index)))
  427. dx = np.zeros(self.numEle + len(index))
  428. for j in range(1, self.numEle + len(index) + 1):
  429. x_i, dx_i, x_n = self.mesh(j, index, x)
  430. X[(j - 1) * self.numBasisFuncs:(j - 1) *
  431. self.numBasisFuncs + self.numBasisFuncs] = x_i
  432. dx[j - 1] = dx_i
  433. x = x_n
  434. estError[0, i] = self.numEle
  435. estError[1, i] = est_error
  436. self.numEle = self.numEle + len(index)
  437. # U_1d = np.zeros(len(U))
  438. # for j in np.arange(len(U)):
  439. # U_1d[j] = U[j]
  440. # Unum = np.array([])
  441. # Xnum = np.array([])
  442. # Qnum = np.array([])
  443. # for j in range(1, self.numEle + 1):
  444. # # Gauss quadrature
  445. # gorder = 10 * self.numBasisFuncs
  446. # xi, wi = np.polynomial.legendre.leggauss(gorder)
  447. # shp, shpx = self.shape(xi, self.numBasisFuncs)
  448. # Xnum = np.hstack((Xnum, (X[(j - 1) * self.numBasisFuncs + self.numBasisFuncs - 1] + X[(j - 1) * self.numBasisFuncs]) / 2 + (
  449. # X[(j - 1) * self.numBasisFuncs + self.numBasisFuncs - 1] - X[(j - 1) * self.numBasisFuncs]) / 2 * xi))
  450. # Unum = np.hstack(
  451. # (Unum, shp.T.dot(U_1d[int(len(U) / 2) + (j - 1) * self.numBasisFuncs:int(len(U) / 2) + j * self.numBasisFuncs])))
  452. # Qnum = np.hstack(
  453. # (Qnum, shp.T.dot(U_1d[int((j - 1) * self.numBasisFuncs):j * self.numBasisFuncs])))
  454. # if i in [0, 4, 9, 19]:
  455. # plt.plot(Xnum, Unum, '-', color='C3')
  456. # plt.plot(X, U[int(len(U) / 2):len(U)], 'C3.')
  457. # plt.xlabel('$x$', fontsize=17)
  458. # plt.ylabel('$u$', fontsize=17)
  459. # plt.axis([-0.05, 1.05, 0, 1.3])
  460. # plt.grid()
  461. # # plt.savefig('u_test_{}.pdf'.format(i+1))
  462. # plt.show()
  463. # plt.clf()
  464. trueError[0, i] = self.numEle
  465. trueError[1, i] = np.abs(
  466. U[self.numEle * self.numBasisFuncs - 1] - np.sqrt(self.kappa))
  467. self.numBasisFuncs = self.numBasisFuncs + 1
  468. self.trueError = trueError
  469. self.estError = estError