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

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

Speed up the check_integrity procedure of neighbour_mesh

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