newport_j
February 4th, 2010, 08:19 PM
I have a question about c programing in Linux or Ubuntu. I think that it is relevant to this Forum. The questions is about using global variables and files. Global variables are available to all subprograms in the computer program. I am going to post two subprograms from a program that I am maintaining and I think the question should be obvious. The first subprogram:
/*! \file int_bnd2.c
\brief integrates Gaussian ray bundles.
\par
\author Phred
\author Weinberg
*/
/* WEG (WAF Eigenray Generator)
Translated to "c" and modifided by Andrew "Phred" Fredricks
form work done by Chic Weinberg.
* Function INT_BND2 integrates Gaussian ray bundles.
Variables
* ZTRG = target depth (km)
* APRMIN = minimum ray bundle aperture (km)
* NANGv = number of source angles
* ANGv = source angle vector (rad)
* PWRv = source pressure squared vector (uPa^2)
* IRAYu1 = leading ray index
* IRAYu2 = trailing ray index
* ZVRTu1 = leading virtual ray depth (km)
* ZVRTu2 = trailing virtual ray depth (km)
* RRAY = ray range (km)
* ZRAYu = unfolded ray depth (km)
* TRAYu = unfolded ray time (s)
* PCOSu = unfolded ray horizontal slowness (s/km)
* PSINu = unfolded ray vertical slowness (s/km)
* NUPRu = number of unfolded upper vertexes
* NLWRu = number of unfolded lower vertexes
* NCRSo = number of ray crossings
* URAYu = unfolded ray attenuation (nepers)
* FRAYu = unfolded ray factor (pres ratio)
* ORAYu = unfolded ray phase (rad)
* NSRF = number of surface reflections
* NBTM = number of bottom reflections
* UPRSUM = weighted upper vertex sum
* LWRSUM = weighted lower vertex sum
* SRCSUM = weighted source angle sum (rad)
* PWRSUM = pressure squared sum (uPa^2)
* TIMSUM = weighted time sum (s)
* PHSSUM = weighted phase sum (rad)
* PCSSUM = weighted horizontal slowness sum (s/km)
* PSNSUM = weighted vertical slowness sum (s/km)
* IMAFLG = eigenray flag {0,1} for {real,imag} eigenrays
* EIGTOLi = eigenray tolerance
************************************************** *********************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <pthread.h>
#include "thread.h"
#include "WAFmutex.h"
#include "WAFmathBase.h"
#include "SIMdefines.h"
#include "envstructdefs.h"
#include "WEGStrDefs.h"
#include "WEGFunDefs.h"
/*!\brief
integrates Gaussian ray bundles.
\param ZTRG target depth (km)
\param PTRG
\param SCST
\param APER
\param TRD Test Ray Data
\param IRAYu1 leading ray index
\param IRAYu2 trailing ray index
\param ZVRTu1 leading virtual ray depth (km)
\param ZVRTu2 trailing virtual ray depth (km)
\param RRAY ray range (km)
\param ZRAYu unfolded ray depth (km)
\param TRAYu unfolded ray time (s)
\param PCOSu unfolded ray horizontal slowness (s/km)
\param PSINu unfolded ray vertical slowness (s/km)
\param NSRF nu
mber of surface reflections
\param NBTM number of bottom reflections
\param NUPRu number of unfolded upper vertexes
\param NLWRu number of unfolded lower vertexes
\param NCRSo number of ray crossings
\param URAYu unfolded ray attenuation (nepers)
\param FRAYu unfolded ray factor (pres ratio)
\param ORAYu unfolded ray phase (rad)
\param UPRSUM weighted upper vertex sum
\param LWRSUM weighted lower vertex sum
\param SRCSUM weighted source angle sum (rad)
\param RCVSUM weighted recive angle sum (rad)
\param PWRSUM pressure squared sum (uPa^2)
\param TIMSUM weighted time sum (s)
\param PHSSUM weighted phase sum (rad)
\param PCSSUM weighted horizontal slowness sum (s/km)
\param PSNSUM weighted vertical slowness sum (s/km)
\param IMAFLG eigenray flag {0,1} for {real,imag} eigenrays
\param Env
*/
void INT_BND2( double ZTRG, double PTRG,
LOGICAL SCST, double APER, TEST_RAY_DATA *TRD,
int IRAYu1, int IRAYu2, double ZVRTu1,
double ZVRTu2, double RRAY, double *ZRAYu, double *TRAYu,
double *PCOSu, double *PSINu, int NSRF, int NBTM,
int *NUPRu, int *NLWRu, int NCRSo, double *URAYu,
double *FRAYu, double *ORAYu, double *UPRSUM, double *LWRSUM,
double *SRCSUM, double *RCVSUM, double *PWRSUM, double *TIMSUM, double *PHSSUM,
double *PCSSUM, double *PSNSUM, int *IMAFLG,
ENV_DEF *Env){
int i, IRAY, NRFLo, NCSTo;
double ZBND1=0.0, ZBND2=0.0, ZDEL, ZAPR, SDEV, ZREL, ARG;
double PWR[MAX_NUM_FREQ], TTRG, ARGMAX;
double APRMIN=666.0, APRMINt, TOTPWR;
/*
EIGTOLi = eigenray tolerance
APRMIN minimum ray bundle aperture (km)
*/
/*-----------------------------------------------------------------
Set the maximum ray bundle argument for ARG = 0.5*(ZREL/SDEV)**2
so that
exp(-ARGMAX/2) = EIGTOLi/10
-----------------------------------------------------------------*/
ARGMAX = - 2.0 * log( 0.1*EIGTOLi + 1.0E-30 );
/*-----------------------------------------
Initialize the eigenray flag (imaginary).
-----------------------------------------*/
*IMAFLG = 1;
/*---------------------------
Set the number of caustics.
---------------------------*/
NRFLo = NSRF + NBTM;
if(NCRSo-NRFLo > 0) NCSTo = NCRSo-NRFLo;
else NCSTo = 0;
/*-------------------------
Initialize weighted sums.
-------------------------*/
*UPRSUM = 0.0;
*LWRSUM = 0.0;
*SRCSUM = 0.0;
*RCVSUM = 0.0;
*TIMSUM = 0.0;
*PCSSUM = 0.0;
*PSNSUM = 0.0;
for(i=0;i<(*Env).Nfreq;i=i+1){
PWRSUM[i] = 0.0;
PHSSUM[i] = 0.0;
};
/*-------------------------------------------------
Integrate the ray bundles within [ZVRTu1,ZVRTu2].
-------------------------------------------------*/
/*printf("IRAYu1=%d IRAYu2=%d\n",IRAYu1,IRAYu2);*/
for(IRAY=IRAYu1;IRAY<=IRAYu2;IRAY=IRAY+1){
/*-----------------------------
Set the initial bundle depth.
-----------------------------*/
if( IRAY > IRAYu1 ) ZBND1 = 0.5*(ZRAYu[IRAY]+ZRAYu[IRAY-1]);
if( IRAY == IRAYu1 ) ZBND1 = 0.5*(ZRAYu[IRAY]+ZVRTu1);
/*---------------------------
Set the final bundle depth.
---------------------------*/
if( IRAY < IRAYu2 ) ZBND2 = 0.5*(ZRAYu[IRAY]+ZRAYu[IRAY+1]);
if( IRAY == IRAYu2 ) ZBND2 = 0.5*(ZRAYu[IRAY]+ZVRTu2);
/*-------------------------------------
Compute the test ray-target distance.
-------------------------------------*/
/*------------------------------------
Set the minimum ray bundle aperture.
------------------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
if( SCST != true ){
if(APER < 16*M_PI/(PTRG*(*Env).frequency_Hz[i])) APRMINt = APER;
else APRMINt = 16*M_PI/(PTRG*(*Env).frequency_Hz[i]);
/*--------------------------------------------------------
Set the minimum ray bundle aperture at a smooth caustic.
--------------------------------------------------------*/
} else {
if(APER < 4*M_PI/(PTRG*(*Env).frequency_Hz[i])) APRMINt = APER;
else APRMINt = 4*M_PI/(PTRG*(*Env).frequency_Hz[i]);
};
if(APRMINt<APRMIN) APRMIN = APRMINt;
};
ZDEL = ZBND2 - ZBND1;
ZAPR = fabs(ZDEL);
if(ZAPR > APRMIN) SDEV = 0.5 * ZAPR;
else SDEV = 0.5 * APRMIN;
ZREL = ZTRG - ZRAYu[IRAY];
/*------------------------------
Compute the Gaussian argument.
------------------------------*/
ARG = 0.5*(ZREL/SDEV)*(ZREL/SDEV);
if( ARG < 0 ){
printf("INT_BND2\n");
exit(1);
};
if( ARG < ARGMAX ){
/*-----------------------------
Compute the pressure squared.
-----------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
PWR[i] = TRD[IRAY].PWRv
* FRAYu[IRAY*MAX_NUM_FREQ+i] * FRAYu[IRAY*MAX_NUM_FREQ+i]
* exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+i]-ARG)
/ (sqrt(2*M_PI)*SDEV*PCOSu[IRAY]*RRAY);
/* GRAB fix !!!!!!!!*/
/*PWR[i] = ZAPR * (exp(-ARG) / (sqrt(2*M_PI)*SDEV))
* FRAYu[IRAY*MAX_NUM_FREQ+i] * FRAYu[IRAY*MAX_NUM_FREQ+i]
* exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+i])/(RRAY * RRAY * 1000.0 * 1000.0);*/
};
/*printf("%f %f %f %f Pzdel=%f ZDEL=%f Normal=%f FRAYu=%f URAYu=%f RRAY=%f Loss=%f",
ARG, exp(-ARG),
(TRD[IRAY].PWRv * 1000.0 * 1000.0 / PCOSu[IRAY]),RRAY,
(TRD[IRAY].PWRv * 1000.0 * 1000.0 / PCOSu[IRAY])*RRAY, ZDEL,
exp(-ARG) / (sqrt(2*M_PI)*SDEV),
20.0*log10(FRAYu[IRAY*MAX_NUM_FREQ+0]),
10.0*log10(exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+0])),
-10.0*log10(RRAY * RRAY * 1000.0 * 1000.0),
10.0*log10( FRAYu[IRAY*MAX_NUM_FREQ+0] * FRAYu[IRAY*MAX_NUM_FREQ+0]
*exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+0])/(RRAY * RRAY * 1000.0 * 1000.0)) );*/
/*Note: the bundle at caustic gets processed twice.*/
if( fabs(ZRAYu[IRAY]-ZVRTu1) <= 0 ||
fabs(ZRAYu[IRAY]-ZVRTu2) <= 0 )
for(i=0;i<(*Env).Nfreq;i=i+1) PWR[i]=0.5*PWR[i];
TOTPWR = 0.0;
for(i=0;i<(*Env).Nfreq;i=i+1) TOTPWR = TOTPWR + PWR[i];
if( TOTPWR > PWRMIN*
(*Env).Nfreq ){
/*-------------------------
Estimate the travel time.
-------------------------*/
TTRG = TRAYu[IRAY] + PSINu[IRAY]*ZREL;
/*-------------------------
Compute the weighted sum.
----------------
---------*/
*UPRSUM = *UPRSUM + TOTPWR * NUPRu[IRAY];
*LWRSUM = *LWRSUM + TOTPWR * NLWRu[IRAY];
*SRCSUM = *SRCSUM + TOTPWR * TRD[IRAY].ANG;
for(i=0;i<(*Env).Nfreq;i=i+1) PWRSUM[i] = PWRSUM[i] + PWR[i];
*TIMSUM = *TIMSUM + TOTPWR * TTRG;
for(i=0;i<(*Env).Nfreq;i=i+1) PHSSUM[i] = PHSSUM[i] + TOTPWR * ORAYu[IRAY];
*PCSSUM = *PCSSUM + TOTPWR * PCOSu[IRAY];
*PSNSUM = *PSNSUM + TOTPWR * PSINu[IRAY];
/*Test Carrying source angle*/
/*printf("Rang=%f acos=%f\n",180.0*TRD[IRAY].RANG/M_PI,180.0*acos(PCOSu[IRAY]/PTRG)/M_PI);*/
/**RCVSUM = *RCVSUM + TOTPWR * TRD[IRAY].RANG;*/
*RCVSUM = *RCVSUM + TOTPWR * acos(PCOSu[IRAY]/PTRG);
/*printf("IRAY=%d PWRSUM=%e TOTPWR=%e ang=%fR %fD RCVSUM=%e ",
IRAY,PWRSUM[0],TOTPWR,acos(PCOSu[IRAY]/PTRG),180.0*acos(PCOSu[IRAY]/PTRG)/M_PI,*RCVSUM);*/
/*-----------------------------
Modify the phase at caustics.
-----------------------------*/
NRFLo = NSRF + NBTM;
if(NCRSo-NRFLo > 0) NCSTo = NCRSo-NRFLo;
else NCSTo = 0;
for(i=0;i<(*Env).Nfreq;i=i+1)
PHSSUM[i] = PHSSUM[i] - PWR[i] * ((M_PI/2.0)*NCSTo);
/*---------------------------------
Set the first eigenray flag real.
---------------------------------*/
if( (ZBND2-ZTRG)*(ZTRG-ZBND1) >= 0 ) *IMAFLG = 0;
};
/*printf(" dB(PWR[0]) = %f dB(PWRSUM[0]) = %f loss = %f\n",
10.0*log10(PWR[0]),10.0*log10(PWRSUM[0]),
20.0*log10(TRD[IRAY].FRAY[0]) + 8.686 * TRD[IRAY].URAY[0] - 20.0*log10(1500.0 * TRD[IRAY].TRAY));*/
};
/*printf("\n");*/
};
}
</code>
and the second subprogram:
<code>
/*! \file WEG.c
\brief WEG (WAF Eigenray Generator)
\par
computes acoustic eigenrays in a two-dimensional environment
using Gaussian ray bundles.
\par
\author Phred
\author Weinberg
\date 6-9-2004
\test
WEGtest will display output of function for testing
*/
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <pthread.h>
#include "thread.h"
#include "WAFmutex.h"
#include "WAFmathBase.h"
#include "SIMdefines.h"
#include "envstructdefs.h"
#include "WEGStrDefs.h"
#include "WEGFunDefs.h"
/*!\brief
WEG (WAF Eigenray Generator)
\par
computes acoustic eigenrays in a two-dimensional environment
using Gaussian ray bundl
es.
\param Env a structer to hold enviromental data
\param eigen_ray_data a structer to hold returned eigen rays
\param WPP Paralla passed info
\param NumThreads Number of Threads (including the king)
\param TRD Test Ray Data
\param TRSD Test RAy Segment Data
\param debug debug flag
\note
More info is needed in this discription.
<HR>
\test
\par <tt><b>WEGtest</b></tt>
Test code for the <tt><b>WEG()</b></tt> function.
\par Command Line Arguments:
<tt><b>WEGtest</b></tt> [<em>#treads</em>]
<em>#treads</em>) number of threads, defaults to one if not spified.
\par Input Files:
<tt>SorRec.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
Line 2) Source range depth\n
Line 3) numbers of targets range depth\n
Line 5 +) target range\n
Line 7 +) target depth\n
Line 8 +) number of freq\n
Line 9 +) frequency\n
Line 11 +) min angle for test ray fan\n
Line 12 +) max angle for test ray fan\n
Line 13 +) angle step size for test ray fan\n
Line 15 +) Max number of surface bounces\n
Line 16 +) Max number of bottom bounces\n
Line 17 +) Max number of upper verticys\n
Line 18 +) Max number of lower verticys
<tt>SeaState.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
<tt>SSP.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
<tt>Bottom.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
\par Code:
Main for test code is in <tt>WEGtest.c</tt>
<HR>
WEG (WAF Eigenray Generator)
*/
void WEG( ENV_DEF *Env, EIGEN_RAY *eigen_ray_data, WEG_P_PASS *WPP, int NumThreads,
TEST_RAY_DATA *TRD, TEST_RAY_SEG_DATA *TRSD, LOGICAL debug ){
int i;
int IRb=0;
int IZTRG=0;
/*test ray fan in RADS*/
double ANGMINi, ANGMAXi, ANGDELi;
/*max range in km*/
double RNGMAXi;
int NANGv;
double PSRC, ATNSRC[MAX_NUM_FREQ];
double P2SRC, GrSRC, GzSRC;
/* Max range */
RNGMAXi=(*Env).Target_Range[(*Env).NtargetR-1];
/* At CloseRange set min*/
if(0.005 > RNGMAXi) RNGMAXi = 0.005;
/*-----------------------------------------------
Compute the source sound slowness and gradient.
-----------------------------------------------*/
SND_SLW2( 1, 0.0, &((*Env).Source_Depth), &P2SRC, &GrSRC, &GzSRC,
&IRb, Env );
PSRC = sqrt(P2SRC);
/*--------------------------------
Generate test ray source angles.
--------------------------------*/
/*Fan of test angles*/
ANGMINi = (*Env).Min_Ang;
ANGMAXi = (*Env).Max_Ang;
ANGDELi = (*Env).Del_Ang;
SRC_ANG2( (*Env).Source_Depth, P2SRC, &NANGv, TRD,
ANGMINi, ANGMAXi, ANGDELi, &IRb, Env, debug);
/*-------------------------------------------
Compute the source attenuation coefficient.
-------------------------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
ATNSRC[i] = 0.0;
VLM_ATN3( (*Env).frequency_Hz[i], &(ATNSRC[i]), 0, Env );
};
/*-------------------------------
Compute.
-------------------------------*/
eigen_ray_data[0].Nfreq = (*Env).Nfreq;
eigen_ray_data[0].ERAY_NDX = 0; /* No rays yet!*/
PRC_BRN2( RNGMAXi, &(eigen_ray_data), WPP, NumThreads, &IRb,
&IZTRG, ATNSRC, GzSRC, NANGv, TRD, TRSD, PSRC, Env, debug);
}
Now these subprograms come from separate computer files. Notice that each has global variables (variables that are defined outside of the program code). Also, notice that the subprogram's global declarations are different. They define global variables to a main subprogram, but they are slightly different. The include files on each subprogram are different.
Now how can this be? I understand that each separate computer file with a subprogram, must define global variables, but at least all of the subprograms global variable definitions should be the same. But here they are different. Why is what I want to know.
Newport_j
/*! \file int_bnd2.c
\brief integrates Gaussian ray bundles.
\par
\author Phred
\author Weinberg
*/
/* WEG (WAF Eigenray Generator)
Translated to "c" and modifided by Andrew "Phred" Fredricks
form work done by Chic Weinberg.
* Function INT_BND2 integrates Gaussian ray bundles.
Variables
* ZTRG = target depth (km)
* APRMIN = minimum ray bundle aperture (km)
* NANGv = number of source angles
* ANGv = source angle vector (rad)
* PWRv = source pressure squared vector (uPa^2)
* IRAYu1 = leading ray index
* IRAYu2 = trailing ray index
* ZVRTu1 = leading virtual ray depth (km)
* ZVRTu2 = trailing virtual ray depth (km)
* RRAY = ray range (km)
* ZRAYu = unfolded ray depth (km)
* TRAYu = unfolded ray time (s)
* PCOSu = unfolded ray horizontal slowness (s/km)
* PSINu = unfolded ray vertical slowness (s/km)
* NUPRu = number of unfolded upper vertexes
* NLWRu = number of unfolded lower vertexes
* NCRSo = number of ray crossings
* URAYu = unfolded ray attenuation (nepers)
* FRAYu = unfolded ray factor (pres ratio)
* ORAYu = unfolded ray phase (rad)
* NSRF = number of surface reflections
* NBTM = number of bottom reflections
* UPRSUM = weighted upper vertex sum
* LWRSUM = weighted lower vertex sum
* SRCSUM = weighted source angle sum (rad)
* PWRSUM = pressure squared sum (uPa^2)
* TIMSUM = weighted time sum (s)
* PHSSUM = weighted phase sum (rad)
* PCSSUM = weighted horizontal slowness sum (s/km)
* PSNSUM = weighted vertical slowness sum (s/km)
* IMAFLG = eigenray flag {0,1} for {real,imag} eigenrays
* EIGTOLi = eigenray tolerance
************************************************** *********************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <pthread.h>
#include "thread.h"
#include "WAFmutex.h"
#include "WAFmathBase.h"
#include "SIMdefines.h"
#include "envstructdefs.h"
#include "WEGStrDefs.h"
#include "WEGFunDefs.h"
/*!\brief
integrates Gaussian ray bundles.
\param ZTRG target depth (km)
\param PTRG
\param SCST
\param APER
\param TRD Test Ray Data
\param IRAYu1 leading ray index
\param IRAYu2 trailing ray index
\param ZVRTu1 leading virtual ray depth (km)
\param ZVRTu2 trailing virtual ray depth (km)
\param RRAY ray range (km)
\param ZRAYu unfolded ray depth (km)
\param TRAYu unfolded ray time (s)
\param PCOSu unfolded ray horizontal slowness (s/km)
\param PSINu unfolded ray vertical slowness (s/km)
\param NSRF nu
mber of surface reflections
\param NBTM number of bottom reflections
\param NUPRu number of unfolded upper vertexes
\param NLWRu number of unfolded lower vertexes
\param NCRSo number of ray crossings
\param URAYu unfolded ray attenuation (nepers)
\param FRAYu unfolded ray factor (pres ratio)
\param ORAYu unfolded ray phase (rad)
\param UPRSUM weighted upper vertex sum
\param LWRSUM weighted lower vertex sum
\param SRCSUM weighted source angle sum (rad)
\param RCVSUM weighted recive angle sum (rad)
\param PWRSUM pressure squared sum (uPa^2)
\param TIMSUM weighted time sum (s)
\param PHSSUM weighted phase sum (rad)
\param PCSSUM weighted horizontal slowness sum (s/km)
\param PSNSUM weighted vertical slowness sum (s/km)
\param IMAFLG eigenray flag {0,1} for {real,imag} eigenrays
\param Env
*/
void INT_BND2( double ZTRG, double PTRG,
LOGICAL SCST, double APER, TEST_RAY_DATA *TRD,
int IRAYu1, int IRAYu2, double ZVRTu1,
double ZVRTu2, double RRAY, double *ZRAYu, double *TRAYu,
double *PCOSu, double *PSINu, int NSRF, int NBTM,
int *NUPRu, int *NLWRu, int NCRSo, double *URAYu,
double *FRAYu, double *ORAYu, double *UPRSUM, double *LWRSUM,
double *SRCSUM, double *RCVSUM, double *PWRSUM, double *TIMSUM, double *PHSSUM,
double *PCSSUM, double *PSNSUM, int *IMAFLG,
ENV_DEF *Env){
int i, IRAY, NRFLo, NCSTo;
double ZBND1=0.0, ZBND2=0.0, ZDEL, ZAPR, SDEV, ZREL, ARG;
double PWR[MAX_NUM_FREQ], TTRG, ARGMAX;
double APRMIN=666.0, APRMINt, TOTPWR;
/*
EIGTOLi = eigenray tolerance
APRMIN minimum ray bundle aperture (km)
*/
/*-----------------------------------------------------------------
Set the maximum ray bundle argument for ARG = 0.5*(ZREL/SDEV)**2
so that
exp(-ARGMAX/2) = EIGTOLi/10
-----------------------------------------------------------------*/
ARGMAX = - 2.0 * log( 0.1*EIGTOLi + 1.0E-30 );
/*-----------------------------------------
Initialize the eigenray flag (imaginary).
-----------------------------------------*/
*IMAFLG = 1;
/*---------------------------
Set the number of caustics.
---------------------------*/
NRFLo = NSRF + NBTM;
if(NCRSo-NRFLo > 0) NCSTo = NCRSo-NRFLo;
else NCSTo = 0;
/*-------------------------
Initialize weighted sums.
-------------------------*/
*UPRSUM = 0.0;
*LWRSUM = 0.0;
*SRCSUM = 0.0;
*RCVSUM = 0.0;
*TIMSUM = 0.0;
*PCSSUM = 0.0;
*PSNSUM = 0.0;
for(i=0;i<(*Env).Nfreq;i=i+1){
PWRSUM[i] = 0.0;
PHSSUM[i] = 0.0;
};
/*-------------------------------------------------
Integrate the ray bundles within [ZVRTu1,ZVRTu2].
-------------------------------------------------*/
/*printf("IRAYu1=%d IRAYu2=%d\n",IRAYu1,IRAYu2);*/
for(IRAY=IRAYu1;IRAY<=IRAYu2;IRAY=IRAY+1){
/*-----------------------------
Set the initial bundle depth.
-----------------------------*/
if( IRAY > IRAYu1 ) ZBND1 = 0.5*(ZRAYu[IRAY]+ZRAYu[IRAY-1]);
if( IRAY == IRAYu1 ) ZBND1 = 0.5*(ZRAYu[IRAY]+ZVRTu1);
/*---------------------------
Set the final bundle depth.
---------------------------*/
if( IRAY < IRAYu2 ) ZBND2 = 0.5*(ZRAYu[IRAY]+ZRAYu[IRAY+1]);
if( IRAY == IRAYu2 ) ZBND2 = 0.5*(ZRAYu[IRAY]+ZVRTu2);
/*-------------------------------------
Compute the test ray-target distance.
-------------------------------------*/
/*------------------------------------
Set the minimum ray bundle aperture.
------------------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
if( SCST != true ){
if(APER < 16*M_PI/(PTRG*(*Env).frequency_Hz[i])) APRMINt = APER;
else APRMINt = 16*M_PI/(PTRG*(*Env).frequency_Hz[i]);
/*--------------------------------------------------------
Set the minimum ray bundle aperture at a smooth caustic.
--------------------------------------------------------*/
} else {
if(APER < 4*M_PI/(PTRG*(*Env).frequency_Hz[i])) APRMINt = APER;
else APRMINt = 4*M_PI/(PTRG*(*Env).frequency_Hz[i]);
};
if(APRMINt<APRMIN) APRMIN = APRMINt;
};
ZDEL = ZBND2 - ZBND1;
ZAPR = fabs(ZDEL);
if(ZAPR > APRMIN) SDEV = 0.5 * ZAPR;
else SDEV = 0.5 * APRMIN;
ZREL = ZTRG - ZRAYu[IRAY];
/*------------------------------
Compute the Gaussian argument.
------------------------------*/
ARG = 0.5*(ZREL/SDEV)*(ZREL/SDEV);
if( ARG < 0 ){
printf("INT_BND2\n");
exit(1);
};
if( ARG < ARGMAX ){
/*-----------------------------
Compute the pressure squared.
-----------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
PWR[i] = TRD[IRAY].PWRv
* FRAYu[IRAY*MAX_NUM_FREQ+i] * FRAYu[IRAY*MAX_NUM_FREQ+i]
* exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+i]-ARG)
/ (sqrt(2*M_PI)*SDEV*PCOSu[IRAY]*RRAY);
/* GRAB fix !!!!!!!!*/
/*PWR[i] = ZAPR * (exp(-ARG) / (sqrt(2*M_PI)*SDEV))
* FRAYu[IRAY*MAX_NUM_FREQ+i] * FRAYu[IRAY*MAX_NUM_FREQ+i]
* exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+i])/(RRAY * RRAY * 1000.0 * 1000.0);*/
};
/*printf("%f %f %f %f Pzdel=%f ZDEL=%f Normal=%f FRAYu=%f URAYu=%f RRAY=%f Loss=%f",
ARG, exp(-ARG),
(TRD[IRAY].PWRv * 1000.0 * 1000.0 / PCOSu[IRAY]),RRAY,
(TRD[IRAY].PWRv * 1000.0 * 1000.0 / PCOSu[IRAY])*RRAY, ZDEL,
exp(-ARG) / (sqrt(2*M_PI)*SDEV),
20.0*log10(FRAYu[IRAY*MAX_NUM_FREQ+0]),
10.0*log10(exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+0])),
-10.0*log10(RRAY * RRAY * 1000.0 * 1000.0),
10.0*log10( FRAYu[IRAY*MAX_NUM_FREQ+0] * FRAYu[IRAY*MAX_NUM_FREQ+0]
*exp(2.0*URAYu[IRAY*MAX_NUM_FREQ+0])/(RRAY * RRAY * 1000.0 * 1000.0)) );*/
/*Note: the bundle at caustic gets processed twice.*/
if( fabs(ZRAYu[IRAY]-ZVRTu1) <= 0 ||
fabs(ZRAYu[IRAY]-ZVRTu2) <= 0 )
for(i=0;i<(*Env).Nfreq;i=i+1) PWR[i]=0.5*PWR[i];
TOTPWR = 0.0;
for(i=0;i<(*Env).Nfreq;i=i+1) TOTPWR = TOTPWR + PWR[i];
if( TOTPWR > PWRMIN*
(*Env).Nfreq ){
/*-------------------------
Estimate the travel time.
-------------------------*/
TTRG = TRAYu[IRAY] + PSINu[IRAY]*ZREL;
/*-------------------------
Compute the weighted sum.
----------------
---------*/
*UPRSUM = *UPRSUM + TOTPWR * NUPRu[IRAY];
*LWRSUM = *LWRSUM + TOTPWR * NLWRu[IRAY];
*SRCSUM = *SRCSUM + TOTPWR * TRD[IRAY].ANG;
for(i=0;i<(*Env).Nfreq;i=i+1) PWRSUM[i] = PWRSUM[i] + PWR[i];
*TIMSUM = *TIMSUM + TOTPWR * TTRG;
for(i=0;i<(*Env).Nfreq;i=i+1) PHSSUM[i] = PHSSUM[i] + TOTPWR * ORAYu[IRAY];
*PCSSUM = *PCSSUM + TOTPWR * PCOSu[IRAY];
*PSNSUM = *PSNSUM + TOTPWR * PSINu[IRAY];
/*Test Carrying source angle*/
/*printf("Rang=%f acos=%f\n",180.0*TRD[IRAY].RANG/M_PI,180.0*acos(PCOSu[IRAY]/PTRG)/M_PI);*/
/**RCVSUM = *RCVSUM + TOTPWR * TRD[IRAY].RANG;*/
*RCVSUM = *RCVSUM + TOTPWR * acos(PCOSu[IRAY]/PTRG);
/*printf("IRAY=%d PWRSUM=%e TOTPWR=%e ang=%fR %fD RCVSUM=%e ",
IRAY,PWRSUM[0],TOTPWR,acos(PCOSu[IRAY]/PTRG),180.0*acos(PCOSu[IRAY]/PTRG)/M_PI,*RCVSUM);*/
/*-----------------------------
Modify the phase at caustics.
-----------------------------*/
NRFLo = NSRF + NBTM;
if(NCRSo-NRFLo > 0) NCSTo = NCRSo-NRFLo;
else NCSTo = 0;
for(i=0;i<(*Env).Nfreq;i=i+1)
PHSSUM[i] = PHSSUM[i] - PWR[i] * ((M_PI/2.0)*NCSTo);
/*---------------------------------
Set the first eigenray flag real.
---------------------------------*/
if( (ZBND2-ZTRG)*(ZTRG-ZBND1) >= 0 ) *IMAFLG = 0;
};
/*printf(" dB(PWR[0]) = %f dB(PWRSUM[0]) = %f loss = %f\n",
10.0*log10(PWR[0]),10.0*log10(PWRSUM[0]),
20.0*log10(TRD[IRAY].FRAY[0]) + 8.686 * TRD[IRAY].URAY[0] - 20.0*log10(1500.0 * TRD[IRAY].TRAY));*/
};
/*printf("\n");*/
};
}
</code>
and the second subprogram:
<code>
/*! \file WEG.c
\brief WEG (WAF Eigenray Generator)
\par
computes acoustic eigenrays in a two-dimensional environment
using Gaussian ray bundles.
\par
\author Phred
\author Weinberg
\date 6-9-2004
\test
WEGtest will display output of function for testing
*/
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <pthread.h>
#include "thread.h"
#include "WAFmutex.h"
#include "WAFmathBase.h"
#include "SIMdefines.h"
#include "envstructdefs.h"
#include "WEGStrDefs.h"
#include "WEGFunDefs.h"
/*!\brief
WEG (WAF Eigenray Generator)
\par
computes acoustic eigenrays in a two-dimensional environment
using Gaussian ray bundl
es.
\param Env a structer to hold enviromental data
\param eigen_ray_data a structer to hold returned eigen rays
\param WPP Paralla passed info
\param NumThreads Number of Threads (including the king)
\param TRD Test Ray Data
\param TRSD Test RAy Segment Data
\param debug debug flag
\note
More info is needed in this discription.
<HR>
\test
\par <tt><b>WEGtest</b></tt>
Test code for the <tt><b>WEG()</b></tt> function.
\par Command Line Arguments:
<tt><b>WEGtest</b></tt> [<em>#treads</em>]
<em>#treads</em>) number of threads, defaults to one if not spified.
\par Input Files:
<tt>SorRec.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
Line 2) Source range depth\n
Line 3) numbers of targets range depth\n
Line 5 +) target range\n
Line 7 +) target depth\n
Line 8 +) number of freq\n
Line 9 +) frequency\n
Line 11 +) min angle for test ray fan\n
Line 12 +) max angle for test ray fan\n
Line 13 +) angle step size for test ray fan\n
Line 15 +) Max number of surface bounces\n
Line 16 +) Max number of bottom bounces\n
Line 17 +) Max number of upper verticys\n
Line 18 +) Max number of lower verticys
<tt>SeaState.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
<tt>SSP.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
<tt>Bottom.in</tt>\n
located in (<em>TOP</em>)<tt>/TestJigs/Inputs/LIB/Acoustics/</tt>,\n
file need to be documented.
\par Code:
Main for test code is in <tt>WEGtest.c</tt>
<HR>
WEG (WAF Eigenray Generator)
*/
void WEG( ENV_DEF *Env, EIGEN_RAY *eigen_ray_data, WEG_P_PASS *WPP, int NumThreads,
TEST_RAY_DATA *TRD, TEST_RAY_SEG_DATA *TRSD, LOGICAL debug ){
int i;
int IRb=0;
int IZTRG=0;
/*test ray fan in RADS*/
double ANGMINi, ANGMAXi, ANGDELi;
/*max range in km*/
double RNGMAXi;
int NANGv;
double PSRC, ATNSRC[MAX_NUM_FREQ];
double P2SRC, GrSRC, GzSRC;
/* Max range */
RNGMAXi=(*Env).Target_Range[(*Env).NtargetR-1];
/* At CloseRange set min*/
if(0.005 > RNGMAXi) RNGMAXi = 0.005;
/*-----------------------------------------------
Compute the source sound slowness and gradient.
-----------------------------------------------*/
SND_SLW2( 1, 0.0, &((*Env).Source_Depth), &P2SRC, &GrSRC, &GzSRC,
&IRb, Env );
PSRC = sqrt(P2SRC);
/*--------------------------------
Generate test ray source angles.
--------------------------------*/
/*Fan of test angles*/
ANGMINi = (*Env).Min_Ang;
ANGMAXi = (*Env).Max_Ang;
ANGDELi = (*Env).Del_Ang;
SRC_ANG2( (*Env).Source_Depth, P2SRC, &NANGv, TRD,
ANGMINi, ANGMAXi, ANGDELi, &IRb, Env, debug);
/*-------------------------------------------
Compute the source attenuation coefficient.
-------------------------------------------*/
for(i=0;i<(*Env).Nfreq;i=i+1){
ATNSRC[i] = 0.0;
VLM_ATN3( (*Env).frequency_Hz[i], &(ATNSRC[i]), 0, Env );
};
/*-------------------------------
Compute.
-------------------------------*/
eigen_ray_data[0].Nfreq = (*Env).Nfreq;
eigen_ray_data[0].ERAY_NDX = 0; /* No rays yet!*/
PRC_BRN2( RNGMAXi, &(eigen_ray_data), WPP, NumThreads, &IRb,
&IZTRG, ATNSRC, GzSRC, NANGv, TRD, TRSD, PSRC, Env, debug);
}
Now these subprograms come from separate computer files. Notice that each has global variables (variables that are defined outside of the program code). Also, notice that the subprogram's global declarations are different. They define global variables to a main subprogram, but they are slightly different. The include files on each subprogram are different.
Now how can this be? I understand that each separate computer file with a subprogram, must define global variables, but at least all of the subprograms global variable definitions should be the same. But here they are different. Why is what I want to know.
Newport_j