source: trunk/anuga_core/source/anuga/fit_interpolate/general_fit_interpolate.py @ 8050

Last change on this file since 8050 was 7751, checked in by hudson, 14 years ago

Refactorings from May ANUGA meeting.

File size: 3.6 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
23from anuga.caching.caching import cache
24from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
25
26from anuga.utilities.sparse import Sparse, Sparse_CSR
27from anuga.utilities.cg_solve import conjugate_gradient, VectorShapeError
28from anuga.utilities.numerical_tools import ensure_numeric
29
30from anuga.coordinate_transforms.geo_reference import Geo_reference
31from anuga.geospatial_data.geospatial_data import Geospatial_data, \
32     ensure_absolute
33from anuga.pmesh.mesh_quadtree import MeshQuadtree
34import anuga.utilities.log as log
35
36import numpy as num
37
38                           
39build_quadtree_time = 0
40
41def get_build_quadtree_time():
42    return build_quadtree_time
43
44class FitInterpolate:
45   
46    def __init__(self,
47                 vertex_coordinates=None,
48                 triangles=None,
49                 mesh=None,
50                 mesh_origin=None,
51                 verbose=False):
52
53
54        """ Build interpolation matrix mapping from
55        function values at vertices to function values at data points
56
57        Pass in a mesh instance or vertex_coordinates and triangles
58        and optionally mesh_origin
59       
60        Inputs:
61
62          vertex_coordinates: List of coordinate pairs [xi, eta] of
63          points constituting a mesh (or an m x 2 numeric array or
64              a geospatial object)
65              Points may appear multiple times
66              (e.g. if vertices have discontinuities)
67
68          triangles: List of 3-tuples (or a numeric array) of
69              integers representing indices of all vertices in the mesh.
70
71        mesh: A mesh instance describing the mesh.
72
73          mesh_origin: A geo_reference object or 3-tuples consisting of
74              UTM zone, easting and northing.
75              If specified vertex coordinates are assumed to be
76              relative to their respective origins.
77
78          Note: Don't supply a vertex coords as a geospatial object and
79              a mesh origin, since geospatial has its own mesh origin.
80        """
81        global build_quadtree_time
82        if mesh is None:
83            if vertex_coordinates is not None and  triangles is not None:
84                # Fixme (DSG) Throw errors if triangles or vertex_coordinates
85                # are None
86           
87                #Convert input to numeric arrays
88                triangles = ensure_numeric(triangles, num.int)
89                vertex_coordinates = ensure_absolute(vertex_coordinates,
90                                                 geo_reference = mesh_origin)
91
92                if verbose: log.critical('FitInterpolate: Building mesh')
93                self.mesh = Mesh(vertex_coordinates, triangles)
94                #self.mesh.check_integrity() # Time consuming
95            else:
96                self.mesh = None
97        else:
98            self.mesh = mesh
99
100        if self.mesh is not None:
101            if verbose: log.critical('FitInterpolate: Building quad tree')
102            #This stores indices of vertices
103            t0 = time.time()
104            self.root = MeshQuadtree(self.mesh)
105       
106            build_quadtree_time =  time.time()-t0
107            self.root.set_last_triangle()
108       
109    def __repr__(self):
110        return 'Interpolation object based on: ' + repr(self.mesh)
Note: See TracBrowser for help on using the repository browser.