source: trunk/anuga_core/source/anuga/__init__.py @ 9486

Last change on this file since 9486 was 9457, checked in by steve, 10 years ago

Cleaning up shallow_water folder

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