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