PtoleMetricTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton, Ian Jones, Benjamin Owen
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /************************************ <lalVerbatim file="PtoleMetricTestCV">
00021 Author: Owen, B. J.,   Jones, D. I.
00022 $Id: PtoleMetricTest.c,v 1.14 2007/06/08 14:41:52 bema Exp $
00023 ********************************************************** </lalVerbatim> */
00024 
00025 /**************************************************************** <lalLaTeX>
00026 
00027 \subsection{Program \texttt{PtoleMetricTest}}
00028 \label{ss:PtoleMetricTest}
00029 
00030 Tests the \texttt{PtoleMetric()} function.
00031 
00032 \subsubsection*{Usage}
00033 \begin{verbatim}
00034 PtoleMetricTest
00035 \end{verbatim}
00036 
00037 \subsubsection*{Description}
00038 
00039 This program computes metric components using \texttt{PtoleMetric()}. The
00040 ordering of the components is $(f_0, \alpha, \delta, f_1, \ldots)$ for the
00041 unprojected metric, and $(\alpha, \delta, f_1, \ldots)$ for the metric with
00042 $f_0$ projected out.
00043 
00044 With no options, this program displays metric components for a single point
00045 in parameter space for the default integration time (see the \texttt{-t}
00046 option).
00047 
00048 The \texttt{-b} option sets the beginning time of integration to the option
00049 argument. (Default is $0$ seconds)
00050 
00051 The \texttt{-e} option causes the program to showcase error messages when
00052 given bad parameter values, etc.
00053 
00054 The \texttt{-m} option sets the mismatch (default is $0.02$).
00055 
00056 The \texttt{-p} option is provided for users who wish to view the
00057 power mismatch contours provided by the \texttt{-x} option (see below)
00058 but don't have xmgrace installed.  All necessary data is simply
00059 written to a file ``nongrace.data''; it's probably best to look at the
00060 code to see the exact format.  The user should then write a small
00061 script to convert the data into the format appropriate to their
00062 favorite graphics package.
00063 
00064 The \texttt{-t} option sets the duration of integration in seconds. The default
00065 is $10^5$ seconds, which is chosen because it is about one day but not an
00066 integer multiple of any astronomically important timescale.
00067 
00068 The \texttt{-x} option produces a graph of the 2\% power mismatch
00069 contours on the sky. Dimensions of the ellipses have been exaggerated
00070 by a factor of \texttt{MAGNIFY} (specified within the code) for
00071 legibility. The purpose of the graph is to get a feel for how ellipses
00072 are varying across parameter space. Note that this option makes a
00073 system call to the \texttt{xmgrace} program, and will not work if that
00074 program is not installed on your system.
00075 
00076 
00077 \subsubsection*{Exit Codes}
00078 ************************************************ </lalLaTeX><lalErrTable> */
00079 #define PTOLEMETRICTESTC_EMEM 1
00080 #define PTOLEMETRICTESTC_ESUB 2
00081 #define PTOLEMETRICTESTC_ESYS 3
00082  
00083 #define PTOLEMETRICTESTC_MSGEMEM "memory (de)allocation error"
00084 #define PTOLEMETRICTESTC_MSGESUB "subroutine failed"
00085 #define PTOLEMETRICTESTC_MSGESYS "system call failed"
00086 /************************************************** </lalErrTable><lalLaTeX>
00087 \subsubsection*{Algorithm}
00088 
00089 \subsubsection*{Uses}
00090 
00091 \begin{verbatim}
00092 lalDebugLevel
00093 LALCheckMemoryLeaks()
00094 LALCreateVector()
00095 LALDestroyVector()
00096 LALDCreateVector()
00097 LALDDestroyVector()
00098 LALProjectMetric()
00099 LALPtoleMetric()
00100 xmgrace
00101 \end{verbatim}
00102 
00103 \subsubsection*{Notes}
00104 
00105 The graph shows that the patches' overall area is independent of right
00106 ascension but that those near the equator rotate, which adds a new
00107 complication to the tiling.
00108 
00109 \vfill{\footnotesize\input{PtoleMetricTestCV}}
00110 
00111 ************************************************************* </lalLaTeX> */
00112 
00113 #include <math.h>
00114 #include <stdio.h>
00115 #include <stdlib.h>
00116 #include <string.h>
00117 #include <sys/types.h>
00118 #include <sys/stat.h>
00119 #include <unistd.h>
00120 #include <lal/AVFactories.h>
00121 #include <lal/LALMalloc.h>
00122 #include <lal/LALStdlib.h>
00123 #include <lal/PtoleMetric.h>
00124 #include <lal/StackMetric.h>
00125 extern char *optarg;
00126 
00127 NRCSID( PTOLEMETRICTESTC, "$Id" );
00128 
00129 #define DEFAULT_DURATION 1e5 /* seconds */
00130 #define NUM_SPINDOWN 0       /* Number of spindown parameters */
00131 #define SPOKES 30
00132 #define MAGNIFY 1.0           /* Magnification factor of ellipses */
00133 
00134 int lalDebugLevel = 0;
00135 
00136 int main( int argc, char *argv[] ) {
00137   static LALStatus status;          /* Status structure */
00138   PtoleMetricIn    in;              /* PtoleMetric() input structure */
00139   REAL4            mismatch;        /* mismatch threshold of mesh */
00140   REAL4Vector     *spindown = NULL; /* Spindown parameters */
00141   REAL8Vector     *metric = NULL;   /* Parameter-space metric */
00142   int              j, k;            /* Parameter-space indices */
00143   int              opt;             /* Command-line option. */
00144   BOOLEAN          test = 0;        /* Whether we showcase error messages */
00145   BOOLEAN          grace = 0;       /* Whether or not we use xmgrace */
00146   BOOLEAN          nongrace = 0;    /* Whether or not to output data to file*/
00147   int              ra, dec, i;      /* Loop variables for xmgrace option */
00148   FILE            *pvc;             /* Temporary file for xmgrace option */
00149   FILE            *fnongrace;       /* File contaning ellipse coordinates */
00150 
00151 
00152   /* Default values. */
00153   in.duration = DEFAULT_DURATION;
00154   in.epoch.gpsSeconds = 0.0;
00155   in.epoch.gpsNanoSeconds = 0.0;
00156   mismatch = 0.02;
00157 
00158 
00159   /* Parse options. */
00160   while ((opt = getopt( argc, argv, "b:em:pt:x" )) != -1) {
00161     switch (opt) {
00162     case 'b':
00163       in.epoch.gpsSeconds = atof( optarg );
00164       break;
00165     case 'e':
00166       test = 1;
00167       lalDebugLevel = 1;
00168       break;
00169     case 'm':
00170       mismatch = atof( optarg );
00171       break;
00172     case 'p':
00173       nongrace = 1;
00174       break;
00175     case 't':
00176       in.duration = atof( optarg );
00177       break;
00178     case 'x':
00179       grace = 1;
00180       break;
00181     }
00182   }
00183 
00184   if (test) {
00185     printf("\nTesting bad I/O structures...\n");
00186     LALPtoleMetric( &status, metric, NULL );
00187     printf("\nTesting bad sky position...\n");
00188     in.position.longitude = 1e10;
00189     LALPtoleMetric( &status, metric, &in );
00190   }
00191   /* Good sky position: ra=19h13m, dec=+16deg, sound familiar? */
00192   in.position.system = COORDINATESYSTEM_EQUATORIAL;
00193   in.position.longitude = 288.25*LAL_PI_180;
00194   in.position.latitude = 16*LAL_PI_180;
00195 
00196   if (test){
00197     printf("\nTesting bad spindown parameters...\n");
00198     LALPtoleMetric( &status, metric, &in );
00199   }
00200   /* Good spindown parameters: all zero (if we have any) */
00201   if( NUM_SPINDOWN > 0 )
00202   {
00203     LALCreateVector( &status, &spindown, NUM_SPINDOWN );
00204     if( status.statusCode )
00205     {
00206       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00207               PTOLEMETRICTESTC_MSGEMEM );
00208       return PTOLEMETRICTESTC_EMEM;
00209     }
00210     for (j=0; (UINT4)j<spindown->length; j++)
00211       spindown->data[j] = 0;
00212     in.spindown = spindown;
00213   }
00214   else
00215     in.spindown = NULL;
00216 
00217   
00218 
00219   if (test) {
00220     REAL4 old_duration = in.duration;
00221     printf("\nTesting bad duration...\n");
00222     in.duration = -1;
00223     LALPtoleMetric( &status, metric, &in );
00224     in.duration = old_duration;
00225   }
00226 
00227   if (test) {
00228     printf("\nTesting bad maximum frequency...\n");
00229     in.maxFreq = 0;
00230     LALPtoleMetric( &status, metric, &in );
00231   }
00232   /* Good maximum frequency */
00233   in.maxFreq = 1e3;
00234 
00235   /* JC: DISABLE THIS
00236   if (test) {
00237     printf("\nTesting bad detector site...\n");
00238     in.site->frDetector.vertexLatitudeRadians = 100 * LAL_PI / 180;
00239     LALPtoleMetric( &status, metric, &in );
00240   }
00241   */
00242   /* Use GEO600 site. */
00243   in.site = &lalCachedDetectors[LALDetectorIndexGEO600DIFF];
00244 
00245   if (test) {
00246     printf("\nTesting bad output contents...\n");
00247     LALPtoleMetric( &status, metric, &in );
00248   }
00249   /* Allocate storage for output metric. */
00250   LALDCreateVector( &status, &metric, (3+NUM_SPINDOWN)*(4+NUM_SPINDOWN)/2 );
00251   if( status.statusCode )
00252   {
00253     printf( "%s line %d: %s\n", __FILE__, __LINE__, PTOLEMETRICTESTC_MSGEMEM );
00254     return PTOLEMETRICTESTC_EMEM;
00255   }
00256 
00257   /* Print results if no options. */
00258   if (argc == 1) {
00259 
00260     printf( "\nValid results for duration %e seconds:\n", in.duration );
00261     LALPtoleMetric( &status, metric, &in );
00262     if( status.statusCode )
00263     {
00264       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00265               PTOLEMETRICTESTC_MSGESUB );
00266       return PTOLEMETRICTESTC_ESUB;
00267     }
00268     for (j=0; j<=2+NUM_SPINDOWN; j++) {
00269       for (k=0; k<=j; k++)
00270         printf( "  %+.3e", metric->data[k+j*(j+1)/2] );
00271       printf("\n");
00272     }
00273     LALProjectMetric( &status, metric, 0 );
00274     if( status.statusCode )
00275     {
00276       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00277               PTOLEMETRICTESTC_MSGESUB );
00278       return PTOLEMETRICTESTC_ESUB;
00279     }
00280     printf( "With f0 projected out:\n" );
00281     for (j=1; j<=2+NUM_SPINDOWN; j++) {
00282       for (k=1; k<=j; k++)
00283         printf( "  %+.3e", metric->data[k+j*(j+1)/2] );
00284       printf( "\n" );
00285     }
00286 
00287 #if 0
00288     printf( "\nValid results for duration 1e7 seconds:\n" );
00289     in.duration = 1e7;
00290     LALPtoleMetric( &status, metric, &in );
00291     if( status.statusCode )
00292     {
00293       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00294               PTOLEMETRICTESTC_MSGESUB );
00295       return PTOLEMETRICTESTC_ESUB;
00296     }
00297     for (j=0; j<=2+NUM_SPINDOWN; j++) {
00298       for (k=0; k<=j; k++)
00299         printf( "  %+.3e", metric->data[k+j*(j+1)/2] );
00300       printf("\n");
00301     }
00302     LALProjectMetric( &status, metric, 0 );
00303     if( status.statusCode )
00304     {
00305       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00306               PTOLEMETRICTESTC_MSGESUB );
00307       return PTOLEMETRICTESTC_ESUB;
00308     }
00309     printf( "With f0 projected out:\n" );
00310     for (j=1; j<=2+NUM_SPINDOWN; j++) {
00311       for (k=1; k<=j; k++)
00312         printf( "  %+.3e", metric->data[k+j*(j+1)/2] );
00313       printf( "\n" );
00314     }
00315 #endif
00316   } /* if (argc...) */
00317 
00318   /* Here is the code that uses xmgrace with the -x option, */
00319   /* and outputs data to a file with the -t option. */
00320   if (grace || nongrace) {
00321 
00322     /* Take care of preliminaries. */
00323     if(grace)
00324       {
00325         pvc = popen( "xmgrace -pipe", "w" );
00326         if( !pvc )
00327           {
00328             printf( "%s line %d: %s\n", __FILE__, __LINE__,
00329                     PTOLEMETRICTESTC_MSGESYS );
00330             return PTOLEMETRICTESTC_ESYS;
00331           }
00332         fprintf( pvc, "@xaxis label \"Right ascension (degrees)\"\n" );
00333         fprintf( pvc, "@yaxis label \"Declination (degrees)\"\n" );
00334       }
00335     if(nongrace)
00336       {
00337         fnongrace = fopen( "nongrace.data", "w" );
00338         if( !fnongrace )
00339           {
00340             printf( "%s line %d: %s\n", __FILE__, __LINE__,
00341                     PTOLEMETRICTESTC_MSGESYS );
00342             return PTOLEMETRICTESTC_ESYS;
00343           }
00344       }
00345 
00346     /* Step around the sky: a grid in ra and dec. */
00347     j = 0;
00348     for (dec=80; dec>0; dec-=10) {
00349       for (ra=0; ra<=90; ra+=15) {
00350         float gaa, gad, gdd, angle, smaj, smin;
00351  
00352         /* Get the metric at this ra, dec. */
00353         in.position.longitude = ra*LAL_PI_180;
00354         in.position.latitude = dec*LAL_PI_180;
00355         LALPtoleMetric( &status, metric, &in );
00356         if( status.statusCode )
00357         {
00358           printf( "%s line %d: %s\n", __FILE__, __LINE__,
00359                   PTOLEMETRICTESTC_MSGESUB );
00360           return PTOLEMETRICTESTC_ESUB;
00361         }
00362 
00363         /*  Project metric: */ 
00364         LALProjectMetric( &status, metric, 0 );
00365         if( status.statusCode )
00366           {
00367           printf( "%s line %d: %s\n", __FILE__, __LINE__,
00368                   PTOLEMETRICTESTC_MSGESUB );
00369           return PTOLEMETRICTESTC_ESUB;
00370           }
00371 
00372         /* Rename \gamma_{\alpha\alpha}. */
00373         gaa = metric->data[2];
00374         /* Rename \gamma_{\alpha\delta}. */
00375         gad = metric->data[4];
00376         /* Rename \gamma_{\delta\delta}. */
00377         gdd = metric->data[5];
00378         /* Semiminor axis from larger eigenvalue of metric. */
00379         smin = gaa+gdd + sqrt( pow(gaa-gdd,2) + pow(2*gad,2) );
00380         smin = sqrt(2*mismatch/smin);
00381         /* Semiminor axis from smaller eigenvalue of metric. */
00382         smaj = gaa+gdd - sqrt( pow(gaa-gdd,2) + pow(2*gad,2) );
00383         smaj = sqrt(2*mismatch/smaj);
00384         /* Angle of semimajor axis with "horizontal" (equator). */
00385         angle = atan2( gad, mismatch/smaj/smaj-gdd );
00386         if (angle <= -LAL_PI_2) angle += LAL_PI;
00387         if (angle > LAL_PI_2) angle -= LAL_PI;
00388  
00389         if(grace)
00390           {
00391             /* Print set header. */
00392             fprintf( pvc, "@s%d color (0,0,0)\n", j );
00393             fprintf( pvc, "@target G0.S%d\n@type xy\n", j++ );
00394             /* Print center of patch. */
00395             fprintf( pvc, "%16.8g %16.8g\n", (float)ra, (float)dec );
00396           }
00397         if(nongrace)
00398           /* Print center of patch. */
00399           fprintf( fnongrace, "%16.8g %16.8g\n", (float)ra, (float)dec );
00400         /* Loop around patch ellipse. */
00401         for (i=0; i<=SPOKES; i++) {
00402           float c, r;
00403           c = LAL_TWOPI*i/SPOKES;
00404           r = MAGNIFY*LAL_180_PI*smaj*smin/sqrt( pow(smaj*sin(c),2)
00405               + pow(smin*cos(c),2) );
00406           if(grace)
00407             fprintf( pvc, "%e %e\n", ra+r*cos(angle-c), dec+r*sin(angle-c) );
00408           if(nongrace)
00409             fprintf( fnongrace, "%e %e\n", ra+r*cos(angle-c), 
00410                      dec+r*sin(angle-c) );
00411 
00412         } /* for (a...) */
00413  
00414       } /* for (ra...) */
00415     } /* for (dec...) */
00416     if(grace)
00417       fclose( pvc );
00418     if(nongrace)
00419       fclose( fnongrace );
00420   } /* if (grace || nongrace) */
00421 
00422   printf("\nCleaning up and leaving...\n");
00423   if( spindown )
00424   {
00425     LALDestroyVector( &status, &spindown );
00426     if( status.statusCode )
00427     {
00428       printf( "%s line %d: %s\n", __FILE__, __LINE__,
00429               PTOLEMETRICTESTC_MSGEMEM );
00430       return PTOLEMETRICTESTC_EMEM;
00431     }
00432   }
00433   LALDDestroyVector( &status, &metric );
00434   if( status.statusCode )
00435   {
00436     printf( "%s line %d: %s\n", __FILE__, __LINE__,
00437             PTOLEMETRICTESTC_MSGEMEM );
00438     return PTOLEMETRICTESTC_EMEM;
00439   }
00440   LALCheckMemoryLeaks();
00441   return 0;
00442 } /* main() */

Generated on Sat Sep 6 03:07:24 2008 for LAL by  doxygen 1.5.2