finally added proper (in my opinon) exception handling to the grid's run_baker method. it should properly autocorrect, and switch the extra point lookup scheme to connectivity-based if solving the system of equations yields a singular solution
This commit is contained in:
+16
-13
@@ -1,6 +1,8 @@
|
||||
import numpy as np
|
||||
import sys
|
||||
|
||||
from tools import smberror
|
||||
|
||||
def get_phis(X, R):
|
||||
"""
|
||||
The get_phis function is used to get barycentric coordonites for a point on a triangle.
|
||||
@@ -26,7 +28,8 @@ def get_phis(X, R):
|
||||
try:
|
||||
phi = np.linalg.solve(A,b)
|
||||
except:
|
||||
print >> sys.stderr, "warning: calculation of phis yielded a linearly dependant system"
|
||||
print >> sys.stderr, "warning: get_phis: calculation of phis yielded a linearly dependant system"
|
||||
raise smberror('get_phis')
|
||||
phi = np.dot(np.linalg.pinv(A), b)
|
||||
|
||||
return phi
|
||||
@@ -58,13 +61,13 @@ def get_phis_3D(X, r):
|
||||
try:
|
||||
phi = np.linalg.solve(A,b)
|
||||
except:
|
||||
print >> sys.stderr, "warning: calculation of phis yielded a linearly dependant system"
|
||||
print >> sys.stderr, "warning: get_phis_3D: calculation of phis yielded a linearly dependant system"
|
||||
phi = np.dot(np.linalg.pinv(A), b)
|
||||
|
||||
return phi
|
||||
|
||||
|
||||
def qlinear(X, R, q):
|
||||
def qlinear(X, R):
|
||||
"""
|
||||
this calculates the linear portion of q from X to r
|
||||
|
||||
@@ -75,9 +78,9 @@ def qlinear(X, R, q):
|
||||
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, phis)])
|
||||
return qlin
|
||||
phis = get_phis(X, R.points)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(R.q, phis)])
|
||||
return phis, qlin
|
||||
|
||||
def qlinear_3D(X, R, q):
|
||||
"""
|
||||
@@ -90,7 +93,7 @@ def qlinear_3D(X, R, q):
|
||||
|
||||
phis = get_phis_3D(X, R)
|
||||
qlin = sum([q_i * phi_i for q_i, phi_i in zip(q, phis)])
|
||||
return qlin
|
||||
return phis, qlin
|
||||
|
||||
def run_baker(X, R, S):
|
||||
"""
|
||||
@@ -106,8 +109,7 @@ def run_baker(X, R, S):
|
||||
"""
|
||||
|
||||
# calculate values only for the triangle
|
||||
phi = get_phis(X, R.points)
|
||||
qlin = qlinear (X, R.points, R.q)
|
||||
phi, qlin = qlinear (X, R)
|
||||
|
||||
if len(S.points) == 0:
|
||||
answer = {
|
||||
@@ -124,10 +126,11 @@ def run_baker(X, R, S):
|
||||
w = [] # baker eq 11
|
||||
|
||||
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])
|
||||
cur_phi, cur_qlin = qlinear(s, R)
|
||||
(phi1, phi2, phi3) = cur_phi
|
||||
|
||||
w.append(q - qlinear(s, R.points, R.q))
|
||||
B.append([phi1 * phi2, phi2 * phi3, phi3 * phi1])
|
||||
w.append(q - cur_qlin)
|
||||
|
||||
B = np.array(B)
|
||||
w = np.array(w)
|
||||
@@ -139,7 +142,7 @@ def run_baker(X, R, S):
|
||||
try:
|
||||
(a, b, c) = np.linalg.solve(A,b)
|
||||
except:
|
||||
print >> sys.stderr, "warning: linear calculation went bad, resorting to np.linalg.pinv"
|
||||
print >> sys.stderr, "warning: run_baker: 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]\
|
||||
|
||||
+14
-7
@@ -8,7 +8,7 @@ import numpy as np
|
||||
import scipy.spatial
|
||||
|
||||
from baker import run_baker, get_phis
|
||||
from tools import exact_func
|
||||
from tools import exact_func, smberror
|
||||
from smcqdelaunay import *
|
||||
|
||||
class face(object):
|
||||
@@ -146,13 +146,20 @@ class grid(object):
|
||||
|
||||
return R, S
|
||||
|
||||
def run_baker(self, X, connectivity_based = False):
|
||||
print >>sys.stderr, "suxor"
|
||||
if connectivity_based:
|
||||
(R, S) = self.get_points_conn(X)
|
||||
else:
|
||||
def run_baker(self, X):
|
||||
answer = None
|
||||
|
||||
try:
|
||||
(R, S) = self.get_simplex_and_nearest_points(X)
|
||||
return run_baker(X, R, S)
|
||||
answer = run_baker(X, R, S)
|
||||
except smberror as e:
|
||||
print "caught error: %s, trying with connectivity-based mesh" % e
|
||||
(R, S) = self.get_points_conn(X)
|
||||
answer = run_baker(X, R, S)
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
|
||||
def construct_connectivity(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user