Subversion Repositories public iLand

Rev

Rev 1113 | Rev 1118 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
1111 werner 2
#include "global.h"
3
#include "saplings.h"
4
 
5
#include "globalsettings.h"
6
#include "model.h"
7
#include "resourceunit.h"
8
#include "resourceunitspecies.h"
9
#include "establishment.h"
10
#include "species.h"
11
#include "seeddispersal.h"
12
 
1113 werner 13
double Saplings::mRecruitmentVariation = 0.1; // +/- 10%
14
double Saplings::mBrowsingPressure = 0.;
1111 werner 15
 
1113 werner 16
 
1111 werner 17
Saplings::Saplings()
18
{
19
 
20
}
21
 
22
void Saplings::setup()
23
{
24
    mGrid.setup(GlobalSettings::instance()->model()->grid()->metricRect(), GlobalSettings::instance()->model()->grid()->cellsize());
25
 
26
    // mask out out-of-project areas
27
    HeightGrid *hg = GlobalSettings::instance()->model()->heightGrid();
28
    for (int i=0;i<mGrid.count();++i) {
29
        if (!hg->valueAtIndex(mGrid.index5(i)).isValid())
30
            mGrid[i].state = SaplingCell::CellInvalid;
31
        else
32
            mGrid[i].state = SaplingCell::CellFree;
33
    }
34
 
35
 
36
}
37
 
38
void Saplings::establishment(const ResourceUnit *ru)
39
{
40
    Grid<float> &seedmap =  const_cast<Grid<float>& > (ru->ruSpecies().first()->species()->seedDispersal()->seedMap() );
41
    HeightGrid *height_grid = GlobalSettings::instance()->model()->heightGrid();
42
    FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid();
43
 
44
    QPoint iseedmap =  seedmap.indexAt(ru->boundingBox().topLeft()) ;
45
    QPoint imap =  mGrid.indexAt(ru->boundingBox().topLeft());
46
 
47
    int species_idx = irandom(0, ru->ruSpecies().size()-1);
48
    for (int s_idx = 0; s_idx<ru->ruSpecies().size(); ++s_idx) {
49
 
50
        // start from a random species (and cycle through the available species)
51
        species_idx = ++species_idx % ru->ruSpecies().size();
52
 
53
        ResourceUnitSpecies *rus = ru->ruSpecies()[species_idx];
54
        // check if there are seeds of the given species on the resource unit
55
        float seeds = 0.f;
56
        for (int iy=0;iy<5;++iy) {
57
            float *p = seedmap.ptr(iseedmap.x(), iseedmap.y());
58
            for (int ix=0;ix<5;++ix)
59
                seeds += *p++;
60
        }
61
        // if there are no seeds: no need to do more
62
        if (seeds==0.f)
63
            continue;
64
 
65
        // calculate the abiotic environment (TACA)
66
        rus->establishment().calculateAbioticEnvironment();
67
        double abiotic_env = rus->establishment().abioticEnvironment();
68
        if (abiotic_env==0.)
69
            continue;
70
 
71
        // loop over all 2m cells on this resource unit
72
        SaplingCell *s;
73
        int isc = 0; // index on 2m cell
74
        for (int iy=0; iy<cPxPerRU; ++iy) {
75
            s = mGrid.ptr(imap.x(), imap.y()+iy); // ptr to the row
76
            isc = mGrid.index(imap.x(), imap.y()+iy);
77
 
78
            for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc, ++mTested) {
79
                if (s->state == SaplingCell::CellFree) {
80
                    bool viable = true;
81
                    // is a sapling of the current species already on the pixel?
82
                    // * test for sapling height already in cell state
83
                    // * test for grass-cover already in cell state
84
                    int i_occupied = -1;
85
                    for (int i=0;i<NSAPCELLS;++i) {
86
                        if (!s->saplings[i].is_occupied() && i_occupied<0)
87
                            i_occupied=i;
88
                        if (s->saplings[i].species_index == species_idx) {
89
                            viable = false;
90
                        }
91
                    }
92
 
1112 werner 93
                    if (viable && i_occupied>=0) {
1111 werner 94
                        // grass cover?
95
                        DBG_IF(i_occupied<0, "establishment", "invalid value i_occupied<0");
96
                        float seed_map_value = seedmap[seedmap.index10(isc)];
97
                        if (seed_map_value==0.f)
98
                            continue;
99
                        const HeightGridValue &hgv = (*height_grid)[height_grid->index5(isc)];
100
                        float lif_value = (*lif_grid)[isc];
101
                        double lif_corrected = rus->species()->speciesSet()->LRIcorrection(lif_value, 4. / hgv.height);
102
                        // check for the combination of seed availability and light on the forest floor
103
                         if (drandom() < seed_map_value*lif_corrected*abiotic_env ) {
104
                             // ok, lets add a sapling at the given position
105
                             s->saplings[i_occupied].setSapling(0.05f, 1, species_idx);
106
                             s->checkState();
107
                             mAdded++;
108
 
109
                         }
110
 
111
                    }
112
 
113
                }
114
            }
115
        }
116
 
117
    }
