Skip to content

Commit

Permalink
document C++ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Aug 28, 2024
1 parent 14d0492 commit 034cc2e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
52 changes: 31 additions & 21 deletions inst/include/common/information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ class Information {
density_components_iterator;
/**< iterator for distribution objects>*/

std::unordered_map<uint32_t, fims::Vector<Type>* >
variable_map; /**<hash map to link a parameter, derived value, or observation
std::unordered_map<uint32_t, fims::Vector<Type>* >
variable_map; /**<hash map to link a parameter, derived value, or observation
to its shared location in memory */
typedef typename std::unordered_map<uint32_t, fims::Vector<Type>* >::iterator
typedef typename std::unordered_map<uint32_t, fims::Vector<Type>* >::iterator
variable_map_iterator; /**< iterator for variable map>*/

Information() {}
Expand Down Expand Up @@ -166,66 +166,76 @@ class Information {
this->parameter_names.push_back(p_name);
}


/**
* Loop over distributions and set links to distribution x value if distribution is a prior type.
*/
void setup_priors(){
for(density_components_iterator it = density_components.begin(); it!= density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "prior"){
INFO_LOG << "Setup prior for distribution " << d->id << std::endl;
variable_map_iterator vmit;
INFO_LOG << "Link prior from distribution " << d->id << " to parameter " << d->key[0] << std::endl;
vmit = this->variable_map.find(d->key[0]);
vmit = this->variable_map.find(d->key[0]);
d->x = *(*vmit).second;
for(size_t i=1; i<d->key.size(); i++){
INFO_LOG << "Link prior from distribution " << d->id << " to parameter " << d->key[i] << std::endl;
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
INFO_LOG << "Prior size for distribution " << d->id << "is: " << d->x.size() << std::endl;
}
}
}

/**
* Loop over distributions and set links to distribution x value if distribution is a random effects type.
*/
void setup_random_effects(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "random_effects"){
INFO_LOG << "Setup random effects for distribution " << d->id << std::endl;
variable_map_iterator vmit;
INFO_LOG << "Link random effects from distribution " << d->id << " to derived value " << d->key[0] << std::endl;
vmit = this->variable_map.find(d->key[0]);
vmit = this->variable_map.find(d->key[0]);
d->x = *(*vmit).second;
for(size_t i=1; i<d->key.size(); i++){
INFO_LOG << "Link random effects from distribution " << d->id << " to derived value " << d->key[i] << std::endl;
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
vmit = this->variable_map.find(d->key[i]);
d->x.insert(std::end(d->x),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
INFO_LOG << "Random effect size for distribution " << d->id << "is: " << d->x.size() << std::endl;
}
}
}

/**
* Loop over distributions and set links to distribution expected value if distribution is a data type.
*/
void setup_data(){
for(density_components_iterator it = this->density_components.begin(); it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
if(d->input_type == "data"){
INFO_LOG << "Setup expected value for data distribution " << d->id << std::endl;
variable_map_iterator vmit;
INFO_LOG << "Link expected value from distribution " << d->id << " to derived value " << d->key[0] << std::endl;
vmit = this->variable_map.find(d->key[0]);
vmit = this->variable_map.find(d->key[0]);
d->expected_values = *(*vmit).second;

for(size_t i=1; i<d->key.size(); i++){
vmit = this->variable_map.find(d->key[i]);
INFO_LOG << "Link expected value from distribution " << d->id << " to derived value " << d->key[i] << std::endl;
d->expected_values.insert(std::end(d->expected_values),
INFO_LOG << "Link expected value from distribution " << d->id << " to derived value " << d->key[i] << std::endl;
d->expected_values.insert(std::end(d->expected_values),
std::begin(*(*vmit).second), std::end(*(*vmit).second));
}
}
INFO_LOG << "Expected value size for distribution " << d->id << "is: " << d->expected_values.size() << std::endl;
}
}
}



/**
Expand Down Expand Up @@ -289,7 +299,7 @@ class Information {
}
INFO_LOG << "Expecting to import " << this->data_objects.size()
<< " data objects." << std::endl;
for(density_components_iterator it = this->density_components.begin();
for(density_components_iterator it = this->density_components.begin();
it!= this->density_components.end(); ++it){
std::shared_ptr<fims_distributions::DensityComponentBase<Type> > d = (*it).second;
INFO_LOG << "Checking for available density components data objects."
Expand Down Expand Up @@ -320,7 +330,7 @@ class Information {
}
// end set data
}

INFO_LOG << "Initializing population objects for "
<< this->populations.size() << " populations." << std::endl;
for (population_iterator it = this->populations.begin();
Expand Down Expand Up @@ -446,7 +456,7 @@ class Information {
//setup priors, random effect, and data density components
INFO_LOG << "Setup priors." << std::endl;
setup_priors();

INFO_LOG << "Completed FIMS model creation." << std::endl;
return valid_model;
}
Expand Down
18 changes: 12 additions & 6 deletions inst/include/population_dynamics/fleet/fleet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
fims::Vector<Type> age_composition; /*!<derived quantity age composition*/

// derived quantities
fims::Vector<Type> observed_catch_lpdf; /*!<observed total catch linked
fims::Vector<Type> observed_catch_lpdf; /*!<observed total catch linked
to log probability density function*/
fims::Vector<Type> observed_index_lpdf; /*!<observed index of abundance linked
fims::Vector<Type> observed_index_lpdf; /*!<observed index of abundance linked
to log probability density function*/
fims::Vector<Type> expected_catch; /*!<model expected total catch*/
fims::Vector<Type> expected_index; /*!<model expected index of abundance*/
fims::Vector<Type> log_expected_index; /*!<model expected index of abundance*/
fims::Vector<Type> expected_catch_lpdf; /*!<model expected total catch linked
fims::Vector<Type> expected_catch_lpdf; /*!<model expected total catch linked
to log probability density function*/
fims::Vector<Type> expected_index_lpdf; /*!<model expected index of abundance linked
fims::Vector<Type> expected_index_lpdf; /*!<model expected index of abundance linked
to log probability density function*/
fims::Vector<Type> catch_numbers_at_age; /*!<model expected catch at age*/
fims::Vector<Type> proportion_catch_numbers_at_age; /*!<model expected catch at age*/
Expand Down Expand Up @@ -133,6 +133,9 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
}
}

/**
* Evaluate the proportion of catch numbers at age.
*/
void evaluate_age_comp() {
for (size_t y = 0; y < this->nyears; y++) {
Type sum = 0.0;
Expand All @@ -143,11 +146,14 @@ struct Fleet : public fims_model_object::FIMSObject<Type> {
for (size_t a = 0; a < this->nages; a++) {
size_t i_age_year = y * this->nages + a;
this->proportion_catch_numbers_at_age[i_age_year] = this->catch_numbers_at_age[i_age_year] / sum;

}
}
}
}

/**
* Evaluate the log of the expected index.
*/
void evaluate_index() {
for(size_t i=0; i<this->expected_index.size(); i++){
log_expected_index[i] = log(this->expected_index[i]);
Expand Down

0 comments on commit 034cc2e

Please sign in to comment.