source: anuga_core/source/anuga/interface/test_model.py @ 7731

Last change on this file since 7731 was 7730, checked in by James Hudson, 15 years ago

Widened scope of new ANUGA API test.

File size: 3.2 KB
Line 
1#!/usr/bin/env python
2
3import tempfile
4import unittest
5
6from model import Model
7from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
8
9
10
11class modelTestCase(unittest.TestCase):
12    def setUp(self):
13        # construct and name model
14        self.model = Model('test_model') 
15        pass
16
17    def tearDown(self):
18        pass
19
20    def test_construction(self):
21        """ Test initial setup of model. """
22
23        a = [0.0, 0.0]
24        b = [0.0, 2.0]
25        c = [2.0, 0.0]
26        d = [0.0, 4.0]
27        e = [2.0, 2.0]
28        f = [4.0, 0.0]
29
30        points = [a, b, c, d, e, f]
31        #              bac,     bce,     ecf,     dbe
32        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
33
34        self.model.set_geometry(points, vertices)
35
36    def test_simple_example(self):
37        """ Run a new version of the channel1 example. """
38        from anuga.shallow_water import Reflective_boundary
39        from anuga.shallow_water import Dirichlet_boundary
40       
41        def topography(x, y):
42            return -x/10                         
43
44        # set quantities
45        self.model.set_quantity('elevation', topography) 
46        self.model.set_quantity('friction', 0.01)         
47        self.model.set_quantity('stage', expression='elevation') 
48
49        # set properties of boundaries (reflective, flow in, etc.)
50        Bi = Dirichlet_boundary([0.4, 0, 0])         # Inflow
51        self.model.set_boundary({'left': Bi, 'right': Bi, 'top': Bi, 'bottom': Bi})
52
53        # set the geometry to use (may be a mesh file, or points/vertices tuple)
54        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
55
56        # build, then run the simulation
57        self.model.build()
58        self.model.run(0.5, 1.0)
59
60
61    def test_wrong_input_order(self):
62        """The user tries to build before model is defined. """
63       
64        self.model.set_quantity('stage',                 # Dry bed
65                            expression='elevation')         
66
67        try:
68            self.model.build()           
69        except:
70            pass
71        else:
72            msg = 'Should have raised exception for missing mesh'
73            raise Exception, msg
74
75
76    def test_duplicate_geometry(self):
77        """The user tries to assign geometry twice. """
78
79        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
80
81        try:
82            self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0)) 
83        except:
84            pass
85        else:
86            msg = 'Should have raised bad input exception'
87            raise Exception, msg
88
89
90    def test_input_after_build(self):
91        """The user tries to change built model. """
92
93        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
94        self.model.build()
95       
96        try:
97            self.model.set_quantity('friction', 0.01) 
98        except:
99            pass
100        else:
101            msg = 'Should have raised exception because model already built'
102            raise Exception, msg
103
104
105
106################################################################################
107
108if __name__ == '__main__':
109    suite = unittest.makeSuite(modelTestCase,'test')
110    runner = unittest.TextTestRunner() #verbosity=2)
111    runner.run(suite)
112
Note: See TracBrowser for help on using the repository browser.