Subversion Repositories public iLand

Rev

Rev 475 | Rev 484 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
189 iland 2
/** @class ResourceUnit
3
  ResourceUnit is the spatial unit that encapsulates a forest stand and links to several environmental components
92 Werner 4
  (Climate, Soil, Water, ...).
5
 
6
  */
7
#include <QtCore>
8
#include "global.h"
9
 
189 iland 10
#include "resourceunit.h"
229 werner 11
#include "resourceunitspecies.h"
111 Werner 12
#include "speciesset.h"
13
#include "species.h"
113 Werner 14
#include "production3pg.h"
200 werner 15
#include "model.h"
208 werner 16
#include "climate.h"
241 werner 17
#include "watercycle.h"
18
#include "helper.h"
92 Werner 19
 
241 werner 20
ResourceUnit::~ResourceUnit()
21
{
22
    if (mWater)
23
        delete mWater;
24
}
111 Werner 25
 
189 iland 26
ResourceUnit::ResourceUnit(const int index)
92 Werner 27
{
455 werner 28
    qDeleteAll(mRUSpecies);
94 Werner 29
    mSpeciesSet = 0;
208 werner 30
    mClimate = 0;
331 werner 31
    mPixelCount=0;
453 werner 32
    mStockedArea = 0;
33
    mStockedPixelCount = 0;
113 Werner 34
    mIndex = index;
450 werner 35
    mSaplingHeightMap = 0;
482 werner 36
    mEffectiveArea_perWLA = 0.;
241 werner 37
    mWater = new WaterCycle();
38
 
157 werner 39
    mTrees.reserve(100); // start with space for 100 trees.
92 Werner 40
}
105 Werner 41
 
241 werner 42
void ResourceUnit::setup()
43
{
44
    mWater->setup(this);
281 werner 45
    // setup variables
46
    mUnitVariables.nitrogenAvailable = GlobalSettings::instance()->settings().valueDouble("model.site.availableNitrogen", 40);
376 werner 47
    mAverageAging = 0.;
281 werner 48
 
241 werner 49
}
451 werner 50
void ResourceUnit::setBoundingBox(const QRectF &bb)
51
{
52
    mBoundingBox = bb;
53
    mCornerCoord = GlobalSettings::instance()->model()->grid()->indexAt(bb.topLeft());
54
}
241 werner 55
 
111 Werner 56
/// set species and setup the species-per-RU-data
189 iland 57
void ResourceUnit::setSpeciesSet(SpeciesSet *set)
111 Werner 58
{
59
    mSpeciesSet = set;
455 werner 60
    qDeleteAll(mRUSpecies);
61
 
62
    //mRUSpecies.resize(set->count()); // ensure that the vector space is not relocated
111 Werner 63
    for (int i=0;i<set->count();i++) {
64
        Species *s = const_cast<Species*>(mSpeciesSet->species(i));
65
        if (!s)
189 iland 66
            throw IException("ResourceUnit::setSpeciesSet: invalid index!");
229 werner 67
 
455 werner 68
        ResourceUnitSpecies *rus = new ResourceUnitSpecies();
69
        mRUSpecies.push_back(rus);
70
        rus->setup(s, this);
229 werner 71
        /* be careful: setup() is called with a pointer somewhere to the content of the mRUSpecies container.
72
           If the container memory is relocated (QVector), the pointer gets invalid!!!
73
           Therefore, a resize() is called before the loop (no resize()-operations during the loop)! */
455 werner 74
        //mRUSpecies[i].setup(s,this); // setup this element
277 werner 75
 
111 Werner 76
    }
77
}
78
 
200 werner 79
ResourceUnitSpecies &ResourceUnit::resourceUnitSpecies(const Species *species)
111 Werner 80
{
455 werner 81
    return *mRUSpecies[species->index()];
111 Werner 82
}
83
 
