Major: made scripts pass pep8 and pyflakes
This commit is contained in:
+2
-3
@@ -1,10 +1,9 @@
|
||||
from interp.grid.delaunay import dgrid as basegrid
|
||||
from interp.tools import baker_exact_2D as exact_func
|
||||
|
||||
from itertools import product
|
||||
|
||||
import numpy as np
|
||||
|
||||
from interp.grid.delaunay import dgrid as basegrid
|
||||
|
||||
class rect_grid(basegrid):
|
||||
def __init__(self, xres = 5, yres = 5):
|
||||
xmin = 0.0
|
||||
|
||||
+2
-6
@@ -1,10 +1,9 @@
|
||||
from interp.grid.delaunay import dgrid as basegrid
|
||||
from interp.tools import baker_exact_3D, log
|
||||
|
||||
from itertools import product
|
||||
|
||||
import numpy as np
|
||||
|
||||
from interp.grid.delaunay import dgrid as basegrid
|
||||
|
||||
class rect_grid(basegrid):
|
||||
def __init__(self, xres = 5, yres = 5, zres = 5):
|
||||
xmin = 0.0
|
||||
@@ -22,7 +21,6 @@ class rect_grid(basegrid):
|
||||
zspan = zmaz - zmin
|
||||
zdel = zspan / float(zres - 1)
|
||||
|
||||
|
||||
verts = []
|
||||
q = np.zeros(xres * yres * zres)
|
||||
for x in xrange(xres):
|
||||
@@ -41,8 +39,6 @@ class random_grid(rect_grid):
|
||||
def __init__(self, num_verts = 100):
|
||||
verts = []
|
||||
|
||||
r = np.random
|
||||
|
||||
appx_side_res = int(np.power(num_verts, 1/3.0))
|
||||
delta = 1.0 / float(appx_side_res)
|
||||
|
||||
|
||||
+199
-191
@@ -1,5 +1,4 @@
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from collections import defaultdict
|
||||
import pickle
|
||||
|
||||
from xml.dom.minidom import Document
|
||||
@@ -9,256 +8,265 @@ from scipy.spatial import KDTree
|
||||
|
||||
from interp.baker import run_baker
|
||||
from interp.baker import get_phis
|
||||
import interp
|
||||
|
||||
import logging
|
||||
log = logging.getLogger("interp")
|
||||
|
||||
MAX_SEARCH_COUNT = 256
|
||||
TOL = 1e-8
|
||||
|
||||
__version__ = interp.__version__
|
||||
|
||||
|
||||
class grid(object):
|
||||
def __init__(self, verts = None, q = None):
|
||||
"""
|
||||
verts = array of arrays (if passed in, will convert to numpy.array)
|
||||
[
|
||||
[x0,y0 <, z0>],
|
||||
[x1,y1 <, z1>],
|
||||
...
|
||||
]
|
||||
def __init__(self, verts=None, q=None):
|
||||
"""
|
||||
verts = array of arrays (if passed in, will convert to numpy.array)
|
||||
[
|
||||
[x0,y0 <, z0>],
|
||||
[x1,y1 <, z1>],
|
||||
...
|
||||
]
|
||||
|
||||
q = array (1D) of physical values
|
||||
"""
|
||||
q = array (1D) of physical values
|
||||
"""
|
||||
|
||||
if verts != None:
|
||||
self.verts = np.array(verts)
|
||||
self.tree = KDTree(self.verts)
|
||||
if verts != None:
|
||||
self.verts = np.array(verts)
|
||||
self.tree = KDTree(self.verts)
|
||||
|
||||
if q != None:
|
||||
self.q = np.array(q)
|
||||
if q != None:
|
||||
self.q = np.array(q)
|
||||
|
||||
self.cells = {}
|
||||
self.cells_for_vert = defaultdict(list)
|
||||
self.cells = {}
|
||||
self.cells_for_vert = defaultdict(list)
|
||||
|
||||
def get_containing_simplex(self, X):
|
||||
if not self.cells:
|
||||
raise Exception("cell connectivity is not set up")
|
||||
def get_containing_simplex(self, X):
|
||||
if not self.cells:
|
||||
raise Exception("cell connectivity is not set up")
|
||||
|
||||
# get closest point
|
||||
(dist, indicies) = self.tree.query(X, 2)
|
||||
closest_point = indicies[0]
|
||||
# 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])
|
||||
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 = list(self.cells_for_vert[closest_point])
|
||||
simplex = None
|
||||
checked_cells = []
|
||||
cells_to_check = list(self.cells_for_vert[closest_point])
|
||||
|
||||
attempts = 0
|
||||
while not simplex and cells_to_check:
|
||||
attempts += 1
|
||||
attempts = 0
|
||||
while not simplex and cells_to_check:
|
||||
attempts += 1
|
||||
|
||||
if attempts > MAX_SEARCH_COUNT:
|
||||
raise Exception("Is the search becoming exhaustive? (%d attempts)" % attempts)
|
||||
if attempts > MAX_SEARCH_COUNT:
|
||||
raise Exception("Is the search becoming exhaustive?'\
|
||||
'(%d attempts)" % attempts)
|
||||
|
||||
cur_cell = cells_to_check.pop(0)
|
||||
checked_cells.append(cur_cell)
|
||||
cur_cell = cells_to_check.pop(0)
|
||||
checked_cells.append(cur_cell)
|
||||
|
||||
if cur_cell.contains(X, self):
|
||||
simplex = cur_cell
|
||||
continue
|
||||
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)
|
||||
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')
|
||||
if not simplex:
|
||||
raise Exception('no containing simplex found')
|
||||
|
||||
log.debug("simplex vert indicies: %s" % simplex.verts)
|
||||
R = self.create_mesh(simplex.verts)
|
||||
log.debug("R:\n%s", R)
|
||||
log.debug("simplex vert indicies: %s" % simplex.verts)
|
||||
R = self.create_mesh(simplex.verts)
|
||||
log.debug("R:\n%s", R)
|
||||
|
||||
log.debug('total attempts before finding simplex: %d' % attempts)
|
||||
return R
|
||||
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).
|
||||
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
|
||||
"""
|
||||
note: the input is indicies, the grid contains verts
|
||||
"""
|
||||
|
||||
return grid(self.verts[indicies], self.q[indicies])
|
||||
return grid(self.verts[indicies], self.q[indicies])
|
||||
|
||||
def get_simplex_and_nearest_points(self, X, extra_points = 3):
|
||||
"""
|
||||
this returns two grid objects: R and S.
|
||||
def get_simplex_and_nearest_points(self, X, extra_points=3):
|
||||
"""
|
||||
this returns two grid objects: R and S.
|
||||
|
||||
R is a grid object that is a containing simplex around point X
|
||||
R is a grid object that is a containing simplex around point X
|
||||
|
||||
S : some verts from all points that are not the simplex
|
||||
"""
|
||||
simplex_size = self.dim + 1
|
||||
log.debug("extra verts: %d" % extra_points)
|
||||
log.debug("simplex size: %d" % simplex_size)
|
||||
S : some verts from all points that are not the simplex
|
||||
"""
|
||||
simplex_size = self.dim + 1
|
||||
log.debug("extra verts: %d" % extra_points)
|
||||
log.debug("simplex size: %d" % simplex_size)
|
||||
|
||||
r_mesh = self.get_containing_simplex(X)
|
||||
r_mesh = self.get_containing_simplex(X)
|
||||
|
||||
# and some UNIQUE extra verts
|
||||
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
||||
log.debug("extra indicies: %s" % indicies)
|
||||
# and some UNIQUE extra verts
|
||||
(dist, indicies) = self.tree.query(X, simplex_size + extra_points)
|
||||
log.debug("extra indicies: %s" % indicies)
|
||||
|
||||
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
|
||||
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]))
|
||||
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)
|
||||
log.debug("indicies: %s" % indicies)
|
||||
log.debug("unique indicies: %s" % unique_indicies)
|
||||
s_mesh = self.create_mesh(unique_indicies)
|
||||
|
||||
return (r_mesh, s_mesh)
|
||||
return (r_mesh, s_mesh)
|
||||
|
||||
def run_baker(self, X, order = 2, extra_points = 3):
|
||||
(R, S) = self.get_simplex_and_nearest_points(X, extra_points)
|
||||
answer = run_baker(X, R, S, order)
|
||||
return answer
|
||||
def run_baker(self, X, order=2, extra_points=3):
|
||||
(R, S) = self.get_simplex_and_nearest_points(X, extra_points)
|
||||
answer = run_baker(X, R, S, order)
|
||||
return answer
|
||||
|
||||
def for_qhull_generator(self):
|
||||
"""
|
||||
this returns a generator that should be fed into qdelaunay
|
||||
"""
|
||||
def for_qhull_generator(self):
|
||||
"""
|
||||
this returns a generator that should be fed into qdelaunay
|
||||
"""
|
||||
|
||||
yield str(len(self.verts[0]));
|
||||
yield '%d' % len(self.verts)
|
||||
yield str(len(self.verts[0]))
|
||||
yield '%d' % len(self.verts)
|
||||
|
||||
for p in self.verts:
|
||||
yield "%f %f %f" % tuple(p)
|
||||
for p in self.verts:
|
||||
yield "%f %f %f" % tuple(p)
|
||||
|
||||
def for_qhull(self):
|
||||
"""
|
||||
this returns a single string that should be fed into qdelaunay
|
||||
"""
|
||||
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)
|
||||
r += "%s\n" % " ".join("%f" % i for i in p)
|
||||
return r
|
||||
def for_qhull(self):
|
||||
"""
|
||||
this returns a single string that should be fed into qdelaunay
|
||||
"""
|
||||
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)
|
||||
r += "%s\n" % " ".join("%f" % i for i in p)
|
||||
return r
|
||||
|
||||
def __str__(self):
|
||||
r = ''
|
||||
assert( len(self.verts) == len(self.q) )
|
||||
for c, i in enumerate(zip(self.verts, self.q)):
|
||||
r += "%d vert(%s): q(%0.4f)" % (c,i[0], i[1])
|
||||
cell_str = ", ".join([str(f.name) for f in self.cells_for_vert[c]])
|
||||
r += " cells: [%s]" % cell_str
|
||||
r += "\n"
|
||||
if self.cells:
|
||||
for v in self.cells.itervalues():
|
||||
r += "%s\n" % v
|
||||
return r
|
||||
def __str__(self):
|
||||
r = ''
|
||||
assert(len(self.verts) == len(self.q))
|
||||
for c, i in enumerate(zip(self.verts, self.q)):
|
||||
r += "%d vert(%s): q(%0.4f)" % (c, i[0], i[1])
|
||||
cell_str = ", ".join([str(f.name) for f in self.cells_for_vert[c]])
|
||||
r += " cells: [%s]" % cell_str
|
||||
r += "\n"
|
||||
if self.cells:
|
||||
for v in self.cells.itervalues():
|
||||
r += "%s\n" % v
|
||||
return r
|
||||
|
||||
def normalize_q(self, new_max = 0.1):
|
||||
largest_number = np.max(np.abs(self.q))
|
||||
self.q *= new_max/largest_number
|
||||
def normalize_q(self, new_max=0.1):
|
||||
largest_number = np.max(np.abs(self.q))
|
||||
self.q *= new_max / largest_number
|
||||
|
||||
def dump_to_blender_files(self,
|
||||
pfile='/tmp/points.p', cfile='/tmp/cells.p'):
|
||||
if len(self.verts[0]) == 2:
|
||||
pickle.dump([(p[0], p[1], 0.0) for p in self.verts],
|
||||
open(pfile, 'w'))
|
||||
else:
|
||||
pickle.dump([(p[0], p[1], p[2]) for p in self.verts],
|
||||
open(pfile, 'w'))
|
||||
|
||||
def dump_to_blender_files(self, pfile = '/tmp/points.p', cfile = '/tmp/cells.p'):
|
||||
if len(self.verts[0]) == 2:
|
||||
pickle.dump([(p[0], p[1], 0.0) for p in self.verts], open(pfile, 'w'))
|
||||
else:
|
||||
pickle.dump([(p[0], p[1], p[2]) for p in self.verts], open(pfile, 'w'))
|
||||
pickle.dump([f.verts for f in self.cells.itervalues()],
|
||||
open(cfile, 'w'))
|
||||
|
||||
pickle.dump([f.verts for f in self.cells.itervalues()], open(cfile, 'w'))
|
||||
def get_xml(self):
|
||||
doc = Document()
|
||||
ps = doc.createElement("points")
|
||||
doc.appendChild(ps)
|
||||
for i in zip(self.verts, self.q):
|
||||
p = doc.createElement("point")
|
||||
|
||||
def get_xml(self):
|
||||
doc = Document()
|
||||
ps = doc.createElement("points")
|
||||
doc.appendChild(ps)
|
||||
for i in zip(self.verts, self.q):
|
||||
p = doc.createElement("point")
|
||||
p.setAttribute("x", str(i[0][0]))
|
||||
p.setAttribute('y', str(i[0][1]))
|
||||
p.setAttribute('z', str(i[0][2]))
|
||||
p.setAttribute('q', str(i[1]))
|
||||
ps.appendChild(p)
|
||||
|
||||
p.setAttribute("x", str(i[0][0]))
|
||||
p.setAttribute('y', str(i[0][1]))
|
||||
p.setAttribute('z', str(i[0][2]))
|
||||
p.setAttribute('q', str(i[1] ))
|
||||
ps.appendChild(p)
|
||||
return doc
|
||||
|
||||
return doc
|
||||
def toxml(self):
|
||||
return self.get_xml().toxml()
|
||||
|
||||
def toxml(self):
|
||||
return self.get_xml().toxml()
|
||||
def toprettyxml(self):
|
||||
return self.get_xml().toprettyxml()
|
||||
def toprettyxml(self):
|
||||
return self.get_xml().toprettyxml()
|
||||
|
||||
|
||||
class cell(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.verts = []
|
||||
self.neighbors = []
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.verts = []
|
||||
self.neighbors = []
|
||||
|
||||
def add_vert(self, v):
|
||||
"""
|
||||
v should be an index into grid.verts
|
||||
"""
|
||||
self.verts.append(v)
|
||||
def add_vert(self, v):
|
||||
"""
|
||||
v should be an index into grid.verts
|
||||
"""
|
||||
self.verts.append(v)
|
||||
|
||||
def add_neighbor(self, n):
|
||||
"""
|
||||
reference to another cell object
|
||||
"""
|
||||
self.neighbors.append(n)
|
||||
def add_neighbor(self, n):
|
||||
"""
|
||||
reference to another cell object
|
||||
"""
|
||||
self.neighbors.append(n)
|
||||
|
||||
def contains(self, X, G):
|
||||
"""
|
||||
X = point of interest
|
||||
G = corrensponding grid object (G.verts)
|
||||
def contains(self, X, G):
|
||||
"""
|
||||
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.
|
||||
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 simply calls grid.simplex.contains
|
||||
"""
|
||||
return contains(X, [G.verts[i] for i in self.verts])
|
||||
this simply calls grid.simplex.contains
|
||||
"""
|
||||
return contains(X, [G.verts[i] for i in self.verts])
|
||||
|
||||
def __str__(self):
|
||||
# neighbors = [str(i.name) for i in self.neighbors]
|
||||
return '<cell %s: verts: %s neighbor count: %s>' %\
|
||||
(
|
||||
self.name,
|
||||
self.verts,
|
||||
len(self.neighbors),
|
||||
# ", ".join(neighbors)
|
||||
)
|
||||
def __str__(self):
|
||||
# neighbors = [str(i.name) for i in self.neighbors]
|
||||
return '<cell %s: verts: %s neighbor count: %s>' %\
|
||||
(
|
||||
self.name,
|
||||
self.verts,
|
||||
len(self.neighbors),
|
||||
# ", ".join(neighbors)
|
||||
)
|
||||
|
||||
__repr__ = __str__
|
||||
__repr__ = __str__
|
||||
|
||||
|
||||
TOL = 1e-8
|
||||
|
||||
def contains(X, R):
|
||||
"""
|
||||
tests if X (point) is in R
|
||||
"""
|
||||
tests if X (point) is in R
|
||||
|
||||
R is a simplex, represented by a list of n-degree coordinates
|
||||
"""
|
||||
phis = get_phis(X, R)
|
||||
R is a simplex, represented by a list of n-degree coordinates
|
||||
"""
|
||||
phis = get_phis(X, R)
|
||||
|
||||
r = True
|
||||
if [i for i in phis if i < 0.0 - TOL]:
|
||||
r = False
|
||||
return r
|
||||
r = True
|
||||
if [i for i in phis if i < 0.0 - TOL]:
|
||||
r = False
|
||||
return r
|
||||
|
||||
+1
-4
@@ -1,7 +1,4 @@
|
||||
import pickle
|
||||
|
||||
from itertools import combinations
|
||||
from collections import defaultdict
|
||||
|
||||
import numpy as np
|
||||
from scipy.spatial import KDTree
|
||||
@@ -36,7 +33,7 @@ class ggrid(grid):
|
||||
|
||||
|
||||
gmsh_file.readline() # $MeshFormat
|
||||
fmat = gmsh_file.readline()
|
||||
gmsh_file.readline()
|
||||
gmsh_file.readline() # $EndMeshFormat
|
||||
|
||||
gmsh_file.readline() # $Nodes
|
||||
|
||||
Reference in New Issue
Block a user