FFSM++  1.1.0
French Forest Sector Model ++
Init.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3  * http://ffsm-project.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version, given the compliance with the *
9  * exceptions listed in the file COPYING that is distribued together *
10  * with this file. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 #include <time.h> // we only use this to seed the random number generator
23 #include <unistd.h>
24 
25 #include "Init.h"
26 //#include "Pixel.h"
27 #include "Scheduler.h"
28 #include "ThreadManager.h"
29 #include "Output.h"
30 #include "ModelCore.h"
31 #include "ModelCoreSpatial.h"
32 
33 #include "Opt.h"
34 #include "Sandbox.h"
35 
36 //using namespace std;
37 
39  MTHREAD=MTHREAD_h;
40  InitState=0;
41 }
42 
44 {
45 }
46 
47 void
48 Init::setInitLevel(int level_h){
49 
50  switch (level_h){
51  case 0:
52  this->setInitLevel0();
53  break;
54  case 1:
55  this->setInitLevel1();
56  break;
57  case 2:
58  this->setInitLevel2();
59  break;
60  case 3:
61  this->setInitLevel3();
62  break;
63  case 4:
64  this->setInitLevel4();
65  break;
66  case 5:
67  this->setInitLevel5();
68  break;
69  case 6:
70  this->setInitLevel6();
71  break;
72  default:
73  msgOut(MSG_ERROR,"unexpected Init level");
74  }
75 }
76 
77 void
79  //unused now
80  InitState=0;
81 }
82 
83 /**
84 Setting up the space
85 <br>Level 1 :
86  - set the environment (settings, available resource name, possible activities)
87  - init the space
88 @see ModelData::setDefaultSettings();
89 @see Gis::setSpace()
90 @see Manager_farmers::setAgentMoulds()
91 
92 */
93 void
95  //Loading data from file.
96  InitState=1;
97  msgOut(MSG_DEBUG,"Entering Init state "+i2s(InitState));
98  time(&now);
99  current = localtime(&now);
100  string timemessage = "Local time is "+i2s(current->tm_hour)+":"+i2s(current->tm_min)+":"+ i2s(current->tm_sec);
101  msgOut(MSG_INFO, timemessage);
102  string scenarioName = MTHREAD->getScenarioName();
103  MTHREAD->MD->setScenarioData(); // set the characteristics (including overriding tables of the scneario)
106  if(MTHREAD->MD->getBoolSetting("newRandomSeed")){
107  // See here for how to use the new C++11 random functions:
108  // http://www.johndcook.com/cpp_TR1_random.html
109  // usage example:
110  // std::normal_distribution<double> d(100000,3);
111  // double x = d(*MTHREAD->gen);
112  srand(time(NULL)+getpid());
113  //std::random_device randev;
114  //MTHREAD->gen = new std::mt19937(randev());
115  MTHREAD->gen = new std::mt19937(time(0)+getpid());
116 
117  //TO.DO change scenarioname to scenarioname_random number
118  uniform_int_distribution<> ud(1, 1000000);
119  int randomscenario = ud(*MTHREAD->gen);
120 
121  MTHREAD->setScenarioName(scenarioName+"_"+i2s(randomscenario));
122 
123  } else {
124  MTHREAD->gen = new std::mt19937(NULL);
125  }
126  MTHREAD->SCD->setYear(MTHREAD->MD->getIntSetting("initialYear"));
128 
136  MTHREAD->MD->applyOverrides(); // Cancel all reg1 level data and trasform them in reg2 level if not already existing. Acts on forDataMap, prodDataMap and reclRules vectors
142  MTHREAD->GIS->setSpace();
144  MTHREAD->TEST->fullTest(); // normally empty function
145 }
146 
147 void
149  InitState=2;
150 }
151 
152 /**
153 Init 3 run the simulation/assign the values for the pre-optimisation year(s)
154 */
155 void
157  InitState=3;
158  MTHREAD->DO->initOutput(); // initialize the output files
159  if(MTHREAD->MD->getBoolSetting("usePixelData")){
161  } else {
163  }
164 }
165 
166 void
168  InitState=4;
169 }
170 
171 /**
172 Init level 5 pass the controll to the Scheduler object for the running of the simulations.
173 */
174 void
176  InitState=5;
177  MTHREAD->SCD->run(); // !!!! go "bello" !!!! start the simulation !!!!!
178 }
179 
180 void
182  InitState=6;
184  msgOut(MSG_INFO, "Model has ended scheduled simulation in a regular way.");
185  time(&now);
186  current = localtime(&now);
187  string timemessage = "Local time is "+i2s(current->tm_hour)+":"+i2s(current->tm_min)+":"+ i2s(current->tm_sec);
188  msgOut(MSG_INFO, timemessage);
189 }
190 
191 
192 
193 
194 
195 
196 
197 
void initOutput()
Definition: Output.cpp:48
Print an ERROR message, but don&#39;t stop the model.
Definition: BaseClass.h:61
void runInitPeriod()
Definition: ModelCore.cpp:50
int getIntSetting(const string &name_h, int position=0, int reg=WORLD) const
Definition: ModelData.cpp:1105
void setDefaultPathogenRules()
Definition: ModelData.cpp:678
void setDefaultForData()
Definition: ModelData.cpp:481
bool getBoolSetting(const string &name_h, int position=0, int reg=WORLD) const
Definition: ModelData.cpp:1117
void setScenarioProdData()
Definition: ModelData.cpp:561
void run()
Definition: Scheduler.cpp:41
string i2s(const int &int_h) const
integer to string conversion
Definition: BaseClass.cpp:219
ThreadManager * MTHREAD
Pointer to the Thread manager.
Definition: BaseClass.h:467
ModelData * MD
the model data object
Definition: ThreadManager.h:72
void applyForestReclassification()
Apply the forest reclassification with the rules defined in reclRules sheet.
Definition: Gis.cpp:439
void setInitLevel6()
End of simulation (e.g. print summary statistics)
Definition: Init.cpp:181
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
Definition: ThreadManager.h:75
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
Definition: BaseClass.cpp:50
void setDefaultSettings()
Definition: ModelData.cpp:194
Gis * GIS
GIS information and methods.
Definition: ThreadManager.h:73
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
void setScenarioForData()
Definition: ModelData.cpp:500
void setScenarioName(const string &scenarioName_h)
ModelCore * CORE
Core of the model.
Definition: ThreadManager.h:77
ModelCoreSpatial * SCORE
Core of the model (spatial version)
Definition: ThreadManager.h:78
void fullTest()
Tests that require a full sandbox object including MTHREAD. Normally empty.
Definition: Sandbox.cpp:599
void setInitLevel2()
Unused, reserver for future use.
Definition: Init.cpp:148
Output * DO
data output
Definition: ThreadManager.h:76
int InitState
One of the 7 possible init states (0..6)
Definition: Init.h:67
void setInitLevel0()
Unused, reserver for future use.
Definition: Init.cpp:78
~Init()
Definition: Init.cpp:43
void applyDebugMode()
Works only a specified subset of regions and products.
Definition: ModelData.cpp:957
void applyOverrides()
Cancel all reg1 level data and trasform them in reg2 level if not already existing.
Definition: ModelData.cpp:749
Print a debug message, normally filtered out.
Definition: BaseClass.h:58
void setInitLevel5()
Simulation start.
Definition: Init.cpp:175
void setScenarioProductResourceMatrixLink()
Definition: ModelData.cpp:632
void setSpace()
Set the initial space environment, including loading data from files.
Definition: Gis.cpp:57
string getScenarioName()
Init(ThreadManager *MTHREAD_h)
Definition: Init.cpp:38
void printFinalOutput()
Definition: Output.cpp:462
void cacheSettings()
Called after input reading, it fix frequently used data;.
Definition: ModelData.cpp:305
void setReclassificationRules()
Definition: ModelData.cpp:663
void setInitLevel3()
Linking object to agents and assigning space proprieties to objects and agents.
Definition: Init.cpp:156
void setForestTypes()
Definition: ModelData.cpp:648
std::mt19937 * gen
used in the sampling from normal distribution
Definition: ThreadManager.h:83
void setInitLevel(int level_h)
Wrapper to the correct setInitLevelX()
Definition: Init.cpp:48
void setInitLevel1()
Setting up the space, the model objects and the agents (definitions only)
Definition: Init.cpp:94
struct tm * current
Definition: Init.h:71
void setScenarioSettings()
Definition: ModelData.cpp:224
Print an INFO message.
Definition: BaseClass.h:59
void setYear(const int &year_h)
Definition: Scheduler.h:50
void createRegions()
Definition: ModelData.cpp:317
void setDefaultProductResourceMatrixLink()
Definition: ModelData.cpp:618
void setScenarioPathogenRules()
Definition: ModelData.cpp:706
void setScenarioData()
Set the infos about this scenario (long description and overriding tables)
Definition: ModelData.cpp:170
void setDefaultProdData()
Definition: ModelData.cpp:531
void setInitLevel4()
Unused, reserver for future use.
Definition: Init.cpp:167
Sandbox * TEST
Various debugging code for development.
Definition: ThreadManager.h:80
time_t now
Definition: Init.h:72