118
 
119
}
1113 werner 120
 
121
void Saplings::saplingGrowth(const ResourceUnit *ru)
122
{
123
    HeightGrid *height_grid = GlobalSettings::instance()->model()->heightGrid();
124
    FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid();
125
 
1115 werner 126
    for (QList<ResourceUnitSpecies*>::const_iterator i=ru->ruSpecies().constBegin(); i!=ru->ruSpecies().constEnd(); ++i)
127
        (*i)->saplingStat().clearStatistics();
128
 
1113 werner 129
    QPoint imap =  mGrid.indexAt(ru->boundingBox().topLeft());
1115 werner 130
    bool need_check=false;
1113 werner 131
    for (int iy=0; iy<cPxPerRU; ++iy) {
132
        SaplingCell *s = mGrid.ptr(imap.x(), imap.y()+iy); // ptr to the row
133
        int isc = mGrid.index(imap.x(), imap.y()+iy);
134
 
135
        for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) {
136
            if (s->state != SaplingCell::CellInvalid) {
1115 werner 137
                need_check=false;
1113 werner 138
                for (int i=0;i<NSAPCELLS;++i) {
139
                    if (s->saplings[i].is_occupied()) {
140
                        // growth of this sapling tree
141
                        const HeightGridValue &hgv = (*height_grid)[height_grid->index5(isc)];
142
                        float lif_value = (*lif_grid)[isc];
143
 
1115 werner 144
                        need_check |= growSapling(ru, s->saplings[i], isc, hgv.height, lif_value);
1113 werner 145
                    }
146
                }
1115 werner 147
                if (need_check)
148
                    s->checkState();
1113 werner 149
            }
150
        }
151
    }
152
 
153
}
154
 
155
void Saplings::updateBrowsingPressure()
156
{
157
    if (GlobalSettings::instance()->settings().valueBool("model.settings.browsing.enabled"))
158
        Saplings::mBrowsingPressure = GlobalSettings::instance()->settings().valueDouble("model.settings.browsing.browsingPressure");
159
    else
160
        Saplings::mBrowsingPressure = 0.;
161
}
162
 
