source: anuga_core/source/anuga/fit_interpolate/general_fit_interpolate.py @ 7317

Last change on this file since 7317 was 7317, checked in by rwilson, 15 years ago

Replaced 'print' statements with log.critical() calls.

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