source: trunk/anuga_core/source/anuga_parallel/test_parallel_shallow_domain.py @ 8283

Last change on this file since 8283 was 8262, checked in by steve, 12 years ago

Added in unit test for dump_triangulation

File size: 4.6 KB
Line 
1
2"""Test a run of the sequential shallow water domain against
3a run of the parallel shallow water domain.
4
5WARNING: This assumes that the command to run jobs is mpirun.
6Tested with MPICH and LAM (Ole)
7"""
8
9#------------------------------------------------------------------------------
10# Import necessary modules
11#------------------------------------------------------------------------------
12
13import unittest
14import os
15import sys
16
17
18
19import numpy as num
20
21#------------------------------------------
22# Import pypar without the initial output
23#------------------------------------------
24class NullStream:
25    def write(self,text):
26        pass
27sys.stdout = NullStream()
28import pypar
29sys.stdout = sys.__stdout__
30
31
32#------------------------------------------
33# anuga imports
34#------------------------------------------
35
36from anuga.utilities.numerical_tools import ensure_numeric
37from anuga.utilities.util_ext        import double_precision
38from anuga.utilities.norms           import l1_norm, l2_norm, linf_norm
39
40from anuga import Domain
41from anuga import Reflective_boundary
42from anuga import Dirichlet_boundary
43from anuga import Time_boundary
44from anuga import Transmissive_boundary
45
46from anuga import rectangular_cross
47from anuga import create_domain_from_file
48
49
50from anuga_parallel import distribute, myid, numprocs, finalize
51
52
53#--------------------------------------------------------------------------
54# Setup parameters
55#--------------------------------------------------------------------------
56
57mesh_filename = "merimbula_10785_1.tsh"
58#mesh_filename = "test-100.tsh"
59yieldstep = 1
60finaltime = 1
61quantity = 'stage'
62nprocs = 2
63verbose = False
64
65#--------------------------------------------------------------------------
66# Setup procedures
67#--------------------------------------------------------------------------
68class Set_Stage:
69    """Set an initial condition with constant water height, for x<x0
70    """
71
72    def __init__(self, x0=0.25, x1=0.5, h=1.0):
73        self.x0 = x0
74        self.x1 = x1
75        self.= h
76
77    def __call__(self, x, y):
78        return self.h*((x>self.x0)&(x<self.x1))
79
80#--------------------------------------------------------------------------
81# Setup test
82#--------------------------------------------------------------------------
83def evolution_test(parallel=False):
84
85
86    domain = create_domain_from_file(mesh_filename)
87    domain.set_quantity('stage', Set_Stage(756000.0, 756500.0, 2.0))
88
89    #--------------------------------------------------------------------------
90    # Create parallel domain if requested
91    #--------------------------------------------------------------------------
92
93    if parallel:
94        if myid == 0 and verbose: print 'DISTRIBUTING PARALLEL DOMAIN'
95        domain = distribute(domain)
96
97    #------------------------------------------------------------------------------
98    # Setup boundary conditions
99    # This must currently happen *after* domain has been distributed
100    #------------------------------------------------------------------------------
101    domain.store = False
102    Br = Reflective_boundary(domain)      # Solid reflective wall
103
104    domain.set_boundary({'outflow' :Br, 'inflow' :Br, 'inner' :Br, 'exterior' :Br, 'open' :Br})
105
106    #------------------------------------------------------------------------------
107    # Evolution
108    #------------------------------------------------------------------------------
109    if parallel:
110        if myid == 0 and verbose: print 'PARALLEL EVOLVE'
111    else:
112        if verbose: print 'SEQUENTIAL EVOLVE'
113
114    for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime):
115        pass
116
117    domain.dump_triangulation()
118
119# Test an nprocs-way run of the shallow water equations
120# against the sequential code.
121
122class Test_parallel_shallow_domain(unittest.TestCase):
123    def test_parallel_shallow_domain(self):
124        #print "Expect this test to fail if not run from the parallel directory."
125        result = os.system("mpirun -np %d python test_parallel_shallow_domain.py" % nprocs)
126        assert_(result == 0)
127
128
129# Because we are doing assertions outside of the TestCase class
130# the PyUnit defined assert_ function can't be used.
131def assert_(condition, msg="Assertion Failed"):
132    if condition == False:
133        #pypar.finalize()
134        raise AssertionError, msg
135
136if __name__=="__main__":
137    if numprocs == 1: 
138        runner = unittest.TextTestRunner()
139        suite = unittest.makeSuite(Test_parallel_shallow_domain, 'test')
140        runner.run(suite)
141    else:
142
143        pypar.barrier()
144        if myid ==0:
145            if verbose: print 'PARALLEL START'
146
147        evolution_test(parallel=True)
148       
149        if myid == 0:     
150            if verbose: print 'Parallel test OK'
151
152
153
154    finalize()
Note: See TracBrowser for help on using the repository browser.