Skip to content

Commit 7f4a510

Browse files
committed
parameterize tests to get rid of vectorized input for shape functions
1 parent ee94ddb commit 7f4a510

File tree

1 file changed

+84
-76
lines changed

1 file changed

+84
-76
lines changed

Diff for: tests/test_femutil.py

+84-76
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,31 @@
99

1010

1111
#%% Tests for Shape functions and derivatives
12-
def test_shape_tri3():
12+
result = np.eye(3)
13+
@pytest.mark.parametrize("r, s, res",[
14+
[0.0, 0.0,result[0]],
15+
[1.0, 0.0,result[1]],
16+
[0.0, 1.0,result[2]]
17+
])
18+
def test_shape_tri3(r,s,res):
1319
# Interpolation condition check
14-
coords = np.array([
15-
[0.0, 0.0],
16-
[1.0, 0.0],
17-
[0.0, 1.0]])
18-
N, _ = fem.shape_tri3(coords[:, 0], coords[:, 1])
19-
assert np.allclose(N, np.eye(3))
20-
21-
22-
def test_shape_tri6():
20+
N, _ = fem.shape_tri3(r,s)
21+
assert np.allclose(N,res)
22+
23+
result = np.eye(6)
24+
@pytest.mark.parametrize("r, s, res",[
25+
[0.0, 0.0,result[0]],
26+
[1.0, 0.0,result[1]],
27+
[0.0, 1.0,result[2]],
28+
[0.5, 0.0,result[3]],
29+
[0.5, 0.5,result[4]],
30+
[0.0, 0.5,result[5]]])
31+
def test_shape_tri6_N(r,s,res):
2332
# Interpolation condition check
24-
coords = np.array([
25-
[0.0, 0.0],
26-
[1.0, 0.0],
27-
[0.0, 1.0],
28-
[0.5, 0.0],
29-
[0.5, 0.5],
30-
[0.0, 0.5]])
31-
N, _ = fem.shape_tri6(coords[:, 0], coords[:, 1])
32-
assert np.allclose(N, np.eye(6))
33+
N, _ = fem.shape_tri6(r,s)
34+
assert np.allclose(N, res)
3335

36+
def test_shape_tri6_dNdr():
3437
# Evaluation at (1/3, 1/3)
3538
N, dNdr = fem.shape_tri6(1/3, 1/3)
3639
N_exp = np.array([-1., -1., -1., 4., 4., 4.])/9
@@ -40,38 +43,41 @@ def test_shape_tri6():
4043
assert np.allclose(N, N_exp)
4144
assert np.allclose(dNdr, dNdr_exp)
4245

43-
44-
def test_shape_quad4():
46+
result = np.eye(4)
47+
@pytest.mark.parametrize("r, s, res",[
48+
[-1.0, -1.0,result[0]],
49+
[1.0, -1.0,result[1]],
50+
[1.0, 1.0,result[2]],
51+
[-1.0, 1.0,result[3]]])
52+
def test_shape_quad4_N(r,s,res):
4553
# Interpolation condition check
46-
coords = np.array([
47-
[-1.0, -1.0],
48-
[1.0, -1.0],
49-
[1.0, 1.0],
50-
[-1.0, 1.0]])
51-
N, _ = fem.shape_quad4(coords[:, 0], coords[:, 1])
52-
assert np.allclose(N, np.eye(4))
54+
N, _ = fem.shape_quad4(r,s)
55+
assert np.allclose(N, res)
5356

57+
def test_shape_quad4_dNdr():
5458
# For point (0, 0)
5559
N, _ = fem.shape_quad4(0, 0)
5660
N_ex = 0.25 * np.array([[1, 1, 1, 1]])
5761
assert np.allclose(N, N_ex)
5862

5963

60-
def test_shape_quad9():
64+
result = np.eye(9)
65+
@pytest.mark.parametrize("r, s, res",[
66+
[-1.0, -1.0,result[0]],
67+
[ 1.0, -1.0,result[1]],
68+
[ 1.0, 1.0,result[2]],
69+
[-1.0, 1.0,result[3]],
70+
[ 0.0, -1.0,result[4]],
71+
[ 1.0, 0.0,result[5]],
72+
[ 0.0, 1.0,result[6]],
73+
[-1.0, 0.0,result[7]],
74+
[ 0.0, 0.0,result[8]]])
75+
def test_shape_quad9_N(r,s,res):
6176
# Interpolation condition check
62-
coords = np.array([
63-
[-1.0, -1.0],
64-
[ 1.0, -1.0],
65-
[ 1.0, 1.0],
66-
[-1.0, 1.0],
67-
[ 0.0, -1.0],
68-
[ 1.0, 0.0],
69-
[ 0.0, 1.0],
70-
[-1.0, 0.0],
71-
[ 0.0, 0.0]])
72-
N, _ = fem.shape_quad9(coords[:, 0], coords[:, 1])
73-
assert np.allclose(N, np.eye(9))
77+
N, _ = fem.shape_quad9(r, s)
78+
assert np.allclose(N, res)
7479

