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

Last change on this file since 9094 was 9094, checked in by steve, 11 years ago

Change to deal with scipy interp1d function when it throws valueException

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