-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
192 lines (177 loc) · 6.15 KB
/
config.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
/**\brief Config File Implementation for Electric Field Sim
* \author Henry J Schmale
* \date March 11, 2015
* \file config.cpp
*
* Contains the complete text of an example ini file for this program
* and it contains a function to write it out to file.
*
* USAGE:
* argv[0] <s|e> [file]
*
* s - simulate, the simulation parameters must be specified in
* file must be specified
* e - output an example file, if file is specified then it is written
* out to stdout
*
*/
#include "config.h"
#include <iostream>
#include <fstream>
#include <ios>
#include <cassert>
#include <cstdio>
#include <cstring>
extern "C"{ // Not included in this header
#include <iniparser.h>
};
#include <glog/logging.h>
// INI Key Id Constant Strings
#define D_XMIN "GLOBAL:Xmin"
#define D_XMAX "GLOBAL:Xmax"
#define D_YMIN "GLOBAL:Ymin"
#define D_YMAX "GLOBAL:Ymax"
#define D_NUMSRCS "GLOBAL:NumSrcs"
#define D_NUMBALLS "GLOBAL:NumBall"
#define D_DXYRES "GLOBAL:dXYRes"
#define D_DTRES "GLOBAL:dTRes"
#define D_SIMULATE "GLOBAL:simulate"
#define D_TSIM "GLOBAL:TSim"
// any of the constants below need to be processed with snprintf for
// index numbers, before using as a key for iniparser
#define D_SRC_XPOS "SRC_%d:xPos"
#define D_SRC_YPOS "SRC_%d:yPos"
#define D_SRC_CHARGE "SRC_%d:charge"
#define D_BLL_XPOS "BLL_%d:xPos"
#define D_BLL_YPOS "BLL_%d:yPos"
#define D_BLL_MASS "BLL_%d:mass"
#define D_BLL_CHARGE "BLL_%d:charge"
using namespace std;
double XMIN;
double XMAX;
double YMIN;
double YMAX;
double DXY_RES;
double DT_RES;
double TSIM;
bool SIMULATE;
int NUMSRCS;
int NUMBALLS;
int VECCOUNT;
vec2d *vectors;
chargeSrc *charges;
pithBall *balls;
static dictionary *dict;
// this is a private function to load the constants from the INI
// file given.
static int loadConstants(char *fname){
char buffer[50] = {0};
LOG(INFO) << "Now loading constants and allocating "
<< " memory for constants";
CHECK(fname != NULL);
dict = iniparser_load(fname); // load config file with iniparser
CHECK_NOTNULL(dict);
// Load the constants
XMIN = iniparser_getdouble(dict, D_XMIN, 0);
XMAX = iniparser_getdouble(dict, D_XMAX, 0);
YMIN = iniparser_getdouble(dict, D_YMIN, 0);
YMAX = iniparser_getdouble(dict, D_YMAX, 0);
DXY_RES = iniparser_getdouble(dict, D_DXYRES, 0);
DT_RES = iniparser_getdouble(dict, D_DTRES, 0);
TSIM = iniparser_getdouble(dict, D_TSIM, 0);
SIMULATE = iniparser_getboolean(dict, D_SIMULATE, 0);
NUMSRCS = iniparser_getint(dict, D_NUMSRCS, 0);
NUMBALLS = iniparser_getint(dict, D_NUMBALLS, 0);
// Verify information fetched
CHECK((XMAX - XMIN) > 0) << "Invalid XMIN & XMAX in job file";
CHECK((YMAX - YMIN) > 0) << "Invalid YMIN & YMAX in job file";
CHECK(DXY_RES > 0);
LOG(INFO) << "Finished Fetching Global Values from INI file";
if(NUMSRCS > 0){
charges = new chargeSrc[NUMSRCS];
CHECK_NOTNULL(charges);
for(int i = 0; i < NUMSRCS; i++){
snprintf(buffer, 50, D_SRC_XPOS, i);
charges[i].m_xPos = iniparser_getdouble(dict, buffer, 0);
snprintf(buffer, 50, D_SRC_YPOS, i);
charges[i].m_yPos = iniparser_getdouble(dict, buffer, 0);
snprintf(buffer, 50, D_SRC_CHARGE, i);
charges[i].m_charge = iniparser_getdouble(dict, buffer, 0);
LOG(INFO) << "ChargeSrc[" << i << "] charge = "
<< charges[i].m_charge;
}
}else{
LOG(ERROR) << "Invalid value defined in NUMSRCS in job file";
}
if((SIMULATE == true) && (NUMBALLS > 0)){
LOG(INFO) << "Simulation has " << NUMBALLS << "pithballs";
balls = new pithBall[NUMBALLS];
CHECK_NOTNULL(balls);
for(int i = 0; i < NUMBALLS; i++){
snprintf(buffer, 50, D_BLL_XPOS, i);
balls[i].cx = iniparser_getdouble(dict, buffer, 0);
snprintf(buffer, 50, D_BLL_YPOS, i);
balls[i].cy = iniparser_getdouble(dict, buffer, 0);
snprintf(buffer, 50, D_BLL_MASS, i);
balls[i].mass = iniparser_getdouble(dict, buffer, 0);
snprintf(buffer, 50, D_BLL_CHARGE, i);
balls[i].charge = iniparser_getdouble(dict, buffer, 0);
LOG(INFO) << "Ball[" << i << "] charge = "
<< balls[i].charge;
}
}else{
LOG(ERROR) << "Invalid value defined in NUMBALLS in job file";
}
int arraySz = ((XMAX - XMIN) / DXY_RES) * ((YMAX - YMIN) / DXY_RES);
VECCOUNT = arraySz;
vectors = new vec2d[arraySz];
CHECK_NOTNULL(vectors);
LOG(INFO) << "memory allocation and initialization is complete";
return 0;
}
int parseArgs(int argc, char **argv){
FLAGS_log_dir = "./logs/";
google::InitGoogleLogging(argv[0]);
if(argc <= 1){
LOG(FATAL) << "ARG FAIL! SEE PROJECT DOC AT main.cpp of this"
<< " project" << std::endl;
return ARG_FAILED; // No Good
}
if(argv[1][0] == 's'){
if(argc != 3){
LOG(FATAL) << "ARG FAIL! Did you forget a file name. "
<< "That is required for the simulate option.\n"
<< "A config file can be generated with the "
<< "command:\n" << argv[0] << " e <file>"
<< std::endl;
return ARG_FAILED;
}else{
loadConstants(argv[2]);
}
return 1; // it's good
}
if(argv[1][0] == 'e'){
if(argv[2] != NULL){
// output to file
fstream out(argv[2], ios::out | ios::binary);
out << EXAMPLE_INI;
out.close();
}else{
// write to stdout
std::cout << EXAMPLE_INI;
}
return ARG_FAILED;
}else{
LOG(FATAL) << "Bad Args Passed and can't be parsed.\n"
<< "Go read the documentation you stinking monkey";
return ARG_FAILED;
}
}
void shutdown(){
LOG(INFO) << "Begining shutdown procedure";
iniparser_freedict(dict);
delete[] balls;
delete[] vectors;
delete[] charges;
LOG(INFO) << "Finished memory clean up";
}