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 config import epsilon |
---|
9 | from Numeric import allclose #, array, ones, Float |
---|
10 | |
---|
11 | |
---|
12 | def add_x_y(x, y): |
---|
13 | return x+y |
---|
14 | |
---|
15 | class TestCase(unittest.TestCase): |
---|
16 | def setUp(self): |
---|
17 | pass |
---|
18 | |
---|
19 | |
---|
20 | def tearDown(self): |
---|
21 | pass |
---|
22 | |
---|
23 | |
---|
24 | def test_region_tags(self): |
---|
25 | """ |
---|
26 | get values based on triangle lists. |
---|
27 | """ |
---|
28 | from mesh_factory import rectangular |
---|
29 | from shallow_water import Domain |
---|
30 | from Numeric import zeros, Float |
---|
31 | |
---|
32 | #Create basic mesh |
---|
33 | points, vertices, boundary = rectangular(1, 3) |
---|
34 | |
---|
35 | #Create shallow water domain |
---|
36 | domain = Domain(points, vertices, boundary) |
---|
37 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
38 | 'top':[4,5], |
---|
39 | 'all':[0,1,2,3,4,5]}) |
---|
40 | |
---|
41 | |
---|
42 | #Set friction |
---|
43 | manning = 0.07 |
---|
44 | domain.set_quantity('friction', manning) |
---|
45 | |
---|
46 | a = Set_region('bottom', 'friction', 0.09) |
---|
47 | b = Set_region('top', 'friction', 1.0) |
---|
48 | domain.set_region([a, b]) |
---|
49 | #print domain.quantities['friction'].get_values() |
---|
50 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
51 | [[ 0.09, 0.09, 0.09], |
---|
52 | [ 0.09, 0.09, 0.09], |
---|
53 | [ 0.07, 0.07, 0.07], |
---|
54 | [ 0.07, 0.07, 0.07], |
---|
55 | [ 1.0, 1.0, 1.0], |
---|
56 | [ 1.0, 1.0, 1.0]]) |
---|
57 | |
---|
58 | #c = Add_Value_To_region('all', 'friction', 10.0) |
---|
59 | domain.set_region(Add_value_to_region('all', 'friction', 10.0)) |
---|
60 | #print domain.quantities['friction'].get_values() |
---|
61 | assert allclose(domain.quantities['friction'].get_values(), |
---|
62 | [[ 10.09, 10.09, 10.09], |
---|
63 | [ 10.09, 10.09, 10.09], |
---|
64 | [ 10.07, 10.07, 10.07], |
---|
65 | [ 10.07, 10.07, 10.07], |
---|
66 | [ 11.0, 11.0, 11.0], |
---|
67 | [ 11.0, 11.0, 11.0]]) |
---|
68 | |
---|
69 | # trying a function |
---|
70 | domain.set_region(Set_region('top', 'friction', add_x_y)) |
---|
71 | #print domain.quantities['friction'].get_values() |
---|
72 | assert allclose(domain.quantities['friction'].get_values(), |
---|
73 | [[ 10.09, 10.09, 10.09], |
---|
74 | [ 10.09, 10.09, 10.09], |
---|
75 | [ 10.07, 10.07, 10.07], |
---|
76 | [ 10.07, 10.07, 10.07], |
---|
77 | [ 5./3, 2.0, 2./3], |
---|
78 | [ 1.0, 2./3, 2.0]]) |
---|
79 | |
---|
80 | domain.set_quantity('elevation', 10.0) |
---|
81 | domain.set_quantity('stage', 10.0) |
---|
82 | domain.set_region(Add_value_to_region('top', 'stage', 1.0,initial_quantity='elevation')) |
---|
83 | #print domain.quantities['stage'].get_values() |
---|
84 | assert allclose(domain.quantities['stage'].get_values(), |
---|
85 | [[ 10., 10., 10.], |
---|
86 | [ 10., 10., 10.], |
---|
87 | [ 10., 10., 10.], |
---|
88 | [ 10., 10., 10.], |
---|
89 | [ 11.0, 11.0, 11.0], |
---|
90 | [ 11.0, 11.0, 11.0]]) |
---|
91 | |
---|
92 | def test_unique_vertices(self): |
---|
93 | """ |
---|
94 | get values based on triangle lists. |
---|
95 | """ |
---|
96 | from mesh_factory import rectangular |
---|
97 | from shallow_water import Domain |
---|
98 | from Numeric import zeros, Float |
---|
99 | |
---|
100 | #Create basic mesh |
---|
101 | points, vertices, boundary = rectangular(1, 3) |
---|
102 | |
---|
103 | #Create shallow water domain |
---|
104 | domain = Domain(points, vertices, boundary) |
---|
105 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
106 | 'top':[4,5], |
---|
107 | 'all':[0,1,2,3,4,5]}) |
---|
108 | |
---|
109 | #Set friction |
---|
110 | manning = 0.07 |
---|
111 | domain.set_quantity('friction', manning) |
---|
112 | |
---|
113 | a = Set_region('bottom', 'friction', 0.09, location = 'unique vertices') |
---|
114 | domain.set_region(a) |
---|
115 | #print domain.quantities['friction'].get_values() |
---|
116 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
117 | [[ 0.09, 0.09, 0.09], |
---|
118 | [ 0.09, 0.09, 0.09], |
---|
119 | [ 0.09, 0.07, 0.09], |
---|
120 | [ 0.07, 0.09, 0.07], |
---|
121 | [ 0.07, 0.07, 0.07], |
---|
122 | [ 0.07, 0.07, 0.07]]) |
---|
123 | |
---|
124 | |
---|
125 | def test_unique_verticesII(self): |
---|
126 | """ |
---|
127 | get values based on triangle lists. |
---|
128 | """ |
---|
129 | from mesh_factory import rectangular |
---|
130 | from shallow_water import Domain |
---|
131 | from Numeric import zeros, Float |
---|
132 | |
---|
133 | #Create basic mesh |
---|
134 | points, vertices, boundary = rectangular(1, 3) |
---|
135 | |
---|
136 | #Create shallow water domain |
---|
137 | domain = Domain(points, vertices, boundary) |
---|
138 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
139 | 'top':[4,5], |
---|
140 | 'all':[0,1,2,3,4,5]}) |
---|
141 | |
---|
142 | #Set friction |
---|
143 | manning = 0.07 |
---|
144 | domain.set_quantity('friction', manning) |
---|
145 | |
---|
146 | domain.set_region(Add_value_to_region('bottom', 'friction', 1.0,initial_quantity='friction', location = 'unique vertices')) |
---|
147 | |
---|
148 | #print domain.quantities['friction'].get_values() |
---|
149 | assert allclose(domain.quantities['friction'].get_values(),\ |
---|
150 | [[ 1.07, 1.07, 1.07], |
---|
151 | [ 1.07, 1.07, 1.07], |
---|
152 | [ 1.07, 0.07, 1.07], |
---|
153 | [ 0.07, 1.07, 0.07], |
---|
154 | [ 0.07, 0.07, 0.07], |
---|
155 | [ 0.07, 0.07, 0.07]]) |
---|
156 | #------------------------------------------------------------- |
---|
157 | if __name__ == "__main__": |
---|
158 | suite = unittest.makeSuite(TestCase,'test') |
---|
159 | runner = unittest.TextTestRunner() |
---|
160 | runner.run(suite) |
---|