#### load correct value of Current Time Step
! my $exefile = $ENV{'CFX5BINDIR'} . "/cfx5mondata.exe";
! my $resfile = getValue("DATA READER", "Current Results File");
! my $command = "\"$exefile\" -res \"$resfile\" -lastvaluesonly -varlist \"CURRENT TIMESTEP\"";
! my $output = `$command`;
! my @temp = split(/\n/, $output);
! my $timestep = 0 + $temp[1] ;

LIBRARY:
  CEL:
    EXPRESSIONS:
      Current Time Step = $timestep
    END
  END
END

#### Centrifugal Compressor Report Template

COMMAND FILE:
  CFX Post Version = 12.0
END


#### Report Description

REPORT:
  Excluded Report Items = /COMMENT:User Data
  REPORT PROPERTIES:
    Report Template Name = Centrifugal Compressor Blade Row Report (Beta)
    Keywords = turbo, centrifugal, radial, rotor
    Report Template Description = A report for a centrifugal compressor blade row analysis
    Report Support Level = Beta
 END
  PUBLISH:
    Default Report Name = Centrifugal Compressor Blade Row Report (Beta)
    Report Path = ./CentrifugalCompressorBladeRowReport.htm
  END
  FILE INFORMATION OPTIONS: 
    Include In Report = false
  END
  MESH STATISTICS OPTIONS: 
    Include In Report = false
  END
  PHYSICS SUMMARY OPTIONS: 
    Include In Report = false
  END
END
TITLE PAGE:
  Report Title = Centrifugal Compressor Blade Row Report (Beta)
END

! use PlotToolsExt;
! use TurboSystemExt;
! use TurboStageExt;
! use BladeLoadingChartExt;
! use StreamwiseChartExt;
! use SpanwiseChartExt;
! use strict;
! no warnings qw(redefine);

#### runCCL - Needed to run CCL from the perl modules
! sub runCCL
! {
!   my ($ccl, $echoOnly) = @_;
!   my @list = split('\n', $ccl);
!   my $line;
!   foreach $line (@list) {
#!     print $line,"\n";
!     if (!$echoOnly) {
$line
!     }
!   }
! }

#### Turbo Initialization

# Determine if solution contains TBR data (Fourier Storage)
! if ( objectExists('Velocity.Trnavg') ) {
!   my $error = 'Transient Blade Row data exists.  This report is not suitable for transient data.';
!   if (defined(&CUEPost::reportError)) { CUEPost::reportError($error) }
!   else { print $error, "\n" }
! }

# Run the Post turbo initialization
> turbo init
> turbo more_vars

# Set the default span locations for plots and charts
! my @spanLoc = (0.1, 0.5, 0.9);

# Select the active case to post-process
! my $case_name = getValue('DATA READER', 'Active Case Name');

# Create and initialize the TurboSystem object
! my $ts = new TurboSystemExt($case_name);
! $ts->InitializeFromPostTurboInfo();
! $ts->CreateLocators();
! $ts->SortComponents();

# Assume compressible flow if not already set by TurboSystem property
! $ts->FlowType('Compressible');

#! $ts->Dump();

# Determine the stages for a compressor, where each stage begins with a rotor
# This algorithm is very simplistic and considers IGV's, EGV's and volutes as part of the stage
! sub DetermineCompressorStages {
!   my ($system) = @_;
!   my @stageList = ();
#   Label the stages as 'Stage 1', 'Stage 2', etc.
!   my $label = 'Stage ';
!   my $i = 1;
!   my $stage = new TurboStageExt( 'Stage ' . $i );
!   foreach my $comp (@{$system->Components()}) {
!     if ( $comp->Type() eq 'Rotor' && $stage->NumComponents() > 0) {
!       push(@stageList, $stage);
!       $i++;
!       $stage = new TurboStageExt( 'Stage ' . $i );
!     }
!     $stage->AddComponent($comp);
!   }
!   push(@stageList, $stage);
!   return @stageList;
! }
! my @stages = DetermineCompressorStages($ts);


#### Define the default expressions for all machine types
# The second (optional) argument (+/-1) specifies the sign convention for the
# tangential direction.  If left unspecified, it will be computed based on the
# sign of omega for the first rotor.  If no rotor exists, it will be 1.

! PlotToolsExt::CreateCommonCEL($ts);

