forked from forefireAPI/forefire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLibForeFire.cpp
More file actions
431 lines (347 loc) · 14.2 KB
/
CLibForeFire.cpp
File metadata and controls
431 lines (347 loc) · 14.2 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
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
/*
Copyright (C) 2012 ForeFire Team, SPE, UniversitŽ de Corse.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US
*/
#include "CLibForeFire.h"
#include "SimulationParameters.h"
using namespace std;
namespace libforefire {
Command executor;
static Command::Session* session = &(executor.currentSession);
FFArray<double>* injectedHeat;
double tp;
Command* getLauncher(){
return &executor;
}
void MNHInit(const double t){
executor.setReferenceTime(t);
tp = t;
/* Reading all the information on the parameters of ForeFire */
ostringstream paramsfile;
paramsfile<<SimulationParameters::GetInstance()->getParameter("caseDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("ForeFireDataDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("paramsFile");
ifstream inputParams(paramsfile.str().c_str());
if ( inputParams ) {
string line;
size_t numLine = 0;
while ( getline( inputParams, line ) ) {
numLine++;
// checking for comments or newline
if((line[0] == '#')||(line[0] == '*')||(line[0] == '\n'))
continue;
// treating the command of the current line
executor.ExecuteCommand(line);
}
} else {
cout<<"ERROR: File for ForeFire initialization not found !!"<<endl;
cout<<'\t'<<"looked in "<<paramsfile.str()<<endl;
}
executor.outputDirs = SimulationParameters::GetInstance()->getParameterArray("atmoOutputDirectories");
}
void MNHCreateDomain(const int id
, const int year, const int month
, const int day, const double t
, const double lat, const double lon
, const int mdimx, const double* meshx
, const int mdimy, const double* meshy
, const int mdimz, const double* zgrid
, const double dt){
/* Defining the Fire Domain */
if (session->fd) delete session->fd;
session->fd = new FireDomain(id, year, month, day, t, lat, lon
, mdimx, meshx, mdimy, meshy, mdimz, dt);
executor.domain = session->fd;
// A FireDomain has been created, the level is increased
executor.increaseLevel();
session->ff = session->fd->getDomainFront();
// Defining the timetable of the events to be be in the domain
if (session->tt) delete session->tt;
session->tt = new TimeTable();
// Associating this timetable to the domain
session->fd->setTimeTable(session->tt);
// Defining the simulator
if (session->sim) delete session->sim;
session->sim = new Simulator(session->tt, session->fd->outputs);
/* Managing the outputs */
ostringstream ffOutputsPattern;
ffOutputsPattern<<SimulationParameters::GetInstance()->getParameter("caseDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("fireOutputDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("outputFiles")
<<"."<<session->fd->getDomainID();
SimulationParameters::GetInstance()->setParameter("ffOutputsPattern", ffOutputsPattern.str());
session->outStrRep = new StringRepresentation(executor.domain);
if ( SimulationParameters::GetInstance()->getInt("outputsUpdate") != 0 ){
session->tt->insert(new FFEvent(session->outStrRep));
}
// TODO createNcFile(params->getParameter("ForeFireDataDirectory"), mdimx, mdimy, mdimz, meshx, meshy, zgrid);
// Reading all the information on the initialization of ForeFire
ostringstream initfile;
if ( SimulationParameters::GetInstance()->getInt("parallelInit") != 1 ) {
// Mono-file case: one file for all the processors
initfile<<SimulationParameters::GetInstance()->getParameter("caseDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("ForeFireDataDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("InitFile");
} else {
// Multi-proc initial conditions
initfile<<SimulationParameters::GetInstance()->getParameter("caseDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("ForeFireDataDirectory")<<'/'
<<SimulationParameters::GetInstance()->getParameter("InitFiles")
<<"."<<id<<"."<<SimulationParameters::GetInstance()->getParameter("InitTime");
}
ifstream inputInit(initfile.str().c_str());
if ( inputInit ) {
string line;
size_t numLine = 0;
// skip the firest "firedomain" line for // init with multiple files
if ( SimulationParameters::GetInstance()->getInt("parallelInit") == 1 ) getline( inputInit, line );
while ( getline( inputInit, line ) ) {
numLine++;
// checking for comments or newline
if((line[0] == '#')||(line[0] == '*')||(line[0] == '\n'))
continue;
// treating the command of the current line
executor.ExecuteCommand(line);
}
} else {
cout<<"File for ForeFire initialization "<<initfile.str()
<<" not found !! No markers will be advected" << endl;
}
// completing the last front
executor.completeFront(executor.currentSession.ff);
// Managing the communication matrices for parallel computation
if ( session->fd->parallel ) session->fd->createFirenodesMatrices();
// advancing the simulation to the beginning of the atmospheric simulation
double deltaT = session->fd->getSecondsFromReferenceTime(year, month, day, t);
executor.setReferenceTime(deltaT);
executor.setStartTime(deltaT);
injectedHeat = new FFArray<double>("heat", 0., mdimx, mdimy);
}
void CheckLayer(const char* lname){
string tmpname(lname);
// searching for concerned layer
FluxLayer<double>* myLayer = session->fd->getFluxLayer(tmpname);
if ( myLayer == 0 ){
if ( !session->fd->addFluxLayer(tmpname) ){
cout<<"WARNING: layer for "<<tmpname
<<" could not be found within ForeFire framework, "
<<"this should cause serious problems when coupling"<<endl;
}
}
}
void MNHStep(double dt){
ostringstream cmd;
cmd << "step[dt=" << dt <<"]";
string scmd = cmd.str();
executor.ExecuteCommand(scmd);
}
void MNHGoTo(double time){
ostringstream cmd;
cmd << "goTo[t=" << time <<"]";
string scmd = cmd.str();
executor.ExecuteCommand(scmd);
}
void executeMNHCommand(const char* cmd){
string scmd(cmd);
executor.ExecuteCommand(scmd);
}
void FFPutString(const char* mname, char* str){
// NOT TO BE CALLED
}
void FFGetString(const char* mname, const char* str){
string name(mname);
string val(str);
SimulationParameters::GetInstance()->setParameter(name, val);
}
void FFPutInt(const char* mname, int* n){
string name(mname);
*n = SimulationParameters::GetInstance()->getInt(name);
}
void FFGetInt(const char* mname, int* n){
string name(mname);
SimulationParameters::GetInstance()->setInt(name, *n);
}
void FFPutIntArray(const char* mname, int* x,
size_t sizein, size_t sizeout){
// TODO
}
void FFGetIntArray(const char* mname, double time
, int* x, int sizein, int sizeout){
// TODO
}
void FFPutDouble(const char* mname, double* x){
string name(mname);
*x = SimulationParameters::GetInstance()->getDouble(name);
}
void FFGetDouble(const char* mname, double* x){
string name(mname);
SimulationParameters::GetInstance()->setDouble(name, *x);
}
void FFPutDoubleArray(const char* mname, double* x,
size_t sizein, size_t sizeout){
string tmpname(mname);
// searching for concerned layer
DataLayer<double>* myLayer = session->fd->getDataLayer(tmpname);
if ( myLayer ){
FFArray<double>* myMatrix;
// getting the pointer
myLayer->getMatrix(&myMatrix, executor.getTime());
// TODO deleting this debug prints, injectedHeat and tp
/* if ( session->fd->getDomainID() == 1 and myLayer->getKey() == "heatFlux" ){
cout<<"getting double array for "<<myLayer->getKey()<<" at t="<<executor.getTime()<<endl;
cout<<myMatrix->print2D()<<endl;
for ( size_t i = 0; i < myMatrix->getDim("x"); i++ ){
for ( size_t j = 0; j < myMatrix->getDim("y"); j++ ){
(*injectedHeat)(i,j) += (*myMatrix)(i,j)*(executor.getTime()-tp);
}
}
cout<<"cumulative injected heat is"<<endl<<injectedHeat->print2D();
tp = executor.getTime();
}
if ( session->fd->getDomainID() == 1 and myLayer->getKey() == "BRatio" ){
cout<<"getting double array for "<<myLayer->getKey()<<" at t="<<executor.getTime()<<endl;
cout<<myMatrix->print2D()<<endl;
}
*/
// copying the data into the atmospheric matrix
myMatrix->copyDataToFortran(x);
} else {
cout<<"Error trying to put data from unknown layer "<<tmpname<<endl;
}
}
void FFGetDoubleArray(const char* mname, double t
, double* x, size_t sizein, size_t sizeout){
string tmpname(mname);
double ct = executor.refTime + t;
// searching for the layer to put data
DataLayer<double>* myLayer = session->fd->getDataLayer(tmpname);
if ( myLayer ){
myLayer->setMatrix(tmpname, x, sizein, sizeout, ct);
} else {
cout<<"Error trying to get data for unknown layer "<<tmpname<<endl;
}
}
void FFDumpDoubleArray(size_t nmodel, size_t nip, const char* mname, double t
, double* x, size_t sizein, size_t ni, size_t nj, size_t nk, size_t sizeout){
string tmpname(mname);
ostringstream outputfile;
double ct = executor.refTime + t;
outputfile<<SimulationParameters::GetInstance()->getParameter("caseDirectory")<<'/'
<<executor.outputDirs[nmodel-1]<<'/'
<<SimulationParameters::GetInstance()->getParameter("outputFiles")
<<"."<<nip<<"."<<tmpname;
size_t niC = (size_t)(ni+0);
size_t njC = (size_t)(nj+0);
size_t nkC = (size_t)(nk+0);
ofstream FileOut(outputfile.str().c_str(), ios_base::binary | ios::app);
FileOut.write(reinterpret_cast<const char*>(&niC), sizeof(size_t));
FileOut.write(reinterpret_cast<const char*>(&njC), sizeof(size_t));
FileOut.write(reinterpret_cast<const char*>(&nkC), sizeof(size_t));
FileOut.write(reinterpret_cast<const char*>(&ct), sizeof(double));
size_t indF = 0;
try {
for ( indF = 0; indF < sizein; indF++ ) {
FileOut.write(reinterpret_cast<const char*>(x+indF), sizeof(double));
}
} catch (...) {
cout << "Problem in passing a Fortran array to C array "
<<tmpname<<endl;
}
FileOut.close();
}
void saveNcRecord(int rec){
size_t i = 0;
size_t j = 0;
size_t k = 0;
ostringstream oss;
oss<<session->params->getParameter("caseDirectory")<<'/'
<<session->params->getParameter("fireOutputDirectory")<<'/'
<<"data.nc";
size_t nx = 20; // Nb de lignes de cells
size_t ny = 30; // Nb de colonnes de cell
size_t nz = 40; // Nb de colonnes de cell
cout << "reopening "<< oss.str()<< endl;
NcFile dataFile(oss.str().c_str(), NcFile::Write);
cout << "file opened Select"<< endl;
if (!dataFile.is_valid())
{
cout << "Couldn't open file!"<< endl;
}
NcVar *cell_active = dataFile.get_var("values");
NcDim* ctDim = dataFile.get_dim("time");
double value[nz][ny][nx];
cout << "*** dumping record " <<oss.str()<< endl;
// cell_active->put(&value[0][0][0], nz, ny,nx);
for (i = 0; i < nx ; i++){ for (j = 0; j < ny ; j++){for (k = 0; k < nz ; k++){value[k][j][i] = k;}}}
cell_active->put_rec(ctDim,&value[0][0][0],0);
for (i = 0; i < nx ; i++){ for (j = 0; j < ny ; j++){for (k = 0; k < nz ; k++){value[k][j][i] = i;}}}
cell_active->put_rec(ctDim,&value[0][0][0],1);
for (i = 0; i < nx ; i++){ for (j = 0; j < ny ; j++){for (k = 0; k < nz ; k++){value[k][j][i] = j*i;}}}
cell_active->put_rec(ctDim,&value[0][0][0],2);
for (i = 0; i < nx ; i++){ for (j = 0; j < ny ; j++){for (k = 0; k < nz ; k++){value[k][j][i] = j;}}}
cell_active->put_rec(ctDim,&value[0][0][0],3);
for (i = 0; i < nx ; i++){ for (j = 0; j < ny ; j++){for (k = 0; k < nz ; k++){value[k][j][i] = 5;}}}
cell_active->put_rec(ctDim,&value[0][0][0],4);
cout << "*** SUCCESS dumping record " <<oss.str()<< endl;
dataFile.close();
}
void createNcFile(string filename
, const int& consted_ni, const int& consted_nj, const int& consted_nk
, const double* meshx, const double* meshy, const double* zgrid){
ostringstream oss;
size_t ndim = 3; // Nb de dimension
// TODO transpose zgrid
size_t i = 0;
size_t j = 0;
size_t k = 0;
NcFile dataFile(filename.c_str(), NcFile::Replace, NULL, 0, NcFile::Netcdf4);
if (!dataFile.is_valid())
{
cout << "Couldn't open file for creation"<< endl;
}
NcVar *cell_geom = NULL;
size_t ni = (size_t)consted_ni;
size_t nj = (size_t)consted_nj;
size_t nk = (size_t)consted_nk;
NcDim* cxDim = dataFile.add_dim("ni", ni);
NcDim* cyDim = dataFile.add_dim("nj", nj);
NcDim* czDim = dataFile.add_dim("nk", nk);
NcDim* cxyzDim = dataFile.add_dim("xyz", ndim);
NcDim* ctDim = dataFile.add_dim("time");
cout << "*** dims added " <<oss.str()<< endl;
double rv[nk][nj][ni][ndim];
cout << "*** in Heap " <<oss.str()<< endl;
for (i = 0; i < ni ; i++){
for (j = 0; j < nj ; j++){
for (k = 0; k < nk ; k++){
rv[k][j][i][0] = meshx[i];
rv[k][j][i][1] = meshy[j];
rv[k][j][i][2] = zgrid[i + j*ni + k*ni*nj];
}
}
}
cout << "*** Geom " <<oss.str()<< endl;
cell_geom = dataFile.add_var("grid_geometry", ncDouble, czDim, cyDim,cxDim,cxyzDim);
cell_geom->put(&rv[0][0][0][0], nk, nj, ni, ndim);
NcVar *cell_active = NULL;
if ( !SimulationParameters::GetInstance()->isValued("outputs.variables") )
cout<<"ERROR: vector of parameters outputs.variables should be valued"<<endl;
vector<string> variables = SimulationParameters::GetInstance()->getParameterArray("outputs.variables");
vector<string>::iterator variable;
for ( variable = variables.begin(); variable != variables.end(); ++variable ){
cell_active = dataFile.add_var(variable->c_str(), ncDouble, ctDim,czDim, cyDim,cxDim);
}
cout << "*** SUCCESS creating " <<oss.str()<< endl;
dataFile.close();
}
}