test_solve_test_problems.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # content of test_module.py
  2. import pytest
  3. import math
  4. import matplotlib
  5. matplotlib.use('Agg') # supress figures in the following modules
  6. from hdpg1d.preprocess import coefficients as coeff
  7. from hdpg1d.adaptation import hdpg1d
  8. testData = [
  9. ([1e-4, 0, 1, 2, 2, 1, 1, 1e-10, 50], 1e-2), # diffusion reaction
  10. ([0, 1, 0, 2, 2, 1, 1, 1e-10, 50], 0), # convection
  11. # ([1, 1, 0, 2, 2, 1, 1,1e-10,50], 1) # diffusion convection)
  12. ]
  13. class TestClass(object):
  14. @pytest.fixture(scope="module", params=testData)
  15. def coeffGen(self, request):
  16. coeffTest = coeff(*request.param[0])
  17. expected = request.param[1]
  18. yield coeffTest, expected # teardown
  19. def test_zeroDivision(self, monkeypatch):
  20. coeffTest = coeff(*([0] * 9))
  21. assert coeffTest.DIFFUSION != 0
  22. def test_solveAdaptive(self, coeffGen):
  23. coeffTest, expected = coeffGen
  24. hdpgTest = hdpg1d(coeffTest)
  25. hdpgTest.adaptive()
  26. # get the target function value
  27. # and compare to the expected value
  28. soln = hdpgTest.trueErrorList[1][-1]
  29. assert math.isclose(soln, expected, rel_tol=1e-5, abs_tol=1e-10)
  30. def test_solvePrimal(self, coeffGen):
  31. coeffTest, expected = coeffGen
  32. # test the primal solver on a refined mesh and higher poly order
  33. coeffTest.pOrder = 5
  34. coeffTest.numEle = 300
  35. hdpgTest = hdpg1d(coeffTest)
  36. hdpgTest.solvePrimal()
  37. soln = hdpgTest.primalSoln[hdpgTest.numEle *
  38. hdpgTest.numBasisFuncs - 1]
  39. assert math.isclose(soln, expected, rel_tol=1e-5, abs_tol=1e-10)