Subversion Repositories public iLand

Rev

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