1 | """Class Domain - 2D triangular domains for finite-volume computations of |
---|
2 | conservation laws. |
---|
3 | |
---|
4 | |
---|
5 | Copyright 2004 |
---|
6 | Ole Nielsen, Stephen Roberts, Duncan Gray |
---|
7 | Geoscience Australia |
---|
8 | """ |
---|
9 | |
---|
10 | from Numeric import allclose, argmax, zeros, Float |
---|
11 | from anuga.config import epsilon |
---|
12 | from anuga.config import beta_euler, beta_rk2 |
---|
13 | |
---|
14 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
15 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
16 | import Boundary |
---|
17 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
18 | import File_boundary |
---|
19 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
20 | import Dirichlet_boundary |
---|
21 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
22 | import Time_boundary |
---|
23 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
24 | import Transmissive_boundary |
---|
25 | |
---|
26 | from anuga.abstract_2d_finite_volumes.pmesh2domain import pmesh_to_domain |
---|
27 | from anuga.abstract_2d_finite_volumes.region\ |
---|
28 | import Set_region as region_set_region |
---|
29 | |
---|
30 | from anuga.utilities.polygon import inside_polygon |
---|
31 | from anuga.abstract_2d_finite_volumes.util import get_textual_float |
---|
32 | |
---|
33 | import types |
---|
34 | from time import time as walltime |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | class Domain(Mesh): |
---|
39 | |
---|
40 | |
---|
41 | def __init__(self, |
---|
42 | source=None, |
---|
43 | triangles=None, |
---|
44 | boundary=None, |
---|
45 | conserved_quantities=None, |
---|
46 | other_quantities=None, |
---|
47 | tagged_elements=None, |
---|
48 | geo_reference=None, |
---|
49 | use_inscribed_circle=False, |
---|
50 | mesh_filename=None, |
---|
51 | use_cache=False, |
---|
52 | verbose=False, |
---|
53 | full_send_dict=None, |
---|
54 | ghost_recv_dict=None, |
---|
55 | processor=0, |
---|
56 | numproc=1, |
---|
57 | number_of_full_nodes=None, |
---|
58 | number_of_full_triangles=None): |
---|
59 | |
---|
60 | |
---|
61 | """Instantiate generic computational Domain. |
---|
62 | |
---|
63 | Input: |
---|
64 | source: Either a mesh filename or coordinates of mesh vertices. |
---|
65 | If it is a filename values specified for triangles will |
---|
66 | be overridden. |
---|
67 | triangles: Mesh connectivity (see mesh.py for more information) |
---|
68 | boundary: See mesh.py for more information |
---|
69 | |
---|
70 | conserved_quantities: List of quantity names entering the |
---|
71 | conservation equations |
---|
72 | other_quantities: List of other quantity names |
---|
73 | |
---|
74 | tagged_elements: |
---|
75 | ... |
---|
76 | |
---|
77 | |
---|
78 | """ |
---|
79 | |
---|
80 | # Determine whether source is a mesh filename or coordinates |
---|
81 | if type(source) == types.StringType: |
---|
82 | mesh_filename = source |
---|
83 | else: |
---|
84 | coordinates = source |
---|
85 | |
---|
86 | |
---|
87 | # In case a filename has been specified, extract content |
---|
88 | if mesh_filename is not None: |
---|
89 | coordinates, triangles, boundary, vertex_quantity_dict, \ |
---|
90 | tagged_elements, geo_reference = \ |
---|
91 | pmesh_to_domain(file_name=mesh_filename, |
---|
92 | use_cache=use_cache, |
---|
93 | verbose=verbose) |
---|
94 | |
---|
95 | |
---|
96 | # Initialise underlying mesh structure |
---|
97 | Mesh.__init__(self, coordinates, triangles, |
---|
98 | boundary=boundary, |
---|
99 | tagged_elements=tagged_elements, |
---|
100 | geo_reference=geo_reference, |
---|
101 | use_inscribed_circle=use_inscribed_circle, |
---|
102 | number_of_full_nodes=number_of_full_nodes, |
---|
103 | number_of_full_triangles=number_of_full_triangles, |
---|
104 | verbose=verbose) |
---|
105 | |
---|
106 | if verbose: print 'Initialising Domain' |
---|
107 | from Numeric import zeros, Float, Int, ones |
---|
108 | from quantity import Quantity |
---|
109 | |
---|
110 | # List of quantity names entering |
---|
111 | # the conservation equations |
---|
112 | if conserved_quantities is None: |
---|
113 | self.conserved_quantities = [] |
---|
114 | else: |
---|
115 | self.conserved_quantities = conserved_quantities |
---|
116 | |
---|
117 | # List of other quantity names |
---|
118 | if other_quantities is None: |
---|
119 | self.other_quantities = [] |
---|
120 | else: |
---|
121 | self.other_quantities = other_quantities |
---|
122 | |
---|
123 | |
---|
124 | #Build dictionary of Quantity instances keyed by quantity names |
---|
125 | self.quantities = {} |
---|
126 | |
---|
127 | #FIXME: remove later - maybe OK, though.... |
---|
128 | for name in self.conserved_quantities: |
---|
129 | self.quantities[name] = Quantity(self) |
---|
130 | for name in self.other_quantities: |
---|
131 | self.quantities[name] = Quantity(self) |
---|
132 | |
---|
133 | #Create an empty list for explicit forcing terms |
---|
134 | self.forcing_terms = [] |
---|
135 | |
---|
136 | #Setup the ghost cell communication |
---|
137 | if full_send_dict is None: |
---|
138 | self.full_send_dict = {} |
---|
139 | else: |
---|
140 | self.full_send_dict = full_send_dict |
---|
141 | |
---|
142 | # List of other quantity names |
---|
143 | if ghost_recv_dict is None: |
---|
144 | self.ghost_recv_dict = {} |
---|
145 | else: |
---|
146 | self.ghost_recv_dict = ghost_recv_dict |
---|
147 | |
---|
148 | self.processor = processor |
---|
149 | self.numproc = numproc |
---|
150 | |
---|
151 | |
---|
152 | # Setup Communication Buffers |
---|
153 | if verbose: print 'Domain: Set up communication buffers (parallel)' |
---|
154 | self.nsys = len(self.conserved_quantities) |
---|
155 | for key in self.full_send_dict: |
---|
156 | buffer_shape = self.full_send_dict[key][0].shape[0] |
---|
157 | self.full_send_dict[key].append(zeros( (buffer_shape,self.nsys) ,Float)) |
---|
158 | |
---|
159 | |
---|
160 | for key in self.ghost_recv_dict: |
---|
161 | buffer_shape = self.ghost_recv_dict[key][0].shape[0] |
---|
162 | self.ghost_recv_dict[key].append(zeros( (buffer_shape,self.nsys) ,Float)) |
---|
163 | |
---|
164 | |
---|
165 | # Setup cell full flag |
---|
166 | # =1 for full |
---|
167 | # =0 for ghost |
---|
168 | N = len(self) #number_of_elements |
---|
169 | self.number_of_elements = N |
---|
170 | self.tri_full_flag = ones(N, Int) |
---|
171 | for i in self.ghost_recv_dict.keys(): |
---|
172 | for id in self.ghost_recv_dict[i][0]: |
---|
173 | self.tri_full_flag[id] = 0 |
---|
174 | |
---|
175 | # Test the assumption that all full triangles are store before |
---|
176 | # the ghost triangles. |
---|
177 | if not allclose(self.tri_full_flag[:self.number_of_full_nodes],1): |
---|
178 | print 'WARNING: Not all full triangles are store before ghost triangles' |
---|
179 | |
---|
180 | |
---|
181 | # Defaults |
---|
182 | from anuga.config import max_smallsteps, beta_w, beta_h, epsilon |
---|
183 | from anuga.config import CFL |
---|
184 | from anuga.config import timestepping_method |
---|
185 | from anuga.config import protect_against_isolated_degenerate_timesteps |
---|
186 | self.beta_w = beta_w |
---|
187 | self.beta_h = beta_h |
---|
188 | self.epsilon = epsilon |
---|
189 | self.protect_against_isolated_degenerate_timesteps = protect_against_isolated_degenerate_timesteps |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | # FIXME: Maybe have separate orders for h-limiter and w-limiter? |
---|
194 | # Or maybe get rid of order altogether and use beta_w and beta_h |
---|
195 | # FIXME (Ole): In any case, this should appear in the config file - not here |
---|
196 | self.set_default_order(1) |
---|
197 | |
---|
198 | self.smallsteps = 0 |
---|
199 | self.max_smallsteps = max_smallsteps |
---|
200 | self.number_of_steps = 0 |
---|
201 | self.number_of_first_order_steps = 0 |
---|
202 | self.CFL = CFL |
---|
203 | self.set_timestepping_method(timestepping_method) |
---|
204 | |
---|
205 | self.boundary_map = None # Will be populated by set_boundary |
---|
206 | |
---|
207 | |
---|
208 | # Model time |
---|
209 | self.time = 0.0 |
---|
210 | self.finaltime = None |
---|
211 | self.min_timestep = self.max_timestep = 0.0 |
---|
212 | self.starttime = 0 # Physical starttime if any |
---|
213 | # (0 is 1 Jan 1970 00:00:00) |
---|
214 | self.timestep = 0.0 |
---|
215 | self.flux_timestep = 0.0 |
---|
216 | |
---|
217 | self.last_walltime = walltime() |
---|
218 | |
---|
219 | # Monitoring |
---|
220 | self.quantities_to_be_monitored = None |
---|
221 | self.monitor_polygon = None |
---|
222 | self.monitor_time_interval = None |
---|
223 | self.monitor_indices = None |
---|
224 | |
---|
225 | # Checkpointing and storage |
---|
226 | from anuga.config import default_datadir |
---|
227 | self.datadir = default_datadir |
---|
228 | self.simulation_name = 'domain' |
---|
229 | self.checkpoint = False |
---|
230 | |
---|
231 | # To avoid calculating the flux across each edge twice, |
---|
232 | # keep an integer (boolean) array, to be used during the flux |
---|
233 | # calculation |
---|
234 | N = len(self) # Number_of_triangles |
---|
235 | self.already_computed_flux = zeros((N, 3), Int) |
---|
236 | |
---|
237 | # Storage for maximal speeds computed for each triangle by |
---|
238 | # compute_fluxes |
---|
239 | # This is used for diagnostics only (reset at every yieldstep) |
---|
240 | self.max_speed = zeros(N, Float) |
---|
241 | |
---|
242 | if mesh_filename is not None: |
---|
243 | # If the mesh file passed any quantity values |
---|
244 | # , initialise with these values. |
---|
245 | if verbose: print 'Domain: Initialising quantity values' |
---|
246 | self.set_quantity_vertices_dict(vertex_quantity_dict) |
---|
247 | |
---|
248 | |
---|
249 | if verbose: print 'Domain: Done' |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | #--------------------------- |
---|
257 | # Public interface to Domain |
---|
258 | #--------------------------- |
---|
259 | def get_conserved_quantities(self, vol_id, vertex=None, edge=None): |
---|
260 | """Get conserved quantities at volume vol_id |
---|
261 | |
---|
262 | If vertex is specified use it as index for vertex values |
---|
263 | If edge is specified use it as index for edge values |
---|
264 | If neither are specified use centroid values |
---|
265 | If both are specified an exeception is raised |
---|
266 | |
---|
267 | Return value: Vector of length == number_of_conserved quantities |
---|
268 | |
---|
269 | """ |
---|
270 | |
---|
271 | from Numeric import zeros, Float |
---|
272 | |
---|
273 | if not (vertex is None or edge is None): |
---|
274 | msg = 'Values for both vertex and edge was specified.' |
---|
275 | msg += 'Only one (or none) is allowed.' |
---|
276 | raise msg |
---|
277 | |
---|
278 | q = zeros( len(self.conserved_quantities), Float) |
---|
279 | |
---|
280 | for i, name in enumerate(self.conserved_quantities): |
---|
281 | Q = self.quantities[name] |
---|
282 | if vertex is not None: |
---|
283 | q[i] = Q.vertex_values[vol_id, vertex] |
---|
284 | elif edge is not None: |
---|
285 | q[i] = Q.edge_values[vol_id, edge] |
---|
286 | else: |
---|
287 | q[i] = Q.centroid_values[vol_id] |
---|
288 | |
---|
289 | return q |
---|
290 | |
---|
291 | def set_time(self, time=0.0): |
---|
292 | """Set the model time (seconds)""" |
---|
293 | |
---|
294 | self.time = time |
---|
295 | |
---|
296 | def get_time(self): |
---|
297 | """Get the model time (seconds)""" |
---|
298 | |
---|
299 | return self.time |
---|
300 | |
---|
301 | def set_default_order(self, n): |
---|
302 | """Set default (spatial) order to either 1 or 2 |
---|
303 | """ |
---|
304 | |
---|
305 | msg = 'Default order must be either 1 or 2. I got %s' %n |
---|
306 | assert n in [1,2], msg |
---|
307 | |
---|
308 | self.default_order = n |
---|
309 | self._order_ = self.default_order |
---|
310 | |
---|
311 | if self.default_order == 1: |
---|
312 | pass |
---|
313 | #self.set_timestepping_method('euler') |
---|
314 | #self.set_all_limiters(beta_euler) |
---|
315 | |
---|
316 | if self.default_order == 2: |
---|
317 | pass |
---|
318 | #self.set_timestepping_method('rk2') |
---|
319 | #self.set_all_limiters(beta_rk2) |
---|
320 | |
---|
321 | |
---|
322 | def set_quantity_vertices_dict(self, quantity_dict): |
---|
323 | """Set values for named quantities. |
---|
324 | The index is the quantity |
---|
325 | |
---|
326 | name: Name of quantity |
---|
327 | X: Compatible list, Numeric array, const or function (see below) |
---|
328 | |
---|
329 | The values will be stored in elements following their |
---|
330 | internal ordering. |
---|
331 | |
---|
332 | """ |
---|
333 | |
---|
334 | # FIXME: Could we name this a bit more intuitively |
---|
335 | # E.g. set_quantities_from_dictionary |
---|
336 | for key in quantity_dict.keys(): |
---|
337 | self.set_quantity(key, quantity_dict[key], location='vertices') |
---|
338 | |
---|
339 | |
---|
340 | def set_quantity(self, name, *args, **kwargs): |
---|
341 | """Set values for named quantity |
---|
342 | |
---|
343 | |
---|
344 | One keyword argument is documented here: |
---|
345 | expression = None, # Arbitrary expression |
---|
346 | |
---|
347 | expression: |
---|
348 | Arbitrary expression involving quantity names |
---|
349 | |
---|
350 | See Quantity.set_values for further documentation. |
---|
351 | """ |
---|
352 | |
---|
353 | # Do the expression stuff |
---|
354 | if kwargs.has_key('expression'): |
---|
355 | expression = kwargs['expression'] |
---|
356 | del kwargs['expression'] |
---|
357 | |
---|
358 | Q = self.create_quantity_from_expression(expression) |
---|
359 | kwargs['quantity'] = Q |
---|
360 | |
---|
361 | # Assign values |
---|
362 | self.quantities[name].set_values(*args, **kwargs) |
---|
363 | |
---|
364 | |
---|
365 | def get_quantity_names(self): |
---|
366 | """Get a list of all the quantity names that this domain is aware of. |
---|
367 | Any value in the result should be a valid input to get_quantity. |
---|
368 | """ |
---|
369 | return self.quantities.keys() |
---|
370 | |
---|
371 | def get_quantity(self, name, location='vertices', indices = None): |
---|
372 | """Get quantity object. |
---|
373 | |
---|
374 | name: Name of quantity |
---|
375 | |
---|
376 | See methods inside the quantity object for more options |
---|
377 | |
---|
378 | FIXME: clean input args |
---|
379 | """ |
---|
380 | |
---|
381 | return self.quantities[name] #.get_values( location, indices = indices) |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | def create_quantity_from_expression(self, expression): |
---|
386 | """Create new quantity from other quantities using arbitrary expression |
---|
387 | |
---|
388 | Combine existing quantities in domain using expression and return |
---|
389 | result as a new quantity. |
---|
390 | |
---|
391 | Note, the new quantity could e.g. be used in set_quantity |
---|
392 | |
---|
393 | Valid expressions are limited to operators defined in class Quantity |
---|
394 | |
---|
395 | Examples creating derived quantities: |
---|
396 | |
---|
397 | Depth = domain.create_quantity_from_expression('stage-elevation') |
---|
398 | |
---|
399 | exp = '(xmomentum*xmomentum + ymomentum*ymomentum)**0.5' |
---|
400 | Absolute_momentum = domain.create_quantity_from_expression(exp) |
---|
401 | |
---|
402 | """ |
---|
403 | |
---|
404 | from anuga.abstract_2d_finite_volumes.util import\ |
---|
405 | apply_expression_to_dictionary |
---|
406 | |
---|
407 | return apply_expression_to_dictionary(expression, self.quantities) |
---|
408 | |
---|
409 | |
---|
410 | |
---|
411 | def set_boundary(self, boundary_map): |
---|
412 | """Associate boundary objects with tagged boundary segments. |
---|
413 | |
---|
414 | Input boundary_map is a dictionary of boundary objects keyed |
---|
415 | by symbolic tags to matched against tags in the internal dictionary |
---|
416 | self.boundary. |
---|
417 | |
---|
418 | As result one pointer to a boundary object is stored for each vertex |
---|
419 | in the list self.boundary_objects. |
---|
420 | More entries may point to the same boundary object |
---|
421 | |
---|
422 | Schematically the mapping is from two dictionaries to one list |
---|
423 | where the index is used as pointer to the boundary_values arrays |
---|
424 | within each quantity. |
---|
425 | |
---|
426 | self.boundary: (vol_id, edge_id): tag |
---|
427 | boundary_map (input): tag: boundary_object |
---|
428 | ---------------------------------------------- |
---|
429 | self.boundary_objects: ((vol_id, edge_id), boundary_object) |
---|
430 | |
---|
431 | |
---|
432 | Pre-condition: |
---|
433 | self.boundary has been built. |
---|
434 | |
---|
435 | Post-condition: |
---|
436 | self.boundary_objects is built |
---|
437 | |
---|
438 | If a tag from the domain doesn't appear in the input dictionary an |
---|
439 | exception is raised. |
---|
440 | However, if a tag is not used to the domain, no error is thrown. |
---|
441 | FIXME: This would lead to implementation of a |
---|
442 | default boundary condition |
---|
443 | |
---|
444 | Note: If a segment is listed in the boundary dictionary and if it is |
---|
445 | not None, it *will* become a boundary - |
---|
446 | even if there is a neighbouring triangle. |
---|
447 | This would be the case for internal boundaries |
---|
448 | |
---|
449 | Boundary objects that are None will be skipped. |
---|
450 | |
---|
451 | If a boundary_map has already been set |
---|
452 | (i.e. set_boundary has been called before), the old boundary map |
---|
453 | will be updated with new values. The new map need not define all |
---|
454 | boundary tags, and can thus change only those that are needed. |
---|
455 | |
---|
456 | FIXME: If set_boundary is called multiple times and if Boundary |
---|
457 | object is changed into None, the neighbour structure will not be |
---|
458 | restored!!! |
---|
459 | |
---|
460 | |
---|
461 | """ |
---|
462 | |
---|
463 | if self.boundary_map is None: |
---|
464 | # This the first call to set_boundary. Store |
---|
465 | # map for later updates and for use with boundary_stats. |
---|
466 | self.boundary_map = boundary_map |
---|
467 | else: |
---|
468 | # This is a modification of an already existing map |
---|
469 | # Update map an proceed normally |
---|
470 | |
---|
471 | for key in boundary_map.keys(): |
---|
472 | self.boundary_map[key] = boundary_map[key] |
---|
473 | |
---|
474 | |
---|
475 | # FIXME (Ole): Try to remove the sorting and fix test_mesh.py |
---|
476 | x = self.boundary.keys() |
---|
477 | x.sort() |
---|
478 | |
---|
479 | # Loop through edges that lie on the boundary and associate them with |
---|
480 | # callable boundary objects depending on their tags |
---|
481 | self.boundary_objects = [] |
---|
482 | for k, (vol_id, edge_id) in enumerate(x): |
---|
483 | tag = self.boundary[ (vol_id, edge_id) ] |
---|
484 | |
---|
485 | if self.boundary_map.has_key(tag): |
---|
486 | B = self.boundary_map[tag] # Get callable boundary object |
---|
487 | |
---|
488 | if B is not None: |
---|
489 | self.boundary_objects.append( ((vol_id, edge_id), B) ) |
---|
490 | self.neighbours[vol_id, edge_id] = \ |
---|
491 | -len(self.boundary_objects) |
---|
492 | else: |
---|
493 | pass |
---|
494 | #FIXME: Check and perhaps fix neighbour structure |
---|
495 | |
---|
496 | |
---|
497 | else: |
---|
498 | msg = 'ERROR (domain.py): Tag "%s" has not been ' %tag |
---|
499 | msg += 'bound to a boundary object.\n' |
---|
500 | msg += 'All boundary tags defined in domain must appear ' |
---|
501 | msg += 'in set_boundary.\n' |
---|
502 | msg += 'The tags are: %s' %self.get_boundary_tags() |
---|
503 | raise msg |
---|
504 | |
---|
505 | |
---|
506 | def set_region(self, *args, **kwargs): |
---|
507 | """ |
---|
508 | This method is used to set quantities based on a regional tag. |
---|
509 | |
---|
510 | It is most often called with the following parameters; |
---|
511 | (self, tag, quantity, X, location='vertices') |
---|
512 | tag: the name of the regional tag used to specify the region |
---|
513 | quantity: Name of quantity to change |
---|
514 | X: const or function - how the quantity is changed |
---|
515 | location: Where values are to be stored. |
---|
516 | Permissible options are: vertices, centroid and unique vertices |
---|
517 | |
---|
518 | A callable region class or a list of callable region classes |
---|
519 | can also be passed into this function. |
---|
520 | """ |
---|
521 | |
---|
522 | if len(args) == 1: |
---|
523 | self._set_region(*args, **kwargs) |
---|
524 | else: |
---|
525 | # Assume it is arguments for the region.set_region function |
---|
526 | func = region_set_region(*args, **kwargs) |
---|
527 | self._set_region(func) |
---|
528 | |
---|
529 | |
---|
530 | def _set_region(self, functions): |
---|
531 | # The order of functions in the list is used. |
---|
532 | if type(functions) not in [types.ListType,types.TupleType]: |
---|
533 | functions = [functions] |
---|
534 | for function in functions: |
---|
535 | for tag in self.tagged_elements.keys(): |
---|
536 | function(tag, self.tagged_elements[tag], self) |
---|
537 | |
---|
538 | |
---|
539 | |
---|
540 | |
---|
541 | def set_quantities_to_be_monitored(self, q, |
---|
542 | polygon=None, |
---|
543 | time_interval=None): |
---|
544 | """Specify which quantities will be monitored for extrema. |
---|
545 | |
---|
546 | q must be either: |
---|
547 | - the name of a quantity or a derived quantity such as 'stage-elevation' |
---|
548 | - a list of quantity names |
---|
549 | - None |
---|
550 | |
---|
551 | In the two first cases, the named quantities will be monitored at |
---|
552 | each internal timestep |
---|
553 | |
---|
554 | If q is None, monitoring will be switched off altogether. |
---|
555 | |
---|
556 | polygon (if specified) will restrict monitoring to triangles inside polygon. |
---|
557 | If omitted all triangles will be included. |
---|
558 | |
---|
559 | time_interval (if specified) will restrict monitoring to time steps in |
---|
560 | that interval. If omitted all timesteps will be included. |
---|
561 | """ |
---|
562 | |
---|
563 | from anuga.abstract_2d_finite_volumes.util import\ |
---|
564 | apply_expression_to_dictionary |
---|
565 | |
---|
566 | if q is None: |
---|
567 | self.quantities_to_be_monitored = None |
---|
568 | self.monitor_polygon = None |
---|
569 | self.monitor_time_interval = None |
---|
570 | self.monitor_indices = None |
---|
571 | return |
---|
572 | |
---|
573 | if isinstance(q, basestring): |
---|
574 | q = [q] # Turn argument into a list |
---|
575 | |
---|
576 | # Check correcness and initialise |
---|
577 | self.quantities_to_be_monitored = {} |
---|
578 | for quantity_name in q: |
---|
579 | msg = 'Quantity %s is not a valid conserved quantity'\ |
---|
580 | %quantity_name |
---|
581 | |
---|
582 | |
---|
583 | if not quantity_name in self.quantities: |
---|
584 | # See if this expression is valid |
---|
585 | apply_expression_to_dictionary(quantity_name, self.quantities) |
---|
586 | |
---|
587 | # Initialise extrema information |
---|
588 | info_block = {'min': None, # Min value |
---|
589 | 'max': None, # Max value |
---|
590 | 'min_location': None, # Argmin (x, y) |
---|
591 | 'max_location': None, # Argmax (x, y) |
---|
592 | 'min_time': None, # Argmin (t) |
---|
593 | 'max_time': None} # Argmax (t) |
---|
594 | |
---|
595 | self.quantities_to_be_monitored[quantity_name] = info_block |
---|
596 | |
---|
597 | |
---|
598 | |
---|
599 | if polygon is not None: |
---|
600 | # Check input |
---|
601 | if isinstance(polygon, basestring): |
---|
602 | |
---|
603 | # Check if multiple quantities were accidentally |
---|
604 | # given as separate argument rather than a list. |
---|
605 | msg = 'Multiple quantities must be specified in a list. ' |
---|
606 | msg += 'Not as multiple arguments. ' |
---|
607 | msg += 'I got "%s" as a second argument' %polygon |
---|
608 | |
---|
609 | if polygon in self.quantities: |
---|
610 | raise Exception, msg |
---|
611 | |
---|
612 | try: |
---|
613 | apply_expression_to_dictionary(polygon, self.quantities) |
---|
614 | except: |
---|
615 | # At least polygon wasn't an expression involving quantitites |
---|
616 | pass |
---|
617 | else: |
---|
618 | raise Exception, msg |
---|
619 | |
---|
620 | # In any case, we don't allow polygon to be a string |
---|
621 | msg = 'argument "polygon" must not be a string: ' |
---|
622 | msg += 'I got polygon=\'%s\' ' %polygon |
---|
623 | raise Exception, msg |
---|
624 | |
---|
625 | |
---|
626 | # Get indices for centroids that are inside polygon |
---|
627 | points = self.get_centroid_coordinates(absolute=True) |
---|
628 | self.monitor_indices = inside_polygon(points, polygon) |
---|
629 | |
---|
630 | |
---|
631 | if time_interval is not None: |
---|
632 | assert len(time_interval) == 2 |
---|
633 | |
---|
634 | |
---|
635 | self.monitor_polygon = polygon |
---|
636 | self.monitor_time_interval = time_interval |
---|
637 | |
---|
638 | |
---|
639 | #-------------------------- |
---|
640 | # Miscellaneous diagnostics |
---|
641 | #-------------------------- |
---|
642 | def check_integrity(self): |
---|
643 | Mesh.check_integrity(self) |
---|
644 | |
---|
645 | for quantity in self.conserved_quantities: |
---|
646 | msg = 'Conserved quantities must be a subset of all quantities' |
---|
647 | assert quantity in self.quantities, msg |
---|
648 | |
---|
649 | ##assert hasattr(self, 'boundary_objects') |
---|
650 | |
---|
651 | |
---|
652 | def write_time(self, track_speeds=False): |
---|
653 | print self.timestepping_statistics(track_speeds) |
---|
654 | |
---|
655 | |
---|
656 | def timestepping_statistics(self, |
---|
657 | track_speeds=False, |
---|
658 | triangle_id=None): |
---|
659 | """Return string with time stepping statistics for printing or logging |
---|
660 | |
---|
661 | Optional boolean keyword track_speeds decides whether to report |
---|
662 | location of smallest timestep as well as a histogram and percentile |
---|
663 | report. |
---|
664 | |
---|
665 | Optional keyword triangle_id can be used to specify a particular |
---|
666 | triangle rather than the one with the largest speed. |
---|
667 | """ |
---|
668 | |
---|
669 | from anuga.utilities.numerical_tools import histogram, create_bins |
---|
670 | |
---|
671 | |
---|
672 | # qwidth determines the text field used for quantities |
---|
673 | qwidth = self.qwidth = 12 |
---|
674 | |
---|
675 | |
---|
676 | msg = '' |
---|
677 | #if self.min_timestep == self.max_timestep: |
---|
678 | # msg += 'Time = %.4f, delta t = %.8f, steps=%d (%d)'\ |
---|
679 | # %(self.time, self.min_timestep, self.number_of_steps, |
---|
680 | # self.number_of_first_order_steps) |
---|
681 | #elif self.min_timestep > self.max_timestep: |
---|
682 | # msg += 'Time = %.4f, steps=%d (%d)'\ |
---|
683 | # %(self.time, self.number_of_steps, |
---|
684 | # self.number_of_first_order_steps) |
---|
685 | #else: |
---|
686 | # msg += 'Time = %.4f, delta t in [%.8f, %.8f], steps=%d (%d)'\ |
---|
687 | # %(self.time, self.min_timestep, |
---|
688 | # self.max_timestep, self.number_of_steps, |
---|
689 | # self.number_of_first_order_steps) |
---|
690 | |
---|
691 | |
---|
692 | if self.min_timestep == self.max_timestep: |
---|
693 | msg += 'Time = %.4f, delta t = %.8f, steps=%d'\ |
---|
694 | %(self.time, self.min_timestep, self.number_of_steps) |
---|
695 | elif self.min_timestep > self.max_timestep: |
---|
696 | msg += 'Time = %.4f, steps=%d'\ |
---|
697 | %(self.time, self.number_of_steps) |
---|
698 | else: |
---|
699 | msg += 'Time = %.4f, delta t in [%.8f, %.8f], steps=%d'\ |
---|
700 | %(self.time, self.min_timestep, |
---|
701 | self.max_timestep, self.number_of_steps) |
---|
702 | |
---|
703 | msg += ' (%ds)' %(walltime() - self.last_walltime) |
---|
704 | self.last_walltime = walltime() |
---|
705 | |
---|
706 | if track_speeds is True: |
---|
707 | msg += '\n' |
---|
708 | |
---|
709 | |
---|
710 | # Setup 10 bins for speed histogram |
---|
711 | bins = create_bins(self.max_speed, 10) |
---|
712 | hist = histogram(self.max_speed, bins) |
---|
713 | |
---|
714 | msg += '------------------------------------------------\n' |
---|
715 | msg += ' Speeds in [%f, %f]\n' %(min(self.max_speed), |
---|
716 | max(self.max_speed)) |
---|
717 | msg += ' Histogram:\n' |
---|
718 | |
---|
719 | hi = bins[0] |
---|
720 | for i, count in enumerate(hist): |
---|
721 | lo = hi |
---|
722 | if i+1 < len(bins): |
---|
723 | # Open upper interval |
---|
724 | hi = bins[i+1] |
---|
725 | msg += ' [%f, %f[: %d\n' %(lo, hi, count) |
---|
726 | else: |
---|
727 | # Closed upper interval |
---|
728 | hi = max(self.max_speed) |
---|
729 | msg += ' [%f, %f]: %d\n' %(lo, hi, count) |
---|
730 | |
---|
731 | |
---|
732 | N = len(self.max_speed) |
---|
733 | if N > 10: |
---|
734 | msg += ' Percentiles (10%):\n' |
---|
735 | speed = self.max_speed.tolist() |
---|
736 | speed.sort() |
---|
737 | |
---|
738 | k = 0 |
---|
739 | lower = min(speed) |
---|
740 | for i, a in enumerate(speed): |
---|
741 | if i % (N/10) == 0 and i != 0: |
---|
742 | # For every 10% of the sorted speeds |
---|
743 | msg += ' %d speeds in [%f, %f]\n' %(i-k, lower, a) |
---|
744 | lower = a |
---|
745 | k = i |
---|
746 | |
---|
747 | msg += ' %d speeds in [%f, %f]\n'\ |
---|
748 | %(N-k, lower, max(speed)) |
---|
749 | |
---|
750 | |
---|
751 | |
---|
752 | |
---|
753 | |
---|
754 | # Find index of largest computed flux speed |
---|
755 | if triangle_id is None: |
---|
756 | k = self.k = argmax(self.max_speed) |
---|
757 | else: |
---|
758 | errmsg = 'Triangle_id %d does not exist in mesh: %s' %(triangle_id, |
---|
759 | str(self)) |
---|
760 | assert 0 <= triangle_id < len(self), errmsg |
---|
761 | k = self.k = triangle_id |
---|
762 | |
---|
763 | |
---|
764 | x, y = self.get_centroid_coordinates()[k] |
---|
765 | radius = self.get_radii()[k] |
---|
766 | area = self.get_areas()[k] |
---|
767 | max_speed = self.max_speed[k] |
---|
768 | |
---|
769 | msg += ' Triangle #%d with centroid (%.4f, %.4f), ' %(k, x, y) |
---|
770 | msg += 'area = %.4f and radius = %.4f ' %(area, radius) |
---|
771 | if triangle_id is None: |
---|
772 | msg += 'had the largest computed speed: %.6f m/s ' %(max_speed) |
---|
773 | else: |
---|
774 | msg += 'had computed speed: %.6f m/s ' %(max_speed) |
---|
775 | |
---|
776 | if max_speed > 0.0: |
---|
777 | msg += '(timestep=%.6f)\n' %(radius/max_speed) |
---|
778 | else: |
---|
779 | msg += '(timestep=%.6f)\n' %(0) |
---|
780 | |
---|
781 | # Report all quantity values at vertices, edges and centroid |
---|
782 | msg += ' Quantity' |
---|
783 | msg += '------------\n' |
---|
784 | for name in self.quantities: |
---|
785 | q = self.quantities[name] |
---|
786 | |
---|
787 | V = q.get_values(location='vertices', indices=[k])[0] |
---|
788 | E = q.get_values(location='edges', indices=[k])[0] |
---|
789 | C = q.get_values(location='centroids', indices=[k]) |
---|
790 | |
---|
791 | s = ' %s: vertex_values = %.4f,\t %.4f,\t %.4f\n'\ |
---|
792 | %(name.ljust(qwidth), V[0], V[1], V[2]) |
---|
793 | |
---|
794 | s += ' %s: edge_values = %.4f,\t %.4f,\t %.4f\n'\ |
---|
795 | %(name.ljust(qwidth), E[0], E[1], E[2]) |
---|
796 | |
---|
797 | s += ' %s: centroid_value = %.4f\n'\ |
---|
798 | %(name.ljust(qwidth), C[0]) |
---|
799 | |
---|
800 | msg += s |
---|
801 | |
---|
802 | return msg |
---|
803 | |
---|
804 | |
---|
805 | def write_boundary_statistics(self, quantities = None, tags = None): |
---|
806 | print self.boundary_statistics(quantities, tags) |
---|
807 | |
---|
808 | def boundary_statistics(self, quantities = None, tags = None): |
---|
809 | """Output statistics about boundary forcing at each timestep |
---|
810 | |
---|
811 | |
---|
812 | Input: |
---|
813 | quantities: either None, a string or a list of strings naming the |
---|
814 | quantities to be reported |
---|
815 | tags: either None, a string or a list of strings naming the |
---|
816 | tags to be reported |
---|
817 | |
---|
818 | |
---|
819 | Example output: |
---|
820 | Tag 'wall': |
---|
821 | stage in [2, 5.5] |
---|
822 | xmomentum in [] |
---|
823 | ymomentum in [] |
---|
824 | Tag 'ocean' |
---|
825 | |
---|
826 | |
---|
827 | If quantities are specified only report on those. Otherwise take all |
---|
828 | conserved quantities. |
---|
829 | If tags are specified only report on those, otherwise take all tags. |
---|
830 | |
---|
831 | """ |
---|
832 | |
---|
833 | # Input checks |
---|
834 | import types, string |
---|
835 | |
---|
836 | if quantities is None: |
---|
837 | quantities = self.conserved_quantities |
---|
838 | elif type(quantities) == types.StringType: |
---|
839 | quantities = [quantities] #Turn it into a list |
---|
840 | |
---|
841 | msg = 'Keyword argument quantities must be either None, ' |
---|
842 | msg += 'string or list. I got %s' %str(quantities) |
---|
843 | assert type(quantities) == types.ListType, msg |
---|
844 | |
---|
845 | |
---|
846 | if tags is None: |
---|
847 | tags = self.get_boundary_tags() |
---|
848 | elif type(tags) == types.StringType: |
---|
849 | tags = [tags] #Turn it into a list |
---|
850 | |
---|
851 | msg = 'Keyword argument tags must be either None, ' |
---|
852 | msg += 'string or list. I got %s' %str(tags) |
---|
853 | assert type(tags) == types.ListType, msg |
---|
854 | |
---|
855 | # Determine width of longest quantity name (for cosmetic purposes) |
---|
856 | maxwidth = 0 |
---|
857 | for name in quantities: |
---|
858 | w = len(name) |
---|
859 | if w > maxwidth: |
---|
860 | maxwidth = w |
---|
861 | |
---|
862 | # Output statistics |
---|
863 | msg = 'Boundary values at time %.4f:\n' %self.time |
---|
864 | for tag in tags: |
---|
865 | msg += ' %s:\n' %tag |
---|
866 | |
---|
867 | for name in quantities: |
---|
868 | q = self.quantities[name] |
---|
869 | |
---|
870 | # Find range of boundary values for tag and q |
---|
871 | maxval = minval = None |
---|
872 | for i, ((vol_id, edge_id), B) in\ |
---|
873 | enumerate(self.boundary_objects): |
---|
874 | if self.boundary[(vol_id, edge_id)] == tag: |
---|
875 | v = q.boundary_values[i] |
---|
876 | if minval is None or v < minval: minval = v |
---|
877 | if maxval is None or v > maxval: maxval = v |
---|
878 | |
---|
879 | if minval is None or maxval is None: |
---|
880 | msg += ' Sorry no information available about' +\ |
---|
881 | ' tag %s and quantity %s\n' %(tag, name) |
---|
882 | else: |
---|
883 | msg += ' %s in [%12.8f, %12.8f]\n'\ |
---|
884 | %(string.ljust(name, maxwidth), minval, maxval) |
---|
885 | |
---|
886 | |
---|
887 | return msg |
---|
888 | |
---|
889 | |
---|
890 | def update_extrema(self): |
---|
891 | """Update extrema if requested by set_quantities_to_be_monitored. |
---|
892 | This data is used for reporting e.g. by running |
---|
893 | print domain.quantity_statistics() |
---|
894 | and may also stored in output files (see data_manager in shallow_water) |
---|
895 | """ |
---|
896 | |
---|
897 | # Define a tolerance for extremum computations |
---|
898 | epsilon = 1.0e-6 # Import 'single_precision' from config |
---|
899 | |
---|
900 | if self.quantities_to_be_monitored is None: |
---|
901 | return |
---|
902 | |
---|
903 | # Observe time interval restriction if any |
---|
904 | if self.monitor_time_interval is not None and\ |
---|
905 | (self.time < self.monitor_time_interval[0] or\ |
---|
906 | self.time > self.monitor_time_interval[1]): |
---|
907 | return |
---|
908 | |
---|
909 | # Update extrema for each specified quantity subject to |
---|
910 | # polygon restriction (via monitor_indices). |
---|
911 | for quantity_name in self.quantities_to_be_monitored: |
---|
912 | |
---|
913 | if quantity_name in self.quantities: |
---|
914 | Q = self.get_quantity(quantity_name) |
---|
915 | else: |
---|
916 | Q = self.create_quantity_from_expression(quantity_name) |
---|
917 | |
---|
918 | info_block = self.quantities_to_be_monitored[quantity_name] |
---|
919 | |
---|
920 | # Update maximum |
---|
921 | # (n > None is always True, but we check explicitly because |
---|
922 | # of the epsilon) |
---|
923 | maxval = Q.get_maximum_value(self.monitor_indices) |
---|
924 | if info_block['max'] is None or\ |
---|
925 | maxval > info_block['max'] + epsilon: |
---|
926 | info_block['max'] = maxval |
---|
927 | maxloc = Q.get_maximum_location() |
---|
928 | info_block['max_location'] = maxloc |
---|
929 | info_block['max_time'] = self.time |
---|
930 | |
---|
931 | |
---|
932 | # Update minimum |
---|
933 | minval = Q.get_minimum_value(self.monitor_indices) |
---|
934 | if info_block['min'] is None or\ |
---|
935 | minval < info_block['min'] - epsilon: |
---|
936 | info_block['min'] = minval |
---|
937 | minloc = Q.get_minimum_location() |
---|
938 | info_block['min_location'] = minloc |
---|
939 | info_block['min_time'] = self.time |
---|
940 | |
---|
941 | |
---|
942 | |
---|
943 | def quantity_statistics(self, precision = '%.4f'): |
---|
944 | """Return string with statistics about quantities for |
---|
945 | printing or logging |
---|
946 | |
---|
947 | Quantities reported are specified through method |
---|
948 | |
---|
949 | set_quantities_to_be_monitored |
---|
950 | |
---|
951 | """ |
---|
952 | |
---|
953 | maxlen = 128 # Max length of polygon string representation |
---|
954 | |
---|
955 | # Output statistics |
---|
956 | msg = 'Monitored quantities at time %.4f:\n' %self.time |
---|
957 | if self.monitor_polygon is not None: |
---|
958 | p_str = str(self.monitor_polygon) |
---|
959 | msg += '- Restricted by polygon: %s' %p_str[:maxlen] |
---|
960 | if len(p_str) >= maxlen: |
---|
961 | msg += '...\n' |
---|
962 | else: |
---|
963 | msg += '\n' |
---|
964 | |
---|
965 | |
---|
966 | if self.monitor_time_interval is not None: |
---|
967 | msg += '- Restricted by time interval: %s\n'\ |
---|
968 | %str(self.monitor_time_interval) |
---|
969 | time_interval_start = self.monitor_time_interval[0] |
---|
970 | else: |
---|
971 | time_interval_start = 0.0 |
---|
972 | |
---|
973 | |
---|
974 | for quantity_name, info in self.quantities_to_be_monitored.items(): |
---|
975 | msg += ' %s:\n' %quantity_name |
---|
976 | |
---|
977 | msg += ' values since time = %.2f in [%s, %s]\n'\ |
---|
978 | %(time_interval_start, |
---|
979 | get_textual_float(info['min'], precision), |
---|
980 | get_textual_float(info['max'], precision)) |
---|
981 | |
---|
982 | msg += ' minimum attained at time = %s, location = %s\n'\ |
---|
983 | %(get_textual_float(info['min_time'], precision), |
---|
984 | get_textual_float(info['min_location'], precision)) |
---|
985 | |
---|
986 | |
---|
987 | msg += ' maximum attained at time = %s, location = %s\n'\ |
---|
988 | %(get_textual_float(info['max_time'], precision), |
---|
989 | get_textual_float(info['max_location'], precision)) |
---|
990 | |
---|
991 | |
---|
992 | return msg |
---|
993 | |
---|
994 | def get_timestepping_method(self): |
---|
995 | return self.timestepping_method |
---|
996 | |
---|
997 | def set_timestepping_method(self,timestepping_method): |
---|
998 | |
---|
999 | if timestepping_method in ['euler', 'rk2', 'rk3']: |
---|
1000 | self.timestepping_method = timestepping_method |
---|
1001 | return |
---|
1002 | |
---|
1003 | msg = '%s is an incorrect timestepping type'% timestepping_method |
---|
1004 | raise Exception, msg |
---|
1005 | |
---|
1006 | def get_name(self): |
---|
1007 | return self.simulation_name |
---|
1008 | |
---|
1009 | def set_name(self, name): |
---|
1010 | """Assign a name to this simulation. |
---|
1011 | This will be used to identify the output sww file. |
---|
1012 | |
---|
1013 | """ |
---|
1014 | if name.endswith('.sww'): |
---|
1015 | name = name[:-4] |
---|
1016 | |
---|
1017 | self.simulation_name = name |
---|
1018 | |
---|
1019 | def get_datadir(self): |
---|
1020 | return self.datadir |
---|
1021 | |
---|
1022 | def set_datadir(self, name): |
---|
1023 | self.datadir = name |
---|
1024 | |
---|
1025 | def get_starttime(self): |
---|
1026 | return self.starttime |
---|
1027 | |
---|
1028 | def set_starttime(self, time): |
---|
1029 | self.starttime = float(time) |
---|
1030 | |
---|
1031 | |
---|
1032 | |
---|
1033 | #-------------------------- |
---|
1034 | # Main components of evolve |
---|
1035 | #-------------------------- |
---|
1036 | |
---|
1037 | def evolve(self, |
---|
1038 | yieldstep = None, |
---|
1039 | finaltime = None, |
---|
1040 | duration = None, |
---|
1041 | skip_initial_step = False): |
---|
1042 | """Evolve model through time starting from self.starttime. |
---|
1043 | |
---|
1044 | |
---|
1045 | yieldstep: Interval between yields where results are stored, |
---|
1046 | statistics written and domain inspected or |
---|
1047 | possibly modified. If omitted the internal predefined |
---|
1048 | max timestep is used. |
---|
1049 | Internally, smaller timesteps may be taken. |
---|
1050 | |
---|
1051 | duration: Duration of simulation |
---|
1052 | |
---|
1053 | finaltime: Time where simulation should end. This is currently |
---|
1054 | relative time. So it's the same as duration. |
---|
1055 | |
---|
1056 | If both duration and finaltime are given an exception is thrown. |
---|
1057 | |
---|
1058 | |
---|
1059 | skip_initial_step: Boolean flag that decides whether the first |
---|
1060 | yield step is skipped or not. This is useful for example to avoid |
---|
1061 | duplicate steps when multiple evolve processes are dove tailed. |
---|
1062 | |
---|
1063 | |
---|
1064 | Evolve is implemented as a generator and is to be called as such, e.g. |
---|
1065 | |
---|
1066 | for t in domain.evolve(yieldstep, finaltime): |
---|
1067 | <Do something with domain and t> |
---|
1068 | |
---|
1069 | |
---|
1070 | All times are given in seconds |
---|
1071 | |
---|
1072 | """ |
---|
1073 | |
---|
1074 | from anuga.config import min_timestep, max_timestep, epsilon |
---|
1075 | |
---|
1076 | # FIXME: Maybe lump into a larger check prior to evolving |
---|
1077 | msg = 'Boundary tags must be bound to boundary objects before ' |
---|
1078 | msg += 'evolving system, ' |
---|
1079 | msg += 'e.g. using the method set_boundary.\n' |
---|
1080 | msg += 'This system has the boundary tags %s '\ |
---|
1081 | %self.get_boundary_tags() |
---|
1082 | assert hasattr(self, 'boundary_objects'), msg |
---|
1083 | |
---|
1084 | |
---|
1085 | if yieldstep is None: |
---|
1086 | yieldstep = max_timestep |
---|
1087 | else: |
---|
1088 | yieldstep = float(yieldstep) |
---|
1089 | |
---|
1090 | self._order_ = self.default_order |
---|
1091 | |
---|
1092 | |
---|
1093 | if finaltime is not None and duration is not None: |
---|
1094 | # print 'F', finaltime, duration |
---|
1095 | msg = 'Only one of finaltime and duration may be specified' |
---|
1096 | raise msg |
---|
1097 | else: |
---|
1098 | if finaltime is not None: |
---|
1099 | self.finaltime = float(finaltime) |
---|
1100 | if duration is not None: |
---|
1101 | self.finaltime = self.starttime + float(duration) |
---|
1102 | |
---|
1103 | |
---|
1104 | |
---|
1105 | N = len(self) # Number of triangles |
---|
1106 | self.yieldtime = 0.0 # Track time between 'yields' |
---|
1107 | |
---|
1108 | # Initialise interval of timestep sizes (for reporting only) |
---|
1109 | self.min_timestep = max_timestep |
---|
1110 | self.max_timestep = min_timestep |
---|
1111 | self.number_of_steps = 0 |
---|
1112 | self.number_of_first_order_steps = 0 |
---|
1113 | |
---|
1114 | |
---|
1115 | # Update ghosts |
---|
1116 | self.update_ghosts() |
---|
1117 | |
---|
1118 | # Initial update of vertex and edge values |
---|
1119 | self.distribute_to_vertices_and_edges() |
---|
1120 | |
---|
1121 | # Update extrema if necessary (for reporting) |
---|
1122 | self.update_extrema() |
---|
1123 | |
---|
1124 | # Initial update boundary values |
---|
1125 | self.update_boundary() |
---|
1126 | |
---|
1127 | # Or maybe restore from latest checkpoint |
---|
1128 | if self.checkpoint is True: |
---|
1129 | self.goto_latest_checkpoint() |
---|
1130 | |
---|
1131 | if skip_initial_step is False: |
---|
1132 | yield(self.time) # Yield initial values |
---|
1133 | |
---|
1134 | while True: |
---|
1135 | |
---|
1136 | # Evolve One Step, using appropriate timestepping method |
---|
1137 | if self.get_timestepping_method() == 'euler': |
---|
1138 | self.evolve_one_euler_step(yieldstep,finaltime) |
---|
1139 | |
---|
1140 | elif self.get_timestepping_method() == 'rk2': |
---|
1141 | self.evolve_one_rk2_step(yieldstep,finaltime) |
---|
1142 | |
---|
1143 | elif self.get_timestepping_method() == 'rk3': |
---|
1144 | self.evolve_one_rk3_step(yieldstep,finaltime) |
---|
1145 | |
---|
1146 | # Update extrema if necessary (for reporting) |
---|
1147 | self.update_extrema() |
---|
1148 | |
---|
1149 | |
---|
1150 | self.yieldtime += self.timestep |
---|
1151 | self.number_of_steps += 1 |
---|
1152 | if self._order_ == 1: |
---|
1153 | self.number_of_first_order_steps += 1 |
---|
1154 | |
---|
1155 | # Yield results |
---|
1156 | if finaltime is not None and self.time >= finaltime-epsilon: |
---|
1157 | |
---|
1158 | if self.time > finaltime: |
---|
1159 | # FIXME (Ole, 30 April 2006): Do we need this check? |
---|
1160 | # Probably not (Ole, 18 September 2008). Now changed to |
---|
1161 | # Exception |
---|
1162 | msg = 'WARNING (domain.py): time overshot finaltime. ' |
---|
1163 | msg += 'Contact Ole.Nielsen@ga.gov.au' |
---|
1164 | raise Exception, msg |
---|
1165 | |
---|
1166 | |
---|
1167 | # Yield final time and stop |
---|
1168 | self.time = finaltime |
---|
1169 | yield(self.time) |
---|
1170 | break |
---|
1171 | |
---|
1172 | |
---|
1173 | if self.yieldtime >= yieldstep: |
---|
1174 | # Yield (intermediate) time and allow inspection of domain |
---|
1175 | |
---|
1176 | if self.checkpoint is True: |
---|
1177 | self.store_checkpoint() |
---|
1178 | self.delete_old_checkpoints() |
---|
1179 | |
---|
1180 | # Pass control on to outer loop for more specific actions |
---|
1181 | yield(self.time) |
---|
1182 | |
---|
1183 | # Reinitialise |
---|
1184 | self.yieldtime = 0.0 |
---|
1185 | self.min_timestep = max_timestep |
---|
1186 | self.max_timestep = min_timestep |
---|
1187 | self.number_of_steps = 0 |
---|
1188 | self.number_of_first_order_steps = 0 |
---|
1189 | self.max_speed = zeros(N, Float) |
---|
1190 | |
---|
1191 | def evolve_one_euler_step(self, yieldstep, finaltime): |
---|
1192 | """ |
---|
1193 | One Euler Time Step |
---|
1194 | Q^{n+1} = E(h) Q^n |
---|
1195 | """ |
---|
1196 | |
---|
1197 | #Compute fluxes across each element edge |
---|
1198 | self.compute_fluxes() |
---|
1199 | |
---|
1200 | #Update timestep to fit yieldstep and finaltime |
---|
1201 | self.update_timestep(yieldstep, finaltime) |
---|
1202 | |
---|
1203 | #Update conserved quantities |
---|
1204 | self.update_conserved_quantities() |
---|
1205 | |
---|
1206 | #update ghosts |
---|
1207 | self.update_ghosts() |
---|
1208 | |
---|
1209 | #Update vertex and edge values |
---|
1210 | self.distribute_to_vertices_and_edges() |
---|
1211 | |
---|
1212 | #Update boundary values |
---|
1213 | self.update_boundary() |
---|
1214 | |
---|
1215 | #Update time |
---|
1216 | self.time += self.timestep |
---|
1217 | |
---|
1218 | |
---|
1219 | |
---|
1220 | |
---|
1221 | def evolve_one_rk2_step(self, yieldstep, finaltime): |
---|
1222 | """ |
---|
1223 | One 2nd order RK timestep |
---|
1224 | Q^{n+1} = 0.5 Q^n + 0.5 E(h)^2 Q^n |
---|
1225 | """ |
---|
1226 | |
---|
1227 | #Save initial initial conserved quantities values |
---|
1228 | self.backup_conserved_quantities() |
---|
1229 | |
---|
1230 | #-------------------------------------- |
---|
1231 | #First euler step |
---|
1232 | #-------------------------------------- |
---|
1233 | |
---|
1234 | #Compute fluxes across each element edge |
---|
1235 | self.compute_fluxes() |
---|
1236 | |
---|
1237 | #Update timestep to fit yieldstep and finaltime |
---|
1238 | self.update_timestep(yieldstep, finaltime) |
---|
1239 | |
---|
1240 | #Update conserved quantities |
---|
1241 | self.update_conserved_quantities() |
---|
1242 | |
---|
1243 | #update ghosts |
---|
1244 | self.update_ghosts() |
---|
1245 | |
---|
1246 | #Update vertex and edge values |
---|
1247 | self.distribute_to_vertices_and_edges() |
---|
1248 | |
---|
1249 | #Update boundary values |
---|
1250 | self.update_boundary() |
---|
1251 | |
---|
1252 | #Update time |
---|
1253 | self.time += self.timestep |
---|
1254 | |
---|
1255 | #------------------------------------ |
---|
1256 | #Second Euler step |
---|
1257 | #------------------------------------ |
---|
1258 | |
---|
1259 | #Compute fluxes across each element edge |
---|
1260 | self.compute_fluxes() |
---|
1261 | |
---|
1262 | #Update conserved quantities |
---|
1263 | self.update_conserved_quantities() |
---|
1264 | |
---|
1265 | #------------------------------------ |
---|
1266 | #Combine initial and final values |
---|
1267 | #of conserved quantities and cleanup |
---|
1268 | #------------------------------------ |
---|
1269 | #combine steps |
---|
1270 | self.saxpy_conserved_quantities(0.5, 0.5) |
---|
1271 | |
---|
1272 | #update ghosts |
---|
1273 | self.update_ghosts() |
---|
1274 | |
---|
1275 | #Update vertex and edge values |
---|
1276 | self.distribute_to_vertices_and_edges() |
---|
1277 | |
---|
1278 | #Update boundary values |
---|
1279 | self.update_boundary() |
---|
1280 | |
---|
1281 | |
---|
1282 | |
---|
1283 | def evolve_one_rk3_step(self, yieldstep, finaltime): |
---|
1284 | """ |
---|
1285 | One 3rd order RK timestep |
---|
1286 | Q^(1) = 3/4 Q^n + 1/4 E(h)^2 Q^n (at time t^n + h/2) |
---|
1287 | Q^{n+1} = 1/3 Q^n + 2/3 E(h) Q^(1) (at time t^{n+1}) |
---|
1288 | """ |
---|
1289 | |
---|
1290 | #Save initial initial conserved quantities values |
---|
1291 | self.backup_conserved_quantities() |
---|
1292 | |
---|
1293 | initial_time = self.time |
---|
1294 | |
---|
1295 | #-------------------------------------- |
---|
1296 | #First euler step |
---|
1297 | #-------------------------------------- |
---|
1298 | |
---|
1299 | #Compute fluxes across each element edge |
---|
1300 | self.compute_fluxes() |
---|
1301 | |
---|
1302 | #Update timestep to fit yieldstep and finaltime |
---|
1303 | self.update_timestep(yieldstep, finaltime) |
---|
1304 | |
---|
1305 | #Update conserved quantities |
---|
1306 | self.update_conserved_quantities() |
---|
1307 | |
---|
1308 | #update ghosts |
---|
1309 | self.update_ghosts() |
---|
1310 | |
---|
1311 | #Update vertex and edge values |
---|
1312 | self.distribute_to_vertices_and_edges() |
---|
1313 | |
---|
1314 | #Update boundary values |
---|
1315 | self.update_boundary() |
---|
1316 | |
---|
1317 | #Update time |
---|
1318 | self.time += self.timestep |
---|
1319 | |
---|
1320 | #------------------------------------ |
---|
1321 | #Second Euler step |
---|
1322 | #------------------------------------ |
---|
1323 | |
---|
1324 | #Compute fluxes across each element edge |
---|
1325 | self.compute_fluxes() |
---|
1326 | |
---|
1327 | #Update conserved quantities |
---|
1328 | self.update_conserved_quantities() |
---|
1329 | |
---|
1330 | #------------------------------------ |
---|
1331 | #Combine steps to obtain intermediate |
---|
1332 | #solution at time t^n + 0.5 h |
---|
1333 | #------------------------------------ |
---|
1334 | |
---|
1335 | #combine steps |
---|
1336 | self.saxpy_conserved_quantities(0.25, 0.75) |
---|
1337 | |
---|
1338 | #update ghosts |
---|
1339 | self.update_ghosts() |
---|
1340 | |
---|
1341 | #Update vertex and edge values |
---|
1342 | self.distribute_to_vertices_and_edges() |
---|
1343 | |
---|
1344 | #Update boundary values |
---|
1345 | self.update_boundary() |
---|
1346 | |
---|
1347 | #set substep time |
---|
1348 | self.time = initial_time + self.timestep*0.5 |
---|
1349 | |
---|
1350 | #------------------------------------ |
---|
1351 | #Third Euler step |
---|
1352 | #------------------------------------ |
---|
1353 | |
---|
1354 | #Compute fluxes across each element edge |
---|
1355 | self.compute_fluxes() |
---|
1356 | |
---|
1357 | #Update conserved quantities |
---|
1358 | self.update_conserved_quantities() |
---|
1359 | |
---|
1360 | #------------------------------------ |
---|
1361 | #Combine final and initial values |
---|
1362 | #and cleanup |
---|
1363 | #------------------------------------ |
---|
1364 | #combine steps |
---|
1365 | self.saxpy_conserved_quantities(2.0/3.0, 1.0/3.0) |
---|
1366 | |
---|
1367 | #update ghosts |
---|
1368 | self.update_ghosts() |
---|
1369 | |
---|
1370 | #Update vertex and edge values |
---|
1371 | self.distribute_to_vertices_and_edges() |
---|
1372 | |
---|
1373 | #Update boundary values |
---|
1374 | self.update_boundary() |
---|
1375 | |
---|
1376 | #set new time |
---|
1377 | self.time = initial_time + self.timestep |
---|
1378 | |
---|
1379 | |
---|
1380 | def evolve_to_end(self, finaltime = 1.0): |
---|
1381 | """Iterate evolve all the way to the end |
---|
1382 | """ |
---|
1383 | |
---|
1384 | for _ in self.evolve(yieldstep=None, finaltime=finaltime): |
---|
1385 | pass |
---|
1386 | |
---|
1387 | |
---|
1388 | def backup_conserved_quantities(self): |
---|
1389 | N = len(self) # Number_of_triangles |
---|
1390 | |
---|
1391 | # Backup conserved_quantities centroid values |
---|
1392 | for name in self.conserved_quantities: |
---|
1393 | Q = self.quantities[name] |
---|
1394 | Q.backup_centroid_values() |
---|
1395 | |
---|
1396 | def saxpy_conserved_quantities(self,a,b): |
---|
1397 | N = len(self) #number_of_triangles |
---|
1398 | |
---|
1399 | # Backup conserved_quantities centroid values |
---|
1400 | for name in self.conserved_quantities: |
---|
1401 | Q = self.quantities[name] |
---|
1402 | Q.saxpy_centroid_values(a,b) |
---|
1403 | |
---|
1404 | |
---|
1405 | def update_boundary(self): |
---|
1406 | """Go through list of boundary objects and update boundary values |
---|
1407 | for all conserved quantities on boundary. |
---|
1408 | It is assumed that the ordering of conserved quantities is |
---|
1409 | consistent between the domain and the boundary object, i.e. |
---|
1410 | the jth element of vector q must correspond to the jth conserved |
---|
1411 | quantity in domain. |
---|
1412 | """ |
---|
1413 | |
---|
1414 | # FIXME: Update only those that change (if that can be worked out) |
---|
1415 | # FIXME: Boundary objects should not include ghost nodes. |
---|
1416 | for i, ((vol_id, edge_id), B) in enumerate(self.boundary_objects): |
---|
1417 | if B is None: |
---|
1418 | print 'WARNING: Ignored boundary segment %d (None)' |
---|
1419 | else: |
---|
1420 | q = B.evaluate(vol_id, edge_id) |
---|
1421 | |
---|
1422 | for j, name in enumerate(self.conserved_quantities): |
---|
1423 | Q = self.quantities[name] |
---|
1424 | Q.boundary_values[i] = q[j] |
---|
1425 | |
---|
1426 | |
---|
1427 | def compute_fluxes(self): |
---|
1428 | msg = 'Method compute_fluxes must be overridden by Domain subclass' |
---|
1429 | raise msg |
---|
1430 | |
---|
1431 | |
---|
1432 | def update_timestep(self, yieldstep, finaltime): |
---|
1433 | |
---|
1434 | from anuga.config import min_timestep, max_timestep |
---|
1435 | |
---|
1436 | |
---|
1437 | |
---|
1438 | # Protect against degenerate timesteps arising from isolated |
---|
1439 | # triangles |
---|
1440 | # FIXME (Steve): This should be in shallow_water as it assumes x and y |
---|
1441 | # momentum |
---|
1442 | if self.protect_against_isolated_degenerate_timesteps is True and\ |
---|
1443 | self.max_speed > 10.0: # FIXME (Ole): Make this configurable |
---|
1444 | |
---|
1445 | # Setup 10 bins for speed histogram |
---|
1446 | from anuga.utilities.numerical_tools import histogram, create_bins |
---|
1447 | |
---|
1448 | bins = create_bins(self.max_speed, 10) |
---|
1449 | hist = histogram(self.max_speed, bins) |
---|
1450 | |
---|
1451 | # Look for characteristic signature |
---|
1452 | if len(hist) > 1 and\ |
---|
1453 | hist[-1] > 0 and\ |
---|
1454 | hist[4] == hist[5] == hist[6] == hist[7] == hist[8] == 0: |
---|
1455 | # Danger of isolated degenerate triangles |
---|
1456 | # print self.timestepping_statistics(track_speeds=True) |
---|
1457 | |
---|
1458 | # Find triangles in last bin |
---|
1459 | # FIXME - speed up using Numeric |
---|
1460 | d = 0 |
---|
1461 | for i in range(self.number_of_full_triangles): |
---|
1462 | if self.max_speed[i] > bins[-1]: |
---|
1463 | msg = 'Time=%f: Ignoring isolated high ' %self.time |
---|
1464 | msg += 'speed triangle ' |
---|
1465 | msg += '#%d of %d with max speed=%f'\ |
---|
1466 | %(i, self.number_of_full_triangles, |
---|
1467 | self.max_speed[i]) |
---|
1468 | |
---|
1469 | # print 'Found offending triangle', i, |
---|
1470 | # self.max_speed[i] |
---|
1471 | self.get_quantity('xmomentum').set_values(0.0, indices=[i]) |
---|
1472 | self.get_quantity('ymomentum').set_values(0.0, indices=[i]) |
---|
1473 | self.max_speed[i]=0.0 |
---|
1474 | d += 1 |
---|
1475 | |
---|
1476 | #print 'Adjusted %d triangles' %d |
---|
1477 | #print self.timestepping_statistics(track_speeds=True) |
---|
1478 | |
---|
1479 | |
---|
1480 | |
---|
1481 | # self.timestep is calculated from speed of characteristics |
---|
1482 | # Apply CFL condition here |
---|
1483 | timestep = min(self.CFL*self.flux_timestep, max_timestep) |
---|
1484 | |
---|
1485 | # Record maximal and minimal values of timestep for reporting |
---|
1486 | self.max_timestep = max(timestep, self.max_timestep) |
---|
1487 | self.min_timestep = min(timestep, self.min_timestep) |
---|
1488 | |
---|
1489 | |
---|
1490 | |
---|
1491 | # Protect against degenerate time steps |
---|
1492 | if timestep < min_timestep: |
---|
1493 | |
---|
1494 | # Number of consecutive small steps taken b4 taking action |
---|
1495 | self.smallsteps += 1 |
---|
1496 | |
---|
1497 | if self.smallsteps > self.max_smallsteps: |
---|
1498 | self.smallsteps = 0 # Reset |
---|
1499 | |
---|
1500 | if self._order_ == 1: |
---|
1501 | msg = 'WARNING: Too small timestep %.16f reached '\ |
---|
1502 | %timestep |
---|
1503 | msg += 'even after %d steps of 1 order scheme'\ |
---|
1504 | %self.max_smallsteps |
---|
1505 | print msg |
---|
1506 | timestep = min_timestep # Try enforcing min_step |
---|
1507 | |
---|
1508 | print self.timestepping_statistics(track_speeds=True) |
---|
1509 | |
---|
1510 | raise Exception, msg |
---|
1511 | else: |
---|
1512 | # Try to overcome situation by switching to 1 order |
---|
1513 | self._order_ = 1 |
---|
1514 | |
---|
1515 | else: |
---|
1516 | self.smallsteps = 0 |
---|
1517 | if self._order_ == 1 and self.default_order == 2: |
---|
1518 | self._order_ = 2 |
---|
1519 | |
---|
1520 | |
---|
1521 | # Ensure that final time is not exceeded |
---|
1522 | if finaltime is not None and self.time + timestep > finaltime : |
---|
1523 | timestep = finaltime-self.time |
---|
1524 | |
---|
1525 | # Ensure that model time is aligned with yieldsteps |
---|
1526 | if self.yieldtime + timestep > yieldstep: |
---|
1527 | timestep = yieldstep-self.yieldtime |
---|
1528 | |
---|
1529 | self.timestep = timestep |
---|
1530 | |
---|
1531 | |
---|
1532 | |
---|
1533 | def compute_forcing_terms(self): |
---|
1534 | """If there are any forcing functions driving the system |
---|
1535 | they should be defined in Domain subclass and appended to |
---|
1536 | the list self.forcing_terms |
---|
1537 | """ |
---|
1538 | |
---|
1539 | for f in self.forcing_terms: |
---|
1540 | f(self) |
---|
1541 | |
---|
1542 | |
---|
1543 | |
---|
1544 | def update_conserved_quantities(self): |
---|
1545 | """Update vectors of conserved quantities using previously |
---|
1546 | computed fluxes specified forcing functions. |
---|
1547 | """ |
---|
1548 | |
---|
1549 | from Numeric import ones, sum, equal, Float |
---|
1550 | |
---|
1551 | N = len(self) # Number_of_triangles |
---|
1552 | d = len(self.conserved_quantities) |
---|
1553 | |
---|
1554 | timestep = self.timestep |
---|
1555 | |
---|
1556 | # Compute forcing terms |
---|
1557 | self.compute_forcing_terms() |
---|
1558 | |
---|
1559 | # Update conserved_quantities |
---|
1560 | for name in self.conserved_quantities: |
---|
1561 | Q = self.quantities[name] |
---|
1562 | Q.update(timestep) |
---|
1563 | |
---|
1564 | # Note that Q.explicit_update is reset by compute_fluxes |
---|
1565 | # Where is Q.semi_implicit_update reset? |
---|
1566 | |
---|
1567 | |
---|
1568 | def update_ghosts(self): |
---|
1569 | pass |
---|
1570 | |
---|
1571 | def distribute_to_vertices_and_edges(self): |
---|
1572 | """Extrapolate conserved quantities from centroid to |
---|
1573 | vertices and edge-midpoints for each volume |
---|
1574 | |
---|
1575 | Default implementation is straight first order, |
---|
1576 | i.e. constant values throughout each element and |
---|
1577 | no reference to non-conserved quantities. |
---|
1578 | """ |
---|
1579 | |
---|
1580 | for name in self.conserved_quantities: |
---|
1581 | Q = self.quantities[name] |
---|
1582 | if self._order_ == 1: |
---|
1583 | Q.extrapolate_first_order() |
---|
1584 | elif self._order_ == 2: |
---|
1585 | Q.extrapolate_second_order() |
---|
1586 | #Q.limit() |
---|
1587 | else: |
---|
1588 | raise 'Unknown order' |
---|
1589 | #Q.interpolate_from_vertices_to_edges() |
---|
1590 | |
---|
1591 | |
---|
1592 | def centroid_norm(self, quantity, normfunc): |
---|
1593 | """Calculate the norm of the centroid values |
---|
1594 | of a specific quantity, using normfunc. |
---|
1595 | |
---|
1596 | normfunc should take a list to a float. |
---|
1597 | |
---|
1598 | common normfuncs are provided in the module utilities.norms |
---|
1599 | """ |
---|
1600 | return normfunc(self.quantities[quantity].centroid_values) |
---|
1601 | |
---|
1602 | |
---|
1603 | |
---|
1604 | #------------------ |
---|
1605 | # Initialise module |
---|
1606 | #------------------ |
---|
1607 | |
---|
1608 | # Optimisation with psyco |
---|
1609 | from anuga.config import use_psyco |
---|
1610 | if use_psyco: |
---|
1611 | try: |
---|
1612 | import psyco |
---|
1613 | except: |
---|
1614 | import os |
---|
1615 | if os.name == 'posix' and os.uname()[4] == 'x86_64': |
---|
1616 | pass |
---|
1617 | # Psyco isn't supported on 64 bit systems, but it doesn't matter |
---|
1618 | else: |
---|
1619 | msg = 'WARNING: psyco (speedup) could not import'+\ |
---|
1620 | ', you may want to consider installing it' |
---|
1621 | print msg |
---|
1622 | else: |
---|
1623 | psyco.bind(Domain.update_boundary) |
---|
1624 | #psyco.bind(Domain.update_timestep) # Not worth it |
---|
1625 | psyco.bind(Domain.update_conserved_quantities) |
---|
1626 | psyco.bind(Domain.distribute_to_vertices_and_edges) |
---|
1627 | |
---|
1628 | |
---|
1629 | if __name__ == "__main__": |
---|
1630 | pass |
---|