-
Notifications
You must be signed in to change notification settings - Fork 2
/
phodymm.cpp
5550 lines (4836 loc) · 163 KB
/
phodymm.cpp
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
#define demcmc_compile 0
#define celerite_compile 1
// This code written in C by Sean M Mills
// To compile lcout:
// make sure demcmc_compile is defined as 0
// Compile with:
// g++ -w -O3 -o lcout -I/home/smills/celerite/celerite/cpp/include -I/home/smills/celerite/celerite/cpp/lib/eigen_3.3.3 -lm -lgsl -lgslcblas -fpermissive phodymm.cpp
// or
// gcc -w -O3 -o lcout.c -lm -lgsl -lgslcblas -fpermissive phodymm.cpp
// Run with:
// ./lcout demcmc.in kep35.pldin [[-rv0=rvs0.file] [-rv1=rvs1.file] ... ]
// To compile demcmc:
// make sure demcmc_compile is defined as 1
// mpic++ -w -Ofast -o demcmc -I/home/smills/celerite/celerite/cpp/include -I/home/smills/celerite/celerite/cpp/lib/eigen_3.3.3 -lm -lgsl -lgslcblas -lmpi -fpermissive phodymm.cpp
// or
// mpic++ -w -Ofast -o demcmc -lm -lgsl -lgslcblas -lmpi -fpermissive phodymm.cpp
// Run with:
// mpirun ./demcmc demcmc.in kep.pldin
// To compile longterm stability
// make sure demcmc_compile is defined as 3
// Compile with:
// $ gcc -Ofast -o stability -lgsl -lgslcblas -fopenmp phodymm.c
// g++ -w -O3 -o lcout -lm -lgsl -lgslcblas -fpermissive phodymm.cpp
#if (demcmc_compile==1)
#include <mpi.h>
#endif
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv.h>
#include <memory.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <float.h>
#include <gsl/gsl_multimin.h>
#if (celerite_compile == 1)
// For CELERITE
#include <cmath>
#include <iostream>
#include <Eigen/Core>
#include "celerite/celerite.h"
using Eigen::VectorXd;
#endif
int CELERITE;
int NCELERITE=4;
int RVCELERITE;
int NRVCELERITE=4;
// These variables are now all defined in the input file instead of here
// turn on N temperature annealing
int NTEMPS;
// turn on one sided inclination distribution
int IGT90;
// turn on positive dilution
int INCPRIOR;
// Limb darkening (Pal+2011, or Maxsted+2018)
int LDLAW;
// dilution capped?
int DIGT0;
// sqrt(e) as parameter?
int SQRTE;
// restrict masses to > 0
int MGT0;
double MASSHIGH;
int MASSPRIOR;
double *MASSPRIORCENTERS;
double *MASSPRIORSIGMAS;
// density cuts
int DENSITYCUTON;
double *MAXDENSITY;// g/cm^3
double MSUNGRAMS = 1.98855e33; // g
double RSUNCM = 6.95700e10; // cm
// eccentricity cuts.
int ECUTON;
double *EMAX;
// turn on eccentricity prior.
int EPRIOR = 0;
int *EPRIORV;
// this sigma is the sigma parameter in a Rayleigh distribution
double ESIGMA;
// define rayleigh distribution
double rayleighpdf (double x) {
double sigma = ESIGMA;
if (x < 0.0) {
printf(" rayleigh pdf requires positive x value\n");
exit(0);
}
double f = x / (sigma*sigma) * exp( -x*x / (2*sigma*sigma) );
return f;
}
// define normal distribution
double normalpdf (double x) {
double sigma = ESIGMA;
double f = (1.0 / (sigma * sqrt(2.*M_PI))) *
exp(-x*x / (2.0 * sigma * sigma));
return f;
}
// Spectroscopy Constraints - Currently only works for single star. Don't use it for multistar.
int SPECTROSCOPY;
// Assumes assymetric Gaussian
double SPECRADIUS=0.;
double SPECERRPOS=0.;
double SPECERRNEG=0.;
// Spectroscopy Constraints - Currently only works for single star. Don't use it for multistar.
int MASSSPECTROSCOPY;
// Assumes assymetric Gaussian
double SPECMASS=0.;
double MASSSPECERRPOS=0.;
double MASSSPECERRNEG=0.;
int RANK;
int SIZE;
double PRINTEPOCH = 800.0;
//// Global Variables
// Initialized in main
int RESTART;
int RVS;
// Initialized in getinput function call
int MULTISTAR;
int NBODIES;
int PPERPLAN;
int PSTAR = 5; // Number of parameters for central star
int PPERWALKER;
char *OUTSTR;
int NPL;
double EPOCH;
long NWALKERS;
long NSTEPS;
int CADENCESWITCH;
char TFILE[1000];
int *PARFIX;
double T0;
double T1;
unsigned long OUTPUTINTERVAL;
unsigned long SEED;
double DISPERSE;
double OPTIMAL;
double RELAX;
int NPERBIN;
double BINWIDTH;
double *PSTEP;
double *SSTEP;
double *STEP;
int STEPFLAG;
double *CSTEP;
int BIMODF;
int *BIMODLIST;
double OFFSETMULT;
double OFFSETMIN;
double OFFSETMINOUT;
double DIST2DIVISOR;
int LTE;
int SPLITINCO;
int XYZFLAG;
int *XYZLIST;
int *RVARR;
char **RVFARR;
int RVJITTERFLAG;
int NSTARSRV;
int NTELESCOPES;
int RVJITTERTOT = 0;
int TTVJITTERFLAG;
int TTVJITTERTOT = 0;
int NSTARSTTV;
int NTELESCOPESTTV;
int OOO = 2;
int CONVERT=1;
#if (demcmc_compile==3)
double TINTERVAL = 1000.0;
double AFRACSTABLE = 0.10;
#endif
// Variables for Fitting additional TTVs
// as of now the ttv input files MUST be SORTED numerically and at same epoch
int TTVCHISQ;
long **NTTV; //number
double **TTTV; // time
double **ETTV; //error
double **MTTV; //modeled
// Often used system constants
const int SOFD = sizeof(double);
const int SOFDS = sizeof(double*);
const int SOFI = sizeof(int);
const int SOFIS = sizeof(int*);
///* Integration parameters */
#define DY 1e-14 ///* Error allowed in parameter values per timestep. */
#define HStart 1e-5 ///* Timestep to start. If you get NaNs, try reducing this. */
///* Some physical constants */
#define G 2.9591220363e-4 ///* Newton's constant, AU^3*days^-2 */
#define RSUNAU 0.0046491 ///* solar radius in au */
#define REARTHORSUN 0.009171 ///* earth radius divided by solar radius */
#define MPSTOAUPD 5.77548327363993e-7 ///* meters per second to au per day conversion factor */
#define MSOMJ 1.04737701464237e3 ///* solar mass in terms of jupiter masses */
#define CAUPD 173.144632674240 ///* speed of light in AU per day */
//Note in dpintegrator and related code:
///* y vector is [x,y,z,v_x,v_y,v_z] */
///* f is d/dt y */
// Check if two doubles are equal (at the DBL_EPSILON level)
int dbleq (double a, double b) {
return fabs(a-b) < DBL_EPSILON;
}
// Find the greater of two values
int compare (const void* a, const void* b) {
double dbl_a = * ( (double*) a);
double dbl_b = * ( (double*) b);
if (dbl_a < dbl_b) return -1;
else if (dbl_b < dbl_a) return 1;
else return 0;
}
// set a 6*npl element vector equal to another 6*npl element vector
int seteq (int npl, double ynew[], const double y[]) {
int i;
for(i=0;i<6*npl;i++) ynew[i]=y[i];
}
// N-body interaction between planets with position vector y and masses in params
int func (double t, const double y[], double f[], void *params) {
int i,j;
int i1,i2;
double * npl_masses = (double *)params;
double * masses = &npl_masses[1];
double dnpl = npl_masses[0];
const int npl = dnpl;
double gmc1, gmc2, gm1, gm2;
double rc1m3, rc2m3, r12m3;
for(i=0; i<npl; i++) {
gmc1 = G*(masses[0]+masses[i+1]);
rc1m3 = pow(pow(y[i*6+0],2)+pow(y[i*6+1],2)+pow(y[i*6+2],2),-3.0/2);
for(j=0; j<3; j++) {
f[i*6+j] = y[i*6+3+j]; /* x dot = v */
f[i*6+3+j] = -gmc1*y[i*6+j]*rc1m3; /* Pull of the star. */
}
}
/* Interaction between each pair of planets. */
/* Eqn 6.8,6.9 in Murray and Dermott */
/* Astrocentric coordinates are used (i.e. center of star is origin) */
for(i1=0; i1<npl-1; i1++) {
gm1 = G*masses[i1+1];
rc1m3 = pow(pow(y[i1*6+0],2)+pow(y[i1*6+1],2)+pow(y[i1*6+2],2),-3.0/2);
for(i2=i1+1; i2<npl; i2++) {
gm2 = G*masses[i2+1];
rc2m3 = pow(pow(y[i2*6+0],2)+pow(y[i2*6+1],2)+pow(y[i2*6+2],2),-3.0/2);
r12m3 = pow(pow(y[i1*6+0]-y[i2*6+0],2)+pow(y[i1*6+1]-y[i2*6+1],2)+pow(y[i1*6+2]-y[i2*6+2],2),-3.0/2);
for(j=0; j<3; j++) f[i1*6+3+j] += -gm2*( (y[i1*6+j]-y[i2*6+j])*r12m3 + y[i2*6+j]*rc2m3 );
for(j=0; j<3; j++) f[i2*6+3+j] += -gm1*( (y[i2*6+j]-y[i1*6+j])*r12m3 + y[i1*6+j]*rc1m3 );
}
}
return GSL_SUCCESS;
}
// We don't use the Jacobian, but still need to define a pointer for gsl
void *jac;
// Helper for checking if certain parameters are out of
int check_boundaries( double *p0local, long nw) {
int notallowed = 0;
const int npl = NPL;
const int pperwalker = PPERWALKER;
const int pperplan = PPERPLAN;
const int sofd = SOFD;
int ip;
for (ip=0; ip<npl; ip++) {
// make sure i and Omega angles are not cycling through:
if ( p0local[nw*pperwalker+ip*pperplan+4] < 0.0 || p0local[nw*pperwalker+ip*pperplan+4] > 180.0 ) notallowed=1;
if ( p0local[nw*pperwalker+ip*pperplan+5] < -180.0 || p0local[nw*pperwalker+ip*pperplan+5] > 180.0 ) notallowed=1;
// make sure i>=90 or i<= 90
if ( (IGT90==1) && (p0local[nw*pperwalker+ip*pperplan+4] < 90.0) ) notallowed=1;
if ( (IGT90==2) && (p0local[nw*pperwalker+ip*pperplan+4] > 90.0) ) notallowed=1;
// make sure m>=0
if ( MGT0 && (p0local[nw*pperwalker+ip*pperplan+6] < 0.0) ) notallowed=1;
//make sure density is allowed
if (DENSITYCUTON) {
double massg = p0local[nw*pperwalker+ip*pperplan+6] / MSOMJ * MSUNGRAMS;
double radcm = p0local[nw*pperwalker+npl*pperplan+1] * p0local[nw*pperwalker+ip*pperplan+7] * RSUNCM;
double rhogcc = massg / (4./3.*M_PI*radcm*radcm*radcm);
if (rhogcc > MAXDENSITY[ip]) notallowed=1;
}
// make sure radius ratios are positive
if (p0local[nw*pperwalker+ip*pperplan+7] < 0.0) notallowed=1;
}
if ( DIGT0 && (p0local[nw*pperwalker+npl*pperplan+4] < 0.0) ) notallowed=1;
// check that RVJITTER >= 0
if (RVS) {
if (RVJITTERFLAG) {
int ki;
for (ki=0; ki<RVJITTERTOT; ki++) {
if (p0local[nw*pperwalker+npl*pperplan+(5+ki)] < 0.0) {
notallowed=1;
}
}
}
}
// check that celerite terms >= 0
if (CELERITE) {
int ki;
for (ki=0; ki<NCELERITE; ki++) {
if (p0local[nw*pperwalker+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+ki] < 0.0) {
notallowed=1;
}
}
}
if (RVCELERITE) {
int ki;
for (ki=0; ki<NRVCELERITE; ki++) {
if (p0local[nw*pperwalker+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+CELERITE*4+ki] < 0.0) {
notallowed=1;
}
}
}
double* evector = malloc(npl*sofd);
if (ECUTON) {
if (SQRTE) {
int i0;
for (i0=0; i0<npl; i0++) {
evector[i0] = pow(sqrt( pow(p0local[nw*pperwalker+i0*pperplan+2], 2) + pow(p0local[nw*pperwalker+i0*pperplan+3], 2) ), 2);
}
} else {
int i0;
for (i0=0; i0<npl; i0++) {
evector[i0] = sqrt( pow(p0local[nw*pperwalker+i0*pperplan+2], 2) + pow(p0local[nw*pperwalker+i0*pperplan+3], 2) );
}
}
// make sure e_d cos w_d is within its range
double* emax = EMAX;
int i0;
for (i0=0; i0<npl; i0++) {
if (evector[i0] > emax[i0] ) notallowed=1;
}
}
// make sure ld coefficients in range
// this prior only applies to the Pal+2011 code
if ( (LDLAW == 0) && (p0local[nw*pperwalker+npl*pperplan+2] < 0.0 || p0local[nw*pperwalker+npl*pperplan+2] > 1.0 || p0local[nw*pperwalker+npl*pperplan+3] < 0.0 || p0local[nw*pperwalker+npl*pperplan+3] > 1.0) ) notallowed=1;
free(evector);
return notallowed;
}
// Helper for computing the priors
double compute_priors(double *p0local, int i) {
double neg2logliketemp = 0.;
const int pperwalker = PPERWALKER;
const int pperplan = PPERPLAN;
const int sofd = SOFD;
const int npl = NPL;
double photoradius;
photoradius = p0local[i*pperwalker+npl*pperplan+1];
double photomass;
photomass = p0local[i*pperwalker+npl*pperplan+0];
double* evector;
evector = malloc(npl*sofd);
if (EPRIOR) {
if (SQRTE) {
int i0;
for (i0=0; i0<npl; i0++) {
evector[i0] = pow(sqrt( pow(p0local[i*pperwalker+i0*pperplan+2], 2) + pow(p0local[i*pperwalker+i0*pperplan+3], 2) ), 2);
}
} else {
int i0;
for (i0=0; i0<npl; i0++) {
evector[i0] = sqrt( pow(p0local[i*pperwalker+i0*pperplan+2], 2) + pow(p0local[i*pperwalker+i0*pperplan+3], 2) );
}
}
int i0;
for (i0=0; i0<NPL; i0++) {
if (EPRIORV[i0]) {
double priorprob;
if (EPRIOR==1) {
priorprob = rayleighpdf(evector[i0]);
} else if (EPRIOR==2) {
priorprob = normalpdf(evector[i0]);
}
neg2logliketemp += -2.0*log( priorprob );
}
}
}
if (MASSPRIOR) {
int i0;
for (i0=0; i0<NPL; i0++) {
double massi = p0local[i*pperwalker+i0*pperplan+6] / MSOMJ;
double massratioi = massi / photomass;
neg2logliketemp += -2.*log(1./(sqrt(2.*M_PI)*MASSPRIORSIGMAS[i0]));
neg2logliketemp += pow((massratioi - MASSPRIORCENTERS[i0])/MASSPRIORSIGMAS[i0], 2);
}
}
if (SPECTROSCOPY) {
double normalization = 1./(sqrt(2.*M_PI));
normalization *= 2./(SPECERRPOS+SPECERRNEG); // normalized asymmetric Gaussian
neg2logliketemp += -2.*log(normalization);
if (photoradius > SPECRADIUS) {
neg2logliketemp += pow( (photoradius - SPECRADIUS) / SPECERRPOS, 2 );
}
else {
neg2logliketemp += pow( (photoradius - SPECRADIUS) / SPECERRNEG, 2 );
}
}
if (MASSSPECTROSCOPY) {
double normalization = 1./(sqrt(2.*M_PI));
normalization *= 2./(MASSSPECERRPOS+MASSSPECERRNEG); // normalized asymmetric Gaussian
neg2logliketemp += -2.*log(normalization);
if (photomass > SPECMASS) neg2logliketemp += pow( (photomass - SPECMASS) / MASSSPECERRPOS, 2 );
else neg2logliketemp += pow( (photomass - SPECMASS) / MASSSPECERRNEG, 2 );
}
if (INCPRIOR) {
int i0;
for (i0=0; i0<npl; i0++) {
neg2logliketemp += -2.0*log( sin(p0local[i*pperwalker+i0*pperplan+4] *M_PI/180.) );
}
}
free(evector);
return neg2logliketemp;
}
#if (celerite_compile == 1)
// Wrapper for computing a celerite fit and returning the effective chi^2
double celerite_fit(double*** flux_rvs, double* p0local, int i, int rvflag, int verbose) {
const int npl = NPL;
const int pperplan = PPERPLAN;
const int pperwalker = PPERWALKER;
const int sofd = SOFD;
double neg2loglike;
double *xs = flux_rvs[rvflag][0];
long maxil = (long) xs[0];
double *trueys = flux_rvs[rvflag][1];
double *modelys = flux_rvs[rvflag][2];
double *es = flux_rvs[rvflag][3];
double *diffys = malloc(sofd*maxil);
int il;
for (il=0; il<maxil; il++) {
diffys[il] = trueys[il+1]-modelys[il+1];
}
double *yvarp = malloc(sofd*maxil);
for (il=0; il<maxil; il++) {
yvarp[il] = es[il+1]*es[il+1];
}
double *xp = &xs[1];
int j_real = 0;
int j_complex;
double jitter, k1, k2, k3, S0, w0, Q;
jitter = p0local[pperwalker*i+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+NCELERITE*CELERITE*rvflag+0];
S0 = p0local[pperwalker*i+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+NCELERITE*CELERITE*rvflag+1];
w0 = p0local[pperwalker*i+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+NCELERITE*CELERITE*rvflag+2];
Q = p0local[pperwalker*i+npl*pperplan+5+RVJITTERTOT+TTVJITTERTOT+NCELERITE*CELERITE*rvflag+3];
if (Q >= 0.5) {
j_complex = 1;
} else {
j_complex = 2;
}
VectorXd a_real(j_real),
c_real(j_real),
a_comp(j_complex),
b_comp(j_complex),
c_comp(j_complex),
d_comp(j_complex);
if (Q >= 0.5) {
k1 = S0*w0*Q;
k2 = sqrt(4.*Q*Q - 1.);
k3 = w0/(2.*Q);
a_comp << k1;
b_comp << k1/k2;
c_comp << k3;
d_comp << k3*k2;
} else {
j_complex = 2;
k1 = 0.5*S0*w0*Q;
k2 = sqrt(1. - 4.*Q*Q);
k3 = w0/(2.*Q);
a_comp << k1*(1. + 1./k2), k1*(1. - 1./k2);
b_comp << 0., 0.;
c_comp << k3*(1. - k2), k3*(1. + k2);
d_comp << 0., 0.;
}
if (verbose) {
printf("%lf %lf %lf %lf\n", jitter, S0, w0, Q);
}
VectorXd x = VectorXd::Map(xp, maxil);
VectorXd yvar = VectorXd::Map(yvarp, maxil);
VectorXd dy = VectorXd::Map(diffys, maxil);
celerite::solver::CholeskySolver<double> solver;
solver.compute(
jitter,
a_real, c_real,
a_comp, b_comp, c_comp, d_comp,
x, yvar // Note: this is the measurement _variance_
);
// see l.186-192 in celerite.py
double logdet, diffs, llike;
logdet = solver.log_determinant();
diffs = solver.dot_solve(dy);
llike = -0.5 * (diffs + logdet);
neg2loglike = diffs+logdet;
if (verbose) {
printf("Celerite params:\n");
printf("a+comp=%25.17lf\n", a_comp[0]);
printf("b+comp=%25.17lf\n", b_comp[0]);
printf("c+comp=%25.17lf\n", c_comp[0]);
printf("d+comp=%25.17lf\n", d_comp[0]);
printf("diffs=%lf\n", diffs);
printf("logdet=%lf\n", logdet);
printf("llike=%lf\n", llike);
VectorXd prediction = solver.predict(dy, x);
printf("predicted\n");
char tmtefstr[1000];
//memset(tmtefstr, '\0', sizeof(tmtefstr));
char str1[100];
if (rvflag) {
strcpy(str1, "rv");
} else {
strcpy(str1, "lc");
}
strcpy(tmtefstr, str1);
strcat(tmtefstr, "_");
strcat(tmtefstr, OUTSTR);
strcat(tmtefstr, ".gp_");
strcat(tmtefstr, str1);
strcat(tmtefstr, "out");
printf("Saving GP fit to: %s\n", tmtefstr);
FILE *tmtef;
tmtef = fopen(tmtefstr, "a");
long ijk;
for (ijk=0; ijk<maxil; ijk++) {
fprintf(tmtef, "%.12lf \n", prediction[ijk]);
}
fclose(tmtef);// = openf(tmtstr,"w");
printf("saved\n");
}
free(yvarp);
free(diffys);
return neg2loglike;
}
#endif
#if (celerite_compile == 0)
// dummy function if celerite is not used to compile the code
double celerite_fit(double*** flux_rvs, double* p0local, int i, int rvflag, int verbose) {}
#endif
// Compute lightcurve if only 1 star / luminous object
double ***dpintegrator_single (double ***int_in, double **tfe, double **tve, double **nte, int *cadencelist) {
const int cadenceswitch = CADENCESWITCH;
double t0 = T0;
double t1 = T1;
int nperbin = NPERBIN;
double binwidth = BINWIDTH;
const int lte = LTE;
const int sofi = SOFI;
const int sofd = SOFD;
const int sofds = SOFDS;
long kk = (long) tfe[0][0];
int nplplus1 = int_in[0][0][0];
const int npl = nplplus1-1;
double tstart = int_in[1][0][0]; //epoch
double rstar = int_in[3][0][0];
double c1 = int_in[3][0][1];
double c2 = int_in[3][0][2];
double dilute = int_in[3][0][3];
const gsl_odeiv_step_type * T
/* = gsl_odeiv_step_bsimp; 14 s */
= gsl_odeiv_step_rk8pd; /* 3 s */
/* = gsl_odeiv_step_rkf45; 14 s */
/* = gsl_odeiv_step_rk4; 26 s */
gsl_odeiv_step * s
= gsl_odeiv_step_alloc (T, 6*npl);
gsl_odeiv_control * c
= gsl_odeiv_control_y_new (DY, 0.0);
gsl_odeiv_evolve * e
= gsl_odeiv_evolve_alloc (6*npl);
long i;
double mu[npl+1];
double npl_mu[npl+2];
gsl_odeiv_system sys = {func, jac, 6*npl, npl_mu};
double t; /* this is the given epoch. */
double hhere;
double y[6*npl], yin[6*npl];
//note:rad is in units of rp/rstar
double *rad = malloc(npl*sofd);
mu[0] = int_in[2][0][0];
for (i=0; i<npl; i++) {
mu[i+1] = int_in[2][i+1][0];
int ih;
for (ih=0; ih<6; ih++) {
yin[i*6+ih] = int_in[2][i+1][ih+1];
}
rad[i] = int_in[2][i+1][7];
}
memcpy(&npl_mu[1], mu, (npl+1)*sofd);
npl_mu[0] = npl;
double mtot=mu[0];
for(i=0;i<npl;i++) mtot += mu[i+1];
double yhone[6*npl], dydt[6*npl], yerrhone[6*npl];
double thone,tstep,dist,vtrans;
double vstar,astar;
int transitcount[npl];
int pl;
for (pl=0;pl<npl;pl++) transitcount[pl]=-1;
#if ( demcmc_compile==3)
double msys[npl];
msys[0]=mu[0]+mu[1];
for (i=1; i<npl; i++) msys[i] = msys[i-1] + mu[i+1];
double *statetokep(double x, double y, double z, double vx, double vy, double vz, double m); //prototype
double *keptoorb(double x, double y, double z, double vx, double vy, double vz, double m); //prototype
double *amax = malloc(npl*sofd);
double *amin = malloc(npl*sofd);
for (i=0; i<npl; i++) {
double *kepelementsin = statetokep(yin[0+i*6], yin[1+i*6], yin[2+i*6], yin[3+i*6], yin[4+i*6], yin[5+i*6], msys[i]);
double aorig = kepelementsin[0];
free(kepelementsin);
amax[i] = aorig*(1.0+AFRACSTABLE);
amin[i] = aorig*(1.0-AFRACSTABLE);
}
#endif
/* Setup forward integration */
seteq(npl, y, yin);
double dp[npl], ddpdt, dpold[npl] ; /* dp = dot-product = x.v */
for(pl=0; pl<npl; pl++) dp[pl]=y[0+pl*6]*y[3+pl*6]+y[1+pl*6]*y[4+pl*6];
double ps2, dist2; /* ps2 = projected separation squared. */
t = tstart;
double h = HStart;
int k;
long maxtransits = 10000;
int toomany=0;
int ntarrelem = 6;
double *transitarr = malloc((maxtransits+1)*ntarrelem*sofd);
if (transitarr == NULL) {
printf("Allocation Error\n");
exit(0);
}
long ntransits = 0;
long vv = 0;
// This messes up reduced chi squared if your rv values are outside of the integration time!!!
// But we are only comparing relative chi squareds
// Plus just don't include rv times that aren't within your integration bounds, because that's silly
double *rvarr;
double *rvtarr;
int onrv = 0;
int rvcount, startrvcount;
if (RVS) {
vv = (long) tve[0][0];
rvarr = calloc(vv+1, sofd);
rvarr[0] = (double) vv;
rvtarr = malloc((vv+2)*sofd);
rvtarr[0] = -HUGE_VAL;
rvtarr[vv+1] = HUGE_VAL;
memcpy(&rvtarr[1], &tve[0][1], vv*sofd);
startrvcount = 1;
while (rvtarr[startrvcount] < tstart) startrvcount+=1;
rvcount = startrvcount;
}
long vvt;
double *ttvarr;
double **ttvarr2;
if (TTVCHISQ) {
ttvarr2 = malloc(npl*sofds);
for (i=0; i<npl; i++) {
ttvarr2[i] = malloc(NTTV[i][0]*sofd);
}
vvt = (long) nte[0][0];
ttvarr = malloc((vvt+1)*sofd);
ttvarr[0] = (double) vvt;
}
#if ( demcmc_compile==3)
int stabilitystatus=0;
double **orbarray = malloc(npl*sofds);
int onorbout = 0;
double orbt = tstart+TINTERVAL/2.0;
int densestart = 1;
double denseinterval=0.1;
double densemax=5000.0;
if (densestart) {
orbt = tstart;// + denseinterval;// /2.0;
}
long nposorbouts=0;
long nnegorbouts=0;
char stabstr[1000];
strcpy(stabstr, "stability_");
strcat(stabstr, OUTSTR);
strcat(stabstr, ".out");
FILE *stabout = fopen(stabstr, "w");
fprintf(stabout, "t\t\t\tpnum\t\tP\t\ta\t\te\t\ti\t\t\tlo\t\t\tbo\t\t\tf\n");
char stabstr2[1000];
strcpy(stabstr2, "stability_xyz_");
strcat(stabstr2, OUTSTR);
strcat(stabstr2, ".out");
FILE *stabout2 = fopen(stabstr2, "w");
fprintf(stabout2, "t\t\t\tpnum\t\tx\t\ty\t\tz\t\tvx\t\tvy\t\tvz\t\t\n");
double tfirst, tlast;
#endif
#if ( demcmc_compile == 0 || demcmc_compile == 3)
FILE **directory = malloc(npl*sizeof(FILE*));
for (i=0; i<npl; i++) {
char str[30];
sprintf(str, "tbv00_%02i.out", i+1);
directory[i]=fopen(str,"w");
}
double tbvmax = 36500.0;
#endif
double **tmte;
double **rvtmte;
double dist2divisor=DIST2DIVISOR;
double baryz;
double eps=1e-10;
double printepoch=PRINTEPOCH;
#if (demcmc_compile != 3)
for (i=0; i<npl; i++) {
int ih;
for (ih=0; ih<6; ih++) {
if (isnan(yin[i*6+ih])) {
goto exitgracefully;
}
}
}
#endif
int *ttvi;
int *ttviinit;
if (TTVCHISQ) {
ttvi = calloc(npl, sofi);
ttviinit = malloc(npl*sofi);
for (i=0; i<npl; i++) {
while(NTTV[i][ttvi[i]+1] < 0) {
ttvi[i]+=1;
}
ttviinit[i] = ttvi[i];
}
}
#if ( demcmc_compile==0 )
printf("starting integration.\n");
printf("t=%lf tepoch=%lf t1=%lf t0=%lf h=%lf\n", t, tstart, t1, t0, h);
#endif
// Main forward integration loop
while (t < t1 ) {
#if ( demcmc_compile==0 )
int printtime=0;
double htimehere;
if (h+t > printepoch) {
printtime=1;
htimehere=h;
h = printepoch-t;
if (h<0) {
printf("Error, bad printepoch\n");
}
printepoch=HUGE_VAL;
}
#endif
if (RVS) {
onrv=0;
if (h + t > rvtarr[rvcount]) {
onrv = 1;
hhere = h;
h = rvtarr[rvcount] - t;
}
}
#if ( demcmc_compile==3)
onorbout = 0;
if (h + t > orbt) {
onorbout = 1;
hhere = h;
h = orbt - t;
}
#endif
int status = gsl_odeiv_evolve_apply (e, c, s,
&sys,
&t, t1,
&h, y);
if (status != GSL_SUCCESS)
break;
#if ( demcmc_compile==0 )
if (printtime) {
char outfile2str[80];
strcpy(outfile2str, "xyz_adjusted_");
strcat(outfile2str, OUTSTR);
strcat(outfile2str, ".pldin");
FILE *outfile2 = fopen(outfile2str, "a");
fprintf(outfile2, "planet x y z v_x v_y v_z m rpors ");
fprintf(outfile2, "\n");
double pnum = 0.1;
for (i=0; i<NPL; i++) {
fprintf(outfile2, "%1.1lf", pnum);
int j;
for (j=0; j<6; j++) {
fprintf(outfile2, "\t%.15lf", y[6*i+j]);
}
fprintf(outfile2, "\t%.15lf", mu[i+1]*MSOMJ);
fprintf(outfile2, "\t%.15lf", rad[i]);
fprintf(outfile2, "\n");
pnum+=0.1;
}
fprintf(outfile2, "%.15lf ; mstar\n", mu[0]);
fprintf(outfile2, "%.15lf ; rstar\n", rstar);
fprintf(outfile2, "%.15lf ; c1\n", c1);
fprintf(outfile2, "%.15lf ; c2\n", c2);
fprintf(outfile2, "%.15lf ; dilute\n", dilute);
fprintf(outfile2, " ; These coordinates are stellar centric\n");
fprintf(outfile2, " ; Tepoch = %0.15lf\n", PRINTEPOCH);
fclose(outfile2);
printtime=0;
h=htimehere;
}
#endif
#if ( demcmc_compile==3)
if (onorbout == 1) {
h = hhere;
for (i=0; i<npl; i++) {
orbarray[i] = statetokep(y[0+i*6], y[1+i*6], y[2+i*6], y[3+i*6], y[4+i*6], y[5+i*6], msys[i]);
double *aeielem = keptoorb(orbarray[i][0], orbarray[i][1], orbarray[i][2], orbarray[i][3], orbarray[i][4], orbarray[i][5], msys[i]);
fprintf(stabout, "%.13lf\t%i\t%.13lf\t%.13lf\t%.13lf\t%.13lf\t%.13lf\t%.13lf\t%.13lf\n", orbt, i, aeielem[0], orbarray[i][0], orbarray[i][1], orbarray[i][2]*180.0/M_PI, orbarray[i][3]*180.0/M_PI, orbarray[i][4]*180.0/M_PI, orbarray[i][5]*180.0/M_PI);
fprintf(stabout2, "%.13lf\t%i\t%.13lf\t%.13lf\t%.13lf\t%.13lf\t%.13lf\t%.13lf\n", orbt, i, y[0+i*6], y[1+i*6], y[2+i*6], y[3+i*6], y[4+i*6], y[5+i*6]);
if (orbarray[i][0] > amax[i] || orbarray[i][0] < amin[i]) {
stabilitystatus = 1;
tlast = orbt;
}
free(orbarray[i]);
free(aeielem);
}
if (densestart) {
if (t<densemax) {
orbt += denseinterval;
} else {
if (t<365.*100000.) orbt += TINTERVAL;
else orbt += TINTERVAL*50.;
}
} else {
if (t<365.*100000.) orbt += TINTERVAL;
else orbt += TINTERVAL*50.;
}
nposorbouts++;
if (nposorbouts % 1 == 0) fflush(stabout);
if (nposorbouts % 1 == 0) fflush(stabout2);
}
if (stabilitystatus == 1) {
printf("Went Unstable -> Break\n");
break;
}
#endif
if (RVS) {
if (onrv ==1) {
h = hhere;
func(t, y, dydt, mu);
double vstar = 0;
for (i=0; i<npl; i++) {
vstar += -y[i*6+5]*mu[i+1]/mtot;
}
rvarr[rvcount] = vstar;
rvcount += 1;
}
}
dist2 = pow(y[0],2)+pow(y[1],2)+pow(y[2],2);
for(pl=0; pl<npl; pl++) { /* Cycle through the planets, searching for a transit. */
dpold[pl]=dp[pl];
dp[pl]=y[0+pl*6]*y[3+pl*6]+y[1+pl*6]*y[4+pl*6];
ps2=y[0+pl*6]*y[0+pl*6]+y[1+pl*6]*y[1+pl*6];