forked from VirtualPlanetaryLaboratory/vplanet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary.c
2523 lines (2123 loc) · 88.3 KB
/
binary.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
/**
@file binary.c
@brief Subroutines that control the integration of the circumbinary planet
orbital dynamics module.
@author David Fleming ([dflemin3](https://github.com/dflemin3/))
@date Jan 12 2016
@par Description
\rst
Module to model circumbinary planet dynamics.
.. note:: body 0 = primary star, body 1 = secondary star, body 2+ = CBP
(circumbinary planet(s))
.. note:: The :cite:`Leung2013` theory ONLY applies to the restricted 3
body approximation \ and hence the CBPs are not allowed to graviationally
interact.
\endrst
*/
#include "vplanet.h"
/** Copy body properties from src to dest for cbp */
void BodyCopyBinary(BODY *dest, BODY *src, int foo, int iNumBodies, int iBody) {
dest[iBody].iBodyType = src[iBody].iBodyType;
dest[iBody].dCBPR = src[iBody].dCBPR;
dest[iBody].dCBPZ = src[iBody].dCBPZ;
dest[iBody].dCBPPhi = src[iBody].dCBPPhi;
dest[iBody].dCBPRDot = src[iBody].dCBPRDot;
dest[iBody].dCBPZDot = src[iBody].dCBPZDot;
dest[iBody].dCBPPhiDot = src[iBody].dCBPPhiDot;
dest[iBody].dFreeEcc = src[iBody].dFreeEcc;
dest[iBody].dFreeInc = src[iBody].dFreeInc;
dest[iBody].dLL13N0 = src[iBody].dLL13N0;
dest[iBody].dLL13K0 = src[iBody].dLL13K0;
dest[iBody].dLL13V0 = src[iBody].dLL13V0;
dest[iBody].dR0 = src[iBody].dR0;
dest[iBody].dArgP = src[iBody].dArgP;
dest[iBody].dInc = src[iBody].dInc;
dest[iBody].dLongA = src[iBody].dLongA;
dest[iBody].dLongP = src[iBody].dLongP;
dest[iBody].dLL13PhiAB = src[iBody].dLL13PhiAB;
dest[iBody].dCBPM0 = src[iBody].dCBPM0;
dest[iBody].dCBPZeta = src[iBody].dCBPZeta;
dest[iBody].dCBPPsi = src[iBody].dCBPPsi;
}
/** Only use this function for malloc'ing stuff
Since nothing has to be malloc'ed for binary, do nothing */
void InitializeBodyBinary(BODY *body, CONTROL *control, UPDATE *update,
int iBody, int iModule) {
}
/** No need to allocate anything */
void InitializeUpdateTmpBodyBinary(BODY *body, CONTROL *control, UPDATE *update,
int iBody) {
}
/**************** BINARY options ********************/
/** Free eccentricity, can't be in primary file
Note: Do error checking for negative values */
void ReadFreeEcc(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (dTmp < 0.0 || dTmp >= 1.0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in range [0,1).\n", options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
} else {
body[iFile - 1].dFreeEcc = dTmp;
}
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dFreeEcc = options->dDefault;
}
}
/** Lee + Leung 2013 Mean Motion N0
This parameter cannot exist in primary file */
void ReadLL13N0(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (dTmp <= 0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be greater than 0.\n", options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
body[iFile - 1].dLL13N0 = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else {
if (iFile > 0) {
AssignDefaultDouble(options, &body[iFile - 1].dLL13N0, files->iNumInputs);
}
}
}
/** Lee + Leung 2013 radial epicyclic frequency
This parameter cannot exist in primary file */
void ReadLL13K0(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (dTmp <= 0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be greater than 0.\n", options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
body[iFile - 1].dLL13K0 = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else {
if (iFile > 0) {
AssignDefaultDouble(options, &body[iFile - 1].dLL13K0, files->iNumInputs);
}
}
}
/** Lee + Leung 2013 vertical epicyclic frequency
This parameter cannot exist in primary file */
void ReadLL13V0(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (dTmp <= 0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be greater than 0.\n", options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
body[iFile - 1].dLL13V0 = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else {
if (iFile > 0) {
AssignDefaultDouble(options, &body[iFile - 1].dLL13V0, files->iNumInputs);
}
}
}
/** This parameter cannot exist in the primary file
Free inclination goes from 0 to 180 degrees */
void ReadFreeInc(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (control->Units[iFile].iAngle == 0) { // Input as radians
if (dTmp < 0 || dTmp > PI) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,PI].\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
} else { // Input as Degrees
if (dTmp < 0 || dTmp > 180) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,180].\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
/* Change to radians */
dTmp *= DEGRAD;
}
body[iFile - 1].dFreeInc = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dFreeInc = options->dDefault;
}
}
/** This parameter cannot exist in the primary file
PhiAB goes from [0,360) if in degrees */
void ReadLL13PhiAB(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (control->Units[iFile].iAngle == 0) { // Input as radians
if (dTmp < 0 || dTmp >= 2 * PI) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,2PI).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
} else { // Input as Degrees
if (dTmp < 0 || dTmp >= 360) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,360).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
/* Change to radians */
dTmp *= DEGRAD;
}
body[iFile - 1].dLL13PhiAB = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dLL13PhiAB = options->dDefault;
}
}
/** This parameter cannot exist in the primary file
CBPM0 goes from [0,360) if in degrees */
void ReadCBPM0(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (control->Units[iFile].iAngle == 0) { // Input as radians
if (dTmp < 0 || dTmp >= 2 * PI) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,2PI).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
} else { // Input as Degrees
if (dTmp < 0 || dTmp >= 360) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,360).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
/* Change to radians */
dTmp *= DEGRAD;
}
body[iFile - 1].dCBPM0 = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dCBPM0 = options->dDefault;
}
}
/** This parameter cannot exist in the primary file.
dCBPZeta goes from [0,360) if in degrees */
void ReadCBPZeta(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (control->Units[iFile].iAngle == 0) { // Input as radians
if (dTmp < 0 || dTmp >= 2 * PI) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,2PI).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
} else { // Input as degrees
if (dTmp < 0 || dTmp >= 360) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,360).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
/* Change to radians */
dTmp *= DEGRAD;
}
body[iFile - 1].dCBPZeta = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dCBPZeta = options->dDefault;
}
}
/** This parameter cannot exist in the primary file.
dCBPPsi goes from [0,360) if in degrees */
void ReadCBPPsi(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
SYSTEM *system, int iFile) {
int lTmp = -1;
double dTmp;
AddOptionDouble(files->Infile[iFile].cIn, options->cName, &dTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
if (control->Units[iFile].iAngle == 0) { // Input as radians
if (dTmp < 0 || dTmp >= 2 * PI) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,2PI).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
} else { // Input as degrees
if (dTmp < 0 || dTmp >= 360) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: %s must be in the range [0,360).\n",
options->cName);
}
LineExit(files->Infile[iFile].cIn, lTmp);
}
/* Change to radians */
dTmp *= DEGRAD;
}
body[iFile - 1].dCBPPsi = dTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
body[iFile - 1].dCBPPsi = options->dDefault;
}
}
/** This parameter cannot exist in primary file */
void ReadHaltHolmanUnstable(BODY *body, CONTROL *control, FILES *files,
OPTIONS *options, SYSTEM *system, int iFile) {
int lTmp = -1;
int bTmp;
AddOptionBool(files->Infile[iFile].cIn, options->cName, &bTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
control->Halt[iFile - 1].bHaltHolmanUnstable = bTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
control->Halt[iFile - 1].bHaltHolmanUnstable = 0;
}
}
/** This parameter cannot exist in primary file */
void ReadHaltRocheLobe(BODY *body, CONTROL *control, FILES *files,
OPTIONS *options, SYSTEM *system, int iFile) {
int lTmp = -1;
int bTmp;
AddOptionBool(files->Infile[iFile].cIn, options->cName, &bTmp, &lTmp,
control->Io.iVerbose);
if (lTmp >= 0) {
NotPrimaryInput(iFile, options->cName, files->Infile[iFile].cIn, lTmp,
control->Io.iVerbose);
control->Halt[iFile - 1].bHaltRocheLobe = bTmp;
UpdateFoundOption(&files->Infile[iFile], options, lTmp, iFile);
} else if (iFile > 0) {
control->Halt[iFile - 1].bHaltRocheLobe = 0;
}
}
/** Initialization Options for BINARY */
void InitializeOptionsBinary(OPTIONS *options, fnReadOption fnRead[]) {
int iOpt, iFile;
sprintf(options[OPT_FREEECC].cName, "dFreeEcc");
sprintf(options[OPT_FREEECC].cDescr, "Circumbinary planet free eccentricity");
sprintf(options[OPT_FREEECC].cDefault, "0.0");
sprintf(options[OPT_FREEECC].cDimension, "nd");
options[OPT_FREEECC].dDefault = 0.0;
options[OPT_FREEECC].iType = 2;
options[OPT_FREEECC].bMultiFile = 1;
fnRead[OPT_FREEECC] = &ReadFreeEcc;
sprintf(options[OPT_FREEECC].cLongDescr,
"The free eccentricity of a circumbinary planet with the BINARY "
"module.\n"
"Must be in the range [0,1).");
sprintf(options[OPT_FREEINC].cName, "dFreeInc");
sprintf(options[OPT_FREEINC].cDescr, "Circumbinary planet free inclination");
sprintf(options[OPT_FREEINC].cDefault, "0.0 degrees");
sprintf(options[OPT_FREEINC].cDimension, "angle");
options[OPT_FREEINC].dDefault = 0.0;
options[OPT_FREEINC].iType = 2;
options[OPT_FREEINC].bMultiFile = 1;
fnRead[OPT_FREEINC] = &ReadFreeInc;
sprintf(options[OPT_FREEINC].cLongDescr,
"The free inclination of a circumbinary planet with the BINARY "
"module.\n"
"Must be in the range [0,pi).");
sprintf(options[OPT_LL13PHIAB].cName, "dLL13PhiAB");
sprintf(options[OPT_LL13PHIAB].cDescr,
"Binary Initial Mean Anomaly for use in the BINARY module");
sprintf(options[OPT_LL13PHIAB].cDefault, "0.0 degrees");
sprintf(options[OPT_LL13PHIAB].cDimension, "angle");
options[OPT_LL13PHIAB].dDefault = 0.0;
options[OPT_LL13PHIAB].iType = 2;
options[OPT_LL13PHIAB].bMultiFile = 1;
fnRead[OPT_LL13PHIAB] = &ReadLL13PhiAB;
sprintf(options[OPT_CBPM0].cName, "dCBPM0");
sprintf(options[OPT_CBPM0].cDescr,
"Circumbinary planet initial mean anomaly for use in the BINARY "
"module");
sprintf(options[OPT_CBPM0].cDefault, "0.0 degrees");
sprintf(options[OPT_CBPM0].cDimension, "angle");
options[OPT_CBPM0].dDefault = 0.0;
options[OPT_CBPM0].iType = 2;
options[OPT_CBPM0].bMultiFile = 1;
fnRead[OPT_CBPM0] = &ReadCBPM0;
sprintf(options[OPT_CBPZETA].cName, "dCBPZeta");
sprintf(options[OPT_CBPZETA].cDescr,
"Circumbinary planet initial z oscillation phase angle");
sprintf(options[OPT_CBPZETA].cDefault, "0.0 degrees");
sprintf(options[OPT_CBPZETA].cDimension, "angle");
options[OPT_CBPZETA].dDefault = 0.0;
options[OPT_CBPZETA].iType = 2;
options[OPT_CBPZETA].bMultiFile = 1;
fnRead[OPT_CBPZETA] = &ReadCBPZeta;
sprintf(options[OPT_CBPPSI].cName, "dCBPPsi");
sprintf(options[OPT_CBPPSI].cDescr,
"Circumbinary planet initial R, phi oscillation phase angle");
sprintf(options[OPT_CBPPSI].cDefault, "0.0 degrees");
sprintf(options[OPT_CBPPSI].cDimension, "angle");
options[OPT_CBPPSI].dDefault = 0.0;
options[OPT_CBPPSI].iType = 2;
options[OPT_CBPPSI].bMultiFile = 1;
fnRead[OPT_CBPPSI] = &ReadCBPPsi;
/* Note: One should never actually set LL13 values as they are ALWAYS
* calculated during initialization. I'll leave them here since there
* were useful for debugging and could be useful in the future if higher
* order modifications to the theory are added.
*/
sprintf(options[OPT_LL13N0].cName, "dLL13N0");
sprintf(options[OPT_LL13N0].cDescr, "Lee+Leung 2013 Mean Motion");
sprintf(options[OPT_LL13N0].cDefault, "1 /yr");
sprintf(options[OPT_LL13N0].cDimension, "time^-1");
options[OPT_LL13N0].dDefault = 1. / YEARSEC;
options[OPT_LL13N0].iType = 2;
options[OPT_LL13N0].bMultiFile = 1;
options[OPT_LL13N0].dNeg = 1. / YEARSEC;
sprintf(options[OPT_LL13N0].cNeg, "/Year");
fnRead[OPT_LL13N0] = &ReadLL13N0;
sprintf(options[OPT_LL13K0].cName, "dLL13K0");
sprintf(options[OPT_LL13K0].cDescr,
"Lee+Leung 2013 Radial Epicyclic Frequency");
sprintf(options[OPT_LL13K0].cDefault, "1 /yr");
sprintf(options[OPT_LL13K0].cDimension, "time^-1");
options[OPT_LL13K0].dDefault = 1. / YEARSEC;
options[OPT_LL13K0].iType = 2;
options[OPT_LL13K0].bMultiFile = 1;
options[OPT_LL13K0].dNeg = 1. / YEARSEC;
sprintf(options[OPT_LL13K0].cNeg, "/Year");
fnRead[OPT_LL13K0] = &ReadLL13K0;
sprintf(options[OPT_LL13V0].cName, "dLL13V0");
sprintf(options[OPT_LL13V0].cDescr,
"Lee+Leung 2013 Radial Epicyclic Frequency");
sprintf(options[OPT_LL13V0].cDefault, "1 /yr");
sprintf(options[OPT_LL13V0].cDimension, "time^-1");
options[OPT_LL13V0].dDefault = 1. / YEARSEC;
options[OPT_LL13V0].iType = 2;
options[OPT_LL13V0].bMultiFile = 1;
options[OPT_LL13V0].dNeg = 1. / YEARSEC;
sprintf(options[OPT_LL13V0].cNeg, "/Year");
fnRead[OPT_LL13V0] = &ReadLL13V0;
sprintf(options[OPT_HALTHOLMAN].cName, "bHaltHolmanUnstable");
sprintf(options[OPT_HALTHOLMAN].cDescr,
"Halt when CBP is Holman-Wiegert Unstable?");
sprintf(options[OPT_HALTHOLMAN].cDefault, "0");
options[OPT_HALTHOLMAN].iType = 0;
fnRead[OPT_HALTHOLMAN] = &ReadHaltHolmanUnstable;
sprintf(options[OPT_HALTROCHELOBE].cName, "bHaltRocheLobe");
sprintf(options[OPT_HALTROCHELOBE].cDescr,
"Halt when Roche lobe crossing occurs?");
sprintf(options[OPT_HALTROCHELOBE].cDefault, "0");
options[OPT_HALTROCHELOBE].iType = 0;
fnRead[OPT_HALTROCHELOBE] = &ReadHaltRocheLobe;
}
/** Read all BINARY input options. */
void ReadOptionsBinary(BODY *body, CONTROL *control, FILES *files,
OPTIONS *options, SYSTEM *system, fnReadOption fnRead[],
int iBody) {
int iOpt;
for (iOpt = OPTSTARTBINARY; iOpt < OPTENDBINARY; iOpt++) {
if (options[iOpt].iType != -1) {
fnRead[iOpt](body, control, files, &options[iOpt], system, iBody + 1);
}
}
}
/******************* Verify BINARY ******************/
/*
* Note: update always has [0] as the terminal index since ONLY binary can
* update these variables. Theory does NOT apply if another phenomena tries
* to change a circumbinary planet's orbital motion. Also, only iaBody eqns
* care about are their own. CBP only cares about itself since bodies 0 and 1
* are assured to be stars in binary, hence binary. This was all done
* intentionally to make it more difficult to mess up the binary equations.
*/
void VerifyCBPR(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPR][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPR][0] = 1;
update[iBody].iaBody[update[iBody].iCBPR][0] =
malloc(update[iBody].iNumBodies[update[iBody].iCBPR][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPR][0][0] = iBody;
update[iBody].pdCBPRBinary =
&update[iBody].daDerivProc[update[iBody].iCBPR][0];
}
void VerifyCBPZ(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPZ][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPZ][0] = 1;
update[iBody].iaBody[update[iBody].iCBPZ][0] =
malloc(update[iBody].iNumBodies[update[iBody].iCBPZ][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPZ][0][0] = iBody;
update[iBody].pdCBPZBinary =
&update[iBody].daDerivProc[update[iBody].iCBPZ][0];
}
void VerifyCBPPhi(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPPhi][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPPhi][0] = 1;
update[iBody].iaBody[update[iBody].iCBPPhi][0] = malloc(
update[iBody].iNumBodies[update[iBody].iCBPPhi][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPPhi][0][0] = iBody;
update[iBody].pdCBPPhiBinary =
&update[iBody].daDerivProc[update[iBody].iCBPPhi][0];
}
void VerifyCBPRDot(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPRDot][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPRDot][0] = 1;
update[iBody].iaBody[update[iBody].iCBPRDot][0] = malloc(
update[iBody].iNumBodies[update[iBody].iCBPRDot][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPRDot][0][0] = iBody;
update[iBody].pdCBPRDotBinary =
&update[iBody].daDerivProc[update[iBody].iCBPRDot][0];
}
void VerifyCBPZDot(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPZDot][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPZDot][0] = 1;
update[iBody].iaBody[update[iBody].iCBPZDot][0] = malloc(
update[iBody].iNumBodies[update[iBody].iCBPZDot][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPZDot][0][0] = iBody;
update[iBody].pdCBPZDotBinary =
&update[iBody].daDerivProc[update[iBody].iCBPZDot][0];
}
void VerifyCBPPhiDot(BODY *body, OPTIONS *options, UPDATE *update, double dAge,
int iBody) {
update[iBody].iaType[update[iBody].iCBPPhiDot][0] = 10;
update[iBody].iNumBodies[update[iBody].iCBPPhiDot][0] = 1;
update[iBody].iaBody[update[iBody].iCBPPhiDot][0] = malloc(
update[iBody].iNumBodies[update[iBody].iCBPPhiDot][0] * sizeof(int));
update[iBody].iaBody[update[iBody].iCBPPhiDot][0][0] = iBody;
update[iBody].pdCBPPhiDotBinary =
&update[iBody].daDerivProc[update[iBody].iCBPPhiDot][0];
}
void fnPropsAuxBinary(BODY *body, EVOLVE *evolve, IO *io, UPDATE *update,
int iBody) {
if (body[iBody].iBodyType == 0) { // CBP
// If not including eqns in the matrix, compute main variables on the fly!
SYSTEM *system = NULL; // dummy variable
int iaBody[1] = {iBody}; // Pick out CBP
body[iBody].dCBPR = fndCBPRBinary(body, system, iaBody);
body[iBody].dCBPZ = fndCBPZBinary(body, system, iaBody);
body[iBody].dCBPPhi = fndCBPPhiBinary(body, system, iaBody);
body[iBody].dCBPRDot = fndCBPRDotBinary(body, system, iaBody);
body[iBody].dCBPPhiDot = fndCBPPhiDotBinary(body, system, iaBody);
body[iBody].dCBPZDot = fndCBPZDotBinary(body, system, iaBody);
// Set CBP orbital elements, mean motion
fnvAssignOrbitalElements(body, iBody);
body[iBody].dMeanMotion = fdSemiToMeanMotion(
body[iBody].dR0, (body[0].dMass + body[1].dMass + body[iBody].dMass));
} else if (body[iBody].iBodyType == 1 && iBody == 1) { // Binary
// Correctly set binary's mean motion
body[iBody].dMeanMotion = fdSemiToMeanMotion(
body[iBody].dSemi, (body[0].dMass + body[1].dMass));
// Compute binary's eccentricity from Kecc and Hecc (since they are primary
// variables)
body[iBody].dEcc =
sqrt(pow(body[iBody].dKecc, 2) + pow(body[iBody].dHecc, 2));
body[iBody].dEccSq = body[iBody].dEcc * body[iBody].dEcc;
} else {
return;
}
}
void fnForceBehaviorBinary(BODY *body, MODULE *module, EVOLVE *evolve, IO *io,
SYSTEM *system, UPDATE *update,
fnUpdateVariable ***fnUpdate, int iBody,
int iModule) {
// Anything here?
}
void AssignBinaryDerivatives(BODY *body, EVOLVE *evolve, UPDATE *update,
fnUpdateVariable ***fnUpdate, int iBody) {
if (body[iBody].iBodyType == 0) // Planets are added to matrix
{
fnUpdate[iBody][update[iBody].iCBPR][0] = &fndCBPRBinary;
fnUpdate[iBody][update[iBody].iCBPZ][0] = &fndCBPZBinary;
fnUpdate[iBody][update[iBody].iCBPPhi][0] = &fndCBPPhiBinary;
fnUpdate[iBody][update[iBody].iCBPRDot][0] = &fndCBPRDotBinary;
fnUpdate[iBody][update[iBody].iCBPZDot][0] = &fndCBPZDotBinary;
fnUpdate[iBody][update[iBody].iCBPPhiDot][0] = &fndCBPPhiDotBinary;
}
}
void NullBinaryDerivatives(BODY *body, EVOLVE *evolve, UPDATE *update,
fnUpdateVariable ***fnUpdate, int iBody) {
if (body[iBody].iBodyType == 0) // Planets are added to matrix
{
// Add equations to the matrix
fnUpdate[iBody][update[iBody].iCBPR][0] = &fndUpdateFunctionTiny;
fnUpdate[iBody][update[iBody].iCBPZ][0] = &fndUpdateFunctionTiny;
fnUpdate[iBody][update[iBody].iCBPPhi][0] = &fndUpdateFunctionTiny;
fnUpdate[iBody][update[iBody].iCBPRDot][0] = &fndUpdateFunctionTiny;
fnUpdate[iBody][update[iBody].iCBPZDot][0] = &fndUpdateFunctionTiny;
fnUpdate[iBody][update[iBody].iCBPPhiDot][0] = &fndUpdateFunctionTiny;
}
}
void VerifyBinary(BODY *body, CONTROL *control, FILES *files, OPTIONS *options,
OUTPUT *output, SYSTEM *system, UPDATE *update, int iBody,
int iModule) {
// If binary is being used, ALL bodies must have correct type
if (iBody < 2) { // Primary or secondary star
if (body[iBody].iBodyType != 1) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: In binary, bodies 0, 1 iBodyType must be 1 "
"(star == 1).\n");
fprintf(stderr, "Body 2 must be 0 (planet == 0).\n");
fprintf(stderr, "iBody: %d iBodyType: %d\n", iBody,
body[iBody].iBodyType);
}
exit(EXIT_INPUT);
}
} else { // planets
if (body[iBody].iBodyType != 0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: In binary, bodies 0, 1 iBodyType must be 1 "
"(star == 1).\n");
fprintf(stderr, "Body number > 1 must be 0 (planet == 0).\n");
fprintf(stderr, "iBody: %d iBodyType: %d\n", iBody,
body[iBody].iBodyType);
}
exit(EXIT_INPUT);
}
}
// If binary is being used, ALL bodies must have bBinary == 1
int i;
for (i = 0; i < control->Evolve.iNumBodies; i++) {
if (body[i].bBinary == 0) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr,
"ERROR: In binary, all bodies must have bBinary == 1.\n");
fprintf(stderr, "body[i].bBinary == 0: %d\n", i);
}
exit(EXIT_INPUT);
}
}
// Binary *should* only allow 3 bodies: 2 stars, 1 CBP since in LL13, cbps
// can't interact But if the user wants to, they can have more, but make sure
// they're warned
if (control->Evolve.iNumBodies > 3 && iBody > 2) {
fprintf(stderr,
"WARNING: In binary, Leung and Lee 2013 is a 3 body problem: 2 "
"stars, 1 planet. Include additional planets at your own peril!\n");
}
// For CBP, cannot have dLL13PhiAB set since that is for the binary!
if (body[iBody].iBodyType == 0) {
if (body[iBody].dLL13PhiAB > dTINY) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: The circumbinary planet cannot have dLL13PhiAB "
"set as that is the BINARY's initial mean anomaly.\n");
}
exit(EXIT_INPUT);
}
}
// For binary, only the secondary should have the orbital elements set!
// Also, shouldn't have CBPM0 set
if (body[iBody].iBodyType == 1) {
if (iBody == 0) { // Primary!
// These values default to -1
if (fabs(body[iBody].dInc) + 1 < dTINY ||
fabs(body[iBody].dEcc) + 1 < dTINY ||
fabs(body[iBody].dSemi + 1) < dTINY ||
fabs(body[iBody].dMeanMotion) + 1 < dTINY) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr,
"ERROR: In binary, binary orbital element information can "
"ONLY be in the secondary star (iBody == 1).\n");
}
exit(EXIT_INPUT);
}
} else { // Secondary
// Was dCBPM0, dCBPZeta, dCBPPsi set for one of the stars?
if (body[iBody].dCBPM0 > dTINY || body[iBody].dCBPZeta > dTINY ||
body[iBody].dCBPPsi > dTINY) {
if (control->Io.iVerbose >= VERBERR) {
fprintf(stderr, "ERROR: In binary, only the CBP can have dCBPM0, "
"dCBPZeta, or dCBPPsi set.\n");
}
exit(EXIT_INPUT);
}
}
}
// Initialize the circumbinary planets
if (body[iBody].iBodyType == 0) { // Planets are added to matrix
// Call verifies to properly set up eqns in matrix
VerifyCBPR(body, options, update, body[iBody].dAge, iBody);
VerifyCBPZ(body, options, update, body[iBody].dAge, iBody);
VerifyCBPPhi(body, options, update, body[iBody].dAge, iBody);
VerifyCBPRDot(body, options, update, body[iBody].dAge, iBody);
VerifyCBPZDot(body, options, update, body[iBody].dAge, iBody);
VerifyCBPPhiDot(body, options, update, body[iBody].dAge, iBody);
// Init parameters needed for subsequent cbp motion
// dR0, dMeanMotion MUST be set before any of the frequencies
body[iBody].dR0 =
body[iBody].dSemi; // CBPs Guiding Radius initial equal to dSemi, must
// be set before N0,K0,V0 !!!
body[iBody].dMeanMotion = fdSemiToMeanMotion(
body[iBody].dR0, (body[0].dMass + body[1].dMass + body[iBody].dMass));
body[iBody].dLL13N0 = fndMeanMotionBinary(body, iBody);
body[iBody].dLL13K0 = fndEpiFreqK(body, iBody);
body[iBody].dLL13V0 = fndEpiFreqV(body, iBody);
// Set Planet initial positions, velocities according to LL13 theory
int iaBody[1] = {iBody}; // Pick out CBP
body[iBody].dCBPR = fndCBPRBinary(body, system, iaBody);
body[iBody].dCBPZ = fndCBPZBinary(body, system, iaBody);
body[iBody].dCBPPhi = fndCBPPhiBinary(body, system, iaBody);
body[iBody].dCBPRDot = fndCBPRDotBinary(body, system, iaBody);
body[iBody].dCBPPhiDot = fndCBPPhiDotBinary(body, system, iaBody);
body[iBody].dCBPZDot = fndCBPZDotBinary(body, system, iaBody);
// Init orbital elements
fnvAssignOrbitalElements(body, iBody);
body[iBody].dSemi =
body[iBody].dR0; // Fix semi-major axis to be guiding center
// Set up initial orbital elements that are primary variables
body[iBody].dHecc = body[iBody].dEcc * sin(body[iBody].dLongP);
body[iBody].dKecc = body[iBody].dEcc * cos(body[iBody].dLongP);
}
// Inits if the body is the secondary (sets required binary parameters)
if (body[iBody].iBodyType == 1 && iBody == 1) {
// Set Initial Poincare H, K using imputted dEcc
body[iBody].dHecc = body[iBody].dEcc * sin(body[iBody].dLongP);
body[iBody].dKecc = body[iBody].dEcc * cos(body[iBody].dLongP);
body[iBody].dEccSq = body[iBody].dEcc * body[iBody].dEcc;
}
// Inits for stars: General
if (body[iBody].iBodyType == 1) {
body[iBody].dR0 = 0.0;
body[iBody].dInc = 0.0; // Binary in the plane
body[iBody].dArgP = 0.0;
body[iBody].dLongA = 0.0;
body[iBody].dCBPR = 0.0;
body[iBody].dCBPRDot = 0.0;
body[iBody].dCBPZ = 0.0;
body[iBody].dCBPZDot = 0.0;
body[iBody].dCBPPhi = 0.0;
body[iBody].dCBPPhiDot = 0.0;
body[iBody].dLL13N0 = 0.0;
body[iBody].dLL13K0 = 0.0;
body[iBody].dLL13V0 = 0.0;
}
// Other things that must be set
control->fnForceBehavior[iBody][iModule] = &fnForceBehaviorBinary;
control->fnPropsAux[iBody][iModule] = &fnPropsAuxBinary;
control->Evolve.fnBodyCopy[iBody][iModule] = &BodyCopyBinary;
}
/**************** BINARY Update ****************/
void InitializeUpdateBinary(BODY *body, UPDATE *update, int iBody) {
/* Only planets should be in matrix, if the user is so inclinded */
if (body[iBody].iBodyType == 0) {
if (update[iBody].iNumCBPR == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPR++;
if (update[iBody].iNumCBPZ == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPZ++;
if (update[iBody].iNumCBPPhi == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPPhi++;
if (update[iBody].iNumCBPRDot == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPRDot++;
if (update[iBody].iNumCBPZDot == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPZDot++;
if (update[iBody].iNumCBPPhiDot == 0) {
update[iBody].iNumVars++;
}
update[iBody].iNumCBPPhiDot++;
}
}
void FinalizeUpdateCBPRBinary(BODY *body, UPDATE *update, int *iEqn, int iVar,
int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPR = (*iEqn)++;
}
void FinalizeUpdateCBPZBinary(BODY *body, UPDATE *update, int *iEqn, int iVar,
int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPZ = (*iEqn)++;
}
void FinalizeUpdateCBPPhiBinary(BODY *body, UPDATE *update, int *iEqn, int iVar,
int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPPhi = (*iEqn)++;
}
void FinalizeUpdateCBPRDotBinary(BODY *body, UPDATE *update, int *iEqn,
int iVar, int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPRDot = (*iEqn)++;
}
void FinalizeUpdateCBPZDotBinary(BODY *body, UPDATE *update, int *iEqn,
int iVar, int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPZDot = (*iEqn)++;
}
void FinalizeUpdateCBPPhiDotBinary(BODY *body, UPDATE *update, int *iEqn,
int iVar, int iBody, int iFoo) {
update[iBody].iaModule[iVar][*iEqn] = BINARY;
update[iBody].iNumCBPPhiDot = (*iEqn)++;
}
/***************** BINARY Halts *****************/
/** If the CBP's dSemi is less than the Holman stability limit, it's unstable
and integration ends */
int fbHaltHolmanUnstable(BODY *body, EVOLVE *evolve, HALT *halt, IO *io,
UPDATE *update, fnUpdateVariable ***fnUpdate,
int iBody) {
double a_crit = fndHolmanStability(body);
// If the body is less than critical stability limit
// Check stability for planets
if (body[iBody].iBodyType == 0) {
if (body[iBody].dSemi <= a_crit) {
if (io->iVerbose >= VERBPROG) {
fprintf(stderr,
"HALT: %s's dSemi: %lf AU, Holman-Wiegert critial a: %lf AU.\n",
body[iBody].cName, body[iBody].dSemi / AUM, a_crit / AUM);
}
return 1;
}
return 0;
} else { // Doesn't apply to stars
return 0;
}
}
/** If the secondary enters the roche lobe of the primary, HALT! */
int fbHaltRocheLobe(BODY *body, EVOLVE *evolve, HALT *halt, IO *io,
UPDATE *update, fnUpdateVariable ***fnUpdate, int iBody) {
double r_crit = fndRocheLobe(body);
// Check for roche lobe crossing
if (body[iBody].iBodyType == 1 && iBody == 1) {
if (body[iBody].dSemi <= r_crit) {
if (io->iVerbose >= VERBPROG) {
fprintf(stderr,
"HALT: %s's dSemi: %lf AU, Primary Roche Lobe: %lf AU.\n",
body[iBody].cName, body[iBody].dSemi / AUM, r_crit / AUM);
}
return 1;
}
return 0;
} else { // Doesn't apply to CBP, central star
return 0;
}
}
void CountHaltsBinary(HALT *halt, int *iHalt) {
if (halt->bHaltHolmanUnstable) {
(*iHalt)++;