From 03549ebdb16e7ebd3be033f90d76c77472f5d4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien.villemot@ens.fr> Date: Mon, 20 Sep 2010 17:04:38 +0200 Subject: [PATCH] Preprocessor: implement STEADY_STATE operator with "use_dll" and "block" options (closes #98) (manually cherry-picked from 648c3cd177a32ac30b1f0504a322496e28932452) --- preprocessor/DynamicModel.cc | 9 ++-- preprocessor/ExprNode.cc | 12 +++-- preprocessor/ExprNode.hh | 7 +-- tests/Makefile.am | 4 ++ tests/steady_state_operator/block.mod | 45 +++++++++++++++++++ tests/steady_state_operator/bytecode_test.mod | 45 +++++++++++++++++++ tests/steady_state_operator/standard.mod | 45 +++++++++++++++++++ tests/steady_state_operator/use_dll.mod | 45 +++++++++++++++++++ 8 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 tests/steady_state_operator/block.mod create mode 100644 tests/steady_state_operator/bytecode_test.mod create mode 100644 tests/steady_state_operator/standard.mod create mode 100644 tests/steady_state_operator/use_dll.mod diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 5e77486326..0369f97622 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -288,7 +288,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const << " % // Simulation type " << BlockSim(simulation_type) << " //" << endl << " % ////////////////////////////////////////////////////////////////////////" << endl; - output << " global options_;" << endl; + output << " global options_ oo_;" << endl; //The Temporary terms if (simulation_type == EVALUATE_BACKWARD || simulation_type == EVALUATE_FORWARD) { @@ -1036,7 +1036,7 @@ DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) mDynamicModelFile << "/* The gateway routine */" << endl << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl << "{" << endl - << " double *y, *x, *params;" << endl + << " double *y, *x, *params, *steady_state;" << endl << " double *residual, *g1, *v2, *v3;" << endl << " int nb_row_x, it_;" << endl << endl @@ -1094,8 +1094,9 @@ DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) << " v3 = mxGetPr(plhs[3]);" << endl << " }" << endl << endl + << " steady_state = mxGetPr(mxGetField(mexGetVariable(\"global\", \"oo_\"), 0, \"steady_state\"));" << endl << " /* Call the C subroutines. */" << endl - << " Dynamic(y, x, nb_row_x, params, it_, residual, g1, v2, v3);" << endl + << " Dynamic(y, x, nb_row_x, params, steady_state, it_, residual, g1, v2, v3);" << endl << "}" << endl; mDynamicModelFile.close(); } @@ -1695,7 +1696,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const } else { - DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, int it_, double *residual, double *g1, double *v2, double *v3)" << endl + DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, double *g1, double *v2, double *v3)" << endl << "{" << endl << " double lhs, rhs;" << endl << endl diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 3f0b6d1f3e..a25595d655 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -557,8 +557,12 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "oo_.steady_state(" << tsid + 1 << ")"; break; case oMatlabDynamicSteadyStateOperator: + case oMatlabDynamicSparseSteadyStateOperator: output << "oo_.steady_state(" << tsid + 1 << ")"; break; + case oCDynamicSteadyStateOperator: + output << "steady_state[" << tsid << "]"; + break; default: assert(false); } @@ -1409,12 +1413,12 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, new_output_type = oLatexDynamicSteadyStateOperator; break; case oCDynamicModel: - cerr << "Steady State Operator not implemented for oCDynamicModel." << endl; - exit(EXIT_FAILURE); + new_output_type = oCDynamicSteadyStateOperator; + break; case oMatlabDynamicModelSparse: case oMatlabDynamicModelSparseLocalTemporaryTerms: - cerr << "Steady State Operator not implemented for oMatlabDynamicModelSparse." << endl; - exit(EXIT_FAILURE); + new_output_type = oMatlabDynamicSparseSteadyStateOperator; + break; default: new_output_type = output_type; break; diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index 573a9e7958..e53c4405a6 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -66,7 +66,8 @@ enum ExprNodeOutputType oLatexDynamicModel, //!< LaTeX code, dynamic model declarations oLatexDynamicSteadyStateOperator, //!< LaTeX code, dynamic model steady state declarations oMatlabDynamicSteadyStateOperator, //!< Matlab code, dynamic model steady state declarations - oMatlabDynamicModelSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model steady state declarations + oMatlabDynamicSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model, inside a steady state operator + oCDynamicSteadyStateOperator, //!< C code, dynamic model, inside a steady state operator oMatlabDynamicModelSparseLocalTemporaryTerms //!< Matlab code, dynamic block decomposed model local temporary_terms }; @@ -77,9 +78,9 @@ enum ExprNodeOutputType || (output_type) == oMatlabDynamicModelSparse \ || (output_type) == oMatlabDynamicModelSparseLocalTemporaryTerms \ || (output_type) == oMatlabDynamicSteadyStateOperator \ - || (output_type) == oMatlabDynamicModelSparseSteadyStateOperator) + || (output_type) == oMatlabDynamicSparseSteadyStateOperator) -#define IS_C(output_type) ((output_type) == oCDynamicModel) +#define IS_C(output_type) ((output_type) == oCDynamicModel || (output_type) == oCDynamicSteadyStateOperator) #define IS_LATEX(output_type) ((output_type) == oLatexStaticModel \ || (output_type) == oLatexDynamicModel \ diff --git a/tests/Makefile.am b/tests/Makefile.am index b5bda79008..22487ba193 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -14,6 +14,10 @@ OCTAVE_MODS = \ ramst_normcdf.mod \ example1_varexo_det.mod \ predetermined_variables.mod \ + steady_state_operator/standard.mod \ + steady_state_operator/use_dll.mod \ + steady_state_operator/block.mod \ + steady_state_operator/bytecode_test.mod \ block_bytecode/fs2000_simk.mod \ block_bytecode/fs2000_lu.mod \ block_bytecode/fs2000_bicgstab.mod \ diff --git a/tests/steady_state_operator/block.mod b/tests/steady_state_operator/block.mod new file mode 100644 index 0000000000..6dad210cad --- /dev/null +++ b/tests/steady_state_operator/block.mod @@ -0,0 +1,45 @@ +// Tests the steady_state operator, in static and dynamic M-files with block decomposition + +var c k w; +varexo x; + +parameters alph gam delt bet aa c_steady_state; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model(block); +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +w = steady_state(k); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +w = 0; +end; + +steady; + +//check; + +shocks; +var x; +periods 1; +values 1.2; +end; + +simul(periods=20, stack_solve_algo=1); + +if(abs(oo_.steady_state(2) - oo_.steady_state(3)) > 1e-10) + error('Test failed in static M-file for steady_state') +end + +if(abs(oo_.steady_state(2) - oo_.endo_simul(3,2)) > 1e-10) + error('Test failed in dynamic M-file for steady_state') +end + diff --git a/tests/steady_state_operator/bytecode_test.mod b/tests/steady_state_operator/bytecode_test.mod new file mode 100644 index 0000000000..b8be426cab --- /dev/null +++ b/tests/steady_state_operator/bytecode_test.mod @@ -0,0 +1,45 @@ +// Tests the steady_state operator, with the bytecode + +var c k w; +varexo x; + +parameters alph gam delt bet aa c_steady_state; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model(block, bytecode); +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +w = steady_state(k); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +w = 0; +end; + +steady(solve_algo=5); + +//check; + +shocks; +var x; +periods 1; +values 1.2; +end; + +simul(periods=20, stack_solve_algo=5); + +if(abs(oo_.steady_state(2) - oo_.steady_state(3)) > 1e-10) + error('Test failed in static M-file for steady_state') +end + +if(abs(oo_.steady_state(2) - oo_.endo_simul(3,2)) > 1e-10) + error('Test failed in dynamic M-file for steady_state') +end + diff --git a/tests/steady_state_operator/standard.mod b/tests/steady_state_operator/standard.mod new file mode 100644 index 0000000000..a09fa1492b --- /dev/null +++ b/tests/steady_state_operator/standard.mod @@ -0,0 +1,45 @@ +// Tests the steady_state operator, in static and dynamic M-files + +var c k w; +varexo x; + +parameters alph gam delt bet aa c_steady_state; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +w = steady_state(k); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +w = 0; +end; + +steady; + +check; + +shocks; +var x; +periods 1; +values 1.2; +end; + +simul(periods=20); + +if(abs(oo_.steady_state(2) - oo_.steady_state(3)) > 1e-10) + error('Test failed in static M-file for steady_state') +end + +if(abs(oo_.steady_state(2) - oo_.endo_simul(3,2)) > 1e-10) + error('Test failed in dynamic M-file for steady_state') +end + diff --git a/tests/steady_state_operator/use_dll.mod b/tests/steady_state_operator/use_dll.mod new file mode 100644 index 0000000000..ee0ea358fb --- /dev/null +++ b/tests/steady_state_operator/use_dll.mod @@ -0,0 +1,45 @@ +// Tests the steady_state operator, in static M-file and dynamic C-file + +var c k w; +varexo x; + +parameters alph gam delt bet aa c_steady_state; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model(use_dll); +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +w = steady_state(k); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +w = 0; +end; + +steady; + +check; + +shocks; +var x; +periods 1; +values 1.2; +end; + +simul(periods=20); + +if(abs(oo_.steady_state(2) - oo_.steady_state(3)) > 1e-10) + error('Test failed in static M-file for steady_state') +end + +if(abs(oo_.steady_state(2) - oo_.endo_simul(3,2)) > 1e-10) + error('Test failed in dynamic M-file for steady_state') +end + -- GitLab