working on getting baker to work in 3D. the code runs, but the numbers are odd. I suspect the rectangular grid, and am going to try a random cloud of points.

This commit is contained in:
Stephen Mardson McQuay 2010-03-18 23:18:59 -06:00
parent a2d7b3f063
commit b33159f8a9
8 changed files with 151 additions and 154 deletions

1
.setup
View File

@ -1,2 +1 @@
export PATH=$PWD/bin:$PWD/test:$PATH export PATH=$PWD/bin:$PWD/test:$PATH
export PYTHONPATH=$PWD/lib

View File

@ -6,18 +6,18 @@ from optparse import OptionParser
import numpy as np import numpy as np
from baker.tools import rms, exact_func from baker.tools import rms, exact_func
import grid from grid.DD import simple_random_grid, simple_rect_grid
def get_mesh(source, destination, use_structured_grid = False): def get_mesh(source, destination, use_structured_grid = False):
mesh_source = None mesh_source = None
mesh_dest = None mesh_dest = None
if use_structured_grid: if use_structured_grid:
mesh_source = grid.simple_rect_grid(source, source) mesh_source = simple_rect_grid(source, source)
mesh_dest = grid.simple_rect_grid(destination, destination) mesh_dest = simple_rect_grid(destination, destination)
else: else:
mesh_source = grid.simple_random_grid(source) mesh_source = simple_random_grid(source)
mesh_dest = grid.simple_random_grid(destination) mesh_dest = simple_random_grid(destination)
if not (mesh_dest and mesh_source): if not (mesh_dest and mesh_source):
raise smberror('problem creating mesh objects') raise smberror('problem creating mesh objects')
@ -39,35 +39,33 @@ if __name__ == '__main__':
dest="extra", dest="extra",
type='int', type='int',
default = 3, default = 3,
help = "how many extra points") help = "how many extra points (%default)")
parser.add_option("-s", parser.add_option("-s",
"--source-total", "--source-total",
dest="source_total", dest="source_total",
type='int', type='int',
default = 100, default = 100,
help = "total number of source points for random,\ help = "total number of source points for random, resolution for structured (%default)")
resolution for structured")
parser.add_option("-d", parser.add_option("-d",
"--destination-total", "--destination-total",
dest="destination_total", dest="destination_total",
type='int', type='int',
default = 100, default = 100,
help = "total number of destination points,\ help = "total number of destination points, resolution for structured (%default)")
resolution for structured")
parser.add_option("-r", parser.add_option("-r",
"--structured", "--structured",
action = 'store_true', action = 'store_true',
default = False, default = False,
help = "use a structured grid instead of random point cloud") help = "use a structured grid instead of random point cloud (%default)")
parser.add_option("-v", parser.add_option("-v",
"--verbose", "--verbose",
action = 'store_true', action = 'store_true',
default = False, default = False,
help = "verbosity") help = "verbosity (%default)")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()

View File

@ -1,9 +1,9 @@
#!/usr/bin/python #!/usr/bin/python2.6
import sys import sys
import pickle import pickle
from grid import simple_rect_grid, simple_random_grid from grid.DD import simple_rect_grid, simple_random_grid
from baker import run_baker from baker import run_baker
from baker.tools import smberror from baker.tools import smberror

View File

@ -41,9 +41,14 @@ def get_phis_3D(X, r):
X -- the destination point (3D) X -- the destination point (3D)
X = [0,0,0] X = [0,0,0]
r -- the four points that make up the tetrahedron (3D) r -- the four points that make up the tetrahedron (3D)
r = [[-1, -1], [0, 2], [1, -1]] r = [
[0.0, 0.0, 1.0],
[0.94280904333606508, 0.0, -0.3333333283722672],
[-0.47140452166803232, 0.81649658244673617, -0.3333333283722672],
[-0.47140452166803298, -0.81649658244673584, -0.3333333283722672],
]
this will return [0.333, 0.333, 0.333] this will return [0.25, 0.25, 0.25, 0.25]
""" """
# baker: eq 7 # baker: eq 7
@ -82,7 +87,7 @@ def qlinear(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 qlinear_3D(X, R, q): def qlinear_3D(X, R):
""" """
this calculates the linear portion of q from X to r this calculates the linear portion of q from X to r
@ -91,8 +96,8 @@ def qlinear_3D(X, R, q):
q = CFD quantities of interest at the simplex points(R) q = CFD quantities of interest at the simplex points(R)
""" """
phis = get_phis_3D(X, R) phis = get_phis_3D(X, R.points)
qlin = sum([q_i * phi_i for q_i, phi_i in zip(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):
@ -103,9 +108,7 @@ def run_baker(X, R, S):
X = [0,0] X = [0,0]
R = Simplex R = Simplex
S = extra points S = extra points
""" """
# calculate values only for the triangle # calculate values only for the triangle
@ -129,7 +132,13 @@ def run_baker(X, R, S):
cur_phi, cur_qlin = qlinear(s, R) cur_phi, cur_qlin = qlinear(s, R)
(phi1, phi2, phi3) = cur_phi (phi1, phi2, phi3) = cur_phi
B.append([phi1 * phi2, phi2 * phi3, phi3 * phi1]) B.append(
[
phi1 * phi2,
phi2 * phi3,
phi3 * phi1,
]
)
w.append(q - cur_qlin) w.append(q - cur_qlin)
B = np.array(B) B = np.array(B)
@ -161,3 +170,87 @@ def run_baker(X, R, S):
} }
return answer return answer
def run_baker_3D(X, R, S):
"""
This is the main function to call to get an interpolation to X from the input meshes
X -- the destination point (3D)
X = [0,0,0]
R = Simplex (4 points, contains X)
S = extra points (surrounding, in some manner, R and X, but not in R)
"""
# calculate values only for the triangle
phi, qlin = qlinear_3D(X, R)
if len(S.points) == 0:
answer = {
'a': None,
'b': None,
'c': None,
'd': None,
'e': None,
'f': None,
'qlin': qlin,
'error': None,
'final': None,
}
return answer
B = [] # baker eq 9
w = [] # baker eq 11
for (s, q) in zip(S.points, S.q):
cur_phi, cur_qlin = qlinear_3D(s, R)
(phi1, phi2, phi3, phi4) = cur_phi
B.append(
[
phi1 * phi2,
phi1 * phi3,
phi1 * phi4,
phi2 * phi3,
phi2 * phi4,
phi3 * phi4,
]
)
w.append(q - cur_qlin)
B = np.array(B)
w = np.array(w)
A = np.dot(B.T, B)
b = np.dot(B.T, w)
# baker solve eq 10
try:
(a, b, c, d, e, f) = np.linalg.solve(A,b)
except:
print >> sys.stderr, "warning: run_baker: linear calculation went bad, resorting to np.linalg.pinv"
(a, b, c, d, e, f) = np.dot(np.linalg.pinv(A), b)
error_term = a * phi[0] * phi[1]\
+ b * phi[0] * phi[2]\
+ c * phi[0] * phi[3]\
+ d * phi[1] * phi[2]\
+ e * phi[1] * phi[3]\
+ f * phi[2] * phi[3]
q_final = qlin + error_term
answer = {
'a': a,
'b': b,
'c': c,
'd': d,
'e': e,
'f': f,
'qlin': qlin,
'error': error_term,
'final': q_final,
}
return answer

View File

@ -24,3 +24,12 @@ def exact_func(x, y):
the exact function used from baker's article (for testing) the exact function used from baker's article (for testing)
""" """
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2) return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
def exact_func_3D(X):
"""
the exact function (3D) used from baker's article (for testing)
"""
x = X[0]
y = X[1]
z = X[2]
return np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) * np.sin(z * np.pi / 2.0)), 2)

View File

@ -7,49 +7,11 @@ from collections import defaultdict
import numpy as np import numpy as np
import scipy.spatial import scipy.spatial
from baker import run_baker, get_phis from baker import run_baker
from baker.tools import exact_func, smberror from baker.tools import exact_func, smberror
from simplex import face
from smcqdelaunay import * from smcqdelaunay import *
class face(object):
def __init__(self, name):
self.name = name
self.verts = []
self.neighbors = []
def add_vert(self, v):
"""
v should be an index into grid.points
"""
self.verts.append(v)
def add_neighbor(self, n):
"""
reference to another face object
"""
self.neighbors.append(n)
def contains(self, X, grid):
R = [grid.points[i] for i in self.verts]
phis = get_phis(X, R)
r = True
if [i for i in phis if i < 0.0]:
r = False
return r
def __str__(self):
neighbors = [i.name for i in self.neighbors]
return '%s: verts: %s neighbors: [%s]' %\
(
self.name,
self.verts,
", ".join(neighbors)
)
class grid(object): class grid(object):
@ -97,7 +59,7 @@ class grid(object):
R is a grid object that is the (a) containing simplex around point X R is a grid object that is the (a) containing simplex around point X
S is S_j from baker's paper : some points from all point that are not the simplex S is S_j from baker's paper : some points from all point that are not the simplex
""" """
(dist, indicies) = self.tree.query(X, 3 + extra_points) (dist, indicies) = self.tree.query(X, simplex_size + extra_points)
# get the containing simplex # get the containing simplex
@ -152,7 +114,7 @@ class grid(object):
try: try:
(R, S) = self.get_simplex_and_nearest_points(X) (R, S) = self.get_simplex_and_nearest_points(X)
answer = run_baker(X, R, S) answer = run_baker(X, R, S)
except smberror as e: except smberror, e:
print "caught error: %s, trying with connectivity-based mesh" % e print "caught error: %s, trying with connectivity-based mesh" % e
(R, S) = self.get_points_conn(X) (R, S) = self.get_points_conn(X)
answer = run_baker(X, R, S) answer = run_baker(X, R, S)
@ -199,27 +161,6 @@ class grid(object):
# self.facets_for_point[int(point[1:])] = [i for i in neighboring_facets.split() if i in self.faces] # self.facets_for_point[int(point[1:])] = [i for i in neighboring_facets.split() if i in self.faces]
def for_qhull_generator(self):
"""
this returns a generator that should be fed into qdelaunay
"""
yield '2';
yield '%d' % len(self.points)
for p in self.points:
yield "%f %f" % (p[0], p[1])
def for_qhull(self):
"""
this returns a single string that should be fed into qdelaunay
"""
r = '2\n'
r += '%d\n' % len(self.points)
for p in self.points:
r += "%f %f\n" % (p[0], p[1])
return r
def __str__(self): def __str__(self):
r = '' r = ''
assert( len(self.points) == len(self.q) ) assert( len(self.points) == len(self.q) )
@ -233,49 +174,7 @@ class grid(object):
r += "%s\n" % v r += "%s\n" % v
return r return r
class simple_rect_grid(grid):
def __init__(self, xres = 5, yres = 5):
xmin = -1.0
xmax = 1.0
xspan = xmax - xmin
xdel = xspan / float(xres - 1)
ymin = -1.0
ymay = 1.0
yspan = ymay - ymin
ydel = yspan / float(yres - 1)
points = []
q = []
for x in xrange(xres):
cur_x = xmin + (x * xdel)
for y in xrange(yres):
cur_y = ymin + (y * ydel)
points.append([cur_x, cur_y])
q.append(exact_func(cur_x, cur_y))
grid.__init__(self, points, q)
self.construct_connectivity()
class simple_random_grid(simple_rect_grid):
def __init__(self, num_points = 10):
points = []
q = []
r = np.random
for i in xrange(num_points):
cur_x = r.rand()
cur_y = r.rand()
points.append([cur_x, cur_y])
q.append(exact_func(cur_x, cur_y))
grid.__init__(self, points, q)
self.points = np.array(self.points)
self.q = np.array(self.q)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -4,7 +4,7 @@ use_setuptools()
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
name = 'bakinterp', name = 'interpolosion',
version = '0.01', version = '0.01',
package_dir = {'':'lib'}, package_dir = {'':'lib'},
packages = find_packages('lib'), packages = find_packages('lib'),

View File

@ -10,7 +10,6 @@ import scipy.spatial
class TestSequenceFunctions(unittest.TestCase): class TestSequenceFunctions(unittest.TestCase):
def setUp(self): def setUp(self):
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]] 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"
self.all_points = [ self.all_points = [
[ 0, 0], # 0 [ 0, 0], # 0
[ 1, 0], # 1 [ 1, 0], # 1
@ -37,9 +36,9 @@ 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 = [self.approx_fmt % i for i in result] result = [round(i, 5) for i in result]
right_answer = [self.approx_fmt % i for i in [1/3.0, 1/3.0, 1/3.0]] right_answer = [round(i, 5) for i in [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.assertEqual(a,b)
@ -78,13 +77,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 = self.approx_fmt % answer['a'] a = round(answer['a'], 5)
b = self.approx_fmt % answer['b'] b = round(answer['b'], 5)
c = self.approx_fmt % answer['c'] c = round(answer['c'], 5)
self.assertEqual(a, c) self.assertEqual(a, c)
self.assertEqual(c, self.approx_fmt % 0.00) self.assertEqual(c, round(0.00 , 5))
self.assertEqual(b, self.approx_fmt % (1/3.0)) self.assertEqual(b, round(1/3.0, 5))
def testRunBaker_2(self): def testRunBaker_2(self):
size_of_simplex = 3 size_of_simplex = 3
@ -98,12 +97,12 @@ class TestSequenceFunctions(unittest.TestCase):
answer = baker.run_baker(self.X, R, S) answer = baker.run_baker(self.X, R, S)
a = self.approx_fmt % answer['a'] a = round(answer['a'], 5)
b = self.approx_fmt % answer['b'] b = round(answer['b'], 5)
c = self.approx_fmt % answer['c'] c = round(answer['c'], 5)
self.assertEqual(a, c) self.assertEqual(a, c)
self.assertEqual(c, self.approx_fmt % float(2/3.0)) self.assertEqual(c, round(float(2/3.0), 5))
def testRunBaker_3(self): def testRunBaker_3(self):
size_of_simplex = 3 size_of_simplex = 3
@ -116,13 +115,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 = self.approx_fmt % answer['a'] a = round(answer['a'], 5)
b = self.approx_fmt % answer['b'] b = round(answer['b'], 5)
c = self.approx_fmt % answer['c'] c = round(answer['c'], 5)
self.assertEqual(a, self.approx_fmt % float(13/14.0)) self.assertEqual(a, round(float(13/14.0), 5))
self.assertEqual(b, self.approx_fmt % float(2 / 7.0)) self.assertEqual(b, round(float(2 / 7.0), 5))
self.assertEqual(c, self.approx_fmt % float(15/14.0)) self.assertEqual(c, round(float(15/14.0), 5))
def testRunBaker_4(self): def testRunBaker_4(self):
size_of_simplex = 3 size_of_simplex = 3
@ -135,13 +134,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 = self.approx_fmt % answer['a'] a = round(answer['a'], 5)
b = self.approx_fmt % answer['b'] b = round(answer['b'], 5)
c = self.approx_fmt % answer['c'] c = round(answer['c'], 5)
self.assertEqual(a, self.approx_fmt % float(48/53.0)) self.assertEqual(a, round(float(48/53.0), 5))
self.assertEqual(b, self.approx_fmt % float(15/53.0)) self.assertEqual(b, round(float(15/53.0), 5))
self.assertEqual(c, self.approx_fmt % float(54/53.0)) self.assertEqual(c, round(float(54/53.0), 5))
if __name__ == '__main__': if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)