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

Last change on this file since 7876 was 7874, checked in by hudson, 14 years ago

Added ungenerate function to public API.

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