source: branches/source_numpy_conversion/anuga/fit_interpolate/general_fit_interpolate.py @ 6768

Last change on this file since 6768 was 5905, checked in by rwilson, 16 years ago

NumPy? conversion.

File size: 4.4 KB
Line 
1"""Least squares interpolation.
2
3   Implements a least-squares interpolation.
4
5   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
6   Geoscience Australia, 2004.
7
8DESIGN ISSUES
9* what variables should be global?
10- if there are no global vars functions can be moved around alot easier
11
12* The public interface
13__init__
14interpolate
15interpolate_block
16
17"""
18
19import time
20import os
21from warnings import warn
22
23import numpy
24
25from anuga.caching.caching import cache
26from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
27
28from anuga.utilities.sparse import Sparse, Sparse_CSR
29from anuga.utilities.cg_solve import conjugate_gradient, VectorShapeError
30from anuga.utilities.numerical_tools import ensure_numeric
31from anuga.utilities.polygon import in_and_outside_polygon
32from anuga.utilities.quad import build_quadtree
33
34from anuga.coordinate_transforms.geo_reference import Geo_reference
35from anuga.geospatial_data.geospatial_data import Geospatial_data, \
36     ensure_absolute
37from anuga.fit_interpolate.search_functions import set_last_triangle
38
39# tests fail if 2 is used
40MAX_VERTICES_PER_CELL = 13 # A value of 8 or lower can cause problems,
41                           # if a vert has 9 triangles.
42                           
43build_quadtree_time = 0
44
45def get_build_quadtree_time():
46    return build_quadtree_time
47
48class FitInterpolate:
49   
50    def __init__(self,
51                 vertex_coordinates=None,
52                 triangles=None,
53                 mesh=None,
54                 mesh_origin=None,
55                 verbose=False,
56                 max_vertices_per_cell=None):
57
58
59        """ Build interpolation matrix mapping from
60        function values at vertices to function values at data points
61
62        Pass in a mesh instance or vertex_coordinates and triangles
63        and optionally mesh_origin
64       
65        Inputs:
66
67          vertex_coordinates: List of coordinate pairs [xi, eta] of
68              points constituting a mesh (or an m x 2 Numeric array or
69              a geospatial object)
70              Points may appear multiple times
71              (e.g. if vertices have discontinuities)
72
73          triangles: List of 3-tuples (or a Numeric array) of
74              integers representing indices of all vertices in the mesh.
75
76        mesh: A mesh instance describing the mesh.
77
78          mesh_origin: A geo_reference object or 3-tuples consisting of
79              UTM zone, easting and northing.
80              If specified vertex coordinates are assumed to be
81              relative to their respective origins.
82
83          max_vertices_per_cell: Number of vertices in a quad tree cell
84          at which the cell is split into 4.
85
86          Note: Don't supply a vertex coords as a geospatial object and
87              a mesh origin, since geospatial has its own mesh origin.
88        """
89        global build_quadtree_time
90        if max_vertices_per_cell == None:
91            max_vertices_per_cell = MAX_VERTICES_PER_CELL
92        if mesh is None:
93            if vertex_coordinates is not None and  triangles is not None:
94                # Fixme (DSG) Throw errors if triangles or vertex_coordinates
95                # are None
96           
97                #Convert input to Numeric arrays
98                triangles = ensure_numeric(triangles, numpy.int)
99                vertex_coordinates = ensure_absolute(vertex_coordinates,
100                                                 geo_reference = mesh_origin)
101
102                if verbose: print 'FitInterpolate: Building mesh'       
103                self.mesh = Mesh(vertex_coordinates, triangles)
104                #self.mesh.check_integrity() # Time consuming
105            else:
106                self.mesh = None
107        else:
108            self.mesh = mesh
109
110        if self.mesh is not None:
111            if verbose: print 'FitInterpolate: Building quad tree'
112            #This stores indices of vertices
113            t0 = time.time()
114            #print "self.mesh.get_extent(absolute=True)", \
115            #self.mesh.get_extent(absolute=True)
116            self.root = build_quadtree(self.mesh,
117                                       max_points_per_cell = max_vertices_per_cell)
118            #print "self.root",self.root.show()
119       
120            build_quadtree_time =  time.time()-t0
121            set_last_triangle()
122       
123    def __repr__(self):
124        return 'Interpolation object based on: ' + repr(self.mesh)
Note: See TracBrowser for help on using the repository browser.