80+
def test_shape_quad9_dNdr():
7581
# Evaluation at (1/4, 1/4)
7682
N, dNdr = fem.shape_quad9(0.25, 0.25)
7783
N_exp = np.array(
@@ -87,47 +93,49 @@ def test_shape_quad9():
8793
assert np.allclose(N, N_exp)
8894
assert np.allclose(dNdr, dNdr_exp)
8995

90-
91-
def test_shape_quad8():
96+
result = np.eye(8)
97+
@pytest.mark.parametrize("r, s, res",[
98+
[-1.0, -1.0,result[0]],
99+
[ 1.0, -1.0,result[1]],
100+
[ 1.0, 1.0,result[2]],
101+
[-1.0, 1.0,result[3]],
102+
[ 0.0, -1.0,result[4]],
103+
[ 1.0, 0.0,result[5]],
104+
[ 0.0, 1.0,result[6]],
105+
[-1.0, 0.0,result[7]]])
106+
def test_shape_quad8(r,s,res):
92107
# Interpolation condition check
93-
coords = np.array([
94-
[-1.0, -1.0],
95-
[ 1.0, -1.0],
96-
[ 1.0, 1.0],
97-
[-1.0, 1.0],
98-
[ 0.0, -1.0],
99-
[ 1.0, 0.0],
100-
[ 0.0, 1.0],
101-
[-1.0, 0.0]])
102-
N, _ = fem.shape_quad8(coords[:, 0], coords[:, 1])
103-
assert np.allclose(N, np.eye(8))
108+
N, _ = fem.shape_quad8(r,s)
109+
assert np.allclose(N, res)
104110

105111

106112
# 3D elements
107-
def test_shape_tet4():
113+
result = np.eye(4)
114+
@pytest.mark.parametrize("r, s, t, res",[
115+
[0.0, 0.0, 0.0,result[0]],
116+
[1.0, 0.0, 0.0,result[1]],
117+
[0.0, 1.0, 0.0,result[2]],
118+
[0.0, 0.0, 1.0,result[3]]
119+
])
120+
def test_shape_tet4(r,s,t,res):
108121
# Interpolation condition check
109-
coords = np.array([
110-
[0.0, 0.0, 0.0],
111-
[1.0, 0.0, 0.0],
112-
[0.0, 1.0, 0.0],
113-
[0.0, 0.0, 1.0]])
114-
N, _ = fem.shape_tet4(coords[:, 0], coords[:, 1], coords[:, 2])
115-
assert np.allclose(N, np.eye(4))
116-
117-
118-
def test_shape_hex():
122+
N, _ = fem.shape_tet4(r, s, t)
123+
assert np.allclose(N, res)
124+
125+
result = np.eye(8)
126+
@pytest.mark.parametrize("r, s, t, res",[
127+
[-1.0, -1.0, -1.0,result[0]],
128+
[ 1.0, -1.0, -1.0,result[1]],
129+
[ 1.0, 1.0, -1.0,result[2]],
130+
[-1.0, 1.0, -1.0,result[3]],
131+
[-1.0, -1.0, 1.0,result[4]],
132+
[ 1.0, -1.0, 1.0,result[5]],
133+
[ 1.0, 1.0, 1.0,result[6]],
134+
[-1.0, 1.0, 1.0,result[7]]])
135+
def test_shape_hex(r,s,t,res):
119136
# Interpolation condition check
120-
coords = np.array([
121-
[-1.0, -1.0, -1.0],
122-
[ 1.0, -1.0, -1.0],
123-
[ 1.0, 1.0, -1.0],
124-
[-1.0, 1.0, -1.0],
125-
[-1.0, -1.0, 1.0],
126-
[ 1.0, -1.0, 1.0],
127-
[ 1.0, 1.0, 1.0],
128-
[-1.0, 1.0, 1.0]])
129-
N, _ = fem.shape_hex8(coords[:, 0], coords[:, 1], coords[:, 2])
130-
assert np.allclose(N, np.eye(8))
137+
N, _ = fem.shape_hex8(r, s, t)
138+
assert np.allclose(N, res)
131139

132140

133141
#%% Jacobian

0 commit comments

Comments
 (0)