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

Last change on this file since 8288 was 8288, checked in by steve, 12 years ago

Added in new test function for balanced code

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