source: branches/anuga_1_2_1/anuga_core/source/anuga/__init__.py @ 8083

Last change on this file since 8083 was 8083, checked in by habili, 13 years ago

Bug fixes, including correct use of starttime and duration.

File size: 12.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
30from anuga.shallow_water.shallow_water_domain import Domain
31
32from anuga.abstract_2d_finite_volumes.util import file_function, \
33                                        sww2timeseries, sww2csv_gauges
34
35from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross, \
36                                                    rectangular
37
38from anuga.file.csv_file import load_csv_as_building_polygons,  \
39                                load_csv_as_polygons
40
41from anuga.file.sts import create_sts_boundary
42
43from anuga.file.ungenerate import load_ungenerate
44
45from anuga.geometry.polygon import read_polygon, plot_polygons, polygon_area, number_mesh_triangles
46from anuga.geometry.polygon_function import Polygon_function
47
48from anuga.abstract_2d_finite_volumes.pmesh2domain import \
49                                            pmesh_to_domain_instance
50
51
52from anuga.utilities.system_tools import file_length
53from anuga.utilities.sww_merge import sww_merge
54from anuga.utilities.file_utils import copy_code_files
55from anuga.utilities.file_utils import start_screen_catcher
56
57from anuga.geometry.polygon import read_polygon
58from anuga.caching import cache
59from anuga.coordinate_transforms.geo_reference import Geo_reference, write_NetCDF_georeference
60
61#-----------------------------
62# Standard Boundaries
63#-----------------------------
64from anuga.shallow_water.boundaries import File_boundary
65from anuga.shallow_water.boundaries import Reflective_boundary
66from anuga.shallow_water.boundaries import Field_boundary
67from anuga.shallow_water.boundaries import \
68                    Transmissive_stage_zero_momentum_boundary
69from anuga.shallow_water.boundaries import \
70                    Transmissive_momentum_set_stage_boundary
71from anuga.shallow_water.boundaries import \
72                    Transmissive_n_momentum_zero_t_momentum_set_stage_boundary
73
74
75#-----------------------------
76# SWW-specific Boundaries
77#-----------------------------
78from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
79                            import Dirichlet_boundary
80from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
81                            import Time_boundary
82from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
83                            import Time_space_boundary
84from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
85                            import Transmissive_boundary
86
87
88
89#-----------------------------
90# Shalow Water Tsunamis
91#-----------------------------
92
93from anuga.shallow_water.smf import slide_tsunami, slump_tsunami
94
95
96#-----------------------------
97# Forcing
98#-----------------------------
99from anuga.shallow_water.forcing import Inflow, Rainfall, Wind_stress
100
101#-----------------------------
102# File conversion utilities
103#-----------------------------
104from anuga.file_conversion.file_conversion import sww2obj, \
105                    timefile2netcdf, tsh2sww
106from anuga.file_conversion.urs2nc import urs2nc
107from anuga.file_conversion.urs2sww import urs2sww 
108from anuga.file_conversion.urs2sts import urs2sts
109from anuga.file_conversion.dem2pts import dem2pts                   
110from anuga.file_conversion.esri2sww import esri2sww   
111from anuga.file_conversion.sww2dem import sww2dem, sww2dem_batch
112from anuga.file_conversion.asc2dem import asc2dem     
113from anuga.file_conversion.ferret2sww import ferret2sww     
114from anuga.file_conversion.dem2dem import dem2dem
115
116
117#-----------------------------
118# Mesh API
119#-----------------------------
120from anuga.pmesh.mesh_interface import create_mesh_from_regions
121
122#-----------------------------
123# SWW file access
124#-----------------------------
125from anuga.shallow_water.sww_interrogate import get_flow_through_cross_section
126   
127
128#-----------------------------
129# rectangular domains
130#-----------------------------
131def rectangular_cross_domain(*args, **kwargs):
132    """
133    Create a rectangular domain with triangulation made
134    up of m+1 by n+1 uniform rectangular cells divided
135    into 4 triangles in a cross pattern
136
137    Arguments
138    m:      number of cells in x direction
139    n:      number of cells in y direction
140    len1:   length of domain in x direction (left to right)
141            (default 1.0)
142    len2:   length of domain in y direction (bottom to top)
143            (default 1.0)
144    origin: tuple (x,y) specifying location of lower left corner
145            of domain (default (0,0))
146    """
147    points, vertices, boundary = rectangular_cross(*args, **kwargs)
148    return Domain(points, vertices, boundary)
149
150#----------------------------
151# Create domain from file
152#----------------------------
153def create_domain_from_file(file):
154    """
155    Create a domain from a file
156    """
157    return pmesh_to_domain_instance(file,Domain)
158
159#---------------------------
160# Create domain from regions
161#---------------------------
162
163def create_domain_from_regions(bounding_polygon,
164                               boundary_tags,
165                               maximum_triangle_area=None,
166                               mesh_filename=None,
167                               interior_regions=None,
168                               interior_holes=None,
169                               poly_geo_reference=None,
170                               mesh_geo_reference=None,
171                               minimum_triangle_angle=28.0,
172                               fail_if_polygons_outside=True,
173                               use_cache=False,
174                               verbose=True):
175    """Create domain from bounding polygons and resolutions.
176
177    bounding_polygon is a list of points in Eastings and Northings,
178    relative to the zone stated in poly_geo_reference if specified.
179    Otherwise points are just x, y coordinates with no particular
180    association to any location.
181
182    boundary_tags is a dictionary of symbolic tags. For every tag there
183    is a list of indices referring to segments associated with that tag.
184    If a segment is omitted it will be assigned the default tag ''.
185
186    maximum_triangle_area is the maximal area per triangle
187    for the bounding polygon, excluding the  interior regions.
188
189    Interior_regions is a list of tuples consisting of (polygon,
190    resolution) for each region to be separately refined. Do not have
191    polygon lines cross or be on-top of each other.  Also do not have
192    polygon close to each other.
193   
194    NOTE: If a interior_region is outside the bounding_polygon it should
195    throw an error
196   
197    interior_holes is a list of ploygons for each hole. These polygons do not
198    need to be closed, but their points must be specified in a counter-clockwise
199    order.
200
201    This function does not allow segments to share points - use underlying
202    pmesh functionality for that
203
204    poly_geo_reference is the geo_reference of the bounding polygon and
205    the interior polygons.
206    If none, assume absolute.  Please pass one though, since absolute
207    references have a zone.
208   
209    mesh_geo_reference is the geo_reference of the mesh to be created.
210    If none is given one will be automatically generated.  It was use
211    the lower left hand corner of  bounding_polygon (absolute)
212    as the x and y values for the geo_ref.
213   
214    Returns the shallow water domain instance
215
216    Note, interior regions should be fully nested, as overlaps may cause
217    unintended resolutions.
218
219    fail_if_polygons_outside: If True (the default) Exception in thrown
220    where interior polygons fall outside bounding polygon. If False, these
221    will be ignored and execution continued.
222       
223   
224    """
225
226
227    # Build arguments and keyword arguments for use with caching or apply.
228    args = (bounding_polygon,
229            boundary_tags)
230   
231    kwargs = {'maximum_triangle_area': maximum_triangle_area,
232              'mesh_filename': mesh_filename,
233              'interior_regions': interior_regions,
234              'interior_holes': interior_holes,
235              'poly_geo_reference': poly_geo_reference,
236              'mesh_geo_reference': mesh_geo_reference,
237              'minimum_triangle_angle': minimum_triangle_angle,
238              'fail_if_polygons_outside': fail_if_polygons_outside,
239              'verbose': verbose} #FIXME (Ole): See ticket:14
240
241    # Call underlying engine with or without caching
242    if use_cache is True:
243        try:
244            from anuga.caching import cache
245        except:
246            msg = 'Caching was requested, but caching module'+\
247                  'could not be imported'
248            raise msg
249
250
251        domain = cache(_create_domain_from_regions,
252                       args, kwargs,
253                       verbose=verbose,
254                       compression=False)
255    else:
256        domain = apply(_create_domain_from_regions,
257                       args, kwargs)
258
259    return domain
260
261       
262def _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                                poly_geo_reference=None,
269                                mesh_geo_reference=None,
270                                minimum_triangle_angle=28.0,
271                                fail_if_polygons_outside=True,
272                                verbose=True):
273    """_create_domain_from_regions - internal function.
274
275    See create_domain_from_regions for documentation.
276    """
277
278    from anuga.shallow_water.shallow_water_domain import Domain
279    from anuga.pmesh.mesh_interface import create_mesh_from_regions
280   
281    create_mesh_from_regions(bounding_polygon,
282                             boundary_tags,
283                             maximum_triangle_area=maximum_triangle_area,
284                             interior_regions=interior_regions,
285                             filename=mesh_filename,
286                             interior_holes=interior_holes,
287                             poly_geo_reference=poly_geo_reference,
288                             mesh_geo_reference=mesh_geo_reference,
289                             minimum_triangle_angle=minimum_triangle_angle,
290                             fail_if_polygons_outside=fail_if_polygons_outside,
291                             use_cache=False,
292                             verbose=verbose)
293    domain = Domain(mesh_filename, use_cache=False, verbose=verbose)
294
295
296    return domain
297   
298import logging as log
299
300from anuga.config import use_psyco
301if use_psyco:
302    # try using psyco if available
303    try:
304        import psyco
305    except:
306        import os
307        if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']:
308            pass
309            # Psyco isn't supported on 64 bit systems, but it doesn't matter
310        else:
311            log.critical('WARNING: psyco (speedup) could not be imported, '
312                         'you may want to consider installing it')
313    else:
314        psyco.full() # aggressively compile everything
315        #psyco.background() # attempt to profile code - only compile most used
316       
317
318#---------------------------
319# User access functions
320#---------------------------
321from anuga.utilities.system_tools import get_user_name, get_host_name
322from anuga.utilities.system_tools import get_revision_number
323
Note: See TracBrowser for help on using the repository browser.