smbinterp/test/baker.test.py

78 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python
import unittest
import baker
import grid
import numpy as np
import scipy.spatial
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
self.approx_fmt = "%0.6f"
def testGetPhis(self):
X = [0,0]
r = [[-1, -1], [0, 2], [1, -1]]
result = baker.get_phis(X, r)
result = [self.approx_fmt % i for i in result]
right_answer = [self.approx_fmt % i for i in [1/3.0, 1/3.0, 1/3.0]]
for a,b in zip(result, right_answer):
self.assertEqual(a,b)
def testGetPhis2(self):
X = [0.5,0.25]
r = [[0, 0], [1, 0], [1, 1]]
result = baker.get_phis(X, r)
right_answer = [0.5, 0.25, 0.25]
for a,b in zip(result, right_answer):
self.assertEqual(a,b)
def testQlinear(self):
X = [0.5, 0.25]
r = [[0, 0], [1, 0], [1, 1]]
q = [1, 0, 0]
result = baker.qlinear(X, r, q)
right_answer = 0.5
self.assertEqual(result, right_answer)
def testRunBaker(self):
X = [0.5, 0.25]
all_points = [
[ 0, 0], # 0
[ 1, 0], # 1
[ 1, 1], # 2
[ 0, 1], # 3
[ 1,-1], # 4
[ 0,-1], # 5
[-1, 1], # 6
[-1, 0], # 7
[-1,-1], # 8
]
q = [1, 0, 0, 0, 0, 0, 0, 0, 0]
mesh = grid.grid(all_points, q)
tree = scipy.spatial.KDTree(all_points)
(final, exact) = baker.run_baker(X, mesh, tree)
print final, exact
result = 3
right_answer = 3
self.assertEqual(result, right_answer)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)