[1018] | 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 Test_Generic_Boundary_Conditions(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 = ['stage', 'ymomentum'] |
---|
| 69 | domain.quantities['stage'] =\ |
---|
| 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 | |
---|
| 94 | #FIXME: should not necessarily be true always. |
---|
| 95 | #E.g. with None as a boundary object. |
---|
| 96 | assert len(domain.boundary) == len(domain.boundary_objects) |
---|
| 97 | |
---|
| 98 | q = T.evaluate(0, 2) #Vol=0, edge=2 |
---|
| 99 | |
---|
| 100 | assert allclose(q, [1.5, 2.5]) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | def test_fileboundary_time_only(self): |
---|
| 104 | """Test that boundary values can be read from file and interpolated |
---|
[1835] | 105 | This is using the .tms file format |
---|
[1018] | 106 | """ |
---|
| 107 | |
---|
| 108 | from domain import Domain |
---|
| 109 | from quantity import Quantity |
---|
| 110 | import time, os |
---|
| 111 | from math import sin, pi |
---|
| 112 | from config import time_format |
---|
| 113 | |
---|
| 114 | a = [0.0, 0.0] |
---|
| 115 | b = [0.0, 2.0] |
---|
| 116 | c = [2.0, 0.0] |
---|
| 117 | d = [0.0, 4.0] |
---|
| 118 | e = [2.0, 2.0] |
---|
| 119 | f = [4.0, 0.0] |
---|
| 120 | |
---|
| 121 | points = [a, b, c, d, e, f] |
---|
| 122 | |
---|
| 123 | #bac, bce, ecf, dbe |
---|
| 124 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 125 | |
---|
| 126 | domain = Domain(points, elements) |
---|
| 127 | domain.conserved_quantities = ['stage', 'ymomentum'] |
---|
| 128 | domain.quantities['stage'] =\ |
---|
| 129 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
| 130 | [0,0,9], [-6, 3, 3]]) |
---|
| 131 | |
---|
| 132 | domain.quantities['ymomentum'] =\ |
---|
| 133 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
| 134 | [0,0,9], [-6, 3, 3]]) |
---|
| 135 | |
---|
| 136 | domain.check_integrity() |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | #Write file |
---|
| 140 | filename = 'boundarytest' + str(time.time()) |
---|
[1671] | 141 | fid = open(filename + '.txt', 'w') |
---|
[1018] | 142 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
| 143 | dt = 5*60 #Five minute intervals |
---|
| 144 | for i in range(10): |
---|
| 145 | t = start + i*dt |
---|
| 146 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
| 147 | |
---|
| 148 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
| 149 | fid.close() |
---|
| 150 | |
---|
| 151 | |
---|
[1671] | 152 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
| 153 | |
---|
[1835] | 154 | from data_manager import timefile2netcdf |
---|
| 155 | timefile2netcdf(filename, quantity_names = ['stage', 'ymomentum']) |
---|
[1671] | 156 | |
---|
[1018] | 157 | |
---|
| 158 | |
---|
[1835] | 159 | F = File_boundary(filename + '.tms', domain) |
---|
[1671] | 160 | |
---|
| 161 | |
---|
| 162 | os.remove(filename + '.txt') |
---|
[1835] | 163 | os.remove(filename + '.tms') |
---|
[1671] | 164 | |
---|
| 165 | |
---|
[2154] | 166 | |
---|
| 167 | |
---|
[1018] | 168 | #Check that midpoint coordinates at boundary are correctly computed |
---|
| 169 | assert allclose( F.midpoint_coordinates, |
---|
[2154] | 170 | [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0], |
---|
| 171 | [3.0, 1.0], [1.0, 3.0], [0.0, 3.0]]) |
---|
[1018] | 172 | |
---|
| 173 | #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0]) |
---|
| 174 | #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0]) |
---|
| 175 | #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0]) |
---|
| 176 | #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0]) |
---|
| 177 | #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0]) |
---|
| 178 | #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0]) |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | #Check time interpolation |
---|
| 182 | from config import default_boundary_tag |
---|
| 183 | domain.set_boundary( {default_boundary_tag: F} ) |
---|
| 184 | |
---|
| 185 | domain.time = 5*30/2 #A quarter way through first step |
---|
| 186 | q = F.evaluate() |
---|
| 187 | assert allclose(q, [1.0/4, sin(2*pi/10)/4]) |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | domain.time = 2.5*5*60 #Half way between steps 2 and 3 |
---|
| 191 | q = F.evaluate() |
---|
| 192 | assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2]) |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | def test_fileboundary_exception(self): |
---|
[1671] | 197 | """Test that boundary object complains if number of |
---|
[1018] | 198 | conserved quantities are wrong |
---|
| 199 | """ |
---|
| 200 | |
---|
| 201 | from domain import Domain |
---|
| 202 | from quantity import Quantity |
---|
| 203 | import time, os |
---|
| 204 | from math import sin, pi |
---|
| 205 | from config import time_format |
---|
| 206 | |
---|
| 207 | a = [0.0, 0.0] |
---|
| 208 | b = [0.0, 2.0] |
---|
| 209 | c = [2.0,0.0] |
---|
| 210 | d = [0.0, 4.0] |
---|
| 211 | e = [2.0, 2.0] |
---|
| 212 | f = [4.0,0.0] |
---|
| 213 | |
---|
| 214 | points = [a, b, c, d, e, f] |
---|
| 215 | |
---|
| 216 | #bac, bce, ecf, dbe |
---|
| 217 | elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
| 218 | |
---|
| 219 | domain = Domain(points, elements) |
---|
| 220 | domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum'] |
---|
| 221 | domain.quantities['stage'] =\ |
---|
| 222 | Quantity(domain, [[1,2,3], [5,5,5], |
---|
| 223 | [0,0,9], [-6, 3, 3]]) |
---|
| 224 | |
---|
| 225 | domain.quantities['xmomentum'] =\ |
---|
| 226 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
| 227 | [0,0,9], [-6, 3, 3]]) |
---|
| 228 | domain.quantities['ymomentum'] =\ |
---|
| 229 | Quantity(domain, [[2,3,4], [5,5,5], |
---|
| 230 | [0,0,9], [-6, 3, 3]]) |
---|
| 231 | |
---|
| 232 | domain.check_integrity() |
---|
| 233 | |
---|
| 234 | #Write file (with only two values) |
---|
| 235 | filename = 'boundarytest' + str(time.time()) |
---|
[1671] | 236 | fid = open(filename + '.txt', 'w') |
---|
[1018] | 237 | start = time.mktime(time.strptime('2000', '%Y')) |
---|
| 238 | dt = 5*60 #Five minute intervals |
---|
| 239 | for i in range(10): |
---|
| 240 | t = start + i*dt |
---|
| 241 | t_string = time.strftime(time_format, time.gmtime(t)) |
---|
| 242 | |
---|
| 243 | fid.write('%s,%f %f\n' %(t_string, 1.0*i, sin(i*2*pi/10))) |
---|
| 244 | fid.close() |
---|
| 245 | |
---|
| 246 | |
---|
[1671] | 247 | #Convert ASCII file to NetCDF (Which is what we really like!) |
---|
[1835] | 248 | from data_manager import timefile2netcdf |
---|
| 249 | timefile2netcdf(filename, quantity_names = ['stage', 'xmomentum']) |
---|
[1671] | 250 | |
---|
| 251 | |
---|
[1018] | 252 | try: |
---|
[1835] | 253 | F = File_boundary(filename + '.tms', domain) |
---|
[1671] | 254 | except: |
---|
[1018] | 255 | pass |
---|
| 256 | else: |
---|
[1671] | 257 | raise 'Should have raised an exception' |
---|
| 258 | |
---|
| 259 | os.remove(filename + '.txt') |
---|
[1835] | 260 | os.remove(filename + '.tms') |
---|
[1018] | 261 | |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | |
---|
[1671] | 265 | |
---|
[1018] | 266 | #------------------------------------------------------------- |
---|
| 267 | if __name__ == "__main__": |
---|
| 268 | suite = unittest.makeSuite(Test_Generic_Boundary_Conditions,'test') |
---|
| 269 | runner = unittest.TextTestRunner() |
---|
| 270 | runner.run(suite) |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|