created a test for tesing the cubic interpolation
This commit is contained in:
parent
1fc988428d
commit
700ccc8c25
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import pickle
|
import pickle
|
||||||
|
import pdb
|
||||||
|
|
||||||
from grid.DD import rect_grid, random_grid
|
from grid.DD import rect_grid, random_grid
|
||||||
from baker import run_baker
|
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)])
|
qlin = sum([q_i * phi_i for q_i, phi_i in zip(R.q, phis)])
|
||||||
return phis, qlin
|
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
|
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)
|
(dist, indicies) = self.tree.query(X, 2)
|
||||||
|
|
||||||
simplex = None
|
simplex = None
|
||||||
for i in self.facets_for_point[indicies[0]]:
|
for facet in self.facets_for_point[indicies[0]]:
|
||||||
if i.contains(X, self):
|
if facet.contains(X, self):
|
||||||
simplex = i
|
simplex = facet
|
||||||
break
|
break
|
||||||
|
|
||||||
if not simplex:
|
if not simplex:
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
from baker import get_phis
|
from baker import get_phis
|
||||||
|
|
||||||
TOL = 1e-3
|
TOL = 1e-8
|
||||||
|
|
||||||
def contains(X, R):
|
def contains(X, R):
|
||||||
|
"""
|
||||||
|
tests if X (point) is in R (a simplex,
|
||||||
|
represented by a list of n-degree coordinates)
|
||||||
|
"""
|
||||||
phis = get_phis(X, R)
|
phis = get_phis(X, R)
|
||||||
r = True
|
r = True
|
||||||
if [i for i in phis if i < 0.0 - TOL]:
|
if [i for i in phis if i < 0.0 - TOL]:
|
||||||
@ -28,8 +32,18 @@ class face(object):
|
|||||||
"""
|
"""
|
||||||
self.neighbors.append(n)
|
self.neighbors.append(n)
|
||||||
|
|
||||||
def contains(self, X, grid):
|
def contains(self, X, G):
|
||||||
return contains(X, grid.points)
|
"""
|
||||||
|
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):
|
def __str__(self):
|
||||||
neighbors = [i.name for i in self.neighbors]
|
neighbors = [i.name for i in self.neighbors]
|
||||||
|
@ -37,12 +37,12 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
r = [[-1, -1], [0, 2], [1, -1]]
|
r = [[-1, -1], [0, 2], [1, -1]]
|
||||||
|
|
||||||
result = baker.get_phis(X, r)
|
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):
|
for a,b in zip(result, right_answer):
|
||||||
self.assertEqual(a,b)
|
self.assertAlmostEqual(a,b)
|
||||||
|
|
||||||
def testGetPhis2(self):
|
def testGetPhis2(self):
|
||||||
|
|
||||||
@ -78,13 +78,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||||
|
|
||||||
answer = baker.run_baker(self.X, R, S)
|
answer = baker.run_baker(self.X, R, S)
|
||||||
a = round(answer['a'], self.accuracy)
|
a = answer['a']
|
||||||
b = round(answer['b'], self.accuracy)
|
b = answer['b']
|
||||||
c = round(answer['c'], self.accuracy)
|
c = answer['c']
|
||||||
|
|
||||||
self.assertEqual(a, c)
|
self.assertAlmostEqual(a, c)
|
||||||
self.assertEqual(c, round(0.00 , self.accuracy))
|
self.assertAlmostEqual(c, 0.0)
|
||||||
self.assertEqual(b, round(1/3.0, self.accuracy))
|
self.assertAlmostEqual(b, 1/3.0)
|
||||||
|
|
||||||
def testRunBaker_2(self):
|
def testRunBaker_2(self):
|
||||||
size_of_simplex = 3
|
size_of_simplex = 3
|
||||||
@ -98,12 +98,12 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
|
|
||||||
answer = baker.run_baker(self.X, R, S)
|
answer = baker.run_baker(self.X, R, S)
|
||||||
|
|
||||||
a = round(answer['a'], 5)
|
a = answer['a']
|
||||||
b = round(answer['b'], 5)
|
b = answer['b']
|
||||||
c = round(answer['c'], 5)
|
c = answer['c']
|
||||||
|
|
||||||
self.assertEqual(a, c)
|
self.assertAlmostEqual(a, c)
|
||||||
self.assertEqual(c, round(float(2/3.0), 5))
|
self.assertAlmostEqual(c, 2/3.0)
|
||||||
|
|
||||||
def testRunBaker_3(self):
|
def testRunBaker_3(self):
|
||||||
size_of_simplex = 3
|
size_of_simplex = 3
|
||||||
@ -116,13 +116,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||||
|
|
||||||
answer = baker.run_baker(self.X, R, S)
|
answer = baker.run_baker(self.X, R, S)
|
||||||
a = round(answer['a'], 5)
|
a = answer['a']
|
||||||
b = round(answer['b'], 5)
|
b = answer['b']
|
||||||
c = round(answer['c'], 5)
|
c = answer['c']
|
||||||
|
|
||||||
self.assertEqual(a, round(float(13/14.0), 5))
|
self.assertAlmostEqual(a, 13/14.0)
|
||||||
self.assertEqual(b, round(float(2 / 7.0), 5))
|
self.assertAlmostEqual(b, 2 / 7.0)
|
||||||
self.assertEqual(c, round(float(15/14.0), 5))
|
self.assertAlmostEqual(c, 15/14.0)
|
||||||
|
|
||||||
def testRunBaker_4(self):
|
def testRunBaker_4(self):
|
||||||
size_of_simplex = 3
|
size_of_simplex = 3
|
||||||
@ -135,13 +135,13 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
self.q[size_of_simplex:size_of_simplex + extra_points])
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
||||||
|
|
||||||
answer = baker.run_baker(self.X, R, S)
|
answer = baker.run_baker(self.X, R, S)
|
||||||
a = round(answer['a'], 5)
|
a = answer['a']
|
||||||
b = round(answer['b'], 5)
|
b = answer['b']
|
||||||
c = round(answer['c'], 5)
|
c = answer['c']
|
||||||
|
|
||||||
self.assertEqual(a, round(float(48/53.0), 5))
|
self.assertAlmostEqual(a, 48/53.0)
|
||||||
self.assertEqual(b, round(float(15/53.0), 5))
|
self.assertAlmostEqual(b, 15/53.0)
|
||||||
self.assertEqual(c, round(float(54/53.0), 5))
|
self.assertAlmostEqual(c, 54/53.0)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
||||||
|
@ -20,17 +20,18 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
def testGetPhis3D(self):
|
def testGetPhis3D(self):
|
||||||
result = [round(i, 5) for i in baker.get_phis_3D(self.X, self.r)]
|
result = baker.get_phis_3D(self.X, self.r)
|
||||||
right_answer = [round(i, 5) for i in [0.25, 0.25, 0.25, 0.25]]
|
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):
|
def testQlinear3D(self):
|
||||||
phi, result = baker.qlinear_3D(self.X, grid.grid(self.r, self.q))
|
phi, result = baker.qlinear_3D(self.X, grid.grid(self.r, self.q))
|
||||||
result = round(result, 5)
|
result = result
|
||||||
right_answer = round(1.0, 5)
|
right_answer = 1.0
|
||||||
self.assertEqual(result, right_answer)
|
self.assertAlmostEqual(result, right_answer)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -2,12 +2,17 @@
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
from baker import run_baker
|
from baker import run_baker
|
||||||
from baker.tools import exact_func
|
|
||||||
|
|
||||||
from grid.DD import grid
|
from grid.DD import grid
|
||||||
from grid.simplex import contains
|
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):
|
class TestSequenceFunctions(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -23,31 +28,51 @@ class TestSequenceFunctions(unittest.TestCase):
|
|||||||
]
|
]
|
||||||
self.q = [exact_func(p) for p in self.points]
|
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.25, 0.4001]
|
||||||
|
self.X = [0.55, 0.45]
|
||||||
|
|
||||||
self.g = grid(self.points, self.q)
|
self.g = grid(self.points, self.q)
|
||||||
self.g.construct_connectivity()
|
self.g.construct_connectivity()
|
||||||
self.R = self.g.create_mesh(range(3))
|
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.exact = exact_func(self.X)
|
||||||
|
|
||||||
self.answer = run_baker(self.X, self.R, self.S)
|
|
||||||
|
|
||||||
self.accuracy = 8
|
self.accuracy = 8
|
||||||
|
|
||||||
def test_R_contains_X(self):
|
def test_R_contains_X(self):
|
||||||
self.assertTrue(contains(self.X, self.R.points))
|
self.assertTrue(contains(self.X, self.R.points))
|
||||||
|
|
||||||
def test_RunBaker(self):
|
def test_RunBaker_1_extra_point(self, extra=1):
|
||||||
print
|
S = self.g.create_mesh(range(3, 3 + extra))
|
||||||
print "X\n", self.X
|
answer = run_baker(self.X, self.R, S)
|
||||||
print "R\n", self.R
|
lin_err = abs(self.exact - answer['qlin'])
|
||||||
print "S\n", self.S
|
final_err = abs(self.exact - answer['final'])
|
||||||
print "exact\n",self.exact
|
self.assertTrue(lin_err >= final_err)
|
||||||
print "qlin\n",self.answer['qlin']
|
def test_RunBaker_2_extra_point(self, extra=2):
|
||||||
self.assertTrue(self.answer)
|
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__':
|
if __name__ == '__main__':
|
||||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
||||||
|
Loading…
Reference in New Issue
Block a user