-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathvplanet.c
203 lines (170 loc) · 5.64 KB
/
vplanet.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
/**
@file vplanet.c
@brief The main entry point for the code. All the magic starts here.
@author Rory Barnes ([RoryBarnes](https://github.com/RoryBarnes/))
@date May 7 2014
*/
#include "vplanet.h"
/*
#ifdef DEBUG
#define _GNU_SOURCE
#include <fenv.h>
#endif
*/
/* Do not change these values */
const double dHUGE = DBL_MAX; // This is the largest possible double value
// according to <float.h>
const double dTINY = 1. / DBL_MAX; // This is the smallest possibled double
// value according to <float.h>
/* Do not change these values */
/*!
Actual implementation of the main function; called from in `int main()` below.
We need this wrapper so we can call `main_impl` from Python.
*/
int main_impl(int argc, char *argv[]) {
#ifdef DEBUG
#ifdef __x86_64__
_MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID);
_MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_OVERFLOW);
_MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_DIV_ZERO);
//_MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_UNDERFLOW);
fprintf(stderr, "INFO: Floating point trapping enabled.\n");
#else
fprintf(stderr,
"WARNING: Floating point trapping only enabled for x86 "
"architectures.\n");
#endif
#endif
// struct timeval start, end;
/* Fix CPU time calculation someday
gettimeofday(&start, NULL);
time_t dStartTime;
dStartTime = time(NULL);
*/
int iArg, iVerbose, iQuiet, iOverwrite;
OPTIONS *options;
OUTPUT *output;
CONTROL control;
UPDATE *update;
BODY *body;
MODULE module;
FILES files;
SYSTEM system;
char *sPrimaryFile = NULL;
fnReadOption fnRead[MODULEOPTEND]; // XXX Pointers?
fnWriteOutput fnWrite[MODULEOUTEND];
fnUpdateVariable ***fnUpdate;
fnIntegrate fnOneStep;
control.sGitVersion = NULL;
#ifdef GITVERSION
fvFormattedString(&control.sGitVersion, GITVERSION);
#else
fvFormattedString(&control.sGitVersion, "Unknown");
#endif
system.cName = NULL;
/** Must initialize all options and outputs for all modules
independent of what is selected. This allows a complete
help screen as well as checks during ReadOptions. This
also requires the only modifications outside of module.c:
Add explicit references to all modules at the end of
options.c, output.c and util.c.
*/
options = malloc(MODULEOPTEND * sizeof(OPTIONS));
InitializeOptions(options, fnRead);
output = malloc(MODULEOUTEND * sizeof(OUTPUT));
InitializeOutput(&files, output, fnWrite);
/* Set to IntegrationMethod to 0, so default can be
assigned if necessary */
control.Evolve.iOneStep = 0;
/* Copy executable file name to the files struct. */
files.cExe = NULL;
fvFormattedString(&files.cExe, argv[0]); // XXX This isn't working!
if (argc == 1) {
fprintf(stderr,
"Usage: %s [-v, -verbose] [-q, -quiet] [-h, -help] [-H, -Help] "
"<file>\n",
argv[0]);
exit(EXIT_EXE);
}
iVerbose = -1;
iQuiet = -1;
iOverwrite = -1;
control.Io.iVerbose = -1;
control.Io.bOverwrite = -1;
/* Check for flags */
for (iArg = 1; iArg < argc; iArg++) {
if (memcmp(argv[iArg], "-v", 2) == 0) {
control.Io.iVerbose = 5;
iVerbose = iArg;
}
if (memcmp(argv[iArg], "-q", 2) == 0) {
control.Io.iVerbose = 0;
iQuiet = iArg;
}
if (memcmp(argv[iArg], "-f", 2) == 0) {
control.Io.bOverwrite = 1;
iOverwrite = iArg;
}
if (memcmp(argv[iArg], "-h", 2) == 0) {
Help(options, output, files.cExe, 0);
}
if (memcmp(argv[iArg], "-H", 2) == 0) {
Help(options, output, files.cExe, 1);
}
}
if (iQuiet != -1 && iVerbose != -1) {
fprintf(stderr, "ERROR: -v and -q cannot be set simultaneously.\n");
exit(EXIT_EXE);
}
/* Now identify input file, usually vpl.in */
for (iArg = 1; iArg < argc; iArg++) {
if (iArg != iVerbose && iArg != iQuiet && iArg != iOverwrite) {
fvFormattedString(&sPrimaryFile, argv[iArg]);
}
}
CheckFileExists(sPrimaryFile);
/* Read input files */
ReadOptions(&body, &control, &files, &module, options, output, &system,
&update, fnRead, sPrimaryFile);
if (control.Io.iVerbose >= VERBINPUT) {
printf("Input files read.\n");
}
/* Check that user options are mutually compatible */
VerifyOptions(body, &control, &files, &module, options, output, &system,
update, &fnOneStep, &fnUpdate);
if (control.Io.iVerbose >= VERBINPUT) {
printf("Input files verified.\n");
}
control.Evolve.dTime = 0;
control.Evolve.bFirstStep = 1;
if (control.Io.bLog) {
WriteLog(body, &control, &files, &module, options, output, &system, update,
fnUpdate, fnWrite, 0);
if (control.Io.iVerbose >= VERBPROG) {
printf("Log file written.\n");
}
}
/* Perform evolution */
if (control.Evolve.bDoForward || control.Evolve.bDoBackward) {
Evolve(body, &control, &files, &module, output, &system, update, fnUpdate,
fnWrite, fnOneStep);
/* If evolution performed, log final system parameters */
if (control.Io.bLog) {
WriteLog(body, &control, &files, &module, options, output, &system,
update, fnUpdate, fnWrite, 1);
if (control.Io.iVerbose >= VERBPROG) {
printf("Log file updated.\n");
}
}
}
// gettimeofday(&end, NULL);
if (control.Io.iVerbose >= VERBPROG) {
printf("Simulation completed.\n");
// printf("Total time: %.4e [sec]\n",
// difftime(end.tv_usec,start.tv_usec)/1e6);
}
exit(0);
}
int main(int argc, char *argv[]) {
main_impl(argc, argv);
}