1 | """ ANUGA models the effect of tsunamis and flooding upon a terrain mesh. |
---|
2 | In typical usage, a Domain class is created for a particular piece of |
---|
3 | terrain. Boundary conditions are specified for the domain, such as inflow |
---|
4 | and outflow, and then the simulation is run. |
---|
5 | |
---|
6 | This is the public API to ANUGA. It provides a toolkit of often-used |
---|
7 | modules, which can be used directly by including the following line in |
---|
8 | the user's code: |
---|
9 | |
---|
10 | import anuga |
---|
11 | |
---|
12 | This usage pattern abstracts away the internal heirarchy of the ANUGA |
---|
13 | system, allowing the user to concentrate on writing simulations without |
---|
14 | searching through the ANUGA source tree for the functions that they need. |
---|
15 | |
---|
16 | Also, it isolates the user from "under-the-hood" refactorings. |
---|
17 | """ |
---|
18 | |
---|
19 | pass |
---|
20 | |
---|
21 | #Add path of package to PYTHONPATH to allow C-extensions to be loaded |
---|
22 | import sys |
---|
23 | sys.path += __path__ |
---|
24 | |
---|
25 | |
---|
26 | #----------------------------------------------------- |
---|
27 | # Make selected classes available directly |
---|
28 | #----------------------------------------------------- |
---|
29 | |
---|
30 | |
---|
31 | from anuga.__metadata__ import __version__, __date__, __author__ |
---|
32 | |
---|
33 | #-------------------------------- |
---|
34 | # Important basic classes |
---|
35 | #-------------------------------- |
---|
36 | from anuga.shallow_water.shallow_water_domain import Domain |
---|
37 | from anuga.abstract_2d_finite_volumes.quantity import Quantity |
---|
38 | from anuga.abstract_2d_finite_volumes.region import Region |
---|
39 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
40 | from anuga.operators.base_operator import Operator |
---|
41 | from anuga.structures.structure_operator import Structure_operator |
---|
42 | |
---|
43 | |
---|
44 | from anuga.abstract_2d_finite_volumes.generic_domain import Generic_Domain |
---|
45 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
46 | #------------------------------------------------------------------------------ |
---|
47 | # Miscellaneous |
---|
48 | #------------------------------------------------------------------------------ |
---|
49 | from anuga.abstract_2d_finite_volumes.util import file_function, \ |
---|
50 | sww2timeseries, sww2csv_gauges, \ |
---|
51 | csv2timeseries_graphs |
---|
52 | |
---|
53 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross, \ |
---|
54 | rectangular |
---|
55 | |
---|
56 | from anuga.file.csv_file import load_csv_as_building_polygons, \ |
---|
57 | load_csv_as_polygons |
---|
58 | |
---|
59 | from anuga.file.sts import create_sts_boundary |
---|
60 | |
---|
61 | from anuga.file.ungenerate import load_ungenerate |
---|
62 | |
---|
63 | from anuga.geometry.polygon import read_polygon |
---|
64 | from anuga.geometry.polygon import plot_polygons |
---|
65 | from anuga.geometry.polygon import inside_polygon |
---|
66 | from anuga.geometry.polygon import polygon_area |
---|
67 | from anuga.geometry.polygon_function import Polygon_function |
---|
68 | |
---|
69 | from anuga.abstract_2d_finite_volumes.pmesh2domain import \ |
---|
70 | pmesh_to_domain_instance |
---|
71 | |
---|
72 | from anuga.utilities.system_tools import file_length |
---|
73 | from anuga.utilities.sww_merge import sww_merge_parallel as sww_merge |
---|
74 | from anuga.utilities.file_utils import copy_code_files |
---|
75 | from anuga.utilities.numerical_tools import safe_acos as acos |
---|
76 | import anuga.utilities.plot_utils as plot_utils |
---|
77 | |
---|
78 | |
---|
79 | from anuga.caching import cache |
---|
80 | from os.path import join |
---|
81 | from anuga.config import indent |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | #---------------------------- |
---|
86 | # Parallel api |
---|
87 | #---------------------------- |
---|
88 | ## from anuga_parallel.parallel_api import distribute |
---|
89 | ## from anuga_parallel.parallel_api import myid, numprocs, get_processor_name |
---|
90 | ## from anuga_parallel.parallel_api import send, receive |
---|
91 | ## from anuga_parallel.parallel_api import pypar_available, barrier, finalize |
---|
92 | |
---|
93 | ## if pypar_available: |
---|
94 | ## from anuga_parallel.parallel_api import sequential_distribute_dump |
---|
95 | ## from anuga_parallel.parallel_api import sequential_distribute_load |
---|
96 | |
---|
97 | from anuga.parallel.parallel_api import distribute |
---|
98 | from anuga.parallel.parallel_api import myid, numprocs, get_processor_name |
---|
99 | from anuga.parallel.parallel_api import send, receive |
---|
100 | from anuga.parallel.parallel_api import pypar_available, barrier, finalize |
---|
101 | |
---|
102 | if pypar_available: |
---|
103 | from anuga.parallel.parallel_api import sequential_distribute_dump |
---|
104 | from anuga.parallel.parallel_api import sequential_distribute_load |
---|
105 | |
---|
106 | |
---|
107 | #----------------------------- |
---|
108 | # Checkpointing |
---|
109 | #----------------------------- |
---|
110 | from anuga.shallow_water.checkpoint import load_checkpoint_file |
---|
111 | |
---|
112 | |
---|
113 | #----------------------------- |
---|
114 | # SwW Standard Boundaries |
---|
115 | #----------------------------- |
---|
116 | from anuga.shallow_water.boundaries import File_boundary |
---|
117 | from anuga.shallow_water.boundaries import Reflective_boundary |
---|
118 | from anuga.shallow_water.boundaries import Field_boundary |
---|
119 | from anuga.shallow_water.boundaries import \ |
---|
120 | Time_stage_zero_momentum_boundary |
---|
121 | from anuga.shallow_water.boundaries import \ |
---|
122 | Transmissive_stage_zero_momentum_boundary |
---|
123 | from anuga.shallow_water.boundaries import \ |
---|
124 | Transmissive_momentum_set_stage_boundary |
---|
125 | from anuga.shallow_water.boundaries import \ |
---|
126 | Transmissive_n_momentum_zero_t_momentum_set_stage_boundary |
---|
127 | from anuga.shallow_water.boundaries import \ |
---|
128 | Flather_external_stage_zero_velocity_boundary |
---|
129 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions import \ |
---|
130 | Compute_fluxes_boundary |
---|
131 | |
---|
132 | |
---|
133 | #----------------------------- |
---|
134 | # General Boundaries |
---|
135 | #----------------------------- |
---|
136 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \ |
---|
137 | import Dirichlet_boundary |
---|
138 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \ |
---|
139 | import Time_boundary |
---|
140 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \ |
---|
141 | import Time_space_boundary |
---|
142 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \ |
---|
143 | import Transmissive_boundary |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | #----------------------------- |
---|
148 | # Shallow Water Tsunamis |
---|
149 | #----------------------------- |
---|
150 | |
---|
151 | from anuga.tsunami_source.smf import slide_tsunami, slump_tsunami |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | #----------------------------- |
---|
156 | # Forcing |
---|
157 | # These are old, should use operators |
---|
158 | #----------------------------- |
---|
159 | from anuga.shallow_water.forcing import Inflow, Rainfall, Wind_stress |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | #----------------------------- |
---|
164 | # File conversion utilities |
---|
165 | #----------------------------- |
---|
166 | from anuga.file_conversion.file_conversion import sww2obj, \ |
---|
167 | timefile2netcdf, tsh2sww |
---|
168 | from anuga.file_conversion.urs2nc import urs2nc |
---|
169 | from anuga.file_conversion.urs2sww import urs2sww |
---|
170 | from anuga.file_conversion.urs2sts import urs2sts |
---|
171 | from anuga.file_conversion.dem2pts import dem2pts |
---|
172 | from anuga.file_conversion.esri2sww import esri2sww |
---|
173 | from anuga.file_conversion.sww2dem import sww2dem, sww2dem_batch |
---|
174 | from anuga.file_conversion.asc2dem import asc2dem |
---|
175 | from anuga.file_conversion.xya2pts import xya2pts |
---|
176 | from anuga.file_conversion.ferret2sww import ferret2sww |
---|
177 | from anuga.file_conversion.dem2dem import dem2dem |
---|
178 | from anuga.file_conversion.sww2array import sww2array |
---|
179 | |
---|
180 | #----------------------------- |
---|
181 | # Parsing arguments |
---|
182 | #----------------------------- |
---|
183 | from anuga.utilities.argparsing import create_standard_parser |
---|
184 | from anuga.utilities.argparsing import parse_standard_args |
---|
185 | |
---|
186 | |
---|
187 | def get_args(): |
---|
188 | """ Explicitly parse the argument list using standard anuga arguments |
---|
189 | |
---|
190 | Don't use this if you want to setup your own parser |
---|
191 | """ |
---|
192 | parser = create_standard_parser() |
---|
193 | return parser.parse_args() |
---|
194 | |
---|
195 | |
---|
196 | #----------------------------- |
---|
197 | # Running Script |
---|
198 | #----------------------------- |
---|
199 | from anuga.utilities.run_anuga_script import run_script as run_anuga_script |
---|
200 | |
---|
201 | |
---|
202 | #----------------------------- |
---|
203 | # Mesh API |
---|
204 | #----------------------------- |
---|
205 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
206 | |
---|
207 | #----------------------------- |
---|
208 | # SWW file access |
---|
209 | #----------------------------- |
---|
210 | from anuga.shallow_water.sww_interrogate import get_flow_through_cross_section |
---|
211 | |
---|
212 | #--------------------------- |
---|
213 | # Operators |
---|
214 | #--------------------------- |
---|
215 | from anuga.operators.kinematic_viscosity_operator import Kinematic_viscosity_operator |
---|
216 | |
---|
217 | from anuga.operators.rate_operators import Rate_operator |
---|
218 | from anuga.operators.set_friction_operators import Depth_friction_operator |
---|
219 | |
---|
220 | from anuga.operators.set_elevation_operator import Set_elevation_operator |
---|
221 | from anuga.operators.set_quantity_operator import Set_quantity_operator |
---|
222 | from anuga.operators.set_stage_operator import Set_stage_operator |
---|
223 | |
---|
224 | from anuga.operators.set_elevation import Set_elevation |
---|
225 | from anuga.operators.set_quantity import Set_quantity |
---|
226 | |
---|
227 | from anuga.operators.erosion_operators import Bed_shear_erosion_operator |
---|
228 | from anuga.operators.erosion_operators import Flat_slice_erosion_operator |
---|
229 | from anuga.operators.erosion_operators import Flat_fill_slice_erosion_operator |
---|
230 | |
---|
231 | #--------------------------- |
---|
232 | # Structure Operators |
---|
233 | #--------------------------- |
---|
234 | |
---|
235 | |
---|
236 | if pypar_available: |
---|
237 | from anuga.parallel.parallel_operator_factory import Inlet_operator |
---|
238 | from anuga.parallel.parallel_operator_factory import Boyd_box_operator |
---|
239 | from anuga.parallel.parallel_operator_factory import Boyd_pipe_operator |
---|
240 | from anuga.parallel.parallel_operator_factory import Weir_orifice_trapezoid_operator |
---|
241 | else: |
---|
242 | from anuga.structures.inlet_operator import Inlet_operator |
---|
243 | from anuga.structures.boyd_box_operator import Boyd_box_operator |
---|
244 | from anuga.structures.boyd_pipe_operator import Boyd_pipe_operator |
---|
245 | from anuga.structures.weir_orifice_trapezoid_operator import Weir_orifice_trapezoid_operator |
---|
246 | |
---|
247 | |
---|
248 | #---------------------------- |
---|
249 | # Parallel distribute |
---|
250 | #---------------------------- |
---|
251 | |
---|
252 | |
---|
253 | #---------------------------- |
---|
254 | # |
---|
255 | #Added by Petar Milevski 10/09/2013 |
---|
256 | #import time, os |
---|
257 | |
---|
258 | from anuga.utilities.model_tools import get_polygon_from_single_file |
---|
259 | from anuga.utilities.model_tools import get_polygons_from_Mid_Mif |
---|
260 | from anuga.utilities.model_tools import get_polygon_list_from_files |
---|
261 | from anuga.utilities.model_tools import get_polygon_dictionary |
---|
262 | from anuga.utilities.model_tools import get_polygon_value_list |
---|
263 | from anuga.utilities.model_tools import read_polygon_dir |
---|
264 | from anuga.utilities.model_tools import read_hole_dir_multi_files_with_single_poly |
---|
265 | from anuga.utilities.model_tools import read_multi_poly_file |
---|
266 | from anuga.utilities.model_tools import read_hole_dir_single_file_with_multi_poly |
---|
267 | from anuga.utilities.model_tools import read_multi_poly_file_value |
---|
268 | from anuga.utilities.model_tools import Create_culvert_bridge_Operator |
---|
269 | |
---|
270 | |
---|
271 | #--------------------------- |
---|
272 | # User Access Functions |
---|
273 | #--------------------------- |
---|
274 | |
---|
275 | from anuga.utilities.system_tools import get_user_name, get_host_name, \ |
---|
276 | get_revision_number |
---|
277 | from anuga.utilities.mem_time_equation import estimate_time_mem |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | #----------------------------- |
---|
283 | # rectangular domains |
---|
284 | #----------------------------- |
---|
285 | def rectangular_cross_domain(*args, **kwargs): |
---|
286 | """ |
---|
287 | Create a rectangular domain with triangulation made |
---|
288 | up of m+1 by n+1 uniform rectangular cells divided |
---|
289 | into 4 triangles in a cross pattern |
---|
290 | |
---|
291 | Arguments |
---|
292 | m: number of cells in x direction |
---|
293 | n: number of cells in y direction |
---|
294 | len1: length of domain in x direction (left to right) |
---|
295 | (default 1.0) |
---|
296 | len2: length of domain in y direction (bottom to top) |
---|
297 | (default 1.0) |
---|
298 | origin: tuple (x,y) specifying location of lower left corner |
---|
299 | of domain (default (0,0)) |
---|
300 | """ |
---|
301 | |
---|
302 | try: |
---|
303 | verbose = kwargs.pop('verbose') |
---|
304 | except: |
---|
305 | verbose = False |
---|
306 | |
---|
307 | |
---|
308 | points, vertices, boundary = rectangular_cross(*args, **kwargs) |
---|
309 | return Domain(points, vertices, boundary, verbose= verbose) |
---|
310 | |
---|
311 | #---------------------------- |
---|
312 | # Create domain from file |
---|
313 | #---------------------------- |
---|
314 | def create_domain_from_file(file, DomainClass=Domain): |
---|
315 | """ |
---|
316 | Create a domain from a file |
---|
317 | """ |
---|
318 | return pmesh_to_domain_instance(file,DomainClass=DomainClass) |
---|
319 | |
---|
320 | #--------------------------- |
---|
321 | # Create domain from regions |
---|
322 | #--------------------------- |
---|
323 | |
---|
324 | def create_domain_from_regions(bounding_polygon, |
---|
325 | boundary_tags, |
---|
326 | maximum_triangle_area=None, |
---|
327 | mesh_filename=None, |
---|
328 | interior_regions=None, |
---|
329 | interior_holes=None, |
---|
330 | hole_tags=None, |
---|
331 | poly_geo_reference=None, |
---|
332 | mesh_geo_reference=None, |
---|
333 | minimum_triangle_angle=28.0, |
---|
334 | fail_if_polygons_outside=True, |
---|
335 | use_cache=False, |
---|
336 | verbose=True): |
---|
337 | """Create domain from bounding polygons and resolutions. |
---|
338 | |
---|
339 | bounding_polygon is a list of points in Eastings and Northings, |
---|
340 | relative to the zone stated in poly_geo_reference if specified. |
---|
341 | Otherwise points are just x, y coordinates with no particular |
---|
342 | association to any location. |
---|
343 | |
---|
344 | boundary_tags is a dictionary of symbolic tags. For every tag there |
---|
345 | is a list of indices referring to segments associated with that tag. |
---|
346 | If a segment is omitted it will be assigned the default tag ''. |
---|
347 | |
---|
348 | maximum_triangle_area is the maximal area per triangle |
---|
349 | for the bounding polygon, excluding the interior regions. |
---|
350 | |
---|
351 | Interior_regions is a list of tuples consisting of (polygon, |
---|
352 | resolution) for each region to be separately refined. Do not have |
---|
353 | polygon lines cross or be on-top of each other. Also do not have |
---|
354 | polygon close to each other. |
---|
355 | |
---|
356 | NOTE: If a interior_region is outside the bounding_polygon it should |
---|
357 | throw an error |
---|
358 | |
---|
359 | interior_holes is a list of ploygons for each hole. These polygons do not |
---|
360 | need to be closed, but their points must be specified in a counter-clockwise |
---|
361 | order. |
---|
362 | |
---|
363 | hole_tags is a list of tag segment dictionaries. |
---|
364 | |
---|
365 | This function does not allow segments to share points - use underlying |
---|
366 | pmesh functionality for that |
---|
367 | |
---|
368 | poly_geo_reference is the geo_reference of the bounding polygon and |
---|
369 | the interior polygons. |
---|
370 | If none, assume absolute. Please pass one though, since absolute |
---|
371 | references have a zone. |
---|
372 | |
---|
373 | mesh_geo_reference is the geo_reference of the mesh to be created. |
---|
374 | If none is given one will be automatically generated. It was use |
---|
375 | the lower left hand corner of bounding_polygon (absolute) |
---|
376 | as the x and y values for the geo_ref. |
---|
377 | |
---|
378 | Returns the shallow water domain instance |
---|
379 | |
---|
380 | Note, interior regions should be fully nested, as overlaps may cause |
---|
381 | unintended resolutions. |
---|
382 | |
---|
383 | fail_if_polygons_outside: If True (the default) Exception in thrown |
---|
384 | where interior polygons fall outside bounding polygon. If False, these |
---|
385 | will be ignored and execution continued. |
---|
386 | |
---|
387 | |
---|
388 | """ |
---|
389 | |
---|
390 | |
---|
391 | # Build arguments and keyword arguments for use with caching or apply. |
---|
392 | args = (bounding_polygon, |
---|
393 | boundary_tags) |
---|
394 | |
---|
395 | kwargs = {'maximum_triangle_area': maximum_triangle_area, |
---|
396 | 'mesh_filename': mesh_filename, |
---|
397 | 'interior_regions': interior_regions, |
---|
398 | 'interior_holes': interior_holes, |
---|
399 | 'hole_tags': hole_tags, |
---|
400 | 'poly_geo_reference': poly_geo_reference, |
---|
401 | 'mesh_geo_reference': mesh_geo_reference, |
---|
402 | 'minimum_triangle_angle': minimum_triangle_angle, |
---|
403 | 'fail_if_polygons_outside': fail_if_polygons_outside, |
---|
404 | 'verbose': verbose} #FIXME (Ole): See ticket:14 |
---|
405 | |
---|
406 | # Call underlying engine with or without caching |
---|
407 | if use_cache is True: |
---|
408 | try: |
---|
409 | from anuga.caching import cache |
---|
410 | except: |
---|
411 | msg = 'Caching was requested, but caching module'+\ |
---|
412 | 'could not be imported' |
---|
413 | raise (msg) |
---|
414 | |
---|
415 | |
---|
416 | domain = cache(_create_domain_from_regions, |
---|
417 | args, kwargs, |
---|
418 | verbose=verbose, |
---|
419 | compression=False) |
---|
420 | else: |
---|
421 | domain = apply(_create_domain_from_regions, |
---|
422 | args, kwargs) |
---|
423 | |
---|
424 | return domain |
---|
425 | |
---|
426 | |
---|
427 | def _create_domain_from_regions(bounding_polygon, |
---|
428 | boundary_tags, |
---|
429 | maximum_triangle_area=None, |
---|
430 | mesh_filename=None, |
---|
431 | interior_regions=None, |
---|
432 | interior_holes=None, |
---|
433 | hole_tags=None, |
---|
434 | poly_geo_reference=None, |
---|
435 | mesh_geo_reference=None, |
---|
436 | minimum_triangle_angle=28.0, |
---|
437 | fail_if_polygons_outside=True, |
---|
438 | verbose=True): |
---|
439 | """_create_domain_from_regions - internal function. |
---|
440 | |
---|
441 | See create_domain_from_regions for documentation. |
---|
442 | """ |
---|
443 | |
---|
444 | #from anuga.shallow_water.shallow_water_domain import Domain |
---|
445 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
446 | |
---|
447 | create_mesh_from_regions(bounding_polygon, |
---|
448 | boundary_tags, |
---|
449 | maximum_triangle_area=maximum_triangle_area, |
---|
450 | interior_regions=interior_regions, |
---|
451 | filename=mesh_filename, |
---|
452 | interior_holes=interior_holes, |
---|
453 | hole_tags=hole_tags, |
---|
454 | poly_geo_reference=poly_geo_reference, |
---|
455 | mesh_geo_reference=mesh_geo_reference, |
---|
456 | minimum_triangle_angle=minimum_triangle_angle, |
---|
457 | fail_if_polygons_outside=fail_if_polygons_outside, |
---|
458 | use_cache=False, |
---|
459 | verbose=verbose) |
---|
460 | |
---|
461 | domain = Domain(mesh_filename, use_cache=False, verbose=verbose) |
---|
462 | |
---|
463 | |
---|
464 | return domain |
---|
465 | |
---|
466 | import logging as log |
---|
467 | |
---|
468 | from anuga.config import use_psyco, g, velocity_protection |
---|
469 | if use_psyco: |
---|
470 | # try using psyco if available |
---|
471 | try: |
---|
472 | import psyco |
---|
473 | except: |
---|
474 | import os |
---|
475 | import sys |
---|
476 | if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']: |
---|
477 | pass |
---|
478 | # Psyco isn't supported on 64 bit systems, but it doesn't matter |
---|
479 | elif sys.version[:3] == '2.7' : |
---|
480 | pass |
---|
481 | # Psyco isn't available for python 2.7 (16/05/2011) |
---|
482 | else: |
---|
483 | log.critical('WARNING: psyco (speedup) could not be imported, ' |
---|
484 | 'you may want to consider installing it') |
---|
485 | else: |
---|
486 | psyco.full() # aggressively compile everything |
---|
487 | #psyco.background() # attempt to profile code - only compile most used |
---|
488 | |
---|
489 | |
---|
490 | |
---|
491 | |
---|
492 | |
---|