COMMENT:Introduction
  Comment Heading = Introduction
  Heading Level = 1
  Comment Text =   <p>This report summarizes the results of a CFD analysis performed for the centrifugal compressor geometry shown in Figure 1.  In the following sections both quantitative and qualitative results are presented in the form of tables, charts and plots.</p>
END


#### Performance Results

COMMENT:Performance Results
  Comment Heading = Performance Results
  Heading Level = 1
  Comment Text =   <p>The quantitative results are summarized in the following table.  This shows the overall performance.</p>
END

# Define the expressions specific to axial compressors
!sub CreateStageTable {
!  my ($stage) = @_;
!  my $name = $stage->Name();
# Find the rotor, if it exists
!  my $rotor = $stage->Rotor();
!  my @list = ();
# Create the expresssions
LIBRARY:
  CEL:
    EXPRESSIONS:
!    my $rotorName = $rotor->Name();
!    my $rotorOut = $rotor->OutletName();
!    my $lastName = $stage->LastComponent()->Name();
!    my $lastOut = $stage->LastComponent()->OutletName();
!    push(@list, 'Rotational Speed', $rotorName . ' omega', '%7.4f');
!    push(@list, 'Tip Diameter', $rotorName . ' D te', '%7.4f');
!    push(@list, 'Tip Speed', $rotorName . ' U te', '%7.4f');
!    push(@list, 'Mass Flow Rate', $rotorName . ' mfr', '%7.4f');
!    if ( exprExists($rotorName . ' mfr') && exprExists($rotorName . ' Ht in') ) {
       $name Power = $rotorName mfr * ($lastName Ht out - $rotorName Ht in)
!      push(@list, 'Power', $name . ' Power', '%7.4f');
!    }
!    if ( exprExists($rotorName . ' Q0 in') ) {
       $name Inlet Flow Coefficient = $rotorName Q0 in / ($rotorName U te * $rotorName D te^2)
!      push(@list, 'Inlet Flow Coefficient', $name . ' Inlet Flow Coefficient', '%7.4f');
!    }
!    if ( exprExists($rotorName . 'U te') && exprExists($rotorName . ' Ht in') ) {
       $name Work Input Coefficient = ($rotorName Ht out - $rotorName Ht in) / $rotorName U te^2
!      push(@list, 'Work Input Coefficient', $name . ' Work Input Coefficient', '%7.4f');
!    }
!    if ( exprExists($lastName . ' Pt out') && exprExists($rotorName . ' Pt in')  ) {
       $name Total Pressure Ratio = ($lastName Pt out + Reference Pressure)/($rotorName Pt in + Reference Pressure)
!      push(@list, 'Total Pressure Ratio', $name . ' Total Pressure Ratio', '%7.4f');
!    }
!    if ( exprExists($lastName . ' Tt out') && exprExists($rotorName . ' Tt in') ) {
       $name Total Temperature Ratio = $lastName Tt out / $rotorName Tt in
!      push(@list, 'Total Temperature Ratio', $name . ' Total Temperature Ratio', '%7.4f');
!    }
!    if ( objectExists('Isentropic Compression Efficiency') ) {
       Effs out = 100 * massFlowAve(Isentropic Compression Efficiency)@$lastOut
!      push(@list, 'Total-to-Total Isentropic Efficiency %', 'Effs out', '%7.4f');
!    }
!    if ( objectExists('Polytropic Compression Efficiency') ) {
       Effp out = 100 * massFlowAve(Polytropic Compression Efficiency)@$lastOut
!      push(@list, 'Total-to-Total Polytropic Efficiency %', 'Effp out', '%7.4f');
       $name Polytropic Head = ($lastName Ht out - $rotorName Ht in) * ( Effp out / 100 )
!      push(@list, 'Polytropic Head', $name . ' Polytropic Head', '%7.4f');
       $name Polytropic Head Coefficient = $name Polytropic Head / $rotorName U te^2
!      push(@list, 'Polytropic Head Coefficient', $name . ' Polytropic Head Coefficient', '%7.4f');
!    }
    END
  END
END
!  PlotToolsExt::CreateResultsTable("$name Performance Results", "$name Performance Results", @list);
!}
# Create the performance results table
! foreach my $stage (@stages) { CreateStageTable($stage) }


#### Detailed Summary

