Subversion Repositories public iLand

Rev

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