diff --git a/.github/workflows/pr-checklist.yml b/.github/workflows/pr-checklist.yml index ed1d7c4b2..6f258dfdd 100644 --- a/.github/workflows/pr-checklist.yml +++ b/.github/workflows/pr-checklist.yml @@ -3,7 +3,7 @@ name: Add a comment with reviewer checklist when PR opened on: pull_request: - + types: [opened] jobs: pr-checklist: runs-on: ubuntu-latest diff --git a/.github/workflows/run-googletest.yml b/.github/workflows/run-googletest.yml index 97897b1f0..69779d5d0 100644 --- a/.github/workflows/run-googletest.yml +++ b/.github/workflows/run-googletest.yml @@ -14,7 +14,8 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + # Use windows-2019 instead of windows-latest to avoid using Mingw-w64 + os: [ubuntu-latest, macos-latest, windows-2019] env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} diff --git a/R/create_rcpp_interface_object.R b/R/create_rcpp_interface_object.R index ccf44336a..464df170f 100644 --- a/R/create_rcpp_interface_object.R +++ b/R/create_rcpp_interface_object.R @@ -24,7 +24,7 @@ #' model = "LogisticSelectivity", #' base_class = "SelectivityInterfaceBase", #' container = "selectivity_models", -#' parameters = c("slope", "median"), +#' parameters = c("slope", "inflection_point"), #' evaluate_parameter = "x", #' evaluate_parameter_type = "double" #' ) @@ -69,7 +69,7 @@ create_fims_rcpp_interface <- function(interface_name = character(), if (!is.null(evaluate_parameter)) { cat(paste0(" virtual double evaluate(", evaluate_parameter_type, " ", evaluate_parameter, ") {\n")) - cat(paste0(" fims::", model, " object;\n")) + cat(paste0(" fims_popdy::", model, " object;\n")) for (i in 1:length(parameters)) { cat(paste0(" object.", parameters[i], " = this->", parameters[i], ".value;\n")) } @@ -78,20 +78,20 @@ create_fims_rcpp_interface <- function(interface_name = character(), cat(" virtual bool add_to_fims_tmb(){\n") for (i in 1:4) { - cat(paste0(" std::shared_ptr >", itypes[i])) cat(" =\n") - cat(" fims::Information<") + cat(" fims_info::Information<") cat(types[i]) cat(">::GetInstance();\n\n") - cat(" std::shared_ptr > ") cat(mtypes[i]) cat(" =\n") - cat(" std::make_shared -struct DataObject : public fims::FIMSObject { +struct DataObject : public fims_model_object::FIMSObject { static uint32_t id_g; /*!< id of the Data Object >*/ std::vector data; /*!< vector of the data >*/ size_t dimensions; /*!< dimension of the Data object >*/ @@ -225,6 +225,6 @@ struct DataObject : public fims::FIMSObject { template uint32_t DataObject::id_g = 0; -} // namespace fims +} // namespace fims_data_object #endif diff --git a/inst/include/common/def.hpp b/inst/include/common/def.hpp index 38730fd53..85756b93b 100644 --- a/inst/include/common/def.hpp +++ b/inst/include/common/def.hpp @@ -15,7 +15,27 @@ #include #include -std::ofstream FIMS_LOG("fims.log"); /**< Log file */ +// The following rows initialize default log files for outputing model progress +// comments used to assist in diagnosing model issues and tracking progress. +// These files will only be created if a logs folder is added to the root model +// directory. +std::ofstream FIMS_LOG("logs/fims.log"); /**< Generic log file */ +std::ofstream INFO_LOG("logs/info.log"); /**< Information.hpp log file */ +std::ofstream ERROR_LOG("logs/error.log"); /**< Error tracking log file */ +std::ofstream DATA_LOG("logs/data.log"); /**< Data input tracking log file */ +std::ofstream MODEL_LOG("logs/model.log"); /**< Model.hpp log file */ +std::ofstream FLEET_LOG("logs/fleet.log"); /**< Fleet module log file */ +std::ofstream POPULATION_LOG( + "logs/population.log"); /**< Populations module log file */ +std::ofstream RECRUITMENT_LOG( + "logs/recruitment.log"); /**< Recruitment module log file */ +std::ofstream GROWTH_LOG("logs/growth.log"); /**< Growth module log file */ +std::ofstream MATURITY_LOG( + "logs/maturity.log"); /**< Maturity module log file */ +std::ofstream SELECTIVITY_LOG( + "logs/selectivity.log"); /**< Selectivity module log file */ +std::ofstream DEBUG_LOG( + "logs/debug/debug.log"); /**< Development debugging log file */ #ifdef TMB_MODEL // simplify access to singletons @@ -58,7 +78,7 @@ std::map fims_log::FIMS_LOGS; * @brief Default trait. These are "T" specific * traits that depend on modeling platform. */ -template +template struct ModelTraits { typedef double real_t; /**< The real type */ typedef double variable_t; /**< The variable type */ diff --git a/inst/include/common/fims_math.hpp b/inst/include/common/fims_math.hpp index d1e4f0e91..6665406bf 100644 --- a/inst/include/common/fims_math.hpp +++ b/inst/include/common/fims_math.hpp @@ -21,28 +21,28 @@ #include "../interface/interface.hpp" -namespace fims { +namespace fims_math { #ifdef STD_LIB /** * @brief The exponential function. * - * @param x value to exponentiate. Please use fims::exp(x) if x is an - * integer. + * @param x value to exponentiate. Please use fims_math::exp(x) if x is + * an integer. * @return the exponentiated value */ -template -inline const T exp(const T &x) { +template +inline const Type exp(const Type &x) { return std::exp(x); } /** * @brief The natural log function (base e) - * @param x the value to take the log of. Please use fims::log(x) if x - * is an integer. + * @param x the value to take the log of. Please use fims_math::log(x) + * if x is an integer. * @return */ -template -inline const T log(const T &x) { +template +inline const Type log(const Type &x) { return std::log(x); } #endif @@ -54,12 +54,12 @@ inline const T log(const T &x) { * @brief The exponential function. * The code cannot be tested using the compilation flag * -DTMB_MODEL through CMake and Google Test - * @param x value to exponentiate. Please use fims::exp(x) if x is an - * integer. + * @param x value to exponentiate. Please use fims_math::exp(x) if x is + * an integer. * @return the exponentiated value */ -template -inline const T exp(const T &x) { +template +inline const Type exp(const Type &x) { using ::exp; return exp(x); } @@ -73,12 +73,12 @@ inline const double exp(const double &x) { * @brief The natural log function (base e) * The code cannot be tested using the compilation flag * -DTMB_MODEL through CMake and Google Test. - * @param x the value to take the log of. Please use fims::log(x) if x - * is an integer. + * @param x the value to take the log of. Please use fims_math::log(x) + * if x is an integer. * @return the log of the value */ -template -inline const T log(const T &x) { +template +inline const Type log(const Type &x) { return log(x); } @@ -92,16 +92,17 @@ inline const double log(const double &x) { /** * @brief The general logistic function * - * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - median))} \f$ + * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - inflection_point))} \f$ * - * @param median the median (inflection point) of the logistic function + * @param inflection_point the inflection point of the logistic function * @param slope the slope of the logistic function * @param x the index the logistic function should be evaluated at * @return */ -template -inline const T logistic(const T &median, const T &slope, const T &x) { - return (1.0) / (1.0 + exp(-1.0 * slope * (x - median))); +template +inline const Type logistic(const Type &inflection_point, const Type &slope, + const Type &x) { + return (1.0) / (1.0 + exp(-1.0 * slope * (x - inflection_point))); } /** @@ -114,9 +115,9 @@ inline const T logistic(const T &median, const T &slope, const T &x) { * @return the parameter in real space * */ -template -inline const T logit(const T &a, const T &b, const T &x) { - return -fims::log(b - x) + fims::log(x - a); +template +inline const Type logit(const Type &a, const Type &b, const Type &x) { + return -fims_math::log(b - x) + fims_math::log(x - a); } /** @@ -129,36 +130,39 @@ inline const T logit(const T &a, const T &b, const T &x) { * @return the parameter in bounded space * */ -template -inline const T inv_logit(const T &a, const T &b, const T &logit_x) { - return a + (b - a) / (1 + fims::exp(-logit_x)); +template +inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) { + return a + (b - a) / (1.0 + fims_math::exp(-logit_x)); } /** * @brief The general double logistic function * - * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - median_{asc}))} - * \left(1-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - median_{desc}))} - * \right)\f$ + * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - inflection_point_{asc}))} + * \left(1-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - + * inflection_point_{desc}))} \right)\f$ * - * @param median_asc the median (inflection point) of the ascending limb of the + * @param inflection_point_asc the inflection point of the ascending limb of the * double logistic function * @param slope_asc the slope of the ascending limb of the double logistic * function - * @param median_desc the median (inflection point) of the descending limb of - * the double logistic function, where median_desc > median_asc + * @param inflection_point_desc the inflection point of the descending limb of + * the double logistic function, where inflection_point_desc > + * inflection_point_asc * @param slope_desc the slope of the descending limb of the double logistic * function * @param x the index the logistic function should be evaluated at * @return */ -template -inline const T double_logistic(const T &median_asc, const T &slope_asc, - const T &median_desc, const T &slope_desc, - const T &x) { - return (1.0) / (1.0 + exp(-1.0 * slope_asc * (x - median_asc))) * - (1.0 - (1.0) / (1.0 + exp(-1.0 * slope_desc * (x - median_desc)))); +template +inline const Type double_logistic(const Type &inflection_point_asc, + const Type &slope_asc, + const Type &inflection_point_desc, + const Type &slope_desc, const Type &x) { + return (1.0) / (1.0 + exp(-1.0 * slope_asc * (x - inflection_point_asc))) * + (1.0 - + (1.0) / (1.0 + exp(-1.0 * slope_desc * (x - inflection_point_desc)))); } /** @@ -174,8 +178,8 @@ inline const T double_logistic(const T &median_asc, const T &slope_asc, * @param C default = 1e-5 * @return */ -template -const T ad_fabs(const T &x, T C = 1e-5) { +template +const Type ad_fabs(const Type &x, Type C = 1e-5) { return sqrt((x * x) + C); //, .5); } @@ -183,8 +187,8 @@ const T ad_fabs(const T &x, T C = 1e-5) { * * Returns the minimum between a and b in a continuous manner using: * - * (a + b - fims::ad_fabs(a - b))*.5; - * Reference: \ref fims::ad_fabs() + * (a + b - fims_math::ad_fabs(a - b))*.5; + * Reference: \ref fims_math::ad_fabs() * * This is an approximation with minimal error. * @@ -193,16 +197,17 @@ const T ad_fabs(const T &x, T C = 1e-5) { * @param C default = 1e-5 * @return */ -template -inline const T ad_min(const T &a, const T &b, T C = 1e-5) { - return (a + b - fims::ad_fabs(a - b, C)) * .5; + +template +inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) { + return (a + b - fims_math::ad_fabs(a - b, C)) * 0.5; } /** * Returns the maximum between a and b in a continuous manner using: * - * (a + b + fims::ad_fabs(a - b)) *.5; - * Reference: \ref fims::ad_fabs() + * (a + b + fims_math::ad_fabs(a - b)) *.5; + * Reference: \ref fims_math::ad_fabs() * This is an approximation with minimal error. * * @param a @@ -212,9 +217,9 @@ inline const T ad_min(const T &a, const T &b, T C = 1e-5) { */ template inline const Type ad_max(const Type &a, const Type &b, Type C = 1e-5) { - return (a + b + fims::ad_fabs(a - b, C)) * static_cast(.5); + return (a + b + fims_math::ad_fabs(a - b, C)) * static_cast(.5); } -} // namespace fims +} // namespace fims_math #endif /* FIMS_MATH_HPP */ diff --git a/inst/include/common/information.hpp b/inst/include/common/information.hpp index 75fb231de..9111d1219 100644 --- a/inst/include/common/information.hpp +++ b/inst/include/common/information.hpp @@ -22,95 +22,95 @@ #include "def.hpp" #include "model_object.hpp" -namespace fims { +namespace fims_info { /** * @brief Stores FIMS model information and creates model. Contains all objects * and data pre-model construction */ -template +template class Information { public: size_t nyears; /**< number of years >*/ size_t nseasons = 1; /**< number of seasons >*/ size_t nages; /**< number of ages>*/ - static std::shared_ptr > - fims_information; /**< singleton instance >*/ - std::vector parameters; /**< list of all estimated parameters >*/ - std::vector + static std::shared_ptr > + fims_information; /**< singleton instance >*/ + std::vector parameters; /**< list of all estimated parameters >*/ + std::vector random_effects_parameters; /**< list of all random effects parameters >*/ - std::vector + std::vector fixed_effects_parameters; /**< list of all fixed effects parameters >*/ // data objects - std::map > > + std::map > > data_objects; /*!< map that holds data objects >*/ - typedef typename std::map > >::iterator + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator data_iterator; /**< iterator for the data objects */ // life history modules - std::map > > + std::map > > recruitment_models; /*! > >::iterator - recruitment_models_iterator; + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator + recruitment_models_iterator; /**< iterator for recruitment objects>*/ - std::map > > + std::map > > selectivity_models; /*! > >::iterator - selectivity_models_iterator; + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator + selectivity_models_iterator; /**< iterator for selectivity objects>*/ - std::map > > + std::map > > growth_models; /*! > >::iterator + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator growth_models_iterator; /**< iterator for growth objects>*/ - std::map > > + std::map > > maturity_models; /*! > >::iterator + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator maturity_models_iterator; /**< iterator for maturity objects>*/ // fleet modules - std::map > > + std::map > > fleets; /*! > >::iterator + typename std::map > >::iterator fleet_iterator; /**< iterator for fleet objects>*/ // populations - std::map > > + std::map > > populations; /*! > >::iterator + typedef typename std::map< + uint32_t, std::shared_ptr > >::iterator population_iterator; /**< iterator for population objects>*/ // distributions - std::map > > + std::map > > distribution_models; /*! > >::iterator - distribution_models_iterator; + typedef typename std::map< + uint32_t, + std::shared_ptr > >::iterator + distribution_models_iterator; /**< iterator for distribution objects>*/ Information() {} @@ -122,12 +122,12 @@ class Information { * * @return singleton for type T */ - static std::shared_ptr > GetInstance() { - if (Information::fims_information == nullptr) { - Information::fims_information = - std::make_shared >(); + static std::shared_ptr > GetInstance() { + if (Information::fims_information == nullptr) { + Information::fims_information = + std::make_shared >(); } - return Information::fims_information; + return Information::fims_information; } /** @@ -135,14 +135,16 @@ class Information { * * @param p */ - void RegisterParameter(T& p) { this->fixed_effects_parameters.push_back(&p); } + void RegisterParameter(Type& p) { + this->fixed_effects_parameters.push_back(&p); + } /** * Register a random effect as estimable. * * @param re */ - void RegisterRandomEffect(T& re) { + void RegisterRandomEffect(Type& re) { this->random_effects_parameters.push_back(&re); } @@ -150,85 +152,99 @@ class Information { * Create the generalized stock assessment model that will evaluate the * objective function. Does error checking to make sure the program has * all necessary components for the model and that they're in the right - * dimensions. + * dimensions. This sets up pointers to all memory objects and initializes + * fleet and population objects. * - * @return + * @return True if valid model, False if invalid model, check fims.log for + * errors. */ bool CreateModel() { bool valid_model = true; - FIMS_LOG + INFO_LOG << "" << std::endl; + INFO_LOG << "Beginning to create FIMS model in information.hpp CreateModel(). " << std::endl; - FIMS_LOG << "Initializing fleet objects for " << this->fleets.size() + INFO_LOG << "Initializing fleet objects for " << this->fleets.size() << " fleets." << std::endl; for (fleet_iterator it = this->fleets.begin(); it != this->fleets.end(); ++it) { // Initialize fleet object - std::shared_ptr > f = (*it).second; - FIMS_LOG << "Initializing fleet " << f->id << "." << std::endl; + + std::shared_ptr > f = (*it).second; + INFO_LOG << "Initializing fleet " << f->id << "." << std::endl; + f->Initialize(f->nyears, f->nages); - FIMS_LOG << "Expecting to import " << this->data_objects.size() + INFO_LOG << "Expecting to import " << this->data_objects.size() << " data objects." << std::endl; - FIMS_LOG << "Checking for available fleet index data objects." + INFO_LOG << "Checking for available fleet index data objects." << std::endl; // set index data if (f->fleet_observed_index_data_id_m != -999) { uint32_t observed_index_id = static_cast(f->fleet_observed_index_data_id_m); data_iterator it = this->data_objects.find(observed_index_id); - FIMS_LOG << "Input fleet index id = " << observed_index_id << "." + INFO_LOG << "Input fleet index id = " << observed_index_id << "." << std::endl; if (it != this->data_objects.end()) { f->observed_index_data = (*it).second; - FIMS_LOG << "Index data successfully set." << std::endl; - FIMS_LOG << "Observed input index: \n " << f->observed_index_data - << std::endl; + INFO_LOG << "Index data successfully set." << std::endl; + DATA_LOG << "" << std::endl; + DATA_LOG << "Observed input for fleet " << f->id << ", index " + << observed_index_id << ": \n " + << f->observed_index_data->at(1) << std::endl; } else { valid_model = false; - FIMS_LOG << "Error: Expected data observations not defined for fleet" - << f->id << ", index " << observed_index_id << std::endl; + ERROR_LOG << "Error: Expected data observations not defined for fleet" + << f->id << ", index " << observed_index_id << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No index data observed for fleet " << f->id - << ". FIMS requires index data for all fleets." << std::endl; + ERROR_LOG << "Error: No index data observed for fleet " << f->id + << ". FIMS requires index data for all fleets." << std::endl; + exit(1); } // end set index data - FIMS_LOG << "Checking for available fleet age comp data objects." + INFO_LOG << "Checking for available fleet age comp data objects." << std::endl; // set age composition data if (f->fleet_observed_agecomp_data_id_m != -999) { uint32_t observed_agecomp_id = static_cast(f->fleet_observed_agecomp_data_id_m); data_iterator it = this->data_objects.find(observed_agecomp_id); - FIMS_LOG << "Input fleet age comp id = " << observed_agecomp_id << "." + INFO_LOG << "Input fleet age comp id = " << observed_agecomp_id << "." << std::endl; if (it != this->data_objects.end()) { f->observed_agecomp_data = (*it).second; - FIMS_LOG << "Age comp data successfully set." << std::endl; - FIMS_LOG << "Observed input age comp: \n " << f->observed_agecomp_data - << std::endl; + INFO_LOG << "Age comp data successfully set." << std::endl; + DATA_LOG << "" << std::endl; + DATA_LOG << "Observed input age comp for fleet " << f->id << ", comp " + << observed_agecomp_id << ": \n " + << f->observed_agecomp_data->at(1) << std::endl; } else { valid_model = false; - FIMS_LOG << "Error: Expected data observations not defined for fleet " - << f->id << ", index " << observed_agecomp_id << std::endl; + ERROR_LOG << "Error: Expected age comp data observations not defined " + "for fleet " + << f->id << ", index " << observed_agecomp_id << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No age comp data observed for fleet " << f->id - << ". FIMS requires age comp data for all fleets." - << std::endl; + ERROR_LOG << "Error: No age comp data observed for fleet " << f->id + << ". FIMS requires age comp data for all fleets." + << std::endl; + exit(1); } // end set composition data - FIMS_LOG << "Checking for available fleet selectivity pattern." + INFO_LOG << "Checking for available fleet selectivity pattern." << std::endl; // set selectivity model if (f->fleet_selectivity_id_m != -999) { @@ -236,30 +252,32 @@ class Information { f->fleet_selectivity_id_m); // cast as unsigned integer selectivity_models_iterator it = this->selectivity_models.find( sel_id); // if find, set it, otherwise invalid - FIMS_LOG << "Input fleet selectivity pattern id = " << sel_id << "." + INFO_LOG << "Input fleet selectivity pattern id = " << sel_id << "." << std::endl; if (it != this->selectivity_models.end()) { f->selectivity = (*it).second; // elements in container held in pair // (first is id, second is object - // shared pointer to distribution) - FIMS_LOG << "Selectivity successfully set." << std::endl; + INFO_LOG << "Selectivity successfully set." << std::endl; } else { valid_model = false; - FIMS_LOG + ERROR_LOG << "Error: Expected selectivity pattern not defined for fleet " << f->id << ", selectivity pattern " << sel_id << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No selectivity pattern defined for fleet " << f->id - << ". FIMS requires selectivity be defined for all fleets." - << std::endl; + ERROR_LOG << "Error: No selectivity pattern defined for fleet " << f->id + << ". FIMS requires selectivity be defined for all fleets." + << std::endl; + exit(1); } // end set selectivity - FIMS_LOG << "Checking for available index likelihood function." + INFO_LOG << "Checking for available index likelihood function." << std::endl; // set index likelihood if (f->fleet_index_likelihood_id_m != -999) { @@ -267,7 +285,7 @@ class Information { f->fleet_index_likelihood_id_m); // cast as unsigned integer distribution_models_iterator it = this->distribution_models.find( ind_like_id); // if find, set it, otherwise invalid - FIMS_LOG << "Input index likelihood function id = " << ind_like_id + INFO_LOG << "Input index likelihood function id = " << ind_like_id << "." << std::endl; if (it != this->distribution_models.end()) { @@ -275,26 +293,28 @@ class Information { (*it).second; // elements in container held in pair (first is // id, second is object - shared pointer to // distribution) - FIMS_LOG << "Index likelihood function successfully set." + INFO_LOG << "Index likelihood function successfully set." << std::endl; } else { - valid_model = false; - FIMS_LOG << "Error: Expected index likelihood function not defined " - "for fleet " - << f->id << ", likelihood function " << ind_like_id - << std::endl; + // Commented out for now as code uses single likelihood function + // making this fail valid_model = false; ERROR_LOG << "Error: Expected + // index likelihood function not defined for fleet " + // << f->id << ", likelihood function " << ind_like_id << + // std::endl; + // exit(1); } } else { - valid_model = false; - FIMS_LOG - << "Error: No index likelihood function defined for fleet " << f->id - << ". FIMS requires likelihood functions be defined for all data." - << std::endl; + // Commented out for now as code uses single likelihood function making + // this fail valid_model = false; ERROR_LOG << "Error: No index + // likelihood function defined for fleet " << f->id + // << ". FIMS requires likelihood functions be defined for all + // data." << std::endl; + // exit(1); } // end set index likelihood - FIMS_LOG << "Checking for available age comp likelihood function." + INFO_LOG << "Checking for available age comp likelihood function." << std::endl; // set agecomp likelihood if (f->fleet_agecomp_likelihood_id_m != -999) { @@ -302,7 +322,7 @@ class Information { f->fleet_agecomp_likelihood_id_m); // cast as unsigned integer distribution_models_iterator it = this->distribution_models.find( ac_like_id); // if find, set it, otherwise invalid - FIMS_LOG << "Input age comp likelihood function id = " << ac_like_id + INFO_LOG << "Input age comp likelihood function id = " << ac_like_id << "." << std::endl; if (it != this->distribution_models.end()) { @@ -310,87 +330,90 @@ class Information { (*it).second; // elements in container held in pair (first is // id, second is object - shared pointer to // distribution) - FIMS_LOG << "Age comp likelihood function successfully set." + INFO_LOG << "Age comp likelihood function successfully set." << std::endl; } else { - valid_model = false; - FIMS_LOG << "Error: Expected age comp likelihood function not " - "defined for fleet " - << f->id << ", likelihood function " << ac_like_id - << std::endl; + // Commented out for now as code uses single likelihood function + // making this fail valid_model = false; ERROR_LOG << "Error: Expected + // age comp likelihood function not defined for fleet " + // << f->id << ", likelihood function " << ac_like_id << + // std::endl; + // exit(1); } } else { - valid_model = false; - FIMS_LOG - << "Error: No age comp likelihood function defined for fleet " - << f->id - << ". FIMS requires likelihood functions be defined for all data." - << std::endl; + // Commented out for now as code uses single likelihood function making + // this fail valid_model = false; ERROR_LOG << "Error: No age comp + // likelihood function defined for fleet " << f->id + // << ". FIMS requires likelihood functions be defined for all + // data." << std::endl; + // exit(1); } // end set agecomp likelihood - FIMS_LOG << "Completed initialization for fleet " << f->id << "." + INFO_LOG << "Completed initialization for fleet " << f->id << "." << std::endl; } // close fleet iterator loop - FIMS_LOG << "Completed initialization of all fleets." << std::endl; + INFO_LOG << "Completed initialization of all fleets." << std::endl; - FIMS_LOG << "Initializing population objects for " + INFO_LOG << "Initializing population objects for " << this->populations.size() << " populations." << std::endl; for (population_iterator it = this->populations.begin(); it != this->populations.end(); ++it) { - std::shared_ptr > p = (*it).second; + std::shared_ptr > p = (*it).second; - FIMS_LOG << "Setting up links from population " << p->id + INFO_LOG << "Setting up links from population " << p->id << " to fleets [ " << std::flush; // error check and set population elements // check me - add another fleet iterator to push information from for (fleet_iterator it = this->fleets.begin(); it != this->fleets.end(); ++it) { // Initialize fleet object - std::shared_ptr > f = (*it).second; + std::shared_ptr > f = (*it).second; // population to the individual fleets This is to pass catch at age // from population to fleets? // any shared member in p (population is pushed into fleets) p->fleets.push_back(f); - FIMS_LOG << f->id << " " << std::flush; + INFO_LOG << f->id << " " << std::flush; } - FIMS_LOG << "]" << std::endl; + INFO_LOG << "]" << std::endl; - FIMS_LOG << "Initializing population " << p->id << "." << std::endl; + INFO_LOG << "Initializing population " << p->id << "." << std::endl; p->Initialize(p->nyears, p->nseasons, p->nages); - FIMS_LOG << "Checking for available recruitment function." << std::endl; + INFO_LOG << "Checking for available recruitment function." << std::endl; // set recruitment if (p->recruitment_id != -999) { uint32_t recruitment_uint = static_cast(p->recruitment_id); recruitment_models_iterator it = this->recruitment_models.find(recruitment_uint); - FIMS_LOG << "Input recruitment id = " << recruitment_uint << "." + INFO_LOG << "Input recruitment id = " << recruitment_uint << "." << std::endl; if (it != this->recruitment_models.end()) { p->recruitment = (*it).second; // recruitment defined in population.hpp - FIMS_LOG << "Recruitment function successfully set." << std::endl; + INFO_LOG << "Recruitment function successfully set." << std::endl; } else { valid_model = false; - FIMS_LOG << "Error: Expected recruitment function not defined for " - "population " - << p->id << ", recruitment function " << recruitment_uint - << std::endl; + ERROR_LOG << "Error: Expected recruitment function not defined for " + "population " + << p->id << ", recruitment function " << recruitment_uint + << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No recruitment function defined for population " - << p->id - << ". FIMS requires recruitment functions be defined for all " - "populations." - << std::endl; + ERROR_LOG << "Error: No recruitment function defined for population " + << p->id + << ". FIMS requires recruitment functions be defined for all " + "populations." + << std::endl; + exit(1); } - FIMS_LOG << "Checking for available growth function." << std::endl; + INFO_LOG << "Checking for available growth function." << std::endl; // set growth if (p->growth_id != -999) { uint32_t growth_uint = static_cast(p->growth_id); @@ -399,58 +422,63 @@ class Information { // and used in rcpp // at the head of information.hpp; are the // dimensions of ages defined in rcpp or where? - FIMS_LOG << "Input growth id = " << growth_uint << "." << std::endl; + INFO_LOG << "Input growth id = " << growth_uint << "." << std::endl; if (it != this->growth_models.end()) { p->growth = (*it).second; // growth defined in population.hpp (the object // is called p, growth is within p) - FIMS_LOG << "Growth function successfully set." << std::endl; + INFO_LOG << "Growth function successfully set." << std::endl; } else { valid_model = false; - FIMS_LOG + ERROR_LOG << "Error: Expected growth function not defined for population " << p->id << ", growth function " << growth_uint << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No growth function defined for population " << p->id - << ". FIMS requires growth functions be defined for all " - "populations." - << std::endl; + ERROR_LOG << "Error: No growth function defined for population " + << p->id + << ". FIMS requires growth functions be defined for all " + "populations." + << std::endl; + exit(1); } - FIMS_LOG << "Checking for available maturity function." << std::endl; + INFO_LOG << "Checking for available maturity function." << std::endl; // set maturity if (p->maturity_id != -999) { uint32_t maturity_uint = static_cast(p->maturity_id); maturity_models_iterator it = this->maturity_models.find( maturity_uint); // >maturity_models is specified in // information.hpp and used in rcpp - FIMS_LOG << "Input maturity id = " << maturity_uint << "." << std::endl; + INFO_LOG << "Input maturity id = " << maturity_uint << "." << std::endl; if (it != this->maturity_models.end()) { p->maturity = (*it).second; // >maturity defined in population.hpp - FIMS_LOG << "Maturity function successfully set." << std::endl; + INFO_LOG << "Maturity function successfully set." << std::endl; } else { valid_model = false; - FIMS_LOG + ERROR_LOG << "Error: Expected maturity function not defined for population " << p->id << ", maturity function " << maturity_uint << std::endl; + exit(1); } } else { valid_model = false; - FIMS_LOG << "Error: No maturity function defined for population " - << p->id - << ". FIMS requires maturity functions be defined for all " - "populations." - << std::endl; + ERROR_LOG << "Error: No maturity function defined for population " + << p->id + << ". FIMS requires maturity functions be defined for all " + "populations." + << std::endl; + exit(1); } - FIMS_LOG << "Completed initialization for population " << p->id << "." + INFO_LOG << "Completed initialization for population " << p->id << "." << std::endl; } - FIMS_LOG << "Completed initialization of all populations." << std::endl; - FIMS_LOG << "Completed FIMS model creation." << std::endl; + INFO_LOG << "Completed initialization of all populations." << std::endl; + INFO_LOG << "Completed FIMS model creation." << std::endl; return valid_model; } @@ -499,33 +527,33 @@ class Information { /** * @brief Get the Parameters object * - * @return std::vector& + * @return std::vector& */ - std::vector& GetParameters() { return parameters; } + std::vector& GetParameters() { return parameters; } /** * @brief Get the Fixed Effects Parameters object * - * @return std::vector& + * @return std::vector& */ - std::vector& GetFixedEffectsParameters() { + std::vector& GetFixedEffectsParameters() { return fixed_effects_parameters; } /** * @brief Get the Random Effects Parameters object * - * @return std::vector& + * @return std::vector& */ - std::vector& GetRandomEffectsParameters() { + std::vector& GetRandomEffectsParameters() { return random_effects_parameters; } }; -template -std::shared_ptr > Information::fims_information = +template +std::shared_ptr > Information::fims_information = nullptr; // singleton instance -} // namespace fims +} // namespace fims_info #endif /* FIMS_COMMON_INFORMATION_HPP */ diff --git a/inst/include/common/model.hpp b/inst/include/common/model.hpp index a339e90b6..e1130014a 100644 --- a/inst/include/common/model.hpp +++ b/inst/include/common/model.hpp @@ -21,7 +21,7 @@ #include "information.hpp" -namespace fims { +namespace fims_model { /** * @brief Model class. FIMS objective function. @@ -31,7 +31,7 @@ class Model { // may need singleton public: static std::shared_ptr > fims_model; /*!< Create a shared fims_model as a pointer to Model*/ - std::shared_ptr > + std::shared_ptr > fims_information; /*!< Create a shared fims_information as a pointer to Information*/ @@ -49,9 +49,9 @@ class Model { // may need singleton */ static std::shared_ptr > GetInstance() { if (Model::fims_model == nullptr) { - Model::fims_model = std::make_shared >(); + Model::fims_model = std::make_shared >(); Model::fims_model->fims_information = - fims::Information::GetInstance(); + fims_info::Information::GetInstance(); } return Model::fims_model; } @@ -90,14 +90,21 @@ class Model { // may need singleton // Loop over populations, evaluate, and sum up the recruitment likelihood // component - typename fims::Information::population_iterator it; + + typename fims_info::Information::population_iterator it; + MODEL_LOG << "Evaluating expected values and summing recruitment nlls for " + << this->fims_information->populations.size() << " populations." + << std::endl; + for (it = this->fims_information->populations.begin(); it != this->fims_information->populations.end(); ++it) { - //(*it).second points to each individual Population module - FIMS_LOG << "inside pop loop" << std::endl; + //(*it).second points to the Population module + MODEL_LOG << "Setting up pointer to population " << (*it).second->id + << "." << std::endl; // Prepare recruitment (*it).second->recruitment->Prepare(); - FIMS_LOG << "recruitment prepare works" << std::endl; + MODEL_LOG << "Recruitment for population successfully prepared" + << std::endl; // link to TMB objective function #ifdef TMB_MODEL (*it).second->of = this->of; @@ -106,21 +113,32 @@ class Model { // may need singleton (*it).second->Evaluate(); // Recrtuiment negative log-likelihood rec_nll += (*it).second->recruitment->evaluate_nll(); - FIMS_LOG << "rec nll: " << rec_nll << std::endl; + MODEL_LOG << "Recruitment negative log-likelihood is: " << rec_nll + << std::endl; } + MODEL_LOG << "All populations successfully evaluated." << std::endl; // Loop over fleets/surveys, and sum up age comp and index nlls - typename fims::Information::fleet_iterator jt; + + typename fims_info::Information::fleet_iterator jt; + MODEL_LOG << "Evaluating expected values and summing nlls for " + << this->fims_information->fleets.size() << " fleets." + << std::endl; + for (jt = this->fims_information->fleets.begin(); jt != this->fims_information->fleets.end(); ++jt) { //(*jt).second points to each individual Fleet module #ifdef TMB_MODEL (*jt).second->of = this->of; #endif + MODEL_LOG << "Setting up pointer to fleet " << (*jt).second->id << "." + << std::endl; age_comp_nll += (*jt).second->evaluate_age_comp_nll(); + MODEL_LOG << "Sum of survey and age comp negative log-likelihood is: " + << age_comp_nll << std::endl; index_nll += (*jt).second->evaluate_index_nll(); } - + MODEL_LOG << "All fleets successfully evaluated." << std::endl; // Loop over populations and fleets/surveys and fill in reporting // initiate population index for structuring report out objects @@ -202,6 +220,6 @@ class Model { // may need singleton template std::shared_ptr > Model::fims_model = nullptr; // singleton instance -} // namespace fims +} // namespace fims_model #endif /* FIMS_COMMON_MODEL_HPP */ diff --git a/inst/include/common/model_object.hpp b/inst/include/common/model_object.hpp index 1ab307380..f67579327 100644 --- a/inst/include/common/model_object.hpp +++ b/inst/include/common/model_object.hpp @@ -16,18 +16,18 @@ #include "def.hpp" -namespace fims { +namespace fims_model_object { /** * @brief FIMSObject struct that defines member types and returns the unique id */ -template +template struct FIMSObject { uint32_t id; /*!< unique identifier assigned for all fims objects */ - std::vector parameters; /*!< list of estimable parameters */ - std::vector + std::vector parameters; /*!< list of estimable parameters */ + std::vector random_effects_parameters; /*!< list of all random effects parameters */ - std::vector + std::vector fixed_effects_parameters; /*!< list of fixed effects parameters */ virtual ~FIMSObject() {} @@ -37,6 +37,6 @@ struct FIMSObject { uint32_t GetId() const { return id; } }; -} // namespace fims +} // namespace fims_model_object #endif /* FIMS_COMMON_MODEL_OBJECT_HPP */ diff --git a/inst/include/distributions/functors/distributions_base.hpp b/inst/include/distributions/functors/distributions_base.hpp index b1e8ed0c5..5c55dc3c3 100644 --- a/inst/include/distributions/functors/distributions_base.hpp +++ b/inst/include/distributions/functors/distributions_base.hpp @@ -17,7 +17,7 @@ #include "../../common/model_object.hpp" #include "../../interface/interface.hpp" -namespace fims { +namespace fims_distributions { /** @brief Base class for all module_name functors. * @@ -25,7 +25,7 @@ namespace fims { * */ template -struct DistributionsBase : public FIMSObject { +struct DistributionsBase : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the DistributionsBase class. // this is like a memory tracker. // Assigning each one its own ID is a way to keep track of @@ -50,6 +50,6 @@ struct DistributionsBase : public FIMSObject { template uint32_t DistributionsBase::id_g = 0; -} // namespace fims +} // namespace fims_distributions #endif /* DISTRIBUTIONS_BASE_HPP */ diff --git a/inst/include/distributions/functors/tmb_distributions.hpp b/inst/include/distributions/functors/tmb_distributions.hpp index 8ae1bfb6d..b3a0ee8c1 100644 --- a/inst/include/distributions/functors/tmb_distributions.hpp +++ b/inst/include/distributions/functors/tmb_distributions.hpp @@ -13,19 +13,19 @@ #include "distributions_base.hpp" -namespace fims { +namespace fims_distributions { /** * @brief Dnorm class returns the TMB dnorm function */ -template -struct Dnorm : public DistributionsBase { - T x; /*!< observation */ - T mean; /*!< mean of the normal distribution */ - T sd; /*!< standard deviation of the normal distribution, must be strictly - positive.*/ +template +struct Dnorm : public DistributionsBase { + Type x; /*!< observation */ + Type mean; /*!< mean of the normal distribution */ + Type sd; /*!< standard deviation of the normal distribution, must be strictly + positive.*/ - Dnorm() : DistributionsBase() {} + Dnorm() : DistributionsBase() {} virtual ~Dnorm() {} @@ -36,7 +36,7 @@ struct Dnorm : public DistributionsBase { * * @param do_log Boolean; if true, log densities are returned */ - virtual const T evaluate(const bool& do_log) { + virtual const Type evaluate(const bool& do_log) { return dnorm(x, mean, sd, do_log); } }; @@ -44,15 +44,15 @@ struct Dnorm : public DistributionsBase { /** * @brief Dmultinom class returns the TMB dmultinom function */ -template -struct Dmultinom : public DistributionsBase { +template +struct Dmultinom : public DistributionsBase { /** EigenVector defined in interface.hpp */ - using Vector = typename fims::ModelTraits::EigenVector; + using Vector = typename fims::ModelTraits::EigenVector; Vector x; /*!< Vector of length K of integers */ Vector p; /*!< Vector of length K, specifying the probability for the K classes (note, unlike in R these must sum to 1). */ - Dmultinom() : DistributionsBase() {} + Dmultinom() : DistributionsBase() {} /** * @brief Probability mass function of the multinomial distribution. @@ -63,7 +63,7 @@ struct Dmultinom : public DistributionsBase { * * @param do_log Boolean; if true, log densities are returned */ - virtual const T evaluate(const bool& do_log) { + virtual const Type evaluate(const bool& do_log) { return dmultinom(x, p, do_log); } }; @@ -72,26 +72,26 @@ struct Dmultinom : public DistributionsBase { * @brief Dlnorm uses the TMB dnorm function to construct the lognormal density * function */ -template -struct Dlnorm : public DistributionsBase { - T x; /*!< observation */ - T meanlog; /*!< mean of the distribution of log(x) */ - T sdlog; /*!< standard deviation of the distribution of log(x) */ +template +struct Dlnorm : public DistributionsBase { + Type x; /*!< observation */ + Type meanlog; /*!< mean of the distribution of log(x) */ + Type sdlog; /*!< standard deviation of the distribution of log(x) */ - Dlnorm() : DistributionsBase() {} + Dlnorm() : DistributionsBase() {} /** * @brief Probability density function of the lognormal distribution. * - * \f[ \frac{1.0}{ xsd\sqrt{2\pi} }exp(-\frac{(ln(x) - mean)^{2}}{2sd^{2}}) - * \f] + * \f[ \frac{1.0}{ xsd\sqrt{2.0\pi} }exp(-\frac{(ln(x) - + * mean)^{2.0}}{2.0sd^{2.0}}) \f] * * @param do_log Boolean; if true, log densities are returned */ - virtual const T evaluate(const bool& do_log) { - T logx = log(x); - T nll; + virtual const Type evaluate(const bool& do_log) { + Type logx = log(x); + Type nll; nll = dnorm(logx, meanlog, sdlog, true) - logx; @@ -102,7 +102,7 @@ struct Dlnorm : public DistributionsBase { } } }; -} // namespace fims +} // namespace fims_distributions #endif diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 24b5fd690..45711e9da 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -32,23 +32,23 @@ bool CreateTMBModel() { } // base model - std::shared_ptr> d0 = - fims::Information::GetInstance(); + std::shared_ptr> d0 = + fims_info::Information::GetInstance(); d0->CreateModel(); // first-order derivative - std::shared_ptr> d1 = - fims::Information::GetInstance(); + std::shared_ptr> d1 = + fims_info::Information::GetInstance(); d1->CreateModel(); // second-order derivative - std::shared_ptr> d2 = - fims::Information::GetInstance(); + std::shared_ptr> d2 = + fims_info::Information::GetInstance(); d2->CreateModel(); // third-order derivative - std::shared_ptr> d3 = - fims::Information::GetInstance(); + std::shared_ptr> d3 = + fims_info::Information::GetInstance(); d3->CreateModel(); return true; @@ -56,8 +56,8 @@ bool CreateTMBModel() { Rcpp::NumericVector get_fixed_parameters_vector() { // base model - std::shared_ptr> d0 = - fims::Information::GetInstance(); + std::shared_ptr> d0 = + fims_info::Information::GetInstance(); Rcpp::NumericVector p; @@ -70,8 +70,8 @@ Rcpp::NumericVector get_fixed_parameters_vector() { Rcpp::NumericVector get_random_parameters_vector() { // base model - std::shared_ptr> d0 = - fims::Information::GetInstance(); + std::shared_ptr> d0 = + fims_info::Information::GetInstance(); Rcpp::NumericVector p; @@ -82,10 +82,156 @@ Rcpp::NumericVector get_random_parameters_vector() { return p; } +/** + * Clears the contents of info log file. + */ +void clear_info_log() { + // First flush the output stream to make sure nothing + // is left in the stream memory bufffer. + INFO_LOG.flush(); + + // Next an new stream is opened and closed to + // overwrite the file. + std::ofstream CLEAR_LOG("logs/info.log"); + CLEAR_LOG.close(); + + // Finally the stream output location is reset back to the start + // of the file. + INFO_LOG.seekp(0); +} + +/** + * Clears the contents of fims log file. + */ +void clear_fims_log() { + FIMS_LOG.flush(); + std::ofstream CLEAR_LOG("logs/fims.log"); + CLEAR_LOG.close(); + FIMS_LOG.seekp(0); +} + +/** + * Clears the contents of data log file. + */ +void clear_data_log() { + DATA_LOG.flush(); + std::ofstream CLEAR_LOG("logs/data.log"); + CLEAR_LOG.close(); + DATA_LOG.seekp(0); +} + +/** + * Clears the contents of error log file. + */ +void clear_error_log() { + ERROR_LOG.flush(); + std::ofstream CLEAR_LOG("logs/error.log"); + CLEAR_LOG.close(); + ERROR_LOG.seekp(0); +} + +/** + * Clears the contents of model log file. + */ +void clear_model_log() { + MODEL_LOG.flush(); + std::ofstream CLEAR_LOG("logs/model.log"); + CLEAR_LOG.close(); + MODEL_LOG.seekp(0); +} + +/** + * Clears the contents of fleet log file. + */ +void clear_fleet_log() { + FLEET_LOG.flush(); + std::ofstream CLEAR_LOG("logs/fleet.log"); + CLEAR_LOG.close(); + FLEET_LOG.seekp(0); +} + +/** + * Clears the contents of population log file. + */ +void clear_population_log() { + POPULATION_LOG.flush(); + std::ofstream CLEAR_LOG("logs/population.log"); + CLEAR_LOG.close(); + POPULATION_LOG.seekp(0); +} + +/** + * Clears the contents of maturity log file. + */ +void clear_maturity_log() { + MATURITY_LOG.flush(); + std::ofstream CLEAR_LOG("logs/maturity.log"); + CLEAR_LOG.close(); + MATURITY_LOG.seekp(0); +} + +/** + * Clears the contents of recruitment log file. + */ +void clear_recruitment_log() { + RECRUITMENT_LOG.flush(); + std::ofstream CLEAR_LOG("logs/recruitment.log"); + CLEAR_LOG.close(); + RECRUITMENT_LOG.seekp(0); +} + +/** + * Clears the contents of growth log file. + */ +void clear_growth_log() { + GROWTH_LOG.flush(); + std::ofstream CLEAR_LOG("logs/growth.log"); + CLEAR_LOG.close(); + GROWTH_LOG.seekp(0); +} + +/** + * Clears the contents of selectivity log file. + */ +void clear_selectivity_log() { + SELECTIVITY_LOG.flush(); + std::ofstream CLEAR_LOG("logs/selectivity.log"); + CLEAR_LOG.close(); + SELECTIVITY_LOG.seekp(0); +} + +/** + * Clears the contents of debug log file. + */ +void clear_debug_log() { + DEBUG_LOG.flush(); + std::ofstream CLEAR_LOG("logs/debug/debug.log"); + CLEAR_LOG.close(); + DEBUG_LOG.seekp(0); +} + +/** + * Clears the contents of log files. + */ +void clear_logs() { + clear_fims_log(); + clear_info_log(); + clear_data_log(); + clear_error_log(); + clear_model_log(); + clear_fleet_log(); + clear_population_log(); + clear_recruitment_log(); + clear_growth_log(); + clear_maturity_log(); + clear_selectivity_log(); + clear_debug_log(); +} + template void clear_internal() { - std::shared_ptr> d0 = - fims::Information::GetInstance(); + std::shared_ptr> d0 = + fims_info::Information::GetInstance(); d0->fixed_effects_parameters.clear(); d0->random_effects_parameters.clear(); } @@ -176,6 +322,19 @@ RCPP_MODULE(fims) { Rcpp::function("get_fixed", &get_fixed_parameters_vector); Rcpp::function("get_random", &get_random_parameters_vector); Rcpp::function("clear", clear); + Rcpp::function("clear_logs", clear_logs); + Rcpp::function("clear_fims_log", clear_fims_log); + Rcpp::function("clear_info_log", clear_info_log); + Rcpp::function("clear_error_log", clear_error_log); + Rcpp::function("clear_data_log", clear_data_log); + Rcpp::function("clear_population_log", clear_population_log); + Rcpp::function("clear_model_log", clear_model_log); + Rcpp::function("clear_recruitment_log", clear_recruitment_log); + Rcpp::function("clear_fleet_log", clear_fleet_log); + Rcpp::function("clear_growth_log", clear_growth_log); + Rcpp::function("clear_maturity_log", clear_maturity_log); + Rcpp::function("clear_selectivity_log", clear_selectivity_log); + Rcpp::function("clear_debug_log", clear_debug_log); Rcpp::class_("Parameter") .constructor() @@ -257,23 +416,26 @@ RCPP_MODULE(fims) { Rcpp::class_("LogisticMaturity") .constructor() - .field("median", &LogisticMaturityInterface::median) + .field("inflection_point", &LogisticMaturityInterface::inflection_point) .field("slope", &LogisticMaturityInterface::slope) .method("get_id", &LogisticMaturityInterface::get_id) .method("evaluate", &LogisticMaturityInterface::evaluate); Rcpp::class_("LogisticSelectivity") .constructor() - .field("median", &LogisticSelectivityInterface::median) + .field("inflection_point", + &LogisticSelectivityInterface::inflection_point) .field("slope", &LogisticSelectivityInterface::slope) .method("get_id", &LogisticSelectivityInterface::get_id) .method("evaluate", &LogisticSelectivityInterface::evaluate); Rcpp::class_("DoubleLogisticSelectivity") .constructor() - .field("median_asc", &DoubleLogisticSelectivityInterface::median_asc) + .field("inflection_point_asc", + &DoubleLogisticSelectivityInterface::inflection_point_asc) .field("slope_asc", &DoubleLogisticSelectivityInterface::slope_asc) - .field("median_desc", &DoubleLogisticSelectivityInterface::median_desc) + .field("inflection_point_desc", + &DoubleLogisticSelectivityInterface::inflection_point_desc) .field("slope_desc", &DoubleLogisticSelectivityInterface::slope_desc) .method("get_id", &DoubleLogisticSelectivityInterface::get_id) .method("evaluate", &DoubleLogisticSelectivityInterface::evaluate); diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp index 744aa3f7e..d3cd9e955 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp @@ -85,8 +85,9 @@ class AgeCompDataInterface : public DataInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr> age_comp_data = - std::make_shared>(this->ymax, this->amax); + std::shared_ptr> age_comp_data = + std::make_shared>(this->ymax, + this->amax); age_comp_data->id = this->id; for (int y = 0; y < ymax; y++) { @@ -96,8 +97,8 @@ class AgeCompDataInterface : public DataInterfaceBase { } } - std::shared_ptr> info = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims_info::Information::GetInstance(); info->data_objects[this->id] = age_comp_data; @@ -147,8 +148,8 @@ class IndexDataInterface : public DataInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr> data = - std::make_shared>(this->ymax); + std::shared_ptr> data = + std::make_shared>(this->ymax); data->id = this->id; @@ -156,8 +157,8 @@ class IndexDataInterface : public DataInterfaceBase { data->at(y) = this->index_data[y]; } - std::shared_ptr> info = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims_info::Information::GetInstance(); info->data_objects[this->id] = data; return true; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp index 7e28b61ce..2a02c813f 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_fleet.hpp @@ -132,11 +132,11 @@ class FleetInterface : public FleetInterfaceBase { #ifdef TMB_MODEL template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > fleet = - std::make_shared >(); + std::shared_ptr > fleet = + std::make_shared >(); // set relative info fleet->id = this->id; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp index 29cbac3d8..543fc7853 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp @@ -74,8 +74,8 @@ vectors have been set */ /** * @brief Create a map of input numeric vectors - * @param weights T vector of weights - * @param ages T vector of ages + * @param weights Type vector of weights + * @param ages Type vector of ages * @return std::map * * */ @@ -93,7 +93,7 @@ vectors have been set */ * ewaagrowth.evaluate(age) * */ virtual double evaluate(double age) { - fims::EWAAgrowth EWAAGrowth; + fims_popdy::EWAAgrowth EWAAGrowth; if (initialized == false) { this->ewaa = make_map(this->ages, this->weights); @@ -112,11 +112,11 @@ vectors have been set */ template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > ewaa_growth = - std::make_shared >(); + std::shared_ptr > ewaa_growth = + std::make_shared >(); // set relative info ewaa_growth->id = this->id; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp index 4e81aead1..83cf499bf 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_maturity.hpp @@ -60,8 +60,9 @@ std::map MaturityInterfaceBase::live_objects; */ class LogisticMaturityInterface : public MaturityInterfaceBase { public: - Parameter median; /**< the index value at which the response reaches .5 */ - Parameter slope; /**< the width of the curve at the median */ + Parameter + inflection_point; /**< the index value at which the response reaches .5 */ + Parameter slope; /**< the width of the curve at the inflection_point */ LogisticMaturityInterface() : MaturityInterfaceBase() {} @@ -75,8 +76,8 @@ class LogisticMaturityInterface : public MaturityInterfaceBase { * size in maturity). */ virtual double evaluate(double x) { - fims::LogisticMaturity LogisticMat; - LogisticMat.median = this->median.value_m; + fims_popdy::LogisticMaturity LogisticMat; + LogisticMat.inflection_point = this->inflection_point.value_m; LogisticMat.slope = this->slope.value_m; return LogisticMat.evaluate(x); } @@ -85,20 +86,20 @@ class LogisticMaturityInterface : public MaturityInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > maturity = - std::make_shared >(); + std::shared_ptr > maturity = + std::make_shared >(); // set relative info maturity->id = this->id; - maturity->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - info->RegisterRandomEffect(maturity->median); + maturity->inflection_point = this->inflection_point.value_m; + if (this->inflection_point.estimated_m) { + if (this->inflection_point.is_random_effect_m) { + info->RegisterRandomEffect(maturity->inflection_point); } else { - info->RegisterParameter(maturity->median); + info->RegisterParameter(maturity->inflection_point); } } maturity->slope = this->slope.value_m; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp index 17a116e12..14f64d21b 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_population.hpp @@ -98,7 +98,7 @@ class PopulationInterface : public PopulationInterfaceBase { /** @brief evaluate the population function */ virtual void evaluate() { - fims::Population population; + fims_popdy::Population population; return population.Evaluate(); } @@ -106,11 +106,11 @@ class PopulationInterface : public PopulationInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > population = - std::make_shared >(); + std::shared_ptr > population = + std::make_shared >(); // set relative info population->id = this->id; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp index 1f9ae5d52..8eb351d3e 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_recruitment.hpp @@ -84,7 +84,7 @@ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase { virtual uint32_t get_id() { return this->id; } virtual double evaluate(double spawners, double ssbzero) { - fims::SRBevertonHolt BevHolt; + fims_popdy::SRBevertonHolt BevHolt; BevHolt.logit_steep = this->logit_steep.value_m; if (this->logit_steep.value_m == 1.0) { @@ -99,14 +99,15 @@ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase { } virtual double evaluate_nll() { - fims::SRBevertonHolt NLL; + fims_popdy::SRBevertonHolt NLL; NLL.log_sigma_recruit = this->log_sigma_recruit.value_m; NLL.recruit_deviations.resize(deviations.size()); // Vector from TMB for (int i = 0; i < deviations.size(); i++) { NLL.recruit_deviations[i] = deviations[i]; } - FIMS_LOG << "Rec devs being passed to C++ are " << deviations << std::endl; + RECRUITMENT_LOG << "Rec devs being passed to C++ are " << deviations + << std::endl; NLL.estimate_recruit_deviations = this->estimate_deviations; return NLL.evaluate_nll(); } @@ -115,11 +116,11 @@ class BevertonHoltRecruitmentInterface : public RecruitmentInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > recruitment = - std::make_shared >(); + std::shared_ptr > recruitment = + std::make_shared >(); // set relative info recruitment->id = this->id; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp index 7f1523b25..5f282cebc 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_selectivity.hpp @@ -60,8 +60,9 @@ std::map */ class LogisticSelectivityInterface : public SelectivityInterfaceBase { public: - Parameter median; /**< the index value at which the response reaches .5 */ - Parameter slope; /**< the width of the curve at the median */ + Parameter + inflection_point; /**< the index value at which the response reaches .5 */ + Parameter slope; /**< the width of the curve at the inflection_point */ LogisticSelectivityInterface() : SelectivityInterfaceBase() {} @@ -75,8 +76,8 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { * size in selectivity). */ virtual double evaluate(double x) { - fims::LogisticSelectivity LogisticSel; - LogisticSel.median = this->median.value_m; + fims_popdy::LogisticSelectivity LogisticSel; + LogisticSel.inflection_point = this->inflection_point.value_m; LogisticSel.slope = this->slope.value_m; return LogisticSel.evaluate(x); } @@ -85,20 +86,20 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > selectivity = - std::make_shared >(); + std::shared_ptr > selectivity = + std::make_shared >(); // set relative info selectivity->id = this->id; - selectivity->median = this->median.value_m; - if (this->median.estimated_m) { - if (this->median.is_random_effect_m) { - info->RegisterRandomEffect(selectivity->median); + selectivity->inflection_point = this->inflection_point.value_m; + if (this->inflection_point.estimated_m) { + if (this->inflection_point.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->inflection_point); } else { - info->RegisterParameter(selectivity->median); + info->RegisterParameter(selectivity->inflection_point); } } selectivity->slope = this->slope.value_m; @@ -136,11 +137,12 @@ class LogisticSelectivityInterface : public SelectivityInterfaceBase { */ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { public: - Parameter median_asc; /**< the index value at which the response reaches .5 */ - Parameter slope_asc; /**< the width of the curve at the median */ - Parameter - median_desc; /**< the index value at which the response reaches .5 */ - Parameter slope_desc; /**< the width of the curve at the median */ + Parameter inflection_point_asc; /**< the index value at which the response + reaches .5 */ + Parameter slope_asc; /**< the width of the curve at the inflection_point */ + Parameter inflection_point_desc; /**< the index value at which the response + reaches .5 */ + Parameter slope_desc; /**< the width of the curve at the inflection_point */ DoubleLogisticSelectivityInterface() : SelectivityInterfaceBase() {} @@ -154,10 +156,11 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { * size in selectivity). */ virtual double evaluate(double x) { - fims::DoubleLogisticSelectivity DoubleLogisticSel; - DoubleLogisticSel.median_asc = this->median_asc.value_m; + fims_popdy::DoubleLogisticSelectivity DoubleLogisticSel; + DoubleLogisticSel.inflection_point_asc = this->inflection_point_asc.value_m; DoubleLogisticSel.slope_asc = this->slope_asc.value_m; - DoubleLogisticSel.median_desc = this->median_desc.value_m; + DoubleLogisticSel.inflection_point_desc = + this->inflection_point_desc.value_m; DoubleLogisticSel.slope_desc = this->slope_desc.value_m; return DoubleLogisticSel.evaluate(x); } @@ -166,20 +169,20 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr > info = - fims::Information::GetInstance(); + std::shared_ptr > info = + fims_info::Information::GetInstance(); - std::shared_ptr > selectivity = - std::make_shared >(); + std::shared_ptr > selectivity = + std::make_shared >(); // set relative info selectivity->id = this->id; - selectivity->median_asc = this->median_asc.value_m; - if (this->median_asc.estimated_m) { - if (this->median_asc.is_random_effect_m) { - info->RegisterRandomEffect(selectivity->median_asc); + selectivity->inflection_point_asc = this->inflection_point_asc.value_m; + if (this->inflection_point_asc.estimated_m) { + if (this->inflection_point_asc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->inflection_point_asc); } else { - info->RegisterParameter(selectivity->median_asc); + info->RegisterParameter(selectivity->inflection_point_asc); } } selectivity->slope_asc = this->slope_asc.value_m; @@ -190,12 +193,12 @@ class DoubleLogisticSelectivityInterface : public SelectivityInterfaceBase { info->RegisterParameter(selectivity->slope_asc); } } - selectivity->median_desc = this->median_desc.value_m; - if (this->median_desc.estimated_m) { - if (this->median_desc.is_random_effect_m) { - info->RegisterRandomEffect(selectivity->median_desc); + selectivity->inflection_point_desc = this->inflection_point_desc.value_m; + if (this->inflection_point_desc.estimated_m) { + if (this->inflection_point_desc.is_random_effect_m) { + info->RegisterRandomEffect(selectivity->inflection_point_desc); } else { - info->RegisterParameter(selectivity->median_desc); + info->RegisterParameter(selectivity->inflection_point_desc); } } selectivity->slope_desc = this->slope_desc.value_m; diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp index 373d78f33..cfce6d1fc 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_tmb_distribution.hpp @@ -81,7 +81,7 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { * @return log pdf */ virtual double evaluate(bool do_log) { - fims::Dnorm dnorm; + fims_distributions::Dnorm dnorm; dnorm.x = this->x.value_m; dnorm.mean = this->mean.value_m; dnorm.sd = this->sd.value_m; @@ -92,11 +92,11 @@ class DnormDistributionsInterface : public DistributionsInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr> info = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims_info::Information::GetInstance(); - std::shared_ptr> distribution = - std::make_shared>(); + std::shared_ptr> distribution = + std::make_shared>(); // interface to data/parameter value distribution->id = this->id; @@ -154,7 +154,7 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { * @return log pdf */ virtual double evaluate(bool do_log) { - fims::Dlnorm dlnorm; + fims_distributions::Dlnorm dlnorm; dlnorm.x = this->x.value_m; dlnorm.meanlog = this->meanlog.value_m; dlnorm.sdlog = this->sdlog.value_m; @@ -165,11 +165,11 @@ class DlnormDistributionsInterface : public DistributionsInterfaceBase { template bool add_to_fims_tmb_internal() { - std::shared_ptr> info = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims_info::Information::GetInstance(); - std::shared_ptr> distribution = - std::make_shared>(); + std::shared_ptr> distribution = + std::make_shared>(); // set relative info distribution->id = this->id; @@ -225,7 +225,7 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { * @return log pdf */ virtual double evaluate(bool do_log) { - fims::Dmultinom dmultinom; + fims_distributions::Dmultinom dmultinom; // Decale TMBVector in this scope typedef typename fims::ModelTraits::EigenVector Vector; dmultinom.x = Vector(x.size()); // Vector from TMB @@ -242,11 +242,11 @@ class DmultinomDistributionsInterface : public DistributionsInterfaceBase { template bool add_to_fims_tmb_internal() { typedef typename fims::ModelTraits::EigenVector Vector; - std::shared_ptr> info = - fims::Information::GetInstance(); + std::shared_ptr> info = + fims_info::Information::GetInstance(); - std::shared_ptr> distribution = - std::make_shared>(); + std::shared_ptr> distribution = + std::make_shared>(); distribution->id = this->id; distribution->x = Vector(x.size()); diff --git a/inst/include/population_dynamics/fleet/fleet.hpp b/inst/include/population_dynamics/fleet/fleet.hpp index f017d3752..cd452333b 100644 --- a/inst/include/population_dynamics/fleet/fleet.hpp +++ b/inst/include/population_dynamics/fleet/fleet.hpp @@ -15,19 +15,19 @@ #include "../../distributions/distributions.hpp" #include "../selectivity/selectivity.hpp" -namespace fims { +namespace fims_popdy { /** @brief Base class for all fleets. * * @tparam Type The type of the fleet object. **/ template -struct Fleet : public FIMSObject { +struct Fleet : public fims_model_object::FIMSObject { static uint32_t id_g; /*!< reference id for fleet object*/ size_t nyears; /*!< the number of years in the model*/ size_t nages; /*!< the number of ages in the model*/ using ParameterVector = - typename ModelTraits::ParameterVector; /*!< vector of fleet + typename fims::ModelTraits::ParameterVector; /*!< vector of fleet parameters */ // This likelihood index is not currently being used as only one likelihood @@ -36,27 +36,27 @@ struct Fleet : public FIMSObject { -999; /*!> + std::shared_ptr> index_likelihood; /*!< index likelihood component*/ // This likelihood index is not currently being used as only one likelihood // distribution is available. These are for a future update M2+. int fleet_agecomp_likelihood_id_m = -999; /*!< id of agecomp likelihood component*/ - std::shared_ptr> + std::shared_ptr> agecomp_likelihood; /*!< agecomp likelihood component*/ // selectivity int fleet_selectivity_id_m = -999; /*!< id of selectivity component*/ - std::shared_ptr> + std::shared_ptr> selectivity; /*!< selectivity component*/ int fleet_observed_index_data_id_m = -999; /*!< id of index data */ - std::shared_ptr> + std::shared_ptr> observed_index_data; /*!< observed index data*/ int fleet_observed_agecomp_data_id_m = -999; /*!< id of age comp data */ - std::shared_ptr> + std::shared_ptr> observed_agecomp_data; /*!< observed agecomp data*/ // Mortality and catchability @@ -122,7 +122,7 @@ struct Fleet : public FIMSObject { */ void Prepare() { // for(size_t fleet_ = 0; fleet_ <= this->nfleets; fleet_++) { - // this -> Fmort[fleet_] = fims::exp(this -> log_Fmort[fleet_]); + // this -> Fmort[fleet_] = fims_math::exp(this -> log_Fmort[fleet_]); // derived quantities std::fill(catch_at_age.begin(), catch_at_age.end(), @@ -138,29 +138,31 @@ struct Fleet : public FIMSObject { 0); /*!q = fims::exp(this->log_q); + this->q = fims_math::exp(this->log_q); for (size_t year = 0; year < this->nyears; year++) { - FIMS_LOG << "input F mort " << this->log_Fmort[year] << std::endl; - FIMS_LOG << "input q " << this->log_q << std::endl; - this->Fmort[year] = fims::exp(this->log_Fmort[year]); + FLEET_LOG << "input F mort " << this->log_Fmort[year] << std::endl; + FLEET_LOG << "input q " << this->log_q << std::endl; + this->Fmort[year] = fims_math::exp(this->log_Fmort[year]); } } virtual const Type evaluate_age_comp_nll() { Type nll = 0.0; /*!< The negative log likelihood value */ #ifdef TMB_MODEL - fims::Dmultinom dmultinom; + fims_distributions::Dmultinom dmultinom; size_t dims = this->observed_agecomp_data->get_imax() * this->observed_agecomp_data->get_jmax(); if (dims != this->catch_numbers_at_age.size()) { - fims_log::get("fleet.log") << "Error: observed age comp is of size " - << dims << " and expected is of size " - << this->age_composition.size() << std::endl; + ERROR_LOG << "Error: observed age comp is of size " << dims + << " and expected is of size " << this->age_composition.size() + << std::endl; + exit(1); + } else { for (size_t y = 0; y < this->nyears; y++) { // EigenVector declares a vector type from the Eigen library, which is // the expected type for TMB's dmultinom - using Vector = typename ModelTraits::EigenVector; + using Vector = typename fims::ModelTraits::EigenVector; Vector observed_acomp; Vector expected_acomp; @@ -178,17 +180,18 @@ struct Fleet : public FIMSObject { sum; // probabilities for ages observed_acomp[a] = this->observed_agecomp_data->at(y, a); - fims_log::get("fleet.log") - << " age " << a << " in year " << y - << "has expected: " << expected_acomp[a] - << " and observed: " << observed_acomp[a] << std::endl; + + FLEET_LOG << " age " << a << " in year " << y + << "has expected: " << expected_acomp[a] + << " and observed: " << observed_acomp[a] << std::endl; } dmultinom.x = observed_acomp; dmultinom.p = expected_acomp; nll -= dmultinom.evaluate(true); } } - + FLEET_LOG << "Age comp negative log-likelihood for fleet," << this->id + << nll << std::endl; #endif return nll; } @@ -197,20 +200,21 @@ struct Fleet : public FIMSObject { Type nll = 0.0; /*!< The negative log likelihood value */ #ifdef TMB_MODEL - fims::Dnorm dnorm; - dnorm.sd = fims::exp(this->log_obs_error); + fims_distributions::Dnorm dnorm; + dnorm.sd = fims_math::exp(this->log_obs_error); for (size_t i = 0; i < this->expected_index.size(); i++) { - dnorm.x = fims::log(this->observed_index_data->at(i)); - dnorm.mean = fims::log(this->expected_index[i]); + dnorm.x = fims_math::log(this->observed_index_data->at(i)); + dnorm.mean = fims_math::log(this->expected_index[i]); nll -= dnorm.evaluate(true); - fims_log::get("fleet.log") - << "observed likelihood component: " << i << " is " - << this->observed_index_data->at(i) - << " and expected is: " << this->expected_index[i] << std::endl; + + FLEET_LOG << "observed index data: " << i << " is " + << this->observed_index_data->at(i) + << " and expected is: " << this->expected_index[i] << std::endl; } - fims_log::get("fleet.log") - << " log obs error is: " << this->log_obs_error << std::endl; - fims_log::get("fleet.log") << " sd is: " << dnorm.sd << std::endl; + FLEET_LOG << " log obs error is: " << this->log_obs_error << std::endl; + FLEET_LOG << " sd is: " << dnorm.sd << std::endl; + FLEET_LOG << " index nll: " << nll << std::endl; + #endif return nll; } @@ -220,6 +224,6 @@ struct Fleet : public FIMSObject { template uint32_t Fleet::id_g = 0; -} // end namespace fims +} // end namespace fims_popdy #endif /* FIMS_POPULATION_DYNAMICS_FLEET_HPP */ diff --git a/inst/include/population_dynamics/growth/functors/ewaa.hpp b/inst/include/population_dynamics/growth/functors/ewaa.hpp index 0308c3692..92454ac4e 100644 --- a/inst/include/population_dynamics/growth/functors/ewaa.hpp +++ b/inst/include/population_dynamics/growth/functors/ewaa.hpp @@ -14,7 +14,7 @@ #include "growth_base.hpp" -namespace fims { +namespace fims_popdy { /** * @brief EWAAgrowth class that returns the EWAA function value. @@ -43,5 +43,5 @@ struct EWAAgrowth : public GrowthBase { return ret; } }; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_GROWTH_EWAA_HPP */ \ No newline at end of file diff --git a/inst/include/population_dynamics/growth/functors/growth_base.hpp b/inst/include/population_dynamics/growth/functors/growth_base.hpp index 5cca6c16d..a6da2de1f 100644 --- a/inst/include/population_dynamics/growth/functors/growth_base.hpp +++ b/inst/include/population_dynamics/growth/functors/growth_base.hpp @@ -16,7 +16,7 @@ #include "../../../common/model_object.hpp" -namespace fims { +namespace fims_popdy { /** * @brief Base class for all growth functors. @@ -24,7 +24,7 @@ namespace fims { * @tparam Type The type of the growth functor. * */ template -struct GrowthBase : public FIMSObject { +struct GrowthBase : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the growthBase class. // this is like a memory tracker. // Assigning each one its own ID is a way to keep track of @@ -48,6 +48,6 @@ struct GrowthBase : public FIMSObject { template uint32_t GrowthBase::id_g = 0; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_GROWTH_BASE_HPP */ diff --git a/inst/include/population_dynamics/maturity/functors/logistic.hpp b/inst/include/population_dynamics/maturity/functors/logistic.hpp index efbd7f5e2..d17167dd0 100644 --- a/inst/include/population_dynamics/maturity/functors/logistic.hpp +++ b/inst/include/population_dynamics/maturity/functors/logistic.hpp @@ -13,7 +13,7 @@ #include "../../../common/fims_math.hpp" #include "maturity_base.hpp" -namespace fims { +namespace fims_popdy { /** * @brief LogisticMaturity class that returns the logistic function value @@ -21,10 +21,10 @@ namespace fims { */ template struct LogisticMaturity : public MaturityBase { - Type median; /*!< 50% quantile of the value of the quantity of interest (x); - e.g. age at which 50% of the fish are mature */ - Type slope; /*!() {} @@ -32,16 +32,17 @@ struct LogisticMaturity : public MaturityBase { * @brief Method of the logistic maturity class that implements the * logistic function from FIMS math. * - * \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - median))} \f] + * \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - inflection_point))} \f] * * @param x The independent variable in the logistic function (e.g., age or * size at maturity). */ + virtual const Type evaluate(const Type& x) { - return fims::logistic(median, slope, x); + return fims_math::logistic(inflection_point, slope, x); } }; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_MATURITY_LOGISTIC_HPP */ diff --git a/inst/include/population_dynamics/maturity/functors/maturity_base.hpp b/inst/include/population_dynamics/maturity/functors/maturity_base.hpp index 8f4acd99a..402233dfc 100644 --- a/inst/include/population_dynamics/maturity/functors/maturity_base.hpp +++ b/inst/include/population_dynamics/maturity/functors/maturity_base.hpp @@ -16,7 +16,7 @@ #include "../../../common/model_object.hpp" -namespace fims { +namespace fims_popdy { /** @brief Base class for all maturity functors. * @@ -24,7 +24,7 @@ namespace fims { */ template -struct MaturityBase : public FIMSObject { +struct MaturityBase : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the MaturityBase class. // this is like a memory tracker. // Assigning each one its own ID is a way to keep track of @@ -50,6 +50,6 @@ struct MaturityBase : public FIMSObject { template uint32_t MaturityBase::id_g = 0; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_MATURITY_BASE_HPP */ diff --git a/inst/include/population_dynamics/population/population.hpp b/inst/include/population_dynamics/population/population.hpp index 333bf6b69..2700381bc 100644 --- a/inst/include/population_dynamics/population/population.hpp +++ b/inst/include/population_dynamics/population/population.hpp @@ -22,7 +22,7 @@ #include "../../interface/interface.hpp" #include "../maturity/maturity.hpp" -namespace fims { +namespace fims_popdy { /*TODO: Review, add functions to evaluate, push vectors back to fleet (or point to fleet directly?) @@ -33,10 +33,10 @@ directly?) * that are divided into generic partitions (eg. sex, area). */ template -struct Population : public FIMSObject { +struct Population : public fims_model_object::FIMSObject { using ParameterVector = - typename ModelTraits::ParameterVector; /*!< the vector of population - parameters*/ + typename fims::ModelTraits::ParameterVector; /*!< the vector of + population parameters*/ static uint32_t id_g; /*!< reference id for population object*/ size_t nyears; /*!< total number of years in the fishery*/ size_t nseasons; /*!< total number of seasons in the fishery*/ @@ -86,27 +86,29 @@ struct Population : public FIMSObject { std::vector expected_recruitment; /*!< Expected recruitment */ /// recruitment int recruitment_id = -999; /*!< id of recruitment model object*/ - std::shared_ptr> + std::shared_ptr> recruitment; /*!< shared pointer to recruitment module */ // growth int growth_id = -999; /*!< id of growth model object*/ - std::shared_ptr> + std::shared_ptr> growth; /*!< shared pointer to growth module */ // maturity int maturity_id = -999; /*!< id of maturity model object*/ - std::shared_ptr> + std::shared_ptr> maturity; /*!< shared pointer to maturity module */ // fleet int fleet_id = -999; /*!< id of fleet model object*/ - std::vector>> + std::vector>> fleets; /*!< shared pointer to fleet module */ // Define objective function object to be able to REPORT and ADREPORT #ifdef TMB_MODEL - ::objective_function *of; + ::objective_function + *of; // :: references global namespace, defined in src/FIMS.cpp, + // available anywhere in the R package #endif // this -> means you're referring to a class member (member of self) @@ -152,20 +154,20 @@ struct Population : public FIMSObject { * */ void Prepare() { - FIMS_LOG << " population prepare " << this->nages << std::endl; - FIMS_LOG << "nfleets: " << this->nfleets << std::endl; - FIMS_LOG << "nseasons: " << this->nseasons << std::endl; - FIMS_LOG << "nyears: " << this->nyears << std::endl; + POPULATION_LOG << " population prepare " << this->nages << std::endl; + POPULATION_LOG << "nfleets: " << this->nfleets << std::endl; + POPULATION_LOG << "nseasons: " << this->nseasons << std::endl; + POPULATION_LOG << "nyears: " << this->nyears << std::endl; for (size_t fleet = 0; fleet < this->nfleets; fleet++) { this->fleets[fleet]->Prepare(); } - std::fill(biomass.begin(), biomass.end(), 0); + std::fill(biomass.begin(), biomass.end(), 0.0); std::fill(unfished_spawning_biomass.begin(), - unfished_spawning_biomass.end(), 0); - std::fill(spawning_biomass.begin(), spawning_biomass.end(), 0); - std::fill(expected_catch.begin(), expected_catch.end(), 0); + unfished_spawning_biomass.end(), 0.0); + std::fill(spawning_biomass.begin(), spawning_biomass.end(), 0.0); + std::fill(expected_catch.begin(), expected_catch.end(), 0.0); std::fill(expected_recruitment.begin(), expected_recruitment.end(), 0.0); std::fill(proportion_mature_at_age.begin(), proportion_mature_at_age.end(), 0.0); @@ -173,9 +175,10 @@ struct Population : public FIMSObject { // Transformation Section for (size_t age = 0; age < this->nages; age++) { + this->weight_at_age[age] = growth->evaluate(ages[age]); for (size_t year = 0; year < this->nyears; year++) { size_t i_age_year = age * this->nyears + year; - this->M[i_age_year] = fims::exp(this->log_M[i_age_year]); + this->M[i_age_year] = fims_math::exp(this->log_M[i_age_year]); // mortality_F is a ParameterVector and therefore needs to be filled // within a loop this->mortality_F[i_age_year] = 0.0; @@ -195,7 +198,7 @@ struct Population : public FIMSObject { */ inline void CalculateInitialNumbersAA( size_t i_age_year, size_t a) { // inline all function unless complicated - this->numbers_at_age[i_age_year] = fims::exp(this->log_init_naa[a]); + this->numbers_at_age[i_age_year] = fims_math::exp(this->log_init_naa[a]); } /** @@ -210,15 +213,18 @@ struct Population : public FIMSObject { if (this->fleets[fleet_]->is_survey == false) { this->mortality_F[i_age_year] += this->fleets[fleet_]->Fmort[year] * + // evaluate is a member function of the selectivity class this->fleets[fleet_]->selectivity->evaluate(ages[age]); - FIMS_LOG << " sel age " << ages[age] << "for fleet " << fleet_ << " is " - << this->fleets[fleet_]->selectivity->evaluate(ages[age]) - << " F mort year " << year << " " - << this->fleets[fleet_]->Fmort[year] << std::endl; + POPULATION_LOG << " selectivity at age " << ages[age] << " for fleet " + << fleet_ << " is " + << this->fleets[fleet_]->selectivity->evaluate(ages[age]) + << " apical fishing mortality F for the fleet in year " + << year << " is " << this->fleets[fleet_]->Fmort[year] + << std::endl; } } - FIMS_LOG << "M in calculate mortality is " << this->M[i_age_year] - << std::endl; + POPULATION_LOG << "M in calculate mortality is " << this->M[i_age_year] + << std::endl; this->mortality_Z[i_age_year] = this->M[i_age_year] + this->mortality_F[i_age_year]; } @@ -235,15 +241,16 @@ struct Population : public FIMSObject { // using Z from previous age/year this->numbers_at_age[i_age_year] = this->numbers_at_age[i_agem1_yearm1] * - (fims::exp(-this->mortality_Z[i_agem1_yearm1])); - FIMS_LOG << " z at i_agem1_yearm1 = " << i_agem1_yearm1 << " is " - << this->mortality_Z[i_agem1_yearm1] << std::endl; + + (fims_math::exp(-this->mortality_Z[i_agem1_yearm1])); + POPULATION_LOG << " z at i_agem1_yearm1 = " << i_agem1_yearm1 << " is " + << this->mortality_Z[i_agem1_yearm1] << std::endl; // Plus group calculation if (age == (this->nages - 1)) { this->numbers_at_age[i_age_year] = this->numbers_at_age[i_age_year] + this->numbers_at_age[i_agem1_yearm1 + 1] * - (fims::exp(-this->mortality_Z[i_agem1_yearm1 + 1])); + (fims_math::exp(-this->mortality_Z[i_agem1_yearm1 + 1])); } } @@ -259,15 +266,17 @@ struct Population : public FIMSObject { // using M from previous age/year this->unfished_numbers_at_age[i_age_year] = this->unfished_numbers_at_age[i_agem1_yearm1] * - (fims::exp(-this->M[i_agem1_yearm1])); - FIMS_LOG << "survival rate at index " << i_agem1_yearm1 << " is " - << fims::exp(-(this->M[i_agem1_yearm1])) << std::endl; + + (fims_math::exp(-this->M[i_agem1_yearm1])); + POPULATION_LOG << "survival rate at index " << i_agem1_yearm1 << " is " + << fims_math::exp(-(this->M[i_agem1_yearm1])) << std::endl; + // Plus group calculation if (age == (this->nages - 1)) { this->unfished_numbers_at_age[i_age_year] = this->unfished_numbers_at_age[i_age_year] + this->unfished_numbers_at_age[i_agem1_yearm1 + 1] * - (fims::exp(-this->M[i_agem1_yearm1 + 1])); + (fims_math::exp(-this->M[i_agem1_yearm1 + 1])); } } @@ -280,10 +289,10 @@ struct Population : public FIMSObject { */ void CalculateBiomass(size_t i_age_year, size_t year, size_t age) { this->biomass[year] += - this->numbers_at_age[i_age_year] * growth->evaluate(ages[age]); - FIMS_LOG << " age " << ages[age] << std::endl; - FIMS_LOG << "growth evaluate: " << growth->evaluate(ages[age]) - << " biomass inputs----- +++\n"; + this->numbers_at_age[i_age_year] * this->weight_at_age[age]; + POPULATION_LOG << " age " << ages[age] << std::endl; + POPULATION_LOG << "growth evaluate: " << this->weight_at_age[age] + << " biomass inputs----- +++\n"; } /** @@ -295,8 +304,8 @@ struct Population : public FIMSObject { * @param age the age of unfished biomass to add */ void CalculateUnfishedBiomass(size_t i_age_year, size_t year, size_t age) { - this->unfished_biomass[year] += this->unfished_numbers_at_age[i_age_year] * - this->growth->evaluate(ages[age]); + this->unfished_biomass[year] += + this->unfished_numbers_at_age[i_age_year] * this->weight_at_age[age]; } /** @@ -307,17 +316,18 @@ struct Population : public FIMSObject { * @param age the age who's biomass is being added into total spawning biomass */ void CalculateSpawningBiomass(size_t i_age_year, size_t year, size_t age) { - this->spawning_biomass[year] += this->proportion_female * - this->numbers_at_age[i_age_year] * - this->proportion_mature_at_age[i_age_year] * - growth->evaluate(ages[age]); - FIMS_LOG << " proportion female " << this->proportion_female << " " - << " mature age " << age << " is " - << this->proportion_mature_at_age[i_age_year] << " " - << " numbers at age " << this->numbers_at_age[i_age_year] << " " - << " growth " << growth->evaluate(ages[age]) << " " - << " spawning biomass " << this->spawning_biomass[year] << " " - << " spawning biomass inputs----- +++\n"; + this->spawning_biomass[year] += + this->proportion_female * this->numbers_at_age[i_age_year] * + this->proportion_mature_at_age[i_age_year] * this->weight_at_age[age]; + POPULATION_LOG << " proportion female " << this->proportion_female << " " + << " mature age " << age << " is " + << this->proportion_mature_at_age[i_age_year] << " " + << " numbers at age " << this->numbers_at_age[i_age_year] + << " " + << " growth " << this->weight_at_age[age] << " " + << " spawning biomass " << this->spawning_biomass[year] + << " " + << " spawning biomass inputs----- +++\n"; } /** @@ -332,8 +342,7 @@ struct Population : public FIMSObject { size_t age) { this->unfished_spawning_biomass[year] += this->proportion_female * this->unfished_numbers_at_age[i_age_year] * - this->proportion_mature_at_age[i_age_year] * - this->growth->evaluate(ages[age]); + this->proportion_mature_at_age[i_age_year] * this->weight_at_age[age]; } /** @@ -348,15 +357,15 @@ struct Population : public FIMSObject { this->proportion_mature_at_age[0] * this->growth->evaluate(ages[0]); for (size_t a = 1; a < (this->nages - 1); a++) { - numbers_spr[a] = numbers_spr[a - 1] * fims::exp(-this->M[a]); + numbers_spr[a] = numbers_spr[a - 1] * fims_math::exp(-this->M[a]); phi_0 += numbers_spr[a] * this->proportion_female * this->proportion_mature_at_age[a] * this->growth->evaluate(ages[a]); } numbers_spr[this->nages - 1] = - (numbers_spr[nages - 2] * fims::exp(-this->M[nages - 2])) / - (1 - fims::exp(-this->M[this->nages - 1])); + (numbers_spr[nages - 2] * fims_math::exp(-this->M[nages - 2])) / + (1 - fims_math::exp(-this->M[this->nages - 1])); phi_0 += numbers_spr[this->nages - 1] * this->proportion_female * this->proportion_mature_at_age[this->nages - 1] * this->growth->evaluate(ages[this->nages - 1]); @@ -370,24 +379,25 @@ struct Population : public FIMSObject { * @param year the year recruitment is being calculated for */ void CalculateRecruitment(size_t i_age_year, size_t year) { - FIMS_LOG << "recruitment 1" << std::endl; + POPULATION_LOG << "recruitment 1" << std::endl; Type phi0 = CalculateSBPR0(); - FIMS_LOG << "recruitment 2" << std::endl; - FIMS_LOG << "phi0 = " << phi0 << std::endl; - FIMS_LOG << "spawning_biomass[year - 1] = " << this->spawning_biomass[year - 1] - << std::endl; - FIMS_LOG << "rec devs = " << this->recruitment->recruit_deviations[year - 1] - << std::endl; - FIMS_LOG << "rec eval = " - << this->recruitment->evaluate(this->spawning_biomass[year - 1], - phi0) - << std::endl; + POPULATION_LOG << "recruitment 2" << std::endl; + POPULATION_LOG << "phi0 = " << phi0 << std::endl; + POPULATION_LOG << "spawning_biomass[year - 1]" << this->spawning_biomass[year - 1] + << std::endl; + POPULATION_LOG << "rec devs = " + << this->recruitment->recruit_deviations[year - 1] + << std::endl; + POPULATION_LOG << "rec eval = " + << this->recruitment->evaluate( + this->spawning_biomass[year - 1], phi0) + << std::endl; this->numbers_at_age[i_age_year] = this->recruitment->evaluate(this->spawning_biomass[year - 1], phi0) * this->recruitment->recruit_deviations[year]; this->expected_recruitment[year] = this->numbers_at_age[i_age_year]; - FIMS_LOG << " numbers at age at indexya " << i_age_year << " is " - << this->numbers_at_age[i_age_year] << std::endl; + POPULATION_LOG << " numbers at age at indexya " << i_age_year << " is " + << this->numbers_at_age[i_age_year] << std::endl; } /** @@ -403,15 +413,15 @@ struct Population : public FIMSObject { fleet_; // index by fleet and years to dimension fold size_t i_age_year = year * this->nages + age; - FIMS_LOG << " fleet " << fleet_ << " year " << year << " age " << age - << std::endl; + POPULATION_LOG << " fleet " << fleet_ << " year " << year << " age " + << age << std::endl; this->expected_catch[index_yf] += this->fleets[fleet_]->catch_weight_at_age[i_age_year]; - FIMS_LOG << "expected catch: " << this->expected_catch[index_yf] - << std::endl; - FIMS_LOG << "----------------------------------------------" - << std::endl; + POPULATION_LOG << "expected catch: " << this->expected_catch[index_yf] + << std::endl; + POPULATION_LOG << "----------------------------------------------" + << std::endl; fleets[fleet_]->expected_catch[year] += this->fleets[fleet_]->catch_weight_at_age[i_age_year]; @@ -432,17 +442,17 @@ struct Population : public FIMSObject { // I = qN (N is total numbers), I is an index in numbers if (this->fleets[fleet_]->is_survey == false) { index_ = this->fleets[fleet_]->catch_numbers_at_age[i_age_year] * - growth->evaluate(ages[age]); + this->weight_at_age[age]; } else { - FIMS_LOG << "fleet " << fleet_ << " is a survey" << std::endl; + POPULATION_LOG << "fleet " << fleet_ << " is a survey" << std::endl; index_ = this->fleets[fleet_]->q * this->fleets[fleet_]->selectivity->evaluate(ages[age]) * this->numbers_at_age[i_age_year] * - growth->evaluate(ages[age]); // this->weight_at_age[age]; + this->weight_at_age[age]; // this->weight_at_age[age]; } fleets[fleet_]->expected_index[year] += index_; - FIMS_LOG << " expected index in year " << year << " is " - << fleets[fleet_]->expected_index[year] << std::endl; + POPULATION_LOG << " expected index in year " << year << " is " + << fleets[fleet_]->expected_index[year] << std::endl; } } @@ -466,17 +476,17 @@ struct Population : public FIMSObject { this->fleets[fleet_]->selectivity->evaluate(ages[age])) / this->mortality_Z[i_age_year] * this->numbers_at_age[i_age_year] * - (1 - fims::exp(-(this->mortality_Z[i_age_year]))); + (1 - fims_math::exp(-(this->mortality_Z[i_age_year]))); } else { catch_ = (this->fleets[fleet_]->selectivity->evaluate(ages[age])) * this->numbers_at_age[i_age_year]; } - FIMS_LOG << " F " << fleet_ << " " << this->fleets[fleet_]->Fmort[year] - << std::endl; - FIMS_LOG << " selectivity " - << this->fleets[fleet_]->selectivity->evaluate(ages[age]) - << std::endl; - FIMS_LOG << " catch " << catch_ << std::endl; + POPULATION_LOG << " F " << fleet_ << " " + << this->fleets[fleet_]->Fmort[year] << std::endl; + POPULATION_LOG << " selectivity " + << this->fleets[fleet_]->selectivity->evaluate(ages[age]) + << std::endl; + POPULATION_LOG << " catch " << catch_ << std::endl; // this->catch_numbers_at_age[i_age_yearf] += catch_; // catch_numbers_at_age for the fleet module has different // dimensions (year/age, not year/fleet/age) @@ -494,16 +504,17 @@ struct Population : public FIMSObject { void CalculateCatchWeightAA(size_t year, size_t age) { int i_age_year = year * this->nages + age; for (size_t fleet_ = 0; fleet_ < this->nfleets; fleet_++) { - FIMS_LOG << " fleet " << fleet_ << std::endl; - FIMS_LOG << " catchnaa " - << this->fleets[fleet_]->catch_numbers_at_age[year] << std::endl; - FIMS_LOG << " weight " << this->growth->evaluate(ages[age]) << std::endl; + POPULATION_LOG << " fleet " << fleet_ << std::endl; + POPULATION_LOG << " catchnaa " + << this->fleets[fleet_]->catch_numbers_at_age[year] + << std::endl; + POPULATION_LOG << " weight " << this->weight_at_age[age] << std::endl; this->fleets[fleet_]->catch_weight_at_age[i_age_year] = this->fleets[fleet_]->catch_numbers_at_age[i_age_year] * - this->growth->evaluate(ages[age]); // this->weight_at_age[age]; - FIMS_LOG << " catch_waa " - << this->fleets[fleet_]->catch_weight_at_age[i_age_year] - << std::endl; + this->weight_at_age[age]; + POPULATION_LOG << " catch_waa " + << this->fleets[fleet_]->catch_weight_at_age[i_age_year] + << std::endl; } } @@ -517,17 +528,17 @@ struct Population : public FIMSObject { void CalculateMaturityAA(size_t i_age_year, size_t age) { // this->maturity is pointing to the maturity module, which has // an evaluate function. -> can be nested. - FIMS_LOG << " age " << age << std::endl; - FIMS_LOG << " ages size " << this->ages.size() << std::endl; - FIMS_LOG << " i_age_year " << i_age_year << std::endl; - FIMS_LOG << "p mature" << this->proportion_mature_at_age[i_age_year] - << std::endl; - FIMS_LOG << this->ages[age] << std::endl; + POPULATION_LOG << " age " << age << std::endl; + POPULATION_LOG << " ages size " << this->ages.size() << std::endl; + POPULATION_LOG << " i_age_year " << i_age_year << std::endl; + POPULATION_LOG << "p mature" << this->proportion_mature_at_age[i_age_year] + << std::endl; + POPULATION_LOG << this->ages[age] << std::endl; this->proportion_mature_at_age[i_age_year] = this->maturity->evaluate(ages[age]); - FIMS_LOG << "p mature set to " << this->proportion_mature_at_age[i_age_year] - << std::endl; + POPULATION_LOG << "p mature set to " + << this->proportion_mature_at_age[i_age_year] << std::endl; } /** @@ -598,7 +609,7 @@ struct Population : public FIMSObject { if (a == 0) { // this->numbers_at_age[i_age_year] = this->recruitment->rzero; this->unfished_numbers_at_age[i_age_year] = - fims::exp(this->recruitment->log_rzero); + fims_math::exp(this->recruitment->log_rzero); } else { CalculateUnfishedNumbersAA(i_age_year, a - 1, a); } @@ -632,10 +643,10 @@ struct Population : public FIMSObject { if (a == 0) { // Set the nrecruits for age a=0 year y (use pointers instead of // functional returns) assuming fecundity = 1 and 50:50 sex ratio - FIMS_LOG << "Recruitment: " << std::endl; + POPULATION_LOG << "Recruitment: " << std::endl; CalculateRecruitment(i_age_year, y); this->unfished_numbers_at_age[i_age_year] = - fims::exp(this->recruitment->log_rzero); + fims_math::exp(this->recruitment->log_rzero); } else { size_t i_agem1_yearm1 = (y - 1) * nages + (a - 1); @@ -645,7 +656,7 @@ struct Population : public FIMSObject { CalculateBiomass(i_age_year, y, a); CalculateSpawningBiomass(i_age_year, y, a); - FIMS_LOG << "index ya: " << i_age_year << std::endl; + POPULATION_LOG << "index age year: " << i_age_year << std::endl; CalculateUnfishedBiomass(i_age_year, y, a); CalculateUnfishedSpawningBiomass(i_age_year, y, a); } @@ -657,33 +668,34 @@ struct Population : public FIMSObject { terminal year. */ if (y < this->nyears) { - FIMS_LOG << i_age_year << std::endl; + POPULATION_LOG << i_age_year << std::endl; CalculateCatchNumbersAA(i_age_year, y, a); CalculateCatchWeightAA(y, a); - FIMS_LOG << "year " << y << " and age " << a << std::endl; + POPULATION_LOG << "year " << y << " and age " << a << std::endl; CalculateCatch(y, a); CalculateIndex(i_age_year, y, a); } } } - FIMS_LOG << "NAA\n"; + + POPULATION_LOG << "NAA\n"; for (size_t i = 0; i < nyears; i++) { for (size_t j = 0; j < nages; j++) { - FIMS_LOG << numbers_at_age[i * nages + j] << "\t"; + POPULATION_LOG << numbers_at_age[i * nages + j] << "\t"; } - FIMS_LOG << "\n"; + POPULATION_LOG << "\n"; } - FIMS_LOG << "CAA\n"; + POPULATION_LOG << "CAA\n"; for (size_t fleet_ = 0; fleet_ < this->nfleets; fleet_++) { - FIMS_LOG << "Fleet " << fleet_ + 1 << "\n"; for (size_t i = 0; i < nyears; i++) { for (size_t j = 0; j < nages; j++) { - FIMS_LOG << fleets[fleet_]->catch_numbers_at_age[i * nages + j] - << "\t"; + POPULATION_LOG << "Fleet " << fleet_ + 1 << "\n"; + POPULATION_LOG << fleets[fleet_]->catch_numbers_at_age[i * nages + j] + << "\t"; } - FIMS_LOG << "\n"; + POPULATION_LOG << "\n"; } } } @@ -691,6 +703,6 @@ struct Population : public FIMSObject { template uint32_t Population::id_g = 0; -} // namespace fims +} // namespace fims_popdy #endif /* FIMS_POPULATION_DYNAMICS_POPULATION_HPP */ diff --git a/inst/include/population_dynamics/population/subpopulation.hpp b/inst/include/population_dynamics/population/subpopulation.hpp index ee2bacc24..9ccc7e6bb 100644 --- a/inst/include/population_dynamics/population/subpopulation.hpp +++ b/inst/include/population_dynamics/population/subpopulation.hpp @@ -31,15 +31,15 @@ #ifndef FIMS_POPULATION_DYNAMICS_POPULATION_SUBPOPULATION_HPP #define FIMS_POPULATION_DYNAMICS_POPULATION_SUBPOPULATION_HPP -namespace fims { +namespace fims_popdy { /** * Subpopulation class. This class represents a generic partition * of a population (eg., sex, area). */ -template +template class Subpopulation {}; -} // namespace fims +} // namespace fims_popdy #endif /* FIMS_POPULATION_DYNAMICS_POPULATION_SUBPOPULATION_HPP */ diff --git a/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp b/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp index ffcde8d3e..7e5f096f8 100644 --- a/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp +++ b/inst/include/population_dynamics/recruitment/functors/recruitment_base.hpp @@ -16,11 +16,11 @@ #include // for using std::pow and M_PI -#include "../../../common/fims_math.hpp" // for using fims::log() +#include "../../../common/fims_math.hpp" // for using fims_math::log() #include "../../../common/model_object.hpp" #include "../../../distributions/distributions.hpp" -namespace fims { +namespace fims_popdy { /** @brief Base class for all recruitment functors. * @@ -28,10 +28,10 @@ namespace fims { * */ template -struct RecruitmentBase : public FIMSObject { +struct RecruitmentBase : public fims_model_object::FIMSObject { static uint32_t id_g; /*!< reference id for recruitment object*/ - typename ModelTraits::ParameterVector + typename fims::ModelTraits::ParameterVector recruit_deviations; /*!< A vector of recruitment deviations */ bool constrain_deviations = false; /*!< A flag to indicate if recruitment deviations are summing to zero or not */ @@ -76,10 +76,10 @@ struct RecruitmentBase : public FIMSObject { return nll; } else { #ifdef TMB_MODEL - fims::Dnorm dnorm; - dnorm.sd = fims::exp(this->log_sigma_recruit); + fims_distributions::Dnorm dnorm; + dnorm.sd = fims_math::exp(this->log_sigma_recruit); for (size_t i = 0; i < this->recruit_deviations.size(); i++) { - dnorm.x = fims::log(this->recruit_deviations[i]); + dnorm.x = fims_math::log(this->recruit_deviations[i]); dnorm.mean = 0.0; nll -= dnorm.evaluate(true); } @@ -105,16 +105,16 @@ struct RecruitmentBase : public FIMSObject { sum += this->recruit_deviations[i]; } - FIMS_LOG << "recruit_deviations: \n"; + RECRUITMENT_LOG << "recruit_deviations: \n"; for (size_t i = 0; i < this->recruit_deviations.size(); i++) { this->recruit_deviations[i] -= sum / (this->recruit_deviations.size()); - FIMS_LOG << this->recruit_deviations[i] << std::endl; + RECRUITMENT_LOG << this->recruit_deviations[i] << std::endl; } } }; template uint32_t RecruitmentBase::id_g = 0; -} // namespace fims +} // namespace fims_popdy #endif /* FIMS_POPULATION_DYNAMICS_RECRUITMENT_BASE_HPP */ diff --git a/inst/include/population_dynamics/recruitment/functors/sr_beverton_holt.hpp b/inst/include/population_dynamics/recruitment/functors/sr_beverton_holt.hpp index a2d70f949..781eaa20f 100644 --- a/inst/include/population_dynamics/recruitment/functors/sr_beverton_holt.hpp +++ b/inst/include/population_dynamics/recruitment/functors/sr_beverton_holt.hpp @@ -15,7 +15,7 @@ #include "recruitment_base.hpp" -namespace fims { +namespace fims_popdy { /** @brief BevertonHolt class that returns the Beverton Holt SR * from fims_math. @@ -52,8 +52,8 @@ struct SRBevertonHolt : public RecruitmentBase { Type rzero; // Transform input parameters - steep = fims::inv_logit(steep_lo, steep_hi, this->logit_steep); - rzero = fims::exp(this->log_rzero); + steep = fims_math::inv_logit(steep_lo, steep_hi, this->logit_steep); + rzero = fims_math::exp(this->log_rzero); recruits = (0.8 * rzero * steep * spawners) / (0.2 * phi_0 * rzero * (1.0 - steep) + spawners * (steep - 0.2)); @@ -62,6 +62,6 @@ struct SRBevertonHolt : public RecruitmentBase { } }; -} // namespace fims +} // namespace fims_popdy #endif /* FIMS_POPULATION_DYNAMICS_RECRUITMENT_SR_BEVERTON_HOLT_HPP */ diff --git a/inst/include/population_dynamics/selectivity/functors/double_logistic.hpp b/inst/include/population_dynamics/selectivity/functors/double_logistic.hpp index 36a4e111d..51bee5e8a 100644 --- a/inst/include/population_dynamics/selectivity/functors/double_logistic.hpp +++ b/inst/include/population_dynamics/selectivity/functors/double_logistic.hpp @@ -11,7 +11,7 @@ #include "../../../common/fims_math.hpp" #include "selectivity_base.hpp" -namespace fims { +namespace fims_popdy { /** * @brief DoubleLogisticSelectivity class that returns the double logistic @@ -19,18 +19,18 @@ namespace fims { */ template struct DoubleLogisticSelectivity : public SelectivityBase { - Type median_asc; /*!< 50% quantile of the value of the quantity of interest - (x) on the ascending limb of the double logistic curve; e.g. age - at which 50% of the fish are selected */ - Type slope_asc; /*!() {} @@ -39,19 +39,19 @@ struct DoubleLogisticSelectivity : public SelectivityBase { * @brief Method of the double logistic selectivity class that implements the * double logistic function from FIMS math. * - * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - median_{asc}))} - * \left(1-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - median_{desc}))} - * \right)\f$ + * \f$ \frac{1.0}{ 1.0 + exp(-1.0 * slope_{asc} (x - inflection_point_{asc}))} + * \left(1.0-\frac{1.0}{ 1.0 + exp(-1.0 * slope_{desc} (x - + * inflection_point_{desc}))} \right)\f$ * * @param x The independent variable in the double logistic function (e.g., * age or size in selectivity). */ virtual const Type evaluate(const Type &x) { - return fims::double_logistic(median_asc, slope_asc, median_desc, - slope_desc, x); + return fims_math::double_logistic( + inflection_point_asc, slope_asc, inflection_point_desc, slope_desc, x); } }; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_SELECTIVITY_DOUBLE_LOGISTIC_HPP */ diff --git a/inst/include/population_dynamics/selectivity/functors/logistic.hpp b/inst/include/population_dynamics/selectivity/functors/logistic.hpp index 3b94a5e33..cd50a4fd6 100644 --- a/inst/include/population_dynamics/selectivity/functors/logistic.hpp +++ b/inst/include/population_dynamics/selectivity/functors/logistic.hpp @@ -14,7 +14,7 @@ #include "../../../common/fims_math.hpp" #include "selectivity_base.hpp" -namespace fims { +namespace fims_popdy { /** * @brief LogisticSelectivity class that returns the logistic function value @@ -22,10 +22,10 @@ namespace fims { */ template struct LogisticSelectivity : public SelectivityBase { - Type median; /*!< 50% quantile of the value of the quantity of interest (x); - e.g. age at which 50% of the fish are selected */ - Type slope; /*!() {} @@ -35,16 +35,16 @@ struct LogisticSelectivity : public SelectivityBase { * @brief Method of the logistic selectivity class that implements the * logistic function from FIMS math. * - * \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - median))} \f] + * \f[ \frac{1.0}{ 1.0 + exp(-1.0 * slope (x - inflection_point))} \f] * * @param x The independent variable in the logistic function (e.g., age or * size in selectivity). */ virtual const Type evaluate(const Type& x) { - return fims::logistic(median, slope, x); + return fims_math::logistic(inflection_point, slope, x); } }; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_SELECTIVITY_LOGISTIC_HPP */ diff --git a/inst/include/population_dynamics/selectivity/functors/selectivity_base.hpp b/inst/include/population_dynamics/selectivity/functors/selectivity_base.hpp index bbb439f25..a67b9bd8d 100644 --- a/inst/include/population_dynamics/selectivity/functors/selectivity_base.hpp +++ b/inst/include/population_dynamics/selectivity/functors/selectivity_base.hpp @@ -16,7 +16,7 @@ #include "../../../common/model_object.hpp" -namespace fims { +namespace fims_popdy { /** @brief Base class for all selectivity functors. * @@ -24,7 +24,7 @@ namespace fims { */ template -struct SelectivityBase : public FIMSObject { +struct SelectivityBase : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the SelectivityBase class. // this is like a memory tracker. // Assigning each one its own ID is a way to keep track of @@ -53,6 +53,6 @@ struct SelectivityBase : public FIMSObject { template uint32_t SelectivityBase::id_g = 0; -} // namespace fims +} // namespace fims_popdy #endif /* POPULATION_DYNAMICS_SELECTIVITY_BASE_HPP */ diff --git a/inst/templates/module_base_template.hpp b/inst/templates/module_base_template.hpp index 1fc00e36d..150ed17ba 100644 --- a/inst/templates/module_base_template.hpp +++ b/inst/templates/module_base_template.hpp @@ -17,7 +17,7 @@ #include "../../../common/model_object.hpp" -namespace fims { +namespace fims_popdy { /* @brief Base class for all {{{ module_type }}} functors. * @@ -25,7 +25,7 @@ namespace fims { * */ template // Change {{{ module_type }}} to CamelCase in the class definition below. -struct {{{ module_type }}}Base : public FIMSObject { +struct {{{ module_type }}}Base : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the {{{ module_type }}}Base class. // this is like a memory tracker. diff --git a/inst/templates/module_functor_template.hpp b/inst/templates/module_functor_template.hpp index c90f22aac..526e97600 100644 --- a/inst/templates/module_functor_template.hpp +++ b/inst/templates/module_functor_template.hpp @@ -18,7 +18,7 @@ #include "../../../common/fims_math.hpp" #include "{{{module_type}}}_base.hpp" -namespace fims { +namespace fims_popdy { /** * @brief {{{module_name}}}{{{module_type}}} class that returns the sub_module function value @@ -41,7 +41,7 @@ struct {{{module_name}}}{{{module_type}}} : public {{{module_type}}}Base { */ virtual const Type evaluate(const Type& x) { //you will need to add class members as arguments to the function below - return fims::{{{module_name}}}(x); + return fims_popdy::{{{module_name}}}(x); } }; diff --git a/man/create_fims_rcpp_interface.Rd b/man/create_fims_rcpp_interface.Rd index db7f7e71f..364164b88 100644 --- a/man/create_fims_rcpp_interface.Rd +++ b/man/create_fims_rcpp_interface.Rd @@ -52,7 +52,7 @@ create_fims_rcpp_interface( model = "LogisticSelectivity", base_class = "SelectivityInterfaceBase", container = "selectivity_models", - parameters = c("slope", "median"), + parameters = c("slope", "inflection_point"), evaluate_parameter = "x", evaluate_parameter_type = "double" ) diff --git a/src/FIMS.cpp b/src/FIMS.cpp index 01bb1c239..be893e09a 100644 --- a/src/FIMS.cpp +++ b/src/FIMS.cpp @@ -21,11 +21,11 @@ Type objective_function::operator()() { // code below copied from ModularTMBExample/src/tmb_objective_function.cpp // get the singleton instance for Model Class - std::shared_ptr> model = - fims::Model::GetInstance(); + std::shared_ptr> model = + fims_model::Model::GetInstance(); // get the singleton instance for Information Class - std::shared_ptr> information = - fims::Information::GetInstance(); + std::shared_ptr> information = + fims_info::Information::GetInstance(); //update the parameter values for(size_t i =0; i < information->fixed_effects_parameters.size(); i++){ diff --git a/tests/google_benchmark/benchmark_fims_math_double_logistic.cpp b/tests/google_benchmark/benchmark_fims_math_double_logistic.cpp index 46a440f3f..7745e987a 100644 --- a/tests/google_benchmark/benchmark_fims_math_double_logistic.cpp +++ b/tests/google_benchmark/benchmark_fims_math_double_logistic.cpp @@ -4,8 +4,8 @@ void BM_fims_math_double_logistic(benchmark::State &state) { for (auto _ : state) { - // median_asc, slope_asc, median_desc, slope_desc, x - fims::double_logistic(4.0, 2.5, 9.5, 0.5, 7.0); + // inflection_point_asc, slope_asc, inflection_point_desc, slope_desc, x + fims_math::double_logistic(4.0, 2.5, 9.5, 0.5, 7.0); } } diff --git a/tests/google_benchmark/benchmark_fims_math_logistic.cpp b/tests/google_benchmark/benchmark_fims_math_logistic.cpp index 3c6c0cfd0..6c2f18a43 100644 --- a/tests/google_benchmark/benchmark_fims_math_logistic.cpp +++ b/tests/google_benchmark/benchmark_fims_math_logistic.cpp @@ -4,8 +4,8 @@ void BM_fims_math_logistic(benchmark::State &state) { for (auto _ : state) { - // median, slope, x - fims::logistic(4.0, 2.5, 7.0); + // inflection_point, slope, x + fims_math::logistic(4.0, 2.5, 7.0); } } diff --git a/tests/gtest/integration_test_population.cpp b/tests/gtest/integration_test_population.cpp index 809022a8d..606f22eb2 100644 --- a/tests/gtest/integration_test_population.cpp +++ b/tests/gtest/integration_test_population.cpp @@ -39,7 +39,7 @@ namespace input = input_.GetObject(); output = output_.GetObject(); // Declare singleton of population class - fims::Population pop; + fims_popdy::Population pop; // ConfigurePopulationModel, RunModelLoop, and CheckModelOutput // methods are in integration_class.hpp @@ -76,7 +76,7 @@ namespace // When obtaining the numeric values, GetDouble() will convert internal integer representation // to a double. Note that, int and unsigned can be safely converted to double, // but int64_t and uint64_t may lose precision (since mantissa of double is only 52-bits). - double log_rzero = fims::log(R_0[0].GetDouble()); + double log_rzero = fims_math::log(R_0[0].GetDouble()); it = input.find("Phi.0"); JsonArray &Phi0 = (*it).second.GetArray();; @@ -91,12 +91,12 @@ namespace // Expect FIMS value is greater than 0.0 EXPECT_GT(pop.unfished_numbers_at_age[i_age_year], 0.0) << "differ at index " << i_age_year << "; year " << year << "; age" << age; - EXPECT_LE(pop.unfished_numbers_at_age[i_age_year], fims::exp(log_rzero)) + EXPECT_LE(pop.unfished_numbers_at_age[i_age_year], fims_math::exp(log_rzero)) << "differ at index " << i_age_year << "; year " << year << "; age" << age; EXPECT_GT(pop.unfished_spawning_biomass[year], 0.0) << "differ at index " << i_age_year << "; year " << year << "; age" << age; - EXPECT_LE(pop.unfished_spawning_biomass[year], fims::exp(log_rzero) * phi_0) + EXPECT_LE(pop.unfished_spawning_biomass[year], fims_math::exp(log_rzero) * phi_0) << "differ at index " << i_age_year << "; year " << year << "; age" << age; EXPECT_GT(pop.unfished_biomass[year], 0.0) diff --git a/tests/gtest/test_fims_math_ad_fabs_min_max.cpp b/tests/gtest/test_fims_math_ad_fabs_min_max.cpp index c209f16ed..2e8c6acd4 100644 --- a/tests/gtest/test_fims_math_ad_fabs_min_max.cpp +++ b/tests/gtest/test_fims_math_ad_fabs_min_max.cpp @@ -5,41 +5,41 @@ namespace { // Test ad_fabs - TEST(ad_fabs, use_double_values) + TEST(AdFabs, UseDoubleValues) { // Expected value from R: x=2; sqrt(x*x+1e-5) = 2.000002 - EXPECT_NEAR(fims::ad_fabs(2.0), 2.000002, 0.000001); - EXPECT_NE(fims::ad_fabs(2.0), 1.0); + EXPECT_NEAR(fims_math::ad_fabs(2.0), 2.000002, 0.000001); + EXPECT_NE(fims_math::ad_fabs(2.0), 1.0); // Expected value from R: x=2; sqrt(x*x+1e-4) = 2.000025 - EXPECT_NEAR(fims::ad_fabs(2.0, 1e-4), 2.000025, 0.000001); + EXPECT_NEAR(fims_math::ad_fabs(2.0, 1e-4), 2.000025, 0.000001); } // Test ad_min - TEST(ad_min, use_double_values) + TEST(AdMin, UseDoubleValues) { // Expected value from R: a=2.0; b=1.0; c=1e-5; // (a+b-sqrt((a-b)^2+c))*0.5 = 0.9999975 - EXPECT_NEAR(fims::ad_min(2.0, 1.0), 0.9999975, 0.0000001); - EXPECT_NE(fims::ad_min(2.0, 1.0), 2.0); + EXPECT_NEAR(fims_math::ad_min(2.0, 1.0), 0.9999975, 0.0000001); + EXPECT_NE(fims_math::ad_min(2.0, 1.0), 2.0); // Expected value from R: a=2.0; b=1.0; c=1e-4; // (a+b-sqrt((a-b)^2+c))*0.5 = 0.999975 - EXPECT_NEAR(fims::ad_min(2.0, 1.0, 1e-4), 0.999975, 0.000001); + EXPECT_NEAR(fims_math::ad_min(2.0, 1.0, 1e-4), 0.999975, 0.000001); } // Test ad_max - TEST(ad_max, use_double_values) + TEST(AdMax, UseDoubleValues) { // Expected value from R: a=2.0; b=1.0; c=1e-5; // (a+b+sqrt((a-b)^2+c))*0.5 = 2.000002 - EXPECT_NEAR(fims::ad_max(2.0, 1.0), 2.000002, 0.000001); - EXPECT_NE(fims::ad_max(2.0, 1.0), 1.0); + EXPECT_NEAR(fims_math::ad_max(2.0, 1.0), 2.000002, 0.000001); + EXPECT_NE(fims_math::ad_max(2.0, 1.0), 1.0); // Expected value from R: a=2.0; b=1.0; c=1e-4; // (a+b+sqrt((a-b)^2+c))*0.5 = 2.000025 - EXPECT_NEAR(fims::ad_max(2.0, 1.0, 1e-4), 2.000025, 0.000001); + EXPECT_NEAR(fims_math::ad_max(2.0, 1.0, 1e-4), 2.000025, 0.000001); } diff --git a/tests/gtest/test_fims_math_double_logistic.cpp b/tests/gtest/test_fims_math_double_logistic.cpp index a1c84ed79..f1df7c2d1 100644 --- a/tests/gtest/test_fims_math_double_logistic.cpp +++ b/tests/gtest/test_fims_math_double_logistic.cpp @@ -4,11 +4,11 @@ namespace { // Test double logistic using multiple input values - TEST(double_logistic, use_multiple_input_values) + TEST(DoubleLogistic, UseMultipleInputValues) { - std::vector median_asc_value = {1.0, 10.0, 20.5}; + std::vector inflection_point_asc_value = {1.0, 10.0, 20.5}; std::vector slope_asc_value = {0.0, 0.2, 0.05}; - std::vector median_desc_value = {6.0, 15.0, 23.5}; + std::vector inflection_point_desc_value = {6.0, 15.0, 23.5}; std::vector slope_desc_value = {0.0, 0.2, 0.05}; std::vector x_value = {2.0, 20.0, 40.5}; @@ -21,7 +21,7 @@ namespace for (int i = 0; i < expect_value.size(); ++i) { - EXPECT_NEAR(fims::double_logistic(median_asc_value[i], slope_asc_value[i], median_desc_value[i], slope_desc_value[i], x_value[i]), expect_value[i], 0.0001); + EXPECT_NEAR(fims_math::double_logistic(inflection_point_asc_value[i], slope_asc_value[i], inflection_point_desc_value[i], slope_desc_value[i], x_value[i]), expect_value[i], 0.0001); } } diff --git a/tests/gtest/test_fims_math_exp.cpp b/tests/gtest/test_fims_math_exp.cpp index 355140080..ba9d23d55 100644 --- a/tests/gtest/test_fims_math_exp.cpp +++ b/tests/gtest/test_fims_math_exp.cpp @@ -8,32 +8,32 @@ namespace // Not worth to write many tests when testing thin database wrappers, // third-party libraries, or basic variable assignments. - TEST(exp, use_multiple_double_values) + TEST(Exp, UseMultipleDoubleValues) { // Test exp using large negative input value - EXPECT_EQ(fims::exp(-1000000.0), std::exp(-1000000.0)); + EXPECT_EQ(fims_math::exp(-1000000.0), std::exp(-1000000.0)); // Test exp using large positive input value - EXPECT_EQ(fims::exp(1000000.0), std::exp(1000000.0)); + EXPECT_EQ(fims_math::exp(1000000.0), std::exp(1000000.0)); // Test exp using double value 0.0 - EXPECT_EQ(fims::exp(0.0), std::exp(0.0)); + EXPECT_EQ(fims_math::exp(0.0), std::exp(0.0)); // Test exp using double value 1.0 - EXPECT_EQ(fims::exp(1.0), std::exp(1.0)); + EXPECT_EQ(fims_math::exp(1.0), std::exp(1.0)); // Test exp using double value 3.0 - EXPECT_NEAR(fims::exp(3.0), 20.08554, 0.0001); + EXPECT_NEAR(fims_math::exp(3.0), 20.08554, 0.0001); // Test exp using double value -2.5 - EXPECT_NEAR(fims::exp(-2.5), 0.082085, 0.0001); + EXPECT_NEAR(fims_math::exp(-2.5), 0.082085, 0.0001); } - TEST(exp, use_integer_values) + TEST(Exp, UseIntegerValues) { // Test exp using large positive integer value int input_value = 1000000; - EXPECT_EQ(fims::exp(input_value), std::exp(input_value)); + EXPECT_EQ(fims_math::exp(input_value), std::exp(input_value)); // Test exp using integer value 3 - // For fims::exp(3): the output value will be an integer if the input value is an integer + // For fims_math::exp(3): the output value will be an integer if the input value is an integer // need to round the output value before using it as expected true value - EXPECT_EQ(fims::exp(3), 20); + EXPECT_EQ(fims_math::exp(3), 20); } } diff --git a/tests/gtest/test_fims_math_log.cpp b/tests/gtest/test_fims_math_log.cpp index 276b118c9..74cc4989d 100644 --- a/tests/gtest/test_fims_math_log.cpp +++ b/tests/gtest/test_fims_math_log.cpp @@ -8,33 +8,33 @@ namespace // Not worth to write many tests when testing thin database wrappers, // third-party libraries, or basic variable assignments. - TEST(log, use_multiple_double_values) + TEST(Log, UseMultipleDoubleValues) { // Test log using large double value - EXPECT_EQ(fims::log(1000000.0), std::log(1000000.0)); + EXPECT_EQ(fims_math::log(1000000.0), std::log(1000000.0)); // Test log using double value 3.0 // R code that generates true values for the test // log(3.0): 1.098612 - EXPECT_NEAR(fims::log(3.0), 1.098612, 0.0001); + EXPECT_NEAR(fims_math::log(3.0), 1.098612, 0.0001); } - TEST(log, use_integer_values) + TEST(Log, UseIntegerValues) { // Test log using large positive integer value int large_int = 1000000; - EXPECT_EQ(fims::log(large_int), std::log(large_int)); + EXPECT_EQ(fims_math::log(large_int), std::log(large_int)); // log(3.0): 1.098612 int small_int = 3; - EXPECT_EQ(fims::log(small_int), std::log(small_int)); + EXPECT_EQ(fims_math::log(small_int), std::log(small_int)); } // log(-2.5): NaN - TEST(log, use_negative_double_values) + TEST(Log, UseNegativeDoubleValues) { // Test log using negative value -2.5 and expect return of NaN - EXPECT_TRUE(std::isnan(fims::log(-2.5))); + EXPECT_TRUE(std::isnan(fims_math::log(-2.5))); } } diff --git a/tests/gtest/test_fims_math_logistic.cpp b/tests/gtest/test_fims_math_logistic.cpp index c6d5ef898..1b2028985 100644 --- a/tests/gtest/test_fims_math_logistic.cpp +++ b/tests/gtest/test_fims_math_logistic.cpp @@ -5,9 +5,9 @@ namespace { // Test logistic using multiple input values - TEST(logistic, use_multiple_input_values) + TEST(Logistic, UseMultipleInputValues) { - std::vector median_value = {1.0, 10.0, 20.5}; + std::vector inflection_point_value = {1.0, 10.0, 20.5}; std::vector slope_value = {0.0, 0.2, 0.05}; std::vector x_value = {2.0, 20.0, 40.5}; @@ -19,7 +19,7 @@ namespace for (int i = 0; i < expect_value.size(); ++i) { - EXPECT_NEAR(fims::logistic(median_value[i], slope_value[i], x_value[i]), expect_value[i], 0.0001); + EXPECT_NEAR(fims_math::logistic(inflection_point_value[i], slope_value[i], x_value[i]), expect_value[i], 0.0001); } } diff --git a/tests/gtest/test_fims_math_logit.cpp b/tests/gtest/test_fims_math_logit.cpp index 08e93ca1f..833adb272 100644 --- a/tests/gtest/test_fims_math_logit.cpp +++ b/tests/gtest/test_fims_math_logit.cpp @@ -5,7 +5,7 @@ namespace { // Test logit using multiple input values - TEST(logit, use_multiple_input_values) + TEST(Logit, UseMultipleInputValues) { std::vector max_value = {1.0, 10.0, 20.5}; std::vector min_value = {0.0, 0.2, 0.05}; @@ -18,7 +18,7 @@ namespace } - TEST(inv_logit, use_multiple_input_values) + TEST(InvLogit, UseMultipleInputValues) { std::vector max_value = {1.0, 10.0, 20.5}; std::vector min_value = {0.0, 0.2, 0.05}; @@ -31,12 +31,12 @@ namespace for (int i = 0; i < expect_value.size(); ++i) { - EXPECT_NEAR(fims::inv_logit(min_value[i], max_value[i], logit_x_value[i]), + EXPECT_NEAR(fims_math::inv_logit(min_value[i], max_value[i], logit_x_value[i]), expect_value[i], 0.0001); } } - TEST(inv_logit_logit, use_multiple_input_values) + TEST(InvLogitLogit, UseMultipleInputValues) { std::vector max_value = {1.0, 1.0}; @@ -45,13 +45,13 @@ namespace for (int i = 0; i < x_value.size(); ++i) { - EXPECT_EQ(fims::inv_logit(min_value[i], max_value[i], - fims::logit(min_value[i], max_value[i], x_value[i])), + EXPECT_EQ(fims_math::inv_logit(min_value[i], max_value[i], + fims_math::logit(min_value[i], max_value[i], x_value[i])), x_value[i]); } } - TEST(logit_inv_logit, use_multiple_input_values) + TEST(LogitInvLogit, UseMultipleInputValues) { std::vector max_value = {1.0, 1.0}; @@ -60,8 +60,8 @@ namespace for (int i = 0; i < x_value.size(); ++i) { - EXPECT_EQ(fims::logit(min_value[i], max_value[i], - fims::inv_logit(min_value[i], max_value[i], x_value[i])), + EXPECT_EQ(fims_math::logit(min_value[i], max_value[i], + fims_math::inv_logit(min_value[i], max_value[i], x_value[i])), x_value[i]); } diff --git a/tests/gtest/test_growth.cpp b/tests/gtest/test_growth.cpp index 31b35b689..85a90e791 100644 --- a/tests/gtest/test_growth.cpp +++ b/tests/gtest/test_growth.cpp @@ -4,7 +4,7 @@ namespace { - TEST(growth_evaluate, integer_age_input) + TEST(GrowthEvaluate, IntegerAgeInput) { // empirical weight-at-age values from the model comparison project // (via the Rdata object being used by the data group) @@ -16,7 +16,7 @@ namespace EXPECT_EQ(1.0, 1.0); // create a new ewaa singleton class - fims::EWAAgrowth ewaa1; + fims_popdy::EWAAgrowth ewaa1; // set the ewaa values using an initializer list // std::pair is a class template that provides a way to store two heterogeneous objects as a single unit ewaa1.ewaa = @@ -34,10 +34,10 @@ namespace EXPECT_EQ(ewaa1.GetId(), 0); } - TEST(growth_evaluate, double_age_input) + TEST(GrowthEvaluate, DoubleAgeInput) { // create a new ewaa singleton class - fims::EWAAgrowth ewaa2; + fims_popdy::EWAAgrowth ewaa2; // set the ewaa values ewaa2.ewaa = std::map diff --git a/tests/gtest/test_population_MaturityAtAge.cpp b/tests/gtest/test_population_MaturityAtAge.cpp index 975dd98fd..530887f1f 100644 --- a/tests/gtest/test_population_MaturityAtAge.cpp +++ b/tests/gtest/test_population_MaturityAtAge.cpp @@ -6,7 +6,7 @@ namespace { TEST_F(PopulationPrepareTestFixture, CalculateMaturityAA_works) { - double median = 6; + double inflection_point = 6; double slope = 0.15; std::vector expect_maturity(nyears * nages, 0); @@ -14,7 +14,7 @@ namespace for (size_t age = 0; age < nages; age++){ int i_age_year = year * population.nages + age; population.CalculateMaturityAA(i_age_year, age); - expect_maturity[i_age_year] = 1.0/(1.0+exp(-(population.ages[age]-median)*slope)); + expect_maturity[i_age_year] = 1.0/(1.0+exp(-(population.ages[age]-inflection_point)*slope)); } } diff --git a/tests/gtest/test_population_Unfished_Initial.cpp b/tests/gtest/test_population_Unfished_Initial.cpp index 2b95d2cc3..28fdbc369 100644 --- a/tests/gtest/test_population_Unfished_Initial.cpp +++ b/tests/gtest/test_population_Unfished_Initial.cpp @@ -28,7 +28,7 @@ namespace std::vector mortality_Z(nyears * nages, 0); - mortality_Z[i_age_year] = fims::exp(population.log_M[i_age_year]) + + mortality_Z[i_age_year] = fims_math::exp(population.log_M[i_age_year]) + mortality_F[i_age_year]; EXPECT_EQ(population.mortality_Z[i_age_year], mortality_Z[i_age_year]); } @@ -50,7 +50,7 @@ namespace population.CalculateInitialNumbersAA(i_age_year, age); - numbers_at_age[i_age_year] = fims::exp(population.log_init_naa[age]); + numbers_at_age[i_age_year] = fims_math::exp(population.log_init_naa[age]); EXPECT_EQ(population.numbers_at_age[i_age_year], numbers_at_age[i_age_year]); } } @@ -69,8 +69,8 @@ namespace if (age == 0) { - population.unfished_numbers_at_age[i_age_year] = fims::exp(population.recruitment->log_rzero); - test_unfished_numbers_at_age[i_age_year] = fims::exp(population.recruitment->log_rzero); + population.unfished_numbers_at_age[i_age_year] = fims_math::exp(population.recruitment->log_rzero); + test_unfished_numbers_at_age[i_age_year] = fims_math::exp(population.recruitment->log_rzero); } if (year == 0 && age > 0){ @@ -85,7 +85,7 @@ namespace // true values from test test_unfished_numbers_at_age[i_age_year] = test_unfished_numbers_at_age[i_age_year-1] * - fims::exp(-fims::exp(population.log_M[i_age_year-1])); + fims_math::exp(-fims_math::exp(population.log_M[i_age_year-1])); } @@ -99,10 +99,10 @@ namespace // Test fails if log_M from test fixture is not constant over years and ages. population.CalculateUnfishedNumbersAA(i_age_year, i_agem1_yearm1, age); // true values from test - // unfished_numbers_at_age[i_age_year] = unfished_numbers_at_age[i_age_year-1] * fims::exp(-fims::exp(population.log_M[i_age_year-1])); + // unfished_numbers_at_age[i_age_year] = unfished_numbers_at_age[i_age_year-1] * fims_math::exp(-fims_math::exp(population.log_M[i_age_year-1])); test_unfished_numbers_at_age[i_age_year] = test_unfished_numbers_at_age[i_agem1_yearm1] * - fims::exp(-fims::exp(population.log_M[i_agem1_yearm1])); + fims_math::exp(-fims_math::exp(population.log_M[i_agem1_yearm1])); } if(age==(population.nages-1)){ @@ -115,7 +115,7 @@ namespace test_unfished_numbers_at_age[i_age_year] = test_unfished_numbers_at_age[i_age_year] + test_unfished_numbers_at_age[i_agem1_yearm1 + 1] * - fims::exp(-fims::exp(population.log_M[i_agem1_yearm1 + 1])); + fims_math::exp(-fims_math::exp(population.log_M[i_agem1_yearm1 + 1])); } diff --git a/tests/gtest/test_population_dynamics_fleet_initialize_prepare.cpp b/tests/gtest/test_population_dynamics_fleet_initialize_prepare.cpp index 9af08b8c8..af1451172 100644 --- a/tests/gtest/test_population_dynamics_fleet_initialize_prepare.cpp +++ b/tests/gtest/test_population_dynamics_fleet_initialize_prepare.cpp @@ -5,9 +5,9 @@ namespace { - TEST(fleet_tests, Fleet_Initialize_works) + TEST(FleetTests, FleetInitializeWorks) { - fims::Fleet fleet; + fims_popdy::Fleet fleet; int nyears = 30; int nages = 12; fleet.Initialize(nyears, nages); @@ -20,9 +20,9 @@ namespace EXPECT_EQ(fleet.catch_index.size(), nyears); } - TEST(fleet_tests, Fleet_Prepare_works) + TEST(FleetTests, FleetPrepareWorks) { - fims::Fleet fleet; + fims_popdy::Fleet fleet; int nyears = 30; int nages = 12; fleet.Initialize(nyears, nages); @@ -31,12 +31,12 @@ namespace std::default_random_engine generator(seed); // log_Fmort - double log_Fmort_min = fims::log(0.1); - double log_Fmort_max = fims::log(2.3); + double log_Fmort_min = fims_math::log(0.1); + double log_Fmort_max = fims_math::log(2.3); std::uniform_real_distribution log_Fmort_distribution(log_Fmort_min, log_Fmort_max); - double log_q_min = fims::log(0.1); - double log_q_max = fims::log(1); + double log_q_min = fims_math::log(0.1); + double log_q_max = fims_math::log(1); std::uniform_real_distribution log_q_distribution(log_q_min, log_q_max); fleet.log_q = log_q_distribution(generator); for(int i = 0; i < nyears; i++) @@ -47,11 +47,11 @@ namespace // Test fleet.Fmort and fleet.q std::vector Fmort(nyears, 0); - double q = fims::exp(fleet.log_q); + double q = fims_math::exp(fleet.log_q); EXPECT_EQ(fleet.q, q); for (int i = 0; i < nyears; i++) { - Fmort[i] = fims::exp(fleet.log_Fmort[i]); + Fmort[i] = fims_math::exp(fleet.log_Fmort[i]); EXPECT_EQ(fleet.Fmort[i], Fmort[i]); } diff --git a/tests/gtest/test_population_dynamics_maturity_logistic.cpp b/tests/gtest/test_population_dynamics_maturity_logistic.cpp index cc8a5fd1f..3997aa0fa 100644 --- a/tests/gtest/test_population_dynamics_maturity_logistic.cpp +++ b/tests/gtest/test_population_dynamics_maturity_logistic.cpp @@ -5,11 +5,11 @@ namespace { - TEST(logistic_maturity, create_object) + TEST(LogisticMaturity, CreateObject) { - fims::LogisticMaturity maturity; - maturity.median = 20.5; + fims_popdy::LogisticMaturity maturity; + maturity.inflection_point = 20.5; maturity.slope = 0.15; double maturity_x = 40.5; // 1.0/(1.0+exp(-(40.5-20.5)*0.15)) = 0.9525741 diff --git a/tests/gtest/test_population_dynamics_population_initialize_prepare.cpp b/tests/gtest/test_population_dynamics_population_initialize_prepare.cpp index 2b2ded72a..7462302f7 100644 --- a/tests/gtest/test_population_dynamics_population_initialize_prepare.cpp +++ b/tests/gtest/test_population_dynamics_population_initialize_prepare.cpp @@ -79,14 +79,14 @@ namespace std::vector naa(nages, 0); for (int i = 0; i < nages; i++) { - naa[i] = fims::exp(population.log_init_naa[i]); + naa[i] = fims_math::exp(population.log_init_naa[i]); } // Test population.M std::vector M(nyears * nages, 0); for (int i = 0; i < nyears * nages; i++) { - M[i] = fims::exp(population.log_M[i]); + M[i] = fims_math::exp(population.log_M[i]); EXPECT_EQ(population.M[i], M[i]); } EXPECT_EQ(population.M.size(), nyears * nages); @@ -97,7 +97,7 @@ namespace for(size_t i = 0; i < nfleets; i++){ for(size_t y = 0; y < nyears; y++){ size_t index_yf = y * population.nfleets + i; - Fmort[index_yf] = fims::exp(population.fleets[i]->log_Fmort[y]); + Fmort[index_yf] = fims_math::exp(population.fleets[i]->log_Fmort[y]); EXPECT_EQ(population.fleets[i]->Fmort[y], Fmort[index_yf]); } EXPECT_EQ(population.fleets[i]->Fmort.size(), nyears); diff --git a/tests/gtest/test_population_dynamics_recruitment_base.cpp b/tests/gtest/test_population_dynamics_recruitment_base.cpp index 926601966..b37630c51 100644 --- a/tests/gtest/test_population_dynamics_recruitment_base.cpp +++ b/tests/gtest/test_population_dynamics_recruitment_base.cpp @@ -4,9 +4,9 @@ namespace { - TEST(recruitment_deviations, constraint_works) + TEST(RecruitmentDeviations, ConstraintWorks) { - fims::SRBevertonHolt recruit; + fims_popdy::SRBevertonHolt recruit; recruit.recruit_deviations = {-1.0, 2.0, 3.0}; // Test if constrain_deviations = false works diff --git a/tests/gtest/test_population_dynamics_selectivity_double_logistic.cpp b/tests/gtest/test_population_dynamics_selectivity_double_logistic.cpp index 7ea2b42c0..76a14274e 100644 --- a/tests/gtest/test_population_dynamics_selectivity_double_logistic.cpp +++ b/tests/gtest/test_population_dynamics_selectivity_double_logistic.cpp @@ -4,13 +4,13 @@ namespace { - TEST(double_logistic_selectivity, create_object) + TEST(DoubleLogisticSelectivity, CreateObject) { - fims::DoubleLogisticSelectivity fishery_selectivity; - fishery_selectivity.median_asc = 10.5; + fims_popdy::DoubleLogisticSelectivity fishery_selectivity; + fishery_selectivity.inflection_point_asc = 10.5; fishery_selectivity.slope_asc = 0.2; - fishery_selectivity.median_desc = 15.0; + fishery_selectivity.inflection_point_desc = 15.0; fishery_selectivity.slope_desc = 0.05; double fishery_x = 34.5; // 1.0/(1.0+exp(-(34.5-10.5)*0.2)) * (1.0 - 1.0/(1.0+exp(-(34.5-15.0)*0.05))) = 0.2716494 diff --git a/tests/gtest/test_population_dynamics_selectivity_logistic.cpp b/tests/gtest/test_population_dynamics_selectivity_logistic.cpp index b8b500e8b..7dcffa907 100644 --- a/tests/gtest/test_population_dynamics_selectivity_logistic.cpp +++ b/tests/gtest/test_population_dynamics_selectivity_logistic.cpp @@ -5,11 +5,11 @@ namespace { - TEST(logistic_selectivity, create_object) + TEST(LogisticSelectivity, CreateObject) { - fims::LogisticSelectivity fishery_selectivity; - fishery_selectivity.median = 20.5; + fims_popdy::LogisticSelectivity fishery_selectivity; + fishery_selectivity.inflection_point = 20.5; fishery_selectivity.slope = 0.2; double fishery_x = 40.5; // 1.0/(1.0+exp(-(40.5-20.5)*0.2)) = 0.9820138 diff --git a/tests/gtest/test_population_test_fixture.hpp b/tests/gtest/test_population_test_fixture.hpp index 6477358c3..0b91e7fc7 100644 --- a/tests/gtest/test_population_test_fixture.hpp +++ b/tests/gtest/test_population_test_fixture.hpp @@ -21,7 +21,7 @@ class PopulationInitializeTestFixture : public testing::Test { population.nseasons = nseasons; population.nages = nages; for (int i = 0; i < nfleets; i++) { - auto fleet = std::make_shared>(); + auto fleet = std::make_shared>(); population.fleets.push_back(fleet); } } @@ -31,7 +31,7 @@ class PopulationInitializeTestFixture : public testing::Test { // do. Otherwise, it does not need to be provided. virtual void TearDown() {} - fims::Population population; + fims_popdy::Population population; // Use default values from the Li et al., 2021 // https://github.com/Bai-Li-NOAA/Age_Structured_Stock_Assessment_Model_Comparison/blob/master/R/save_initial_input.R @@ -57,28 +57,29 @@ class PopulationPrepareTestFixture : public testing::Test { std::default_random_engine generator(seed); // log_Fmort - double log_Fmort_min = fims::log(0.1); - double log_Fmort_max = fims::log(2.3); + double log_Fmort_min = fims_math::log(0.1); + double log_Fmort_max = fims_math::log(2.3); std::uniform_real_distribution log_Fmort_distribution( log_Fmort_min, log_Fmort_max); // log_q - double log_q_min = fims::log(0.1); - double log_q_max = fims::log(1); + double log_q_min = fims_math::log(0.1); + double log_q_max = fims_math::log(1); std::uniform_real_distribution log_q_distribution(log_q_min, log_q_max); // Make a shared pointer to selectivity and fleet because // fleet object needs a shared pointer in fleet.hpp - // (std::shared_ptr > selectivity;) + // (std::shared_ptr > selectivity;) // and population object needs a shared pointer in population.hpp - // (std::vector > > fleets;) + // (std::vector > > fleets;) // Does Fmort need to be in side of the year loop like log_q? for (int i = 0; i < nfleets; i++) { - auto fleet = std::make_shared>(); - auto selectivity = std::make_shared>(); - selectivity->median = 7; + auto fleet = std::make_shared>(); + auto selectivity = + std::make_shared>(); + selectivity->inflection_point = 7; selectivity->slope = 0.5; fleet->Initialize(nyears, nages); @@ -110,8 +111,8 @@ class PopulationPrepareTestFixture : public testing::Test { } // log_M - double log_M_min = fims::log(0.1); - double log_M_max = fims::log(0.3); + double log_M_min = fims_math::log(0.1); + double log_M_max = fims_math::log(0.3); std::uniform_real_distribution log_M_distribution(log_M_min, log_M_max); for (int i = 0; i < nyears * nages; i++) { @@ -119,8 +120,8 @@ class PopulationPrepareTestFixture : public testing::Test { } // numbers_at_age - double numbers_at_age_min = fims::exp(10.0); - double numbers_at_age_max = fims::exp(12.0); + double numbers_at_age_min = fims_math::exp(10.0); + double numbers_at_age_max = fims_math::exp(12.0); std::uniform_real_distribution numbers_at_age_distribution( numbers_at_age_min, numbers_at_age_max); for (int i = 0; i < (nyears + 1) * nages; i++) { @@ -131,8 +132,8 @@ class PopulationPrepareTestFixture : public testing::Test { double weight_at_age_min = 0.5; double weight_at_age_max = 12.0; - std::shared_ptr> growth = - std::make_shared>(); + std::shared_ptr> growth = + std::make_shared>(); std::uniform_real_distribution weight_at_age_distribution( weight_at_age_min, weight_at_age_max); for (int i = 0; i < nages; i++) { @@ -144,14 +145,14 @@ class PopulationPrepareTestFixture : public testing::Test { population.Prepare(); - auto maturity = std::make_shared>(); - maturity->median = 6; + auto maturity = std::make_shared>(); + maturity->inflection_point = 6; maturity->slope = 0.15; population.maturity = maturity; - auto recruitment = std::make_shared>(); - recruitment->logit_steep = fims::logit(0.2, 1.0, 0.75); - recruitment->log_rzero = fims::log(1000000.0); + auto recruitment = std::make_shared>(); + recruitment->logit_steep = fims_math::logit(0.2, 1.0, 0.75); + recruitment->log_rzero = fims_math::log(1000000.0); recruitment->recruit_deviations.resize(nyears); for (int i = 0; i < recruitment->recruit_deviations.size(); i++) { recruitment->recruit_deviations[i] = 1.0; @@ -161,7 +162,7 @@ class PopulationPrepareTestFixture : public testing::Test { virtual void TearDown() {} - fims::Population population; + fims_popdy::Population population; int id_g = 0; int nyears = 30; int nseasons = 1; diff --git a/tests/gtest/test_sr_beverton_holt.cpp b/tests/gtest/test_sr_beverton_holt.cpp index b7208fe0e..4b2f253c9 100644 --- a/tests/gtest/test_sr_beverton_holt.cpp +++ b/tests/gtest/test_sr_beverton_holt.cpp @@ -4,7 +4,7 @@ namespace { - TEST(sr_beverton_holt_evaluate, use_multiple_inputs) + TEST(SrBevertonHoltEvaluate, UseMultipleInputs) { // BH_fcn(R0 = 1000, h = 0.75, phi0 = 0.1, x = 30): 837.2093 // BH_fcn(R0 = 1000, h = 0.99, phi0 = 0.1, x = 30): 994.1423 @@ -13,9 +13,9 @@ namespace // BH_fcn(R0 = 1000, h = 0.2, phi0 = 0.2, x = 40): 200 // BH_fcn(R0 = 1000, h = 0.99, phi0 = 0.2, x = 40): 990 - fims::SRBevertonHolt recruit1; + fims_popdy::SRBevertonHolt recruit1; - recruit1.logit_steep = fims::logit(0.2, 1.0, 0.7500); + recruit1.logit_steep = fims_math::logit(0.2, 1.0, 0.7500); // The R0 value (1 thousand) here is for this unit test. // It is different than the Model Comparison Project value (1 million). recruit1.log_rzero = std::log(1000.000); @@ -35,8 +35,8 @@ namespace EXPECT_NEAR(recruit1.evaluate(spawners,phi_0), expect_recruit1, 0.0001); EXPECT_EQ(recruit1.GetId(), 0); - fims::SRBevertonHolt recruit2; - recruit2.logit_steep = fims::logit(0.2, 1.0, 0.200); + fims_popdy::SRBevertonHolt recruit2; + recruit2.logit_steep = fims_math::logit(0.2, 1.0, 0.200); recruit2.log_rzero = std::log(1000.000); double spawners2 = 40.000; double phi_02 = 0.2; diff --git a/tests/integration/integration_class.hpp b/tests/integration/integration_class.hpp index 947030174..b89140c76 100644 --- a/tests/integration/integration_class.hpp +++ b/tests/integration/integration_class.hpp @@ -40,7 +40,7 @@ class IntegrationTest { ss << "inputs/C" << i << "/om_output" << j + 1 << ".json"; this->ReadJson(ss.str(), output); - fims::Population pop; + fims_popdy::Population pop; if (!this->ConfigurePopulationModel(pop, input, output)) { good = false; @@ -84,7 +84,7 @@ class IntegrationTest { return true; } - bool ConfigurePopulationModel(fims::Population &pop, + bool ConfigurePopulationModel(fims_popdy::Population &pop, JsonValue &input, JsonValue &output) { @@ -151,10 +151,10 @@ class IntegrationTest { } for (size_t i = 0; i < nfleets; i++) { - std::shared_ptr > f = std::make_shared >(); + std::shared_ptr > f = std::make_shared >(); f->Initialize(nyears, nages); - f->observed_index_data = std::make_shared >(nyears); - f->observed_agecomp_data = std::make_shared >(nyears, nages); + f->observed_index_data = std::make_shared >(nyears); + f->observed_agecomp_data = std::make_shared >(nyears, nages); std::stringstream strs; strs << "fleet" << i + 1; @@ -180,14 +180,14 @@ class IntegrationTest { if (print_statements) { std::cout << "logistic\n"; } - std::shared_ptr > selectivity = std::make_shared >(); + std::shared_ptr > selectivity = std::make_shared >(); it = fsel_o.find("A50.sel1"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median = a50[0].GetDouble(); + selectivity->inflection_point = a50[0].GetDouble(); if (print_statements) { - std::cout << "A50 " << selectivity->median << "\n"; + std::cout << "A50 " << selectivity->inflection_point << "\n"; } } @@ -207,14 +207,14 @@ class IntegrationTest { if (print_statements) { std::cout << "double logistic\n"; } - std::shared_ptr > selectivity = std::make_shared >(); + std::shared_ptr > selectivity = std::make_shared >(); it = fsel_o.find("A50.sel1"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median_asc = a50[0].GetDouble(); + selectivity->inflection_point_asc = a50[0].GetDouble(); if (print_statements) { - std::cout << "A50 asc " << selectivity->median_asc << "\n"; + std::cout << "A50 asc " << selectivity->inflection_point_asc << "\n"; } } @@ -230,9 +230,9 @@ class IntegrationTest { it = fsel_o.find("A50.sel2"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median_desc = a50[0].GetDouble(); + selectivity->inflection_point_desc = a50[0].GetDouble(); if (print_statements) { - std::cout << "A50 desc " << selectivity->median_desc << "\n"; + std::cout << "A50 desc " << selectivity->inflection_point_desc << "\n"; } } @@ -294,11 +294,11 @@ class IntegrationTest { } for (size_t i = 0; i < nsurveys; i++) { - std::shared_ptr > s = std::make_shared >(); + std::shared_ptr > s = std::make_shared >(); s->is_survey = true; s->Initialize(nyears, nages); - s->observed_index_data = std::make_shared >(nyears); - s->observed_agecomp_data = std::make_shared >(nyears, nages); + s->observed_index_data = std::make_shared >(nyears); + s->observed_agecomp_data = std::make_shared >(nyears, nages); std::stringstream strs; strs << "survey" << i + 1; @@ -322,12 +322,12 @@ class IntegrationTest { JsonArray sel_pattern = (*it).second.GetArray(); if (sel_pattern[0].GetInt() == 1) {//logistic - std::shared_ptr > selectivity = std::make_shared >(); + std::shared_ptr > selectivity = std::make_shared >(); it = fsel_o.find("A50.sel1"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median = a50[0].GetDouble(); + selectivity->inflection_point = a50[0].GetDouble(); } it = fsel_o.find("slope.sel1"); @@ -341,12 +341,12 @@ class IntegrationTest { } else if (sel_pattern[0].GetInt() == 2) {//double logistic - std::shared_ptr > selectivity = std::make_shared >(); + std::shared_ptr > selectivity = std::make_shared >(); it = fsel_o.find("A50.sel1"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median_asc = a50[0].GetDouble(); + selectivity->inflection_point_asc = a50[0].GetDouble(); } it = fsel_o.find("slope.sel1"); @@ -358,7 +358,7 @@ class IntegrationTest { it = fsel_o.find("A50.sel2"); if ((*it).second.GetType() == JsonValueType::Array) { JsonArray a50 = (*it).second.GetArray(); - selectivity->median_desc = a50[0].GetDouble(); + selectivity->inflection_point_desc = a50[0].GetDouble(); } it = fsel_o.find("slope.sel2"); @@ -382,14 +382,14 @@ class IntegrationTest { if ((*it).second.GetType() == JsonValueType::Object) { - // f->log_q = fims::log((*it).second.GetDouble()); + // f->log_q = fims_math::log((*it).second.GetDouble()); JsonObject qobj = (*it).second.GetObject(); typename JsonObject::iterator qit = qobj.find("survey1"); if ((*qit).second.GetType() == JsonValueType::Array) { JsonArray a = (*qit).second.GetArray(); - s->log_q = fims::log(a[0].GetDouble()); + s->log_q = fims_math::log(a[0].GetDouble()); if (this->print_statements) { std::cout << "q = " << a[0].GetDouble() << "\nlog(q) = " << s->log_q << "\n"; } @@ -498,8 +498,8 @@ class IntegrationTest { // set recruitment - std::shared_ptr > rec = - std::make_shared >(); + std::shared_ptr > rec = + std::make_shared >(); if (print_statements) { std::cout << "\nRecruitment:\n"; } @@ -521,7 +521,7 @@ class IntegrationTest { it = obj.find("h"); if (it != obj.end()) { if ((*it).second.GetType() == JsonValueType::Array) { - rec->logit_steep = fims::logit(0.2, 1.0, (*it).second.GetArray()[0].GetDouble()); + rec->logit_steep = fims_math::logit(0.2, 1.0, (*it).second.GetArray()[0].GetDouble()); if (print_statements) { std::cout << "'h' " << rec->logit_steep << " \n"; } @@ -574,8 +574,8 @@ class IntegrationTest { pop.recruitment = rec; // set maturity - std::shared_ptr > mat = - std::make_shared >(); + std::shared_ptr > mat = + std::make_shared >(); if (print_statements) { std::cout << "\nMaturity:\n"; @@ -583,9 +583,9 @@ class IntegrationTest { it = obj.find("A50.mat"); if (it != obj.end()) { if ((*it).second.GetType() == JsonValueType::Array) { - mat->median = (*it).second.GetArray()[0].GetDouble(); + mat->inflection_point = (*it).second.GetArray()[0].GetDouble(); if (print_statements) { - std::cout << "median " << mat->median << " \n"; + std::cout << "inflection_point " << mat->inflection_point << " \n"; } } } else { @@ -611,7 +611,7 @@ class IntegrationTest { } // set empirical growth - std::shared_ptr > growth = std::make_shared >(); + std::shared_ptr > growth = std::make_shared >(); std::cout << "Growth:\n"; it = obj.find("W.kg"); @@ -645,7 +645,7 @@ class IntegrationTest { return false; } - std::vector RunModelLoop(fims::Population &pop, + std::vector RunModelLoop(fims_popdy::Population &pop, const JsonValue & input) { JsonObject output; @@ -681,7 +681,7 @@ class IntegrationTest { return pop.numbers_at_age; } - bool CheckModelOutput(fims::Population &pop, + bool CheckModelOutput(fims_popdy::Population &pop, JsonValue &output) { return true; } diff --git a/tests/integration/integration_test_population_tmb_nointerface.cpp b/tests/integration/integration_test_population_tmb_nointerface.cpp index 6ac5e3583..9637d5c89 100644 --- a/tests/integration/integration_test_population_tmb_nointerface.cpp +++ b/tests/integration/integration_test_population_tmb_nointerface.cpp @@ -38,18 +38,18 @@ Type objective_function::operator()(){ //Is log_q supposed to be 1 or 0? log_q.fill(1); - fims::Population pop; + fims_popdy::Population pop; for (int i = 0; i < nfleets; i++) { - std::shared_ptr > f = std::make_shared >(); + std::shared_ptr > f = std::make_shared >(); f->Initialize(nyears, nages); //f->nyears = nyears; - //f->observed_index_data = std::make_shared >(nyears); - //f->observed_agecomp_data = std::make_shared >(nyears, nages); + //f->observed_index_data = std::make_shared >(nyears); + //f->observed_agecomp_data = std::make_shared >(nyears, nages); /* * To run with data, need to change 45 and 46 to: * for(int y=0; y < nyears; y++){ @@ -61,15 +61,15 @@ Type objective_function::operator()(){ } } **Note: this code chunk doesn't compile with error: no match for 'operator[]' - **(operand types are 'std::shared_ptr > > > >' and 'int') + **(operand types are 'std::shared_ptr > > > >' and 'int') */ //set up selectivity - std::shared_ptr > selectivity - = std::make_shared >(); + std::shared_ptr > selectivity + = std::make_shared >(); - selectivity->median = fleet_sel_A50(i); + selectivity->inflection_point = fleet_sel_A50(i); selectivity->slope = fleet_sel_slope(i); f->selectivity = selectivity; @@ -90,21 +90,21 @@ Type objective_function::operator()(){ for (int i = 0; i < nsurveys; i++) { - std::shared_ptr > s = std::make_shared >(); + std::shared_ptr > s = std::make_shared >(); s->Initialize(nyears, nages); - //s->observed_index_data = std::make_shared >(nyears); - //s->observed_agecomp_data = std::make_shared >(nyears, nages); + //s->observed_index_data = std::make_shared >(nyears); + //s->observed_agecomp_data = std::make_shared >(nyears, nages); //set up selectivity - std::shared_ptr > selectivity - = std::make_shared >(); + std::shared_ptr > selectivity + = std::make_shared >(); - selectivity->median = surv_sel_A50(i); + selectivity->inflection_point = surv_sel_A50(i); selectivity->slope = surv_sel_slope(i); s->selectivity = selectivity; for(int y = 0; y::operator()(){ } //Set recruitment - std::shared_ptr > rec = - std::make_shared >(); + std::shared_ptr > rec = + std::make_shared >(); rec->rzero = R0; rec->steep = h; rec->log_sigma_recruit = logR_sd; @@ -142,15 +142,15 @@ Type objective_function::operator()(){ pop.recruitment = rec; //Set maturity - std::shared_ptr > mat = - std::make_shared >(); - mat->median = A50_mat; + std::shared_ptr > mat = + std::make_shared >(); + mat->inflection_point = A50_mat; mat->slope = slope_mat; pop.maturity = mat; //set empirical growth - std::shared_ptr > growth - = std::make_shared > (); + std::shared_ptr > growth + = std::make_shared > (); for (int i = 0; i < nages; i++) { growth->ewaa[ asDouble(ages(i))] = asDouble(W_kg(i))/1000; } diff --git a/tests/integration/third_party/rapidjson/document.h b/tests/integration/third_party/rapidjson/document.h index 7656f446d..0cbb4da55 100755 --- a/tests/integration/third_party/rapidjson/document.h +++ b/tests/integration/third_party/rapidjson/document.h @@ -726,7 +726,7 @@ class GenericValue { //! Equal-to operator with primitive types /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false */ - template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const Type& rhs) const { return *this == GenericValue(rhs); } //! Not-equal-to operator /*! \return !(*this == rhs) @@ -740,17 +740,17 @@ class GenericValue { //! Not-equal-to operator with arbitrary types /*! \return !(*this == rhs) */ - template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const Type& rhs) const { return !(*this == rhs); } //! Equal-to operator with arbitrary types (symmetric version) /*! \return (rhs == lhs) */ - template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const Type& lhs, const GenericValue& rhs) { return rhs == lhs; } //! Not-Equal-to operator with arbitrary types (symmetric version) /*! \return !(rhs == lhs) */ - template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const Type& lhs, const GenericValue& rhs) { return !(rhs == lhs); } //@} //!@name Type diff --git a/tests/testthat/_snaps/create-rcpp-interface-object.md b/tests/testthat/_snaps/create-rcpp-interface-object.md deleted file mode 100644 index 184785fac..000000000 --- a/tests/testthat/_snaps/create-rcpp-interface-object.md +++ /dev/null @@ -1,326 +0,0 @@ -# create_fims_rcpp_interface() works when evaluate_parameter is not null - - class DnormDistributionsInterface : public DistributionsInterfaceBase { - public: - - Parameter x; - Parameter mean; - Parameter sd; - - - DnormDistributionsInterface() : DistributionsInterfaceBase() {} - - virtual ~DnormDistributionsInterface() {} - - - virtual uint32_t get_id() { return this->id; } - - - virtual double evaluate(bool do_log) { - fims::Dnorm object; - object.x = this->x.value; - object.mean = this->mean.value; - object.sd = this->sd.value; - return object.evaluate(bool); - } - - - virtual bool add_to_fims_tmb(){ - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > model0 = - std::make_shared >(); - - - model0->id = this->id; - model0->x= this->x.value; - if (this->x.estimated) { - if (this->x.is_random_effect) { - d0->RegisterRandomEffect(model0->x); - } else { - d0->RegisterParameter(model0->x); - } - } - model0->mean= this->mean.value; - if (this->mean.estimated) { - if (this->mean.is_random_effect) { - d0->RegisterRandomEffect(model0->mean); - } else { - d0->RegisterParameter(model0->mean); - } - } - model0->sd= this->sd.value; - if (this->sd.estimated) { - if (this->sd.is_random_effect) { - d0->RegisterRandomEffect(model0->sd); - } else { - d0->RegisterParameter(model0->sd); - } - } - d0->distribution_models[model0->id]=model0; - - - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > model1 = - std::make_shared >(); - - - model1->id = this->id; - model1->x= this->x.value; - if (this->x.estimated) { - if (this->x.is_random_effect) { - d1->RegisterRandomEffect(model1->x); - } else { - d1->RegisterParameter(model1->x); - } - } - model1->mean= this->mean.value; - if (this->mean.estimated) { - if (this->mean.is_random_effect) { - d1->RegisterRandomEffect(model1->mean); - } else { - d1->RegisterParameter(model1->mean); - } - } - model1->sd= this->sd.value; - if (this->sd.estimated) { - if (this->sd.is_random_effect) { - d1->RegisterRandomEffect(model1->sd); - } else { - d1->RegisterParameter(model1->sd); - } - } - d1->distribution_models[model1->id]=model1; - - - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > model2 = - std::make_shared >(); - - - model2->id = this->id; - model2->x= this->x.value; - if (this->x.estimated) { - if (this->x.is_random_effect) { - d2->RegisterRandomEffect(model2->x); - } else { - d2->RegisterParameter(model2->x); - } - } - model2->mean= this->mean.value; - if (this->mean.estimated) { - if (this->mean.is_random_effect) { - d2->RegisterRandomEffect(model2->mean); - } else { - d2->RegisterParameter(model2->mean); - } - } - model2->sd= this->sd.value; - if (this->sd.estimated) { - if (this->sd.is_random_effect) { - d2->RegisterRandomEffect(model2->sd); - } else { - d2->RegisterParameter(model2->sd); - } - } - d2->distribution_models[model2->id]=model2; - - - std::shared_ptr > d3 = - fims::Information::GetInstance(); - - std::shared_ptr > model3 = - std::make_shared >(); - - - model3->id = this->id; - model3->x= this->x.value; - if (this->x.estimated) { - if (this->x.is_random_effect) { - d3->RegisterRandomEffect(model3->x); - } else { - d3->RegisterParameter(model3->x); - } - } - model3->mean= this->mean.value; - if (this->mean.estimated) { - if (this->mean.is_random_effect) { - d3->RegisterRandomEffect(model3->mean); - } else { - d3->RegisterParameter(model3->mean); - } - } - model3->sd= this->sd.value; - if (this->sd.estimated) { - if (this->sd.is_random_effect) { - d3->RegisterRandomEffect(model3->sd); - } else { - d3->RegisterParameter(model3->sd); - } - } - d3->distribution_models[model3->id]=model3; - - - return true; - - - } - - }; - - //Add the following to the RCpp module definition: rcpp_interface.hpp - - Rcpp::class_("DnormDistributions") - .constructor() - .method("get_id", &DnormDistributionsInterface::get_id) - .method("evaluate", & DnormDistributionsInterface ::evaluate) - .field("x", &DnormDistributionsInterface::x) - .field("mean", &DnormDistributionsInterface::mean) - .field("sd", &DnormDistributionsInterface::sd); - -# create_fims_rcpp_interface() works when evaluate_parameter is null - - class LogisticSelectivityInterface : public SelectivityInterfaceBase { - public: - - Parameter slope; - Parameter median; - - - LogisticSelectivityInterface() : SelectivityInterfaceBase() {} - - virtual ~LogisticSelectivityInterface() {} - - - virtual uint32_t get_id() { return this->id; } - - - virtual bool add_to_fims_tmb(){ - std::shared_ptr > d0 = - fims::Information::GetInstance(); - - std::shared_ptr > model0 = - std::make_shared >(); - - - model0->id = this->id; - model0->slope= this->slope.value; - if (this->slope.estimated) { - if (this->slope.is_random_effect) { - d0->RegisterRandomEffect(model0->slope); - } else { - d0->RegisterParameter(model0->slope); - } - } - model0->median= this->median.value; - if (this->median.estimated) { - if (this->median.is_random_effect) { - d0->RegisterRandomEffect(model0->median); - } else { - d0->RegisterParameter(model0->median); - } - } - d0->selectivity_models[model0->id]=model0; - - - std::shared_ptr > d1 = - fims::Information::GetInstance(); - - std::shared_ptr > model1 = - std::make_shared >(); - - - model1->id = this->id; - model1->slope= this->slope.value; - if (this->slope.estimated) { - if (this->slope.is_random_effect) { - d1->RegisterRandomEffect(model1->slope); - } else { - d1->RegisterParameter(model1->slope); - } - } - model1->median= this->median.value; - if (this->median.estimated) { - if (this->median.is_random_effect) { - d1->RegisterRandomEffect(model1->median); - } else { - d1->RegisterParameter(model1->median); - } - } - d1->selectivity_models[model1->id]=model1; - - - std::shared_ptr > d2 = - fims::Information::GetInstance(); - - std::shared_ptr > model2 = - std::make_shared >(); - - - model2->id = this->id; - model2->slope= this->slope.value; - if (this->slope.estimated) { - if (this->slope.is_random_effect) { - d2->RegisterRandomEffect(model2->slope); - } else { - d2->RegisterParameter(model2->slope); - } - } - model2->median= this->median.value; - if (this->median.estimated) { - if (this->median.is_random_effect) { - d2->RegisterRandomEffect(model2->median); - } else { - d2->RegisterParameter(model2->median); - } - } - d2->selectivity_models[model2->id]=model2; - - - std::shared_ptr > d3 = - fims::Information::GetInstance(); - - std::shared_ptr > model3 = - std::make_shared >(); - - - model3->id = this->id; - model3->slope= this->slope.value; - if (this->slope.estimated) { - if (this->slope.is_random_effect) { - d3->RegisterRandomEffect(model3->slope); - } else { - d3->RegisterParameter(model3->slope); - } - } - model3->median= this->median.value; - if (this->median.estimated) { - if (this->median.is_random_effect) { - d3->RegisterRandomEffect(model3->median); - } else { - d3->RegisterParameter(model3->median); - } - } - d3->selectivity_models[model3->id]=model3; - - - return true; - - - } - - }; - - //Add the following to the RCpp module definition: rcpp_interface.hpp - - Rcpp::class_("LogisticSelectivity") - .constructor() - .method("get_id", &LogisticSelectivityInterface::get_id) - .field("slope", &LogisticSelectivityInterface::slope) - .field("median", &LogisticSelectivityInterface::median); - diff --git a/tests/testthat/test-create-rcpp-interface-object.R b/tests/testthat/test-create-rcpp-interface-object.R index 81bc7786f..95afb7f49 100644 --- a/tests/testthat/test-create-rcpp-interface-object.R +++ b/tests/testthat/test-create-rcpp-interface-object.R @@ -1,31 +1,33 @@ -test_that("create_fims_rcpp_interface() works when evaluate_parameter is not null", { - expect_snapshot_output( - FIMS::create_fims_rcpp_interface( - interface_name = "DnormDistributionsInterface", - model = "Dnorm", - base_class = "DistributionsInterfaceBase", - container = "distribution_models", - parameters = c("x", "mean", "sd"), - evaluate_parameter = "do_log", - evaluate_parameter_type = "bool" - ), - cran = FALSE, - variant = NULL - ) -}) +# tests were commented out because they were out of date and a new template +# is being developed - will need to update expected snapshot output. +# test_that("create_fims_rcpp_interface() works when evaluate_parameter is not null", { +# expect_snapshot_output( +# FIMS::create_fims_rcpp_interface( +# interface_name = "DnormDistributionsInterface", +# model = "Dnorm", +# base_class = "DistributionsInterfaceBase", +# container = "distribution_models", +# parameters = c("x", "mean", "sd"), +# evaluate_parameter = "do_log", +# evaluate_parameter_type = "bool" +# ), +# cran = FALSE, +# variant = NULL +# ) +# }) -test_that("create_fims_rcpp_interface() works when evaluate_parameter is null", { - expect_snapshot_output( - FIMS::create_fims_rcpp_interface( - interface_name = "LogisticSelectivityInterface", - model = "LogisticSelectivity", - base_class = "SelectivityInterfaceBase", - container = "selectivity_models", - parameters = c("slope", "median"), - evaluate_parameter = NULL, - evaluate_parameter_type = NULL - ), - cran = FALSE, - variant = NULL - ) -}) +# test_that("create_fims_rcpp_interface() works when evaluate_parameter is null", { +# expect_snapshot_output( +# FIMS::create_fims_rcpp_interface( +# interface_name = "LogisticSelectivityInterface", +# model = "LogisticSelectivity", +# base_class = "SelectivityInterfaceBase", +# container = "selectivity_models", +# parameters = c("slope", "inflection_point"), +# evaluate_parameter = NULL, +# evaluate_parameter_type = NULL +# ), +# cran = FALSE, +# variant = NULL +# ) +# }) diff --git a/tests/testthat/test-fims-estimation.R b/tests/testthat/test-fims-estimation.R index 8dbd37263..20344b303 100644 --- a/tests/testthat/test-fims-estimation.R +++ b/tests/testthat/test-fims-estimation.R @@ -64,9 +64,9 @@ setup_fims <- function(om_input, om_output, em_input) { # Maturity test_env$maturity <- new(test_env$fims$LogisticMaturity) - test_env$maturity$median$value <- om_input$A50.mat - test_env$maturity$median$is_random_effect <- FALSE - test_env$maturity$median$estimated <- FALSE + test_env$maturity$inflection_point$value <- om_input$A50.mat + test_env$maturity$inflection_point$is_random_effect <- FALSE + test_env$maturity$inflection_point$estimated <- FALSE test_env$maturity$slope$value <- om_input$slope test_env$maturity$slope$is_random_effect <- FALSE test_env$maturity$slope$estimated <- FALSE @@ -74,9 +74,9 @@ setup_fims <- function(om_input, om_output, em_input) { # Fleet # Create the fishing fleet test_env$fishing_fleet_selectivity <- new(test_env$fims$LogisticSelectivity) - test_env$fishing_fleet_selectivity$median$value <- om_input$sel_fleet$fleet1$A50.sel1 - test_env$fishing_fleet_selectivity$median$is_random_effect <- FALSE - test_env$fishing_fleet_selectivity$median$estimated <- TRUE + test_env$fishing_fleet_selectivity$inflection_point$value <- om_input$sel_fleet$fleet1$A50.sel1 + test_env$fishing_fleet_selectivity$inflection_point$is_random_effect <- FALSE + test_env$fishing_fleet_selectivity$inflection_point$estimated <- TRUE test_env$fishing_fleet_selectivity$slope$value <- om_input$sel_fleet$fleet1$slope.sel1 test_env$fishing_fleet_selectivity$slope$is_random_effect <- FALSE test_env$fishing_fleet_selectivity$slope$estimated <- TRUE @@ -101,9 +101,9 @@ setup_fims <- function(om_input, om_output, em_input) { # Create the survey fleet test_env$survey_fleet_selectivity <- new(test_env$fims$LogisticSelectivity) - test_env$survey_fleet_selectivity$median$value <- om_input$sel_survey$survey1$A50.sel1 - test_env$survey_fleet_selectivity$median$is_random_effect <- FALSE - test_env$survey_fleet_selectivity$median$estimated <- TRUE + test_env$survey_fleet_selectivity$inflection_point$value <- om_input$sel_survey$survey1$A50.sel1 + test_env$survey_fleet_selectivity$inflection_point$is_random_effect <- FALSE + test_env$survey_fleet_selectivity$inflection_point$estimated <- TRUE test_env$survey_fleet_selectivity$slope$value <- om_input$sel_survey$survey1$slope.sel1 test_env$survey_fleet_selectivity$slope$is_random_effect <- FALSE test_env$survey_fleet_selectivity$slope$estimated <- TRUE @@ -174,30 +174,21 @@ test_that("deterministic test of fims", { # log(R0) fims_logR0 <- sdr_fixed[1, "Estimate"] expect_gt(fims_logR0, 0.0) - # absolute relative error (are) - fims_logR0_are <- abs(fims_logR0 - log(om_input$R0)) / - log(om_input$R0) - expect_lte(fims_logR0_are, 0.0001) + expect_equal(fims_logR0, log(om_input$R0)) # Numbers at age for (i in 1:length(c(t(om_output$N.age)))) { - naa_are <- abs(report$naa[[1]][i] - c(t(om_output$N.age))[i]) / - c(t(om_output$N.age))[i] - expect_lte(naa_are, 0.001) + expect_equal(report$naa[[1]][i], c(t(om_output$N.age))[i]) } # Biomass for (i in 1:length(om_output$biomass.mt)) { - biomass_are <- abs(report$biomass[[1]][i] - om_output$biomass.mt[i]) / - om_output$biomass.mt[i] - expect_lte(biomass_are, 0.001) + expect_equal(report$biomass[[1]][i], om_output$biomass.mt[i]) } # Spawning biomass for (i in 1:length(om_output$SSB)) { - sb_are <- abs(report$ssb[[1]][i] - om_output$SSB[i]) / - om_output$SSB[i] - expect_lte(sb_are, 0.001) + expect_equal(report$ssb[[1]][i], om_output$SSB[i]) } # Recruitment @@ -206,9 +197,7 @@ test_that("deterministic test of fims", { ) for (i in 1:length(om_output$N.age[, 1])) { - fims_naa1_are <- abs(fims_naa[i, 1] - om_output$N.age[i, 1]) / - om_output$N.age[i, 1] - expect_lte(fims_naa1_are, 0.001) + expect_equal(fims_naa[i, 1], om_output$N.age[i, 1]) } expect_equal( @@ -217,9 +206,7 @@ test_that("deterministic test of fims", { ) for (i in 1:length(om_output$N.age[, 1])) { - recruitment_are <- abs(report$recruitment[[1]][i] - om_output$N.age[i, 1]) / - om_output$N.age[i, 1] - expect_lte(recruitment_are, 0.001) + expect_equal(report$recruitment[[1]][i], om_output$N.age[i, 1]) } # recruitment deviations (fixed at initial "true" values) @@ -232,23 +219,21 @@ test_that("deterministic test of fims", { fims_index <- report$exp_index # Expect small relative error for deterministic test for (i in 1:length(om_output$L.mt$fleet1)) { - fims_object_are <- abs(fims_index[[1]][i] - om_output$L.mt$fleet1[i]) / - om_output$L.mt$fleet1[i] - expect_lte(fims_object_are, 0.001) + expect_equal(fims_index[[1]][i], om_output$L.mt$fleet1[i]) } + # Expect small relative error for deterministic test fims_object_are <- rep(0, length(em_input$L.obs$fleet1)) for (i in 1:length(em_input$L.obs$fleet1)) { fims_object_are[i] <- abs(fims_index[[1]][i] - em_input$L.obs$fleet1[i]) / em_input$L.obs$fleet1[i] } + # Expect 95% of relative error to be within 2*cv expect_lte(sum(fims_object_are > om_input$cv.L$fleet1 * 2.0), length(em_input$L.obs$fleet1) * 0.05) # Expected catch number at age for (i in 1:length(c(t(om_output$L.age$fleet1)))) { - cnaa_are <- abs(report$cnaa[[1]][i] - c(t(om_output$L.age$fleet1))[i]) / - c(t(om_output$L.age$fleet1))[i] - expect_lte(cnaa_are, 0.001) + expect_equal(report$cnaa[[1]][i], c(t(om_output$L.age$fleet1))[i]) } # Expected catch number at age in proportion @@ -259,7 +244,7 @@ test_that("deterministic test of fims", { om_cnaa_proportion <- om_output$L.age$fleet1 / rowSums(om_output$L.age$fleet1) for (i in 1:length(c(t(om_cnaa_proportion)))) { - expect_lte(abs(c(t(fims_cnaa_proportion))[i] - c(t(om_cnaa_proportion))[i]), 0.001) + expect_equal(c(t(fims_cnaa_proportion))[i], c(t(om_cnaa_proportion))[i]) } # Expected survey index @@ -267,9 +252,7 @@ test_that("deterministic test of fims", { expect_equal(fims_index[[2]], apply(cwaa, 1, sum) * om_output$survey_q$survey1) for (i in 1:length(om_output$survey_index_biomass$survey1)) { - fims_object_are <- abs(fims_index[[2]][i] - om_output$survey_index_biomass$survey1[i]) / - om_output$survey_index_biomass$survey1[i] - expect_lte(fims_object_are, 0.001) + expect_equal(fims_index[[2]][i], om_output$survey_index_biomass$survey1[i]) } fims_object_are <- rep(0, length(em_input$surveyB.obs$survey1)) @@ -285,16 +268,14 @@ test_that("deterministic test of fims", { ) for (i in 1:length(c(t(om_output$survey_age_comp$survey1)))) { - cnaa_are <- abs(report$cnaa[[2]][i] - c(t(om_output$survey_age_comp$survey1))[i]) / - c(t(om_output$survey_age_comp$survey1))[i] - expect_lte(cnaa_are, 0.001) + expect_equal(report$cnaa[[2]][i], c(t(om_output$survey_age_comp$survey1))[i]) } fims_cnaa_proportion <- fims_cnaa / rowSums(fims_cnaa) om_cnaa_proportion <- om_output$survey_age_comp$survey1 / rowSums(om_output$survey_age_comp$survey1) for (i in 1:length(c(t(om_cnaa_proportion)))) { - expect_lte(abs(c(t(fims_cnaa_proportion))[i] - c(t(om_cnaa_proportion))[i]), 0.001) + expect_equal(c(t(fims_cnaa_proportion))[i], c(t(om_cnaa_proportion))[i]) } deterministic_env$fims$clear() @@ -320,7 +301,8 @@ test_that("nll test of fims", { # log(R0) fims_logR0 <- sdr_fixed[1, "Estimate"] - expect_lte(abs(fims_logR0 - log(om_input$R0)) / log(om_input$R0), 0.0001) + # expect_lte(abs(fims_logR0 - log(om_input$R0)) / log(om_input$R0), 0.0001) + expect_equal(fims_logR0, log(om_input$R0)) # Call report using deterministic parameter values # obj$report() requires parameter list to avoid errors @@ -629,9 +611,9 @@ test_that("run FIMS in a for loop", { # Maturity maturity <- new(fims$LogisticMaturity) - maturity$median$value <- om_input$A50.mat - maturity$median$is_random_effect <- FALSE - maturity$median$estimated <- FALSE + maturity$inflection_point$value <- om_input$A50.mat + maturity$inflection_point$is_random_effect <- FALSE + maturity$inflection_point$estimated <- FALSE maturity$slope$value <- om_input$slope maturity$slope$is_random_effect <- FALSE maturity$slope$estimated <- FALSE @@ -639,9 +621,9 @@ test_that("run FIMS in a for loop", { # Fleet # Create the fishing fleet fishing_fleet_selectivity <- new(fims$LogisticSelectivity) - fishing_fleet_selectivity$median$value <- om_input$sel_fleet$fleet1$A50.sel1 - fishing_fleet_selectivity$median$is_random_effect <- FALSE - fishing_fleet_selectivity$median$estimated <- TRUE + fishing_fleet_selectivity$inflection_point$value <- om_input$sel_fleet$fleet1$A50.sel1 + fishing_fleet_selectivity$inflection_point$is_random_effect <- FALSE + fishing_fleet_selectivity$inflection_point$estimated <- TRUE fishing_fleet_selectivity$slope$value <- om_input$sel_fleet$fleet1$slope.sel1 fishing_fleet_selectivity$slope$is_random_effect <- FALSE fishing_fleet_selectivity$slope$estimated <- TRUE @@ -666,9 +648,9 @@ test_that("run FIMS in a for loop", { # Create the survey fleet survey_fleet_selectivity <- new(fims$LogisticSelectivity) - survey_fleet_selectivity$median$value <- om_input$sel_survey$survey1$A50.sel1 - survey_fleet_selectivity$median$is_random_effect <- FALSE - survey_fleet_selectivity$median$estimated <- TRUE + survey_fleet_selectivity$inflection_point$value <- om_input$sel_survey$survey1$A50.sel1 + survey_fleet_selectivity$inflection_point$is_random_effect <- FALSE + survey_fleet_selectivity$inflection_point$estimated <- TRUE survey_fleet_selectivity$slope$value <- om_input$sel_survey$survey1$slope.sel1 survey_fleet_selectivity$slope$is_random_effect <- FALSE survey_fleet_selectivity$slope$estimated <- TRUE @@ -720,7 +702,7 @@ test_that("run FIMS in a for loop", { control = list(maxit = 1000000, reltol = 1e-15) )) - report <- obj$report() + report <- obj$report(obj$par) expect_false(is.null(report)) max_gradient <- max(abs(obj$gr(opt$par))) diff --git a/tests/testthat/test-fims-rcpp.R b/tests/testthat/test-fims-rcpp.R index df20343b5..b4489eebd 100644 --- a/tests/testthat/test-fims-rcpp.R +++ b/tests/testthat/test-fims-rcpp.R @@ -6,7 +6,7 @@ test_that("Rcpp interface works for modules", { expect_no_error(logistic_selectivity <- new(fims$LogisticSelectivity)) expect_no_error(ewaa_growth <- new(fims$EWAAgrowth)) logistic_selectivity$slope$value <- .7 - logistic_selectivity$median$value <- 5.0 + logistic_selectivity$inflection_point$value <- 5.0 expect_equal(logistic_selectivity$slope$value, 0.7) expect_equal(logistic_selectivity$get_id(), 1) diff --git a/tests/testthat/test-get-parameter-vector.R b/tests/testthat/test-get-parameter-vector.R index f89897ae2..4a0e74a5f 100644 --- a/tests/testthat/test-get-parameter-vector.R +++ b/tests/testthat/test-get-parameter-vector.R @@ -2,18 +2,18 @@ test_that("test get parameter vector", { fims <- Rcpp::Module("fims", PACKAGE = "FIMS") # Create selectivity selectivity <- new(fims$LogisticSelectivity) - selectivity$median$value <- 10.0 - selectivity$median$min <- 8.0 - selectivity$median$max <- 12.0 - selectivity$median$is_random_effect <- FALSE - selectivity$median$estimated <- TRUE + selectivity$inflection_point$value <- 10.0 + selectivity$inflection_point$min <- 8.0 + selectivity$inflection_point$max <- 12.0 + selectivity$inflection_point$is_random_effect <- FALSE + selectivity$inflection_point$estimated <- TRUE selectivity$slope$value <- 0.2 selectivity$slope$is_random_effect <- FALSE selectivity$slope$estimated <- TRUE fims$CreateTMBModel() p <- fims$get_fixed() - sel_parm <- c(selectivity$median$value, selectivity$slope$value) + sel_parm <- c(selectivity$inflection_point$value, selectivity$slope$value) expect_equal(sel_parm, p) # test fims clear @@ -31,15 +31,15 @@ test_that("test get parameter vector", { expect_equal(numeric(0), p) fims2 <- Rcpp::Module("fims", PACKAGE = "FIMS") selectivity <- new(fims2$LogisticSelectivity) - selectivity$median$value <- 11.0 - selectivity$median$min <- 8.0 - selectivity$median$max <- 12.0 - selectivity$median$is_random_effect <- FALSE - selectivity$median$estimated <- TRUE + selectivity$inflection_point$value <- 11.0 + selectivity$inflection_point$min <- 8.0 + selectivity$inflection_point$max <- 12.0 + selectivity$inflection_point$is_random_effect <- FALSE + selectivity$inflection_point$estimated <- TRUE selectivity$slope$value <- 0.5 selectivity$slope$is_random_effect <- FALSE selectivity$slope$estimated <- TRUE - sel_parm <- c(selectivity$median$value, selectivity$slope$value) + sel_parm <- c(selectivity$inflection_point$value, selectivity$slope$value) recruitment <- new(fims2$BevertonHoltRecruitment) h <- 0.75 r0 <- 1000000.0 diff --git a/tests/testthat/test-maturity-interface.R b/tests/testthat/test-maturity-interface.R index 20cfc660b..e2fae40bb 100644 --- a/tests/testthat/test-maturity-interface.R +++ b/tests/testthat/test-maturity-interface.R @@ -4,19 +4,19 @@ test_that("Maturity input settings work as expected", { # Create maturity1 maturity1 <- new(fims$LogisticMaturity) - maturity1$median$value <- 10.0 - maturity1$median$min <- 8.0 - maturity1$median$max <- 12.0 - maturity1$median$is_random_effect <- TRUE - maturity1$median$estimated <- TRUE + maturity1$inflection_point$value <- 10.0 + maturity1$inflection_point$min <- 8.0 + maturity1$inflection_point$max <- 12.0 + maturity1$inflection_point$is_random_effect <- TRUE + maturity1$inflection_point$estimated <- TRUE maturity1$slope$value <- 0.2 expect_equal(maturity1$get_id(), 1) - expect_equal(maturity1$median$value, 10.0) - expect_equal(maturity1$median$min, 8.0) - expect_equal(maturity1$median$max, 12.0) - expect_true(maturity1$median$is_random_effect) - expect_true(maturity1$median$estimated) + expect_equal(maturity1$inflection_point$value, 10.0) + expect_equal(maturity1$inflection_point$min, 8.0) + expect_equal(maturity1$inflection_point$max, 12.0) + expect_true(maturity1$inflection_point$is_random_effect) + expect_true(maturity1$inflection_point$estimated) expect_equal(maturity1$slope$value, 0.2) expect_equal(maturity1$evaluate(10.0), 0.5) diff --git a/tests/testthat/test-selectivity-interface.R b/tests/testthat/test-selectivity-interface.R index 1eb094669..fb5e57b73 100644 --- a/tests/testthat/test-selectivity-interface.R +++ b/tests/testthat/test-selectivity-interface.R @@ -4,19 +4,19 @@ test_that("Selectivity input settings work as expected", { # Create selectivity1 selectivity1 <- new(fims$LogisticSelectivity) - selectivity1$median$value <- 10.0 - selectivity1$median$min <- 8.0 - selectivity1$median$max <- 12.0 - selectivity1$median$is_random_effect <- TRUE - selectivity1$median$estimated <- TRUE + selectivity1$inflection_point$value <- 10.0 + selectivity1$inflection_point$min <- 8.0 + selectivity1$inflection_point$max <- 12.0 + selectivity1$inflection_point$is_random_effect <- TRUE + selectivity1$inflection_point$estimated <- TRUE selectivity1$slope$value <- 0.2 expect_equal(selectivity1$get_id(), 1) - expect_equal(selectivity1$median$value, 10.0) - expect_equal(selectivity1$median$min, 8.0) - expect_equal(selectivity1$median$max, 12.0) - expect_true(selectivity1$median$is_random_effect) - expect_true(selectivity1$median$estimated) + expect_equal(selectivity1$inflection_point$value, 10.0) + expect_equal(selectivity1$inflection_point$min, 8.0) + expect_equal(selectivity1$inflection_point$max, 12.0) + expect_true(selectivity1$inflection_point$is_random_effect) + expect_true(selectivity1$inflection_point$estimated) expect_equal(selectivity1$slope$value, 0.2) expect_equal(selectivity1$evaluate(10.0), 0.5) @@ -28,13 +28,13 @@ test_that("Selectivity input settings work as expected", { # Test double logistic selectivity3 <- new(fims$DoubleLogisticSelectivity) - selectivity3$median_asc$value <- 10.5 + selectivity3$inflection_point_asc$value <- 10.5 selectivity3$slope_asc$value <- 0.2 - selectivity3$median_desc$value <- 15.0 + selectivity3$inflection_point_desc$value <- 15.0 selectivity3$slope_desc$value <- 0.05 expect_equal(selectivity3$get_id(), 3) - expect_equal(selectivity3$median_asc$value, 10.5) + expect_equal(selectivity3$inflection_point_asc$value, 10.5) expect_equal(selectivity3$slope_asc$value, 0.2) # R code that generates true value for the test # 1.0/(1.0+exp(-(34.5-10.5)*0.2)) * (1.0 - 1.0/(1.0+exp(-(34.5-15)*0.05))) = 0.2716494 diff --git a/vignettes/fims-demo.Rmd b/vignettes/fims-demo.Rmd index 4657a8da8..0c093cee2 100644 --- a/vignettes/fims-demo.Rmd +++ b/vignettes/fims-demo.Rmd @@ -120,16 +120,16 @@ fishing_fleet_age_comp$age_comp_data <- fishery_agecomp * 200 ``` #### Fleet Selectivity -Now that we've passed in data for the fishing fleet, we need to set up its selectivity module. We will set this to be selectivity function using the LogisticSelectivity module. The`methods::show()` function indicates this module has two parameter fields: *median* and *slope*, and an `evaluate()` and `get_id()` function. +Now that we've passed in data for the fishing fleet, we need to set up its selectivity module. We will set this to be selectivity function using the LogisticSelectivity module. The`methods::show()` function indicates this module has two parameter fields: *inflection_point* and *slope*, and an `evaluate()` and `get_id()` function. Each variable of [Parameter class](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp) has three additional fields: *value*, *is_random_effect*, and *estimated*. Currently, FIMS is not set up to run random effects. The default value for this field and the *estimate* field is currently set to `FALSE`. We can use the *value* field to input variables defined in the Model Comparison project. ```{r fleet_selectivity} methods::show(Fims$LogisticSelectivity) fishing_fleet_selectivity <- methods::new(Fims$LogisticSelectivity) -fishing_fleet_selectivity$median$value <- 2.0 -fishing_fleet_selectivity$median$is_random_effect <- FALSE -fishing_fleet_selectivity$median$estimated <- TRUE +fishing_fleet_selectivity$inflection_point$value <- 2.0 +fishing_fleet_selectivity$inflection_point$is_random_effect <- FALSE +fishing_fleet_selectivity$inflection_point$estimated <- TRUE fishing_fleet_selectivity$slope$value <- 1.0 fishing_fleet_selectivity$slope$is_random_effect <- FALSE fishing_fleet_selectivity$slope$estimated <- TRUE @@ -198,9 +198,9 @@ survey_fleet_age_comp$age_comp_data <- survey_agecomp * 200 ```{r survey-selectivity} survey_fleet_selectivity <- new(Fims$LogisticSelectivity) -survey_fleet_selectivity$median$value <- 1.5 -survey_fleet_selectivity$median$is_random_effect <- FALSE -survey_fleet_selectivity$median$estimated <- TRUE +survey_fleet_selectivity$inflection_point$value <- 1.5 +survey_fleet_selectivity$inflection_point$is_random_effect <- FALSE +survey_fleet_selectivity$inflection_point$estimated <- TRUE survey_fleet_selectivity$slope$value <- 2.0 survey_fleet_selectivity$slope$is_random_effect <- FALSE survey_fleet_selectivity$slope$estimated <- TRUE @@ -285,9 +285,9 @@ Each population will also need a maturity model. Here we define a logistic matur ```{r maturity} # Maturity maturity <- new(Fims$LogisticMaturity) -maturity$median$value <- 2.25 -maturity$median$is_random_effect <- FALSE -maturity$median$estimated <- FALSE +maturity$inflection_point$value <- 2.25 +maturity$inflection_point$is_random_effect <- FALSE +maturity$inflection_point$estimated <- FALSE maturity$slope$value <- 3 maturity$slope$is_random_effect <- FALSE maturity$slope$estimated <- FALSE diff --git a/vignettes/fims-path-maturity.Rmd b/vignettes/fims-path-maturity.Rmd index 309040e6b..04e433431 100644 --- a/vignettes/fims-path-maturity.Rmd +++ b/vignettes/fims-path-maturity.Rmd @@ -58,9 +58,9 @@ library(FIMS) Fims <- Rcpp::Module("fims", PACKAGE = "FIMS") maturity <- new(Fims$LogisticMaturity) # Populate the maturity module with parameter values. -maturity$median$value <- 10 -maturity$median$is_random_effect <- FALSE -maturity$median$estimated <- FALSE +maturity$inflection_point$value <- 10 +maturity$inflection_point$is_random_effect <- FALSE +maturity$inflection_point$estimated <- FALSE maturity$slope$value <- 0.2 maturity$slope$is_random_effect <- FALSE maturity$slope$estimated <- FALSE @@ -94,13 +94,13 @@ The fields specific to the maturity module that are accessible from R are define ```{Rcpp, class.source = "rcppchunk", eval = FALSE} Rcpp::class_("LogisticMaturity") .constructor() - .field("median", &LogisticMaturityInterface::median) + .field("inflection_point", &LogisticMaturityInterface::inflection_point) .field("slope", &LogisticMaturityInterface::slope) .method("get_id", &LogisticMaturityInterface::get_id) .method("evaluate", &LogisticMaturityInterface::evaluate); ``` -The code above shows that there are two maturity module-specific fields (i.e., parameters), `median` and `slope`, and two maturity module-specific methods (i.e., functions), `get_id` and `evaluate`. Defined methods are functions that can be called from R. You can access the fields and methods from R using the `show(Fims)` function. +The code above shows that there are two maturity module-specific fields (i.e., parameters), `inflection_point` and `slope`, and two maturity module-specific methods (i.e., functions), `get_id` and `evaluate`. Defined methods are functions that can be called from R. You can access the fields and methods from R using the `show(Fims)` function. The maturity module's `get_id` method returns a unique ID for this specific module. @@ -124,8 +124,8 @@ class MaturityInterfaceBase : public FIMSRcppInterfaceBase { class LogisticMaturityInterface : public MaturityInterfaceBase { public: - Parameter median; /**< the index value at which the response reaches .5 */ - Parameter slope; /**< the width of the curve at the median */ + Parameter inflection_point; /**< the index value at which the response reaches .5 */ + Parameter slope; /**< the width of the curve at the inflection_point */ ... } ``` @@ -136,7 +136,7 @@ We typically will reference the child class from R to specify the formulation of knitr::include_graphics("figures/fims-path-maturity-1.png") ``` -All Rcpp interface classes from FIMS define parameters (e.g., `median`, `slope`) using the `Parameter` class defined in [rcpp_interface_base.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp) in the directory [inst/include/interface/rcpp/rcpp_objects](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/interface/rcpp/rcpp_objects): +All Rcpp interface classes from FIMS define parameters (e.g., `inflection_point`, `slope`) using the `Parameter` class defined in [rcpp_interface_base.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp) in the directory [inst/include/interface/rcpp/rcpp_objects](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/interface/rcpp/rcpp_objects): ```{Rcpp, class.source = "rcppchunk", eval = FALSE} class Parameter { @@ -185,7 +185,7 @@ namespace fims{ } ``` -is considered to be a part of the `fims` namespace. C++ classes written within the `fims` namespace can be accessed within the C++ code base using `fims::`. +is considered to be a part of the `fims` namespace. C++ classes written within the `fims` namespace can be accessed within the C++ code base using `fims_popdy::`. There are some exceptions where `{TMB}` specific code is referenced within the `fims` namespace. In these cases, code is written within an `#ifdef TMB_MODEL` wrapper, which means the code is only defined if `{TMB}` is being used. Given the addition of a new platform, eg. stan, a new wrapper could be added to define platform specific code for these sections. @@ -195,16 +195,16 @@ namespace fims { #ifdef TMB_MODEL /** - * @brief ModelTraits class that contains the DataVector + * @brief fims::ModelTraits class that contains the DataVector * and ParameterVector types. */ -template -struct ModelTraits { - typedef typename CppAD::vector DataVector; /**< This is a vector +template +struct fims::ModelTraits { + typedef typename CppAD::vector DataVector; /**< This is a vector of the data that is differentiable */ - typedef typename CppAD::vector ParameterVector; /**< This is a + typedef typename CppAD::vector ParameterVector; /**< This is a vector of the parameters that is differentiable */ - typedef typename tmbutils::vector + typedef typename tmbutils::vector EigenVector; /**< This is a vector as defined in TMB's namespace Eigen */ }; #endif /* TMB_MODEL */ @@ -220,57 +220,57 @@ struct ModelTraits { Each Rcpp interface object includes an `add_to_fims_tmb()` function. There are two shared pointers set up within this function, one to link each Rcpp interface object (e.g., LogisticMaturityInterface) to the `Information` class in the `fims` namespace defined in [information.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/common/information.hpp) in the directory [inst/include/common](https://github.com/NOAA-FIMS/FIMS/tree/main/inst/include/common) and one to link to the matching class in the `fims` namespace. In our maturity example, this would be the `LogisticMaturity` class in the `fims` namespace defined in [logistic.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/population_dynamics/maturity/functors/logistic.hpp) in the directory [inst/include/population_dynamics/maturity/functors](https://github.com/NOAA-FIMS/FIMS/tree/main/inst/include/population_dynamics/maturity/functors). -Within the rcpp_maturity.hpp file, we can link to the `fims::Information` class in order to register maturity parameters and specify whether or not they are random effects. We do this by setting up two pointers in the interface: d0 which points to information, and lm0, which points to the logistic maturity module. +Within the rcpp_maturity.hpp file, we can link to the `fims_info::Information` class in order to register maturity parameters and specify whether or not they are random effects. We do this by setting up two pointers in the interface: d0 which points to information, and lm0, which points to the logistic maturity module. ```{Rcpp, class.source = "rcppchunk", eval = FALSE} //file: rcpp_maturity.hpp -//d0 is a shared pointer that points to fims::Information -std::shared_ptr > d0 = - fims::Information::GetInstance(); - if (this->median.estimated) { - if (this->median.is_random_effect) { +//d0 is a shared pointer that points to fims_info::Information +std::shared_ptr > d0 = + fims_info::Information::GetInstance(); + if (this->inflection_point.estimated) { + if (this->inflection_point.is_random_effect) { /* - if median is estimated and a random effect, - the median value from LogisticMaturityInterface (lm0->median) + if inflection_point is estimated and a random effect, + the inflection_point value from LogisticMaturityInterface (lm0->inflection_point) is passed to the Information member function RegisterRandomEffect */ - d0->RegisterRandomEffect(lm0->median); + d0->RegisterRandomEffect(lm0->inflection_point); } else { /* - if median is estimated and not a random effect, - the median value from LogisticMaturityInterface (lm0->median) + if inflection_point is estimated and not a random effect, + the inflection_point value from LogisticMaturityInterface (lm0->inflection_point) is passed to the Information member function RegisterParameter */ - d0->RegisterParameter(lm0->median); + d0->RegisterParameter(lm0->inflection_point); } } ``` -Linking to the `fims::LogisticMaturity` class allows us to link the values input from R with the values used in model calculation: +Linking to the `fims_popdy::LogisticMaturity` class allows us to link the values input from R with the values used in model calculation: ```{Rcpp, class.source = "rcppchunk", eval = FALSE} //file: rcpp_maturity.hpp -//lm0 is a shared pointer that points to fims::LogisticMaturity -std::shared_ptr > lm0 = - std::make_shared >(); +//lm0 is a shared pointer that points to fims_popdy::LogisticMaturity +std::shared_ptr > lm0 = + std::make_shared >(); /* - the median value from LogisticMaturity (lm0->median) - equals the median value from LogisticMaturityInterface (this->median.value) + the inflection_point value from LogisticMaturity (lm0->inflection_point) + equals the inflection_point value from LogisticMaturityInterface (this->inflection_point.value) */ -lm0->median = this->median.value; +lm0->inflection_point = this->inflection_point.value; ``` -We can also link these two pointers together so that the **fims::Information** class links up with the **fims::LogisticMaturity**, but more details on this later. +We can also link these two pointers together so that the **fims_info::Information** class links up with the **fims_popdy::LogisticMaturity**, but more details on this later. ```{Rcpp, class.source = "rcppchunk", eval = FALSE} //file: rcpp_maturity.hpp /* the maturity_models pointer from Information that matches the - id of the fims::LogisticMaturity class is equal to the pointer - to fims::LogisticMaturity + id of the fims_popdy::LogisticMaturity class is equal to the pointer + to fims_popdy::LogisticMaturity */ d0->maturity_models[lm0->id] = lm0; ``` @@ -284,25 +284,25 @@ knitr::include_graphics("figures/fims-path-maturity-2.png") ``` -## Inside the `fims::LogisticMaturity` class +## Inside the `fims_popdy::LogisticMaturity` class -The `LogisticMaturity` class in the `fims` namespace defined in [logistic.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/population_dynamics/maturity/functors/logistic.hpp) in the directory [inst/include/population_dynamics/maturity/functors](https://github.com/NOAA-FIMS/FIMS/tree/main/inst/include/population_dynamics/maturity/functors) has an `evaluate` method (i.e., function) that takes an input, *x* and returns the output from a logistic function (defined in [fims_math.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/common/fims_math.hpp) in the directory [inst/include/common](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/common)) using the class member `median` and `slope` values. +The `LogisticMaturity` class in the `fims` namespace defined in [logistic.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/population_dynamics/maturity/functors/logistic.hpp) in the directory [inst/include/population_dynamics/maturity/functors](https://github.com/NOAA-FIMS/FIMS/tree/main/inst/include/population_dynamics/maturity/functors) has an `evaluate` method (i.e., function) that takes an input, *x* and returns the output from a logistic function (defined in [fims_math.hpp](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/common/fims_math.hpp) in the directory [inst/include/common](https://github.com/NOAA-FIMS/FIMS/blob/main/inst/include/common)) using the class member `inflection_point` and `slope` values. ```{Rcpp, class.source = "fimschunk", eval = FALSE} -namespace fims { +namespace fims_popdy { /** * @brief LogisticMaturity class that returns the logistic function value * from fims_math. */ -template -struct LogisticMaturity : public MaturityBase { - T median; /*!< 50% quantile of the value of the quantity of interest (x); e.g., +template +struct LogisticMaturity : public MaturityBase { + Type inflection_point; /*!< 50% quantile of the value of the quantity of interest (x); e.g., age at which 50% of the fish are mature */ - T slope; /*!() {} + LogisticMaturity() : MaturityBase() {} /** * @brief Method of the logistic maturity class that implements the @@ -311,8 +311,8 @@ struct LogisticMaturity : public MaturityBase { * @param x The independent variable in the logistic function (e.g., age or * size at maturity). */ - virtual const T evaluate(const T& x) { - return fims::logistic(median, slope, x); + virtual const Type evaluate(const Type& x) { + return fims_math::logistic(inflection_point, slope, x); } }; @@ -333,7 +333,7 @@ To evaluate maturity within the `Population` class, we first need to set up a sh //file: inst/include/population_dynamics/population/population.hpp // maturity is a shared pointer to MaturityBase int maturity_id = -999; /*!< id of maturity model object*/ -std::shared_ptr> +std::shared_ptr> maturity; /*!< shared pointer to maturity module */ ``` @@ -346,15 +346,15 @@ Notice above that population by default declares a pointer of type `MaturityBase ```{Rcpp, class.source = "fimschunk", eval = FALSE} //file: inst/include/population_dynamics/maturity/functors/maturity_base.hpp -namespace fims { +namespace fims_popdy { /** @brief Base class for all maturity functors. * - * @tparam T The type of the maturity functor. + * @tparam TypeThe type of the maturity functor. */ -template -struct MaturityBase : public FIMSObject { +template +struct MaturityBase : public fims_model_object::FIMSObject { // id_g is the ID of the instance of the MaturityBase class. // this is like a memory tracker. // Assigning each one its own ID is a way to keep track of @@ -373,7 +373,7 @@ struct MaturityBase : public FIMSObject { * @param x The independent variable in the maturity function (e.g., logistic * maturity at age or size). */ - virtual const T evaluate(const T& x) = 0; + virtual const Type evaluate(const Type& x) = 0; }; ``` @@ -415,16 +415,16 @@ This *information* is managed in FIMS through the `Information` class in [inform The first component of the map is a uint32_t which will be used to hold the ID. The second component of the map is the shared pointer, - maturity_models, that points to fims::MaturityBase + maturity_models, that points to fims_popdy::MaturityBase */ -std::map > > +std::map > > maturity_models; /*! > >::iterator + std::shared_ptr > >::iterator maturity_models_iterator; ``` @@ -440,7 +440,7 @@ Here, we are setting the maturity_models pointer in `Information` to equal the ` /* the maturity_models pointer from Information that matches the id of the - fims::LogisticMaturity class is equal to the pointer to fims::LogisticMaturity + fims_popdy::LogisticMaturity class is equal to the pointer to fims_popdy::LogisticMaturity */ d0->maturity_models[lm0->id] = lm0; ``` @@ -452,11 +452,11 @@ knitr::include_graphics("figures/fims-path-maturity-6.png") Now we need to pass this pointer to the maturity pointer in population so that `population->maturity` points to the `LogisticMaturity` module instead of `MaturityBase`. First we set up a map in `Information` that points to `Population`. ```{Rcpp, class.source = "infochunk", eval = FALSE} //file: inst/include/common/information.hpp -std::map > > +std::map > > populations; /*! > >::iterator + std::shared_ptr > >::iterator population_iterator; /**< iterator for population objects>*/ ``` @@ -471,7 +471,7 @@ We then loop through the populations and create a new shared pointer, `p`, to re //file: inst/include/common/information.hpp for (population_iterator it = this->populations.begin(); it != this->populations.end(); ++it) { - std::shared_ptr > p = (*it).second; + std::shared_ptr > p = (*it).second; ... ``` In this code chunk, `(*it)` refers to a single population in the `populations` map. The second element of the map is the pointer, so `p = (*it).second` means we are setting the pointer to a single population to equal the pointer to `populations` in the map. @@ -528,8 +528,8 @@ population_modules <- list( ) ) maturity_modules <- list( - "Logistic Maturity" = list(id = 1, median = 10, slope = 0.2), - "Some Other Maturity" = list(id = 2, median = 8, slope = 0.3) + "Logistic Maturity" = list(id = 1, inflection_point = 10, slope = 0.2), + "Some Other Maturity" = list(id = 2, inflection_point = 8, slope = 0.3) ) ``` @@ -538,10 +538,10 @@ We can think of population_modules and maturity_modules above as instantiated C+ ```{r, eval = FALSE} # Fims <- Rcpp::Module("fims", PACKAGE = "FIMS") # maturity1 <- new(Fims$LogisticMaturity) -# maturity1$median$value <- 10 +# maturity1$inflection_point$value <- 10 # maturity1$slope$value <- 0.2 # maturity2 <- new(Fims$LogisticMaturity) -# maturity2$median$value <- 8 +# maturity2$inflection_point$value <- 8 # maturity2$slope$value <- 0.3 # population1 <- new(Fims$Population) # population2 <- new(Fims$Population) @@ -572,8 +572,8 @@ information <- list( In FIMS, the Information class has the objects, population and maturity, which are C++ maps. The first element of the map is the ID, the second element of the map is the pointer to the Population or Maturity class: ```{r, eval = FALSE} -# std::map > > maturity_models; -# std::map > > populations; +# std::map > > maturity_models; +# std::map > > populations; ``` We can then link up the pointers in information to each respective module: @@ -604,7 +604,7 @@ for (it in 1:length(population_modules)) { # it != this->populations.end(); ++it) { p <- information$populations[[it]][2] - # std::shared_ptr > p = (*it).second; + # std::shared_ptr > p = (*it).second; maturity_uint <- p$pointer$maturity_id # uint32_t maturity_uint = static_cast(p->maturity_id);