diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 5e774863265617c29b1964e34385e0db8414849a..0369f97622e170629040cb3fb666bd92884e8e6b 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 3f0b6d1f3e00b378278786e9e68396ef8349749c..a25595d6553f7ed0460ac894ac3c084ab6111d60 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 573a9e7958ab846739f86fc4fcaa91dac03ede23..e53c4405a69587ad8630088470da153627df25d5 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 b5bda790088b6bf7e81bae18442b5e3bcfeae357..22487ba193553eb9e30817122b43535a2b5d3ae1 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 0000000000000000000000000000000000000000..6dad210cad148a3df91b86fe918977f6667dbd71 --- /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 0000000000000000000000000000000000000000..b8be426cab423f265ec4fbc7b0ebb91fa2d679ff --- /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 0000000000000000000000000000000000000000..a09fa1492b42a57b73563497bcb754aa8ab12d42 --- /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 0000000000000000000000000000000000000000..ee0ea358fb3be5644c390d0a9d3d728143b9b23e --- /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 +