# Subroutine to calculate performance parameters for a gas turbine
# based on the GAS_TURBINE performace macro in CFX-TASCflow.  The
# disclaimer from TASCflow:
#
# Some of the definitions used in these macros have
# alternate forms in the common literature.  Constant fluid specific
# heats are assumed.  Please check the definitions in the macros to
# be sure you get what you expect.

# Macro GUI begin
#
# macro name = Gas Turbine Performance
# macro subroutine = turbinePerform
# macro report file = TurbinePerform_report.html
#
# macro parameter = Inlet Region
#  type = Location
#  location type = Boundary
#
# macro parameter = Outlet Region
#  type = Location
#  location type = Boundary
#
# macro parameter = Rotor Blade(s)
#  type = Location
#  location type = Boundary
#
# macro parameter = Machine Axis
#  type = Combo
#  list = x, y, z
#  default = z
#
# macro parameter = Rot. Speed
#  type = Float
#  quantity type = angular velocity
#  range = 0 [rad/sec], 2000 [rad/sec]
#  default = 1000 [rad/sec]
#
# macro parameter = Num. Main Blades
#  type = Int
#  range = 1, 1000
#  default = 1
#
# macro parameter = Ref. Pressure
#  type = Float
#  quantity type = pressure
#  range = 0 [Pa], 200000 [Pa]
#  default = 100000 [Pa]
#
# macro parameter = Ref Radius
#  type = Float
#  quantity type = length
#  range = 0 [m], 0.1 [m]
#  default = 0.05 [m]
#
# macro parameter = Fluid Gamma
#  type = Float
#  range = 1, 2
#  default = 1.4

#
# Macro GUI end

! no warnings 'redefine';
! use strict 'vars';

