Skip to content

Commit

Permalink
bug: fix compile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Havron-NOAA committed Sep 8, 2023
1 parent 09b4b29 commit 2dd4e6c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 63 deletions.
11 changes: 6 additions & 5 deletions inst/include/common/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
#include "def.hpp"

#include "../pop_dy/von_bertalanffy.hpp"
#include "../common/data.hpp"

template<typename Type>
class Model{
public:

std::vector<Type> data; //TODO: make sure data not empty
std::vector<Type> ages;
std::vector<Type> predicted;
std::shared_ptr< VonBertalanffy<Type> > vb;
std::shared_ptr< ObsData<Type> > obsdata;

std::vector<Type*> parameters;

Model(){
this->vb = std::make_shared<VonBertalanffy<Type> >();
this->obsdata = std::make_shared<ObsData<Type> >();
}


Expand All @@ -39,10 +40,10 @@ class Model{
*/
Type evaluate(){
Type norm2 = 0.0;
for(int i =0; i < ages.size(); i++){
Type pred = vb -> evaluate(ages[i]);
for(int i =0; i < obsdata -> ages.size(); i++){
Type pred = vb -> evaluate(obsdata -> ages[i]);
this->predicted[i] = pred;
norm2+=(pred-data[i])*(pred-data[i]);
norm2+=(pred-obsdata -> data[i])*(pred-obsdata -> data[i]);
}
return norm2;
}
Expand Down
15 changes: 13 additions & 2 deletions inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

#include "rcpp_objects/rcpp_growth.hpp"
#include "rcpp_objects/rcpp_data.hpp"

bool CreateModel(){
for (size_t i = 0; i < RcppInterfaceBase::interface_objects.size();
i++) {
RcppInterfaceBase::interface_objects[i]->prepare();
}
}

/**
* Exposes the Variable and vonBertalanffyInterface classes to R.
*/
RCPP_EXPOSED_CLASS(Variable)
RCPP_EXPOSED_CLASS(vonBertalanffyInterface)
RCPP_EXPOSED_CLASS(ObsDataInterface)

/**
* Returns the initial values for the parameter set
Expand Down Expand Up @@ -43,10 +52,11 @@ RCPP_MODULE(growth) {
.field("estimable",&Variable::estimable);
Rcpp::class_<ObsDataInterface>("ObsData")
.constructor()
.method("prepare", &ObsDataInterface::prepare);
.method("finalize", &ObsDataInterface::finalize)
.field("Data", &ObsDataInterface::data)
.field("ages", &ObsDataInterface::ages);
Rcpp::class_<vonBertalanffyInterface>("vonBertalanffy")
.constructor()
.method("prepare", &vonBertalanffyInterface::prepare)
.method("finalize", &vonBertalanffyInterface::finalize)
.field("k", &vonBertalanffyInterface::k)
.field("l_inf", &vonBertalanffyInterface::l_inf)
Expand All @@ -55,6 +65,7 @@ RCPP_MODULE(growth) {
.field("beta", &vonBertalanffyInterface::beta);
Rcpp::function("get_parameter_vector", get_parameter_vector);
Rcpp::function("clear", clear);
Rcpp::function("CreateModel", CreateModel);
};

#endif
68 changes: 61 additions & 7 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class ObsDataInterface : public RcppInterfaceBase {
public:
Rcpp::NumericVector data;
Rcpp::NumericVector ages;
Rcpp::NumericVector predicted;

virtual bool prepare(){

if(this->data.size() != this->ages.size()){
Rcpp::stop("ages vector length not equal to data vector length");
}

ObsData<double>* obsdata;
template<typename Type>
bool prepare_local() {

std::shared_ptr<Model<Type> > model = Model<Type>::getInstance();
std::shared_ptr< ObsData<Type> > obsdata;
obsdata = std::make_shared<ObsData<Type> >();

obsdata->ages.resize(this->ages.size());
obsdata->data.resize(this->data.size());
Expand All @@ -24,7 +24,61 @@ class ObsDataInterface : public RcppInterfaceBase {
obsdata->ages[i] = this->ages[i];
obsdata->data[i] = this->data[i];
}
model->predicted.resize(this->data.size());

return true;
}

/**
* Prepares the model to work with TMB.
*/
virtual bool prepare() {


if (this->data.size() != this->ages.size()) {
Rcpp::stop("ages vector length not equal to data vector length");
}

this->predicted = Rcpp::NumericVector(this->ages.size());

#ifdef TMB_MODEL

this->prepare_local<TMB_FIMS_REAL_TYPE>();
this->prepare_local<TMB_FIMS_FIRST_ORDER>();
this->prepare_local<TMB_FIMS_SECOND_ORDER>();
this->prepare_local<TMB_FIMS_THIRD_ORDER>();

#endif

return true;
}

/**
* Update the model parameter values and finalize. Sets the parameter values and evaluates the
* portable model once and transfers values back to the Rcpp interface.
*/
void finalize(Rcpp::NumericVector v) {
std::shared_ptr< Model<double> > model = Model<double>::getInstance();

if (this->data.size() != this->ages.size()) {
Rcpp::stop("finalize: ages vector length not equal to data vector length");
}


for (int i = 0; i < this->predicted.size(); i++) {
this->predicted[i] = model->predicted[i];
}
}

/**
* Print model values.
*/
void show_() {
Rcpp::Rcout << std::setw(15) << "observed " << std::setw(15) << "predicted\n";
//Rcpp::Rcout << "Predicted size: " << this->predicted.size() << std::endl;
for (int i = 0; i < this->predicted.size(); i++) {
Rcpp::Rcout << std::left << std::setw(15) << this->data[i] << std::setw(15) << this->predicted[i] << "\n";
}
}

};
Expand Down
66 changes: 20 additions & 46 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,65 @@ class vonBertalanffyInterface {


template<typename Type>
void prepare_local() {
bool prepare_local() {

std::shared_ptr<Model<Type> > model_1 = Model<Type>::getInstance();
std::shared_ptr<Model<Type> > model = Model<Type>::getInstance();
std::shared_ptr< VonBertalanffy<Type> > vb;
vb = std::make_shared<VonBertalanffy<Type> >();

// model_1->clear();
model_1->predicted.resize(this->data.size());
model_1->ages.resize(this->ages.size());
model_1->data.resize(this->data.size());
for (int i = 0; i < this->data.size(); i++) {
model_1->ages[i] = this->ages[i];
model_1->data[i] = this->data[i];
}

//initialize k
model_1->vb->k = this->k.value;

vb->k = this->k.value;

//initialize l_inf
model_1->vb->l_inf = this->l_inf.value;

vb->l_inf = this->l_inf.value;

//initialize a_min
model_1->vb->a_min = this->a_min.value;

vb->a_min = this->a_min.value;

//initialize alpha
model_1->vb->alpha = this->alpha.value;

vb->alpha = this->alpha.value;

//initialize beta
model_1->vb->beta = this->beta.value;
vb->beta = this->beta.value;


if (this->k.estimable) {
model_1->parameters.push_back(&(model_1->vb)->k);
model->parameters.push_back(&(vb)->k);
}

if (this->l_inf.estimable) {
model_1->parameters.push_back(&(model_1->vb)->l_inf);
model->parameters.push_back(&(vb)->l_inf);
}

if (this->a_min.estimable) {
model_1->parameters.push_back(&(model_1->vb)->a_min);
model->parameters.push_back(&(vb)->a_min);
}

if (this->alpha.estimable) {
model_1->parameters.push_back(&(model_1->vb)->alpha);
model->parameters.push_back(&(vb)->alpha);
}

if (this->beta.estimable) {
model_1->parameters.push_back(&(model_1->vb)->beta);
model->parameters.push_back(&(vb)->beta);
}
return true;

}

/**
* Prepares the model to work with TMB.
*/
void prepare() {


if (this->data.size() != this->ages.size()) {
Rcpp::stop("ages vector length not equal to data vector length");
}

this->predicted = Rcpp::NumericVector(this->ages.size());
virtual bool prepare() {

#ifdef TMB_MODEL

this->prepare_local<TMB_FIMS_REAL_TYPE>();
this->prepare_local<TMB_FIMS_FIRST_ORDER>();
this->prepare_local<TMB_FIMS_SECOND_ORDER>();
this->prepare_local<TMB_FIMS_THIRD_ORDER>();

#endif
return true;

}

Expand All @@ -100,11 +85,9 @@ class vonBertalanffyInterface {
*/
void finalize(Rcpp::NumericVector v) {
std::shared_ptr< Model<double> > model = Model<double>::getInstance();
std::shared_ptr<VonBertalanffy<double> > vb = model->vb;
std::shared_ptr< VonBertalanffy<double> > vb;
vb = std::make_shared<VonBertalanffy<double> >();

if (this->data.size() != this->ages.size()) {
Rcpp::stop("finalize: ages vector length not equal to data vector length");
}

for (int i = 0; i < v.size(); i++) {
(*model->parameters[i]) = v[i];
Expand All @@ -117,9 +100,6 @@ class vonBertalanffyInterface {
this->l_inf.value = vb->l_inf;


for (int i = 0; i < this->predicted.size(); i++) {
this->predicted[i] = model->predicted[i];
}
}

/**
Expand All @@ -130,12 +110,6 @@ class vonBertalanffyInterface {
Rcpp::Rcout << "k = " << this->k.value << "\n";
Rcpp::Rcout << "a_min = " << this->a_min.value << "\n";
Rcpp::Rcout << "l_inf = " << this->l_inf.value << "\n";
Rcpp::Rcout << std::setw(15) << "observed " << std::setw(15) << "predicted\n";
//Rcpp::Rcout << "Predicted size: " << this->predicted.size() << std::endl;
for (int i = 0; i < this->predicted.size(); i++) {
Rcpp::Rcout << std::left << std::setw(15) << this->data[i] << std::setw(15) << this->predicted[i] << "\n";
}

}

};
Expand Down
7 changes: 4 additions & 3 deletions test/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ vonB$l_inf$value<-7
vonB$l_inf$estimable<-TRUE

#set data
vonB$data <-data
Dat <- new(g$ObsData)
Dat$Data <- data

#set ages
vonB$ages<-ages
Dat$ages<-ages

#prepare for interfacing with TMB
vonB$prepare()
g$CreateModel()

#create an empty data list (data set above)
data <- list()
Expand Down

0 comments on commit 2dd4e6c

Please sign in to comment.