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

Last change on this file since 7810 was 7810, checked in by hudson, 12 years ago

Added aggressive psyco optimisation, fixed benchmark app to work with new API.

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