1 | |
---|
2 | """Run parallel shallow water domain. |
---|
3 | |
---|
4 | with check pointing |
---|
5 | """ |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | #------------------------------------------------------------------------------ |
---|
10 | # Import necessary modules |
---|
11 | #------------------------------------------------------------------------------ |
---|
12 | |
---|
13 | import os |
---|
14 | import sys |
---|
15 | import time |
---|
16 | import numpy as num |
---|
17 | |
---|
18 | #------------------------ |
---|
19 | # ANUGA Modules |
---|
20 | #------------------------ |
---|
21 | from anuga import Reflective_boundary |
---|
22 | from anuga import Transmissive_n_momentum_zero_t_momentum_set_stage_boundary |
---|
23 | |
---|
24 | from anuga import create_domain_from_file |
---|
25 | |
---|
26 | from anuga import distribute, myid, numprocs, finalize, barrier |
---|
27 | |
---|
28 | |
---|
29 | #-------------------------------------------------------------------------- |
---|
30 | # Setup parameters |
---|
31 | #-------------------------------------------------------------------------- |
---|
32 | |
---|
33 | verbose = False |
---|
34 | domain_name = 'checkpoint' |
---|
35 | checkpoint_dir = 'CHECKPOINTS' |
---|
36 | finaltime = 1000.0 |
---|
37 | useCheckpointing = True |
---|
38 | |
---|
39 | mesh_filename = "merimbula_10785_1.tsh" |
---|
40 | x0 = 756000.0 |
---|
41 | x1 = 756500.0 |
---|
42 | yieldstep = 50 |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | #-------------------------------------------------------------------------- |
---|
47 | # Setup procedures |
---|
48 | #-------------------------------------------------------------------------- |
---|
49 | class Set_Stage: |
---|
50 | """Set an initial condition with constant water height, for x0<x<x1 |
---|
51 | """ |
---|
52 | |
---|
53 | def __init__(self, x0=0.25, x1=0.5, h=1.0): |
---|
54 | self.x0 = x0 |
---|
55 | self.x1 = x1 |
---|
56 | self.h = h |
---|
57 | |
---|
58 | def __call__(self, x, y): |
---|
59 | return self.h*((x>self.x0)&(x<self.x1))+1.0 |
---|
60 | |
---|
61 | |
---|
62 | class Set_Elevation: |
---|
63 | """Set an elevation |
---|
64 | """ |
---|
65 | |
---|
66 | def __init__(self, h=1.0): |
---|
67 | self.x0 = x0 |
---|
68 | self.x1 = x1 |
---|
69 | self.h = h |
---|
70 | |
---|
71 | def __call__(self, x, y): |
---|
72 | return x/self.h |
---|
73 | |
---|
74 | def wave(t): |
---|
75 | from math import sin |
---|
76 | return sin(t/200) |
---|
77 | |
---|
78 | #------------------------------------------------------------------------------ |
---|
79 | # Use a try statement to read in previous checkpoint file and if not possible |
---|
80 | # just go ahead as normal and produce domain as usual. |
---|
81 | # |
---|
82 | # Though in the part of the code where you create the domain as normal, |
---|
83 | # remember to turn on checkpointing via domain.set_checkpointing(checkpoint_time = 5) |
---|
84 | # (see code below) |
---|
85 | #------------------------------------------------------------------------------ |
---|
86 | try: |
---|
87 | |
---|
88 | from anuga import load_checkpoint_file |
---|
89 | |
---|
90 | domain = load_checkpoint_file(domain_name = domain_name, checkpoint_dir = checkpoint_dir) |
---|
91 | |
---|
92 | except: |
---|
93 | #-------------------------------------------------------------------------- |
---|
94 | # Normal Setup of Domain on processor 0 |
---|
95 | #-------------------------------------------------------------------------- |
---|
96 | if myid == 0: |
---|
97 | domain = create_domain_from_file(mesh_filename) |
---|
98 | domain.set_quantity('stage', Set_Stage(x0, x1, 1.0)) |
---|
99 | |
---|
100 | domain.set_name(domain_name) |
---|
101 | domain.set_store(True) |
---|
102 | |
---|
103 | domain.set_store_vertices_smoothly(False) |
---|
104 | else: |
---|
105 | domain = None |
---|
106 | |
---|
107 | #-------------------------------------------------------------------------- |
---|
108 | # Distribute sequential domain on processor 0 to other processors |
---|
109 | #-------------------------------------------------------------------------- |
---|
110 | |
---|
111 | if myid == 0 and verbose: print 'DISTRIBUTING DOMAIN' |
---|
112 | domain = distribute(domain, verbose=verbose) |
---|
113 | |
---|
114 | #-------------------------------------------------------------------------- |
---|
115 | # On all processors, setup evolve parameters for domains on all processors |
---|
116 | # (all called "domain" |
---|
117 | #-------------------------------------------------------------------------- |
---|
118 | |
---|
119 | domain.set_flow_algorithm('DE0') |
---|
120 | domain.set_store_centroids() |
---|
121 | |
---|
122 | domain.set_quantities_to_be_stored({'elevation':1, |
---|
123 | 'friction':1, |
---|
124 | 'stage':2, |
---|
125 | 'xmomentum':2, |
---|
126 | 'ymomentum':2}) |
---|
127 | |
---|
128 | #------------------------------------------------------------------------------ |
---|
129 | # Setup boundary conditions |
---|
130 | # This must currently happen *after* domain has been distributed |
---|
131 | #------------------------------------------------------------------------------ |
---|
132 | Br = Reflective_boundary(domain) # Solid reflective wall |
---|
133 | |
---|
134 | Bts = Transmissive_n_momentum_zero_t_momentum_set_stage_boundary(domain, wave) |
---|
135 | |
---|
136 | domain.set_boundary({'outflow' :Br, 'inflow' :Br, 'inner' :Br, 'exterior' :Br, 'open' :Bts}) |
---|
137 | |
---|
138 | |
---|
139 | #----------------------------------------------------------------------------- |
---|
140 | # Turn on checkpointing every 5 sec (just for testing, more reasonable to |
---|
141 | # set to 15 minutes = 15*60 sec |
---|
142 | #----------------------------------------------------------------------------- |
---|
143 | if useCheckpointing: |
---|
144 | domain.set_checkpointing(checkpoint_time = 5) |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | #------------------------------------------------------------------------------ |
---|
149 | # Evolution |
---|
150 | #------------------------------------------------------------------------------ |
---|
151 | if myid == 0 and verbose: print 'EVOLVE' |
---|
152 | |
---|
153 | barrier() |
---|
154 | import time |
---|
155 | t0 = time.time() |
---|
156 | |
---|
157 | |
---|
158 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
159 | if myid == 0: domain.write_time() |
---|
160 | |
---|
161 | |
---|
162 | barrier() |
---|
163 | |
---|
164 | for p in range(numprocs): |
---|
165 | if myid == p: |
---|
166 | print 'Processor %g ' %myid |
---|
167 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
168 | print 'Communication time %.2f seconds'%domain.communication_time |
---|
169 | print 'Reduction Communication time %.2f seconds'%domain.communication_reduce_time |
---|
170 | print 'Broadcast time %.2f seconds'%domain.communication_broadcast_time |
---|
171 | else: |
---|
172 | pass |
---|
173 | |
---|
174 | barrier() |
---|
175 | |
---|
176 | |
---|
177 | #-------------------------------------------------- |
---|
178 | # Merge the individual sww files into one file |
---|
179 | # But don't delete the sub domain sww files |
---|
180 | #-------------------------------------------------- |
---|
181 | domain.sww_merge() |
---|
182 | |
---|
183 | |
---|
184 | finalize() |
---|