Merge branch 'pytest-curl-div-grad-2' into development

This commit is contained in:
Martin Diehl 2020-05-10 13:03:07 +02:00
commit 7b89d74833
1 changed files with 184 additions and 0 deletions

View File

@ -101,6 +101,7 @@ class TestGridFilters:
with pytest.raises(ValueError):
function(uneven)
@pytest.mark.parametrize('mode',[True,False])
@pytest.mark.parametrize('function',[grid_filters.node_coord0_gridSizeOrigin,
grid_filters.cell_coord0_gridSizeOrigin])
@ -120,3 +121,186 @@ class TestGridFilters:
grid = np.random.randint(8,32,(3))
F = np.broadcast_to(np.eye(3), tuple(grid)+(3,3))
assert all(grid_filters.regrid(size,F,grid) == np.arange(grid.prod()))
@pytest.mark.parametrize('differential_operator',[grid_filters.curl,
grid_filters.divergence,
grid_filters.gradient])
def test_differential_operator_constant(self,differential_operator):
size = np.random.random(3)+1.0
grid = np.random.randint(8,32,(3))
shapes = {
grid_filters.curl: [(3,),(3,3)],
grid_filters.divergence:[(3,),(3,3)],
grid_filters.gradient: [(1,),(3,)]
}
for shape in shapes[differential_operator]:
field = np.ones(tuple(grid)+shape)*np.random.random()*1.0e5
assert np.allclose(differential_operator(size,field),0.0)
grad_test_data = [
(['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0'],
['np.cos(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '0.0']),
(['0.0', 'np.cos(np.pi*2*nodes[...,1]/size[1])', '0.0' ],
['0.0', '0.0', '0.0',
'0.0', '-np.pi*2/size[1]*np.sin(np.pi*2*nodes[...,1]/size[1])', '0.0',
'0.0', '0.0', '0.0' ]),
(['1.0', '0.0', '2.0*np.cos(np.pi*2*nodes[...,2]/size[2])'],
['0.0', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '-2.0*np.pi*2/size[2]*np.sin(np.pi*2*nodes[...,2]/size[2])']),
(['np.cos(np.pi*2*nodes[...,2]/size[2])', '3.0', 'np.sin(np.pi*2*nodes[...,2]/size[2])'],
['0.0', '0.0', '-np.sin(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]',
'0.0', '0.0', '0.0',
'0.0', '0.0', ' np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]']),
(['np.sin(np.pi*2*nodes[...,0]/size[0])',
'np.sin(np.pi*2*nodes[...,1]/size[1])',
'np.sin(np.pi*2*nodes[...,2]/size[2])'],
['np.cos(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]', '0.0', '0.0',
'0.0', 'np.cos(np.pi*2*nodes[...,1]/size[1])*np.pi*2/size[1]', '0.0',
'0.0', '0.0', 'np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]']),
(['np.sin(np.pi*2*nodes[...,0]/size[0])'],
['np.cos(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]', '0.0', '0.0']),
(['8.0'],
['0.0', '0.0', '0.0' ])
]
@pytest.mark.parametrize('field_def,grad_def',grad_test_data)
def test_grad(self,field_def,grad_def):
size = np.random.random(3)+1.0
grid = np.random.randint(8,32,(3))
nodes = grid_filters.cell_coord0(grid,size)
my_locals = locals() # needed for list comprehension
field = np.stack([np.broadcast_to(eval(f,globals(),my_locals),grid) for f in field_def],axis=-1)
field = field.reshape(tuple(grid) + ((3,) if len(field_def)==3 else (1,)))
grad = np.stack([np.broadcast_to(eval(c,globals(),my_locals),grid) for c in grad_def], axis=-1)
grad = grad.reshape(tuple(grid) + ((3,3) if len(grad_def)==9 else (3,)))
assert np.allclose(grad,grid_filters.gradient(size,field))
curl_test_data = [
(['np.sin(np.pi*2*nodes[...,2]/size[2])', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '0.0'],
['0.0' , '0.0', '0.0',
'np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]', '0.0', '0.0',
'0.0', '0.0', '0.0']),
(['np.cos(np.pi*2*nodes[...,1]/size[1])', '0.0', '0.0',
'0.0', '0.0', '0.0',
'np.cos(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0'],
['0.0', '0.0', '0.0',
'0.0', '0.0', '0.0',
'np.sin(np.pi*2*nodes[...,1]/size[1])*np.pi*2/size[1]', '0.0', '0.0']),
(['np.sin(np.pi*2*nodes[...,0]/size[0])','np.cos(np.pi*2*nodes[...,1]/size[1])','np.sin(np.pi*2*nodes[...,2]/size[2])',
'np.sin(np.pi*2*nodes[...,0]/size[0])','np.cos(np.pi*2*nodes[...,1]/size[1])','np.sin(np.pi*2*nodes[...,2]/size[2])',
'np.sin(np.pi*2*nodes[...,0]/size[0])','np.cos(np.pi*2*nodes[...,1]/size[1])','np.sin(np.pi*2*nodes[...,2]/size[2])'],
['0.0', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '0.0']),
(['5.0', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '2*np.cos(np.pi*2*nodes[...,1]/size[1])'],
['0.0', '0.0', '-2*np.pi*2/size[1]*np.sin(np.pi*2*nodes[...,1]/size[1])',
'0.0', '0.0', '0.0',
'0.0', '0.0', '0.0']),
([ '4*np.sin(np.pi*2*nodes[...,2]/size[2])',
'8*np.sin(np.pi*2*nodes[...,0]/size[0])',
'16*np.sin(np.pi*2*nodes[...,1]/size[1])'],
['16*np.pi*2/size[1]*np.cos(np.pi*2*nodes[...,1]/size[1])',
'4*np.pi*2/size[2]*np.cos(np.pi*2*nodes[...,2]/size[2])',
'8*np.pi*2/size[0]*np.cos(np.pi*2*nodes[...,0]/size[0])']),
(['0.0',
'np.cos(np.pi*2*nodes[...,0]/size[0])+5*np.cos(np.pi*2*nodes[...,2]/size[2])',
'0.0'],
['5*np.sin(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]',
'0.0',
'-np.sin(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]'])
]
@pytest.mark.parametrize('field_def,curl_def',curl_test_data)
def test_curl(self,field_def,curl_def):
size = np.random.random(3)+1.0
grid = np.random.randint(8,32,(3))
nodes = grid_filters.cell_coord0(grid,size)
my_locals = locals() # needed for list comprehension
field = np.stack([np.broadcast_to(eval(f,globals(),my_locals),grid) for f in field_def],axis=-1)
field = field.reshape(tuple(grid) + ((3,3) if len(field_def)==9 else (3,)))
curl = np.stack([np.broadcast_to(eval(c,globals(),my_locals),grid) for c in curl_def], axis=-1)
curl = curl.reshape(tuple(grid) + ((3,3) if len(curl_def)==9 else (3,)))
assert np.allclose(curl,grid_filters.curl(size,field))
div_test_data =[
(['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0',
'0.0' , '0.0', '0.0',
'0.0' , '0.0', '0.0'],
['np.cos(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]','0.0', '0.0']),
(['0.0', '0.0', '0.0',
'0.0', 'np.cos(np.pi*2*nodes[...,1]/size[1])', '0.0',
'0.0', '0.0', '0.0'],
['0.0', '-np.sin(np.pi*2*nodes[...,1]/size[1])*np.pi*2/size[1]', '0.0']),
(['1.0', '0.0', '0.0',
'0.0', '0.0', '0.0',
'0.0', '0.0', '2*np.cos(np.pi*2*nodes[...,2]/size[2])' ],
['0.0', '0.0', '-2.0*np.pi*2/size[2]*np.sin(np.pi*2*nodes[...,2]/size[2])']
),
([ '23.0', '0.0', 'np.sin(np.pi*2*nodes[...,2]/size[2])',
'0.0', '100.0', 'np.sin(np.pi*2*nodes[...,2]/size[2])',
'0.0', '0.0', 'np.sin(np.pi*2*nodes[...,2]/size[2])'],
['np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]',\
'np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]', \
'np.cos(np.pi*2*nodes[...,2]/size[2])*np.pi*2/size[2]']),
(['400.0', '0.0', '0.0',
'np.sin(np.pi*2*nodes[...,0]/size[0])', 'np.sin(np.pi*2*nodes[...,1]/size[1])', 'np.sin(np.pi*2*nodes[...,2]/size[2])',
'0.0', '10.0', '6.0'],
['0.0','np.sum(np.cos(np.pi*2*nodes/size)*np.pi*2/size,axis=-1)', '0.0' ]),
(['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0'],
['np.cos(np.pi*2*nodes[...,0]/size[0])*np.pi*2/size[0]',]),
(['0.0', 'np.cos(np.pi*2*nodes[...,1]/size[1])', '0.0' ],
['-np.sin(np.pi*2*nodes[...,1]/size[1])*np.pi*2/size[1]'])
]
@pytest.mark.parametrize('field_def,div_def',div_test_data)
def test_div(self,field_def,div_def):
size = np.random.random(3)+1.0
grid = np.random.randint(8,32,(3))
nodes = grid_filters.cell_coord0(grid,size)
my_locals = locals() # needed for list comprehension
field = np.stack([np.broadcast_to(eval(f,globals(),my_locals),grid) for f in field_def],axis=-1)
field = field.reshape(tuple(grid) + ((3,3) if len(field_def)==9 else (3,)))
div = np.stack([np.broadcast_to(eval(c,globals(),my_locals),grid) for c in div_def], axis=-1)
if len(div_def)==3:
div = div.reshape(tuple(grid) + ((3,)))
else:
div=div.reshape(tuple(grid))
assert np.allclose(div,grid_filters.divergence(size,field))