forked from forefireAPI/firefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFireDomain.cpp
4358 lines (3806 loc) · 138 KB
/
FireDomain.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
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
/*
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 "FireDomain.h"
#include "BurningMapLayer.h"
#include "RosLayer.h"
namespace libforefire{
// Static variables
long ForeFireAtom::instanceNRCount = 0;
const double FireDomain::endChain = -1.;
const double FireDomain::endCom = -10.;
const double FireDomain::noCom = -100.;
const string FireDomain::altitude = "altitude";
const FFPoint FireDomain::outPoint
= FFPoint(numeric_limits<double>::infinity(), numeric_limits<double>::infinity());
bool FireDomain::outputs = false;
bool FireDomain::commandOutputs = false;
bool FireDomain::recycleNodes = false;
bool FireDomain::recycleFronts = false;
list<FireNode*> FireDomain::createdNodes;
list<FireNode*> FireDomain::trashNodes;
list<FireFront*> FireDomain::trashFronts;
FireFrontData* FireDomain::mainFrontBackup;
FireDomain::FrontDepthScheme FireDomain::fdScheme = FireDomain::normalDir;
const size_t FireDomain::NUM_MAX_PROPMODELS;
const size_t FireDomain::NUM_MAX_FLUXMODELS;
PropagationModel* FireDomain::propModelsTable[FireDomain::NUM_MAX_PROPMODELS];
FluxModel* FireDomain::fluxModelsTable[FireDomain::NUM_MAX_FLUXMODELS];
FireDomain::PropModelMap& FireDomain::prop_instantiatorMap(){
static FireDomain::PropModelMap* inst = new FireDomain::PropModelMap;
return *inst;
}
FireDomain::FluxModelMap& FireDomain::flux_instantiatorMap(){
static FireDomain::FluxModelMap* inst = new FireDomain::FluxModelMap;
return *inst;
}
// Default constructor
FireDomain::FireDomain() : ForeFireAtom(0.) {
cout<<"Trying to instantiate an empty FireDomain, not relevant"<<endl;
}
FireDomain::FireDomain(const double& t
, FFPoint& sw, FFPoint& ne)
: ForeFireAtom(t), SWCorner(sw), NECorner(ne) {
params = SimulationParameters::GetInstance();
// Maximum time-step for Firenodes is not constrained
dtMax = numeric_limits<double>::infinity();
// Mesh size
atmoNX = params->getSize("atmoNX");
atmoNY = params->getSize("atmoNY");
atmoNZ = params->getSize("atmoNZ");
// computing the cells' mesh
double cellsMeshX[atmoNX+1];
double cellsMeshY[atmoNY+1];
cellsMeshX[0] = sw.getX();
cellsMeshY[0] = sw.getY();
double dx, dy;
dx = (ne.getX()-sw.getX())/atmoNX;
dy = (ne.getY()-sw.getY())/atmoNY;
for ( size_t i = 1; i < atmoNX; i++ ) {
cellsMeshX[i] = cellsMeshX[i-1] + dx;
}
for ( size_t j = 1; j < atmoNY; j++ ) {
cellsMeshY[j] = cellsMeshY[j-1] + dy;
}
cellsMeshX[atmoNX] = ne.getX();
cellsMeshY[atmoNY] = ne.getY();
// simulation won't be run in parallel
params->setInt("parallel", 0);
// simulation won't be be run with mnh
params->setParameter("runmode", "standalone");
atmosphericCoupling = false;
// reference time
int year = params->getInt("refYear");
int yday = params->getInt("refDay");
if (yday == 0)
{
year = params->getInt("year");
int month = params->getInt("month");
int day = params->getInt("day");
yday = getDayNumber(year, month, day);
}
// Common initialization for all constructors
commonInitialization(cellsMeshX,cellsMeshY , year, yday);
}
FireDomain::FireDomain(const int& mpirank
, 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& dt)
: ForeFireAtom(t), refLatitude(lat), refLongitude(lon) {
getNewID(mpirank);
params = SimulationParameters::GetInstance();
// Maximum time-step for Firenodes is constrained by the atmospheric model
dtMax = dt;
/* simulation is run with mnh */
params->setParameter("runmode", "coupled");
params->setInt("parallel", 1);
params->setInt("mpirank", mpirank);
atmosphericCoupling = true;
/* Definition of the mesh */
atmoNX = (size_t) mdimx;
params->setSize("atmoNX", atmoNX);
atmoNY = (size_t) mdimy;
params->setSize("atmoNY", atmoNY);
atmoNZ = (size_t) mdimz;
params->setSize("atmoNZ", atmoNZ);
// computing the position of the physical and numerical corners
SWCorner = FFPoint(meshx[0], meshy[0]);
NECorner = FFPoint(2.*meshx[atmoNX-1]-meshx[atmoNX-2]
, 2.*meshy[atmoNY-1]-meshy[atmoNY-2]);
// computing the cells' mesh
double cellsMeshX[atmoNX+1];
double cellsMeshY[atmoNY+1];
for ( size_t i = 0; i < atmoNX; i++ ) {
cellsMeshX[i] = meshx[i];
}
cellsMeshX[atmoNX] = NECornerX();
for ( size_t j = 0; j < atmoNY; j++ ) {
cellsMeshY[j] = meshy[j];
}
cellsMeshY[atmoNY] = NECornerY();
int yday = getDayNumber(year, month, day);
// Common initialization for all constructors
commonInitialization(cellsMeshX,cellsMeshY, year, yday);
}
FireDomain::~FireDomain() {
// Deleting nodes
while( !createdNodes.empty() ){
delete createdNodes.back();
createdNodes.pop_back();
}
// Deleting fronts
delete domainFront;
while( !trashFronts.empty() ){
delete trashFronts.back();
trashFronts.pop_back();
}
// Deleting cells
for ( size_t i = 0; i < atmoNX; ++i ) {
delete [] cells[i];
}
delete [] cells;
delete trashCell;
// Deleting frontiers
while( !frontiers.empty() ){
delete frontiers.back();
frontiers.pop_back();
}
while( !infrontiers.empty() ){
delete infrontiers.back();
infrontiers.pop_back();
}
// Deleting halos
while( !innerHalos.empty() ){
delete innerHalos.back();
innerHalos.pop_back();
}
while( !outerHalos.empty() ){
delete outerHalos.back();
outerHalos.pop_back();
}
// Deleting data broker related objects
delete dataBroker;
delete propagativeLayer;
// Deleting time table
delete schedule;
}
void FireDomain::backupState(){
/* Backup of the simulation */
if ( mainFrontBackup != 0 ) delete mainFrontBackup;
mainFrontBackup = new FireFrontData(domainFront);
}
void FireDomain::restoreValidState(){
/* Restoring a previously saved valid state */
if ( mainFrontBackup == 0 ){
debugOutput<<getDomainID()<<": PROBLEM, tried to return to a valid state"
<<" while no backup state is available"<<endl;
} else {
debugOutput<<getDomainID()<<": RECONSTRUCTING STATE"<<endl;
}
/* Trashing the current state */
trashFrontsAndNodes();
/* Restoring the valid state */
mainFrontBackup->reconstructState(domainFront);
}
void FireDomain::trashFrontsAndNodes(){
list<FireNode*> toBeTrashed;
list<FireNode*>::iterator node;
for ( size_t i = 0; i < atmoNX; i++ ) {
for ( size_t j = 0; j < atmoNY; j++ ) {
// deleting the nodes in the cell
toBeTrashed = cells[i][j].fireNodes;
for ( node = toBeTrashed.begin();
node != toBeTrashed.end(); ++node ){
debugOutput<<getDomainID()<<": FireDomain::trashFrontsAndNodes -> ";
addToTrashNodes(*node);
}
}
}
list<FireFront*> fronts = domainFront->getInnerFronts();
list<FireFront*>::iterator front;
for ( front = fronts.begin(); front != fronts.end(); ++front ){
addToTrashFronts(*front);
}
}
// Accessors
int FireDomain::getReferenceYear(){
return refYear;
}
int FireDomain::getReferenceDay(){
return refDay;
}
double FireDomain::getSecondsFromReferenceTime(
const int& year, const int& month
, const int& day, const int& t){
if ( year != refYear ) cout<<"WARNING: change of year "<< year <<" to "<< refYear <<" is not handled in ForeFire data at YY/MM/DD "<<year<<"/"<<month<<"/"<<day<<endl;
int iday = getDayNumber(year, month, day);
return (iday-refDay)*86400. + t;
}
list<FireFront*> FireDomain::getMainFronts(){
return domainFront->getInnerFronts();
}
FireFront* FireDomain::getDomainFront(){
return domainFront;
}
TimeTable* FireDomain::getTimeTable(){
return schedule;
}
double FireDomain::getSimulationTime(){
if ( schedule->getTime() > this->getTime() ) {
return double(int(schedule->getTime()));
} else {
return this->getTime();
}
}
FFPoint& FireDomain::getSWCorner(){
return SWCorner;
}
FFPoint& FireDomain::getNECorner(){
return NECorner;
}
double& FireDomain::SWCornerX(){
return SWCorner.getX();
}
double& FireDomain::SWCornerY(){
return SWCorner.getY();
}
double& FireDomain::NWCornerX(){
return NWCorner.getX();
}
double& FireDomain::NWCornerY(){
return NWCorner.getY();
}
double& FireDomain::NECornerX(){
return NECorner.getX();
}
double& FireDomain::NECornerY(){
return NECorner.getY();
}
double& FireDomain::SECornerX(){
return SECorner.getX();
}
double& FireDomain::SECornerY(){
return SECorner.getY();
}
double& FireDomain::getPerimeterResolution(){
return perimeterResolution;
}
double& FireDomain::getSpatialIncrement(){
return spatialIncrement;
}
double& FireDomain::getMaxTimeStep(){
return dtMax;
}
int FireDomain::getNumIterationAtmoModel(){
return numIterationAtmoModel;
}
void FireDomain::increaseNumIterationAtmoModel(){
numIterationAtmoModel++;
}
int FireDomain::getDayNumber(const int& year
, const int& month, const int& day){
bool leap = ( ( year%4 == 0 and year%100 != 0 ) or year%400 == 0 );
int febNumDays;
leap == true ? febNumDays = 29 : febNumDays = 28;
int numDays[11] = {31, febNumDays, 31, 30, 31, 30, 31, 31, 30, 31, 30};
int numDay = 0;
for ( int imon = 1; imon < month; imon++ ){
numDay += numDays[imon-1];
}
numDay += day;
return numDay;
}
// Mutators
void FireDomain::setSafeTopologyMode(bool safeMode){
safeTopologyMode = safeMode;
}
void FireDomain::setBoundariesFront(FireFront* ff){
domainFront = ff;
}
void FireDomain::setTimeTable(TimeTable* tt){
schedule = tt;
}
// input function
void FireDomain::input(){
}
// update function
void FireDomain::update(){
setTime(getUpdateTime());
}
// advance in time function
void FireDomain::timeAdvance(){
setUpdateTime(numeric_limits<double>::infinity());
}
// Output function
void FireDomain::output(){
}
// Adding a newly created Atom to the simulation
void FireDomain::addNewAtomToSimulation(ForeFireAtom* atom){
schedule->insertBefore(new FFEvent(atom));
}
// Deleting an Atom of the simulation
void FireDomain::deleteAtomOfSimulation(ForeFireAtom* atom){
schedule->dropAtomEvents(atom);
}
// Searching for an atom in the entire domain
FireNode* FireDomain::getFireNodeByID(const long& sid){
FireNode* tmpAtom;
for ( size_t i=0; i<atmoNX; i++ ){
for ( size_t j=0; j<atmoNY; j++ ){
tmpAtom = cells[i][j].getFirenodeByID(sid);
if ( tmpAtom ) return tmpAtom;
}
}
return 0;
}
FireNode* FireDomain::getFireNodeByID(const double sid){
long lsid = getIDfromDouble(sid);
return getFireNodeByID(lsid);
}
FireNode* FireDomain::getFireNodeByIDNeighborCells(
const double id, FDCell* cell, int range){
FireNode* tmpAtom;
/* Looking in the current cell */
tmpAtom = cell->getFirenodeByID(id);
if ( tmpAtom ) return tmpAtom;
/* searching in neighboring cells */
list<FDCell*> proxCells = getProxCells(cell, range);
list<FDCell*>::iterator curCell;
for ( curCell = proxCells.begin();
curCell != proxCells.end(); ++curCell){
tmpAtom = (*curCell)->getFirenodeByID(id);
if ( tmpAtom ) return tmpAtom;
}
// No firenode was found with this ID
debugOutput<<getDomainID()<<": "
<<"could not find a firenode with Id "
<<getDomainID(id)<<", "<<getShortID(id)<<endl;
return 0;
}
FireNode* FireDomain::getFirenodeInInnerHalo(const double& sid){
list<FDCell*>::iterator cell;
FireNode* tmpfn = 0;
for ( cell = innerHaloCells.begin();
cell != innerHaloCells.end(); ++cell ){
tmpfn = (*cell)->getFirenodeByID(sid);
if ( tmpfn ) return tmpfn;
}
return 0;
}
FireNode* FireDomain::getFirenodeInOuterHalo(const double& sid){
list<FDCell*>::iterator cell;
FireNode* tmpfn = 0;
for ( cell = outerHaloCells.begin();
cell != outerHaloCells.end(); ++cell ){
tmpfn = (*cell)->getFirenodeByID(sid);
if ( tmpfn ) return tmpfn;
}
return 0;
}
int FireDomain::registerPropagationModelInstantiator(string modelname
, PropagationModelInstantiator func){
PropModelMap::iterator pmodel = prop_instantiatorMap().find(modelname);
if ( pmodel != prop_instantiatorMap().end() ){
cout<<"WARNING: propagation model "<<modelname
<<" has the same name as a previously"
<<" registered propagation model, check your settings"<<endl;
}
prop_instantiatorMap().insert(make_pair(modelname,func));
return 1;
}
void FireDomain::updateFuelTable( string key, double value){
for ( size_t i = 0; i < NUM_MAX_PROPMODELS; i++ ){
if(propModelsTable[i] != 0){
getDataBroker()->updateFuelValues(propModelsTable[i],key,value);
}
}
}
int FireDomain::registerFluxModelInstantiator(string modelname
, FluxModelInstantiator func){
FluxModelMap::iterator fmodel = flux_instantiatorMap().find(modelname);
if ( fmodel != flux_instantiatorMap().end() ){
cout<<"WARNING: flux model "<<modelname
<<" has the same name as a previously"
<<" registered flux model, check your settings"<<endl;
}
flux_instantiatorMap().insert(make_pair(modelname,func));
return 1;
}
PropagationModel* FireDomain::propModelInstanciation(const int& index, string modelname){
PropModelMap::iterator pmodel = prop_instantiatorMap().find(modelname);
if ( pmodel != prop_instantiatorMap().end() ){
return (pmodel->second)(index, dataBroker);
} else {
cout<<"ERROR: Propagation model "<<modelname
<<" is not recognized, check your configuration !!"<< endl;
return 0;
}
}
FluxModel* FireDomain::fluxModelInstanciation(const int& index, string modelname){
FluxModelMap::iterator fmodel;
fmodel = flux_instantiatorMap().find(modelname);
if ( fmodel != flux_instantiatorMap().end() ){
return (fmodel->second)(index, dataBroker);
} else {
cout<<"ERROR: Flux model "<<modelname
<<" is not recognized, check your configuration !!"<< endl;
return 0;
}
}
void FireDomain::registerPropagationModel(const int& index, PropagationModel* model){
propModelsTable[index] = model;
}
void FireDomain::registerFluxModel(const int& index, FluxModel* model){
fluxModelsTable[index] = model;
}
bool FireDomain::addPropagativeLayer(string mname){
/* searching if there exists a propagation model with associated name */
/* affecting it to free index */
size_t mindex = getFreePropModelIndex();
PropagationModel* model = propModelInstanciation(mindex, mname);
if ( model == 0 ) return false;
/* Instantiating a flux layer related to this model */
propagativeLayer = new PropagativeLayer<double>(mname, mindex);
return true;
}
bool FireDomain::addLayer(string type, string layername, string keyname){
if ( type == "BRatio" ){
BurningRatioLayer<double>* brlayer = new BurningRatioLayer<double>(layername, atmoNX, atmoNY, cells);
dataBroker->registerLayer(layername, brlayer);
return true;
}
if ( type == "MaxRos" ){
RosLayer<double>* mrlayer = new RosLayer<double>(layername, atmoNX, atmoNY, cells);
dataBroker->registerLayer(layername, mrlayer);
return true;
}
if ( !params->isValued(keyname) ){
cout << "Unknown parameter "<<keyname<<" for "<<layername << " layer of type "<< type <<", please set parameter"<<endl;
return false;
}
double timespan = 0;
size_t nnx = 1;
size_t nny = 1;
size_t nnz = 1;
size_t nnk = 1;
double spanx = NECorner.x -SWCorner.x;
double spany = NECorner.y -SWCorner.y;
if ( type == "data" ){
double* values = new double[1];
values[0] = params->getDouble(keyname);
return addScalarLayer( type, layername,SWCorner.getX(), SWCorner.getY(), getTime(), spanx , spany, timespan, nnx, nny, nnz, nnk, values);
}else{
int* values = new int[1];
values[0] = params->getInt(keyname);
return addIndexLayer( type, layername,SWCorner.getX(), SWCorner.getY(), getTime(), spanx , spany, timespan, nnx, nny, nnz, nnk, values);
}
}
bool FireDomain::addScalarLayer(string type, string name, double &x0, double &y0, double& t0, double& width, double& height, double& timespan, size_t& nnx, size_t& nny, size_t& nnz, size_t& nnk, double* values){
FFPoint origin = FFPoint(x0, y0);
FFPoint span = FFPoint(width, height);
XYZTDataLayer<double>* newLayer = new XYZTDataLayer<double>(name, origin,t0, span, timespan, nnx, nny, nnz, nnk, values);
dataBroker->registerLayer(name, newLayer);
return true;
}
bool FireDomain::addIndexLayer(string type, string name, double &x0, double &y0, double& t0, double& width, double& height, double& timespan, size_t& nnx, size_t& nny, size_t& nnz, size_t& nnk, int* values){
FFPoint origin = FFPoint(x0, y0);
FFPoint span = FFPoint(width, height);
if ( type == "flux" ){
size_t mindex = values[0];
string fmname = name;
FluxModel* model = fluxModelInstanciation(mindex, fmname);
if ( model != 0 ){
/* Instantiating a flux layer related to this model */
FluxLayer<double>* newlayer = new FluxLayer<double>(name,SWCorner, NECorner,atmoNX, atmoNY, getCells(), values, origin, t0, span, timespan, nnx, nny, nnz, nnk);
dataBroker->registerFluxLayer(name, newlayer);
return true;
}
}
if ( type == "propagation" ){
size_t mindex = getFreePropModelIndex();
PropagationModel* model = propModelInstanciation(mindex, name);
if ( model == 0 ) return false;
/* Instantiating a prop layer related to this model */
propagativeLayer = new PropagativeLayer<double>(name, mindex);
return true;
}
if ( type == "table" ){
FuelDataLayer<double>* newlayer = new FuelDataLayer<double>(name, origin, t0, span, timespan, nnx, nny, nnz, nnk, values);
dataBroker->registerLayer(name, newlayer);
}
return false;
}
bool FireDomain::addFluxLayer(string lname){
/* searching if there exists a flux model with associated name */
/* affecting it to free index */
// Burning ratio is special as it is derived from the heat flux layer
if ( lname == "BRatio" or lname == "Bratio" or lname == "bratio" ){
/* Instantiating a burning ratio layer */
BurningRatioLayer<double>* brlayer =
new BurningRatioLayer<double>(lname, atmoNX, atmoNY, cells);
dataBroker->registerLayer(lname, brlayer);
return true;
}
// Otherwise, searching for the model in the available ones
size_t mindex = getFreeFluxModelIndex();
string fmname = lname;
if ( lname == "heatFlux" ) fmname = "heatFluxBasic";
if ( lname == "vaporFlux" ) fmname = "vaporFluxBasic";
FluxModel* model = fluxModelInstanciation(mindex, fmname);
if ( model != 0 ){
/* Instantiating a flux layer related to this model */
FluxLayer<double>* newlayer = new FluxLayer<double>(lname, SWCorner, NECorner, atmoNX, atmoNY, cells, mindex);
registerFluxModel(model->index, model);
dataBroker->registerFluxLayer(lname, newlayer);
return true;
}
return false;
}
size_t FireDomain::getFreePropModelIndex(){
size_t mindex = NUM_MAX_PROPMODELS - 1;
while ( propModelsTable[mindex] != 0 ) mindex--;
return mindex;
}
size_t FireDomain::getFreeFluxModelIndex(){
size_t mindex = 0;
while ( fluxModelsTable[mindex] != 0 ){
if(mindex >= NUM_MAX_FLUXMODELS -1){
cout<<"ERROR No mor flx models allowed, max:"<< NUM_MAX_FLUXMODELS<<endl;
return mindex;
}
mindex++;
}
return mindex;
}
double FireDomain::getArrivalTime(FFPoint& loc){
if ( !striclyWithinDomain(loc) ) return numeric_limits<double>::infinity();
size_t ii = (size_t) ((loc.getX()-SWCornerX())/burningMatrixResX);
size_t jj = (size_t) ((loc.getY()-SWCornerY())/burningMatrixResY);
return getArrivalTime(ii, jj);
}
double FireDomain::getArrivalTime(const size_t& ii, const size_t& jj){
if ( ii > globalBMapSizeX-1 ) return numeric_limits<double>::infinity();
if ( jj > globalBMapSizeY-1 ) return numeric_limits<double>::infinity();
size_t i = ii/localBMapSizeX;
size_t j = jj/localBMapSizeY;
return cells[i][j].getArrivalTime(ii%localBMapSizeX,jj%localBMapSizeY);
}
void FireDomain::setArrivalTime(const size_t& ii, const size_t& jj, const double& t){
// getting the cell where to pixel to set to burning lies
if ( ii > globalBMapSizeX-1 ) return ;
if ( jj > globalBMapSizeY-1 ) return ;
if ( ii > globalBMapSizeX-1 ) return ;
if ( jj > globalBMapSizeY-1 ) return ;
size_t i = ii/localBMapSizeX;
size_t j = jj/localBMapSizeY;
// setting the arrival time of this pixel
cells[i][j].setArrivalTime(ii%localBMapSizeX,jj%localBMapSizeY,t);
}
bool FireDomain::burnCheck(const size_t& ii, const size_t& jj, const double& t){
if ( getArrivalTime(ii-1,jj-1) > t ) return true;
if ( getArrivalTime(ii-1,jj) > t ) return true;
if ( getArrivalTime(ii,jj-1) > t ) return true;
if ( getArrivalTime(ii,jj) > t ) return true;
return false;
}
void FireDomain::firenodeBurningScan(FireNode* fn){
/* Local scan of the domain around a firenode. */
/* First a bounding box for scanning is computed.
* Then a local optimized polygon is defined locally
* to optimize the "point in polygon" algorithm,
* and lastly the scan is made */
// I/ Compute the region to be scanned
FFPoint swc, nec;
if ( fn->getPrev() == 0 or fn->getNext() == 0 ){
debugOutput<<getDomainID()<<": WARNING, launching a "
<<"FireDomain::firenodeBurningScan for a firenode "
<<"with no previous or next"<<endl;
swc.setX(fn->getX() - 2.*getPerimeterResolution());
swc.setY(fn->getY() - 2.*getPerimeterResolution());
nec.setX(fn->getX() + 2.*getPerimeterResolution());
nec.setY(fn->getY() + 2.*getPerimeterResolution());
} else {
computeBoundingBox(fn->getPrev(), fn, fn->getNext(), swc, nec);
}
// II/ Construct the vertices of a local optimized polygon
vector<double> nodesx, nodesy;
constructLocalSurroundingPolygon(fn, swc, nec, nodesx, nodesy);
// III/ Scanning the region
size_t nvert = nodesx.size();
double* vertx = 0;
double* verty = 0;
if ( nvert > 0 ){
vertx = new double[nvert];
verty = new double[nvert];
for ( size_t i = 0; i < nvert; i++ ){
vertx[i] = nodesx[i];
verty[i] = nodesy[i];
}
} else {
size_t nvert = fn->getFront()->getNumFN();
vertx = new double[nvert];
verty = new double[nvert];
fn->getFront()->storeVertices(vertx, verty, nvert);
}
singlePolygonAreaBurningScan(swc, nec, fn->getTime()
, fn->getFront()->isExpanding(), nvert, vertx, verty);
delete [] vertx;
delete [] verty;
}
void FireDomain::constructLocalSurroundingPolygon(FireNode* fn,
FFPoint& swc, FFPoint& nec, vector<double>& px, vector<double>& py){
/* Constructing a local polygon around the desired bounding box
* and taking into account the position of the firenodes in the bounding box */
// Constructing the polygon of the bounding box
size_t npoly = 4;
double* xpoly = new double[4];
double* ypoly = new double[4];
xpoly[0] = swc.getX();
ypoly[0] = swc.getY();
xpoly[1] = swc.getX();
ypoly[1] = nec.getY();
xpoly[2] = nec.getX();
ypoly[2] = nec.getY();
xpoly[3] = nec.getX();
ypoly[3] = swc.getY();
// Searching for the previous node outside this bounding box
FireNode* tmpfn = (fn->getPrev())->getPrev();
if ( tmpfn == 0 ){
delete [] xpoly;
delete [] ypoly;
return;
}
while ( tmpfn->getLoc().pointInPolygon(npoly, xpoly, ypoly) and tmpfn != fn ){
tmpfn = tmpfn->getPrev();
if ( tmpfn == 0 ){
delete [] xpoly;
delete [] ypoly;
return;
}
}
// If the front just happens to be contained in the bounding box returning nothing
if ( tmpfn == fn ){
delete [] xpoly;
delete [] ypoly;
return;
}
// Storing the locations of the firenodes inside the bounding box
// (with the previous and next nodes outside this box)
FFPoint pOut = tmpfn->getLoc();
tmpfn = tmpfn->getNext();
FFPoint pIn = tmpfn->getLoc();
while ( tmpfn->getLoc().pointInPolygon(npoly, xpoly, ypoly) ){
px.push_back(tmpfn->getX());
py.push_back(tmpfn->getY());
tmpfn = tmpfn->getNext();
if ( tmpfn == 0 ){
while ( !px.empty() ) {
px.pop_back();
py.pop_back();
}
return;
}
}
FFPoint nOut = tmpfn->getLoc();
FFPoint nIn = tmpfn->getPrev()->getLoc();
// Finding the intersection with the bounding box
int begSeg, endSeg;
FFPoint pInter = findIntersectionWithBoundingBox(pIn, pOut, swc, nec, endSeg);
FFPoint nInter = findIntersectionWithBoundingBox(nIn, nOut, swc, nec, begSeg);
px.push_back(nInter.getX());
py.push_back(nInter.getY());
// Linking the last and first points to have a closed polygon
FFPoint corner;
if ( begSeg == endSeg ){
if ( distanceOnBoundingBox(begSeg, nInter, pInter) < 0. ){
begSeg = (begSeg+1)%4;
corner = getBoundingBoxCornerFromIndex(begSeg, swc, nec);
px.push_back(corner.getX());
py.push_back(corner.getY());
}
}
while ( begSeg != endSeg ){
begSeg = (begSeg+1)%4;
corner = getBoundingBoxCornerFromIndex(begSeg, swc, nec);
px.push_back(corner.getX());
py.push_back(corner.getY());
}
px.push_back(pInter.getX());
py.push_back(pInter.getY());
delete [] xpoly;
delete [] ypoly;
}
void FireDomain::singlePolygonAreaBurningScan(FFPoint& swc, FFPoint& nec, double t
, bool expanding, size_t& nvert, double* vertx, double* verty){
/* Scanning a domain with a given front */
/* looking at the vertices of the matrix of arrival times.
* If this vertice is inside the firefront, setting the
* arrival time of the four adjacent cells */
// I/ Defining the region to be scanned in terms of burning matrix
size_t minI = 1;
if ( swc.getX() > SWCornerX()+burningMatrixResX )
minI = (size_t) ((swc.getX() - SWCornerX())/burningMatrixResX);
size_t maxI = (size_t) ((nec.getX() - SWCornerX())/burningMatrixResX) + 1;
if ( maxI > globalBMapSizeX - 1 ) maxI = globalBMapSizeX - 1;
size_t minJ = 1;
if ( swc.getY() > SWCornerY()/burningMatrixResY )
minJ = (size_t) ((swc.getY() - SWCornerY())/burningMatrixResY);
size_t maxJ = (size_t) ((nec.getY() - SWCornerY())/burningMatrixResY) + 1;
if ( maxJ > globalBMapSizeY - 1 ) maxJ = globalBMapSizeY - 1;
// II/ Scanning the region
FFPoint node;
for ( size_t i = minI; i <= maxI; i++ ){
for ( size_t j = minJ; j <= maxJ; j++ ){
node.setX(SWCornerX()+i*burningMatrixResX);
node.setY(SWCornerY()+j*burningMatrixResY);
if ( burnCheck(i,j,t) and
node.pointInPolygon(nvert, vertx, verty) == expanding ){
setArrivalTime(i-1, j-1, t);
setArrivalTime(i-1, j, t);
setArrivalTime(i, j-1, t);
setArrivalTime(i, j, t);
}
}
}
}
void FireDomain::areaBurningScan(FFPoint& swc, FFPoint& nec, double t){
/* Global scan of a bounding box for the update of the burning matrix. */
// I/ Defining the region to be scanned in terms of burning matrix
size_t minI = 1;
if ( swc.getX() > SWCornerX()+burningMatrixResX )
minI = (size_t) ((swc.getX() - SWCornerX())/burningMatrixResX);
size_t maxI = (size_t) ((nec.getX() - SWCornerX())/burningMatrixResX) + 1;
if ( maxI > globalBMapSizeX - 1 ) maxI = globalBMapSizeX - 1;
size_t minJ = 1;
if ( swc.getY() > SWCornerY()+burningMatrixResY )
minJ = (size_t) ((swc.getY() - SWCornerY())/burningMatrixResY);
size_t maxJ = (size_t) ((nec.getY() - SWCornerY())/burningMatrixResY) + 1;
if ( maxJ > globalBMapSizeY - 1 ) maxJ = globalBMapSizeY - 1;
// II/ Preparing the fronts for the point in polygon algorithm
domainFront->constructVerticesVectors();
// III/ Scanning the region
FFPoint node;
node.setX(SWCornerX()+minI*burningMatrixResX);
for ( size_t i = minI; i <= maxI; i++ ){
node.setY(SWCornerY()+minJ*burningMatrixResY);
for ( size_t j = minJ; j <= maxJ; j++ ){
if ( burnCheck(i, j, t) and checkForBurningStatus(node) ){
setArrivalTime(i-1, j-1, t);
setArrivalTime(i-1, j, t);
setArrivalTime(i, j-1, t);
setArrivalTime(i, j, t);
}
node.setY(node.getY()+burningMatrixResY);
}
node.setX(node.getX()+burningMatrixResX);
}
domainFront->deleteVerticesVectors();
}
void FireDomain::frontInitialBurningScan(const double& t, FireFront* ff
, const double& dmax, const double& dt){
/* Global scan of a front bounding box for
* the update of the burning matrix.
* Points inside the front are updated
* according to their distance to the front */
bool constantTimeInit = true;
if ( dmax > 0. ) constantTimeInit = false;
// I/ Construct the vertices of the current front and of all the fronts
size_t nvert = ff->getNumFN();
double* vertx = new double[nvert];
double* verty = new double[nvert];
ff->storeVertices(vertx, verty, nvert);
domainFront->constructVerticesVectors();
// II/ Compute the region to be scanned
FFPoint swc, nec;
ff->computeBoundingBox(swc, nec);
size_t minI = 1;
if ( swc.getX() > SWCornerX()+2.*burningMatrixResX )
minI = (size_t) ((swc.getX() - SWCornerX())/burningMatrixResX);
size_t maxI = (size_t) ((nec.getX() - SWCornerX())/burningMatrixResX) + 1;
if ( maxI > globalBMapSizeX - 1 ) maxI = globalBMapSizeX - 1;
size_t minJ = 1;
if ( swc.getY() > SWCornerY()+burningMatrixResY )
minJ = (size_t) ((swc.getY() - SWCornerY())/burningMatrixResY);
size_t maxJ = (size_t) ((nec.getY() - SWCornerY())/burningMatrixResY) + 1;
if ( maxJ > globalBMapSizeY - 1 ) maxJ = globalBMapSizeY - 1;
// III/ Scanning the region
double dist, at;
FFPoint node;
node.setX(SWCornerX()+minI*burningMatrixResX);
for ( size_t i = minI; i <= maxI; i++ ){
node.setY(SWCornerY()+minJ*burningMatrixResY);
for ( size_t j = minJ; j <= maxJ; j++ ){
if ( checkForBurningStatus(node) ){
if ( constantTimeInit ){
setArrivalTime(i-1, j-1, t);
setArrivalTime(i-1, j, t);
setArrivalTime(i, j-1, t);
setArrivalTime(i, j, t);
} else {
dist = node.signedDistanceToPolygon(
nvert, vertx, verty, ff->isExpanding());
at = t - dist*dt/dmax;
setArrivalTime(i-1, j-1, at);
setArrivalTime(i-1, j, at);
setArrivalTime(i, j-1, at);
setArrivalTime(i, j, at);
}
}
node.setY(node.getY()+burningMatrixResY);
}
node.setX(node.getX()+burningMatrixResX);
}
domainFront->deleteVerticesVectors();
delete [] vertx;
delete [] verty;
}
void FireDomain::frontBurningScan(FireFront* ff, const double& t){
/* Global scan of a front bounding box for the update of the burning matrix. */
// I/ Compute the region to be scanned
FFPoint swc, nec;