# Subroutine to calculate performance parameters for a liquid turbine
# Based on the LIQUID_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 = Liquid Turbine Performance
# macro subroutine = liquidTurbPerform
# macro report file = LiquidTurbPerform_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 = Flow Rate
#  type = Float
#  quantity type = volumetric flow
#  range = 0.1 [m^3/s], 100 [m^3/s]
#  default = 0.1 [m^3/s]
#
# macro parameter = Head Rise
#  type = Float
#  quantity type = length
#  range = 0 [m], 1000 [m]
#  default = 1 [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 liquidTurbPerform {
! my ($inletLoc, $outletLoc, $bladeRowLoc, $axis, $omega, $nMainBlades, $pref,
!     $rRadius, $rHeight, $qDesign, $hDesign, $dref, $gravity ) = @_;
!
! my ($rRadiusVal, $omegaVal, $rHeightVal, $qDesignValue, $hDesignValue, 
!     $gravityValue, $drefValue, $units);
!
# 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;
! }
!
#
# 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;
! }
!
! ($hDesignValue,$units) = evaluate($hDesign);
! if ($hDesignValue == 0) {
!   reportError( "'Head Rise' must not be zero." );
!   return;
! }
!
! ($qDesignValue,$units) = evaluate($qDesign);
! if ($qDesignValue == 0) {
!   reportError( "'Flow Rate' must not be 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);
!
#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)@$inletLoc - minVal(Radius)@$inletLoc 
# 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

  totalHead = ( pTotalOut - pTotalIn ) / ( $dref * $gravity )    # Total head

  disCoef = mFlowTotal / ($dref * Absolute Omega * (2.0 * rRadius)^3 ) * 1 [rad] # Discharge coefficient

  disCoefPrime = mFlowTotal / ($dref * sqrt($hDesign * 1[m]) * (2.0 * rRadius)^2 )  # Unit discharge

  rotSpeedPrime = Absolute Omega * 2.0 * rRadius / sqrt($hDesign * 1[m]) * 60.0 / (2 * pi )  # Unit rotational speed

  effPower  = ( ( pTotalIn - pTotalOut ) * abs( mFlowTotal ) / $dref -  tAllBlades * Absolute Omega / 1 [rad] ) / ( $dref * $gravity * $qDesign * $hDesign ) # Efficiency from power
   
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 $HEAD = getExprString(totalHead);
! my $QWD = getExprString(disCoef);
! my $Q1PRIME = getExprString(disCoefPrime);
! my $N1PRIME = getExprString(rotSpeedPrime);
! my $ETAW = getExprString(effPower);

#Create the output file
! open(OFH,">LiquidTurbPerform_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 Turbine Results</title>\n";
! print OFH "</head>\n";
! print OFH "<body>\n";
! print OFH "<b><i><font color=\"#000099\"><font size=+4>Liquid 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 liquid turbines.&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>Design Flow Rate</td><td>$qDesign</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Design Head Rise</td><td>$hDesign</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>Discharge Coefficient</td><td>$QWD</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Unit Discharge</td><td>$Q1PRIME</td>\n";
! print OFH "</tr>\n";
! print OFH "<tr>\n";
! print OFH "<td>Unit Rot. Speed</td><td>$N1PRIME</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)

!}

