11from mint import Grid , PolylineIntegral
22import numpy
3- import sys
4- from pathlib import Path
3+ import pytest
54
65
76def potentialFunc (p ):
87 x , y = p [:2 ]
98 return x + 2 * y
109
1110
12- def test_simple ():
11+ def singularPotentialFunc (p ):
12+ x , y = p [:2 ]
13+ return numpy .arctan2 (y , x )/ (2. * numpy .pi )
14+
15+
16+ @pytest .mark .parametrize ("nx" , [3 ])
17+ @pytest .mark .parametrize ("ny" , [2 ])
18+ @pytest .mark .parametrize ("potFunc" , [potentialFunc , singularPotentialFunc ])
19+ @pytest .mark .parametrize ("xyz" , [numpy .array ([(1. , 0. , 0. ),
20+ (0. , 1. , 0. )]),
21+ numpy .array ([(0. , 0. , 0. ),
22+ (1. , 0. , 0. ),
23+ (1. , 1. , 0. ),
24+ (0. , 1. , 0. )])])
25+ def test_simple (nx , ny , potFunc , xyz ):
1326
1427 # create the grid and the edge data
1528 gr = Grid ()
1629
17- nx , ny = 3 , 2
1830 points = numpy .zeros ((nx * ny , 4 , 3 ), numpy .float64 )
1931 data = numpy .zeros ((nx * ny , 4 ))
2032 dx = 1.0 / float (nx )
@@ -46,40 +58,36 @@ def test_simple():
4658 # | |
4759 # +-->--+
4860 # 0
49- data [k , 0 ] = potentialFunc (points [k , 1 , :]) - potentialFunc (points [k , 0 , :])
50- data [k , 1 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 1 , :])
51- data [k , 2 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 3 , :])
52- data [k , 3 ] = potentialFunc (points [k , 3 , :]) - potentialFunc (points [k , 0 , :])
61+ data [k , 0 ] = potFunc (points [k , 1 , :]) - potFunc (points [k , 0 , :])
62+ data [k , 1 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 1 , :])
63+ data [k , 2 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 3 , :])
64+ data [k , 3 ] = potFunc (points [k , 3 , :]) - potFunc (points [k , 0 , :])
5365
5466 # increment the cell counter
5567 k += 1
5668
5769 gr .setPoints (points )
5870 gr .dump ('test_polyline_integral.vtk' )
5971
60-
6172 pli = PolylineIntegral ()
62-
63- # create the polyline through which the flux will be integrated
64- xyz = numpy .array ([(0. , 0. , 0. ),
65- (1. , 0. , 0. ),
66- (1. , 1. , 0. ),
67- (0. , 1. , 0. )])
68-
6973 # no periodicity in x
7074 pli .build (gr , xyz , counterclock = False , periodX = 0.0 )
7175
7276 flux = pli .getIntegral (data )
73- exactFlux = potentialFunc (xyz [- 1 , :]) - potentialFunc (xyz [0 , :])
77+ exactFlux = potFunc (xyz [- 1 , :]) - potFunc (xyz [0 , :])
7478 print (f'total flux: { flux :.3f} exact flux: { exactFlux :.3f} ' )
7579 assert abs (flux - exactFlux ) < 1.e-10
7680
77- def test_partially_outside ():
7881
82+ @pytest .mark .parametrize ("nx" , [3 ])
83+ @pytest .mark .parametrize ("ny" , [2 ])
84+ @pytest .mark .parametrize ("potFunc" , [potentialFunc ])
85+ def test_partially_outside (nx , ny , potFunc ):
86+
87+ print ('target line is partially outside the domain, expect a warning!' )
7988 # create the grid and the edge data
8089 gr = Grid ()
8190
82- nx , ny = 3 , 2
8391 points = numpy .zeros ((nx * ny , 4 , 3 ), numpy .float64 )
8492 data = numpy .zeros ((nx * ny , 4 ))
8593 dx = 1.0 / float (nx )
@@ -111,10 +119,10 @@ def test_partially_outside():
111119 # | |
112120 # +-->--+
113121 # 0
114- data [k , 0 ] = potentialFunc (points [k , 1 , :]) - potentialFunc (points [k , 0 , :])
115- data [k , 1 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 1 , :])
116- data [k , 2 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 3 , :])
117- data [k , 3 ] = potentialFunc (points [k , 3 , :]) - potentialFunc (points [k , 0 , :])
122+ data [k , 0 ] = potFunc (points [k , 1 , :]) - potFunc (points [k , 0 , :])
123+ data [k , 1 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 1 , :])
124+ data [k , 2 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 3 , :])
125+ data [k , 3 ] = potFunc (points [k , 3 , :]) - potFunc (points [k , 0 , :])
118126
119127 # increment the cell counter
120128 k += 1
@@ -135,19 +143,23 @@ def test_partially_outside():
135143 flux = pli .getIntegral (data )
136144
137145 # because the first point is outside the domain, only the contribution
138- # stemming from the path inside the domain will be computed. Let's
146+ # stemming from the path inside the domain will be computed. Let's
139147 # correct for this by moving the first point inwards
140148 xyz [0 , 0 ] = 0.
141149 exactFlux = potentialFunc (xyz [- 1 , :]) - potentialFunc (xyz [0 , :])
142150 print (f'total flux: { flux :.3f} exact flux: { exactFlux :.3f} ' )
143151 assert abs (flux - exactFlux ) < 1.e-10
144152
145- def test_completely_outside ():
146153
154+ @pytest .mark .parametrize ("nx" , [3 ])
155+ @pytest .mark .parametrize ("ny" , [2 ])
156+ @pytest .mark .parametrize ("potFunc" , [potentialFunc ])
157+ def test_completely_outside (nx , ny , potFunc ):
158+
159+ print ('target line is outside the domain, expect warnings!' )
147160 # create the grid and the edge data
148161 gr = Grid ()
149162
150- nx , ny = 3 , 2
151163 points = numpy .zeros ((nx * ny , 4 , 3 ), numpy .float64 )
152164 data = numpy .zeros ((nx * ny , 4 ))
153165 dx = 1.0 / float (nx )
@@ -179,10 +191,10 @@ def test_completely_outside():
179191 # | |
180192 # +-->--+
181193 # 0
182- data [k , 0 ] = potentialFunc (points [k , 1 , :]) - potentialFunc (points [k , 0 , :])
183- data [k , 1 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 1 , :])
184- data [k , 2 ] = potentialFunc (points [k , 2 , :]) - potentialFunc (points [k , 3 , :])
185- data [k , 3 ] = potentialFunc (points [k , 3 , :]) - potentialFunc (points [k , 0 , :])
194+ data [k , 0 ] = potFunc (points [k , 1 , :]) - potFunc (points [k , 0 , :])
195+ data [k , 1 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 1 , :])
196+ data [k , 2 ] = potFunc (points [k , 2 , :]) - potFunc (points [k , 3 , :])
197+ data [k , 3 ] = potFunc (points [k , 3 , :]) - potFunc (points [k , 0 , :])
186198
187199 # increment the cell counter
188200 k += 1
@@ -208,8 +220,16 @@ def test_completely_outside():
208220
209221if __name__ == '__main__' :
210222
211- test_simple ()
212- test_partially_outside ()
213- test_completely_outside ()
223+ # polyline through which the line integral will be computed
224+ xyz = numpy .array ([(1. , 0. , 0. ),
225+ (0. , 1. , 0. )])
226+ test_simple (3 , 2 , singularPotentialFunc , xyz )
214227
228+ xyz = numpy .array ([(0. , 0. , 0. ),
229+ (1. , 0. , 0. ),
230+ (1. , 1. , 0. ),
231+ (0. , 1. , 0. )])
232+ test_simple (3 , 2 , potentialFunc , xyz )
215233
234+ test_partially_outside (2 , 3 , potentialFunc )
235+ test_completely_outside (2 , 3 , potentialFunc )
0 commit comments