COMMENT:Detailed Summary
  Comment Heading = Detailed Summary
  Heading Level = 1
  Comment Text =   <p>The table below gives a summary of the mass or area averaged solution variables and derived quantities computed at the inlet, leading edge (LE Cut), trailing edge (TE Cut) and outlet locations.  The flow angles are measured with respect to the axial direction.</p>
END

# Create a data table for each component
! foreach my $comp (@{$ts->Components()}) { PlotToolsExt::CreateReducedComponentDataTable($comp) }


#### Meanline 1D Charts

COMMENT:Meanline 1D Charts
  Comment Heading = Meanline 1-D Charts
  Heading Level = 1
  Comment Text =   <p>The following charts show streamwise mass or area averaged quantities from the inlet to the outlet of the machine.</p>
END

! if ( objectExists('Mach Number') ) {
!   my $streamchart1 = new StreamwiseChartExt( 'Streamwise Mach Number Chart' );
!   if ( $streamchart1 ) {
!     $streamchart1->Caption( 'Chart showing streamwise, area averaged relative Mach number versus averaged normalized M.' );
!     $streamchart1->AddLine( 'Mach Number', 'area', 5, 'Mach' );
!     $streamchart1->Plot();
!   }
! }
! if ( objectExists('Static Entropy') ) {
!   my $streamchart2 = new StreamwiseChartExt( 'Streamwise Entropy Chart' );
!   if ( $streamchart2 ) {
!     $streamchart2->Caption( 'Chart showing streamwise, mass averaged static entropy versus averaged normalized M.' );
!     $streamchart2->AddLine( 'Static Entropy', 'mass', 5, 's' );
!     $streamchart2->Plot();
!   }
! }


#### Blade Loading Charts

COMMENT:Blade Loading Charts
  Comment Heading = Blade Loading Charts
  Heading Level = 1
  Comment Text =   <p>The following chart show the blade loading.</p>
END

! foreach my $comp (@{$ts->Components()}) {
!   if ( $comp->HasBlade() ) {
!     my $name = $comp->Name();
!     my $chart = new BladeLoadingChartExt( $name . ' Blade Loading Chart', 'Pressure', 'static pressure' );
!     if ( $chart ) {
!       $chart->Caption( $name . ' blade loading chart' );
!       foreach my $loc (@spanLoc) {
!         $chart->AddLine( $comp, $loc );
!       }
!       $chart->Plot();
!     }
!   }
! }


#### Spanwise Charts

COMMENT:Spanwise Charts
  Comment Heading = Spanwise Charts
  Heading Level = 1
  Comment Text =   <p>The following charts show circumferentially averaged quantities along hub-to-shroud lines located at the leading and trailing edges of the blade.</p>
END

