-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickCBC.c
7948 lines (5996 loc) · 225 KB
/
QuickCBC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************************
Copyright (c) 2019 Neil Cornish
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "QuickCBC.h"
#include "ConstCBC.h"
#include "Utilities.h"
#include "IMRPhenomD.h"
#ifndef _OPENMP
#define omp ignore
#endif
//OSX
// clang -Xpreprocessor -fopenmp -lomp -w -o QuickCBC QuickCBC.c Utilities.c IMRPhenomD_internals.c IMRPhenomD.c -lgsl -lgslcblas -lm
// Linux
// gcc -std=gnu99 -fopenmp -w -o QuickCBC QuickCBC.c IMRPhenomD_internals.c IMRPhenomD.c -lgsl -lgslcblas -lm
//##############################################
//MT modifications
gsl_rng **rvec;
//##############################################
int main(int argc, char *argv[])
{
int i, j, k, M, N, Nf, Nstep, Nclean, m, rs, tsi, tti;
int ii, jj, kk, Nlines, id;
int *mxc;
int nt, bn;
int oflag, flag;
int imin, imax;
double SNR, max;
double Fplus, Fcross;
double ciota, Fscale, Ap, Ac;
double psi, alpha, sindelta;
double junk, Tobs, fix, f, t, t0, dt, dtm, df, x, y, z, dx, dtx;
double dfx, Q, fny, scale, dlnf;
double *freqs, *ref;
double *inp, *oup, *slice;
double *H1dat, *L1dat;
double *waveH, *waveL;
double **D, **DW, **Dtime;
double *Dds;
double **SN, **SM;
double *sdata;
double *intime, *sqf;
double sigmean, sigmedian;
int subscale, octaves;
int mmax;
double SNRsq, SNRold, pmax;
double SNRH, SNRL, pw;
double t_rise, s1, s2, ascale, fac, Tpad;
double av, var, Tfull;
double ttrig, tstart, tstart_clean, Tclean, starttime, endtime, Dfmax;
double q, mc, mt, eta, m1, m2;
int Oflag;
int modelprint;
double *linef, *linew, *lineh, *lineQ;
double *time;
double *DHfull, *DLfull;
double *DHcopy, *DLcopy;
char filename[1024];
char command[1024];
char Dname[1024];
int n;
const gsl_rng_type * P;
gsl_rng * r;
gsl_rng_env_setup();
P = gsl_rng_default;
r = gsl_rng_alloc (P);
FILE *in;
FILE *ifp;
FILE *out;
struct Net *net = malloc(sizeof(struct Net));
gsl_rng_set(r, 18346443564);
//##############################################
//open MP modifications
omp_set_num_threads(NC);
rvec = (gsl_rng **)malloc(sizeof(gsl_rng *) * (NC+1));
for(i = 0 ; i<= NC; i++){
rvec[i] = gsl_rng_alloc(P);
gsl_rng_set(rvec[i] , i);
}
//##############################################
if(argc<4)
{
printf("./QuickCBC Tobs trig_time detector-list\n");
printf("The detector list can be just one detector, e.g. 0 for H, 1 for L, 2 for V\n");
printf("Can also enter 1 2 or 0 1 2 etc. The order doesn't matter, except that the first on the list becomes the reference detector\n");
return 1;
}
// Note that H is always called 0, L is 1 and V is 2 for data read and antenna response. But the ifo order can be anything. The labels array handles the mapping. e.g. if the command line has 2 0 1, then ifo order is 0=Virgo, 1=H and 2=L.
Tobs = atof(argv[1]);
ttrig = atof(argv[2]);
// Hour angle
net->GMST = gmst(ttrig);
net->Tobs = Tobs;
printf("GMST = %f\n", net->GMST);
if(Tobs < 4.0)
{
printf("Observation time too short - need at least 4 seconds of data\n");
return(-1);
}
net->Nifo = argc-3;
net->labels = int_vector(net->Nifo);
for (i = 0; i < net->Nifo; ++i) net->labels[i] = atoi(argv[3+i]);
net->tds = double_vector(net->Nifo);
net->delays = double_matrix(net->Nifo,net->Nifo);
pairs(net);
time_delays(net);
for (i = 0; i < net->Nifo; ++i)
{
for (j = 0; j < net->Nifo; ++j)
{
printf("delay %d-%d = %f ", net->labels[i], net->labels[j], net->delays[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 1; i < net->Nifo; ++i) printf("delay 0-%d = %f ", net->labels[i], net->tds[i]);
printf("\n");
starttime = floor(ttrig)-Tobs+2.0;
if(fabs(ttrig-floor(ttrig)) < 1.0e-8) // integer merger time
{
net->tmax = Tobs-0.5; // upper peak time (Trigger time is Tobs-2)
net->tmin = Tobs-3.5; // lower peak time (Trigger time is Tobs-2)
if(net->tmin < 1.5) net->tmin = 1.5; // need at least one second of good data
}
else
{
net->tmax = Tobs-2.0+(ttrig-floor(ttrig))+0.5*twidth;
net->tmin = Tobs-2.0+(ttrig-floor(ttrig))-0.5*twidth;
}
printf("%f %f\n", net->tmin, net->tmax);
/********************* This section reads in the data and the PSD estimates *************************/
// Read in the frame data for the first detector and use it to identify the data length
sprintf(command, "framed_%d_%d_%d.dat", (int)(Tobs), (int)ttrig, net->labels[0]);
in = fopen(command,"r");
N = -1;
while(!feof(in))
{
fscanf(in,"%lf%lf", &x, &y);
N++;
}
rewind(in);
dt = Tobs/(double)(N);
printf("%d\n", N);
time = (double*)malloc(sizeof(double)* (N));
D= double_matrix(net->Nifo,N);
Dtime = double_matrix(net->Nifo,N);
DW = double_matrix(net->Nifo,N);
for (i = 0; i < N; ++i) fscanf(in,"%lf%lf", &time[i], &D[0][i]);
fclose(in);
for (j = 1; j < net->Nifo; ++j)
{
sprintf(command, "framed_%d_%d_%d.dat", (int)(Tobs), (int)ttrig, net->labels[j]);
in = fopen(command,"r");
for (i = 0; i < N; ++i) fscanf(in,"%lf%lf", &x, &D[j][i]);
fclose(in);
}
// keep a copy of the time domain data for later glitch removal
for (j = 0; j < net->Nifo; ++j)
{
for (i = 0; i < N; ++i) Dtime[j][i] = D[j][i];
}
SN = double_matrix(net->Nifo,N/2); // full PSD (smooth plus lines)
SM = double_matrix(net->Nifo,N/2); // smooth PSD
// read in the PSDs
for (j = 0; j < net->Nifo; ++j)
{
sprintf(command, "spec_%d_%d_%d.dat", (int)(Tobs), (int)ttrig, net->labels[j]);
in = fopen(command,"r");
for (i = 0; i < N/2; ++i) fscanf(in,"%lf%lf%lf%lf", &x, &SN[j][i], &SM[j][i], &x);
fclose(in);
}
// Tukey window parameter. Flat for (1-alpha) of data
t_rise = 0.4; // Standard LAL setting
alpha = (2.0*t_rise/Tobs);
tukey_scale(&s1, &s2, alpha, N);
// Apply Tukey window to each detector time series
for (i = 0; i < net->Nifo; ++i)
{
tukey(D[i], alpha, N); // Tukey window
gsl_fft_real_radix2_transform(D[i], 1, N); // FFT
for (j = 0; j < N; ++j) DW[i][j] = D[i][j];
}
// Apply scaling to FFTed data
fac = sqrt((double)(N/2))/sqrt(Tobs);
for (i = 0; i < net->Nifo; ++i)
{
whiten(DW[i], SN[i], N); // whiten
gsl_fft_halfcomplex_radix2_inverse(DW[i], 1, N); // iFFT
for (j = 0; j < N; ++j) DW[i][j] /= fac;
}
// output whitened data
out = fopen("dataw.dat","w");
for (j = 0; j < N; ++j)
{
fprintf(out,"%e ", (double)(j)*dt-Tobs+2.0);
for (i = 0; i < net->Nifo; ++i) fprintf(out,"%e ", DW[i][j]);
fprintf(out,"\n");
}
fclose(out);
fac = Tobs/((double)(N)*(double)(N));
sprintf(command, "pspec_%d_%d.dat", (int)(Tobs), (int)ttrig);
out = fopen(command,"w");
for (i = 0; i < N/2; ++i)
{
fprintf(out,"%.15e ", (double)(i)/Tobs);
for (j = 0; j < net->Nifo; ++j) fprintf(out,"%.15e ", fac*2.0*(D[j][i]*D[j][i]+D[j][N-i]*D[j][N-i]));
fprintf(out,"\n");
}
fclose(out);
sprintf(command, "wcheck_%d_%d.dat", (int)(Tobs), (int)ttrig);
out = fopen(command,"w");
for (i = 0; i < N/2; ++i)
{
fprintf(out,"%.15e ", (double)(i)/Tobs);
for (j = 0; j < net->Nifo; ++j) fprintf(out,"%.15e ", fac*2.0*(D[j][i]*D[j][i]+D[j][N-i]*D[j][N-i])/SN[j][i]);
fprintf(out,"\n");
}
fclose(out);
fac = sqrt(Tobs)/(double)(N);
for (i = 0; i < N; ++i)
{
for (k = 0; k < net->Nifo; ++k) D[k][i] *= fac;
}
for (i = 0; i < net->Nifo; ++i)
{
for (j = 0; j < N; ++j) DW[i][j] = D[i][j];
}
struct PSD *psd = malloc(sizeof(struct PSD));
psd->Nspline = int_vector(net->Nifo);
psd->Nlines = int_vector(net->Nifo);
i = 0; k = 0;
for (j = 0; j < net->Nifo; ++j)
{
sprintf(command, "summary_%d.dat", net->labels[j]);
in = fopen(command,"r");
fscanf(in,"%d%d%d", &psd->Nspline[j], &psd->Nlines[j], &psd->Nsample);
if(psd->Nspline[j] > i) i = psd->Nspline[j];
if(psd->Nlines[j] > k) k = psd->Nlines[j];
fclose(in);
}
psd->xspline = double_tensor(net->Nifo,psd->Nsample,i);
psd->ffit = double_matrix(net->Nifo,i);
psd->linef = double_tensor(net->Nifo,psd->Nsample,k);
psd->lineh = double_tensor(net->Nifo,psd->Nsample,k);
psd->lineQ = double_tensor(net->Nifo,psd->Nsample,k);
psd->linew = double_tensor(net->Nifo,psd->Nsample,k);
psd->deltafmax = double_tensor(net->Nifo,psd->Nsample,k);
for (j = 0; j < net->Nifo; ++j)
{
sprintf(command, "sfile_%d.dat", net->labels[j]);
in = fopen(command,"r");
for (i = 0; i < psd->Nsample; ++i)
{
for (k = 0; k < psd->Nspline[j]; ++k) fscanf(in, "%lf", &psd->xspline[j][i][k]);
}
fclose(in);
sprintf(command, "ffile_%d.dat", net->labels[j]);
in = fopen(command,"r");
for (k = 0; k < psd->Nspline[j]; ++k) fscanf(in, "%lf", &psd->ffit[j][k]);
fclose(in);
}
for (j = 0; j < net->Nifo; ++j)
{
sprintf(command, "lfile_%d.dat", net->labels[j]);
in = fopen(command,"r");
for (i = 0; i < psd->Nsample; ++i)
{
for (k = 0; k < psd->Nlines[j]; ++k)
{
fscanf(in, "%lf%lf%lf%lf%lf", &psd->linef[j][i][k], &psd->lineh[j][i][k], &psd->lineQ[j][i][k], &psd->linew[j][i][k], &psd->deltafmax[j][i][k]);
}
}
fclose(in);
}
/********************* With the data read in, we can now do the CBC PE *************************/
// These need to be passed back and forth between BayesWave and the CBC PE code
// The CBC PE code also needs the PSDs and glitch removed residuals D
double ***global;
double **skyx;
int *who;
double *heat;
double **paramx;
double **pallx;
double ***history;
double ***historyall;
RealVector *freq;
FILE *chainE;
FILE *chainI;
FILE *chainA;
FILE *chainS;
chainS = fopen("searchchain.dat","w");
chainE = fopen("extrinsicchain.dat","w");
chainI = fopen("intrinsicchain.dat","w");
chainA = fopen("allchain.dat","w");
global = double_tensor(NQ,NM,N);
skyx = double_matrix(NC+1,NS);
who = int_vector(NC+1);
heat = double_vector(NC+1);
paramx = double_matrix(NC+1,NX+3*net->Nifo);
history = double_tensor(NC+1,NH,NX);
freq = CreateRealVector((N/2));
mxc = int_vector(3);
for (i = 0; i < 3; i++) mxc[i] = 0;
freq->data[0] = 1.0/Tobs;
for (i=1; i< N/2; i++) freq->data[i] = (double)(i)/Tobs;
pallx = double_matrix(NC+1,NP);
historyall = double_tensor(NC+1,NH,NP);
// The CBC_start function does a search to find the signal and initializes the arrays
CBC_start(net, mxc, chainS, paramx, skyx, pallx, who, heat, history, global, freq, D, Dtime, SN, N, Tobs, r);
/*
in = fopen("maplike_all.dat","r");
fscanf(in,"%lf", &x);
for (i=0; i< NP; i++) fscanf(in,"%lf", &pallx[1][i]);
fclose(in);
for(i=1; i<=NC; i++) who[i] = i;
for(i=1; i<=NCC; i++) heat[i] = 1.0;
for(i=NCC+1; i<=NC; i++) heat[i] = heat[i-1]*ladder; */
MCMC_all(net, psd, mxc, Nall, chainA, pallx, who, heat, historyall, global, freq, D, SN, SM, N, Tobs, ttrig, r);
//###############################################
//MT modification
for(i =0 ;i<= NC; i++){
gsl_rng_free(rvec[i]);
}
free(rvec);
//###############################################
return 0;
}
double gmst(double ttrig)
{
double GMST, x, toff, lp, dsec;
/* Code based on Javascript online calculator https://celnav.de/longterm.htm */
lp = leap(ttrig);
time_t ttime = ttrig+EPOCH_UNIX_GPS-lp;
struct tm *timeinfo;
/* Get current time, print it and modify */
timeinfo = gmtime(&ttime);
printf("Trigger time: %04d-%02d-%02d %02d:%02d:%02d (%s)\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, timeinfo->tm_zone);
double year, month, day, hour, minute, second, dayfraction;
year = (double)timeinfo->tm_year + 1900.0;
month = (double)timeinfo->tm_mon + 1.0;
day = (double)timeinfo->tm_mday;
hour = (double)timeinfo->tm_hour;
minute = (double)timeinfo->tm_min;
second = (double)timeinfo->tm_sec;
dayfraction = (hour + minute/60.0 + second/3600.0)/24.0;
if(month <= 2.0) {year -=1.0; month += 12;}
double A = floor(year/100.0);
double B = 2.0-A+floor(A/4.0);
double JD0h = floor(365.25*(year+4716.0))+floor(30.6001*(month+1.0))+day+B-1524.5;
double JD = JD0h+dayfraction;
//Julian centuries (GMT) since 2000 January 0.5
double T = (JD-2451545.0)/36525.0;
double T2 = T*T;
double T3 = T*T2;
double T4 = T*T3;
double T5 = T*T4;
double GHAAmean = (280.46061837+ 360.98564736629*(JD-2451545.0)+0.000387933*T2-T3/38710000.0);
while(GHAAmean < 360.0) GHAAmean += 360.0;
x = floor(GHAAmean/360.0);
GHAAmean = GHAAmean - x*360.0;
double GMSTdecimal = GHAAmean/15.0;
double GMSTh = floor(GMSTdecimal);
double GMSTmdecimal = 60.0*(GMSTdecimal-GMSTh);
double GMSTm = floor(GMSTmdecimal);
double GMSTsdecimal = 60.0*(GMSTmdecimal-GMSTm);
double GMSTs = rint(1000.0*GMSTsdecimal)/1000.0;
printf("GMST h %f m %f s %f\n", GMSTh, GMSTm, GMSTs);
// convert to radians
GMST = ((GMSTs/60.0 +GMSTm)/60.0+ GMSTh)*PIn/12.0;
return GMST;
}
double leap(double tgps)
{
int i;
double x;
// 18 leap seconds since 1980
double lp[] = {46828800.0, 78364801.0, 109900802.0, 173059203.0, 252028804.0, 315187205.0, 346723206.0, \
393984007.0,425520008.0, 457056009.0, 504489610.0, 551750411.0, 599184012.0, 820108813.0, \
914803214.0, 1025136015.0, 1119744016.0, 1167264017.0};
x = 0.0;
for (i = 0; i < 18; ++i)
{
if(tgps > lp[i]) x += 1.0;
}
return(x);
}
void CBC_start(struct Net *net, int *mxc, FILE *chainS, double **paramx, double **skyx, double **pallx, int *who, double *heat, double ***history, double ***global, RealVector *freq, double **D, double **Dtime, double **SN, int N, double Tobs, gsl_rng *r)
{
int i, j, jj, k, kk, id;
double *params;
double *logL, *logLsky, *logLstart, *logLfull;
double q, mc, m2, m1, mt;
double x, y, qx, tx, fac, alpha;
double lMc, lMcmin, lMcmax, dlMc, lmsun;
double lMcx, lMtx;
double **data, ***wave;
double *Larray, **Tarray;
double *DD;
double **WW;
double ***DHc, ***DHs, ***HH;
int imin, imax;
int nt, bn;
double dtx, dt;
double *SNRsq;
double **rho;
double Lmax;
int jmax;
char command[1024];
FILE *chain;
FILE *in;
FILE *out;
dt = Tobs/(double)(N);
// intialize the who, heat, history and counter arrays
for(i=1; i<=NC; i++) who[i] = i;
// run cold to force ML
for(i=1; i<=NCC; i++) heat[i] = 0.25;
for(i=NCC+1; i<=NC; i++) heat[i] = heat[i-1]*ladder; // spacing can be big here since we start at low temperature
// with 10 hot chains this gets the hottest up to heat > 1.
for(j = 1; j <= NC; j++)
{
for(k = 0; k <= NH; k++)
{
for(i = 0; i < NX; i++)
{
history[j][k][i] = 0.0;
}
}
}
mxc[0] = 0;
mxc[1] = 0;
logL = double_vector(NC+1);
logLstart = double_vector(NC+1);
logLsky = double_vector(NC+1);
logLfull = double_vector(NC+1);
rho = double_matrix(NC+1,net->Nifo);
params = (double*)malloc(sizeof(double)* (NX+3*net->Nifo));
// whitened signal in each detector
wave = double_tensor(NC+1,net->Nifo,N);
Larray = double_vector(N);
Tarray = double_matrix(net->Nifo,N);
// sky parameter order
//[0] alpha, [1] sin(delta) [2] psi [3] ellipticity [4] scale [5] dphi [6] dt
// The sky paramters [4], [5] and [6] hold the shift in the waveform in the reference detector
// [4] Holds the amplitude rescaling
// [5] Holds the time shift
// [6] Holds the phase shift
params[0] = log(1.0*MSUN_SI); // log(Mc)
q = 1.5; // q = m1/m2
mc = exp(params[0]);
m2 = mc*pow(1.0+q, 0.2)/pow(q,0.6);
m1 = q*m2;
mt = m1+m2;
params[1] = log(mt); // log(Mt)
params[2] = 0.0; // chi1
params[3] = 0.0; // chi2
params[4] = 0.0; // phi0
params[5] = Tobs/2.0; // peak time (at reference detector, not geocenter)
params[6] = log(1.0e8 * PC_SI); //distance (referenced to a F=1 antenna response in the reference detector, need antenna pattern factor to get physical distance).
for (i = 1; i < net->Nifo; ++i)
{
params[NX+(i-1)*3] = 0.0; // phase offset
params[NX+(i-1)*3+1] = 0.0; // time offset
params[NX+(i-1)*3+2] = 1.0; // amplitude ratio
}
freq->data[0] = 1.0/Tobs;
for (i=1; i< N/2; i++) freq->data[i] = (double)(i)/Tobs;
lMcmax = log(mcmax*MSUN_SI);
lMcmin = log(mcmin*MSUN_SI);
dlMc = (lMcmax-lMcmin)/(double)(NM);
for(k = 0; k < NQ; k++)
{
for(j = 0; j < NM; j++)
{
for(i = -N/2; i < N/2; i++)
{
global[k][j][i+N/2] = cap;
}
}
}
// Initial search to find signal
MCMC_intrinsic(net, 1, mxc, Nsearch, chainS, paramx, skyx, pallx, who, heat, history, global, freq, D, SN, N, Tobs, r);
// find current max likelihood waveform
Lmax = 0.0;
for(k = 1; k <= NC; k++)
{
logL[k] = log_likelihood_intrinsic(net, D, paramx[k], freq, SN, N, Tobs);
if(logL[k] > Lmax)
{
Lmax = logL[k];
kk = k;
}
}
// params[NX+(k-1)*3+1] = delt[k]-delt[0]; // time offset
// record ML f-tf track
double *tm, *fr;
tm = double_vector(N);
fr = double_vector(N);
for (i = 0; i < N; i++) tm[i] = Tobs/(double)(N)*(double)(i);
f_of_t(paramx[kk], tm, fr, N);
for(j = 0; j < net->Nifo; j++)
{
sprintf(command, "tftrack_search_%d.dat", net->labels[j]);
out = fopen(command,"w");
if(j == 0)
{
for (i = 0; i < N; i++) fprintf(out,"%e %e\n", tm[i]-Tobs+2.0, fr[i]);
}
else // put in time shift
{
for (i = 0; i < N; i++) fprintf(out,"%e %e\n", tm[i]-Tobs+2.0+params[NX+(j-1)*3+1], fr[i]);
}
fclose(out);
}
free_double_vector(tm);
free_double_vector(fr);
//Here we remove any glitches and re-compute the SNR. Uses best-fit template
// compute waveform in each detector using maxL chain parameters
double **twave;
double *SM, *SX;
twave = double_matrix(net->Nifo,N);
templates(net, twave, freq, paramx[kk], N);
//Restore time domain data
for (j = 0; j < net->Nifo; ++j)
{
for (i = 0; i < N; ++i) D[j][i] = Dtime[j][i];
}
SM = (double*)malloc(sizeof(double)*(N/2));
SX = (double*)malloc(sizeof(double)*(N/2));
fac = sqrt(Tobs)/(double)(N);
// the spec code uses a different scaling
for (j = 0; j < net->Nifo; ++j)
{
for (i = 0; i < N; ++i) twave[j][i] /= fac;
}
// make Qscan for data - signal
if(printQ == 1)
{
for (j = 0; j < net->Nifo; ++j)
{
qscanres(D[j], twave[j], SN[j], Tobs, N);
sprintf(command, "cp Qtranres.dat Qres_%d.dat", net->labels[j]);
system(command);
sprintf(command, "cp Qtranres.dat Qtransform.dat", net->labels[j]);
system(command);
sprintf(command, "gnuplot Qscan.gnu");
system(command);
sprintf(command, "cp Qscan.png Qres_%d.png", net->labels[j]);
system(command);
}
}
// make Qscan for signal
if(printQ == 1)
{
for (j = 0; j < net->Nifo; ++j)
{
qscanf(twave[j], SN[j], Tobs, N);
sprintf(command, "cp Qsig.dat Qsig_%d.dat", net->labels[j]);
system(command);
sprintf(command, "cp Qsig.dat Qtransform.dat", net->labels[j]);
system(command);
sprintf(command, "gnuplot Qscan.gnu");
system(command);
sprintf(command, "cp Qscan.png Qsig_%d.png", net->labels[j]);
system(command);
}
}
// recompute the PSD and remove any glitches. The signal is removed for PSD estimation and
// glitch finding, but remains in the cleaned data that is returned
for (j = 0; j < net->Nifo; ++j)
{
specest(D[j], twave[j], N, N/2, dt, 1.0/(2.0*dt), SN[j], SM, SX);
sprintf(command, "cp wglitch.dat wglitch_%d.dat", net->labels[j]);
system(command);
sprintf(command, "clean_%d.dat", net->labels[j]);
out = fopen(command,"w");
for (i = 0; i < N; ++i) fprintf(out,"%e %e\n", dt*(double)(i), D[j][i]);
fclose(out);
}
if(printQ == 1)
{
// This section is just for making Q-scans of the de-glitched residual
for (j = 0; j < net->Nifo; ++j)
{
for (i = 0; i < N; ++i) Dtime[j][i] = D[j][i]; // use the cleaned data
specest(Dtime[j], twave[j], N, N/2, dt, 1.0/(2.0*dt), SX, SM, SX);
sprintf(command, "cp Qtransform.dat Qfullres_%d.dat", net->labels[j]);
system(command);
sprintf(command, "gnuplot Qscan.gnu");
system(command);
sprintf(command, "cp Qscan.png Qfullres_%d.png", net->labels[j]);
system(command);
}
// This section is just for making Q-scans of the de-glitched data
for (j = 0; j < net->Nifo; ++j)
{
for (i = 0; i < N; ++i) twave[j][i] = 0.0; // don't subtract the signal
for (i = 0; i < N; ++i) Dtime[j][i] = D[j][i]; // use the cleaned data
specest(Dtime[j], twave[j], N, N/2, dt, 1.0/(2.0*dt), SX, SM, SX);
sprintf(command, "cp Qtransform.dat Qclean_%d.dat", net->labels[j]);
system(command);
sprintf(command, "gnuplot Qscan.gnu");
system(command);
sprintf(command, "cp Qscan.png Qclean_%d.png", net->labels[j]);
system(command);
}
// end Qscan making section
}
double t_rise = 0.4; // Standard LAL setting
alpha = (2.0*t_rise/Tobs);
// Apply Tukey window to each detector time series and FFT
for (i = 0; i < net->Nifo; ++i)
{
tukey(D[i], alpha, N); // Tukey window
gsl_fft_real_radix2_transform(D[i], 1, N); // FFT
}
for (i = 0; i < N; ++i)
{
for (k = 0; k < net->Nifo; ++k) D[k][i] *= fac;
}
// kill contribution below fmin Hz
j = (int)(fmin*Tobs);
for (i = 0; i < j; ++i)
{
for (k = 0; k < net->Nifo; ++k) SN[k][i] = 1.0;
}
free(SM);
free(SX);
free_double_matrix(twave,net->Nifo);
// re-set with multiple cold chains for regular MCMC
for(i=1; i<=NCC; i++) heat[i] = 1.0;
for(i=NCC+1; i<=NC; i++) heat[i] = heat[i-1]*ladder;
// refine the intrinsic parameters now any glitches have been removed
MCMC_intrinsic(net, 0, mxc, Nintrinsic, chainS, paramx, skyx, pallx, who, heat, history, global, freq, D, SN, N, Tobs, r);
imin = (int)((fmin*Tobs));
imax = (int)((fmax*Tobs));
upsample(N, Tobs, &nt, &bn);
dtx = Tobs/(double)(bn);
printf("Time steps = %d time resolution = %f\n", nt, dtx);
DD = double_vector(net->Nifo);
WW = double_matrix(NC+1,net->Nifo);
DHc = double_tensor(NC+1,net->Nifo,nt);
DHs = double_tensor(NC+1,net->Nifo,nt);
HH = double_tensor(NC+1,net->Nifo,3);
// whitened data in each detector
data = double_matrix(net->Nifo,N);
for (id = 0; id < net->Nifo; ++id)
{
data[id][0] = 0.0;
data[id][N/2] = 0.0;
}
for (i = 1; i < N/2; ++i)
{
for (id = 0; id < net->Nifo; ++id)
{
x = 1.0/sqrt(SN[id][i]);
data[id][i] = D[id][i]*x;
data[id][N-i] = D[id][N-i]*x;
}
}
Lmax = 0.0;
printf("Finding sky location\n");
// Find sky location and set up whitend signal arrays
#pragma omp parallel for
for(j = 1; j <= NC; j++)
{
int mtid, mti;
double mty, Scale;
double *SNRsq;
double **hwave;
SNRsq = double_vector(net->Nifo);
hwave = double_matrix(net->Nifo,N);
templates(net, hwave, freq, paramx[j], N);
// might want to put a catch here for any chains that didn't reach a decent logL and re-run the rearch phase
logLstart[j] = log_likelihood_intrinsic(net, D, paramx[j], freq, SN, N, Tobs);
//printf("%d %f\n", j, logLstart[j]);
mty = 0.0;
for (mtid = 0; mtid < net->Nifo; ++mtid)
{
SNRsq[mtid] = 0.0;
for (mti = 1; mti < N/2; ++mti)
{
// The sky mapping code puts in the amplitude and phase shifts. The signals only differ due to the whitening
SNRsq[mtid] += 4.0*(hwave[0][mti]*hwave[0][mti]+hwave[0][N-mti]*hwave[0][N-mti])/SN[mtid][mti];
}
Scale = 1.0;
if(mtid > 0) Scale = paramx[j][(mtid-1)*3+NX+2]*paramx[j][(mtid-1)*3+NX+2];
SNRsq[mtid] *= Scale;
mty += SNRsq[mtid];
}
// printf("%f %f\n", SNRsq[0], SNRsq[1]);
// find a sky location roughly consistent with the time delays
skyring(net, paramx[j], skyx[j], pallx[j], SNRsq, freq, D, SN, N, Tobs, rvec[j]);
// find a sky location roughly consistent with the time delay, amplitude ratio and phase difference
//skystart(net, paramx[j], skyx[j], pallx[j], SNRsq, freq, D, SN, N, Tobs, rvec[j]);
dshifts(net, skyx[j], paramx[j]);
pmap(net, pallx[j], paramx[j], skyx[j]);
// get the geocenter reference template. This does depend on the assumed sky location, hence follows skystart
geotemplate(hwave[0], freq, pallx[j], N);
for (mtid = 0; mtid < net->Nifo; ++mtid)
{
wave[j][mtid][0] = 0.0;
wave[j][mtid][N/2] = 0.0;
for (mti = 1; mti < N/2; ++mti)
{
// The sky mapping code puts in the amplitude and phase shifts. The signals only differ due to the whitening
mty = 1.0/sqrt(SN[mtid][mti]);
wave[j][mtid][mti] = hwave[0][mti]*mty;
wave[j][mtid][N-mti] = hwave[0][N-mti]*mty;
}
}
skylikesetup(net, data, wave[j], DD, WW[j], DHc[j], DHs[j], Tobs, N, bn, nt, imin, imax);
fisherskysetup(net, wave[j], HH[j], Tobs, N);
logLsky[j] = skylike(net, skyx[j], DD, WW[j], DHc[j], DHs[j], dtx, nt, 0);
logL[j] = log_likelihood_intrinsic(net, D, paramx[j], freq, SN, N, Tobs);
logLfull[j] = log_likelihood_full(net, D, pallx[j], freq, SN, rho[j], N, Tobs);
printf("%d logL initial %f intrinsic %f full %f sky %f\n", j, logLstart[j], logL[j], logLfull[j], logLsky[j]);
free_double_vector(SNRsq);
free_double_matrix(hwave,net->Nifo);
}
chain = fopen("searchsky.dat","w");
skymcmc(net, Nsky, mxc, chain, paramx, skyx, pallx, who, heat, dtx, nt, DD, WW, DHc, DHs, HH, Tobs, r);
fclose(chain);
// make a map
sprintf(command, "source sky.sh searchsky.dat 0");
system(command);
for(j = 1; j <= NC; j++)
{
pmap(net, pallx[j], paramx[j], skyx[j]);
logL[j] = log_likelihood_intrinsic(net, D, paramx[j], freq, SN, N, Tobs);
logLfull[j] = log_likelihood_full(net, D, pallx[j], freq, SN, rho[j], N, Tobs);
}
for(jj = 1; jj <= NC; jj++)
{
printf("\n");
printf("%d logL initial %f intrinsic %f full %f\n", jj, logLstart[jj], logL[jj], logLfull[jj]);
for (id = 0; id < net->Nifo; ++id)
{
printf("ifo %d %f ", net->labels[id], rho[jj][id]);
}
printf("\n");
}
x = 0.0;
k = who[1];
for(jj = 1; jj <= NC; jj++)
{
if(logLfull[jj] > x)
{
x = logLfull[jj];
k = jj;
}
}
printf("%d %f\n", k, x);
// set threshold that all chains start withing 20% of highest likelihood