Subversion Repositories public iLand

Rev

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

Rev Author Line No. Line
1
 
1111 werner 2
#ifndef SAPLINGS_H
3
#define SAPLINGS_H
4
 
5
#include "grid.h"
1113 werner 6
#include "snag.h"
1162 werner 7
#include <QRectF>
1174 werner 8
class ResourceUnitSpecies; // forward
9
class ResourceUnit; // forward
1111 werner 10
 
11
struct SaplingTree {
1113 werner 12
    SaplingTree() { clear(); }
1111 werner 13
    short unsigned int age;  // number of consectuive years the sapling suffers from dire conditions
1118 werner 14
    short signed int species_index; // index of the species within the resource-unit-species container
1111 werner 15
    unsigned char stress_years; // (upper 16bits) + age of sapling (lower 16 bits)
1165 werner 16
    unsigned char flags; // flags, e.g. whether sapling stems from sprouting
1111 werner 17
    float height; // height of the sapling in meter
18
    bool is_occupied() const { return height>0.f; }
1118 werner 19
    void clear()  {  age=0; species_index=-1; stress_years=0; flags=0; height=0.f;  }
1158 werner 20
    void setSapling(const float h_m, const int age_yrs, const int species_idx) { height=h_m; age=static_cast<short unsigned int>(age_yrs); stress_years=0; species_index=static_cast<short signed int>(species_idx); }
1165 werner 21
    // flags
22
    bool is_sprout() const { return flags & 1; }
23
    void set_sprout(const bool sprout) {if (sprout) flags |= 1; else flags &= (1 ^ 0xffffff ); }
1174 werner 24
    // get resource unit species of the sapling tree
25
    ResourceUnitSpecies *resourceUnitSpecies(const ResourceUnit *ru);
1111 werner 26
};
27
#define NSAPCELLS 5
28
struct SaplingCell {
29
    enum ECellState { CellInvalid=0, CellFree=1, CellFull=2};
30
    SaplingCell() {
31
        state=CellInvalid;
32
    }
33
    ECellState state;
34
    SaplingTree saplings[NSAPCELLS];
35
    void checkState() { if (state==CellInvalid) return;
1112 werner 36
                        bool free = false;
1111 werner 37
                        for (int i=0;i<NSAPCELLS;++i) {
38
                            // locked for all species, if a sapling of one species >1.3m
1112 werner 39
                            if (saplings[i].height>1.3f) {state = CellFull; return; }
1111 werner 40
                            // locked, if all slots are occupied.
41
                            if (!saplings[i].is_occupied())
42
                                free=true;
43
                        }
44
                        state = free? CellFree : CellFull;
45
                      }
1117 werner 46
    /// get an index to an open slot in the cell, or -1 if all slots are occupied
47
    int free_index() {
48
        for (int i=0;i<NSAPCELLS;++i)
49
            if (!saplings[i].is_occupied())
50
                return i;
51
        return -1;
52
    }
1175 werner 53
    /// count the number of occupied slots on the pixel
54
    int n_occupied() {
55
        int n=0;
56
        for (int i=0;i<NSAPCELLS;++i)
57
            n+=saplings[i].is_occupied();
58
        return n;
59
    }
60
 
1117 werner 61
    /// add a sapling to this cell, return a pointer to the tree on success, or 0 otherwise
62
    SaplingTree *addSapling(const float h_m, const int age_yrs, const int species_idx) {
63
        int idx = free_index();
64
        if (idx==-1)
65
            return 0;
66
        saplings[idx].setSapling(h_m, age_yrs, species_idx);
67
        return &saplings[idx];
68
    }
69
    /// return the maximum height on the pixel
70
    float max_height() { if (state==CellInvalid) return 0.f;
71
                         float h_max = 0.f;
72
                         for (int i=0;i<NSAPCELLS;++i)
73
                             h_max = std::max(saplings[i].height, h_max);
74
                         return h_max;
75
                       }
76
    /// return the sapling tree of the requested species, or 0
77
    SaplingTree *sapling(int species_index) {
78
        if (state==CellInvalid) return 0;
79
        for (int i=0;i<NSAPCELLS;++i)
80
            if (saplings[i].species_index == species_index)
81
                return &saplings[i];
82
        return 0;
83
    }
1111 werner 84
};
85
class ResourceUnit;
1113 werner 86
class Saplings;
1111 werner 87
 
1117 werner 88
/** The SaplingStat class stores statistics on the resource unit x species level.
89
 */
