[6145] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt |
---|
| 5 | |
---|
| 6 | from domain import * |
---|
| 7 | from region import * |
---|
| 8 | #from anuga.config import epsilon |
---|
| 9 | |
---|
[6304] | 10 | import numpy as num |
---|
[6145] | 11 | |
---|
| 12 | |
---|
| 13 | """ |
---|
| 14 | This is what the mesh in these tests look like; |
---|
| 15 | |
---|
| 16 | 3---7 |
---|
| 17 | |5 /| |
---|
| 18 | | /6| |
---|
| 19 | 2---6 |
---|
| 20 | |3 /| |
---|
| 21 | | /2| |
---|
| 22 | 1---5 |
---|
| 23 | |1 /| |
---|
| 24 | | /0| |
---|
| 25 | 0---4 |
---|
| 26 | """ |
---|
| 27 | |
---|
| 28 | def add_x_y(x, y): |
---|
| 29 | return x+y |
---|
| 30 | |
---|
| 31 | def give_me_23(x, y): |
---|
| 32 | return 23.0 |
---|
| 33 | |
---|
| 34 | class Test_Region(unittest.TestCase): |
---|
| 35 | def setUp(self): |
---|
| 36 | pass |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | def tearDown(self): |
---|
| 40 | pass |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | def test_region_tags(self): |
---|
[6304] | 44 | """get values based on triangle lists.""" |
---|
| 45 | |
---|
[6145] | 46 | from mesh_factory import rectangular |
---|
| 47 | from shallow_water import Domain |
---|
| 48 | |
---|
| 49 | #Create basic mesh |
---|
| 50 | points, vertices, boundary = rectangular(1, 3) |
---|
| 51 | |
---|
| 52 | #Create shallow water domain |
---|
| 53 | domain = Domain(points, vertices, boundary) |
---|
[6304] | 54 | domain.build_tagged_elements_dictionary({'bottom': [0,1], |
---|
| 55 | 'top': [4,5], |
---|
| 56 | 'all': [0,1,2,3,4,5]}) |
---|
[6145] | 57 | |
---|
| 58 | #Set friction |
---|
| 59 | manning = 0.07 |
---|
| 60 | domain.set_quantity('friction', manning) |
---|
| 61 | |
---|
| 62 | a = Set_region('bottom', 'friction', 0.09) |
---|
| 63 | b = Set_region('top', 'friction', 1.0) |
---|
| 64 | domain.set_region([a, b]) |
---|
| 65 | |
---|
[6304] | 66 | expected = [[ 0.09, 0.09, 0.09], |
---|
| 67 | [ 0.09, 0.09, 0.09], |
---|
| 68 | [ 0.07, 0.07, 0.07], |
---|
| 69 | [ 0.07, 0.07, 0.07], |
---|
| 70 | [ 1.0, 1.0, 1.0], |
---|
| 71 | [ 1.0, 1.0, 1.0]] |
---|
| 72 | msg = ("\ndomain.quantities['friction']=%s\nexpected value=%s" |
---|
| 73 | % (str(domain.quantities['friction'].get_values()), |
---|
| 74 | str(expected))) |
---|
| 75 | assert num.allclose(domain.quantities['friction'].get_values(), |
---|
| 76 | expected), msg |
---|
| 77 | |
---|
[6145] | 78 | #c = Add_Value_To_region('all', 'friction', 10.0) |
---|
| 79 | domain.set_region(Add_value_to_region('all', 'friction', 10.0)) |
---|
| 80 | #print domain.quantities['friction'].get_values() |
---|
| 81 | assert num.allclose(domain.quantities['friction'].get_values(), |
---|
| 82 | [[ 10.09, 10.09, 10.09], |
---|
| 83 | [ 10.09, 10.09, 10.09], |
---|
| 84 | [ 10.07, 10.07, 10.07], |
---|
| 85 | [ 10.07, 10.07, 10.07], |
---|
| 86 | [ 11.0, 11.0, 11.0], |
---|
| 87 | [ 11.0, 11.0, 11.0]]) |
---|
| 88 | |
---|
| 89 | # trying a function |
---|
| 90 | domain.set_region(Set_region('top', 'friction', add_x_y)) |
---|
| 91 | #print domain.quantities['friction'].get_values() |
---|
| 92 | assert num.allclose(domain.quantities['friction'].get_values(), |
---|
| 93 | [[ 10.09, 10.09, 10.09], |
---|
| 94 | [ 10.09, 10.09, 10.09], |
---|
| 95 | [ 10.07, 10.07, 10.07], |
---|
| 96 | [ 10.07, 10.07, 10.07], |
---|
| 97 | [ 5./3, 2.0, 2./3], |
---|
| 98 | [ 1.0, 2./3, 2.0]]) |
---|
| 99 | |
---|
| 100 | domain.set_quantity('elevation', 10.0) |
---|
| 101 | domain.set_quantity('stage', 10.0) |
---|
| 102 | domain.set_region(Add_value_to_region('top', 'stage', 1.0,initial_quantity='elevation')) |
---|
| 103 | #print domain.quantities['stage'].get_values() |
---|
| 104 | assert num.allclose(domain.quantities['stage'].get_values(), |
---|
| 105 | [[ 10., 10., 10.], |
---|
| 106 | [ 10., 10., 10.], |
---|
| 107 | [ 10., 10., 10.], |
---|
| 108 | [ 10., 10., 10.], |
---|
| 109 | [ 11.0, 11.0, 11.0], |
---|
| 110 | [ 11.0, 11.0, 11.0]]) |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | domain.set_quantity('elevation', 10.0) |
---|
| 114 | domain.set_quantity('stage', give_me_23) |
---|
| 115 | #this works as well, (is cleaner, but doesn't work for regions) |
---|
| 116 | #domain.set_quantity('stage', |
---|
| 117 | # domain.quantities['stage'].vertex_values+ \ |
---|
| 118 | # domain.quantities['elevation'].vertex_values) |
---|
| 119 | domain.set_region(Add_quantities('top', 'elevation','stage')) |
---|
| 120 | #print domain.quantities['stage'].get_values() |
---|
| 121 | assert num.allclose(domain.quantities['elevation'].get_values(), |
---|
| 122 | [[ 10., 10., 10.], |
---|
| 123 | [ 10., 10., 10.], |
---|
| 124 | [ 10., 10., 10.], |
---|
| 125 | [ 10., 10., 10.], |
---|
| 126 | [ 33., 33.0, 33.], |
---|
| 127 | [ 33.0, 33., 33.]]) |
---|
| 128 | |
---|
| 129 | def test_unique_vertices(self): |
---|
[6304] | 130 | """get values based on triangle lists.""" |
---|
| 131 | |
---|
[6145] | 132 | from mesh_factory import rectangular |
---|
| 133 | from shallow_water import Domain |
---|
| 134 | |
---|
| 135 | #Create basic mesh |
---|
| 136 | points, vertices, boundary = rectangular(1, 3) |
---|
| 137 | |
---|
| 138 | #Create shallow water domain |
---|
| 139 | domain = Domain(points, vertices, boundary) |
---|
| 140 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
| 141 | 'top':[4,5], |
---|
| 142 | 'all':[0,1,2,3,4,5]}) |
---|
| 143 | |
---|
| 144 | #Set friction |
---|
| 145 | manning = 0.07 |
---|
| 146 | domain.set_quantity('friction', manning) |
---|
| 147 | |
---|
| 148 | a = Set_region('bottom', 'friction', 0.09, location = 'unique vertices') |
---|
| 149 | domain.set_region(a) |
---|
[6304] | 150 | assert num.allclose(domain.quantities['friction'].get_values(), |
---|
[6145] | 151 | [[ 0.09, 0.09, 0.09], |
---|
| 152 | [ 0.09, 0.09, 0.09], |
---|
| 153 | [ 0.09, 0.07, 0.09], |
---|
| 154 | [ 0.07, 0.09, 0.07], |
---|
| 155 | [ 0.07, 0.07, 0.07], |
---|
| 156 | [ 0.07, 0.07, 0.07]]) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | def test_unique_verticesII(self): |
---|
| 160 | """ |
---|
| 161 | get values based on triangle lists. |
---|
| 162 | """ |
---|
| 163 | from mesh_factory import rectangular |
---|
| 164 | from shallow_water import Domain |
---|
| 165 | |
---|
| 166 | #Create basic mesh |
---|
| 167 | points, vertices, boundary = rectangular(1, 3) |
---|
| 168 | |
---|
| 169 | #Create shallow water domain |
---|
| 170 | domain = Domain(points, vertices, boundary) |
---|
| 171 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
| 172 | 'top':[4,5], |
---|
| 173 | 'all':[0,1,2,3,4,5]}) |
---|
| 174 | |
---|
| 175 | #Set friction |
---|
| 176 | manning = 0.07 |
---|
| 177 | domain.set_quantity('friction', manning) |
---|
| 178 | |
---|
| 179 | domain.set_region(Add_value_to_region('bottom', 'friction', 1.0,initial_quantity='friction', location = 'unique vertices')) |
---|
| 180 | |
---|
| 181 | #print domain.quantities['friction'].get_values() |
---|
| 182 | assert num.allclose(domain.quantities['friction'].get_values(),\ |
---|
| 183 | [[ 1.07, 1.07, 1.07], |
---|
| 184 | [ 1.07, 1.07, 1.07], |
---|
| 185 | [ 1.07, 0.07, 1.07], |
---|
| 186 | [ 0.07, 1.07, 0.07], |
---|
| 187 | [ 0.07, 0.07, 0.07], |
---|
| 188 | [ 0.07, 0.07, 0.07]]) |
---|
| 189 | |
---|
| 190 | def test_unique_vertices_average_loc_vert(self): |
---|
| 191 | """ |
---|
| 192 | get values based on triangle lists. |
---|
| 193 | """ |
---|
| 194 | from mesh_factory import rectangular |
---|
| 195 | from shallow_water import Domain |
---|
| 196 | |
---|
| 197 | #Create basic mesh |
---|
| 198 | points, vertices, boundary = rectangular(1, 3) |
---|
| 199 | #Create shallow water domain |
---|
| 200 | domain = Domain(points, vertices, boundary) |
---|
| 201 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
| 202 | 'top':[4,5], |
---|
| 203 | 'not_bottom':[2,3,4,5]}) |
---|
| 204 | |
---|
| 205 | #Set friction |
---|
| 206 | domain.set_quantity('friction', add_x_y) |
---|
| 207 | av_bottom = 2.0/3.0 |
---|
| 208 | add = 60.0 |
---|
| 209 | calc_frict = av_bottom + add |
---|
| 210 | domain.set_region(Add_value_to_region('bottom', 'friction', add, |
---|
| 211 | initial_quantity='friction', |
---|
| 212 | #location='unique vertices', |
---|
| 213 | location='vertices', |
---|
| 214 | average=True |
---|
| 215 | )) |
---|
| 216 | |
---|
| 217 | #print domain.quantities['friction'].get_values() |
---|
| 218 | frict_points = domain.quantities['friction'].get_values() |
---|
[6304] | 219 | expected = [calc_frict, calc_frict, calc_frict] |
---|
| 220 | msg = ('frict_points[0]=%s\nexpected=%s' % (str(frict_points[0]), |
---|
| 221 | str(expected))) |
---|
| 222 | assert num.allclose(frict_points[0], expected), msg |
---|
| 223 | msg = ('frict_points[1]=%s\nexpected=%s' % (str(frict_points[1]), |
---|
| 224 | str(expected))) |
---|
| 225 | assert num.allclose(frict_points[1], expected), msg |
---|
[6145] | 226 | |
---|
| 227 | def test_unique_vertices_average_loc_unique_vert(self): |
---|
| 228 | """ |
---|
| 229 | get values based on triangle lists. |
---|
| 230 | """ |
---|
| 231 | from mesh_factory import rectangular |
---|
| 232 | from shallow_water import Domain |
---|
| 233 | |
---|
| 234 | #Create basic mesh |
---|
| 235 | points, vertices, boundary = rectangular(1, 3) |
---|
| 236 | #Create shallow water domain |
---|
| 237 | domain = Domain(points, vertices, boundary) |
---|
| 238 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
| 239 | 'top':[4,5], |
---|
| 240 | 'not_bottom':[2,3,4,5]}) |
---|
| 241 | |
---|
| 242 | #Set friction |
---|
| 243 | domain.set_quantity('friction', add_x_y) |
---|
| 244 | av_bottom = 2.0/3.0 |
---|
| 245 | add = 60.0 |
---|
| 246 | calc_frict = av_bottom + add |
---|
| 247 | domain.set_region(Add_value_to_region('bottom', 'friction', add, |
---|
| 248 | initial_quantity='friction', |
---|
| 249 | location='unique vertices', |
---|
| 250 | average=True |
---|
| 251 | )) |
---|
| 252 | |
---|
| 253 | #print domain.quantities['friction'].get_values() |
---|
| 254 | frict_points = domain.quantities['friction'].get_values() |
---|
| 255 | assert num.allclose(frict_points[0],\ |
---|
| 256 | [ calc_frict, calc_frict, calc_frict]) |
---|
| 257 | assert num.allclose(frict_points[1],\ |
---|
| 258 | [ calc_frict, calc_frict, calc_frict]) |
---|
| 259 | assert num.allclose(frict_points[2],\ |
---|
| 260 | [ calc_frict, 1.0 + 2.0/3.0, calc_frict]) |
---|
| 261 | assert num.allclose(frict_points[3],\ |
---|
| 262 | [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0]) |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | #------------------------------------------------------------- |
---|
| 266 | if __name__ == "__main__": |
---|
[6304] | 267 | suite = unittest.makeSuite(Test_Region, 'test') |
---|
[6145] | 268 | runner = unittest.TextTestRunner() |
---|
| 269 | runner.run(suite) |
---|