Subversion Repositories public iLand

Rev

Rev 1118 | Rev 1159 | 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
    HeightGrid *height_grid = GlobalSettings::instance()->model()->heightGrid();
41
    FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid();
42
 
1118 werner 43
    QPoint imap = ru->cornerPointOffset(); // offset on LIF/saplings grid
44
    QPoint iseedmap = QPoint(imap.x()/10, imap.y()/10); // seed-map has 20m resolution, LIF 2m -> factor 10
1111 werner 45
 
1158 werner 46
    for (QList<ResourceUnitSpecies*>::const_iterator i=ru->ruSpecies().constBegin(); i!=ru->ruSpecies().constEnd(); ++i)
47
        (*i)->saplingStat().clearStatistics();
48
 
49
    double lif_corr[cPxPerHectare];
50
    for (int i=0;i<cPxPerHectare;++i)
51
        lif_corr[i]=-1.;
52
 
1111 werner 53
    int species_idx = irandom(0, ru->ruSpecies().size()-1);
54
    for (int s_idx = 0; s_idx<ru->ruSpecies().size(); ++s_idx) {
55
 
56
        // start from a random species (and cycle through the available species)
57
        species_idx = ++species_idx % ru->ruSpecies().size();
58
 
59
        ResourceUnitSpecies *rus = ru->ruSpecies()[species_idx];
60
        // check if there are seeds of the given species on the resource unit
61
        float seeds = 0.f;
1118 werner 62
        Grid<float> &seedmap =  const_cast<Grid<float>& >(rus->species()->seedDispersal()->seedMap());
1111 werner 63
        for (int iy=0;iy<5;++iy) {
64
            float *p = seedmap.ptr(iseedmap.x(), iseedmap.y());
65
            for (int ix=0;ix<5;++ix)
66
                seeds += *p++;
67
        }
68
        // if there are no seeds: no need to do more
69
        if (seeds==0.f)
70
            continue;
71
 
72
        // calculate the abiotic environment (TACA)
73
        rus->establishment().calculateAbioticEnvironment();
74
        double abiotic_env = rus->establishment().abioticEnvironment();
75
        if (abiotic_env==0.)
76
            continue;
77
 
78
        // loop over all 2m cells on this resource unit
79
        SaplingCell *s;
80
        int isc = 0; // index on 2m cell
81
        for (int iy=0; iy<cPxPerRU; ++iy) {
82
            s = mGrid.ptr(imap.x(), imap.y()+iy); // ptr to the row
83
            isc = mGrid.index(imap.x(), imap.y()+iy);
84
 
1158 werner 85
            for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) {
1111 werner 86
                if (s->state == SaplingCell::CellFree) {
87
                    // is a sapling of the current species already on the pixel?
88
                    // * test for sapling height already in cell state
89
                    // * test for grass-cover already in cell state
1158 werner 90
                    SaplingTree *stree=0;
91
                    SaplingTree *slot=s->saplings;
92
                    for (int i=0;i<NSAPCELLS;++i, ++slot) {
93
                        if (!stree && !slot->is_occupied())
94
                            stree=slot;
95
                        if (slot->species_index == species_idx) {
96
                            stree=0;
97
                            break;
1111 werner 98
                        }
99
                    }
100
 
1158 werner 101
                    if (stree) {
1111 werner 102
                        // grass cover?
1118 werner 103
                        float seed_map_value = seedmap[mGrid.index10(isc)];
1111 werner 104
                        if (seed_map_value==0.f)
105
                            continue;
1118 werner 106
                        const HeightGridValue &hgv = (*height_grid)[mGrid.index5(isc)];
1111 werner 107
                        float lif_value = (*lif_grid)[isc];
1158 werner 108
 
109
                        double &lif_corrected = lif_corr[iy*cPxPerRU+ix];
110
                        // calculate the LIFcorrected only once per pixel
111
                        if (lif_corrected<0.)
112
                            lif_corrected = rus->species()->speciesSet()->LRIcorrection(lif_value, 4. / hgv.height);
113
 
1111 werner 114
                        // check for the combination of seed availability and light on the forest floor
1158 werner 115
                        if (drandom() < seed_map_value*lif_corrected*abiotic_env ) {
116
                            // ok, lets add a sapling at the given position (age is incremented later)
117
                            stree->setSapling(0.05f, 0, species_idx);
118
                            s->checkState();
119
                            rus->saplingStat().mAdded++;
1111 werner 120
 
1158 werner 121
                        }
1111 werner 122
 
123
                    }
124
 
125
                }
126
            }
127
        }
