Subversion Repositories public iLand

Rev

Rev 1112 | Rev 1115 | 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
 
126
    QPoint imap =  mGrid.indexAt(ru->boundingBox().topLeft());
127
    for (int iy=0; iy<cPxPerRU; ++iy) {
128
        SaplingCell *s = mGrid.ptr(imap.x(), imap.y()+iy); // ptr to the row
129
        int isc = mGrid.index(imap.x(), imap.y()+iy);
130
 
131
        for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) {
132
            if (s->state != SaplingCell::CellInvalid) {
133
                for (int i=0;i<NSAPCELLS;++i) {
134
                    if (s->saplings[i].is_occupied()) {
135
                        // growth of this sapling tree
136
                        const HeightGridValue &hgv = (*height_grid)[height_grid->index5(isc)];
137
                        float lif_value = (*lif_grid)[isc];
138
 
139
                        growSapling(ru, s->saplings[i], isc, hgv.height, lif_value);
140
                    }
141
                }
142
            }
143
        }
144
    }
145
 
146
}
147
 
148
void Saplings::updateBrowsingPressure()
149
{
150
    if (GlobalSettings::instance()->settings().valueBool("model.settings.browsing.enabled"))
151
        Saplings::mBrowsingPressure = GlobalSettings::instance()->settings().valueDouble("model.settings.browsing.browsingPressure");
152
    else
153
        Saplings::mBrowsingPressure = 0.;
154
}
155
 
156
void Saplings::growSapling(const ResourceUnit *ru, SaplingTree &tree, int isc, float dom_height, float lif_value)
157
{
158
    ResourceUnitSpecies *rus = const_cast<ResourceUnitSpecies*>(ru->ruSpecies()[tree.species_index]);
159
    const Species *species = rus->species();
160
 
161
    // (1) calculate height growth potential for the tree (uses linerization of expressions...)
162
    double h_pot = species->saplingGrowthParameters().heightGrowthPotential.calculate(tree.height);
163
    double delta_h_pot = h_pot - tree.height;
164
 
165
    // (2) reduce height growth potential with species growth response f_env_yr and with light state (i.e. LIF-value) of home-pixel.
166
    if (dom_height==0.f)
167
        throw IException(QString("growSapling: height grid at %1/%2 has value 0").arg(isc));
168
 
169
    double rel_height = tree.height / dom_height;
170
 
171
    double lif_corrected = species->speciesSet()->LRIcorrection(lif_value, rel_height); // correction based on height
172
 
173
    double lr = species->lightResponse(lif_corrected); // species specific light response (LUI, light utilization index)
174
 
175
    double delta_h_factor = rus->prod3PG().fEnvYear() * lr; // relative growth
176
 
177
    if (h_pot<0. || delta_h_pot<0. || lif_corrected<0. || lif_corrected>1. || delta_h_factor<0. || delta_h_factor>1. )
178
        qDebug() << "invalid values in Sapling::growSapling";
179
 
180
    // check browsing
181
    if (mBrowsingPressure>0. && tree.height<=2.f) {
182
        double p = rus->species()->saplingGrowthParameters().browsingProbability;
183
        // calculate modifed annual browsing probability via odds-ratios
184
        // odds = p/(1-p) -> odds_mod = odds * browsingPressure -> p_mod = odds_mod /( 1 + odds_mod) === p*pressure/(1-p+p*pressure)
185
        double p_browse = p*mBrowsingPressure / (1. - p + p*mBrowsingPressure);
186
        if (drandom() < p_browse) {
187
            delta_h_factor = 0.;
188
        }
189
    }
190
 
191
    // check mortality of saplings
192
    if (delta_h_factor < species->saplingGrowthParameters().stressThreshold) {
193
        tree.stress_years++;
194
        if (tree.stress_years > species->saplingGrowthParameters().maxStressYears) {
195
            // sapling dies...
196
            tree.clear();
197
            rus->saplingStat().addCarbonOfDeadSapling( tree.height / species->saplingGrowthParameters().hdSapling * 100. );
198
            return;
199
        }
200
    } else {
201
        tree.stress_years=0; // reset stress counter
202
    }
203
    DBG_IF(delta_h_pot*delta_h_factor < 0.f || delta_h_pot*delta_h_factor > 2., "Sapling::growSapling", "inplausible height growth.");
204
 
205
    // grow
206
    tree.height += delta_h_pot * delta_h_factor;
207
    tree.age++; // increase age of sapling by 1
208
 
209
    // recruitment?
210
    if (tree.height > 4.f) {
211
        rus->saplingStat().mRecruited++;
212
 
213
        float dbh = tree.height / species->saplingGrowthParameters().hdSapling * 100.f;
214
        // the number of trees to create (result is in trees per pixel)
215
        double n_trees = species->saplingGrowthParameters().representedStemNumber(dbh);
216
        int to_establish = static_cast<int>( n_trees );
217
 
218
        // if n_trees is not an integer, choose randomly if we should add a tree.
219
        // e.g.: n_trees = 2.3 -> add 2 trees with 70% probability, and add 3 trees with p=30%.
220
        if (drandom() < (n_trees-to_establish) || to_establish==0)
221
            to_establish++;
222
 
223
        // add a new tree
224
        for (int i=0;i<to_establish;i++) {
225
            Tree &bigtree = const_cast<ResourceUnit*>(ru)->newTree();
226
 
227
            bigtree.setPosition(mGrid.indexOf(isc));
228
            // add variation: add +/-10% to dbh and *independently* to height.
229
            bigtree.setDbh(dbh * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation));
230
            bigtree.setHeight(tree.height * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation));
231
            bigtree.setSpecies( const_cast<Species*>(species) );
232
            bigtree.setAge(tree.age,tree.height);
233
            bigtree.setRU(const_cast<ResourceUnit*>(ru));
234
            bigtree.setup();
235
            const Tree *t = &bigtree;
236
            const_cast<ResourceUnitSpecies*>(rus)->statistics().add(t, 0); // count the newly created trees already in the stats
237
        }
238
        // clear all regeneration from this pixel (including this tree)
239
        tree.clear(); // clear this tree (no carbon flow to the ground)
240
        SaplingCell &s=mGrid[isc];
241
        for (int i=0;i<NSAPCELLS;++i) {
242
            if (s.saplings[i].is_occupied()) {
243
                // add carbon to the ground
244
                rus->saplingStat().addCarbonOfDeadSapling( s.saplings[i].height / species->saplingGrowthParameters().hdSapling * 100.f );
245
                s.saplings[i].clear();
246
            }
247
        }
248
        return;
249
    }
250
    // book keeping (only for survivors) for the sapling of the resource unit / species
251
    SaplingStat &ss = rus->saplingStat();
252
    ss.mLiving++;
253
    ss.mAvgHeight+=tree.height;
254
    ss.mAvgAge+=tree.age;
255
    ss.mAvgDeltaHPot+=delta_h_pot;
256
    ss.mAvgHRealized += delta_h_pot * delta_h_factor;
257
 
258
}
259
 
260
void SaplingStat::clearStatistics()
261
{
262
    mRecruited=mDied=mLiving=0;
263
    mSumDbhDied=0.;
264
    mAvgHeight=0.;
265
    mAvgAge=0.;
266
    mAvgDeltaHPot=mAvgHRealized=0.;
267
 
268
}