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 
24 #include "Init.h"
25 //#include "Pixel.h"
26 #include "Scheduler.h"
27 #include "ThreadManager.h"
28 #include "Output.h"
29 #include "ModelCore.h"
30 #include "ModelCoreSpatial.h"
31 
32 #include "Opt.h"
33 #include "Sandbox.h"
34 
35 //using namespace std;
36 
38  MTHREAD=MTHREAD_h;
39  InitState=0;
40 }
41 
43 {
44 }
45 
46 void
47 Init::setInitLevel(int level_h){
48 
49  switch (level_h){
50  case 0:
51  this->setInitLevel0();
52  break;
53  case 1:
54  this->setInitLevel1();
55  break;
56  case 2:
57  this->setInitLevel2();
58  break;
59  case 3:
60  this->setInitLevel3();
61  break;
62  case 4:
63  this->setInitLevel4();
64  break;
65  case 5:
66  this->setInitLevel5();
67  break;
68  case 6:
69  this->setInitLevel6();
70  break;
71  default:
72  msgOut(MSG_ERROR,"unexpected Init level");
73  }
74 }
75 
76 void
78  //unused now
79  InitState=0;
80 }
81 
82 /**
83 Setting up the space
84 <br>Level 1 :
85  - set the environment (settings, available resource name, possible activities)
86  - init the space
87 @see ModelData::setDefaultSettings();
88 @see Gis::setSpace()
89 @see Manager_farmers::setAgentMoulds()
90 
91 */
92 void
94  //Loading data from file.
95  InitState=1;
96  msgOut(MSG_DEBUG,"Entering Init state "+i2s(InitState));
97  time(&now);
98  current = localtime(&now);
99  string timemessage = "Local time is "+i2s(current->tm_hour)+":"+i2s(current->tm_min)+":"+ i2s(current->tm_sec);
100  msgOut(MSG_INFO, timemessage);
101  string scenarioName = MTHREAD->getScenarioName();
102  MTHREAD->MD->setScenarioData(); // set the characteristics (including overriding tables of the scneario)
105  if(MTHREAD->MD->getBoolSetting("newRandomSeed")){
106  // See here for how to use the new C++11 random functions:
107  // http://www.johndcook.com/cpp_TR1_random.html
108  // usage example:
109  // std::normal_distribution<double> d(100000,3);
110  // double x = d(*MTHREAD->gen);
111  srand(time(NULL));
112  //std::random_device randev;
113  //MTHREAD->gen = new std::mt19937(randev());
114  MTHREAD->gen = new std::mt19937(time(0));
115 
116  //TO.DO change scenarioname to scenarioname_random number
117  uniform_int_distribution<> ud(1, 1000000);
118  int randomscenario = ud(*MTHREAD->gen);
119 
120  MTHREAD->setScenarioName(scenarioName+"_"+i2s(randomscenario));
121 
122  } else {
123  MTHREAD->gen = new std::mt19937(NULL);
124  }
125  MTHREAD->SCD->setYear(MTHREAD->MD->getIntSetting("initialYear"));
127 
135  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
141  MTHREAD->GIS->setSpace();
143  MTHREAD->TEST->fullTest(); // normally empty function
144 }
145 
146 void
148  InitState=2;
149 }
150 
151 /**
152 Init 3 run the simulation/assign the values for the pre-optimisation year(s)
153 */
154 void
156  InitState=3;
157  MTHREAD->DO->initOutput(); // initialize the output files
158  if(MTHREAD->MD->getBoolSetting("usePixelData")){
160  } else {
162  }
163 }
164 
165 void
167  InitState=4;
168 }
169 
170 /**
171 Init level 5 pass the controll to the Scheduler object for the running of the simulations.
172 */
173 void
175  InitState=5;
176  MTHREAD->SCD->run(); // !!!! go "bello" !!!! start the simulation !!!!!
177 }
178 
179 void
181  InitState=6;
183  msgOut(MSG_INFO, "Model has ended scheduled simulation in a regular way.");
184  time(&now);
185  current = localtime(&now);
186  string timemessage = "Local time is "+i2s(current->tm_hour)+":"+i2s(current->tm_min)+":"+ i2s(current->tm_sec);
187  msgOut(MSG_INFO, timemessage);
188 }
189 
190 
191 
192 
193 
194 
195 
196 
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:180
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:147
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:77
~Init()
Definition: Init.cpp:42
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:174
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:37
void printFinalOutput()
Definition: Output.cpp:464
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:155
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:47
void setInitLevel1()
Setting up the space, the model objects and the agents (definitions only)
Definition: Init.cpp:93
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:166
Sandbox * TEST
Various debugging code for development.
Definition: ThreadManager.h:80
time_t now
Definition: Init.h:72