128
 
129
    }
130
 
131
}
1113 werner 132
 
133
void Saplings::saplingGrowth(const ResourceUnit *ru)
134
{
135
    HeightGrid *height_grid = GlobalSettings::instance()->model()->heightGrid();
136
    FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid();
137
 
138
    QPoint imap =  mGrid.indexAt(ru->boundingBox().topLeft());
1115 werner 139
    bool need_check=false;
1113 werner 140
    for (int iy=0; iy<cPxPerRU; ++iy) {
141
        SaplingCell *s = mGrid.ptr(imap.x(), imap.y()+iy); // ptr to the row
142
        int isc = mGrid.index(imap.x(), imap.y()+iy);
143
 
144
        for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) {
145
            if (s->state != SaplingCell::CellInvalid) {
1115 werner 146
                need_check=false;
1113 werner 147
                for (int i=0;i<NSAPCELLS;++i) {
148
                    if (s->saplings[i].is_occupied()) {
149
                        // growth of this sapling tree
150
                        const HeightGridValue &hgv = (*height_grid)[height_grid->index5(isc)];
151
                        float lif_value = (*lif_grid)[isc];
152
 
1115 werner 153
                        need_check |= growSapling(ru, s->saplings[i], isc, hgv.height, lif_value);
1113 werner 154
                    }
155
                }
1115 werner 156
                if (need_check)
157
                    s->checkState();
1113 werner 158
            }
159
        }
160
    }
161
 
1158 werner 162
 
163
 
164
 
165
    // store statistics on saplings/regeneration
166
    for (QList<ResourceUnitSpecies*>::const_iterator i=ru->ruSpecies().constBegin(); i!=ru->ruSpecies().constEnd(); ++i) {
167
        (*i)->saplingStat().calculate((*i)->species(), const_cast<ResourceUnit*>(ru));
168
        (*i)->statistics().add(&((*i)->saplingStat()));
169
    }
1113 werner 170
}
171
 
172
void Saplings::updateBrowsingPressure()
173
{
174
    if (GlobalSettings::instance()->settings().valueBool("model.settings.browsing.enabled"))
175
        Saplings::mBrowsingPressure = GlobalSettings::instance()->settings().valueDouble("model.settings.browsing.browsingPressure");
176
    else
177
        Saplings::mBrowsingPressure = 0.;
178
}
179
 
