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

Last change on this file since 6157 was 6157, checked in by rwilson, 15 years ago

Change Numeric imports to general form - ready to change to NumPy?.

File size: 6.5 KB
Line 
1#!/usr/bin/env python
2#
3"""
4Let's do some system wide tests
5"""
6import tempfile
7import unittest
8import os
9
10from Scientific.IO.NetCDF import NetCDFFile
11import Numeric as num
12
13from anuga.shallow_water import Domain
14from anuga.shallow_water import Dirichlet_boundary, Time_boundary
15from anuga.shallow_water import File_boundary
16from anuga.pmesh.mesh import Mesh
17from anuga.abstract_2d_finite_volumes.pmesh2domain import pmesh_instance_to_domain_instance
18from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
19
20
21class 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.set_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            #print domain.boundary_statistics('stage')
63            q = Bd.evaluate()
64   
65            # FIXME (Ole): This test would not have passed in
66            # changeset:5846.
67            msg = 'Time boundary not evaluated correctly'
68            assert num.allclose(t, q[0]), msg
69           
70            #print domain.get_quantity('stage').get_values()
71            #domain.write_time()
72            #print "domain.time", domain.time
73           
74        return boundary_filename
75   
76    def test_boundary_time(self):
77        """
78        test_boundary_time(self):
79        test that the starttime of a boundary condition is carried thru
80        to the output sww file.
81       
82        """
83     
84        boundary_starttime = 500
85        boundary_filename = self.create_sww_boundary(boundary_starttime)
86        filename = tempfile.mktemp(".sww")
87        #print "filename",filename
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
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 = 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
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], 11.9867153168),msg
181       
182       
183
184        # clean up
185        os.remove(boundary_filename)
186        os.remove(filename)
187       
188#-------------------------------------------------------------
189if __name__ == "__main__":
190    suite = unittest.makeSuite(Test_system,'test')
191    #suite = unittest.makeSuite(Test_system,'test_boundary_timeII')
192    runner = unittest.TextTestRunner()
193    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.