removed more traces of the delaunay stuff from the DD file.
This commit is contained in:
parent
fca5a5c600
commit
305b1c82cf
@ -1,4 +1,4 @@
|
||||
from interp.grid import delaunay_grid as basegrid
|
||||
from interp.grid.delaunay import grid as basegrid
|
||||
|
||||
from interp.tools import exact_func, log
|
||||
|
||||
@ -9,27 +9,6 @@ class grid(basegrid):
|
||||
def __init__(self, verts, q):
|
||||
basegrid.__init__(self, verts, q)
|
||||
|
||||
def for_qhull_generator(self):
|
||||
"""
|
||||
this returns a generator that should be fed into qdelaunay
|
||||
"""
|
||||
|
||||
yield '2';
|
||||
yield '%d' % len(self.verts)
|
||||
|
||||
for p in self.verts:
|
||||
yield "%f %f" % (p[0], p[1])
|
||||
|
||||
def for_qhull(self):
|
||||
"""
|
||||
this returns a single string that should be fed into qdelaunay
|
||||
"""
|
||||
r = '2\n'
|
||||
r += '%d\n' % len(self.verts)
|
||||
for p in self.verts:
|
||||
r += "%f %f\n" % (p[0], p[1])
|
||||
return r
|
||||
|
||||
class rect_grid(grid):
|
||||
def __init__(self, xres = 5, yres = 5):
|
||||
xmin = -1.0
|
||||
|
@ -1,5 +1,4 @@
|
||||
import sys
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
from xml.dom.minidom import Document
|
||||
@ -8,10 +7,10 @@ import numpy as np
|
||||
from scipy.spatial import KDTree
|
||||
|
||||
from interp.baker import run_baker
|
||||
from interp.tools import log
|
||||
from interp.grid.simplex import cell, contains
|
||||
from interp.grid.smcqdelaunay import *
|
||||
import interp.grid.simplex
|
||||
|
||||
#TODO: do logging right
|
||||
from interp.tools import log
|
||||
|
||||
class grid(object):
|
||||
def __init__(self, verts = None, q = None):
|
||||
@ -140,7 +139,7 @@ class grid(object):
|
||||
this returns a generator that should be fed into qdelaunay
|
||||
"""
|
||||
|
||||
yield '3';
|
||||
yield str(len(self.verts[0]));
|
||||
yield '%d' % len(self.verts)
|
||||
|
||||
for p in self.verts:
|
||||
@ -150,7 +149,7 @@ class grid(object):
|
||||
"""
|
||||
this returns a single string that should be fed into qdelaunay
|
||||
"""
|
||||
r = '3\n'
|
||||
r = '%d\n' % len(self.verts[0])
|
||||
r += '%d\n' % len(self.verts)
|
||||
for p in self.verts:
|
||||
r += "%f %f %f\n" % tuple(p)
|
||||
@ -196,196 +195,44 @@ class grid(object):
|
||||
return self.get_xml().toprettyxml()
|
||||
|
||||
|
||||
class cell(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.verts = []
|
||||
self.neighbors = []
|
||||
|
||||
|
||||
class delaunay_grid(grid):
|
||||
cell_re = re.compile(r'''
|
||||
-\s+(?P<cell>f\d+).*?
|
||||
vertices:\s(?P<verts>.*?)\n.*?
|
||||
neighboring\s cells:\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, verts, q):
|
||||
grid.__init__(self, verts,q)
|
||||
|
||||
|
||||
def get_containing_simplex(self, X):
|
||||
if not self.cells:
|
||||
self.construct_connectivity()
|
||||
|
||||
# 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
|
||||
checked_cells = []
|
||||
cells_to_check = self.cells_for_vert[closest_point]
|
||||
|
||||
attempts = 0
|
||||
while not simplex and cells_to_check:
|
||||
attempts += 1
|
||||
# if attempts > 20:
|
||||
# raise Exception("probably recursing to many times")
|
||||
cur_cell = cells_to_check.pop(0)
|
||||
checked_cells.append(cur_cell)
|
||||
|
||||
if cur_cell.contains(X, self):
|
||||
simplex = cur_cell
|
||||
continue
|
||||
|
||||
for neighbor in cur_cell.neighbors:
|
||||
if (neighbor not in checked_cells) and (neighbor not in cells_to_check):
|
||||
cells_to_check.append(neighbor)
|
||||
|
||||
if not simplex:
|
||||
raise Exception('no containing simplex found')
|
||||
|
||||
R = self.create_mesh(simplex.verts)
|
||||
|
||||
log.debug('total attempts before finding simplex: %d' % attempts)
|
||||
return R
|
||||
|
||||
|
||||
def get_simplex_and_nearest_points(self, X, extra_points = 3, simplex_size = 3):
|
||||
def add_vert(self, v):
|
||||
"""
|
||||
this returns two grid objects: R and S.
|
||||
|
||||
R is a grid object that is supposedly a containing simplex
|
||||
around point X (it tends not to be)
|
||||
|
||||
S is S_j from baker's paper : some verts from all point that are not the simplex
|
||||
v should be an index into grid.verts
|
||||
"""
|
||||
log.debug("extra verts: %d" % extra_points)
|
||||
log.debug("simplex size: %d" % simplex_size)
|
||||
self.verts.append(v)
|
||||
|
||||
r_mesh = self.get_containing_simplex(X)
|
||||
# log.debug("R:\n%s" % r_mesh)
|
||||
|
||||
# and some UNIQUE extra verts
|
||||
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
||||
|
||||
unique_indicies = []
|
||||
for index in indicies:
|
||||
if self.verts[index] not in r_mesh.verts:
|
||||
unique_indicies.append(index)
|
||||
|
||||
log.debug("indicies: %s" % ",".join([str(i) for i in indicies]))
|
||||
log.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.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")
|
||||
|
||||
return (r_mesh, s_mesh)
|
||||
|
||||
def get_points_conn(self, X):
|
||||
def add_neighbor(self, n):
|
||||
"""
|
||||
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 verts based on
|
||||
connectivity information, not just nearest-neighbor.
|
||||
in theory, this will work much better for situations like
|
||||
verts near a short edge in a boundary layer cell where the
|
||||
nearest verts would all be colinear
|
||||
|
||||
also, it guarantees that we find a containing simplex
|
||||
|
||||
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 verts
|
||||
reference to another cell object
|
||||
"""
|
||||
if not self.cells:
|
||||
self.construct_connectivity()
|
||||
self.neighbors.append(n)
|
||||
|
||||
# get closest point
|
||||
(dist, indicies) = self.tree.query(X, 2)
|
||||
|
||||
simplex = None
|
||||
for cell in self.cells_for_vert[indicies[0]]:
|
||||
if cell.contains(X, self):
|
||||
simplex = cell
|
||||
break
|
||||
|
||||
if not simplex:
|
||||
raise AssertionError('no containing simplex found')
|
||||
|
||||
# self.create_mesh(simplex.verts)
|
||||
R = self.get_containing_simplex(X)
|
||||
|
||||
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, extra_points = 3, order = 2):
|
||||
answer = None
|
||||
|
||||
try:
|
||||
(R, S) = self.get_simplex_and_nearest_points(X)
|
||||
if not contains(X, R.verts):
|
||||
raise Exception("run_baker with get_simplex_and_nearest_points returned non-containing simplex")
|
||||
answer = run_baker(X, R, S, order)
|
||||
except Exception, e:
|
||||
log.error("caught error: %s, trying with connectivity-based mesh" % e)
|
||||
(R, S) = self.get_points_conn(X)
|
||||
answer = run_baker(X, R, S, order)
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
|
||||
def construct_connectivity(self):
|
||||
def contains(self, X, G):
|
||||
"""
|
||||
a call to this method prepares the internal connectivity structure.
|
||||
X = point of interest
|
||||
G = corrensponding grid object (G.verts)
|
||||
because of the way i'm storing things,
|
||||
a cell simply stores indicies, and so one
|
||||
must pass in a reference to the grid object
|
||||
containing real verts.
|
||||
|
||||
this is part of the __init__ for a rect_grid, but can be called from any grid object
|
||||
this simply calls grid.simplex.contains
|
||||
"""
|
||||
log.debug('start')
|
||||
qdelaunay_string = get_qdelaunay_dump_str(self)
|
||||
# log.debug(qdelaunay_string)
|
||||
cell_to_cells = []
|
||||
for matcher in delaunay_grid.cell_re.finditer(qdelaunay_string):
|
||||
d = matcher.groupdict()
|
||||
return interp.grid.simplex.contains(X, [G.verts[i] for i in self.verts])
|
||||
|
||||
cell_name = d['cell']
|
||||
verticies = d['verts']
|
||||
neighboring_cells = d['neigh']
|
||||
def __str__(self):
|
||||
neighbors = [str(i.name) for i in self.neighbors]
|
||||
return '<cell %s: verts: %s neighbors: [%s]>' %\
|
||||
(
|
||||
self.name,
|
||||
self.verts,
|
||||
", ".join(neighbors)
|
||||
)
|
||||
|
||||
cur_cell = cell(cell_name)
|
||||
self.cells[cell_name] = cur_cell
|
||||
|
||||
for v in grid.vert_re.findall(verticies):
|
||||
vertex_index = int(v[1:])
|
||||
cur_cell.add_vert(vertex_index)
|
||||
self.cells_for_vert[vertex_index].append(cur_cell)
|
||||
|
||||
nghbrs = [(cell_name, i) for i in neighboring_cells.split()]
|
||||
cell_to_cells.extend(nghbrs)
|
||||
log.debug(cell_to_cells)
|
||||
|
||||
for rel in cell_to_cells:
|
||||
if rel[1] in self.cells:
|
||||
self.cells[rel[0]].add_neighbor(self.cells[rel[1]])
|
||||
|
||||
log.debug(self.cells)
|
||||
log.debug('end')
|
||||
__repr__ = __str__
|
||||
|
Loading…
Reference in New Issue
Block a user