!sub spancharts {
!   my $comp = shift;
!
!   if ( $comp->HasBlade() ) {
!     my $name = $comp->Name();
!     my $leLoc = $comp->LEStreamLoc();
!     my $teLoc = $comp->TEStreamLoc();
!     if ( objectExists("$name Flow Angle Alpha") && objectExists("$name Flow Angle Beta") ) {
!       my $chart = new SpanwiseChartExt( $name . ' Spanwise Flow Angles at LE Chart' );
!       if ( $chart ) {
!         $chart->Caption( $name . ' chart showing circumferentially averaged flow angles at the LE' );
!         if ( $comp->Type() eq 'Rotor' ) {
!           $chart->AddLine( "$name Flow Angle Alpha", $leLoc, 'area', 20, 'Alpha' );
!           $chart->AddLine( "$name Flow Angle Beta", $leLoc, 'area', 20, 'Beta' );
!         }
!         else {
!           $chart->AddLine( "$name Flow Angle Beta", $leLoc, 'area', 20, 'Beta' );
!         }
!         $chart->Plot();
!       }
!     }
!     if ( objectExists('Mach Number') ) {
!       my $chart = new SpanwiseChartExt( $name . ' Spanwise Relative Mach Number at LE Chart' );
!       if ( $chart ) {
!         $chart->Caption( $name . ' chart showing circumferentially averaged relative Mach number at the LE' );
!         $chart->AddLine( 'Mach Number', $leLoc, 'area', 20, 'M rel' );
!         $chart->Plot();
!       }
!     }
!     if ( objectExists("$name Flow Angle Alpha") && objectExists("$name Flow Angle Beta") ) {
!       my $chart = new SpanwiseChartExt( $name . ' Spanwise Flow Angles at TE Chart' );
!       if ( $chart ) {
!         $chart->Caption( $name . ' chart showing circumferentially averaged flow angles at the TE' );
!         if ( $comp->Type() eq 'Rotor' ) {
!           $chart->AddLine( "$name Flow Angle Alpha", $teLoc, 'area', 20, 'Alpha' );
!           $chart->AddLine( "$name Flow Angle Beta", $teLoc, 'area', 20, 'Beta' );
!         }
!         else {
!           $chart->AddLine( "$name Flow Angle Beta", $teLoc, 'area', 20, 'Beta' );
!         }
!         $chart->Plot();
!       }
!     }
!     if ( objectExists('Mach Number') ) {
!       my $chart = new SpanwiseChartExt( $name . ' Spanwise Mach Number at TE Chart' );
!       if ( $chart ) {
!         if ( $comp->Type() eq 'Rotor' ) {
!           $chart->Caption( $name . ' chart showing circumferentially averaged relative and absolute Mach number at the TE' );
!           $chart->AddLine( 'Mach Number', $teLoc, 'area', 20, 'M rel' );
!           $chart->AddLine( 'Mach Number in Stn Frame', $teLoc, 'area', 20, 'M abs' );
!         }
!         else {
!           $chart->Caption( $name . ' chart showing circumferentially averaged relative Mach number at the TE' );
!           $chart->AddLine( 'Mach Number', $teLoc, 'area', 20, 'M rel' );
!         }
!         $chart->Plot();
!       }
!     }
!     if ( objectExists('Mach Number') ) {
!       my $chart = new SpanwiseChartExt( $name . ' Spanwise Meridional Velocity at TE Chart' );
!       if ( $chart ) {
!         $chart->Caption( $name . ' chart showing circumferentially averaged meridional velocity at the TE' );
!         $chart->AddLine( 'Velocity Meridional', $teLoc, 'area', 20, 'Cm' );
!         $chart->Plot();
!       }
!     }
!   }
! }
# Create spanwise charts for each component
! foreach my $comp (@{$ts->Components()}) { spancharts($comp) }
# Set initial view to first stage geometry view
! my $firstStage = $stages[0]->Name();
>centerview 1


#### Meridional Contours

COMMENT:Meridional Contours
  Comment Heading = Meridional Contours
  Heading Level = 1
  Comment Text =   <p>The following plots show circumferentially averaged meridional views.</p>
END

! foreach my $stage (@stages) {
!   my $label = $stage->Name();
#   Meridional Contour views
!   PlotToolsExt::CreateMeridionalContourView( $stage->Name(), $stage->Components(), 'Pressure', 'static pressure' );
!   if ( objectExists('Mach Number') ) {
!     PlotToolsExt::CreateMeridionalContourView( $stage->Name(), $stage->Components(), 'Mach Number', 'relative Mach number' );
!   }
!   PlotToolsExt::CreateMeridionalContourView( $stage->Name(), $stage->Components(), 'Velocity Meridional', 'meridional velocity' );
!}


#### Blade to Blade Contours

COMMENT:Blade to Blade Contours
  Comment Heading = Blade to Blade Contours
  Heading Level = 1
  Comment Text =   <p>The following plots show blade-to-blade contour and vector views.</p>
END

! foreach my $stage (@stages) {
!   my $label = $stage->Name();
#   Blade-to-blade views for each span location in @spanLoc
!   if ( objectExists('Mach Number') ) {
!     foreach my $loc (@spanLoc) {
!       PlotToolsExt::CreateBladetoBladeContourView( $stage->Name(), $stage->Components(), 'Mach Number', 'relative Mach number', $loc );
!     }
!   }
!   if ( objectExists('Total Pressure') ) {
!     foreach my $loc (@spanLoc) {
!   	PlotToolsExt::CreateBladetoBladeContourView( $stage->Name(), $stage->Components(), 'Total Pressure', 'relative total pressure', $loc );
!     }
!   }
!   foreach my $loc (@spanLoc) {
!     PlotToolsExt::CreateBladetoBladeVectorView( $stage->Name(), $stage->Components(), $loc );
!   }
!}

# Set initial view to meridional mach number contour view
! my $firstStage = $stages[0]->Name();
>setViewportView cmd=set, view=/VIEW:$firstStage Contour static pressure Meridional Surface View, viewport=1
>centerview 1
