[4561] | 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 |
---|
| 11 | from Numeric import allclose |
---|
| 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 |
---|
| 18 | |
---|
| 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 | """ |
---|
| 36 | tide = 5 |
---|
| 37 | boundary_filename = tempfile.mktemp(".sww") |
---|
| 38 | dir, base = os.path.split(boundary_filename) |
---|
| 39 | boundary_name = base[:-4] |
---|
| 40 | |
---|
| 41 | # Setup computational domain |
---|
| 42 | mesh = Mesh() |
---|
| 43 | mesh.add_region_from_polygon([[0,0], [100,0], [100,100], [0,100]]) |
---|
| 44 | mesh.generate_mesh(verbose=False) |
---|
| 45 | |
---|
| 46 | domain = pmesh_instance_to_domain_instance(mesh, Domain) |
---|
| 47 | domain.set_name(boundary_name) |
---|
| 48 | domain.set_datadir(dir) |
---|
| 49 | domain.starttime = boundary_starttime |
---|
| 50 | |
---|
| 51 | # Setup initial conditions |
---|
| 52 | domain.set_quantity('elevation', 0.0) |
---|
| 53 | domain.set_quantity('stage', tide) |
---|
| 54 | |
---|
| 55 | # Setup boundary conditions |
---|
| 56 | Bd = Dirichlet_boundary([tide,0.,0.]) # Constant boundary values |
---|
| 57 | Bd = Time_boundary(domain=domain, # Time dependent boundary |
---|
| 58 | f=lambda t: [t, 0.0, 0.0]) |
---|
| 59 | domain.set_boundary({'exterior': Bd}) |
---|
| 60 | for t in domain.evolve(yieldstep = 10, finaltime = 20.0): |
---|
| 61 | pass |
---|
| 62 | #domain.write_time() |
---|
| 63 | #print "domain.time", domain.time |
---|
| 64 | return boundary_filename |
---|
| 65 | |
---|
| 66 | def test_boundary_time(self): |
---|
| 67 | """ |
---|
| 68 | test_boundary_time(self): |
---|
| 69 | test that the starttime of a boundary condition is carried thru |
---|
| 70 | to the output sww file. |
---|
| 71 | |
---|
| 72 | """ |
---|
| 73 | boundary_starttime = 500 |
---|
| 74 | boundary_filename = self.create_sww_boundary(boundary_starttime) |
---|
| 75 | filename = tempfile.mktemp(".sww") |
---|
| 76 | #print "filename",filename |
---|
| 77 | dir, base = os.path.split(filename) |
---|
| 78 | senario_name = base[:-4] |
---|
| 79 | |
---|
| 80 | mesh = Mesh() |
---|
| 81 | ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) |
---|
| 82 | mesh.add_region_from_polygon([[0,0], [100,0], [100,100], [0,100]]) |
---|
| 83 | mesh.generate_mesh(verbose=False) |
---|
| 84 | |
---|
| 85 | domain = pmesh_instance_to_domain_instance(mesh, Domain) |
---|
| 86 | domain.set_name(senario_name) |
---|
| 87 | domain.set_datadir(dir) |
---|
| 88 | |
---|
| 89 | # Setup initial conditions |
---|
| 90 | domain.set_quantity('elevation', 0.0) |
---|
| 91 | domain.set_quantity('stage', 0.0) |
---|
| 92 | Bf = File_boundary(boundary_filename, |
---|
| 93 | domain, use_cache=False, verbose=False) |
---|
| 94 | |
---|
| 95 | # Setup boundary conditions |
---|
| 96 | domain.set_boundary({'exterior': Bf}) |
---|
| 97 | for t in domain.evolve(yieldstep = 5, finaltime = 10.0): |
---|
| 98 | pass |
---|
| 99 | #domain.write_time() |
---|
| 100 | #print "domain.time", domain.time |
---|
| 101 | |
---|
| 102 | # do an assertion on the time of the produced sww file |
---|
| 103 | fid = NetCDFFile(filename, 'r') #Open existing file for read |
---|
| 104 | times = fid.variables['time'][:] |
---|
| 105 | #print "times", times |
---|
| 106 | #print "fid.starttime", fid.starttime |
---|
| 107 | assert allclose(fid.starttime, boundary_starttime) |
---|
| 108 | fid.close() |
---|
| 109 | |
---|
| 110 | # clean up |
---|
| 111 | os.remove(boundary_filename) |
---|
| 112 | os.remove(filename) |
---|
| 113 | |
---|
| 114 | def test_boundary_timeII(self): |
---|
| 115 | """ |
---|
| 116 | test_boundary_timeII(self): |
---|
| 117 | Test that starttime can be set in the middle of a boundary condition |
---|
| 118 | """ |
---|
| 119 | boundary_starttime = 500 |
---|
| 120 | boundary_filename = self.create_sww_boundary(boundary_starttime) |
---|
| 121 | #print "boundary_filename",boundary_filename |
---|
| 122 | |
---|
| 123 | filename = tempfile.mktemp(".sww") |
---|
| 124 | #print "filename",filename |
---|
| 125 | dir, base = os.path.split(filename) |
---|
| 126 | senario_name = base[:-4] |
---|
| 127 | |
---|
| 128 | mesh = Mesh() |
---|
| 129 | mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) |
---|
| 130 | mesh.generate_mesh(verbose=False) |
---|
| 131 | |
---|
| 132 | domain = pmesh_instance_to_domain_instance(mesh, Domain) |
---|
| 133 | domain.set_name(senario_name) |
---|
| 134 | domain.set_datadir(dir) |
---|
| 135 | new_starttime = 510. |
---|
| 136 | domain.starttime = new_starttime |
---|
| 137 | |
---|
| 138 | # Setup initial conditions |
---|
| 139 | domain.set_quantity('elevation', 0.0) |
---|
| 140 | domain.set_quantity('stage', 0.0) |
---|
| 141 | Bf = File_boundary(boundary_filename, |
---|
| 142 | domain, use_cache=False, verbose=False) |
---|
| 143 | |
---|
| 144 | # Setup boundary conditions |
---|
| 145 | domain.set_boundary({'exterior': Bf}) |
---|
| 146 | for t in domain.evolve(yieldstep = 5, finaltime = 9.0): |
---|
| 147 | pass |
---|
| 148 | #domain.write_time() |
---|
| 149 | #print "domain.time", domain.time |
---|
| 150 | |
---|
| 151 | # do an assertion on the time of the produced sww file |
---|
| 152 | fid = NetCDFFile(filename, 'r') #Open existing file for read |
---|
| 153 | times = fid.variables['time'][:] |
---|
| 154 | stage = fid.variables['stage'][:] |
---|
| 155 | #print "times", times |
---|
| 156 | #print "fid.starttime", fid.starttime |
---|
| 157 | assert allclose(fid.starttime, new_starttime) |
---|
| 158 | fid.close() |
---|
| 159 | #print "stage[2,0]", stage[2,0] |
---|
| 160 | msg = "This test is a bit hand crafted, based on the output file. " |
---|
| 161 | msg += "Not logic. " |
---|
| 162 | msg += "It's testing that starttime is working" |
---|
| 163 | assert allclose(stage[2,0], 5.959411),msg |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | # clean up |
---|
| 168 | os.remove(boundary_filename) |
---|
| 169 | os.remove(filename) |
---|
| 170 | |
---|
| 171 | #------------------------------------------------------------- |
---|
| 172 | if __name__ == "__main__": |
---|
| 173 | suite = unittest.makeSuite(Test_system,'test') |
---|
| 174 | #suite = unittest.makeSuite(Test_system,'test_boundary_timeII') |
---|
| 175 | runner = unittest.TextTestRunner() |
---|
| 176 | runner.run(suite) |
---|