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