#!/usr/bin/python import sys import re from collections import defaultdict import numpy as np import scipy.spatial from baker import run_baker from baker.tools import exact_func, smberror from simplex import face from smcqdelaunay import * class grid(object): facet_re = re.compile(r''' -\s+(?Pf\d+).*? vertices:\s(?P.*?)\n.*? neighboring\s facets:\s+(?P[\sf\d]*) ''', re.S|re.X) point_re = re.compile(r''' -\s+(?Pp\d+).*? neighbors:\s+(?P[\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): p = [self.points[i] for i in indicies] 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 the (a) containing simplex around point X S is S_j from baker's paper : some points from all point that are not the simplex """ (dist, indicies) = self.tree.query(X, simplex_size + extra_points) # get the containing simplex r_mesh = self.create_mesh(indicies[:simplex_size]) # and some extra points s_mesh = self.create_mesh(indicies[simplex_size:]) 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 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 for i in self.facets_for_point[indicies[0]]: if i.contains(X, self): simplex = i break if not simplex: raise AssertionError('no containing simplex found') R = self.create_mesh(simplex.verts) 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 def run_baker(self, X): answer = None try: (R, S) = self.get_simplex_and_nearest_points(X) answer = run_baker(X, R, S) except smberror, 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): """ a call to this method prepares the internal connectivity structure. this is part of the __init__ for a simple_rect_grid, but can be called from any grid object """ 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]]) # for matcher in grid.point_re.finditer(qdelaunay_string): # d = matcher.groupdict() # point = d['point'] # neighboring_facets = d['neigh'] # self.facets_for_point[int(point[1:])] = [i for i in neighboring_facets.split() if i in self.faces] 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 if __name__ == '__main__': try: resolution = int(sys.argv[1]) except: resolution = 10 g = simple_rect_grid(resolution, resolution) print g.for_qhull()