1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import tempfile |
---|
5 | import unittest |
---|
6 | import os |
---|
7 | |
---|
8 | from Scientific.IO.NetCDF import NetCDFFile |
---|
9 | import numpy as num |
---|
10 | |
---|
11 | import anuga |
---|
12 | from anuga.pmesh.mesh import Mesh |
---|
13 | from anuga.abstract_2d_finite_volumes.pmesh2domain import \ |
---|
14 | pmesh_to_domain_instance |
---|
15 | from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a |
---|
16 | |
---|
17 | |
---|
18 | class Test_system(unittest.TestCase): |
---|
19 | """ |
---|
20 | Some system wide, high-level tests. |
---|
21 | """ |
---|
22 | def setUp(self): |
---|
23 | pass |
---|
24 | |
---|
25 | def tearDown(self): |
---|
26 | pass |
---|
27 | |
---|
28 | def create_sww_boundary(self, boundary_starttime): |
---|
29 | """ |
---|
30 | This creates a boundary file with ; |
---|
31 | time stage |
---|
32 | 0 5 |
---|
33 | 10 2.15268 |
---|
34 | 20 13.9773 |
---|
35 | """ |
---|
36 | |
---|
37 | tide = 5 |
---|
38 | boundary_filename = tempfile.mktemp(".sww") |
---|
39 | dir, base = os.path.split(boundary_filename) |
---|
40 | boundary_name = base[:-4] |
---|
41 | |
---|
42 | # Setup computational domain |
---|
43 | mesh = Mesh() |
---|
44 | mesh.add_region_from_polygon([[0,0], [100,0], [100,100], [0,100]]) |
---|
45 | mesh.generate_mesh(verbose=False) |
---|
46 | |
---|
47 | domain = pmesh_to_domain_instance(mesh, anuga.Domain) |
---|
48 | domain.set_name(boundary_name) |
---|
49 | domain.set_datadir(dir) |
---|
50 | domain.set_starttime(boundary_starttime) |
---|
51 | |
---|
52 | # Setup initial conditions |
---|
53 | domain.set_quantity('elevation', 0.0) |
---|
54 | domain.set_quantity('stage', tide) |
---|
55 | |
---|
56 | # Setup boundary conditions |
---|
57 | Bd = anuga.Dirichlet_boundary([tide,0.,0.]) # Constant boundary values |
---|
58 | Bd = anuga.Time_boundary(domain=domain, # Time dependent boundary |
---|
59 | function=lambda t: [t, 0.0, 0.0]) |
---|
60 | domain.set_boundary({'exterior': Bd}) |
---|
61 | for t in domain.evolve(yieldstep = 10, finaltime = 20.0): |
---|
62 | pass |
---|
63 | #print domain.boundary_statistics('stage') |
---|
64 | q = Bd.evaluate() |
---|
65 | |
---|
66 | # FIXME (Ole): This test would not have passed in |
---|
67 | # changeset:5846. |
---|
68 | msg = 'Time boundary not evaluated correctly' |
---|
69 | assert num.allclose(t, q[0]), msg |
---|
70 | |
---|
71 | #print domain.get_quantity('stage').get_values() |
---|
72 | #domain.write_time() |
---|
73 | #print "domain.time", domain.time |
---|
74 | |
---|
75 | return boundary_filename |
---|
76 | |
---|
77 | def test_boundary_time(self): |
---|
78 | """ |
---|
79 | test_boundary_time(self): |
---|
80 | test that the starttime of a boundary condition is carried thru |
---|
81 | to the output sww file. |
---|
82 | |
---|
83 | """ |
---|
84 | |
---|
85 | boundary_starttime = 0 |
---|
86 | boundary_filename = self.create_sww_boundary(boundary_starttime) |
---|
87 | filename = tempfile.mktemp(".sww") |
---|
88 | dir, base = os.path.split(filename) |
---|
89 | senario_name = base[:-4] |
---|
90 | |
---|
91 | mesh = Mesh() |
---|
92 | ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) |
---|
93 | mesh.add_region_from_polygon([[0,0], [100,0], [100,100], [0,100]]) |
---|
94 | mesh.generate_mesh(verbose=False) |
---|
95 | |
---|
96 | domain = pmesh_to_domain_instance(mesh, anuga.Domain) |
---|
97 | domain.set_name(senario_name) |
---|
98 | domain.set_datadir(dir) |
---|
99 | |
---|
100 | # Setup initial conditions |
---|
101 | domain.set_quantity('elevation', 0.0) |
---|
102 | domain.set_quantity('stage', 0.0) |
---|
103 | Bf = anuga.File_boundary(boundary_filename, |
---|
104 | domain, use_cache=False, verbose=False) |
---|
105 | |
---|
106 | # Setup boundary conditions |
---|
107 | domain.set_boundary({'exterior': Bf}) |
---|
108 | |
---|
109 | |
---|
110 | for t in domain.evolve(yieldstep = 5.0, finaltime = 10.0): |
---|
111 | pass |
---|
112 | #print domain.write_time() |
---|
113 | #print "domain.time", domain.time |
---|
114 | |
---|
115 | # do an assertion on the time of the produced sww file |
---|
116 | fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read |
---|
117 | times = fid.variables['time'][:] |
---|
118 | #print "times", times |
---|
119 | #print "fid.starttime", fid.starttime |
---|
120 | assert num.allclose(fid.starttime, boundary_starttime) |
---|
121 | fid.close() |
---|
122 | |
---|
123 | # clean up |
---|
124 | os.remove(boundary_filename) |
---|
125 | os.remove(filename) |
---|
126 | |
---|
127 | def test_boundary_timeII(self): |
---|
128 | """ |
---|
129 | test_boundary_timeII(self): |
---|
130 | Test that starttime can be set in the middle of a boundary condition |
---|
131 | """ |
---|
132 | |
---|
133 | boundary_starttime = 0 |
---|
134 | boundary_filename = self.create_sww_boundary(boundary_starttime) |
---|
135 | #print "boundary_filename",boundary_filename |
---|
136 | |
---|
137 | filename = tempfile.mktemp(".sww") |
---|
138 | #print "filename",filename |
---|
139 | dir, base = os.path.split(filename) |
---|
140 | senario_name = base[:-4] |
---|
141 | |
---|
142 | mesh = Mesh() |
---|
143 | mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) |
---|
144 | mesh.generate_mesh(verbose=False) |
---|
145 | |
---|
146 | domain = pmesh_to_domain_instance(mesh, anuga.Domain) |
---|
147 | domain.set_name(senario_name) |
---|
148 | domain.set_datadir(dir) |
---|
149 | new_starttime = 0. |
---|
150 | domain.set_starttime(new_starttime) |
---|
151 | |
---|
152 | # Setup initial conditions |
---|
153 | domain.set_quantity('elevation', 0.0) |
---|
154 | domain.set_quantity('stage', 0.0) |
---|
155 | Bf = anuga.File_boundary(boundary_filename, |
---|
156 | domain, use_cache=False, verbose=False) |
---|
157 | |
---|
158 | # Setup boundary conditions |
---|
159 | domain.set_boundary({'exterior': Bf}) |
---|
160 | for t in domain.evolve(yieldstep = 5, finaltime = 9.0): |
---|
161 | pass |
---|
162 | #print domain.boundary_statistics() |
---|
163 | #domain.write_time() |
---|
164 | #print "domain.time", domain.time |
---|
165 | |
---|
166 | # do an assertion on the time of the produced sww file |
---|
167 | fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read |
---|
168 | times = fid.variables['time'][:] |
---|
169 | stage = fid.variables['stage'][:] |
---|
170 | #print stage |
---|
171 | #print "times", times |
---|
172 | #print "fid.starttime", fid.starttime |
---|
173 | assert num.allclose(fid.starttime, new_starttime) |
---|
174 | fid.close() |
---|
175 | |
---|
176 | #print "stage[2,0]", stage[2,0] |
---|
177 | msg = "This test is a bit hand crafted, based on the output file. " |
---|
178 | msg += "Not logic. " |
---|
179 | msg += "It's testing that starttime is working" |
---|
180 | assert num.allclose(stage[2,0], 4.7981238),msg |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | # clean up |
---|
185 | os.remove(boundary_filename) |
---|
186 | os.remove(filename) |
---|
187 | |
---|
188 | #------------------------------------------------------------- |
---|
189 | |
---|
190 | if __name__ == "__main__": |
---|
191 | suite = unittest.makeSuite(Test_system,'test') |
---|
192 | runner = unittest.TextTestRunner() |
---|
193 | runner.run(suite) |
---|