trotsiuk / r3PG

An R package for forest growth simulation using the 3-PG process-based model
https://trotsiuk.github.io/r3PG/
GNU General Public License v3.0
27 stars 16 forks source link

Regeneration during the dormant period cause model crash #102

Open trotsiuk opened 1 week ago

trotsiuk commented 1 week ago

Description

If there is a new cohort regenerated of broadleaf species during the dormant period it seems to cause the model crash. This issue is occurring most likely due to the correct_bias part, where we have the log(age). At this point in time the age is 0 which case the issue.

https://github.com/trotsiuk/r3PG/blob/master/src/md_3PG.f95#L1643

This issue seems not to be a problem if the regeneration occurs during the growing period (not dormant).

Another explanation can be that there are some further allocations related to it.

Code

This is very specific issue. With the code below everything works well those. Need more tests for the code

library(r3PG)
library(dplyr)
library(ggplot2)

# Run with regeneration ---------------------------------------------------

# create a regeneration part

d_species_r <- d_species %>%
  dplyr::mutate( species = paste0(species, '_r'),
                 planted = '2006-01',
                 stems_n = 1000,
                 biom_stem = 1,
                 biom_root = 1,
                 biom_foliage = 1) %>%
  # dplyr::bind_rows(dplyr::mutate(d_species, dplyr::across( c(stems_n, biom_stem, biom_root, biom_foliage), ~.*0.2)), .)
  dplyr::bind_rows( d_species, .)

d_thinning_r <- d_thinning %>%
  dplyr::mutate(stems_n = stems_n * 0.2)

d_parameters_r <- d_parameters %>%
  dplyr::rename_at( vars(-parameter),function(x) paste0(x,"_r") ) %>%
  dplyr::inner_join( d_parameters, ., by = 'parameter')

d_sizeDist_r <- d_sizeDist %>%
  dplyr::rename_at( vars(-parameter),function(x) paste0(x,"_r") ) %>%
  dplyr::inner_join( d_sizeDist, ., by = 'parameter')

out_3PG <- run_3PG(
  site        = d_site, 
  species     = d_species_r, 
  climate     = d_climate, 
  # thinning    = NULL,
  thinning    = d_thinning_r,
  parameters  = d_parameters_r, 
  size_dist   = d_sizeDist_r,
  settings    = list(light_model = 2, transp_model = 2, phys_model = 2, 
                     height_model = 1, correct_bias = 1, calculate_d13c = 0),
  check_input = TRUE, df_out = TRUE)

i_var <- c('stems_n',  'dbh', 'height', 'biom_stem', 'biom_root', 'biom_foliage')
i_lab <- c('Stem density', 'DBH', 'Height', 'Stem biomass', 'Root biomass', 'Foliage biomass')

out_3PG %>%
  filter(variable %in% i_var) %>%
  mutate(variable = factor(variable, levels = i_var)) %>%
  ggplot( aes(date, value))+
  geom_line( aes(color = species), size = 0.5)+
  facet_wrap( ~ variable, scales = 'free_y', ncol = 3, 
              labeller = labeller(variable = setNames(i_lab, i_var) )) +
  scale_color_brewer('', palette = 'Dark2') +
  theme_classic()+
  theme(legend.position="bottom")+
  xlab("Calendar date") + ylab('Value')  

explore.data <- out_3PG %>%
  dplyr::filter( date %in% c(as.Date('2006-12-31'), as.Date('2006-01-31'), as.Date('2006-02-28'), as.Date('2006-03-31'), as.Date('2006-04-30'), as.Date('2006-05-31'))) %>%
  tidyr::pivot_wider( names_from = 'date', values_from = 'value')

image