/*
* Cooldt cool-down time calculator for multi-layers pipe
*
* Copyright 2010, 2013, 2014, Benjamin DEGLO DE BESSES. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Benjamin DEGLO DE BESSES "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Benjamin DEGLO DE BESSES OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "cooldt.h"
/*
* Write Results to the *fp stream
*/
void WriteResults(FILE *fp, PipeDicretize *PZ, double *Tinitial, double *Tcurrent, TfluidSolid *Tot, double CDT)
{
/* Write the cool down time */
fprintf(fp,"#\tCool Down Time\t\t%f\t(h)\n", CDT/3600.0) ;
fprintf(fp,"#\n");
/* Write the header for T(t), T(r)... */
fprintf(fp, "# t\tt\tTemp.(t) at fluid\tCell\tLayer\tRadius\tDiameter\tInitial Temp.(r)\tFinal Temp.(r)\n") ;
fprintf(fp, "# (s)\t(h)\tsolid interface (C)\t(-)\t(-)\t(m)\t(m)\t(C)\t(C)\n") ;
/*
* Write calculation and manage the Tot and PZ arrays length differences
*/
int i = 0 ;
while ( (i <= Tot->n) || (i <= PZ->N) )
{
if (i <= Tot->n)
{
fprintf(fp,"%.1f\t%f\t%f\t", Tot->tn[i], Tot->tn[i] / 3600.0, Tot->T[i] );
}
else
fprintf(fp,"%.1f\t%f\t%f\t", Tot->tn[Tot->n], Tot->tn[Tot->n] / 3600.0, Tot->T[Tot->n] );
if(i <= PZ->N)
{
fprintf(fp,"%i\t", i );
fprintf(fp,"%i\t", PZ->L[i] );
fprintf(fp,"%f\t", PZ->rc[i] );
fprintf(fp,"%f\t", 2.0 * PZ->rc[i] );
fprintf(fp,"%f\t", Tinitial[i] );
fprintf(fp,"%f\n", Tcurrent[i] );
}
else
{
fprintf(fp,"%i\t", PZ->N );
fprintf(fp,"%i\t", PZ->L[PZ->N] );
fprintf(fp,"%f\t", PZ->rc[PZ->N] );
fprintf(fp,"%f\t", 2.0 * PZ->rc[PZ->N] );
fprintf(fp,"%f\t", Tinitial[PZ->N] );
fprintf(fp,"%f\n", Tcurrent[PZ->N] );
}
i++ ;
}
}
/*
* Calculate solution and return only CDT
*/
double SolutionSimpleOutput(PipeProblem *PP)
{
double CDT ;
PipeDicretize* PZ = (PipeDicretize*)malloc(sizeof(PipeDicretize)) ;
BuildModel(PP, PZ) ;
double *T = (double*)malloc((PZ->N + 1) * sizeof(double)) ;
CalcSteadyState(PP, PZ, T) ;
TfluidSolid* TfS = (TfluidSolid*)malloc(sizeof(TfluidSolid)) ;
CDT = CalcTransient(PP, PZ, T, TfS) ;
FreeTfluidSolid(TfS) ;
free(TfS);
free(T) ;
FreePipeDiscretize(PZ) ;
free(PZ);
return CDT ;
}
/*
* Calculate solution, and send the output to the *fp stream
*/
double SolutionFullOutput(FILE *fp, PipeProblem *PP)
{
int i ;
double CDT ;
PipeDicretize* PZ = (PipeDicretize*)malloc(sizeof(PipeDicretize)) ;
BuildModel(PP, PZ) ;
double *Tinitial = (double*)malloc((PZ->N + 1) * sizeof(double)) ;
CalcSteadyState(PP, PZ, Tinitial) ;
double *Tcurrent = (double*)malloc((PZ->N + 1) * sizeof(double)) ;
for( i = 0 ; i <= PZ->N ; i++)
Tcurrent[i] = Tinitial[i] ;
TfluidSolid* TfS = (TfluidSolid*)malloc(sizeof(TfluidSolid)) ;
CDT = CalcTransient(PP, PZ, Tcurrent, TfS) ;
WriteResults(fp, PZ, Tinitial, Tcurrent, TfS, CDT) ;
FreeTfluidSolid(TfS) ;
free(TfS) ;
FreePipeDiscretize(PZ) ;
free(PZ) ;
free(Tinitial) ;
free(Tcurrent) ;
return CDT ;
}
/* End of solution.c */