! sub turbinePerform {
! my ($inletLoc, $outletLoc, $bladeRowLoc, $axis, $omega, $nMainBlades, 
!     $pref, $rRadius, $gamma) = @_;
!

# Subroutine to calculate performance parameters for a gas turbine
# Based on the GAS_TURBINE performace macros in CFX-TASCflow.  The
# disclaimer from TASCflow:
#
# Some of the definitions used in these macros have
# alternate forms in the common literature.  Constant fluid specific
# heats are assumed.  Please check the definitions in the macros to
# be sure you get what you expect.

# Error checkings
#
# First, make sure all essential dependent variables exist
#
!
! if( getValue("Total Pressure in Stn Frame", "User Level") eq "" ) {
!   reportError( "Variable 'Total Pressure in Stn Frame' does not exist." );
!   return;
! }
!
! if( getValue("Total Temperature in Stn Frame", "User Level") eq "" ) {
!   reportError( "Variable 'Total Temperature in Stn Frame' does not exist." );
!   return;
! }
!
! if( getValue("Total Enthalpy in Stn Frame", "User Level") eq "" ) {
!   reportError( "Variable 'Total Enthalpy in Stn Frame' does not exist." );
!   return;
! }
!
! if( getValue("Specific Heat Capacity at Constant Pressure", "User Level") eq "" ) {
!   reportError( "Variable 'Specific Heat Capacity at Constant Pressure' does not exist." );
!   return;
! }
!
#
# Input variable values should make sense
#
!
! if (!$inletLoc) {
!   reportError( "Inlet is not defined." );
!   return;
! }
!
! if (!$outletLoc) {
!   reportError( "Outlet is not defined." );
!   return;
! }
!
! if (!$bladeRowLoc) {
!   reportError( "Blade row is not defined." );
!   return;
! }
!
! if ($inletLoc eq $outletLoc) {
!   reportError( "Inlet and outlet regions must be different." );
!   return;
! }
!
! if ($inletLoc eq $bladeRowLoc) {
!   reportError( "Inlet and blade row regions must be different." );
!   return;
! }
!
! if ($outletLoc eq $bladeRowLoc) {
!   reportError( "Outlet and blade row regions must be different." );
!   return;
! }
!
! my ($rRadiusVal,$rUnits) = evaluate($rRadius);
! if ($rRadiusVal <= 0) {
!   reportError( "'Reference Radius' has to be greater than zero." );
!   return;
! }
!
! my ($omegaVal,$omegaUnits) = evaluate($omega);
! if ( !$omegaVal ) {
!   reportError( "'Angular Velocity' is not defined or is equal to zero." );
!   return;
! }
!
! if ($nMainBlades <= 0) {
!   reportError( "'Number of Main Blades' has to be greater than zero." );
!   return;
! }
!
! if ($gamma < 1) {
!   reportError( "'Fluid Gamma' has to be greater than or equal to 1." );
!   return;
! }
! 
! my ($bladeTorqueExpr, $isValidBlade) = getBladeTorqueExpr($bladeRowLoc,$axis);
! my $cpRefExpr = getExprOnLocators("massFlowAveAbs(Specific Heat Capacity at Constant Pressure)",
!                                                      $inletLoc);
! my $pInExp = getExprOnLocators("massFlowAveAbs(Pressure)", $inletLoc);
! my $pOutExpr = getExprOnLocators("massFlowAveAbs(Pressure)", $outletLoc);
! my $pTotalInExpr = getExprOnLocators("massFlowAveAbs(Total Pressure in Stn Frame)", 
!                                                         $inletLoc);
! my $pTotalOutExpr = getExprOnLocators("massFlowAveAbs(Total Pressure in Stn Frame)",
!                                                          $outletLoc);
! my $tInExpr = getExprOnLocators("massFlowAveAbs(Temperature)",$inletLoc);
!
! my $tOutExpr = getExprOnLocators("massFlowAveAbs(Temperature)",$outletLoc);
! my $tTotalInExpr = getExprOnLocators("massFlowAveAbs(Total Temperature in Stn Frame)",
!                                      $inletLoc);
! my $tTotalOutExpr = getExprOnLocators("massFlowAveAbs(Total Temperature in Stn Frame)",
!                                       $outletLoc);
! my $HInExpr = getExprOnLocators("massFlowAveAbs(Static Enthalpy)", $inletLoc);
! my $HOutExpr = getExprOnLocators("massFlowAveAbs(Static Enthalpy)", $outletLoc);
! my $HTInExpr = getExprOnLocators("massFlowAveAbs(Total Enthalpy in Stn Frame)", 
!                                  $inletLoc);
! my $HTOutExpr = getExprOnLocators("massFlowAveAbs(Total Enthalpy in Stn Frame)", 
!                                   $outletLoc);
! my $mFlowExpr = getExprOnLocators("massFlow()", $inletLoc);
!
! my $effTTExpr;
! my $effpTTExpr;
! if ( objectExists('Isentropic Expansion Efficiency') ) {
!   $effTTExpr = getExprOnLocators("massFlowAve(Isentropic Expansion Efficiency)", 
!	                                  $outletLoc);
! }
! if ( objectExists('Polytropic Expansion Efficiency') ) {
!   $effpTTExpr = getExprOnLocators("massFlowAve(Polytropic Expansion Efficiency)", 
!	                                  $outletLoc);
! }

#Define the expressions of interest
LIBRARY:
CEL:
EXPRESSIONS:
  Absolute Omega = abs($omega)
  rRadius = $rRadius
  nMainBlades = $nMainBlades
  gamma = $gamma # Cp/Cv
  vTip = rRadius * Absolute Omega / 1 [rad]
  cpRef = $cpRefExpr
  Rgas = cpRef*(gamma-1)/gamma # Cp-Cv
  tBladeRow = $bladeTorqueExpr
  tAllBlades = tBladeRow * nMainBlades
  Power = tAllBlades * Absolute Omega / 1 [rad]

  pIn = $pref + $pInExp
  pOut = $pref + $pOutExpr
  pTotalIn = $pref + $pTotalInExpr
  pTotalOut = $pref + $pTotalOutExpr
  tIn = $tInExpr
  tOut = $tOutExpr
  tTotalIn = $tTotalInExpr
  tTotalOut = $tTotalOutExpr
  HIn = $HInExpr
  HTIn = $HTInExpr
  HOut = $HOutExpr
  HTOut = $HTOutExpr

  HSOut = HIn+cpRef*tIn*((pOut/pIn)^((gamma-1)/gamma)-1) # Isentropic enthalpy at the exit
  HTSOut = HTIn+cpRef*tTotalIn*((pTotalOut/pTotalIn)^((gamma-1)/gamma)-1) # Isentropic total enthalpy at the exit
  mFlow = abs($mFlowExpr)
  mFlowTotal = mFlow * nMainBlades
  aIn = sqrt(gamma*Rgas*tTotalIn)
  mFlowNonDim = mFlowTotal*sqrt(Rgas*tTotalIn/gamma)/(pTotalIn*pi*rRadius^2) # non-dimensional mass flow (theta)
  mU = vTip/aIn               # Dimensionless impeller tip speed
  isenVelRatio = (gamma-1)/2 * mU^2/(1-(pTotalOut/pTotalIn)^((gamma-1)/gamma)) #ivr  
  Pssi= 1.0/((gamma-1)*mU^2)*(1-(tTotalOut/tTotalIn)) # Total 'pressure' coefficient
  Wnd= Pssi*mFlowNonDim*mU^2 # Power coefficient
  Tc = Pssi*mFlowNonDim*mU  # torque coefficient
! if ( defined($effTTExpr) ) {
  effTT = $effTTExpr
! } else {
  effTT = (1-tTotalOut/tTotalIn)/(1-(pTotalOut/pTotalIn)^((gamma-1)/gamma)) # total to total isentropic efficiency
!}
  effTS = (1-tTotalOut/tTotalIn)/(1-(pOut/pTotalIn)^((gamma-1)/gamma)) # total to static isentropic efficiency
! if ( defined($effpTTExpr) ) {
  effPTT = $effpTTExpr
! } else {
  effPTT = log10(tTotalOut/tTotalIn)/(((gamma-1)/gamma)*log10(pTotalOut/pTotalIn)) # total to total polytropic efficiency
!}
  effPTS = log10(tTotalOut/tTotalIn)/(((gamma-1)/gamma)*log10(pOut/pTotalIn)) # total to static polytropic efficiency
END
END
END

# Evaluate the expressions to perl variables for output.

! my $TIN = getExprString(tIn); 
! my $TOUT = getExprString(tOut); 
! my $TRAT = getExprVal(tOut)/getExprVal(tIn);
! my $PIN = getExprString(pIn); 
! my $POUT =getExprString(pOut); 
! my $PRAT = getExprVal(pOut)/getExprVal(pIn);
! my $TTIN = getExprString(tTotalIn); 
! my $TTOUT = getExprString(tTotalOut); 
! my $TTRAT = getExprVal(tTotalOut)/getExprVal(tTotalIn);
! my $PTIN = getExprString(pTotalIn); 
! my $PTOUT =getExprString(pTotalOut); 
! my $PTRAT = getExprVal(pTotalOut)/getExprVal(pTotalIn);
! my $HIN = getExprString(HIn); 
! my $HOUT = getExprString(HOut); 
! my $HRAT = getExprVal(HOut)/getExprVal(HIn);
! my $HTIN = getExprString(HTIn); 
! my $HTOUT = getExprString(HTOut); 
! my $HTRAT = getExprVal(HTOut)/getExprVal(HTIn);
! my $T1 = getExprString(tBladeRow);
! my $TALL = getExprString(tAllBlades);
! my $POWER = getExprString(Power);
! my $THETTA =getExprString(mFlowNonDim);
! my $MU = getExprString(mU);
! my $IVR = getExprString(isenVelRatio);
! my $PSSI = getExprString(Pssi);
! my $WND = getExprString(Wnd);
! my $TC = getExprString(Tc);
! my $ETATT = getExprString(effTT);
! my $ETATS = getExprString(effTS);
! my $ETAPTT = getExprString(effPTT);
! my $ETAPTS = getExprString(effPTS);

#Create the output file
! open(OFH,">TurbinePerform_report.html");
! print OFH "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n";
! print OFH "<html>\n";
! print OFH "<head>\n";
! print OFH "   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
! print OFH "   <meta name=\"Author\" content=\"CFD-Post\">\n";
! print OFH "   <meta name=\"GENERATOR\" content=\"CFD-Post\">\n";
! print OFH "   <title>Gas Turbine Results</title>\n";
! print OFH "</head>\n";
! print OFH "<body>\n";
! print OFH "<b><i><font color=\"#000099\"><font size=+4>Gas Turbine Performance Report</font></font></b></i>\n";
! print OFH "<hr NOSHADE WIDTH=\"100%\">\n";
! print OFH "<p><b>DISCLAIMER</b>:&nbsp; This report has been supplied\n";
! print OFH "to assist in the performance estimation of gas turbines.";
! print OFH "&nbsp; The equations\n";
! print OFH "were obtained from available literature, however some equations may have\n";
! print OFH "alternate definitions. It is YOUR RESPONSIBILITY to verify the accuracy\n";
! print OFH "of these definitions.\n";
! my $fileName = getValue("DATA READER","Current Results File");
! my $date = scalar(localtime);
! print OFH "<p>Generated by CFD-Post from results file <i>$fileName</i> at <i>$date</i>\n";
! print OFH "<p><b><font size=+2>User Input</font></b>\n";
! print OFH "<table BORDER CELLPADDING=2 >\n";
! print OFH "<tr>\n";
! print OFH "<td>Inlet Region</td><td>$inletLoc</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Outlet Region</td><td>$outletLoc</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Blade Row Region</td><td>$bladeRowLoc</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Reference Radius</td><td>$rRadius</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Number of Main Blades</td><td>$nMainBlades</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Machine Axis</td><td>$axis</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Rotation Speed</td><td>$omega</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Gamma</td><td>$gamma</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Reference Pressure</td><td>$pref</td>\n";
! print OFH "</tr>\n";
! print OFH "</table>\n";
! print OFH "<p><b><font size=+2>Mass Averages</font></b>\n";
! print OFH "<br><b><font size=+2></font></b>&nbsp;\n";
! print OFH "<table BORDER CELLPADDING=2 >\n";
! print OFH "<tr>\n";
! print OFH "<td>Quantity</td><td>Inlet</td><td>Outlet</td><td>Ratio (Out/In)</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Temperature</td><td>$TIN</td><td>$TOUT</td><td>$TRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Temperature</td><td>$TTIN</td><td>$TTOUT</td><td>$TTRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Pressure</td><td>$PIN</td><td>$POUT</td><td>$PRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Pressure</td><td>$PTIN</td><td>$PTOUT</td><td>$PTRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Enthalpy</td><td>$HIN</td><td>$HOUT</td><td>$HRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Enthalpy</td><td>$HTIN</td><td>$HTOUT</td><td>$HTRAT</td>\n";
! print OFH "</tr>\n";
! print OFH "</table>\n";
! print OFH "<b><font size=+2></font></b>\n";
! print OFH "<p><b><font size=+2>Results</font></b>\n";
! my ($iTag1, $iTag2) = ("","");
! if ( !$isValidBlade ) {
!   print OFH "<p><i><b>WARNING</b>:&nbsp;";
!   print OFH "Blade definition includes several regions. Note that some calculations\n";
!   print OFH "will not be correct if there is any overlap between the blade regions\n";
!   print OFH "(marked in italic font).</i><p>\n";
!   $iTag1 = "<i>";
!   $iTag2 = "</i>";
! }
! print OFH "<table BORDER CELLPADDING=2 >\n";
! print OFH "<tr>\n";
! print OFH "<td>Torque (one blade row)</td><td>$iTag1$T1$iTag2</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Torque (all blades)</td><td>$iTag1$TALL$iTag2</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Power (all blades)</td><td>$iTag1$POWER$iTag2</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Dimensionless mass flow (theta)</td><td>$THETTA</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Dimensionless impeller tip speed (M<sub>u</sub>)</td><td>$MU</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Isentropic velocity ratio (ivr)</td><td>$IVR</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Power Coefficient (wnd)</td><td>$WND</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Torque Coefficient (tc)</td><td>$TC</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total-to-total isen. efficiency</td><td>$ETATT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total-to-static isen. efficiency</td><td>$ETATS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total-to-total poly. efficiency</td><td>$ETAPTT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total-to-static poly. efficiency</td><td>$ETAPTS</td></tr>\n";
! print OFH "</table>\n";
! print OFH "<br>&nbsp;\n";
! print OFH "<br>&nbsp;\n";
! print OFH "</body>\n";
! print OFH "</html>\n";
! close(OFH)

!}

