forked from DualSPHysics/DualSPHysics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJCfgRunBase.cpp
More file actions
359 lines (328 loc) * 13.6 KB
/
JCfgRunBase.cpp
File metadata and controls
359 lines (328 loc) * 13.6 KB
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
//HEAD_DSPH
/*
EPHYSLAB Environmental Physics Laboratory, Universidade de Vigo, Ourense, Spain.
School of Mechanical, Aerospace and Civil Engineering, University of Manchester, Manchester, U.K.
This file is part of DualSPHysics.
DualSPHysics is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
DualSPHysics is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with DualSPHysics. If not, see .
*/
/// \file JCfgRunBase.cpp \brief Implements the class \ref JCfgRunBase.
#include "JCfgRunBase.h"
#ifdef JCfgRunBase_UseDSCfg
#include "JAppInfo.h"
#include "JDsphConfig.h"
#endif
using namespace std;
//==============================================================================
/// Constructor.
//==============================================================================
JCfgRunBase::JCfgRunBase(bool noparms):NoParms(noparms){
ClassName="JCfgRunBase";
Reset();
}
//==============================================================================
/// Initialization of variables.
//==============================================================================
void JCfgRunBase::Reset(){
PrintInfo=false; ParmDef=0;
CreateDirs=true;
CsvSepComa=false;
}
//==============================================================================
/// Load configuration from DsphConfig.xml.
//==============================================================================
void JCfgRunBase::LoadDsphConfig(std::string path){
#ifdef JCfgRunBase_UseDSCfg
JDsphConfig dsphconfig;
dsphconfig.Init(path);
if(!dsphconfig.GetFileCfg().empty())printf("LoadDsphConfig> %s\n",fun::GetPathLevels(dsphconfig.GetFileCfg(),3).c_str());
if(dsphconfig.GetCreateDirs()!=-1)CreateDirs=(dsphconfig.GetCreateDirs()==1);
if(dsphconfig.GetCsvSeparator()!=-1)CsvSepComa=(dsphconfig.GetCsvSeparator()==1);
#endif
}
//==============================================================================
/// Loads execution parameters from the command line.
//==============================================================================
void JCfgRunBase::LoadArgv(int argc,char** argv){
Reset();
//-Loads configuration from DsphConfig.xml.
#ifdef JCfgRunBase_UseDSCfg
LoadDsphConfig(AppInfo.GetProgramPath());
#endif
//-Loads execution parameters.
const int MAXOPTS=100;
string *optlis=new string[MAXOPTS];
int optn=0;
for(int c=0;c1;c++){
string tex=fun::StrTrim(argv[c+1]);
int pos=int(tex.find(" "));
if(pos>0){
while(pos>0){
bool divide=((tex[0]=='-' || tex[0]=='#') || (pos+2size() && ((tex[pos+1]=='-' && tex[pos+2]!=' ') || tex[pos+1]=='#')));
//printf(" tex[%s] pos:%d divide=%d\n",tex.c_str(),pos,(divide? 1: 0));
if(divide){
if(optn>=MAXOPTS)Run_Exceptioon("Has exceeded the maximum configuration options.");
optlis[optn]=tex.substr(0,pos); optn++;
tex=fun::StrTrim(tex.substr(pos+1)); //-StrTrim() removes spaces between options.
pos=int(tex.find(" "));
}
else pos=int(tex.find(" ",pos+1));
}
}
if(optn>=MAXOPTS)Run_Exceptioon("Has exceeded the maximum configuration options.");
if(!tex.empty()){//-Ignores empty parameters.
optlis[optn]=tex; optn++;
}
}
//for(int c=0;c
if(optn || NoParms)LoadOpts(optlis,optn,0,"");
delete[] optlis;
if(!optn && !NoParms)PrintInfo=true;
if(!PrintInfo){ //-Configuracion por defecto
//VisuConfig();
}
else VisuInfo();
}
//==============================================================================
/// Loads execution parameters from a text file.
//==============================================================================
void JCfgRunBase::LoadFile(std::string fname,int lv){
//printf("\nFile:[%s] lv:%d\n",fname.c_str(),lv);
const int MAXOPTS=50;
int optn=0;
std::string *optlis=new std::string[MAXOPTS];
std::ifstream pf;
pf.open(fname.c_str());
if(pf){
while(!pf.eof()&&optn
std::string tex; pf >> tex;
if(tex!=""){
if(optn
optn++;
}
}
if(!pf.eof() && pf.fail())Run_ExceptioonFile("Error reading data from the file.",fname);
pf.close();
}
else Run_ExceptioonFile("The file can not be opened.",fname);
if(optn>=MAXOPTS)Run_ExceptioonFile(fun::PrintStr("File with too many lines (Maximum=%d)",MAXOPTS),fname);
if(optn>0)LoadOpts(optlis,optn,lv,fname);
delete[] optlis;
}
//==============================================================================
/// Generates error of unknown parameter.
//==============================================================================
void JCfgRunBase::ErrorParm(const std::string &opt,int optc,int lv
,const std::string &file)const
{
std::string tx=fun::PrintStr("Parameter \"%s\" unrecognised or invalid. ",opt.c_str());
tx=tx+fun::PrintStr("(Level cfg:%d, Parameter:%d)",lv,optc);
Run_ExceptioonFile(tx,file);
}
//==============================================================================
/// Generates error on parameter with indicated text.
//==============================================================================
void JCfgRunBase::ErrorParmText(const std::string &text,int optc,int lv
,const std::string &file)const
{
std::string tx=text+fun::PrintStr(" (Level cfg:%d, Parameter:%d)",lv,optc);
Run_ExceptioonFile(tx,file);
}
//==============================================================================
/// Returns string of version info according parameters in veropt.
//==============================================================================
std::string JCfgRunBase::VerText(const std::string fullname,const std::string veropt){
string ret=fullname;
if(veropt.size()>4){
string fname=fullname;
//string vname,vver,vdate;
string vname=fun::StrSplit(" v",fname);
string vver =string("v")+fun::StrSplit(" (",fname);
string vdate=(fname.empty() || fname[fname.size()-1]!=')'? "???": fname.substr(0,fname.size()-1));
if(!vdate.empty() && vdate[0]=='v'){ //-Manage special cases: "DualSPHysics-XXXX vX.X (vX.X) (DD-MM-YYYY)"
string ver2=string("(")+fun::StrSplit(" (",fname);
vver=vver+ver2;
vdate=(fname.empty() || fname[fname.size()-1]!=')'? "???": fname.substr(0,fname.size()-1));
}
int s1=atoi(fun::StrSplitValue(":",veropt,1).c_str());
int s2=atoi(fun::StrSplitValue(":",veropt,2).c_str());
if(s1>0)vname=fun::StrFillEnd(vname," ",s1);
if(s2>0)vver =fun::StrFillEnd(vver ," ",s2);
//printf("[%s]->[%d,%d] [%s]---[%s]---[%s]\n",FullName.c_str(),s1,s2,vname.c_str(),vver.c_str(),vdate.c_str());
//printf("%s %s %s\n",vname.c_str(),vver.c_str(),vdate.c_str());
ret=vname+" "+vver+" "+vdate;
}
return(ret);
}
//==============================================================================
/// Loads nv values float using command options. Returns number of loaded
/// values.
//==============================================================================
unsigned JCfgRunBase::LoadFloats(std::string txopt,float def,unsigned nv
,std::vector<float> &vv)
{
//printf("txopt=[%s]\n",txopt.c_str());
vv.clear();
for(unsigned c=0;cpush_back(def);
unsigned num=0;
std::string aux=txopt;
for(;!aux.empty() && num
std::string txv=fun::StrSplit(":",aux);
if(!txv.empty())vv[num]=float(atof(txv.c_str()));
}
return(num);
}
//==============================================================================
/// Loads nv values double using command options. Returns number of loaded
/// values.
//==============================================================================
unsigned JCfgRunBase::LoadDoubles(std::string txopt,double def,unsigned nv
,std::vector<double> &vv)
{
//printf("txopt=[%s]\n",txopt.c_str());
vv.clear();
for(unsigned c=0;cpush_back(def);
unsigned num=0;
std::string aux=txopt;
for(;!aux.empty() && num
std::string txv=fun::StrSplit(":",aux);
if(!txv.empty())vv[num]=atof(txv.c_str());
}
return(num);
}
//==============================================================================
/// Loads nv values tfloat3 using command options. Returns number of loaded
/// values.
//==============================================================================
unsigned JCfgRunBase::LoadFloat3(std::string txopt,float def,unsigned nv,tfloat3 *v){
//printf("txopt=[%s]\n",txopt.c_str());
unsigned num=0;
float *values=(float*)v;
for(unsigned c=0;c3;c++)values[c]=def;
string ttx=txopt;
for(unsigned tc=0;ttx!="" && tc3;tc++){
int tpos=int(ttx.find(":"));
string ttxopt=(tpos>0? ttx.substr(0,tpos): ttx);
string ttxopt2;
if(tpos>0)ttxopt2=ttx.substr(tpos+1);
values[tc]=float(atof(ttxopt.c_str()));
ttx=ttxopt2;
num++;
}
return(num);
}
//==============================================================================
/// Loads nv values tdouble3 using command options. Returns number of loaded
/// values.
//==============================================================================
unsigned JCfgRunBase::LoadDouble3(std::string txopt,double def,unsigned nv,tdouble3 *v){
//printf("txopt=[%s]\n",txopt.c_str());
unsigned num=0;
double *values=(double*)v;
for(unsigned c=0;c3;c++)values[c]=def;
string ttx=txopt;
for(unsigned tc=0;ttx!="" && tc3;tc++){
int tpos=int(ttx.find(":"));
string ttxopt=(tpos>0? ttx.substr(0,tpos): ttx);
string ttxopt2;
if(tpos>0)ttxopt2=ttx.substr(tpos+1);
values[tc]=atof(ttxopt.c_str());
ttx=ttxopt2;
num++;
}
return(num);
}
//==============================================================================
/// Load 1 value tfloat3 using command options.
//==============================================================================
void JCfgRunBase::LoadFloat3(std::string txopt,float def,tfloat3 &v1){
//printf("txopt=[%s]\n",txopt.c_str());
tdouble3 vd;
LoadDouble3(txopt,def,vd);
v1=ToTFloat3(vd);
}
//==============================================================================
/// Load 2 values tfloat3 using command options.
//==============================================================================
void JCfgRunBase::LoadFloat6(std::string txopt,float def,tfloat3 &v1,tfloat3 &v2){
tdouble3 vd1,vd2;
LoadDouble6(txopt,def,vd1,vd2);
v1=ToTFloat3(vd1);
v2=ToTFloat3(vd2);
}
//==============================================================================
/// Load 1 value tdouble2 using command options.
//==============================================================================
void JCfgRunBase::LoadDouble2(std::string txopt,double def,tdouble2 &v1){
//printf("txopt=[%s]\n",txopt.c_str());
double values[2]={def,def};
string aux=txopt;
for(int tc=0;!aux.empty() && tc<2;tc++){
string txv=fun::StrSplit(":",aux);
if(!txv.empty())values[tc]=atof(txv.c_str());
}
v1=TDouble2(values[0],values[1]);
}
//==============================================================================
/// Load 1 value tdouble3 using command options.
//==============================================================================
void JCfgRunBase::LoadDouble3(std::string txopt,double def,tdouble3 &v1){
//printf("txopt=[%s]\n",txopt.c_str());
double values[3]={def,def,def};
string aux=txopt;
for(int tc=0;!aux.empty() && tc<3;tc++){
string txv=fun::StrSplit(":",aux);
if(!txv.empty())values[tc]=atof(txv.c_str());
}
v1=TDouble3(values[0],values[1],values[2]);
}
//==============================================================================
/// Load 2 values tdouble3 using command options.
//==============================================================================
void JCfgRunBase::LoadDouble6(std::string txopt,double def,tdouble3 &v1,tdouble3 &v2){
//printf("txopt=[%s]\n",txopt.c_str());
double values[6]={def,def,def,def,def,def};
string aux=txopt;
for(int tc=0;!aux.empty() && tc<6;tc++){
string txv=fun::StrSplit(":",aux);
if(!txv.empty())values[tc]=atof(txv.c_str());
}
v1=TDouble3(values[0],values[1],values[2]);
v2=TDouble3(values[3],values[4],values[5]);
}
//==============================================================================
/// Splits options in txoptfull, txopt, txopt2, txopt3, txopt4 and txopt5.
//==============================================================================
void JCfgRunBase::SplitsOpts(const std::string &opt,std::string &txword
,std::string &txoptfull,std::string &txopt1,std::string &txopt2
,std::string &txopt3,std::string &txopt4,std::string &txopt5)const
{
txword=txoptfull=txopt1=txopt2=txopt3=txopt4=txopt5="";
string tx=opt.substr(1);
int pos=int(tx.find("#"));
if(pos>0)tx=tx.substr(0,pos);
pos=int(tx.find(":"));
txword=fun::StrUpper(pos>0? tx.substr(0,pos): tx);
if(pos>=0)txopt1=tx.substr(pos+1);
txoptfull=txopt1;
tx=txopt1;
pos=int(tx.find(":"));
txopt1=(pos>=0? tx.substr(0,pos): tx);
if(pos>=0)txopt2=tx.substr(pos+1);
tx=txopt2;
pos=int(tx.find(":"));
txopt2=(pos>=0? tx.substr(0,pos): tx);
if(pos>=0)txopt3=tx.substr(pos+1);
tx=txopt3;
pos=int(tx.find(":"));
txopt3=(pos>=0? tx.substr(0,pos): tx);
if(pos>=0)txopt4=tx.substr(pos+1);
tx=txopt4;
pos=int(tx.find(":"));
txopt4=(pos>=0? tx.substr(0,pos): tx);
if(pos>=0)txopt5=tx.substr(pos+1);
}
You can't perform that action at this time.