smbinterp/lib/grid.py

78 lines
1.7 KiB
Python
Raw Normal View History

2009-12-27 09:48:27 -08:00
#!/usr/bin/python
import sys
import numpy as np
import scipy.spatial
def exact_func(x, y):
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
return np.sin(x * np.pi) * np.cos(y * np.pi)
class grid(object):
def __init__(self, points, q):
self.points = np.array(points)
self.q = np.array(q)
def __str__(self):
r = ''
assert( len(self.points) == len(self.q) )
for i in xrange(len(self.points)):
r += "%r: %0.4f\n" % ( self.points[i], self.q[i] )
return r
class simple_rect_grid(grid):
def __init__(self, xres = 5, yres = 5):
xmin = -1.0
xmax = 1.0
xspan = xmax - xmin
xdel = xspan / float(xres - 1)
ymin = -1.0
ymay = 1.0
yspan = ymay - ymin
ydel = yspan / float(yres - 1)
self.points = []
self.q = []
for x in xrange(xres):
cur_x = xmin + (x * xdel)
for y in xrange(yres):
cur_y = ymin + (y * ydel)
self.points.append([cur_x, cur_y])
self.q.append(exact_func(cur_x, cur_y))
self.points = np.array(self.points)
self.q = np.array(self.q)
def for_qhull(self):
r = '2\n'
r += '%d\n' % len(self.points)
for p in self.points:
r += "%f %f\n" % (p[0], p[1])
return r
class simple_random_grid(simple_rect_grid):
def __init__(self, num_points = 10):
self.points = []
self.q = []
r = np.random
for i in xrange(num_points):
cur_x = r.rand()
cur_y = r.rand()
self.points.append([cur_x, cur_y])
self.q.append(exact_func(cur_x, cur_y))
self.points = np.array(self.points)
self.q = np.array(self.q)
if __name__ == '__main__':
g = simple_random_grid(100)
print g.for_qhull()