1115 werner 163
bool Saplings::growSapling(const ResourceUnit *ru, SaplingTree &tree, int isc, float dom_height, float lif_value)
1113 werner 164
{
165
    ResourceUnitSpecies *rus = const_cast<ResourceUnitSpecies*>(ru->ruSpecies()[tree.species_index]);
166
    const Species *species = rus->species();
167
 
168
    // (1) calculate height growth potential for the tree (uses linerization of expressions...)
169
    double h_pot = species->saplingGrowthParameters().heightGrowthPotential.calculate(tree.height);
170
    double delta_h_pot = h_pot - tree.height;
171
 
172
    // (2) reduce height growth potential with species growth response f_env_yr and with light state (i.e. LIF-value) of home-pixel.
173
    if (dom_height==0.f)
174
        throw IException(QString("growSapling: height grid at %1/%2 has value 0").arg(isc));
175
 
176
    double rel_height = tree.height / dom_height;
177
 
178
    double lif_corrected = species->speciesSet()->LRIcorrection(lif_value, rel_height); // correction based on height
179
 
180
    double lr = species->lightResponse(lif_corrected); // species specific light response (LUI, light utilization index)
181
 
182
    double delta_h_factor = rus->prod3PG().fEnvYear() * lr; // relative growth
183
 
184
    if (h_pot<0. || delta_h_pot<0. || lif_corrected<0. || lif_corrected>1. || delta_h_factor<0. || delta_h_factor>1. )
185
        qDebug() << "invalid values in Sapling::growSapling";
186
 
187
    // check browsing
188
    if (mBrowsingPressure>0. && tree.height<=2.f) {
189
        double p = rus->species()->saplingGrowthParameters().browsingProbability;
190
        // calculate modifed annual browsing probability via odds-ratios
191
        // odds = p/(1-p) -> odds_mod = odds * browsingPressure -> p_mod = odds_mod /( 1 + odds_mod) === p*pressure/(1-p+p*pressure)
192
        double p_browse = p*mBrowsingPressure / (1. - p + p*mBrowsingPressure);
193
        if (drandom() < p_browse) {
194
            delta_h_factor = 0.;
195
        }
196
    }
197
 
198
    // check mortality of saplings
199
    if (delta_h_factor < species->saplingGrowthParameters().stressThreshold) {
200
        tree.stress_years++;
201
        if (tree.stress_years > species->saplingGrowthParameters().maxStressYears) {
202
            // sapling dies...
203
            tree.clear();
1115 werner 204
            rus->saplingStat().addCarbonOfDeadSapling( tree.height / species->saplingGrowthParameters().hdSapling * 100.f );
205
            return true; // need cleanup
1113 werner 206
        }
207
    } else {
208
        tree.stress_years=0; // reset stress counter
209
    }
210
    DBG_IF(delta_h_pot*delta_h_factor < 0.f || delta_h_pot*delta_h_factor > 2., "Sapling::growSapling", "inplausible height growth.");
211
 
212
    // grow
213
    tree.height += delta_h_pot * delta_h_factor;
214
    tree.age++; // increase age of sapling by 1
215
 
216
    // recruitment?
217
    if (tree.height > 4.f) {
218
        rus->saplingStat().mRecruited++;
219
 
220
        float dbh = tree.height / species->saplingGrowthParameters().hdSapling * 100.f;
221
        // the number of trees to create (result is in trees per pixel)
222
        double n_trees = species->saplingGrowthParameters().representedStemNumber(dbh);
223
        int to_establish = static_cast<int>( n_trees );
224
 
225
        // if n_trees is not an integer, choose randomly if we should add a tree.
226
        // e.g.: n_trees = 2.3 -> add 2 trees with 70% probability, and add 3 trees with p=30%.
227
        if (drandom() < (n_trees-to_establish) || to_establish==0)
228
            to_establish++;
229
 
230
        // add a new tree
231
        for (int i=0;i<to_establish;i++) {
232
            Tree &bigtree = const_cast<ResourceUnit*>(ru)->newTree();
233
 
234
            bigtree.setPosition(mGrid.indexOf(isc));
235
            // add variation: add +/-10% to dbh and *independently* to height.
236
            bigtree.setDbh(dbh * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation));
237
            bigtree.setHeight(tree.height * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation));
238
            bigtree.setSpecies( const_cast<Species*>(species) );
239
            bigtree.setAge(tree.age,tree.height);
240
            bigtree.setRU(const_cast<ResourceUnit*>(ru));
241
            bigtree.setup();
242
            const Tree *t = &bigtree;
243
            const_cast<ResourceUnitSpecies*>(rus)->statistics().add(t, 0); // count the newly created trees already in the stats
244
        }
245
        // clear all regeneration from this pixel (including this tree)
246
        tree.clear(); // clear this tree (no carbon flow to the ground)
247
        SaplingCell &s=mGrid[isc];
248
        for (int i=0;i<NSAPCELLS;++i) {
249
            if (s.saplings[i].is_occupied()) {
250
                // add carbon to the ground
251
                rus->saplingStat().addCarbonOfDeadSapling( s.saplings[i].height / species->saplingGrowthParameters().hdSapling * 100.f );
252
                s.saplings[i].clear();
253
            }
254
        }
1115 werner 255
        return true; // need cleanup
1113 werner 256
    }
257
    // book keeping (only for survivors) for the sapling of the resource unit / species
258
    SaplingStat &ss = rus->saplingStat();
259
    ss.mLiving++;
260
    ss.mAvgHeight+=tree.height;
261
    ss.mAvgAge+=tree.age;
262
    ss.mAvgDeltaHPot+=delta_h_pot;
263
    ss.mAvgHRealized += delta_h_pot * delta_h_factor;
1115 werner 264
    return false;
1113 werner 265
}
266
 
267
void SaplingStat::clearStatistics()
268
{
269
    mRecruited=mDied=mLiving=0;
270
    mSumDbhDied=0.;
271
    mAvgHeight=0.;
272
    mAvgAge=0.;
273
    mAvgDeltaHPot=mAvgHRealized=0.;
274
 
275
}