Browse Source

working on adding 3D baker. also starting to massage together a good baker method test (2D), and added a stub file for 3D testing

Stephen Mardson McQuay 9 years ago
parent
commit
f4b2c95cf5
5 changed files with 152 additions and 20 deletions
  1. 14
    1
      bin/driver-random.py
  2. 56
    15
      lib/baker.py
  3. 5
    3
      test/baker.test.py
  4. 76
    0
      test/baker3d.test.py
  5. 1
    1
      tools/multi/smp.py

+ 14
- 1
bin/driver-random.py View File

@@ -35,9 +35,22 @@ if __name__ == '__main__':
35 35
   print >> sys.stderr, "wrote output to %s" % options.output
36 36
 
37 37
   errors = []
38
+  success = 0
38 39
   for x in mesh_dest.points:
39
-    (final, exact) = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose)
40
+    lin, error, final = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose)
41
+    exact = exact_func(x[0], x[1])
42
+    if np.abs(exact - final) < np.abs(exact - lin):
43
+      success += 1
44
+
45
+    if options.verbose:
46
+      print "current point : %s" % x
47
+      print "exact         : %0.4f" % exact
48
+      print "qlin          : %0.4f" % lin
49
+      print "q_final       : %0.4f" % final
50
+      print "qlinerr       : %0.4f" % (exact - lin,)
51
+      print "q_final_err   : %0.4f" % (exact - final,)
40 52
     cur_error = np.abs(final - exact)
41 53
     errors.append(cur_error)
42 54
 
43 55
   print rms(errors)
56
+  print "%s of %s won" % (success, options.destination_total)

+ 56
- 15
lib/baker.py View File

@@ -16,11 +16,14 @@ def get_phis(X, r):
16 16
 
17 17
   # baker: eq 7
18 18
   A = np.array([
19
-                [1,   1,   1  ],
19
+                [      1,       1,       1],
20 20
                 [r[0][0], r[1][0], r[2][0]],
21 21
                 [r[0][1], r[1][1], r[2][1]],
22 22
                 ])
23
-  b = np.array([1, X[0], X[1]])
23
+  b = np.array([    1,
24
+                  X[0],
25
+                  X[1]
26
+              ])
24 27
   try:
25 28
     phi = np.linalg.solve(A,b)
26 29
   except:
@@ -29,6 +32,39 @@ def get_phis(X, r):
29 32
 
30 33
   return phi
31 34
 
35
+def get_phis_3D(X, r):
36
+  """
37
+    The get_phis function is used to get barycentric coordonites for a point on a triangle.
38
+
39
+    X -- the destination point (3D)
40
+         X = [0,0,0]
41
+    r -- the four points that make up the tetrahedron (3D)
42
+         r = [[-1, -1], [0, 2], [1, -1]]
43
+
44
+    this will return [0.333, 0.333, 0.333]
45
+  """
46
+
47
+  # baker: eq 7
48
+  A = np.array([
49
+                [      1,       1,       1,       1 ],
50
+                [r[0][0], r[1][0], r[2][0], r[3][0]],
51
+                [r[0][1], r[1][1], r[2][1], r[3][1]],
52
+                [r[0][2], r[1][2], r[2][2], r[3][2]],
53
+                ])
54
+  b = np.array([    1,
55
+                  X[0],
56
+                  X[1],
57
+                  X[2]
58
+              ])
59
+  try:
60
+    phi = np.linalg.solve(A,b)
61
+  except:
62
+    print >> sys.stderr, "warning: calculation of phis yielded a linearly dependant system"
63
+    phi = np.dot(np.linalg.pinv(A), b)
64
+
65
+  return phi
66
+
67
+
32 68
 def qlinear(X, r, q):
33 69
   """
34 70
     this calculates the linear portion of q from X to r
@@ -42,6 +78,19 @@ def qlinear(X, r, q):
42 78
   qlin = sum([q_i * phi_i for q_i, phi_i in zip(q[:len(phis)], phis)])
43 79
   return qlin
44 80
 
81
+def qlinear_3D(X, r, q):
82
+  """
83
+    this calculates the linear portion of q from X to r
84
+
85
+    X = destination point
86
+    r = simplex points
87
+    q = CFD quantities of interest at the simplex points(r)
88
+  """
89
+
90
+  phis = get_phis_3D(X, r)
91
+  qlin = sum([q_i * phi_i for q_i, phi_i in zip(q[:len(phis)], phis)])
92
+  return qlin
93
+
45 94
 def run_baker(X, g, tree, extra_points = 3, verbose = False):
46 95
   """
47 96
     This is the main function to call to get an interpolation to X from the tree
@@ -52,6 +101,7 @@ def run_baker(X, g, tree, extra_points = 3, verbose = False):
52 101
     g -- the grid object
53 102
 
54 103
     tree -- the kdtree search object (built from the g mesh)
