#
# 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 = 10e-10 [W m^-3], 10e-12 [W m^-3]
#  default = 10e-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',
!              'Numb