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