created a test for tesing the cubic interpolation
This commit is contained in:
parent
1fc988428d
commit
700ccc8c25
@ -2,6 +2,7 @@
|
||||
|
||||
import sys
|
||||
import pickle
|
||||
import pdb
|
||||
|
||||
from grid.DD import rect_grid, random_grid
|
||||
from baker import run_baker
|
||||
|
@ -102,7 +102,7 @@ def qlinear_3D(X, R):
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(R.q, phis)])
|
||||
return phis, qlin
|
||||
|
||||
def run_baker(X, R, S):
|
||||
def run_baker(X, R, S, order=2):
|
||||
"""
|
||||
This is the main function to call to get an interpolation to X from the input meshes
|
||||
|
||||
|
@ -88,9 +88,9 @@ class grid(object):
|
||||
(dist, indicies) = self.tree.query(X, 2)
|
||||
|
||||
simplex = None
|
||||
for i in self.facets_for_point[indicies[0]]:
|
||||
if i.contains(X, self):
|
||||
simplex = i
|
||||
for facet in self.facets_for_point[indicies[0]]:
|
||||
if facet.contains(X, self):
|
||||
simplex = facet
|
||||
break
|
||||
|
||||
if not simplex:
|
||||
|
@ -1,13 +1,17 @@
|
||||
from baker import get_phis
|
||||
|
||||
TOL = 1e-3
|
||||
TOL = 1e-8
|
||||
|
||||
def contains(X, R):
|
||||
phis = get_phis(X, R)
|
||||
r = True
|
||||
if [i for i in phis if i < 0.0 - TOL]:
|
||||
r = False
|
||||
return r
|
||||
"""
|
||||
tests if X (point) is in R (a simplex,
|
||||
represented by a list of n-degree coordinates)
|
||||
"""
|
||||
phis = get_phis(X, R)
|
||||
r = True
|
||||
if [i for i in phis if i < 0.0 - TOL]:
|
||||
r = False
|
||||
return r
|
||||
|
||||
|
||||
class face(object):
|
||||
@ -28,8 +32,18 @@ class face(object):
|
||||
"""
|
||||
self.neighbors.append(n)
|
||||
|
||||
def contains(self, X, grid):
|
||||
return contains(X, grid.points)
|
||||
def contains(self, X, G):
|
||||
"""
|
||||
X = point of interest
|
||||
G = corrensponding grid object (G.points)
|
||||
because of the way i'm storing things,
|
||||
a face simply stores indicies, and so one
|
||||
must pass in a reference to the grid object
|
||||
containing real points.
|
||||
|
||||
this simply calls grid.simplex.contains
|
||||
"""
|
||||
return contains(X, [G.points[i] for i in self.verts])
|
||||
|
||||
def __str__(self):
|
||||
neighbors = [i.name for i in self.neighbors]
|
||||
|
@ -37,12 +37,12 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
r = [[-1, -1], [0, 2], [1, -1]]
|
||||
|
||||
result = baker.get_phis(X, r)
|
||||
result = [round(i, self.accuracy) for i in result]
|
||||
#result = [round(i, self.accuracy) for i in result]
|
||||
|
||||
right_answer = [round(i, self.accuracy) for i in [1/3.0, 1/3.0, 1/3.0]]
|
||||
right_answer = [1/3.0, 1/3.0, 1/3.0]
|
||||
|
||||
for a,b in zip(result, right_answer):
|
||||
self.assertEqual(a,b)
|
||||
self.assertAlmostEqual(a,b)
|
||||
|
||||
def testGetPhis2(self):
|
||||
|
||||
@ -78,13 +78,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||
|
||||
answer = baker.run_baker(self.X, R, S)
|
||||
a = round(answer['a'], self.accuracy)
|
||||
b = round(answer['b'], self.accuracy)
|
||||
c = round(answer['c'], self.accuracy)
|
||||
a = answer['a']
|
||||
b = answer['b']
|
||||
c = answer['c']
|
||||
|
||||
self.assertEqual(a, c)
|
||||
self.assertEqual(c, round(0.00 , self.accuracy))
|
||||
self.assertEqual(b, round(1/3.0, self.accuracy))
|
||||
self.assertAlmostEqual(a, c)
|
||||
self.assertAlmostEqual(c, 0.0)
|
||||
self.assertAlmostEqual(b, 1/3.0)
|
||||
|
||||
def testRunBaker_2(self):
|
||||
size_of_simplex = 3
|
||||
@ -98,12 +98,12 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
|
||||
answer = baker.run_baker(self.X, R, S)
|
||||
|
||||
a = round(answer['a'], 5)
|
||||
b = round(answer['b'], 5)
|
||||
c = round(answer['c'], 5)
|
||||
a = answer['a']
|
||||
b = answer['b']
|
||||
c = answer['c']
|
||||
|
||||
self.assertEqual(a, c)
|
||||
self.assertEqual(c, round(float(2/3.0), 5))
|
||||
self.assertAlmostEqual(a, c)
|
||||
self.assertAlmostEqual(c, 2/3.0)
|
||||
|
||||
def testRunBaker_3(self):
|
||||
size_of_simplex = 3
|
||||
@ -116,13 +116,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||
|
||||
answer = baker.run_baker(self.X, R, S)
|
||||
a = round(answer['a'], 5)
|
||||
b = round(answer['b'], 5)
|
||||
c = round(answer['c'], 5)
|
||||
a = answer['a']
|
||||
b = answer['b']
|
||||
c = answer['c']
|
||||
|
||||
self.assertEqual(a, round(float(13/14.0), 5))
|
||||
self.assertEqual(b, round(float(2 / 7.0), 5))
|
||||
self.assertEqual(c, round(float(15/14.0), 5))
|
||||
self.assertAlmostEqual(a, 13/14.0)
|
||||
self.assertAlmostEqual(b, 2 / 7.0)
|
||||
self.assertAlmostEqual(c, 15/14.0)
|
||||
|
||||
def testRunBaker_4(self):
|
||||
size_of_simplex = 3
|
||||
@ -135,13 +135,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||
|
||||
answer = baker.run_baker(self.X, R, S)
|
||||
a = round(answer['a'], 5)
|
||||
b = round(answer['b'], 5)
|
||||
c = round(answer['c'], 5)
|
||||
a = answer['a']
|
||||
b = answer['b']
|
||||
c = answer['c']
|
||||
|
||||
self.assertEqual(a, round(float(48/53.0), 5))
|
||||
self.assertEqual(b, round(float(15/53.0), 5))
|
||||
self.assertEqual(c, round(float(54/53.0), 5))
|
||||
self.assertAlmostEqual(a, 48/53.0)
|
||||
self.assertAlmostEqual(b, 15/53.0)
|
||||
self.assertAlmostEqual(c, 54/53.0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
||||
|
@ -20,17 +20,18 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
|
||||
|
||||
def testGetPhis3D(self):
|
||||
result = [round(i, 5) for i in baker.get_phis_3D(self.X, self.r)]
|
||||
right_answer = [round(i, 5) for i in [0.25, 0.25, 0.25, 0.25]]
|
||||
result = baker.get_phis_3D(self.X, self.r)
|
||||
right_answer = [0.25, 0.25, 0.25, 0.25]
|
||||
|
||||
self.assertEqual(result, right_answer)
|
||||
for a,b in zip(result, right_answer):
|
||||
self.assertAlmostEqual(a,b)
|
||||
|
||||
|
||||
def testQlinear3D(self):
|
||||
phi, result = baker.qlinear_3D(self.X, grid.grid(self.r, self.q))
|
||||
result = round(result, 5)
|
||||
right_answer = round(1.0, 5)
|
||||
self.assertEqual(result, right_answer)
|
||||
result = result
|
||||
right_answer = 1.0
|
||||
self.assertAlmostEqual(result, right_answer)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -2,12 +2,17 @@
|
||||
|
||||
import unittest
|
||||
|
||||
import math
|
||||
|
||||
from baker import run_baker
|
||||
from baker.tools import exact_func
|
||||
|
||||
from grid.DD import grid
|
||||
from grid.simplex import contains
|
||||
|
||||
def exact_func(X):
|
||||
x = X[0]
|
||||
y = X[0]
|
||||
return 1 - math.sin((x-0.5)**2 + (y-0.5)**2)
|
||||
|
||||
class TestSequenceFunctions(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@ -23,31 +28,51 @@ class TestSequenceFunctions(unittest.TestCase):
|
||||
]
|
||||
self.q = [exact_func(p) for p in self.points]
|
||||
|
||||
self.X = [0.55, 0.45]
|
||||
self.X = [0.25, 0.4001]
|
||||
self.X = [0.55, 0.45]
|
||||
|
||||
self.g = grid(self.points, self.q)
|
||||
self.g.construct_connectivity()
|
||||
self.R = self.g.create_mesh(range(3))
|
||||
self.S = self.g.create_mesh(range(3,len(self.points)))
|
||||
|
||||
self.exact = exact_func(self.X)
|
||||
|
||||
self.answer = run_baker(self.X, self.R, self.S)
|
||||
|
||||
self.accuracy = 8
|
||||
|
||||
def test_R_contains_X(self):
|
||||
self.assertTrue(contains(self.X, self.R.points))
|
||||
|
||||
def test_RunBaker(self):
|
||||
print
|
||||
print "X\n", self.X
|
||||
print "R\n", self.R
|
||||
print "S\n", self.S
|
||||
print "exact\n",self.exact
|
||||
print "qlin\n",self.answer['qlin']
|
||||
self.assertTrue(self.answer)
|
||||
def test_RunBaker_1_extra_point(self, extra=1):
|
||||
S = self.g.create_mesh(range(3, 3 + extra))
|
||||
answer = run_baker(self.X, self.R, S)
|
||||
lin_err = abs(self.exact - answer['qlin'])
|
||||
final_err = abs(self.exact - answer['final'])
|
||||
self.assertTrue(lin_err >= final_err)
|
||||
def test_RunBaker_2_extra_point(self, extra=2):
|
||||
S = self.g.create_mesh(range(3, 3 + extra))
|
||||
answer = run_baker(self.X, self.R, S)
|
||||
lin_err = abs(self.exact - answer['qlin'])
|
||||
final_err = abs(self.exact - answer['final'])
|
||||
self.assertTrue(lin_err >= final_err)
|
||||
def test_RunBaker_3_extra_point(self, extra=3):
|
||||
S = self.g.create_mesh(range(3, 3 + extra))
|
||||
answer = run_baker(self.X, self.R, S)
|
||||
lin_err = abs(self.exact - answer['qlin'])
|
||||
final_err = abs(self.exact - answer['final'])
|
||||
self.assertTrue(lin_err >= final_err)
|
||||
def test_RunBaker_4_extra_point(self, extra=4):
|
||||
S = self.g.create_mesh(range(3, 3 + extra))
|
||||
answer = run_baker(self.X, self.R, S)
|
||||
lin_err = abs(self.exact - answer['qlin'])
|
||||
final_err = abs(self.exact - answer['final'])
|
||||
self.assertTrue(lin_err >= final_err)
|
||||
def test_RunBaker_5_extra_point(self, extra=5):
|
||||
S = self.g.create_mesh(range(3, 3 + extra))
|
||||
answer = run_baker(self.X, self.R, S)
|
||||
lin_err = abs(self.exact - answer['qlin'])
|
||||
final_err = abs(self.exact - answer['final'])
|
||||
self.assertTrue(lin_err >= final_err)
|
||||
|
||||
if __name__ == '__main__':
|
||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
||||
|
Loading…
Reference in New Issue
Block a user