Subversion Repositories public iLand

Rev

Rev 454 | Rev 461 | 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();
278 werner 146
    }
147
 
107 Werner 148
}
110 Werner 149
 
112 Werner 150
/** production() is the "stand-level" part of the biomass production (3PG).
151
    - The amount of radiation intercepted by the stand is calculated
331 werner 152
    - the water cycle is calculated
153
    - statistics for each species are cleared
154
    - The 3PG production for each species and ressource unit is called (calculates species-responses and NPP production)
298 werner 155
    see also: http://iland.boku.ac.at/individual+tree+light+availability */
189 iland 156
void ResourceUnit::production()
110 Werner 157
{
241 werner 158
 
151 iland 159
    if (mAggregatedWLA==0 || mPixelCount==0) {
112 Werner 160
        // nothing to do...
161
        return;
162
    }
151 iland 163
 
164
    // the pixel counters are filled during the height-grid-calculations
230 werner 165
    mStockedArea = 100. * mStockedPixelCount; // m2 (1 height grid pixel = 10x10m)
166
 
112 Werner 167
    // calculate the leaf area index (LAI)
151 iland 168
    double LAI = mAggregatedLA / mStockedArea;
112 Werner 169
    // calculate the intercepted radiation fraction using the law of Beer Lambert
200 werner 170
    const double k = Model::settings().lightExtinctionCoefficient;
112 Werner 171
    double interception_fraction = 1. - exp(-k * LAI);
251 werner 172
    mEffectiveArea = mStockedArea * interception_fraction; // m2
112 Werner 173
 
230 werner 174
    // calculate the total weighted leaf area on this RU:
251 werner 175
    mLRI_modification = interception_fraction *  mStockedArea / mAggregatedWLA;
265 werner 176
    if (mLRI_modification == 0.)
177
        qDebug() << "lri modifaction==0!";
205 werner 178
 
251 werner 179
 
180
    DBGMODE(qDebug() << QString("production: LAI: %1 (intercepted fraction: %2, stocked area: %4). LRI-Multiplier: %3")
230 werner 181
            .arg(LAI)
182
            .arg(interception_fraction)
251 werner 183
            .arg(mLRI_modification)
230 werner 184
            .arg(mStockedArea);
185
    );
367 werner 186
 
187
    // calculate LAI fractions
455 werner 188
    QList<ResourceUnitSpecies*>::const_iterator i;
189
    QList<ResourceUnitSpecies*>::const_iterator iend = mRUSpecies.constEnd();
190
    for (i=mRUSpecies.constBegin(); i!=iend; ++i) {
191
         (*i)->setLAIfactor((*i)->statistics().leafAreaIndex() / leafAreaIndex());
367 werner 192
    }
193
 
241 werner 194
    // soil water model - this determines soil water contents needed for response calculations
195
    {
196
    DebugTimer tw("water:run");
197
    mWater->run();
198
    }
112 Werner 199
 
200
    // invoke species specific calculation (3PG)
455 werner 201
    for (i=mRUSpecies.constBegin(); i!=iend; ++i) {
202
        (*i)->calculate();
203
        if (logLevelInfo() &&  (*i)->LAIfactor()>0)
204
            qDebug() << "ru" << mIndex << "species" << (*i)->species()->id() << "LAIfraction" << (*i)->LAIfactor() << "raw_gpp_m2"
205
                     << (*i)->prod3PG().GPPperArea() << "area:" << productiveArea() << "gpp:"
206
                     << productiveArea()*(*i)->prod3PG().GPPperArea()
207
                     << "aging(lastyear):" << averageAging() << "f_env,yr:" << (*i)->prod3PG().fEnvYear();
112 Werner 208
    }
110 Werner 209
}
210
 
251 werner 211
void ResourceUnit::calculateInterceptedArea()
212
{
265 werner 213
    if (mAggregatedLR==0) {
214
        mEffectiveArea_perWLA = 0.;
215
        return;
216
    }
251 werner 217
    Q_ASSERT(mAggregatedLR>0.);
218
    mEffectiveArea_perWLA = mEffectiveArea / mAggregatedLR;
431 werner 219
    if (logLevelDebug()) qDebug() << "RU: aggregated lightresponse:" << mAggregatedLR  << "eff.area./wla:" << mEffectiveArea_perWLA;
251 werner 220
}
221
 
