/* Test the Get and Set of a vector double....................................................................... */
void VariableSuite_TestGetValueAtDouble( VariableSuiteData* data ) {
    Index                   ii;
    double tmp;
    Variable*      var = Variable_Register_GetByName( data->vr, "velocity" );

    /* Fill the velocity array with a known pattern of kinda random (bit filling) numbers. */
    for( ii = 0; ii < data->aSize[1]; ii++ ) {
        int         d;

        for( d = 0; d < 3; d++ ) {
            data->velocity[ii][d] = 1.0f / ((data->aSize[1]*3)+2) * (ii*3+d+1);
        }
    }

    /* Check that Variable_GetPtrDouble on the velocity Variable returns the right numbers */
    for( ii = 0; ii < data->aSize[1]; ii++ ) {
        int         d;

        for( d = 0; d < 3; d++ ) {
            tmp = 1.0f / ((data->aSize[1]*3)+2) * (ii*3+d+1);

            pcu_check_true( fabs(Variable_GetValueAtDouble(var, ii, d ) - tmp) < 1e-12);
        }
    }
}
void TimeIntegrationSuite_TestDriver( TimeIntegrationSuiteData* data, char *_name, char *_DerivName0, char *_DerivName1, int _order ) {
   Stg_ComponentFactory* cf;
   Stream*               stream;
   Dictionary*           dictionary;
   TimeIntegrator*       timeIntegrator;
   TimeIntegrand*        timeIntegrand;
   TimeIntegrand*        timeIntegrandList[2];
   DomainContext*        context;
   Variable*             variable;
   Variable*             variableList[2];
   double*               array;
   double*               array2;
   Index                 size0 = 11;
   Index                 size1 = 7;
   Index                 array_I;
   Index                 timestep = 0;
   Index                 maxTimesteps = 10;
   Bool                  simultaneous;
   unsigned              order;
   double                error = 0.0;
   Name                  derivName;
   double                tolerance = 0.001;
   Index                 integrand_I;
   Index                 integrandCount = 2;
   char                  expected_file[PCU_PATH_MAX];

   dictionary = Dictionary_New();
   Dictionary_Add( dictionary, (Dictionary_Entry_Key)"outputPath", Dictionary_Entry_Value_FromString("./output") );
   Dictionary_Add( dictionary, (Dictionary_Entry_Key)"DerivName0", Dictionary_Entry_Value_FromString(_DerivName0) );
   Dictionary_Add( dictionary, (Dictionary_Entry_Key)"DerivName1", Dictionary_Entry_Value_FromString(_DerivName1) );

   context = DomainContext_New( "context", 0, 0, MPI_COMM_WORLD, NULL );
   cf = stgMainConstruct( dictionary, NULL, data->comm, context );
   stgMainBuildAndInitialise( cf );
      
   ContextEP_Append( context, AbstractContext_EP_Dt, TimeIntegrationSuite_GetDt );

   /* Create Stuff */
   order = _order;
   simultaneous = False;
   variableList[0] = Variable_NewVector( "testVariable", (AbstractContext*)context, Variable_DataType_Double, 2, &size0, NULL, (void**)&array, NULL );
   variableList[1] = Variable_NewVector( "testVariable2", (AbstractContext*)context, Variable_DataType_Double, 2, &size1, NULL, (void**)&array2, NULL );
   timeIntegrator = TimeIntegrator_New( "testTimeIntegrator", order, simultaneous, NULL, NULL );
   timeIntegrator->context = context;
   timeIntegrandList[0] = TimeIntegrand_New( "testTimeIntegrand0", context, timeIntegrator, variableList[0], 0, NULL, True );
   timeIntegrandList[1] = TimeIntegrand_New( "testTimeIntegrand1", context, timeIntegrator, variableList[1], 0, NULL, True );

   Journal_Enable_AllTypedStream( True );
   stream = Journal_Register( Info_Type, (Name)"EulerStream"  );
   Stream_RedirectFile( stream, _name );

   Stream_Enable( timeIntegrator->info, False );
   derivName = Dictionary_GetString( dictionary, (Dictionary_Entry_Key)"DerivName0" );
   timeIntegrandList[0]->_calculateTimeDeriv = TimeIntegrationSuite_GetFunctionPtr( derivName  );
   Journal_Printf( stream, "DerivName0 - %s\n", derivName );
   derivName = Dictionary_GetString( dictionary, (Dictionary_Entry_Key)"DerivName1" );
   timeIntegrandList[1]->_calculateTimeDeriv = TimeIntegrationSuite_GetFunctionPtr( derivName  );
   Journal_Printf( stream, "DerivName1 - %s\n", derivName );

   /* Print Stuff to file */
   Journal_PrintValue( stream, order );
   Journal_PrintBool( stream, simultaneous );

   /* Add stuff to EPs */
   TimeIntegrator_AppendSetupEP( timeIntegrator, "start1", TimeIntegrationSuite_TestContextType, CURR_MODULE_NAME, context );
   TimeIntegrator_AppendFinishEP( timeIntegrator, "finish1", TimeIntegrationSuite_TestVariableType, CURR_MODULE_NAME, variableList[0] );
   TimeIntegrator_PrependSetupEP( timeIntegrator, "start0", TimeIntegrationSuite_TestVariableType, CURR_MODULE_NAME, variableList[0] );
   TimeIntegrator_PrependFinishEP( timeIntegrator, "finish0", TimeIntegrationSuite_TestContextType, CURR_MODULE_NAME, context );

   /* Build */
   Stg_Component_Build( variableList[0], context, False );
   Stg_Component_Build( variableList[1], context, False );
   Stg_Component_Build( timeIntegrator, context, False );
   Stg_Component_Build( timeIntegrandList[0], context, False );
   Stg_Component_Build( timeIntegrandList[1], context, False );
   array = Memory_Alloc_Array( double, 2 * size0, "name" );
   array2 = Memory_Alloc_Array( double, 2 * size1, "name" );

   /* Initialise */
   memset( array, 0, sizeof(double) * 2 * size0 );
   memset( array2, 0, sizeof(double) * 2 * size1 );
   Stg_Component_Initialise( timeIntegrator, context, False );
   Stg_Component_Initialise( variableList[0], context, False );
   Stg_Component_Initialise( variableList[1], context, False );
   Stg_Component_Initialise( timeIntegrandList[0], context, False );
   Stg_Component_Initialise( timeIntegrandList[1], context, False );

   for ( timestep = 0.0 ; timestep < maxTimesteps ; timestep ++ ) {
      Journal_Printf( stream, "Step %u - Time = %.3g\n", timestep, context->currentTime );

      Stg_Component_Execute( timeIntegrator, context, True );
      context->currentTime += AbstractContext_Dt( context );

      for ( integrand_I = 0 ; integrand_I < integrandCount ; integrand_I++ ) {
         timeIntegrand   = timeIntegrandList[ integrand_I ];
         variable         = variableList[ integrand_I ];
         for ( array_I = 0 ; array_I < variable->arraySize ; array_I++ ) {
            if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_ConstantTimeDeriv ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) - 2.0 * array_I * context->currentTime );
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) + array_I * context->currentTime );
            }
            else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_ConstantTimeDeriv2 ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) + 0.5 * array_I * context->currentTime );
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) - 3 * array_I * context->currentTime );
            }
            else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_LinearTimeDeriv ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) - array_I * context->currentTime * context->currentTime );
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) + 0.5 * array_I * context->currentTime * context->currentTime );
            }
            else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_LinearTimeDeriv2 ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) + 0.25 * array_I * context->currentTime * context->currentTime );
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) - 1.5 * array_I * context->currentTime * context->currentTime );
            }
            else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_CubicTimeDeriv ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) - 2.0 * array_I * ( 0.25 * pow( context->currentTime, 4.0 ) - pow( context->currentTime, 3.0)/3.0));
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) + array_I * ( 0.25 * pow( context->currentTime, 4.0 ) - pow( context->currentTime, 3.0 )/3.0));
            }
            else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_CubicTimeDeriv2 ) {
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 0 ) + 0.5 * array_I * ( 0.25 * pow( context->currentTime, 4.0 ) - pow( context->currentTime, 3.0)/3.0));
               error += fabs( Variable_GetValueAtDouble( variable, array_I, 1 ) - 3.0 * array_I * ( 0.25 * pow( context->currentTime, 4.0 ) - pow( context->currentTime, 3.0 )/3.0));
            }
            else
               Journal_Firewall( 0 , Journal_Register( Error_Type, (Name)CURR_MODULE_NAME  ), "Don't understand _calculateTimeDeriv = %p\n", timeIntegrand->_calculateTimeDeriv );
         }
      }
   }
   pcu_check_lt( error, tolerance );

   if ( error < tolerance )
      Journal_Printf( stream, "Passed\n" );
   else
      Journal_Printf( stream, "Failed - Error = %lf\n", error );
   
   Journal_Enable_AllTypedStream( False );

   if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_ConstantTimeDeriv
      || timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_ConstantTimeDeriv2 ) {
      pcu_filename_expected( "testTimeIntegrationEulerOutput.expected", expected_file );
   }
   else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_LinearTimeDeriv
      || timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_LinearTimeDeriv2 ) {
      pcu_filename_expected( "testTimeIntegrationRK2Output.expected", expected_file );
   }
   else if ( timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_CubicTimeDeriv
      || timeIntegrand->_calculateTimeDeriv == TimeIntegrationSuite_CubicTimeDeriv2 ) {
      pcu_filename_expected( "testTimeIntegrationRK4Output.expected", expected_file );
   }

   pcu_check_fileEq( _name, expected_file );

   /* Destroy stuff */
   Stream_CloseAndFreeFile( stream );
   Memory_Free( array );
   Memory_Free( array2 );
   Stg_Class_Delete( variable );
   _Stg_Component_Delete( timeIntegrator );
   _Stg_Component_Delete( timeIntegrandList[0] );
   _Stg_Component_Delete( timeIntegrandList[1] );
   remove( _name );
}
void VariableSuite_TestVariable_Double( VariableSuiteData* data ) {
    typedef double Triple[3];

    double* array;
    Triple* structArray;
    Index length = 10;

    double testValues[] = { 123456789.0, 0.987654321 };
    Index testValueCount = 2;
    Index test_I;
    double testValue;

    Variable* var;
    Variable* vec;
    Variable* vecVar[3];

    int i, j;

    array = Memory_Alloc_Array( double, length, "test" );
    structArray = Memory_Alloc_Array( Triple, length, "test" );

    var = Variable_NewScalar( "Double-Scalar", NULL, Variable_DataType_Double, &length, NULL, (void**)&array, data->vr );
    vec = Variable_NewVector( "Double-Three", NULL, Variable_DataType_Double, 3, &length, NULL, (void**)&structArray, data->vr, "a", "b", "c" );

    vecVar[0] = Variable_Register_GetByName( data->vr, "a" );
    vecVar[1] = Variable_Register_GetByName( data->vr, "b" );
    vecVar[2] = Variable_Register_GetByName( data->vr, "c" );

    Variable_Register_BuildAll( data->vr );


    for ( test_I = 0; test_I < testValueCount; ++test_I ) {

        testValue = testValues[test_I];

        for ( i = 0; i < length; ++i ) {
            Variable_SetValueDouble( var, i, testValue );

            Variable_SetValueAtDouble( vec, i, 0, testValue );
            Variable_SetValueAtDouble( vec, i, 1, testValue );
            Variable_SetValueAtDouble( vec, i, 2, testValue );
        }

        /* "~~~Scalar~~~\n" */
        for ( i = 0; i < length; ++i ) {
            pcu_check_true( Variable_GetValueDouble( var, i ) == (double)(double)testValue );
            pcu_check_true( Variable_GetValueDoubleAsChar( var, i ) == (char)(double)testValue );
            pcu_check_true( Variable_GetValueDoubleAsShort( var, i ) == (short)(double)testValue );
            pcu_check_true( Variable_GetValueDoubleAsInt( var, i ) == (int)(double)testValue );
            pcu_check_true( Variable_GetValueDoubleAsFloat( var, i ) == (float)(double)testValue );
        }

        /*~~~Vector~~~*/
        for ( i = 0; i < length; ++i ) {
            pcu_check_true( Variable_GetValueAtDouble( vec, i, 0 ) == (double)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsChar( vec, i, 0 ) == (char)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsShort( vec, i, 0 ) == (short)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsInt( vec, i, 0 ) == (int)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsFloat( vec, i, 0 ) == (float)(double)testValue );
            pcu_check_true( Variable_GetPtrAtDouble( vec, i, 0 ) == &structArray[i][0] );

            pcu_check_true( Variable_GetValueAtDouble( vec, i, 1 ) == (double)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsChar( vec, i, 1 ) == (char)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsShort( vec, i, 1 ) == (short)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsInt( vec, i, 1 ) == (int)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsFloat( vec, i, 1 ) == (float)(double)testValue );
            pcu_check_true( Variable_GetPtrAtDouble( vec, i, 1 ) == &structArray[i][1] );

            pcu_check_true( Variable_GetValueAtDouble( vec, i, 2 ) == (double)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsChar( vec, i, 2 ) == (char)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsShort( vec, i, 2 ) == (short)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsInt( vec, i, 2 ) == (int)(double)testValue );
            pcu_check_true( Variable_GetValueAtDoubleAsFloat( vec, i, 2 ) == (float)(double)testValue );
            pcu_check_true( Variable_GetPtrAtDouble( vec, i, 2 ) == &structArray[i][2] );
        }

        /*~~~Vector: Sub-Variable~~~*/
        for ( i = 0; i < length; ++i ) {
            for ( j = 0; j < 3; ++j ) {
                pcu_check_true( Variable_GetStructPtr( vecVar[j], i ) == &structArray[i][j] );
            }
        }
    }
}