init
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
||||
from grid import exact_func
|
||||
import numpy as np
|
||||
import sys
|
||||
|
||||
def get_phis(X, r):
|
||||
"""
|
||||
The get_phis function is used to get barycentric coordonites for a point on a triangle.
|
||||
|
||||
X -- the destination point (2D)
|
||||
X = [0,0]
|
||||
r -- the three points that make up the triangle (2D)
|
||||
r = [[-1, -1], [0, 2], [1, -1]]
|
||||
|
||||
this will return [0.333, 0.333, 0.333]
|
||||
"""
|
||||
|
||||
# baker: eq 7
|
||||
A = np.array([
|
||||
[1, 1, 1 ],
|
||||
[r[0][0], r[1][0], r[2][0]],
|
||||
[r[0][1], r[1][1], r[2][1]],
|
||||
])
|
||||
b = np.array([1, X[0], X[1]])
|
||||
try:
|
||||
phi = np.linalg.solve(A,b)
|
||||
except:
|
||||
print >> sys.stderr, "warning: calculation of phis yielded a linearly dependant system"
|
||||
phi = np.dot(np.linalg.pinv(A), b)
|
||||
|
||||
return phi
|
||||
|
||||
def qlinear(X, r, q):
|
||||
"""
|
||||
this calculates the linear portion of q from X to r
|
||||
|
||||
X = destination point
|
||||
r = simplex points
|
||||
q = CFD quantities of interest at the simplex points
|
||||
"""
|
||||
|
||||
phis = get_phis(X, r)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(q[:len(phis)], phis)])
|
||||
return qlin
|
||||
|
||||
def run_baker(X, g, tree, extra_points = 3, verbose = False):
|
||||
"""
|
||||
This is the main function to call to get an interpolation to X from the tree
|
||||
|
||||
X -- the destination point (2D)
|
||||
X = [0,0]
|
||||
|
||||
g -- the grid object
|
||||
|
||||
tree -- the kdtree search object (built from the g mesh)
|
||||
"""
|
||||
|
||||
(dist, indicies) = tree.query(X, 3 + extra_points)
|
||||
|
||||
nn = [g.points[i] for i in indicies]
|
||||
nq = [g.q[i] for i in indicies]
|
||||
|
||||
phi = get_phis(X, nn[:3])
|
||||
qlin = nq[0] * phi[0] + nq[1] * phi[1] + nq[2] * phi[2]
|
||||
|
||||
error_term = 0.0
|
||||
|
||||
if extra_points != 0:
|
||||
B = [] # baker eq 9
|
||||
w = [] # baker eq 11
|
||||
|
||||
for index in indicies[3:]:
|
||||
(phi1,phi2,phi3) = get_phis(g.points[index], nn)
|
||||
B.append([phi1 * phi2, phi2*phi3, phi3*phi1])
|
||||
|
||||
w.append(g.q[index] - qlinear(g.points[index], nn, nq))
|
||||
|
||||
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) = np.linalg.solve(A,b)
|
||||
except:
|
||||
print >> sys.stderr, "warning: linear calculation went bad, resorting to np.linalg.pinv"
|
||||
(a, b, c) = np.dot(np.linalg.pinv(A), b)
|
||||
|
||||
error_term = a * phi[0] * phi[1]\
|
||||
+ b * phi[1] * phi[2]\
|
||||
+ c * phi[2] * phi[0]
|
||||
|
||||
exact = exact_func(X[0], X[1])
|
||||
q_final = qlin + error_term
|
||||
|
||||
if verbose:
|
||||
print "current point : %s" % X
|
||||
print "exact : %0.4f" % exact
|
||||
print "qlin : %0.4f" % qlin
|
||||
print "qlinerr : %0.4f" % np.abs(exact - qlin)
|
||||
print "q_final : %0.4f" % q_final
|
||||
print "q_final_err : %0.4f" % np.abs(exact - q_final)
|
||||
print
|
||||
|
||||
return (q_final, exact)
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
import numpy as np
|
||||
import scipy.spatial
|
||||
|
||||
def exact_func(x, y):
|
||||
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
|
||||
return np.sin(x * np.pi) * np.cos(y * np.pi)
|
||||
|
||||
|
||||
class grid(object):
|
||||
def __init__(self, points, q):
|
||||
self.points = np.array(points)
|
||||
self.q = np.array(q)
|
||||
|
||||
def __str__(self):
|
||||
r = ''
|
||||
assert( len(self.points) == len(self.q) )
|
||||
for i in xrange(len(self.points)):
|
||||
r += "%r: %0.4f\n" % ( self.points[i], self.q[i] )
|
||||
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)
|
||||
|
||||
|
||||
self.points = []
|
||||
self.q = []
|
||||
for x in xrange(xres):
|
||||
cur_x = xmin + (x * xdel)
|
||||
for y in xrange(yres):
|
||||
cur_y = ymin + (y * ydel)
|
||||
self.points.append([cur_x, cur_y])
|
||||
self.q.append(exact_func(cur_x, cur_y))
|
||||
self.points = np.array(self.points)
|
||||
self.q = np.array(self.q)
|
||||
|
||||
|
||||
def for_qhull(self):
|
||||
r = '2\n'
|
||||
r += '%d\n' % len(self.points)
|
||||
for p in self.points:
|
||||
r += "%f %f\n" % (p[0], p[1])
|
||||
return r
|
||||
|
||||
|
||||
class simple_random_grid(simple_rect_grid):
|
||||
def __init__(self, num_points = 10):
|
||||
self.points = []
|
||||
self.q = []
|
||||
|
||||
r = np.random
|
||||
|
||||
for i in xrange(num_points):
|
||||
cur_x = r.rand()
|
||||
cur_y = r.rand()
|
||||
|
||||
self.points.append([cur_x, cur_y])
|
||||
self.q.append(exact_func(cur_x, cur_y))
|
||||
|
||||
self.points = np.array(self.points)
|
||||
self.q = np.array(self.q)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
g = simple_random_grid(100)
|
||||
print g.for_qhull()
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "hello world"
|
||||
@@ -0,0 +1,12 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def rms(errors):
|
||||
"""
|
||||
root mean square calculation
|
||||
"""
|
||||
r = 0.0
|
||||
for i in errors:
|
||||
r += np.power(i, 2)
|
||||
r = np.sqrt(r / len(errors))
|
||||
return r
|
||||
Reference in New Issue
Block a user