Subversion Repositories public iLand

Rev

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