1115 werner 180
bool Saplings::growSapling(const ResourceUnit *ru, SaplingTree &tree, int isc, float dom_height, float lif_value)
1113 werner 181
{
182
    ResourceUnitSpecies *rus = const_cast<ResourceUnitSpecies*>(ru->ruSpecies()[tree.species_index]);
183
    const Species *species = rus->species();
184
 
185
    // (1) calculate height growth potential for the tree (uses linerization of expressions...)
186
    double h_pot = species->saplingGrowthParameters().heightGrowthPotential.calculate(tree.height);
187
    double delta_h_pot = h_pot - tree.height;
188
 
189
    // (2) reduce height growth potential with species growth response f_env_yr and with light state (i.e. LIF-value) of home-pixel.
190
    if (dom_height==0.f)
191
        throw IException(QString("growSapling: height grid at %1/%2 has value 0").arg(isc));
192
 
193
    double rel_height = tree.height / dom_height;
194
 
195
    double lif_corrected = species->speciesSet()->LRIcorrection(lif_value, rel_height); // correction based on height
196
 
197
    double lr = species->lightResponse(lif_corrected); // species specific light response (LUI, light utilization index)
198
 
1118 werner 199
    rus->calculate(true); // calculate the 3pg module (this is done only if that did not happen up to now); true: call comes from regeneration
200
    double f_env_yr = rus->prod3PG().fEnvYear();
1113 werner 201
 
1118 werner 202
    double delta_h_factor = f_env_yr * lr; // relative growth
203
 
1113 werner 204
    if (h_pot<0. || delta_h_pot<0. || lif_corrected<0. || lif_corrected>1. || delta_h_factor<0. || delta_h_factor>1. )
205
        qDebug() << "invalid values in Sapling::growSapling";
206
 
207
    // check browsing
208
    if (mBrowsingPressure>0. && tree.height<=2.f) {
209
        double p = rus->species()->saplingGrowthParameters().browsingProbability;
210
        // calculate modifed annual browsing probability via odds-ratios
211
        // odds = p/(1-p) -> odds_mod = odds * browsingPressure -> p_mod = odds_mod /( 1 + odds_mod) === p*pressure/(1-p+p*pressure)
212
        double p_browse = p*mBrowsingPressure / (1. - p + p*mBrowsingPressure);
213
        if (drandom() < p_browse) {
214
            delta_h_factor = 0.;
215
        }
216
    }
217
 
218
    // check mortality of saplings
219
    if (delta_h_factor < species->saplingGrowthParameters().stressThreshold) {
220
        tree.stress_years++;
221
        if (tree.stress_years > species->saplingGrowthParameters().maxStressYears) {
222
            // sapling dies...
223
            tree.clear();
1115 werner 224
            rus->saplingStat().addCarbonOfDeadSapling( tree.height / species->saplingGrowthParameters().hdSapling * 100.f );
1158 werner 225
            rus->saplingStat().mDied++;
1115 werner 226
            return true; // need cleanup
1113 werner 227
        }
228
    } else {
229
        tree.stress_years=0; // reset stress counter
230
    }
231
    DBG_IF(delta_h_pot*delta_h_factor < 0.f || delta_h_pot*delta_h_factor > 2., "Sapling::growSapling", "inplausible height growth.");
232
 
233
    // grow
234
    tree.height += delta_h_pot * delta_h_factor;
235
    tree.age++; // increase age of sapling by 1
236
 
237
    // recruitment?
238
    if (tree.height > 4.f) {
239
        rus->saplingStat().mRecruited++;
240
 
241
        float dbh = tree.height / species->saplingGrowthParameters().hdSapling * 100.f;
242
        // the number of trees to create (result is in trees per pixel)
243
        double n_trees = species->saplingGrowthParameters().representedStemNumber(dbh);
244
        int to_establish = static_cast<int>( n_trees );
245
 
246
        // if n_trees is not an integer, choose randomly if we should add a tree.
247
        // e.g.: n_trees = 2.3 -> add 2 trees with 70% probability, and add 3 trees with p=30%.
248
        if (drandom() < (n_trees-to_establish) || to_establish==0)
249
            to_establish++;
250
 
251
        // add a new tree
252
        for (int i=0;i<to_establish;i++) {
253
            Tree &bigtree = const_cast<ResourceUnit*>(ru)->newTree();
254
 
255
            bigtree.setPosition(mGrid.indexOf(isc));
256
            // add variation: add +/-10% to dbh and *independently* to height.
1158 werner 257
            bigtree.setDbh(static_cast<float>(dbh * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation)));
258
            bigtree.setHeight(static_cast<float>(tree.height * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation)));
1113 werner 259
            bigtree.setSpecies( const_cast<Species*>(species) );
260
            bigtree.setAge(tree.age,tree.height);
261
            bigtree.setRU(const_cast<ResourceUnit*>(ru));
262
            bigtree.setup();
263
            const Tree *t = &bigtree;
264
            const_cast<ResourceUnitSpecies*>(rus)->statistics().add(t, 0); // count the newly created trees already in the stats
265
        }
266
        // clear all regeneration from this pixel (including this tree)
267
        tree.clear(); // clear this tree (no carbon flow to the ground)
268
        SaplingCell &s=mGrid[isc];
269
        for (int i=0;i<NSAPCELLS;++i) {
270
            if (s.saplings[i].is_occupied()) {
271
                // add carbon to the ground
272
                rus->saplingStat().addCarbonOfDeadSapling( s.saplings[i].height / species->saplingGrowthParameters().hdSapling * 100.f );
273
                s.saplings[i].clear();
274
            }
275
        }
1115 werner 276
        return true; // need cleanup
1113 werner 277
    }
278
    // book keeping (only for survivors) for the sapling of the resource unit / species
279
    SaplingStat &ss = rus->saplingStat();
280
    ss.mLiving++;
281
    ss.mAvgHeight+=tree.height;
282
    ss.mAvgAge+=tree.age;
283
    ss.mAvgDeltaHPot+=delta_h_pot;
284
    ss.mAvgHRealized += delta_h_pot * delta_h_factor;
1115 werner 285
    return false;
1113 werner 286
}
287
 