1113 werner 90
class SaplingStat
91
{
92
public:
1115 werner 93
    SaplingStat() { clearStatistics(); }
1113 werner 94
    void clearStatistics();
1176 werner 95
    /// calculate statistics (and carbon flows) for the saplings of species 'species' on 'ru'.
96
    /// The 'cohorts_per_area' gives the average cohort density on the RU (avg. cohorts/pixel).
1175 werner 97
    void calculate(const Species *species, ResourceUnit *ru, double cohorts_per_area);
1113 werner 98
    // actions
1160 werner 99
    void addCarbonOfDeadSapling(float dbh) { mDied++; mSumDbhDied+=dbh;  }
1111 werner 100
 
1113 werner 101
    // access to statistics
102
    int newSaplings() const { return mAdded; }
103
    int diedSaplings() const { return mDied; }
104
    int livingSaplings() const { return mLiving; } ///< get the number
105
    int recruitedSaplings() const { return mRecruited; }
106
    ///  returns the *represented* (Reineke's Law) number of trees (N/ha) and the mean dbh/height (cm/m)
1162 werner 107
    double livingStemNumber(const Species *species, double &rAvgDbh, double &rAvgHeight, double &rAvgAge) const;
1113 werner 108
 
109
    double averageHeight() const { return mAvgHeight; }
110
    double averageAge() const { return mAvgAge; }
111
    double averageDeltaHPot() const { return mAvgDeltaHPot; }
112
    double averageDeltaHRealized() const { return mAvgHRealized; }
113
    // carbon and nitrogen
114
    const CNPair &carbonLiving() const { return mCarbonLiving; } ///< state of the living
115
    const CNPair &carbonGain() const { return mCarbonGain; } ///< state of the living
116
 
117
private:
118
    int mAdded; ///< number of trees added
119
    int mRecruited; ///< number recruited (i.e. grown out of regeneration layer)
120
    int mDied; ///< number of trees died
121
    double mSumDbhDied; ///< running sum of dbh of died trees (used to calculate detritus)
122
    int mLiving; ///< number of trees (cohorts!!!) currently in the regeneration layer
1175 werner 123
    double mLivingSaplings; ///< number of individual trees in the regen layer (using Reinekes R)
1113 werner 124
    double mAvgHeight; ///< average height of saplings (m)
125
    double mAvgAge; ///< average age of saplings (years)
126
    double mAvgDeltaHPot; ///< average height increment potential (m)
127
    double mAvgHRealized; ///< average realized height increment
1175 werner 128
    CNPair mCarbonLiving; ///< kg Carbon (kg/ru) of saplings
1113 werner 129
    CNPair mCarbonGain; ///< net growth (kg / ru) of saplings
130
 
131
    friend class Saplings;
132
 
133
};
1117 werner 134
/** The Saplings class the container for the establishment and sapling growth in iLand.
135
 *
136
*/
1111 werner 137
class Saplings
138
{
139
public:
140
    Saplings();
141
    void setup();
142
    // main functions
143
    void establishment(const ResourceUnit *ru);
1113 werner 144
    void saplingGrowth(const ResourceUnit *ru);
145
 
1117 werner 146
    // access
1162 werner 147
    /// return the SaplingCell (i.e. container for the ind. saplings) for the given 2x2m coordinates
148
    /// if 'only_valid' is true, then 0 is returned if no living saplings are on the cell
149
    /// 'rRUPtr' is a pointer to a RU-ptr: if provided, a pointer to the resource unit is stored
150
    SaplingCell *cell(QPoint lif_coords, bool only_valid=true, ResourceUnit **rRUPtr=0);
151
    /// clear/kill all saplings within the rectangle given by 'rectangle'.
1165 werner 152
    /// If 'remove_biomass' is true, then the biomass is extracted (e.g. burnt), otherwise they are moved to soil
1162 werner 153
    void clearSaplings(const QRectF &rectangle, const bool remove_biomass);
1165 werner 154
    /// clear all saplings on a given cell 's' (if 'remove_biomass' is true: biomass removed from system (e.g. burnt))
155
    void clearSaplings(SaplingCell *s, ResourceUnit *ru, const bool remove_biomass);
1111 werner 156
 
1165 werner 157
    /// generate vegetative offspring from 't' (sprouts)
158
    int addSprout(const Tree *t);
159
 
1113 werner 160
    static void setRecruitmentVariation(const double variation) { mRecruitmentVariation = variation; }
161
    static void updateBrowsingPressure();
162
 
1111 werner 163
private:
1159 werner 164
    bool growSapling(const ResourceUnit *ru, SaplingCell &scell, SaplingTree &tree, int isc, float dom_height, float lif_value);
165
    //Grid<SaplingCell> mGrid;
1113 werner 166
    static double mRecruitmentVariation;
167
    static double mBrowsingPressure;
1111 werner 168
};
169
 
170
#endif // SAPLINGS_H