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

Last change on this file since 8330 was 8330, checked in by gray, 12 years ago

Adding functions to estimate the time and memory used by simulations.

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, \
148    get_revision_number
149from anuga.utilities.mem_time_equation import estimate_time_mem
150
151
152
153
154#-----------------------------
155# rectangular domains
156#-----------------------------
157def rectangular_cross_domain(*args, **kwargs):
158    """
159    Create a rectangular domain with triangulation made
160    up of m+1 by n+1 uniform rectangular cells divided
161    into 4 triangles in a cross pattern
162
163    Arguments
164    m:      number of cells in x direction
165    n:      number of cells in y direction
166    len1:   length of domain in x direction (left to right)
167            (default 1.0)
168    len2:   length of domain in y direction (bottom to top)
169            (default 1.0)
170    origin: tuple (x,y) specifying location of lower left corner
171            of domain (default (0,0))
172    """
173    points, vertices, boundary = rectangular_cross(*args, **kwargs)
174    return Domain(points, vertices, boundary)
175
176#----------------------------
177# Create domain from file
178#----------------------------
179def create_domain_from_file(file, DomainClass=Domain):
180    """
181    Create a domain from a file
182    """
183    return pmesh_to_domain_instance(file,DomainClass=DomainClass)
184
185#---------------------------
186# Create domain from regions
187#---------------------------
188
189def create_domain_from_regions(bounding_polygon,
190                               boundary_tags,
191                               maximum_triangle_area=None,
192                               mesh_filename=None,
193                               interior_regions=None,
194                               interior_holes=None,
195                               hole_tags=None,
196                               poly_geo_reference=None,
197                               mesh_geo_reference=None,
198                               minimum_triangle_angle=28.0,
199                               fail_if_polygons_outside=True,
200                               use_cache=False,
201                               verbose=True):
202    """Create domain from bounding polygons and resolutions.
203
204    bounding_polygon is a list of points in Eastings and Northings,
205    relative to the zone stated in poly_geo_reference if specified.
206    Otherwise points are just x, y coordinates with no particular
207    association to any location.
208
209    boundary_tags is a dictionary of symbolic tags. For every tag there
210    is a list of indices referring to segments associated with that tag.
211    If a segment is omitted it will be assigned the default tag ''.
212
213    maximum_triangle_area is the maximal area per triangle
214    for the bounding polygon, excluding the  interior regions.
215
216    Interior_regions is a list of tuples consisting of (polygon,
217    resolution) for each region to be separately refined. Do not have
218    polygon lines cross or be on-top of each other.  Also do not have
219    polygon close to each other.
220   
221    NOTE: If a interior_region is outside the bounding_polygon it should
222    throw an error
223   
224    interior_holes is a list of ploygons for each hole. These polygons do not
225    need to be closed, but their points must be specified in a counter-clockwise
226    order.
227
228    hole_tags  is a list of tag segment dictionaries.
229
230    This function does not allow segments to share points - use underlying
231    pmesh functionality for that
232
233    poly_geo_reference is the geo_reference of the bounding polygon and
234    the interior polygons.
235    If none, assume absolute.  Please pass one though, since absolute
236    references have a zone.
237   
238    mesh_geo_reference is the geo_reference of the mesh to be created.
239    If none is given one will be automatically generated.  It was use
240    the lower left hand corner of  bounding_polygon (absolute)
241    as the x and y values for the geo_ref.
242   
243    Returns the shallow water domain instance
244
245    Note, interior regions should be fully nested, as overlaps may cause
246    unintended resolutions.
247
248    fail_if_polygons_outside: If True (the default) Exception in thrown
249    where interior polygons fall outside bounding polygon. If False, these
250    will be ignored and execution continued.
251       
252   
253    """
254
255
256    # Build arguments and keyword arguments for use with caching or apply.
257    args = (bounding_polygon,
258            boundary_tags)
259   
260    kwargs = {'maximum_triangle_area': maximum_triangle_area,
261              'mesh_filename': mesh_filename,
262              'interior_regions': interior_regions,
263              'interior_holes': interior_holes,
264              'hole_tags': hole_tags,
265              'poly_geo_reference': poly_geo_reference,
266              'mesh_geo_reference': mesh_geo_reference,
267              'minimum_triangle_angle': minimum_triangle_angle,
268              'fail_if_polygons_outside': fail_if_polygons_outside,
269              'verbose': verbose} #FIXME (Ole): See ticket:14
270
271    # Call underlying engine with or without caching
272    if use_cache is True:
273        try:
274            from anuga.caching import cache
275        except:
276            msg = 'Caching was requested, but caching module'+\
277                  'could not be imported'
278            raise (msg)
279
280
281        domain = cache(_create_domain_from_regions,
282                       args, kwargs,
283                       verbose=verbose,
284                       compression=False)
285    else:
286        domain = apply(_create_domain_from_regions,
287                       args, kwargs)
288
289    return domain
290
291       
292def _create_domain_from_regions(bounding_polygon,
293                                boundary_tags,
294                                maximum_triangle_area=None,
295                                mesh_filename=None,                           
296                                interior_regions=None,
297                                interior_holes=None,
298                                hole_tags=None,
299                                poly_geo_reference=None,
300                                mesh_geo_reference=None,
301                                minimum_triangle_angle=28.0,
302                                fail_if_polygons_outside=True,
303                                verbose=True):
304    """_create_domain_from_regions - internal function.
305
306    See create_domain_from_regions for documentation.
307    """
308
309    #from anuga.shallow_water.shallow_water_domain import Domain
310    from anuga.pmesh.mesh_interface import create_mesh_from_regions
311   
312    create_mesh_from_regions(bounding_polygon,
313                             boundary_tags,
314                             maximum_triangle_area=maximum_triangle_area,
315                             interior_regions=interior_regions,
316                             filename=mesh_filename,
317                             interior_holes=interior_holes,
318                             hole_tags=hole_tags,
319                             poly_geo_reference=poly_geo_reference,
320                             mesh_geo_reference=mesh_geo_reference,
321                             minimum_triangle_angle=minimum_triangle_angle,
322                             fail_if_polygons_outside=fail_if_polygons_outside,
323                             use_cache=False,
324                             verbose=verbose)
325
326    domain = Domain(mesh_filename, use_cache=False, verbose=verbose)
327
328
329    return domain
330   
331import logging as log
332
333from anuga.config import use_psyco, g, velocity_protection
334if use_psyco:
335    # try using psyco if available
336    try:
337        import psyco
338    except:
339        import os
340        import sys
341        if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']:
342            pass
343            # Psyco isn't supported on 64 bit systems, but it doesn't matter
344        elif sys.version[:3] == '2.7' :
345            pass
346            # Psyco isn't available for python 2.7 (16/05/2011)
347        else:
348            log.critical('WARNING: psyco (speedup) could not be imported, '
349                         'you may want to consider installing it')
350    else:
351        psyco.full() # aggressively compile everything
352        #psyco.background() # attempt to profile code - only compile most used
353       
354
355
356
357
Note: See TracBrowser for help on using the repository browser.