Subversion Repositories public iLand

Rev

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