Changeset 2450


Ignore:
Timestamp:
Feb 27, 2006, 12:23:18 PM (19 years ago)
Author:
duncan
Message:

Further changes to second example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • documentation/user_manual/anuga_user_manual.tex

    r2449 r2450  
    423423    momentum quantities assumed to be the second and third conserved
    424424    quantities.
    425    
     425
    426426    \item[Transmissive boundary]Returns same conserved quantities as
    427427    those present in its neighbour volume.
    428    
     428
    429429    \item[Dirichlet boundary]Specifies a fixed value at the
    430430boundary.
     
    458458series of steps indicated by the values of \code{yieldstep} and
    459459\code{finaltime}, which can be altered as required.
    460 The value of \code{yieldstep} controls the time interval between successive model outputs. 
     460The value of \code{yieldstep} controls the time interval between successive model outputs.
    461461Behind the scenes more time steps are generally taken.
    462462
     
    489489\section{An Example with Real Data}
    490490
    491 The following discussion builds on the concepts introduced through \code{bedslope.py}
    492 example and introduces a second example, \code{run_sydney_smf.py}, that follows
    493 the same basic outline, but incorporates many more
    494 complex features.
    495 
    496 The chief difference is that data is taken from a file and represents an actual physical
    497 scenario rather than an illustrative example. However a different
    498 method is used to create the mesh. Instead of imposing a mesh
    499 structure on a rectangular grid, the technique used for this example involves building
    500 mesh structures inside polygons.
    501 
    502 In its simplest form, the mesh is created within a single polygon
    503 whose vertices are at geographical locations specified by the user.  A
    504 triangular mesh is created using points inside the polygon selected
    505 through a random process, the user specifying the
    506 \emph{resolution}---that is, the maximal area of a triangle used for
    507 triangulation.
     491The following discussion builds on the concepts introduced through
     492the \code{bedslope.py} example and introduces a second example,
     493\code{run_sydney_smf.py}, that follows the same basic outline, but
     494incorporates many more complex features.
     495
     496Here is the code for \code{run_sydney_smf.py}:
     497
     498{\scriptsize \begin{verbatim}
     499"""Script for running a tsunami
     500inundation scenario for Sydney, NSW, Australia.
     501
     502Source data such as elevation and boundary data is assumed to be
     503available in directories specified by project.py The output sww file
     504is stored in project.outputdir
     505
     506The scenario is defined by a triangular mesh created from
     507project.polygon, the elevation data and a simulated submarine
     508landslide.
     509
     510Ole Nielsen and Duncan Gray, GA - 2005 and Adrian Hitchman and Jane
     511Sexton, GA - 2006 """
     512
     513
     514#-------------------------------------------------------------------------------#
     515Import necessary modules
     516#-------------------------------------------------------------------------------
     517
     518# Standard modules
     519import os
     520import time
     521
     522# Related major packages
     523from pyvolution.shallow_water import
     524Domain, Reflective_boundary
     525from pyvolution.data_manager import
     526convert_dem_from_ascii2netcdf, dem2pts
     527from pyvolution.combine_pts
     528import combine_rectangular_points_files
     529from pyvolution.pmesh2domain
     530import pmesh_to_domain_instance
     531
     532# Application specific imports
     533import project                 #
     534Definition of file names and polygons
     535from smf import slump_tsunami
     536# Function for submarine mudslide
     537
     538
     539#-------------------------------------------------------------------------------
     540# Preparation of topographic data #
     541# Convert ASC 2 DEM 2 PTS using
     542source data and store result in source data
     543# Do for coarse and fine
     544data
     545
     546# Fine pts file to be clipped to area of interest
     547#-------------------------------------------------------------------------------
     548
     549# filenames
     550coarsedemname = project.coarsedemname
     551finedemname =
     552project.finedemname meshname = project.meshname+'.msh'
     553
     554# coarse data convert_dem_
     555from_ascii2netcdf(coarsedemname,
     556use_cache=True, verbose=True) dem2pts(coarsedemname, use_cache=True,
     557verbose=True)
     558
     559# fine data (clipping the points file to smaller area)
     560convert_dem_from_ascii2netcdf(finedemname, use_cache=True,
     561verbose=True) dem2pts(finedemname,
     562        easting_min=project.eastingmin,
     563        easting_max=project.eastingmax,
     564        northing_min=project.northingmin,
     565        northing_max= project.northingmax,
     566        use_cache=True,
     567        verbose=True)
     568
     569
     570# combining the coarse and fine data
     571combine_rectangular_points_files(project.finedemname + '.pts',
     572                                 project.coarsedemname + '.pts',
     573                                 project.combineddemname + '.pts')
     574
     575
     576#-------------------------------------------------------------------------------
     577# Create the triangular mesh based on overall clipping polygon with
     578a tagged # boundary and interior regions defined in project.py along
     579with # resolutions (maximal area of per triangle) for each polygon
     580#-------------------------------------------------------------------------------
     581
     582#def get_polygon_from_file(filename): #    fid = open(filename) #
     583lines = fid.readlines() #    fid.close()
     584
     585#    polygon = [] #    for line in lines[1:]: #       fields =
     586line.split(',') #        x = float(fields[3]) #        y =
     587float(fields[4]) #        polygon.append([x, y])
     588
     589#    return interior_polygon
     590
     591from pmesh.create_mesh import create_mesh_from_regions
     592
     593num_polygons = 1 filelist = [] fileext = '.csv' filename =
     594project.polygonptsfile interior_res = 50000
     595
     596for p in range(1, num_polygons+1):
     597    thefilename = filename + str(p) + fileext
     598    interior_polygon = get_polygon_from_file(thefilename)
     599    interior_regions.append([interior_polygon, interior_res])
     600
     601# test #interior_regions = [[project.poly1, interior_res], #
     602[project.poly2, interior_res]] # original #interior_regions =
     603[[project.harbour_polygon_2, interior_res], #
     604[project.botanybay_polygon_2, interior_res]]
     605
     606#FIXME: Fix caching of this one once the mesh_interface is ready
     607from caching import cache _ = cache(create_mesh_from_regions,
     608          project.diffpolygonall,
     609          {'boundary_tags': {'bottom': [0],
     610                             'right1': [1], 'right0': [2],
     611                             'right2': [3], 'top': [4], 'left1': [5],
     612                             'left2': [6], 'left3': [7]},
     613           'resolution': 100000,
     614           'filename': meshname,
     615           'interior_regions': interior_regions,
     616           'UTM': True,
     617           'refzone': project.refzone},
     618          verbose = True)
     619
     620
     621#-------------------------------------------------------------------------------
     622# Setup computational domain
     623#-------------------------------------------------------------------------------
     624
     625domain = pmesh_to_domain_instance(meshname, Domain,
     626                                  use_cache = True,
     627                                  verbose = True)
     628
     629print 'Number of triangles = ', len(domain) print 'The extent is ',
     630domain.get_extent()
     631
     632domain.set_name(project.basename)
     633domain.set_datadir(project.outputdir)
     634domain.set_quantities_to_be_stored(['stage', 'xmomentum',
     635'ymomentum'])
     636
     637
     638#-------------------------------------------------------------------------------
     639# Set up scenario (tsunami_source is a callable object used with
     640set_quantity)
     641#-------------------------------------------------------------------------------
     642
     643tsunami_source = slump_tsunami(length=30000.0,
     644                               depth=400.0,
     645                               slope=6.0,
     646                               thickness=176.0,
     647                               radius=3330,
     648                               dphi=0.23,
     649                               x0=project.slump_origin[0],
     650                               y0=project.slump_origin[1],
     651                               alpha=0.0,
     652                               domain=domain)
     653
     654
     655#-------------------------------------------------------------------------------
     656# Setup initial conditions
     657#-------------------------------------------------------------------------------
     658
     659domain.set_quantity('stage', tsunami_source)
     660domain.set_quantity('friction', 0.03) # supplied by Benfield
     661domain.set_quantity('elevation',
     662                    filename = project.combineddemname + '.pts',
     663                    use_cache = True,
     664                    verbose = True)
     665
     666
     667#-------------------------------------------------------------------------------
     668# Setup boundary conditions (all reflective)
     669#-------------------------------------------------------------------------------
     670
     671print 'Available boundary tags', domain.get_boundary_tags()
     672
     673Br = Reflective_boundary(domain) domain.set_boundary( {'bottom': Br,
     674'right1': Br, 'right0': Br,
     675                      'right2': Br, 'top': Br, 'left1': Br,
     676                      'left2': Br, 'left3': Br} )
     677
     678
     679#-------------------------------------------------------------------------------
     680# Evolve system through time
     681#-------------------------------------------------------------------------------
     682
     683import time t0 = time.time()
     684
     685for t in domain.evolve(yieldstep = 120, finaltime = 18000):
     686    domain.write_time()
     687    domain.write_boundary_statistics(tags = 'bottom')
     688
     689print 'That took %.2f seconds' %(time.time()-t0)
     690
     691\end{verbatim}}
     692
     693Before discussing the details of this example, let us take a more
     694high-level perspective of the various tasks it undertakes.
     695
     696If we compare this example with \code{bedslope.py}, the most
     697noticeable difference is that data is taken from a file. This is to
     698be expected, since the example now represents an actual physical
     699scenario rather than an illustrative example.
     700
     701However, another important difference is that this example uses a
     702different method to create the mesh---one more suited to use for a
     703physical scenario. Instead of imposing a mesh structure on a
     704rectangular grid, the technique used for this example involves
     705building mesh structures inside polygons specified by the user.
     706
     707In its simplest form, this technique creates the mesh within a
     708single polygon whose vertices are at geographical locations
     709specified by the user. A triangular mesh is created using points
     710inside the polygon selected through a random process, the user
     711specifying the \emph{resolution}---that is, the maximal area of a
     712triangle used for triangulation.
    508713
    509714Figure XXX shows a simple example, in which the triangulation is
     
    523728
    524729
     730
     731
     732
     733
     734
    525735\chapter{\anuga Public Interface}
    526736
    527 This chapter lists the functions and classes available at the public interface. 
     737This chapter lists the functions and classes available at the public interface.
    528738
    529739\section{Functions and Classes}
     
    531741\indexedcodeheader{create_mesh_from_region}
    532742
    533   Creates a triangular mesh based on a bounding polygon and 
    534   a number of internal polygons. For each polygon the user specifies a resolution---that is, the maximal area 
    535   of triangles in the mesh. The bounding polygon also has symbolic \code{tags} associated with it.   
    536  
    537   \textbf{Arguments:} 
    538  
     743  Creates a triangular mesh based on a bounding polygon and
     744  a number of internal polygons. For each polygon the user specifies a resolution---that is, the maximal area
     745  of triangles in the mesh. The bounding polygon also has symbolic \code{tags} associated with it.
     746
     747  \textbf{Arguments:}
     748
    539749  \begin{itemize}
    540750    \item the bounding polygon, %do we need to spell out how a polygon is specified?
    541     \item a dictionary of boundary tags, for all segments of the bounding polygon, 
     751    \item a dictionary of boundary tags, for all segments of the bounding polygon,
    542752    \emph{[not clear what the keys and values of this dictionary are]}
    543753    \item the resolution for the bounding polygon,
    544754    \item (optional) a filename,  \emph{[what is the file for?]}
    545     \item a list of 2-tuples \code{(polygon, resolution)}, specifying the interior polygons and 
     755    \item a list of 2-tuples \code{(polygon, resolution)}, specifying the interior polygons and
    546756    their associated resolutions.
    547757  \end{itemize}
    548  
    549    
     758
     759
    550760\indexedcodeheader{pmesh_to_domain_instance}
    551761
    552  Converts a generated mesh file to a domain object. 
    553  
    554   \textbf{Arguments:} 
    555  
     762 Converts a generated mesh file to a domain object.
     763
     764  \textbf{Arguments:}
     765
    556766  \begin{itemize}
    557767    \item \code{file_name} is the name of the mesh file to convert, including the extension
     
    560770    \item \code{use_cache}: \code{True} means that caching is attempted for the computed domain.
    561771  \end{itemize}
    562  
    563  
     772
     773
    564774  \begin{itemize}
    565775    \item Mesh file name
    566     \item Class name, specifying the domain class to be instantiated. 
     776    \item Class name, specifying the domain class to be instantiated.
    567777  \end{itemize}
    568  
    569 \indexedcodeheader{file_function} %in util.py "High priority" 
     778
     779\indexedcodeheader{file_function} %in util.py "High priority"
    570780
    571781  Reads the time history of spatial data from NetCDF file and returns a callable object.
    572782
    573783  \textbf{Input variables:}
    574    
     784
    575785    \code{filename} - Name of \code{sww} or \code{tms} file (see
    576786    Section \ref{sec:file formats} on page \pageref{sec:file formats}
    577787    for details about file formats).
    578        
     788
    579789       \begin{quote}
    580790       If the file has extension \code{sww} then it is assumed to be spatio-temporal
     
    589799       \end{quote}
    590800
    591     \code{domain} - Associated domain object   
     801    \code{domain} - Associated domain object
    592802       If domain is specified, model time (\code{domain.starttime})
    593803       will be checked and possibly modified.
    594    
     804
    595805       \begin{quote}
    596806       All times are assumed to be in UTC.
    597        
     807
    598808       All spatial information is assumed to be in absolute UTM coordinates.
    599809       \end{quote}
     
    601811    \code{quantities} - the name of the quantity to be interpolated or a
    602812                 list of quantity names. The resulting function will return
    603                  a tuple of values -- one for each quantity. 
     813                 a tuple of values -- one for each quantity.
    604814
    605815    \code{interpolation_points} - list of absolute UTM coordinates for points at
    606816    which values are sought
    607    
     817
    608818    \code{use_cache}: \code{True} means that caching of intermediate result of
    609819               \code{Interpolation_function} is attempted
    610820
    611    
    612   %  See Interpolation function for further documentation 
     821
     822  %  See Interpolation function for further documentation
    613823\indexedcodeheader{Interpolation_function}
    614824 Creates a callable object \code{f(t, id)} or \code{f(t,x,y)}
     
    617827
    618828    Let $m$ be the number of vertices, $n$ the number of triangles
    619     and $p$ the number of time steps. 
     829    and $p$ the number of time steps.
    620830
    621831    \textbf{Mandatory input:}
    622    
     832
    623833        \begin{tabular}{ll}
    624834        \code{time}: & $p \times 1$ array of monotonously increasing times (Float)\\
    625        
     835
    626836        \code{quantities}: & Dictionary of arrays or one array (Float). The arrays must  \\
    627837        & have dimensions either $p \times m$ or $m \times 1$. The resulting function \\
     
    629839        & in the latter case.\\
    630840        \end{tabular}
    631        
    632        
     841
     842
    633843    \textbf{Optional input:}
    634    
     844
    635845        \begin{tabular}{ll}
    636846        \code{quantity_names}: & List of keys into the quantities dictionary\\
    637        
     847
    638848        \code{vertex_coordinates}: & $m \times 2$ array of coordinates (Float)\\
    639        
     849
    640850        \code{triangles}: & $n \times 3$ array of indices into \code{vertex_coordinates} (Int)\\
    641        
     851
    642852        \code{interpolation_points}: & $N \times 2$ array of coordinates to be interpolated to \\
    643        
     853
    644854        \code{verbose}: & Level of reporting\\
    645855        \end{tabular}
    646    
     856
    647857    The quantities returned by the callable object are specified by
    648858    the list quantities which must contain the names of the
     
    654864    quantities are to be computed whenever the object is called.
    655865    If \code{None}, returns average value.
    656    
    657 
    658 \indexedcodeheader{set_region} ``Low priority. Will be merged into set\_quantity'' 
    659    
     866
     867
     868\indexedcodeheader{set_region} ``Low priority. Will be merged into set\_quantity''
     869
    660870\indexedcodeheader{set_quantity} ``Pretty mature''
    661  
     871
    662872\indexedcodeheader{set_boundary} ``Pretty mature''
    663  
     873
    664874
    665875
     
    755965  \begin{array}{ccr}
    756966  2 & 4 & 4\\
    757   1 & 1 & 1 
    758   \end{array} 
     967  1 & 1 & 1
     968  \end{array}
    759969  \right]
    760970\]
     
    8251035\section{caching}
    8261036
    827   The \code{cache} function is used to provide supervised caching of function results. A Python 
     1037  The \code{cache} function is used to provide supervised caching of function results. A Python
    8281038  function call of the form
    8291039
     
    8381048      result = cache(func,(arg1,...,argn))
    8391049      \end{verbatim}}
    840  
     1050
    8411051  which returns the same output but reuses cached
    8421052  results if the function has been computed previously in the same context.
    8431053  \code{result} and the arguments can be simple types, tuples, list, dictionaries or
    844   objects, but not unhashable types such as functions or open file objects. 
     1054  objects, but not unhashable types such as functions or open file objects.
    8451055  The function \code{func} may be a member function of an object or a module.
    8461056
     
    8521062  If the function definition changes after a result has been cached it will be
    8531063  detected by examining the functions \code{bytecode (co_code, co_consts,
    854   func_defualts, co_argcount)} and it will be recomputed. 
    855  
    856   Options are set 
    857   by means of the function \code{set_option(key, value)}, where \code{key} is a key associated with a 
    858   Python dictionary \code{options} that stores settings such as the name of the directory used, the maximum 
     1064  func_defualts, co_argcount)} and it will be recomputed.
     1065
     1066  Options are set
     1067  by means of the function \code{set_option(key, value)}, where \code{key} is a key associated with a
     1068  Python dictionary \code{options} that stores settings such as the name of the directory used, the maximum
    8591069  number of cached files allowed, and so on.
    860  
     1070
    8611071  The \code{cache} function allows the user also to specify a list of dependent files. If any of these
    8621072  have been changed, the function is recomputed and the results stored again.
    863  
     1073
    8641074  Other features include support for compression and a capability to \ldots
    8651075
    866  
     1076
    8671077   \textbf{USAGE:}
    868  
     1078
    8691079    {\scriptsize \begin{verbatim}
    8701080    result = cache(func, args, kwargs, dependencies, cachedir, verbose,
     
    8731083
    8741084  \textbf{ARGUMENTS:}
    875  
     1085
    8761086  \begin{tabular}{ll}
    8771087    \code{func} & Function object (Required)\\
    8781088    \code{args} & Arguments to func (Default: ())\\
    879     \code{kwargs} & Keyword arguments to func (Default: {})  \\ 
     1089    \code{kwargs} & Keyword arguments to func (Default: {})  \\
    8801090    \code{dependencies} & Filenames that func depends on (Default: \code{None})\\
    8811091    \code{cachedir} & Directory for cache files (Default: \code{options['cachedir']})\\
     
    8851095    \code{evaluate} & Flag forced evaluation of func (Default: 0)\\
    8861096    \code{test} & Flag test for cached results (Default: 0)\\
    887     \code{clear} & Flag delete cached results (Default: 0)\\   
    888     \code{return_filename} & Flag return of cache filename (Default: 0)\\   
     1097    \code{clear} & Flag delete cached results (Default: 0)\\
     1098    \code{return_filename} & Flag return of cache filename (Default: 0)\\
    8891099  \end{tabular}
    890  
     1100
    8911101
    8921102  \textbf{LIMITATIONS:}
    893  
     1103
    8941104  \begin{itemize}
    8951105   \item Caching uses the apply function and will work with anything that can be
    896       pickled, so any limitation in apply or pickle extends to caching. 
    897      
     1106      pickled, so any limitation in apply or pickle extends to caching.
     1107
    8981108   \item A function to be cached should not depend on global variables
    8991109      as wrong results may occur if globals are changed after a result has
     
    9011111   \end{itemize}
    9021112
    903  
     1113
    9041114
    9051115
     
    10551265    \item \indexedbold{resolution}   refers to the maximal area of each triangular cell in the mesh
    10561266
    1057     \item \indexedbold{polygon} A sequence of points in the plane. (Arbitrary polygons can be created 
     1267    \item \indexedbold{polygon} A sequence of points in the plane. (Arbitrary polygons can be created
    10581268    in this way )
    1059     ANUGA represents polygons as either a list of 2-tuples, where the latter are either Python tuples 
    1060     or Python lists of length 2. The unit square, for example, would be represented by the polygon 
    1061     [ [0,0], [1,0], [1,1], [0,1] ]. Alternatively, polygons can be represented as $N \times 2$ Numeric 
     1269    ANUGA represents polygons as either a list of 2-tuples, where the latter are either Python tuples
     1270    or Python lists of length 2. The unit square, for example, would be represented by the polygon
     1271    [ [0,0], [1,0], [1,1], [0,1] ]. Alternatively, polygons can be represented as $N \times 2$ Numeric
    10621272    arrays, where $N$ is the number of points.
    10631273
Note: See TracChangeset for help on using the changeset viewer.