cboisvenue / spadesCBM

A Carbon Budget family of modules for SpaDES - logic from CBM-CFS3
2 stars 2 forks source link

remake the equivalent of CBM_defaults module #43

Open cboisvenue opened 6 months ago

cboisvenue commented 6 months ago

The CBM_default module was designed to read-in all Canadian parameters that CBM-CFS3 uses. This is akin to the ArchiveIndex in CBM-CFS3 runs. This module let's a user see and find all parameters. The libcbm provides more flexibility then the CBM-CFS3 does and does not access the parameters like CBM-CFS3 did with ArchiveIndex (table the MS Access version). We need to build CBM_default so all Canadian parameters are read in this module and that it runs independently from other spadesCBM modules. This is important for people to be able to change or work with the ecological, forest management, disturbance matrices proportions, etc.

### Tasks
Get the CBM_default to read the same database as Scott's work. Scott's file is here:
https://github.com/cat-cfs/libcbm_py/tree/master/libcbm/resources/cbm_defaults_db
Scott have scripts (libcbm functions?) to generate that same database schema from any CBM3 archive index. 
For the parameters from here: https://github.com/cat-cfs/libcbm_py/tree/cbm_exn/libcbm/model/cbm_exn/parameters
These are different from the old parameters because:
1 the disturbance matrix values have been adjusted to only have the single biomass and snag pools (SW Merch, HW Merch is now just "Merch")
2 decay parameters drop hw and sw specific snag parameters (they were identical)
3 turnover parameters carry a sw/hw column, meaning each row is specific to a spuid,sw_hw combination
4 flux.json - carries fluxes without hw/sw pools
### Things to check that were partly fixed in spadesCBM and may no have made it to CBM/libcbm
Problem:
$allProcess$BioTurnover$[15 matrices]: (e.g., spadesCBMout$allProcesses$BioTurnover$'10')
the proportions did not all to one, carbon was transfered but not subtracted from original pool.
Fix: 
lines were added in the biomassTuroverMatrix function in the spadesCBMdefaultsFunctions.r script (now in the CBMutils package, flowMatrices.R) to ensure that proportions added to one.

Problem:
$allProcess$SlowMixing$'1' (only one matrix of this transaction) was double the amount of carbon in pool "AboveGroundSlowSoil". It was doing the appropriate transfer (0.006 from row 17 - AboveGroundSlowSoil to row 18 - BelowGroundSlowSoil, and 0.994 from row 17 to row 17) but also have a 1 from 17 to 17 because of the creation of the identity matrix as the 1st step of the matrix creation in the function computerSlowMixingMatrix() CBMutils/flowMatrices.R .
Fix:
added a line in the function computeSlowMixingMatrix() to remove the value of "1" for row 17 AboveGroundSlowSoil - this is a line:   mat <- mat[-AboveGroundSlowSoil,]

Problem: NEED TO UPDATE THIS -ISSUE IN THE C++ CODE WAS FOUND
There is a problem in the C++ functions: it does the 1st operation correctly (1st year of simulation is ok but not the second. For example, "disturbance-followed by annual processes". We (Alex Chubaty and CBoisvenue) identified that the matrix used for representing the c-transfer in a stand in a given year was not being re-zeroes in the following year.
Fix:
After hiring two consultants to help with the C++ code. The 1st produced the pseudocode for the c++ (now part of the CBMutils package https://github.com/PredictiveEcology/CBMutils), and the 2nd (Alex Chubaty), was able to identify where the c transfer matrix needed to be cleared. An old branch (saskatchewan) has a r-version of the annual processes (growth 1 domturnover bioturnover overmature growth 2 domDecay  slow decay slow mixing) and an r-version of disturbances in the annual event in the spadesCBMcore.R module.