Subversion Repositories public iLand

Rev

Rev 240 | Rev 251 | 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
111 Werner 56
    }
57
}
58
 
200 werner 59
ResourceUnitSpecies &ResourceUnit::resourceUnitSpecies(const Species *species)
111 Werner 60
{
61
    return mRUSpecies[species->index()];
62
}
63
 
189 iland 64
Tree &ResourceUnit::newTree()
105 Werner 65
{
66
    // start simple: just append to the vector...
67
    mTrees.append(Tree());
68
    return mTrees.back();
69
}
107 Werner 70
 
157 werner 71
/// remove dead trees from tree list
72
/// reduce size of vector if lots of space is free
73
/// tests showed that this way of cleanup is very fast,
74
/// because no memory allocations are performed (simple memmove())
75
/// when trees are moved.
189 iland 76
void ResourceUnit::cleanTreeList()
157 werner 77
{
78
    QVector<Tree>::iterator last=mTrees.end()-1;
79
    QVector<Tree>::iterator current = mTrees.begin();
158 werner 80
    while (last>=current && (*last).isDead())
157 werner 81
        --last;
107 Werner 82
 
157 werner 83
    while (current<last) {
158 werner 84
        if ((*current).isDead()) {
157 werner 85
            *current = *last; // copy data!
86
            --last; //
158 werner 87
            while (last>=current && (*last).isDead())
157 werner 88
                --last;
89
        }
90
        ++current;
91
    }
92
    ++last; // last points now to the first dead tree
93
 
94
    // free ressources
95
    mTrees.erase(last, mTrees.end());
96
    if (mTrees.capacity()>100) {
97
        if (mTrees.count() / double(mTrees.capacity()) < 0.2) {
98
            int target_size = mTrees.count()*2;
99
            qDebug() << "reduce size from "<<mTrees.capacity() << "to" << target_size;
100
            mTrees.reserve(qMax(target_size, 100));
101
        }
102
    }
103
}
104
 
189 iland 105
void ResourceUnit::newYear()
107 Werner 106
{
107
    mAggregatedWLA = 0.f;
110 Werner 108
    mAggregatedLA = 0.f;
151 iland 109
    mPixelCount = mStockedPixelCount = 0;
111 Werner 110
    // clear statistics global and per species...
107 Werner 111
}
110 Werner 112
 
240 werner 113
 
114
 
112 Werner 115
/** production() is the "stand-level" part of the biomass production (3PG).
116
    - The amount of radiation intercepted by the stand is calculated
117
    - The 3PG production for each species and ressource unit is invoked  */
189 iland 118
void ResourceUnit::production()
110 Werner 119
{
241 werner 120
 
151 iland 121
    if (mAggregatedWLA==0 || mPixelCount==0) {
112 Werner 122
        // nothing to do...
241 werner 123
        mStatistics.clear();
112 Werner 124
        return;
125
    }
151 iland 126
 
127
    // the pixel counters are filled during the height-grid-calculations
230 werner 128
    mStockedArea = 100. * mStockedPixelCount; // m2 (1 height grid pixel = 10x10m)
129
 
112 Werner 130
    // calculate the leaf area index (LAI)
151 iland 131
    double LAI = mAggregatedLA / mStockedArea;
112 Werner 132
    // calculate the intercepted radiation fraction using the law of Beer Lambert
200 werner 133
    const double k = Model::settings().lightExtinctionCoefficient;
112 Werner 134
    double interception_fraction = 1. - exp(-k * LAI);
135
 
230 werner 136
    // calculate the total weighted leaf area on this RU:
137
    mEffectiveArea_perWLA = interception_fraction *  mStockedArea / mAggregatedWLA;
205 werner 138
 
230 werner 139
    DBGMODE(qDebug() << QString("production: LAI: %1 (intercepted fraction: %2, stocked area: %4). Effective Area / wla: %3")
140
            .arg(LAI)
141
            .arg(interception_fraction)
142
            .arg(mEffectiveArea_perWLA)
143
            .arg(mStockedArea);
144
    );
241 werner 145
    // soil water model - this determines soil water contents needed for response calculations
146
    {
147
    DebugTimer tw("water:run");
148
    mWater->run();
149
    }
112 Werner 150
 
151
    // invoke species specific calculation (3PG)
229 werner 152
    //QVector<ResourceUnitSpecies>::iterator i;
153
    ResourceUnitSpecies *i;
189 iland 154
    QVector<ResourceUnitSpecies>::iterator iend = mRUSpecies.end();
241 werner 155
    mStatistics.clear();
112 Werner 156
    for (i=mRUSpecies.begin(); i!=iend; ++i) {
229 werner 157
        i->calculate();
158
        i->statistics().clear();
231 werner 159
        qDebug() << "species" << (*i).species()->id() << "raw_gpp_m2" << i->prod3PG().GPPperArea();
112 Werner 160
    }
110 Werner 161
}
162
 
189 iland 163
void ResourceUnit::yearEnd()
180 werner 164
{
165
    // calculate statistics for all tree species of the ressource unit
166
    int c = mRUSpecies.count();
167
    for (int i=0;i<c; i++) {
168
        mRUSpecies[i].statistics().calculate();
169
        mStatistics.add(mRUSpecies[i].statistics());
170
    }
171
    mStatistics.calculate(); // aggreagte on stand level
172
}
173
 
241 werner 174
/// refresh of tree based statistics.
240 werner 175
void ResourceUnit::createStandStatistics()
176
{
241 werner 177
    // clear statistics (ru-level and ru-species level)
240 werner 178
    mStatistics.clear();
179
    for (int i=0;i<mRUSpecies.count();i++)
180
        mRUSpecies[i].statistics().clear();
241 werner 181
 
182
    // add all trees to the statistics objects of the species
240 werner 183
    foreach(const Tree &t, mTrees) {
184
        if (!t.isDead())
185
            resourceUnitSpecies(t.species()).statistics().add(&t);
186
    }
241 werner 187
    // summarize statistics for the whole resource unit
240 werner 188
    for (int i=0;i<mRUSpecies.count();i++) {
189
        mRUSpecies[i].statistics().calculate();
190
        mStatistics.add(mRUSpecies[i].statistics());
191
    }
192
}