From 2614278006609bcc10549521b3528851c33aa89f Mon Sep 17 00:00:00 2001 From: Christine Stawitz - NOAA <47904621+ChristineStawitz-NOAA@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:41:55 -0800 Subject: [PATCH] special case last year of recdevs --- .../interface/rcpp/rcpp_objects/rcpp_recruitment.hpp | 2 +- inst/include/population_dynamics/population/population.hpp | 7 +++++++ tests/testthat/test-fims-estimation.R | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp index 69d48e928..1b7fe205c 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp @@ -148,7 +148,7 @@ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase { } } - recruitment->log_recruit_devs.resize(this->log_devs.size()+1); + recruitment->log_recruit_devs.resize(this->log_devs.size()); if (this->estimate_log_devs) { for (size_t i = 0; i < recruitment->log_recruit_devs.size(); i++) { recruitment->log_recruit_devs[i] = this->log_devs[i]; diff --git a/inst/include/population_dynamics/population/population.hpp b/inst/include/population_dynamics/population/population.hpp index 2cb8abba2..fc6b48ff4 100644 --- a/inst/include/population_dynamics/population/population.hpp +++ b/inst/include/population_dynamics/population/population.hpp @@ -391,6 +391,12 @@ struct Population : public fims_model_object::FIMSObject { << this->recruitment->evaluate( this->spawning_biomass[year - 1], phi0) << std::endl; + if(i_dev==this->nyears){ + this->numbers_at_age[i_age_year] = + this->recruitment->evaluate(this->spawning_biomass[year - 1], phi0); + /*the final year of the time series has no data to inform recruitment devs, + so this value is set to the mean recruitment.*/ + } else{ this->numbers_at_age[i_age_year] = this->recruitment->evaluate(this->spawning_biomass[year - 1], phi0) * /*the log_recruit_dev vector does not include a value for year == 0 @@ -398,6 +404,7 @@ struct Population : public fims_model_object::FIMSObject { corresponds to the second year of the time series.*/ fims_math::exp(this->recruitment->log_recruit_devs[i_dev-1]); this->expected_recruitment[year] = this->numbers_at_age[i_age_year]; + } POPULATION_LOG << " numbers at age at index i_age_year " << i_age_year << " is " << this->numbers_at_age[i_age_year] << std::endl; } diff --git a/tests/testthat/test-fims-estimation.R b/tests/testthat/test-fims-estimation.R index 2b42ab2fb..abe99c4a1 100644 --- a/tests/testthat/test-fims-estimation.R +++ b/tests/testthat/test-fims-estimation.R @@ -691,12 +691,12 @@ test_that("run FIMS in a for loop", { fims$CreateTMBModel() parameters <- list(p = fims$get_fixed()) obj <- TMB::MakeADFun(data = list(), parameters, DLL = "FIMS") - + opt <- with(obj, optim(par, fn, gr, method = "BFGS", control = list(maxit = 1000000, reltol = 1e-15) )) - + report <- obj$report(obj$par) expect_false(is.null(report))