189 iland 84
Tree &ResourceUnit::newTree()
105 Werner 85
{
86
    // start simple: just append to the vector...
87
    mTrees.append(Tree());
88
    return mTrees.back();
89
}
287 werner 90
int ResourceUnit::newTreeIndex()
91
{
92
    // start simple: just append to the vector...
93
    mTrees.append(Tree());
94
    return mTrees.count()-1;
95
}
107 Werner 96
 
157 werner 97
/// remove dead trees from tree list
98
/// reduce size of vector if lots of space is free
99
/// tests showed that this way of cleanup is very fast,
100
/// because no memory allocations are performed (simple memmove())
101
/// when trees are moved.
189 iland 102
void ResourceUnit::cleanTreeList()
157 werner 103
{
104
    QVector<Tree>::iterator last=mTrees.end()-1;
105
    QVector<Tree>::iterator current = mTrees.begin();
158 werner 106
    while (last>=current && (*last).isDead())
157 werner 107
        --last;
107 Werner 108
 
157 werner 109
    while (current<last) {
158 werner 110
        if ((*current).isDead()) {
157 werner 111
            *current = *last; // copy data!
112
            --last; //
158 werner 113
            while (last>=current && (*last).isDead())
157 werner 114
                --last;
115
        }
116
        ++current;
117
    }
118
    ++last; // last points now to the first dead tree
119
 
120
    // free ressources
278 werner 121
    if (last!=mTrees.end()) {
122
        mTrees.erase(last, mTrees.end());
123
        if (mTrees.capacity()>100) {
124
            if (mTrees.count() / double(mTrees.capacity()) < 0.2) {
125
                int target_size = mTrees.count()*2;
126
                qDebug() << "reduce size from "<<mTrees.capacity() << "to" << target_size;
127
                mTrees.reserve(qMax(target_size, 100));
128
            }
157 werner 129
        }
130
    }
131
}
132
 
189 iland 133
void ResourceUnit::newYear()
107 Werner 134
{
251 werner 135
    mAggregatedWLA = 0.;
136
    mAggregatedLA = 0.;
137
    mAggregatedLR = 0.;
138
    mEffectiveArea = 0.;
482 werner 139
    mAverageAging = 0.;
151 iland 140
    mPixelCount = mStockedPixelCount = 0;
111 Werner 141
    // clear statistics global and per species...
455 werner 142
    QList<ResourceUnitSpecies*>::const_iterator i;
143
    QList<ResourceUnitSpecies*>::const_iterator iend = mRUSpecies.constEnd();
278 werner 144
    mStatistics.clear();
455 werner 145
    for (i=mRUSpecies.constBegin(); i!=iend; ++i) {
146
        (*i)->statisticsDead().clear();
147
        (*i)->statisticsMgmt().clear();
475 werner 148
        (*i)->snagNewYear();
278 werner 149
    }
150
 
107 Werner 151
}
110 Werner 152
 
112 Werner 153
/** production() is the "stand-level" part of the biomass production (3PG).
154
    - The amount of radiation intercepted by the stand is calculated
331 werner 155
    - the water cycle is calculated
156
    - statistics for each species are cleared
157
    - The 3PG production for each species and ressource unit is called (calculates species-responses and NPP production)
298 werner 158
    see also: http://iland.boku.ac.at/individual+tree+light+availability */
