FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
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
47void
48Init::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
77void
79 //unused now
80 InitState=0;
81}
82
83/**
84Setting 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*/
93void
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
147void
151
152/**
153Init 3 run the simulation/assign the values for the pre-optimisation year(s)
154*/
155void
157 InitState=3;
158 MTHREAD->DO->initOutput(); // initialize the output files
159 if(MTHREAD->MD->getBoolSetting("usePixelData")){
161 } else {
163 }
164}
165
166void
170
171/**
172Init level 5 pass the controll to the Scheduler object for the running of the simulations.
173*/
174void
176 InitState=5;
177 MTHREAD->SCD->run(); // !!!! go "bello" !!!! start the simulation !!!!!
178}
179
180void
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
@ MSG_ERROR
Print an ERROR message, but don't stop the model.
Definition BaseClass.h:61
@ MSG_DEBUG
Print a debug message, normally filtered out.
Definition BaseClass.h:58
@ MSG_INFO
Print an INFO message.
Definition BaseClass.h:59
ThreadManager * MTHREAD
Pointer to the Thread manager.
Definition BaseClass.h:467
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
string i2s(const int &int_h) const
integer to string conversion
void applyForestReclassification()
Apply the forest reclassification with the rules defined in reclRules sheet.
Definition Gis.cpp:439
void setSpace()
Set the initial space environment, including loading data from files.
Definition Gis.cpp:57
int InitState
One of the 7 possible init states (0..6)
Definition Init.h:70
void setInitLevel6()
End of simulation (e.g. print summary statistics)
Definition Init.cpp:181
void setInitLevel5()
Simulation start.
Definition Init.cpp:175
void setInitLevel0()
Unused, reserver for future use.
Definition Init.cpp:78
time_t now
Definition Init.h:72
void setInitLevel(int level_h)
Wrapper to the correct setInitLevelX()
Definition Init.cpp:48
void setInitLevel3()
Linking object to agents and assigning space proprieties to objects and agents.
Definition Init.cpp:156
void setInitLevel1()
Setting up the space, the model objects and the agents (definitions only)
Definition Init.cpp:94
Init(ThreadManager *MTHREAD_h)
Definition Init.cpp:38
void setInitLevel4()
Unused, reserver for future use.
Definition Init.cpp:167
~Init()
Definition Init.cpp:43
void setInitLevel2()
Unused, reserver for future use.
Definition Init.cpp:148
struct tm * current
Definition Init.h:71
void runInitPeriod()
Definition ModelCore.cpp:50
void cacheSettings()
Called after input reading, it fix frequently used data;.
void setDefaultForData()
void setScenarioData()
Set the infos about this scenario (long description and overriding tables)
bool getBoolSetting(const string &name_h, int position=0, int reg=WORLD) const
void createRegions()
void setDefaultSettings()
void setScenarioProdData()
void applyDebugMode()
Works only a specified subset of regions and products.
void setDefaultPathogenRules()
void setScenarioPathogenRules()
void setDefaultProdData()
void setScenarioProductResourceMatrixLink()
void setReclassificationRules()
void setScenarioSettings()
void setForestTypes()
int getIntSetting(const string &name_h, int position=0, int reg=WORLD) const
void setDefaultProductResourceMatrixLink()
void applyOverrides()
Cancel all reg1 level data and trasform them in reg2 level if not already existing.
void setScenarioForData()
void printFinalOutput()
Definition Output.cpp:462
void initOutput()
Definition Output.cpp:48
void fullTest()
Tests that require a full sandbox object including MTHREAD. Normally empty.
Definition Sandbox.cpp:600
void run()
Definition Scheduler.cpp:41
void setYear(const int &year_h)
Definition Scheduler.h:50
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
void setScenarioName(const string &scenarioName_h)
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
ModelCoreSpatial * SCORE
Core of the model (spatial version)
Sandbox * TEST
Various debugging code for development.
string getScenarioName()
Gis * GIS
GIS information and methods.
ModelData * MD
the model data object
std::mt19937 * gen
used in the sampling from normal distribution
Output * DO
data output
ModelCore * CORE
Core of the model.