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

Last change on this file since 9516 was 9500, checked in by steve, 10 years ago

setup up to move anuga_parllel to anuga.parallel

File size: 18.6 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#----------------------------
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
97from anuga.parallel.parallel_api import distribute
98from anuga.parallel.parallel_api import myid, numprocs, get_processor_name
99from anuga.parallel.parallel_api import send, receive
100from anuga.parallel.parallel_api import pypar_available, barrier, finalize
101
102if 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#-----------------------------
110from anuga.shallow_water.checkpoint import load_checkpoint_file
111
112
113#-----------------------------
114# SwW Standard Boundaries
115#-----------------------------
116from anuga.shallow_water.boundaries import File_boundary
117from anuga.shallow_water.boundaries import Reflective_boundary
118from anuga.shallow_water.boundaries import Field_boundary
119from anuga.shallow_water.boundaries import \
120                    Time_stage_zero_momentum_boundary
121from anuga.shallow_water.boundaries import \
122                    Transmissive_stage_zero_momentum_boundary
123from anuga.shallow_water.boundaries import \
124                    Transmissive_momentum_set_stage_boundary
125from anuga.shallow_water.boundaries import \
126                    Transmissive_n_momentum_zero_t_momentum_set_stage_boundary
127from anuga.shallow_water.boundaries import \
128                    Flather_external_stage_zero_velocity_boundary
129from anuga.abstract_2d_finite_volumes.generic_boundary_conditions import \
130                    Compute_fluxes_boundary
131       
132
133#-----------------------------
134# General Boundaries
135#-----------------------------
136from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
137                            import Dirichlet_boundary
138from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
139                            import Time_boundary
140from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
141                            import Time_space_boundary
142from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
143                            import Transmissive_boundary
144
145
146
147#-----------------------------
148# Shallow Water Tsunamis
149#-----------------------------
150
151from anuga.tsunami_source.smf import slide_tsunami, slump_tsunami
152
153
154
155#-----------------------------
156# Forcing
157# These are old, should use operators
158#-----------------------------
159from anuga.shallow_water.forcing import Inflow, Rainfall, Wind_stress
160
161
162
163#-----------------------------
164# File conversion utilities
165#-----------------------------
166from anuga.file_conversion.file_conversion import sww2obj, \
167                    timefile2netcdf, tsh2sww
168from anuga.file_conversion.urs2nc import urs2nc
169from anuga.file_conversion.urs2sww import urs2sww 
170from anuga.file_conversion.urs2sts import urs2sts
171from anuga.file_conversion.dem2pts import dem2pts                   
172from anuga.file_conversion.esri2sww import esri2sww   
173from anuga.file_conversion.sww2dem import sww2dem, sww2dem_batch
174from anuga.file_conversion.asc2dem import asc2dem
175from anuga.file_conversion.xya2pts import xya2pts     
176from anuga.file_conversion.ferret2sww import ferret2sww     
177from anuga.file_conversion.dem2dem import dem2dem
178from anuga.file_conversion.sww2array import sww2array
179
180#-----------------------------
181# Parsing arguments
182#-----------------------------
183from anuga.utilities.argparsing import create_standard_parser
184from anuga.utilities.argparsing import parse_standard_args
185
186
187def 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#-----------------------------
199from anuga.utilities.run_anuga_script import run_script as run_anuga_script
200
201
202#-----------------------------
203# Mesh API
204#-----------------------------
205from anuga.pmesh.mesh_interface import create_mesh_from_regions
206
207#-----------------------------
208# SWW file access
209#-----------------------------
210from anuga.shallow_water.sww_interrogate import get_flow_through_cross_section
211   
212#---------------------------
213# Operators
214#---------------------------
215from anuga.operators.kinematic_viscosity_operator import Kinematic_viscosity_operator
216
217from anuga.operators.rate_operators import Rate_operator
218from anuga.operators.set_friction_operators import Depth_friction_operator
219
220from anuga.operators.set_elevation_operator import Set_elevation_operator
221from anuga.operators.set_quantity_operator import Set_quantity_operator
222from anuga.operators.set_stage_operator import Set_stage_operator
223
224from anuga.operators.set_elevation import Set_elevation
225from anuga.operators.set_quantity import Set_quantity
226
227from anuga.operators.erosion_operators import Bed_shear_erosion_operator
228from anuga.operators.erosion_operators import Flat_slice_erosion_operator
229from anuga.operators.erosion_operators import Flat_fill_slice_erosion_operator
230
231#---------------------------
232# Structure Operators
233#---------------------------
234
235
236if 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
241else:
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
258from anuga.utilities.model_tools import get_polygon_from_single_file
259from anuga.utilities.model_tools import get_polygons_from_Mid_Mif
260from anuga.utilities.model_tools import get_polygon_list_from_files
261from anuga.utilities.model_tools import get_polygon_dictionary
262from anuga.utilities.model_tools import get_polygon_value_list
263from anuga.utilities.model_tools import read_polygon_dir
264from anuga.utilities.model_tools import read_hole_dir_multi_files_with_single_poly
265from anuga.utilities.model_tools import read_multi_poly_file
266from anuga.utilities.model_tools import read_hole_dir_single_file_with_multi_poly
267from anuga.utilities.model_tools import read_multi_poly_file_value
268from anuga.utilities.model_tools import Create_culvert_bridge_Operator
269
270
271#---------------------------
272# User Access Functions
273#---------------------------
274
275from anuga.utilities.system_tools import get_user_name, get_host_name, \
276    get_revision_number
277from anuga.utilities.mem_time_equation import estimate_time_mem
278
279
280
281
282#-----------------------------
283# rectangular domains
284#-----------------------------
285def 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#----------------------------
314def 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
324def 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       
427def _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   
466import logging as log
467
468from anuga.config import use_psyco, g, velocity_protection
469if 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
Note: See TracBrowser for help on using the repository browser.