2010-03-20 16:10:02 -07:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
from collections import defaultdict
|
2010-04-30 10:56:01 -07:00
|
|
|
import inspect
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import scipy.spatial
|
|
|
|
|
|
|
|
from baker import run_baker
|
2010-04-29 22:29:35 -07:00
|
|
|
from baker.tools import exact_func, smberror, smblog
|
2010-04-23 15:29:31 -07:00
|
|
|
from simplex import face, contains
|
2010-03-20 16:10:02 -07:00
|
|
|
from smcqdelaunay import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class grid(object):
|
|
|
|
facet_re = re.compile(r'''
|
|
|
|
-\s+(?P<facet>f\d+).*?
|
|
|
|
vertices:\s(?P<verts>.*?)\n.*?
|
|
|
|
neighboring\s facets:\s+(?P<neigh>[\sf\d]*)
|
|
|
|
''', re.S|re.X)
|
|
|
|
|
|
|
|
point_re = re.compile(r'''
|
|
|
|
-\s+(?P<point>p\d+).*?
|
|
|
|
neighbors:\s+(?P<neigh>[\sf\d]*)
|
|
|
|
''', re.S|re.X)
|
|
|
|
|
|
|
|
vert_re = re.compile(r'''
|
|
|
|
(p\d+)
|
|
|
|
''', re.S|re.X)
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, points, q):
|
|
|
|
"""
|
|
|
|
this thing eats two pre-constructed arrays of stuff:
|
|
|
|
points = array of arrays (i will convert to numpy.array)
|
|
|
|
[[x0,y0], [x1,y1], ...]
|
|
|
|
q = array (1D) of important values
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.points = np.array(points)
|
|
|
|
self.q = np.array(q)
|
|
|
|
self.tree = scipy.spatial.KDTree(self.points)
|
|
|
|
self.faces = {}
|
|
|
|
self.facets_for_point = defaultdict(list)
|
|
|
|
|
|
|
|
def create_mesh(self, indicies):
|
2010-04-30 10:56:01 -07:00
|
|
|
"""
|
|
|
|
this function takes a list of indicies, and then creates
|
|
|
|
and returns a grid object (collection of points and q).
|
|
|
|
|
|
|
|
note: the input is indicies, the grid contains points
|
|
|
|
"""
|
2010-03-20 16:10:02 -07:00
|
|
|
p = [self.points[i] for i in indicies]
|
|
|
|
q = [self.q[i] for i in indicies]
|
|
|
|
return grid(p, q)
|
|
|
|
|
2010-04-23 15:29:31 -07:00
|
|
|
def get_containing_simplex(self, X):
|
2010-04-24 17:26:44 -07:00
|
|
|
if not self.faces:
|
|
|
|
self.construct_connectivity()
|
|
|
|
|
|
|
|
# get closest point
|
|
|
|
(dist, indicies) = self.tree.query(X, 2)
|
|
|
|
closest_point = indicies[0]
|
|
|
|
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.debug('X: %s' % X)
|
|
|
|
smblog.debug('point index: %d' % closest_point)
|
|
|
|
smblog.debug('actual point %s' % self.points[closest_point])
|
|
|
|
smblog.debug('distance = %0.4f' % dist[0])
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
simplex = None
|
|
|
|
checked_facets = []
|
|
|
|
facets_to_check = self.facets_for_point[closest_point]
|
|
|
|
|
|
|
|
attempts = 0
|
2010-04-30 10:56:01 -07:00
|
|
|
while not simplex and facets_to_check:
|
2010-04-24 17:26:44 -07:00
|
|
|
attempts += 1
|
2010-04-29 22:29:35 -07:00
|
|
|
# if attempts > 20:
|
|
|
|
# raise smberror("probably recursing to many times")
|
2010-04-30 10:56:01 -07:00
|
|
|
cur_facet = facets_to_check.pop(0)
|
2010-04-24 17:26:44 -07:00
|
|
|
checked_facets.append(cur_facet)
|
|
|
|
|
|
|
|
if cur_facet.contains(X, self):
|
|
|
|
simplex = cur_facet
|
2010-04-30 10:56:01 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
new_facest = []
|
|
|
|
for neighbor in cur_facet.neighbors:
|
|
|
|
if (neighbor not in checked_facets) and (neighbor not in facets_to_check):
|
|
|
|
facets_to_check.append(neighbor)
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
if not simplex:
|
|
|
|
raise AssertionError('no containing simplex found')
|
|
|
|
|
|
|
|
R = self.create_mesh(simplex.verts)
|
|
|
|
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.debug('total attempts before finding simplex: %d' % attempts)
|
2010-04-24 17:26:44 -07:00
|
|
|
return R
|
2010-04-23 15:29:31 -07:00
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
def get_simplex_and_nearest_points(self, X, extra_points = 3, simplex_size = 3):
|
|
|
|
"""
|
|
|
|
this returns two grid objects: R and S.
|
|
|
|
|
2010-04-23 08:58:30 -07:00
|
|
|
R is a grid object that is supposedly a containing simplex
|
|
|
|
around point X (it tends not to be)
|
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
S is S_j from baker's paper : some points from all point that are not the simplex
|
|
|
|
"""
|
2010-04-30 10:56:01 -07:00
|
|
|
smblog.debug(inspect.stack()[1][3])
|
|
|
|
smblog.debug("extra points: %d" % extra_points)
|
|
|
|
smblog.debug("simplex size: %d" % simplex_size)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-04-24 17:26:44 -07:00
|
|
|
r_mesh = self.get_containing_simplex(X)
|
2010-04-30 10:56:01 -07:00
|
|
|
# smblog.debug("R:\n%s" % r_mesh)
|
|
|
|
|
|
|
|
# and some UNIQUE extra points
|
|
|
|
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
|
|
|
|
|
|
|
unique_indicies = []
|
|
|
|
for index in indicies:
|
|
|
|
if self.points[index] not in r_mesh.points:
|
|
|
|
unique_indicies.append(index)
|
|
|
|
|
|
|
|
smblog.debug("indicies: %s" % ",".join([str(i) for i in indicies]))
|
|
|
|
smblog.debug("indicies: %s" % ",".join([str(i) for i in unique_indicies]))
|
|
|
|
s_mesh = self.create_mesh(unique_indicies)# indicies[simplex_size:])
|
|
|
|
|
|
|
|
# TODO: eventually remove this test:
|
|
|
|
for point in s_mesh.points:
|
|
|
|
if point in r_mesh.points:
|
|
|
|
smblog.error("ERROR")
|
|
|
|
smblog.error("\n%s\nin\n%s" % (point, r_mesh))
|
|
|
|
raise smberror("repeating point S and R")
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
return (r_mesh, s_mesh)
|
|
|
|
|
|
|
|
def get_points_conn(self, X):
|
|
|
|
"""
|
|
|
|
this returns two grid objects: R and S.
|
|
|
|
|
|
|
|
this function differes from the get_simplex_and_nearest_points
|
|
|
|
function in that it builds up the extra points based on
|
|
|
|
connectivity information, not just nearest-neighbor.
|
|
|
|
in theory, this will work much better for situations like
|
|
|
|
points near a short edge in a boundary layer cell where the
|
|
|
|
nearest points would all be colinear
|
|
|
|
|
2010-04-23 08:58:30 -07:00
|
|
|
also, it guarantees that we find a containing simplex
|
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
R is a grid object that is the (a) containing simplex around point X
|
|
|
|
S is a connectivity-based nearest-neighbor lookup, limited to 3 extra points
|
|
|
|
"""
|
|
|
|
if not self.faces:
|
|
|
|
self.construct_connectivity()
|
|
|
|
|
|
|
|
# get closest point
|
|
|
|
(dist, indicies) = self.tree.query(X, 2)
|
|
|
|
|
|
|
|
simplex = None
|
2010-03-20 19:09:46 -07:00
|
|
|
for facet in self.facets_for_point[indicies[0]]:
|
|
|
|
if facet.contains(X, self):
|
|
|
|
simplex = facet
|
2010-03-20 16:10:02 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
if not simplex:
|
|
|
|
raise AssertionError('no containing simplex found')
|
|
|
|
|
2010-04-30 10:56:01 -07:00
|
|
|
# self.create_mesh(simplex.verts)
|
|
|
|
R = self.get_containing_simplex(X)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
s = []
|
|
|
|
for c,i in enumerate(simplex.neighbors):
|
|
|
|
s.extend([guy for guy in i.verts if not guy in simplex.verts])
|
|
|
|
S = self.create_mesh(s)
|
|
|
|
|
|
|
|
return R, S
|
|
|
|
|
2010-04-29 22:29:35 -07:00
|
|
|
def run_baker(self, X, extra_points = 3, order = 2):
|
2010-03-20 16:10:02 -07:00
|
|
|
answer = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
(R, S) = self.get_simplex_and_nearest_points(X)
|
2010-04-23 15:29:31 -07:00
|
|
|
if not contains(X, R.points):
|
|
|
|
raise smberror("run_baker with get_simplex_and_nearest_points returned non-containing simplex")
|
2010-04-29 22:29:35 -07:00
|
|
|
answer = run_baker(X, R, S, order)
|
2010-03-20 16:10:02 -07:00
|
|
|
except smberror, e:
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.error("caught error: %s, trying with connectivity-based mesh" % e)
|
2010-03-20 16:10:02 -07:00
|
|
|
(R, S) = self.get_points_conn(X)
|
2010-04-29 22:29:35 -07:00
|
|
|
answer = run_baker(X, R, S, order)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def construct_connectivity(self):
|
|
|
|
"""
|
|
|
|
a call to this method prepares the internal connectivity structure.
|
|
|
|
|
|
|
|
this is part of the __init__ for a rect_grid, but can be called from any grid object
|
|
|
|
"""
|
2010-04-30 10:56:01 -07:00
|
|
|
smblog.debug('start')
|
2010-03-20 16:10:02 -07:00
|
|
|
qdelaunay_string = get_qdelaunay_dump_str(self)
|
|
|
|
facet_to_facets = []
|
|
|
|
for matcher in grid.facet_re.finditer(qdelaunay_string):
|
|
|
|
d = matcher.groupdict()
|
|
|
|
|
|
|
|
facet_name = d['facet']
|
|
|
|
verticies = d['verts']
|
|
|
|
neighboring_facets = d['neigh']
|
|
|
|
|
|
|
|
cur_face = face(facet_name)
|
|
|
|
self.faces[facet_name] = cur_face
|
|
|
|
|
|
|
|
for v in grid.vert_re.findall(verticies):
|
|
|
|
vertex_index = int(v[1:])
|
|
|
|
cur_face.add_vert(vertex_index)
|
|
|
|
self.facets_for_point[vertex_index].append(cur_face)
|
|
|
|
|
|
|
|
nghbrs = [(facet_name, i) for i in neighboring_facets.split()]
|
|
|
|
facet_to_facets.extend(nghbrs)
|
|
|
|
|
|
|
|
for rel in facet_to_facets:
|
|
|
|
if rel[1] in self.faces:
|
|
|
|
self.faces[rel[0]].add_neighbor(self.faces[rel[1]])
|
|
|
|
|
2010-04-30 10:56:01 -07:00
|
|
|
smblog.debug('end')
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
r = ''
|
|
|
|
assert( len(self.points) == len(self.q) )
|
|
|
|
for c, i in enumerate(zip(self.points, self.q)):
|
|
|
|
r += "%d %r: %0.4f" % (c,i[0], i[1])
|
|
|
|
facet_str = ", ".join([f.name for f in self.facets_for_point[c]])
|
|
|
|
r += " faces: [%s]" % facet_str
|
|
|
|
r += "\n"
|
|
|
|
if self.faces:
|
|
|
|
for v in self.faces.itervalues():
|
|
|
|
r += "%s\n" % v
|
|
|
|
return r
|