Dynare++: finish to apply Dynare C++ coding style and extensions (.cc/.hh)

parent b58e3d9f
RINTERNALS=/usr/share/R/include/ RINTERNALS=/usr/share/R/include/
sylvcppsource := $(wildcard ../../sylv/cc/*.cpp) sylvcppsource := $(wildcard ../../sylv/cc/*.cc)
sylvhsource := $(wildcard ../../sylv/cc/*.h) sylvhsource := $(wildcard ../../sylv/cc/*.hh)
sylvobjects := $(patsubst %.cpp, %.o, $(sylvcppsource)) sylvobjects := $(patsubst %.cc, %.o, $(sylvcppsource))
tlcwebsource := $(wildcard ../../tl/cc/*.cweb) tlcppsource := $(wildcard ../../tl/cc/*.cc)
tlcppsource := $(patsubst %.cweb,%.cpp,$(tlcwebsource)) tlhsource := $(wildcard ../../tl/cc/*.hh)
tlhwebsource := $(wildcard ../../tl/cc/*.hweb)
tlhsource := $(patsubst %.hweb,%.h,$(tlhwebsource)) kordcppsource := $(wildcard ../../kord/*.cc)
tlobjects := $(patsubst %.cweb,%.o,$(tlcwebsource)) kordhsource := $(wildcard ../../kord/*.hh)
kordcwebsource := $(wildcard ../../kord/*.cweb) integcppsource := $(wildcard ../../integ/cc/*.cc)
kordcppsource := $(patsubst %.cweb,%.cpp,$(kordcwebsource)) integhsource := $(wildcard ../../integ/cc/*.hh)
kordhwebsource := $(wildcard ../../kord/*.hweb)
kordhsource := $(patsubst %.hweb,%.h,$(kordhwebsource)) parserhsource := $(wildcard ../../parser/cc/*.hh)
kordobjects := $(patsubst %.cweb,%.o,$(kordcwebsource)) parsercppsource := $(wildcard ../parser/cc/*.cc)
integcwebsource := $(wildcard ../../integ/cc/*.cweb) utilshsource := $(wildcard ../../utils/cc/*.hh)
integcppsource := $(patsubst %.cweb,%.cpp,$(integcwebsource)) utilscppsource := $(wildcard ../utils/cc/*.cc)
integhwebsource := $(wildcard ../../integ/cc/*.hweb)
integhsource := $(patsubst %.hweb,%.h,$(integhwebsource)) srccpp := dynare3.cc dynare_model.cc planner_builder.cc dynare_atoms.cc dynare_params.cc nlsolve.cc
integobjects := $(patsubst %.cweb,%.o,$(integcwebsource)) objects := $(patsubst %.cc,../../src/%.o,$(srccpp)) \
$(patsubst %.yy,%_ll.o,$(wildcard ../../src/*.yy)) \
parserhsource := $(wildcard ../../parser/cc/*.h) $(patsubst %.ll,%_tab.o,$(wildcard ../../src/*.ll))
parsercppsource := $(wildcard ../parser/cc/*.cpp)
utilshsource := $(wildcard ../../utils/cc/*.h)
utilscppsource := $(wildcard ../utils/cc/*.cpp)
srccpp := dynare3.cpp dynare_model.cpp planner_builder.cpp dynare_atoms.cpp dynare_params.cpp nlsolve.cpp
objects := $(patsubst %.cpp,../../src/%.o,$(srccpp)) \
$(patsubst %.y,%_ll.o,$(wildcard ../../src/*.y)) \
$(patsubst %.lex,%_tab.o,$(wildcard ../../src/*.lex))
PKG_CPPFLAGS= -I../../tl/cc -I../../sylv/cc -I../../kord -I../../src -I../.. -I$(RINTERNALS) PKG_CPPFLAGS= -I../../tl/cc -I../../sylv/cc -I../../kord -I../../src -I../.. -I$(RINTERNALS)
PKG_LIBS= ${LAPACK_LIBS} ${BLAS_LIBS} $(objects) $(kordobjects) $(integobjects) $(tlobjects) ../../parser/cc/parser.a ../../utils/cc/utils.a $(sylvobjects) -lpthread -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ PKG_LIBS= ${LAPACK_LIBS} ${BLAS_LIBS} $(objects) $(kordobjects) $(integobjects) $(tlobjects) ../../parser/cc/parser.a ../../utils/cc/utils.a $(sylvobjects) -lpthread -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++
...@@ -43,12 +34,12 @@ endif ...@@ -43,12 +34,12 @@ endif
dynareR.so: dynareR.o dynareR.so: dynareR.o
g++ -shared -o dynareR.so dynareR.o -L/usr/lib/R/lib -lR $(PKG_LIBS) g++ -shared -o dynareR.so dynareR.o -L/usr/lib/R/lib -lR $(PKG_LIBS)
dynareR.o: dynareR.cpp dynareR.o: dynareR.cc
g++ -I/usr/share/R/include -I/usr/share/R/include $(PKG_CPPFLAGS) \ g++ -I/usr/share/R/include -I/usr/share/R/include $(PKG_CPPFLAGS) \
-fpic -g -O2 -c dynareR.cpp -o dynareR.o -DDEBUG -fpic -g -O2 -c dynareR.cc -o dynareR.o -DDEBUG
test: test.cpp dynareR.cpp test: test.cc dynareR.cc
g++ -O0 -g -o test test.cpp -DDEBUG $(PKG_LIBS) $(PKG_CPPFLAGS) g++ -O0 -g -o test test.cc -DDEBUG $(PKG_LIBS) $(PKG_CPPFLAGS)
test-debug: test-debug:
valgrind --leak-check=yes ./test valgrind --leak-check=yes ./test
...@@ -2,23 +2,23 @@ ...@@ -2,23 +2,23 @@
// Copyright 2006, Tamas K Papp // Copyright 2006, Tamas K Papp
#include "dynare3.h" // Dynare class #include "dynare3.hh" // Dynare class
#include "approximation.h" // Approximation class #include "approximation.hh" // Approximation class
// exceptions // exceptions
#include "dynare_exception.h" #include "dynare_exception.hh"
#include "parser/cc/parser_exception.h" #include "parser/cc/parser_exception.hh"
#include "utils/cc/exception.h" #include "utils/cc/exception.hh"
#include "SylvException.h" #include "SylvException.hh"
#include "tl_exception.h" #include "tl_exception.hh"
#include "kord_exception.h" #include "kord_exception.hh"
#include <algorithm> #include <algorithm>
#include <string.h> #include <string.h>
#ifdef DEBUG #ifdef DEBUG
#include <stdio.h> # include <stdio.h>
#endif #endif
#include <R_ext/Memory.h> #include <R_ext/Memory.h>
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
* *
* It is not recommended that you use this interface for anything else * It is not recommended that you use this interface for anything else
* but R. * but R.
*/ */
/** Error codes: these error codes correspond to possible /** Error codes: these error codes correspond to possible
* exceptions. */ * exceptions. */
...@@ -45,10 +45,12 @@ ...@@ -45,10 +45,12 @@
* managed by R, ie it will be garbage collected after the .C call * managed by R, ie it will be garbage collected after the .C call
* returns and the contents are duplicated. * returns and the contents are duplicated.
*/ */
char *passmessage(const char *errormessage) { char *
long l = strlen(errormessage); passmessage(const char *errormessage)
char *em = R_alloc(l, 1); {
return strcpy(em, errormessage); long l = strlen(errormessage);
char *em = R_alloc(l, 1);
return strcpy(em, errormessage);
} }
/** This function puts the mapping between the newtotal items after /** This function puts the mapping between the newtotal items after
...@@ -56,26 +58,30 @@ char *passmessage(const char *errormessage) { ...@@ -56,26 +58,30 @@ char *passmessage(const char *errormessage) {
* be at least as long as newtotal. The function uses R indexing, * be at least as long as newtotal. The function uses R indexing,
* that is to say, the first index is 1. * that is to say, the first index is 1.
*/ */
int matchnames(const char **orig, int origtotal, int
const NameList &nl, int offset, int newtotal, matchnames(const char **orig, int origtotal,
int *perm) { const NameList &nl, int offset, int newtotal,
int *perm)
{
#ifdef DEBUG #ifdef DEBUG
printf("matching names (R indexing):\n"); printf("matching names (R indexing):\n");
#endif #endif
for (int i=0; i < newtotal; i++) { for (int i = 0; i < newtotal; i++)
int j; {
for (j=0; j < origtotal; j++) int j;
if (strcmp(nl.getName(offset+i), *(orig+j))==0) { for (j = 0; j < origtotal; j++)
*(perm+i) = j+1; if (strcmp(nl.getName(offset+i), *(orig+j)) == 0)
{
*(perm+i) = j+1;
#ifdef DEBUG #ifdef DEBUG
printf("%d -> %d\n",i+1,j+1); printf("%d -> %d\n", i+1, j+1);
#endif #endif
break; break;
} }
if (j==origtotal) if (j == origtotal)
return 1; return 1;
} }
return 0; return 0;
} }
/** dynareR is the interface function. The user provides: /** dynareR is the interface function. The user provides:
...@@ -107,143 +113,159 @@ int matchnames(const char **orig, int origtotal, ...@@ -107,143 +113,159 @@ int matchnames(const char **orig, int origtotal,
* collected after duplication. * collected after duplication.
*/ */
extern "C" { extern "C" {
void dynareR(const char** endo, const int* num_endo, void
const char** exo, const int* num_exo, dynareR(const char **endo, const int *num_endo,
const char** par, const int* num_par, const char **exo, const int *num_exo,
const char** equations, const int* ord, const char* jnlfile, const char **par, const int *num_par,
const double *parval, const double *vcov, const char **equations, const int *ord, const char *jnlfile,
const double *initval, const double *parval, const double *vcov,
const int *num_steps, const double *initval,
double* tensorbuffer, const int *num_steps,
int *num_state, int *ordering_state, double *tensorbuffer,
int *ordering_endo, int *ordering_exo, int *num_state, int *ordering_state,
double *newinitval, int *ordering_endo, int *ordering_exo,
int* error, char **errormessage, int *kordcode) { double *newinitval,
// construct the model here int *error, char **errormessage, int *kordcode)
try { {
#ifdef DEBUG // will print only first var names etc. // construct the model here
printf("eq: %s\nendo: %d %s\nexo: %d %s\npar: %d %s\nord: %d\n", try
*equations,*num_endo,*endo,*num_exo,*exo,*num_par,*par,*ord); {
#ifdef DEBUG // will print only first var names etc.
printf("eq: %s\nendo: %d %s\nexo: %d %s\npar: %d %s\nord: %d\n",
*equations, *num_endo, *endo, *num_exo, *exo, *num_par, *par, *ord);
#endif #endif
// create journal // create journal
Journal journal(jnlfile); Journal journal(jnlfile);
// create Dynare object // create Dynare object
Dynare dynare(endo, *num_endo, exo, *num_exo, Dynare dynare(endo, *num_endo, exo, *num_exo,
par, *num_par, *equations, strlen(*equations), par, *num_par, *equations, strlen(*equations),
*ord, journal); *ord, journal);
// set Vcov and parameter values // set Vcov and parameter values
copy(parval,parval+(*num_par),dynare.getParams().base()); copy(parval, parval+(*num_par), dynare.getParams().base());
#ifdef DEBUG #ifdef DEBUG
printf("parameter values (%d):\n",dynare.getParams().length()); printf("parameter values (%d):\n", dynare.getParams().length());
dynare.getParams().print(); dynare.getParams().print();
#endif #endif
copy(vcov,vcov+(*num_exo)*(*num_exo),dynare.getVcov().base()); copy(vcov, vcov+(*num_exo)*(*num_exo), dynare.getVcov().base());
#ifdef DEBUG #ifdef DEBUG
printf("vcov matrix:\n"); printf("vcov matrix:\n");
dynare.getVcov().print(); dynare.getVcov().print();
#endif #endif
// set initial values // set initial values
Vector iv(initval,*num_endo); Vector iv(initval, *num_endo);
#ifdef DEBUG #ifdef DEBUG
printf("initial values:\n"); printf("initial values:\n");
iv.print(); iv.print();
#endif #endif
dynare.setInitOuter(iv); dynare.setInitOuter(iv);
// construct approximation // construct approximation
tls.init(dynare.order(), tls.init(dynare.order(),
dynare.nstat()+2*dynare.npred()+3*dynare.nboth()+ dynare.nstat()+2*dynare.npred()+3*dynare.nboth()
2*dynare.nforw()+dynare.nexog()); +2*dynare.nforw()+dynare.nexog());
Approximation approximation(dynare,journal,*num_steps); Approximation approximation(dynare, journal, *num_steps);
approximation.walkStochSteady(); approximation.walkStochSteady();
// write the steady state into the buffer // write the steady state into the buffer
int ny = dynare.ny(); int ny = dynare.ny();
const Vector ss(dynare.getSteady()); const Vector ss(dynare.getSteady());
// ss = ConstVector(approximation.getSS(), 0); // FIXME allow // ss = ConstVector(approximation.getSS(), 0); // FIXME allow
// // for nonzero // // for nonzero
int s = dynare.getStateNames().getNum(); int s = dynare.getStateNames().getNum();
int sm = s; int sm = s;
tensorbuffer = copy(ss.base(),ss.base()+ny,tensorbuffer); tensorbuffer = copy(ss.base(), ss.base()+ny, tensorbuffer);
// write the tensors into buffer // write the tensors into buffer
const UnfoldDecisionRule& udr = const UnfoldDecisionRule &udr
approximation.getUnfoldDecisionRule(); = approximation.getUnfoldDecisionRule();
for (int i=1; i <= *ord; i++) { for (int i = 1; i <= *ord; i++)
const UFSTensor* t = udr.get(Symmetry(i)); {
const UFSTensor *t = udr.get(Symmetry(i));
#ifdef DEBUG #ifdef DEBUG
printf("tensor %d:\n", i); printf("tensor %d:\n", i);
t->print(); t->print();
#endif #endif
tensorbuffer = copy(t->base(), t->base()+ny*sm, tensorbuffer); tensorbuffer = copy(t->base(), t->base()+ny*sm, tensorbuffer);
sm *= s; sm *= s;
} }
// save number of endogenous states // save number of endogenous states
*num_state = s-(*num_exo); *num_state = s-(*num_exo);
// ordering // ordering
#ifdef DEBUG #ifdef DEBUG
printf("all endo names:\n"); printf("all endo names:\n");
dynare.getAllEndoNames().print(); dynare.getAllEndoNames().print();
printf("all state names:\n"); printf("all state names:\n");
dynare.getStateNames().print(); dynare.getStateNames().print();
#endif #endif
if (matchnames(endo, *num_endo, dynare.getAllEndoNames(), if (matchnames(endo, *num_endo, dynare.getAllEndoNames(),
0, *num_endo, ordering_endo) || 0, *num_endo, ordering_endo)
matchnames(endo, *num_endo, dynare.getStateNames(), || matchnames(endo, *num_endo, dynare.getStateNames(),
0, *num_state, ordering_state) || 0, *num_state, ordering_state)
matchnames(exo, *num_exo, dynare.getStateNames(), || matchnames(exo, *num_exo, dynare.getStateNames(),
*num_state, *num_exo, ordering_exo)) { *num_state, *num_exo, ordering_exo))
*error = DYNARER_NAMESMATCHINGERROR; {
*errormessage = "There was a problem when matching names. This is weird and should not happen."; *error = DYNARER_NAMESMATCHINGERROR;
return; *errormessage = "There was a problem when matching names. This is weird and should not happen.";
} return;
// return new init values (first column of SS matrix) }
ConstVector newinit((const GeneralMatrix&) approximation.getSS(), 0); // return new init values (first column of SS matrix)
ConstVector newinit((const GeneralMatrix &)approximation.getSS(), 0);
#ifdef DEBUG #ifdef DEBUG
printf("new initial values:\n"); printf("new initial values:\n");
newinit.print(); newinit.print();
#endif #endif
copy(newinit.base(),newinit.base()+(*num_endo),newinitval); copy(newinit.base(), newinit.base()+(*num_endo), newinitval);
} catch (const SylvException &e) { }
*error = DYNARER_SYLVEXCEPTION; catch (const SylvException &e)
char errorbuffer[501]; {
e.printMessage(errorbuffer, 500); *error = DYNARER_SYLVEXCEPTION;
*errormessage = passmessage(errorbuffer); char errorbuffer[501];
e.printMessage(errorbuffer, 500);
*errormessage = passmessage(errorbuffer);
#ifdef DEBUG #ifdef DEBUG
printf("Caught Sylv exception: "); printf("Caught Sylv exception: ");
e.printMessage(); e.printMessage();
#endif #endif
return; return;
} catch (const DynareException &e) { }
*error = DYNARER_DYNAREEXCEPTION; catch (const DynareException &e)
*errormessage = passmessage(e.message()); {
*error = DYNARER_DYNAREEXCEPTION;
*errormessage = passmessage(e.message());
#ifdef DEBUG #ifdef DEBUG
printf("Caught Dynare exception: %s\n", e.message()); printf("Caught Dynare exception: %s\n", e.message());
#endif #endif
return; return;
} catch (const ogu::Exception &e) { }
*error = DYNARER_OGUEXCEPTION; catch (const ogu::Exception &e)
*errormessage = passmessage(e.message()); {
*error = DYNARER_OGUEXCEPTION;
*errormessage = passmessage(e.message());
#ifdef DEBUG #ifdef DEBUG
printf("Caught ogu::Exception: "); printf("Caught ogu::Exception: ");
e.print(); e.print();
#endif #endif
return; return;
} catch (const TLException &e) { }
*error = DYNARER_TLEXCEPTION; catch (const TLException &e)
*errormessage = passmessage(e.getmessage()); {
*error = DYNARER_TLEXCEPTION;
*errormessage = passmessage(e.getmessage());
#ifdef DEBUG #ifdef DEBUG
printf("Caugth TL exception: "); printf("Caugth TL exception: ");
e.print(); e.print();
#endif #endif
return; return;
} catch (const KordException &e) { }
*error = DYNARER_KORDEXCEPTION; catch (const KordException &e)
*errormessage = passmessage(e.getmessage()); {
*kordcode = e.code(); // Kord error code *error = DYNARER_KORDEXCEPTION;
*errormessage = passmessage(e.getmessage());
*kordcode = e.code(); // Kord error code
#ifdef DEBUG #ifdef DEBUG
printf("Caugth Kord exception: "); printf("Caugth Kord exception: ");
e.print(); e.print();
#endif #endif
return; return;
} }
*error = 0; *error = 0;
return;} return;
}
} }
#include "dynareR.cc"
int
main(void)
{
const char *parameters[] = {"beta", "gamma", "rho", "alpha", "delta"};
const char *varendo[] = {"k", "c", "a"};
const char *varexo[] = {"eps"};
const int numpar = 5;
const int numendo = 3;
const int numexo = 1;
const int ord = 2;
const int numsteps = 0;
const double parval[] = {.99, 2, .9, .3, .025};
const double vcov[] = {0.001};
const double initval[] = {0.066, 0.43, 0.01};
int e;
double tensorbuffer[100];
int num_state;
int ordering_state[] = {0, 0, 0};
int ordering_endo[] = {0, 0, 0};
int ordering_exo[] = {0};
double newinitval[] = {0, 0, 0};
const char *modeleq[] = {"(c/c(1))^gamma*beta*(alpha*exp(a(1))*k^(alpha-1)+1-delta)=1; a=rho*a(-1)+eps; k+c=exp(a)*k(-1)^alpha+(1-delta)*k(-1);"};
dynareR(varendo, &numendo, varexo, &numexo, parameters, &numpar, modeleq,
&ord, "journal", parval, vcov, initval,
&numsteps, tensorbuffer,
&num_state, ordering_state, ordering_endo, ordering_exo,
newinitval, &e);
printf("error code: %d\n", e);
}
#include "dynareR.cpp"
int main(void) {
const char *parameters[] = {"beta","gamma","rho","alpha","delta"};
const char *varendo[] = {"k","c","a"};
const char *varexo[] = {"eps"};
const int numpar = 5;
const int numendo = 3;
const int numexo = 1;
const int ord = 2;
const int numsteps = 0;
const double parval[] = {.99,2,.9,.3,.025};
const double vcov[] = {0.001};
const double initval[] = {0.066, 0.43, 0.01};
int e;
double tensorbuffer[100];
int num_state;
int ordering_state[] = {0,0,0};
int ordering_endo[] = {0,0,0};
int ordering_exo[] = {0};
double newinitval[] = {0,0,0};
const char *modeleq[] = {"(c/c(1))^gamma*beta*(alpha*exp(a(1))*k^(alpha-1)+1-delta)=1; a=rho*a(-1)+eps; k+c=exp(a)*k(-1)^alpha+(1-delta)*k(-1);"};
dynareR(varendo, &numendo, varexo, &numexo, parameters, &numpar, modeleq,
&ord, "journal", parval, vcov, initval,
&numsteps, tensorbuffer,
&num_state, ordering_state, ordering_endo, ordering_exo,
newinitval,&e);
printf("error code: %d\n", e);
}
// Copyright (C) 2005-2011, Ondra Kamenik
// This is the mexFunction providing interface to
// DecisionRule<>::simulate(). It takes the following input
// parameters:
// order the order of approximation, needs order+1 derivatives
// nstat
// npred
// nboth
// nforw
// nexog
// ystart starting value (full vector of endogenous)
// shocks matrix of shocks (nexog x number of period)
// vcov covariance matrix of shocks (nexog x nexog)
// seed integer seed
// ysteady full vector of decision rule's steady
// ... order+1 matrices of derivatives
// output:
// res simulated results
#include "dynmex.h"
#include "mex.h"
#include "decision_rule.hh"
#include "fs_tensor.hh"
#include "SylvException.hh"
extern "C" {
void
mexFunction(int nlhs, mxArray *plhs[],
int nhrs, const mxArray *prhs[])
{
if (nhrs < 12 || nlhs != 2)
DYN_MEX_FUNC_ERR_MSG_TXT("dynare_simul_ must have at least 12 input parameters and exactly 2 output arguments.\n");
int order = (int) mxGetScalar(prhs[0]);
if (nhrs != 12 + order)
DYN_MEX_FUNC_ERR_MSG_TXT("dynare_simul_ must have exactly 11+order input parameters.\n");
int nstat = (int) mxGetScalar(prhs[1]);
int npred = (int) mxGetScalar(prhs[2]);
int nboth = (int) mxGetScalar(prhs[3]);
int nforw = (int) mxGetScalar(prhs[4]);
int nexog = (int) mxGetScalar(prhs[5]);