189 iland 159
void ResourceUnit::production()
110 Werner 160
{
241 werner 161
 
151 iland 162
    if (mAggregatedWLA==0 || mPixelCount==0) {
112 Werner 163
        // nothing to do...
164
        return;
165
    }
151 iland 166
 
167
    // the pixel counters are filled during the height-grid-calculations
230 werner 168
    mStockedArea = 100. * mStockedPixelCount; // m2 (1 height grid pixel = 10x10m)
169
 
112 Werner 170
    // calculate the leaf area index (LAI)
151 iland 171
    double LAI = mAggregatedLA / mStockedArea;
112 Werner 172
    // calculate the intercepted radiation fraction using the law of Beer Lambert
200 werner 173
    const double k = Model::settings().lightExtinctionCoefficient;
112 Werner 174
    double interception_fraction = 1. - exp(-k * LAI);
251 werner 175
    mEffectiveArea = mStockedArea * interception_fraction; // m2
112 Werner 176
 
230 werner 177
    // calculate the total weighted leaf area on this RU:
251 werner 178
    mLRI_modification = interception_fraction *  mStockedArea / mAggregatedWLA;
265 werner 179
    if (mLRI_modification == 0.)
180
        qDebug() << "lri modifaction==0!";
205 werner 181
 
251 werner 182
 
183
    DBGMODE(qDebug() << QString("production: LAI: %1 (intercepted fraction: %2, stocked area: %4). LRI-Multiplier: %3")
230 werner 184
            .arg(LAI)
185
            .arg(interception_fraction)
251 werner 186
            .arg(mLRI_modification)
230 werner 187
            .arg(mStockedArea);
188
    );
367 werner 189
 
190
    // calculate LAI fractions
455 werner 191
    QList<ResourceUnitSpecies*>::const_iterator i;
192
    QList<ResourceUnitSpecies*>::const_iterator iend = mRUSpecies.constEnd();
193
    for (i=mRUSpecies.constBegin(); i!=iend; ++i) {
194
         (*i)->setLAIfactor((*i)->statistics().leafAreaIndex() / leafAreaIndex());
367 werner 195
    }
196
 
241 werner 197
    // soil water model - this determines soil water contents needed for response calculations
198
    {
199
    DebugTimer tw("water:run");
200
    mWater->run();
201
    }
112 Werner 202
 
203
    // invoke species specific calculation (3PG)
455 werner 204
    for (i=mRUSpecies.constBegin(); i!=iend; ++i) {
205
        (*i)->calculate();
206
        if (logLevelInfo() &&  (*i)->LAIfactor()>0)
207
            qDebug() << "ru" << mIndex << "species" << (*i)->species()->id() << "LAIfraction" << (*i)->LAIfactor() << "raw_gpp_m2"
208
                     << (*i)->prod3PG().GPPperArea() << "area:" << productiveArea() << "gpp:"
209
                     << productiveArea()*(*i)->prod3PG().GPPperArea()
210
                     << "aging(lastyear):" << averageAging() << "f_env,yr:" << (*i)->prod3PG().fEnvYear();
112 Werner 211
    }
110 Werner 212
}
213
 
251 werner 214
void ResourceUnit::calculateInterceptedArea()
215
{
265 werner 216
    if (mAggregatedLR==0) {
217
        mEffectiveArea_perWLA = 0.;
218
        return;
219
    }
251 werner 220
    Q_ASSERT(mAggregatedLR>0.);
221
    mEffectiveArea_perWLA = mEffectiveArea / mAggregatedLR;
431 werner 222
    if (logLevelDebug()) qDebug() << "RU: aggregated lightresponse:" << mAggregatedLR  << "eff.area./wla:" << mEffectiveArea_perWLA;
251 werner 223
}
224
 
376 werner 225
// function is called immediately before the growth of individuals
226
void ResourceUnit::beforeGrow()
227
{
228
    mAverageAging = 0.;
229
}
230
 
231
// function is called after finishing the indivdual growth / mortality.
232
void ResourceUnit::afterGrow()
233
{
234
    mAverageAging = leafArea()>0.?mAverageAging/leafArea():0; // calculate aging value (calls to addAverageAging() by individual trees)
235
    if (mAverageAging>0. && mAverageAging<0.00001)
236
        qDebug() << "ru" << mIndex << "aging <0.00001";
482 werner 237
    if (mAverageAging<0. || mAverageAging>1.)
238
        qDebug() << "Average aging invalid: (RU, LAI):" << index() << mStatistics.leafAreaIndex();
376 werner 239
}
240
 
