iLand
biteagent.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 BITEAGENT_H
21#define BITEAGENT_H
22
23#include <QObject>
24#include <QJSValue>
25
26#include "bitecell.h"
27#include "grid.h"
28#include "biteitem.h"
29#include "bitecellscript.h"
30#include "bitewrapper.h"
31#include "biteclimate.h"
32
33namespace ABE {
34class FMTreeList; // forward
35}
36class ScriptGrid;
37
38namespace BITE {
39
45 int nActive;
47 double agentBiomass;
49 double m3Killed;
50 double totalImpact;
53};
54
55struct BACellStat {
58 int nKilled;
60 double m3Killed;
61 double totalImpact;
65
66};
67
68class BiteLifeCycle;
69
70class BiteAgent : public QObject
71{
72 Q_OBJECT
73 Q_PROPERTY(QString name READ name)
74 Q_PROPERTY(QString description READ description)
75 Q_PROPERTY(int cellSize READ cellSize)
76 Q_PROPERTY(int width READ width)
77 Q_PROPERTY(int height READ height)
78 Q_PROPERTY(bool verbose READ verbose WRITE setVerbose)
79 Q_PROPERTY(ScriptGrid* drawGrid READ drawGrid)
80 Q_PROPERTY(QStringList variables READ variables)
82
83public:
84 BiteAgent(QObject *parent = nullptr);
85 Q_INVOKABLE BiteAgent(QJSValue obj);
86 ~BiteAgent();
87
88 ScriptGrid *drawGrid() { Q_ASSERT(mDrawGrid != nullptr); return mDrawGrid; }
89 Grid<double> *baseDrawGrid() { return &mBaseDrawGrid; }
90
91
93 void setup(QJSValue obj);
95 static void setCPPOwnership(QObject *obj);
96 BiteWrapperCore *wrapper() { return &mWrapperCore; }
97 const BiteClimate &biteClimate() const { return mClimateProvider; }
98
100 const QVector<ResourceUnit*> &largeCellRUs(int cellindex) { return mRULookup[cellindex]; }
101 void setLargeCellRuList(int cellindex, QVector<ResourceUnit*> &list);
102
104 QString name() const {return mName; }
106 QString description() const {return mDesc; }
108 int cellSize() const { return mCellSize; }
109 int width() const {return grid().sizeX(); }
110 int height() const {return grid().sizeY(); }
111
112 // return a list of all currently available cell variables
113 QStringList variables();
114
115 bool verbose() const { return mVerbose; }
116 void setVerbose(bool v) { mVerbose = v; }
117
118 // property that controls the reasons when the onTreeRemoved event handler should be called
119 int onTreeRemovedFilter() const { return mOnTreeRemovedFilter; }
120 void setOnTreeRemovedFilter(int value);
121 void runOnTreeRemovedFilter(Tree *tree, int reason);
122
123 const Grid<BiteCell*> &grid() const { return mGrid; }
126 BAgentStats &stats() { return mStats; }
127 BiteLifeCycle *lifeCycle() const { return mLC; }
129 void createStatsGrid();
132
133
134public slots:
135 BiteCellScript *cell(int x, int y);
137 bool isCellValid(int x, int y) { return grid().isIndexValid(x,y); }
138 void run();
139 void run(BiteCellScript *cell);
141 BiteItem* item(QString name);
142 QString info();
143
144 double evaluate(BiteCellScript *cell, QString expr);
146 void addVariable(ScriptGrid *grid, QString var_name);
148 void addVariable(QString var_name);
150 void updateVariable(QString var_name, double value);
152 void updateVariable(QString var_name, QString expression);
154 void updateVariable(QString var_name, QJSValue func);
156 void updateDrawGrid(QString expression);
157 void updateDrawGrid(QJSValue func);
159 void saveGrid(QString expression, QString file_name);
160private:
161 static void runCell(BiteCell &cell);
162 static QHash<QThread*, ABE::FMTreeList* > mTreeLists;
163 static QHash<QThread*, ABE::FMSaplingList* > mSaplingLists;
164
165 void setupScripting();
166
167 BiteWrapperCore mWrapperCore;
168 BiteClimate mClimateProvider;
169
170 BAgentStats mStats;
171
172 // grids
173 void createBaseGrid();
174 Grid<BiteCell*> mGrid;
175 QVector<BiteCell> mCells;
176
177 // grids for stats
178 Grid<BACellStat> mStatsGrid;
179
180 // grid for drawing
181 Grid<double> mBaseDrawGrid;
182 ScriptGrid *mDrawGrid;
183
184 Events mEvents;
185 QJSValue mThis;
186 QJSValueList mTreeRemovedParams;
187
188 // elements (i.e. processes)
189 QVector<BiteItem*> mItems;
190
191 // structure for looking up resource units covered by a cell (for large cells)
192 QHash<int, QVector<ResourceUnit*> > mRULookup;
193
194 BiteLifeCycle *mLC;
195
196 QString mName;
197 QString mDesc;
198 int mCellSize;
199 bool mVerbose;
200 int mOnTreeRemovedFilter;
201
202 // JS helper
203 BiteCellScript mCell;
204 QJSValue mScriptCell;
205 QJSValue mTreeValue;
206 ScriptTree mTree;
207 QVector<Grid<double> *> mCreatedVarGrids;
208
209
210};
211
212} // end namespace
213
214#endif // BITEAGENT_H
Definition: fmsaplinglist.h:12
The FMTreeList class implements low-level functionality for selecting and harvesting of trees.
Definition: fmtreelist.h:34
Definition: biteagent.h:71
QString name() const
(short) name of the agent
Definition: biteagent.h:104
QString description
Definition: biteagent.h:74
BiteLifeCycle * lifeCycle() const
Definition: biteagent.h:127
BAgentStats & stats()
Definition: biteagent.h:126
int width
Definition: biteagent.h:76
int height() const
Definition: biteagent.h:110
int cellSize() const
cell size in meters
Definition: biteagent.h:108
QStringList variables
Definition: biteagent.h:80
QString description() const
(user defined) description of the agent
Definition: biteagent.h:106
void notifyItems(BiteCell *cell, BiteCell::ENotification what)
Definition: biteagent.cpp:145
int height
Definition: biteagent.h:77
void run()
Definition: biteagent.cpp:213
void addVariable(ScriptGrid *grid, QString var_name)
add an existing grid (e.g. from file) as an agent variable
Definition: biteagent.cpp:288
bool verbose() const
Definition: biteagent.h:115
void setup(QJSValue obj)
setup of the agent with a data structure (provided via JS)
Definition: biteagent.cpp:61
const Grid< BiteCell * > & grid() const
Definition: biteagent.h:123
int onTreeRemovedFilter
Definition: biteagent.h:81
BiteAgent(QObject *parent=nullptr)
Definition: biteagent.cpp:37
void setLargeCellRuList(int cellindex, QVector< ResourceUnit * > &list)
Definition: biteagent.cpp:154
void saveGrid(QString expression, QString file_name)
evaluate 'expression' for each cell, update the internal drawing grid, and save the content to 'file_...
Definition: biteagent.cpp:392
const BiteClimate & biteClimate() const
Definition: biteagent.h:97
BiteItem * item(QString name)
elements of the agent
Definition: biteagent.cpp:260
BiteCellScript * cell(int x, int y)
Definition: biteagent.cpp:469
static ABE::FMSaplingList * threadSaplingList()
Definition: biteagent.cpp:447
void runOnTreeRemovedFilter(Tree *tree, int reason)
Definition: biteagent.cpp:195
void updateVariable(QString var_name, double value)
set the value of var_name to value for all cells
Definition: biteagent.cpp:307
ScriptGrid * drawGrid
Definition: biteagent.h:79
static ABE::FMTreeList * threadTreeList()
Definition: biteagent.cpp:437
static void setCPPOwnership(QObject *obj)
helper function to set C++ ownership for 'obj'
Definition: biteagent.cpp:140
void createStatsGrid()
create stats grid (on demand)
Definition: biteagent.cpp:457
void setOnTreeRemovedFilter(int value)
Definition: biteagent.cpp:167
Grid< double > * baseDrawGrid()
Definition: biteagent.h:89
QString info()
Definition: biteagent.cpp:268
void updateDrawGrid(QString expression)
evaluate 'expression' for each cell and update the internal drawing grid
Definition: biteagent.cpp:361
QString name
Definition: biteagent.h:73
ScriptGrid * drawGrid()
Definition: biteagent.h:88
int onTreeRemovedFilter() const
Definition: biteagent.h:119
void setVerbose(bool v)
Definition: biteagent.h:116
int cellSize
Definition: biteagent.h:75
bool isCellValid(int x, int y)
returns true, if a valid cell is at x/y
Definition: biteagent.h:137
const QVector< ResourceUnit * > & largeCellRUs(int cellindex)
Definition: biteagent.h:100
double evaluate(BiteCellScript *cell, QString expr)
Definition: biteagent.cpp:280
QStringList variables()
~BiteAgent()
Definition: biteagent.cpp:50
BACellStat * cellStat(const BiteCell *cell)
return the cell statistics (if available) for the given 'cell'
Definition: biteagent.cpp:462
int width() const
Definition: biteagent.h:109
bool verbose
Definition: biteagent.h:78
BiteWrapperCore * wrapper()
Definition: biteagent.h:96
Definition: bitecell.h:38
ENotification
Definition: bitecell.h:71
Definition: bitecellscript.h:37
Definition: biteclimate.h:31
Definition: biteitem.h:37
Definition: bitelifecycle.h:28
Definition: bitewrapper.h:31
Definition: bitecellscript.h:88
Grid class (template).
Definition: grid.h:44
Definition: scriptgrid.h:29
Definition: scripttree.h:10
A tree is the basic simulation entity of iLand and represents a single tree.
Definition: tree.h:44
Definition: abegrid.h:22
Definition: biteagent.cpp:32
Definition: biteagent.h:55
int nHostSaplings
number of saplings (cohorts) that are potential hosts
Definition: biteagent.h:62
int saplingsImpact
number of saplings affected (e.g. by browsing) (<4m)
Definition: biteagent.h:64
double totalImpact
impact on tree compartments (depending on the mode)
Definition: biteagent.h:61
int nKilled
number of trees (>4m) killed
Definition: biteagent.h:58
BACellStat()
Definition: biteagent.h:56
int saplingsKilled
number of saplings (cohorts) killed (<4m)
Definition: biteagent.h:63
void clear()
Definition: biteagent.h:57
int nHostTrees
number of trees (>4m) that are potential host trees (hostFilter)
Definition: biteagent.h:59
double m3Killed
volume of all killed trees (>4m)
Definition: biteagent.h:60
Definition: biteagent.h:40
double m3Killed
volume of all killed trees (>4m)
Definition: biteagent.h:49
int nColonizable
number of cells that are tested for colonization
Definition: biteagent.h:44
int nDispersal
number of cells that are active source of dispersal
Definition: biteagent.h:43
int treesKilled
number of all killed trees (>4m)
Definition: biteagent.h:48
int saplingsKilled
number of saplings (cohorts) killed (<4m)
Definition: biteagent.h:51
int nNewlyColonized
number of cells that are colonized (successfully)
Definition: biteagent.h:46
int nActive
number of cells that are active at the end of the year
Definition: biteagent.h:45
int saplingsImpact
number of saplings affected (e.g. by browsing) (<4m)
Definition: biteagent.h:52
double agentBiomass
total agent biomass in all active cells
Definition: biteagent.h:47
BAgentStats()
Definition: biteagent.h:41
void clear()
Definition: biteagent.h:42
double totalImpact
impact on tree compartments (depending on the mode)
Definition: biteagent.h:50