forked from VirtualPlanetaryLaboratory/vplanet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
control.c
1220 lines (1078 loc) · 35 KB
/
control.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 control.c
@brief These subroutines deal with control, including I/O, units, and files.
@author Rory Barnes ([RoryBarnes](https://github.com/RoryBarnes/))
@date Jan 7 2015
*/
#include "vplanet.h"
void BodyCopyNULL(BODY *dest, BODY *src, int foo, int iNumBodies, int iBody) {
// Nothing
}
/*
* SORTING FUNCTIONS
*/
//! Case-insensitive `strcmp`
int strcicmp(char const *a, char const *b) {
for (;; a++, b++) {
int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a) {
return d;
}
}
}
//! Dummy struct used to sort options
typedef struct {
int index;
char name[OPTLEN];
} SORTED_OPTIONS;
//! Dummy struct used to sort output
typedef struct {
int index;
char name[OUTLEN];
} SORTED_OUTPUT;
//! Comparison function for option names
int compare_option_names(const void *p, const void *q) {
return strcicmp(((SORTED_OPTIONS *)p)->name, ((SORTED_OPTIONS *)q)->name);
}
//! Comparison function for output names
int compare_output_names(const void *p, const void *q) {
return strcicmp(((SORTED_OUTPUT *)p)->name, ((SORTED_OUTPUT *)q)->name);
}
//! Sort the OPTIONS struct by name
void sort_options(OPTIONS *options, int sorted[]) {
int iOpt;
// Sort the options alphabetically by name
SORTED_OPTIONS sorted_options[MODULEOPTEND];
for (iOpt = 0; iOpt < MODULEOPTEND; iOpt++) {
sorted_options[iOpt].index = iOpt;
strcpy(sorted_options[iOpt].name, options[iOpt].cName);
}
qsort(sorted_options, MODULEOPTEND, sizeof(sorted_options[0]),
compare_option_names);
// Copy over to the array of indices
for (iOpt = 0; iOpt < MODULEOPTEND; iOpt++) {
sorted[iOpt] = sorted_options[iOpt].index;
}
}
//! Sort the OUTPUT struct by name
void sort_output(OUTPUT *output, int sorted[]) {
int iOpt;
// Sort the output alphabetically by name
SORTED_OUTPUT sorted_output[MODULEOUTEND];
for (iOpt = 0; iOpt < MODULEOUTEND; iOpt++) {
sorted_output[iOpt].index = iOpt;
strcpy(sorted_output[iOpt].name, output[iOpt].cName);
}
qsort(sorted_output, MODULEOUTEND, sizeof(sorted_output[0]),
compare_output_names);
// Copy over to the array of indices
for (iOpt = 0; iOpt < MODULEOUTEND; iOpt++) {
sorted[iOpt] = sorted_output[iOpt].index;
}
}
/*
* Struct Initialization
*/
void InitializeControl(CONTROL *control, MODULE *module) {
int iBody, iModule;
control->bOutputLapl = 0;
control->iMassRad = malloc(control->Evolve.iNumBodies * sizeof(int));
control->fnForceBehavior =
malloc(control->Evolve.iNumBodies * sizeof(fnForceBehaviorModule *));
control->fnForceBehaviorMulti =
malloc(control->Evolve.iNumBodies * sizeof(fnForceBehaviorModule *));
control->iNumMultiForce = malloc(control->Evolve.iNumBodies * sizeof(int));
control->Halt = malloc(control->Evolve.iNumBodies * sizeof(HALT));
control->fnPropsAux =
malloc(control->Evolve.iNumBodies * sizeof(fnPropsAuxModule *));
control->fnPropsAuxMulti =
malloc(control->Evolve.iNumBodies * sizeof(fnPropsAuxModule *));
for (iBody = 0; iBody < control->Evolve.iNumBodies; iBody++) {
control->fnForceBehavior[iBody] =
malloc(module->iNumModules[iBody] * sizeof(fnForceBehaviorModule));
control->fnPropsAux[iBody] =
malloc(module->iNumModules[iBody] * sizeof(fnPropsAuxModule));
for (iModule = 0; iModule < module->iNumModules[iBody]; iModule++) {
module->fnInitializeControl[iBody][iModule](control, iBody);
control->fnPropsAux[iBody][iModule] = &PropsAuxNULL;
}
}
// Initialize IO error messages
control->Io.baRocheMessage = malloc(control->Evolve.iNumBodies * sizeof(int));
control->Io.baCassiniOneMessage =
malloc(control->Evolve.iNumBodies * sizeof(int));
control->Io.baCassiniTwoMessage =
malloc(control->Evolve.iNumBodies * sizeof(int));
control->Io.baEnterHZMessage =
malloc(control->Evolve.iNumBodies * sizeof(int));
for (iBody = 0; iBody < control->Evolve.iNumBodies; iBody++) {
control->Io.baRocheMessage[iBody] = 0;
control->Io.baCassiniOneMessage[iBody] = 0;
control->Io.baCassiniTwoMessage[iBody] = 0;
control->Io.baEnterHZMessage[iBody] = 0;
}
control->Io.bDeltaTimeMessage = 0;
control->Io.bMutualIncMessage = 0;
// Initialize this value, too. Probably need a whole new structure for
// CheckProgress
control->Io.dMaxMutualInc = 0;
}
/**
This function performs the following tasks:
1) Allocates control->Evolve.fnBodyCopy, iNumMultiProps, tmpUpdate, daDeriv,
and control->iNumMultiProps.
2) Initializes control->bOrbiters
3) Initializes control->Evolve.iNumModules
*/
void InitializeControlEvolve(BODY *body, CONTROL *control, MODULE *module,
UPDATE *update) {
int iBody, iModule, iSubStep;
control->Evolve.fnBodyCopy =
malloc(control->Evolve.iNumBodies * sizeof(fnBodyCopyModule *));
control->Evolve.iNumModules =
malloc(control->Evolve.iNumBodies * sizeof(int));
control->iNumMultiProps = malloc(control->Evolve.iNumBodies * sizeof(int));
control->Evolve.tmpUpdate =
malloc(control->Evolve.iNumBodies * sizeof(UPDATE));
control->Evolve.tmpBody = malloc(control->Evolve.iNumBodies * sizeof(BODY));
InitializeBodyModules(&control->Evolve.tmpBody, control->Evolve.iNumBodies);
for (iBody = 0; iBody < control->Evolve.iNumBodies; iBody++) {
control->Evolve.fnBodyCopy[iBody] =
malloc(module->iNumModules[iBody] * sizeof(fnBodyCopyModule));
for (iModule = 0; iModule < module->iNumModules[iBody]; iModule++) {
control->Evolve.fnBodyCopy[iBody][iModule] = &BodyCopyNULL;
}
}
/* Currently this only matters for RK4 integration. This should
be generalized for any integration method. */
if (control->Evolve.iOneStep == RUNGEKUTTA) {
control->Evolve.daDeriv = malloc(4 * sizeof(double **));
control->Evolve.daDerivProc = malloc(4 * sizeof(double ***));
for (iSubStep = 0; iSubStep < 4; iSubStep++) {
control->Evolve.daDeriv[iSubStep] =
malloc(control->Evolve.iNumBodies * sizeof(double *));
control->Evolve.daDerivProc[iSubStep] =
malloc(control->Evolve.iNumBodies * sizeof(double **));
}
}
// Default to no orbiting bodies
control->bOrbiters = 0;
/* First we must determine all the primary variables. The user may not
input them, but instead a redundant variable. Yet these need to be
defined before we can call InitializeUpdate. */
for (iBody = 0; iBody < control->Evolve.iNumBodies; iBody++) {
/* First pass NumModules from MODULE -> CONTROL->EVOLVE */
control->Evolve.iNumModules[iBody] = module->iNumModules[iBody];
// If any body has an orbit related module initialized, we have orbiters!
if (body[iBody].bEqtide || body[iBody].bDistOrb || body[iBody].bPoise ||
body[iBody].bAtmEsc || body[iBody].bGalHabit || body[iBody].bSpiNBody) {
control->bOrbiters = 1;
}
// Why does bUsingDistOrb exist? Why can't body[iBody].bDistOrb work?
if (!body[iBody].bDistOrb) {
control->Evolve.bUsingDistOrb = 0;
}
}
}
/*
* Help functions
*/
void PrintFileTypes(int iFileType, int bPadString) {
if (iFileType == 0) {
if (bPadString)
printf("Primary Only "
" ");
else
printf("Primary Only");
} else if (iFileType == 1) {
if (bPadString)
printf("Body Only "
" ");
else
printf("Body Only");
} else if (iFileType == 2) {
if (bPadString)
printf("Any "
" ");
else
printf("Any");
}
}
void WriteDescription(char cLongDescr[], char cDescr[], int iMaxChars) {
int bFoo;
int iCharsLeft, iWord, iLineWord, iLine, iNumWords, iLineWordNow, iChar;
char cDescription[MAXARRAY][OPTLEN];
char cLine[MAXARRAY][OPTLEN];
for (iLineWordNow = 0; iLineWordNow < MAXARRAY; iLineWordNow++) {
memset(cLine[iLineWordNow], '\0', OPTLEN);
memset(cDescription[iLineWordNow], '\0', OPTLEN);
}
// Try Long Description first
GetWords(cLongDescr, cDescription, &iNumWords, &bFoo);
if (memcmp(cDescription[0], "null", 4) == 0) {
// No long description, try short
memset(cDescription[0], '\0', OPTLEN);
GetWords(cDescr, cDescription, &iNumWords, &bFoo);
}
iCharsLeft = iMaxChars;
iWord = 0; // counter for word in description
iLineWord = 0; // counter for word in line
iLine = 0;
while (iWord < iNumWords) {
// Extra two is for spaces on either side
while (iCharsLeft > iCharsLeft - strlen(cDescription[iWord]) - 2) {
strcpy(cLine[iLineWord], cDescription[iWord]);
// extra 1 for space
iCharsLeft -= (strlen(cLine[iLineWord]) + 1);
iWord++;
iLineWord++;
if (iWord == iNumWords) {
// Hit end of description inside inner loop
break;
}
}
// Line is full
if (iLine == 0) {
printf("| Description || ");
} else {
printf("| || ");
}
for (iLineWordNow = 0; iLineWordNow < iLineWord; iLineWordNow++) {
// write and erase
printf("%s ", cLine[iLineWordNow]);
}
for (iChar = 0; iChar < iCharsLeft; iChar++) {
printf(" ");
}
printf(" |\n");
// Now reset counters
iCharsLeft = iMaxChars;
for (iLineWordNow = 0; iLineWordNow < MAXARRAY; iLineWordNow++) {
memset(cLine[iLineWordNow], '\0', OPTLEN);
}
iLine++;
iLineWord = 0;
}
printf("+-----------------+--------------------------------------------------"
"------------------+\n");
/* Reset description for next time
for (iLineWordNow = 0; iLineWordNow < MAXARRAY; iLineWordNow++) {
memset(cDescription[iLineWordNow],'\0',OPTLEN);
}
*/
}
void WriteHelpOption(OPTIONS *options, int bLong) {
int iChar, iCharsLeft, iPosChar;
int iMaxChars = 65; // Max # of chars in value field
int iMaxHeaderChar = 80; // Max # of chars in header
int iWord, iNumWords, iLineWord, iDescrWord, iLineWordNow, iLine;
int bFoo; // Dummy for "bContinue" in GetWords
if (memcmp(options->cName, "null", 4)) {
if (bLong == 0) {
// ** Short help **
if (options->dNeg != 0) {
printf("[-]");
}
printf("%s (", options->cName);
// Cast
if (options->iType == 0) {
printf("Bool");
} else if (options->iType == 1) {
printf("Int");
} else if (options->iType == 2) {
printf("Double");
} else if (options->iType == 3) {
printf("String");
} else if (options->iType == 10) {
printf("Bool-Array");
} else if (options->iType == 11) {
printf("Int-Array");
} else if (options->iType == 12) {
printf("Double-Array");
} else if (options->iType == 13) {
printf("String-Array");
}
printf(") -- %s ", options->cDescr);
if (options->dNeg != 0) {
printf(" [Negative = %s] ", options->cNeg);
}
// allowed modules
printf("{Modules = ");
if (options->iModuleBit) {
PrintModuleList(stdout, options->iModuleBit, 0);
} else {
printf("ALL");
}
printf("} ");
// allowed input files
printf("<Files = ");
PrintFileTypes(options->iFileType, 0);
printf("> ");
// default (always last)
printf("(Default = %s).\n", options->cDefault);
} else {
// ** Long help **
// Header
// Properties
printf("+----------------------------------------------------------------"
"----------------------+\n");
printf("| **%s**", options->cName);
for (iPosChar = 0; iPosChar < (iMaxHeaderChar - strlen(options->cName));
iPosChar++) {
printf(" ");
}
printf(" |\n");
// printf("+=================+====================================================================+\n");
printf("+-----------------+----------------------------------------------"
"----------------------+\n");
WriteDescription(options->cLongDescr, options->cDescr, iMaxChars);
/*
// Try Long Description first
GetWords(options->cLongDescr,cDescription,&iNumWords,&bFoo);
if (memcmp(cDescription[0], "null", 4) == 0) {
// No long description, try short
memset(cDescription[0],'\0',OPTLEN);
GetWords(options->cDescr,cDescription,&iNumWords,&bFoo);
}
iCharsLeft = iMaxChars;
iWord = 0; // counter for word in description
iLineWord = 0; // counter for word in line
iLine = 0;
while (iWord < iNumWords) {
// Extra two is for spaces on either side
while (iCharsLeft > iCharsLeft - strlen(cDescription[iWord]) - 2)
{ strcpy(cLine[iLineWord],cDescription[iWord]);
// extra 1 for space
iCharsLeft -= (strlen(cLine[iLineWord])+1);
iWord++;
iLineWord++;
if (iWord == iNumWords) {
// Hit end of description inside inner loop
break;
}
}
// Line is full
if (iLine == 0) {
printf("| Description || ");
} else {
printf("| || ");
}
for (iLineWordNow = 0; iLineWordNow < iLineWord; iLineWordNow++) {
// write and erase
printf("%s ",cLine[iLineWordNow]);
}
for (iChar = 0; iChar < iCharsLeft; iChar++) {
printf(" ");
}
printf(" |\n");
// Now reset counters
iCharsLeft = iMaxChars;
for (iLineWordNow = 0; iLineWordNow < MAXARRAY; iLineWordNow++) {
memset(cLine[iLineWordNow],'\0',OPTLEN);
}
iLine++;
iLineWord = 0;
}
printf("+-----------------+--------------------------------------------------------------------+\n");
// Reset description for next time
for (iLineWordNow = 0; iLineWordNow < MAXARRAY; iLineWordNow++) {
memset(cDescription[iLineWordNow],'\0',OPTLEN);
}
*/
// Type
int typelen;
char *typestr;
if (options->iType == 0) {
typestr = "Bool";
} else if (options->iType == 1) {
typestr = "Int";
} else if (options->iType == 2) {
typestr = "Double";
} else if (options->iType == 3) {
typestr = "String";
} else if (options->iType == 10) {
typestr = "Bool-Array";
} else if (options->iType == 11) {
typestr = "Int-Array";
} else if (options->iType == 12) {
typestr = "Double-Array";
} else if (options->iType == 13) {
typestr = "String-Array";
} else {
fprintf(stderr, "ERROR: Unknown value for typestr in "
"control.c:WriteHelpOption.\n");
exit(EXIT_UNITS);
}
printf("| Type || %s", typestr);
for (typelen = 0; typelen < (iMaxChars - strlen(typestr)); typelen++) {
printf(" ");
}
printf(" |\n+-----------------+------------------------------------------"
"--------------------------+\n");
// Custom Unit
if (options->bNeg == 1) {
printf("| Custom unit || %s", options->cNeg);
int unitlen;
for (unitlen = 0; unitlen < (iMaxChars - strlen(options->cNeg));
unitlen++) {
printf(" ");
}
printf(" |\n");
printf("+-----------------+--------------------------------------------"
"------------------------+\n");
}
if (options->iType == 2) {
printf("| Dimension(s) || %s", options->cDimension);
int dimlen;
for (dimlen = 0; dimlen < (iMaxChars - strlen(options->cDimension));
dimlen++) {
printf(" ");
}
printf(" |\n");
printf("+-----------------+--------------------------------------------"
"------------------------+\n");
}
// Module List
if (options->iModuleBit) {
printf("| Modules || ");
PrintModuleList(stdout, options->iModuleBit, 1);
printf(" |\n");
} else {
printf("| Modules || ALL "
" |\n");
}
printf("+-----------------+----------------------------------------------"
"----------------------+\n");
// File List
printf("| Files || ");
PrintFileTypes(options->iFileType, 1);
printf(" |\n");
printf("+-----------------+----------------------------------------------"
"----------------------+\n");
// Default Value
printf("| Default value || %s", options->cDefault);
for (iChar = 0; iChar < (iMaxChars - strlen(options->cDefault));
iChar++) {
printf(" ");
}
printf(" |\n+-----------------+------------------------------------------"
"--------------------------+\n");
// Allowed Values
if (memcmp(options->cValues, "null", 4)) {
printf("| Allowed values || %s", options->cValues);
int alvalen;
for (alvalen = 0; alvalen < (iMaxChars - strlen(options->cValues));
alvalen++) {
printf(" ");
}
printf(" |\n+-----------------+----------------------------------------"
"----------------------------+"
"\n\n");
} else {
printf("\n");
}
}
}
}
void WriteHelpOutput(OUTPUT *output, int bLong) {
int iMaxChars = 65; // Max # of chars in value field
int iMaxHeaderChar = 80; // Max # of chars in header
if (memcmp(output->cName, "null", 4)) {
if (bLong == 0) {
// ** Short help **
if (output->bNeg == 1) {
printf("[-]");
}
printf("%s -- %s.", output->cName, output->cDescr);
if (output->bNeg == 1) {
printf(" [Negative = %s]", output->cNeg);
}
printf("\n");
} else {
// ** Long help **
// Properties
printf("+----------------------------------------------------------------"
"----------------------+\n");
printf("| **%s**", output->cName);
int i;
for (i = 0; i < (iMaxHeaderChar - strlen(output->cName)); i++) {
printf(" ");
}
printf(" |\n");
printf("+=================+=============================================="
"======================+\n");
WriteDescription(output->cLongDescr, output->cDescr, iMaxChars);
// Long description
// if (memcmp(output->cLongDescr,"null",4)) {
// printf("\n**Overview**\n");
// printf("%s\n",output->cLongDescr);
// } else {
// printf("**Description** %s\n", output->cDescr);
// }
// Negative Option
if (output->bNeg != 0) {
printf("| Custom unit || %s", output->cNeg);
int unitlen;
for (unitlen = 0; unitlen < (iMaxChars - strlen(output->cNeg));
unitlen++) {
printf(" ");
}
printf(" |\n");
printf("+-----------------+--------------------------------------------"
"------------------------+\n");
}
// Module List
if (output->iModuleBit) {
printf("| Modules || ");
PrintModuleList(stdout, output->iModuleBit, 1);
printf(" |\n");
} else {
printf("| Modules || ALL "
" |\n");
}
printf("+-----------------+----------------------------------------------"
"----------------------+\n\n");
// printf("\n");
// printf("**Description** %s\n", output->cDescr);
// printf("+-----------------+------------------------------------+\n");
// // Long description
// if (memcmp(output->cLongDescr,"null",4)) {
// printf("%s\n\n",output->cLongDescr);
// }
}
}
}
void HelpOptions(OPTIONS *options, int bLong) {
int iOpt;
// Sort the OPTIONS struct
int sorted[MODULEOPTEND];
sort_options(options, sorted);
if (!bLong) {
printf("Format: [Negative forces units] Name -- Description [Negative "
"unit] {Compatible modules} <Permited files> (Default value)\n\n");
}
for (iOpt = 0; iOpt < MODULEOPTEND; iOpt++) {
WriteHelpOption(&options[sorted[iOpt]], bLong);
}
}
void HelpOutput(OUTPUT *output, int bLong) {
int iOut;
// Sort the OUTPUT struct
int sorted[MODULEOUTEND];
sort_output(output, sorted);
for (iOut = 0; iOut < MODULEOUTEND; iOut++) {
WriteHelpOutput(&output[sorted[iOut]], bLong);
}
}
void Help(OPTIONS *options, OUTPUT *output, char exe[], int bLong) {
printf("\n\t\tHelp Message for %s\n\t\t", exe);
char *pch = strchr(exe, '\0');
int sz = (int)(pch - exe) + 17;
int i;
for (i = 0; i < sz; i++) {
printf("-");
}
printf("\n\n");
printf("Lead Developer: Rory Barnes ([email protected])\n\n");
printf("\nVPLanet is a general purpose planetary evolution integrator. From "
"the "
"command\n");
printf("line, enter one optional command line option and one file name, "
"e.g.:\n\n"
"> vplanet vpl.in\n\n"
"where vpl.in is the \"primary\" input file consisting of options and "
"a list\n"
"of files that contain the initial conditions for all bodies in a "
"system.\n"
"The code then simulates planetary systen evolution forward or "
"backward in\n"
"time. This onboard help provides information regarding the input and "
"output\n"
"files, command line options, and each option and output parameter. "
"For more\n"
"more information, see "
"https://virtualplanetarylaboratory.github.io/vplanet,\n"
"or consult the examples directory.\n\n");
printf("Support for VPLanet has been provided through grants by NASA, the "
"NSF.\n\n");
printf("==================== "
"============================================================\n");
printf("Command Line Options Description "
" \n");
printf("-------------------- "
"------------------------------------------------------------\n");
printf("-v, -verbose -- Maximum verbosity, i.e. display all "
"warnings and updates.\n");
printf("-q, -quiet -- No verbosity, i.e. nothing printed to "
"device.\n");
printf("-h, -help -- Display short help.\n");
printf("-H, -Help -- Display extended help.\n");
printf("==================== "
"============================================================\n\n");
printf("Input File Structure\n");
printf("--------------------\n\n");
printf("- Options must be the first string on the line and must be written\n"
" exactly as shown below; the options are case-sensitive.\n"
"- All characters to the right of a # sign are treated as a comment\n"
" and are ignored. Blank lines are also ignored.\n"
"- Options that take an array of arguments may span multiple lines if "
"a $ is\n"
" placed at the end of the line.\n"
"- Output parameters (the arguments to %s) are not case "
"sensitive, and\n",
options[OPT_OUTPUTORDER].cName);
printf(" need only enough characters to be unambiguous.\n\n");
printf("Output File Structure\n");
printf("---------------------\n\n");
printf("Output files consist columns of data separated by white space in\n"
"the same order as listed in %s.\n\n",
options[OPT_OUTPUTORDER].cName);
printf("Input Options\n");
printf("-------------\n\n");
HelpOptions(options, bLong);
printf("\n\nOutput Parameters\n");
printf("---------------------\n\n");
if (!bLong) {
printf("Format: [Negative forces units] Name -- Description [Negative "
"unit]\n\n");
}
printf("These options follow the argument %s.\n\n",
options[OPT_OUTPUTORDER].cName);
HelpOutput(output, bLong);
exit(0);
}
/*
* I/O
*/
void LineExit(char cFile[], int iLine) {
fprintf(stderr, "\t%s: Line %d\n", cFile, iLine + 1);
exit(EXIT_INPUT);
}
char *sLower(char cString[]) {
int iPos;
for (iPos = 0; cString[iPos]; iPos++) {
cString[iPos] = tolower(cString[iPos]);
}
return cString;
}
void fprintd(FILE *fp, double x, int iExp, int iDig) {
double dMin, dMax;
dMax = pow(10, iExp);
dMin = pow(10, -iExp);
if ((fabs(x) > dMax || fabs(x) < dMin) && x != 0) {
if (iDig == 0) {
fprintf(fp, "%.0e", x);
}
if (iDig == 1) {
fprintf(fp, "%.1e", x);
}
if (iDig == 2) {
fprintf(fp, "%.2e", x);
}
if (iDig == 3) {
fprintf(fp, "%.3e", x);
}
if (iDig == 4) {
fprintf(fp, "%.4e", x);
}
if (iDig == 5) {
fprintf(fp, "%.5e", x);
}
if (iDig == 6) {
fprintf(fp, "%.6e", x);
}
if (iDig == 7) {
fprintf(fp, "%.7e", x);
}
if (iDig == 8) {
fprintf(fp, "%.8e", x);
}
if (iDig == 9) {
fprintf(fp, "%.9e", x);
}
if (iDig == 10) {
fprintf(fp, "%.10e", x);
}
if (iDig == 11) {
fprintf(fp, "%.11e", x);
}
if (iDig == 12) {
fprintf(fp, "%.12e", x);
}
if (iDig == 13) {
fprintf(fp, "%.13e", x);
}
if (iDig == 14) {
fprintf(fp, "%.14e", x);
}
if (iDig == 15) {
fprintf(fp, "%.15e", x);
}
if (iDig == 16) {
fprintf(fp, "%.16e", x);
}
} else {
if (iDig == 0) {
fprintf(fp, "%.0lf", x);
}
if (iDig == 1) {
fprintf(fp, "%.1lf", x);
}
if (iDig == 2) {
fprintf(fp, "%.2lf", x);
}
if (iDig == 3) {
fprintf(fp, "%.3lf", x);
}
if (iDig == 4) {
fprintf(fp, "%.4lf", x);
}
if (iDig == 5) {
fprintf(fp, "%.5lf", x);
}
if (iDig == 6) {
fprintf(fp, "%.6lf", x);
}
if (iDig == 7) {
fprintf(fp, "%.7lf", x);
}
if (iDig == 8) {
fprintf(fp, "%.8lf", x);
}
if (iDig == 9) {
fprintf(fp, "%.9lf", x);
}
if (iDig == 10) {
fprintf(fp, "%.10lf", x);
}
if (iDig == 11) {
fprintf(fp, "%.11lf", x);
}
if (iDig == 12) {
fprintf(fp, "%.12lf", x);
}
if (iDig == 13) {
fprintf(fp, "%.13lf", x);
}
if (iDig == 14) {
fprintf(fp, "%.14lf", x);
}
if (iDig == 15) {
fprintf(fp, "%.15lf", x);
}
if (iDig == 16) {
fprintf(fp, "%.16lf", x);
}
}
}
/*
* Unit Conversions
*/
/* Return proper length conversion */
double fdUnitsLength(int iType) {
if (iType == U_METER) {
return 1;
} else if (iType == U_CENTIMETER) {
return 0.01;
} else if (iType == U_KILOMETER) {
return 1000;
} else if (iType == U_SOLARRADIUS) {
return RSUN;
} else if (iType == U_EARTHRADIUS) {
return REARTH;
} else if (iType == U_JUPRADIUS) {
return RJUP;
} else if (iType == U_AU) {
return AUM;
} else {
fprintf(stderr, "ERROR: Unknown iUnitLength %d.\n", iType);
exit(EXIT_UNITS);
}
}
void fsUnitsLength(int iType, char cUnit[]) {
if (iType == U_METER) {
sprintf(cUnit, "m");
} else if (iType == U_CENTIMETER) {
sprintf(cUnit, "cm");
} else if (iType == U_KILOMETER) {
sprintf(cUnit, "km");
} else if (iType == U_SOLARRADIUS) {
sprintf(cUnit, "Rsun");
} else if (iType == U_EARTHRADIUS) {
sprintf(cUnit, "Rearth");
} else if (iType == U_JUPRADIUS) {
sprintf(cUnit, "Rjupiter");
} else if (iType == U_AU) {
sprintf(cUnit, "au");
} else {
fprintf(stderr, "ERROR: Unknown iUnitLength %d.\n", iType);
exit(EXIT_UNITS);
}
}
/* Return proper time conversion */
double fdUnitsTime(int iType) {
if (iType == U_SECOND) {
return 1;
} else if (iType == U_DAY) {
return DAYSEC;
} else if (iType == U_YEAR) {
return YEARSEC;
} else if (iType == U_MYR) {
return 1e6 * YEARSEC;
} else if (iType == U_GYR) {
return 1e9 * YEARSEC;
} else {
fprintf(stderr, "ERROR: Unknown iUnitTime: %d.\n", iType);
exit(EXIT_UNITS);
}
}
void fsUnitsTime(int iType, char cUnit[]) {
if (iType == U_SECOND) {
sprintf(cUnit, "sec");
} else if (iType == U_DAY) {
sprintf(cUnit, "day");
} else if (iType == U_YEAR) {
sprintf(cUnit, "year");
} else if (iType == U_MYR) {
sprintf(cUnit, "Myr");
} else if (iType == U_GYR) {
sprintf(cUnit, "Gyr");
} else {
fprintf(stderr, "ERROR: Unknown iUnitTime: %d.\n", iType);
exit(EXIT_UNITS);
}
}
double fdUnitsMass(int iType) {
if (iType == U_KILOGRAM) {
return 1;
} else if (iType == U_GRAM) {
return 1e-3;
} else if (iType == U_SOLARMASS) {
return MSUN;
} else if (iType == U_EARTHMASS) {
return MEARTH;
} else if (iType == U_JUPITERMASS) {
return MJUP;
} else if (iType == U_NEPTUNEMASS) {
return MNEP;
} else {
fprintf(stderr, "ERROR: Unknown iUnitMass: %d.\n", iType);
exit(EXIT_UNITS);
}
}
void fsUnitsMass(int iType, char cUnit[]) {
if (iType == U_KILOGRAM) {
sprintf(cUnit, "kg");
} else if (iType == U_GRAM) {
sprintf(cUnit, "gm");
} else if (iType == U_SOLARMASS) {
sprintf(cUnit, "Msun");
} else if (iType == U_EARTHMASS) {
sprintf(cUnit, "Mearth");
} else if (iType == U_JUPITERMASS) {
sprintf(cUnit, "Mjupiter");
} else if (iType == U_NEPTUNEMASS) {
sprintf(cUnit, "Mneptune");
} else {
fprintf(stderr, "ERROR: Unknown iUnitMass: %d.\n", iType);
exit(EXIT_UNITS);
}
}
double fdUnitsAngle(int iType) {
if (iType == U_RADIANS) {
return 1;
} else if (iType == U_DEGREES) {
return DEGRAD;
} else {
fprintf(stderr, "ERROR: Unknown Angle type %d\n.", iType);
exit(EXIT_UNITS);
}
}