FFSM++  1.1.0
French Forest Sector Model ++
ModelCoreSpatial.h
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 #ifndef MODELCORESPATIAL_H
23 #define MODELCORESPATIAL_H
24 
25 // Core C++ headers
26 #include <string>
27 #include <vector>
28 #include <map>
29 #include <stdexcept>
30 #include <iostream>
31 #include <sstream>
32 
33 // Qt headers...
34 
35 // FFSM headers...
36 #include "BaseClass.h"
37 #include "ThreadManager.h"
38 #include "ModelData.h"
39 #include "Pixel.h"
40 
41 /**
42 * \brief The core of the model (spatial version).
43 *
44 * Once the environment is initialised (mainly data load, space created), the model is run through the two functions runInitPeriod()
45 * and runSimulationYear().
46 *
47 *
48 * Some importan notes:
49 * V (volumes) -> at the end of the year
50 * In (inventary) -> at the beginning of the year
51 * Area -> at the end of the year
52 * Harvesting -> at the beginning of the year
53 * Volumes are in Mm^3, Areas in the model in Ha (10000 m^2), in the layers in m^2, vHa in m^3/ha. Prices are in €/m^3.
54 * Policy balances (- -> public cost,subside; + -> public revenue) and surpluses are in milion €
55 *
56 * BALANCE:
57 * PROD_forLocal (sl) + PROD_forExp (sa) + IMP (da) + sum_reg(reg_trade_in) = CONS_fromLocal (dl) + CONS_fromImp (da) + EXP (sa) + sum_reg(reg_trade_out)
58 * note that this means that sl includes alread reg_trade_out, and dl includes already reg_trade_in
59 *
60 * Where are volumes information ?
61 * - ip px->vol - by px, ft and dc
62 * - in forDataMap (through gft()) - by reg, ft and dc
63 * Where is area information ?
64 * - in px->area - by px, ft and dc
65 * - in forDataMap (through gft()) - by reg, ft and dc
66 * - in px->values map (forArea_* layer, through px->getDoubleValue()) - by px and ft
67 *
68 * Aggregation of the Expected returns
69 *
70 * The problem is how to aggregate the expected returns, given at pixel anf ft level, first at the regional level, then at the ft group level (B/C) and
71 * total forest level and finally at national level from regional one.
72 *
73 * A - From pixel to region
74 * - weighted by total forest area in the pixel
75 * B1 - From ft to ft group
76 * - in each pixel we take the highest expRet within the pixel and we weight by farea to get the regional value
77 * B2 - From ft group to forest
78 * - actually, from ft to group: like b1, but we take the highest value in each px for any ft and we weight by forest area in the px to get the regional value
79 * C - From region to country
80 * - we weight the individual ft, ft group and forest by the different regional total forest areas.*
81 *
82 */
83 class ModelCoreSpatial : public BaseClass {
84 
85 public:
86  ModelCoreSpatial(ThreadManager* MTHREAD_h);
88 
89  void runInitPeriod();
90  void runSimulationYear();
91 
92  void initMarketModule(); ///< computes st and pw for second year and several needed-only-at-t0-vars for the market module
93  void runMarketModule(); ///< computes st (supply total) and pw (weighted price). Optimisation inside.
94  void runBiologicalModule(); ///< computes hV, hArea and new vol at end of year
95  void runManagementModule(); ///< computes regArea and expectedReturns
96  void sumRegionalForData(); ///< computes vol, hV, harvestedArea, regArea and expReturns at reg level from the pixel level
97  void initialiseCarbonModule(); ///< call initialiseDeathBiomassStocks(), initialiseProductsStocks() and initialiseEmissionCounters()
98  void initialiseDeathTimber(); ///< Set deathTimberInventory to zero for the previous years (under the hipotesis that we don't have advanced stock of death biomass usable as timber at the beginning of the simulation)
99 
100  void registerCarbonEvents(); ///< call registerHarvesting(), registerDeathBiomass(), registerProducts() and registerTransports()
101  void cacheSettings(); ///< just cache exogenous settings from ModelData
102  void cacheDynamicSettings(); ///< cache settings that may change with time
103  void initializePixelVolumes(); ///< distribuite regional exogenous volumes to pixel volumes using corine land cover area as weight
104  void assignSpMultiplierPropToVols(); // assign the spatial multiplier (used in the time of return) based no more on a Normal distribution but on the volumes present in the pixel: more volume, more the pixel is fit for the ft
105  void initializePixelArea(); ///< compute px->area for each ft and dc
106  void resetPixelValues(); ///< swap volumes->lagged_volumes and reset the other pixel vectors
107  void cachePixelExogenousData();///< computes pixel level tp, meta and mort
108  void computeInventary(); ///< in=f(vol_t-1)
109  void computeCumulativeData(); ///< computes cumTp_exp, vHa_exp, vHa
110  void updateMapAreas(); ///< computes forArea_{ft}
111  void updateOtherMapData(); ///< update (if the layer exists) other gis-based data, as volumes and expected returns, taking them from the data in the px object
112  double computeExpectedPrice(const double & curLocPrice, const double & worldCurPrice, const double & worldFutPrice, const double & sl, const double & sa, const double & expCoef); ///< Compute weighted expected price for a given product.
113  void printDebugInitRegionalValues(); ///< print initial inv, st, sl and sa in each region
114  vector <double> allocateHarvesting(vector<double> total_st, const int & regId); ///< Using the deathTimberInventory map, this function allocate the total st in st from death timber (that goes reduce the deathTimberInventory map) and stFromHarvesting that is what it remains after the allocation to death timber.
115  void loadExogenousForestLayers(const string & what); ///< Set pixel volumes (what="vol") OR areas (what="area") by specific forest types as defined in gis layers for volumes and proportionally to volumes for areas.
116  void computeEconomicBalances();///< compute the policy balances (- -> costs; + -> rev) and the producer/consumer surpluses
117  double getAvgAgeByDc(Pixel* px, int ft, int dc); ///< return the average age of a tree at a given diameter class, using the cumTp vector
118 
119  // added for carbon project
120  double getVHaByYear (const Pixel* px, const int& ft, const int& year, const double& extraBiomass_ratio, const int& regId ) const; ///< return the Volume/ha in a forest after a given number of year after planting, for a specific forest type
121 
122  // convenient handles to equivalent ModelData functions..
123  double gpd(const string &type_h, const int& regId_h, const string &prodId_h, const int& year=DATA_NOW, const string &freeDim_h="") const {return MTHREAD->MD->getProdData(type_h, regId_h, prodId_h, year, freeDim_h);};
124  double gfd(const string &type_h, const int& regId_h, const string &forType_h, const string &freeDim_h, const int& year=DATA_NOW) const {return MTHREAD->MD->getForData(type_h, regId_h, forType_h, freeDim_h, year);};
125  void spd(const double& value_h, const string &type_h, const int& regId_h, const string &prodId_h, const int& year=DATA_NOW, const bool& allowCreate=false, const string &freeDim_h="") const {MTHREAD->MD->setProdData(value_h, type_h, regId_h, prodId_h, year, allowCreate, freeDim_h);};
126  void sfd(const double& value_h, const string &type_h, const int& regId_h, const string &forType_h, const string &freeDim_h, const int& year=DATA_NOW, const bool& allowCreate=false) const {MTHREAD->MD->setForData(value_h, type_h, regId_h, forType_h, freeDim_h, year, allowCreate);};
127  bool app(const string &prod_h, const string &forType_h, const string &dClass_h) const {return MTHREAD->MD->assessProdPossibility(prod_h, forType_h, dClass_h);};
128 
129 private:
130  ModelData* MD;
134  int WL2;
135  vector <int> regIds2;
136  vector <string> priProducts;
137  vector <string> secProducts;
138  vector <string> allProducts;
139  vector <string> dClasses;
140  vector <string> pDClasses;
141  vector <string> fTypes;
142  vector <vector <int> > l2r;
143  string regType;
145  //double mr;
146  vector <Pixel*> regPx; // pixels behaving to the current region
150  double ir; // interest rate
151 };
152 
153 #endif // MODELCORESPATIAL_H
The required data is for the current year.
Definition: BaseClass.h:73
void runBiologicalModule()
computes hV, hArea and new vol at end of year
vector< vector< int > > l2r
vector< string > dClasses
void runManagementModule()
computes regArea and expectedReturns
void initializePixelVolumes()
distribuite regional exogenous volumes to pixel volumes using corine land cover area as weight ...
vector< Pixel * > regPx
void sumRegionalForData()
computes vol, hV, harvestedArea, regArea and expReturns at reg level from the pixel level ...
void spd(const double &value_h, const string &type_h, const int &regId_h, const string &prodId_h, const int &year=DATA_NOW, const bool &allowCreate=false, const string &freeDim_h="") const
ThreadManager * MTHREAD
Pointer to the Thread manager.
Definition: BaseClass.h:467
ModelData * MD
the model data object
Definition: ThreadManager.h:72
void loadExogenousForestLayers(const string &what)
Set pixel volumes (what="vol") OR areas (what="area") by specific forest types as defined in gis laye...
vector< string > priProducts
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
void resetPixelValues()
swap volumes->lagged_volumes and reset the other pixel vectors
vector< string > pDClasses
void computeInventary()
in=f(vol_t-1)
Regional data, including macros and settings.
Definition: ModelData.h:80
const double getProdData(const string &type_h, const int &regId_h, const string &prodId_h, const int &year=DATA_NOW, const string &freeDim_h="")
Definition: ModelData.cpp:1216
void runMarketModule()
computes st (supply total) and pw (weighted price). Optimisation inside.
void cacheSettings()
just cache exogenous settings from ModelData
const double getForData(const string &type_h, const int &regId_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW)
Definition: ModelData.cpp:1281
void setProdData(const double &value_h, const string &type_h, const int &regId_h, const string &prodId_h, const int &year=DATA_NOW, const bool &allowCreate=false, const string &freeDim_h="")
Definition: ModelData.cpp:1352
void updateOtherMapData()
update (if the layer exists) other gis-based data, as volumes and expected returns, taking them from the data in the px object
void initialiseDeathTimber()
Set deathTimberInventory to zero for the previous years (under the hipotesis that we don&#39;t have advan...
vector< string > fTypes
Pixel-level class.
Definition: Pixel.h:47
double getVHaByYear(const Pixel *px, const int &ft, const int &year, const double &extraBiomass_ratio, const int &regId) const
return the Volume/ha in a forest after a given number of year after planting, for a specific forest t...
vector< string > allProducts
vector< string > secProducts
double gpd(const string &type_h, const int &regId_h, const string &prodId_h, const int &year=DATA_NOW, const string &freeDim_h="") const
Base class for the regmas application.
Definition: BaseClass.h:239
ModelCoreSpatial(ThreadManager *MTHREAD_h)
double getAvgAgeByDc(Pixel *px, int ft, int dc)
return the average age of a tree at a given diameter class, using the cumTp vector ...
void initMarketModule()
computes st and pw for second year and several needed-only-at-t0-vars for the market module ...
double computeExpectedPrice(const double &curLocPrice, const double &worldCurPrice, const double &worldFutPrice, const double &sl, const double &sa, const double &expCoef)
Compute weighted expected price for a given product.
void initializePixelArea()
compute px->area for each ft and dc
vector< int > regIds2
void sfd(const double &value_h, const string &type_h, const int &regId_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW, const bool &allowCreate=false) const
void setForData(const double &value_h, const string &type_h, const int &regId_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW, const bool &allowCreate=false)
Definition: ModelData.cpp:1412
double gfd(const string &type_h, const int &regId_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW) const
bool app(const string &prod_h, const string &forType_h, const string &dClass_h) const
The core of the model (spatial version).
void assignSpMultiplierPropToVols()
ModelCoreSpatial::assignSpMultiplierPropToVols assigns the spatial multiplier (used in the time of re...
void cacheDynamicSettings()
cache settings that may change with time
void initialiseCarbonModule()
call initialiseDeathBiomassStocks(), initialiseProductsStocks() and initialiseEmissionCounters() ...
void cachePixelExogenousData()
computes pixel level tp, meta and mort
This file is the header of BaseClass and it is included by ALL compiled code.
void updateMapAreas()
computes forArea_{ft}
void registerCarbonEvents()
call registerHarvesting(), registerDeathBiomass(), registerProducts() and registerTransports() ...
const bool assessProdPossibility(const string &prod_h, const string &forType_h, const string &dClass_h)
A simple function to assess if a specified product can be made by a certain forest type and diameter ...
Definition: ModelData.cpp:441
void computeCumulativeData()
computes cumTp_exp, vHa_exp, vHa
void printDebugInitRegionalValues()
print initial inv, st, sl and sa in each region
void computeEconomicBalances()
compute the policy balances (- -> costs; + -> rev) and the producer/consumer surpluses ...
vector< double > allocateHarvesting(vector< double > total_st, const int &regId)
Using the deathTimberInventory map, this function allocate the total st in st from death timber (that...