iLand
model.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef MODEL_H
21#define MODEL_H
22#include <QtCore>
23#include <QtXml>
24
25#include "global.h"
26
27#include "grid.h"
28#include "threadrunner.h"
29#include "modelsettings.h"
30
31// forward declarations
32class ResourceUnit;
33class SpeciesSet;
34class Management;
35class Saplings;
36
37namespace ABE {
38class ForestManagementEngine;
39}
40class Climate;
41class Environment;
42class TimeEvents;
43class MapGrid;
44class Modules;
45class DEM;
46class GrassCover;
47class SVDStates;
48namespace BITE { class BiteEngine; }
49
51{
52 float height;
53 int count() const { return mCount & 0x0000ffff; }
54 void increaseCount() { mCount++; }
55 void resetCount() { mCount &= 0xffff0000; }
59 bool isValid() const { return !isBitSet(mCount, 16); }
60 void setValid(const bool valid) { setBit(mCount, 16, !valid); }
61 void setForestOutside(const bool is_outside) { setBit(mCount, 17, is_outside); }
62 bool isForestOutside() const {return isBitSet(mCount, 17); }
63 void setIsRadiating() { setBit(mCount, 18, true); }
64 bool isRadiating() const { return isBitSet(mCount, 18); }
66 float stemHeight() const { return static_cast<float> (mCount >> 20 & 0xff);}
68 void clearStemHeight() { mCount &= 0xF00FFFFF; }
70 void setStemHeight(float h) { unsigned int hval = static_cast<unsigned int>(h + 0.5f); // round: +0.5 + truncate
71 unsigned int w = mCount;
72 w &= 0xF00FFFFF;
73 w |= hval << 20;
74 mCount = w; }
76 void init(const float aheight, const int acount) { height=aheight;mCount=static_cast<unsigned int>(acount); }
77private:
84 quint32 mCount; // 32 bit unsigned int (even on 64bit platforms)
85
86};
88
89class Model
90{
91public:
92 Model();
93 ~Model();
94 // start/stop/run
95 void beforeRun();
96 void runYear();
97 void afterStop();
98
99 // access to elements
100 const ThreadRunner &threadExec() const {return threadRunner; }
101 const QRectF &extent() const { return mModelRect; }
102 double totalStockableArea() const { return mTotalStockableArea; }
103 ResourceUnit *ru() { return mRU.front(); }
104 ResourceUnit *ru(QPointF coord);
105 ResourceUnit *ru(int index) { return (index>=0&&index<mRU.count())? mRU[index] : NULL; }
106 ResourceUnit *ruById(int id) const;
108 const QList<ResourceUnit*> &ruList() const {return mRU; }
109 Management *management() const { return mManagement; }
110 ABE::ForestManagementEngine *ABEngine() const { return mABEManagement; }
111 BITE::BiteEngine *biteEngine() const { return mBiteEngine; }
112
113 Environment *environment() const {return mEnvironment; }
114 Saplings *saplings() const {return mSaplings; }
115 TimeEvents *timeEvents() const { return mTimeEvents; }
116 Modules *modules() const { return mModules; }
117 const DEM *dem() const { return mDEM; }
118 GrassCover *grassCover() const { return mGrassCover; }
119 SpeciesSet *speciesSet() const { if (mSpeciesSets.count()==1) return mSpeciesSets.first(); return NULL; }
120 const QList<Climate*> climates() const { return mClimates; }
121 SVDStates *svdStates() const { return mSVDStates; }
122
123 // global grids
124 FloatGrid *grid() { return mGrid; }
125 HeightGrid *heightGrid() { return mHeightGrid; }
126 const MapGrid *standGrid() { return mStandGrid; }
127 const Grid<ResourceUnit*> &RUgrid() { return mRUmap; }
129 const HeightGridValue heightGridValue(const int ix, const int iy) const { return mHeightGrid->constValueAtIndex(ix/cPxPerHeight, iy/cPxPerHeight); }
130 const HeightGridValue &heightGridValue(const float *lif_ptr) const { QPoint p = mGrid->indexOf(lif_ptr); return mHeightGrid->constValueAtIndex(p.x()/cPxPerHeight, p.y()/cPxPerHeight); }
131
132 // setup/maintenance
133 void clear();
134 void loadProject();
135 bool isSetup() const { return mSetup; }
136 static const ModelSettings &settings() {return mSettings;}
137 static ModelSettings &changeSettings() {return mSettings;}
138 void onlyApplyLightPattern() { applyPattern(); readPattern(); }
139 void reloadABE();
140 QString currentTask() const { return mCurrentTask; }
141 void setCurrentTask(QString what) { mCurrentTask = what; }
142
143 // actions
147 void cleanTreeLists(bool recalculate_stats);
149 void executePerResourceUnit(void (*funcptr)(ResourceUnit*), const bool forceSingleThreaded=false) { threadRunner.run(funcptr, forceSingleThreaded);}
150
151private:
152 void initialize();
153 void setupSpace();
154 void initOutputDatabase();
155
156 void applyPattern();
157 void readPattern();
158 void grow();
159
160 void calculateStockedArea();
161 void calculateStockableArea();
162 void initializeGrid();
163
164 void test();
165 void debugCheckAllTrees();
166 bool multithreading() const { return threadRunner.multithreading(); }
167 ThreadRunner threadRunner;
168 static ModelSettings mSettings;
169 QString mCurrentTask;
170 bool mSetup;
172 QList<ResourceUnit*> mRU;
174 Grid<ResourceUnit*> mRUmap;
176 QList<SpeciesSet*> mSpeciesSets;
178 QList<Climate*> mClimates;
179 //
180 Modules *mModules;
181 //
182 QRectF mModelRect;
183 double mTotalStockableArea;
184 // global grids...
185 FloatGrid *mGrid;
186 HeightGrid *mHeightGrid;
187 Saplings *mSaplings;
188 Management *mManagement;
189 ABE::ForestManagementEngine *mABEManagement;
190 BITE::BiteEngine *mBiteEngine;
191 Environment *mEnvironment;
192 TimeEvents *mTimeEvents;
193 MapGrid *mStandGrid;
194 // Digital elevation model
195 DEM *mDEM;
196 GrassCover *mGrassCover;
197 // SVD States
198 SVDStates *mSVDStates;
199};
200
201class Tree;
203{
204public:
205 AllTreeIterator(Model* model): mModel(model), mTreeEnd(0),mCurrent(0) {}
206 void reset() { mTreeEnd=0; mCurrent=0; }
207 Tree *next();
208 Tree *nextLiving();
209 Tree *current() const;
210 Tree *operator*() const { return current(); }
211 ResourceUnit *currentRU() const { return *mRUIterator; }
212private:
213 Model *mModel;
214 Tree *mTreeEnd;
215 Tree *mCurrent;
216 QList<ResourceUnit*>::const_iterator mRUIterator;
217};
218#endif // MODEL_H
the ForestManagementEngine is the container for the agent based forest management engine.
Definition: forestmanagementengine.h:43
Definition: model.h:203
Tree * operator*() const
Definition: model.h:210
Tree * current() const
Definition: model.cpp:115
Tree * next()
iterate over all trees of the model.
Definition: model.cpp:72
ResourceUnit * currentRU() const
Definition: model.h:211
AllTreeIterator(Model *model)
Definition: model.h:205
void reset()
Definition: model.h:206
Tree * nextLiving()
Definition: model.cpp:109
Definition: biteengine.h:30
Climate handles climate input data and performs some basic related calculations on that data.
Definition: climate.h:66
DEM is a digital elevation model class.
Definition: dem.h:40
Environment specifes the geographical properties of the landscape.
Definition: environment.h:34
The GrassCover class specifies the limiting effect of ground vegetation (grasses, herbs) on the regen...
Definition: grasscover.h:39
Grid class (template).
Definition: grid.h:44
const T & constValueAtIndex(const QPoint &pos) const
value at position defined by a (integer) QPoint
Definition: grid.h:109
QPoint indexOf(const int index) const
get index (x/y) of the (linear) index 'index' (0..count-1)
Definition: grid.h:127
management routines.
Definition: management.h:32
Definition: mapgrid.h:32
Main object of the iLand model composited of various sub models / sub components.
Definition: model.h:90
Management * management() const
Definition: model.h:109
void afterStop()
finish and cleanup
Definition: model.cpp:945
GrassCover * grassCover() const
Definition: model.h:118
void createStandStatistics()
build stand statistics (i.e. stats based on resource units)
Definition: model.cpp:1249
const Grid< ResourceUnit * > & RUgrid()
Definition: model.h:127
SVDStates * svdStates() const
Definition: model.h:121
ResourceUnit * ru(int index)
get resource unit by index
Definition: model.h:105
const MapGrid * standGrid()
retrieve the spatial grid that defines the stands (10m resolution)
Definition: model.h:126
static ModelSettings & changeSettings()
write access to global model settings.
Definition: model.h:137
void reloadABE()
force a recreate of the agent based forest management engine
Definition: model.cpp:592
void executePerResourceUnit(void(*funcptr)(ResourceUnit *), const bool forceSingleThreaded=false)
execute a function for each resource unit using multiple threads. "funcptr" is a ptr to a simple func...
Definition: model.h:149
BITE::BiteEngine * biteEngine() const
Definition: model.h:111
HeightGrid * heightGrid()
stores maximum heights of trees and some flags (currently 10x10m)
Definition: model.h:125
const ThreadRunner & threadExec() const
Definition: model.h:100
bool isSetup() const
return true if the model world is correctly setup.
Definition: model.h:135
ResourceUnit * ru()
Definition: model.h:103
FloatGrid * grid()
this is the global 'LIF'-grid (light patterns) (currently 2x2m)
Definition: model.h:124
void clear()
free ressources
Definition: model.cpp:416
void beforeRun()
initializations
Definition: model.cpp:689
const HeightGridValue & heightGridValue(const float *lif_ptr) const
Definition: model.h:130
void loadProject()
setup and load a project
Definition: model.cpp:479
ABE::ForestManagementEngine * ABEngine() const
Definition: model.h:110
Environment * environment() const
Definition: model.h:113
const HeightGridValue heightGridValue(const int ix, const int iy) const
get the value of the (10m) Height grid at the position index ix and iy (of the LIF grid)
Definition: model.h:129
double totalStockableArea() const
total stockable area of the landscape (ha)
Definition: model.h:102
void runYear()
run a single year
Definition: model.cpp:778
ResourceUnit * ruById(int id) const
find the resource unit with Id 'id' or return NULL
Definition: model.cpp:619
TimeEvents * timeEvents() const
Definition: model.h:115
void setCurrentTask(QString what)
Definition: model.h:141
const DEM * dem() const
Definition: model.h:117
const QList< Climate * > climates() const
Definition: model.h:120
~Model()
Definition: model.cpp:132
Model()
Definition: model.cpp:122
SpeciesSet * speciesSet() const
Definition: model.h:119
Saplings * saplings() const
Definition: model.h:114
Modules * modules() const
Definition: model.h:116
void cleanTreeLists(bool recalculate_stats)
clean the tree data structures (remove harvested trees) - call after management operations.
Definition: model.cpp:1258
static const ModelSettings & settings()
access to global model settings.
Definition: model.h:136
const QList< ResourceUnit * > & ruList() const
ruList contains all simulated resource units (that are in the project area)
Definition: model.h:108
QString currentTask() const
Definition: model.h:140
void onlyApplyLightPattern()
Definition: model.h:138
const QRectF & extent() const
extent of the model (without buffer)
Definition: model.h:101
Definition: modelsettings.h:25
The Modules class is the container for iLand modules (e.g.
Definition: modules.h:35
ResourceUnit is the spatial unit that encapsulates a forest stand and links to several environmental ...
Definition: resourceunit.h:49
Definition: svdstate.h:49
The Saplings class the container for the establishment and sapling growth in iLand.
Definition: saplings.h:192
A SpeciesSet acts as a container for individual Species objects.
Definition: speciesset.h:30
Encapsulates the invokation of multiple threads for paralellized tasks.
Definition: threadrunner.h:27
bool multithreading() const
Definition: threadrunner.h:35
void run(void(*funcptr)(ResourceUnit *), const bool forceSingleThreaded=false) const
execute 'funcptr' for all resource units in parallel
Definition: threadrunner.cpp:60
Definition: timeevents.h:25
A tree is the basic simulation entity of iLand and represents a single tree.
Definition: tree.h:44
const int cPxPerHeight
Definition: globalsettings.h:9
bool isBitSet(const unsigned int value, const int bit)
Definition: global.h:97
void setBit(unsigned int &rTarget, const int bit, const bool value)
Definition: global.h:90
Grid< HeightGridValue > HeightGrid
Definition: model.h:87
Definition: abegrid.h:22
Definition: biteagent.cpp:32
Definition: model.h:51
float height
dominant tree height (m)
Definition: model.h:52
bool isRadiating() const
Definition: model.h:64
void setValid(const bool valid)
set bit to 1: pixel is not valid
Definition: model.h:60
void clearStemHeight()
reset the stem height
Definition: model.h:68
float stemHeight() const
get the (coarse, 1m classes) local height (only trees with the stem on the cell are counted here)
Definition: model.h:66
void setForestOutside(const bool is_outside)
Definition: model.h:61
bool isValid() const
a value of 1: not valid (returns false).
Definition: model.h:59
void increaseCount()
increase the number of trees on pixel
Definition: model.h:54
bool isForestOutside() const
true if a pixel is outside of the project area but considered as forested
Definition: model.h:62
void init(const float aheight, const int acount)
set values for height and count (this overwrites all the flags!)
Definition: model.h:76
void setIsRadiating()
bit 18: if set, the pixel is actively radiating influence on the LIF (such pixels are on the edge of ...
Definition: model.h:63
void setStemHeight(float h)
set the height of the tree local (i.e. the stem of the tree is on the pixel); compare to the 'height'...
Definition: model.h:70
int count() const
get count of trees on pixel
Definition: model.h:53
void resetCount()
set the count to 0
Definition: model.h:55