2010-03-20 16:10:02 -07:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import numpy as np
|
2010-11-08 15:25:59 -08:00
|
|
|
from scipy.spatial import KDTree
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-10-23 12:49:15 -07:00
|
|
|
from interp.baker import run_baker
|
2010-11-01 14:53:06 -07:00
|
|
|
from interp.tools import log
|
2010-11-08 15:25:59 -08:00
|
|
|
from interp.grid.simplex import cell, contains
|
|
|
|
from interp.grid.smcqdelaunay import *
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
class grid(object):
|
2010-10-23 16:01:10 -07:00
|
|
|
def __init__(self, verts = None, q = None):
|
2010-10-22 15:10:58 -07:00
|
|
|
"""
|
2010-10-23 16:01:10 -07:00
|
|
|
verts = array of arrays (if passed in, will convert to numpy.array)
|
|
|
|
[
|
|
|
|
[x0,y0],
|
|
|
|
[x1,y1], ...
|
|
|
|
]
|
|
|
|
|
|
|
|
q = array (1D) of physical values
|
2010-10-22 15:10:58 -07:00
|
|
|
"""
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
if verts:
|
|
|
|
self.verts = np.array(verts)
|
2010-11-08 15:25:59 -08:00
|
|
|
self.tree = KDTree(self.verts)
|
2010-10-23 16:01:10 -07:00
|
|
|
if q:
|
|
|
|
self.q = np.array(q)
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
self.cells = {}
|
|
|
|
self.cells_for_vert = defaultdict(list)
|
2010-10-22 15:10:58 -07:00
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
def get_containing_simplex(self, X):
|
2010-11-08 14:07:48 -08:00
|
|
|
if not self.cells:
|
|
|
|
raise Exception("cell connectivity is not set up")
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
# get closest point
|
|
|
|
(dist, indicies) = self.tree.query(X, 2)
|
|
|
|
closest_point = indicies[0]
|
|
|
|
|
|
|
|
log.debug('X: %s' % X)
|
|
|
|
log.debug('point index: %d' % closest_point)
|
|
|
|
log.debug('actual point %s' % self.verts[closest_point])
|
|
|
|
log.debug('distance = %0.4f' % dist[0])
|
|
|
|
|
|
|
|
simplex = None
|
2010-11-08 14:07:48 -08:00
|
|
|
checked_cells = []
|
|
|
|
cells_to_check = list(self.cells_for_vert[closest_point])
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
attempts = 0
|
2010-11-08 14:07:48 -08:00
|
|
|
while not simplex and cells_to_check:
|
2010-10-23 16:01:10 -07:00
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
# if attempts > 20:
|
|
|
|
# raise Exception("probably recursing to many times")
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
cur_cell = cells_to_check.pop(0)
|
|
|
|
checked_cells.append(cur_cell)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
if cur_cell.contains(X, self):
|
|
|
|
simplex = cur_cell
|
2010-10-23 16:01:10 -07:00
|
|
|
continue
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
for neighbor in cur_cell.neighbors:
|
|
|
|
if (neighbor not in checked_cells) and (neighbor not in cells_to_check):
|
|
|
|
cells_to_check.append(neighbor)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
if not simplex:
|
2010-11-01 19:24:12 -07:00
|
|
|
raise Exception('no containing simplex found')
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2010-11-01 19:24:12 -07:00
|
|
|
log.debug("simplex vert indicies: %s" % simplex.verts)
|
2010-10-23 16:01:10 -07:00
|
|
|
R = self.create_mesh(simplex.verts)
|
|
|
|
|
|
|
|
log.debug('total attempts before finding simplex: %d' % attempts)
|
|
|
|
return R
|
|
|
|
|
|
|
|
def create_mesh(self, indicies):
|
|
|
|
"""
|
|
|
|
this function takes a list of indicies, and then creates
|
|
|
|
and returns a grid object (collection of verts and q).
|
|
|
|
|
|
|
|
note: the input is indicies, the grid contains verts
|
|
|
|
"""
|
2010-10-29 11:43:18 -07:00
|
|
|
p = [self.verts[i] for i in indicies]
|
2010-10-23 16:01:10 -07:00
|
|
|
q = [self.q[i] for i in indicies]
|
|
|
|
return grid(p, q)
|
|
|
|
|
|
|
|
def get_simplex_and_nearest_points(self, X, extra_points = 3, simplex_size = 3):
|
|
|
|
"""
|
|
|
|
this returns two grid objects: R and S.
|
|
|
|
|
|
|
|
R is a grid object that is supposedly a containing simplex
|
2010-10-29 11:43:18 -07:00
|
|
|
around point X (TODO: it tends not to be)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
S is S_j from baker's paper : some verts from all point that are not the simplex
|
|
|
|
"""
|
2010-10-29 11:43:18 -07:00
|
|
|
log.debug("extra verts: %d" % extra_points)
|
|
|
|
log.debug("simplex size: %d" % simplex_size)
|
|
|
|
|
|
|
|
r_mesh = self.get_containing_simplex(X)
|
|
|
|
|
|
|
|
# and some UNIQUE extra verts
|
|
|
|
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
2010-11-01 19:24:12 -07:00
|
|
|
log.debug("extra indicies: %s" % indicies)
|
2010-10-29 11:43:18 -07:00
|
|
|
|
|
|
|
unique_indicies = []
|
|
|
|
for index in indicies:
|
|
|
|
close_point_in_R = False
|
|
|
|
for rvert in r_mesh.verts:
|
|
|
|
if all(rvert == self.verts[index]):
|
|
|
|
close_point_in_R = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not close_point_in_R:
|
|
|
|
unique_indicies.append(index)
|
|
|
|
else:
|
|
|
|
log.debug('throwing out %s: %s' % (index, self.verts[index]))
|
|
|
|
|
|
|
|
log.debug("indicies: %s" % indicies)
|
|
|
|
log.debug("unique indicies: %s" % unique_indicies)
|
|
|
|
s_mesh = self.create_mesh(unique_indicies)
|
|
|
|
|
|
|
|
return (r_mesh, s_mesh)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
def run_baker(self, X, extra_points = 3, order = 2):
|
2010-10-29 11:43:18 -07:00
|
|
|
(R, S) = self.get_simplex_and_nearest_points(X, extra_points)
|
|
|
|
answer = run_baker(X, R, S, order)
|
|
|
|
return answer
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
r = ''
|
|
|
|
assert( len(self.verts) == len(self.q) )
|
|
|
|
for c, i in enumerate(zip(self.verts, self.q)):
|
2010-10-29 11:43:18 -07:00
|
|
|
r += "%d vert(%s): q(%0.4f)" % (c,i[0], i[1])
|
2010-11-08 14:07:48 -08:00
|
|
|
cell_str = ", ".join([str(f.name) for f in self.cells_for_vert[c]])
|
|
|
|
r += " cells: [%s]" % cell_str
|
2010-10-23 16:01:10 -07:00
|
|
|
r += "\n"
|
2010-11-08 14:07:48 -08:00
|
|
|
if self.cells:
|
|
|
|
for v in self.cells.itervalues():
|
2010-10-23 16:01:10 -07:00
|
|
|
r += "%s\n" % v
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-22 15:10:58 -07:00
|
|
|
class delaunay_grid(grid):
|
2010-11-08 14:07:48 -08:00
|
|
|
cell_re = re.compile(r'''
|
|
|
|
-\s+(?P<cell>f\d+).*?
|
2010-03-20 16:10:02 -07:00
|
|
|
vertices:\s(?P<verts>.*?)\n.*?
|
2010-11-08 14:07:48 -08:00
|
|
|
neighboring\s cells:\s+(?P<neigh>[\sf\d]*)
|
2010-03-20 16:10:02 -07:00
|
|
|
''', 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)
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
def __init__(self, verts, q):
|
|
|
|
grid.__init__(self, verts,q)
|
2010-04-30 10:56:01 -07:00
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-04-23 15:29:31 -07:00
|
|
|
def get_containing_simplex(self, X):
|
2010-11-08 14:07:48 -08:00
|
|
|
if not self.cells:
|
2010-04-24 17:26:44 -07:00
|
|
|
self.construct_connectivity()
|
|
|
|
|
|
|
|
# get closest point
|
|
|
|
(dist, indicies) = self.tree.query(X, 2)
|
|
|
|
closest_point = indicies[0]
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
log.debug('X: %s' % X)
|
|
|
|
log.debug('point index: %d' % closest_point)
|
|
|
|
log.debug('actual point %s' % self.verts[closest_point])
|
|
|
|
log.debug('distance = %0.4f' % dist[0])
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
simplex = None
|
2010-11-08 14:07:48 -08:00
|
|
|
checked_cells = []
|
|
|
|
cells_to_check = self.cells_for_vert[closest_point]
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
attempts = 0
|
2010-11-08 14:07:48 -08:00
|
|
|
while not simplex and cells_to_check:
|
2010-04-24 17:26:44 -07:00
|
|
|
attempts += 1
|
2010-04-29 22:29:35 -07:00
|
|
|
# if attempts > 20:
|
2010-10-23 16:01:10 -07:00
|
|
|
# raise Exception("probably recursing to many times")
|
2010-11-08 14:07:48 -08:00
|
|
|
cur_cell = cells_to_check.pop(0)
|
|
|
|
checked_cells.append(cur_cell)
|
2010-04-24 17:26:44 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
if cur_cell.contains(X, self):
|
|
|
|
simplex = cur_cell
|
2010-04-30 10:56:01 -07:00
|
|
|
continue
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
for neighbor in cur_cell.neighbors:
|
|
|
|
if (neighbor not in checked_cells) and (neighbor not in cells_to_check):
|
|
|
|
cells_to_check.append(neighbor)
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
if not simplex:
|
2010-11-01 19:24:12 -07:00
|
|
|
raise Exception('no containing simplex found')
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
R = self.create_mesh(simplex.verts)
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
log.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-10-23 16:01:10 -07:00
|
|
|
S is S_j from baker's paper : some verts from all point that are not the simplex
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2010-10-23 16:01:10 -07:00
|
|
|
log.debug("extra verts: %d" % extra_points)
|
|
|
|
log.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-10-23 16:01:10 -07:00
|
|
|
# log.debug("R:\n%s" % r_mesh)
|
2010-04-30 10:56:01 -07:00
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
# and some UNIQUE extra verts
|
2010-04-30 10:56:01 -07:00
|
|
|
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
|
|
|
|
|
|
|
unique_indicies = []
|
|
|
|
for index in indicies:
|
2010-10-23 16:01:10 -07:00
|
|
|
if self.verts[index] not in r_mesh.verts:
|
2010-04-30 10:56:01 -07:00
|
|
|
unique_indicies.append(index)
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
log.debug("indicies: %s" % ",".join([str(i) for i in indicies]))
|
|
|
|
log.debug("indicies: %s" % ",".join([str(i) for i in unique_indicies]))
|
2010-04-30 10:56:01 -07:00
|
|
|
s_mesh = self.create_mesh(unique_indicies)# indicies[simplex_size:])
|
|
|
|
|
|
|
|
# TODO: eventually remove this test:
|
2010-10-23 16:01:10 -07:00
|
|
|
for point in s_mesh.verts:
|
|
|
|
if point in r_mesh.verts:
|
|
|
|
log.error("ERROR")
|
|
|
|
log.error("\n%s\nin\n%s" % (point, r_mesh))
|
|
|
|
raise Exception("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
|
2010-10-23 16:01:10 -07:00
|
|
|
function in that it builds up the extra verts based on
|
2010-03-20 16:10:02 -07:00
|
|
|
connectivity information, not just nearest-neighbor.
|
|
|
|
in theory, this will work much better for situations like
|
2010-10-23 16:01:10 -07:00
|
|
|
verts near a short edge in a boundary layer cell where the
|
|
|
|
nearest verts would all be colinear
|
2010-03-20 16:10:02 -07:00
|
|
|
|
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
|
2010-10-23 16:01:10 -07:00
|
|
|
S is a connectivity-based nearest-neighbor lookup, limited to 3 extra verts
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2010-11-08 14:07:48 -08:00
|
|
|
if not self.cells:
|
2010-03-20 16:10:02 -07:00
|
|
|
self.construct_connectivity()
|
|
|
|
|
|
|
|
# get closest point
|
|
|
|
(dist, indicies) = self.tree.query(X, 2)
|
|
|
|
|
|
|
|
simplex = None
|
2010-11-08 14:07:48 -08:00
|
|
|
for cell in self.cells_for_vert[indicies[0]]:
|
|
|
|
if cell.contains(X, self):
|
|
|
|
simplex = cell
|
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-10-23 16:01:10 -07:00
|
|
|
if not contains(X, R.verts):
|
|
|
|
raise Exception("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-10-23 16:01:10 -07:00
|
|
|
except Exception, e:
|
|
|
|
log.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-10-23 16:01:10 -07:00
|
|
|
log.debug('start')
|
2010-03-20 16:10:02 -07:00
|
|
|
qdelaunay_string = get_qdelaunay_dump_str(self)
|
2010-11-08 14:07:48 -08:00
|
|
|
cell_to_cells = []
|
2010-11-08 15:25:59 -08:00
|
|
|
for matcher in delaunay_grid.cell_re.finditer(qdelaunay_string):
|
2010-03-20 16:10:02 -07:00
|
|
|
d = matcher.groupdict()
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
cell_name = d['cell']
|
2010-03-20 16:10:02 -07:00
|
|
|
verticies = d['verts']
|
2010-11-08 14:07:48 -08:00
|
|
|
neighboring_cells = d['neigh']
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
cur_cell = cell(cell_name)
|
|
|
|
self.cells[cell_name] = cur_cell
|
2010-03-20 16:10:02 -07:00
|
|
|
|
|
|
|
for v in grid.vert_re.findall(verticies):
|
|
|
|
vertex_index = int(v[1:])
|
2010-11-08 14:07:48 -08:00
|
|
|
cur_cell.add_vert(vertex_index)
|
|
|
|
self.cells_for_vert[vertex_index].append(cur_cell)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
nghbrs = [(cell_name, i) for i in neighboring_cells.split()]
|
|
|
|
cell_to_cells.extend(nghbrs)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
for rel in cell_to_cells:
|
|
|
|
if rel[1] in self.cells:
|
|
|
|
self.cells[rel[0]].add_neighbor(self.cells[rel[1]])
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
log.debug('end')
|