SPModel: libSPModel/src/LinearInterpolator.c Source File
VPAC - Computational Software Development
Main | SPModel | StGermain FrameWork |
Main Page | Alphabetical List | Class List | Directories | File List | Class Members | File Members

LinearInterpolator.c

Go to the documentation of this file.
00001 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00002 **
00003 ** Copyright (C), 2004, Victorian Partnership for Advanced Computing (VPAC) Ltd, 110 Victoria Street, Melbourne, 3053, Australia.
00004 **
00005 ** Authors:
00006 **  Ogar R. Widjaja, Computational Scientist, VPAC.
00007 **  Raquibul Hassan, Software Engineer, VPAC. (raq@vpac.org)
00008 **  Keith Hsuan, Computational Scientist, VPAC (keith@vpac.org)
00009 **  William F. Appelbe, Director, VPAC. (bill@vpac.org)
00010 **  Stevan M. Quenette, Senior Software Engineer, VPAC. (steve@vpac.org)
00011 **  Patrick D. Sunter, Software Engineer, VPAC. (patrick@vpac.org)
00012 **
00013 ** This file may be distributed under the terms of the VPAC Public License
00014 ** as defined by VPAC of Australia and appearing in the file
00015 ** LICENSE.VPL included in the packaging of this file.
00016 **
00017 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00018 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00019 **
00020 */
00030 #include <mpi.h>
00031 #include <StGermain/StGermain.h>
00032 #include <Cascade/cascade.h>
00033 
00034 #include "types.h"
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <assert.h>
00040 #include <math.h>
00041 #include "Catchment.h"
00042 #include "SurfaceMesh.h"
00043 #include "SurfaceRegularMesh.h"
00044 #include "Context.h"
00045 #include "_Interpolator.h"
00046 #include "LinearInterpolator.h"
00047 
00048 /* Textual name of this class */
00049 const Type LinearInterpolator_Type = "LinearInterpolator";
00050 
00051 LinearInterpolator *LinearInterpolator_DefaultNew( Name name )
00052 {
00053     return _LinearInterpolator_New( sizeof( LinearInterpolator ),
00054                                 LinearInterpolator_Type,
00055                                 _LinearInterpolator_Delete,
00056                                 _LinearInterpolator_Print,
00057                                 NULL,
00058                                 (void*)LinearInterpolator_DefaultNew,
00059                                 _LinearInterpolator_Construct,
00060                                 _LinearInterpolator_Build,
00061                                 _LinearInterpolator_Initialise,
00062                                 _LinearInterpolator_Execute,
00063                                 _LinearInterpolator_Destroy,
00064                                 name,
00065                                 False,
00066                                 NULL,
00067                                 NULL,
00068                                 NULL );
00069     
00070 }
00071 
00072 LinearInterpolator *LinearInterpolator_New( Name name, Dictionary *dictionary, SurfaceMesh *mesh, SurfaceRegularMesh *regularMesh )
00073 {
00074     return _LinearInterpolator_New( sizeof( LinearInterpolator ),
00075                                 LinearInterpolator_Type,
00076                                 _LinearInterpolator_Delete,
00077                                 _LinearInterpolator_Print,
00078                                 NULL,
00079                                 (void*)LinearInterpolator_DefaultNew,
00080                                 _LinearInterpolator_Construct,
00081                                 _LinearInterpolator_Build,
00082                                 _LinearInterpolator_Initialise,
00083                                 _LinearInterpolator_Execute,
00084                                 _LinearInterpolator_Destroy,
00085                                 name,
00086                                 True,
00087                                 dictionary,
00088                                 mesh,
00089                                 regularMesh );
00090     
00091 }
00092 
00093 LinearInterpolator          *_LinearInterpolator_New( SizeT                         _sizeOfSelf,
00094                                     Type                            type,
00095                                     Stg_Class_DeleteFunction*               _delete,
00096                                     Stg_Class_PrintFunction*                _print,
00097                                     Stg_Class_CopyFunction*             _copy, 
00098                                     Stg_Component_DefaultConstructorFunction*   _defaultConstructor,
00099                                     Stg_Component_ConstructFunction*            _construct,
00100                                     Stg_Component_BuildFunction*        _build,
00101                                     Stg_Component_InitialiseFunction*       _initialise,
00102                                     Stg_Component_ExecuteFunction*      _execute,
00103                                     Stg_Component_DestroyFunction*      _destroy,
00104                                     Name                            name,
00105                                     Bool                            initFlag,
00106                                     Dictionary                      *dictionary,
00107                                     SurfaceMesh                     *mesh,
00108                                     SurfaceRegularMesh              *regularMesh )
00109 {
00110     LinearInterpolator* self;
00111     
00112     /* Allocate memory */
00113     assert( _sizeOfSelf >= sizeof(LinearInterpolator) );
00114     self = (LinearInterpolator*)_Interpolator_New( _sizeOfSelf, type, _delete, _print, _copy, _defaultConstructor, _construct, _build, 
00115             _initialise, _execute, _destroy, name, NON_GLOBAL, dictionary, mesh, regularMesh, LinearInterpolator_InterpolateFromGridToMesh );
00116 
00117     if( initFlag ){
00118         _LinearInterpolator_Init( self );
00119     }
00120 
00121     return self;
00122 }
00123 
00124 void _LinearInterpolator_Init( LinearInterpolator *self )
00125 {
00126     _Interpolator_Init( (_Interpolator*)self );
00127 }
00128 
00129 void _LinearInterpolator_Print( void *linearInterpolator, Stream *stream )
00130 {
00131     int numNodes;
00132     
00133     LinearInterpolator *self = (LinearInterpolator*) linearInterpolator;
00134 
00135     assert( self );
00136     assert( stream );
00137     
00138     if( self->mesh->rank == MASTER_PROC ){
00139         numNodes = self->mesh->numNodes;
00140     }
00141     else{
00142         numNodes = self->mesh->myLoad;
00143     }
00144 
00145     /* print parent */
00146     _Stg_Component_Print( (void*) self, stream );
00147     
00148     _Interpolator_Print( (_Interpolator*) self, stream );
00149 
00150     /* general info */
00151     Journal_Printf( stream, "LinearInterpolator (ptr): (%p)\n", self );
00152 
00153     /* Virtual Info */
00154 
00155     /* LinearInterpolator Info */
00156 }
00157 
00158 void _LinearInterpolator_Delete( void *linearInterpolator )
00159 {
00160     LinearInterpolator *self = (LinearInterpolator*)linearInterpolator;
00161 
00162     assert( self );
00163 
00164     free( self->meshWeights[0] );
00165     free( self->meshWeights );
00166     
00167     _Interpolator_Delete( self );
00168 }
00169 
00170 void _LinearInterpolator_Construct( void *linearInterpolator, Stg_ComponentFactory *cf )
00171 {
00172     
00173 }
00174 
00175 void _LinearInterpolator_Build( void *linearInterpolator, void *data )
00176 {
00177     float dx, dy;
00178     int i, id;
00179     float gridNodeX, gridNodeY, s, t;
00180     SurfaceMesh *mesh = NULL;
00181     SurfaceMesh *globalMesh = NULL;
00182     SurfaceRegularMesh *regularMesh = NULL;
00183 
00184     LinearInterpolator *self = (LinearInterpolator*)linearInterpolator;
00185     
00186     assert( self );
00187 
00188     mesh = self->mesh;
00189     regularMesh = self->regularMesh;
00190     globalMesh = ( SurfaceMesh* )data;
00191     assert( globalMesh );
00192 
00193     Journal_Firewall( (self->regularMesh->nx > 0), Journal_Register( Error_Type, "errorStream" ), "0 nodes in x direction..!\n" );
00194     Journal_Firewall( (self->regularMesh->ny > 0), Journal_Register( Error_Type, "errorStream" ), "0 nodes in y direction..!\n" );
00195 
00196     _Interpolator_Build( linearInterpolator, data );
00197     
00198     self->meshWeights = (float**)malloc( sizeof(float*) * self->mesh->numNodes );
00199     self->meshWeights[0] = (float*)malloc( sizeof(float) * 4 * self->mesh->numNodes );
00200     for( i=0; i<self->mesh->numNodes; i++ ){
00201         self->meshWeights[i] = (self->meshWeights[0] + i * 4 );
00202     }
00203     
00204     dx = self->mesh->sideX / (self->regularMesh->nx-1);
00205     dy = self->mesh->sideY / (self->regularMesh->ny-1);
00206 
00207     for( i=0; i<self->mesh->myLoad; i++ ){
00208         id = self->mesh->id[i];
00209 
00210             
00211             gridNodeX = self->regularMesh->regularNodes[self->gridPointsForNode[id-1][0].i-1][self->gridPointsForNode[id-1][0].j-1].x;
00212             gridNodeY = self->regularMesh->regularNodes[self->gridPointsForNode[id-1][0].i-1][self->gridPointsForNode[id-1][0].j-1].y;
00213 
00214             s = 2.0*(self->mesh->x[i] - gridNodeX)/dx - 1.0;
00215             t = 2.0*(self->mesh->y[i] - gridNodeY)/dy - 1.0;
00216      
00217             self->meshWeights[id-1][0] = 0.25*(1.0 - s)*(1.0 - t);
00218             self->meshWeights[id-1][1] = 0.25*(1.0 + s)*(1.0 - t);
00219             self->meshWeights[id-1][2] = 0.25*(1.0 + s)*(1.0 + t);
00220             self->meshWeights[id-1][3] = 0.25*(1.0 - s)*(1.0 + t);
00221         
00222             /*for( j=0; j<4; j++ )
00223                 printf( "\ts %lf, t %lf, meshWeights[%d][%d] = %lf\n", s, t, i, j, self->meshWeights[i][j] );*/
00224     }
00225 }
00226 
00227 void _LinearInterpolator_Initialise( void *linearInterpolator, void *data )
00228 {
00229 
00230 }
00231 
00232 void _LinearInterpolator_Execute( void *linearInterpolator, void *data )
00233 {
00234     
00235 }
00236 
00237 void _LinearInterpolator_Destroy( void *linearInterpolator, void *data )
00238 {
00239     
00240 }
00241 
00242 void LinearInterpolator_InterpolateFromGridToMesh( _Interpolator *interpolator,
00243         float **gridDeflectionArray,
00244         float *meshDeflectionArray )
00245 {
00246     int i = 0, j = 0;
00247     LinearInterpolator *self = NULL;
00248 
00249     assert( interpolator );
00250     
00251     assert( gridDeflectionArray );
00252     assert( meshDeflectionArray );
00253     
00254     self = (LinearInterpolator*)interpolator;
00255     
00256     memset( meshDeflectionArray, 0, sizeof( float ) * interpolator->mesh->numNodes );
00257     for( i=0; i<interpolator->mesh->numNodes; i++ ){
00258         meshDeflectionArray[i] = 0.0f;
00259         for( j=0; j<4; j++ ){
00260             
00261             
00262             if( (interpolator->gridPointsForNode[i][j].i > 0) && (interpolator->gridPointsForNode[i][j].j > 0) ){
00263                 
00264                 meshDeflectionArray[i] += self->meshWeights[i][j] *
00265                     gridDeflectionArray[interpolator->gridPointsForNode[i][j].i-1]
00266                                         [interpolator->gridPointsForNode[i][j].j-1];
00267             }
00268         }
00269     }
00270 }
00271