189 iland 241
void ResourceUnit::yearEnd()
180 werner 242
{
243
    // calculate statistics for all tree species of the ressource unit
244
    int c = mRUSpecies.count();
245
    for (int i=0;i<c; i++) {
455 werner 246
        mRUSpecies[i]->statisticsDead().calculate(); // calculate the dead trees
247
        mRUSpecies[i]->statisticsMgmt().calculate(); // stats of removed trees
248
        mRUSpecies[i]->updateGWL(); // get sum of dead trees (died + removed)
249
        mRUSpecies[i]->statistics().calculate(); // calculate the living (and add removed volume to gwl)
250
        mStatistics.add(mRUSpecies[i]->statistics());
180 werner 251
    }
252
    mStatistics.calculate(); // aggreagte on stand level
482 werner 253
 
180 werner 254
}
255
 
482 werner 256
void ResourceUnit::addTreeAgingForAllTrees()
257
{
258
    mAverageAging = 0.;
259
    foreach(const Tree &t, mTrees) {
260
        addTreeAging(t.leafArea(), t.species()->aging(t.height(), t.age()));
261
    }
262
 
263
}
264
 
241 werner 265
/// refresh of tree based statistics.
482 werner 266
/// WARNING: this function is only called once (during startup).
267
/// see function "yearEnd()" above!!!
240 werner 268
void ResourceUnit::createStandStatistics()
269
{
241 werner 270
    // clear statistics (ru-level and ru-species level)
240 werner 271
    mStatistics.clear();
262 werner 272
    for (int i=0;i<mRUSpecies.count();i++) {
455 werner 273
        mRUSpecies[i]->statistics().clear();
274
        mRUSpecies[i]->statisticsDead().clear();
275
        mRUSpecies[i]->statisticsMgmt().clear();
262 werner 276
    }
241 werner 277
 
278
    // add all trees to the statistics objects of the species
240 werner 279
    foreach(const Tree &t, mTrees) {
280
        if (!t.isDead())
257 werner 281
            resourceUnitSpecies(t.species()).statistics().add(&t, 0);
240 werner 282
    }
241 werner 283
    // summarize statistics for the whole resource unit
240 werner 284
    for (int i=0;i<mRUSpecies.count();i++) {
455 werner 285
        mRUSpecies[i]->statistics().calculate();
286
        mStatistics.add(mRUSpecies[i]->statistics());
240 werner 287
    }
331 werner 288
    mStatistics.calculate();
376 werner 289
    mAverageAging = mStatistics.leafAreaIndex()>0.?mAverageAging / (mStatistics.leafAreaIndex()*area()):0.;
482 werner 290
    if (mAverageAging<0. || mAverageAging>1.)
291
        qDebug() << "Average aging invalid: (RU, LAI):" << index() << mStatistics.leafAreaIndex();
240 werner 292
}
452 werner 293
 
461 werner 294
void ResourceUnit::setMaxSaplingHeightAt(const QPoint &position, const float height)
452 werner 295
{
296
    Q_ASSERT(mSaplingHeightMap);
297
    int pixel_index = cPxPerRU*(position.x()-mCornerCoord.x())+(position.y()-mCornerCoord.y());
461 werner 298
    if (pixel_index<0 || pixel_index>=cPxPerRU*cPxPerRU) {
453 werner 299
        qDebug() << "setSaplingHeightAt-Error for position" << position << "for RU at" << boundingBox() << "with corner" << mCornerCoord;
461 werner 300
    } else {
301
        if (mSaplingHeightMap[pixel_index]<height)
302
            mSaplingHeightMap[pixel_index]=height;
303
    }
452 werner 304
}
305
 
454 werner 306
/// clear all saplings of all species on a given position (after recruitment)
307
void ResourceUnit::clearSaplings(const QPoint &position)
308
{
455 werner 309
    foreach(ResourceUnitSpecies* rus, mRUSpecies)
310
        rus->clearSaplings(position);
454 werner 311
 
312
}
313