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

ParameterTimeSeries.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: ParameterTimeSeries.c 177 2005-08-23 10:15:00Z RaquibulHassan $
00021 **
00022 **~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
00023 
00024 #include <mpi.h>
00025 #include <StGermain/StGermain.h>
00026 #include <Cascade/cascade.h>
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <assert.h>
00032 #include <math.h>
00033 #include <limits.h>
00034 #include "types.h"
00035 #include "Context.h"
00036 #include "ParameterTimeSeries.h"
00037 
00038 /* Textual name of this class */
00039 const Type ParameterTimeSeries_Type = "ParameterTimeSeries";
00040 
00041 ParameterTimeSeries *ParameterTimeSeries_DefaultNew( Name name )
00042 {
00043     return _ParameterTimeSeries_New( sizeof( ParameterTimeSeries ),
00044                                         ParameterTimeSeries_Type,
00045                                         ParameterTimeSeries_Delete,
00046                                         ParameterTimeSeries_Print,
00047                                         NULL,
00048                                         (void*)ParameterTimeSeries_DefaultNew,
00049                                         ParameterTimeSeries_Construct,
00050                                         ParameterTimeSeries_Build,
00051                                         ParameterTimeSeries_Initialise,
00052                                         ParameterTimeSeries_Execute,
00053                                         ParameterTimeSeries_Destroy,
00054                                         name,
00055                                         0.0f,
00056                                         False,
00057                                         NULL );
00058 }
00059 
00060 ParameterTimeSeries *ParameterTimeSeries_New( Name name, double defaultValue, Dictionary *dictionary )
00061 {
00062     return _ParameterTimeSeries_New( sizeof( ParameterTimeSeries ),
00063                                         ParameterTimeSeries_Type,
00064                                         ParameterTimeSeries_Delete,
00065                                         ParameterTimeSeries_Print,
00066                                         NULL,
00067                                         (void*)ParameterTimeSeries_DefaultNew,
00068                                         ParameterTimeSeries_Construct,
00069                                         ParameterTimeSeries_Build,
00070                                         ParameterTimeSeries_Initialise,
00071                                         ParameterTimeSeries_Execute,
00072                                         ParameterTimeSeries_Destroy,
00073                                         name,
00074                                         defaultValue,
00075                                         True,
00076                                         dictionary);
00077 }
00078 
00079 ParameterTimeSeries *_ParameterTimeSeries_New( SizeT                            _sizeOfSelf,
00080                                     Type                            type,
00081                                     Stg_Class_DeleteFunction*               _delete,
00082                                     Stg_Class_PrintFunction*                _print,
00083                                     Stg_Class_CopyFunction*             _copy, 
00084                                     Stg_Component_DefaultConstructorFunction*   _defaultConstructor,
00085                                     Stg_Component_ConstructFunction*            _construct,
00086                                     Stg_Component_BuildFunction*        _build,
00087                                     Stg_Component_InitialiseFunction*       _initialise,
00088                                     Stg_Component_ExecuteFunction*      _execute,
00089                                     Stg_Component_DestroyFunction*      _destroy,
00090                                     Name                            name,
00091                                     double                          defaultValue,
00092                                     Bool                            initFlag,
00093                                     Dictionary                      *dictionary )
00094 {
00095     ParameterTimeSeries* self;
00096     
00097     /* Allocate memory */
00098     assert( _sizeOfSelf >= sizeof(ParameterTimeSeries) );
00099     self = (ParameterTimeSeries*)_Stg_Component_New( _sizeOfSelf, type, _delete, _print, _copy, _defaultConstructor, _construct, _build, 
00100             _initialise, _execute, _destroy, name, NON_GLOBAL );
00101 
00102     assert( dictionary );
00103     
00104     self->dictionary = dictionary;
00105     self->defaultValue = defaultValue;
00106 
00107     if( initFlag ){
00108         ParameterTimeSeries_Init( self );
00109     }
00110 
00111     return self;
00112 }
00113 
00114 void ParameterTimeSeries_Init( ParameterTimeSeries *self )
00115 {
00116     assert( self );
00117 
00118     self->numEntries = 0;
00119     self->value = 0.0f;
00120     self->currIndex = 0;
00121 }
00122 
00123 void ParameterTimeSeries_Print( void *parameterTimeSeries, Stream *stream )
00124 {
00125     ParameterTimeSeries *self = (ParameterTimeSeries*) parameterTimeSeries;
00126 
00127     assert( self );
00128     assert( stream );
00129 
00130     /* print parent */
00131     _Stg_Component_Print( (void*) self, stream );
00132 
00133     /* general info */
00134     Journal_Printf( stream, "ParameterTimeSeries (ptr): (%p)\n", self );
00135 
00136     /* Virtual Info */
00137 
00138     /* SurfaceMeshCyclicBC Info */
00139     
00140 }
00141 
00142 void ParameterTimeSeries_Delete( void *parameterTimeSeries )
00143 {
00144     ParameterTimeSeries *self = ( ParameterTimeSeries* ) parameterTimeSeries;
00145 
00146     assert( self );
00147 
00148     if( self->parameterValueArray ){
00149         free( self->parameterValueArray );
00150     }
00151 
00152     if( self->timeStepArray ){
00153         free( self->timeStepArray );
00154     }
00155     
00156     _Stg_Component_Delete( parameterTimeSeries );
00157 }
00158 
00159 void ParameterTimeSeries_Construct( void *parameterTimeSeries, Stg_ComponentFactory *cf )
00160 {
00161     
00162 }
00163 
00164 void ParameterTimeSeries_Build( void *parameterTimeSeries, void *data )
00165 {
00166     ParameterTimeSeries *self = (ParameterTimeSeries*)parameterTimeSeries;
00167     char temp[500];
00168     char *parameterSeriesFileName = NULL;
00169     FILE *f = NULL;
00170     int count = 0;
00171     int i = 0;
00172 
00173     assert( self );
00174 
00175     memset( temp, 0, sizeof( temp ) );
00176 
00177     sprintf( temp, "%s%s", self->name, "File" );
00178 
00179     parameterSeriesFileName = Dictionary_GetString( self->dictionary, temp );
00180     
00181     if( strlen(parameterSeriesFileName) ){
00182         f = fopen( parameterSeriesFileName, "r" );
00183         if( f ){
00184             count = 0;
00185             while( fgets( temp, sizeof( temp ), f ) != NULL ){
00186                 count++;
00187             }
00188 
00189             if( count ){
00190                 self->numEntries = count;
00191 
00192                 self->timeStepArray = malloc( sizeof( double ) * count );
00193                 memset( self->timeStepArray, 0, sizeof( double ) * count );
00194                 self->parameterValueArray = malloc( sizeof( double ) * count );
00195                 memset( self->parameterValueArray, 0, sizeof( double ) * count );
00196             }
00197 
00198             fclose( f );
00199             
00200             f = fopen( parameterSeriesFileName, "r" );
00201             
00202             memset( temp, 0, sizeof( temp ) );
00203             i = 0;
00204             while( fgets( temp, sizeof(temp), f ) != NULL ){
00205                 sscanf( temp, "%lf %lf", &(self->timeStepArray[i]), &(self->parameterValueArray[i]) );
00206                 memset( temp, 0, sizeof( temp ) );
00207                 i++;
00208             }
00209             fclose( f );
00210         }
00211         else{
00212             fprintf( stderr, "File %s not found..!\n", parameterSeriesFileName );
00213         }
00214     }
00215     else{
00216         self->value = Dictionary_GetDouble_WithDefault( self->dictionary, self->name, self->defaultValue );
00217     }
00218 }
00219 
00220 void ParameterTimeSeries_Initialise( void *parameterTimeSeries, void *data )
00221 {
00222     
00223 }
00224 
00225 void ParameterTimeSeries_Execute( void *parameterTimeSeries, void *data )
00226 {
00227     
00228 }
00229 
00230 void ParameterTimeSeries_Destroy( void *parameterTimeSeries, void *data )
00231 {
00232     
00233 }
00234 
00235 void ParameterTimeSeries_InterpolateFunc( ParameterTimeSeries *parameterTimeSeries, void *data )
00236 {
00237     ParameterTimeSeries *self = parameterTimeSeries;
00238     SPModel_Context *context = (SPModel_Context*)data;
00239 
00240     assert( self );
00241     assert( context );
00242 
00243     if( context->currentTime >= self->timeStepArray[self->currIndex] ){
00244         self->currIndex++;
00245     }
00246     
00247     if( self->currIndex <= self->numEntries ){
00248         if( (context->currentTime <= self->timeStepArray[self->currIndex]) ){
00249             self->value = self->parameterValueArray[self->currIndex] +
00250                             ( (-self->parameterValueArray[self->currIndex-1] + self->parameterValueArray[self->currIndex]) / 
00251                               (-self->timeStepArray[self->currIndex-1] + self->timeStepArray[self->currIndex]) * 
00252                               (context->currentTime - self->timeStepArray[self->currIndex]) );
00253         }
00254         else{
00255             fprintf( stderr, "Error in %sFile\n", self->name );
00256         }
00257     }
00258 }
00259