288
void SaplingStat::clearStatistics()
289
{
290
    mRecruited=mDied=mLiving=0;
291
    mSumDbhDied=0.;
292
    mAvgHeight=0.;
293
    mAvgAge=0.;
294
    mAvgDeltaHPot=mAvgHRealized=0.;
1158 werner 295
    mAdded=0;
1113 werner 296
 
297
}
1158 werner 298
 
299
void SaplingStat::calculate(const Species *species, ResourceUnit *ru)
300
{
301
    if (mLiving) {
302
        mAvgHeight /= double(mLiving);
303
        mAvgAge /= double(mLiving);
304
        mAvgDeltaHPot /= double(mLiving);
305
        mAvgHRealized /= double(mLiving);
306
    }
307
 
308
    // calculate carbon balance
309
    CNPair old_state = mCarbonLiving;
310
    mCarbonLiving.clear();
311
 
312
    CNPair dead_wood, dead_fine; // pools for mortality
313
    // average dbh
314
    if (mLiving>0) {
315
        // calculate the avg dbh and number of stems
316
        double avg_dbh = mAvgHeight / species->saplingGrowthParameters().hdSapling * 100.;
317
        double n = mLiving * species->saplingGrowthParameters().representedStemNumber( avg_dbh );
318
        // woody parts: stem, branchse and coarse roots
319
        double woody_bm = species->biomassWoody(avg_dbh) + species->biomassBranch(avg_dbh) + species->biomassRoot(avg_dbh);
320
        double foliage = species->biomassFoliage(avg_dbh);
321
        double fineroot = foliage*species->finerootFoliageRatio();
322
 
323
        mCarbonLiving.addBiomass( woody_bm*n, species->cnWood()  );
324
        mCarbonLiving.addBiomass( foliage*n, species->cnFoliage()  );
325
        mCarbonLiving.addBiomass( fineroot*n, species->cnFineroot()  );
326
 
327
        // turnover
328
        if (ru->snag())
329
            ru->snag()->addTurnoverLitter(species, foliage*species->turnoverLeaf(), fineroot*species->turnoverRoot());
330
 
331
        // calculate the "mortality from competition", i.e. carbon that stems from reduction of stem numbers
332
        // from Reinekes formula.
333
        //
334
        if (avg_dbh>1.) {
335
            double avg_dbh_before = (mAvgHeight - mAvgHRealized) / species->saplingGrowthParameters().hdSapling * 100.;
336
            double n_before = mLiving * species->saplingGrowthParameters().representedStemNumber( qMax(1.,avg_dbh_before) );
337
            if (n<n_before) {
338
                dead_wood.addBiomass( woody_bm * (n_before-n), species->cnWood() );
339
                dead_fine.addBiomass( foliage * (n_before-n), species->cnFoliage()  );
340
                dead_fine.addBiomass( fineroot * (n_before-n), species->cnFineroot()  );
341
            }
342
        }
343
 
344
    }
345
    if (mDied) {
346
        double avg_dbh_dead = mSumDbhDied / double(mDied);
347
        double n = mDied * species->saplingGrowthParameters().representedStemNumber( avg_dbh_dead );
348
        // woody parts: stem, branchse and coarse roots
349
 
350
        dead_wood.addBiomass( ( species->biomassWoody(avg_dbh_dead) + species->biomassBranch(avg_dbh_dead) + species->biomassRoot(avg_dbh_dead)) * n, species->cnWood()  );
351
        double foliage = species->biomassFoliage(avg_dbh_dead)*n;
352
 
353
        dead_fine.addBiomass( foliage, species->cnFoliage()  );
354
        dead_fine.addBiomass( foliage*species->finerootFoliageRatio(), species->cnFineroot()  );
355
    }
356
    if (!dead_wood.isEmpty() || !dead_fine.isEmpty())
357
        if (ru->snag())
358
            ru->snag()->addToSoil(species, dead_wood, dead_fine);
359
 
360
    // calculate net growth:
361
    // delta of stocks
362
    mCarbonGain = mCarbonLiving + dead_fine + dead_wood - old_state;
363
    if (mCarbonGain.C < 0)
364
        mCarbonGain.clear();
365
 
366
 
367
    GlobalSettings::instance()->systemStatistics()->saplingCount+=mLiving;
368
    GlobalSettings::instance()->systemStatistics()->newSaplings+=mAdded;
369
 
370
}