[4712] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | """ |
---|
| 4 | Let's do some system wide tests |
---|
| 5 | """ |
---|
| 6 | import tempfile |
---|
| 7 | import unittest |
---|
| 8 | import os |
---|
| 9 | |
---|
| 10 | from Scientific.IO.NetCDF import NetCDFFile |
---|
[6304] | 11 | import numpy as num |
---|
[4712] | 12 | |
---|
| 13 | from anuga.shallow_water import Domain |
---|
| 14 | from anuga.shallow_water import Dirichlet_boundary, Time_boundary |
---|
| 15 | from anuga.shallow_water import File_boundary |
---|
| 16 | from anuga.pmesh.mesh import Mesh |
---|
| 17 | from anuga.abstract_2d_finite_volumes.pmesh2domain import pmesh_instance_to_domain_instance |
---|
[6086] | 18 | from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a |
---|
[4712] | 19 | |
---|
| 20 | |
---|
| 21 | class Test_system(unittest.TestCase): |
---|
| 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 | """ |
---|
[6304] | 36 | |
---|
[4712] | 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_instance_to_domain_instance(mesh, 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 = Dirichlet_boundary([tide,0.,0.]) # Constant boundary values |
---|
| 58 | Bd = Time_boundary(domain=domain, # Time dependent boundary |
---|
| 59 | f=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 |
---|
[5878] | 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' |
---|
[6157] | 69 | assert num.allclose(t, q[0]), msg |
---|
[5878] | 70 | |
---|
| 71 | #print domain.get_quantity('stage').get_values() |
---|
[4712] | 72 | #domain.write_time() |
---|
| 73 | #print "domain.time", domain.time |
---|
[5878] | 74 | |
---|
[4712] | 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 = 500 |
---|
| 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_instance_to_domain_instance(mesh, 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 = 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 |
---|
[6086] | 116 | fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read |
---|
[4712] | 117 | times = fid.variables['time'][:] |
---|
| 118 | #print "times", times |
---|
| 119 | #print "fid.starttime", fid.starttime |
---|
[6157] | 120 | assert num.allclose(fid.starttime, boundary_starttime) |
---|
[4712] | 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 = 500 |
---|
| 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_instance_to_domain_instance(mesh, Domain) |
---|
| 147 | domain.set_name(senario_name) |
---|
| 148 | domain.set_datadir(dir) |
---|
| 149 | new_starttime = 510. |
---|
| 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 = 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 |
---|
[5878] | 162 | #print domain.boundary_statistics() |
---|
[5847] | 163 | #domain.write_time() |
---|
[4712] | 164 | #print "domain.time", domain.time |
---|
| 165 | |
---|
| 166 | # do an assertion on the time of the produced sww file |
---|
[6086] | 167 | fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read |
---|
[4712] | 168 | times = fid.variables['time'][:] |
---|
| 169 | stage = fid.variables['stage'][:] |
---|
[5847] | 170 | #print stage |
---|
[4712] | 171 | #print "times", times |
---|
| 172 | #print "fid.starttime", fid.starttime |
---|
[6157] | 173 | assert num.allclose(fid.starttime, new_starttime) |
---|
[4712] | 174 | fid.close() |
---|
[5878] | 175 | |
---|
[4712] | 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" |
---|
[6157] | 180 | assert num.allclose(stage[2,0], 11.9867153168),msg |
---|
[4712] | 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | # clean up |
---|
| 185 | os.remove(boundary_filename) |
---|
| 186 | os.remove(filename) |
---|
| 187 | |
---|
| 188 | #------------------------------------------------------------- |
---|
[6410] | 189 | |
---|
[4712] | 190 | if __name__ == "__main__": |
---|
| 191 | suite = unittest.makeSuite(Test_system,'test') |
---|
| 192 | runner = unittest.TextTestRunner() |
---|
| 193 | runner.run(suite) |
---|