#
# A CFD-Post session file 
#
# Macro GUI begin
#
# macro name = Fan Noise
# macro subroutine = fanNoise
#
# macro parameter = separator
#  type = Separator
#
# macro parameter = comment
#  type = Comment
#  default = Calculates the tonal noise generated by a low speed fan (Mach Number less than 0.4)
#
#
# macro parameter = separator
#  type = Separator
#
# macro parameter = Domain
#  type = Location
#  location type = Domain
#
# macro parameter = Blade Selection
#  type = Combo
#  list = Automatic, Custom
#
# macro parameter = >  Custom Blade
#  type = Location
#  location category = region2d
#
# macro parameter = >  Custom # Blades
#  type = Integer
#
# macro parameter = # of Harmonics
#  type = Integer
#  range = 1, 100
#  default = 6
#
# macro parameter = Observer (r)
#  type = Float
#  quantity type = length
#  range = 0.1 [m], 10 [m]
#  default = 1 [m]
#
# macro parameter = Observer (theta)
#  type = Float
#  quantity type = angle
#  range = 0 [degree], 360 [degree]
#  default = 0 [degree]
#
# macro parameter = Theta Sectors
#  type = Integer
#  range = 1, 1000
#  default = 36
#
# macro parameter = Directivity Harm. #
#  type = Integer
#  range = 1, 100
#  default = 1
#
# macro parameter = Loading Coeff.
#  type = Float
#  range = 2, 2.5
#  default = 2.2
#
# macro parameter = Acou. Ref. Pressure
#  type = Float
#  quantity type = pressure
#  range = 2e-5 [Pa], 1 [Pa]
#  default = 2e-5 [Pa]
#
# macro parameter = Acou. Ref. Power
#  type = Float
#  quantity type = Energy Source
#  range = 1e-10 [W m^-3], 1e-12 [W m^-3]
#  default = 1e-12 [W m^-3]
#
# macro parameter = Sound Speed
#  type = Float
#  quantity type = Velocity
#  range = 300 [m/s], 400 [m/s]
#  default = 340 [m/s]
#
# Macro GUI end

COMMAND FILE:
  CFX Post Version = 11.0
END


! no warnings 'redefine';

