source: anuga_core/source/anuga/shallow_water/test_system.py @ 7736

Last change on this file since 7736 was 7736, checked in by hudson, 14 years ago

Shallow water refactorings - all unit tests pass, and new file conversion test module created.

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