MAJOR: updated the baker method. it's more generic, and should allow me to 3D-ifiy it more simply. a ton of other things
This commit is contained in:
+45
-40
@@ -8,7 +8,7 @@ def get_phis(X, r):
|
||||
|
||||
X -- the destination point (2D)
|
||||
X = [0,0]
|
||||
r -- the three points that make up the triangle (2D)
|
||||
r -- the three points that make up the triangular simplex (2D)
|
||||
r = [[-1, -1], [0, 2], [1, -1]]
|
||||
|
||||
this will return [0.333, 0.333, 0.333]
|
||||
@@ -65,33 +65,35 @@ def get_phis_3D(X, r):
|
||||
return phi
|
||||
|
||||
|
||||
def qlinear(X, r, q):
|
||||
def qlinear(X, R, q):
|
||||
"""
|
||||
this calculates the linear portion of q from X to r
|
||||
|
||||
also, this is baker eq 3
|
||||
|
||||
X = destination point
|
||||
r = simplex points
|
||||
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)])
|
||||
phis = get_phis(X, R)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(q, phis)])
|
||||
return qlin
|
||||
|
||||
def qlinear_3D(X, r, q):
|
||||
def qlinear_3D(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(r)
|
||||
R = simplex points
|
||||
q = CFD quantities of interest at the simplex points(R)
|
||||
"""
|
||||
|
||||
phis = get_phis_3D(X, r)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(q[:len(phis)], phis)])
|
||||
phis = get_phis_3D(X, R)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(q, phis)])
|
||||
return qlin
|
||||
|
||||
def run_baker(X, g, tree, extra_points = 3, verbose = False):
|
||||
def run_baker(X, R, S, extra_points = 3, verbose = False):
|
||||
"""
|
||||
This is the main function to call to get an interpolation to X from the tree
|
||||
|
||||
@@ -104,44 +106,47 @@ def run_baker(X, g, tree, extra_points = 3, verbose = False):
|
||||
|
||||
"""
|
||||
|
||||
(dist, indicies) = tree.query(X, 3 + extra_points)
|
||||
|
||||
nn = [g.points[i] for i in indicies]
|
||||
nq = [g.q[i] for i in indicies]
|
||||
|
||||
# calculate values only for the triangle
|
||||
phi = get_phis(X, nn[:3])
|
||||
qlin = qlinear(X, nn[:3], nq[:3])# nq[0] * phi[0] + nq[1] * phi[1] + nq[2] * phi[2]
|
||||
phi = get_phis(X, S.points)
|
||||
qlin = qlinear (X, S.points, S.q)
|
||||
|
||||
error_term = 0.0
|
||||
if extra_points == 0: return qlin
|
||||
|
||||
if extra_points != 0:
|
||||
B = [] # baker eq 9
|
||||
w = [] # baker eq 11
|
||||
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])
|
||||
for (s, q) in zip(S.points, S.q):
|
||||
(phi1, phi2, phi3) = get_phis(s, R.points)
|
||||
B.append([phi1 * phi2, phi2*phi3, phi3*phi1])
|
||||
|
||||
w.append(g.q[index] - qlinear(g.points[index], nn, nq))
|
||||
w.append(q - qlinear(s, R.points, R.q))
|
||||
|
||||
B = np.array(B)
|
||||
w = np.array(w)
|
||||
B = np.array(B)
|
||||
w = np.array(w)
|
||||
|
||||
A = np.dot(B.T, B)
|
||||
b = np.dot(B.T, 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)
|
||||
# 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]
|
||||
error_term = a * phi[0] * phi[1]\
|
||||
+ b * phi[1] * phi[2]\
|
||||
+ c * phi[2] * phi[0]
|
||||
|
||||
q_final = qlin + error_term
|
||||
|
||||
return qlin, error_term, q_final
|
||||
answer = {
|
||||
'a': a,
|
||||
'b': b,
|
||||
'c': c,
|
||||
'qlin': qlin,
|
||||
'error': error_term,
|
||||
'final': q_final,
|
||||
}
|
||||
|
||||
return answer
|
||||
|
||||
+5
-1
@@ -71,5 +71,9 @@ class simple_random_grid(simple_rect_grid):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
g = simple_random_grid(100)
|
||||
try:
|
||||
resolution = int(sys.argv[1])
|
||||
except:
|
||||
resolution = 10
|
||||
g = simple_rect_grid(resolution, resolution)
|
||||
print g.for_qhull()
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import numpy as np
|
||||
import grid
|
||||
|
||||
|
||||
class smberror(Exception):
|
||||
def __init__(self, val):
|
||||
@@ -21,19 +19,3 @@ def rms(errors):
|
||||
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)
|
||||
|
||||
|
||||
def get_mesh(source, destination, use_structured_grid = False):
|
||||
mesh_source = None
|
||||
mesh_dest = None
|
||||
if use_structured_grid:
|
||||
mesh_source = grid.simple_rect_grid(source, source)
|
||||
mesh_dest = grid.simple_rect_grid(destination, destination)
|
||||
else:
|
||||
mesh_source = grid.simple_random_grid(source)
|
||||
mesh_dest = grid.simple_random_grid(destination)
|
||||
|
||||
if not (mesh_dest and mesh_source):
|
||||
raise smberror('problem creating mesh objects')
|
||||
else:
|
||||
return mesh_source, mesh_dest
|
||||
|
||||
Reference in New Issue
Block a user