diff --git a/bin/driver-random.py b/bin/driver-random.py deleted file mode 100755 index ddaf93a..0000000 --- a/bin/driver-random.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/python - -import sys -from optparse import OptionParser - -import numpy as np -import scipy.spatial - -from grid import simple_random_grid, exact_func -import baker - -def rms(errors): - r = 0.0 - for i in errors: - r += np.power(i, 2) - r = np.sqrt(r / len(errors)) - return r - -if __name__ == '__main__': - parser = OptionParser() - parser.add_option("-o", "--output-file", dest="output", type='str', default = '/tmp/for_qhull.txt', help = "qhull output file") - parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points") - parser.add_option("-s", "--source-total", dest="source_total", type='int', default = 100, help = "total number of source points") - parser.add_option("-d", "--destination-total",dest="destination_total",type='int', default = 100, help = "total number of destination points") - parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity") - - (options, args) = parser.parse_args() - print >> sys.stderr, options - - mesh_source = simple_random_grid(options.source_total) - tree = scipy.spatial.KDTree(mesh_source.points) - mesh_dest = simple_random_grid(options.destination_total) - - open(options.output, 'w').write(mesh_source.for_qhull()) - print >> sys.stderr, "wrote output to %s" % options.output - - errors = [] - success = 0 - for x in mesh_dest.points: - lin, error, final = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose) - exact = exact_func(x[0], x[1]) - if np.abs(exact - final) < np.abs(exact - lin): - success += 1 - - if options.verbose: - print "current point : %s" % x - print "exact : %0.4f" % exact - print "qlin : %0.4f" % lin - print "q_final : %0.4f" % final - print "qlinerr : %0.4f" % (exact - lin,) - print "q_final_err : %0.4f" % (exact - final,) - cur_error = np.abs(final - exact) - errors.append(cur_error) - - print rms(errors) - print "%s of %s won" % (success, options.destination_total) diff --git a/bin/driver-structured.py b/bin/driver-structured.py deleted file mode 100755 index b547b05..0000000 --- a/bin/driver-structured.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/python - -import sys -from optparse import OptionParser - -import numpy as np -import scipy.spatial - -from grid import simple_rect_grid, exact_func -import baker -from tools import rms - - -if __name__ == '__main__': - parser = OptionParser() - parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points") - parser.add_option("-s", "--source-density", dest="source", type='int', default = 11, help = "resolution of source mesh") - parser.add_option("-d", "--dest-density", dest="dest", type='int', default = 11, help = "resolution of dest mesh") - parser.add_option("-t", "--random-total", dest="random_total", type='int', default = 100, help = "total number of random points") - parser.add_option("-r", "--random-dest", action = 'store_true', default = False, help = "verbosity") - parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity") - - (options, args) = parser.parse_args() - print >> sys.stderr, options - - mesh_source = simple_rect_grid(options.source, options.source) - tree = scipy.spatial.KDTree(mesh_source.points) - - mesh_dest = simple_rect_grid(options.dest, options.dest) - - if options.random_dest: - x = [] - for i in xrange(options.random_total): - rx = np.random.rand() * 2 - 1 - ry = np.random.rand() * 2 - 1 - x.append([rx, ry]) - mesh_dest.points = np.array(x) - - errors = [] - for x in mesh_dest.points: - (final, exact) = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose) - cur_error = np.abs(final - exact) - errors.append(cur_error) - - print rms(errors) diff --git a/bin/driver.py b/bin/driver.py new file mode 100755 index 0000000..0bbc835 --- /dev/null +++ b/bin/driver.py @@ -0,0 +1,52 @@ +#!/usr/bin/python + +import sys +from optparse import OptionParser + +import numpy as np +import scipy.spatial + +import baker +from tools import rms, get_mesh, exact_func + + +if __name__ == '__main__': + parser = OptionParser() + parser.add_option("-o", "--output-file", dest="output", type='str', default = '/tmp/for_qhull.txt', help = "qhull output file") + parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points") + + parser.add_option("-s", "--source-total", dest="source_total", type='int', default = 100, help = "total number of source points for random, resolution for structured") + parser.add_option("-d", "--destination-total", dest="destination_total", type='int', default = 100, help = "total number of destination points, resolution for structured") + + parser.add_option("-r", "--structured", action = 'store_true', default = False, help = "use a structured grid instead of random point cloud") + parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity") + + (options, args) = parser.parse_args() + print >> sys.stderr, options + + mesh_source, mesh_dest = get_mesh(options.source_total, options.destination_total, options.structured) + + tree = scipy.spatial.KDTree(mesh_source.points) + open(options.output, 'w').write(mesh_source.for_qhull()) + print >> sys.stderr, "wrote source mesh output to %s" % options.output + + errors = [] + success = 0 + for x in mesh_dest.points: + lin, error, final = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose) + exact = exact_func(x[0], x[1]) + if np.abs(exact - final) < np.abs(exact - lin): + success += 1 + + if options.verbose: + print "current point : %s" % x + print "exact : %0.4f" % exact + print "qlin : %0.4f" % lin + print "q_final : %0.4f" % final + print "qlinerr : %0.4f" % (exact - lin,) + print "q_final_err : %0.4f" % (exact - final,) + cur_error = np.abs(final - exact) + errors.append(cur_error) + + print rms(errors) + print "%s of %s won" % (success, options.destination_total) diff --git a/data/geomview.txt b/data/geomview.txt new file mode 100644 index 0000000..075f582 --- /dev/null +++ b/data/geomview.txt @@ -0,0 +1,42 @@ +{appearance {linewidth 3} LIST # | qdelaunay Qt GD2 +{ OFF 3 1 1 # f24 + -1 -8.01e-15 0 +-8.01e-15 -1 0 +-8.01e-15 -8.01e-15 0 +3 0 1 2 0.7071 0.7071 0 1.0 } +{ OFF 3 1 1 # f25 +-8.01e-15 -1 0 + -1 -8.01e-15 0 + -1 -1 0 +3 0 1 2 0.7071 0.7071 0 1.0 } +{ OFF 3 1 1 # f28 +8.01e-15 -1 0 + 1 -8.01e-15 0 +8.01e-15 -8.01e-15 0 +3 0 1 2 0.9659 0.2588 0 1.0 } +{ OFF 3 1 1 # f29 + 1 -8.01e-15 0 +8.01e-15 -1 0 + 1 -1 0 +3 0 1 2 0.9659 0.2588 0 1.0 } +{ OFF 3 1 1 # f32 +-8.01e-15 1 0 + -1 8.01e-15 0 +-8.01e-15 8.01e-15 0 +3 0 1 2 0.2588 0.9659 0 1.0 } +{ OFF 3 1 1 # f33 + -1 8.01e-15 0 +-8.01e-15 1 0 + -1 1 0 +3 0 1 2 0.2588 0.9659 0 1.0 } +{ OFF 3 1 1 # f36 + 1 8.01e-15 0 +8.01e-15 1 0 +8.01e-15 8.01e-15 0 +3 0 1 2 0.7071 0.7071 0 1.0 } +{ OFF 3 1 1 # f38 +8.01e-15 1 0 + 1 8.01e-15 0 + 1 1 0 +3 0 1 2 0.7071 0.7071 0 1.0 } +} diff --git a/lib/grid.py b/lib/grid.py index 12aa0c5..066d64a 100755 --- a/lib/grid.py +++ b/lib/grid.py @@ -4,9 +4,7 @@ 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) +from tools import exact_func class grid(object): diff --git a/lib/tools.py b/lib/tools.py index 65c3b66..8a99e52 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -1,6 +1,13 @@ import numpy as np +import grid +class smberror(Exception): + def __init__(self, val): + self.value = val + def __str__(self): + return repr(self.value) + def rms(errors): """ root mean square calculation @@ -10,3 +17,23 @@ def rms(errors): r += np.power(i, 2) r = np.sqrt(r / len(errors)) return r + +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) + + +def get_mesh(source, destination, use_structured_grid = False): + mesh_source = None + mesh_dest = None + if use_structured_grid: + mesh_source = grid.simple_rect_grid(source, source) + mesh_dest = grid.simple_rect_grid(destination, destination) + else: + mesh_source = grid.simple_random_grid(source) + mesh_dest = grid.simple_random_grid(destination) + + if not (mesh_dest and mesh_source): + raise smberror('problem creating mesh objects') + else: + return mesh_source, mesh_dest diff --git a/test/qhull.test.py b/test/qhull.test.py index bd0b0a1..5a12ba0 100755 --- a/test/qhull.test.py +++ b/test/qhull.test.py @@ -1,33 +1,29 @@ #!/usr/bin/python + +import random +import unittest import delaunay -import subprocess -QFILE = '/tmp/forQhull.txt' +class TestSequenceFunctions(unittest.TestCase): + def setUp(self): + self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]] -l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]] -qdelauney_file = open(QFILE, 'w') + def testQhull(self): + dt = delaunay.Triangulation(self.l) + answer = [ + [4,1,3], + [1,5,0], + [5,1,4], + [7,3,6], + [7,4,3], + [7,5,4], + [5,7,8], + ] -qdelauney_file.write("%d\n" % len(l[0])) -qdelauney_file.write("%d\n" % len(l)) -for i in l: - qdelauney_file.write("%0.3f %0.3f\n" % (i[0], i[1])) + self.assertEqual(dt.indices, answer) -dt = delaunay.Triangulation(l) -print dt.indices - -answer = [ - [4,1,3], - [1,5,0], - [5,1,4], - [7,3,6], - [7,4,3], - [7,5,4], - [5,7,8], - ] - -print answer == dt.indices -print dt.indices - -print "now run /usr/bin/qdelaunay Qt i < %s" % QFILE +if __name__ == '__main__': + suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) + unittest.TextTestRunner(verbosity=5).run(suite) diff --git a/test/utest.py b/test/utest.py deleted file mode 100755 index 5a12ba0..0000000 --- a/test/utest.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/python - - -import random -import unittest -import delaunay - -class TestSequenceFunctions(unittest.TestCase): - def setUp(self): - self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]] - - - def testQhull(self): - dt = delaunay.Triangulation(self.l) - answer = [ - [4,1,3], - [1,5,0], - [5,1,4], - [7,3,6], - [7,4,3], - [7,5,4], - [5,7,8], - ] - - self.assertEqual(dt.indices, answer) - -if __name__ == '__main__': - suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) - unittest.TextTestRunner(verbosity=5).run(suite)