GetAMCoeffsTest.c

Go to the documentation of this file.
00001 /*
00002  * 
00003  * Copyright (C) 2006 Reinhard Prix
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with with program; see the file COPYING. If not, write to the 
00017  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
00018  *  MA  02111-1307  USA
00019  */
00020 
00021 /*********************************************************************************/
00022 /** \author Reinhard Prix
00023  * \file 
00024  * \brief Test for LALGetAMCoeffs(): compare results to older, well-tested
00025  * (but less efficient, and harder to understand) function  LALComputeAM()
00026  *                                                                          
00027  *********************************************************************************/
00028 #include <math.h>
00029 #include <sys/times.h>
00030 
00031 #include <lal/ComputeFstat.h>
00032 #include <lal/LALBarycenter.h>
00033 #include <lal/LALInitBarycenter.h>
00034 #include <lal/AVFactories.h>
00035 
00036 NRCSID (GETAMCOEFFSTEST, "$Id: GetAMCoeffsTest.c,v 1.5 2006/10/27 15:18:20 reinhard Exp $");
00037 
00038 /** \name Error codes */
00039 /*@{*/
00040 #define GETAMCOEFFSTEST_ENORM   0
00041 #define GETAMCOEFFSTEST_ESUB    1
00042 
00043 #define GETAMCOEFFSTEST_MSGENORM    "Normal exit"
00044 #define GETAMCOEFFSTEST_MSGESUB     "Subroutine failed"
00045 /*@}*/
00046 
00047 
00048 #define RELERROR(x, y) fabs( 2.0 * ((x) - (y)) / ( (x) + (y) ) )
00049 #define SQ(x) ( (x) * (x) )
00050 #define MYMAX(a,b) ( (a) > (b) ? (a) : (b) )
00051 
00052 /*********************************************************************/
00053 /* Macros for printing errors & testing subroutines (from Creighton) */
00054 /*********************************************************************/
00055 
00056 #define ERROR( code, msg, statement )                                \
00057 do {                                                                 \
00058   if ( lalDebugLevel & LALERROR )                                    \
00059     LALPrintError( "Error[0] %d: program %s, file %s, line %d, %s\n" \
00060                    "        %s %s\n", (code), *argv, __FILE__,       \
00061               __LINE__, GETAMCOEFFSTEST, statement ? statement :  \
00062                    "", (msg) );                                      \
00063 } while (0)
00064 
00065 #define INFO( statement )                                            \
00066 do {                                                                 \
00067   if ( lalDebugLevel & LALINFO )                                     \
00068     LALPrintError( "Info[0]: program %s, file %s, line %d, %s\n"     \
00069                    "        %s\n", *argv, __FILE__, __LINE__,        \
00070               GETAMCOEFFSTEST, (statement) );                     \
00071 } while (0)
00072 
00073 #define SUB( func, statusptr )                                       \
00074 do {                                                                 \
00075   if ( (func), (statusptr)->statusCode ) {                           \
00076     ERROR( GETAMCOEFFSTEST_ESUB, GETAMCOEFFSTEST_MSGESUB,            \
00077            "Function call \"" #func "\" failed:" );                  \
00078     return GETAMCOEFFSTEST_ESUB;                                     \
00079   }                                                                  \
00080 } while (0)
00081 
00082 
00083 static const LALStatus empty_status;
00084 static const AMCoeffsParams empty_AMCoeffsParams;
00085 static const AMCoeffs empty_AMCoeffs;
00086 
00087 extern int lalDebugLevel;
00088 
00089 /** Very simple test: pick random skyposition, compute a_i, b_i using
00090  *  once LALComputeAM() and once LALGetAMCoeffs(), and look at the errors
00091  *  sum_i (a_i - a_i')^2
00092  */
00093 int main(int argc, char *argv[])
00094 {
00095   LALStatus status = empty_status;
00096 
00097   LIGOTimeGPS startTime = {714180733, 0};
00098   REAL8 duration = 180000;      /* 50 hours */
00099   REAL8 Tsft = 1800;            /* assume 30min SFTs */
00100   LIGOTimeGPSVector *timestamps = NULL;
00101   DetectorStateSeries *detStates = NULL;
00102   SkyPosition skypos = empty_SkyPosition;
00103   EphemerisData edat = empty_EphemerisData;
00104   BarycenterInput baryinput = empty_BarycenterInput;
00105   LALDetector *det = NULL;
00106   AMCoeffs AMold = empty_AMCoeffs, AMnew = empty_AMCoeffs;
00107   REAL8 alpha, delta;
00108   AMCoeffsParams amParams = empty_AMCoeffsParams;
00109   EarthState earth;
00110   UINT4 i;
00111   REAL8 maxerr_a, maxerr_b, averr_a, averr_b;
00112   REAL8 tolerance = 1e-2;       /* be generous: allow 1% error */
00113   struct tms buf;
00114 
00115   const CHAR *sites[] = {"H1", "L1", "V2", "G1", "T1" };
00116   UINT4 pickedSite;
00117   char earthEphem[] = "earth00-04.dat";
00118   char sunEphem[] = "sun00-04.dat";
00119 
00120   lalDebugLevel = 0;
00121   if ( argc == 2 && !strcmp(argv[1], "-v1") )
00122     lalDebugLevel = 1;
00123 
00124 
00125   /* init random-generator */
00126   srand ( times(&buf) );
00127 
00128   /* ----- init ephemeris ----- */
00129   edat.ephiles.earthEphemeris = earthEphem;
00130   edat.ephiles.sunEphemeris = sunEphem;
00131   edat.leap = 0;
00132   SUB ( LALInitBarycenter(&status, &edat), &status);
00133 
00134   /* ----- get timestamps ----- */
00135   SUB ( LALMakeTimestamps ( &status, &timestamps, startTime, duration, Tsft ), &status );
00136 
00137   /* ----- allocate memory for AM-coeffs ----- */
00138   AMold.a = XLALCreateREAL4Vector ( timestamps->length );
00139   AMold.b = XLALCreateREAL4Vector ( timestamps->length );
00140   AMnew.a = XLALCreateREAL4Vector ( timestamps->length );
00141   AMnew.b = XLALCreateREAL4Vector ( timestamps->length );
00142 
00143   /* ----- pick detector-site at random ----- */
00144   pickedSite = floor( 5 * (1.0 * rand() / (RAND_MAX + 1.0) ) );  /* int in [0,5) */
00145   if ( ( det = XLALGetSiteInfo ( sites[pickedSite] )) == NULL ) 
00146     {
00147       LALPrintError ("\nCall to XLALGetSiteInfo() has failed for site = '%s'... \n\n", 
00148                      sites[pickedSite]);
00149       return GETAMCOEFFSTEST_ESUB;
00150     }
00151 
00152   /* ----- pick skyposition at random ----- */
00153   alpha = LAL_TWOPI * (1.0 * rand() / ( RAND_MAX + 1.0 ) );  /* uniform in [0, 2pi) */
00154   delta = LAL_PI_2 - acos ( 1 - 2.0 * rand()/RAND_MAX );        /* sin(delta) uniform in [-1,1] */
00155 
00156   /* ===== compute AM-coeffs the 'old way': ===== */
00157   baryinput.site.location[0] = det->location[0]/LAL_C_SI;
00158   baryinput.site.location[1] = det->location[1]/LAL_C_SI;
00159   baryinput.site.location[2] = det->location[2]/LAL_C_SI;
00160   baryinput.alpha = alpha;
00161   baryinput.delta = delta;
00162   baryinput.dInv = 0.e0;
00163 
00164   /* amParams structure to compute a(t) and b(t) */
00165   amParams.das = (LALDetAndSource *)LALMalloc(sizeof(LALDetAndSource));
00166   amParams.das->pSource = (LALSource *)LALMalloc(sizeof(LALSource));
00167   amParams.baryinput = &baryinput;
00168   amParams.earth = &earth; 
00169   amParams.edat = &edat;
00170   amParams.das->pDetector = det;
00171   amParams.das->pSource->equatorialCoords.longitude = alpha;
00172   amParams.das->pSource->equatorialCoords.latitude = delta;
00173   amParams.das->pSource->orientation = 0.0;
00174   amParams.das->pSource->equatorialCoords.system = COORDINATESYSTEM_EQUATORIAL;
00175   amParams.polAngle = 0; 
00176   amParams.leapAcc = LALLEAPSEC_STRICT;
00177 
00178   SUB (LALComputeAM ( &status, &AMold, timestamps->data, &amParams), &status); 
00179 
00180   /* ===== compute AM-coeffs the 'new way' using LALGetAMCoeffs() */
00181 
00182   /* ----- get detector-state series ----- */
00183   SUB ( LALGetDetectorStates (&status, &detStates, timestamps, det, &edat, 0 ), &status );
00184 
00185   skypos.system = COORDINATESYSTEM_EQUATORIAL;
00186   skypos.longitude = alpha;
00187   skypos.latitude = delta;
00188 
00189   SUB ( LALGetAMCoeffs ( &status, &AMnew, detStates, skypos ), &status );
00190 
00191 
00192   /* ===== analyse relative error ===== */
00193   maxerr_a = maxerr_b = averr_a = averr_b = 0;
00194   for ( i=0; i < timestamps->length; i ++ )
00195     {
00196       REAL8 thisErr;
00197       thisErr = sqrt( SQ ( AMold.a->data[i] -  AMnew.a->data[i] ) / AMold.A );
00198       averr_a += thisErr;
00199       maxerr_a = MYMAX( thisErr, maxerr_a );
00200       thisErr = sqrt( SQ ( AMold.b->data[i] - AMnew.b->data[i] ) / AMold.B );
00201       averr_b += thisErr;
00202       maxerr_b = MYMAX( thisErr, maxerr_b );
00203     }
00204   averr_a /= timestamps->length;
00205   averr_b /= timestamps->length;
00206 
00207   if ( lalDebugLevel ) 
00208     {
00209       printf ("Parameters: IFO = %s, skypos = [%g, %g]\n", sites[pickedSite], alpha, delta );
00210       printf ("Maximal relative errors: maxerr(a) = %g %%, maxerr(b) = %g %% \n", 
00211               100.0 * maxerr_a, 100.0 * maxerr_b);
00212       printf ("Average relative errors: averr(a)  = %g %%, averr(b)  = %g %% \n",
00213               100.0 * averr_a, 100.0 * averr_b );
00214     }
00215   else
00216     printf ("%d %g %g %g %g %g %g \n", pickedSite, alpha, delta, averr_a, averr_b, maxerr_a, maxerr_b);
00217 
00218   if ( (averr_a > tolerance) || (averr_b > tolerance) || (maxerr_a > tolerance) ||(maxerr_b > tolerance))
00219     {
00220       LALPrintError ("Maximal error-tolerance of %g %% was exceeded!\n", 100.0 * tolerance );
00221       return 1;
00222     }
00223 
00224   /* ----- free memory ----- */
00225   XLALDestroyTimestampVector ( timestamps );
00226   XLALDestroyREAL4Vector ( AMold.a );
00227   XLALDestroyREAL4Vector ( AMold.b );
00228   XLALDestroyREAL4Vector ( AMnew.a );
00229   XLALDestroyREAL4Vector ( AMnew.b );
00230   LALFree ( det );
00231   XLALDestroyDetectorStateSeries ( detStates );
00232   LALFree ( amParams.das->pSource );
00233   LALFree ( amParams.das );
00234   
00235   LALFree(edat.ephemE);
00236   LALFree(edat.ephemS);
00237 
00238   
00239   LALCheckMemoryLeaks();
00240 
00241   return 0;     /* OK */
00242 
00243 } /* main() */
00244 
00245 

Generated on Sat Oct 11 02:31:36 2008 for LAL by  doxygen 1.5.2