00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <math.h>
00071
00072
00073
00074
00075 #include <lal/LALStdlib.h>
00076 #include <lal/FitToPulsar.h>
00077
00078
00079 NRCSID( FITTOPULSARC, "$Id: FitToPulsarTest.c,v 1.4 2007/06/08 14:41:52 bema Exp $" );
00080
00081
00082 #define INICHISQU 1.e50
00083 #define INIMAXEH 0
00084 #define INIMINEH 1e10
00085
00086
00087
00088
00089
00090
00091
00092 void
00093 LALCoarseFitToPulsar ( LALStatus *status,
00094 CoarseFitOutput *output,
00095 CoarseFitInput *input,
00096 CoarseFitParams *params )
00097
00098
00099 {
00100
00101
00102 UINT4 n,i;
00103 REAL8 psi;
00104 REAL8 h0,h;
00105 REAL8 cosIota;
00106 REAL8 phase;
00107 REAL8 chiSquare;
00108 LALDetector detector;
00109 LALSource pulsar;
00110 LALDetAndSource detAndSource;
00111 LALDetAMResponse computedResponse;
00112 REAL4Vector *Fp, *Fc;
00113 REAL8Vector *var;
00114 REAL8 cos2phase,sin2phase;
00115 COMPLEX16 Xp, Xc;
00116 REAL8 Y, cosIota2;
00117 COMPLEX16 A,B;
00118 REAL8 sumAA, sumBB, sumAB;
00119 REAL8 h0BestFit=0,phaseBestFit=0, psiBestFit=0, cosIotaBestFit=0;
00120 REAL8 minChiSquare;
00121 COMPLEX16 eh0;
00122 REAL8 oldMinEh0, oldMaxEh0, weh0;
00123 UINT4 iH0, iCosIota, iPhase, iPsi, arg;
00124 LALGPSandAcc pGPSandAcc;
00125
00126
00127 INITSTATUS(status, "LALCoarseFitToPulsar", FITTOPULSARC);
00128 ATTATCHSTATUSPTR(status);
00129
00130
00131 ASSERT(output != (CoarseFitOutput *)NULL, status,
00132 FITTOPULSARH_ENULLOUTPUT, FITTOPULSARH_MSGENULLOUTPUT);
00133
00134 ASSERT(params != (CoarseFitParams *)NULL, status,
00135 FITTOPULSARH_ENULLPARAMS, FITTOPULSARH_MSGENULLPARAMS);
00136
00137 ASSERT(input != (CoarseFitInput *)NULL, status,
00138 FITTOPULSARH_ENULLINPUT, FITTOPULSARH_MSGENULLINPUT);
00139
00140 ASSERT(input->B->length == input->var->length, status,
00141 FITTOPULSARH_EVECSIZE , FITTOPULSARH_MSGEVECSIZE );
00142
00143
00144
00145 n = input->B->length;
00146
00147 for (i = 0; i < n; i++)
00148 ASSERT(input->var->data[i].re > 0.0 && input->var->data[i].im > 0.0, status,
00149 FITTOPULSARH_EVAR, FITTOPULSARH_MSGEVAR);
00150
00151 detector = params->detector;
00152 detAndSource.pDetector = &detector;
00153
00154 pulsar = params->pulsarSrc;
00155
00156
00157
00158 Fp = NULL;
00159 LALSCreateVector(status->statusPtr, &Fp, n);
00160 Fp->length = n;
00161
00162 Fc = NULL;
00163 LALSCreateVector(status->statusPtr, &Fc, n);
00164 Fc->length = n;
00165
00166 var = NULL;
00167 LALDCreateVector(status->statusPtr, &var, n);
00168 var->length = n;
00169
00170
00171 minChiSquare = INICHISQU;
00172 oldMaxEh0 = INIMAXEH;
00173 oldMinEh0 = INIMINEH;
00174
00175
00176 for (i=0;i<n;i++)
00177 {
00178 var->data[i] = input->var->data[i].re + input->var->data[i].im;
00179 }
00180
00181
00182 for (iPsi = 0; iPsi < params->meshPsi[2]; iPsi++)
00183 {
00184 psi = params->meshPsi[0] + iPsi*params->meshPsi[1];
00185 pulsar.orientation = psi;
00186 detAndSource.pSource = &pulsar;
00187
00188 pGPSandAcc.accuracy = 1.0;
00189
00190 for (i=0;i<n;i++)
00191 {
00192 pGPSandAcc.gps = input->t[i];
00193 LALComputeDetAMResponse(status->statusPtr, &computedResponse,&detAndSource, &pGPSandAcc);
00194 Fp->data[i] = (REAL8)computedResponse.plus;
00195 Fc->data[i] = (REAL8)computedResponse.cross;
00196 }
00197
00198 for (iPhase = 0; iPhase < params->meshPhase[2]; iPhase++)
00199 {
00200 phase = params->meshPhase[0] + iPhase*params->meshPhase[1];
00201 cos2phase = cos(2.0*phase);
00202 sin2phase = sin(2.0*phase);
00203 for (iCosIota = 0; iCosIota < params->meshCosIota[2]; iCosIota++)
00204 {
00205 cosIota = params->meshCosIota[0] + iCosIota*params->meshCosIota[1];
00206 cosIota2 = 1.0 + cosIota*cosIota;
00207 Xp.re = cosIota2 * cos2phase;
00208 Xp.im = cosIota2 * sin2phase;
00209
00210 Y = 2.0*cosIota;
00211
00212 Xc.re = Y*sin2phase;
00213 Xc.im = -Y*cos2phase;
00214
00215 sumAB = 0.0;
00216 sumAA = 0.0;
00217 sumBB = 0.0;
00218 eh0.re=0.0;
00219 eh0.im=0.0;
00220
00221 for (i = 0; i < n; i++)
00222 {
00223 B.re = input->B->data[i].re;
00224 B.im = input->B->data[i].im;
00225
00226 A.re = Fp->data[i]*Xp.re + Fc->data[i]*Xc.re;
00227 A.im = Fp->data[i]*Xp.im + Fc->data[i]*Xc.im;
00228
00229 sumBB += (B.re*B.re + B.im*B.im) / var->data[i];
00230 sumAA += (A.re*A.re + A.im*A.im) / var->data[i];
00231 sumAB += (B.re*A.re + B.im*A.im) / var->data[i];
00232
00233
00234 eh0.re += (Fp->data[i]*cosIota2*cos2phase
00235 + 2.0*Fc->data[i]*cosIota*sin2phase)
00236 * (Fp->data[i]*cosIota2*cos2phase
00237 + 2.0*Fc->data[i]*cosIota*sin2phase) / input->var->data[i].re;
00238
00239 eh0.im += (Fp->data[i]*cosIota2*sin2phase
00240 - 2.0*Fc->data[i]*cosIota*cos2phase)
00241 * (Fp->data[i]*cosIota2*sin2phase
00242 - 2.0*Fc->data[i]*cosIota*cos2phase) / input->var->data[i].im;
00243 }
00244
00245 for (iH0 = 0; iH0 < params->meshH0[2]; iH0++)
00246 {
00247 h0 = params->meshH0[0] + iH0*params->meshH0[1];
00248 chiSquare = sumBB - 2.0*h0*sumAB + h0*h0*sumAA;
00249
00250 if (chiSquare<minChiSquare)
00251 {
00252 minChiSquare = chiSquare;
00253 h0BestFit = h0;
00254 cosIotaBestFit = cosIota;
00255 psiBestFit = psi;
00256 phaseBestFit = phase;
00257 output->eh0[0] = 1.0 /sqrt(sqrt(eh0.re*eh0.re + eh0.im*eh0.im));
00258 }
00259
00260 weh0 = 1.0 /sqrt(sqrt(eh0.re*eh0.re + eh0.im*eh0.im));
00261
00262 if (weh0>oldMaxEh0)
00263 {
00264 output->eh0[2] = weh0;
00265 oldMaxEh0 = weh0;
00266 }
00267
00268 if (weh0<oldMinEh0)
00269 {
00270 output->eh0[1] = weh0;
00271 oldMinEh0 = weh0;
00272 }
00273 arg = iH0 + params->meshH0[2]*(iCosIota + params->meshCosIota[2]*(iPhase + params->meshPhase[2]*iPsi));
00274 output->mChiSquare->data[arg] = chiSquare;
00275 }
00276 }
00277 }
00278 }
00279
00280
00281
00282 output->h0 = h0BestFit;
00283 output->cosIota = cosIotaBestFit;
00284 output->phase = phaseBestFit;
00285 output->psi = psiBestFit;
00286 output->chiSquare = minChiSquare;
00287
00288
00289 ASSERT(minChiSquare < INICHISQU, status,
00290 FITTOPULSARH_EMAXCHI , FITTOPULSARH_MSGEMAXCHI);
00291
00292 LALSDestroyVector(status->statusPtr, &Fp);
00293 LALSDestroyVector(status->statusPtr, &Fc);
00294 LALDDestroyVector(status->statusPtr, &var);
00295 DETATCHSTATUSPTR(status);
00296 RETURN(status);
00297
00298
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 #include <lal/FitToPulsar.h>
00339 #include <lal/Random.h>
00340
00341
00342
00343 NRCSID( FitToPulsarTestC, "$Id: FitToPulsarTest.c,v 1.4 2007/06/08 14:41:52 bema Exp $" );
00344
00345
00346
00347
00348 #define FITTOPULSARTESTC_ENOM 0
00349 #define FITTOPULSARTESTC_ECHK 1
00350 #define FITTOPULSARTESTC_EFLS 2
00351
00352 #define FITTOPULSARTESTC_MSGENOM "Nominal exit"
00353 #define FITTOPULSARTESTC_MSGECHK "Error checking failed to catch bad data"
00354 #define FITTOPULSARTESTC_MSGEFLS "Incorrect answer for valid data"
00355
00356
00357
00358
00359
00360 #define FITTOPULSARTEST_LENGTH 24*10
00361 #define FITTOPULSARTEST_T0 630720013
00362
00363
00364 int lalDebugLevel = 0;
00365 int main(void)
00366 {
00367 static LALStatus status;
00368 LALDetector detector;
00369 LALSource pulsar;
00370 CoarseFitOutput output;
00371 CoarseFitInput input;
00372 CoarseFitParams params;
00373 LIGOTimeGPS tgps[FITTOPULSARTEST_LENGTH];
00374 REAL4 cosIota;
00375 REAL4 phase;
00376 REAL4 psi;
00377 REAL4 h0;
00378 REAL4 cos2phase, sin2phase;
00379 static RandomParams *randomParams;
00380 static REAL4Vector *noise;
00381 INT4 seed = 0;
00382 LALDetAndSource detAndSource;
00383 LALDetAMResponseSeries pResponseSeries = {NULL,NULL,NULL};
00384 REAL4TimeSeries Fp, Fc, Fs;
00385 LALTimeIntervalAndNSample time_info;
00386 UINT4 i;
00387 LALGPSandAcc pGPSandAcc;
00388
00389
00390
00391
00392 input.B = NULL;
00393 input.var = NULL;
00394
00395 LALZCreateVector( &status, &input.B, FITTOPULSARTEST_LENGTH);
00396 LALZCreateVector( &status, &input.var, FITTOPULSARTEST_LENGTH);
00397
00398 noise = NULL;
00399 LALCreateVector( &status, &noise, FITTOPULSARTEST_LENGTH);
00400 LALCreateRandomParams( &status, &randomParams, seed);
00401
00402 Fp.data = NULL;
00403 Fc.data = NULL;
00404 Fs.data = NULL;
00405
00406 pResponseSeries.pPlus = &(Fp);
00407 pResponseSeries.pCross = &(Fc);
00408 pResponseSeries.pScalar = &(Fs);
00409
00410 LALSCreateVector(&status, &(pResponseSeries.pPlus->data), 1);
00411 LALSCreateVector(&status, &(pResponseSeries.pCross->data), 1);
00412 LALSCreateVector(&status, &(pResponseSeries.pScalar->data), 1);
00413
00414 input.t = tgps;
00415
00416
00417 time_info.epoch.gpsSeconds = FITTOPULSARTEST_T0;
00418 time_info.epoch.gpsNanoSeconds = 0;
00419 time_info.deltaT = 60;
00420 time_info.nSample = FITTOPULSARTEST_LENGTH;
00421 time_info.accuracy = 1.0;
00422
00423 cosIota = 0.5;
00424 psi = 0.1;
00425 phase = 0.4;
00426 h0 = 5.0;
00427
00428 cos2phase = cos(2.0*phase);
00429 sin2phase = sin(2.0*phase);
00430
00431 detector = lalCachedDetectors[LALDetectorIndexGEO600DIFF];
00432 pulsar.equatorialCoords.longitude = 1.4653;
00433 pulsar.equatorialCoords.latitude = -1.2095;
00434 pulsar.equatorialCoords.system = COORDINATESYSTEM_EQUATORIAL;
00435 pulsar.orientation = psi;
00436 strcpy(pulsar.name, "fakepulsar");
00437
00438 detAndSource.pDetector = &detector;
00439 detAndSource.pSource = &pulsar;
00440
00441 LALNormalDeviates( &status, noise, randomParams );
00442
00443 LALComputeDetAMResponseSeries(&status, &pResponseSeries, &detAndSource, &time_info);
00444
00445 for (i = 0;i < FITTOPULSARTEST_LENGTH; i++)
00446 {
00447 input.t[i].gpsSeconds = FITTOPULSARTEST_T0 + 60*i;
00448 input.t[i].gpsNanoSeconds = 0;
00449
00450 input.B->data[i].re = pResponseSeries.pPlus->data->data[i]*h0*(1.0 + cosIota*cosIota)*cos2phase
00451 + 2.0*pResponseSeries.pCross->data->data[i]*h0*cosIota*sin2phase;
00452 input.B->data[i].im = pResponseSeries.pPlus->data->data[i]*h0*(1.0 + cosIota*cosIota)*sin2phase
00453 - 2.0*pResponseSeries.pCross->data->data[i]*h0*cosIota*cos2phase;
00454 input.var->data[i].re = noise->data[FITTOPULSARTEST_LENGTH-i-1]*noise->data[FITTOPULSARTEST_LENGTH-i-1];
00455 input.var->data[i].im = noise->data[i]*noise->data[i];
00456 }
00457
00458 input.B->length = FITTOPULSARTEST_LENGTH;
00459 input.var->length = FITTOPULSARTEST_LENGTH;
00460
00461
00462
00463 params.detector = detector;
00464 params.pulsarSrc = pulsar;
00465
00466 params.meshH0[0] = 4.0;
00467 params.meshH0[1] = 0.2;
00468 params.meshH0[2] = 10;
00469
00470 params.meshCosIota[0] = 0.0;
00471 params.meshCosIota[1] = 0.1;
00472 params.meshCosIota[2] = 10;
00473
00474 params.meshPhase[0] = 0.0;
00475 params.meshPhase[1] = 0.1;
00476 params.meshPhase[2] = 10;
00477
00478 params.meshPsi[0] = 0.0;
00479 params.meshPsi[1] = 0.1;
00480 params.meshPsi[2] = 10;
00481
00482 output.mChiSquare = NULL;
00483 LALDCreateVector( &status, &output.mChiSquare,params.meshH0[2]*params.meshCosIota[2]*params.meshPhase[2]*params.meshPsi[2]);
00484
00485
00486 LALCoarseFitToPulsar(&status,&output, &input, ¶ms);
00487
00488 if(status.statusCode)
00489 {
00490 printf("Unexpectedly got error code %d and message %s\n",
00491 status.statusCode, status.statusDescription);
00492 return FITTOPULSARTESTC_EFLS;
00493 }
00494
00495
00496 if(output.phase > phase + params.meshPhase[2] || output.phase < phase - params.meshPhase[2])
00497 {
00498 printf("Got incorrect phase %f when expecting %f \n",
00499 output.phase, phase);
00500 return FITTOPULSARTESTC_EFLS;
00501 }
00502
00503 if(output.cosIota > cosIota + params.meshCosIota[2] || output.cosIota < cosIota - params.meshCosIota[2])
00504 {
00505 printf("Got incorrect cosIota %f when expecting %f \n",
00506 output.cosIota, cosIota);
00507 return FITTOPULSARTESTC_EFLS;
00508 }
00509
00510 if(output.psi > psi + params.meshPsi[2] || output.psi < psi - params.meshPsi[2])
00511 {
00512 printf("Got incorrect psi %f when expecting %f \n",
00513 output.psi, psi);
00514 return FITTOPULSARTESTC_EFLS;
00515 }
00516
00517
00518
00519 #ifndef LAL_NDEBUG
00520 if ( ! lalNoDebug ) {
00521
00522
00523
00524 LALCoarseFitToPulsar(&status, NULL, &input, ¶ms);
00525
00526 if (status.statusCode != FITTOPULSARH_ENULLOUTPUT
00527 || strcmp(status.statusDescription, FITTOPULSARH_MSGENULLOUTPUT))
00528 {
00529 printf( "Got error code %d and message %s\n",
00530 status.statusCode, status.statusDescription);
00531 printf( "Expected error code %d and message %s\n",
00532 FITTOPULSARH_ENULLOUTPUT, FITTOPULSARH_MSGENULLOUTPUT);
00533 return FITTOPULSARTESTC_ECHK;
00534 }
00535
00536 LALCoarseFitToPulsar(&status, &output, NULL, ¶ms);
00537
00538 if (status.statusCode != FITTOPULSARH_ENULLINPUT
00539 || strcmp(status.statusDescription, FITTOPULSARH_MSGENULLINPUT))
00540 {
00541 printf( "Got error code %d and message %s\n",
00542 status.statusCode, status.statusDescription);
00543 printf( "Expected error code %d and message %s\n",
00544 FITTOPULSARH_ENULLINPUT, FITTOPULSARH_MSGENULLINPUT);
00545 return FITTOPULSARTESTC_ECHK;
00546 }
00547
00548 LALCoarseFitToPulsar(&status, &output, &input, NULL);
00549
00550 if (status.statusCode != FITTOPULSARH_ENULLPARAMS
00551 || strcmp(status.statusDescription, FITTOPULSARH_MSGENULLPARAMS))
00552 {
00553 printf( "Got error code %d and message %s\n",
00554 status.statusCode, status.statusDescription);
00555 printf( "Expected error code %d and message %s\n",
00556 FITTOPULSARH_ENULLPARAMS, FITTOPULSARH_MSGENULLPARAMS);
00557 return FITTOPULSARTESTC_ECHK;
00558 }
00559
00560
00561 input.var->length = 1;
00562 LALCoarseFitToPulsar(&status, &output, &input, ¶ms);
00563
00564 if (status.statusCode != FITTOPULSARH_EVECSIZE
00565 || strcmp(status.statusDescription, FITTOPULSARH_MSGEVECSIZE))
00566 {
00567 printf( "Got error code %d and message %s\n",
00568 status.statusCode, status.statusDescription);
00569 printf( "Expected error code %d and message %s\n",
00570 FITTOPULSARH_EVECSIZE, FITTOPULSARH_MSGEVECSIZE);
00571 return FITTOPULSARTESTC_ECHK;
00572 }
00573
00574 input.var->length = FITTOPULSARTEST_LENGTH;
00575
00576 }
00577 #endif
00578
00579
00580
00581 LALZDestroyVector(&status, &input.B);
00582 LALZDestroyVector(&status, &input.var);
00583 LALDestroyVector(&status, &noise);
00584 LALDDestroyVector(&status, &output.mChiSquare);
00585 LALDestroyRandomParams(&status, &randomParams);
00586 LALSDestroyVector(&status, &(pResponseSeries.pPlus->data));
00587 LALSDestroyVector(&status, &(pResponseSeries.pCross->data));
00588 LALSDestroyVector(&status, &(pResponseSeries.pScalar->data));
00589
00590 LALCheckMemoryLeaks();
00591
00592 return FITTOPULSARTESTC_ENOM;
00593 }
00594