landcover.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file landcover.cpp
  3. /// \brief Functions handling landcover aspects, such as creating or resizing Stands
  4. ///
  5. /// Landcover change.
  6. ///
  7. /// \author Mats Lindeskog,
  8. /// \Part of code in this file as well as in cropphenology.cpp, cropallocation.cpp and
  9. /// \management.cpp based on LPJ-mL C++ code received from Alberte Bondeau in 2008.
  10. /// $Date$
  11. ///
  12. ///////////////////////////////////////////////////////////////////////////////////////
  13. #include "config.h"
  14. #include "landcover.h"
  15. #include "guessmath.h"
  16. #define CHECK_NLIM -13
  17. //#define CHECK_NLIM -8
  18. //#define CHECK_NLIM -5
  19. // prevent a check error to terminate the run, this should only be used in a test run.
  20. #define FAIL dprintf
  21. //#define FAIL fail
  22. /// Query whether a date is within a period spanned by two dates.
  23. bool dayinperiod(int day, int start, int end) {
  24. bool acrossnewyear = false;
  25. if(day < 0 || start < 0 || end < 0) // a negative value should not be a valid day
  26. return false;
  27. if(start > end)
  28. acrossnewyear = true;
  29. if(day >= start && day <= end && !acrossnewyear || (day >= start || day <= end) && acrossnewyear)
  30. return true;
  31. else
  32. return false;
  33. }
  34. /// Step n days from a date.
  35. /** Local proxy to the Date class function in order to simplify call syntax
  36. from the modules that #includes landcover.h anyway.
  37. */
  38. int stepfromdate(int day, int step) {
  39. return Date::stepfromdate(day, step);
  40. }
  41. /// Help function to access two-dimentional arrays that have been created dynamically
  42. int index(int from, int to, int ncols = nst) {
  43. return from * ncols + to;
  44. }
  45. /// Initial creation of stands when run_landcover==true based on gridcell stand type area fractions.
  46. void landcover_init(Gridcell& gridcell, InputModule* input_module) {
  47. // Set CFT-specific members of gridcellpft:
  48. for(unsigned int p = 0; p < gridcell.pft.nobj; p++) {
  49. Gridcellpft& gcpft = gridcell.pft[p];
  50. if (gridcell.get_lat() >= 0.0) {
  51. gcpft.sdate_default = gcpft.pft.sdatenh;
  52. gcpft.hlimitdate_default = gcpft.pft.hlimitdatenh;
  53. }
  54. else {
  55. gcpft.sdate_default = gcpft.pft.sdatesh;
  56. gcpft.hlimitdate_default = gcpft.pft.hlimitdatesh;
  57. }
  58. }
  59. // get landcover and crop area fractions from landcover input file(s) or ins-file.
  60. input_module->getlandcover(gridcell);
  61. stlist.firstobj();
  62. while (stlist.isobj) {
  63. StandType& st = stlist.getobj();
  64. Gridcellst& gcst = gridcell.st[st.id];
  65. gcst.frac_old = gcst.frac;
  66. if(gcst.frac > 0.0) {
  67. gridcell.create_stand_lu(st, gcst.frac);
  68. }
  69. stlist.nextobj();
  70. }
  71. }
  72. enum {SECONDARY_MATURE, SECONDARY_YOUNG, PRIMARY, NFORESTCLASSES};
  73. /// identifies which stands to reduce in area and sets standtype.nstands
  74. /** Updates frac, frac_change, frac_old and gross_frac_decrease for reduced stands
  75. * Updates frac_old and frac_temp for all stands
  76. *
  77. * INTPUT PARAMETERS
  78. *
  79. * \param st_frac_transfer array with this year's transferred area fractions between the different stand types
  80. */
  81. void reduce_stands(Gridcell& gridcell, double* st_frac_transfer, forest_st_frac_transfer& forest_st_frac_transfer_s) {
  82. for(unsigned int i = 0; i < gridcell.st.nobj; i++) {
  83. Gridcellst& gcst = gridcell.st[i];
  84. gcst.nstands = 0;
  85. }
  86. for(unsigned int i = 0; i < gridcell.nbr_stands(); i++) {
  87. Stand& stand = gridcell[i];
  88. stand.frac_old = stand.get_gridcell_fraction();
  89. stand.frac_temp = stand.frac_old;
  90. stand.frac_change = 0.0;
  91. stand.gross_frac_increase = 0.0;
  92. stand.gross_frac_decrease = 0.0;
  93. stand.cloned_fraction = 0.0;
  94. if(stand.transfer_area_st) {
  95. for(int i=0;i<nst;i++)
  96. stand.transfer_area_st[i] = 0.0;
  97. }
  98. gridcell.st[stand.stid].nstands++;
  99. }
  100. for(int from=0; from<nst; from++) {
  101. StandType& st = stlist[from];
  102. Gridcellst& gcst = gridcell.st[from];
  103. if(gcst.gross_frac_decrease > 0.0) {
  104. if(gcst.nstands > 1) {
  105. /* Rules for selecting stands:
  106. natural to cropland: young_stands_first=true
  107. ifprimary_lc_transfer:
  108. secondary mature lap: young_stands_first=true, secondary_harvest=true (will use young -> old secondary stands over age limit)
  109. restart: young_stands_first=false, secondary_harvest=false, ignore age_limit_reduce (will use primary stand before age-limited stands)
  110. (secondary young lap): young_stands_first=true, secondary_harvest=true (will use young -> old secondary stands over age limit)
  111. restart: young_stands_first=true, secondary_harvest=false, ignore age_limit_reduce (will use primary stand before age-limited stands)
  112. primary lap: young_stands_first=false, secondary_harvest=false (will use old -> young secondary stands if no more primary stand)
  113. restart: young_stands_first=false, secondary_harvest=false, ignore age_limit_reduce (will use age-limited stands)
  114. natural to natural: young_stands_first=false
  115. ifprimary_lc_transfer:
  116. secondary mature lap: young_stands_first=false, secondary_harvest=true (will use old -> young secondary stands over age limit)
  117. restart: young_stands_first=false, secondary_harvest=false, ignore age_limit_reduce (will use primary stand before age-limited stands)
  118. secondary young lap: young_stands_first=true, secondary_harvest=true (will use young -> old secondary stands over age limit)
  119. restart: young_stands_first=true, secondary_harvest=false, ignore age_limit_reduce (will use age-limited stands before primary stand)
  120. primary lap: young_stands_first=false, secondary_harvest=false (will use old -> young secondary stands if no more primary stand)
  121. restart: young_stands_first=false, secondary_harvest=false, ignore age_limit_reduce (will use age-limited stands)
  122. */
  123. int nlaps = 1;
  124. if(use_primary_lc_transfer)
  125. nlaps = NFORESTCLASSES;
  126. for(int n=0; n<nlaps; n++) {
  127. for(int to=0; to<nst; to++) {
  128. if(st_frac_transfer[index(from, to)] > 0.0) {
  129. double st_change_remain = -st_frac_transfer[index(from, to)];
  130. bool secondary_harvest = false;
  131. bool young_stands_first = true; //convert area from youngest stands first
  132. if(st.landcover == NATURAL || st.landcover == FOREST) {
  133. if(stlist[to].landcover == NATURAL || stlist[to].landcover == FOREST)
  134. young_stands_first = false;
  135. else
  136. young_stands_first = true;
  137. }
  138. if(use_primary_lc_transfer) {
  139. if(n == SECONDARY_MATURE) {
  140. // First reduce secondary (mature) stands (fraction not in primary_st_frac_transfer array + secondary_young_st_frac_transfer[index(from, to))
  141. st_change_remain += forest_st_frac_transfer_s.primary[index(from, to)] + forest_st_frac_transfer_s.secondary_young[index(from, to)];
  142. secondary_harvest = true;
  143. } else if(n == SECONDARY_YOUNG) {
  144. // Then reduce young secondary stands
  145. st_change_remain = -forest_st_frac_transfer_s.secondary_young[index(from, to)];
  146. young_stands_first = true;
  147. secondary_harvest = true;
  148. }
  149. else if(n == PRIMARY) {
  150. // Finally, reduce primary stands (fraction in primary_st_frac_transfer array)
  151. st_change_remain = -forest_st_frac_transfer_s.primary[index(from, to)];
  152. young_stands_first = false;
  153. }
  154. }
  155. bool restart = false;
  156. while(st_change_remain < 0.0) {
  157. int count_st = 0;
  158. for(unsigned int i = 0; i < gridcell.size(); i++) {
  159. int index;
  160. double stands_frac_sum = 0.0;
  161. for(unsigned int j = 0; j < gridcell.nbr_stands(); j++) {
  162. Stand& stand = gridcell[j];
  163. if(stand.stid == st.id)
  164. stands_frac_sum += stand.get_gridcell_fraction();
  165. }
  166. if(st_change_remain > -INPUT_RESOLUTION * 0.1 || stands_frac_sum == 0.0) {
  167. if(st_change_remain <= -INPUT_RESOLUTION && stands_frac_sum == 0.0)
  168. dprintf("\nWarning: no more stand area left of stand type %d ! Residual reduction demand %.15f ignored.\n", st.id, -st_change_remain);
  169. st_change_remain = 0.0;
  170. break;
  171. }
  172. if(young_stands_first)
  173. index = (int) gridcell.size() - 1 - i;
  174. else
  175. index = i;
  176. Stand& stand = gridcell[index];
  177. if(stand.stid == st.id) {
  178. count_st++;
  179. int first_year = max(stand.first_year, stand.clone_year);
  180. if(secondary_harvest && first_year == 0)
  181. continue;
  182. // Don't reduce stands younger than the age limit, unless this is the last stand in the loop or the initial stand has been killed
  183. if(date.year - first_year < age_limit_reduce && !reduce_all_stands && !restart)
  184. continue;
  185. // convert equal percentage of areas from all stands
  186. if(reduce_all_stands) {
  187. stand.frac_change += st_change_remain * stand.get_gridcell_fraction() / gcst.frac;
  188. stand.gross_frac_decrease -= st_change_remain * stand.get_gridcell_fraction() / gcst.frac;
  189. stand.transfer_area_st[to] = -st_change_remain * stand.get_gridcell_fraction() / gcst.frac;
  190. stand.set_gridcell_fraction(stand.get_gridcell_fraction() + st_change_remain * stand.get_gridcell_fraction() / gcst.frac);
  191. }
  192. else {
  193. if(stand.get_gridcell_fraction() > 0.0) {
  194. //all natural landcover decrease is taken from this stand
  195. if(stand.get_gridcell_fraction() >= -st_change_remain) {
  196. stand.frac_change += st_change_remain;
  197. stand.gross_frac_decrease -= st_change_remain;
  198. stand.transfer_area_st[to] -= st_change_remain;
  199. stand.set_gridcell_fraction(stand.get_gridcell_fraction() + st_change_remain);
  200. if(stand.get_gridcell_fraction() < INPUT_RESOLUTION * 0.1) {
  201. gridcell.landcover.acflux_landuse_change += stand.ccont() * stand.get_gridcell_fraction();
  202. gridcell.landcover.anflux_landuse_change += stand.ncont() * stand.get_gridcell_fraction();
  203. stand.set_gridcell_fraction(0.0);
  204. }
  205. st_change_remain = 0.0;
  206. break;
  207. }
  208. //more stands will have to be reduced
  209. else {
  210. stand.frac_change -= stand.get_gridcell_fraction();
  211. stand.gross_frac_decrease += stand.get_gridcell_fraction();
  212. stand.transfer_area_st[to] += stand.get_gridcell_fraction();
  213. st_change_remain += stand.get_gridcell_fraction();
  214. stand.set_gridcell_fraction(0.0); //will be killed below
  215. }
  216. }
  217. }
  218. }
  219. }
  220. // Restart loop and reduce oldest stand first if the rules above precluded reduction of the required area.
  221. if(st_change_remain < 0.0) {
  222. if(reduce_all_stands) {
  223. st_change_remain = 0.0;
  224. }
  225. else {
  226. if(n != SECONDARY_YOUNG)
  227. young_stands_first = false;
  228. secondary_harvest = false;
  229. restart = true;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. else if(gcst.nstands == 1 ) {
  238. for(unsigned int i = 0; i < gridcell.size(); i++) {
  239. Stand& stand = gridcell[i];
  240. if(stand.stid == gcst.id) {
  241. bool expand_to_new_stand = gridcell.landcover.expand_to_new_stand[stand.landcover];
  242. if(gcst.gross_frac_decrease > stand.get_gridcell_fraction()) {
  243. if((gcst.gross_frac_decrease - stand.get_gridcell_fraction() > INPUT_RESOLUTION))
  244. dprintf("\nYear %d: Warning. no more stand area left of stand type %d ! Residual reduction demand %.15f ignored.\n", date.year, stand.stid, gcst.gross_frac_decrease - stand.get_gridcell_fraction());
  245. gcst.gross_frac_decrease = stand.get_gridcell_fraction();
  246. }
  247. stand.frac_change = -gcst.gross_frac_decrease;
  248. stand.gross_frac_decrease = gcst.gross_frac_decrease;
  249. stand.set_gridcell_fraction(stand.get_gridcell_fraction() + stand.frac_change);
  250. if(stand.get_gridcell_fraction() < INPUT_RESOLUTION && (!gcst.gross_frac_increase || expand_to_new_stand)) {
  251. gridcell.landcover.acflux_landuse_change += stand.ccont() * stand.get_gridcell_fraction();
  252. gridcell.landcover.anflux_landuse_change += stand.ncont() * stand.get_gridcell_fraction();
  253. stand.set_gridcell_fraction(0.0);
  254. }
  255. for(int to=0; to<nst; to++) {
  256. if(st_frac_transfer[index(st.id, to)])
  257. stand.transfer_area_st[to] = st_frac_transfer[index(st.id, to)];
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. for(unsigned int j = 0; j < gridcell.nbr_stands(); j++) {
  265. Stand& stand = gridcell[j];
  266. StandType& st = stlist[stand.stid];
  267. Gridcellst& gcst = gridcell.st[stand.stid];
  268. if(!gcst.frac && stand.get_gridcell_fraction() < INPUT_RESOLUTION * 100.0) {
  269. if(stand.get_gridcell_fraction() > INPUT_RESOLUTION * 10.0)
  270. dprintf("\nYear %d: remaining stand when stand type %d fraction is 0. Residual fraction %.15f ignored. Stand killed.\n", date.year, st.id, stand.get_gridcell_fraction());
  271. gridcell.landcover.acflux_landuse_change += stand.ccont() * stand.get_gridcell_fraction();
  272. gridcell.landcover.anflux_landuse_change += stand.ncont() * stand.get_gridcell_fraction();
  273. stand.set_gridcell_fraction(0.0);
  274. }
  275. }
  276. }
  277. /// identifies which stands to expand in area
  278. /** Updates frac, frac_change, frac_old and gross_frac_increase for expanded stands
  279. * Should be preceded by a call to reduce_stands() and, optionally, transfer_to_new_stand()
  280. *
  281. * INTPUT PARAMETERS
  282. *
  283. * \param st_frac_transfer array with this year's transferred area fractions between the different stand types
  284. */
  285. void expand_stands(Gridcell& gridcell, double* st_frac_transfer) {
  286. // Updated variables are zeroed in reduce_stands()
  287. for(int i=0; i<nst; i++) {
  288. StandType& st = stlist[i];
  289. Gridcellst& gcst = gridcell.st[i];
  290. landcovertype lc = st.landcover;
  291. bool expand_to_new_stand = gridcell.landcover.expand_to_new_stand[lc];
  292. if(gcst.gross_frac_increase > 0.0) { // Not cloned stands
  293. if(!expand_to_new_stand) {
  294. for(unsigned int i = 0; i < gridcell.size(); i++) {
  295. Stand& stand = gridcell[i];
  296. if(stand.stid == st.id) {
  297. // Identify which stands to expand (not secondary stands)
  298. if(!stand.cloned) {
  299. stand.frac_change += gcst.gross_frac_increase;
  300. stand.gross_frac_increase = gcst.gross_frac_increase;
  301. stand.set_gridcell_fraction(stand.get_gridcell_fraction() + gcst.gross_frac_increase);
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. /// sets land cover transfer matrix from gross land cover change data when no input is available
  310. /** Uses rules to select preferred transfers between land covers
  311. * NB. New land cover types must be included in the preference arrays !
  312. * Also, PEATLAND, URBAN and BARREN needs to be included when using dynamic fractions for these land cover types.
  313. * INPUT PARAMETERS
  314. * \param landcoverfrac_change array with this year's difference in area fractions of the different landcovers
  315. *
  316. * OUTPUT PARAMETERS
  317. *
  318. * \param lc_frac_transfer array with this year's transferred area fractions between the different land covers
  319. */
  320. void set_lc_change_array(double landcoverfrac_change[], double lc_frac_transfer[][NLANDCOVERTYPES]) {
  321. const int NRANK = 6;
  322. int target_preference[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0};
  323. int origin_preference[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0};
  324. double receptor_remain[NLANDCOVERTYPES] = {0.0};
  325. double donor_remain[NLANDCOVERTYPES] = {0.0};
  326. int ndonor_lc = 0;
  327. int nreceptor_lc = 0;
  328. target_preference[CROPLAND][PASTURE] = 5;
  329. target_preference[CROPLAND][NATURAL] = 6;
  330. target_preference[CROPLAND][FOREST] = 4;
  331. target_preference[CROPLAND][URBAN] = 3;
  332. target_preference[CROPLAND][BARREN] = 2;
  333. target_preference[CROPLAND][PEATLAND] = 1;
  334. target_preference[PASTURE][CROPLAND] = 4;
  335. target_preference[PASTURE][NATURAL] = 6;
  336. target_preference[PASTURE][FOREST] = 5;
  337. target_preference[PASTURE][URBAN] = 3;
  338. target_preference[PASTURE][BARREN] = 1;
  339. target_preference[PASTURE][PEATLAND] = 2;
  340. target_preference[FOREST][CROPLAND] = 4;
  341. target_preference[FOREST][PASTURE] = 5;
  342. target_preference[FOREST][NATURAL] = 6;
  343. target_preference[FOREST][URBAN] = 2;
  344. target_preference[FOREST][BARREN] = 1;
  345. target_preference[FOREST][PEATLAND] = 3;
  346. target_preference[NATURAL][CROPLAND] = 4;
  347. target_preference[NATURAL][PASTURE] = 5;
  348. target_preference[NATURAL][FOREST] = 6;
  349. target_preference[NATURAL][URBAN] = 2;
  350. target_preference[NATURAL][BARREN] = 1;
  351. target_preference[NATURAL][PEATLAND] = 3;
  352. target_preference[URBAN][CROPLAND] = 5;
  353. target_preference[URBAN][PASTURE] = 4;
  354. target_preference[URBAN][FOREST] = 2;
  355. target_preference[URBAN][NATURAL] = 3;
  356. target_preference[URBAN][BARREN] = 6;
  357. target_preference[URBAN][PEATLAND] = 1;
  358. target_preference[BARREN][CROPLAND] = 4;
  359. target_preference[BARREN][PASTURE] = 5;
  360. target_preference[BARREN][FOREST] = 2;
  361. target_preference[BARREN][NATURAL] = 6;
  362. target_preference[BARREN][URBAN] = 1;
  363. target_preference[BARREN][PEATLAND] = 3;
  364. origin_preference[PASTURE][CROPLAND] = 5;
  365. origin_preference[NATURAL][CROPLAND] = 6;
  366. origin_preference[FOREST][CROPLAND] = 3;
  367. origin_preference[BARREN][CROPLAND] = 1;
  368. origin_preference[URBAN][CROPLAND] = 2;
  369. origin_preference[PEATLAND][CROPLAND] = 4;
  370. origin_preference[CROPLAND][PASTURE] = 5;
  371. origin_preference[NATURAL][PASTURE] = 6;
  372. origin_preference[FOREST][PASTURE] = 3;
  373. origin_preference[BARREN][PASTURE] = 1;
  374. origin_preference[URBAN][PASTURE] = 2;
  375. origin_preference[PEATLAND][PASTURE] = 4;
  376. origin_preference[CROPLAND][NATURAL] = 5;
  377. origin_preference[PASTURE][NATURAL] = 6;
  378. origin_preference[FOREST][NATURAL] = 4;
  379. origin_preference[BARREN][NATURAL] = 2;
  380. origin_preference[URBAN][NATURAL] = 1;
  381. origin_preference[PEATLAND][NATURAL] = 3;
  382. origin_preference[CROPLAND][FOREST] = 4;
  383. origin_preference[PASTURE][FOREST] = 5;
  384. origin_preference[NATURAL][FOREST] = 6;
  385. origin_preference[BARREN][FOREST] = 2;
  386. origin_preference[URBAN][FOREST] = 1;
  387. origin_preference[PEATLAND][FOREST] = 3;
  388. origin_preference[CROPLAND][URBAN] = 6;
  389. origin_preference[PASTURE][URBAN] = 5;
  390. origin_preference[NATURAL][URBAN] = 4;
  391. origin_preference[BARREN][URBAN] = 3;
  392. origin_preference[FOREST][URBAN] = 1;
  393. origin_preference[PEATLAND][URBAN] = 2;
  394. origin_preference[CROPLAND][PEATLAND] = 2;
  395. origin_preference[PASTURE][PEATLAND] = 4;
  396. origin_preference[NATURAL][PEATLAND] = 5;
  397. origin_preference[BARREN][PEATLAND] = 6;
  398. origin_preference[FOREST][PEATLAND] = 3;
  399. origin_preference[URBAN][PEATLAND] = 1;
  400. origin_preference[CROPLAND][BARREN] = 6;
  401. origin_preference[PASTURE][BARREN] = 5;
  402. origin_preference[NATURAL][BARREN] = 4;
  403. origin_preference[URBAN][BARREN] = 3;
  404. origin_preference[FOREST][BARREN] = 1;
  405. origin_preference[PEATLAND][BARREN] = 2;
  406. for(int i=0; i<NLANDCOVERTYPES; i++) {
  407. if(landcoverfrac_change[i] > 0.0) {
  408. receptor_remain[i] = landcoverfrac_change[i];
  409. nreceptor_lc++;
  410. }
  411. else if(landcoverfrac_change[i] < 0.0){
  412. donor_remain[i] = -landcoverfrac_change[i];
  413. ndonor_lc++;
  414. }
  415. }
  416. // Simplest cases: no ambiguities
  417. if(ndonor_lc == 1 || nreceptor_lc == 1) {
  418. for(int from=0; from<NLANDCOVERTYPES; from++) {
  419. if(landcoverfrac_change[from] < 0.0) {
  420. for(int to=0; to<NLANDCOVERTYPES; to++) {
  421. if(ndonor_lc == 1) {
  422. if(landcoverfrac_change[to] > 0.0)
  423. lc_frac_transfer[from][to] += landcoverfrac_change[to];
  424. }
  425. else if(nreceptor_lc == 1) {
  426. if(landcoverfrac_change[to] > 0.0)
  427. lc_frac_transfer[from][to] += - landcoverfrac_change[from];
  428. }
  429. }
  430. }
  431. }
  432. }
  433. else {
  434. for(int score=NRANK*2; score>0; score--) {
  435. for(int from=0; from<NLANDCOVERTYPES; from++) {
  436. if(donor_remain[from] > INPUT_RESOLUTION * 0.1) {
  437. for(int to=0; to<NLANDCOVERTYPES; to++) {
  438. // Identify receiving land covers:
  439. if(target_preference[from][to] + origin_preference[from][to] == score && receptor_remain[to] > INPUT_RESOLUTION * 0.1 && donor_remain[from] > INPUT_RESOLUTION * 0.1) {
  440. // all donor land is put into this land cover
  441. if(receptor_remain[to] >= donor_remain[from]) {
  442. lc_frac_transfer[from][to] += donor_remain[from];
  443. receptor_remain[to] -= donor_remain[from];
  444. donor_remain[from] = 0.0;
  445. break;
  446. }
  447. // transfer to more land cover types
  448. else {
  449. lc_frac_transfer[from][to] += receptor_remain[to];
  450. donor_remain[from] -= receptor_remain[to];
  451. receptor_remain[to] = 0.0;
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. /// sets stand type transfer matrix from land cover transfer matrix when no stand type transfer input is available
  461. /** Distributes land cover transfers equally between stand types within a land cover but may use
  462. * rules to select preferred transfers between stand types/land covers (see set_lc_change_array()) if
  463. * equal_distribution is set to false.
  464. *
  465. * INPUT PARAMETERS
  466. * \param lc_frac_transfer array with this year's transferred area fractions between the different land covers
  467. *
  468. * OUTPUT PARAMETERS
  469. *
  470. * \param st_frac_transfer array with this year's transferred area fractions between the different stand types
  471. */
  472. void set_st_change_array(Gridcell& gridcell, double lc_frac_transfer[][NLANDCOVERTYPES], double* st_frac_transfer, forest_lc_frac_transfer& forest_lc_frac_transfer_s, forest_st_frac_transfer& forest_st_frac_transfer_s) {
  473. double* recip_donor_remain = new double[nst];
  474. double* recip_receptor_remain = new double[nst];
  475. double recip_lc_change[NLANDCOVERTYPES] = {0.0};
  476. double recip_lc_frac_transfer[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  477. double recip_transfer_remain[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  478. double net_transfer_remain[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  479. double net_lc_decrease[NLANDCOVERTYPES] = {0.0};
  480. for(int i=0;i<nst;i++) {
  481. recip_donor_remain[i] = 0.0;
  482. recip_receptor_remain[i] = 0.0;
  483. }
  484. // Quantify "reciprocal" lc change (gross lc change - net lc change)
  485. for(int from=0; from<NLANDCOVERTYPES; from++) {
  486. for(int to=0; to<NLANDCOVERTYPES; to++) {
  487. recip_lc_frac_transfer[from][to] = min(lc_frac_transfer[from][to], lc_frac_transfer[to][from]);
  488. recip_transfer_remain[from][to] = recip_lc_frac_transfer[from][to];
  489. recip_lc_change[from] += recip_lc_frac_transfer[from][to];
  490. net_transfer_remain[from][to] = lc_frac_transfer[from][to] - recip_lc_frac_transfer[from][to];
  491. net_lc_decrease[from] += net_transfer_remain[from][to];
  492. }
  493. }
  494. // Net land cover change
  495. int nsts[NLANDCOVERTYPES] = {0};
  496. int nsts_active[NLANDCOVERTYPES] = {0};
  497. bool multi_st = false;
  498. for(int lc=0; lc<NLANDCOVERTYPES; lc++) {
  499. for(int i=0; i<nst; i++) {
  500. if(lc == stlist[i].landcover) {
  501. nsts[lc]++;
  502. if(gridcell.st[i].frac || gridcell.st[i].frac_old)
  503. nsts_active[lc]++;
  504. }
  505. }
  506. if(nsts[lc] > 1)
  507. multi_st = true;
  508. }
  509. // Net transfers between land covers with only one stand type.
  510. for(int from=0; from<nst; from++) {
  511. StandType& st_donor = stlist[from];
  512. for(int to=0; to<nst; to++) {
  513. StandType& st_receptor = stlist[to];
  514. if(nsts[st_receptor.landcover] < 2 && nsts[st_donor.landcover] < 2 && net_transfer_remain[st_donor.landcover][st_receptor.landcover] > INPUT_RESOLUTION * 0.1) {
  515. st_frac_transfer[index(from, to)] += net_transfer_remain[st_donor.landcover][st_receptor.landcover];
  516. net_transfer_remain[st_donor.landcover][st_receptor.landcover] = 0.0;
  517. }
  518. }
  519. }
  520. // Net transfers to and from land covers that have several stand types.
  521. if(multi_st) {
  522. double net_lc_receptor_remain[NLANDCOVERTYPES] = {0.0};
  523. double net_transfer_remain_initial[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  524. for(int from=0; from<NLANDCOVERTYPES; from++) {
  525. for(int to=0; to<NLANDCOVERTYPES; to++) {
  526. net_lc_receptor_remain[to] += net_transfer_remain[from][to];
  527. net_transfer_remain_initial[from][to] = net_transfer_remain[from][to];
  528. }
  529. }
  530. double* influx_st = new double[nst];
  531. double* outflux_st = new double[nst];
  532. for(int i=0;i<nst;i++) {
  533. influx_st[i] = 0.0;
  534. outflux_st[i] = 0.0;
  535. }
  536. for(int from=0; from<nst; from++) {
  537. for(int to=0; to<nst; to++) {
  538. if(st_frac_transfer[index(from, to)] > 0.0) {
  539. influx_st[to] += st_frac_transfer[index(from, to)];
  540. outflux_st[from] += st_frac_transfer[index(from, to)];
  541. }
  542. }
  543. }
  544. // Transfer from land covers with more than one stand type:
  545. double* reduce = new double[nst];
  546. double* receptor_weight = new double[nst];
  547. for(int i=0;i<nst;i++) {
  548. reduce[i] = 0.0;
  549. receptor_weight[i] = 0.0;
  550. }
  551. double exclude_frac = 0.0;
  552. double exclude_frac_0 = 0.0;
  553. bool recalc_increase = false;
  554. for(int from=0; from<nst; from++) {
  555. StandType& st_donor = stlist[from];
  556. Gridcellst& gcst_donor = gridcell.st[from];
  557. if(gridcell.landcover.frac[st_donor.landcover])
  558. receptor_weight[from] = gcst_donor.frac / gridcell.landcover.frac[st_donor.landcover];
  559. }
  560. for(int from_lc=0; from_lc<NLANDCOVERTYPES; from_lc++) {
  561. double negative = 0.0;
  562. double reducable_frac = 0.0;
  563. bool recalc_reduce = false;
  564. if(!net_lc_decrease[from_lc])
  565. continue;
  566. for(int from=0; from<nst; from++) {
  567. StandType& st_donor = stlist[from];
  568. Gridcellst& gcst_donor = gridcell.st[from];
  569. if(st_donor.landcover == from_lc && nsts[st_donor.landcover] >=2) {
  570. double net_st_increase = net_lc_receptor_remain[st_donor.landcover] * receptor_weight[from];
  571. double net_st_decrease = net_st_increase - gcst_donor.frac_change;
  572. reduce[from] = (influx_st[from] - outflux_st[from]) + net_st_decrease;
  573. if(reduce[from] < 0.0) {
  574. negative += -reduce[from];
  575. recalc_reduce = true;
  576. }
  577. else {
  578. reducable_frac += reduce[from];
  579. }
  580. double remain = reduce[from] - gcst_donor.frac_old;
  581. if(remain > INPUT_RESOLUTION * 0.1) {
  582. exclude_frac_0 += gcst_donor.frac;
  583. exclude_frac += receptor_weight[from] * net_lc_receptor_remain[st_donor.landcover] - remain;
  584. if(net_lc_receptor_remain[st_donor.landcover])
  585. receptor_weight[from] -= remain / net_lc_receptor_remain[st_donor.landcover];
  586. reduce[from] = gcst_donor.frac_old;
  587. recalc_increase = true;
  588. }
  589. }
  590. }
  591. if(recalc_reduce) {
  592. for(int from=0; from<nst; from++) {
  593. StandType& st_donor = stlist[from];
  594. Gridcellst& gcst_donor = gridcell.st[from];
  595. if(st_donor.landcover == from_lc && reducable_frac && reduce[from] > 0.0) {
  596. reduce[from] = max(0.0, reduce[from] - negative * reduce[from] / reducable_frac);
  597. }
  598. else {
  599. reduce[from] = 0.0;
  600. }
  601. }
  602. }
  603. if(recalc_increase) {
  604. for(int from=0; from<nst; from++) {
  605. StandType& st_donor = stlist[from];
  606. Gridcellst& gcst_donor = gridcell.st[from];
  607. if(st_donor.landcover == from_lc) {
  608. if(receptor_weight[from] == gcst_donor.frac / gridcell.landcover.frac[st_donor.landcover] && gridcell.landcover.frac[st_donor.landcover] != exclude_frac_0) {
  609. receptor_weight[from] = gcst_donor.frac / (gridcell.landcover.frac[st_donor.landcover] - exclude_frac_0) * (net_lc_receptor_remain[st_donor.landcover] - exclude_frac) / net_lc_receptor_remain[st_donor.landcover];
  610. double net_st_increase = net_lc_receptor_remain[st_donor.landcover] * receptor_weight[from];
  611. double net_st_decrease = net_st_increase - gcst_donor.frac_change;
  612. reduce[from] = (influx_st[from] - outflux_st[from]) + net_st_decrease;
  613. }
  614. }
  615. }
  616. }
  617. }
  618. for(int from=0; from<NLANDCOVERTYPES; from++) {
  619. for(int to=0; to<NLANDCOVERTYPES; to++) {
  620. if(nsts[from] >=2 && net_transfer_remain[from][to] > INPUT_RESOLUTION * 0.1) {
  621. for(int i=0; i<nst; i++) {
  622. StandType& st_donor = stlist[i];
  623. if(st_donor.landcover == from && gridcell.landcover.frac_old[from]) {
  624. for(int j=0; j<nst; j++) {
  625. StandType& st_receptor = stlist[j];
  626. Gridcellst& gcst_receptor = gridcell.st[j];
  627. if(st_receptor.landcover == to && net_lc_decrease[from] && gridcell.landcover.frac[to]) {
  628. // This shouldn't happen really...
  629. if(reduce[i] < 0.0)
  630. dprintf("Year %d: st %d reduce[from] = %.15f !\n", date.get_calendar_year(), i, reduce[i]);
  631. double transfer = reduce[i] * net_transfer_remain_initial[from][to] / net_lc_decrease[from] * (gcst_receptor.frac / gridcell.landcover.frac[to]);
  632. st_frac_transfer[index(i, j)] += transfer;
  633. net_transfer_remain[from][to] -= transfer;
  634. net_lc_receptor_remain[to] -= transfer;
  635. }
  636. }
  637. }
  638. }
  639. net_transfer_remain[from][to] = 0.0;
  640. net_transfer_remain_initial[from][to] = 0.0;
  641. }
  642. }
  643. }
  644. // Transfer to land covers with more than one stand type:
  645. for(int from=0; from<nst; from++) {
  646. StandType& st_donor = stlist[from];
  647. Gridcellst& gcst_donor = gridcell.st[from];
  648. for(int to=0; to<nst; to++) {
  649. StandType& st_receptor = stlist[to];
  650. if(nsts[st_receptor.landcover] >= 2 && net_transfer_remain_initial[st_donor.landcover][st_receptor.landcover] > INPUT_RESOLUTION * 0.1
  651. && gridcell.landcover.frac_old[st_donor.landcover]) {
  652. double transfer = net_transfer_remain_initial[st_donor.landcover][st_receptor.landcover] * receptor_weight[to] * gcst_donor.frac_old / gridcell.landcover.frac_old[st_donor.landcover];
  653. st_frac_transfer[index(from, to)] += transfer;
  654. net_transfer_remain[st_donor.landcover][st_receptor.landcover] -= transfer;
  655. }
  656. }
  657. }
  658. if(influx_st)
  659. delete[] influx_st;
  660. if(outflux_st)
  661. delete[] outflux_st;
  662. if(reduce)
  663. delete[] reduce;
  664. if(receptor_weight)
  665. delete[] receptor_weight;
  666. }
  667. // Add "reciprocal" lc change (gross-net)
  668. // Also transitions from primary to secondary natural stands are done here
  669. double* donated_st = new double[nst];
  670. double* received_st = new double[nst];
  671. for(int i=0; i<nst; i++) {
  672. donated_st[i] = 0.0;
  673. received_st[i] = 0.0;
  674. }
  675. for(int from=0; from<nst; from++) {
  676. for(int to=0; to<nst; to++) {
  677. donated_st[from] += st_frac_transfer[index(from, to)];
  678. received_st[to] += st_frac_transfer[index(from, to)];
  679. }
  680. }
  681. double max_transfer_lc[NLANDCOVERTYPES] = {0.0};
  682. double max_donor_lc[NLANDCOVERTYPES] = {0.0};
  683. double max_receptor_lc[NLANDCOVERTYPES] = {0.0};
  684. for(int i=0; i<nst; i++) {
  685. StandType& st = stlist[i];
  686. Gridcellst& gcst = gridcell.st[i];
  687. max_transfer_lc[st.landcover] += min(gcst.frac - received_st[i], gcst.frac_old - donated_st[i]);
  688. max_receptor_lc[st.landcover] += gcst.frac - received_st[i];
  689. max_donor_lc[st.landcover] += gcst.frac_old - donated_st[i];
  690. }
  691. for(int lc=0; lc<NLANDCOVERTYPES; lc++) {
  692. if(!recip_lc_change[lc])
  693. continue;
  694. if(max_transfer_lc[lc] >= recip_lc_change[lc]) {
  695. for(int i=0; i<nst; i++) {
  696. StandType& st = stlist[i];
  697. Gridcellst& gcst = gridcell.st[i];
  698. if(st.landcover == lc && recip_lc_change[st.landcover] > 0.0) {
  699. double max_transfer_st = min(gcst.frac - received_st[i], gcst.frac_old - donated_st[i]);
  700. if(max_transfer_lc[st.landcover]) {
  701. recip_receptor_remain[i] = recip_lc_change[st.landcover] * max_transfer_st / max_transfer_lc[st.landcover];
  702. recip_donor_remain[i] = recip_lc_change[st.landcover] * max_transfer_st / max_transfer_lc[st.landcover];
  703. if(gcst.frac_old - donated_st[i] - recip_donor_remain[i] < -1e-13) {
  704. dprintf("Year %d: not enough room for reciprocal transfer for st %d, %g missing\n", date.get_calendar_year(), i, -(gcst.frac_old - donated_st[i] - recip_donor_remain[i]));
  705. }
  706. if(gcst.frac - received_st[i] - recip_receptor_remain[i] < -1e-13) {
  707. dprintf("Year %d: not enough room for reciprocal transfer for st %d, %g missing\n", date.get_calendar_year(), i, -(gcst.frac - received_st[i] - recip_receptor_remain[i]));
  708. }
  709. }
  710. }
  711. }
  712. }
  713. else {
  714. for(int i=0; i<nst; i++) {
  715. StandType& st = stlist[i];
  716. Gridcellst& gcst = gridcell.st[i];
  717. if(st.landcover == lc) {
  718. if(max_receptor_lc[st.landcover]) {
  719. recip_receptor_remain[i] = recip_lc_change[st.landcover] * (gcst.frac - received_st[i]) / max_receptor_lc[st.landcover];
  720. }
  721. if(max_donor_lc[st.landcover]) {
  722. recip_donor_remain[i] = recip_lc_change[st.landcover] * (gcst.frac_old - donated_st[i]) / max_donor_lc[st.landcover];
  723. }
  724. }
  725. }
  726. }
  727. }
  728. if(donated_st)
  729. delete[] donated_st;
  730. if(received_st)
  731. delete[] received_st;
  732. for(int from=0; from<nst; from++) {
  733. StandType& st_donor = stlist[from];
  734. Gridcellst& gcst_donor = gridcell.st[from];
  735. if(recip_donor_remain[from] > INPUT_RESOLUTION * 0.1) {
  736. for(int to=0; to<nst; to++) {
  737. StandType& st_receptor = stlist[to];
  738. Gridcellst& gcst_receptor = gridcell.st[to];
  739. if(recip_transfer_remain[st_donor.landcover][st_receptor.landcover] > INPUT_RESOLUTION * 0.1
  740. && recip_receptor_remain[to] > INPUT_RESOLUTION * 0.1 && recip_donor_remain[from] > INPUT_RESOLUTION * 0.1) {
  741. double donor_effective = min(recip_donor_remain[from], recip_transfer_remain[st_donor.landcover][st_receptor.landcover]);
  742. double receptor_effective = min(recip_receptor_remain[to], recip_transfer_remain[st_donor.landcover][st_receptor.landcover]);
  743. // all donor land is put into this stand type
  744. if(receptor_effective >= donor_effective) {
  745. st_frac_transfer[index(from, to)] += donor_effective;
  746. recip_receptor_remain[to] -= donor_effective;
  747. recip_donor_remain[from] -= donor_effective;
  748. recip_transfer_remain[st_donor.landcover][st_receptor.landcover] -= donor_effective;
  749. }
  750. // transfer to more stand types
  751. else {
  752. st_frac_transfer[index(from, to)] += receptor_effective;
  753. recip_donor_remain[from] -= receptor_effective;
  754. recip_receptor_remain[to] -= receptor_effective;
  755. recip_transfer_remain[st_donor.landcover][st_receptor.landcover] -= receptor_effective;
  756. }
  757. }
  758. }
  759. }
  760. }
  761. // Balance fractions for stand types within a landcover
  762. double* change_st = new double[nst];
  763. double* dif_st = new double[nst];
  764. for(int from=0; from<nst; from++) {
  765. change_st[from] = 0.0;
  766. dif_st[from] = 0.0;
  767. }
  768. for(int from=0; from<nst; from++) {
  769. for(int to=0; to<nst; to++) {
  770. change_st[from] -= st_frac_transfer[index(from, to)];
  771. change_st[to] += st_frac_transfer[index(from, to)];
  772. }
  773. }
  774. double dif_lc[NLANDCOVERTYPES] = {0.0};
  775. for(int from=0; from<nst; from++) {
  776. Gridcellst& gcst = gridcell.st[from];
  777. dif_st[from] = gcst.frac - (gcst.frac_old + change_st[from]); // fraction needed to reach target
  778. dif_lc[stlist[from].landcover] += fabs(dif_st[from]) / 2.0;
  779. }
  780. const double ROUNDING_ERROR = 1.0e-16;
  781. for(int lc=0; lc<NLANDCOVERTYPES; lc++) {
  782. // identify land cover with need for st frac adjustment
  783. if(nsts[lc] >= 2 && dif_lc[lc] >= INPUT_RESOLUTION * 0.1) {
  784. // find second land cover that has most transfers to or from the first land cover (with only one stand type with frac/frac_old > 0)
  785. bool error = true;
  786. for(int use_lc=0; use_lc<NLANDCOVERTYPES && error; use_lc++) {
  787. bool use_donor = true;
  788. if(nsts_active[use_lc] == 1 && max(lc_frac_transfer[lc][use_lc], lc_frac_transfer[use_lc][lc]) > dif_lc[lc]) {
  789. // determine whether to use transfer to or from land cover 2
  790. if(dif_lc[lc] <= lc_frac_transfer[use_lc][lc]) {
  791. bool skip = false;
  792. for(int st_help=0; st_help<nst; st_help++) {
  793. StandType& st_donor = stlist[st_help];
  794. if(st_donor.landcover == use_lc && (gridcell.st[st_help].frac || gridcell.st[st_help].frac_old)) {
  795. for(int st_dif=0; st_dif<nst; st_dif++) {
  796. StandType& st_receptor = stlist[st_dif];
  797. if(st_receptor.landcover == lc && !(dif_st[st_dif] >= 0.0 || st_frac_transfer[index(st_help, st_dif)] + dif_st[st_dif] > -ROUNDING_ERROR)) {
  798. skip = true;
  799. break;
  800. }
  801. }
  802. }
  803. }
  804. if(!skip) {
  805. use_donor = true;
  806. error = false;
  807. }
  808. }
  809. if(error && dif_lc[lc] <= lc_frac_transfer[lc][use_lc]) {
  810. bool skip = false;
  811. for(int st_dif=0; st_dif<nst; st_dif++) {
  812. StandType& st_donor = stlist[st_dif];
  813. if(st_donor.landcover == lc) {
  814. for(int st_help=0; st_help<nst; st_help++) {
  815. StandType& st_receptor = stlist[st_help];
  816. if(st_receptor.landcover == use_lc && (gridcell.st[st_help].frac || gridcell.st[st_help].frac_old) && !(dif_st[st_dif] <= 0.0 || st_frac_transfer[index(st_dif, st_help)] - dif_st[st_dif] > -ROUNDING_ERROR)) {
  817. skip = true;
  818. break;
  819. }
  820. }
  821. }
  822. }
  823. if(!skip) {
  824. use_donor = false;
  825. error = false;
  826. }
  827. }
  828. if(!error) {
  829. for(int st_dif=0; st_dif<nst; st_dif++) {
  830. StandType& st_donor = stlist[st_dif];
  831. if(st_donor.landcover == lc && fabs(dif_st[st_dif]) >= INPUT_RESOLUTION * 0.1) {
  832. for(int st_help=0; st_help<nst; st_help++) {
  833. StandType& st_receptor = stlist[st_help];
  834. if(st_receptor.landcover == use_lc && (gridcell.st[st_help].frac || gridcell.st[st_help].frac_old) && fabs(dif_st[st_dif]) >= INPUT_RESOLUTION * 0.1) {
  835. // after tests above, this should always work (overshoots < ROUNDING_ERROR are zeroed here)
  836. if(use_donor) {
  837. st_frac_transfer[index(st_help, st_dif)] += max(-st_frac_transfer[index(st_help, st_dif)], dif_st[st_dif]);
  838. dif_st[st_dif] = 0.0;
  839. }
  840. else {
  841. st_frac_transfer[index(st_dif, st_help)] -= min(st_frac_transfer[index(st_dif, st_help)], dif_st[st_dif]);
  842. dif_st[st_dif] = 0.0;
  843. }
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. }
  851. if(error) {
  852. // If stand type fractions still unbalanced, try shuffling within a land cover
  853. for(int from=0; from<nst; from++) {
  854. StandType& st_donor = stlist[from];
  855. if(st_donor.landcover == lc && -dif_st[from] > INPUT_RESOLUTION * 0.1) {
  856. for(int to=0; to<nst; to++) {
  857. StandType& st_receptor = stlist[to];
  858. if(st_donor.landcover == st_receptor.landcover && -dif_st[from] > INPUT_RESOLUTION * 0.1 && dif_st[to] >= INPUT_RESOLUTION * 0.1) {
  859. double donor_effective = -dif_st[from];
  860. double receptor_effective = dif_st[to];
  861. // all donor land going to this land cover is put into this stand type
  862. if(receptor_effective >= donor_effective) {
  863. st_frac_transfer[index(from, to)] += donor_effective;
  864. lc_frac_transfer[st_donor.landcover][st_donor.landcover] += donor_effective;
  865. dif_st[from] += donor_effective;
  866. dif_st[to] -= donor_effective;
  867. }
  868. // transfer to more stand types within this land cover
  869. else {
  870. st_frac_transfer[index(from, to)] += receptor_effective;
  871. lc_frac_transfer[st_donor.landcover][st_donor.landcover] += receptor_effective;
  872. dif_st[from] += receptor_effective;
  873. dif_st[to] -= receptor_effective;
  874. }
  875. }
  876. }
  877. }
  878. }
  879. }
  880. }
  881. }
  882. if(change_st)
  883. delete[] change_st;
  884. if(dif_st)
  885. delete[] dif_st;
  886. // Remove very small transfer values derived from rounding errors in the balancing section above
  887. for(int from=0; from<nst; from++) {
  888. for(int to=0; to<nst; to++) {
  889. if(st_frac_transfer[index(from, to)] < ROUNDING_ERROR)
  890. st_frac_transfer[index(from, to)] = 0.0;
  891. }
  892. }
  893. for(int from=0; from<nst; from++) {
  894. StandType& st_donor = stlist[from];
  895. Gridcellst& gcst_donor = gridcell.st[from];
  896. for(int to=0; to<nst; to++) {
  897. StandType& st_receptor = stlist[to];
  898. Gridcellst& gcst_receptor = gridcell.st[to];
  899. double transfer_frac = st_frac_transfer[index(from, to)];
  900. if(transfer_frac > (gcst_donor.frac_old) || transfer_frac > (gcst_receptor.frac)) {
  901. if((transfer_frac - gcst_receptor.frac) > INPUT_RESOLUTION || (transfer_frac - gcst_receptor.frac) > INPUT_RESOLUTION) {
  902. dprintf("\nYear %d: st_change_array sum not compatible with st.frac_old/frac values for stand types %d and %d\n", date.year, from, to);
  903. dprintf("st_change_array=%.15f, st_donor.frac_old=%.15f, st_receptor.frac=%.15f\n", transfer_frac, gcst_donor.frac_old, gcst_receptor.frac);
  904. }
  905. if(transfer_frac > (gcst_receptor.frac) && transfer_frac < (gcst_receptor.frac + INPUT_RESOLUTION * 100.0)) {
  906. if((transfer_frac - gcst_receptor.frac) > INPUT_RESOLUTION)
  907. dprintf("Removing overshoot in array %.15f\n\n", st_frac_transfer[index(from, to)] - gcst_receptor.frac);
  908. st_frac_transfer[index(from, to)] = gcst_receptor.frac;
  909. }
  910. else if(transfer_frac > (gcst_donor.frac_old) && transfer_frac < (gcst_donor.frac_old + INPUT_RESOLUTION * 100.0)) {
  911. if((transfer_frac - gcst_donor.frac_old) > INPUT_RESOLUTION)
  912. dprintf("Removing overshoot in array %.15f\n\n", st_frac_transfer[index(from, to)] - gcst_donor.frac_old);
  913. st_frac_transfer[index(from, to)] = gcst_donor.frac_old;
  914. }
  915. }
  916. }
  917. }
  918. // Set the transfer fraction from primary stands within a stand type (simple case: equal primary/secondary ratio for all stand types in a lc->lc transfer)
  919. if(use_primary_lc_transfer) {
  920. for(int from=0; from<nst; from++) {
  921. StandType& st_donor = stlist[from];
  922. for(int to=0; to<nst; to++) {
  923. StandType& st_receptor = stlist[to];
  924. double primary_frac = 0.0;
  925. double secondary_young_frac = 0.0;
  926. if(lc_frac_transfer[st_donor.landcover][st_receptor.landcover] > 0.0) {
  927. primary_frac = forest_lc_frac_transfer_s.primary[st_donor.landcover][st_receptor.landcover] / lc_frac_transfer[st_donor.landcover][st_receptor.landcover];
  928. secondary_young_frac = forest_lc_frac_transfer_s.secondary_young[st_donor.landcover][st_receptor.landcover] / lc_frac_transfer[st_donor.landcover][st_receptor.landcover];
  929. }
  930. forest_st_frac_transfer_s.primary[index(from, to)] = primary_frac * st_frac_transfer[index(from, to)];
  931. forest_st_frac_transfer_s.secondary_young[index(from, to)] = secondary_young_frac * st_frac_transfer[index(from, to)];
  932. }
  933. }
  934. }
  935. if(recip_donor_remain)
  936. delete[] recip_donor_remain;
  937. if(recip_receptor_remain)
  938. delete[] recip_receptor_remain;
  939. }
  940. /// Handles harvest and turnover of reduced stands at landcover change.
  941. /** Sets landcover.updated to true
  942. * Stores carbon, nitrogen and water of harvested area in a temporary struct.
  943. * Should be followed by a call to stand_dynamics() to kill stands with a new area of 0
  944. *
  945. * INPUT PARAMETERS
  946. *
  947. * \param receiving_fraction sum of added area to expanding stands
  948. * \param landcover_receptor restriction of donor stands transferring to specified receptor landcover
  949. * \param landcover_donor restriction of donor stands according to landcover
  950. * \param stid_receptor restriction of donor stands transferring to specified receptor stand type
  951. * \param stid_donor restriction of stands according to stand type
  952. * \param standid restriction of donor stand
  953. *
  954. * OUTPUT PARAMETERS
  955. * \param landcover_change_transfer struct containing the following pft-specific public members:
  956. * - transfer_litter_leaf
  957. * - transfer_litter_sap
  958. * - transfer_litter_heart
  959. * - transfer_litter_root
  960. * - transfer_litter_repr
  961. * - transfer_nmass_litter_leaf
  962. * - transfer_nmass_litter_root
  963. * - transfer_nmass_litter_sap
  964. * - transfer_nmass_litter_heart
  965. * - transfer_harvested_products_slow
  966. * - transfer_harvested_products_slow_nmass
  967. * ,the following patch-level public members
  968. * - transfer_cpool_fast
  969. * - transfer_cpool_slow
  970. * - transfer_nmass_avail
  971. * - transfer_wcont_evap
  972. * - transfer_snowpack
  973. * - transfer_decomp_litter_mean
  974. * - transfer_k_soilfast_mean
  975. * - transfer_acflux_harvest
  976. * - transfer_anflux_harvest
  977. * ,the following water soil layer-specific public member:
  978. * - transfer_wcont
  979. * and the following century soil pool-specific public members:
  980. * - transfer_sompool.cmass
  981. * - transfer_sompool.fireresist
  982. * - transfer_sompool.fracremain
  983. * - transfer_sompool.ligcfrac
  984. * - transfer_sompool.nmass
  985. * - transfer_sompool.ntoc
  986. */
  987. void donor_stand_change(Gridcell& gridcell, double& receiving_fraction, landcover_change_transfer& to,
  988. int landcover_receptor = -1, int landcover_donor = -1,
  989. int stid_receptor = -1, int stid_donor = -1,
  990. int standid = -1, lc_change_harvest_params* harv_params = NULL) {
  991. int count = 0;
  992. Landcover& lc = gridcell.landcover;
  993. for(unsigned int i=0; i<gridcell.nbr_stands(); i++) {
  994. Stand& stand = gridcell[i];
  995. double donor_area = 0.0;
  996. if(stid_receptor >= 0)
  997. donor_area = stand.transfer_area_st[stid_receptor];
  998. else if(landcover_receptor >= 0)
  999. donor_area = stand.transfer_area_lc((landcovertype)landcover_receptor);
  1000. else
  1001. donor_area = stand.gross_frac_decrease;
  1002. if(donor_area > 0.0 && (stand.id == standid || standid < 0) && (stand.landcover == landcover_donor || landcover_donor < 0) && (stand.stid == stid_donor || stid_donor < 0)) {
  1003. count++;
  1004. stand.frac_temp -= donor_area;
  1005. stand.frac_temp = max(0.0, stand.frac_temp);
  1006. double scale = donor_area / receiving_fraction / (double)stand.nobj;
  1007. // Add non-living C, N and water of stand to transfer struct:
  1008. to.add_from_stand(stand, scale);
  1009. // Harvest and turnover of copies of individuals, add to transfer copy:
  1010. stand.firstobj();
  1011. while(stand.isobj) {
  1012. Patch& patch = stand.getobj();
  1013. Vegetation& vegetation = patch.vegetation;
  1014. vegetation.firstobj();
  1015. while(vegetation.isobj) {
  1016. Harvest_CN cp;
  1017. Individual& indiv = vegetation.getobj();
  1018. Patchpft& patchpft = patch.pft[indiv.pft.id];
  1019. if(indiv.has_daily_turnover())
  1020. cp.copy_from_indiv(indiv, true, false);
  1021. else
  1022. cp.copy_from_indiv(indiv, false, false);
  1023. bool lc_pool = (landcover_receptor == -1) && (stid_receptor == -1);
  1024. bool wood_harvest = false;
  1025. if(stand.landcover == NATURAL || stand.landcover == FOREST) {
  1026. if(lc_pool) {
  1027. if(stand.transfer_area_lc((landcovertype)NATURAL) || stand.transfer_area_lc((landcovertype)FOREST))
  1028. wood_harvest = true;
  1029. }
  1030. else if(landcover_receptor == NATURAL || landcover_receptor == FOREST || stid_receptor > -1 && (stlist[stid_receptor].landcover == NATURAL || stlist[stid_receptor].landcover == FOREST)) {
  1031. wood_harvest = true;
  1032. }
  1033. }
  1034. // Harvest of transferred areas:
  1035. switch (stand.landcover)
  1036. {
  1037. case CROPLAND:
  1038. harvest_crop(cp, indiv.pft, indiv.alive, indiv.cropindiv->isintercropgrass);
  1039. break;
  1040. case PASTURE:
  1041. harvest_pasture(cp, indiv.pft, indiv.alive);
  1042. break;
  1043. case NATURAL:
  1044. case FOREST:
  1045. case URBAN:
  1046. case PEATLAND:
  1047. if(harv_params) {
  1048. harvest_wood(cp, indiv.pft, indiv.alive, 1.0, harv_params->harv_eff, harv_params->res_outtake_twig, harv_params->res_outtake_coarse_root);
  1049. }
  1050. else if(wood_harvest) {
  1051. if(lc_pool) {
  1052. double lu_change_ratio = 1.0 - (stand.transfer_area_lc((landcovertype)NATURAL) + stand.transfer_area_lc((landcovertype)FOREST)) / stand.gross_frac_decrease;
  1053. // Land cover change area: frac_cut=1, harv_eff=1, res_outtake_twig=0.95, res_outtake_coarse_root=0.9
  1054. // Wood harvest area: frac_cut=1, harv_eff=pft.harv_eff, res_outtake_twig=pft.res_outtake, res_outtake_coarse_root=0
  1055. harvest_wood(cp, indiv.pft, indiv.alive, 1.0, 1.0 * lu_change_ratio + indiv.pft.harv_eff * (1.0 - lu_change_ratio), 0.95 * lu_change_ratio + indiv.pft.res_outtake * (1.0 - lu_change_ratio), 0.9 * lu_change_ratio);
  1056. }
  1057. else {
  1058. harvest_wood(cp, indiv.pft, indiv.alive, 1.0, indiv.pft.harv_eff, indiv.pft.res_outtake);
  1059. }
  1060. }
  1061. else {
  1062. harvest_wood(cp, indiv.pft, indiv.alive, 1.0, 1.0, 0.95, 0.9); // frac_cut=1, harv_eff=1, res_outtake_twig=0.95, res_outtake_coarse_root=0.9
  1063. }
  1064. break;
  1065. case BARREN: // Assuming there is nothing to harvest on barren
  1066. break;
  1067. default:
  1068. fail("Modify code to deal with landcover harvest at landcover change!\n");
  1069. }
  1070. turnover(indiv.pft.turnover_leaf, indiv.pft.turnover_root,
  1071. indiv.pft.turnover_sap, indiv.pft.lifeform, indiv.pft.landcover,
  1072. cp.cmass_leaf, cp.cmass_root, cp.cmass_sap, cp.cmass_heart,
  1073. cp.nmass_leaf, cp.nmass_root, cp.nmass_sap, cp.nmass_heart,
  1074. cp.litter_leaf,
  1075. cp.litter_root,
  1076. cp.nmass_litter_leaf,
  1077. cp.nmass_litter_root,
  1078. cp.nstore_longterm, cp.max_n_storage,
  1079. indiv.alive);
  1080. // In case any vegetation left (eg. cmass_root in pasture or grass in woodland):
  1081. kill_remaining_vegetation(cp, indiv.pft, indiv.alive, indiv.istruecrop_or_intercropgrass(), false);
  1082. //Sum added litter C & N:
  1083. to.transfer_litter_leaf[indiv.pft.id] += cp.litter_leaf * scale;
  1084. to.transfer_litter_root[indiv.pft.id] += cp.litter_root * scale;
  1085. to.transfer_litter_sap[indiv.pft.id] += cp.litter_sap * scale;
  1086. to.transfer_litter_heart[indiv.pft.id] += cp.litter_heart * scale;
  1087. to.transfer_nmass_litter_leaf[indiv.pft.id] += cp.nmass_litter_leaf * scale;
  1088. to.transfer_nmass_litter_root[indiv.pft.id] += cp.nmass_litter_root * scale;
  1089. to.transfer_nmass_litter_sap[indiv.pft.id] += cp.nmass_litter_sap * scale;
  1090. to.transfer_nmass_litter_heart[indiv.pft.id] += cp.nmass_litter_heart * scale;
  1091. lc.dcflux_landuse_change += cp.acflux_harvest * donor_area / (double)stand.nobj; // ecev3 - reset to 0 each day
  1092. lc.acflux_landuse_change += cp.acflux_harvest * donor_area / (double)stand.nobj;
  1093. lc.acflux_landuse_change_lc[stand.landcover] += cp.acflux_harvest * donor_area / (double)stand.nobj;
  1094. lc.anflux_landuse_change += cp.anflux_harvest * donor_area / (double)stand.nobj;
  1095. lc.anflux_landuse_change_lc[stand.landcover] += cp.anflux_harvest * donor_area / (double)stand.nobj;
  1096. // gridcell.acflux_landuse_change += -cp.debt_excess * donor_area / (double)stand.nobj;
  1097. if(ifslowharvestpool) {
  1098. to.transfer_harvested_products_slow[indiv.pft.id] += cp.harvested_products_slow * scale;
  1099. to.transfer_harvested_products_slow_nmass[indiv.pft.id] += cp.harvested_products_slow_nmass * scale;
  1100. }
  1101. vegetation.nextobj();
  1102. }
  1103. stand.nextobj();
  1104. }
  1105. }
  1106. }
  1107. }
  1108. /// Creates and kills stands at landcover change.
  1109. /** Harvest of reduced stands need to be done before with donor_stand_change().
  1110. * Should be followed by a call to receiving_stand_change() for transfer of
  1111. * carbon, nitrogen and water.
  1112. *
  1113. */
  1114. void stand_dynamics(Gridcell& gridcell) {
  1115. stlist.firstobj();
  1116. while (stlist.isobj) {
  1117. StandType& st=stlist.getobj();
  1118. Gridcellst& gcst = gridcell.st[st.id];
  1119. landcovertype lc = st.landcover;
  1120. bool expand_to_new_stand = gridcell.landcover.expand_to_new_stand[lc];
  1121. if(gcst.gross_frac_increase || gcst.gross_frac_decrease || gcst.frac == 0.0) {
  1122. double stand_sum = 0.0;
  1123. Gridcell::iterator gc_itr = gridcell.begin();
  1124. while (gc_itr != gridcell.end()) {
  1125. Stand& stand = *gc_itr;
  1126. if(stand.stid == st.id)
  1127. stand_sum += stand.get_gridcell_fraction();
  1128. ++gc_itr;
  1129. }
  1130. // first stand created
  1131. if((gcst.frac_old == 0.0 || stand_sum == 0.0) && gcst.gross_frac_increase > 0.0) {
  1132. int npatch;
  1133. if(date.year == 0)
  1134. // with npatch=0, number of patches will be chosen in the stand constructor
  1135. npatch = 0;
  1136. else
  1137. npatch = npatch_secondarystand;
  1138. Stand& stand = gridcell.create_stand_lu(st, gcst.gross_frac_increase, npatch);
  1139. }
  1140. // last stand killed
  1141. else if(gcst.frac_old > 0.0 && !gcst.frac) {
  1142. Gridcell::iterator gc_itr = gridcell.begin();
  1143. while (gc_itr != gridcell.end()) {
  1144. Stand& stand = *gc_itr;
  1145. if(stand.stid == st.id) {
  1146. gridcell.landcover.acflux_landuse_change += stand.ccont() * stand.get_gridcell_fraction();
  1147. gridcell.landcover.anflux_landuse_change += stand.ncont() * stand.get_gridcell_fraction();
  1148. gc_itr = gridcell.delete_stand(gc_itr);
  1149. }
  1150. else {
  1151. ++gc_itr;
  1152. }
  1153. }
  1154. }
  1155. else {
  1156. if(expand_to_new_stand) {
  1157. // new stand created from other landcover types (pooled option, not created in transfer_to_new_stand())
  1158. if(gcst.gross_frac_increase > 0.0) {
  1159. // Fewer patches in secondary stands if npatch_secondarystand < npatch:
  1160. Stand& stand = gridcell.create_stand_lu(st, gcst.gross_frac_increase, npatch_secondarystand);
  1161. }
  1162. }
  1163. // secondary stand killed if all of its area converted to other landcover types
  1164. if(gcst.nstands > 1 && gcst.gross_frac_decrease > 0.0) {
  1165. Gridcell::iterator gc_itr = gridcell.begin();
  1166. while (gc_itr != gridcell.end()) {
  1167. Stand& stand = *gc_itr;
  1168. if(stand.stid == st.id && stand.get_gridcell_fraction() == 0) {
  1169. gc_itr = gridcell.delete_stand(gc_itr);
  1170. if(gcst.frac < 1e-14)
  1171. gcst.frac = 0.0;
  1172. }
  1173. else {
  1174. ++gc_itr;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. stlist.nextobj();
  1181. }
  1182. }
  1183. /// Transfers litter etc. of reduced stands to expanding stands at landcover change.
  1184. /** Transfers carbon, nitrogen and water of harvested area to expanded areas from a temporary struct.
  1185. * Is additive - can be called several times to the same landcover or standtype, if the parameter donorfrac_rel is used
  1186. *
  1187. * INPUT PARAMETERS
  1188. *
  1189. * \param landcover_change_transfer& from struct containing the following pft-specific public members:
  1190. * - transfer_litter_leaf
  1191. * - transfer_litter_sap
  1192. * - transfer_litter_heart
  1193. * - transfer_litter_root
  1194. * - transfer_litter_repr
  1195. * - transfer_nmass_litter_leaf
  1196. * - transfer_nmass_litter_root
  1197. * - transfer_nmass_litter_sap
  1198. * - transfer_nmass_litter_heart
  1199. * - transfer_harvested_products_slow
  1200. * - transfer_harvested_products_slow_nmass
  1201. * ,the following patch-level public members:
  1202. * - transfer_cpool_fast
  1203. * - transfer_cpool_slow
  1204. * - transfer_nmass_avail
  1205. * - transfer_cpool_slow
  1206. * - transfer_wcont_evap
  1207. * - transfer_snowpack
  1208. * - transfer_decomp_litter_mean
  1209. * - transfer_k_soilfast_mean
  1210. * - transfer_acflux_harvest
  1211. * - transfer_anflux_harvest
  1212. * ,the following water soil layer-specific public member:
  1213. * - transfer_wcont
  1214. * and the following century soil pool-specific public members:
  1215. * - transfer_sompool.cmass
  1216. * - transfer_sompool.fireresist
  1217. * - transfer_sompool.fracremain
  1218. * - transfer_sompool.ligcfrac
  1219. * - transfer_sompool.nmass
  1220. * - transfer_sompool.ntoc
  1221. *
  1222. * \param LCchangeCtransfer whether to transfer carbon, nitrogen and water of reduced stands to expanding stands
  1223. * \param landcover restricts receiving stands to a certain landcover, default no restriction
  1224. * \param stid restricts receiving stands to a certain standtype, default no restriction
  1225. * \param donorfrac_rel relative part of fraction (to a receiving stand) from a particular donor
  1226. * \param standid restricts receiving stands to a certain stand, default no restriction
  1227. */
  1228. void receiving_stand_change(Gridcell& gridcell, landcover_change_transfer& from,
  1229. bool LCchangeCtransfer, int landcover = -1, int stid = -1,
  1230. double donorfrac_rel = 1.0, int standid = -1) {
  1231. Gridcell::iterator gc_itr = gridcell.begin();
  1232. while (gc_itr != gridcell.end()) {
  1233. Stand& stand = *gc_itr;
  1234. if(stand.gross_frac_increase > 0.0 && (stand.landcover == landcover || landcover < 0) &&
  1235. (stand.stid == stid || stid < 0 && (stand.id == standid || standid < 0))) {
  1236. double old_frac = stand.frac_temp;
  1237. double added_frac = donorfrac_rel * stand.gross_frac_increase;
  1238. double new_frac = old_frac + added_frac;
  1239. stand.frac_temp += added_frac;
  1240. if(LCchangeCtransfer) {
  1241. stand.firstobj();
  1242. while(stand.isobj) {
  1243. Patch& patch=stand.getobj();
  1244. // add litter C & N:
  1245. for (int i=0; i<npft; i++) {
  1246. Patchpft& patchpft = patch.pft[i];
  1247. patchpft.litter_leaf = (patchpft.litter_leaf * old_frac + from.transfer_litter_leaf[i] * added_frac) / new_frac;
  1248. patchpft.litter_sap = (patchpft.litter_sap * old_frac + from.transfer_litter_sap[i] * added_frac) / new_frac;
  1249. patchpft.litter_heart = (patchpft.litter_heart * old_frac + from.transfer_litter_heart[i] * added_frac) / new_frac;
  1250. patchpft.litter_root = (patchpft.litter_root * old_frac + from.transfer_litter_root[i] * added_frac) / new_frac;
  1251. patchpft.litter_repr = (patchpft.litter_repr * old_frac + from.transfer_litter_repr[i] * added_frac) / new_frac;
  1252. patchpft.nmass_litter_leaf = (patchpft.nmass_litter_leaf * old_frac + from.transfer_nmass_litter_leaf[i] * added_frac) / new_frac;
  1253. patchpft.nmass_litter_root = (patchpft.nmass_litter_root * old_frac + from.transfer_nmass_litter_root[i] * added_frac) / new_frac;
  1254. patchpft.nmass_litter_sap = (patchpft.nmass_litter_sap * old_frac + from.transfer_nmass_litter_sap[i] * added_frac) / new_frac;
  1255. patchpft.nmass_litter_heart = (patchpft.nmass_litter_heart * old_frac + from.transfer_nmass_litter_heart[i] * added_frac) / new_frac;
  1256. if(ifslowharvestpool) {
  1257. patchpft.harvested_products_slow = (patchpft.harvested_products_slow * old_frac + from.transfer_harvested_products_slow[i] * added_frac) / new_frac;
  1258. patchpft.harvested_products_slow_nmass = (patchpft.harvested_products_slow_nmass * old_frac + from.transfer_harvested_products_slow_nmass[i] * added_frac) / new_frac;
  1259. }
  1260. }
  1261. // add soil C & N:
  1262. patch.soil.cpool_fast = (patch.soil.cpool_fast * old_frac + from.transfer_cpool_fast * added_frac) / new_frac;
  1263. patch.soil.cpool_slow = (patch.soil.cpool_slow * old_frac + from.transfer_cpool_slow * added_frac) / new_frac;
  1264. for(int i=0; i<NSOMPOOL; i++) {
  1265. patch.soil.sompool[i].cmass = (patch.soil.sompool[i].cmass * old_frac + from.transfer_sompool[i].cmass * added_frac) / new_frac;
  1266. patch.soil.sompool[i].fireresist = (patch.soil.sompool[i].fireresist * old_frac + from.transfer_sompool[i].fireresist * added_frac) / new_frac;
  1267. patch.soil.sompool[i].fracremain = (patch.soil.sompool[i].fracremain * old_frac + from.transfer_sompool[i].fracremain * added_frac) / new_frac;
  1268. patch.soil.sompool[i].ligcfrac = (patch.soil.sompool[i].ligcfrac *old_frac + from.transfer_sompool[i].ligcfrac * added_frac) / new_frac;
  1269. patch.soil.sompool[i].litterme = (patch.soil.sompool[i].litterme * old_frac + from.transfer_sompool[i].litterme * added_frac) / new_frac;
  1270. patch.soil.sompool[i].nmass = (patch.soil.sompool[i].nmass * old_frac + from.transfer_sompool[i].nmass * added_frac) / new_frac;
  1271. patch.soil.sompool[i].ntoc = (patch.soil.sompool[i].ntoc * old_frac + from.transfer_sompool[i].ntoc * added_frac) / new_frac;
  1272. }
  1273. patch.soil.nmass_avail = (patch.soil.nmass_avail * old_frac + from.transfer_nmass_avail * added_frac) / new_frac;
  1274. // ecev3 - check
  1275. const double EPS = 1.0e-16;
  1276. if (patch.soil.nmass_avail < 0.0) {
  1277. if (patch.soil.nmass_avail >= -EPS)
  1278. patch.soil.nmass_avail = 0.0; // Reset to 0 if negative but tiny
  1279. else {
  1280. dprintf("ERROR! Year %d day %d Stand %d: Negative soil.nmass_avail in receiving_stand_change: %.15f\n", date.year, date.day, patch.stand.id, patch.soil.nmass_avail);
  1281. dprintf("%.15f %.15f %.15f\n", from.transfer_nmass_avail, added_frac, new_frac);
  1282. }
  1283. }
  1284. // add other soil stuff:
  1285. for(int i=0; i<NSOILLAYER; i++) {
  1286. patch.soil.wcont[i] = (patch.soil.wcont[i] * old_frac + from.transfer_wcont[i] * added_frac) / new_frac;
  1287. }
  1288. patch.soil.wcont_evap = (patch.soil.wcont_evap * old_frac + from.transfer_wcont_evap * added_frac) / new_frac;
  1289. patch.soil.snowpack = (patch.soil.snowpack * old_frac + from.transfer_snowpack * added_frac) / new_frac;
  1290. patch.soil.snowpack_nmass = (patch.soil.snowpack_nmass * old_frac + from.transfer_snowpack_nmass * added_frac) / new_frac;
  1291. patch.soil.decomp_litter_mean = (patch.soil.decomp_litter_mean * old_frac + from.transfer_decomp_litter_mean * added_frac) / new_frac;
  1292. patch.soil.k_soilfast_mean = (patch.soil.k_soilfast_mean * old_frac + from.transfer_k_soilfast_mean * added_frac) / new_frac;
  1293. patch.soil.k_soilslow_mean = (patch.soil.k_soilslow_mean * old_frac + from.transfer_k_soilslow_mean * added_frac) / new_frac;
  1294. double aaet_5_cp[NYEARAAET] = {0.0};
  1295. patch.aaet_5.to_array(aaet_5_cp);
  1296. for(unsigned int i=0;i<from.transfer_aaet_5.size();i++)
  1297. patch.aaet_5.add((aaet_5_cp[i] * old_frac + from.transfer_aaet_5[i] * added_frac) / new_frac);
  1298. patch.soil.anfix_calc = (patch.soil.anfix_calc * old_frac + from.transfer_anfix_calc * added_frac) / new_frac;
  1299. // save individual C and N content for use in scale_indiv()
  1300. for(unsigned int i=0; i<patch.vegetation.nobj ;i++) {
  1301. Individual& indiv = patch.vegetation[i];
  1302. indiv.save_cmass_luc();
  1303. indiv.save_nmass_luc();
  1304. }
  1305. stand.nextobj();
  1306. }
  1307. // set scaling factor to be used in growth() for scaling vegetation C and N:
  1308. stand.scale_LC_change = (stand.frac_old - stand.gross_frac_decrease + min(0.0, stand.cloned_fraction)) / stand.get_gridcell_fraction();
  1309. gridcell.landcover.updated = true;
  1310. }
  1311. }
  1312. ++gc_itr;
  1313. }
  1314. }
  1315. enum {NONEWSTAND, CLONESTAND, CLONESTAND_KILLTREES, NEWSTAND_KILLALL};
  1316. /// contains rules for creation of new stands at land cover change
  1317. /** Options CLONESTAND and CLONESTAND_KILLTREES require that the receptor landcover
  1318. * allows growth of natural grass and/or tree PFTs
  1319. *
  1320. * INTPUT PARAMETERS
  1321. *
  1322. * \param landcover_donor landcover type of donor stand
  1323. * \param landcover_receptor landcover type of rexeptor stand
  1324. */
  1325. int copy_stand_type(int landcover_donor, int landcover_receptor) {
  1326. int copy_type = NONEWSTAND;
  1327. if(landcover_donor == CROPLAND) {
  1328. if(landcover_receptor == NATURAL || landcover_receptor == FOREST)
  1329. copy_type = NEWSTAND_KILLALL;
  1330. }
  1331. else if(landcover_donor == PASTURE) {
  1332. if(landcover_receptor == NATURAL || landcover_receptor == FOREST)
  1333. copy_type = CLONESTAND;
  1334. }
  1335. else if(landcover_donor == NATURAL || landcover_donor == FOREST) {
  1336. if(landcover_receptor == FOREST || landcover_receptor == NATURAL)
  1337. copy_type = CLONESTAND; // or CLONESTAND_KILLTREES/NEWSTAND_KILLALL for clearcut
  1338. }
  1339. return copy_type;
  1340. }
  1341. /// Creates unique stands from transfer events if copy_stand_type() returns >= 1 for landcovers combination
  1342. /** Either clones donor stand or creates new stand from scratch
  1343. *
  1344. * INPUT PARAMETERS
  1345. *
  1346. * \param stid_donor donor stand type
  1347. * \param stid_receptor receptor stand type
  1348. */
  1349. double transfer_to_new_stand(Gridcell& gridcell, int stid_donor, int stid_receptor) {
  1350. double cloned_area = 0.0;
  1351. for(unsigned int i=0; i<gridcell.nbr_stands(); i++) {
  1352. Stand& stand = gridcell[i];
  1353. double transfer_area = stand.transfer_area_st[stid_receptor];
  1354. if(transfer_area > 0.0 && (stand.stid == stid_donor || stid_donor < 0)) {
  1355. int copy_type = copy_stand_type(stand.landcover, stlist[stid_receptor].landcover); // NONEWSTAND, CLONESTAND, CLONESTAND_KILLTREES, NEWSTAND_KILLALL
  1356. if(copy_type) {
  1357. if(copy_type == CLONESTAND || copy_type == CLONESTAND_KILLTREES) {
  1358. Stand& new_stand = stand.clone(stlist[stid_receptor], transfer_area);
  1359. if(stand.landcover == NATURAL && (stlist[stid_receptor].naturalveg == ""))
  1360. dprintf("WARNING: cloning natural stand without allowing natural pft:s to grow in the new stand. Was this intended ?\n");
  1361. landcover_change_transfer transfer;
  1362. new_stand.cloned = true;
  1363. new_stand.gross_frac_increase = 0.0;
  1364. new_stand.frac_change = 0.0;
  1365. new_stand.cloned_fraction = transfer_area;
  1366. new_stand.origin = stand.landcover;
  1367. new_stand.firstobj();
  1368. while(new_stand.isobj) {
  1369. Patch& patch = new_stand.getobj();
  1370. Vegetation& vegetation = patch.vegetation;
  1371. vegetation.firstobj();
  1372. while(vegetation.isobj) {
  1373. Individual& indiv = vegetation.getobj();
  1374. Standpft& standpft = new_stand.pft[indiv.pft.id];
  1375. if(!standpft.active) {
  1376. // Treatment of pft individuals not allowed to grow anymore in the new stand:
  1377. // Clearcut
  1378. harvest_wood(indiv, 1.0, 1.0, 0.95, 0.9, true); // frac_cut=1, harv_eff=1, res_outtake_twig=0.95, res_outtake_coarse_root=0.9
  1379. // Grass killed, C+N goes to soil
  1380. kill_remaining_vegetation(indiv);
  1381. vegetation.killobj();
  1382. }
  1383. else if(copy_type == CLONESTAND_KILLTREES && indiv.pft.lifeform != GRASS) {
  1384. harvest_wood(indiv, 1.0, 1.0, 0.95, 0.9, true); // frac_cut=1, harv_eff=1, res_outtake_twig=0.95, res_outtake_coarse_root=0.9
  1385. vegetation.killobj();
  1386. }
  1387. else {
  1388. vegetation.nextobj();
  1389. }
  1390. }
  1391. new_stand.nextobj();
  1392. }
  1393. }
  1394. else if(copy_type == NEWSTAND_KILLALL) {
  1395. landcover_change_transfer transfer;
  1396. lc_change_harvest_params harvest_params;
  1397. // default values for natural to cropland transition
  1398. harvest_params.harv_eff = 1.0;
  1399. harvest_params.res_outtake_twig = 0.95;
  1400. harvest_params.res_outtake_coarse_root = 0.9;
  1401. donor_stand_change(gridcell, transfer_area, transfer, -1,-1, stid_receptor, -1, stand.id, &harvest_params);
  1402. Stand& new_stand = gridcell.create_stand_lu(stlist[stid_receptor], transfer_area, npatch_secondarystand);
  1403. receiving_stand_change(gridcell, transfer, true, -1, -1, 1.0, new_stand.id);
  1404. new_stand.cloned = true;
  1405. new_stand.gross_frac_increase = 0.0;
  1406. new_stand.frac_change = 0.0;
  1407. new_stand.cloned_fraction = transfer_area;
  1408. }
  1409. cloned_area += transfer_area;
  1410. stand.cloned_fraction -= transfer_area;
  1411. gridcell.st[stid_donor].gross_frac_decrease -= transfer_area;
  1412. gridcell.st[stid_donor].frac_change += transfer_area;
  1413. stand.gross_frac_decrease -= transfer_area;
  1414. stand.frac_change += transfer_area;
  1415. stand.transfer_area_st[stid_receptor] -= transfer_area;
  1416. gridcell.st[stid_receptor].gross_frac_increase -= transfer_area;
  1417. gridcell.st[stid_receptor].frac_change -= transfer_area;
  1418. if(gridcell.st[stid_donor].gross_frac_decrease < INPUT_RESOLUTION * 0.1)
  1419. gridcell.st[stid_donor].gross_frac_decrease = 0.0;
  1420. if(stand.gross_frac_decrease < INPUT_RESOLUTION * 0.1)
  1421. stand.gross_frac_decrease = 0.0;
  1422. if(stand.transfer_area_st[stid_receptor] < INPUT_RESOLUTION * 0.1)
  1423. stand.transfer_area_st[stid_receptor] = 0.0;
  1424. if(gridcell.st[stid_receptor].gross_frac_increase < INPUT_RESOLUTION * 0.1)
  1425. gridcell.st[stid_receptor].gross_frac_increase = 0.0;
  1426. }
  1427. }
  1428. }
  1429. return cloned_area;
  1430. }
  1431. /// Simulates gross landcover change by adding a specified fraction to the lc_frac_transfer array
  1432. void simulate_gross_lc_transfer(Gridcell& gridcell, double lc_frac_transfer[][NLANDCOVERTYPES]) {
  1433. // Added gross fraction transfer as percentage of the lesser of two stand types belonging to certain landcover types
  1434. double gross_lc_change_frac[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  1435. gross_lc_change_frac[CROPLAND][NATURAL] = 0.05;
  1436. gross_lc_change_frac[NATURAL][CROPLAND] = 0.05;
  1437. gross_lc_change_frac[CROPLAND][PASTURE] = 0.05;
  1438. gross_lc_change_frac[PASTURE][CROPLAND] = 0.05;
  1439. gross_lc_change_frac[PASTURE][NATURAL] = 0.05;
  1440. gross_lc_change_frac[NATURAL][PASTURE] = 0.05;
  1441. gross_lc_change_frac[FOREST][NATURAL] = 0.05;
  1442. gross_lc_change_frac[NATURAL][FOREST] = 0.05;
  1443. gross_lc_change_frac[FOREST][PASTURE] = 0.05;
  1444. gross_lc_change_frac[PASTURE][FOREST] = 0.05;
  1445. Landcover& lc = gridcell.landcover;
  1446. for(int from=0; from<NLANDCOVERTYPES; from++) {
  1447. for(int to=0; to<NLANDCOVERTYPES; to++) {
  1448. lc_frac_transfer[from][to] += gross_lc_change_frac[from][to] * min(lc.frac_old[from], lc.frac_old[to]);
  1449. }
  1450. }
  1451. }
  1452. /// Simulates gross landcover change by adding a specified fraction to the st_frac_transfer array
  1453. void simulate_gross_st_transfer(Gridcell& gridcell, double* st_frac_transfer) {
  1454. // Added gross fraction transfer as percentage of the lesser of two stand types belonging to certain landcover types
  1455. double gross_lc_change_frac[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  1456. gross_lc_change_frac[CROPLAND][NATURAL] = 0.05;
  1457. gross_lc_change_frac[NATURAL][CROPLAND] = 0.05;
  1458. gross_lc_change_frac[CROPLAND][PASTURE] = 0.05;
  1459. gross_lc_change_frac[PASTURE][CROPLAND] = 0.05;
  1460. gross_lc_change_frac[PASTURE][NATURAL] = 0.05;
  1461. gross_lc_change_frac[NATURAL][PASTURE] = 0.05;
  1462. gross_lc_change_frac[FOREST][NATURAL] = 0.05;
  1463. gross_lc_change_frac[NATURAL][FOREST] = 0.05;
  1464. gross_lc_change_frac[FOREST][PASTURE] = 0.05;
  1465. gross_lc_change_frac[PASTURE][FOREST] = 0.05;
  1466. for(int from=0; from<nst; from++) {
  1467. StandType& st_donor = stlist[from];
  1468. Gridcellst& gcst_donor = gridcell.st[from];
  1469. for(int to=0; to<nst; to++) {
  1470. StandType& st_receptor = stlist[to];
  1471. Gridcellst& gcst_receptor = gridcell.st[to];
  1472. if(gross_lc_change_frac[st_donor.landcover][st_receptor.landcover]) {
  1473. // All stand types with an area:
  1474. st_frac_transfer[index(from, to)] += gross_lc_change_frac[st_donor.landcover][st_receptor.landcover]
  1475. * min(min(gcst_donor.frac_old, gcst_donor.frac), min(gcst_receptor.frac_old, gcst_receptor.frac));
  1476. }
  1477. }
  1478. }
  1479. }
  1480. /// Called after update of st fraction and transfer values
  1481. bool check_fractions(Gridcell& gridcell, double landcoverfrac_change[], double lc_change_array[][NLANDCOVERTYPES],
  1482. double* st_change_array, bool check_lc_st_transfer = false) {
  1483. bool error = false;
  1484. // Check that landcover sum is 1:
  1485. double lc_frac_sum = 0.0;
  1486. for(int i=0; i<NLANDCOVERTYPES; i++)
  1487. lc_frac_sum += gridcell.landcover.frac[i];
  1488. if(!negligible(lc_frac_sum - 1.0, CHECK_NLIM)) {
  1489. dprintf("\nCheck 1: Year %d: landcover fraction sum: %.15f", date.get_calendar_year(), lc_frac_sum);
  1490. error = true;
  1491. }
  1492. /////////
  1493. // Check that stand type sum is 1:
  1494. double st_frac_sum = 0.0;
  1495. for(int i=0; i<nst; i++)
  1496. st_frac_sum += gridcell.st[i].frac;
  1497. if(fabs(st_frac_sum - 1.0) > INPUT_RESOLUTION * 10.0) {
  1498. dprintf("\nCheck 2: Year %d: stand type fraction sum: %.15f", date.year, st_frac_sum);
  1499. error = true;
  1500. }
  1501. /////////
  1502. // Test if the sum of gross lcc for a landcover is the same as net lcc:
  1503. double test_lc_change[NLANDCOVERTYPES] = {0.0};
  1504. for(int from=0; from<NLANDCOVERTYPES; from++) {
  1505. for(int to=0; to<NLANDCOVERTYPES; to++) {
  1506. test_lc_change[from] -= lc_change_array[from][to];
  1507. test_lc_change[to] += lc_change_array[from][to];
  1508. }
  1509. }
  1510. for(int i=0; i<NLANDCOVERTYPES; i++) {
  1511. if(fabs(test_lc_change[i] - landcoverfrac_change[i]) > INPUT_RESOLUTION * 10.0) {
  1512. dprintf("\nCheck 3: Year %d: lc_change_array sum not equal to landcoverfrac_change value for landcover %d\n", date.year, i);
  1513. dprintf("dif=%.15f", test_lc_change[i] - landcoverfrac_change[i]);
  1514. error = true;
  1515. }
  1516. }
  1517. ////////////
  1518. // Test if the sum of gross lcc for a stand type is the same as net lcc:
  1519. double *test_st_change;
  1520. test_st_change = new double[nst];
  1521. for(int i=0;i<nst;i++) {
  1522. test_st_change[i] = 0.0;
  1523. }
  1524. for(int from=0; from<nst; from++) {
  1525. for(int to=0; to<nst; to++) {
  1526. test_st_change[from] -= st_change_array[index(from, to)];
  1527. test_st_change[to] += st_change_array[index(from, to)];
  1528. }
  1529. }
  1530. for(int i=0; i<nst; i++) {
  1531. StandType& st = stlist[i];
  1532. Gridcellst& gcst = gridcell.st[i];
  1533. if(fabs(test_st_change[i] - gcst.frac_change) > INPUT_RESOLUTION * 10.0) {
  1534. dprintf("\nCheck 4: Year %d: st_change_array sum not equal to st.frac_change value for stand type %d\n", date.year, i);
  1535. dprintf("dif=%.15f", test_st_change[i] - gcst.frac_change);
  1536. error = true;
  1537. }
  1538. }
  1539. if(test_st_change)
  1540. delete[] test_st_change;
  1541. ////////////
  1542. // Test if st_change_array is compatible with st.frac_old/frac values
  1543. for(int from=0; from<nst; from++) {
  1544. StandType& st_donor = stlist[from];
  1545. Gridcellst& gcst_donor = gridcell.st[from];
  1546. for(int to=0; to<nst; to++) {
  1547. StandType& st_receptor = stlist[to];
  1548. Gridcellst& gcst_receptor = gridcell.st[to];
  1549. if(st_change_array[index(from, to)] > (gcst_donor.frac_old + INPUT_RESOLUTION) || st_change_array[index(from, to)] > (gcst_receptor.frac + INPUT_RESOLUTION)) {
  1550. dprintf("\nCheck 5: Year %d: st_change_array sum not compatible with st.frac_old/frac values for stand types %d and %d\n", date.year, from, to);
  1551. dprintf("st_change_array=%.15f, st_donor.frac_old=%.15f, st_receptor.frac=%.15f", st_change_array[index(from, to)], gcst_donor.frac_old, gcst_receptor.frac);
  1552. error = true;
  1553. }
  1554. }
  1555. }
  1556. // Test if stand type and landcover transfer matrices match each other:
  1557. if(check_lc_st_transfer) {
  1558. double lc_change[NLANDCOVERTYPES] = {0.0};
  1559. double lc_change_arr[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  1560. for(int from=0; from<nst; from++) {
  1561. StandType& st = stlist[from];
  1562. for(int to=0; to<nst; to++) {
  1563. StandType& st_dest = stlist[to];
  1564. lc_change[st.landcover] -= st_change_array[index(from, to)];
  1565. lc_change[st_dest.landcover] += st_change_array[index(from, to)];
  1566. lc_change_arr[st.landcover][st_dest.landcover] += st_change_array[index(from, to)];
  1567. }
  1568. }
  1569. for(int i=0; i<NLANDCOVERTYPES; i++) {
  1570. if(fabs(lc_change[i] - landcoverfrac_change[i]) > INPUT_RESOLUTION * 10.0) {
  1571. dprintf("\nCheck 6: Year %d: st_change_array LC sum not equal to LC value for %d\n", date.year, i);
  1572. dprintf("dif=%.15f", fabs(lc_change[i] - landcoverfrac_change[i]));
  1573. error = true;
  1574. }
  1575. }
  1576. for(int from=0; from<NLANDCOVERTYPES; from++) {
  1577. for(int to=0; to<NLANDCOVERTYPES; to++) {
  1578. if(fabs(lc_change_arr[from][to] - lc_change_array[from][to]) > INPUT_RESOLUTION * 10.0) {
  1579. dprintf("\nCheck 7: Year %d: lc_change_arr sum not equal to lc_change_array value for %d, %d\n", date.year, from, to);
  1580. dprintf("dif=%.15f", lc_change_arr[from][to] - lc_change_array[from][to]);
  1581. error = true;
  1582. }
  1583. }
  1584. }
  1585. // Test that no lc transfers are negative:
  1586. for(int from=0; from<NLANDCOVERTYPES; from++) {
  1587. for(int to=0; to<NLANDCOVERTYPES; to++) {
  1588. if(lc_change_array[from][to] < 0.0) {
  1589. dprintf("\nCheck 7b: Year %d: Negative lc_change_array[%d][%d]: %.15f\n", date.year, from, to, lc_change_array[from][to]);
  1590. error = true;
  1591. }
  1592. }
  1593. }
  1594. }
  1595. if(error)
  1596. dprintf("\n\n");
  1597. return error;
  1598. }
  1599. /// Called before updating stand.frac (reduce_stands)
  1600. bool check_fractions1(Gridcell& gridcell) {
  1601. bool error = false;
  1602. // Test if the stand type reduction is smaller than the remaining stand sum:
  1603. for(int s=0; s<nst; s++) {
  1604. StandType& st = stlist[s];
  1605. Gridcellst& gcst = gridcell.st[s];
  1606. if(gcst.frac_change < 0.0){
  1607. double stands_frac_sum = 0.0;
  1608. for(unsigned int i = 0; i < gridcell.nbr_stands(); i++) {
  1609. Stand& stand = gridcell[i];
  1610. if(stand.stid == st.id)
  1611. stands_frac_sum += stand.get_gridcell_fraction();
  1612. }
  1613. if(-gcst.frac_change - stands_frac_sum > INPUT_RESOLUTION * 10.0) {
  1614. dprintf("\nCheck 8: Year %d: stand type %d fraction reduction bigger than sum of stands\n", date.year, s);
  1615. dprintf("dif=%.15f", -gcst.frac_change - stands_frac_sum);
  1616. error = true;
  1617. }
  1618. }
  1619. }
  1620. if(error)
  1621. dprintf("\n\n");
  1622. return error;
  1623. }
  1624. /// Called after updating stand.frac and transfer values (reduce_stands)
  1625. bool check_fractions2(Gridcell& gridcell, double* st_change_array) {
  1626. bool error = false;
  1627. // Test if stand.transfer_area_st[to] sum for a stand type is equal to st_change_array[from][to)]
  1628. for(int from=0; from<nst; from++) {
  1629. StandType& st = stlist[from];
  1630. for(int to=0; to<nst; to++) {
  1631. double *test_st_change;
  1632. test_st_change = new double[nst];
  1633. for(int i=0;i<nst;i++)
  1634. test_st_change[i] = 0.0;
  1635. for(unsigned int i=0; i<gridcell.nbr_stands(); i++) {
  1636. Stand& stand = gridcell[i];
  1637. if(stand.stid == st.id)
  1638. test_st_change[to] += stand.transfer_area_st[to];
  1639. }
  1640. if(fabs(test_st_change[to] - st_change_array[index(from, to)]) > INPUT_RESOLUTION * 100.0) {
  1641. dprintf("\nCheck 9: Year %d: stand transfer area sum not equal to stand type value for stand types %d and %d\n", date.year, from, to);
  1642. dprintf("dif=%.15f", fabs(test_st_change[to] - st_change_array[index(from, to)]));
  1643. error = true;
  1644. }
  1645. if(test_st_change)
  1646. delete[] test_st_change;
  1647. }
  1648. }
  1649. ////////////
  1650. // Test if stand.frac_change for a stand is equal to stand.gross_frac_increase + stand.gross_frac_decrease:
  1651. for(unsigned int i=0; i<gridcell.nbr_stands(); i++) {
  1652. Stand& stand = gridcell[i];
  1653. if(largerthanzero(stand.frac_change - (stand.gross_frac_increase - stand.gross_frac_decrease), CHECK_NLIM+1)) {
  1654. dprintf("\nCheck 10: Year %d: frac_change is not equal to gross_frac_increase + gross_frac_decrease for stand %d\n", date.get_calendar_year(), stand.id);
  1655. dprintf("dif=%.15f\n", fabs(stand.frac_change - (stand.gross_frac_increase - stand.gross_frac_decrease)));
  1656. dprintf("frac_change=%.15f, gross_frac_increase=%.15f, gross_frac_decrease=%.15f", stand.frac_change, stand.gross_frac_increase, stand.gross_frac_decrease);
  1657. error = true;
  1658. }
  1659. }
  1660. ////////////
  1661. // Test that no stands remain when stand type fraction is 0 (after reduce_stands, so stands may remain, but with frac 0):
  1662. for(int s=0; s<nst; s++) {
  1663. StandType& st = stlist[s];
  1664. Gridcellst& gcst = gridcell.st[s];
  1665. if(gcst.frac == 0.0) {
  1666. double stands_frac_sum = 0.0;
  1667. for(unsigned int i = 0; i < gridcell.nbr_stands(); i++) {
  1668. Stand& stand = gridcell[i];
  1669. if(stand.stid == st.id)
  1670. stands_frac_sum += stand.get_gridcell_fraction();
  1671. }
  1672. if(stands_frac_sum) {
  1673. dprintf("\nCheck 11: Year %d: remaining stand when stand type %d fraction is 0\n", date.get_calendar_year(), s);
  1674. dprintf("remain=%.20f", stands_frac_sum);
  1675. error = true;
  1676. }
  1677. }
  1678. }
  1679. if(error)
  1680. dprintf("\n\n");
  1681. return error;
  1682. }
  1683. /// Called after updating stand.frac and transfer values of reduced stands (reduce_stands)
  1684. bool check_fractions3(Gridcell& gridcell) {
  1685. bool error = false;
  1686. // Test if sum of stands not equal to stand type value for stand type for reduced stands
  1687. for(int s=0; s<nst; s++) {
  1688. StandType& st = stlist[s];
  1689. Gridcellst& gcst = gridcell.st[s];
  1690. double stands_frac_sum = 0.0;
  1691. for(unsigned int i = 0; i < gridcell.nbr_stands(); i++) {
  1692. Stand& stand = gridcell[i];
  1693. if(stand.stid == st.id)
  1694. stands_frac_sum += stand.get_gridcell_fraction();
  1695. }
  1696. if(gcst.frac_change < 0.0) {
  1697. if(fabs(gcst.frac - stands_frac_sum - gcst.gross_frac_increase) > INPUT_RESOLUTION * 100.0) {
  1698. dprintf("\nCheck 12: Year %d: fraction sum of stands not equal to stand type value for stand type %d\n", date.year, s);
  1699. dprintf("dif=%.15f", fabs(gcst.frac - stands_frac_sum - gcst.gross_frac_increase));
  1700. error = true;
  1701. }
  1702. }
  1703. }
  1704. if(error)
  1705. dprintf("\n\n");
  1706. return error;
  1707. }
  1708. /// Called after creating new stands (stand_dynamics)
  1709. bool check_fractions4(Gridcell& gridcell) {
  1710. bool error = false;
  1711. // Test if sum of stands not equal to stand type value for stand type for increased or new stands
  1712. for(int s=0; s<nst; s++) {
  1713. StandType& st = stlist[s];
  1714. Gridcellst& gcst = gridcell.st[s];
  1715. double stands_frac_sum = 0.0;
  1716. for(unsigned int i = 0; i < gridcell.nbr_stands(); i++) {
  1717. Stand& stand = gridcell[i];
  1718. if(stand.stid == st.id) {
  1719. stands_frac_sum += stand.get_gridcell_fraction();
  1720. }
  1721. }
  1722. if(gcst.frac_change >= 0.0) {
  1723. if(gcst.frac && !stands_frac_sum) {
  1724. dprintf("\nCheck 13a: Year %d: no stand present when fraction is > 0 for stand type %d\n", date.year, s);
  1725. dprintf("st frac=%.15f\n\n", gcst.frac);
  1726. }
  1727. else if(fabs(gcst.frac - stands_frac_sum) > INPUT_RESOLUTION * 100.0) {
  1728. dprintf("\nCheck 13: Year %d: fraction sum of stands not equal to stand type value for stand type %d\n", date.year, s);
  1729. dprintf("dif=%.15f", fabs(gcst.frac - stands_frac_sum));
  1730. error = true;
  1731. }
  1732. }
  1733. }
  1734. if(error)
  1735. dprintf("\n\n");
  1736. return error;
  1737. }
  1738. /// Gets this year's landcover and crop area fractions, landcover transitions and checks that area changes are significant.
  1739. /** Stores changes in area fractions for the stand types
  1740. *
  1741. * OUTPUT PARAMETERS
  1742. * \param LCchangeCtransfer whether to transfer carbon, nitrogen and water of reduced stands to expanding stands
  1743. */
  1744. bool lc_changed(Gridcell& gridcell, bool& LCchangeCtransfer, InputModule* input_module) {
  1745. double stfrac_sum_old[NLANDCOVERTYPES] = {0.0};
  1746. double change_stand = 0.0;
  1747. double changeLC = 0.0;
  1748. double change_st = 0.0;
  1749. double transferred_fraction = 0.0;
  1750. double receiving_fraction = 0.0;
  1751. bool change = true;
  1752. bool force_small_change = false;
  1753. Landcover& lc = gridcell.landcover;
  1754. // Get new gridcell.landcoverfrac and/or standtype.frac from LUdata and CFTdata
  1755. input_module->getlandcover(gridcell);
  1756. // Check if any changes in land fractions have been made
  1757. for(unsigned int i = 0; i < gridcell.st.nobj; i++) {
  1758. Gridcellst& gcst = gridcell.st[i];
  1759. stfrac_sum_old[gcst.st.landcover] += gcst.frac_old;
  1760. if(fabs(gcst.frac_change) && (!gcst.frac || !gcst.frac_old))
  1761. force_small_change = true;
  1762. }
  1763. for(int i=0; i<NLANDCOVERTYPES; i++) {
  1764. changeLC += fabs(lc.frac_change[i]) / 2.0;
  1765. }
  1766. for(int lc=0; lc<NLANDCOVERTYPES; lc++) {
  1767. if(run[lc] && (!frac_fixed[lc] || !lcfrac_fixed)) {
  1768. for(unsigned int i = 0; i < gridcell.st.nobj; i++) {
  1769. Gridcellst& gcst = gridcell.st[i];
  1770. if(gcst.st.landcover == lc) {
  1771. double stfrac_change = gcst.frac_change;
  1772. if(stfrac_change < 0.0)
  1773. transferred_fraction -= stfrac_change;
  1774. else if(stfrac_change > 0.0)
  1775. receiving_fraction += stfrac_change;
  1776. if(stfrac_sum_old[lc] != 0.0) {
  1777. change_st += fabs(stfrac_change) / 2.0;
  1778. }
  1779. else {
  1780. change_st += fabs(stfrac_change);
  1781. }
  1782. change_stand += fabs(stfrac_change) / 2.0;
  1783. }
  1784. }
  1785. }
  1786. }
  1787. double change_gross_lcc = 0.0;
  1788. for(int from=0; from<NLANDCOVERTYPES; from++) {
  1789. if(run[from]) {
  1790. for(int to=0; to<NLANDCOVERTYPES; to++) {
  1791. if(run[to]) {
  1792. change_gross_lcc += lc.frac_transfer[to][from];
  1793. }
  1794. }
  1795. }
  1796. }
  1797. // if no changes, do nothing.
  1798. if(changeLC < INPUT_RESOLUTION * 0.1 && change_st < INPUT_RESOLUTION * 0.1 && change_gross_lcc < INPUT_RESOLUTION * 0.1 && !force_small_change) {
  1799. change = false;
  1800. }
  1801. // check for balance of reduced and increased stand fractions
  1802. else {
  1803. if(fabs(transferred_fraction - receiving_fraction) > 0.0001 || fabs(change_stand-receiving_fraction) > 0.0001) {
  1804. if(run[CROPLAND] && run[NATURAL] && run[PASTURE]) {
  1805. // end program if balance is expected (no landcovers inactivated)
  1806. FAIL("Transferred landcover fractions not balanced ! Abort. \n");
  1807. }
  1808. else {
  1809. // allow program to continue, but inactivate landcover change mass transfer
  1810. LCchangeCtransfer = false;
  1811. dprintf("Transferred landcover fractions not balanced !\nLandcover change fluxes not calculated.\n");
  1812. }
  1813. }
  1814. change = true;
  1815. }
  1816. return change;
  1817. }
  1818. /// Transfers harvested forest area to transition matrix
  1819. bool transfer_harvested_fractions(Gridcell& gridcell, double lc_frac_transfer[][NLANDCOVERTYPES], forest_lc_frac_transfer& forest_lc_frac_transfer_s) {
  1820. bool transfer_harvest = false;
  1821. if(gridcell.landcover.wood_harvest.prim_frac > 0.0) {
  1822. lc_frac_transfer[NATURAL][NATURAL] += gridcell.landcover.wood_harvest.prim_frac;
  1823. forest_lc_frac_transfer_s.primary[NATURAL][NATURAL] += gridcell.landcover.wood_harvest.prim_frac;
  1824. use_primary_lc_transfer = true;
  1825. transfer_harvest = true;
  1826. }
  1827. if(harvest_secondary_to_new_stand && gridcell.landcover.wood_harvest.sec_mature_frac + gridcell.landcover.wood_harvest.sec_young_frac > 0.0) {
  1828. lc_frac_transfer[NATURAL][NATURAL] += gridcell.landcover.wood_harvest.sec_mature_frac;
  1829. lc_frac_transfer[NATURAL][NATURAL] += gridcell.landcover.wood_harvest.sec_young_frac;
  1830. forest_lc_frac_transfer_s.secondary_young[NATURAL][NATURAL] += gridcell.landcover.wood_harvest.sec_young_frac;
  1831. transfer_harvest = true;
  1832. }
  1833. return transfer_harvest;
  1834. }
  1835. /// Updates all landcover, stand type and stand area fractions each year, possibly resulting in the creation and killing of stands.
  1836. /** Harvests transferred areas and transfers litter etc. of reduced stands to expanding stands and harvested matter to fluxes
  1837. * and (in the case of wood) to long-lived pools.
  1838. *
  1839. * Land cover fraction files need to be read together with gross land transition files. Net and gross land cover change is
  1840. * expected to be compatible, although rounding errors are handled by the input code.
  1841. *
  1842. * If instruction file parameter iftransfer_to_new_stand is true, new stands may be created for separate land transfers
  1843. * in transfer_to_new_stand(), according to rules in copy_stand_type().
  1844. *
  1845. * Otherwise, new transferred areas are pooled according to instruction file parameter transfer_level (0: one big pool; 1: land cover-level;
  1846. * 2: stand type-level) and rules in the gridcell arrays pool_from_all_landcovers[to] and pool_to_all_landcovers[from], set
  1847. * in the Gridcell constructor.
  1848. * New stands may then be created in stand_dynamics() from the pooled land if the gridcell array expand_to_new_stand[lc] value for the
  1849. * receiving land cover, set in the Gridcell constructor, is true (natural and forest).
  1850. * If not, soil and litter carbon and nitrogen as well as water is pooled with the receiving stands (cropland, pasture).
  1851. *
  1852. * Transferred land with living plant mass after land cover change should be put in new stands, using the stand cloning mode in
  1853. * transfer_to_new_stand(). Stands with living plant mass should not be pooled with newly transferred land, unconditionally so
  1854. * if they contain trees (pasture can be expanded without too much problems). New stands should instead be created, either in stand_dynamics()
  1855. * or transfer_to_new_stand() as described above.
  1856. */
  1857. void landcover_dynamics(Gridcell& gridcell, InputModule* input_module) {
  1858. // ecev3
  1859. int year = date.get_calendar_year();
  1860. if (false && ECEARTH && gridcell.fixedLUafter >= 0 && year >= gridcell.fixedLUafter ) {
  1861. dprintf("Return from landcover_dynamics: fixedLUfter = %d \n", year);
  1862. return;
  1863. }
  1864. double* st_frac_transfer = NULL;
  1865. forest_st_frac_transfer forest_st_frac_transfer_s(nst);
  1866. bool LCchangeCtransfer = true;
  1867. Landcover& lc = gridcell.landcover;
  1868. st_frac_transfer = new double[nst * nst];
  1869. for (int i = 0; i<nst; i++) {
  1870. gridcell.st[i].gross_frac_increase = 0.0;
  1871. gridcell.st[i].gross_frac_decrease = 0.0;
  1872. }
  1873. for(int i=0;i<NLANDCOVERTYPES;i++) {
  1874. lc.frac_change[i] = 0.0;
  1875. for(int j=0;j<NLANDCOVERTYPES;j++) {
  1876. lc.frac_transfer[i][j] = 0.0;
  1877. lc.forest_lc_frac_transfer_s.primary[i][j] = 0.0;
  1878. lc.forest_lc_frac_transfer_s.secondary_young[i][j] = 0.0;
  1879. }
  1880. }
  1881. for(int i=0;i<nst*nst;i++) {
  1882. st_frac_transfer[i] = 0.0;
  1883. forest_st_frac_transfer_s.primary[i] = 0.0;
  1884. forest_st_frac_transfer_s.secondary_young[i] = 0.0;
  1885. }
  1886. gridcell.landcover.updated = false;
  1887. for(unsigned int i=0; i<gridcell.nbr_stands(); ++i)
  1888. gridcell[i].scale_LC_change = 1.0;
  1889. bool no_changes = true;
  1890. // Transfer area to new stands during wood harvest.
  1891. if(transfer_harvested_fractions(gridcell, lc.frac_transfer, lc.forest_lc_frac_transfer_s))
  1892. no_changes = false;
  1893. // Rescale stand fractions so sum is 1
  1894. stlist.firstobj();
  1895. while (stlist.isobj) {
  1896. StandType& st = stlist.getobj();
  1897. Gridcellst& gcst = gridcell.st[st.id];
  1898. double frac_sum = 0.0;
  1899. for(unsigned int s=0;s<gridcell.nbr_stands();s++) {
  1900. Stand& stand = gridcell[s];
  1901. if(stand.stid == st.id)
  1902. frac_sum += stand.get_gridcell_fraction();
  1903. }
  1904. for(unsigned int s=0;s<gridcell.nbr_stands();s++) {
  1905. Stand& stand = gridcell[s];
  1906. if(stand.stid == st.id && frac_sum && gcst.frac)
  1907. stand.set_gridcell_fraction(stand.get_gridcell_fraction() / (frac_sum / gcst.frac));
  1908. }
  1909. stlist.nextobj();
  1910. }
  1911. // Get new landcover and stand type area fractions from input files, read transition arrays.
  1912. if(!all_fracs_const) {
  1913. // this call returns 0, causing this function to return, if no significant landcover changes this year,
  1914. // sets LCchangeCtransfer to 0 if unbalanced landcover changes (if some landcovers are inactivated), thus inactivating transfer of C and N
  1915. if(lc_changed(gridcell, LCchangeCtransfer, input_module)) {
  1916. no_changes = false;
  1917. }
  1918. }
  1919. //CLN LUCHANGE HERE
  1920. if(no_changes) {
  1921. delete[] st_frac_transfer;
  1922. return;
  1923. }
  1924. // Transfer all landcover changes to stand type transition matrix, if not already done.
  1925. if(gross_land_transfer == 3) {
  1926. // Option to read stand type transitions from file.
  1927. fail("Currently no code for option gross_land_transfer==3\n");
  1928. }
  1929. else if(gross_land_transfer == 2 && gross_input_present) {
  1930. // Option to read landcover transitions from file. Update the st_frac_transfer array.
  1931. set_st_change_array(gridcell, lc.frac_transfer, st_frac_transfer, lc.forest_lc_frac_transfer_s, forest_st_frac_transfer_s);
  1932. if(check_fractions(gridcell, lc.frac_change, lc.frac_transfer, st_frac_transfer, true))
  1933. dprintf("Fraction error after set_st_change_array()\n\n");
  1934. }
  1935. else {
  1936. // Option not to read landcover or stand type transitions or to simulate these.
  1937. // The st_frac_transfer array always needs to be updated.
  1938. const bool simulate_st = true; // gross lcc simulation at land cover level (false) or stand type level (true)
  1939. set_lc_change_array(lc.frac_change, lc.frac_transfer);
  1940. if(gross_land_transfer && !simulate_st)
  1941. simulate_gross_lc_transfer(gridcell, lc.frac_transfer);
  1942. // The lc_frac_transfer-array may contain overshoots if wood harvest with fraction transfer occurred.
  1943. double dummy;
  1944. adjust_gross_transfers(gridcell, lc.frac_change, lc.frac_transfer, lc.forest_lc_frac_transfer_s, dummy);
  1945. set_st_change_array(gridcell, lc.frac_transfer, st_frac_transfer, lc.forest_lc_frac_transfer_s, forest_st_frac_transfer_s);
  1946. if(check_fractions(gridcell, lc.frac_change, lc.frac_transfer, st_frac_transfer, true))
  1947. dprintf("Fraction error after set_st_change_array()\n\n");
  1948. if(gross_land_transfer && simulate_st)
  1949. simulate_gross_st_transfer(gridcell, st_frac_transfer);
  1950. }
  1951. check_fractions(gridcell, lc.frac_change, lc.frac_transfer, st_frac_transfer);
  1952. check_fractions1(gridcell);
  1953. for(int from=0; from<nst; from++) {
  1954. for(int to=0; to<nst; to++) {
  1955. if(st_frac_transfer[index(from, to)] > 0.0) {
  1956. gridcell.st[to].gross_frac_increase += st_frac_transfer[index(from, to)];
  1957. gridcell.st[from].gross_frac_decrease += st_frac_transfer[index(from, to)];
  1958. }
  1959. }
  1960. }
  1961. double ccont_tot_1 = gridcell.ccont();
  1962. double cflux_tot_1 = gridcell.cflux();
  1963. double ncont_tot_1 = gridcell.ncont();
  1964. double nflux_tot_1 = gridcell.nflux();
  1965. // check how many stands of each stand type exist
  1966. // identify which stands to reduce in area
  1967. // set stand variables frac, frac_old, frac_change and gross_frac_decrease for reduced stands and stand types
  1968. reduce_stands(gridcell, st_frac_transfer, forest_st_frac_transfer_s);
  1969. int error = check_fractions2(gridcell, st_frac_transfer);
  1970. error += check_fractions3(gridcell);
  1971. if(error) {
  1972. FAIL("Fraction error after reduce_stands()\n\n");
  1973. }
  1974. // Create new stands for land cover transitions when natural vegetation remains or when each transition requires a new stand:
  1975. if(iftransfer_to_new_stand) {
  1976. bool new_stand = false;
  1977. for(int from=0; from<nst; from++) {
  1978. for(int to=0; to<nst; to++) {
  1979. if(st_frac_transfer[index(from, to)] > 0.0) {
  1980. double new_stand_frac = transfer_to_new_stand(gridcell, from, to);
  1981. if(new_stand_frac) {
  1982. st_frac_transfer[index(from, to)] -= new_stand_frac;
  1983. if(st_frac_transfer[index(from, to)] < INPUT_RESOLUTION * 0.1)
  1984. st_frac_transfer[index(from, to)] = 0.0;
  1985. new_stand = true;
  1986. }
  1987. }
  1988. }
  1989. }
  1990. if(new_stand) {
  1991. int error = check_fractions(gridcell, lc.frac_change, lc.frac_transfer, st_frac_transfer);
  1992. error += check_fractions2(gridcell, st_frac_transfer);
  1993. if(error)
  1994. dprintf("Fraction error after transfer_to_new_stand()\n\n");
  1995. }
  1996. }
  1997. // set stand variables frac, frac_change and gross_frac_increase for expanding stands and stand types
  1998. expand_stands(gridcell, st_frac_transfer);
  1999. error += check_fractions(gridcell, lc.frac_change, lc.frac_transfer, st_frac_transfer);
  2000. error += check_fractions2(gridcell, st_frac_transfer);
  2001. if(error)
  2002. FAIL("Fraction error after expand_stands()\n");
  2003. // Pooling options
  2004. // transfer_level: 0: one big pool; 1: land cover-level; 2: stand type-level
  2005. if(transfer_level == 0) { // One big pool for all transfers (as in old code)
  2006. landcover_change_transfer transfer;
  2007. double receiving_fraction = 0.0;
  2008. for(int from=0; from<nst; from++) {
  2009. for(int to=0; to<nst; to++) {
  2010. if(st_frac_transfer[index(from, to)] > 0.0)
  2011. receiving_fraction += st_frac_transfer[index(from, to)];
  2012. }
  2013. }
  2014. // handle harvest and turnover of reduced stands at landcover change
  2015. donor_stand_change(gridcell, receiving_fraction, transfer);
  2016. // create and kill stands at landcover change
  2017. stand_dynamics(gridcell);
  2018. error += check_fractions4(gridcell);
  2019. if(error)
  2020. fail("Fraction error after stand_dynamics()\n");
  2021. // transfer litter etc. of reduced stands to expanding stands at landcover change
  2022. receiving_stand_change(gridcell, transfer, LCchangeCtransfer);
  2023. }
  2024. else if(transfer_level == 1) { // Landcover-level pools, larger pools available by the gridcell variables pool_from_all_landcovers and pool_to_all_landcovers (set in gridcell constructor)
  2025. landcover_change_transfer transfer_lc_2d[NLANDCOVERTYPES][NLANDCOVERTYPES];
  2026. landcover_change_transfer transfer_lc[NLANDCOVERTYPES];
  2027. landcover_change_transfer transfer_lc_from[NLANDCOVERTYPES];
  2028. double gross_landcoverfrac_increase[NLANDCOVERTYPES] = {0.0};
  2029. double gross_landcoverfrac_decrease[NLANDCOVERTYPES] = {0.0};
  2030. double gross_landcoverfrac_transfer[NLANDCOVERTYPES][NLANDCOVERTYPES] = {0.0};
  2031. for(int from=0; from<nst; from++) {
  2032. for(int to=0; to<nst; to++) {
  2033. if(st_frac_transfer[index(from, to)] > 0.0) {
  2034. gross_landcoverfrac_increase[stlist[to].landcover] += st_frac_transfer[index(from, to)];
  2035. gross_landcoverfrac_decrease[stlist[from].landcover] += st_frac_transfer[index(from, to)];
  2036. gross_landcoverfrac_transfer[stlist[from].landcover][stlist[to].landcover] += st_frac_transfer[index(from, to)];
  2037. }
  2038. }
  2039. }
  2040. // handle harvest and turnover of reduced stands at landcover change
  2041. // pool all transferred area from one landcover:
  2042. for(int from=0; from<NLANDCOVERTYPES; from++) {
  2043. if(gridcell.landcover.pool_to_all_landcovers[from]) { // alt.c
  2044. if(gross_landcoverfrac_decrease[from] > 0.0) {
  2045. donor_stand_change(gridcell, gross_landcoverfrac_decrease[from], transfer_lc_from[from], -1, from);
  2046. }
  2047. }
  2048. }
  2049. for(int to=0; to<NLANDCOVERTYPES; to++) {
  2050. double receiving_fraction = gross_landcoverfrac_increase[to];
  2051. for(int from=0; from<NLANDCOVERTYPES; from++) {
  2052. double receiving_fraction_2d = gross_landcoverfrac_transfer[from][to];
  2053. if(receiving_fraction_2d > 0.0) {
  2054. if(gridcell.landcover.pool_from_all_landcovers[to]) {
  2055. if(!gridcell.landcover.pool_to_all_landcovers[from]) { // alt.a
  2056. // Add from->to transfer to to-pool:
  2057. donor_stand_change(gridcell, receiving_fraction, transfer_lc[to], to, from);
  2058. }
  2059. else { // alt.a+c
  2060. // Add from-pool value to to-pool:
  2061. transfer_lc[to].add(transfer_lc_from[from], receiving_fraction_2d / receiving_fraction);
  2062. }
  2063. }
  2064. else {
  2065. if(!gridcell.landcover.pool_to_all_landcovers[from]) { // alt.b
  2066. // Add from->to transfer to [from][to] place in 2d-array:
  2067. donor_stand_change(gridcell, receiving_fraction_2d, transfer_lc_2d[from][to], to, from);
  2068. }
  2069. else { // alt.c
  2070. // Copy from-pool value to [from][to] place in 2d-array:
  2071. transfer_lc_2d[from][to].copy(transfer_lc_from[from]);
  2072. }
  2073. }
  2074. }
  2075. }
  2076. }
  2077. // create and kill stands at landcover change
  2078. stand_dynamics(gridcell);
  2079. error += check_fractions4(gridcell);
  2080. if(error)
  2081. fail("Fraction error after stand_dynamics()\n");
  2082. // transfer litter etc. of reduced stands to expanding stands at landcover change
  2083. for(int to=0; to<NLANDCOVERTYPES; to++) {
  2084. if(gridcell.landcover.pool_from_all_landcovers[to]) {
  2085. // Alt.a: All donor lc:s are pooled into receiving lc:s
  2086. if(gross_landcoverfrac_increase[to] > 0.0) {
  2087. receiving_stand_change(gridcell, transfer_lc[to], LCchangeCtransfer, to);
  2088. }
  2089. }
  2090. else {
  2091. for(int from=0; from<NLANDCOVERTYPES; from++) {
  2092. if(gross_landcoverfrac_transfer[from][to] > 0.0) {
  2093. if(!gridcell.landcover.pool_to_all_landcovers[from]) {
  2094. // Alt.b: Transfers from donors are independent:
  2095. receiving_stand_change(gridcell, transfer_lc_2d[from][to], LCchangeCtransfer, to, -1, gross_landcoverfrac_transfer[from][to] / gross_landcoverfrac_increase[to]);
  2096. }
  2097. else {
  2098. // Alt.c: Donor lc:s are pooled before transfer to recipients:
  2099. receiving_stand_change(gridcell, transfer_lc_from[from], LCchangeCtransfer, to, -1, gross_landcoverfrac_transfer[from][to] / gross_landcoverfrac_increase[to]);
  2100. }
  2101. }
  2102. }
  2103. }
  2104. }
  2105. }
  2106. else if(transfer_level == 2) { // Unique transfers between stand types. Stand type-level pools available by the gridcell variables pool_from_all_landcovers and pool_to_all_landcovers (set in gridcell constructor)
  2107. landcover_change_transfer* transfer_st_2d;
  2108. landcover_change_transfer* transfer_st;
  2109. landcover_change_transfer* transfer_st_from;
  2110. transfer_st_2d = new landcover_change_transfer[nst * nst];
  2111. transfer_st = new landcover_change_transfer[nst];
  2112. transfer_st_from = new landcover_change_transfer[nst];
  2113. // handle harvest and turnover of reduced stands at landcover change
  2114. // pool all transferred area from one landcover:
  2115. for(int from=0; from<nst; from++) {
  2116. StandType& st = stlist[from];
  2117. Gridcellst& gcst = gridcell.st[from];
  2118. // Pooling of donor stands within a stand type
  2119. if(gridcell.landcover.pool_to_all_landcovers[st.landcover] || !(st.landcover == NATURAL || st.landcover == FOREST)) { // alt.c
  2120. if(gcst.gross_frac_decrease > 0.0) {
  2121. donor_stand_change(gridcell, gcst.gross_frac_decrease, transfer_st_from[from], -1, -1, -1, from);
  2122. }
  2123. }
  2124. }
  2125. for(int to=0;to<nst;to++) {
  2126. StandType& st_to = stlist[to];
  2127. Gridcellst& gcst_to = gridcell.st[to];
  2128. double receiving_fraction = gcst_to.gross_frac_increase;
  2129. for(int from=0; from<nst; from++) {
  2130. StandType& st_from = stlist[from];
  2131. Gridcellst& gcst_from = gridcell.st[from];
  2132. double receiving_fraction_2d = st_frac_transfer[index(from, to)];
  2133. if(receiving_fraction_2d > 0.0) {
  2134. if(gridcell.landcover.pool_from_all_landcovers[st_to.landcover]) {
  2135. if(!(gridcell.landcover.pool_to_all_landcovers[st_from.landcover] || !(st_from.landcover == NATURAL || st_from.landcover == FOREST))) { // alt.a
  2136. // Add from->to transfer to to-pool:
  2137. donor_stand_change(gridcell, receiving_fraction, transfer_st[to], -1, -1, to, from);
  2138. }
  2139. else { // alt.a+c
  2140. // Add from-pool value to to-pool:
  2141. transfer_st[to].add(transfer_st_from[from], receiving_fraction_2d / receiving_fraction);
  2142. }
  2143. }
  2144. else {
  2145. if(!(gridcell.landcover.pool_to_all_landcovers[st_from.landcover] || !(st_from.landcover == NATURAL || st_from.landcover == FOREST))) { // alt.b
  2146. // Add from->to transfer to [from][to] place in 2d-array:
  2147. donor_stand_change(gridcell, receiving_fraction_2d, transfer_st_2d[index(from, to)], -1, -1, to, from);
  2148. }
  2149. }
  2150. }
  2151. }
  2152. }
  2153. // create and kill stands at landcover change
  2154. stand_dynamics(gridcell);
  2155. error += check_fractions4(gridcell);
  2156. if(error)
  2157. fail("Fraction error after stand_dynamics()\n");
  2158. // transfer litter etc. of reduced stands to expanding stands at landcover change
  2159. for(int to=0; to<nst; to++) {
  2160. StandType& st = stlist[to];
  2161. Gridcellst& gcst = gridcell.st[to];
  2162. if(gridcell.landcover.pool_from_all_landcovers[st.landcover]) {
  2163. // Alt.a: All donor st:s are pooled into receiving st:s
  2164. if(gcst.gross_frac_increase > 0.0) {
  2165. receiving_stand_change(gridcell, transfer_st[to], LCchangeCtransfer, -1, to);
  2166. }
  2167. }
  2168. else {
  2169. for(int from=0; from<nst; from++) {
  2170. if(st_frac_transfer[index(from, to)] > 0.0) {
  2171. StandType& st_from = stlist[from];
  2172. Gridcellst& gcst_from = gridcell.st[from];
  2173. if(!(gridcell.landcover.pool_to_all_landcovers[st_from.landcover] || !(st_from.landcover == NATURAL || st_from.landcover == FOREST))) {
  2174. // Alt.b: Transfers between donor and receptor st:s are independent:
  2175. receiving_stand_change(gridcell, transfer_st_2d[index(from, to)], LCchangeCtransfer, st.landcover, to, st_frac_transfer[index(from, to)] / gcst.gross_frac_increase);
  2176. }
  2177. else {
  2178. // Alt.c: Donor stands in a stand type are pooled before transfer to recipients:
  2179. receiving_stand_change(gridcell, transfer_st_from[from], LCchangeCtransfer, st.landcover, to, st_frac_transfer[index(from, to)] / gcst.gross_frac_increase);
  2180. }
  2181. }
  2182. }
  2183. }
  2184. }
  2185. if(transfer_st_2d)
  2186. delete[] transfer_st_2d;
  2187. if(transfer_st)
  2188. delete[] transfer_st;
  2189. if(transfer_st_from)
  2190. delete[] transfer_st_from;
  2191. }
  2192. double ccont_tot = 0.0;
  2193. double cflux_tot = gridcell.cflux();
  2194. for(unsigned int i=0; i<gridcell.size(); i++) {
  2195. Stand& stand = gridcell[i];
  2196. double ccont_stand = stand.ccont(stand.scale_LC_change);
  2197. ccont_tot += ccont_stand * stand.get_gridcell_fraction();
  2198. }
  2199. if(!negligible(cflux_tot - cflux_tot_1 + ccont_tot - ccont_tot_1, -11))
  2200. dprintf("WARNING ! C balance after lcc off by %.12f\n",cflux_tot - cflux_tot_1 + ccont_tot - ccont_tot_1);
  2201. double ncont_tot = 0.0;
  2202. double nflux_tot = 0.0;
  2203. for(unsigned int i=0; i<gridcell.size(); i++) {
  2204. Stand& stand = gridcell[i];
  2205. double ncont_stand = stand.ncont(stand.scale_LC_change);
  2206. ncont_tot += ncont_stand * stand.get_gridcell_fraction();
  2207. nflux_tot += stand.nflux() * stand.get_gridcell_fraction();
  2208. }
  2209. nflux_tot_1 = nflux_tot; // Disregard fluxes not already reset this year and the associated scaling problems
  2210. nflux_tot += gridcell.landcover.anflux_landuse_change + gridcell.landcover.anflux_harvest_slow;
  2211. // if(!negligible(nflux_tot - nflux_tot_1 + ncont_tot - ncont_tot_1, CHECK_NLIM+1))
  2212. // dprintf("WARNING ! N balance after lcc off\n");
  2213. if(st_frac_transfer)
  2214. delete[] st_frac_transfer;
  2215. }
  2216. ////////////////////////////////////////////////////////////////////////////////
  2217. // Implementation of landcover_change_transfer member functions
  2218. ////////////////////////////////////////////////////////////////////////////////
  2219. /// landcover_change_transfer constructor
  2220. landcover_change_transfer::landcover_change_transfer() {
  2221. transfer_litter_leaf = transfer_litter_sap = transfer_litter_heart = NULL;
  2222. transfer_litter_root = transfer_litter_repr = transfer_harvested_products_slow = NULL;
  2223. transfer_nmass_litter_leaf = transfer_nmass_litter_sap = transfer_nmass_litter_heart = NULL;
  2224. transfer_nmass_litter_root = transfer_harvested_products_slow_nmass = NULL;
  2225. transfer_acflux_harvest = transfer_anflux_harvest = transfer_cpool_fast = 0.0;
  2226. transfer_cpool_slow = transfer_wcont_evap = transfer_decomp_litter_mean = 0.0;
  2227. transfer_k_soilfast_mean = transfer_k_soilslow_mean = transfer_nmass_avail = 0.0;
  2228. transfer_snowpack = transfer_snowpack_nmass = transfer_anfix_calc = 0.0;
  2229. for(int i=0;i<NSOILLAYER;i++)
  2230. transfer_wcont[i] = 0.0;
  2231. for(int i=0; i<NSOMPOOL; i++)
  2232. transfer_sompool[i].ntoc = 0.0;
  2233. allocate();
  2234. }
  2235. /// landcover_change_transfer deconstructor
  2236. landcover_change_transfer::~landcover_change_transfer() {
  2237. if(transfer_litter_leaf) delete[] transfer_litter_leaf;
  2238. if(transfer_litter_sap) delete[] transfer_litter_sap;
  2239. if(transfer_litter_heart) delete[] transfer_litter_heart;
  2240. if(transfer_litter_root) delete[] transfer_litter_root;
  2241. if(transfer_litter_repr) delete[] transfer_litter_repr;
  2242. if(transfer_harvested_products_slow) delete[] transfer_harvested_products_slow;
  2243. if(transfer_nmass_litter_leaf) delete[] transfer_nmass_litter_leaf;
  2244. if(transfer_nmass_litter_sap) delete[] transfer_nmass_litter_sap;
  2245. if(transfer_nmass_litter_heart) delete[] transfer_nmass_litter_heart;
  2246. if(transfer_nmass_litter_root) delete[] transfer_nmass_litter_root;
  2247. if(transfer_harvested_products_slow_nmass) delete[] transfer_harvested_products_slow_nmass;
  2248. }
  2249. /// allocates memory for landcover_change_transfer object
  2250. void landcover_change_transfer::allocate() {
  2251. transfer_litter_leaf = new double[npft];
  2252. transfer_litter_sap = new double[npft];
  2253. transfer_litter_heart = new double[npft];
  2254. transfer_litter_root = new double[npft];
  2255. transfer_litter_repr = new double[npft];
  2256. transfer_harvested_products_slow = new double[npft];
  2257. transfer_nmass_litter_leaf = new double[npft];
  2258. transfer_nmass_litter_sap = new double[npft];
  2259. transfer_nmass_litter_heart = new double[npft];
  2260. transfer_nmass_litter_root = new double[npft];
  2261. transfer_harvested_products_slow_nmass = new double[npft];
  2262. for(int i=0;i<npft;i++) {
  2263. transfer_litter_leaf[i] = 0.0;
  2264. transfer_litter_sap[i] = 0.0;
  2265. transfer_litter_heart[i] = 0.0;
  2266. transfer_litter_root[i] = 0.0;
  2267. transfer_litter_repr[i] = 0.0;
  2268. transfer_harvested_products_slow[i] = 0.0;
  2269. transfer_nmass_litter_leaf[i] = 0.0;
  2270. transfer_nmass_litter_sap[i] = 0.0;
  2271. transfer_nmass_litter_heart[i] = 0.0;
  2272. transfer_nmass_litter_root[i] = 0.0;
  2273. transfer_harvested_products_slow_nmass[i] = 0.0;
  2274. }
  2275. }
  2276. //////////////////////////////////////////////////////////////////////////////////////////
  2277. // REFERENCES
  2278. //
  2279. // Bondeau A, Smith PC, Zaehle S, Schaphoff S, Lucht W, Cramer W, Gerten D, Lotze-Campen H,
  2280. // Müller C, Reichstein M & Smith B 2007. Modelling the role of agriculture for the
  2281. // 20th century global terrestrial carbon balance. Global Change Biology, 13:679-706.
  2282. // Lindeskog M, Arneth A, Bondeau A, Waha K, Seaquist J, Olin S, & Smith B 2013.
  2283. // Implications of accounting for land use in simulations of ecosystem carbon cycling
  2284. // in Africa. Earth Syst Dynam 4:385-407.