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

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

changed the order of calls in anuga.init.py to hopefully call the correct Boyd_box_operator in parallel

File size: 15.9 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
162
163#---------------------------
164# Structure Operators
165#---------------------------
166
167
168if pypar_available:
169    from anuga_parallel.parallel_operator_factory import Inlet_operator
170    from anuga_parallel.parallel_operator_factory import Boyd_box_operator
171    from anuga_parallel.parallel_operator_factory import Boyd_pipe_operator
172    from anuga_parallel.parallel_operator_factory import Weir_orifice_trapezoid_operator
173else:
174    from anuga.structures.boyd_box_operator import Boyd_box_operator
175    from anuga.structures.boyd_pipe_operator import Boyd_pipe_operator
176    from anuga.structures.weir_orifice_trapezoid_operator import Weir_orifice_trapezoid_operator
177    from anuga.structures.inlet_operator import Inlet_operator
178
179#----------------------------
180# Parallel distribute
181#----------------------------
182
183
184#----------------------------
185#
186#Added by Petar Milevski 10/09/2013
187#import time, os
188
189from anuga.utilities.model_tools import get_polygon_from_single_file
190from anuga.utilities.model_tools import get_polygons_from_Mid_Mif
191from anuga.utilities.model_tools import get_polygon_list_from_files
192from anuga.utilities.model_tools import get_polygon_dictionary
193from anuga.utilities.model_tools import get_polygon_value_list
194from anuga.utilities.model_tools import read_polygon_dir
195from anuga.utilities.model_tools import read_hole_dir_multi_files_with_single_poly
196from anuga.utilities.model_tools import read_multi_poly_file
197from anuga.utilities.model_tools import read_hole_dir_single_file_with_multi_poly
198from anuga.utilities.model_tools import read_multi_poly_file_value
199from anuga.utilities.model_tools import Create_culvert_bridge_Operator
200
201
202#---------------------------
203# User Access Functions
204#---------------------------
205
206from anuga.utilities.system_tools import get_user_name, get_host_name, \
207    get_revision_number
208from anuga.utilities.mem_time_equation import estimate_time_mem
209
210
211
212
213#-----------------------------
214# rectangular domains
215#-----------------------------
216def rectangular_cross_domain(*args, **kwargs):
217    """
218    Create a rectangular domain with triangulation made
219    up of m+1 by n+1 uniform rectangular cells divided
220    into 4 triangles in a cross pattern
221
222    Arguments
223    m:      number of cells in x direction
224    n:      number of cells in y direction
225    len1:   length of domain in x direction (left to right)
226            (default 1.0)
227    len2:   length of domain in y direction (bottom to top)
228            (default 1.0)
229    origin: tuple (x,y) specifying location of lower left corner
230            of domain (default (0,0))
231    """
232
233    try:
234        verbose = kwargs.pop('verbose')
235    except:
236        verbose = False
237
238
239    points, vertices, boundary = rectangular_cross(*args, **kwargs)
240    return Domain(points, vertices, boundary, verbose= verbose)
241
242#----------------------------
243# Create domain from file
244#----------------------------
245def create_domain_from_file(file, DomainClass=Domain):
246    """
247    Create a domain from a file
248    """
249    return pmesh_to_domain_instance(file,DomainClass=DomainClass)
250
251#---------------------------
252# Create domain from regions
253#---------------------------
254
255def create_domain_from_regions(bounding_polygon,
256                               boundary_tags,
257                               maximum_triangle_area=None,
258                               mesh_filename=None,
259                               interior_regions=None,
260                               interior_holes=None,
261                               hole_tags=None,
262                               poly_geo_reference=None,
263                               mesh_geo_reference=None,
264                               minimum_triangle_angle=28.0,
265                               fail_if_polygons_outside=True,
266                               use_cache=False,
267                               verbose=True):
268    """Create domain from bounding polygons and resolutions.
269
270    bounding_polygon is a list of points in Eastings and Northings,
271    relative to the zone stated in poly_geo_reference if specified.
272    Otherwise points are just x, y coordinates with no particular
273    association to any location.
274
275    boundary_tags is a dictionary of symbolic tags. For every tag there
276    is a list of indices referring to segments associated with that tag.
277    If a segment is omitted it will be assigned the default tag ''.
278
279    maximum_triangle_area is the maximal area per triangle
280    for the bounding polygon, excluding the  interior regions.
281
282    Interior_regions is a list of tuples consisting of (polygon,
283    resolution) for each region to be separately refined. Do not have
284    polygon lines cross or be on-top of each other.  Also do not have
285    polygon close to each other.
286   
287    NOTE: If a interior_region is outside the bounding_polygon it should
288    throw an error
289   
290    interior_holes is a list of ploygons for each hole. These polygons do not
291    need to be closed, but their points must be specified in a counter-clockwise
292    order.
293
294    hole_tags  is a list of tag segment dictionaries.
295
296    This function does not allow segments to share points - use underlying
297    pmesh functionality for that
298
299    poly_geo_reference is the geo_reference of the bounding polygon and
300    the interior polygons.
301    If none, assume absolute.  Please pass one though, since absolute
302    references have a zone.
303   
304    mesh_geo_reference is the geo_reference of the mesh to be created.
305    If none is given one will be automatically generated.  It was use
306    the lower left hand corner of  bounding_polygon (absolute)
307    as the x and y values for the geo_ref.
308   
309    Returns the shallow water domain instance
310
311    Note, interior regions should be fully nested, as overlaps may cause
312    unintended resolutions.
313
314    fail_if_polygons_outside: If True (the default) Exception in thrown
315    where interior polygons fall outside bounding polygon. If False, these
316    will be ignored and execution continued.
317       
318   
319    """
320
321
322    # Build arguments and keyword arguments for use with caching or apply.
323    args = (bounding_polygon,
324            boundary_tags)
325   
326    kwargs = {'maximum_triangle_area': maximum_triangle_area,
327              'mesh_filename': mesh_filename,
328              'interior_regions': interior_regions,
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              'verbose': verbose} #FIXME (Ole): See ticket:14
336
337    # Call underlying engine with or without caching
338    if use_cache is True:
339        try:
340            from anuga.caching import cache
341        except:
342            msg = 'Caching was requested, but caching module'+\
343                  'could not be imported'
344            raise (msg)
345
346
347        domain = cache(_create_domain_from_regions,
348                       args, kwargs,
349                       verbose=verbose,
350                       compression=False)
351    else:
352        domain = apply(_create_domain_from_regions,
353                       args, kwargs)
354
355    return domain
356
357       
358def _create_domain_from_regions(bounding_polygon,
359                                boundary_tags,
360                                maximum_triangle_area=None,
361                                mesh_filename=None,                           
362                                interior_regions=None,
363                                interior_holes=None,
364                                hole_tags=None,
365                                poly_geo_reference=None,
366                                mesh_geo_reference=None,
367                                minimum_triangle_angle=28.0,
368                                fail_if_polygons_outside=True,
369                                verbose=True):
370    """_create_domain_from_regions - internal function.
371
372    See create_domain_from_regions for documentation.
373    """
374
375    #from anuga.shallow_water.shallow_water_domain import Domain
376    from anuga.pmesh.mesh_interface import create_mesh_from_regions
377   
378    create_mesh_from_regions(bounding_polygon,
379                             boundary_tags,
380                             maximum_triangle_area=maximum_triangle_area,
381                             interior_regions=interior_regions,
382                             filename=mesh_filename,
383                             interior_holes=interior_holes,
384                             hole_tags=hole_tags,
385                             poly_geo_reference=poly_geo_reference,
386                             mesh_geo_reference=mesh_geo_reference,
387                             minimum_triangle_angle=minimum_triangle_angle,
388                             fail_if_polygons_outside=fail_if_polygons_outside,
389                             use_cache=False,
390                             verbose=verbose)
391
392    domain = Domain(mesh_filename, use_cache=False, verbose=verbose)
393
394
395    return domain
396   
397import logging as log
398
399from anuga.config import use_psyco, g, velocity_protection
400if use_psyco:
401    # try using psyco if available
402    try:
403        import psyco
404    except:
405        import os
406        import sys
407        if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']:
408            pass
409            # Psyco isn't supported on 64 bit systems, but it doesn't matter
410        elif sys.version[:3] == '2.7' :
411            pass
412            # Psyco isn't available for python 2.7 (16/05/2011)
413        else:
414            log.critical('WARNING: psyco (speedup) could not be imported, '
415                         'you may want to consider installing it')
416    else:
417        psyco.full() # aggressively compile everything
418        #psyco.background() # attempt to profile code - only compile most used
419       
420
421
422
423
Note: See TracBrowser for help on using the repository browser.