104
+
55 105
   """
56 106
 
57 107
   (dist, indicies) = tree.query(X, 3 + extra_points)
@@ -59,8 +109,9 @@ def run_baker(X, g, tree, extra_points = 3, verbose = False):
59 109
   nn = [g.points[i] for i in indicies]
60 110
   nq = [g.q[i]      for i in indicies]
61 111
 
62
-  phi = get_phis(X, nn[:3])
63
-  qlin =  nq[0] * phi[0] + nq[1] * phi[1] + nq[2] * phi[2]
112
+  # calculate values only for the triangle
113
+  phi  = get_phis(X, nn[:3])
114
+  qlin = qlinear(X, nn[:3], nq[:3])# nq[0] * phi[0] + nq[1] * phi[1] + nq[2] * phi[2]
64 115
 
65 116
   error_term = 0.0
66 117
 
@@ -91,16 +142,6 @@ def run_baker(X, g, tree, extra_points = 3, verbose = False):
91 142
                 + b * phi[1] * phi[2]\
92 143
                 + c * phi[2] * phi[0]
93 144
 
94
-  exact = exact_func(X[0], X[1])
95 145
   q_final = qlin + error_term
96 146
 
97
-  if verbose:
98
-    print "current point : %s" % X
99
-    print "exact         : %0.4f" % exact
100
-    print "qlin          : %0.4f" % qlin
101
-    print "qlinerr       : %0.4f" % np.abs(exact - qlin)
102
-    print "q_final       : %0.4f" % q_final
103
-    print "q_final_err   : %0.4f" % np.abs(exact - q_final)
104
-    print
105
-
106
-  return (q_final, exact)
147
+  return qlin, error_term, q_final

+ 5
- 3
test/baker.test.py View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 import unittest
4 4
 import baker
5
+import grid
5 6
 
6 7
 import numpy as np
7 8
 import scipy.spatial
@@ -62,11 +63,12 @@ class TestSequenceFunctions(unittest.TestCase):
62 63
                    [-1,-1], # 8
63 64
                  ]
64 65
     q = [1, 0, 0, 0, 0, 0, 0, 0, 0]
65
-
66
+    mesh = grid.grid(all_points, q)
66 67
     tree = scipy.spatial.KDTree(all_points)
67
-    baker.run_baker(X, 
68
+    (final, exact) = baker.run_baker(X, mesh, tree)
69
+    print final, exact
68 70
     result = 3
69
-    right_answer = 5
71
+    right_answer = 3
70 72
 
71 73
     self.assertEqual(result, right_answer)
72 74
 

+ 76
- 0
test/baker3d.test.py View File

@@ -0,0 +1,76 @@
1
+#!/usr/bin/python
2
+
3
+import unittest
4
+import baker
5
+
6
+import numpy as np
7
+import scipy.spatial
8
+
9
+class TestSequenceFunctions(unittest.TestCase):
10
+  def setUp(self):
11
+    self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
12
+    self.r = [[1,2,3], [2,2,3], [1,3,3], [1,2,9]]
13
+    self.approx_fmt = "%0.6f"
14
+
15
+  def testGetPhis(self):
16
+
17
+    X = [0,0]
18
+    r = [[-1, -1], [0, 2], [1, -1]]
19
+
20
+    result = baker.get_phis(X, r)
21
+    result = [self.approx_fmt % i for i in result]
22
+
23
+    right_answer = [self.approx_fmt % i for i in [1/3.0, 1/3.0, 1/3.0]]
24
+
25
+    for a,b in zip(result, right_answer):
26
+      self.assertEqual(a,b)
27
+
28
+  def testGetPhis2(self):
29
+
30
+    X = [0.5,0.25]
31
+    r = [[0, 0], [1, 0], [1, 1]]
32
+
33
+    result = baker.get_phis(X, r)
34
+
35
+    right_answer = [0.5, 0.25, 0.25]
36
+
37
+    for a,b in zip(result, right_answer):
38
+      self.assertEqual(a,b)
39
+
40
+  def testQlinear(self):
41
+    X = [0.5, 0.25]
42
+    r = [[0, 0], [1, 0], [1, 1]]
43
+    q = [1, 0, 0]
44
+
45
+    result = baker.qlinear(X, r, q)
46
+
47
+    right_answer = 0.5
48
+
49
+    self.assertEqual(result, right_answer)
50
+
51
+  def testRunBaker(self):
52
+    X = [0.5, 0.25]
53
+
54
+    all_points = [
55
+                   [ 0, 0], # 0
56
+                   [ 1, 0], # 1
57
+                   [ 1, 1], # 2
58
+                   [ 0, 1], # 3
59
+                   [ 1,-1], # 4
60
+                   [ 0,-1], # 5
61
+                   [-1, 1], # 6
62
+                   [-1, 0], # 7
63
+                   [-1,-1], # 8
64
+                 ]
65
+    q = [1, 0, 0, 0, 0, 0, 0, 0, 0]
66
+
67
+    tree = scipy.spatial.KDTree(all_points)
68
+    baker.run_baker(X, 
69
+    result = 3
70
+    right_answer = 5
71
+
72
+    self.assertEqual(result, right_answer)
73
+
74
+if __name__ == '__main__':
75
+  suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
76
+  unittest.TextTestRunner(verbosity=2).run(suite)

+ 1
- 1
tools/multi/smp.py View File

@@ -6,7 +6,7 @@ def f(l, i, d):
6 6
   d['a'] = 'shutup'
7 7
   print 'hello world', i, d
8 8
   j = 0.0
9
-  for i in xrange(1000000):
9
+  for i in xrange(10000000):
10 10
     j = j + j/2.0
11 11
 
12 12
 if __name__ == '__main__':

Loading…
Cancel
Save