test_gjp_rec.py
Go to the documentation of this file.
1 import subprocess
2 
3 import numpy as np
4 import pytest
5 
6 
7 def extract_values(output_str):
8  lines = output_str.strip().split("\n")
9  roots = []
10  weights = []
11  for line in lines:
12  _, root, _, weight = line.split()
13  roots.append(float(root))
14  weights.append(float(weight))
15  return np.array(roots), np.array(weights)
16 
17 
18 def run_fortran(n, alpha, beta, method):
19  result = subprocess.run(
20  [
21  "fpm",
22  "run",
23  "gjp_quad",
24  "--",
25  str(n),
26  "{:.1f}".format(alpha),
27  "{:.1f}".format(beta),
28  method,
29  ],
30  stdout=subprocess.PIPE,
31  )
32  return extract_values(result.stdout.decode())
33 
34 
35 def run_python(n, alpha, beta):
36  result = subprocess.run(
37  [
38  "python",
39  "scripts/sympy_gauss_jac.py",
40  "--npts",
41  str(n),
42  "--alpha",
43  str(alpha),
44  "--beta",
45  str(beta),
46  ],
47  stdout=subprocess.PIPE,
48  )
49  return extract_values(result.stdout.decode())
50 
51 
52 @pytest.mark.parametrize( "n, alpha, beta, method", [ (3, 1, 5, "rec"),
53  (5, 2, 3, "rec"),
54  pytest.param(
55  10,
56  0.0,
57  30.0,
58  "rec",
59  marks=pytest.mark.xfail(reason="High beta values diverge"),
60  ),
61  (3, 1, 5, "gw"),
62  (5, 2, 3, "gw"),
63  pytest.param(
64  10,
65  0.0,
66  30.0,
67  "gw",
68  ),
69  ],
70 )
71 def test_gjp_quad_rec(n, alpha, beta, method):
72  fortran_roots, fortran_weights = run_fortran(n, alpha, beta, method)
73  python_roots, python_weights = run_python(n, alpha, beta)
74 
75  assert np.allclose(fortran_roots, python_roots, atol=1e-14)
76  assert np.allclose(fortran_weights, python_weights, atol=1e-14)
77 
def run_python(n, alpha, beta)
Definition: test_gjp_rec.py:35
def test_gjp_quad_rec(n, alpha, beta, method)
Definition: test_gjp_rec.py:74
def run_fortran(n, alpha, beta, method)
Definition: test_gjp_rec.py:18
def extract_values(output_str)
Definition: test_gjp_rec.py:7
Code