! sub fanNoise {
! ($domain, $bladeSelect, $userBlade, $userNBlades, $harmonic, $oRadius, $oTheta, $tSectors, $dirHarm, \
   $loadCoeff, $pref, $wref, $soundSpeed ) = @_;

! $dataDir = $ENV{CUE_LOCAL_DATA_DIR};
> readsession filename=$dataDir/PostReports/turboReportCommon.cse


COMMENT: Noise Analysis Description
  Comment Heading = Noise Analysis
  Heading Level = 1
  Comment Text = \
  <p>This analysis has been supplied to assist in the evaluation of tonal noise levels generated by low speed fans (Mach Number less than 0.4). The equations were obtained from available literature, however some equations may have alternate definitions. It is <b>your responsibility</b> to verify the accuracy of these definitions.</p>
END

# Get turbo prameters

! if($domain  eq "") {
!   my $alldomainlist=getValue('All Domains', 'Member List');
!   my @domains=split(/,/,"$alldomainlist");
!   $domain = $domains[0];
! }

! my @retVal = collectTurboInfo($domain);
! if ( !@retVal ) {
!   exit 1;
! }
! $inlet = $retVal[0];
! $outlet = $retVal[1];
! $bladeRow = $retVal[2];
! $axis = $retVal[3];
! $omega = $retVal[4];
! $nBladeRows = $retVal[5];

! if( $bladeSelect ne "Automatic" ) {
!    $bladeRow = $userBlade;
!    $nBladeRows = $userNBlades;
! }

# Error checking
! if( $domain eq "" ) {
!   reportError( "A valid rotating domain must be specified first." );
!   return;
! }
! if( $bladeRow eq "" ) {
!   reportError( "Blade region not defined in domain $domain.
!		  Use 'Custom' blade selection and select a valid
!		  blade region and the number of blades." );
!   return;
! }
! if( $omega eq "" ) {
!   reportError( "No angular velocity found for domain $domain.
!		  Noise can only be evaluated for a rotating domain." );
!   return;
! }

# Blade force expression

! my ($bladeForceExpr, $isValidBlade) = getBladeForceExpr($bladeRow,$axis);

# Loop over harmonics and add sound curve points

! my @Lp;
! my @Lw;
! my @Freq;
! my @LpT;
! my @LwT;
! my @Theta;

! for( $nHarmCount = 0; $nHarmCount <= $harmonic; ++$nHarmCount ) {
# do theta sectors only for the specified directivity harmonic
! $thetaSectorLoop = 0;
! $nHarm = $nHarmCount;
! $maxThetaLoops = 0;
! if( $nHarmCount == 0 ) {
!   $thetaSectorLoop = 1;
!   $nHarm = $dirHarm;
!   $maxThetaLoops = $tSectors;
! }
! for( $thetaSector = 0; $thetaSector <= $maxThetaLoops; ++$thetaSector ) {
! if( $thetaSectorLoop ) {
!   $theta = getExprVal( "$thetaSector * 2 * pi / $tSectors" );
! }
# we are not doing theta sector loop, so just do the calculation once for this harmonic
! else { 
!   $theta = $oTheta;
! }

# Reference density is based on the final timestep's density
! ($dref, $drefUnits) = evaluate( "maxVal( Density )\@$domain" );

LIBRARY:
 CEL:
  EXPRESSIONS:
   Number of Blade Rows = $nBladeRows
   Number of Harmonics = $harmonic
   Angular Velocity = $omega
   Absolute Angular Velocity = abs($omega)
   Harmonic Number = $nHarm
   Observer Radius = $oRadius
   Observer Theta = $theta
   Loading Coefficient = $loadCoeff
   Acoustic Reference Pressure = $pref
   Acoustic Reference Power = $wref
   Reference Density = $dref [$drefUnits]
   Sound Speed = $soundSpeed
   Axial Blade Force = abs($bladeForceExpr)
   Shroud Radius = maxVal( Radius )@$domain
   Hub Radius = minVal( Radius )@$domain
   Blade Center = 2 * ( Shroud Radius^3 - Hub Radius^3 ) / ( 3 * ( Shroud Radius^2 - Hub Radius^2 ) )
   Rotational Mach Number = Absolute Angular Velocity * Blade Center / (Sound Speed * 1 [radian])
   Doppler Factor = Harmonic Number * $nBladeRows * Rotational Mach Number * sin( Observer Theta )
   Acoustic Pressure = Harmonic Number * $nBladeRows^2 * Absolute Angular Velocity / ( 2 * sqrt( 2 ) * pi * Sound Speed * Observer Radius * 1 [radian] ) \
	* cos( Observer Theta ) * Axial Blade Force * ( Harmonic Number * $nBladeRows )^-Loading Coefficient * besselJ( 0, Doppler Factor )
   Sound Pressure Level = 10 * log10( ( Acoustic Pressure / Acoustic Reference Pressure )^2 )
   Acoustic Impedence = maxVal( Reference Density )@$domain * Sound Speed
   Sound Power Level = 10 * log10( 4 * pi * Observer Radius^2 * Acoustic Pressure^2 / ( Acoustic Impedence * Acoustic Reference Power * 1[m^3] ) )
  END
 END
END


! my ($Lpm,$LpmUnits) = evaluate("Sound Pressure Level");
! my ($Lwm,$LwmUnits) = evaluate("Sound Power Level");

# Clip to 20 [dB]
! if( $Lpm < 20 ) {
!   $Lpm = 20;
! }
! if( $Lwm < 20 ) {
!   $Lwm = 20;
! }

! if( $thetaSectorLoop ) {
!   $LpT[$thetaSector] = $Lpm;
!   $Theta[$thetaSector] = getExprVal( "$theta" );
! }
! else {
!   $Lp[$nHarm] = $Lpm;
!   $Lw[$nHarm] = $Lwm;
!   $Freq[$nHarm] = getExprVal("$nHarm * $nBladeRows * Absolute Angular Velocity / ( 2 * pi )");
! }

! } # end of loop over theta sectors
! } # end of loop over harmonics

# Overall SPL and SWL

! $splExp = 0;
! $swlExp = 0;
! for( $nHarm = 1; $nHarm <= $harmonic; ++$nHarm ) {
!   $splExp += 10 ** ( $Lp[$nHarm]/10 );
!   $swlExp += 10 ** ( $Lw[$nHarm]/10 );
! }
! $overallSPL = getExprVal( "10 * log10( $splExp )" );
! $overallSWL = getExprVal( "10 * log10( $swlExp )" );

#---------------------------------------------------------------------------
# Create turbo noise charts
#---------------------------------------------------------------------------

# Sound Charts

! open(OFH1,">turboNoise_spl_data.csv");
! open(OFH2,">turboNoise_swl_data.csv");
! print OFH1 "[Data]\nFrequency [Hz], Sound Pressure Level [dB]\n";
! print OFH2 "[Data]\nFrequency [Hz], Sound Power Level [dB]\n";

! for( $nHarm = 1; $nHarm <= $harmonic; ++$nHarm ) {
!   print OFH1 "$Freq[$nHarm], $Lp[$nHarm]\n";
!   print OFH2 "$Freq[$nHarm], $Lw[$nHarm]\n";
! }
! close(OFH1);
! close(OFH2);

! if( objectExists( "/CHART:SPL Chart" ) ) {
> delete /CHART:SPL Chart
! }
! if( objectExists( "/CHART:SWL Chart" ) ) {
> delete /CHART:SWL Chart
! }

# Noise input data

COMMENT: Noise Input Comment
  Comment Heading = Noise input data
  Heading Level = 2
  Comment Text = <p>This data is based on the input to the Fan Noise macro. To change the input values, select Turbo tab > Fan Noise macro, change parameters and select Calculate to re-generate the report. </p>
END

!{
!  my @list = (
!              'Domain', $domain, '%7.4f',
!              'Blade Region', $bladeRow, '%7.4f',
!              'Number of Blade Rows', 'Number of Blade Rows', '%3.0f',
!              'Angular Velocity', 'Angular Velocity', '%7.4f',
!              'Number of Harmonics', 'Number of Harmonics', '%3.0f',
!              'Observer Location (radius)', 'Observer Radius', '%7.4f',
!              'Observer Location (theta)', 'Observer Theta', '%7.4f',
!              'Loading Coefficient', 'Loading Coefficient', '%7.4f',
!              'Reference Pressure', 'Acoustic Reference Pressure', '%11.4e',
!              'Reference Power', 'Acoustic Reference Power','%11.4e',
!              'Rotational Mach Number', 'Rotational Mach Number', '%7.4f' );
!  computedResultsTable("Noise input data", @list);

# Set the Domain and 'Blade Region' values again since they are not
# expressions, and we don't want them to appear red in the final table:
!  my @listrev = (
!                 'Domain', $domain,
!                 'Blade Region', $bladeRow );
!  setResultsTableValues("Noise input data", @listrev);
!}


# Sound Pressure Levels

COMMENT: SPL Comment
  Comment Heading = Sound Pressure Levels
  Heading Level = 2
  Comment Text =
END

TABLE: Sound Pressure Levels Table
  OBJECT REPORT OPTIONS:
    Report Caption = Sound Pressure Levels
  END
  TABLE CELLS:
    A1 = "Harmonic", True, False, False, Center, False, 0, Font Name, 1|1, %3.0f, True, ffffff, 000000, True
    B1 = "Frequency [Hz]", True, False, False, Center, False, 0, Font Name, 1|1, %6.3f, True, ffffff, 000000, True
    C1 = "Sound Pressure Level - Lp [dB]", True, False, False, Center, False, 0, Font Name, 1|1, %6.3f, True, ffffff, 000000, True

!  my $rowStart = 2;
!  for( $nHarm = 1; $nHarm <= $harmonic; ++$nHarm ) {

!   my $rowNumber = $nHarm + $rowStart - 1;

    A$rowNumber = "$nHarm", False, False, False, Left, False, 0, Font Name, 1|1, %3.0f, True, ffffff, 000000, True
    B$rowNumber = "$Freq[$nHarm]", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True
    C$rowNumber = "$Lp[$nHarm]", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True

!  }
  END
END

CHART: SPL Chart
  Chart Title = Sound Pressure Levels
  Chart Legend = false
  CHART LINE: Chart Line 1
    Auto Chart Line Colour = Off
    Chart Line Colour = 0.2, 0.2, 1.0
    Chart Line Filename = turboNoise_spl_data.csv
    Chart Line Style = Dot
    Chart Line Type = From File
    Chart Line Visibility = On
    Chart Symbol Colour = 0.2, 0.2, 1.0
    Chart Symbol Style = Ellipse
    Line Name = New Line
  END
  OBJECT REPORT OPTIONS:
    Report Caption = Sound Pressure Levels
  END
END

# Sound Power Levels

COMMENT: SWL Comment
  Comment Heading = Sound Power Levels
  Heading Level = 2
  Comment Text =
END

TABLE: Sound Power Levels Table
  OBJECT REPORT OPTIONS:
    Report Caption = Sound Power Levels
  END
  TABLE CELLS:
    A1 = "Harmonic", True, False, False, Center, False, 0, Font Name, 1|1, %3.0f, True, ffffff, 000000, True
    B1 = "Frequency [Hz]", True, False, False, Center, False, 0, Font Name, 1|1, %6.3f, True, ffffff, 000000, True
    C1 = "Sound Power Level - Lp [dB]", True, False, False, Center, False, 0, Font Name, 1|1, %6.3f, True, ffffff, 000000, True

!  $rowStart = 2;
!  for( $nHarm = 1; $nHarm <= $harmonic; ++$nHarm ) {

!   my $rowNumber = $nHarm + $rowStart - 1;

    A$rowNumber = "$nHarm", False, False, False, Left, False, 0, Font Name, 1|1, %3.0f, True, ffffff, 000000, True
    B$rowNumber = "$Freq[$nHarm]", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True
    C$rowNumber = "$Lw[$nHarm]", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True

!  }
  END
END

CHART: SWL Chart
  Chart Title = Sound Power Levels
  Chart Legend = false
  CHART LINE: Chart Line 1
    Auto Chart Line Colour = Off
    Chart Line Colour = 0.2, 0.2, 1.0
    Chart Line Filename = turboNoise_swl_data.csv
    Chart Line Style = Dot
    Chart Line Type = From File
    Chart Line Visibility = On
    Chart Symbol Colour = 0.2, 0.2, 1.0
    Chart Symbol Style = Ellipse
    Line Name = New Line
  END
  OBJECT REPORT OPTIONS:
    Report Caption = Sound Power Levels
  END
END
> chart print, Chart Name = SPL Chart, filename = turboNoise_spl_chart.png, \
    x size = 600, y size = 500, format = png
> chart print, Chart Name = SWL Chart, filename = turboNoise_swl_chart.png, \
    x size = 600, y size = 500, format = png

# Directivity Chart

!($dummy, $angleUnits) = evaluate( $theta );

! open(OFH1,">turboNoise_splt_data.csv");
! print OFH1 "[Data]\nTheta [$angleUnits], Sound Pressure Level [dB]\n";

! for( $thetaSector = 0; $thetaSector <= $tSectors; ++$thetaSector ) {
!   print OFH1 "$Theta[$thetaSector], $LpT[$thetaSector]\n";
! }
! close(OFH1);

! if( objectExists( "/CHART:SPLT Chart" ) ) {
> delete /CHART:SPLT Chart
! }

COMMENT: Directivity Comment
  Comment Heading = Directivity
  Heading Level = 2
  Comment Text =
END

CHART: SPLT Chart
  Chart Title = Directivity
  Chart Legend = false
  CHART LINE: Chart Line 1
    Auto Chart Line Colour = Off
    Chart Line Colour = 0.2, 0.2, 1.0
    Chart Line Filename = turboNoise_splt_data.csv
    Chart Line Style = Solid
    Chart Line Type = From File
    Chart Line Visibility = On
    Chart Symbol Style = None
    Line Name = New Line
  END
  OBJECT REPORT OPTIONS:
    Report Caption = Directivity
  END
END

> chart print, Chart Name = SPLT Chart, filename = turboNoise_splt_chart.png, \
    x size = 600, y size = 500, format = png

COMMENT: Overall Noise Comment
  Comment Heading = Overall Noise
  Heading Level = 2
  Comment Text =
END

TABLE: Overall Noise Table
  OBJECT REPORT OPTIONS:
    Report Caption = Overall Noise
  END
  TABLE CELLS:
    A1 = "Sound Pressure Level [dB]", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B1 = "$overallSPL", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True
    A2 = "Sound Power Level [dB]", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B2 = "$overallSWL", False, False, False, Right, False, 0, Font Name, 1|1, %7.4f, False, ffffff, 000000, True
  END
END

#--------------------------------
#  Broadband noise
#--------------------------------

COMMENT: Broadband Noise Comment
  Comment Heading = Broadband Noise
  Heading Level = 2
  Comment Text = \
<p>Broadband noise model is derived from Proudman's formula (see expression <i>Proudman Sound Power Exp</i>), which predicts overall sound power. Associated variable (<i>Proudman Sound Power</i>) is evaluated on the entire domain, allowing visualization of isosurfaces that can be used to locate the portion of the flow that is responsible for noise generation.</p> \
<p>Note that this model predicts overall noise levels, not at a specific observer location.</p>

END

LIBRARY:
 CEL:
  EXPRESSIONS:
   Proudman Sound Power Watt = max((0.1 * Reference Density * Turbulence Eddy Dissipation * ( sqrt( 2 * Turbulence Kinetic Energy ) / Sound Speed )^5), 1e-15 [kg m^-1 s^-3])
   Proudman Sound Power Exp = 10 * log( Proudman Sound Power Watt / Acoustic Reference Power )
   # Clip to positive range only
   Proudman Sound Power Clipped Exp =step(Proudman Sound Power Exp)*Proudman Sound Power Exp 
  END
 END
END
USER SCALAR VARIABLE: Proudman Sound Power
  Recipe = Expression
  Expression = Proudman Sound Power Clipped Exp
END

# Table

TABLE: Proudman Sound Power Table
  OBJECT REPORT OPTIONS:
    Report Caption = Proudman Sound Power
  END
  TABLE CELLS:
    A1 = "Minimum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B1 = "=minVal( Proudman Sound Power )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C1 = "[dB]", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    A2 = "Maximum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B2 = "=maxVal( Proudman Sound Power )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C2 = "[dB]", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    A3 = "Average", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B3 = "=ave( Proudman Sound Power )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C3 = "[dB]", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    A4 = "Total Power", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B4 = "=volumeInt( Proudman Sound Power Watt )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.4e, False, ffffff, 000000, True
    C4 = "=volumeInt( Proudman Sound Power Watt )@$domain", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
  END
END

# Broadband plot

! ($isoVal, $isoUnits) = evaluate( "0.95 * maxVal( Proudman Sound Power )\@$domain" );

ISOSURFACE: Broadband Noise Isosurface
  Variable = Proudman Sound Power
  Value = $isoVal [$isoUnits]
  Colour Mode = Constant
  Colour = 1, 0.4, 0.4
END
SURFACE GROUP: Blade Group
  Location List = $bladeRow
  Transparency = 0.5
END
VIEW: Broadband Noise Figure
  Is A Figure = true
  Object Visibility List = /ISOSURFACE:Broadband Noise Isosurface
  OBJECT REPORT OPTIONS:
    Report Caption = Isosurface at 95% of Proudman Sound Power
  END
END
>setViewportView cmd=set, view=/VIEW:Broadband Noise Figure, viewport=1
>setcamera viewport=1, camera=Isometric Y
# fit view before showing the blade
>centerview 1
>show Blade Group, view=/VIEW:Broadband Noise Figure


#--------------------------------
#  Noise Sources
#--------------------------------

COMMENT: Noise Sources Comment
  Comment Heading = Noise Sources
  Heading Level = 2
  Comment Text = \
<p>This section reports on Monopole, Dipole and Quadrupole noise sources, derived from Ffowcs Williams and Hawkings (FW-H) equations. These sources can be compared with each other and with the broadband noise to determine the dominant noise source in the design.</p> \
<p><b>Monopole source</b> is related to the movement of the source surface. It defines the volume displacement of the source. It is usually called self noise.</p> \
<p><b>Dipole source</b> describes the interaction between the fluid and the surface of the source. It defines the loading fluctuations exerted on the surface.</p> \
<p><b>Quadrupole source</b> is related to the turbulence fluctuation levels of the fluid. It is also called self noise.</p>
END

# Results may contain Lighthill stress. If so, use it, otherwise calculate it.
! $lighthillAvailable = 0;
! if( objectExists("Lighthill Stress uu") ) {
! $lighthillAvailable = 1;
! }

LIBRARY:
 CEL:
  EXPRESSIONS:
   Monopole Source Strength Exp = Reference Density * abs( Angular Velocity * Radius * ( Theta Direction X * Normal X + Theta Direction Y * Normal Y + Theta Direction Z * Normal Z ) / 1 [radian] )
! if( $lighthillAvailable ) {
   Quadrupole Source Strength Exp = (Lighthill Stress uu + Lighthill Stress vv + Lighthill Stress ww) / 3
! } else {
   Quadrupole Source Strength Exp = Reference Density * Velocity^2 / 3
! } 
  END
 END
END


USER SCALAR VARIABLE: Monopole Source Strength
  Recipe = Expression
  Expression = Monopole Source Strength Exp
END
USER SCALAR VARIABLE: Dipole Source Strength
  Recipe = Expression
  Expression = Pressure
END
USER SCALAR VARIABLE: Quadrupole Source Strength
  Recipe = Expression
  Expression = Quadrupole Source Strength Exp
END

# Monopole/Dipole time charts

# makes sense only for transient cases
! $timestepList = getValue("DATA READER", "Timestep List");
! @timesteps = split(/, /, $timestepList );
! $nTimesteps = @timesteps;

! if( $nTimesteps > 1 ) { 
CHART: Monopole Source Time Chart
  Chart Title = Variation of maximum Monopole Source Strength at the blade with time
  Chart Type = Time
  Use Data For Axis Labels = Off
  Chart X Axis Label = Time <units>
  Chart Y Axis Label = Monopole Source Strength <units>
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Monopole Source Strength)@Blade Group
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END

CHART: Monopole Source Time Chart FFT
  Chart Title = Spectrum of maximum Monopole Source Strength at the blade
  Chart Type = Time
  Use Data For Axis Labels = On
  Is FFT Chart = Yes
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Monopole Source Strength)@Blade Group
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END

CHART: Dipole Source Time Chart
  Chart Title = Variation of maximum Dipole Source Strength at the blade with time
  Chart Type = Time
  Use Data For Axis Labels = Off
  Chart X Axis Label = Time <units>
  Chart Y Axis Label = Dipole Source Strength <units>
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Dipole Source Strength)@Blade Group
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END
CHART: Dipole Source Time Chart FFT
  Chart Title = Spectrum of maximum Dipole Source Strength at the blade
  Chart Type = Time
  Use Data For Axis Labels = On
  Is FFT Chart = Yes
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Dipole Source Strength)@Blade Group
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END
! } # if transient

