Rev 1211 | Rev 1214 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
1211 | werner | 2 | /******************************************************************************************** |
3 | ** iLand - an individual based forest landscape and disturbance model |
||
4 | ** http://iland.boku.ac.at |
||
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
6 | ** |
||
7 | ** This program is free software: you can redistribute it and/or modify |
||
8 | ** it under the terms of the GNU General Public License as published by |
||
9 | ** the Free Software Foundation, either version 3 of the License, or |
||
10 | ** (at your option) any later version. |
||
11 | ** |
||
12 | ** This program is distributed in the hope that it will be useful, |
||
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | ** GNU General Public License for more details. |
||
16 | ** |
||
17 | ** You should have received a copy of the GNU General Public License |
||
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | ********************************************************************************************/ |
||
1111 | werner | 20 | #include "global.h" |
21 | #include "saplings.h" |
||
22 | |||
23 | #include "globalsettings.h" |
||
24 | #include "model.h" |
||
25 | #include "resourceunit.h" |
||
26 | #include "resourceunitspecies.h" |
||
27 | #include "establishment.h" |
||
28 | #include "species.h" |
||
29 | #include "seeddispersal.h" |
||
1213 | werner | 30 | #include "mapgrid.h" |
1111 | werner | 31 | |
1113 | werner | 32 | double Saplings::mRecruitmentVariation = 0.1; // +/- 10% |
33 | double Saplings::mBrowsingPressure = 0.; |
||
1111 | werner | 34 | |
1113 | werner | 35 | |
1111 | werner | 36 | Saplings::Saplings() |
37 | { |
||
38 | |||
39 | } |
||
40 | |||
41 | void Saplings::setup() |
||
42 | { |
||
1159 | werner | 43 | //mGrid.setup(GlobalSettings::instance()->model()->grid()->metricRect(), GlobalSettings::instance()->model()->grid()->cellsize()); |
44 | FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid(); |
||
1111 | werner | 45 | // mask out out-of-project areas |
46 | HeightGrid *hg = GlobalSettings::instance()->model()->heightGrid(); |
||
1159 | werner | 47 | for (int i=0; i<lif_grid->count(); ++i) { |
48 | SaplingCell *s = cell(lif_grid->indexOf(i), false); // false: retrieve also invalid cells |
||
49 | if (s) { |
||
50 | if (!hg->valueAtIndex(lif_grid->index5(i)).isValid()) |
||
51 | s->state = SaplingCell::CellInvalid; |
||
52 | else |
||
53 | s->state = SaplingCell::CellFree; |
||
54 | } |
||
55 | |||
1111 | werner | 56 | } |
57 | |||
58 | } |
||
59 | |||
1178 | werner | 60 | void Saplings::calculateInitialStatistics(const ResourceUnit *ru) |
61 | { |
||
62 | SaplingCell *sap_cells = ru->saplingCellArray(); |
||
1196 | werner | 63 | if (!sap_cells) |
64 | return; |
||
65 | |||
1178 | werner | 66 | SaplingCell *s = sap_cells; |
67 | |||
68 | for (int i=0; i<cPxPerHectare; ++i, ++s) { |
||
69 | if (s->state != SaplingCell::CellInvalid) { |
||
70 | int cohorts_on_px = s->n_occupied(); |
||
71 | for (int j=0;j<NSAPCELLS;++j) { |
||
72 | if (s->saplings[j].is_occupied()) { |
||
73 | SaplingTree &tree=s->saplings[j]; |
||
74 | ResourceUnitSpecies *rus = tree.resourceUnitSpecies(ru); |
||
75 | rus->saplingStat().mLiving++; |
||
76 | double n_repr = rus->species()->saplingGrowthParameters().representedStemNumberH(tree.height) / static_cast<double>(cohorts_on_px); |
||
77 | if (tree.height>1.3f) |
||
78 | rus->saplingStat().mLivingSaplings += n_repr; |
||
79 | else |
||
80 | rus->saplingStat().mLivingSmallSaplings += n_repr; |
||
81 | |||
82 | rus->saplingStat().mAvgHeight+=tree.height; |
||
83 | rus->saplingStat().mAvgAge+=tree.age; |
||
84 | |||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | |||
91 | } |
||
92 | |||
1111 | werner | 93 | void Saplings::establishment(const ResourceUnit *ru) |
94 | { |
||
95 | FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid(); |
||
96 | |||
1118 | werner | 97 | QPoint imap = ru->cornerPointOffset(); // offset on LIF/saplings grid |
98 | QPoint iseedmap = QPoint(imap.x()/10, imap.y()/10); // seed-map has 20m resolution, LIF 2m -> factor 10 |
||
1111 | werner | 99 | |
1158 | werner | 100 | for (QList<ResourceUnitSpecies*>::const_iterator i=ru->ruSpecies().constBegin(); i!=ru->ruSpecies().constEnd(); ++i) |
101 | (*i)->saplingStat().clearStatistics(); |
||
102 | |||
103 | double lif_corr[cPxPerHectare]; |
||
104 | for (int i=0;i<cPxPerHectare;++i) |
||
105 | lif_corr[i]=-1.; |
||
106 | |||
1164 | werner | 107 | int species_idx; |
108 | QVector<int>::const_iterator sbegin, send; |
||
109 | ru->speciesSet()->randomSpeciesOrder(sbegin, send); |
||
110 | for (QVector<int>::const_iterator s_idx=sbegin; s_idx!=send;++s_idx) { |
||
1111 | werner | 111 | |
112 | // start from a random species (and cycle through the available species) |
||
1164 | werner | 113 | species_idx = *s_idx; |
1111 | werner | 114 | |
115 | ResourceUnitSpecies *rus = ru->ruSpecies()[species_idx]; |
||
1168 | werner | 116 | rus->establishment().clear(); |
117 | |||
1111 | werner | 118 | // check if there are seeds of the given species on the resource unit |
119 | float seeds = 0.f; |
||
1118 | werner | 120 | Grid<float> &seedmap = const_cast<Grid<float>& >(rus->species()->seedDispersal()->seedMap()); |
1111 | werner | 121 | for (int iy=0;iy<5;++iy) { |
122 | float *p = seedmap.ptr(iseedmap.x(), iseedmap.y()); |
||
123 | for (int ix=0;ix<5;++ix) |
||
124 | seeds += *p++; |
||
125 | } |
||
126 | // if there are no seeds: no need to do more |
||
127 | if (seeds==0.f) |
||
128 | continue; |
||
129 | |||
130 | // calculate the abiotic environment (TACA) |
||
131 | rus->establishment().calculateAbioticEnvironment(); |
||
132 | double abiotic_env = rus->establishment().abioticEnvironment(); |
||
1168 | werner | 133 | if (abiotic_env==0.) { |
134 | rus->establishment().writeDebugOutputs(); |
||
1111 | werner | 135 | continue; |
1168 | werner | 136 | } |
1111 | werner | 137 | |
138 | // loop over all 2m cells on this resource unit |
||
1159 | werner | 139 | SaplingCell *sap_cells = ru->saplingCellArray(); |
1111 | werner | 140 | SaplingCell *s; |
141 | int isc = 0; // index on 2m cell |
||
142 | for (int iy=0; iy<cPxPerRU; ++iy) { |
||
1159 | werner | 143 | s = &sap_cells[iy*cPxPerRU]; // pointer to a row |
144 | isc = lif_grid->index(imap.x(), imap.y()+iy); |
||
1111 | werner | 145 | |
1158 | werner | 146 | for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) { |
1111 | werner | 147 | if (s->state == SaplingCell::CellFree) { |
148 | // is a sapling of the current species already on the pixel? |
||
149 | // * test for sapling height already in cell state |
||
150 | // * test for grass-cover already in cell state |
||
1158 | werner | 151 | SaplingTree *stree=0; |
152 | SaplingTree *slot=s->saplings; |
||
153 | for (int i=0;i<NSAPCELLS;++i, ++slot) { |
||
154 | if (!stree && !slot->is_occupied()) |
||
155 | stree=slot; |
||
156 | if (slot->species_index == species_idx) { |
||
157 | stree=0; |
||
158 | break; |
||
1111 | werner | 159 | } |
160 | } |
||
161 | |||
1158 | werner | 162 | if (stree) { |
1111 | werner | 163 | // grass cover? |
1159 | werner | 164 | float seed_map_value = seedmap[lif_grid->index10(isc)]; |
1111 | werner | 165 | if (seed_map_value==0.f) |
166 | continue; |
||
167 | float lif_value = (*lif_grid)[isc]; |
||
1158 | werner | 168 | |
169 | double &lif_corrected = lif_corr[iy*cPxPerRU+ix]; |
||
1182 | werner | 170 | // calculate the LIFcorrected only once per pixel; the relative height is 0 (light level on the forest floor) |
1158 | werner | 171 | if (lif_corrected<0.) |
1178 | werner | 172 | lif_corrected = rus->species()->speciesSet()->LRIcorrection(lif_value, 0.); |
1158 | werner | 173 | |
1111 | werner | 174 | // check for the combination of seed availability and light on the forest floor |
1158 | werner | 175 | if (drandom() < seed_map_value*lif_corrected*abiotic_env ) { |
176 | // ok, lets add a sapling at the given position (age is incremented later) |
||
177 | stree->setSapling(0.05f, 0, species_idx); |
||
178 | s->checkState(); |
||
179 | rus->saplingStat().mAdded++; |
||
1111 | werner | 180 | |
1158 | werner | 181 | } |
1111 | werner | 182 | |
183 | } |
||
184 | |||
185 | } |
||
186 | } |
||
187 | } |
||
1168 | werner | 188 | // create debug output related to establishment |
189 | rus->establishment().writeDebugOutputs(); |
||
1111 | werner | 190 | } |
191 | |||
192 | } |
||
1113 | werner | 193 | |
194 | void Saplings::saplingGrowth(const ResourceUnit *ru) |
||
195 | { |
||
196 | HeightGrid *height_grid = GlobalSettings::instance()->model()->heightGrid(); |
||
197 | FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid(); |
||
198 | |||
1159 | werner | 199 | QPoint imap = ru->cornerPointOffset(); |
1115 | werner | 200 | bool need_check=false; |
1159 | werner | 201 | SaplingCell *sap_cells = ru->saplingCellArray(); |
1177 | werner | 202 | |
1113 | werner | 203 | for (int iy=0; iy<cPxPerRU; ++iy) { |
1159 | werner | 204 | SaplingCell *s = &sap_cells[iy*cPxPerRU]; // ptr to row |
205 | int isc = lif_grid->index(imap.x(), imap.y()+iy); |
||
1113 | werner | 206 | |
207 | for (int ix=0;ix<cPxPerRU; ++ix, ++s, ++isc) { |
||
208 | if (s->state != SaplingCell::CellInvalid) { |
||
1115 | werner | 209 | need_check=false; |
1177 | werner | 210 | int n_on_px = s->n_occupied(); |
1113 | werner | 211 | for (int i=0;i<NSAPCELLS;++i) { |
212 | if (s->saplings[i].is_occupied()) { |
||
213 | // growth of this sapling tree |
||
214 | const HeightGridValue &hgv = (*height_grid)[height_grid->index5(isc)]; |
||
215 | float lif_value = (*lif_grid)[isc]; |
||
216 | |||
1177 | werner | 217 | need_check |= growSapling(ru, *s, s->saplings[i], isc, hgv.height, lif_value, n_on_px); |
1113 | werner | 218 | } |
219 | } |
||
1115 | werner | 220 | if (need_check) |
221 | s->checkState(); |
||
1175 | werner | 222 | |
1113 | werner | 223 | } |
224 | } |
||
225 | } |
||
226 | |||
1158 | werner | 227 | |
228 | // store statistics on saplings/regeneration |
||
229 | for (QList<ResourceUnitSpecies*>::const_iterator i=ru->ruSpecies().constBegin(); i!=ru->ruSpecies().constEnd(); ++i) { |
||
1177 | werner | 230 | (*i)->saplingStat().calculate((*i)->species(), const_cast<ResourceUnit*>(ru)); |
1158 | werner | 231 | (*i)->statistics().add(&((*i)->saplingStat())); |
232 | } |
||
1168 | werner | 233 | |
234 | // debug output related to saplings |
||
235 | if (GlobalSettings::instance()->isDebugEnabled(GlobalSettings::dSaplingGrowth)) { |
||
236 | |||
237 | // establishment details |
||
238 | for (QList<ResourceUnitSpecies*>::const_iterator it=ru->ruSpecies().constBegin();it!=ru->ruSpecies().constEnd();++it) { |
||
1177 | werner | 239 | if ((*it)->saplingStat().livingCohorts() == 0) |
1168 | werner | 240 | continue; |
241 | DebugList &out = GlobalSettings::instance()->debugList(ru->index(), GlobalSettings::dSaplingGrowth); |
||
242 | out << (*it)->species()->id() << ru->index() <<ru->id(); |
||
1177 | werner | 243 | out << (*it)->saplingStat().livingCohorts() << (*it)->saplingStat().averageHeight() << (*it)->saplingStat().averageAge() |
1168 | werner | 244 | << (*it)->saplingStat().averageDeltaHPot() << (*it)->saplingStat().averageDeltaHRealized(); |
245 | out << (*it)->saplingStat().newSaplings() << (*it)->saplingStat().diedSaplings() |
||
246 | << (*it)->saplingStat().recruitedSaplings() <<(*it)->species()->saplingGrowthParameters().referenceRatio; |
||
247 | } |
||
248 | } |
||
249 | |||
1113 | werner | 250 | } |
251 | |||
1162 | werner | 252 | SaplingCell *Saplings::cell(QPoint lif_coords, bool only_valid, ResourceUnit **rRUPtr) |
1159 | werner | 253 | { |
254 | FloatGrid *lif_grid = GlobalSettings::instance()->model()->grid(); |
||
255 | |||
256 | // in this case, getting the actual cell is quite cumbersome: first, retrieve the resource unit, then the |
||
257 | // cell based on the offset of the given coordiantes relative to the corner of the resource unit. |
||
258 | ResourceUnit *ru = GlobalSettings::instance()->model()->ru(lif_grid->cellCenterPoint(lif_coords)); |
||
1162 | werner | 259 | if (rRUPtr) |
260 | *rRUPtr = ru; |
||
1160 | werner | 261 | |
1159 | werner | 262 | if (ru) { |
263 | QPoint local_coords = lif_coords - ru->cornerPointOffset(); |
||
264 | int idx = local_coords.y() * cPxPerRU + local_coords.x(); |
||
265 | DBGMODE( if (idx<0 || idx>=cPxPerHectare) |
||
266 | qDebug("invalid coords in Saplings::cell"); |
||
267 | ); |
||
268 | SaplingCell *s=&ru->saplingCellArray()[idx]; |
||
269 | if (s && (!only_valid || s->state!=SaplingCell::CellInvalid)) |
||
270 | return s; |
||
271 | } |
||
272 | return 0; |
||
273 | } |
||
274 | |||
1162 | werner | 275 | void Saplings::clearSaplings(const QRectF &rectangle, const bool remove_biomass) |
276 | { |
||
277 | GridRunner<float> runner(GlobalSettings::instance()->model()->grid(), rectangle); |
||
278 | ResourceUnit *ru; |
||
279 | while (runner.next()) { |
||
280 | SaplingCell *s = cell(runner.currentIndex(), true, &ru); |
||
281 | if (s) { |
||
1165 | werner | 282 | clearSaplings(s, ru, remove_biomass); |
283 | } |
||
1162 | werner | 284 | |
1165 | werner | 285 | } |
286 | } |
||
287 | |||
288 | void Saplings::clearSaplings(SaplingCell *s, ResourceUnit *ru, const bool remove_biomass) |
||
289 | { |
||
290 | if (s) { |
||
291 | for (int i=0;i<NSAPCELLS;++i) |
||
292 | if (s->saplings[i].is_occupied()) { |
||
293 | if (!remove_biomass) { |
||
1174 | werner | 294 | ResourceUnitSpecies *rus = s->saplings[i].resourceUnitSpecies(ru); |
1165 | werner | 295 | if (!rus && !rus->species()) { |
296 | qDebug() << "Saplings::clearSaplings(): invalid resource unit!!!"; |
||
297 | return; |
||
1162 | werner | 298 | } |
1165 | werner | 299 | rus->saplingStat().addCarbonOfDeadSapling( s->saplings[i].height / rus->species()->saplingGrowthParameters().hdSapling * 100.f ); |
1162 | werner | 300 | } |
1165 | werner | 301 | s->saplings[i].clear(); |
302 | } |
||
303 | s->checkState(); |
||
1162 | werner | 304 | |
1165 | werner | 305 | } |
306 | |||
307 | } |
||
308 | |||
309 | int Saplings::addSprout(const Tree *t) |
||
310 | { |
||
311 | if (t->species()->saplingGrowthParameters().sproutGrowth==0.) |
||
312 | return 0; |
||
313 | SaplingCell *sc = cell(t->positionIndex()); |
||
314 | if (!sc) |
||
315 | return 0; |
||
316 | clearSaplings(sc, const_cast<ResourceUnit*>(t->ru()), false ); |
||
317 | SaplingTree *st=sc->addSapling(0.05f, 0, t->species()->index()); |
||
318 | if (st) |
||
319 | st->set_sprout(true); |
||
320 | |||
321 | // neighboring cells |
||
322 | double crown_area = t->crownRadius()*t->crownRadius() * M_PI; //m2 |
||
323 | // calculate how many cells on the ground are covered by the crown (this is a rather rough estimate) |
||
324 | // n_cells: in addition to the original cell |
||
325 | int n_cells = static_cast<int>(round( crown_area / static_cast<double>(cPxSize*cPxSize) - 1.)); |
||
326 | if (n_cells>0) { |
||
327 | ResourceUnit *ru; |
||
328 | static const int offsets_x[8] = {1,1,0,-1,-1,-1,0,1}; |
||
329 | static const int offsets_y[8] = {0,1,1,1,0,-1,-1,-1}; |
||
330 | int s=irandom(0,8); |
||
331 | while(n_cells) { |
||
332 | sc = cell(t->positionIndex()+QPoint(offsets_x[s], offsets_y[s]),true,&ru); |
||
333 | if (sc) { |
||
334 | clearSaplings(sc, ru, false ); |
||
335 | SaplingTree *st=sc->addSapling(0.05f, 0, t->species()->index()); |
||
336 | if (st) |
||
337 | st->set_sprout(true); |
||
338 | } |
||
339 | |||
340 | s = (s+1)%8; --n_cells; |
||
1162 | werner | 341 | } |
342 | } |
||
1165 | werner | 343 | return 1; |
1162 | werner | 344 | } |
345 | |||
1113 | werner | 346 | void Saplings::updateBrowsingPressure() |
347 | { |
||
348 | if (GlobalSettings::instance()->settings().valueBool("model.settings.browsing.enabled")) |
||
349 | Saplings::mBrowsingPressure = GlobalSettings::instance()->settings().valueDouble("model.settings.browsing.browsingPressure"); |
||
350 | else |
||
351 | Saplings::mBrowsingPressure = 0.; |
||
352 | } |
||
353 | |||
1177 | werner | 354 | bool Saplings::growSapling(const ResourceUnit *ru, SaplingCell &scell, SaplingTree &tree, int isc, float dom_height, float lif_value, int cohorts_on_px) |
1113 | werner | 355 | { |
1174 | werner | 356 | ResourceUnitSpecies *rus = tree.resourceUnitSpecies(ru); |
357 | |||
1113 | werner | 358 | const Species *species = rus->species(); |
359 | |||
360 | // (1) calculate height growth potential for the tree (uses linerization of expressions...) |
||
361 | double h_pot = species->saplingGrowthParameters().heightGrowthPotential.calculate(tree.height); |
||
362 | double delta_h_pot = h_pot - tree.height; |
||
363 | |||
364 | // (2) reduce height growth potential with species growth response f_env_yr and with light state (i.e. LIF-value) of home-pixel. |
||
365 | if (dom_height==0.f) |
||
366 | throw IException(QString("growSapling: height grid at %1/%2 has value 0").arg(isc)); |
||
367 | |||
368 | double rel_height = tree.height / dom_height; |
||
369 | |||
370 | double lif_corrected = species->speciesSet()->LRIcorrection(lif_value, rel_height); // correction based on height |
||
371 | |||
372 | double lr = species->lightResponse(lif_corrected); // species specific light response (LUI, light utilization index) |
||
373 | |||
1182 | werner | 374 | rus->calculate(true); // calculate the 3pg module (this is done only once per RU); true: call comes from regeneration |
1118 | werner | 375 | double f_env_yr = rus->prod3PG().fEnvYear(); |
1113 | werner | 376 | |
1118 | werner | 377 | double delta_h_factor = f_env_yr * lr; // relative growth |
378 | |||
1113 | werner | 379 | if (h_pot<0. || delta_h_pot<0. || lif_corrected<0. || lif_corrected>1. || delta_h_factor<0. || delta_h_factor>1. ) |
380 | qDebug() << "invalid values in Sapling::growSapling"; |
||
381 | |||
1165 | werner | 382 | // sprouts grow faster. Sprouts therefore are less prone to stress (threshold), and can grow higher than the growth potential. |
383 | if (tree.is_sprout()) |
||
384 | delta_h_factor = delta_h_factor *species->saplingGrowthParameters().sproutGrowth; |
||
385 | |||
1113 | werner | 386 | // check browsing |
387 | if (mBrowsingPressure>0. && tree.height<=2.f) { |
||
388 | double p = rus->species()->saplingGrowthParameters().browsingProbability; |
||
389 | // calculate modifed annual browsing probability via odds-ratios |
||
390 | // odds = p/(1-p) -> odds_mod = odds * browsingPressure -> p_mod = odds_mod /( 1 + odds_mod) === p*pressure/(1-p+p*pressure) |
||
391 | double p_browse = p*mBrowsingPressure / (1. - p + p*mBrowsingPressure); |
||
392 | if (drandom() < p_browse) { |
||
393 | delta_h_factor = 0.; |
||
394 | } |
||
395 | } |
||
396 | |||
397 | // check mortality of saplings |
||
398 | if (delta_h_factor < species->saplingGrowthParameters().stressThreshold) { |
||
399 | tree.stress_years++; |
||
400 | if (tree.stress_years > species->saplingGrowthParameters().maxStressYears) { |
||
401 | // sapling dies... |
||
1160 | werner | 402 | rus->saplingStat().addCarbonOfDeadSapling( tree.height / species->saplingGrowthParameters().hdSapling * 100.f ); |
1113 | werner | 403 | tree.clear(); |
1115 | werner | 404 | return true; // need cleanup |
1113 | werner | 405 | } |
406 | } else { |
||
407 | tree.stress_years=0; // reset stress counter |
||
408 | } |
||
1165 | werner | 409 | DBG_IF(delta_h_pot*delta_h_factor < 0.f || (!tree.is_sprout() && delta_h_pot*delta_h_factor > 2.), "Sapling::growSapling", "inplausible height growth."); |
1113 | werner | 410 | |
411 | // grow |
||
412 | tree.height += delta_h_pot * delta_h_factor; |
||
413 | tree.age++; // increase age of sapling by 1 |
||
414 | |||
415 | // recruitment? |
||
416 | if (tree.height > 4.f) { |
||
417 | rus->saplingStat().mRecruited++; |
||
418 | |||
419 | float dbh = tree.height / species->saplingGrowthParameters().hdSapling * 100.f; |
||
420 | // the number of trees to create (result is in trees per pixel) |
||
421 | double n_trees = species->saplingGrowthParameters().representedStemNumber(dbh); |
||
422 | int to_establish = static_cast<int>( n_trees ); |
||
423 | |||
424 | // if n_trees is not an integer, choose randomly if we should add a tree. |
||
425 | // e.g.: n_trees = 2.3 -> add 2 trees with 70% probability, and add 3 trees with p=30%. |
||
426 | if (drandom() < (n_trees-to_establish) || to_establish==0) |
||
427 | to_establish++; |
||
428 | |||
429 | // add a new tree |
||
430 | for (int i=0;i<to_establish;i++) { |
||
431 | Tree &bigtree = const_cast<ResourceUnit*>(ru)->newTree(); |
||
432 | |||
1159 | werner | 433 | bigtree.setPosition(GlobalSettings::instance()->model()->grid()->indexOf(isc)); |
1182 | werner | 434 | // add variation: add +/-N% to dbh and *independently* to height. |
1158 | werner | 435 | bigtree.setDbh(static_cast<float>(dbh * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation))); |
436 | bigtree.setHeight(static_cast<float>(tree.height * nrandom(1. - mRecruitmentVariation, 1. + mRecruitmentVariation))); |
||
1113 | werner | 437 | bigtree.setSpecies( const_cast<Species*>(species) ); |
438 | bigtree.setAge(tree.age,tree.height); |
||
439 | bigtree.setRU(const_cast<ResourceUnit*>(ru)); |
||
440 | bigtree.setup(); |
||
441 | const Tree *t = &bigtree; |
||
442 | const_cast<ResourceUnitSpecies*>(rus)->statistics().add(t, 0); // count the newly created trees already in the stats |
||
443 | } |
||
444 | // clear all regeneration from this pixel (including this tree) |
||
445 | tree.clear(); // clear this tree (no carbon flow to the ground) |
||
446 | for (int i=0;i<NSAPCELLS;++i) { |
||
1159 | werner | 447 | if (scell.saplings[i].is_occupied()) { |
1113 | werner | 448 | // add carbon to the ground |
1174 | werner | 449 | ResourceUnitSpecies *srus = scell.saplings[i].resourceUnitSpecies(ru); |
450 | srus->saplingStat().addCarbonOfDeadSapling( scell.saplings[i].height / srus->species()->saplingGrowthParameters().hdSapling * 100.f ); |
||
1159 | werner | 451 | scell.saplings[i].clear(); |
1113 | werner | 452 | } |
453 | } |
||
1115 | werner | 454 | return true; // need cleanup |
1113 | werner | 455 | } |
456 | // book keeping (only for survivors) for the sapling of the resource unit / species |
||
457 | SaplingStat &ss = rus->saplingStat(); |
||
1177 | werner | 458 | double n_repr = species->saplingGrowthParameters().representedStemNumberH(tree.height) / static_cast<double>(cohorts_on_px); |
459 | if (tree.height>1.3f) |
||
460 | ss.mLivingSaplings += n_repr; |
||
461 | else |
||
462 | ss.mLivingSmallSaplings += n_repr; |
||
1113 | werner | 463 | ss.mLiving++; |
464 | ss.mAvgHeight+=tree.height; |
||
465 | ss.mAvgAge+=tree.age; |
||
466 | ss.mAvgDeltaHPot+=delta_h_pot; |
||
467 | ss.mAvgHRealized += delta_h_pot * delta_h_factor; |
||
1115 | werner | 468 | return false; |
1113 | werner | 469 | } |
470 | |||
471 | void SaplingStat::clearStatistics() |
||
472 | { |
||
473 | mRecruited=mDied=mLiving=0; |
||
1177 | werner | 474 | mLivingSaplings=0.; mLivingSmallSaplings=0.; |
1113 | werner | 475 | mSumDbhDied=0.; |
476 | mAvgHeight=0.; |
||
477 | mAvgAge=0.; |
||
478 | mAvgDeltaHPot=mAvgHRealized=0.; |
||
1158 | werner | 479 | mAdded=0; |
1113 | werner | 480 | |
481 | } |
||
1158 | werner | 482 | |
1177 | werner | 483 | void SaplingStat::calculate(const Species *species, ResourceUnit *ru) |
1158 | werner | 484 | { |
485 | if (mLiving) { |
||
486 | mAvgHeight /= double(mLiving); |
||
487 | mAvgAge /= double(mLiving); |
||
488 | mAvgDeltaHPot /= double(mLiving); |
||
489 | mAvgHRealized /= double(mLiving); |
||
490 | } |
||
1178 | werner | 491 | if (GlobalSettings::instance()->currentYear()==0) |
492 | return; // no need for carbon flows in initial run |
||
1158 | werner | 493 | |
494 | // calculate carbon balance |
||
495 | CNPair old_state = mCarbonLiving; |
||
496 | mCarbonLiving.clear(); |
||
497 | |||
498 | CNPair dead_wood, dead_fine; // pools for mortality |
||
499 | // average dbh |
||
500 | if (mLiving>0) { |
||
501 | // calculate the avg dbh and number of stems |
||
502 | double avg_dbh = mAvgHeight / species->saplingGrowthParameters().hdSapling * 100.; |
||
1177 | werner | 503 | // the number of "real" stems is given by the Reineke formula |
504 | double n = mLivingSaplings; // total number of saplings (>0.05m) |
||
1175 | werner | 505 | |
1158 | werner | 506 | // woody parts: stem, branchse and coarse roots |
507 | double woody_bm = species->biomassWoody(avg_dbh) + species->biomassBranch(avg_dbh) + species->biomassRoot(avg_dbh); |
||
508 | double foliage = species->biomassFoliage(avg_dbh); |
||
509 | double fineroot = foliage*species->finerootFoliageRatio(); |
||
510 | |||
511 | mCarbonLiving.addBiomass( woody_bm*n, species->cnWood() ); |
||
512 | mCarbonLiving.addBiomass( foliage*n, species->cnFoliage() ); |
||
513 | mCarbonLiving.addBiomass( fineroot*n, species->cnFineroot() ); |
||
514 | |||
1160 | werner | 515 | DBGMODE( |
516 | if (isnan(mCarbonLiving.C)) |
||
517 | qDebug("carbon NaN in SaplingStat::calculate (living trees)."); |
||
518 | ); |
||
519 | |||
1158 | werner | 520 | // turnover |
521 | if (ru->snag()) |
||
522 | ru->snag()->addTurnoverLitter(species, foliage*species->turnoverLeaf(), fineroot*species->turnoverRoot()); |
||
523 | |||
524 | // calculate the "mortality from competition", i.e. carbon that stems from reduction of stem numbers |
||
525 | // from Reinekes formula. |
||
526 | // |
||
527 | if (avg_dbh>1.) { |
||
528 | double avg_dbh_before = (mAvgHeight - mAvgHRealized) / species->saplingGrowthParameters().hdSapling * 100.; |
||
1177 | werner | 529 | double n_before = mLiving * species->saplingGrowthParameters().representedStemNumber( qMax(1.,avg_dbh_before) ); |
1158 | werner | 530 | if (n<n_before) { |
531 | dead_wood.addBiomass( woody_bm * (n_before-n), species->cnWood() ); |
||
532 | dead_fine.addBiomass( foliage * (n_before-n), species->cnFoliage() ); |
||
533 | dead_fine.addBiomass( fineroot * (n_before-n), species->cnFineroot() ); |
||
1160 | werner | 534 | DBGMODE( |
535 | if (isnan(dead_fine.C)) |
||
536 | qDebug("carbon NaN in SaplingStat::calculate (self thinning)."); |
||
537 | ); |
||
538 | |||
1158 | werner | 539 | } |
540 | } |
||
541 | |||
542 | } |
||
543 | if (mDied) { |
||
544 | double avg_dbh_dead = mSumDbhDied / double(mDied); |
||
1177 | werner | 545 | double n = mDied * species->saplingGrowthParameters().representedStemNumber( avg_dbh_dead ); |
1158 | werner | 546 | // woody parts: stem, branchse and coarse roots |
547 | |||
548 | dead_wood.addBiomass( ( species->biomassWoody(avg_dbh_dead) + species->biomassBranch(avg_dbh_dead) + species->biomassRoot(avg_dbh_dead)) * n, species->cnWood() ); |
||
549 | double foliage = species->biomassFoliage(avg_dbh_dead)*n; |
||
550 | |||
551 | dead_fine.addBiomass( foliage, species->cnFoliage() ); |
||
552 | dead_fine.addBiomass( foliage*species->finerootFoliageRatio(), species->cnFineroot() ); |
||
1160 | werner | 553 | DBGMODE( |
554 | if (isnan(dead_fine.C)) |
||
555 | qDebug("carbon NaN in SaplingStat::calculate (died trees)."); |
||
556 | ); |
||
557 | |||
1158 | werner | 558 | } |
559 | if (!dead_wood.isEmpty() || !dead_fine.isEmpty()) |
||
560 | if (ru->snag()) |
||
561 | ru->snag()->addToSoil(species, dead_wood, dead_fine); |
||
562 | |||
563 | // calculate net growth: |
||
564 | // delta of stocks |
||
565 | mCarbonGain = mCarbonLiving + dead_fine + dead_wood - old_state; |
||
566 | if (mCarbonGain.C < 0) |
||
567 | mCarbonGain.clear(); |
||
568 | |||
569 | |||
570 | GlobalSettings::instance()->systemStatistics()->saplingCount+=mLiving; |
||
571 | GlobalSettings::instance()->systemStatistics()->newSaplings+=mAdded; |
||
572 | |||
573 | } |
||
1162 | werner | 574 | |
575 | double SaplingStat::livingStemNumber(const Species *species, double &rAvgDbh, double &rAvgHeight, double &rAvgAge) const |
||
576 | { |
||
577 | rAvgHeight = averageHeight(); |
||
578 | rAvgDbh = rAvgHeight / species->saplingGrowthParameters().hdSapling * 100.f; |
||
579 | rAvgAge = averageAge(); |
||
580 | double n= species->saplingGrowthParameters().representedStemNumber(rAvgDbh); |
||
581 | return n; |
||
582 | // *** old code (sapling.cpp) *** |
||
583 | // double total = 0.; |
||
584 | // double dbh_sum = 0.; |
||
585 | // double h_sum = 0.; |
||
586 | // double age_sum = 0.; |
||
587 | // const SaplingGrowthParameters &p = mRUS->species()->saplingGrowthParameters(); |
||
588 | // for (QVector<SaplingTreeOld>::const_iterator it = mSaplingTrees.constBegin(); it!=mSaplingTrees.constEnd(); ++it) { |
||
589 | // float dbh = it->height / p.hdSapling * 100.f; |
||
590 | // if (dbh<1.) // minimum size: 1cm |
||
591 | // continue; |
||
592 | // double n = p.representedStemNumber(dbh); // one cohort on the pixel represents that number of trees |
||
593 | // dbh_sum += n*dbh; |
||
594 | // h_sum += n*it->height; |
||
595 | // age_sum += n*it->age.age; |
||
596 | // total += n; |
||
597 | // } |
||
598 | // if (total>0.) { |
||
599 | // dbh_sum /= total; |
||
600 | // h_sum /= total; |
||
601 | // age_sum /= total; |
||
602 | // } |
||
603 | // rAvgDbh = dbh_sum; |
||
604 | // rAvgHeight = h_sum; |
||
605 | // rAvgAge = age_sum; |
||
606 | // return total; |
||
607 | } |
||
1174 | werner | 608 | |
609 | ResourceUnitSpecies *SaplingTree::resourceUnitSpecies(const ResourceUnit *ru) |
||
610 | { |
||
611 | if (!ru || !is_occupied()) |
||
612 | return 0; |
||
613 | ResourceUnitSpecies *rus = ru->resourceUnitSpecies(species_index); |
||
614 | return rus; |
||
615 | } |
||
1213 | werner | 616 | |
617 | SaplingCellRunner::SaplingCellRunner(const int stand_id, const MapGrid *stand_grid) |
||
618 | { |
||
619 | mRunner = 0; |
||
620 | mRU = 0; |
||
621 | mStandId = stand_id; |
||
622 | mStandGrid = stand_grid ? stand_grid : GlobalSettings::instance()->model()->standGrid(); |
||
623 | QRectF box = mStandGrid->boundingBox(stand_id); |
||
624 | mRunner = new GridRunner<float>(GlobalSettings::instance()->model()->grid(), box); |
||
625 | |||
626 | } |
||
627 | |||
628 | SaplingCell *SaplingCellRunner::next() |
||
629 | { |
||
630 | if (!mRunner) |
||
631 | return 0; |
||
632 | while (float *n = mRunner->next()) { |
||
633 | if (!n) |
||
634 | return 0; // end of the bounding box |
||
635 | if (mStandGrid->standIDFromLIFCoord(mRunner->currentIndex()) != mStandId) |
||
636 | continue; // pixel does not belong to the target stand |
||
637 | mRU = GlobalSettings::instance()->model()->ru(mRunner->currentCoord()); |
||
638 | SaplingCell *sc=0; |
||
639 | if (mRU) |
||
640 | sc=mRU->saplingCell(mRunner->currentIndex()); |
||
641 | if (sc) |
||
642 | return sc; |
||
643 | qDebug() << "SaplingCellRunner::next(): unexected missing SaplingCell!"; |
||
644 | return 0; |
||
645 | } |
||
646 | return 0; |
||
647 | } |
||
648 | |||
649 | QPointF SaplingCellRunner::currentCoord() const |
||
650 | { |
||
651 | return mRunner->currentCoord(); |
||
652 | } |