postprocess.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. A module for postprocessing the numerical results from HDPG1d solver.
  3. """
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. def errorL2(solution):
  7. errorL2 = 0.
  8. # solve the uniform case
  9. x = np.linspace(0, 1, solution.n_ele + 1)
  10. U, _ = solution.solve_local([], x)
  11. errorL2 = np.abs(U[solution.p * solution.n_ele - 1] -
  12. np.sqrt(solution.kappa))
  13. return errorL2
  14. def uniConv(solution):
  15. p = np.arange(2, 3)
  16. n_ele = 2**np.arange(1, 9)
  17. uniError = np.zeros((n_ele.size, p.size))
  18. for i in range(p.size):
  19. solution.p = p[i]
  20. for j, n in enumerate(n_ele):
  21. solution.n_ele = n
  22. uniError[j, i] = errorL2(solution)
  23. return n_ele, uniError
  24. def convHistory(solution):
  25. plt.loglog(solution.trueError[0, 0:-1],
  26. solution.trueError[1, 0:-1], '-ro')
  27. # plt.axis([1, 250, 1e-13, 1e-2])
  28. n_ele, errorL2 = uniConv(solution)
  29. plt.loglog(n_ele, errorL2, '-o')
  30. plt.loglog(solution.estError[0, :],
  31. solution.estError[1, :], '--', color='#1f77b4')
  32. plt.xlabel('Number of elements', fontsize=17)
  33. plt.ylabel('Error', fontsize=17)
  34. plt.grid()
  35. plt.legend(('Adaptive', 'Uniform', 'Estimator'), loc=3, fontsize=15)
  36. plt.show()