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

Last change on this file since 5224 was 4712, checked in by steve, 17 years ago

Working on 2nd order time stepping

File size: 6.0 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
11from Numeric import allclose
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
18
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            #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     
74        boundary_starttime = 500
75        boundary_filename = self.create_sww_boundary(boundary_starttime)
76        filename = tempfile.mktemp(".sww")
77        #print "filename",filename
78        dir, base = os.path.split(filename)
79        senario_name = base[:-4]
80 
81        mesh = Mesh()
82        ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]])
83        mesh.add_region_from_polygon([[0,0], [100,0], [100,100], [0,100]])
84        mesh.generate_mesh(verbose=False)
85       
86        domain = pmesh_instance_to_domain_instance(mesh, Domain) 
87        domain.set_name(senario_name)                 
88        domain.set_datadir(dir) 
89
90        # Setup initial conditions
91        domain.set_quantity('elevation', 0.0) 
92        domain.set_quantity('stage', 0.0)         
93        Bf = File_boundary(boundary_filename,
94                           domain,  use_cache=False, verbose=False)
95
96        # Setup boundary conditions
97        domain.set_boundary({'exterior': Bf})
98
99       
100        for t in domain.evolve(yieldstep = 5.0, finaltime = 10.0):
101            pass
102            #print domain.write_time()
103            #print "domain.time", domain.time
104
105        # do an assertion on the time of the produced sww file
106        fid = NetCDFFile(filename, 'r')    #Open existing file for read
107        times = fid.variables['time'][:]
108        #print "times", times
109        #print "fid.starttime", fid.starttime
110        assert allclose(fid.starttime, boundary_starttime)
111        fid.close()
112
113        # clean up
114        os.remove(boundary_filename)
115        os.remove(filename)
116       
117    def test_boundary_timeII(self):
118        """
119        test_boundary_timeII(self):
120        Test that starttime can be set in the middle of a boundary condition
121        """
122       
123        boundary_starttime = 500
124        boundary_filename = self.create_sww_boundary(boundary_starttime)
125        #print "boundary_filename",boundary_filename
126       
127        filename = tempfile.mktemp(".sww")
128        #print "filename",filename
129        dir, base = os.path.split(filename)
130        senario_name = base[:-4]
131 
132        mesh = Mesh()
133        mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]])
134        mesh.generate_mesh(verbose=False)
135       
136        domain = pmesh_instance_to_domain_instance(mesh, Domain) 
137        domain.set_name(senario_name)                 
138        domain.set_datadir(dir)
139        new_starttime = 510.
140        domain.set_starttime(new_starttime)
141
142        # Setup initial conditions
143        domain.set_quantity('elevation', 0.0) 
144        domain.set_quantity('stage', 0.0)         
145        Bf = File_boundary(boundary_filename,
146                           domain,  use_cache=False, verbose=False)
147
148        # Setup boundary conditions
149        domain.set_boundary({'exterior': Bf})
150        for t in domain.evolve(yieldstep = 5, finaltime = 9.0):
151            pass
152            #print domain.write_time()
153            #print "domain.time", domain.time
154
155        # do an assertion on the time of the produced sww file
156        fid = NetCDFFile(filename, 'r')    #Open existing file for read
157        times = fid.variables['time'][:]
158        stage = fid.variables['stage'][:]
159        #print "times", times
160        #print "fid.starttime", fid.starttime
161        assert allclose(fid.starttime, new_starttime)
162        fid.close()
163        #print "stage[2,0]", stage[2,0]
164        msg = "This test is a bit hand crafted, based on the output file. "
165        msg += "Not logic. "
166        msg += "It's testing that starttime is working"
167        assert allclose(stage[2,0], 5.959411),msg
168       
169       
170
171        # clean up
172        os.remove(boundary_filename)
173        os.remove(filename)
174       
175#-------------------------------------------------------------
176if __name__ == "__main__":
177    suite = unittest.makeSuite(Test_system,'test')
178    #suite = unittest.makeSuite(Test_system,'test_boundary_timeII')
179    runner = unittest.TextTestRunner()
180    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.