[1] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt, pi |
---|
| 5 | |
---|
| 6 | from generic_boundary_conditions import * |
---|
| 7 | from config import epsilon |
---|
| 8 | from Numeric import allclose, array |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | class TestCase(unittest.TestCase): |
---|
| 12 | def setUp(self): |
---|
| 13 | pass |
---|
| 14 | #print " Setting up" |
---|
| 15 | |
---|
| 16 | def tearDown(self): |
---|
| 17 | pass |
---|
| 18 | #print " Tearing down" |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | def test_generic(self): |
---|
| 22 | b = Boundary() |
---|
| 23 | |
---|
| 24 | try: |
---|
| 25 | b.evaluate() |
---|
| 26 | except: |
---|
| 27 | pass |
---|
| 28 | else: |
---|
| 29 | raise 'Should have raised exception' |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | def test_dirichlet_empty(self): |
---|
| 33 | |
---|
| 34 | try: |
---|
| 35 | Bd = Dirichlet_boundary() |
---|
| 36 | except: |
---|
| 37 | pass |
---|
| 38 | else: |
---|
| 39 | raise 'Should have raised exception' |
---|
| 40 | |
---|
| 41 | def test_dirichlet(self): |
---|
| 42 | x = [3.14,0,0.1] |
---|
| 43 | Bd = Dirichlet_boundary(x) |
---|
| 44 | |
---|
| 45 | q = Bd.evaluate() |
---|
| 46 | assert allclose(q, x) |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | def test_transmissive(self): |
---|
| 50 | from domain import Domain |
---|
| 51 | from quantity import Quantity |
---|
| 52 | |
---|
| 53 | a = [0.0, 0.0] |
---|
| 54 | b = [0.0, 2.0] |
---|
| 55 | c = [2.0,0.0] |
---|
| 56 | d = [0.0, 4.0] |
---|
| 57 | e = [2.0, 2.0] |
---|
| 58 | f = [4.0,0.0] |
---|
| 59 | |
---|
| 60 | points = [a, b, c, d, e, f] |
---|
| 61 | |
---|
| 62 | #bac, bce, ecf, dbe |
---|
| 63 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 64 | |
---|
| 65 | domain = Domain(points, elements) |
---|
| 66 | domain.check_integrity() |
---|
| 67 | |
---|
| 68 | domain.conserved_quantities = ['level', 'ymomentum'] |
---|
| 69 | domain.quantities['level'] =\ |
---|
| 70 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
| 71 | [0,0,9], [-6, 3, 3]]) |
---|
| 72 | |
---|
| 73 | domain.quantities['ymomentum'] =\ |
---|
| 74 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
| 75 | [0,0,9], [-6, 3, 3]]) |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | domain.check_integrity() |
---|
| 79 | |
---|
| 80 | #Test transmissve bdry |
---|
| 81 | try: |
---|
| 82 | T = Transmissive_boundary() |
---|
| 83 | except: |
---|
| 84 | pass |
---|
| 85 | else: |
---|
| 86 | raise 'Should have raised exception' |
---|
| 87 | |
---|
| 88 | T = Transmissive_boundary(domain) |
---|
| 89 | |
---|
| 90 | from config import default_boundary_tag |
---|
| 91 | domain.set_boundary( {default_boundary_tag: T} ) |
---|
| 92 | |
---|
| 93 | assert len(domain.boundary) == len(domain.boundary_segments) |
---|
| 94 | assert len(domain.boundary_objects) == len(domain.boundary_segments) |
---|
| 95 | |
---|
| 96 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
| 97 | |
---|
| 98 | assert allclose(q, [1.5, 2.5]) |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | def test_fileboundary(self): |
---|
| 102 | """Test that boundary values can be read from file and interpolated |
---|
| 103 | """ |
---|
| 104 | |
---|
| 105 | from domain import Domain |
---|
| 106 | from quantity import Quantity |
---|
| 107 | import time, os |
---|
| 108 | from math import sin, pi |
---|
| 109 | from config import time_format |
---|
| 110 | |
---|
| 111 | a = [0.0, 0.0] |
---|
| 112 | b = [0.0, 2.0] |
---|
| 113 | c = [2.0,0.0] |
---|
| 114 | d = [0.0, 4.0] |
---|
| 115 | e = [2.0, 2.0] |
---|
| 116 | f = [4.0,0.0] |
---|
| 117 | |
---|
| 118 | points = [a, b, c, d, e, f] |
---|
| 119 | |
---|
| 120 | #bac, bce, ecf, dbe |
---|
| 121 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 122 | |
---|
| 123 | domain = Domain(points, elements) |
---|
| 124 | domain.conserved_quantities = ['level', 'ymomentum'] |
---|
| 125 | domain.quantities['level'] =\ |
---|
| 126 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
| 127 | [0,0,9], [-6, 3, 3]]) |
---|
| 128 | |
---|
| 129 | domain.quantities['ymomentum'] =\ |
---|
| 130 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
| 131 | [0,0,9], [-6, 3, 3]]) |
---|
| 132 | |
---|
| 133 | domain.check_integrity() |
---|
| 134 | |
---|
| 135 | domain.check_integrity() |
---|
| 136 | |
---|
| 137 | #Write file |
---|
| 138 | filename = 'boundarytest' + str(time.time()) |
---|
| 139 | fid = open(filename, 'w') |
---|
| 140 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
| 141 | dt = 5*60 #Five minute intervals |
---|
| 142 | for i in range(10): |
---|
| 143 | t = start + i*dt |
---|
| 144 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
| 148 | fid.close() |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | F = File_boundary(domain, filename) |
---|
| 152 | |
---|
| 153 | from config import default_boundary_tag |
---|
| 154 | domain.set_boundary( {default_boundary_tag: F} ) |
---|
| 155 | |
---|
| 156 | domain.time = 5*30/2 #A quarter way through first step |
---|
| 157 | q = F.evaluate() |
---|
| 158 | assert allclose(q, [1.0/4, sin(2*pi/10)/4]) |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | domain.time = 2.5*5*60 #Half way between stepe 2 and 3 |
---|
| 162 | q = F.evaluate() |
---|
| 163 | assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2]) |
---|
| 164 | |
---|
| 165 | os.remove(filename) |
---|
| 166 | |
---|
| 167 | #------------------------------------------------------------- |
---|
| 168 | if __name__ == "__main__": |
---|
| 169 | suite = unittest.makeSuite(TestCase,'test') |
---|
| 170 | runner = unittest.TextTestRunner() |
---|
| 171 | runner.run(suite) |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|