376 werner 222
// function is called immediately before the growth of individuals
223
void ResourceUnit::beforeGrow()
224
{
225
    mAverageAging = 0.;
226
}
227
 
228
// function is called after finishing the indivdual growth / mortality.
229
void ResourceUnit::afterGrow()
230
{
231
    mAverageAging = leafArea()>0.?mAverageAging/leafArea():0; // calculate aging value (calls to addAverageAging() by individual trees)
232
    if (mAverageAging>0. && mAverageAging<0.00001)
233
        qDebug() << "ru" << mIndex << "aging <0.00001";
234
}
235
 
189 iland 236
void ResourceUnit::yearEnd()
180 werner 237
{
238
    // calculate statistics for all tree species of the ressource unit
239
    int c = mRUSpecies.count();
240
    for (int i=0;i<c; i++) {
455 werner 241
        mRUSpecies[i]->statisticsDead().calculate(); // calculate the dead trees
242
        mRUSpecies[i]->statisticsMgmt().calculate(); // stats of removed trees
243
        mRUSpecies[i]->updateGWL(); // get sum of dead trees (died + removed)
244
        mRUSpecies[i]->statistics().calculate(); // calculate the living (and add removed volume to gwl)
245
        mStatistics.add(mRUSpecies[i]->statistics());
180 werner 246
    }
247
    mStatistics.calculate(); // aggreagte on stand level
248
}
249
 
241 werner 250
/// refresh of tree based statistics.
240 werner 251
void ResourceUnit::createStandStatistics()
252
{
241 werner 253
    // clear statistics (ru-level and ru-species level)
240 werner 254
    mStatistics.clear();
262 werner 255
    for (int i=0;i<mRUSpecies.count();i++) {
455 werner 256
        mRUSpecies[i]->statistics().clear();
257
        mRUSpecies[i]->statisticsDead().clear();
258
        mRUSpecies[i]->statisticsMgmt().clear();
262 werner 259
    }
241 werner 260
 
261
    // add all trees to the statistics objects of the species
240 werner 262
    foreach(const Tree &t, mTrees) {
263
        if (!t.isDead())
257 werner 264
            resourceUnitSpecies(t.species()).statistics().add(&t, 0);
240 werner 265
    }
241 werner 266
    // summarize statistics for the whole resource unit
240 werner 267
    for (int i=0;i<mRUSpecies.count();i++) {
455 werner 268
        mRUSpecies[i]->statistics().calculate();
269
        mStatistics.add(mRUSpecies[i]->statistics());
240 werner 270
    }
331 werner 271
    mStatistics.calculate();
376 werner 272
    mAverageAging = mStatistics.leafAreaIndex()>0.?mAverageAging / (mStatistics.leafAreaIndex()*area()):0.;
240 werner 273
}
452 werner 274
 
275
void ResourceUnit::setSaplingHeightAt(const QPoint &position, const float height)
276
{
277
    Q_ASSERT(mSaplingHeightMap);
278
    int pixel_index = cPxPerRU*(position.x()-mCornerCoord.x())+(position.y()-mCornerCoord.y());
279
    if (pixel_index<0 || pixel_index>=cPxPerRU*cPxPerRU)
453 werner 280
        qDebug() << "setSaplingHeightAt-Error for position" << position << "for RU at" << boundingBox() << "with corner" << mCornerCoord;
452 werner 281
    else
282
        mSaplingHeightMap[pixel_index]=height;
283
}
284
 
454 werner 285
/// clear all saplings of all species on a given position (after recruitment)
286
void ResourceUnit::clearSaplings(const QPoint &position)
287
{
455 werner 288
    foreach(ResourceUnitSpecies* rus, mRUSpecies)
289
        rus->clearSaplings(position);
454 werner 290
 
291
}
292