/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/sph/tests/test_equations.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | # Standard library imports.
import numpy
from textwrap import dedent
import unittest
# Local imports.
from pysph.base.cython_generator import KnownType
from pysph.sph.equation import (BasicCodeBlock, Context, Equation,
Group, sort_precomputed)
class TestContext(unittest.TestCase):
def test_basic_usage(self):
c = Context(a=1, b=2)
self.assertEqual(c.a, 1)
self.assertEqual(c.b, 2)
self.assertEqual(c['a'], 1)
self.assertEqual(c['b'], 2)
c.c = 3
self.assertEqual(c.c, 3)
self.assertEqual(c['c'], 3)
def test_context_behaves_like_dict(self):
c = Context(a=1)
c.b = 2
keys = list(c.keys())
keys.sort()
self.assertEqual(keys, ['a', 'b'])
values = list(c.values())
values.sort()
self.assertEqual(values, [1, 2])
self.assertTrue('a' in c)
self.assertTrue('b' in c)
self.assertTrue('c' not in c)
class TestBase(unittest.TestCase):
def assert_seq_equal(self, got, expect):
g = list(got)
g.sort()
e = list(expect)
e.sort()
self.assertEqual(g, e, 'got %s, expected %s'%(g, e))
class TestBasicCodeBlock(TestBase):
def test_basic_code_block(self):
code = '''
x = 1
d_x[d_idx] += s_x[s_idx] + x
'''
cb = BasicCodeBlock(code=code)
expect = ['d_idx', 'd_x', 's_idx', 's_x', 'x']
self.assert_seq_equal(cb.symbols, expect)
self.assert_seq_equal(cb.src_arrays, ['s_x'])
self.assert_seq_equal(cb.dest_arrays, ['d_x'])
ctx = cb.context
self.assertEqual(ctx.s_idx, 0)
self.assertEqual(ctx.d_idx, 0)
x = numpy.zeros(2, dtype=float)
self.assertTrue(numpy.alltrue(ctx.d_x == x))
self.assertTrue(numpy.alltrue(ctx.s_x == x))
def test_that_code_block_is_callable(self):
code = '''
x = 1
d_x[d_idx] += s_x[s_idx] + x
'''
cb = BasicCodeBlock(code=code)
# The code block should be callable.
res = cb()
self.assertTrue(sum(res.d_x) == 1)
# Should take arguments to update the context.
x = numpy.ones(2, dtype=float)*10
res = cb(s_x=x)
self.assertTrue(sum(res.d_x) == 11)
class TestEquations(TestBase):
def test_simple_equation(self):
eq = Equation('fluid', None)
self.assertEqual(eq.name, 'Equation')
self.assertEqual(eq.no_source, True)
self.assertEqual(eq.dest, 'fluid')
self.assertEqual(eq.sources, None)
self.assertFalse(hasattr(eq, 'loop'))
self.assertFalse(hasattr(eq, 'post_loop'))
self.assertFalse(hasattr(eq, 'initialize'))
eq = Equation('fluid', sources=['fluid'])
self.assertEqual(eq.name, 'Equation')
self.assertEqual(eq.no_source, False)
self.assertEqual(eq.dest, 'fluid')
self.assertEqual(eq.sources, ['fluid'])
class Test(Equation):
pass
eq = Test('fluid', [])
self.assertEqual(eq.name, 'Test')
self.assertEqual(eq.no_source, True)
def test_continuity_equation(self):
from pysph.sph.basic_equations import ContinuityEquation
e = ContinuityEquation(dest='fluid', sources=['fluid'])
# Call the loop code.
d_arho = [0.0, 0.0, 0.0]
s_m = [0.0, 0.0]
r = e.loop(d_idx=0, d_arho=d_arho, s_idx=0, s_m=s_m,
DWIJ=[0,0,0], VIJ=[0,0,0])
self.assertEqual(d_arho[0], 0.0)
self.assertEqual(d_arho[1], 0.0)
# Now call with specific arguments.
s_m = [1, 1]
r = e.loop(d_idx=0, d_arho=d_arho, s_idx=0, s_m=s_m,
DWIJ=[1,1,1], VIJ=[1,1,1])
self.assertEqual(d_arho[0], 3.0)
self.assertEqual(d_arho[1], 0.0)
def test_order_of_precomputed(self):
try:
pre_comp = Group.pre_comp
pre_comp.AIJ = BasicCodeBlock(code=dedent("""
AIJ[0] = XIJ[0]/RIJ
AIJ[1] = XIJ[1]/RIJ
AIJ[2] = XIJ[2]/RIJ
"""),
AIJ=[1.0, 0.0, 0.0])
input = dict((x, pre_comp[x]) for x in ['RIJ', 'R2IJ',
'XIJ', 'HIJ', 'AIJ'])
pre = sort_precomputed(input)
self.assertEqual(
list(pre.keys()), ['HIJ', 'XIJ', 'R2IJ', 'RIJ', 'AIJ']
)
finally:
from pysph.sph.equation import precomputed_symbols
Group.pre_comp = precomputed_symbols()
class TestEq1(Equation):
def loop(self, WIJ=0.0):
x = WIJ
def post_loop(self, d_idx, d_h):
x = d_h[d_idx]
class TestEq2(Equation):
def loop(self, d_idx, s_idx):
x = s_idx + d_idx
class TestGroup(TestBase):
def setUp(self):
from pysph.sph.basic_equations import SummationDensity
from pysph.sph.wc.basic import TaitEOS
self.group = Group(
[SummationDensity('f', ['f']),
TaitEOS('f', None, rho0=1.0, c0=1.0, gamma=1.4, p0=1.0)]
)
def test_precomputed(self):
g = self.group
self.assertEqual(len(g.precomputed), 5)
self.assertEqual(list(g.precomputed.keys()),
['HIJ', 'XIJ', 'R2IJ', 'RIJ', 'WIJ'])
def test_array_names(self):
g = self.group
src, dest = g.get_array_names()
s_ex = ['s_m', 's_x', 's_y', 's_z', 's_h']
d_ex = ['d_rho', 'd_p', 'd_h', 'd_cs', 'd_x', 'd_y', 'd_z']
self.assert_seq_equal(src, s_ex)
self.assert_seq_equal(dest, d_ex)
def test_variable_names(self):
g = self.group
names = g.get_variable_names()
expect = ['WIJ', 'RIJ', 'R2IJ', 'XIJ', 'HIJ']
self.assert_seq_equal(names, expect)
def test_array_declarations(self):
g = self.group
expect = 'cdef double* d_x'
self.assertEqual(g.get_array_declarations(['d_x']), expect)
def test_array_declarations_with_known_types(self):
# Given
g = self.group
known_types = {'d_x': KnownType('float*')}
# When
result = g.get_array_declarations(['d_x'], known_types)
# Then.
expect = 'cdef float* d_x'
self.assertEqual(result, expect)
def test_variable_declarations(self):
g = self.group
context = Context(x=1.0)
expect = 'cdef double x = 1.0'
self.assertEqual(g.get_variable_declarations(context), expect)
context = Context(x=1)
expect = 'cdef long x = 1'
self.assertEqual(g.get_variable_declarations(context), expect)
context = Context(x=[1., 2.])
expect = ('cdef DoubleArray _x = DoubleArray(aligned(2, 8)*self.n_threads)\n'
'cdef double* x = _x.data')
self.assertEqual(g.get_variable_declarations(context), expect)
context = Context(x=(0, 1., 2.))
expect = ('cdef DoubleArray _x = DoubleArray(aligned(3, 8)*self.n_threads)\n'
'cdef double* x = _x.data')
self.assertEqual(g.get_variable_declarations(context), expect)
def test_loop_code(self):
from pysph.base.kernels import CubicSpline
k = CubicSpline(dim=3)
e1 = TestEq1('f', ['f'])
e2 = TestEq2('f', ['f'])
g = Group([e1, e2])
# First get the equation wrappers so the equation names are setup.
w = g.get_equation_wrappers()
result = g.get_loop_code(k)
expect = dedent('''\
HIJ = 0.5*(d_h[d_idx] + s_h[s_idx])
XIJ[0] = d_x[d_idx] - s_x[s_idx]
XIJ[1] = d_y[d_idx] - s_y[s_idx]
XIJ[2] = d_z[d_idx] - s_z[s_idx]
R2IJ = XIJ[0]*XIJ[0] + XIJ[1]*XIJ[1] + XIJ[2]*XIJ[2]
RIJ = sqrt(R2IJ)
WIJ = self.kernel.kernel(XIJ, RIJ, HIJ)
self.test_eq10.loop(WIJ)
self.test_eq20.loop(d_idx, s_idx)
''')
msg = 'EXPECTED:\n%s\nGOT:\n%s'%(expect, result)
self.assertEqual(result, expect, msg)
def test_post_loop_code(self):
from pysph.base.kernels import CubicSpline
k = CubicSpline(dim=3)
e1 = TestEq1('f', ['f'])
e2 = TestEq2('f', ['f'])
g = Group([e1, e2])
# First get the equation wrappers so the equation names are setup.
w = g.get_equation_wrappers()
result = g.get_post_loop_code(k)
expect = dedent('''\
self.test_eq10.post_loop(d_idx, d_h)
''')
msg = 'EXPECTED:\n%s\nGOT:\n%s'%(expect, result)
self.assertEqual(result, expect, msg)
if __name__ == '__main__':
unittest.main()
|