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

Catchment.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 ** $Id: CatchmentList.c 153 2005-04-21 01:02:43Z RaquibulHassan $
00021 **
00022 **~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
00023 
00024 #include <mpi.h>
00025 #include <StGermain/StGermain.h>
00026 #include <Cascade/cascade.h>
00027 
00028 
00029 #include "types.h"
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 #include <math.h>
00036 #include <limits.h>
00037 #include "Catchment.h"
00038 #include "SurfaceMesh.h"
00039 #include "Misc.h"
00040 
00041 /* Textual name of this class */
00042 const Type CatchmentList_Type = "CatchmentList";
00043 
00044 CatchmentList *CatchmentList_DefaultNew( Name name )
00045 {
00046     return _CatchmentList_New( sizeof( CatchmentList ),
00047                                 CatchmentList_Type,
00048                                 _CatchmentList_Delete,
00049                                 _CatchmentList_Print,
00050                                 NULL,
00051                                 (void*)CatchmentList_DefaultNew,
00052                                 _CatchmentList_Construct,
00053                                 _CatchmentList_Build,
00054                                 _CatchmentList_Initialise,
00055                                 _CatchmentList_Execute,
00056                                 _CatchmentList_Destroy,
00057                                 name,
00058                                 False,
00059                                 NULL,
00060                                 0,
00061                                 NULL );
00062 }
00063 
00064 CatchmentList *CatchmentList_New( Name name, SurfaceMesh *mesh, int numProcs, Dictionary *dictionary )
00065 {
00066     return _CatchmentList_New( sizeof( CatchmentList ),
00067                                 CatchmentList_Type,
00068                                 _CatchmentList_Delete,
00069                                 _CatchmentList_Print,
00070                                 NULL,
00071                                 (void*)CatchmentList_DefaultNew,
00072                                 _CatchmentList_Construct,
00073                                 _CatchmentList_Build,
00074                                 _CatchmentList_Initialise,
00075                                 _CatchmentList_Execute,
00076                                 _CatchmentList_Destroy,
00077                                 name,
00078                                 True,
00079                                 mesh,
00080                                 numProcs,
00081                                 dictionary );
00082 }
00083 
00084 CatchmentList   *_CatchmentList_New( SizeT                          _sizeOfSelf,
00085                         Type                            type,
00086                         Stg_Class_DeleteFunction*               _delete,
00087                         Stg_Class_PrintFunction*                _print,
00088                         Stg_Class_CopyFunction*             _copy, 
00089                         Stg_Component_DefaultConstructorFunction*   _defaultConstructor,
00090                         Stg_Component_ConstructFunction*            _construct,
00091                         Stg_Component_BuildFunction*        _build,
00092                         Stg_Component_InitialiseFunction*       _initialise,
00093                         Stg_Component_ExecuteFunction*      _execute,
00094                         Stg_Component_DestroyFunction*      _destroy,
00095                         Name                            name,
00096                         Bool                            initFlag,
00097                         SurfaceMesh*                    mesh,
00098                         int                             numProcs,
00099                         Dictionary*                     dictionary )
00100 {
00101     CatchmentList *self = NULL;
00102 
00103     assert( _sizeOfSelf >= sizeof( CatchmentList ) );
00104     
00105     self = (CatchmentList*) _Stg_Component_New( _sizeOfSelf, type, _delete, _print, NULL, _defaultConstructor, 
00106             _construct, _build, _initialise, _execute, _destroy, name, NON_GLOBAL );
00107     
00108     self->mesh = mesh;
00109     self->dictionary = dictionary;
00110     self->numProcs = numProcs;
00111 
00112     if( initFlag ){
00113         _CatchmentList_Init( self );
00114     }
00115 
00116     return self;
00117 }
00118 
00119 void CatchmentList_Init(    CatchmentList *self, 
00120                             Name name, 
00121                             SurfaceMesh *mesh, 
00122                             int numProcs, 
00123                             Dictionary *dictionary )
00124 {
00125     self->type = CatchmentList_Type;
00126     self->_sizeOfSelf = sizeof( CatchmentList );
00127     self->_deleteSelf = True;
00128     self->dictionary = dictionary;
00129 
00130     self->_delete = _CatchmentList_Delete;
00131     self->_print = _CatchmentList_Print;
00132     self->_copy = NULL;
00133     self->_defaultConstructor = (void*)CatchmentList_DefaultNew;
00134     self->_construct = _CatchmentList_Construct;
00135     self->_build = _CatchmentList_Build;
00136     self->_initialise = _CatchmentList_Initialise;
00137     self->_execute = _CatchmentList_Execute;
00138     self->_destroy = _CatchmentList_Destroy;
00139     
00140     _Stg_Class_Init( (Stg_Class*)self );
00141     _Stg_Object_Init( (Stg_Object*)self, name, NON_GLOBAL );
00142     _Stg_Component_Init( (Stg_Component*)self );
00143 
00144     _CatchmentList_Init( self );
00145 }
00146     
00147 void _CatchmentList_Init( CatchmentList *self )
00148 {
00149     assert( self );
00150 
00151     self->catchments = NULL;
00152 }
00153 
00154 void _CatchmentList_Delete( void *cList )
00155 {
00156     CatchmentList *self = NULL;
00157     
00158     /* freeing all the memory allocated previously */
00159 
00160     assert( cList );
00161 
00162     self = (CatchmentList*) cList;
00163 
00164     if( self->catchments ){
00165         Stg_Class_Delete( self->catchments );
00166     }
00167 
00168     _Stg_Class_Delete( cList );
00169 }
00170     
00171 void _CatchmentList_Print( void *cList, Stream *stream )
00172 {
00173     CatchmentList *self = NULL;
00174 
00175     assert( cList );
00176     assert( stream );
00177 
00178     self = ( CatchmentList* )cList;
00179     
00180     /* printting parent information */
00181     _Stg_Class_Print( cList, stream );
00182     Stg_Class_Print( self->catchments, stream );
00183 }
00184 
00185 void _CatchmentList_Construct ( void *cList, Stg_ComponentFactory *cf )
00186 {
00187     
00188 }
00189 
00190 void _CatchmentList_Build ( void *cList, void *data )
00191 {
00192     CatchmentList *self = NULL;
00193     
00194     assert( cList );
00195 
00196     self = (CatchmentList*) cList;
00197 
00198     if( self->mesh->rank == MASTER_PROC ){
00199         self->catchments = BTree_New(
00200                                 catchmentCompareFunction,
00201                                 NULL,
00202                                 NULL,
00203                                 catchmentPrintFunction,
00204                                 BTREE_ALLOW_DUPLICATES);
00205         CatchmentList_InsertCatchments( self );
00206         
00207         CalculateCatchmentCentres( self );
00208     }
00209     else{
00210     }
00211 }
00212 
00213 void _CatchmentList_Initialise( void *cList, void *data )
00214 {
00215 
00216 }
00217     
00218 void _CatchmentList_Execute( void *cList, void *data )
00219 {
00220     
00221 }
00222 
00223 void _CatchmentList_Destroy( void *cList, void *data )
00224 {
00225     
00226 }
00227 
00228 void CatchmentList_InsertCatchments( CatchmentList *self )
00229 {   
00230     Catchment *newCatch = NULL;
00231     int i = 0;
00232     SurfaceMesh *mesh = NULL;
00233     
00234     assert( self );
00235     mesh = self->mesh;
00236     
00237     /* Inserting catchments into the catchmentList */
00238     for( i=0; i<mesh->numNodes; i++ ){
00239     
00240         if( mesh->id[i] == mesh->receiver[i] ){
00241             newCatch = (Catchment*) malloc(sizeof(Catchment));
00242             
00243             if ( newCatch == NULL ) {
00244                 printf( "Catchment not inserted, not enough memory\n" );
00245                 exit(0);
00246             }
00247             else {
00248                 newCatch->localMinimum = mesh->id[i];
00249                 newCatch->subCatchments = NULL;
00250                 newCatch->netSize = mesh->size[i];
00251                 
00252                 AssignCatchmentNumber( self, mesh->id[i], mesh->id[i] );
00253                 
00254                 BTree_InsertNode( self->catchments, newCatch, sizeof( Catchment * ) );
00255             }
00256         }
00257     }
00258 }
00259 
00260 void AssignCatchmentNumber( CatchmentList *self, int nodeId, int locMin )
00261 {
00262     int i, q;
00263     int *result = NULL;
00264     SurfaceMesh *mesh = NULL;
00265     
00266     assert( self );
00267     mesh = self->mesh;
00268     
00269     /* this function recurses uptree and assigns the local minimum to each node's
00270      * nCatch. In other words node's become aware of which catchment they are a part of */
00271     
00272     mesh->nCatch[nodeId-1] = locMin;
00273     for (i=0; i<mesh->nodeProviders[nodeId-1]->nodeCount; i++) {
00274         result = LinkedList_ReturnNodeDataAt(mesh->nodeProviders[nodeId-1], i, int);
00275         assert(result);
00276         q = mesh->id[*result - 1];
00277         AssignCatchmentNumber( self, q, locMin );
00278     }   
00279 }
00280 
00281 void CalculateCatchmentCentres( CatchmentList *self )
00282 {
00283     BTreeIterator *iterator = NULL;
00284     Catchment *result = NULL;
00285 
00286     assert( self );
00287 
00288     iterator = BTreeIterator_New( self->catchments );
00289 
00290     for( result = (Catchment*)BTreeIterator_First( iterator ); result; result = BTreeIterator_Next( iterator ) ){
00291         CalculateCatchmentCentreHelper( self, result->localMinimum, &(result->catchmentCentre) );
00292 
00293         result->catchmentCentre.x /= result->netSize;
00294         result->catchmentCentre.y /= result->netSize;
00295         result->catchmentCentre.h /= result->netSize;
00296         #ifdef USE_DEBUG
00297         printf( "catchment with local minumum %d, netSize %d, has a centre x %lf, y %lf, h %lf\n", result->localMinimum,
00298             result->netSize, result->catchmentCentre.x,result->catchmentCentre.y, result->catchmentCentre.h );
00299         #endif
00300     }
00301 
00302     Stg_Class_Delete( iterator );
00303 }
00304 
00305 void CalculateCatchmentCentreHelper( CatchmentList *self, int nodeId, vector *sum )
00306 {
00307     int i, q;
00308     int *result = NULL;
00309     SurfaceMesh *mesh = NULL;
00310     
00311     assert( self );
00312     assert( sum );
00313     
00314     mesh = self->mesh;
00315     
00316     /* this function recurses uptree and calculates the geometric centre of the
00317      * catchment */
00318 
00319     sum->x += mesh->x[nodeId-1];
00320     sum->y += mesh->y[nodeId-1];
00321     sum->h += mesh->h[nodeId-1];
00322 
00323     for (i=0; i<mesh->nodeProviders[nodeId-1]->nodeCount; i++) {
00324         result = LinkedList_ReturnNodeDataAt(mesh->nodeProviders[nodeId-1], i, int);
00325         assert(result);
00326         q = mesh->id[*result - 1];
00327         CalculateCatchmentCentreHelper( self, q, sum );
00328     }   
00329 }
00330