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

Misc.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 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <assert.h>
00037 #include <math.h>
00038 #include <limits.h>
00039 #include "types.h"
00040 #include "SurfaceMesh.h"
00041 #include "Catchment.h"
00042 #include "Misc.h"
00043 
00044 
00045 void providersDataDeleteFunction( void *data )
00046 {
00047     
00048 }
00049 
00050 /* used in SurfaceMesh.c */
00051 int providersCompareFunction(void *data1, void *data2)
00052 {
00053     int *d1 = NULL, *d2 = NULL;
00054 
00055     d1 = (int*)data1;
00056     d2 = (int*)data2;
00057 
00058     if (d1 == NULL || d2 == NULL){
00059         return 0;   
00060     }
00061     
00062     if (*d1 > *d2){
00063         return  1;
00064     }
00065     else if (*d1 == *d2){
00066         return 0;
00067     }
00068     else{
00069         return -1;
00070     }
00071 }
00072 
00073 /* used in SurfaceMesh.c */
00074 void providersDataPrintFunction( void *nodeData, void *args )
00075 {
00076     Stream *myStream = (Stream*)args;
00077     
00078     assert (nodeData);
00079     assert( myStream );
00080 
00081     Journal_Printf( myStream, "\t%d\n", *(int*)nodeData );
00082 }
00083 
00084 /* used in SurfaceMesh.c */
00085 void providersDataCopyFunction( void **nodeData, void *newData, SizeT dataSize)
00086 {
00087     *nodeData = malloc(dataSize);
00088     memset(*nodeData, 0, dataSize);
00089 
00090     memcpy(*nodeData, newData, dataSize);
00091 }
00092 
00093 /* used in Catchment.c */
00094 int catchmentCompareFunction( void *data1, void *data2 )
00095 {
00096     Catchment *c1, *c2;
00097 
00098     c1 = (Catchment*)data1;
00099     c2 = (Catchment*)data2;
00100 
00101     if( data1 == NULL || data2 == NULL ){
00102         return 0;
00103     }
00104     
00105     if(c1->netSize < c2->netSize){
00106         return 1;
00107     }
00108     else if (c1->netSize == c2->netSize){
00109         return 0;
00110     }
00111     else
00112     {
00113         return -1;
00114     }
00115 }
00116 
00117 /* used in Catchment.c */
00118 void catchmentPrintFunction( void *data, Stream *stream )
00119 {
00120     Catchment *catch = NULL;
00121 
00122     assert( data );
00123     assert( stream );
00124 
00125     catch = (Catchment*) data;
00126 
00127     Journal_Printf( stream, "Catchment with local minimum - %d, size %d\n",
00128                         catch->localMinimum, catch->netSize );
00129 }
00130 
00131 void providerSyncDataCopyFunction( void **nodeData, void *newData, SizeT dataSize)
00132 {
00133     *nodeData = malloc(dataSize);
00134     memset(*nodeData, 0, dataSize);
00135 
00136     memcpy(*nodeData, newData, dataSize);
00137 }
00138 
00139 int providerSyncCompareFunction( void *data1, void *data2 )
00140 {
00141     providerSyncStruct *ps1, *ps2;
00142 
00143     ps1 = (providerSyncStruct*)data1;
00144     ps2 = (providerSyncStruct*)data2;
00145 
00146     if( data1 == NULL || data2 == NULL ){
00147         return 0;
00148     }
00149     
00150     if(ps1->nodeId < ps2->nodeId){
00151         return 1;
00152     }
00153     else if (ps1->nodeId == ps2->nodeId){
00154         return 0;
00155     }
00156     else{
00157         return -1;
00158     }
00159 }
00160 
00161 void providerSyncDataPrintFunction( void *nodeData, void *args )
00162 {
00163     Stream *myStream = (Stream*)args;
00164     providerSyncStruct *data;
00165     
00166     assert (nodeData);
00167     assert( myStream );
00168 
00169     data = (providerSyncStruct*)nodeData;
00170     
00171     Journal_Printf( myStream, "\tnode Id %d, provider %d\n", data->nodeId, data->provider );
00172 }
00173 
00174 /* the following tensor and matrix allocation functions have been adopted from David Hanson's (david@geo.au.dk) code */
00175 double **dmatrix(long nrl, long nrh, long ncl, long nch)
00176 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
00177 {
00178     long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
00179     double **m;
00180 
00181     /* allocate pointers to rows */
00182     m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
00183     if (!m) printf("allocation failure 1 in matrix()\n");
00184     m += NR_END;
00185     m -= nrl;
00186 
00187     /* allocate rows and set pointers to them */
00188     m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
00189     if (!m[nrl]) printf("allocation failure 2 in matrix()\n");
00190     m[nrl] += NR_END;
00191     m[nrl] -= ncl;
00192 
00193     for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
00194 
00195     /* return pointer to array of pointers to rows */
00196     return m;
00197 }
00198 
00199 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
00200 /* free a double matrix allocated by dmatrix() */
00201 {
00202     free((FREE_ARG) (m[nrl]+ncl-NR_END));
00203     free((FREE_ARG) (m+nrl-NR_END));
00204 }
00205 
00206 double ***d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
00207 /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
00208 {
00209     long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
00210     double ***t;
00211 
00212     /* allocate pointers to pointers to rows */
00213     t=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double**)));
00214     if (!t) printf("allocation failure 1 in d3tensor()\n");
00215     t += NR_END;
00216     t -= nrl;
00217 
00218     /* allocate pointers to rows and set pointers to them */
00219     t[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double*)));
00220     if (!t[nrl]) printf("allocation failure 2 in d3tensor()\n");
00221     t[nrl] += NR_END;
00222     t[nrl] -= ncl;
00223 
00224     /* allocate rows and set pointers to them */
00225     t[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(double)));
00226     if (!t[nrl][ncl]) printf("allocation failure 3 in d3tensor()\n");
00227     t[nrl][ncl] += NR_END;
00228     t[nrl][ncl] -= ndl;
00229 
00230     for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
00231     for(i=nrl+1;i<=nrh;i++) {
00232         t[i]=t[i-1]+ncol;
00233         t[i][ncl]=t[i-1][ncl]+ncol*ndep;
00234         for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
00235     }
00236 
00237     /* return pointer to array of pointers to rows */
00238     return t;
00239 }
00240 
00241 void free_d3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
00242     long ndl, long ndh)
00243 /* free a double d3tensor allocated by d3tensor() */
00244 {
00245     free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
00246     free((FREE_ARG) (t[nrl]+ncl-NR_END));
00247     free((FREE_ARG) (t+nrl-NR_END));
00248 }
00249