Subversion Repositories public iLand

Rev

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