8000 Minor changes by stineb · Pull Request #217 · geco-bern/rsofun · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Minor changes #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion R/cost_likelihood_biomee.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#' @param targets A character vector indicating the target variables for which the
#' optimization will be done. This should be a subset of \code{c("GPP", "LAI",
#' "Density", "Biomass")}.
#' @param verbose A logical specifying whether to print parameter and log-likelihood
#' values during each iteration of the calibration as message. Defaults to
#' \code{FALSE}
#'
#' @return The log-likelihood of the simulated
#' targets by the biomee model versus the observed targets.
Expand Down Expand Up @@ -46,7 +49,8 @@ cost_likelihood_biomee <- function(
par,
obs,
drivers,
targets
targets,
verbose = FALSE
){

# predefine variables for CRAN check compliance
Expand Down Expand Up @@ -108,6 +112,18 @@ cost_likelihood_biomee <- function(
if(is.nan(ll) || is.na(ll) | ll == 0){
ll <- -Inf
}

if (verbose){
message(
paste("Par = ", paste(drivers$params_species, collapse = ", "))
)
message(
paste("Log-likelihood = ", ll)
)
message(
paste("--------------------------")
)
}

return(ll)
}
18 changes: 17 additions & 1 deletion R/cost_likelihood_pmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#' \code{FALSE}.
#' @param ncores An integer specifying the number of cores used for parallel
#' computing. Defaults to 2.
#' @param verbose A logical specifying whether to print parameter and log-likelihood
#' values during each iteration of the calibration as message. Defaults to
#' \code{FALSE}
#'
#' @return The log-likelihood of the observed target values, assuming that they
#' are independent, normally distributed and centered on the predictions
Expand Down Expand Up @@ -78,7 +81,8 @@ cost_likelihood_pmodel <- function(
targets,
par_fixed = NULL, # non-calibrated model parameters
parallel = FALSE,
ncores = 2
ncores = 2,
verbose = FALSE
){
# predefine variables for CRAN check compliance
sitename <- data <- gpp_mod <- NULL
Expand Down Expand Up @@ -215,6 +219,18 @@ cost_likelihood_pmodel <- function(

# trap boundary conditions
if(is.nan(ll) | is.na(ll) | ll == 0){ll <- -Inf}

if (verbose){
message(
paste("Par = ", paste(params_modl, collapse = ", "))
)
message(
paste("Log-likelihood = ", ll)
)
message(
paste("--------------------------")
)
}

return(ll)
}
Expand Down
18 changes: 17 additions & 1 deletion R/cost_rmse_biomee.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#' @param obs A nested data frame of observations, following the structure of \code{biomee_validation},
#' for example.
#' @param drivers A nested data frame of driver data, for example \code{biomee_gs_leuning_drivers}.
#' @param verbose A logical specifying whether to print parameter and cost (RMSE)
#' values during each iteration of the calibration as message. Defaults to
#' \code{FALSE}
#'
#' @return The root mean squared error (RMSE) between the observed and simulated
#' values of \code{'GPP','LAI','Density'} and \code{'Biomass'} (all variables
Expand All @@ -38,7 +41,8 @@
cost_rmse_biomee <- function(
par,
obs,
drivers
drivers,
verbose = FALSE
){

# predefine variables for CRAN check compliance
Expand Down Expand Up @@ -85,5 +89,17 @@ cost_rmse_biomee <- function(
## Calculate cost (RMSE) across the N targets
cost <- mean(dff$error_rel^2, na.rm = TRUE)

if (verbose){
message(
paste("Par = ", paste(drivers$params_species, collapse = ", "))
)
message(
paste("RMSE = ", cost)
)
message(
paste("--------------------------")
)
}

return(cost)
}
18 changes: 17 additions & 1 deletion R/cost_rmse_pmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#' \code{FALSE}.
#' @param ncores An integer specifying the number of cores used for parallel
#' computing. Defaults to 2.
#' @param verbose A logical specifying whether to print parameter and cost (RMSE)
#' values during each iteration of the calibration as message. Defaults to
#' \code{FALSE}
#'
#' @return The root mean squared error (RMSE) between observed values and P-model
#' predictions. The RMSE is computed for each target separately and then aggregated
Expand Down Expand Up @@ -75,7 +78,8 @@ cost_rmse_pmodel <- function(
target_weights = NULL, # if using several targets, how are the individual
# RMSE weighted? named vector
parallel = FALSE,
ncores = 2
ncores = 2,
verbose = FALSE
){

# predefine variables for CRAN check compliance
Expand Down Expand Up @@ -204,6 +208,18 @@ cost_rmse_pmodel <- function(
}else{
cost <- mean(rmse, na.rm = TRUE)
}

if (verbose){
message(
paste("Par = ", paste(params_modl, collapse = ", "))
)
message(
paste("RMSE = ", cost)
)
message(
paste("--------------------------")
)
}

return(cost)
}
11 changes: 7 additions & 4 deletions R/run_pmodel_f_bysite.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ run_pmodel_f_bysite <- function(
"temp",
"rain",
"vpd",
"ppfd",
"netrad",
"fsun",
"snow",
"co2",
"fapar",
Expand Down Expand Up @@ -282,7 +285,7 @@ run_pmodel_f_bysite <- function(
}

# Check model parameters
if( sum( names(params_modl) %in% c('kphio', 'kphio_par_a', 'kphio_par_b',
if ( sum( names(params_modl) %in% c('kphio', 'kphio_par_a', 'kphio_par_b',
'soilm_thetastar', 'soilm_betao',
'beta_unitcostratio', 'rd_to_vcmax',
'tau_acclim', 'kc_jmax')
Expand All @@ -302,13 +305,13 @@ run_pmodel_f_bysite <- function(
in_netrad <- FALSE # net radiation is currently ignored as a model forcing, but is internally simulated by SPLASH.

# Check if fsun is available
if(! (in_ppfd & in_netrad)){
if (! (in_ppfd & in_netrad)){
# fsun must be available when one of ppfd or netrad is missing
if(any(is.na(forcing$fsun))) continue <- FALSE
if (any(is.na(forcing$fsun))) continue <- FALSE
}
}

if(continue){
if (continue){

# convert to matrix
forcing <- as.matrix(forcing)
Expand Down
19 changes: 16 additions & 3 deletions analysis/demo_bug.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ library(dplyr)
library(ggplot2)
library(purrr)

## BiomeE (original with gs-leuning) -----------
## BiomeE -----------
# original with gs-leuning photosynthesis
nruns <- 3
test_biomee_gs_leuning <- function(){
# run the model
Expand All @@ -26,7 +27,13 @@ vec_test <- purrr::map_lgl(
# did any simulation fail?
if (any(!vec_test)){
stop(
"At least one BiomeE-gs-leuning simulation failed."
paste(
"At least one BiomeE-gs-leuning simulation failed. Successful run? ",
paste(
vec_test,
collapse = ", "
)
)
)
} else {
message(
Expand Down Expand Up @@ -57,7 +64,13 @@ vec_test <- purrr::map_lgl(
# did any simulation fail?
if (any(!vec_test)){
stop(
"At least one BiomeEP simulation failed."
paste(
"At least one BiomeEP simulation failed. Successful run? ",
paste(
vec_test,
collapse = ", "
)
)
)
} else {
message(
Expand Down
6 changes: 5 additions & 1 deletion man/cost_likelihood_biomee.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/cost_likelihood_pmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/cost_rmse_biomee.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/cost_rmse_pmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Makevars.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# -fsanitize=address: Using the Address Sanitizer https://rstudio.github.io/r-manuals/r-exts/Debugging.html#using-the-address-sanitizer
# PKG_FFLAGS = -frecursive -fbounds-check -Wall -Wextra -Wpedantic -g -O0 -fsanitize=address

# PKG_FFLAGS = -g -fsanitize=address

# C objects
C_OBJS = wrappersc.o

Expand Down
4 changes: 2 additions & 2 deletions src/vegetation_biomee.mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ subroutine vegn_CNW_budget( vegn, forcing, init )
do i = 1, vegn%n_cohorts

cc => vegn%cohorts(i)
associate ( sp => spdata(cc%species) )
! associate ( sp => spdata(cc%species) )

! increment the cohort age
cc%age = cc%age + myinterface%dt_fast_yr
Expand All @@ -76,7 +76,7 @@ subroutine vegn_CNW_budget( vegn, forcing, init )
cc%plabl%c%c12 = cc%plabl%c%c12 + cc%npp
cc%plabl%n%n14 = cc%plabl%n%n14 + cc%fixedN

end associate
! end associate
enddo ! all cohorts

! update soil carbon
Expand Down
3 changes: 2 additions & 1 deletion vignettes/new_cost_function.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pars_calib_rmse <- calib_sofun(
tau_acclim = 30.0,
kc_jmax = 0.41
),
targets = "gpp" # define target variable GPP
targets = "gpp", # define target variable GPP
verbose = FALSE # set to TRUE to print parameter and cost values during each iteration of the calibration
)
```

Expand Down
1 change: 1 addition & 0 deletions vignettes/pmodel_use.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,4 @@ ggplot(data = df_gpp_plot) +
```

For details on the optimization settings we refer to the manuals of [GenSA](https://cran.r-project.org/package=GenSA) and [BayesianTools](https://github.com/florianhartig/BayesianTools).

Loading
0