Subversion Repositories public iLand

Rev

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