landcover.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file landcover.h
  3. /// \brief Functions handling landcover aspects, such as creating or resizing Stands
  4. ///
  5. /// \author Mats Lindeskog
  6. /// $Date: 2018-12-21 08:30:58 +0100 (ven, 21 déc 2018) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #ifndef LPJ_GUESS_LANDCOVER_H
  10. #define LPJ_GUESS_LANDCOVER_H
  11. #include "guess.h"
  12. #include "growth.h"
  13. #include "canexch.h"
  14. #include "cropsowing.h"
  15. #include "cropphenology.h"
  16. #include "cropallocation.h"
  17. #include "management.h"
  18. #include "externalinput.h"
  19. #include "inputmodule.h"
  20. const int def_verndate_ndays_after_last_springdate = 60;
  21. /// Creates stands for landcovers present in the gridcell first year of the simulation
  22. void landcover_init(Gridcell& gridcell, InputModule* input_module);
  23. /// Handles changes in the landcover fractions from year to year
  24. /** This function will for instance kill or create new stands
  25. * if needed.
  26. */
  27. void landcover_dynamics(Gridcell& gridcell, InputModule* input_module);
  28. /// Query whether a date is within a period spanned by two dates.
  29. bool dayinperiod(int day, int start, int end);
  30. /// Step n days from a date.
  31. int stepfromdate(int day, int step);
  32. /// struct storing carbon, nitrogen and water (plus aaet_5 and anfix_calc) during landcover change
  33. struct landcover_change_transfer {
  34. double *transfer_litter_leaf;
  35. double *transfer_litter_sap;
  36. double *transfer_litter_heart;
  37. double *transfer_litter_root;
  38. double *transfer_litter_repr;
  39. double *transfer_harvested_products_slow;
  40. double *transfer_nmass_litter_leaf;
  41. double *transfer_nmass_litter_sap;
  42. double *transfer_nmass_litter_heart;
  43. double *transfer_nmass_litter_root;
  44. double *transfer_harvested_products_slow_nmass;
  45. double transfer_acflux_harvest;
  46. double transfer_anflux_harvest;
  47. double transfer_cpool_fast;
  48. double transfer_cpool_slow;
  49. double transfer_wcont[NSOILLAYER];
  50. double transfer_wcont_evap;
  51. double transfer_decomp_litter_mean;
  52. double transfer_k_soilfast_mean;
  53. double transfer_k_soilslow_mean;
  54. Sompool transfer_sompool[NSOMPOOL];
  55. double transfer_nmass_avail;
  56. double transfer_snowpack;
  57. double transfer_snowpack_nmass;
  58. Historic<double, NYEARAAET> transfer_aaet_5;
  59. double transfer_anfix_calc;
  60. landcover_change_transfer();
  61. ~landcover_change_transfer();
  62. void allocate();
  63. double ccont() {
  64. double ccont = transfer_acflux_harvest;
  65. for(int i=0; i<npft; i++) {
  66. ccont += transfer_litter_leaf[i];
  67. ccont += transfer_litter_sap[i];
  68. ccont += transfer_litter_heart[i];
  69. ccont += transfer_litter_root[i];
  70. ccont += transfer_litter_repr[i];
  71. ccont += transfer_harvested_products_slow[i];
  72. }
  73. for(int i=0; i<NSOMPOOL-1; i++)
  74. ccont += transfer_sompool[i].cmass;
  75. return ccont;
  76. }
  77. double ncont() {
  78. double ncont = transfer_anflux_harvest;
  79. for(int i=0; i<npft; i++) {
  80. ncont += transfer_nmass_litter_leaf[i];
  81. ncont += transfer_nmass_litter_sap[i];
  82. ncont += transfer_nmass_litter_heart[i];
  83. ncont += transfer_nmass_litter_root[i];
  84. ncont += transfer_harvested_products_slow_nmass[i];
  85. }
  86. for(int i=0; i<NSOMPOOL-1; i++)
  87. ncont += transfer_sompool[i].nmass;
  88. ncont += transfer_nmass_avail;
  89. ncont += transfer_snowpack_nmass;
  90. return ncont;
  91. }
  92. // Adds non-living C, N and water from a stand
  93. void add_from_stand(Stand& stand, double scale) {
  94. stand.firstobj();
  95. while(stand.isobj) {
  96. Patch& patch = stand.getobj();
  97. // sum original litter C & N:
  98. for(int n=0; n<npft; n++)
  99. {
  100. transfer_litter_leaf[n] += patch.pft[n].litter_leaf * scale;
  101. transfer_litter_root[n] += patch.pft[n].litter_root * scale;
  102. transfer_litter_sap[n] += patch.pft[n].litter_sap * scale;
  103. transfer_litter_heart[n] += patch.pft[n].litter_heart * scale;
  104. transfer_litter_repr[n] += patch.pft[n].litter_repr * scale;
  105. transfer_nmass_litter_leaf[n] += patch.pft[n].nmass_litter_leaf * scale;
  106. transfer_nmass_litter_root[n] += patch.pft[n].nmass_litter_root * scale;
  107. transfer_nmass_litter_sap[n] += patch.pft[n].nmass_litter_sap * scale;
  108. transfer_nmass_litter_heart[n] += patch.pft[n].nmass_litter_heart * scale;
  109. if(ifslowharvestpool) {
  110. transfer_harvested_products_slow[n] += patch.pft[n].harvested_products_slow * scale;
  111. transfer_harvested_products_slow_nmass[n] += patch.pft[n].harvested_products_slow_nmass * scale;
  112. }
  113. }
  114. // sum litter C & N:
  115. transfer_cpool_fast += patch.soil.cpool_fast * scale;
  116. transfer_cpool_slow += patch.soil.cpool_slow * scale;
  117. // sum soil C & N:
  118. for(int i=0; i<NSOMPOOL; i++) {
  119. transfer_sompool[i].cmass += patch.soil.sompool[i].cmass * scale;
  120. transfer_sompool[i].fireresist += patch.soil.sompool[i].fireresist * scale;
  121. transfer_sompool[i].fracremain += patch.soil.sompool[i].fracremain * scale;
  122. transfer_sompool[i].ligcfrac += patch.soil.sompool[i].ligcfrac * scale;
  123. transfer_sompool[i].litterme += patch.soil.sompool[i].litterme * scale;
  124. transfer_sompool[i].nmass += patch.soil.sompool[i].nmass * scale;
  125. if (patch.soil.sompool[i].ntoc < 1e-20) patch.soil.sompool[i].ntoc = 0.0; // ecev3 - Peter Anthoni (KIT) - prevent SIGFPE
  126. transfer_sompool[i].ntoc += patch.soil.sompool[i].ntoc * scale;
  127. }
  128. transfer_nmass_avail += patch.soil.nmass_avail * scale;
  129. // sum wcont:
  130. for(int i=0; i<NSOILLAYER; i++) {
  131. transfer_wcont[i] += patch.soil.wcont[i] * scale;
  132. }
  133. transfer_wcont_evap += patch.soil.wcont_evap * scale;
  134. transfer_snowpack += patch.soil.snowpack * scale;
  135. transfer_snowpack_nmass += patch.soil.snowpack_nmass * scale;
  136. transfer_decomp_litter_mean += patch.soil.decomp_litter_mean * scale;
  137. transfer_k_soilfast_mean += patch.soil.k_soilfast_mean * scale;
  138. transfer_k_soilslow_mean += patch.soil.k_soilslow_mean * scale;
  139. double aaet_5_cp[NYEARAAET] = {0.0};
  140. transfer_aaet_5.to_array(aaet_5_cp);
  141. for(unsigned int i=0;i<patch.aaet_5.size();i++)
  142. transfer_aaet_5.add(aaet_5_cp[i] + patch.aaet_5[i] * scale);
  143. transfer_anfix_calc += patch.soil.anfix_calc * scale;
  144. stand.nextobj();
  145. }
  146. return;
  147. }
  148. void copy(landcover_change_transfer& from) {
  149. if(transfer_litter_leaf)
  150. memcpy(transfer_litter_leaf, from.transfer_litter_leaf, sizeof(double) * npft);
  151. if(transfer_litter_sap)
  152. memcpy(transfer_litter_sap, from.transfer_litter_sap, sizeof(double) * npft);
  153. if(transfer_litter_heart)
  154. memcpy(transfer_litter_heart, from.transfer_litter_heart, sizeof(double) * npft);
  155. if(transfer_litter_root)
  156. memcpy(transfer_litter_root, from.transfer_litter_root, sizeof(double) * npft);
  157. if(transfer_litter_repr)
  158. memcpy(transfer_litter_repr, from.transfer_litter_repr, sizeof(double) * npft);
  159. if(transfer_harvested_products_slow)
  160. memcpy(transfer_harvested_products_slow, from.transfer_harvested_products_slow, sizeof(double) * npft);
  161. if(transfer_nmass_litter_leaf)
  162. memcpy(transfer_nmass_litter_leaf, from.transfer_nmass_litter_leaf, sizeof(double) * npft);
  163. if(transfer_nmass_litter_sap)
  164. memcpy(transfer_nmass_litter_sap, from.transfer_nmass_litter_sap, sizeof(double) * npft);
  165. if(transfer_nmass_litter_heart)
  166. memcpy(transfer_nmass_litter_heart, from.transfer_nmass_litter_heart, sizeof(double) * npft);
  167. if(transfer_nmass_litter_root)
  168. memcpy(transfer_nmass_litter_root, from.transfer_nmass_litter_root, sizeof(double) * npft);
  169. if(transfer_harvested_products_slow_nmass)
  170. memcpy(transfer_harvested_products_slow_nmass, from.transfer_harvested_products_slow_nmass, sizeof(double) * npft);
  171. transfer_acflux_harvest = from.transfer_acflux_harvest;
  172. transfer_anflux_harvest = from.transfer_anflux_harvest;
  173. transfer_cpool_fast = from.transfer_cpool_fast;
  174. transfer_cpool_slow = from.transfer_cpool_slow;
  175. for(int i=0; i<NSOILLAYER; i++)
  176. transfer_wcont[i] = from.transfer_wcont[i];
  177. transfer_wcont_evap = from.transfer_wcont_evap;
  178. transfer_decomp_litter_mean = from.transfer_decomp_litter_mean;
  179. transfer_k_soilfast_mean = from.transfer_k_soilfast_mean;
  180. for(int i=0; i<NSOMPOOL; i++) {
  181. transfer_sompool[i].cmass = from.transfer_sompool[i].cmass;
  182. transfer_sompool[i].fireresist = from.transfer_sompool[i].fireresist;
  183. transfer_sompool[i].fracremain = from.transfer_sompool[i].fracremain;
  184. transfer_sompool[i].ligcfrac = from.transfer_sompool[i].ligcfrac;
  185. transfer_sompool[i].litterme = from.transfer_sompool[i].litterme;
  186. transfer_sompool[i].nmass = from.transfer_sompool[i].nmass;
  187. transfer_sompool[i].ntoc = from.transfer_sompool[i].ntoc;
  188. }
  189. transfer_nmass_avail = from.transfer_nmass_avail;
  190. transfer_snowpack = from.transfer_snowpack;
  191. transfer_snowpack_nmass = from.transfer_snowpack_nmass;
  192. for(unsigned int i=0;i<from.transfer_aaet_5.size();i++)
  193. transfer_aaet_5.add(from.transfer_aaet_5[i]);
  194. transfer_anfix_calc = from.transfer_anfix_calc;
  195. }
  196. void add(landcover_change_transfer& from, double multiplier = 1.0) {
  197. for(int i=0; i<npft; i++) {
  198. if(transfer_litter_leaf)
  199. transfer_litter_leaf[i] += from.transfer_litter_leaf[i] * multiplier;
  200. if(transfer_litter_sap)
  201. transfer_litter_sap[i] += from.transfer_litter_sap[i] * multiplier;
  202. if(transfer_litter_heart)
  203. transfer_litter_heart[i] += from.transfer_litter_heart[i] * multiplier;
  204. if(transfer_litter_root)
  205. transfer_litter_root[i] += from.transfer_litter_root[i] * multiplier;
  206. if(transfer_litter_repr)
  207. transfer_litter_repr[i] += from.transfer_litter_repr[i] * multiplier;
  208. if(transfer_harvested_products_slow)
  209. transfer_harvested_products_slow[i] += from.transfer_harvested_products_slow[i] * multiplier;
  210. if(transfer_nmass_litter_leaf)
  211. transfer_nmass_litter_leaf[i] += from.transfer_nmass_litter_leaf[i] * multiplier;
  212. if(transfer_nmass_litter_sap)
  213. transfer_nmass_litter_sap[i] += from.transfer_nmass_litter_sap[i] * multiplier;
  214. if(transfer_nmass_litter_heart)
  215. transfer_nmass_litter_heart[i] += from.transfer_nmass_litter_heart[i] * multiplier;
  216. if(transfer_nmass_litter_root)
  217. transfer_nmass_litter_root[i] += from.transfer_nmass_litter_root[i] * multiplier;
  218. if(transfer_harvested_products_slow_nmass)
  219. transfer_harvested_products_slow_nmass[i] += from.transfer_harvested_products_slow_nmass[i] * multiplier;
  220. }
  221. transfer_acflux_harvest += from.transfer_acflux_harvest * multiplier;
  222. transfer_anflux_harvest += from.transfer_anflux_harvest * multiplier;
  223. transfer_cpool_fast += from.transfer_cpool_fast * multiplier;
  224. transfer_cpool_slow += from.transfer_cpool_slow * multiplier;
  225. for(int i=0; i<NSOILLAYER; i++)
  226. transfer_wcont[i] += from.transfer_wcont[i] * multiplier;
  227. transfer_wcont_evap += from.transfer_wcont_evap * multiplier;
  228. transfer_decomp_litter_mean += from.transfer_decomp_litter_mean * multiplier;
  229. transfer_k_soilfast_mean += from.transfer_k_soilfast_mean * multiplier;
  230. for(int i=0; i<NSOMPOOL; i++) {
  231. transfer_sompool[i].cmass += from.transfer_sompool[i].cmass * multiplier;
  232. transfer_sompool[i].fireresist += from.transfer_sompool[i].fireresist * multiplier;
  233. transfer_sompool[i].fracremain += from.transfer_sompool[i].fracremain * multiplier;
  234. transfer_sompool[i].ligcfrac += from.transfer_sompool[i].ligcfrac * multiplier;
  235. transfer_sompool[i].litterme += from.transfer_sompool[i].litterme * multiplier;
  236. transfer_sompool[i].nmass += from.transfer_sompool[i].nmass * multiplier;
  237. transfer_sompool[i].ntoc += from.transfer_sompool[i].ntoc * multiplier;
  238. }
  239. transfer_nmass_avail += from.transfer_nmass_avail * multiplier;
  240. transfer_snowpack += from.transfer_snowpack * multiplier;
  241. transfer_snowpack_nmass += from.transfer_snowpack_nmass * multiplier;
  242. double aaet_5_cp[NYEARAAET] = {0.0};
  243. for(unsigned int i=0;i<transfer_aaet_5.size();i++)
  244. aaet_5_cp[i] = transfer_aaet_5[i];
  245. for(unsigned int i=0;i<from.transfer_aaet_5.size();i++)
  246. transfer_aaet_5.add(aaet_5_cp[i] + from.transfer_aaet_5[i] * multiplier);
  247. transfer_anfix_calc += from.transfer_anfix_calc * multiplier;
  248. }
  249. };
  250. struct lc_change_harvest_params {
  251. double harv_eff;
  252. double res_outtake_twig;
  253. double res_outtake_coarse_root;
  254. bool burn; // to be developed
  255. lc_change_harvest_params() {
  256. res_outtake_twig = 0.0;
  257. res_outtake_coarse_root = 0.0;
  258. burn = false;
  259. }
  260. };
  261. #endif // LPJ_GUESS_LANDCOVER_H