# Subroutine to calculate performance parameters for a liquid pump
# Based on the LIQUID_PUMP 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 = Liquid Pump Performance
# macro subroutine = pumpPerform
# macro report file = PumpPerform_report.html
#
# macro parameter = Inlet
#  type = Location
#  location type = Boundary
#
# macro parameter = Outlet
#  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 = Ref Height
#  type = Float
#  quantity type = length
#  range = 0.0001 [m], 0.1 [m]
#  default = 0.05 [m]
#
# macro parameter = Ref. Density
#  type = Float
#  quantity type = density
#  range = 0 [kg/m^3], 10000 [kg/m^3]
#  default = 1000 [kg/m^3]
#
# macro parameter = Gravity Accel.
#  type = Float
#  quantity type = acceleration
#  range = 1 [m/s^2], 10 [m/s^2]
#  default = 9.81 [m/s^2]

#
# Macro GUI end

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

! sub pumpPerform {
! my ($inletLoc, $outletLoc, $bladeRowLoc, $axis, $omega, 
!     $nMainBlades, $pref, $rRadius, $rHeight, $dref, $gravity) = @_;
!
! my ($rRadiusVal, $omegaVal, $rHeightVal, $gravityValue, $drefValue, $units);

# Error checkings
#
# First, make sure all essential dependent variables exist
#
!
! if ( !objectExists('/SCALAR VARIABLE:Total Pressure in Stn Frame') ) {
!   reportError( "Variable 'Total Pressure in Stn Frame' does not exist." );
!   return;
! }
!
! if ( !objectExists('/VECTOR VARIABLE:Theta Direction') ) {
!   reportError( "Turbo variables do not exist.\nPlease initialize turbo mode first." );
!   return;
! }
!
! if ( !objectExists('/SCALAR VARIABLE:Radius') ) {
!   reportError( "Variable 'Radius' does not exist.\nPlease initialize turbo mode first." );
!   return;
! }
!
! if ( !objectExists('/VECTOR VARIABLE:Velocity in Stn Frame') ) {
!   reportError( "Variable 'Velocity in Stn Frame' 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;
! }
!
! ($rRadiusVal,$units) = evaluate($rRadius);
! if ($rRadiusVal <= 0) {
!   reportError( "'Reference Radius' has to be greater than zero." );
!   return;
! }
!
! ($omegaVal,$units) = evaluate($omega);
! if ( !$omegaVal ) {
!   reportError( "'Angular Velocity' is not defined or is equal to zero." );
!   return;
! }
!
! ($rHeightVal,$units) = evaluate($rHeight);
! if ($rHeightVal <= 0) {
!   reportError( "'Reference Height' has to be greater than zero." );
!   return;
! }
!
! if ($nMainBlades <= 0) {
!   reportError( "'Number of Main Blades' has to be greater than zero." );
!   return;
! }
!
!
! ($drefValue,$units) = evaluate($dref);
! if ($drefValue <= 0) {
!   reportError( "'Ref. Density' has to be greater than zero." );
!   return;
! }
!
! ($gravityValue,$units) = evaluate($gravity);
! if ($gravityValue <= 0) {
!   reportError( "'Gravity Acceleration' has to be greater than zero." );
!   return;
! }
!
! 
! my ($bladeTorqueExpr, $isValidBlade) = getBladeTorqueExpr($bladeRowLoc,$axis);
! 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 $mFlowExpr = getExprOnLocators("massFlow()", $inletLoc);
! my $c1Expr = getExprOnLocators("areaAve(Velocity in Stn Frame)",$inletLoc);
! my $c2Expr = getExprOnLocators("areaAve(Velocity in Stn Frame)",$outletLoc);
! my $uc1Expr = getExprOnLocators("massFlowAveAbs(Radius * omega * VelTheta)",$inletLoc);
! my $uc2Expr = getExprOnLocators("massFlowAveAbs(Radius * omega * VelTheta)",$outletLoc);

#Define the expressions of interest
LIBRARY:
CEL:
EXPRESSIONS:
  Absolute Omega = abs($omega)
  rRadius = $rRadius
  rHeight = $rHeight
  vTip = rRadius * Absolute Omega / 1 [rad]
# height = maxVal(Radius)@$outletLoc - minVal(Radius)@$outletLoc 
# aTip = 2.0 * pi * rRadius * height
  aTip = 2.0 * pi * rRadius * rHeight
  nMainBlades = $nMainBlades

  tBladeRow = $bladeTorqueExpr
  tAllBlades = tBladeRow * nMainBlades
  Power = tAllBlades * Absolute Omega / 1 [rad]

  pIn = $pref + $pInExp
  pOut = $pref + $pOutExpr
  pTotalIn = $pref + $pTotalInExpr
  pTotalOut = $pref + $pTotalOutExpr

  mFlow = abs($mFlowExpr)
  mFlowTotal = mFlow * nMainBlades

  qRef = 0.5 * $dref * vTip^2                          # Stagnation 'pressure'
  phiD = mFlowTotal / ( $dref * aTip * vTip )          # Discharge coefficient
  presCoef = ( pOut - pIn ) / qRef                     # Static 'pressure' coefficient
  presTotalCoef = ( pTotalOut - pTotalIn ) / qRef      # Total 'pressure' coefficient


  VelTheta = abs( Velocity in Stn Frame u * Theta Direction X + Velocity in Stn Frame v * Theta Direction Y  + Velocity in Stn Frame w * Theta Direction Z ) 
  uc1 = ($uc1Expr) / 1 [rad]
  uc2 = ($uc2Expr) / 1 [rad]
  perfCoef = 2 * ( uc2 - uc1 ) / vTip^2                 # Performance coefficient

  c1 = $c1Expr
  c2 = $c2Expr
  deltaHS  = ( uc2  - c2^2 / 2.0 ) - ( uc1  - c1^2 / 2.0 )  # Euler Static enthalpy difference
  deltaHT  = uc2 - uc1                                      # Euler Total enthalpy difference
  deltaHSS = ( pOut - pIn ) / $dref                         # Static enthalpy difference
  deltaHTS = ( pTotalOut - pTotalIn ) / $dref               # Total enthalpy difference

  effStatic = deltaHSS / deltaHS                      # Static isentropic efficiency
  effTotal  = deltaHTS / deltaHT                      # Total isentropic efficiency
  effGlobal = deltaHSS / deltaHT                      # Global isentropic efficiency

  effPower  = abs( mFlowTotal * ( pTotalOut - pTotalIn ) / ( $dref * tAllBlades * Absolute Omega ) ) * 1[rad] # Efficiency from power
  totalHead = deltaHTS / $gravity                     # Total head
END
END
END

# Evaluate the expressions to perl variables for output.

! my $PIN = getExprString(pIn); 
! my $POUT =getExprString(pOut); 
! my $PRAT = getExprVal(pOut)/getExprVal(pIn);
! my $PTIN = getExprString(pTotalIn); 
! my $PTOUT =getExprString(pTotalOut); 
! my $PTRAT = getExprVal(pTotalOut)/getExprVal(pTotalIn);
! my $T1 = getExprString(tBladeRow);
! my $TALL = getExprString(tAllBlades);
! my $POWER = getExprString(Power);
! my $QREF = getExprString(qRef);
! my $PHID = getExprString(phiD);
! my $PSIPU = getExprString(presCoef);
! my $PSSI = getExprString(presTotalCoef);
! my $PSIU = getExprString(perfCoef);
! my $DELTAHS = getExprString(deltaHS);
! my $DELTAHT = getExprString(deltaHT);
! my $DELTAHSS = getExprString(deltaHSS);
! my $DELTAHTS = getExprString(deltaHTS);
! my ($value,$unit) = evaluate(deltaHS);
! my ($ETAS, $ETATS, $ETAG);
! if ($value) {
!   $ETAS = getExprString(effStatic);
! }
! ($value,$unit) = evaluate(deltaHT);
! if ($value) {
!   $ETATS = getExprString(effTotal);
!   $ETAG = getExprString(effGlobal);
! }
! my $ETAW = getExprString(effPower);
! my $HEAD = getExprString(totalHead);

#Create the output file
! open(OFH,">PumpPerform_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>Liquid Pump Results</title>\n";
! print OFH "</head>\n";
! print OFH "<body>\n";
! print OFH "<b><i><font color=\"#000099\"><font size=+4>Liquid Pump 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 liquid pumps.&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>Reference Height</td><td>$rHeight</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>Reference Density</td><td>$dref</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 "<tr>\n";
! print OFH "<td>Gravity Acceleration</td><td>$gravity</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>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 "</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>Stagnation Pressure (Q_ref)</td><td>$QREF</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Discharge Coefficient (PHI_d)</td><td>$PHID</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Static 'Pressure' Coefficient</td><td>$PSIPU</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total 'Pressure' Coefficient</td><td>$PSSI</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Performance Coefficient</td><td>$PSIU</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Euler Static Enthalpy Loss (delta HS)</td><td>$DELTAHS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Euler Total Enthalpy Loss (delta HT)</td><td>$DELTAHT</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Static Enthalpy Loss (delta HSS)</td><td>$DELTAHSS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Enthalpy Loss (delta HTS)</td><td>$DELTAHTS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Static Isentropic Efficiency</td><td>$ETAS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Isentropic Efficiency</td><td>$ETATS</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Global Isentropic Efficiency</td><td>$ETAG</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Efficiency From Power</td><td>$iTag1$ETAW$iTag2</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Total Head </td><td>$HEAD</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)

!}