# Monopole/Dipole table

TABLE: Blade Noise Sources Table
  OBJECT REPORT OPTIONS:
    Report Caption = Summary of noise sources at the blade and at the final timestep
  END
  TABLE CELLS:
    B1 = "Monopole Source Strength", True, False, False, Right, False, 0, Font Name, 1|1, %7.1f    
    C1 = "Dipole Source Strength", True, False, False, Right, False, 0, Font Name, 1|1, %7.1f    
    A2 = "Minimum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B2 = "=minVal( Monopole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C2 = "=minVal( Dipole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    D2 = "=minVal( Dipole Source Strength )@Blade Group", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
    A3 = "Maximum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B3 = "=maxVal( Monopole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C3 = "=maxVal( Dipole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    D3 = "=maxVal( Dipole Source Strength )@Blade Group", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
    A4 = "Average", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B4 = "=ave( Monopole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C4 = "=ave( Dipole Source Strength )@Blade Group", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    D4 = "=ave( Dipole Source Strength )@Blade Group", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
  END
END

# Monopole/Dipole plots

CONTOUR: Monopole Source Contour
  Location List = Blade Group
  Colour Variable = Monopole Source Strength
END
CONTOUR: Dipole Source Contour
  Location List = Blade Group
  Colour Variable = Dipole Source Strength
END

LEGEND: Monopole Source Figure Legend
  Legend Format = %6.1f
  Legend Plot = Monopole Source Contour
  Legend X Justification = Left
  Legend Title Mode = Variable
END
VIEW: Monopole Source Figure
  Is A Figure = true
  Object Visibility List = /CONTOUR:Monopole Source Contour, /LEGEND:Monopole Source Figure Legend
  OBJECT REPORT OPTIONS:
    Report Caption = Monopole Source
  END
END
>setViewportView cmd=set, view=/VIEW:Monopole Source Figure, viewport=1
>setcamera viewport=1, camera=Isometric Y
>centerview 1

LEGEND: Dipole Source Figure Legend
  Legend Format = %6.1f
  Legend Plot = Dipole Source Contour
  Legend X Justification = Left
  Legend Title Mode = Variable
END
VIEW: Dipole Source Figure
  Is A Figure = true
  Object Visibility List = /CONTOUR:Dipole Source Contour, /LEGEND:Dipole Source Figure Legend
  OBJECT REPORT OPTIONS:
    Report Caption = Dipole Source
  END
END
>setViewportView cmd=set, view=/VIEW:Dipole Source Figure, viewport=1
>setcamera viewport=1, camera=Isometric Y
>centerview 1

# Quadrupole time chart

! if( $nTimesteps > 1 ) { 

CHART: Quadrupole Source Time Chart
  Chart Title = Variation of maximum Quadrupole Source Strength in the passage with time
  Chart Type = Time
  Use Data For Axis Labels = Off
  Chart X Axis Label = Time <units>
  Chart Y Axis Label = Quadrupole Source Strength <units>
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Quadrupole Source Strength)@$domain
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END
CHART: Quadrupole Source Time Chart FFT
  Chart Title = Spectrum of maximum Quadrupole Source Strength in the passage
  Chart Type = Time
  Use Data For Axis Labels = On
  Is FFT Chart = Yes
  Chart Legend = false
  CHART LINE: Maximum
    Auto Chart Line Colour = Off
    Chart Line Colour = 1, 0, 0
    Chart Line Style = Solid
    Line Name = Maximum
    Time Chart Expression = maxVal(Quadrupole Source Strength)@$domain
    Time Chart Type = Expression
    Time Chart Variable = Dipole Source Strength   # TODO: Should not be needed
  END
END

# TODO: we need a global refresh for all time charts 
> chart refresh=/CHART:Monopole Source Time Chart
> chart refresh=/CHART:Dipole Source Time Chart
> chart refresh=/CHART:Quadrupole Source Time Chart
> chart refresh=/CHART:Monopole Source Time Chart FFT
> chart refresh=/CHART:Dipole Source Time Chart FFT
> chart refresh=/CHART:Quadrupole Source Time Chart FFT
! } # if transient

# Quadrupole table

TABLE: Quadrupole Sources Table
  OBJECT REPORT OPTIONS:
    Report Caption = Summary of quadrupole sources at the final timestep
  END
  TABLE CELLS:
    B1 = "Quadrupole Source Strength", True, False, False, Right, False, 0, Font Name, 1|1, %7.1f    
    A2 = "Minimum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B2 = "=minVal( Quadrupole Source Strength )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C2 = "=minVal( Quadrupole Source Strength )@$domain", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
    A3 = "Maximum", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B3 = "=maxVal( Quadrupole Source Strength )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C3 = "=maxVal( Quadrupole Source Strength )@$domain", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
    A4 = "Average", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, True
    B4 = "=ave( Quadrupole Source Strength )@$domain", False, False, False, Right, False, 0, Font Name, 1|1, %7.1f, False, ffffff, 000000, True
    C4 = "=ave( Quadrupole Source Strength )@$domain", False, False, False, Left, False, 0, Font Name, 1|1, %7.4f, True, ffffff, 000000, False
  END
END

# Quadrupole plots

! ($isoVal, $isoUnits) = evaluate( "0.8 * maxVal( Quadrupole Source Strength )\@$domain" );

ISOSURFACE: Quadrupole Source Isosurface
  Variable = Quadrupole Source Strength
  Value = $isoVal [$isoUnits]
  Colour Mode = Constant
  Colour = 1, 0.4, 0.4
END
SURFACE GROUP: Blade Group
  Transparency = 0.5
END
VIEW: Quadrupole Source Figure
  Is A Figure = true
  Object Visibility List = /ISOSURFACE:Quadrupole Source Isosurface
  OBJECT REPORT OPTIONS:
    Report Caption = Isosurface at 80% of Quadrupole Source Strength
  END
END
>setViewportView cmd=set, view=/VIEW:Quadrupole Source Figure, viewport=1
>setcamera viewport=1, camera=Isometric Y
# fit view before showing the blade
>centerview 1
>show Blade Group, view=/VIEW:Quadrupole Source Figure

!}

# A version of the macro with default values
! sub fanNoiseDefault() {
!   fanNoise('', Automatic, "", 0, 6, "1 [m]", "0 [degree]", 36, 1, 2.2, "2e-5 [Pa]", "1e-12 [W m^-3]", "340 [m/s]" );
! }
