1 | |
---|
2 | """Test a run of the sequential shallow water domain against |
---|
3 | a run of the parallel shallow water domain. |
---|
4 | |
---|
5 | WARNING: This assumes that the command to run jobs is mpirun. |
---|
6 | Tested with MPICH and LAM (Ole) |
---|
7 | """ |
---|
8 | |
---|
9 | #------------------------------------------------------------------------------ |
---|
10 | # Import necessary modules |
---|
11 | #------------------------------------------------------------------------------ |
---|
12 | |
---|
13 | import unittest |
---|
14 | import os |
---|
15 | import sys |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | import numpy as num |
---|
20 | |
---|
21 | #------------------------------------------ |
---|
22 | # Import pypar without the initial output |
---|
23 | #------------------------------------------ |
---|
24 | class NullStream: |
---|
25 | def write(self,text): |
---|
26 | pass |
---|
27 | sys.stdout = NullStream() |
---|
28 | import pypar |
---|
29 | sys.stdout = sys.__stdout__ |
---|
30 | |
---|
31 | |
---|
32 | #------------------------------------------ |
---|
33 | # anuga imports |
---|
34 | #------------------------------------------ |
---|
35 | |
---|
36 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
37 | from anuga.utilities.util_ext import double_precision |
---|
38 | from anuga.utilities.norms import l1_norm, l2_norm, linf_norm |
---|
39 | |
---|
40 | from anuga import Domain |
---|
41 | from anuga import Reflective_boundary |
---|
42 | from anuga import Dirichlet_boundary |
---|
43 | from anuga import Time_boundary |
---|
44 | from anuga import Transmissive_boundary |
---|
45 | |
---|
46 | from anuga import rectangular_cross |
---|
47 | from anuga import create_domain_from_file |
---|
48 | |
---|
49 | |
---|
50 | from anuga_parallel import distribute, myid, numprocs, finalize |
---|
51 | |
---|
52 | |
---|
53 | #-------------------------------------------------------------------------- |
---|
54 | # Setup parameters |
---|
55 | #-------------------------------------------------------------------------- |
---|
56 | |
---|
57 | mesh_filename = "merimbula_10785_1.tsh" |
---|
58 | #mesh_filename = "test-100.tsh" |
---|
59 | yieldstep = 1 |
---|
60 | finaltime = 1 |
---|
61 | quantity = 'stage' |
---|
62 | nprocs = 2 |
---|
63 | verbose = False |
---|
64 | |
---|
65 | #-------------------------------------------------------------------------- |
---|
66 | # Setup procedures |
---|
67 | #-------------------------------------------------------------------------- |
---|
68 | class 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 = h |
---|
76 | |
---|
77 | def __call__(self, x, y): |
---|
78 | return self.h*((x>self.x0)&(x<self.x1)) |
---|
79 | |
---|
80 | #-------------------------------------------------------------------------- |
---|
81 | # Setup test |
---|
82 | #-------------------------------------------------------------------------- |
---|
83 | def 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 | |
---|
122 | class 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. |
---|
131 | def assert_(condition, msg="Assertion Failed"): |
---|
132 | if condition == False: |
---|
133 | #pypar.finalize() |
---|
134 | raise AssertionError, msg |
---|
135 | |
---|
136 | if __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() |
---|