From 3019c80f567f1a2605e4a4d1e732cb5c61ae6379 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 23 Sep 2013 14:27:43 +0200 Subject: [PATCH 001/162] front end for Geweke convergence diagnostics for single chains --- DynareBison.yy | 8 ++++++-- DynareFlex.ll | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 5207b3a0..dd2192a7 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -113,7 +113,7 @@ class ParsingDriver; %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS SOLVE_MAXIT %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN -%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS +%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF @@ -1555,7 +1555,9 @@ estimation_options : o_datafile | o_ar | o_endogenous_prior | o_use_univariate_filters_if_singularity_is_detected - | o_qz_zero_threshold + | o_qz_zero_threshold + | o_taper_steps + | o_geweke_interval ; list_optim_option : QUOTED_STRING COMMA QUOTED_STRING @@ -2388,6 +2390,8 @@ o_noprint : NOPRINT { driver.option_num("noprint", "1"); }; o_xls_sheet : XLS_SHEET EQUAL symbol { driver.option_str("xls_sheet", $3); }; o_xls_range : XLS_RANGE EQUAL range { driver.option_str("xls_range", $3); }; o_filter_step_ahead : FILTER_STEP_AHEAD EQUAL vec_int { driver.option_vec_int("filter_step_ahead", $3); }; +o_taper_steps : TAPER_STEPS EQUAL vec_int { driver.option_vec_int("taper_steps", $3); }; +o_geweke_interval : GEWEKE_INTERVAL EQUAL vec_value { driver.option_num("geweke_interval",$3); }; o_constant : CONSTANT { driver.option_num("noconstant", "0"); }; o_noconstant : NOCONSTANT { driver.option_num("noconstant", "1"); }; o_mh_recover : MH_RECOVER { driver.option_num("mh_recover", "1"); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index e4045035..2024f625 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -222,6 +222,8 @@ string eofbuff; presample {return token::PRESAMPLE;} lik_algo {return token::LIK_ALGO;} lik_init {return token::LIK_INIT;} +taper_steps {return token::TAPER_STEPS;} +geweke_interval {return token::GEWEKE_INTERVAL;} graph {return token::GRAPH;} nograph {return token::NOGRAPH;} nodisplay {return token::NODISPLAY;} From 5f2bf105216090b6ce5ae3eec7cf292ca0cf91d1 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 24 Sep 2013 14:29:05 +0200 Subject: [PATCH 002/162] front end for maxit: ref #293, #262 --- DynareBison.yy | 13 +++++++------ DynareFlex.ll | 1 - 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 5207b3a0..621a092b 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -111,7 +111,7 @@ class ParsingDriver; %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT -%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS SOLVE_MAXIT +%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS %token NAME @@ -878,7 +878,7 @@ steady_options : o_solve_algo | o_homotopy_steps | o_homotopy_force_continue | o_markowitz - | o_maxit + | o_steady_maxit | o_nocheck ; @@ -922,7 +922,7 @@ simul_options : o_periods | o_stack_solve_algo | o_markowitz | o_minimal_solving_periods - | o_maxit + | o_simul_maxit ; external_function : EXTERNAL_FUNCTION '(' external_function_options_list ')' ';' @@ -1729,7 +1729,7 @@ discretionary_policy_options_list : discretionary_policy_options_list COMMA disc discretionary_policy_options : ramsey_policy_options | o_discretionary_tol; - | o_solve_maxit; + | o_dp_maxit; ; ramsey_policy_options_list : ramsey_policy_options_list COMMA ramsey_policy_options @@ -2266,10 +2266,11 @@ o_periods : PERIODS EQUAL INT_NUMBER { driver.option_num("periods", $3); }; o_solver_periods : SOLVER_PERIODS EQUAL INT_NUMBER { driver.option_num("ep.periods", $3); }; o_extended_path_order : ORDER EQUAL INT_NUMBER { driver.option_num("ep.stochastic.order", $3); }; o_hybrid : HYBRID { driver.option_num("ep.stochastic.hybrid_order", "2"); }; -o_maxit : MAXIT EQUAL INT_NUMBER { driver.option_num("maxit_", $3); }; +o_steady_maxit : MAXIT EQUAL INT_NUMBER { driver.option_num("steady.maxit", $3); }; +o_simul_maxit : MAXIT EQUAL INT_NUMBER { driver.option_num("simul.maxit", $3); }; +o_dp_maxit : MAXIT EQUAL INT_NUMBER { driver.option_num("dp.maxit", $3); }; o_osr_maxit : MAXIT EQUAL INT_NUMBER { driver.option_num("osr.maxit", $3); }; o_osr_tolf : TOLF EQUAL non_negative_number { driver.option_num("osr.tolf", $3); }; -o_solve_maxit : SOLVE_MAXIT EQUAL INT_NUMBER { driver.option_num("solve_maxit", $3); }; o_cutoff : CUTOFF EQUAL non_negative_number { driver.cutoff($3); }; o_markowitz : MARKOWITZ EQUAL non_negative_number { driver.option_num("markowitz", $3); }; o_minimal_solving_periods : MINIMAL_SOLVING_PERIODS EQUAL non_negative_number { driver.option_num("minimal_solving_periods", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index e4045035..b045719c 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -424,7 +424,6 @@ string eofbuff; max_block_iterations {return token::MAX_BLOCK_ITERATIONS;} max_repeated_optimization_runs {return token::MAX_REPEATED_OPTIMIZATION_RUNS;} maxit {return token::MAXIT;} -solve_maxit {return token::SOLVE_MAXIT;} function_convergence_criterion {return token::FUNCTION_CONVERGENCE_CRITERION;} parameter_convergence_criterion {return token::PARAMETER_CONVERGENCE_CRITERION;} number_of_large_perturbations {return token::NUMBER_OF_LARGE_PERTURBATIONS;} From aaa50d6a40eeab72385f65896a9f52b853a81f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 25 Sep 2013 14:44:49 +0200 Subject: [PATCH 003/162] Fix a bug similar to #476 in estimated_params_bounds --- ComputingTasks.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 01be77d7..458a1db2 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -703,7 +703,8 @@ EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basen { if (symb_type == eExogenous) { - output << "tmp1 = find((estim_params_.corrx(:,1)==" << symb_id << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ");" << endl; + output << "tmp1 = find((estim_params_.corrx(:,1)==" << symb_id << " & estim_params_.corrx(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ") | " + << "(estim_params_.corrx(:,2)==" << symb_id << " & estim_params_.corrx(:,1)==" << symbol_table.getTypeSpecificID(it->name2)+1 << "));" << endl; output << "estim_params_.corrx(tmp1,4) = "; it->low_bound->writeOutput(output); @@ -715,7 +716,8 @@ EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basen } else if (symb_type == eEndogenous) { - output << "tmp1 = find((estim_params_.corrn(:,1)==" << symb_id << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ";" << endl; + output << "tmp1 = find((estim_params_.corrn(:,1)==" << symb_id << " & estim_params_.corrn(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ") | " + << "(estim_params_.corrn(:,2)==" << symb_id << " & estim_params_.corrn(:,1)==" << symbol_table.getTypeSpecificID(it->name2)+1 << "));" << endl; output << "estim_params_.corrn(tmp1,4) = "; it->low_bound->writeOutput(output); From eeb0596831dbde308e1599adddb3178db19430f1 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 11:12:32 +0200 Subject: [PATCH 004/162] bug fix for date_number: don't permit weeks >= 53 --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 2024f625..35616b1d 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -691,7 +691,7 @@ string eofbuff; return token::INT_NUMBER; } -([1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|([1-2][0-9]{3}[Qq][1-4])|([1-2][0-9]{3}[Ww](([1-9]{1})|([1-5][0-9]))) { +([1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|([1-2][0-9]{3}[Qq][1-4])|([1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|([5][0-2]))) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From 50d1a71aad2a8eedb4e92ce9de0142873a344716 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:12:31 +0200 Subject: [PATCH 005/162] support negative dates --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 35616b1d..9e3a9d55 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -691,7 +691,7 @@ string eofbuff; return token::INT_NUMBER; } -([1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|([1-2][0-9]{3}[Qq][1-4])|([1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|([5][0-2]))) { +(-?[1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|(-?[1-2][0-9]{3}[Qq][1-4])|(-?[1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|([5][0-2]))) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From 3e5f03fadcdbe0470b4653a70b566199a725f3de Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:16:33 +0200 Subject: [PATCH 006/162] simplify regex --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 9e3a9d55..2f2b89b4 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -691,7 +691,7 @@ string eofbuff; return token::INT_NUMBER; } -(-?[1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|(-?[1-2][0-9]{3}[Qq][1-4])|(-?[1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|([5][0-2]))) { +(-?[1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|(-?[1-2][0-9]{3}[Qq][1-4])|(-?[1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|(5[0-2]))) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From afc73796d43134fb80ddf2384b8a721d7f0d668e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:48:34 +0200 Subject: [PATCH 007/162] support unbounded years --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 2f2b89b4..2193ff9d 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -691,7 +691,7 @@ string eofbuff; return token::INT_NUMBER; } -(-?[1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|(-?[1-2][0-9]{3}[Qq][1-4])|(-?[1-2][0-9]{3}[Ww](([1-9]{1})|([1-4][0-9])|(5[0-2]))) { +(-?[0-9]+[Mm](([1-9])|(1[0-2])))|(-?[0-9]+[Qq][1-4])|(-?[0-9]+[Ww](([1-9]{1})|([1-4][0-9])|(5[0-2]))) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From bba6318bdbd9fca95449402420da03c3248ced53 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:56:38 +0200 Subject: [PATCH 008/162] remove unnecessary parenthesis --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 2193ff9d..6d7f3ab6 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -691,7 +691,7 @@ string eofbuff; return token::INT_NUMBER; } -(-?[0-9]+[Mm](([1-9])|(1[0-2])))|(-?[0-9]+[Qq][1-4])|(-?[0-9]+[Ww](([1-9]{1})|([1-4][0-9])|(5[0-2]))) { +-?[0-9]+[Mm]([1-9]|1[0-2])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From b42790cd6e962db91eeac82842dda5f1c7a754b0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:41:05 +0200 Subject: [PATCH 009/162] preprocessor: replace dates with dynDates --- ParsingDriver.cc | 3 ++- Statement.cc | 69 +++++++++++++++++++++++++++++++++++++++++++++++- Statement.hh | 6 +++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 38bf5a78..37029fdc 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2449,7 +2449,8 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in void ParsingDriver::add_native(const string &s) { - mod_file->addStatement(new NativeStatement(s)); + string ss = string(s); + mod_file->addStatement(new NativeStatement(ss)); } void diff --git a/Statement.cc b/Statement.cc index 59021287..242c73a8 100644 --- a/Statement.cc +++ b/Statement.cc @@ -18,6 +18,7 @@ */ #include "Statement.hh" +#include ModFileStructure::ModFileStructure() : check_present(false), @@ -65,11 +66,77 @@ Statement::computingPass() { } -NativeStatement::NativeStatement(const string &native_statement_arg) : +NativeStatement::NativeStatement(string &native_statement_arg) : native_statement(native_statement_arg) { } +void +NativeStatement::computingPass() +{ + using namespace boost::xpressive; + // Return if this is a comment + sregex comment_expr = sregex::compile( "\%.*" ); + match_results results; + if (regex_match(native_statement, results, comment_expr)) + return; + + // Otherwise, look at the line and consider substituting date + size_t idx = -1; + vector apostrophes; + while((idx = native_statement.find("'", idx + 1)) != string::npos) + if (apostrophes.size() < 2) + apostrophes.push_back(idx); + else + if (idx == apostrophes.back() + 1) + apostrophes.pop_back(); + else + apostrophes.push_back(idx); + + if (apostrophes.size() % 2) + { + cerr << native_statement << + " seems to be invalid Matlab syntax (an odd number of apostrophes was encountered)" << endl; + exit(EXIT_FAILURE); + } + + bool skip = false; + string newstr = ""; + int lastidx = 0; + sregex date_expr = sregex::compile( "-?[0-9]+[Mm]([1-9]|1[0-2])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])" ); + string format( "dynDate('$&')" ); + for (size_t i = 0; i < apostrophes.size(); i++) + if (apostrophes[i] == 0) + skip = true; + else + { + if (skip) + { + skip = false; + newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); + } + else + { + skip = true; + newstr.append(regex_replace(native_statement.substr(lastidx, apostrophes[i] - lastidx), + date_expr, format)); + } + lastidx = apostrophes[i]; + } + + //Replace last (or only) element + if (apostrophes.empty()) + lastidx = 0; + newstr.append(regex_replace(native_statement.substr(lastidx, native_statement.size() - lastidx), + date_expr, format)); + native_statement = newstr; +} + +void +regexReplace() +{ +} + void NativeStatement::writeOutput(ostream &output, const string &basename) const { diff --git a/Statement.hh b/Statement.hh index cd3fd5cf..82f64fa7 100644 --- a/Statement.hh +++ b/Statement.hh @@ -121,10 +121,12 @@ public: class NativeStatement : public Statement { private: - const string native_statement; + string native_statement; public: - NativeStatement(const string &native_statement_arg); + NativeStatement(string &native_statement_arg); + virtual void computingPass(); virtual void writeOutput(ostream &output, const string &basename) const; + void regexReplace(); }; class OptionsList From a96d79a3f0efd06ab10adfd5a606ed2fd57c8084 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 09:09:18 +0200 Subject: [PATCH 010/162] remove unnecessary test --- Statement.cc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Statement.cc b/Statement.cc index 242c73a8..0a449b85 100644 --- a/Statement.cc +++ b/Statement.cc @@ -123,10 +123,6 @@ NativeStatement::computingPass() } lastidx = apostrophes[i]; } - - //Replace last (or only) element - if (apostrophes.empty()) - lastidx = 0; newstr.append(regex_replace(native_statement.substr(lastidx, native_statement.size() - lastidx), date_expr, format)); native_statement = newstr; From bb4ce64add592e9270fe0889818592613fc59819 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 10:48:29 +0200 Subject: [PATCH 011/162] catch leading space before comment --- Statement.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Statement.cc b/Statement.cc index 0a449b85..8ac208fe 100644 --- a/Statement.cc +++ b/Statement.cc @@ -76,7 +76,7 @@ NativeStatement::computingPass() { using namespace boost::xpressive; // Return if this is a comment - sregex comment_expr = sregex::compile( "\%.*" ); + sregex comment_expr = sregex::compile( "\\s*\%.*" ); match_results results; if (regex_match(native_statement, results, comment_expr)) return; From 50b2c9d45427aeaa895d986bc5abccc4d07c2c5d Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 15:43:38 +0200 Subject: [PATCH 012/162] clarify error message --- Statement.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Statement.cc b/Statement.cc index 8ac208fe..c60be843 100644 --- a/Statement.cc +++ b/Statement.cc @@ -95,8 +95,7 @@ NativeStatement::computingPass() if (apostrophes.size() % 2) { - cerr << native_statement << - " seems to be invalid Matlab syntax (an odd number of apostrophes was encountered)" << endl; + cerr << "ERROR: A Matlab Statement has an odd number of apostrophes: " << native_statement << endl; exit(EXIT_FAILURE); } From 1b527aa0043ebab1ed33f13a2dfdb914755714b1 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 16:19:35 +0200 Subject: [PATCH 013/162] fix regular expression --- Statement.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Statement.cc b/Statement.cc index c60be843..5e08c164 100644 --- a/Statement.cc +++ b/Statement.cc @@ -102,7 +102,7 @@ NativeStatement::computingPass() bool skip = false; string newstr = ""; int lastidx = 0; - sregex date_expr = sregex::compile( "-?[0-9]+[Mm]([1-9]|1[0-2])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])" ); + sregex date_expr = sregex::compile( "-?[0-9]+[Mm](1[0-2]|[1-9])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-4][0-9]|5[0-2]|[1-9])" ); string format( "dynDate('$&')" ); for (size_t i = 0; i < apostrophes.size(); i++) if (apostrophes[i] == 0) From dea0cdbdf3954bb009ccc32bb27de821bf49714a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 15:30:22 +0200 Subject: [PATCH 014/162] handle inline comments --- Statement.cc | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/Statement.cc b/Statement.cc index 5e08c164..649d99a5 100644 --- a/Statement.cc +++ b/Statement.cc @@ -101,10 +101,11 @@ NativeStatement::computingPass() bool skip = false; string newstr = ""; - int lastidx = 0; sregex date_expr = sregex::compile( "-?[0-9]+[Mm](1[0-2]|[1-9])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-4][0-9]|5[0-2]|[1-9])" ); string format( "dynDate('$&')" ); - for (size_t i = 0; i < apostrophes.size(); i++) + size_t length, i, lastidx, commentidx; + length = lastidx = 0; + for (i = 0; i < apostrophes.size(); i++) if (apostrophes[i] == 0) skip = true; else @@ -116,14 +117,40 @@ NativeStatement::computingPass() } else { + length = apostrophes[i] - lastidx; + commentidx = native_statement.substr(lastidx, length).find("%", 0); + if (commentidx != string::npos) + length = commentidx; + + newstr.append(regex_replace(native_statement.substr(lastidx, length), date_expr, format)); + if (commentidx != string::npos) + { + lastidx += commentidx; + while (i++ < apostrophes.size()) + { + newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); + lastidx = apostrophes[i]; + } + native_statement = newstr; + return; + } skip = true; - newstr.append(regex_replace(native_statement.substr(lastidx, apostrophes[i] - lastidx), - date_expr, format)); } lastidx = apostrophes[i]; } - newstr.append(regex_replace(native_statement.substr(lastidx, native_statement.size() - lastidx), - date_expr, format)); + length = native_statement.length() - lastidx; + commentidx = native_statement.substr(lastidx, length).find("%", 0); + if (commentidx != string::npos) + length = commentidx; + + newstr.append(regex_replace(native_statement.substr(lastidx, length), date_expr, format)); + + if (commentidx != string::npos) + { + lastidx += commentidx; + newstr.append(native_statement.substr(lastidx, native_statement.length() - lastidx)); + } + native_statement = newstr; } From a29ed07ea56b6bf1d7f1d558e2410996d8201031 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Oct 2013 16:34:51 +0200 Subject: [PATCH 015/162] remove unused portion of code, code cleanup --- Statement.cc | 51 +++++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/Statement.cc b/Statement.cc index 649d99a5..4017d995 100644 --- a/Statement.cc +++ b/Statement.cc @@ -103,43 +103,26 @@ NativeStatement::computingPass() string newstr = ""; sregex date_expr = sregex::compile( "-?[0-9]+[Mm](1[0-2]|[1-9])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-4][0-9]|5[0-2]|[1-9])" ); string format( "dynDate('$&')" ); - size_t length, i, lastidx, commentidx; - length = lastidx = 0; - for (i = 0; i < apostrophes.size(); i++) + size_t lastidx = 0; + for (size_t i = 0; i < apostrophes.size(); i++) if (apostrophes[i] == 0) skip = true; else - { - if (skip) - { - skip = false; - newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); - } - else - { - length = apostrophes[i] - lastidx; - commentidx = native_statement.substr(lastidx, length).find("%", 0); - if (commentidx != string::npos) - length = commentidx; - - newstr.append(regex_replace(native_statement.substr(lastidx, length), date_expr, format)); - if (commentidx != string::npos) - { - lastidx += commentidx; - while (i++ < apostrophes.size()) - { - newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); - lastidx = apostrophes[i]; - } - native_statement = newstr; - return; - } - skip = true; - } - lastidx = apostrophes[i]; - } - length = native_statement.length() - lastidx; - commentidx = native_statement.substr(lastidx, length).find("%", 0); + if (skip) + { + newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); + lastidx = apostrophes[i]; + skip = false; + } + else + { + newstr.append(regex_replace(native_statement.substr(lastidx, apostrophes[i] - lastidx), + date_expr, format)); + lastidx = apostrophes[i]; + skip = true; + } + size_t length = native_statement.length() - lastidx; + size_t commentidx = native_statement.substr(lastidx, length).find("%", 0); if (commentidx != string::npos) length = commentidx; From 7b523eccd0d31fc9f349bb2dac769fd6cbacca72 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 4 Oct 2013 16:17:29 +0200 Subject: [PATCH 016/162] remove error message because odd numbers of apostrophes are valid for transposing matrices in Matlab syntax --- Statement.cc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Statement.cc b/Statement.cc index 4017d995..38da6e36 100644 --- a/Statement.cc +++ b/Statement.cc @@ -93,12 +93,6 @@ NativeStatement::computingPass() else apostrophes.push_back(idx); - if (apostrophes.size() % 2) - { - cerr << "ERROR: A Matlab Statement has an odd number of apostrophes: " << native_statement << endl; - exit(EXIT_FAILURE); - } - bool skip = false; string newstr = ""; sregex date_expr = sregex::compile( "-?[0-9]+[Mm](1[0-2]|[1-9])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-4][0-9]|5[0-2]|[1-9])" ); From 7063e2d8beafa2a58492bdeee156e870c909a8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Wed, 9 Oct 2013 13:06:06 +0200 Subject: [PATCH 017/162] Changed name of options_.maxit as options_.simul.maxit. --- DynamicModel.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 52eabbed..d19727fa 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -1889,7 +1889,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri << " disp (['MODEL SIMULATION: (method=' mthd ')']) ;" << endl << " fprintf('\\n') ;" << endl << " periods=options_.periods;" << endl - << " maxit_=options_.maxit_;" << endl + << " maxit_=options_.simul.maxit;" << endl << " solve_tolf=options_.solve_tolf;" << endl << " y=oo_.endo_simul';" << endl << " x=oo_.exo_simul;" << endl; @@ -1988,7 +1988,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << block + 1 << "'" <<", y, x, params, steady_state, y_index, " << nze <<", options_.periods, " << blocks_linear[block] - <<", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, 1, 1, 0);\n"; + <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, 1, 1, 0);\n"; mDynamicModelFile << " tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n"; mDynamicModelFile << " if any(isnan(tmp) | isinf(tmp))\n"; mDynamicModelFile << " disp(['Inf or Nan value during the resolution of block " << block <<"']);\n"; @@ -2018,7 +2018,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << block + 1 << "'" <<", y, x, params, steady_state, y_index, " << nze <<", options_.periods, " << blocks_linear[block] - <<", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, 1, 1, 0);\n"; + <<", blck_num, y_kmin, options.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, 1, 1, 0);\n"; mDynamicModelFile << " tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n"; mDynamicModelFile << " if any(isnan(tmp) | isinf(tmp))\n"; mDynamicModelFile << " disp(['Inf or Nan value during the resolution of block " << block <<"']);\n"; @@ -2048,7 +2048,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri <<", options_.periods, " << max_leadlag_block[block].first <<", " << max_leadlag_block[block].second <<", " << blocks_linear[block] - <<", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo);\n"; + <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo);\n"; mDynamicModelFile << " tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n"; mDynamicModelFile << " if any(isnan(tmp) | isinf(tmp))\n"; mDynamicModelFile << " disp(['Inf or Nan value during the resolution of block " << block <<"']);\n"; From 496036d770c458c2fbb6b25e1646eb389fd52a66 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 10 Oct 2013 11:04:56 +0200 Subject: [PATCH 018/162] Revert "preprocessor: replace dates with dynDates" This reverts commit 030fe52affb763f11766801f6deeb797e90365e2. Conflicts: preprocessor/Statement.cc --- ParsingDriver.cc | 3 +-- Statement.cc | 68 +----------------------------------------------- Statement.hh | 6 ++--- 3 files changed, 4 insertions(+), 73 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 37029fdc..38bf5a78 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2449,8 +2449,7 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in void ParsingDriver::add_native(const string &s) { - string ss = string(s); - mod_file->addStatement(new NativeStatement(ss)); + mod_file->addStatement(new NativeStatement(s)); } void diff --git a/Statement.cc b/Statement.cc index 38da6e36..59021287 100644 --- a/Statement.cc +++ b/Statement.cc @@ -18,7 +18,6 @@ */ #include "Statement.hh" -#include ModFileStructure::ModFileStructure() : check_present(false), @@ -66,76 +65,11 @@ Statement::computingPass() { } -NativeStatement::NativeStatement(string &native_statement_arg) : +NativeStatement::NativeStatement(const string &native_statement_arg) : native_statement(native_statement_arg) { } -void -NativeStatement::computingPass() -{ - using namespace boost::xpressive; - // Return if this is a comment - sregex comment_expr = sregex::compile( "\\s*\%.*" ); - match_results results; - if (regex_match(native_statement, results, comment_expr)) - return; - - // Otherwise, look at the line and consider substituting date - size_t idx = -1; - vector apostrophes; - while((idx = native_statement.find("'", idx + 1)) != string::npos) - if (apostrophes.size() < 2) - apostrophes.push_back(idx); - else - if (idx == apostrophes.back() + 1) - apostrophes.pop_back(); - else - apostrophes.push_back(idx); - - bool skip = false; - string newstr = ""; - sregex date_expr = sregex::compile( "-?[0-9]+[Mm](1[0-2]|[1-9])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-4][0-9]|5[0-2]|[1-9])" ); - string format( "dynDate('$&')" ); - size_t lastidx = 0; - for (size_t i = 0; i < apostrophes.size(); i++) - if (apostrophes[i] == 0) - skip = true; - else - if (skip) - { - newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); - lastidx = apostrophes[i]; - skip = false; - } - else - { - newstr.append(regex_replace(native_statement.substr(lastidx, apostrophes[i] - lastidx), - date_expr, format)); - lastidx = apostrophes[i]; - skip = true; - } - size_t length = native_statement.length() - lastidx; - size_t commentidx = native_statement.substr(lastidx, length).find("%", 0); - if (commentidx != string::npos) - length = commentidx; - - newstr.append(regex_replace(native_statement.substr(lastidx, length), date_expr, format)); - - if (commentidx != string::npos) - { - lastidx += commentidx; - newstr.append(native_statement.substr(lastidx, native_statement.length() - lastidx)); - } - - native_statement = newstr; -} - -void -regexReplace() -{ -} - void NativeStatement::writeOutput(ostream &output, const string &basename) const { diff --git a/Statement.hh b/Statement.hh index 82f64fa7..cd3fd5cf 100644 --- a/Statement.hh +++ b/Statement.hh @@ -121,12 +121,10 @@ public: class NativeStatement : public Statement { private: - string native_statement; + const string native_statement; public: - NativeStatement(string &native_statement_arg); - virtual void computingPass(); + NativeStatement(const string &native_statement_arg); virtual void writeOutput(ostream &output, const string &basename) const; - void regexReplace(); }; class OptionsList From cbb0167c4c9ecaa2ea9c7976b7167c23274e1fea Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 11 Oct 2013 11:13:15 +0200 Subject: [PATCH 019/162] simplify date regex and require [YyAa] to follow annual date --- DynareFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index 19829085..ad894a6e 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -690,7 +690,7 @@ string eofbuff; return token::INT_NUMBER; } --?[0-9]+[Mm]([1-9]|1[0-2])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]) { +-?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } From c2a3d342d81d2db0d8fdfa633b90a7498ba2fbe3 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 10 Oct 2013 17:53:06 +0200 Subject: [PATCH 020/162] macroprocessor: substitute dates, don't substitute dates preceded by $ or dates(' --- macro/MacroFlex.ll | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 05a60329..792efce7 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,6 +51,7 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY +%x END_DATE %{ // Increments location counter for every token read @@ -60,6 +61,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ +DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ @@ -87,6 +89,12 @@ CONT \\\\ ^{SPC}*@# { yylloc->step(); BEGIN(STMT); } @\{ { yylloc->step(); BEGIN(EXPR); } +{DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } +${DATE} { yylloc->step(); *yyout << yytext + 1; } + +dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } +{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } + \} { BEGIN(INITIAL); return token::EOL; } {CONT}{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); } From aee8202a20c31ed828baa584391844c864e66c1e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 11 Oct 2013 16:28:14 +0200 Subject: [PATCH 021/162] preprocessor: only accept dates of the form: dates('date') in mod file --- ComputingTasks.cc | 8 ++++---- DynareBison.yy | 14 +++++--------- DynareFlex.ll | 2 +- ParsingDriver.cc | 5 +---- Statement.cc | 4 ++-- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 458a1db2..70d20bf5 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1767,10 +1767,10 @@ SubsamplesStatement::writeOutput(ostream &output, const string &basename) const it != subsample_declaration_map.end(); it++, map_indx++) output << "estimation_info.subsamples(subsamples_indx).range_index(" << map_indx << ") = {'" << it->first << "'};" << endl - << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date1 = dynDate('" - << it->second.first << "');" << endl - << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date2 = dynDate('" - << it->second.second << "');" << endl; + << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date1 = " + << it->second.first << endl + << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date2 = " + << it->second.second << endl; // Initialize associated subsample substructures in estimation_info const SymbolType symb_type = symbol_table.getType(name1); diff --git a/DynareBison.yy b/DynareBison.yy index 85663273..a53fa0d3 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -178,7 +178,7 @@ class ParsingDriver; %type expression expression_or_empty %type equation hand_side -%type non_negative_number signed_number signed_integer date_number +%type non_negative_number signed_number signed_integer %type filename symbol vec_of_vec_value vec_value_list %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf @@ -1024,10 +1024,6 @@ non_negative_number : INT_NUMBER | FLOAT_NUMBER ; -date_number : DATE_NUMBER - | INT_NUMBER - ; - signed_number : PLUS non_negative_number { $$ = $2; } | MINUS non_negative_number @@ -1243,7 +1239,7 @@ prior_pdf : BETA_PDF { $$ = eInvGamma2; } ; -set_time : SET_TIME '(' date_number ')' ';' +set_time : SET_TIME '(' DATE_NUMBER ')' ';' { driver.set_time($3); } ; @@ -2295,10 +2291,10 @@ o_conditional_variance_decomposition : CONDITIONAL_VARIANCE_DECOMPOSITION EQUAL { driver.option_vec_int("conditional_variance_decomposition", $3); } ; o_first_obs : FIRST_OBS EQUAL INT_NUMBER { driver.option_num("first_obs", $3); }; -o_new_estimation_data_first_obs : FIRST_OBS EQUAL date_number +o_new_estimation_data_first_obs : FIRST_OBS EQUAL DATE_NUMBER { driver.option_date("first_obs", $3); } ; -o_last_obs : LAST_OBS EQUAL date_number +o_last_obs : LAST_OBS EQUAL DATE_NUMBER { driver.option_date("last_obs", $3); } ; o_shift : SHIFT EQUAL signed_number { driver.option_num("shift", $3); }; @@ -2342,7 +2338,7 @@ list_allowed_graph_formats : allowed_graph_formats | list_allowed_graph_formats COMMA allowed_graph_formats ; -o_subsample_name : symbol EQUAL date_number ':' date_number +o_subsample_name : symbol EQUAL DATE_NUMBER ':' DATE_NUMBER { driver.set_subsample_name_equal_to_date_range($1, $3, $5); } ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index ad894a6e..c836be7f 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -690,7 +690,7 @@ string eofbuff; return token::INT_NUMBER; } --?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) { +dates[[:space:]]*\([[:space:]]*\'[[:space:]]*-?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 38bf5a78..31d5fbfa 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1226,10 +1226,7 @@ ParsingDriver::set_unit_root_vars() void ParsingDriver::set_time(string *arg) { - string arg1 = *arg; - for (size_t i=0; iaddStatement(new SetTimeStatement(options_list)); options_list.clear(); } diff --git a/Statement.cc b/Statement.cc index 59021287..76b600b7 100644 --- a/Statement.cc +++ b/Statement.cc @@ -94,7 +94,7 @@ OptionsList::writeOutput(ostream &output) const for (date_options_t::const_iterator it = date_options.begin(); it != date_options.end(); it++) - output << "options_." << it->first << " = dynDate('" << it->second << "');" << endl; + output << "options_." << it->first << " = " << it->second << ";" << endl; for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) @@ -137,7 +137,7 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const for (date_options_t::const_iterator it = date_options.begin(); it != date_options.end(); it++) - output << option_group << "." << it->first << " = dynDate('" << it->second << "');" << endl; + output << option_group << "." << it->first << " = " << it->second << ";" << endl; for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) From f8eb08bd550fc279c4b5e3f1475d5a123a90665f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 14 Oct 2013 16:30:17 +0200 Subject: [PATCH 022/162] disallow -0 year --- DynareFlex.ll | 2 +- macro/MacroFlex.ll | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index c836be7f..a5a3f6d7 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -690,7 +690,7 @@ string eofbuff; return token::INT_NUMBER; } -dates[[:space:]]*\([[:space:]]*\'[[:space:]]*-?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { +dates[[:space:]]*\([[:space:]]*\'[[:space:]]*(-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 792efce7..9235cf3b 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -61,7 +61,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ -DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) +DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ From 1cd7ed1f03c80d55254a6c6e3ba3136e16076c7d Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 14 Oct 2013 18:16:07 +0200 Subject: [PATCH 023/162] macroprocessor: if you encounter dates(date), insert quotes --- macro/MacroFlex.ll | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 9235cf3b..09c04da0 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,7 +51,8 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x END_DATE +%x DATE_MATCH +%x CLOSE_DATE %{ // Increments location counter for every token read @@ -92,8 +93,10 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- {DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } ${DATE} { yylloc->step(); *yyout << yytext + 1; } -dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } -{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } +dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; BEGIN(DATE_MATCH); } +\'{SPC}*{DATE}{SPC}*\' { yylloc->step(); *yyout << yytext; BEGIN(CLOSE_DATE); } +{DATE} { yylloc->step(); *yyout << "'" << yytext << "'"; BEGIN(CLOSE_DATE); } +{SPC}*\) { yylloc->step(); *yyout << ")"; BEGIN(INITIAL); } \} { BEGIN(INITIAL); return token::EOL; } From 9ecd0f2b4113c273a776a9539d30b3b4b3a5f44b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 10 Oct 2013 17:53:06 +0200 Subject: [PATCH 024/162] macroprocessor: substitute dates, don't substitute dates preceded by $ or dates(' --- macro/MacroFlex.ll | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 05a60329..792efce7 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,6 +51,7 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY +%x END_DATE %{ // Increments location counter for every token read @@ -60,6 +61,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ +DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ @@ -87,6 +89,12 @@ CONT \\\\ ^{SPC}*@# { yylloc->step(); BEGIN(STMT); } @\{ { yylloc->step(); BEGIN(EXPR); } +{DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } +${DATE} { yylloc->step(); *yyout << yytext + 1; } + +dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } +{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } + \} { BEGIN(INITIAL); return token::EOL; } {CONT}{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); } From e19da3777c87c43a3596cbaadf72be2faab11678 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 11 Oct 2013 16:28:14 +0200 Subject: [PATCH 025/162] preprocessor: only accept dates of the form: dates('date') in mod file --- ComputingTasks.cc | 8 ++++---- DynareBison.yy | 14 +++++--------- DynareFlex.ll | 2 +- ParsingDriver.cc | 5 +---- Statement.cc | 4 ++-- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 458a1db2..70d20bf5 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1767,10 +1767,10 @@ SubsamplesStatement::writeOutput(ostream &output, const string &basename) const it != subsample_declaration_map.end(); it++, map_indx++) output << "estimation_info.subsamples(subsamples_indx).range_index(" << map_indx << ") = {'" << it->first << "'};" << endl - << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date1 = dynDate('" - << it->second.first << "');" << endl - << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date2 = dynDate('" - << it->second.second << "');" << endl; + << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date1 = " + << it->second.first << endl + << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date2 = " + << it->second.second << endl; // Initialize associated subsample substructures in estimation_info const SymbolType symb_type = symbol_table.getType(name1); diff --git a/DynareBison.yy b/DynareBison.yy index 85663273..a53fa0d3 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -178,7 +178,7 @@ class ParsingDriver; %type expression expression_or_empty %type equation hand_side -%type non_negative_number signed_number signed_integer date_number +%type non_negative_number signed_number signed_integer %type filename symbol vec_of_vec_value vec_value_list %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf @@ -1024,10 +1024,6 @@ non_negative_number : INT_NUMBER | FLOAT_NUMBER ; -date_number : DATE_NUMBER - | INT_NUMBER - ; - signed_number : PLUS non_negative_number { $$ = $2; } | MINUS non_negative_number @@ -1243,7 +1239,7 @@ prior_pdf : BETA_PDF { $$ = eInvGamma2; } ; -set_time : SET_TIME '(' date_number ')' ';' +set_time : SET_TIME '(' DATE_NUMBER ')' ';' { driver.set_time($3); } ; @@ -2295,10 +2291,10 @@ o_conditional_variance_decomposition : CONDITIONAL_VARIANCE_DECOMPOSITION EQUAL { driver.option_vec_int("conditional_variance_decomposition", $3); } ; o_first_obs : FIRST_OBS EQUAL INT_NUMBER { driver.option_num("first_obs", $3); }; -o_new_estimation_data_first_obs : FIRST_OBS EQUAL date_number +o_new_estimation_data_first_obs : FIRST_OBS EQUAL DATE_NUMBER { driver.option_date("first_obs", $3); } ; -o_last_obs : LAST_OBS EQUAL date_number +o_last_obs : LAST_OBS EQUAL DATE_NUMBER { driver.option_date("last_obs", $3); } ; o_shift : SHIFT EQUAL signed_number { driver.option_num("shift", $3); }; @@ -2342,7 +2338,7 @@ list_allowed_graph_formats : allowed_graph_formats | list_allowed_graph_formats COMMA allowed_graph_formats ; -o_subsample_name : symbol EQUAL date_number ':' date_number +o_subsample_name : symbol EQUAL DATE_NUMBER ':' DATE_NUMBER { driver.set_subsample_name_equal_to_date_range($1, $3, $5); } ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index ad894a6e..c836be7f 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -690,7 +690,7 @@ string eofbuff; return token::INT_NUMBER; } --?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) { +dates[[:space:]]*\([[:space:]]*\'[[:space:]]*-?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 38bf5a78..31d5fbfa 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1226,10 +1226,7 @@ ParsingDriver::set_unit_root_vars() void ParsingDriver::set_time(string *arg) { - string arg1 = *arg; - for (size_t i=0; iaddStatement(new SetTimeStatement(options_list)); options_list.clear(); } diff --git a/Statement.cc b/Statement.cc index 59021287..76b600b7 100644 --- a/Statement.cc +++ b/Statement.cc @@ -94,7 +94,7 @@ OptionsList::writeOutput(ostream &output) const for (date_options_t::const_iterator it = date_options.begin(); it != date_options.end(); it++) - output << "options_." << it->first << " = dynDate('" << it->second << "');" << endl; + output << "options_." << it->first << " = " << it->second << ";" << endl; for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) @@ -137,7 +137,7 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const for (date_options_t::const_iterator it = date_options.begin(); it != date_options.end(); it++) - output << option_group << "." << it->first << " = dynDate('" << it->second << "');" << endl; + output << option_group << "." << it->first << " = " << it->second << ";" << endl; for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) From f3ef1f583f278c63a11310b9086563f3945e07d4 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 14 Oct 2013 16:30:17 +0200 Subject: [PATCH 026/162] disallow -0 year --- DynareFlex.ll | 2 +- macro/MacroFlex.ll | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index c836be7f..a5a3f6d7 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -690,7 +690,7 @@ string eofbuff; return token::INT_NUMBER; } -dates[[:space:]]*\([[:space:]]*\'[[:space:]]*-?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { +dates[[:space:]]*\([[:space:]]*\'[[:space:]]*(-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { yylval->string_val = new string(yytext); return token::DATE_NUMBER; } diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 792efce7..9235cf3b 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -61,7 +61,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ -DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) +DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ From 5b0bde1b8d6427b168fb3a89b4df9831660ff9ca Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 14 Oct 2013 18:16:07 +0200 Subject: [PATCH 027/162] macroprocessor: if you encounter dates(date), insert quotes --- macro/MacroFlex.ll | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 9235cf3b..09c04da0 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,7 +51,8 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x END_DATE +%x DATE_MATCH +%x CLOSE_DATE %{ // Increments location counter for every token read @@ -92,8 +93,10 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- {DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } ${DATE} { yylloc->step(); *yyout << yytext + 1; } -dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } -{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } +dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; BEGIN(DATE_MATCH); } +\'{SPC}*{DATE}{SPC}*\' { yylloc->step(); *yyout << yytext; BEGIN(CLOSE_DATE); } +{DATE} { yylloc->step(); *yyout << "'" << yytext << "'"; BEGIN(CLOSE_DATE); } +{SPC}*\) { yylloc->step(); *yyout << ")"; BEGIN(INITIAL); } \} { BEGIN(INITIAL); return token::EOL; } From 72effd0156ed738ad82abf1c2f03741894b74f3a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 21 Oct 2013 17:44:57 +0200 Subject: [PATCH 028/162] macroprocessor: copy anything within "dates()" straight to preprocessing stage --- macro/MacroDriver.hh | 2 ++ macro/MacroFlex.ll | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh index 0945a329..0ae177ff 100644 --- a/macro/MacroDriver.hh +++ b/macro/MacroDriver.hh @@ -86,6 +86,8 @@ private: //! If current context is the body of a loop, contains the location of the beginning of the body Macro::parser::location_type for_body_loc; + //! Temporary variable used for counting parens in dates statement + int dates_parens_nb; //! Temporary variable used in FOR_BODY mode string for_body_tmp; //! Temporary variable used in FOR_BODY mode diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 09c04da0..dc8fe29b 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,8 +51,7 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x DATE_MATCH -%x CLOSE_DATE +%x COPY_DATE_INFO %{ // Increments location counter for every token read @@ -93,10 +92,17 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- {DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } ${DATE} { yylloc->step(); *yyout << yytext + 1; } -dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; BEGIN(DATE_MATCH); } -\'{SPC}*{DATE}{SPC}*\' { yylloc->step(); *yyout << yytext; BEGIN(CLOSE_DATE); } -{DATE} { yylloc->step(); *yyout << "'" << yytext << "'"; BEGIN(CLOSE_DATE); } -{SPC}*\) { yylloc->step(); *yyout << ")"; BEGIN(INITIAL); } +dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; dates_parens_nb=1; BEGIN(COPY_DATE_INFO); } +<> { driver.error(*yylloc, "Unexpected end of file in dates statement"); } +{EOL} { yylloc->lines(1); yylloc->step(); } +[^()] { yylloc->step(); *yyout << yytext; } +\( { yylloc->step(); *yyout << yytext; dates_parens_nb++; } +\) { + yylloc->step(); + *yyout << yytext; + if (--dates_parens_nb == 0) + BEGIN(INITIAL); + } \} { BEGIN(INITIAL); return token::EOL; } From 447177d7712050d94a18c43b6dfdaf663f6997fa Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 23 Oct 2013 15:27:49 +0200 Subject: [PATCH 029/162] simplify flex code --- macro/MacroFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index dc8fe29b..25fb4dbf 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -95,7 +95,6 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; dates_parens_nb=1; BEGIN(COPY_DATE_INFO); } <> { driver.error(*yylloc, "Unexpected end of file in dates statement"); } {EOL} { yylloc->lines(1); yylloc->step(); } -[^()] { yylloc->step(); *yyout << yytext; } \( { yylloc->step(); *yyout << yytext; dates_parens_nb++; } \) { yylloc->step(); @@ -103,6 +102,7 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- if (--dates_parens_nb == 0) BEGIN(INITIAL); } +. { yylloc->step(); *yyout << yytext; } \} { BEGIN(INITIAL); return token::EOL; } From 7d3dcb74c9e632ffa6510b11112206e8e8f84451 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 22 Oct 2013 17:57:44 +0200 Subject: [PATCH 030/162] preprocessor: allow anything of the form dates(*) to be accepted as a valid date by the preprocessor --- DynareBison.yy | 23 +++++++++++++++-------- DynareFlex.ll | 28 ++++++++++++++++++---------- ParsingDriver.hh | 3 +++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index a53fa0d3..10be9dcb 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -99,14 +99,13 @@ class ParsingDriver; %token DATAFILE FILE DETERMINISTIC DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT EXTENDED_PATH ENDOGENOUS_PRIOR %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME -%token FLOAT_NUMBER +%token FLOAT_NUMBER DATES %token DEFAULT FIXED_POINT %token FORECAST K_ORDER_SOLVER INSTRUMENTS PRIOR SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN %token GAMMA_PDF GRAPH GRAPH_FORMAT CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID HYBRID %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT %token INT_NUMBER -%token DATE_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LYAPUNOV @@ -178,8 +177,8 @@ class ParsingDriver; %type expression expression_or_empty %type equation hand_side -%type non_negative_number signed_number signed_integer -%type filename symbol vec_of_vec_value vec_value_list +%type non_negative_number signed_number signed_integer date_str +%type filename symbol vec_of_vec_value vec_value_list date_expr %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf %type change_type_arg @@ -1239,7 +1238,15 @@ prior_pdf : BETA_PDF { $$ = eInvGamma2; } ; -set_time : SET_TIME '(' DATE_NUMBER ')' ';' +date_str : DATES { $$ = $1; } + +date_expr : date_str + { $$ = $1; } + | date_expr PLUS INT_NUMBER + { $$ = $1; $$->append("+").append(*$3); } + ; + +set_time : SET_TIME '(' date_expr ')' ';' { driver.set_time($3); } ; @@ -2291,10 +2298,10 @@ o_conditional_variance_decomposition : CONDITIONAL_VARIANCE_DECOMPOSITION EQUAL { driver.option_vec_int("conditional_variance_decomposition", $3); } ; o_first_obs : FIRST_OBS EQUAL INT_NUMBER { driver.option_num("first_obs", $3); }; -o_new_estimation_data_first_obs : FIRST_OBS EQUAL DATE_NUMBER +o_new_estimation_data_first_obs : FIRST_OBS EQUAL date_expr { driver.option_date("first_obs", $3); } ; -o_last_obs : LAST_OBS EQUAL DATE_NUMBER +o_last_obs : LAST_OBS EQUAL date_expr { driver.option_date("last_obs", $3); } ; o_shift : SHIFT EQUAL signed_number { driver.option_num("shift", $3); }; @@ -2338,7 +2345,7 @@ list_allowed_graph_formats : allowed_graph_formats | list_allowed_graph_formats COMMA allowed_graph_formats ; -o_subsample_name : symbol EQUAL DATE_NUMBER ':' DATE_NUMBER +o_subsample_name : symbol EQUAL date_expr ':' date_expr { driver.set_subsample_name_equal_to_date_range($1, $3, $5); } ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index a5a3f6d7..24955a76 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -60,6 +60,7 @@ string eofbuff; %x VERBATIM_BLOCK %x NATIVE %x NATIVE_COMMENT +%x DATES_STATEMENT %x LINE1 %x LINE2 %x LINE3 @@ -88,13 +89,13 @@ string eofbuff; } /* spaces, tabs and carriage returns are ignored */ -[ \t\r\f]+ { yylloc->step(); } -[\n]+ { yylloc->step(); } +[ \t\r\f]+ { yylloc->step(); } +[\n]+ { yylloc->step(); } /* Comments */ -["%"].* -["/"]["/"].* -"/*" {comment_caller = YY_START; BEGIN COMMENT;} +["%"].* +["/"]["/"].* +"/*" {comment_caller = YY_START; BEGIN COMMENT;} "*/" {BEGIN comment_caller;} . @@ -201,6 +202,7 @@ string eofbuff; corr {BEGIN DYNARE_STATEMENT; return token::CORR;} /* Inside of a Dynare statement */ +dates {dates_parens_nb=0; BEGIN DATES_STATEMENT; yylval->string_val = new string("dates");} file {return token::FILE;} datafile {return token::DATAFILE;} nobs {return token::NOBS;} @@ -690,10 +692,16 @@ string eofbuff; return token::INT_NUMBER; } -dates[[:space:]]*\([[:space:]]*\'[[:space:]]*(-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2]))[[:space:]]*\'[[:space:]]*\) { - yylval->string_val = new string(yytext); - return token::DATE_NUMBER; -} +\( { yylval->string_val->append(yytext); dates_parens_nb++; } +\) { + yylval->string_val->append(yytext); + if (--dates_parens_nb == 0) + { + BEGIN DYNARE_STATEMENT; + return token::DATES; + } + } +. { yylval->string_val->append(yytext); } \'[^\']+\' { yylval->string_val = new string(yytext + 1); @@ -786,7 +794,7 @@ string eofbuff; "*/"[[:space:]]*\n { BEGIN NATIVE; } . -<> { yyterminate(); } +<> { yyterminate(); } <*>. { driver.error(*yylloc, "character unrecognized by lexer"); } %% diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 8b9b21e9..8ad5a9f0 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -71,6 +71,9 @@ public: //! Increment the location counter given a token void location_increment(Dynare::parser::location_type *yylloc, const char *yytext); + + //! Count parens in dates statement + int dates_parens_nb; }; //! Drives the scanning and parsing of the .mod file, and constructs its abstract representation From 3706338bf4043143d196badeb5579101be70adb6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 23 Oct 2013 17:03:52 +0200 Subject: [PATCH 031/162] ms-sbvar: ms_simulation: add option to save mcmc draws --- DynareBison.yy | 4 +++- DynareFlex.ll | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index 85663273..7aabbd89 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -116,7 +116,7 @@ class ParsingDriver; %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS -%token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF +%token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS %token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED %token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE %token PRINT PRIOR_MC PRIOR_TRUNC PRIOR_MODE PRIOR_MEAN POSTERIOR_MODE POSTERIOR_MEAN POSTERIOR_MEDIAN PRUNING @@ -1991,6 +1991,7 @@ ms_simulation_option : o_output_file_tag | o_ms_drop | o_thinning_factor | o_adaptive_mh_draws + | o_save_draws ; ms_simulation_options_list : ms_simulation_option COMMA ms_simulation_options_list @@ -2652,6 +2653,7 @@ o_random_parameter_convergence_criterion : RANDOM_PARAMETER_CONVERGENCE_CRITERIO { driver.option_num("ms.random_parameter_convergence_criterion",$3); }; o_thinning_factor : THINNING_FACTOR EQUAL INT_NUMBER { driver.option_num("ms.thinning_factor",$3); }; o_adaptive_mh_draws : ADAPTIVE_MH_DRAWS EQUAL INT_NUMBER { driver.option_num("ms.adaptive_mh_draws",$3); }; +o_save_draws : SAVE_DRAWS { driver.option_num("ms.save_draws","1"); }; o_proposal_draws : PROPOSAL_DRAWS EQUAL INT_NUMBER { driver.option_num("ms.proposal_draws",$3); }; o_use_mean_center : USE_MEAN_CENTER { driver.option_num("ms.use_mean_center","1"); }; o_proposal_type : PROPOSAL_TYPE EQUAL INT_NUMBER { driver.option_num("ms.proposal_type",$3); } diff --git a/DynareFlex.ll b/DynareFlex.ll index ad894a6e..b88a8617 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -473,6 +473,7 @@ string eofbuff; filter_decomposition {return token::FILTER_DECOMPOSITION; } selected_variables_only {return token::SELECTED_VARIABLES_ONLY; } pruning {return token::PRUNING; } +save_draws {return token::SAVE_DRAWS; } deflator {return token::DEFLATOR;} log_deflator {return token::LOG_DEFLATOR;} growth_factor {return token::GROWTH_FACTOR;} From b6c0850fb730464b886a7558da8cf50b8d181b2e Mon Sep 17 00:00:00 2001 From: ferhat Date: Mon, 28 Oct 2013 14:22:23 +0100 Subject: [PATCH 032/162] Add a flag for calib_smoother command and apply a setup similar to the one used for stoch_simul command: - Add auxiliary variables for leaded and lagged exogenous - Set the cutoff applied to the Jacobian at zero for the static and the dynamic models --- ComputingTasks.cc | 6 ++++++ ComputingTasks.hh | 1 + ModFile.cc | 15 ++++++++++----- Statement.cc | 3 ++- Statement.hh | 2 ++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 458a1db2..9434b315 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2411,6 +2411,12 @@ CalibSmootherStatement::CalibSmootherStatement(const SymbolList &symbol_list_arg { } +void +CalibSmootherStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + mod_file_struct.calib_smoother_present = true; +} + void CalibSmootherStatement::writeOutput(ostream &output, const string &basename) const { diff --git a/ComputingTasks.hh b/ComputingTasks.hh index 926e082d..59c6a264 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -516,6 +516,7 @@ private: public: CalibSmootherStatement(const SymbolList &symbol_list_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; }; diff --git a/ModFile.cc b/ModFile.cc index 4ef0ab43..631dc4fa 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -123,7 +123,8 @@ ModFile::checkPass() || mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.ramsey_policy_present - || mod_file_struct.discretionary_policy_present; + || mod_file_struct.discretionary_policy_present + || mod_file_struct.calib_smoother_present; // Allow empty model only when doing a standalone BVAR estimation if (dynamic_model.equation_number() == 0 @@ -318,7 +319,8 @@ ModFile::transformPass(bool nostrict) || mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.ramsey_policy_present - || mod_file_struct.discretionary_policy_present) + || mod_file_struct.discretionary_policy_present + || mod_file_struct.calib_smoother_present) { // In stochastic models, create auxiliary vars for leads and lags greater than 2, on both endos and exos dynamic_model.substituteEndoLeadGreaterThanTwo(false); @@ -423,7 +425,8 @@ ModFile::computingPass(bool no_tmp_terms) { if (mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present) + || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.calib_smoother_present) static_model.set_cutoff_to_zero(); const bool static_hessian = mod_file_struct.identification_present @@ -437,7 +440,8 @@ ModFile::computingPass(bool no_tmp_terms) if (mod_file_struct.simul_present || mod_file_struct.check_present || mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present) + || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.calib_smoother_present) { if (mod_file_struct.simul_present) dynamic_model.computingPass(true, false, false, false, global_eval_context, no_tmp_terms, block, use_dll, byte_code); @@ -445,7 +449,8 @@ ModFile::computingPass(bool no_tmp_terms) { if (mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present) + || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.calib_smoother_present) dynamic_model.set_cutoff_to_zero(); if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3) { diff --git a/Statement.cc b/Statement.cc index 59021287..af77dc24 100644 --- a/Statement.cc +++ b/Statement.cc @@ -47,7 +47,8 @@ ModFileStructure::ModFileStructure() : dsge_var_estimated(false), bayesian_irf_present(false), estimation_data_statement_present(false), - last_markov_switching_chain(0) + last_markov_switching_chain(0), + calib_smoother_present(false) { } diff --git a/Statement.hh b/Statement.hh index cd3fd5cf..493dabfa 100644 --- a/Statement.hh +++ b/Statement.hh @@ -57,6 +57,8 @@ public: bool planner_objective_present; //! Whether an extended_path statement is present bool extended_path_present; + //! Whether a calib_smoother statement is present + bool calib_smoother_present; //! The value of the "order" option of stoch_simul, estimation, osr, ramsey_policy //! Derivation order /*! First initialized to zero. If user sets order option somewhere in the MOD file, it will be equal to the maximum of order options. Otherwise will default to 2 */ From b21252b8951a32b6d0e8f8e3dad57cfe5a933ee6 Mon Sep 17 00:00:00 2001 From: ferhat Date: Mon, 28 Oct 2013 16:41:21 +0100 Subject: [PATCH 033/162] Comment the trend test for a balance growth path => problematic with eLogTrend --- ModFile.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 631dc4fa..61b551d4 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -416,8 +416,8 @@ ModFile::computingPass(bool no_tmp_terms) // Mod file may have no equation (for example in a standalone BVAR estimation) if (dynamic_model.equation_number() > 0) { - if (nonstationary_variables) - trend_dynamic_model.runTrendTest(global_eval_context); + /*if (nonstationary_variables) + trend_dynamic_model.runTrendTest(global_eval_context);*/ // Compute static model and its derivatives dynamic_model.toStatic(static_model); From 46c7622f1cd7a2d4c8a87696873e17d679353969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 29 Oct 2013 18:46:54 +0800 Subject: [PATCH 034/162] Reactivate and fix the test for the balanced growth path. If the homogeneized equation evaluates to zero, then we skip the test (otherwise the 2nd derivative of the log is infinite, and the test fails while it should not necessarily). Closes #506 --- DynamicModel.cc | 39 +++++++++++++++++++++++---------------- ModFile.cc | 4 ++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index d19727fa..2fd7f54c 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -3757,22 +3757,28 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context || symbol_table.getType(it->first.first) == eLogTrend) for (int eq = 0; eq < (int) equations.size(); eq++) { - expr_t testeq = AddLog(AddMinus(equations[eq]->get_arg1(), // F: a = b -> ln(a - b) - equations[eq]->get_arg2())); - testeq = testeq->getDerivative(it->second); // d F / d Trend - for (deriv_id_table_t::const_iterator endogit = deriv_id_table.begin(); - endogit != deriv_id_table.end(); endogit++) - if (symbol_table.getType(endogit->first.first) == eEndogenous) - { - double nearZero = testeq->getDerivative(endogit->second)->eval(eval_context); // eval d F / d Trend d Endog - if (fabs(nearZero) > ZERO_BAND) + expr_t homogeneq = AddMinus(equations[eq]->get_arg1(), + equations[eq]->get_arg2()) + + // Do not run the test if the term inside the log is zero + if (fabs(homogeneq->eval(eval_context)) > ZERO_BAND) + { + expr_t testeq = AddLog(homogeneq); // F = log(lhs-rhs) + testeq = testeq->getDerivative(it->second); // d F / d Trend + for (deriv_id_table_t::const_iterator endogit = deriv_id_table.begin(); + endogit != deriv_id_table.end(); endogit++) + if (symbol_table.getType(endogit->first.first) == eEndogenous) { - cerr << "ERROR: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " w.r.t. trend variable " - << symbol_table.getName(it->first.first) << " and endogenous variable " - << symbol_table.getName(endogit->first.first) << " is not null. " << endl; - exit(EXIT_FAILURE); + double nearZero = testeq->getDerivative(endogit->second)->eval(eval_context); // eval d F / d Trend d Endog + if (fabs(nearZero) > ZERO_BAND) + { + cerr << "ERROR: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " w.r.t. trend variable " + << symbol_table.getName(it->first.first) << " and endogenous variable " + << symbol_table.getName(endogit->first.first) << " is not null. " << endl; + exit(EXIT_FAILURE); + } } - } + } } } @@ -4134,8 +4140,9 @@ DynamicModel::transformPredeterminedVariables() void DynamicModel::detrendEquations() { - for (nonstationary_symbols_map_t::const_iterator it = nonstationary_symbols_map.begin(); - it != nonstationary_symbols_map.end(); it++) + // We go backwards in the list of trend_vars, to deal correctly with I(2) processes + for (nonstationary_symbols_map_t::const_reverse_iterator it = nonstationary_symbols_map.rbegin(); + it != nonstationary_symbols_map.rend(); ++it) for (int i = 0; i < (int) equations.size(); i++) { BinaryOpNode *substeq = dynamic_cast(equations[i]->detrend(it->first, it->second.first, it->second.second)); diff --git a/ModFile.cc b/ModFile.cc index 61b551d4..631dc4fa 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -416,8 +416,8 @@ ModFile::computingPass(bool no_tmp_terms) // Mod file may have no equation (for example in a standalone BVAR estimation) if (dynamic_model.equation_number() > 0) { - /*if (nonstationary_variables) - trend_dynamic_model.runTrendTest(global_eval_context);*/ + if (nonstationary_variables) + trend_dynamic_model.runTrendTest(global_eval_context); // Compute static model and its derivatives dynamic_model.toStatic(static_model); From e487dab6bbdc1aab356e57ec33b660552e4dab99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 29 Oct 2013 18:47:59 +0800 Subject: [PATCH 035/162] Refuse non-stationary endogenous variables in deflators. It is not clear what we should do in that case anyways. --- ModelTree.cc | 7 +++++++ ModelTree.hh | 2 ++ ParsingDriver.cc | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/ModelTree.cc b/ModelTree.cc index 2a798017..362a5622 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -1567,3 +1567,10 @@ ModelTree::computeParamsDerivativesTemporaryTerms() it != hessian_params_derivatives.end(); ++it) it->second->computeTemporaryTerms(reference_count, params_derivs_temporary_terms, true); } + +bool ModelTree::isNonstationary(int symb_id) const +{ + return (nonstationary_symbols_map.find(symb_id) + != nonstationary_symbols_map.end()); +} + diff --git a/ModelTree.hh b/ModelTree.hh index 3a29aa90..30afd27d 100644 --- a/ModelTree.hh +++ b/ModelTree.hh @@ -307,6 +307,8 @@ public: void addTrendVariables(vector trend_vars, expr_t growth_factor) throw (TrendException); //! Adds a nonstationary variables with their (common) deflator void addNonstationaryVariables(vector nonstationary_vars, bool log_deflator, expr_t deflator) throw (TrendException); + //! Is a given variable non-stationary? + bool isNonstationary(int symb_id) const; void set_cutoff_to_zero(); //! Helper for writing the Jacobian elements in MATLAB and C /*! Writes either (i+1,j+1) or [i+j*no_eq] */ diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 38bf5a78..911ac6d1 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -357,6 +357,13 @@ ParsingDriver::end_nonstationary_var(bool log_deflator, expr_t deflator) { error("Variable " + e.name + " was listed more than once as following a trend."); } + + set > r; + deflator->collectVariables(eEndogenous, r); + for (set >::const_iterator it = r.begin(); it != r.end(); ++it) + if (dynamic_model->isNonstationary(it->first)) + error("The deflator contains a non-stationary endogenous variable. This is not allowed. Please use only stationary endogenous and/or {log_}trend_vars."); + declared_nonstationary_vars.clear(); reset_data_tree(); } From 114cb8bc25663f2106f3e3911c26ee46216c25ef Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 29 Oct 2013 12:38:28 +0100 Subject: [PATCH 036/162] missing semicolon --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 2fd7f54c..16219282 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -3758,7 +3758,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context for (int eq = 0; eq < (int) equations.size(); eq++) { expr_t homogeneq = AddMinus(equations[eq]->get_arg1(), - equations[eq]->get_arg2()) + equations[eq]->get_arg2()); // Do not run the test if the term inside the log is zero if (fabs(homogeneq->eval(eval_context)) > ZERO_BAND) From 4852c120996cc58a8c4e35ed0c7dfb25299f29ec Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 29 Oct 2013 12:47:40 +0100 Subject: [PATCH 037/162] fix header to remove compilation warning --- Statement.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Statement.hh b/Statement.hh index 493dabfa..ff0a53e0 100644 --- a/Statement.hh +++ b/Statement.hh @@ -57,8 +57,6 @@ public: bool planner_objective_present; //! Whether an extended_path statement is present bool extended_path_present; - //! Whether a calib_smoother statement is present - bool calib_smoother_present; //! The value of the "order" option of stoch_simul, estimation, osr, ramsey_policy //! Derivation order /*! First initialized to zero. If user sets order option somewhere in the MOD file, it will be equal to the maximum of order options. Otherwise will default to 2 */ @@ -99,6 +97,8 @@ public: bool estimation_data_statement_present; //! Last chain number for Markov Switching statement int last_markov_switching_chain; + //! Whether a calib_smoother statement is present + bool calib_smoother_present; }; class Statement From bd9e8acdabd80901145ad5a83a163ced75f2442f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 28 Oct 2013 15:29:41 +0100 Subject: [PATCH 038/162] preprocessor: move M_.Correlation_matrix and M_.Correlation_matrix_ME to preprocessor #392 --- ModFile.cc | 9 +++++++-- Shocks.cc | 13 ++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 631dc4fa..1abb76ce 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -537,15 +537,20 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b symbol_table.writeOutput(mOutputFile); - // Initialize M_.Sigma_e and M_.H + // Initialize M_.Sigma_e, M_.Correlation_matrix, M_.H, and M_.Correlation_matrix_ME mOutputFile << "M_.Sigma_e = zeros(" << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ");" << endl + << "M_.Correlation_matrix = eye(" << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ");" << endl; if (mod_file_struct.calibrated_measurement_errors) mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl + << "M_.Correlation_matrix_ME = eye(" << symbol_table.observedVariablesNbr() << ", " << symbol_table.observedVariablesNbr() << ");" << endl; else - mOutputFile << "M_.H = 0;" << endl; + mOutputFile << "M_.H = 0;" << endl + << "M_.Correlation_matrix_ME = 1;" << endl; if (linear == 1) mOutputFile << "options_.linear = 1;" << endl; diff --git a/Shocks.cc b/Shocks.cc index ab5b36df..1a943d2c 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -155,17 +155,19 @@ ShocksStatement::writeCovarOrCorrShock(ostream &output, covar_and_corr_shocks_t: SymbolType type2 = symbol_table.getType(it->first.second); assert((type1 == eExogenous && type2 == eExogenous) || (symbol_table.isObservedVariable(it->first.first) && symbol_table.isObservedVariable(it->first.second))); - string matrix; + string matrix, corr_matrix; int id1, id2; if (type1 == eExogenous) { matrix = "M_.Sigma_e"; + corr_matrix = "M_.Correlation_matrix"; id1 = symbol_table.getTypeSpecificID(it->first.first) + 1; id2 = symbol_table.getTypeSpecificID(it->first.second) + 1; } else { matrix = "M_.H"; + corr_matrix = "M_.Correlation_matrix_ME"; id1 = symbol_table.getObservedVariableIndex(it->first.first) + 1; id2 = symbol_table.getObservedVariableIndex(it->first.second) + 1; } @@ -178,6 +180,15 @@ ShocksStatement::writeCovarOrCorrShock(ostream &output, covar_and_corr_shocks_t: output << ";" << endl << matrix << "(" << id2 << ", " << id1 << ") = " << matrix << "(" << id1 << ", " << id2 << ");" << endl; + + if (corr) + { + output << corr_matrix << "(" << id1 << ", " << id2 << ") = "; + it->second->writeOutput(output); + output << ";" << endl + << corr_matrix << "(" << id2 << ", " << id1 << ") = " + << corr_matrix << "(" << id1 << ", " << id2 << ");" << endl; + } } void From 49060c3abd9fc214cc796e29f2d9644fd5b3d9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 4 Nov 2013 15:42:59 +0100 Subject: [PATCH 039/162] Allow a custom function named 'prior' as a value for 'mode_compute' option Closes #507 --- DynareBison.yy | 5 +++-- DynareFlex.ll | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 7aabbd89..913d0405 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -101,7 +101,7 @@ class ParsingDriver; %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME %token FLOAT_NUMBER %token DEFAULT FIXED_POINT -%token FORECAST K_ORDER_SOLVER INSTRUMENTS PRIOR SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN +%token FORECAST K_ORDER_SOLVER INSTRUMENTS SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN %token GAMMA_PDF GRAPH GRAPH_FORMAT CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID HYBRID %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT @@ -151,7 +151,7 @@ class ParsingDriver; %token VLISTLOG VLISTPER %token RESTRICTION RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST %token DUMMY_OBS NSTATES INDXSCALESSTATES NO_BAYESIAN_PRIOR SPECIFICATION SIMS_ZHA -%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE +%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE PRIOR %token GSIG2_LMDM Q_DIAG FLAT_PRIOR NCSK NSTD %token INDXPARR INDXOVR INDXAP APBAND INDXIMF IMFBAND INDXFORE FOREBAND INDXGFOREHAT INDXGIMFHAT %token INDXESTIMA INDXGDLS EQ_MS FILTER_COVARIANCE FILTER_DECOMPOSITION @@ -2795,6 +2795,7 @@ symbol : NAME | FIG | NONE | DR + | PRIOR ; %% diff --git a/DynareFlex.ll b/DynareFlex.ll index b88a8617..7ff3c1c9 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -196,7 +196,10 @@ string eofbuff; subsamples {return token::SUBSAMPLES;} options {return token::OPTIONS;} -prior {return token::PRIOR;} +prior { + yylval->string_val = new string(yytext); + return token::PRIOR; +} std {BEGIN DYNARE_STATEMENT; return token::STD;} corr {BEGIN DYNARE_STATEMENT; return token::CORR;} From 1634c7c50fc227e4ab6309f5f72425cfc5ca5e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 4 Nov 2013 16:02:28 +0100 Subject: [PATCH 040/162] Always initialize M_.maximum_{endo,exo}_{lead,lag} and M_.params Closes #515 --- DynamicModel.cc | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 16219282..8c47907c 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2913,26 +2913,23 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de output << "M_.exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl << "M_.maximum_lag = " << max_lag << ";" << endl << "M_.maximum_lead = " << max_lead << ";" << endl; - if (symbol_table.endo_nbr()) - { - output << "M_.maximum_endo_lag = " << max_endo_lag << ";" << endl - << "M_.maximum_endo_lead = " << max_endo_lead << ";" << endl - << "oo_.steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl; - } - if (symbol_table.exo_nbr()) - { - output << "M_.maximum_exo_lag = " << max_exo_lag << ";" << endl - << "M_.maximum_exo_lead = " << max_exo_lead << ";" << endl - << "oo_.exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl; - } + + output << "M_.maximum_endo_lag = " << max_endo_lag << ";" << endl + << "M_.maximum_endo_lead = " << max_endo_lead << ";" << endl + << "oo_.steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl; + + output << "M_.maximum_exo_lag = " << max_exo_lag << ";" << endl + << "M_.maximum_exo_lead = " << max_exo_lead << ";" << endl + << "oo_.exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl; + if (symbol_table.exo_det_nbr()) { output << "M_.maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl << "M_.maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl << "oo_.exo_det_steady_state = zeros(" << symbol_table.exo_det_nbr() << ", 1);" << endl; } - if (symbol_table.param_nbr()) - output << "M_.params = NaN(" << symbol_table.param_nbr() << ", 1);" << endl; + + output << "M_.params = NaN(" << symbol_table.param_nbr() << ", 1);" << endl; // Write number of non-zero derivatives // Use -1 if the derivatives have not been computed From b34a55b36a883ebb08eb3dc185641cf993b92d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 5 Nov 2013 17:16:32 +0100 Subject: [PATCH 041/162] Finish implementation of mcmc_jumping_covariance option Closes #112, #514 --- DynareBison.yy | 13 +++++++++++-- DynareFlex.ll | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 913d0405..77b82e25 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -113,7 +113,7 @@ class ParsingDriver; %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN -%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL +%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS @@ -151,7 +151,7 @@ class ParsingDriver; %token VLISTLOG VLISTPER %token RESTRICTION RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST %token DUMMY_OBS NSTATES INDXSCALESSTATES NO_BAYESIAN_PRIOR SPECIFICATION SIMS_ZHA -%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE PRIOR +%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE PRIOR PRIOR_VARIANCE HESSIAN IDENTITY_MATRIX %token GSIG2_LMDM Q_DIAG FLAT_PRIOR NCSK NSTD %token INDXPARR INDXOVR INDXAP APBAND INDXIMF IMFBAND INDXFORE FOREBAND INDXGFOREHAT INDXGIMFHAT %token INDXESTIMA INDXGDLS EQ_MS FILTER_COVARIANCE FILTER_DECOMPOSITION @@ -1558,6 +1558,7 @@ estimation_options : o_datafile | o_qz_zero_threshold | o_taper_steps | o_geweke_interval + | o_mcmc_jumping_covariance ; list_optim_option : QUOTED_STRING COMMA QUOTED_STRING @@ -2677,6 +2678,14 @@ o_discretionary_tol: DISCRETIONARY_TOL EQUAL non_negative_number { driver.option o_analytic_derivation : ANALYTIC_DERIVATION { driver.option_num("analytic_derivation", "1"); } o_endogenous_prior : ENDOGENOUS_PRIOR { driver.option_num("endogenous_prior", "1"); } o_use_univariate_filters_if_singularity_is_detected : USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED EQUAL INT_NUMBER { driver.option_num("use_univariate_filters_if_singularity_is_detected", $3); } +o_mcmc_jumping_covariance : MCMC_JUMPING_COVARIANCE EQUAL HESSIAN + { driver.option_str("MCMC_jumping_covariance", $3); } | MCMC_JUMPING_COVARIANCE EQUAL PRIOR_VARIANCE + { driver.option_str("MCMC_jumping_covariance", $3); } + | MCMC_JUMPING_COVARIANCE EQUAL IDENTITY_MATRIX + { driver.option_str("MCMC_jumping_covariance", $3); } + | MCMC_JUMPING_COVARIANCE EQUAL filename + { driver.option_str("MCMC_jumping_covariance", $3); } + ; range : symbol ':' symbol { diff --git a/DynareFlex.ll b/DynareFlex.ll index 7ff3c1c9..9e073081 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -439,6 +439,19 @@ string eofbuff; random_parameter_convergence_criterion {return token::RANDOM_PARAMETER_CONVERGENCE_CRITERION;} tolf {return token::TOLF;} instruments {return token::INSTRUMENTS;} +hessian { + yylval->string_val = new string(yytext); + return token::HESSIAN; +} +prior_variance { + yylval->string_val = new string(yytext); + return token::PRIOR_VARIANCE; +} +identity_matrix { + yylval->string_val = new string(yytext); + return token::IDENTITY_MATRIX; +} +mcmc_jumping_covariance {return token::MCMC_JUMPING_COVARIANCE;} /* These four (var, varexo, varexo_det, parameters) are for change_type */ var { return token::VAR; } From 1ae5832e1345a14e70a8217e001ea50903407443 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 5 Nov 2013 13:51:18 +0100 Subject: [PATCH 042/162] preprocessor and matlab mex fixes for clang compilation --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 8c47907c..5f6b5bbe 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -210,7 +210,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const ostringstream tmp_output, tmp1_output, global_output; expr_t lhs = NULL, rhs = NULL; BinaryOpNode *eq_node; - ostringstream Uf[symbol_table.endo_nbr()]; + vector Uf(symbol_table.endo_nbr()); map reference_count; temporary_terms_t local_temporary_terms; ofstream output; From 74ad93e0f461b9319f4d36dd4e46085c611b15bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 5 Nov 2013 18:13:24 +0100 Subject: [PATCH 043/162] Remove select1st (GNU extension), replace it by a call to boost::bind --- ExprNode.cc | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index e3a5bad8..45247ec1 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -21,15 +21,11 @@ #include #include -// For select1st() -#ifdef __GNUC__ -# include -using namespace __gnu_cxx; -#endif - #include #include +#include + #include "ExprNode.hh" #include "DataTree.hh" #include "ModFile.hh" @@ -110,7 +106,7 @@ ExprNode::collectModelLocalVariables(set &result) const set > symb_ids; collectVariables(eModelLocalVariable, symb_ids); transform(symb_ids.begin(), symb_ids.end(), inserter(result, result.begin()), - select1st >()); + boost::bind(&pair::first,_1)); } void From 6d6701aa50a17a29dc8a685e6915f7aa89271de7 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 5 Nov 2013 18:21:05 +0100 Subject: [PATCH 044/162] revert change that breaks gcc build --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 5f6b5bbe..8c47907c 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -210,7 +210,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const ostringstream tmp_output, tmp1_output, global_output; expr_t lhs = NULL, rhs = NULL; BinaryOpNode *eq_node; - vector Uf(symbol_table.endo_nbr()); + ostringstream Uf[symbol_table.endo_nbr()]; map reference_count; temporary_terms_t local_temporary_terms; ofstream output; From 8513c59c9ed06f07c7645dc12357139dacf513e5 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 6 Nov 2013 10:36:58 +0100 Subject: [PATCH 045/162] preprocessor: add use_calibration option for estimated_params_init block. closes #447, closes #512 --- ComputingTasks.cc | 9 +++++++-- ComputingTasks.hh | 4 +++- DynareBison.yy | 7 +++++-- DynareFlex.ll | 2 +- ParsingDriver.cc | 4 ++-- ParsingDriver.hh | 2 +- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 9434b315..ec201921 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -581,15 +581,20 @@ EstimatedParamsStatement::writeOutput(ostream &output, const string &basename) c } EstimatedParamsInitStatement::EstimatedParamsInitStatement(const vector &estim_params_list_arg, - const SymbolTable &symbol_table_arg) : + const SymbolTable &symbol_table_arg, + const bool use_calibration_arg) : estim_params_list(estim_params_list_arg), - symbol_table(symbol_table_arg) + symbol_table(symbol_table_arg), + use_calibration(use_calibration_arg) { } void EstimatedParamsInitStatement::writeOutput(ostream &output, const string &basename) const { + if (use_calibration) + output << "options_.use_calibration_initialization = 1;" << endl; + vector::const_iterator it; for (it = estim_params_list.begin(); it != estim_params_list.end(); it++) diff --git a/ComputingTasks.hh b/ComputingTasks.hh index 59c6a264..156a3421 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -290,9 +290,11 @@ class EstimatedParamsInitStatement : public Statement private: const vector estim_params_list; const SymbolTable &symbol_table; + const bool use_calibration; public: EstimatedParamsInitStatement(const vector &estim_params_list_arg, - const SymbolTable &symbol_table_arg); + const SymbolTable &symbol_table_arg, + const bool use_calibration_arg); virtual void writeOutput(ostream &output, const string &basename) const; }; diff --git a/DynareBison.yy b/DynareBison.yy index 77b82e25..f50b09ff 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -90,7 +90,7 @@ class ParsingDriver; %} %token AIM_SOLVER ANALYTIC_DERIVATION AR AUTOCORR -%token BAYESIAN_IRF BETA_PDF BLOCK +%token BAYESIAN_IRF BETA_PDF BLOCK USE_CALIBRATION %token BVAR_DENSITY BVAR_FORECAST %token BVAR_PRIOR_DECAY BVAR_PRIOR_FLAT BVAR_PRIOR_LAMBDA %token BVAR_PRIOR_MU BVAR_PRIOR_OMEGA BVAR_PRIOR_TAU BVAR_PRIOR_TRAIN @@ -1141,7 +1141,10 @@ estimated_elem3 : expression_or_empty COMMA expression_or_empty ; estimated_params_init : ESTIMATED_PARAMS_INIT ';' estimated_init_list END ';' - { driver.estimated_params_init(); }; + { driver.estimated_params_init(); } + | ESTIMATED_PARAMS_INIT '(' USE_CALIBRATION ')' ';' estimated_init_list END ';' + { driver.estimated_params_init(true); } + ; estimated_init_list : estimated_init_list estimated_init_elem { driver.add_estimated_params_element(); } diff --git a/DynareFlex.ll b/DynareFlex.ll index 9e073081..b53719bc 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -395,7 +395,7 @@ string eofbuff; return token::CNUM; } banact {return token::BANACT;} - +use_calibration {return token::USE_CALIBRATION;} output_file_tag {return token::OUTPUT_FILE_TAG;} file_tag {return token::FILE_TAG;}; no_create_init {return token::NO_CREATE_INIT;}; diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 911ac6d1..55bf7aed 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1209,9 +1209,9 @@ ParsingDriver::estimated_params() } void -ParsingDriver::estimated_params_init() +ParsingDriver::estimated_params_init(bool use_calibration) { - mod_file->addStatement(new EstimatedParamsInitStatement(estim_params_list, mod_file->symbol_table)); + mod_file->addStatement(new EstimatedParamsInitStatement(estim_params_list, mod_file->symbol_table, use_calibration)); estim_params_list.clear(); } diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 8b9b21e9..8466deb0 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -391,7 +391,7 @@ public: //! Writes estimated params command void estimated_params(); //! Writes estimated params init command - void estimated_params_init(); + void estimated_params_init(bool use_calibration = false); //! Writes estimated params bound command void estimated_params_bounds(); //! Adds a declaration for a user-defined external function From f964d06e223eca0e0ec9cb4c1189ee272902fab3 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 13 Nov 2013 11:29:27 +0100 Subject: [PATCH 046/162] saves variables if they exist at end of dynare run. closes #517 --- ModFile.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ModFile.cc b/ModFile.cc index 1abb76ce..1a1b7e3e 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -703,7 +703,15 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b if (block && !byte_code) mOutputFile << "rmpath " << basename << ";" << endl; - mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl; + mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl + << "if exist('estim_params_', 'var') == 1" << endl + << " save('" << basename << "_results.mat', 'estim_params_', '-append');" << endl << "end" << endl + << "if exist('bayestopt_', 'var') == 1" << endl + << " save('" << basename << "_results.mat', 'bayestopt_', '-append');" << endl << "end" << endl + << "if exist('dataset_', 'var') == 1" << endl + << " save('" << basename << "_results.mat', 'dataset_', '-append');" << endl << "end" << endl + << "if exist('estimation_info', 'var') == 1" << endl + << " save('" << basename << "_results.mat', 'estimation_info', '-append');" << endl << "end" << endl; config_file.writeEndParallel(mOutputFile); From eabad03581b56972fe6748ba00b3459ba2c89f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 13 Nov 2013 14:34:46 +0100 Subject: [PATCH 047/162] Preprocessor interface for irf_plot_threshold Closes #498 --- DynareBison.yy | 5 ++++- DynareFlex.ll | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index f50b09ff..7b047031 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -107,7 +107,7 @@ class ParsingDriver; %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT %token INT_NUMBER %token DATE_NUMBER -%token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS +%token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS IRF_PLOT_THRESHOLD %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT @@ -991,6 +991,7 @@ stoch_simul_primary_options : o_dr_algo | o_dr_cycle_reduction_tol | o_dr_logarithmic_reduction_tol | o_dr_logarithmic_reduction_maxiter + | o_irf_plot_threshold ; stoch_simul_options : stoch_simul_primary_options @@ -1562,6 +1563,7 @@ estimation_options : o_datafile | o_taper_steps | o_geweke_interval | o_mcmc_jumping_covariance + | o_irf_plot_threshold ; list_optim_option : QUOTED_STRING COMMA QUOTED_STRING @@ -2689,6 +2691,7 @@ o_mcmc_jumping_covariance : MCMC_JUMPING_COVARIANCE EQUAL HESSIAN | MCMC_JUMPING_COVARIANCE EQUAL filename { driver.option_str("MCMC_jumping_covariance", $3); } ; +o_irf_plot_threshold : IRF_PLOT_THRESHOLD EQUAL non_negative_number { driver.option_num("impulse_responses.plot_threshold", $3); }; range : symbol ':' symbol { diff --git a/DynareFlex.ll b/DynareFlex.ll index b53719bc..c58ca30a 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -567,6 +567,7 @@ string eofbuff; mh_recover {return token::MH_RECOVER;} planner_discount {return token::PLANNER_DISCOUNT;} calibration {return token::CALIBRATION;} +irf_plot_threshold {return token::IRF_PLOT_THRESHOLD;} equation {return token::EQUATION;} exclusion {return token::EXCLUSION;} From d368a501d7e5f69da401a58ce888090d2790fcd8 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 15 Nov 2013 09:46:18 +0100 Subject: [PATCH 048/162] preprocessor: support long name for endogenous, #478 --- DynareBison.yy | 20 ++++++++++++++++++-- DynareFlex.ll | 2 ++ ParsingDriver.cc | 44 +++++++++++++++++++++++++++----------------- ParsingDriver.hh | 6 +++--- SymbolTable.cc | 13 +++++++++++-- SymbolTable.hh | 16 ++++++++++++++++ 6 files changed, 77 insertions(+), 24 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 7b047031..a12a7ad0 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -131,7 +131,7 @@ class ParsingDriver; %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED %token VALUES VAR VAREXO VAREXO_DET VAROBS PREDETERMINED_VARIABLES %token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL -%token XLS_SHEET XLS_RANGE +%token XLS_SHEET XLS_RANGE LONG_NAME %left COMMA %left EQUAL_EQUAL EXCLAMATION_EQUAL %left LESS GREATER LESS_EQUAL GREATER_EQUAL @@ -181,7 +181,7 @@ class ParsingDriver; %type non_negative_number signed_number signed_integer date_number %type filename symbol vec_of_vec_value vec_value_list %type vec_value_1 vec_value signed_inf signed_number_w_inf -%type range vec_value_w_inf vec_value_1_w_inf +%type range vec_value_w_inf vec_value_1_w_inf named_var %type change_type_arg %type change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt %type vec_int_elem vec_int_1 vec_int vec_int_number @@ -338,6 +338,12 @@ nonstationary_var_list : nonstationary_var_list symbol { driver.declare_nonstationary_var($3, $4); } | symbol TEX_NAME { driver.declare_nonstationary_var($1, $2); } + | nonstationary_var_list symbol TEX_NAME named_var + { driver.declare_nonstationary_var($2, $3, $4); } + | nonstationary_var_list COMMA symbol TEX_NAME named_var + { driver.declare_nonstationary_var($3, $4, $5); } + | symbol TEX_NAME named_var + { driver.declare_nonstationary_var($1, $2, $3); } ; varexo : VAREXO varexo_list ';'; @@ -348,6 +354,10 @@ predetermined_variables : PREDETERMINED_VARIABLES predetermined_variables_list ' parameters : PARAMETERS parameter_list ';'; +named_var : '(' LONG_NAME EQUAL QUOTED_STRING ')' + { $$ = $4; } + ; + var_list : var_list symbol { driver.declare_endogenous($2); } | var_list COMMA symbol @@ -360,6 +370,12 @@ var_list : var_list symbol { driver.declare_endogenous($3, $4); } | symbol TEX_NAME { driver.declare_endogenous($1, $2); } + | var_list symbol TEX_NAME named_var + { driver.declare_endogenous($2, $3, $4); } + | var_list COMMA symbol TEX_NAME named_var + { driver.declare_endogenous($3, $4, $5); } + | symbol TEX_NAME named_var + { driver.declare_endogenous($1, $2, $3); } ; varexo_list : varexo_list symbol diff --git a/DynareFlex.ll b/DynareFlex.ll index c58ca30a..31fb72c2 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -499,6 +499,8 @@ string eofbuff; analytic_derivation {return token::ANALYTIC_DERIVATION;} solver_periods {return token::SOLVER_PERIODS;} endogenous_prior {return token::ENDOGENOUS_PRIOR;} +long_name {return token::LONG_NAME;} + [\$][^$]*[\$] { strtok(yytext+1, "$"); diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 55bf7aed..6d389865 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -131,14 +131,17 @@ ParsingDriver::warning(const string &m) } void -ParsingDriver::declare_symbol(const string *name, SymbolType type, const string *tex_name) +ParsingDriver::declare_symbol(const string *name, SymbolType type, const string *tex_name, const string *long_name) { try { - if (tex_name == NULL) + if (tex_name == NULL && long_name == NULL) mod_file->symbol_table.addSymbol(*name, type); else - mod_file->symbol_table.addSymbol(*name, type, *tex_name); + if (long_name == NULL) + mod_file->symbol_table.addSymbol(*name, type, *tex_name); + else + mod_file->symbol_table.addSymbol(*name, type, *tex_name, *long_name); } catch (SymbolTable::AlreadyDeclaredException &e) { @@ -150,18 +153,20 @@ ParsingDriver::declare_symbol(const string *name, SymbolType type, const string } void -ParsingDriver::declare_endogenous(string *name, string *tex_name) +ParsingDriver::declare_endogenous(string *name, string *tex_name, string *long_name) { - declare_symbol(name, eEndogenous, tex_name); + declare_symbol(name, eEndogenous, tex_name, long_name); delete name; if (tex_name != NULL) delete tex_name; + if (long_name != NULL) + delete long_name; } void ParsingDriver::declare_exogenous(string *name, string *tex_name) { - declare_symbol(name, eExogenous, tex_name); + declare_symbol(name, eExogenous, tex_name, NULL); delete name; if (tex_name != NULL) delete tex_name; @@ -170,7 +175,7 @@ ParsingDriver::declare_exogenous(string *name, string *tex_name) void ParsingDriver::declare_exogenous_det(string *name, string *tex_name) { - declare_symbol(name, eExogenousDet, tex_name); + declare_symbol(name, eExogenousDet, tex_name, NULL); delete name; if (tex_name != NULL) delete tex_name; @@ -179,7 +184,7 @@ ParsingDriver::declare_exogenous_det(string *name, string *tex_name) void ParsingDriver::declare_parameter(string *name, string *tex_name) { - declare_symbol(name, eParameter, tex_name); + declare_symbol(name, eParameter, tex_name, NULL); delete name; if (tex_name != NULL) delete tex_name; @@ -191,7 +196,7 @@ ParsingDriver::declare_statement_local_variable(string *name) if (mod_file->symbol_table.exists(*name)) error("Symbol " + *name + " cannot be assigned within a statement " + "while being assigned elsewhere in the modfile"); - declare_symbol(name, eStatementDeclaredVariable, NULL); + declare_symbol(name, eStatementDeclaredVariable, NULL, NULL); delete name; } @@ -215,7 +220,7 @@ ParsingDriver::begin_trend() void ParsingDriver::declare_trend_var(bool log_trend, string *name, string *tex_name) { - declare_symbol(name, log_trend ? eLogTrend : eTrend, tex_name); + declare_symbol(name, log_trend ? eLogTrend : eTrend, tex_name, NULL); declared_trend_vars.push_back(mod_file->symbol_table.getID(*name)); delete name; if (tex_name != NULL) @@ -333,17 +338,22 @@ ParsingDriver::add_expression_variable(string *name) } void -ParsingDriver::declare_nonstationary_var(string *name, string *tex_name) +ParsingDriver::declare_nonstationary_var(string *name, string *tex_name, string *long_name) { if (tex_name != NULL) - declare_endogenous(new string(*name), new string(*tex_name)); + if (long_name != NULL) + declare_endogenous(new string(*name), new string(*tex_name), new string(*long_name)); + else + declare_endogenous(new string(*name), new string(*tex_name)); else - declare_endogenous(new string(*name), tex_name); + declare_endogenous(new string(*name)); declared_nonstationary_vars.push_back(mod_file->symbol_table.getID(*name)); mod_file->nonstationary_variables = true; delete name; if (tex_name != NULL) delete tex_name; + if (long_name != NULL) + delete long_name; } void @@ -2295,7 +2305,7 @@ ParsingDriver::external_function_option(const string &name_option, const string { if (opt.empty()) error("An argument must be passed to the 'name' option of the external_function() statement."); - declare_symbol(&opt, eExternalFunction, NULL); + declare_symbol(&opt, eExternalFunction, NULL, NULL); current_external_function_id = mod_file->symbol_table.getID(opt); } else if (name_option == "first_deriv_provided") @@ -2304,7 +2314,7 @@ ParsingDriver::external_function_option(const string &name_option, const string current_external_function_options.firstDerivSymbID = eExtFunSetButNoNameProvided; else { - declare_symbol(&opt, eExternalFunction, NULL); + declare_symbol(&opt, eExternalFunction, NULL, NULL); current_external_function_options.firstDerivSymbID = mod_file->symbol_table.getID(opt); } } @@ -2314,7 +2324,7 @@ ParsingDriver::external_function_option(const string &name_option, const string current_external_function_options.secondDerivSymbID = eExtFunSetButNoNameProvided; else { - declare_symbol(&opt, eExternalFunction, NULL); + declare_symbol(&opt, eExternalFunction, NULL, NULL); current_external_function_options.secondDerivSymbID = mod_file->symbol_table.getID(opt); } } @@ -2438,7 +2448,7 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in if (in_model_block) error("To use an external function (" + *function_name + ") within the model block, you must first declare it via the external_function() statement."); - declare_symbol(function_name, eExternalFunction, NULL); + declare_symbol(function_name, eExternalFunction, NULL, NULL); current_external_function_options.nargs = stack_external_function_args.top().size(); mod_file->external_functions_table.addExternalFunction(mod_file->symbol_table.getID(*function_name), current_external_function_options, in_model_block); diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 8466deb0..db19f2b0 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -91,7 +91,7 @@ private: void check_symbol_is_endogenous_or_exogenous(string *name); //! Helper to add a symbol declaration - void declare_symbol(const string *name, SymbolType type, const string *tex_name); + void declare_symbol(const string *name, SymbolType type, const string *tex_name, const string *long_name); //! Creates option "optim_opt" in OptionsList if it doesn't exist, else add a comma, and adds the option name void optim_options_helper(const string &name); @@ -258,7 +258,7 @@ public: //! Sets the FILENAME for the initial value in initval void initval_file(string *filename); //! Declares an endogenous variable - void declare_endogenous(string *name, string *tex_name = NULL); + void declare_endogenous(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares an exogenous variable void declare_exogenous(string *name, string *tex_name = NULL); //! Declares an exogenous deterministic variable @@ -641,7 +641,7 @@ public: //! Ends declaration of trend variable void end_trend_var(expr_t growth_factor); //! Declares a nonstationary variable with its deflator - void declare_nonstationary_var(string *name, string *tex_name = NULL); + void declare_nonstationary_var(string *name, string *tex_name = NULL, string *long_name = NULL); //! Ends declaration of nonstationary variable void end_nonstationary_var(bool log_deflator, expr_t deflator); //! Add a graph format to the list of formats requested diff --git a/SymbolTable.cc b/SymbolTable.cc index b90b1ce5..039024e5 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -39,7 +39,7 @@ SymbolTable::SymbolTable() : frozen(false), size(0) } int -SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException) +SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name, const string &long_name) throw (AlreadyDeclaredException, FrozenException) { if (frozen) throw FrozenException(); @@ -58,10 +58,17 @@ SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_na type_table.push_back(type); name_table.push_back(name); tex_name_table.push_back(tex_name); + long_name_table.push_back(long_name); return id; } +int +SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException) +{ + return addSymbol(name, type, tex_name, name); +} + int SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException) { @@ -188,10 +195,12 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) { output << "M_.endo_names = '" << getName(endo_ids[0]) << "';" << endl; output << "M_.endo_names_tex = '" << getTeXName(endo_ids[0]) << "';" << endl; + output << "M_.endo_names_long = '" << getLongName(endo_ids[0]) << "';" << endl; for (int id = 1; id < endo_nbr(); id++) { output << "M_.endo_names = char(M_.endo_names, '" << getName(endo_ids[id]) << "');" << endl - << "M_.endo_names_tex = char(M_.endo_names_tex, '" << getTeXName(endo_ids[id]) << "');" << endl; + << "M_.endo_names_tex = char(M_.endo_names_tex, '" << getTeXName(endo_ids[id]) << "');" << endl + << "M_.endo_names_long = char(M_.endo_names_long, '" << getLongName(endo_ids[id]) << "');" << endl; } } if (param_nbr() > 0) diff --git a/SymbolTable.hh b/SymbolTable.hh index a8a032f5..dfc1e0ae 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -87,6 +87,8 @@ private: vector name_table; //! Maps IDs to TeX names vector tex_name_table; + //! Maps IDs to string names of variables + vector long_name_table; //! Maps IDs to types vector type_table; @@ -182,6 +184,9 @@ private: public: //! Add a symbol /*! Returns the symbol ID */ + int addSymbol(const string &name, SymbolType type, const string &tex_name, const string &long_name) throw (AlreadyDeclaredException, FrozenException); + //! Add a symbol without its long name (will be equal to its name) + /*! Returns the symbol ID */ int addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException); //! Add a symbol without its TeX name (will be equal to its name) /*! Returns the symbol ID */ @@ -242,6 +247,8 @@ public: inline string getName(int id) const throw (UnknownSymbolIDException); //! Get TeX name inline string getTeXName(int id) const throw (UnknownSymbolIDException); + //! Get long name + inline string getLongName(int id) const throw (UnknownSymbolIDException); //! Get type (by ID) inline SymbolType getType(int id) const throw (UnknownSymbolIDException); //! Get type (by name) @@ -318,6 +325,15 @@ SymbolTable::getTeXName(int id) const throw (UnknownSymbolIDException) return tex_name_table[id]; } +inline string +SymbolTable::getLongName(int id) const throw (UnknownSymbolIDException) +{ + if (id < 0 || id >= size) + throw UnknownSymbolIDException(id); + else + return long_name_table[id]; +} + inline SymbolType SymbolTable::getType(int id) const throw (UnknownSymbolIDException) { From 8b34189bc3966d604944b513d298f89f5c3b3b40 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 15 Nov 2013 10:39:00 +0100 Subject: [PATCH 049/162] preprocessor: support long name for exogenous, #478 --- DynareBison.yy | 12 ++++++++++++ ParsingDriver.cc | 12 ++++++++---- ParsingDriver.hh | 4 ++-- SymbolTable.cc | 8 ++++++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index a12a7ad0..08052f77 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -390,6 +390,12 @@ varexo_list : varexo_list symbol { driver.declare_exogenous($3, $4); } | symbol TEX_NAME { driver.declare_exogenous($1, $2); } + | varexo_list symbol TEX_NAME named_var + { driver.declare_exogenous($2, $3, $4); } + | varexo_list COMMA symbol TEX_NAME named_var + { driver.declare_exogenous($3, $4, $5); } + | symbol TEX_NAME named_var + { driver.declare_exogenous($1, $2, $3); } ; varexo_det_list : varexo_det_list symbol @@ -404,6 +410,12 @@ varexo_det_list : varexo_det_list symbol { driver.declare_exogenous_det($3, $4); } | symbol TEX_NAME { driver.declare_exogenous_det($1, $2); } + | varexo_det_list symbol TEX_NAME named_var + { driver.declare_exogenous_det($2, $3, $4); } + | varexo_det_list COMMA symbol TEX_NAME named_var + { driver.declare_exogenous_det($3, $4, $5); } + | symbol TEX_NAME named_var + { driver.declare_exogenous_det($1, $2, $3); } ; parameter_list : parameter_list symbol diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 6d389865..79505b31 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -164,21 +164,25 @@ ParsingDriver::declare_endogenous(string *name, string *tex_name, string *long_n } void -ParsingDriver::declare_exogenous(string *name, string *tex_name) +ParsingDriver::declare_exogenous(string *name, string *tex_name, string *long_name) { - declare_symbol(name, eExogenous, tex_name, NULL); + declare_symbol(name, eExogenous, tex_name, long_name); delete name; if (tex_name != NULL) delete tex_name; + if (long_name != NULL) + delete long_name; } void -ParsingDriver::declare_exogenous_det(string *name, string *tex_name) +ParsingDriver::declare_exogenous_det(string *name, string *tex_name, string *long_name) { - declare_symbol(name, eExogenousDet, tex_name, NULL); + declare_symbol(name, eExogenousDet, tex_name, long_name); delete name; if (tex_name != NULL) delete tex_name; + if (long_name != NULL) + delete long_name; } void diff --git a/ParsingDriver.hh b/ParsingDriver.hh index db19f2b0..27d9800b 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -260,9 +260,9 @@ public: //! Declares an endogenous variable void declare_endogenous(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares an exogenous variable - void declare_exogenous(string *name, string *tex_name = NULL); + void declare_exogenous(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares an exogenous deterministic variable - void declare_exogenous_det(string *name, string *tex_name = NULL); + void declare_exogenous_det(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares a parameter void declare_parameter(string *name, string *tex_name = NULL); //! Declares a statement local variable diff --git a/SymbolTable.cc b/SymbolTable.cc index 039024e5..102814a9 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -175,20 +175,24 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) { output << "M_.exo_names = '" << getName(exo_ids[0]) << "';" << endl; output << "M_.exo_names_tex = '" << getTeXName(exo_ids[0]) << "';" << endl; + output << "M_.exo_names_long = '" << getLongName(exo_ids[0]) << "';" << endl; for (int id = 1; id < exo_nbr(); id++) { output << "M_.exo_names = char(M_.exo_names, '" << getName(exo_ids[id]) << "');" << endl - << "M_.exo_names_tex = char(M_.exo_names_tex, '" << getTeXName(exo_ids[id]) << "');" << endl; + << "M_.exo_names_tex = char(M_.exo_names_tex, '" << getTeXName(exo_ids[id]) << "');" << endl + << "M_.exo_names_long = char(M_.exo_names_long, '" << getLongName(exo_ids[id]) << "');" << endl; } } if (exo_det_nbr() > 0) { output << "M_.exo_det_names = '" << getName(exo_det_ids[0]) << "';" << endl; output << "M_.exo_det_names_tex = '" << getTeXName(exo_det_ids[0]) << "';" << endl; + output << "M_.exo_det_names_long = '" << getLongName(exo_det_ids[0]) << "';" << endl; for (int id = 1; id < exo_det_nbr(); id++) { output << "M_.exo_det_names = char(M_.exo_det_names, '" << getName(exo_det_ids[id]) << "');" << endl - << "M_.exo_det_names_tex = char(M_.exo_det_names_tex, '" << getTeXName(exo_det_ids[id]) << "');" << endl; + << "M_.exo_det_names_tex = char(M_.exo_det_names_tex, '" << getTeXName(exo_det_ids[id]) << "');" << endl + << "M_.exo_det_names_long = char(M_.exo_det_names_long, '" << getLongName(exo_det_ids[id]) << "');" << endl; } } if (endo_nbr() > 0) From 14b6cf98fb7b90e4cd12f68ac020d9ac9a1831a8 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 15 Nov 2013 10:56:48 +0100 Subject: [PATCH 050/162] preprocessor: support long name for parameters, #478 --- DynareBison.yy | 6 ++++++ ParsingDriver.cc | 6 ++++-- ParsingDriver.hh | 2 +- SymbolTable.cc | 4 +++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 08052f77..8753ac38 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -430,6 +430,12 @@ parameter_list : parameter_list symbol { driver.declare_parameter($3, $4); } | symbol TEX_NAME { driver.declare_parameter($1, $2); } + | parameter_list symbol TEX_NAME named_var + { driver.declare_parameter($2, $3, $4); } + | parameter_list COMMA symbol TEX_NAME named_var + { driver.declare_parameter($3, $4, $5); } + | symbol TEX_NAME named_var + { driver.declare_parameter($1, $2, $3); } ; predetermined_variables_list : predetermined_variables_list symbol diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 79505b31..4e0a0e05 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -186,12 +186,14 @@ ParsingDriver::declare_exogenous_det(string *name, string *tex_name, string *lon } void -ParsingDriver::declare_parameter(string *name, string *tex_name) +ParsingDriver::declare_parameter(string *name, string *tex_name, string *long_name) { - declare_symbol(name, eParameter, tex_name, NULL); + declare_symbol(name, eParameter, tex_name, long_name); delete name; if (tex_name != NULL) delete tex_name; + if (long_name != NULL) + delete long_name; } void diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 27d9800b..cb266d45 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -264,7 +264,7 @@ public: //! Declares an exogenous deterministic variable void declare_exogenous_det(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares a parameter - void declare_parameter(string *name, string *tex_name = NULL); + void declare_parameter(string *name, string *tex_name = NULL, string *long_name = NULL); //! Declares a statement local variable void declare_statement_local_variable(string *name); //! Completes a subsample statement diff --git a/SymbolTable.cc b/SymbolTable.cc index 102814a9..ae07a516 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -211,10 +211,12 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) { output << "M_.param_names = '" << getName(param_ids[0]) << "';" << endl; output << "M_.param_names_tex = '" << getTeXName(param_ids[0]) << "';" << endl; + output << "M_.param_names_long = '" << getLongName(param_ids[0]) << "';" << endl; for (int id = 1; id < param_nbr(); id++) { output << "M_.param_names = char(M_.param_names, '" << getName(param_ids[id]) << "');" << endl - << "M_.param_names_tex = char(M_.param_names_tex, '" << getTeXName(param_ids[id]) << "');" << endl; + << "M_.param_names_tex = char(M_.param_names_tex, '" << getTeXName(param_ids[id]) << "');" << endl + << "M_.param_names_long = char(M_.param_names_long, '" << getLongName(param_ids[id]) << "');" << endl; if (getName(param_ids[id]) == "dsge_prior_weight") output << "options_.dsge_var = 1;" << endl; From c03f89eb8d3bac2caed3c98c66a454e6da8c5154 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 14 Nov 2013 12:57:00 +0100 Subject: [PATCH 051/162] preprocessor: simplify if statement --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 8c47907c..ef9f5fb2 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -719,7 +719,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const Uf[eqr] << "+g1(" << eq+1-block_recursive << "+Per_J_, " << var+1-block_recursive << "+y_size*(it_+" << lag-1 << "))*y(it_+" << lag << ", " << varr+1 << ")"; - else if (lag < 0) + else Uf[eqr] << "+g1(" << eq+1-block_recursive << "+Per_J_, " << var+1-block_recursive << "+y_size*(it_" << lag-1 << "))*y(it_" << lag << ", " << varr+1 << ")"; From c23cbb5edd800d25945495e46bb14a48f853c2c9 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 14 Nov 2013 12:59:13 +0100 Subject: [PATCH 052/162] clang compilation: replace ostringstream array with vector of strings --- DynamicModel.cc | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index ef9f5fb2..61f60f35 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -210,7 +210,8 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const ostringstream tmp_output, tmp1_output, global_output; expr_t lhs = NULL, rhs = NULL; BinaryOpNode *eq_node; - ostringstream Uf[symbol_table.endo_nbr()]; + ostringstream Ufoss; + vector Uf(symbol_table.endo_nbr(), ""); map reference_count; temporary_terms_t local_temporary_terms; ofstream output; @@ -536,7 +537,9 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const feedback_variables.push_back(variable_ID); output << " % equation " << equation_ID+1 << " variable : " << sModel << " (" << variable_ID+1 << ") " << c_Equation_Type(equ_type) << " symb_id=" << symbol_table.getID(eEndogenous, variable_ID) << endl; - Uf[equation_ID] << " b(" << i+1-block_recursive << "+Per_J_) = -residual(" << i+1-block_recursive << ", it_)"; + Ufoss << " b(" << i+1-block_recursive << "+Per_J_) = -residual(" << i+1-block_recursive << ", it_)"; + Uf[equation_ID] = Ufoss.str(); + Ufoss.str(""); output << " residual(" << i+1-block_recursive << ", it_) = ("; goto end; default: @@ -708,21 +711,24 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const if (eq >= block_recursive && var >= block_recursive) { if (lag == 0) - Uf[eqr] << "+g1(" << eq+1-block_recursive - << "+Per_J_, " << var+1-block_recursive - << "+Per_K_)*y(it_, " << varr+1 << ")"; + Ufoss << "+g1(" << eq+1-block_recursive + << "+Per_J_, " << var+1-block_recursive + << "+Per_K_)*y(it_, " << varr+1 << ")"; else if (lag == 1) - Uf[eqr] << "+g1(" << eq+1-block_recursive - << "+Per_J_, " << var+1-block_recursive - << "+Per_y_)*y(it_+1, " << varr+1 << ")"; + Ufoss << "+g1(" << eq+1-block_recursive + << "+Per_J_, " << var+1-block_recursive + << "+Per_y_)*y(it_+1, " << varr+1 << ")"; else if (lag > 0) - Uf[eqr] << "+g1(" << eq+1-block_recursive - << "+Per_J_, " << var+1-block_recursive - << "+y_size*(it_+" << lag-1 << "))*y(it_+" << lag << ", " << varr+1 << ")"; + Ufoss << "+g1(" << eq+1-block_recursive + << "+Per_J_, " << var+1-block_recursive + << "+y_size*(it_+" << lag-1 << "))*y(it_+" << lag << ", " << varr+1 << ")"; else - Uf[eqr] << "+g1(" << eq+1-block_recursive - << "+Per_J_, " << var+1-block_recursive - << "+y_size*(it_" << lag-1 << "))*y(it_" << lag << ", " << varr+1 << ")"; + Ufoss << "+g1(" << eq+1-block_recursive + << "+Per_J_, " << var+1-block_recursive + << "+y_size*(it_" << lag-1 << "))*y(it_" << lag << ", " << varr+1 << ")"; + Uf[eqr] = Ufoss.str(); + Ufoss.str(""); + if (lag == 0) tmp_output << " g1(" << eq+1-block_recursive << "+Per_J_, " << var+1-block_recursive << "+Per_K_) = "; @@ -751,7 +757,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const for (unsigned int i = 0; i < block_size; i++) { if (i >= block_recursive) - output << " " << Uf[getBlockEquationID(block, i)].str() << ";\n"; + output << " " << Uf[getBlockEquationID(block, i)] << ";\n"; #ifdef CONDITION output << " if (fabs(condition(" << i+1 << ")) Date: Mon, 18 Nov 2013 12:42:01 +0100 Subject: [PATCH 053/162] Revert "simplify flex code" This reverts commit 3d57c1e5e95de4fc36e2dee095aa8a9c187ddfc4. --- macro/MacroFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 25fb4dbf..dc8fe29b 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -95,6 +95,7 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; dates_parens_nb=1; BEGIN(COPY_DATE_INFO); } <> { driver.error(*yylloc, "Unexpected end of file in dates statement"); } {EOL} { yylloc->lines(1); yylloc->step(); } +[^()] { yylloc->step(); *yyout << yytext; } \( { yylloc->step(); *yyout << yytext; dates_parens_nb++; } \) { yylloc->step(); @@ -102,7 +103,6 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- if (--dates_parens_nb == 0) BEGIN(INITIAL); } -. { yylloc->step(); *yyout << yytext; } \} { BEGIN(INITIAL); return token::EOL; } From 9a28f4103de8e7743b77c8541e3af41b0e056d3d Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Nov 2013 12:42:25 +0100 Subject: [PATCH 054/162] Revert "macroprocessor: copy anything within "dates()" straight to preprocessing stage" This reverts commit 4f93207a30478d28f026cc1c9ac51640bd134f88. --- macro/MacroDriver.hh | 2 -- macro/MacroFlex.ll | 18 ++++++------------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh index 0ae177ff..0945a329 100644 --- a/macro/MacroDriver.hh +++ b/macro/MacroDriver.hh @@ -86,8 +86,6 @@ private: //! If current context is the body of a loop, contains the location of the beginning of the body Macro::parser::location_type for_body_loc; - //! Temporary variable used for counting parens in dates statement - int dates_parens_nb; //! Temporary variable used in FOR_BODY mode string for_body_tmp; //! Temporary variable used in FOR_BODY mode diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index dc8fe29b..09c04da0 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,7 +51,8 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x COPY_DATE_INFO +%x DATE_MATCH +%x CLOSE_DATE %{ // Increments location counter for every token read @@ -92,17 +93,10 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- {DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } ${DATE} { yylloc->step(); *yyout << yytext + 1; } -dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; dates_parens_nb=1; BEGIN(COPY_DATE_INFO); } -<> { driver.error(*yylloc, "Unexpected end of file in dates statement"); } -{EOL} { yylloc->lines(1); yylloc->step(); } -[^()] { yylloc->step(); *yyout << yytext; } -\( { yylloc->step(); *yyout << yytext; dates_parens_nb++; } -\) { - yylloc->step(); - *yyout << yytext; - if (--dates_parens_nb == 0) - BEGIN(INITIAL); - } +dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; BEGIN(DATE_MATCH); } +\'{SPC}*{DATE}{SPC}*\' { yylloc->step(); *yyout << yytext; BEGIN(CLOSE_DATE); } +{DATE} { yylloc->step(); *yyout << "'" << yytext << "'"; BEGIN(CLOSE_DATE); } +{SPC}*\) { yylloc->step(); *yyout << ")"; BEGIN(INITIAL); } \} { BEGIN(INITIAL); return token::EOL; } From 0e6c4735ffddaf3baff525c129e31e203bd453cf Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Nov 2013 12:42:48 +0100 Subject: [PATCH 055/162] Revert "macroprocessor: if you encounter dates(date), insert quotes" This reverts commit 9eb34cee4a68d54a0943f41a7977899a36a60a80. --- macro/MacroFlex.ll | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 09c04da0..9235cf3b 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,8 +51,7 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x DATE_MATCH -%x CLOSE_DATE +%x END_DATE %{ // Increments location counter for every token read @@ -93,10 +92,8 @@ DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1- {DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } ${DATE} { yylloc->step(); *yyout << yytext + 1; } -dates{SPC}*\({SPC}* { yylloc->step(); *yyout << "dates("; BEGIN(DATE_MATCH); } -\'{SPC}*{DATE}{SPC}*\' { yylloc->step(); *yyout << yytext; BEGIN(CLOSE_DATE); } -{DATE} { yylloc->step(); *yyout << "'" << yytext << "'"; BEGIN(CLOSE_DATE); } -{SPC}*\) { yylloc->step(); *yyout << ")"; BEGIN(INITIAL); } +dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } +{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } \} { BEGIN(INITIAL); return token::EOL; } From 71cd4c1a52a0383f4ac2c632641b21dd9d0b2c2b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Nov 2013 12:53:42 +0100 Subject: [PATCH 056/162] Revert macro/MacroFlex.ll of 858159a4f53e23a4654d5cb447e7a937faf30168 --- macro/MacroFlex.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 9235cf3b..792efce7 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -61,7 +61,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ -DATE (-[1-9][0-9]*|[0-9]+)([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) +DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ From 9d5d6b713dd534ad8882bfc829710a46f3581daa Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Nov 2013 12:53:55 +0100 Subject: [PATCH 057/162] Revert "macroprocessor: substitute dates, don't substitute dates preceded by $ or dates('" This reverts commit 9be1d084fd2b510e7d229c7b0e02bde500e34ce1. --- macro/MacroFlex.ll | 8 -------- 1 file changed, 8 deletions(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index 792efce7..05a60329 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -51,7 +51,6 @@ typedef Macro::parser::token token; %x FOR_BODY %x THEN_BODY %x ELSE_BODY -%x END_DATE %{ // Increments location counter for every token read @@ -61,7 +60,6 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n CONT \\\\ -DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) %% /* Code put at the beginning of yylex() */ @@ -89,12 +87,6 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 ^{SPC}*@# { yylloc->step(); BEGIN(STMT); } @\{ { yylloc->step(); BEGIN(EXPR); } -{DATE} { yylloc->step(); *yyout << "dates('" << yytext << "')"; } -${DATE} { yylloc->step(); *yyout << yytext + 1; } - -dates{SPC}*\({SPC}*\'{DATE} { yylloc->step(); *yyout << yytext; BEGIN(END_DATE); } -{SPC}*\'{SPC}*\) { yylloc->step(); *yyout << yytext; BEGIN(INITIAL); } - \} { BEGIN(INITIAL); return token::EOL; } {CONT}{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); } From 29507f8e45ffafe5ae9d7cf86582e0086000049b Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 19 Nov 2013 11:53:00 +0100 Subject: [PATCH 058/162] proprocessor: block code bug fix: M and oo not being passed to solve_two_boundaries --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 61f60f35..2252db98 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2054,7 +2054,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri <<", options_.periods, " << max_leadlag_block[block].first <<", " << max_leadlag_block[block].second <<", " << blocks_linear[block] - <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo);\n"; + <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, M_, oo_);\n"; mDynamicModelFile << " tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n"; mDynamicModelFile << " if any(isnan(tmp) | isinf(tmp))\n"; mDynamicModelFile << " disp(['Inf or Nan value during the resolution of block " << block <<"']);\n"; From 1707e6aa96888d9f1d9e377b12eb99d1a33b3820 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 19 Nov 2013 11:56:39 +0100 Subject: [PATCH 059/162] preprocessor: bug fix: return oo --- DynamicModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 2252db98..bc90d42f 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2049,7 +2049,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri mDynamicModelFile << " else\n"; mDynamicModelFile << " blck_num = 1;\n"; mDynamicModelFile << " end;\n"; - mDynamicModelFile << " y = solve_two_boundaries('" << dynamic_basename << "_" << block + 1 << "'" + mDynamicModelFile << " [y oo_] = solve_two_boundaries('" << dynamic_basename << "_" << block + 1 << "'" <<", y, x, params, steady_state, y_index, " << nze <<", options_.periods, " << max_leadlag_block[block].first <<", " << max_leadlag_block[block].second From fae00ea7292dd00ad5543c6adfa94715d58f75fc Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 19 Nov 2013 12:53:15 +0100 Subject: [PATCH 060/162] fix bug introduced by ee812f38816294450a307ccb8f2fb2a27fc032f0 --- DynamicModel.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index bc90d42f..f776c88e 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -218,6 +218,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const int nze, nze_exo, nze_exo_det, nze_other_endo; vector feedback_variables; ExprNodeOutputType local_output_type; + Ufoss.str(""); local_output_type = oMatlabDynamicModelSparse; if (global_temporary_terms) @@ -538,7 +539,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const output << " % equation " << equation_ID+1 << " variable : " << sModel << " (" << variable_ID+1 << ") " << c_Equation_Type(equ_type) << " symb_id=" << symbol_table.getID(eEndogenous, variable_ID) << endl; Ufoss << " b(" << i+1-block_recursive << "+Per_J_) = -residual(" << i+1-block_recursive << ", it_)"; - Uf[equation_ID] = Ufoss.str(); + Uf[equation_ID] += Ufoss.str(); Ufoss.str(""); output << " residual(" << i+1-block_recursive << ", it_) = ("; goto end; @@ -726,7 +727,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const Ufoss << "+g1(" << eq+1-block_recursive << "+Per_J_, " << var+1-block_recursive << "+y_size*(it_" << lag-1 << "))*y(it_" << lag << ", " << varr+1 << ")"; - Uf[eqr] = Ufoss.str(); + Uf[eqr] += Ufoss.str(); Ufoss.str(""); if (lag == 0) From d2e93452c3c8dea123a3318a8276713e516ad30c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Nov 2013 16:29:21 +0100 Subject: [PATCH 061/162] preprocessor: match dates in dynare syntax --- DynareFlex.ll | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/DynareFlex.ll b/DynareFlex.ll index b4dce1d7..7343a5ad 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -69,6 +69,9 @@ string eofbuff; // Increments location counter for every token read #define YY_USER_ACTION location_increment(yylloc, yytext); %} + +DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])) + %% /* Code put at the beginning of yylex() */ %{ @@ -205,7 +208,24 @@ string eofbuff; corr {BEGIN DYNARE_STATEMENT; return token::CORR;} /* Inside of a Dynare statement */ -dates {dates_parens_nb=0; BEGIN DATES_STATEMENT; yylval->string_val = new string("dates");} +{DATE} { + char *yycopy = strdup(yytext); + char *uput = yycopy + yyleng; + unput(')'); + unput('\''); + while (uput > yycopy) + unput(*--uput); + unput('\''); + unput('('); + unput('s'); + unput('e'); + unput('t'); + unput('a'); + unput('d'); + free( yycopy ); + } +${DATE} { yylloc->step(); *yyout << yytext + 1; } +dates {dates_parens_nb=0; BEGIN DATES_STATEMENT; yylval->string_val = new string("dates");} file {return token::FILE;} datafile {return token::DATAFILE;} nobs {return token::NOBS;} From e12726a353cba9288815437f00c269527474420a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 19 Nov 2013 16:35:12 +0100 Subject: [PATCH 062/162] preprocessor: deal with dates in NativeStatement --- Statement.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Statement.cc b/Statement.cc index 53d06911..4dbf8ba5 100644 --- a/Statement.cc +++ b/Statement.cc @@ -18,6 +18,7 @@ */ #include "Statement.hh" +#include ModFileStructure::ModFileStructure() : check_present(false), @@ -74,7 +75,14 @@ NativeStatement::NativeStatement(const string &native_statement_arg) : void NativeStatement::writeOutput(ostream &output, const string &basename) const { - output << native_statement << endl; + using namespace boost::xpressive; + string date_regex = "(-?\\d+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4]\\d|5[0-2])))"; + sregex regex_lookbehind = sregex::compile("(? Date: Wed, 20 Nov 2013 11:36:01 +0100 Subject: [PATCH 063/162] preprocessor: remove "using namespace std" where not needed --- DynareMain.cc | 2 -- DynareMain2.cc | 2 -- Shocks.cc | 2 -- SigmaeInitialization.hh | 2 -- Statement.hh | 2 -- WarningConsolidation.cc | 2 -- 6 files changed, 12 deletions(-) diff --git a/DynareMain.cc b/DynareMain.cc index 44227c9c..4e8709b2 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -17,8 +17,6 @@ * along with Dynare. If not, see . */ -using namespace std; - #include #include #include diff --git a/DynareMain2.cc b/DynareMain2.cc index eb044620..66ade0c8 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -17,8 +17,6 @@ * along with Dynare. If not, see . */ -using namespace std; - #include #include "ParsingDriver.hh" diff --git a/Shocks.cc b/Shocks.cc index 1a943d2c..d1c2ea70 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -17,8 +17,6 @@ * along with Dynare. If not, see . */ -using namespace std; - #include #include #include diff --git a/SigmaeInitialization.hh b/SigmaeInitialization.hh index c2decb3f..ddfcc7f1 100644 --- a/SigmaeInitialization.hh +++ b/SigmaeInitialization.hh @@ -20,8 +20,6 @@ #ifndef _SIGMAEINITIALIZATION_HH #define _SIGMAEINITIALIZATION_HH -using namespace std; - #include #include diff --git a/Statement.hh b/Statement.hh index ff0a53e0..0563ced9 100644 --- a/Statement.hh +++ b/Statement.hh @@ -20,8 +20,6 @@ #ifndef _STATEMENT_HH #define _STATEMENT_HH -using namespace std; - #include #include #include diff --git a/WarningConsolidation.cc b/WarningConsolidation.cc index beaf061d..653d4730 100644 --- a/WarningConsolidation.cc +++ b/WarningConsolidation.cc @@ -17,8 +17,6 @@ * along with Dynare. If not, see . */ -using namespace std; - #include "WarningConsolidation.hh" #include From 07f29031ec098c28be6cb11e1c5bc50b75223edc Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 20 Nov 2013 12:06:26 +0100 Subject: [PATCH 064/162] preprocessor: remove unused private field in EstimationStatement --- ComputingTasks.cc | 6 ++---- ComputingTasks.hh | 4 +--- ParsingDriver.cc | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index dda585d4..9af570b7 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -261,11 +261,9 @@ DiscretionaryPolicyStatement::writeOutput(ostream &output, const string &basenam } EstimationStatement::EstimationStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg, - const SymbolTable &symbol_table_arg) : + const OptionsList &options_list_arg) : symbol_list(symbol_list_arg), - options_list(options_list_arg), - symbol_table(symbol_table_arg) + options_list(options_list_arg) { } diff --git a/ComputingTasks.hh b/ComputingTasks.hh index 156a3421..6d67638c 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -157,11 +157,9 @@ class EstimationStatement : public Statement private: const SymbolList symbol_list; const OptionsList options_list; - const SymbolTable &symbol_table; public: EstimationStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg, - const SymbolTable &symbol_table); + const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; }; diff --git a/ParsingDriver.cc b/ParsingDriver.cc index f28c4eeb..d39903bd 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1540,7 +1540,7 @@ ParsingDriver::set_corr_options(string *name1, string *name2, string *subsample_ void ParsingDriver::run_estimation() { - mod_file->addStatement(new EstimationStatement(symbol_list, options_list, mod_file->symbol_table)); + mod_file->addStatement(new EstimationStatement(symbol_list, options_list)); symbol_list.clear(); options_list.clear(); } From 790b138042f2476fdf78a24f50c354d39c7fe0f9 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 20 Nov 2013 12:17:07 +0100 Subject: [PATCH 065/162] preprocessor: remove unused private field in ConditionalForecastPathsStatement --- ParsingDriver.cc | 2 +- Shocks.cc | 3 +-- Shocks.hh | 4 +--- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index d39903bd..a5a4d8d6 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1948,7 +1948,7 @@ ParsingDriver::plot_conditional_forecast(string *periods) void ParsingDriver::conditional_forecast_paths() { - mod_file->addStatement(new ConditionalForecastPathsStatement(det_shocks, mod_file->symbol_table)); + mod_file->addStatement(new ConditionalForecastPathsStatement(det_shocks)); det_shocks.clear(); } diff --git a/Shocks.cc b/Shocks.cc index d1c2ea70..cc0b7700 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -312,9 +312,8 @@ MShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidat mod_file_struct.shocks_present_but_simul_not_yet = true; } -ConditionalForecastPathsStatement::ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg, const SymbolTable &symbol_table_arg) : +ConditionalForecastPathsStatement::ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg) : paths(paths_arg), - symbol_table(symbol_table_arg), path_length(-1) { } diff --git a/Shocks.hh b/Shocks.hh index de20945c..539a4033 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -90,11 +90,9 @@ class ConditionalForecastPathsStatement : public Statement { private: const AbstractShocksStatement::det_shocks_t paths; - const SymbolTable &symbol_table; int path_length; public: - ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg, - const SymbolTable &symbol_table_arg); + ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; }; From 8ed5bc2a9740fef0b0d548cf8c8ba50adba1b898 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 21 Nov 2013 14:47:44 +0100 Subject: [PATCH 066/162] preprocessor: eliminate clang warning about using namespace std --- SymbolList.hh | 4 ++-- WarningConsolidation.hh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SymbolList.hh b/SymbolList.hh index dafa965b..45c7023f 100644 --- a/SymbolList.hh +++ b/SymbolList.hh @@ -20,12 +20,12 @@ #ifndef _SYMBOL_LIST_HH #define _SYMBOL_LIST_HH -using namespace std; - #include #include #include +using namespace std; + //! Used to store a list of symbols /*! This class is no more than a vector, with a pretty-printer for Matlab */ class SymbolList diff --git a/WarningConsolidation.hh b/WarningConsolidation.hh index ba633014..135a54a3 100644 --- a/WarningConsolidation.hh +++ b/WarningConsolidation.hh @@ -20,12 +20,12 @@ #ifndef _WARNINGCONSOLIDATION_HH #define _WARNINGCONSOLIDATION_HH -using namespace std; - #include #include #include "location.hh" +using namespace std; + //! Stores Warnings issued by the Preprocessor class WarningConsolidation { From c9d7971df23ffe97bfd1b8b3fbf63531755e9416 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 21 Nov 2013 14:48:11 +0100 Subject: [PATCH 067/162] preprocessor: ExprNodeLess is a struct --- ExprNode.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ExprNode.hh b/ExprNode.hh index d251f332..be7d4b38 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -115,7 +115,7 @@ class ExprNode friend class DynamicModel; friend class StaticModel; friend class ModelTree; - friend class ExprNodeLess; + friend struct ExprNodeLess; friend class NumConstNode; friend class VariableNode; friend class UnaryOpNode; From 645b830dc9176e31a5585bc117aab84cc641e30f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 21 Nov 2013 15:11:57 +0100 Subject: [PATCH 068/162] preprocessor: allow empty estimated_params_init block --- DynareBison.yy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DynareBison.yy b/DynareBison.yy index 1b2dea70..3640edc6 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -1172,6 +1172,8 @@ estimated_elem3 : expression_or_empty COMMA expression_or_empty estimated_params_init : ESTIMATED_PARAMS_INIT ';' estimated_init_list END ';' { driver.estimated_params_init(); } + | ESTIMATED_PARAMS_INIT '(' USE_CALIBRATION ')' ';' END ';' + { driver.estimated_params_init(true); } | ESTIMATED_PARAMS_INIT '(' USE_CALIBRATION ')' ';' estimated_init_list END ';' { driver.estimated_params_init(true); } ; From 173dc32e60e9d0423e17cd9132e403822fe58c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 29 Nov 2013 14:50:58 +0100 Subject: [PATCH 069/162] Refuse that estimated parameters appear in the expressions defining the variance/covariance matrix of shocks Closes #469 --- ComputingTasks.cc | 6 ++++++ ModFile.cc | 22 ++++++++++++++++++++++ Shocks.cc | 19 +++++++++++++++++++ Statement.hh | 6 ++++++ 4 files changed, 53 insertions(+) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 9af570b7..e037f2d4 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -517,6 +517,12 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo } } } + + // Fill in mod_file_struct.estimated_parameters (related to #469) + for (vector::const_iterator it = estim_params_list.begin(); + it != estim_params_list.end(); it++) + if (it->type == 2) + mod_file_struct.estimated_parameters.insert(symbol_table.getID(it->name)); } void diff --git a/ModFile.cc b/ModFile.cc index 1a1b7e3e..3e5f0a86 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -264,6 +264,28 @@ ModFile::checkPass() || dynamic_model.isBinaryOpUsed(oEqualEqual) || dynamic_model.isBinaryOpUsed(oDifferent))) warnings << "WARNING: you are using a function (max, min, abs, sign) or an operator (<, >, <=, >=, ==, !=) which is unsuitable for a stochastic context; see the reference manual, section about \"Expressions\", for more details." << endl; + + // Test if some estimated parameters are used within the values of shocks + // statements (see issue #469) + set parameters_intersect; + set_intersection(mod_file_struct.parameters_within_shocks_values.begin(), + mod_file_struct.parameters_within_shocks_values.end(), + mod_file_struct.estimated_parameters.begin(), + mod_file_struct.estimated_parameters.end(), + inserter(parameters_intersect, parameters_intersect.begin())); + if (parameters_intersect.size() > 0) + { + cerr << "ERROR: some estimated parameters ("; + for (set::const_iterator it = parameters_intersect.begin(); + it != parameters_intersect.end(); ) + { + cerr << symbol_table.getName(*it); + if (++it != parameters_intersect.end()) + cerr << ", "; + } + cerr << ") also appear in the expressions defining the variance/covariance matrix of shocks; this is not allowed." << endl; + exit(EXIT_FAILURE); + } } void diff --git a/Shocks.cc b/Shocks.cc index cc0b7700..2e93a3ac 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -284,6 +284,25 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati || symbol_table.isObservedVariable(it->first.second)) mod_file_struct.calibrated_measurement_errors = true; } + + // Fill in mod_file_struct.parameters_with_shocks_values (related to #469) + set > params_lags; + for (var_and_std_shocks_t::const_iterator it = var_shocks.begin(); + it != var_shocks.end(); ++it) + it->second->collectVariables(eParameter, params_lags); + for (var_and_std_shocks_t::const_iterator it = std_shocks.begin(); + it != std_shocks.end(); ++it) + it->second->collectVariables(eParameter, params_lags); + for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin(); + it != covar_shocks.end(); ++it) + it->second->collectVariables(eParameter, params_lags); + for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin(); + it != corr_shocks.end(); ++it) + it->second->collectVariables(eParameter, params_lags); + + for (set >::const_iterator it = params_lags.begin(); + it != params_lags.end(); ++it) + mod_file_struct.parameters_within_shocks_values.insert(it->first); } MShocksStatement::MShocksStatement(const det_shocks_t &det_shocks_arg, diff --git a/Statement.hh b/Statement.hh index 0563ced9..d4dad9b8 100644 --- a/Statement.hh +++ b/Statement.hh @@ -23,6 +23,7 @@ #include #include #include +#include #include "SymbolList.hh" #include "WarningConsolidation.hh" @@ -97,6 +98,11 @@ public: int last_markov_switching_chain; //! Whether a calib_smoother statement is present bool calib_smoother_present; + //! Set of parameters used within shocks blocks, inside the expressions + //! defining the values of covariances (stored as symbol ids) + set parameters_within_shocks_values; + //! Set of estimated parameters (stored as symbol ids) + set estimated_parameters; }; class Statement From 1414f98cff8c736a3f2aa813270c7e1e792123dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 29 Nov 2013 15:32:49 +0100 Subject: [PATCH 070/162] Refactor code for collecting variables appearing in expressions - rename ExprNode::collectVariables in ExprNode::collectDynamicVariables - new ExprNode::collectVariables: same as above, but without lag information - remove ExprNode::findUnusedEndogenous: essentially redundant with the above) - remove ExprNode::collectModelLocalVariables: idem --- DynamicModel.cc | 30 +++++++++------ DynamicModel.hh | 2 +- ExprNode.cc | 92 ++++++++++++--------------------------------- ExprNode.hh | 41 +++++++++----------- ModFile.cc | 3 +- ModelTree.cc | 2 +- ParsingDriver.cc | 6 +-- Shocks.cc | 13 ++----- SteadyStateModel.cc | 8 ++-- 9 files changed, 74 insertions(+), 123 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index f776c88e..aa682d19 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -3475,7 +3475,7 @@ DynamicModel::computeRamseyPolicyFOCs(const StaticModel &static_model) int max_eq_lead = 0; int max_eq_lag = 0; for (int i = 0; i < (int) equations.size(); i++) - equations[i]->collectVariables(eEndogenous, dynvars); + equations[i]->collectDynamicVariables(eEndogenous, dynvars); for (set >::const_iterator it = dynvars.begin(); it != dynvars.end(); it++) @@ -3572,11 +3572,17 @@ DynamicModel::toStatic(StaticModel &static_model) const static_model.addAuxEquation((*it)->toStatic(static_model)); } -void -DynamicModel::findUnusedEndogenous(set &unusedEndogs) +set +DynamicModel::findUnusedEndogenous() { + set usedEndo, unusedEndo; for (int i = 0; i < (int) equations.size(); i++) - equations[i]->findUnusedEndogenous(unusedEndogs); + equations[i]->collectVariables(eEndogenous, usedEndo); + set allEndo = symbol_table.getEndogenous(); + set_difference(allEndo.begin(), allEndo.end(), + usedEndo.begin(), usedEndo.end(), + inserter(unusedEndo, unusedEndo.begin())); + return unusedEndo; } void @@ -3585,17 +3591,17 @@ DynamicModel::computeDerivIDs() set > dynvars; for (int i = 0; i < (int) equations.size(); i++) - equations[i]->collectVariables(eEndogenous, dynvars); + equations[i]->collectDynamicVariables(eEndogenous, dynvars); dynJacobianColsNbr = dynvars.size(); for (int i = 0; i < (int) equations.size(); i++) { - equations[i]->collectVariables(eExogenous, dynvars); - equations[i]->collectVariables(eExogenousDet, dynvars); - equations[i]->collectVariables(eParameter, dynvars); - equations[i]->collectVariables(eTrend, dynvars); - equations[i]->collectVariables(eLogTrend, dynvars); + equations[i]->collectDynamicVariables(eExogenous, dynvars); + equations[i]->collectDynamicVariables(eExogenousDet, dynvars); + equations[i]->collectDynamicVariables(eParameter, dynvars); + equations[i]->collectDynamicVariables(eTrend, dynvars); + equations[i]->collectDynamicVariables(eLogTrend, dynvars); } for (set >::const_iterator it = dynvars.begin(); @@ -3993,7 +3999,7 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type, bool deterministic_model // Substitute in used model local variables set used_local_vars; for (size_t i = 0; i < equations.size(); i++) - equations[i]->collectModelLocalVariables(used_local_vars); + equations[i]->collectVariables(eModelLocalVariable, used_local_vars); for (set::const_iterator it = used_local_vars.begin(); it != used_local_vars.end(); ++it) @@ -4230,7 +4236,7 @@ DynamicModel::isModelLocalVariableUsed() const size_t i = 0; while (i < equations.size() && used_local_vars.size() == 0) { - equations[i]->collectModelLocalVariables(used_local_vars); + equations[i]->collectVariables(eModelLocalVariable, used_local_vars); i++; } return used_local_vars.size() > 0; diff --git a/DynamicModel.hh b/DynamicModel.hh index 8cc151a3..c02d1130 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -223,7 +223,7 @@ public: void toStatic(StaticModel &static_model) const; //! Find endogenous variables not used in model - void findUnusedEndogenous(set &unusedEndogs); + set findUnusedEndogenous(); //! Copies a dynamic model (only the equations) /*! It assumes that the dynamic model given in argument has just been allocated */ diff --git a/ExprNode.cc b/ExprNode.cc index 45247ec1..2fff674b 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -80,11 +80,20 @@ ExprNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) const return 0; } +void +ExprNode::collectVariables(SymbolType type, set &result) const +{ + set > symbs_lags; + collectDynamicVariables(type, symbs_lags); + transform(symbs_lags.begin(), symbs_lags.end(), inserter(result, result.begin()), + boost::bind(&pair::first,_1)); +} + void ExprNode::collectEndogenous(set > &result) const { set > symb_ids; - collectVariables(eEndogenous, symb_ids); + collectDynamicVariables(eEndogenous, symb_ids); for (set >::const_iterator it = symb_ids.begin(); it != symb_ids.end(); it++) result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(it->first), it->second)); @@ -94,21 +103,12 @@ void ExprNode::collectExogenous(set > &result) const { set > symb_ids; - collectVariables(eExogenous, symb_ids); + collectDynamicVariables(eExogenous, symb_ids); for (set >::const_iterator it = symb_ids.begin(); it != symb_ids.end(); it++) result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(it->first), it->second)); } -void -ExprNode::collectModelLocalVariables(set &result) const -{ - set > symb_ids; - collectVariables(eModelLocalVariable, symb_ids); - transform(symb_ids.begin(), symb_ids.end(), inserter(result, result.begin()), - boost::bind(&pair::first,_1)); -} - void ExprNode::computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, @@ -325,12 +325,7 @@ NumConstNode::compile(ostream &CompileCode, unsigned int &instruction_number, } void -NumConstNode::collectVariables(SymbolType type_arg, set > &result) const -{ -} - -void -NumConstNode::findUnusedEndogenous(set &unusedEndogs) const +NumConstNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { } @@ -875,20 +870,12 @@ VariableNode::computeTemporaryTerms(map &reference_count, } void -VariableNode::collectVariables(SymbolType type_arg, set > &result) const +VariableNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { if (type == type_arg) result.insert(make_pair(symb_id, lag)); if (type == eModelLocalVariable) - datatree.local_variables_table[symb_id]->collectVariables(type_arg, result); -} - -void -VariableNode::findUnusedEndogenous(set &unusedEndogs) const -{ - set::iterator it = unusedEndogs.find(symb_id); - if (it != unusedEndogs.end()) - unusedEndogs.erase(it); + datatree.local_variables_table[symb_id]->collectDynamicVariables(type_arg, result); } pair @@ -2003,15 +1990,9 @@ UnaryOpNode::compile(ostream &CompileCode, unsigned int &instruction_number, } void -UnaryOpNode::collectVariables(SymbolType type_arg, set > &result) const +UnaryOpNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { - arg->collectVariables(type_arg, result); -} - -void -UnaryOpNode::findUnusedEndogenous(set &unusedEndogs) const -{ - arg->findUnusedEndogenous(unusedEndogs); + arg->collectDynamicVariables(type_arg, result); } pair @@ -3080,17 +3061,10 @@ BinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int & } void -BinaryOpNode::collectVariables(SymbolType type_arg, set > &result) const +BinaryOpNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { - arg1->collectVariables(type_arg, result); - arg2->collectVariables(type_arg, result); -} - -void -BinaryOpNode::findUnusedEndogenous(set &unusedEndogs) const -{ - arg1->findUnusedEndogenous(unusedEndogs); - arg2->findUnusedEndogenous(unusedEndogs); + arg1->collectDynamicVariables(type_arg, result); + arg2->collectDynamicVariables(type_arg, result); } expr_t @@ -4057,19 +4031,11 @@ TrinaryOpNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int } void -TrinaryOpNode::collectVariables(SymbolType type_arg, set > &result) const +TrinaryOpNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { - arg1->collectVariables(type_arg, result); - arg2->collectVariables(type_arg, result); - arg3->collectVariables(type_arg, result); -} - -void -TrinaryOpNode::findUnusedEndogenous(set &unusedEndogs) const -{ - arg1->findUnusedEndogenous(unusedEndogs); - arg2->findUnusedEndogenous(unusedEndogs); - arg3->findUnusedEndogenous(unusedEndogs); + arg1->collectDynamicVariables(type_arg, result); + arg2->collectDynamicVariables(type_arg, result); + arg3->collectDynamicVariables(type_arg, result); } pair @@ -4625,19 +4591,11 @@ ExternalFunctionNode::computeTemporaryTerms(map &reference_count, } void -ExternalFunctionNode::collectVariables(SymbolType type_arg, set > &result) const +ExternalFunctionNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) - (*it)->collectVariables(type_arg, result); -} - -void -ExternalFunctionNode::findUnusedEndogenous(set &unusedEndogs) const -{ - for (vector::const_iterator it = arguments.begin(); - it != arguments.end(); it++) - (*it)->findUnusedEndogenous(unusedEndogs); + (*it)->collectDynamicVariables(type_arg, result); } void diff --git a/ExprNode.hh b/ExprNode.hh index be7d4b38..65199d66 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -202,14 +202,23 @@ public: const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const; - //! Computes the set of all variables of a given symbol type in the expression + //! Computes the set of all variables of a given symbol type in the expression (with information on lags) /*! Variables are stored as integer pairs of the form (symb_id, lag). They are added to the set given in argument. Note that model local variables are substituted by their expression in the computation (and added if type_arg = ModelLocalVariable). */ - virtual void collectVariables(SymbolType type_arg, set > &result) const = 0; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const = 0; + + //! Computes the set of all variables of a given symbol type in the expression (without information on lags) + /*! + Variables are stored as symb_id. + They are added to the set given in argument. + Note that model local variables are substituted by their expression in the computation + (and added if type_arg = ModelLocalVariable). + */ + void collectVariables(SymbolType type_arg, set &result) const; //! Computes the set of endogenous variables in the expression /*! @@ -227,18 +236,8 @@ public: */ virtual void collectExogenous(set > &result) const; - //! Computes the set of model local variables in the expression - /*! - Symbol IDs of these model local variables are added to the set given in argument. - Note that this method is called recursively on the expressions associated to the model local variables detected. - */ - virtual void collectModelLocalVariables(set &result) const; - virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const = 0; - //! Removes used endogenous variables from the provided list of endogs - virtual void findUnusedEndogenous(set &unusedEndogs) const = 0; - virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, map > &first_occurence, @@ -438,8 +437,7 @@ public: }; virtual void prepareForDerivation(); virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const; @@ -484,8 +482,7 @@ public: VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg); virtual void prepareForDerivation(); virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, map > &first_occurence, @@ -564,8 +561,7 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException, EvalExternalFunctionException); virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); @@ -643,8 +639,7 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; static double eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivOrder) throw (EvalException, EvalExternalFunctionException); virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); @@ -738,8 +733,7 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException, EvalExternalFunctionException); virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); @@ -810,8 +804,7 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void collectVariables(SymbolType type_arg, set > &result) const; - virtual void findUnusedEndogenous(set &unusedEndogs) const; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); unsigned int compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, diff --git a/ModFile.cc b/ModFile.cc index 3e5f0a86..56c5fb30 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -293,8 +293,7 @@ ModFile::transformPass(bool nostrict) { if (nostrict) { - set unusedEndogs = symbol_table.getEndogenous(); - dynamic_model.findUnusedEndogenous(unusedEndogs); + set unusedEndogs = dynamic_model.findUnusedEndogenous(); for (set::iterator it = unusedEndogs.begin(); it != unusedEndogs.end(); it++) { symbol_table.changeType(*it, eUnusedEndogenous); diff --git a/ModelTree.cc b/ModelTree.cc index 362a5622..f51eeb6f 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -1179,7 +1179,7 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t const temporary_terms_t tt; for (size_t i = 0; i < equations.size(); i++) - equations[i]->collectModelLocalVariables(used_local_vars); + equations[i]->collectVariables(eModelLocalVariable, used_local_vars); for (set::const_iterator it = used_local_vars.begin(); it != used_local_vars.end(); ++it) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index a5a4d8d6..ee7f0718 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -374,10 +374,10 @@ ParsingDriver::end_nonstationary_var(bool log_deflator, expr_t deflator) error("Variable " + e.name + " was listed more than once as following a trend."); } - set > r; + set r; deflator->collectVariables(eEndogenous, r); - for (set >::const_iterator it = r.begin(); it != r.end(); ++it) - if (dynamic_model->isNonstationary(it->first)) + for (set::const_iterator it = r.begin(); it != r.end(); ++it) + if (dynamic_model->isNonstationary(*it)) error("The deflator contains a non-stationary endogenous variable. This is not allowed. Please use only stationary endogenous and/or {log_}trend_vars."); declared_nonstationary_vars.clear(); diff --git a/Shocks.cc b/Shocks.cc index 2e93a3ac..4b4037b2 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -286,23 +286,18 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati } // Fill in mod_file_struct.parameters_with_shocks_values (related to #469) - set > params_lags; for (var_and_std_shocks_t::const_iterator it = var_shocks.begin(); it != var_shocks.end(); ++it) - it->second->collectVariables(eParameter, params_lags); + it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values); for (var_and_std_shocks_t::const_iterator it = std_shocks.begin(); it != std_shocks.end(); ++it) - it->second->collectVariables(eParameter, params_lags); + it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values); for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin(); it != covar_shocks.end(); ++it) - it->second->collectVariables(eParameter, params_lags); + it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values); for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin(); it != corr_shocks.end(); ++it) - it->second->collectVariables(eParameter, params_lags); - - for (set >::const_iterator it = params_lags.begin(); - it != params_lags.end(); ++it) - mod_file_struct.parameters_within_shocks_values.insert(it->first); + it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values); } MShocksStatement::MShocksStatement(const det_shocks_t &det_shocks_arg, diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index f85aee07..a8770c78 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -78,16 +78,16 @@ SteadyStateModel::checkPass(bool ramsey_policy) const // Check that expression has no undefined symbol if (!ramsey_policy) { - set > used_symbols; + set used_symbols; expr_t expr = def_table.find(symb_ids)->second; expr->collectVariables(eEndogenous, used_symbols); expr->collectVariables(eModFileLocalVariable, used_symbols); - for (set >::const_iterator it = used_symbols.begin(); + for (set::const_iterator it = used_symbols.begin(); it != used_symbols.end(); ++it) - if (find(so_far_defined.begin(), so_far_defined.end(), it->first) + if (find(so_far_defined.begin(), so_far_defined.end(), *it) == so_far_defined.end()) { - cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(it->first) + cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(*it) << "' is undefined in the declaration of variable '" << symbol_table.getName(symb_ids[0]) << "'" << endl; exit(EXIT_FAILURE); } From 6ba09804602002867c0db409db03f883223069dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 29 Nov 2013 16:03:15 +0100 Subject: [PATCH 071/162] Add a warning when some exogenous are not used in the model --- DynamicModel.cc | 13 +++++++++++++ DynamicModel.hh | 2 ++ ModFile.cc | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/DynamicModel.cc b/DynamicModel.cc index aa682d19..ecd0881b 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -3585,6 +3585,19 @@ DynamicModel::findUnusedEndogenous() return unusedEndo; } +set +DynamicModel::findUnusedExogenous() +{ + set usedExo, unusedExo; + for (int i = 0; i < (int) equations.size(); i++) + equations[i]->collectVariables(eExogenous, usedExo); + set allExo = symbol_table.getExogenous(); + set_difference(allExo.begin(), allExo.end(), + usedExo.begin(), usedExo.end(), + inserter(unusedExo, unusedExo.begin())); + return unusedExo; +} + void DynamicModel::computeDerivIDs() { diff --git a/DynamicModel.hh b/DynamicModel.hh index c02d1130..5f32ed11 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -224,6 +224,8 @@ public: //! Find endogenous variables not used in model set findUnusedEndogenous(); + //! Find exogenous variables not used in model + set findUnusedExogenous(); //! Copies a dynamic model (only the equations) /*! It assumes that the dynamic model given in argument has just been allocated */ diff --git a/ModFile.cc b/ModFile.cc index 56c5fb30..6d6af32b 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -286,6 +286,21 @@ ModFile::checkPass() cerr << ") also appear in the expressions defining the variance/covariance matrix of shocks; this is not allowed." << endl; exit(EXIT_FAILURE); } + + // Check if some exogenous is not used in the model block + set unusedExo = dynamic_model.findUnusedExogenous(); + if (unusedExo.size() > 1) + { + warnings << "WARNING: some exogenous ("; + for (set::const_iterator it = unusedExo.begin(); + it != unusedExo.end(); ) + { + warnings << symbol_table.getName(*it); + if (++it != unusedExo.end()) + warnings << ", "; + } + warnings << ") are declared but not used in the model. This may lead to crashes or unexpected behaviour." << endl; + } } void From 8d2c28f3bfbced4d6a6e39c5026891ab3357bc42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 29 Nov 2013 16:29:25 +0100 Subject: [PATCH 072/162] Forbid the use of trend variables in expressions outside the model block --- ParsingDriver.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index ee7f0718..11e41faa 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -336,6 +336,10 @@ ParsingDriver::add_expression_variable(string *name) if (mod_file->symbol_table.getType(*name) == eModelLocalVariable) error("Variable " + *name + " not allowed outside model declaration. Its scope is only inside model."); + if (mod_file->symbol_table.getType(*name) == eTrend + || mod_file->symbol_table.getType(*name) == eLogTrend) + error("Variable " + *name + " not allowed outside model declaration, because it is a trend variable."); + int symb_id = mod_file->symbol_table.getID(*name); expr_t id = data_tree->AddVariable(symb_id); From 58d6f0170863b5a228234508980c0b63e1904696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 2 Dec 2013 14:58:33 +0100 Subject: [PATCH 073/162] Fix bug with DSGE-VAR introduced in f7cdc39f --- ComputingTasks.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index e037f2d4..8d213250 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -521,7 +521,7 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo // Fill in mod_file_struct.estimated_parameters (related to #469) for (vector::const_iterator it = estim_params_list.begin(); it != estim_params_list.end(); it++) - if (it->type == 2) + if (it->type == 2 && it->name != "dsge_prior_weight") mod_file_struct.estimated_parameters.insert(symbol_table.getID(it->name)); } From 78ea0645922ee7e640a86e02dba9afe589c71eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 2 Dec 2013 17:21:08 +0100 Subject: [PATCH 074/162] Cleanup more preprocessor-generated files at each run --- ModFile.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 6d6af32b..56257c19 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -618,14 +618,16 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b << "end" << endl; // Erase possible remnants of previous runs - string dynfile = basename + "_dynamic.m"; - unlink(dynfile.c_str()); + unlink((basename + "_dynamic.m").c_str()); + unlink((basename + "_dynamic.cod").c_str()); + unlink((basename + "_dynamic.bin").c_str()); - string statfile = basename + "_static.m"; - unlink(statfile.c_str()); + unlink((basename + "_static.m").c_str()); + unlink((basename + "_static.cod").c_str()); + unlink((basename + "_static.bin").c_str()); - string steadystatefile = basename + "_steadystate2.m"; - unlink(steadystatefile.c_str()); + unlink((basename + "_steadystate2.m").c_str()); + unlink((basename + "_set_auxiliary_variables.m").c_str()); if (!use_dll) { From 3c433ad6ea590ca1badc03bb150e685f4158eaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Mon, 9 Dec 2013 11:07:44 +0100 Subject: [PATCH 075/162] Copy the generated time series in Base workspace (extended_path). --- ComputingTasks.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 8d213250..2a9d3b2c 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2463,8 +2463,8 @@ ExtendedPathStatement::writeOutput(ostream &output, const string &basename) cons if (it->first != string("periods")) output << "options_." << it->first << " = " << it->second << ";" << endl; - output << "oo_.endo_simul = [ oo_.steady_state, extended_path([], " << options_list.num_options.find("periods")->second - << ") ];" << endl + output << "extended_path([], " << options_list.num_options.find("periods")->second + << ");" << endl << "oo_.exo_simul = oo_.ep.shocks;" << endl; } From 64d3cfa601615b1b2b5aa411a3c306eb78314d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 9 Dec 2013 16:23:49 +0100 Subject: [PATCH 076/162] Forbid the combination of estimation/mode_file and estimated_params_init/use_calibration Closes #549 --- ComputingTasks.cc | 14 ++++++++++++++ ComputingTasks.hh | 1 + Statement.cc | 3 ++- Statement.hh | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 2a9d3b2c..b5668549 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -342,6 +342,13 @@ EstimationStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsoli cerr << "ERROR: The estimation statement requires a data file to be supplied via the datafile option." << endl; exit(EXIT_FAILURE); } + + if (options_list.string_options.find("mode_file") != options_list.string_options.end() && + mod_file_struct.estim_params_use_calib) + { + cerr << "ERROR: The mode_file option of the estimation statement is incompatible with the use_calibration option of the estimated_params_init block." << endl; + exit(EXIT_FAILURE); + } } void @@ -593,6 +600,13 @@ EstimatedParamsInitStatement::EstimatedParamsInitStatement(const vector &estim_params_list_arg, const SymbolTable &symbol_table_arg, const bool use_calibration_arg); + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; }; diff --git a/Statement.cc b/Statement.cc index 4dbf8ba5..f990f611 100644 --- a/Statement.cc +++ b/Statement.cc @@ -49,7 +49,8 @@ ModFileStructure::ModFileStructure() : bayesian_irf_present(false), estimation_data_statement_present(false), last_markov_switching_chain(0), - calib_smoother_present(false) + calib_smoother_present(false), + estim_params_use_calib(false) { } diff --git a/Statement.hh b/Statement.hh index d4dad9b8..7cd7b921 100644 --- a/Statement.hh +++ b/Statement.hh @@ -98,6 +98,8 @@ public: int last_markov_switching_chain; //! Whether a calib_smoother statement is present bool calib_smoother_present; + //! Whether there is an estimated_params_init with use_calibration + bool estim_params_use_calib; //! Set of parameters used within shocks blocks, inside the expressions //! defining the values of covariances (stored as symbol ids) set parameters_within_shocks_values; From 0d1fe6899f4a5fd39cbd69481cac2d1df6af3326 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 9 Dec 2013 15:51:27 +0100 Subject: [PATCH 077/162] allow long_name to be used without specifying a latex name, #478 --- DynareBison.yy | 30 ++++++++++++++++++++++++++++++ ParsingDriver.cc | 19 ++++++++++++------- SymbolTable.cc | 18 ++++++++++++++++-- SymbolTable.hh | 6 ++++++ 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3640edc6..ea7d82e0 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -331,6 +331,12 @@ nonstationary_var_list : nonstationary_var_list symbol { driver.declare_nonstationary_var($3); } | symbol { driver.declare_nonstationary_var($1); } + | nonstationary_var_list symbol named_var + { driver.declare_nonstationary_var($2, NULL, $3); } + | nonstationary_var_list COMMA symbol named_var + { driver.declare_nonstationary_var($3, NULL, $4); } + | symbol named_var + { driver.declare_nonstationary_var($1, NULL, $2); } | nonstationary_var_list symbol TEX_NAME { driver.declare_nonstationary_var($2, $3); } | nonstationary_var_list COMMA symbol TEX_NAME @@ -363,6 +369,12 @@ var_list : var_list symbol { driver.declare_endogenous($3); } | symbol { driver.declare_endogenous($1); } + | var_list symbol named_var + { driver.declare_endogenous($2, NULL, $3); } + | var_list COMMA symbol named_var + { driver.declare_endogenous($3, NULL, $4); } + | symbol named_var + { driver.declare_endogenous($1, NULL, $2); } | var_list symbol TEX_NAME { driver.declare_endogenous($2, $3); } | var_list COMMA symbol TEX_NAME @@ -383,6 +395,12 @@ varexo_list : varexo_list symbol { driver.declare_exogenous($3); } | symbol { driver.declare_exogenous($1); } + | varexo_list symbol named_var + { driver.declare_exogenous($2, NULL, $3); } + | varexo_list COMMA symbol named_var + { driver.declare_exogenous($3, NULL, $4); } + | symbol named_var + { driver.declare_exogenous($1, NULL, $2); } | varexo_list symbol TEX_NAME { driver.declare_exogenous($2, $3); } | varexo_list COMMA symbol TEX_NAME @@ -403,6 +421,12 @@ varexo_det_list : varexo_det_list symbol { driver.declare_exogenous_det($3); } | symbol { driver.declare_exogenous_det($1); } + | varexo_det_list symbol named_var + { driver.declare_exogenous_det($2, NULL, $3); } + | varexo_det_list COMMA symbol named_var + { driver.declare_exogenous_det($3, NULL, $4); } + | symbol named_var + { driver.declare_exogenous_det($1, NULL, $2); } | varexo_det_list symbol TEX_NAME { driver.declare_exogenous_det($2, $3); } | varexo_det_list COMMA symbol TEX_NAME @@ -423,6 +447,12 @@ parameter_list : parameter_list symbol { driver.declare_parameter($3); } | symbol { driver.declare_parameter($1); } + | parameter_list symbol named_var + { driver.declare_parameter($2, NULL, $3); } + | parameter_list COMMA symbol named_var + { driver.declare_parameter($3, NULL, $4); } + | symbol named_var + { driver.declare_parameter($1, NULL, $2); } | parameter_list symbol TEX_NAME { driver.declare_parameter($2, $3); } | parameter_list COMMA symbol TEX_NAME diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 11e41faa..e551078b 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -138,7 +138,9 @@ ParsingDriver::declare_symbol(const string *name, SymbolType type, const string if (tex_name == NULL && long_name == NULL) mod_file->symbol_table.addSymbol(*name, type); else - if (long_name == NULL) + if (tex_name == NULL) + mod_file->symbol_table.createTexNameAndAddSymbolWithLongName(*name, type, *long_name); + else if (long_name == NULL) mod_file->symbol_table.addSymbol(*name, type, *tex_name); else mod_file->symbol_table.addSymbol(*name, type, *tex_name, *long_name); @@ -350,13 +352,16 @@ ParsingDriver::add_expression_variable(string *name) void ParsingDriver::declare_nonstationary_var(string *name, string *tex_name, string *long_name) { - if (tex_name != NULL) - if (long_name != NULL) - declare_endogenous(new string(*name), new string(*tex_name), new string(*long_name)); - else - declare_endogenous(new string(*name), new string(*tex_name)); - else + if (tex_name == NULL && long_name == NULL) declare_endogenous(new string(*name)); + else + if (tex_name == NULL) + declare_endogenous(new string(*name), NULL, new string(*long_name)); + else if (long_name == NULL) + declare_endogenous(new string(*name), new string(*tex_name)); + else + declare_endogenous(new string(*name), new string(*tex_name), new string(*long_name)); + declared_nonstationary_vars.push_back(mod_file->symbol_table.getID(*name)); mod_file->nonstationary_variables = true; delete name; diff --git a/SymbolTable.cc b/SymbolTable.cc index ae07a516..d9404d71 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -71,16 +71,30 @@ SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_na int SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException) +{ + string tex_name; + return addSymbol(name, type, construct_tex_name(name, tex_name)); +} + +int +SymbolTable::createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException) +{ + string tex_name; + return addSymbol(name, type, construct_tex_name(name, tex_name), long_name); +} + +string & +SymbolTable::construct_tex_name(const string &name, string &tex_name) { // Construct "tex_name" by prepending an antislash to all underscores in "name" - string tex_name = name; + tex_name = name; size_t pos = 0; while ((pos = tex_name.find('_', pos)) != string::npos) { tex_name.insert(pos, "\\"); pos += 2; } - return addSymbol(name, type, tex_name); + return tex_name; } void diff --git a/SymbolTable.hh b/SymbolTable.hh index dfc1e0ae..e078f267 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -191,6 +191,12 @@ public: //! Add a symbol without its TeX name (will be equal to its name) /*! Returns the symbol ID */ int addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException); + //! Tmp addSymbol function that creates a tex_name and declares a symbol + //! when the name and long_name are passed + /*! Returns the symbol ID */ + int createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException); + //! Create a tex_name based on the variable name + string &construct_tex_name(const string &name, string &tex_name); //! Adds an auxiliary variable for endogenous with lead >= 2 /*! \param[in] index Used to construct the variable name From 973f20dab58d8521394627316730b3d3fba65a17 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 10 Dec 2013 11:39:41 +0100 Subject: [PATCH 078/162] preprocessor: streamline addSymbol --- ParsingDriver.cc | 4 ++-- SymbolTable.cc | 50 ++++++++++++++++++------------------------------ SymbolTable.hh | 9 --------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index e551078b..c556220f 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -139,9 +139,9 @@ ParsingDriver::declare_symbol(const string *name, SymbolType type, const string mod_file->symbol_table.addSymbol(*name, type); else if (tex_name == NULL) - mod_file->symbol_table.createTexNameAndAddSymbolWithLongName(*name, type, *long_name); + mod_file->symbol_table.addSymbol(*name, type, "", *long_name); else if (long_name == NULL) - mod_file->symbol_table.addSymbol(*name, type, *tex_name); + mod_file->symbol_table.addSymbol(*name, type, *tex_name, ""); else mod_file->symbol_table.addSymbol(*name, type, *tex_name, *long_name); } diff --git a/SymbolTable.cc b/SymbolTable.cc index d9404d71..1c8652e5 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -52,49 +52,37 @@ SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_na throw AlreadyDeclaredException(name, false); } + string final_tex_name = tex_name; + if (final_tex_name.empty()) + { + final_tex_name = name; + size_t pos = 0; + while ((pos = final_tex_name.find('_', pos)) != string::npos) + { + final_tex_name.insert(pos, "\\"); + pos += 2; + } + } + + string final_long_name = long_name; + if (final_long_name.empty()) + final_long_name = name; + int id = size++; symbol_table[name] = id; type_table.push_back(type); name_table.push_back(name); - tex_name_table.push_back(tex_name); - long_name_table.push_back(long_name); + tex_name_table.push_back(final_tex_name); + long_name_table.push_back(final_long_name); return id; } -int -SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException) -{ - return addSymbol(name, type, tex_name, name); -} - int SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException) { - string tex_name; - return addSymbol(name, type, construct_tex_name(name, tex_name)); -} - -int -SymbolTable::createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException) -{ - string tex_name; - return addSymbol(name, type, construct_tex_name(name, tex_name), long_name); -} - -string & -SymbolTable::construct_tex_name(const string &name, string &tex_name) -{ - // Construct "tex_name" by prepending an antislash to all underscores in "name" - tex_name = name; - size_t pos = 0; - while ((pos = tex_name.find('_', pos)) != string::npos) - { - tex_name.insert(pos, "\\"); - pos += 2; - } - return tex_name; + return addSymbol(name, type, "", ""); } void diff --git a/SymbolTable.hh b/SymbolTable.hh index e078f267..49c20855 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -185,18 +185,9 @@ public: //! Add a symbol /*! Returns the symbol ID */ int addSymbol(const string &name, SymbolType type, const string &tex_name, const string &long_name) throw (AlreadyDeclaredException, FrozenException); - //! Add a symbol without its long name (will be equal to its name) - /*! Returns the symbol ID */ - int addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException); //! Add a symbol without its TeX name (will be equal to its name) /*! Returns the symbol ID */ int addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException); - //! Tmp addSymbol function that creates a tex_name and declares a symbol - //! when the name and long_name are passed - /*! Returns the symbol ID */ - int createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException); - //! Create a tex_name based on the variable name - string &construct_tex_name(const string &name, string &tex_name); //! Adds an auxiliary variable for endogenous with lead >= 2 /*! \param[in] index Used to construct the variable name From 1d7646a6e57190ea5af2a60520c519fbd2ce20ce Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 10 Dec 2013 18:17:31 +0100 Subject: [PATCH 079/162] For Matlab >= 2013a on osx, use renamed SDKROOT macro, MW_SDKROOT --- ModFile.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 56257c19..94f5a1a5 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -685,10 +685,17 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; # else // MacOS // MATLAB/MacOS - mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + mOutputFile << " if matlab_ver_less_than('8.1')" << endl; + mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " << basename << "_static.c " << basename << "_static_mex.c')" << endl; + mOutputFile << " else" << endl; + mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_static.c " << basename << "_static_mex.c')" << endl; + mOutputFile << " end" << endl; # endif #endif mOutputFile << "else" << endl // Octave From 9dfcf897f7c2162782e5b6d94b136ea76f1d1072 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Fri, 22 Nov 2013 21:09:04 +0100 Subject: [PATCH 080/162] adding 3rd order derivatives to Static Model for evaluation of Ramsey policy computed at order = 2 --- ComputingTasks.cc | 6 +-- ModFile.cc | 2 +- StaticModel.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++- StaticModel.hh | 2 +- 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index b5668549..ae764910 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -179,9 +179,9 @@ RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConso if (it != options_list.num_options.end()) { int order = atoi(it->second.c_str()); - if (order > 1) + if (order > 2) { - cerr << "ERROR: ramsey_policy: order > 1 is not yet implemented" << endl; + cerr << "ERROR: ramsey_policy: order > 2 is not implemented" << endl; exit(EXIT_FAILURE); } mod_file_struct.order_option = max(mod_file_struct.order_option, order + 1); @@ -968,7 +968,7 @@ PlannerObjectiveStatement::getPlannerObjective() const void PlannerObjectiveStatement::computingPass() { - model_tree->computingPass(eval_context_t(), false, true, false, false, false); + model_tree->computingPass(eval_context_t(), false, true, true, false, false, false); } void diff --git a/ModFile.cc b/ModFile.cc index 94f5a1a5..2c44e3f2 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -470,7 +470,7 @@ ModFile::computingPass(bool no_tmp_terms) const bool paramsDerivatives = mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation; static_model.computingPass(global_eval_context, no_tmp_terms, static_hessian, - paramsDerivatives, block, byte_code); + false, paramsDerivatives, block, byte_code); } // Set things to compute for dynamic model if (mod_file_struct.simul_present || mod_file_struct.check_present diff --git a/StaticModel.cc b/StaticModel.cc index c78324ea..d82efaed 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1047,7 +1047,7 @@ StaticModel::collect_first_order_derivatives_endogenous() } void -StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool paramsDerivatives, bool block, bool bytecode) +StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatives, bool paramsDerivatives, bool block, bool bytecode) { initializeVariablesAndEquations(); @@ -1070,6 +1070,12 @@ StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms computeHessian(vars); } + if (thirdDerivatives) + { + cout << " - order 3" << endl; + computeThirdDerivatives(vars); + } + if (paramsDerivatives) { cout << " - derivatives of Jacobian/Hessian w.r. to parameters" << endl; @@ -1142,7 +1148,7 @@ StaticModel::writeStaticMFile(const string &func_name) const exit(EXIT_FAILURE); } - output << "function [residual, g1, g2] = " << func_name + "_static(y, x, params)" << endl + output << "function [residual, g1, g2, g3] = " << func_name + "_static(y, x, params)" << endl << "%" << endl << "% Status : Computes static model for Dynare" << endl << "%" << endl @@ -1160,6 +1166,9 @@ StaticModel::writeStaticMFile(const string &func_name) const << "% g2 [M_.endo_nbr by (M_.endo_nbr)^2] double Hessian matrix of the static model equations;" << endl << "% columns: equations in order of declaration" << endl << "% rows: variables in declaration order" << endl + << "% g3 [M_.endo_nbr by (M_.endo_nbr)^3] double Third derivatives matrix of the static model equations;" << endl + << "% columns: variables in declaration order" << endl + << "% rows: equations in order of declaration" << endl << "%" << endl << "%" << endl << "% Warning : this file is generated automatically by Dynare" << endl @@ -1176,6 +1185,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const ostringstream model_output; // Used for storing model equations ostringstream jacobian_output; // Used for storing jacobian equations ostringstream hessian_output; // Used for storing Hessian equations + ostringstream third_derivatives_output; // Used for storing third order derivatives equations ExprNodeOutputType output_type = (use_dll ? oCStaticModel : oMatlabStaticModel); deriv_node_temp_terms_t tef_terms; @@ -1185,6 +1195,10 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const writeModelEquations(model_output, output_type); + int nrows = equations.size(); + int JacobianColsNbr = symbol_table.endo_nbr(); + int hessianColsNbr = JacobianColsNbr*JacobianColsNbr; + // Write Jacobian w.r. to endogenous only for (first_derivatives_t::const_iterator it = first_derivatives.begin(); it != first_derivatives.end(); it++) @@ -1247,6 +1261,64 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const } } + // Writing third derivatives + k = 0; // Keep the line of a 3rd derivative in v3 + for (third_derivatives_t::const_iterator it = third_derivatives.begin(); + it != third_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second.first; + int var3 = it->first.second.second.second; + expr_t d3 = it->second; + + int id1 = getSymbIDByDerivID(var1); + int id2 = getSymbIDByDerivID(var2); + int id3 = getSymbIDByDerivID(var3); + + + // Reference column number for the g3 matrix + int ref_col = id1 * hessianColsNbr + id2 * JacobianColsNbr + id3; + + sparseHelper(3, third_derivatives_output, k, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, third_derivatives_output, k, 1, output_type); + third_derivatives_output << "=" << ref_col + 1 << ";" << endl; + + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << "="; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); + third_derivatives_output << ";" << endl; + + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + set cols; + cols.insert(id1 * hessianColsNbr + id3 * JacobianColsNbr + id2); + cols.insert(id2 * hessianColsNbr + id1 * JacobianColsNbr + id3); + cols.insert(id2 * hessianColsNbr + id3 * JacobianColsNbr + id1); + cols.insert(id3 * hessianColsNbr + id1 * JacobianColsNbr + id2); + cols.insert(id3 * hessianColsNbr + id2 * JacobianColsNbr + id1); + + int k2 = 1; // Keeps the offset of the permutation relative to k + for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) + if (*it2 != ref_col) + { + sparseHelper(3, third_derivatives_output, k+k2, 0, output_type); + third_derivatives_output << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, third_derivatives_output, k+k2, 1, output_type); + third_derivatives_output << "=" << *it2 + 1 << ";" << endl; + + sparseHelper(3, third_derivatives_output, k+k2, 2, output_type); + third_derivatives_output << "="; + sparseHelper(3, third_derivatives_output, k, 2, output_type); + third_derivatives_output << ";" << endl; + + k2++; + } + k += k2; + } + if (!use_dll) { StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl @@ -1280,6 +1352,20 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const else StaticOutput << " g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl; StaticOutput << "end" << endl; + // Initialize g3 matrix + StaticOutput << "if nargout >= 4," << endl + << " %" << endl + << " % Third order derivatives" << endl + << " %" << endl + << endl; + int ncols = hessianColsNbr * JacobianColsNbr; + if (third_derivatives.size()) + StaticOutput << " v3 = zeros(" << NNZDerivatives[2] << ",3);" << endl + << third_derivatives_output.str() + << " g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl; + else // Either 3rd derivatives is all zero, or we didn't compute it + StaticOutput << " g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl; + } else { @@ -1305,6 +1391,14 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll) const << " {" << endl << hessian_output.str() << " }" << endl; + if (third_derivatives.size()) + StaticOutput << " /* Third derivatives for endogenous and exogenous variables */" << endl + << " if (v3 == NULL)" << endl + << " return;" << endl + << " else" << endl + << " {" << endl + << third_derivatives_output.str() + << " }" << endl; } } diff --git a/StaticModel.hh b/StaticModel.hh index f193c4c4..e76a5f0f 100644 --- a/StaticModel.hh +++ b/StaticModel.hh @@ -161,7 +161,7 @@ public: \param hessian whether 2nd derivatives w.r. to exo, exo_det and endo should be computed \param paramsDerivatives whether 2nd derivatives w.r. to a pair (endo/exo/exo_det, parameter) should be computed */ - void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian,bool paramsDerivatives, bool block, bool bytecode); + void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatices, bool paramsDerivatives, bool block, bool bytecode); //! Adds informations for simulation in a binary file for a block decomposed model void Write_Inf_To_Bin_File_Block(const string &static_basename, const string &bin_basename, const int &num, From 16ac13a9ab917ee71b835bd432ad972e0388732b Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Fri, 6 Dec 2013 18:34:32 +0100 Subject: [PATCH 081/162] osr: added optimal value of parameters to oo_.osr.optim_params; moved osr_example to ./tests/optimal_policy --- ComputingTasks.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index ae764910..05d6576d 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -834,7 +834,7 @@ OsrStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); symbol_list.writeOutput("var_list_", output); - output << "osr(var_list_,osr_params_,obj_var_,optim_weights_);\n"; + output << "oo_.osr = osr(var_list_,osr_params_,obj_var_,optim_weights_);\n"; } OptimWeightsStatement::OptimWeightsStatement(const var_weights_t &var_weights_arg, From fe24d9bcab20c4d3fdd6f7b8c62117b804e8779c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Wed, 18 Dec 2013 16:39:41 +0100 Subject: [PATCH 082/162] Closes #567. --- DynareBison.yy | 4 +++- DynareFlex.ll | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index ea7d82e0..3e95184c 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -110,7 +110,7 @@ class ParsingDriver; %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT -%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS +%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE %token NAME @@ -1633,6 +1633,7 @@ estimation_options : o_datafile | o_geweke_interval | o_mcmc_jumping_covariance | o_irf_plot_threshold + | o_posterior_max_subsample_draws ; list_optim_option : QUOTED_STRING COMMA QUOTED_STRING @@ -2423,6 +2424,7 @@ o_subsample_name : symbol EQUAL date_expr ':' date_expr ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; o_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("mh_replic", $3); }; +o_posterior_max_subsample_draws : POSTERIOR_MAX_SUBSAMPLE_DRAWS EQUAL INT_NUMBER { driver.option_num("posterior_max_subsample_draws", $3); }; o_mh_drop : MH_DROP EQUAL non_negative_number { driver.option_num("mh_drop", $3); }; o_mh_jscale : MH_JSCALE EQUAL non_negative_number { driver.option_num("mh_jscale", $3); }; o_optim : OPTIM EQUAL '(' optim_options ')'; diff --git a/DynareFlex.ll b/DynareFlex.ll index 7343a5ad..9b6b5ed4 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -284,6 +284,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 dsge_var {return token::DSGE_VAR;} dsge_varlag {return token::DSGE_VARLAG;} moments_varendo {return token::MOMENTS_VARENDO;} +posterior_max_subsample_draws {return token::POSTERIOR_MAX_SUBSAMPLE_DRAWS;} filtered_vars {return token::FILTERED_VARS;} filter_step_ahead {return token::FILTER_STEP_AHEAD;} relative_irf {return token::RELATIVE_IRF;} From 57d62a3171c34e2660cc0df3dddbd3294c6f2e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 27 Dec 2013 12:18:57 +0100 Subject: [PATCH 083/162] Fix bug in deterministic conditional forecast. The type of the first shock was not set according to the user request. Concretely, it would be random, and almost always a perfect foresight shock. --- ParsingDriver.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 98985b1c..017c9361 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -215,7 +215,7 @@ private: bool nostrict; public: - ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) : warnings(warnings_arg), nostrict(nostrict_arg) { }; + ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) : det_shocks_expectation_pf(false), warnings(warnings_arg), nostrict(nostrict_arg) { }; //! Starts parsing, and constructs the MOD file representation /*! The returned pointer should be deleted after use */ From 2975072c1bc9d4064466c6c75471b5d3ca860f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Fri, 27 Dec 2013 18:35:53 +0100 Subject: [PATCH 084/162] Added new option for perfect foresight simulations (sim1 routine, available only with stack_solve_algo==0). Try to reduce the size of the nonlinear system of equations by skipping the (last) periods for wich the residuals are already (almost) zero. The number of periods is not constant during the Newton, the effective number of periods for each iteration of the Newton is available in oo_.deterministic_simulation.vperiods. --- DynareBison.yy | 4 +++- DynareFlex.ll | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3e95184c..8e806bd9 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -122,7 +122,7 @@ class ParsingDriver; %token QUOTED_STRING %token QZ_CRITERIUM QZ_ZERO_THRESHOLD FULL DSGE_VAR DSGE_VARLAG DSGE_PRIOR_WEIGHT TRUNCATE %token RELATIVE_IRF REPLIC SIMUL_REPLIC RPLOT SAVE_PARAMS_AND_STEADY_STATE PARAMETER_UNCERTAINTY -%token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SIMULATION_TYPE +%token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SIMULATION_TYPE ENDOGENOUS_TERMINAL_PERIOD %token SMOOTHER SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL STOCHASTIC SOLVE_ALGO SOLVER_PERIODS %token STDERR STEADY STOCH_SIMUL SURPRISE SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME %token TEX RAMSEY_POLICY PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL @@ -986,6 +986,7 @@ simul_options : o_periods | o_markowitz | o_minimal_solving_periods | o_simul_maxit + | o_endogenous_terminal_period ; external_function : EXTERNAL_FUNCTION '(' external_function_options_list ')' ';' @@ -2329,6 +2330,7 @@ o_simul_algo : SIMUL_ALGO EQUAL INT_NUMBER { driver.error("simul_algo=1 option is no longer supported"); }; o_stack_solve_algo : STACK_SOLVE_ALGO EQUAL INT_NUMBER { driver.option_num("stack_solve_algo", $3); }; +o_endogenous_terminal_period : ENDOGENOUS_TERMINAL_PERIOD { driver.option_num("endogenous_terminal_period", "1"); }; o_linear : LINEAR { driver.linear(); }; o_order : ORDER EQUAL INT_NUMBER { driver.option_num("order", $3); }; o_replic : REPLIC EQUAL INT_NUMBER { driver.option_num("replic", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index 9b6b5ed4..98dda266 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -295,6 +295,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 nocorr {return token::NOCORR;} optim {return token::OPTIM;} periods {return token::PERIODS;} +endogenous_terminal_period {return token::ENDOGENOUS_TERMINAL_PERIOD;} sub_draws {return token::SUB_DRAWS;} minimal_solving_periods {return token::MINIMAL_SOLVING_PERIODS;} markowitz {return token::MARKOWITZ;} From 10c4b8a08065cb886032688bf2fe00063bbc4b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Sat, 28 Dec 2013 20:44:30 +0100 Subject: [PATCH 085/162] Fix compatibility issue with Bison 3. Closes #570 --- DynareBison.yy | 26 +++++++++++++------------- macro/MacroBison.yy | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3e95184c..4b63badb 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -38,23 +38,23 @@ class ParsingDriver; /* Little hack: we redefine the macro which computes the locations, because we need to access the location from within the parsing driver for error and warning messages. */ -#define YYLLOC_DEFAULT(Current, Rhs, N) \ - do { \ - if (N) \ - { \ - (Current).begin = (Rhs)[1].begin; \ - (Current).end = (Rhs)[N].end; \ - } \ - else \ - { \ - (Current).begin = (Current).end = (Rhs)[0].end; \ - } \ - driver.location = (Current); \ +#define YYLLOC_DEFAULT(Current, Rhs, N) \ + do { \ + if (N) \ + { \ + (Current).begin = YYRHSLOC(Rhs, 1).begin; \ + (Current).end = YYRHSLOC(Rhs, N).end; \ + } \ + else \ + { \ + (Current).begin = (Current).end = YYRHSLOC(Rhs, 0).end; \ + } \ + driver.location = (Current); \ } while(false) %} -%name-prefix="Dynare" +%name-prefix "Dynare" %parse-param { ParsingDriver &driver } %lex-param { ParsingDriver &driver } diff --git a/macro/MacroBison.yy b/macro/MacroBison.yy index a7a6456e..260bc77d 100644 --- a/macro/MacroBison.yy +++ b/macro/MacroBison.yy @@ -35,7 +35,7 @@ using namespace std; class MacroDriver; %} -%name-prefix="Macro" +%name-prefix "Macro" %parse-param { MacroDriver &driver } %parse-param { ostream &out } From 1afd46b880d8d076f08f76edfd5724ed9c8284fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Sat, 28 Dec 2013 21:16:47 +0100 Subject: [PATCH 086/162] Refactor bison files using new prologue syntax. --- DynareBison.yy | 53 ++++++++++++++++++++------------------------- macro/MacroBison.yy | 22 ++++++++----------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 4b63badb..3f8b24c3 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -18,41 +18,19 @@ */ %skeleton "lalr1.cc" -%require "2.3" +%require "2.4" %defines -/* Prologue: - In Bison <= 2.3, it is inserted in both the .cc and .hh files. - In Bison >= 2.3a, it is inserted only in the .cc file. - Since Bison 2.4, the new %code directives provide a cleaner way of dealing - with the prologue. -*/ -%{ +%code top { using namespace std; class ParsingDriver; +} +%code requires { #include "ExprNode.hh" #include "CodeInterpreter.hh" - -/* Little hack: we redefine the macro which computes the locations, because - we need to access the location from within the parsing driver for error - and warning messages. */ -#define YYLLOC_DEFAULT(Current, Rhs, N) \ - do { \ - if (N) \ - { \ - (Current).begin = YYRHSLOC(Rhs, 1).begin; \ - (Current).end = YYRHSLOC(Rhs, N).end; \ - } \ - else \ - { \ - (Current).begin = (Current).end = YYRHSLOC(Rhs, 0).end; \ - } \ - driver.location = (Current); \ - } while(false) - -%} +} %name-prefix "Dynare" @@ -79,7 +57,24 @@ class ParsingDriver; PriorDistributions prior_distributions_val; }; -%{ +%code { +/* Little hack: we redefine the macro which computes the locations, because + we need to access the location from within the parsing driver for error + and warning messages. */ +#define YYLLOC_DEFAULT(Current, Rhs, N) \ + do { \ + if (N) \ + { \ + (Current).begin = YYRHSLOC(Rhs, 1).begin; \ + (Current).end = YYRHSLOC(Rhs, N).end; \ + } \ + else \ + { \ + (Current).begin = (Current).end = YYRHSLOC(Rhs, 0).end; \ + } \ + driver.location = (Current); \ + } while(false) + #include "ParsingDriver.hh" /* this "connects" the bison parser in the driver to the flex scanner class @@ -87,7 +82,7 @@ class ParsingDriver; * current lexer object of the driver context. */ #undef yylex #define yylex driver.lexer->lex -%} +} %token AIM_SOLVER ANALYTIC_DERIVATION AR AUTOCORR %token BAYESIAN_IRF BETA_PDF BLOCK USE_CALIBRATION diff --git a/macro/MacroBison.yy b/macro/MacroBison.yy index 260bc77d..ea838ad3 100644 --- a/macro/MacroBison.yy +++ b/macro/MacroBison.yy @@ -18,22 +18,14 @@ */ %skeleton "lalr1.cc" -%require "2.3" +%require "2.4" %defines -/* Prologue: - In Bison <= 2.3, it is inserted in both the .cc and .hh files. - In Bison >= 2.3a, it is inserted only in the .cc file. - Since Bison 2.4, the new %code directives provide a cleaner way of dealing - with the prologue. -*/ -%{ +%code top { using namespace std; -#include "MacroValue.hh" - class MacroDriver; -%} +} %name-prefix "Macro" @@ -51,6 +43,10 @@ class MacroDriver; %debug %error-verbose +%code requires { +#include "MacroValue.hh" +} + %union { string *string_val; @@ -58,7 +54,7 @@ class MacroDriver; const MacroValue *mv; }; -%{ +%code { #include // Pour atoi() #include "MacroDriver.hh" @@ -77,7 +73,7 @@ class MacroDriver; driver.error(loc, e.message); \ } -%} +} %token DEFINE LINE FOR IN IF ELSE ENDIF ECHO_DIR ERROR IFDEF IFNDEF %token LPAREN RPAREN LBRACKET RBRACKET EQUAL EOL LENGTH From e0f662129a619f2addab128e7cede615030f0f60 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 30 Dec 2013 14:45:17 +0100 Subject: [PATCH 087/162] preprocessor: namespace fixes to remove clang warnings --- DynareBison.yy | 2 -- ExprNode.hh | 4 ++-- macro/MacroBison.yy | 2 -- macro/MacroValue.hh | 4 ++-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3f8b24c3..1d67a8d1 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -22,8 +22,6 @@ %defines %code top { -using namespace std; - class ParsingDriver; } diff --git a/ExprNode.hh b/ExprNode.hh index 65199d66..aebdead2 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -20,13 +20,13 @@ #ifndef _EXPR_NODE_HH #define _EXPR_NODE_HH -using namespace std; - #include #include #include #include +using namespace std; + #include "SymbolTable.hh" #include "CodeInterpreter.hh" #include "ExternalFunctionsTable.hh" diff --git a/macro/MacroBison.yy b/macro/MacroBison.yy index ea838ad3..272af6a0 100644 --- a/macro/MacroBison.yy +++ b/macro/MacroBison.yy @@ -22,8 +22,6 @@ %defines %code top { -using namespace std; - class MacroDriver; } diff --git a/macro/MacroValue.hh b/macro/MacroValue.hh index d79c024c..25016478 100644 --- a/macro/MacroValue.hh +++ b/macro/MacroValue.hh @@ -20,12 +20,12 @@ #ifndef _MACRO_VALUE_HH #define _MACRO_VALUE_HH -using namespace std; - #include #include #include +using namespace std; + class MacroDriver; //! Base class for representing values in macro language From e2d9f2476c24cd570dfe835bdb18be8bfb487950 Mon Sep 17 00:00:00 2001 From: ferhat Date: Tue, 31 Dec 2013 01:57:30 +0100 Subject: [PATCH 088/162] The previous syntax used to call the conditional forecast using the extended path method was problematic. It does not allow to match the endogenous and the shock that are flipped. This is problematic as soon as the forecast periods are heterogeneous or the type of expectation (surprise or perfect foresight) are different. We get rid of the previous syntax. --- DynareBison.yy | 20 +++----------------- DynareFlex.ll | 5 ----- ParsingDriver.cc | 12 ++---------- ParsingDriver.hh | 6 +----- Shocks.cc | 14 ++++++-------- Shocks.hh | 2 +- 6 files changed, 13 insertions(+), 46 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index b6ec3431..c04819b7 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -89,7 +89,7 @@ class ParsingDriver; %token BVAR_PRIOR_MU BVAR_PRIOR_OMEGA BVAR_PRIOR_TAU BVAR_PRIOR_TRAIN %token BVAR_REPLIC BYTECODE ALL_VALUES_REQUIRED %token CALIB_SMOOTHER CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF CYCLE_REDUCTION LOGARITHMIC_REDUCTION -%token DATAFILE FILE DETERMINISTIC DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS +%token DATAFILE FILE DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT EXTENDED_PATH ENDOGENOUS_PRIOR %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME %token FLOAT_NUMBER DATES @@ -115,8 +115,8 @@ class ParsingDriver; %token QUOTED_STRING %token QZ_CRITERIUM QZ_ZERO_THRESHOLD FULL DSGE_VAR DSGE_VARLAG DSGE_PRIOR_WEIGHT TRUNCATE %token RELATIVE_IRF REPLIC SIMUL_REPLIC RPLOT SAVE_PARAMS_AND_STEADY_STATE PARAMETER_UNCERTAINTY -%token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SIMULATION_TYPE ENDOGENOUS_TERMINAL_PERIOD -%token SMOOTHER SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL STOCHASTIC SOLVE_ALGO SOLVER_PERIODS +%token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD +%token SMOOTHER SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS %token STDERR STEADY STOCH_SIMUL SURPRISE SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME %token TEX RAMSEY_POLICY PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL %token TEX_NAME @@ -877,12 +877,6 @@ period_list : period_list COMMA INT_NUMBER { driver.add_period($1); } ; -expectation_type : PERFECT_FORESIGHT - { driver.add_expectation_pf(true); } - | SURPRISE - { driver.add_expectation_pf(false); } - ; - sigma_e : SIGMA_E EQUAL '[' triangular_matrix ']' ';' { driver.do_sigma_e(); }; value_list : value_list COMMA '(' expression ')' @@ -2233,7 +2227,6 @@ conditional_forecast_option : o_periods | o_conf_sig | o_controlled_varexo | o_parameter_set - | o_simulation_type ; plot_conditional_forecast : PLOT_CONDITIONAL_FORECAST symbol_list ';' @@ -2252,8 +2245,6 @@ conditional_forecast_paths_shock_list : conditional_forecast_paths_shock_elem conditional_forecast_paths_shock_elem : VAR symbol ';' PERIODS period_list ';' VALUES value_list ';' { driver.add_det_shock($2, true); } - | VAR symbol ';' PERIODS period_list ';' VALUES value_list ';' EXPECTATION expectation_type ';' - { driver.add_det_shock($2, true); } ; steady_state_model : STEADY_STATE_MODEL ';' { driver.begin_steady_state_model(); } @@ -2563,11 +2554,6 @@ o_parameter_set : PARAMETER_SET EQUAL PRIOR_MODE | PARAMETER_SET EQUAL CALIBRATION { driver.option_str("parameter_set", "calibration"); } ; -o_simulation_type : SIMULATION_TYPE EQUAL DETERMINISTIC - { driver.option_str("simulation_type", "deterministic"); } - | SIMULATION_TYPE EQUAL STOCHASTIC - { driver.option_str("simulation_type", "stochastic"); } - ; o_ms_drop : DROP EQUAL INT_NUMBER { driver.option_num("ms.drop", $3); }; o_ms_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("ms.mh_replic", $3); }; o_freq : FREQ EQUAL INT_NUMBER diff --git a/DynareFlex.ll b/DynareFlex.ll index 98dda266..f8395e20 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -505,9 +505,6 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 posterior_mode {return token::POSTERIOR_MODE; } posterior_mean {return token::POSTERIOR_MEAN; } posterior_median {return token::POSTERIOR_MEDIAN; } -simulation_type {return token::SIMULATION_TYPE; } -deterministic {return token::DETERMINISTIC; } -stochastic {return token::STOCHASTIC; } k_order_solver {return token::K_ORDER_SOLVER; } filter_covariance {return token::FILTER_COVARIANCE; } filter_decomposition {return token::FILTER_DECOMPOSITION; } @@ -537,8 +534,6 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 stderr {return token::STDERR;} values {return token::VALUES;} corr {return token::CORR;} -surprise {return token::SURPRISE;} -perfect_foresight {return token::PERFECT_FORESIGHT;} periods {return token::PERIODS;} cutoff {return token::CUTOFF;} mfs {return token::MFS;} diff --git a/ParsingDriver.cc b/ParsingDriver.cc index c556220f..f4bf3a75 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -670,12 +670,10 @@ ParsingDriver::add_det_shock(string *var, bool conditional_forecast) v.push_back(dse); } - det_shocks[symb_id].first = v; - det_shocks[symb_id].second = det_shocks_expectation_pf; - + det_shocks[symb_id] = v; + det_shocks_periods.clear(); det_shocks_values.clear(); - det_shocks_expectation_pf = false; delete var; } @@ -795,12 +793,6 @@ ParsingDriver::add_value(string *v) det_shocks_values.push_back(id); } -void -ParsingDriver::add_expectation_pf(bool pf) -{ - det_shocks_expectation_pf = pf; -} - void ParsingDriver::begin_svar_identification() { diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 017c9361..e856a2e0 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -134,8 +134,6 @@ private: vector > det_shocks_periods; //! Temporary storage for values of deterministic shocks vector det_shocks_values; - //! Temporary storage for perfect foresight of deterministic shocks in conditional forecast - bool det_shocks_expectation_pf; //! Temporary storage for variances of shocks ShocksStatement::var_and_std_shocks_t var_shocks; //! Temporary storage for standard errors of shocks @@ -215,7 +213,7 @@ private: bool nostrict; public: - ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) : det_shocks_expectation_pf(false), warnings(warnings_arg), nostrict(nostrict_arg) { }; + ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) : warnings(warnings_arg), nostrict(nostrict_arg) { }; //! Starts parsing, and constructs the MOD file representation /*! The returned pointer should be deleted after use */ @@ -347,8 +345,6 @@ public: //! Adds a deterministic shock value /*! \param v a string containing a (possibly negative) numeric constant */ void add_value(string *v); - //! Adds a expectation type for conditional forecast with deterministic simulation - void add_expectation_pf(bool pf); //! Writes a Sigma_e block void do_sigma_e(); //! Ends row of Sigma_e block diff --git a/Shocks.cc b/Shocks.cc index 4b4037b2..73032fbb 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -44,11 +44,11 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const bool exo_det = (symbol_table.getType(it->first) == eExogenousDet); int set_shocks_index = ((int) mshocks) + 2 * ((int) exo_det); - for (size_t i = 0; i < it->second.first.size(); i++) + for (size_t i = 0; i < it->second.size(); i++) { - const int &period1 = it->second.first[i].period1; - const int &period2 = it->second.first[i].period2; - const expr_t value = it->second.first[i].value; + const int &period1 = it->second[i].period1; + const int &period2 = it->second[i].period2; + const expr_t value = it->second[i].value; if (period1 == period2) { @@ -339,7 +339,7 @@ ConditionalForecastPathsStatement::checkPass(ModFileStructure &mod_file_struct, it != paths.end(); it++) { int this_path_length = 0; - const vector &elems = it->second.first; + const vector &elems = it->second; for (int i = 0; i < (int) elems.size(); i++) // Period1 < Period2, as enforced in ParsingDriver::add_period() this_path_length = max(this_path_length, elems[i].period2); @@ -368,16 +368,14 @@ ConditionalForecastPathsStatement::writeOutput(ostream &output, const string &ba if (it == paths.begin()) { output << "constrained_vars_ = " << it->first +1 << ";" << endl; - output << "constrained_perfect_foresight_ = " << it->second.second << ";" << endl; } else { output << "constrained_vars_ = [constrained_vars_; " << it->first +1 << "];" << endl; - output << "constrained_perfect_foresight_ = [constrained_perfect_foresight_; " << it->second.second << "];" << endl; } - const vector &elems = it->second.first; + const vector &elems = it->second; for (int i = 0; i < (int) elems.size(); i++) for (int j = elems[i].period1; j <= elems[i].period2; j++) { diff --git a/Shocks.hh b/Shocks.hh index 539a4033..68d8eee2 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -41,7 +41,7 @@ public: }; //The boolean element indicates if the shock is a surprise (false) or a perfect foresight (true) shock. //This boolean is used only in case of conditional forecast with extended path method (simulation_type = deterministic). - typedef map, bool> > det_shocks_t; + typedef map > det_shocks_t; protected: //! Is this statement a "mshocks" statement ? (instead of a "shocks" statement) const bool mshocks; From 12b9d6d8b9f708a7ff366f21a73af7d2a285e7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 3 Jan 2014 12:09:39 +0100 Subject: [PATCH 089/162] Remove more remnants from old deterministic conditional forecast syntax. --- ComputingTasks.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 05d6576d..8f549dbf 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -1338,7 +1338,7 @@ void ConditionalForecastStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output, "options_cond_fcst_"); - output << "imcforecast(constrained_paths_, constrained_vars_, options_cond_fcst_, constrained_perfect_foresight_);" << endl; + output << "imcforecast(constrained_paths_, constrained_vars_, options_cond_fcst_);" << endl; } PlotConditionalForecastStatement::PlotConditionalForecastStatement(int periods_arg, const SymbolList &symbol_list_arg) : From 314f9ccb0dfab86bf53ca845528d2ade424f7fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 13 Jan 2014 14:56:29 +0100 Subject: [PATCH 090/162] It's bison 2.5 that's actually required now. --- DynareBison.yy | 4 ++-- macro/MacroBison.yy | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index c04819b7..91233f31 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -18,7 +18,7 @@ */ %skeleton "lalr1.cc" -%require "2.4" +%require "2.5" %defines %code top { diff --git a/macro/MacroBison.yy b/macro/MacroBison.yy index 272af6a0..2bd8072a 100644 --- a/macro/MacroBison.yy +++ b/macro/MacroBison.yy @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2013 Dynare Team + * Copyright (C) 2008-2014 Dynare Team * * This file is part of Dynare. * @@ -18,7 +18,7 @@ */ %skeleton "lalr1.cc" -%require "2.4" +%require "2.5" %defines %code top { From 4d2f91813bfccc88d4d01613b2f4c82ce8f5a9a1 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 7 Jan 2014 11:56:39 +0100 Subject: [PATCH 091/162] build system: replace hard-coded flex include dir with that found by autoconf, #575 --- Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index 6556fd44..a9cecac8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -61,7 +61,7 @@ dynare_m_LDADD = macro/libmacro.a DynareFlex.cc FlexLexer.h: DynareFlex.ll $(LEX) -oDynareFlex.cc DynareFlex.ll - cp /usr/include/FlexLexer.h . + cp $(LEXINC)/FlexLexer.h . DynareBison.cc DynareBison.hh location.hh stack.hh position.hh: DynareBison.yy $(YACC) -o DynareBison.cc DynareBison.yy From 47a8fd0147b53157c096808d71ba56d915cf8046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 27 Jan 2014 16:41:43 +0100 Subject: [PATCH 092/162] Add line numbers of equations in preprocessor error messages. Closes #584 --- DynamicModel.cc | 39 ++++++++++++++++++++------------------- DynamicModel.hh | 7 +++++-- ModelTree.cc | 11 ++++++----- ModelTree.hh | 11 +++++++---- ParsingDriver.cc | 8 ++++---- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index ecd0881b..87239eea 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -3427,9 +3427,8 @@ DynamicModel::cloneDynamic(DynamicModel &dynamic_model) const dynamic_model.AddLocalVariable(it->first, it->second->cloneDynamic(dynamic_model)); // Convert equations - for (vector::const_iterator it = equations.begin(); - it != equations.end(); it++) - dynamic_model.addEquation((*it)->cloneDynamic(dynamic_model)); + for (size_t i = 0; i < equations.size(); i++) + dynamic_model.addEquation(equations[i]->cloneDynamic(dynamic_model), equations_lineno[i]); // Convert auxiliary equations for (deque::const_iterator it = aux_equations.begin(); @@ -3437,18 +3436,18 @@ DynamicModel::cloneDynamic(DynamicModel &dynamic_model) const dynamic_model.addAuxEquation((*it)->cloneDynamic(dynamic_model)); // Convert static_only equations - for (vector::const_iterator it = static_only_equations.begin(); - it != static_only_equations.end(); it++) - dynamic_model.addStaticOnlyEquation((*it)->cloneDynamic(dynamic_model)); + for (size_t i = 0; i < static_only_equations.size(); i++) + dynamic_model.addStaticOnlyEquation(static_only_equations[i]->cloneDynamic(dynamic_model), + static_only_equations_lineno[i]); } void DynamicModel::replaceMyEquations(DynamicModel &dynamic_model) const { dynamic_model.equations.clear(); - for (vector::const_iterator it = equations.begin(); - it != equations.end(); it++) - dynamic_model.addEquation((*it)->cloneDynamic(dynamic_model)); + for (size_t i = 0; i < equations.size(); i++) + dynamic_model.addEquation(equations[i]->cloneDynamic(dynamic_model), + equations_lineno[i]); } void @@ -3468,7 +3467,7 @@ DynamicModel::computeRamseyPolicyFOCs(const StaticModel &static_model) // Add Planner Objective to equations to include in computeDerivIDs assert(static_model.equations.size() == 1); - addEquation(static_model.equations[0]->cloneDynamic(*this)); + addEquation(static_model.equations[0]->cloneDynamic(*this), static_model.equations_lineno[0]); // Get max endo lead and max endo lag set > dynvars; @@ -3513,7 +3512,7 @@ DynamicModel::computeRamseyPolicyFOCs(const StaticModel &static_model) } equations.clear(); - addEquation(AddEqual(lagrangian, Zero)); + addEquation(AddEqual(lagrangian, Zero), -1); computeDerivIDs(); //Compute derivatives and overwrite equations @@ -3527,7 +3526,7 @@ DynamicModel::computeRamseyPolicyFOCs(const StaticModel &static_model) // Add new equations equations.clear(); for (int i = 0; i < (int) neweqs.size(); i++) - addEquation(neweqs[i]); + addEquation(neweqs[i], -1); } void @@ -3559,11 +3558,11 @@ DynamicModel::toStatic(StaticModel &static_model) const // If yes, replace it by an equation marked [static] if (is_dynamic_only) { - static_model.addEquation(static_only_equations[static_only_index]->toStatic(static_model)); + static_model.addEquation(static_only_equations[static_only_index]->toStatic(static_model), static_only_equations_lineno[static_only_index]); static_only_index++; } else - static_model.addEquation(equations[i]->toStatic(static_model)); + static_model.addEquation(equations[i]->toStatic(static_model), equations_lineno[i]); } // Convert auxiliary equations @@ -3795,7 +3794,8 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context double nearZero = testeq->getDerivative(endogit->second)->eval(eval_context); // eval d F / d Trend d Endog if (fabs(nearZero) > ZERO_BAND) { - cerr << "ERROR: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " w.r.t. trend variable " + cerr << "ERROR: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " (line " + << equations_lineno[eq] << ") w.r.t. trend variable " << symbol_table.getName(it->first.first) << " and endogenous variable " << symbol_table.getName(endogit->first.first) << " is not null. " << endl; exit(EXIT_FAILURE); @@ -4075,7 +4075,7 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type, bool deterministic_model // Add new equations for (int i = 0; i < (int) neweqs.size(); i++) - addEquation(neweqs[i]); + addEquation(neweqs[i], -1); // Order of auxiliary variable definition equations: // - expectation (entered before this function is called) @@ -4135,7 +4135,7 @@ DynamicModel::substituteExpectation(bool partial_information_model) // Add new equations for (int i = 0; i < (int) neweqs.size(); i++) - addEquation(neweqs[i]); + addEquation(neweqs[i], -1); // Add the new set of equations at the *beginning* of aux_equations copy(neweqs.rbegin(), neweqs.rend(), front_inserter(aux_equations)); @@ -4256,12 +4256,13 @@ DynamicModel::isModelLocalVariableUsed() const } void -DynamicModel::addStaticOnlyEquation(expr_t eq) +DynamicModel::addStaticOnlyEquation(expr_t eq, int lineno) { BinaryOpNode *beq = dynamic_cast(eq); assert(beq != NULL && beq->get_op_code() == oEqual); static_only_equations.push_back(beq); + static_only_equations_lineno.push_back(lineno); } size_t diff --git a/DynamicModel.hh b/DynamicModel.hh index 5f32ed11..07716efe 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -35,6 +35,9 @@ private: /*! They will be used in toStatic() to replace equations marked as [dynamic] */ vector static_only_equations; + //! Stores line numbers of equations declared as [static] + vector static_only_equations_lineno; + typedef map, int> deriv_id_table_t; //! Maps a pair (symbol_id, lag) to a deriv ID deriv_id_table_t deriv_id_table; @@ -237,7 +240,7 @@ public: void replaceMyEquations(DynamicModel &dynamic_model) const; //! Adds an equation marked as [static] - void addStaticOnlyEquation(expr_t eq); + void addStaticOnlyEquation(expr_t eq, int lineno); //! Returns number of static only equations size_t staticOnlyEquationsNbr() const; diff --git a/ModelTree.cc b/ModelTree.cc index f51eeb6f..5daf09df 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -270,7 +270,7 @@ ModelTree::evaluateAndReduceJacobian(const eval_context_t &eval_context, jacob_m } catch (ExprNode::EvalException &e) { - cerr << "ERROR: evaluation of Jacobian failed for equation " << eq+1 << " and variable " << symbol_table.getName(symb) << "(" << lag << ") [" << symb << "] !" << endl; + cerr << "ERROR: evaluation of Jacobian failed for equation " << eq+1 << " (line " << equations_lineno[eq] << ") and variable " << symbol_table.getName(symb) << "(" << lag << ") [" << symb << "] !" << endl; Id->writeOutput(cerr, oMatlabDynamicModelSparse, temporary_terms); cerr << endl; exit(EXIT_FAILURE); @@ -1376,21 +1376,22 @@ ModelTree::writeLatexModelFile(const string &filename, ExprNodeOutputType output } void -ModelTree::addEquation(expr_t eq) +ModelTree::addEquation(expr_t eq, int lineno) { BinaryOpNode *beq = dynamic_cast(eq); assert(beq != NULL && beq->get_op_code() == oEqual); equations.push_back(beq); + equations_lineno.push_back(lineno); } void -ModelTree::addEquation(expr_t eq, vector > &eq_tags) +ModelTree::addEquation(expr_t eq, int lineno, vector > &eq_tags) { int n = equation_number(); for (size_t i = 0; i < eq_tags.size(); i++) equation_tags.push_back(make_pair(n, eq_tags[i])); - addEquation(eq); + addEquation(eq, lineno); } void diff --git a/ModelTree.hh b/ModelTree.hh index 30afd27d..ffdb02ac 100644 --- a/ModelTree.hh +++ b/ModelTree.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -54,6 +54,9 @@ protected: //! Stores declared and generated auxiliary equations vector equations; + //! Stores line numbers of declared equations; -1 means undefined + vector equations_lineno; + //! Only stores generated auxiliary equations, in an order meaningful for evaluation deque aux_equations; @@ -295,10 +298,10 @@ public: 3 : the variables belonging to a non normalizable non linear equation are considered as feedback variables default value = 0 */ int mfs; - //! Declare a node as an equation of the model - void addEquation(expr_t eq); + //! Declare a node as an equation of the model; also give its line number + void addEquation(expr_t eq, int lineno); //! Declare a node as an equation of the model, also giving its tags - void addEquation(expr_t eq, vector > &eq_tags); + void addEquation(expr_t eq, int lineno, vector > &eq_tags); //! Declare a node as an auxiliary equation of the model, adding it at the end of the list of auxiliary equations void addAuxEquation(expr_t eq); //! Returns the number of equations in the model diff --git a/ParsingDriver.cc b/ParsingDriver.cc index f4bf3a75..58311c0e 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -1741,7 +1741,7 @@ ParsingDriver::end_planner_objective(expr_t expr) { // Add equation corresponding to expression expr_t eq = model_tree->AddEqual(expr, model_tree->Zero); - model_tree->addEquation(eq); + model_tree->addEquation(eq, location.begin.line); mod_file->addStatement(new PlannerObjectiveStatement(dynamic_cast(model_tree))); @@ -1988,10 +1988,10 @@ ParsingDriver::add_model_equal(expr_t arg1, expr_t arg2) if (!id->isInStaticForm()) error("An equation tagged [static] cannot contain leads, lags, expectations or STEADY_STATE operators"); - dynamic_model->addStaticOnlyEquation(id); + dynamic_model->addStaticOnlyEquation(id, location.begin.line); } else - model_tree->addEquation(id, eq_tags); + model_tree->addEquation(id, location.begin.line, eq_tags); eq_tags.clear(); return id; From 1ae4bebf5ef52171ff688854b0fa9d6199d3037c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Fri, 31 Jan 2014 11:27:36 +0100 Subject: [PATCH 093/162] Added logdata option. Closes #600. --- DynareBison.yy | 4 +++- DynareFlex.ll | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index 91233f31..e1b38aa1 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -101,7 +101,7 @@ class ParsingDriver; %token INT_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS IRF_PLOT_THRESHOLD %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF -%token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LYAPUNOV +%token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LOGDATA LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN @@ -1572,6 +1572,7 @@ estimation_options : o_datafile | o_mh_nblocks | o_load_mh_file | o_loglinear + | o_logdata | o_nodiagnostic | o_bayesian_irf | o_dsge_var @@ -2427,6 +2428,7 @@ o_mh_mode : MH_MODE EQUAL INT_NUMBER { driver.option_num("mh_mode", $3); }; o_mh_nblocks : MH_NBLOCKS EQUAL INT_NUMBER { driver.option_num("mh_nblck", $3); }; o_load_mh_file : LOAD_MH_FILE { driver.option_num("load_mh_file", "1"); }; o_loglinear : LOGLINEAR { driver.option_num("loglinear", "1"); }; +o_logdata : LOGDATA { driver.option_num("logdata", "1"); }; o_nodiagnostic : NODIAGNOSTIC { driver.option_num("nodiagnostic", "1"); }; o_bayesian_irf : BAYESIAN_IRF { driver.option_num("bayesian_irf", "1"); }; o_dsge_var : DSGE_VAR EQUAL non_negative_number diff --git a/DynareFlex.ll b/DynareFlex.ll index f8395e20..457d1ffd 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -275,6 +275,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 mh_nblocks {return token::MH_NBLOCKS;} load_mh_file {return token::LOAD_MH_FILE;} loglinear {return token::LOGLINEAR;} +logdata {return token::LOGDATA;} nodiagnostic {return token::NODIAGNOSTIC;} kalman_algo {return token::KALMAN_ALGO;} kalman_tol {return token::KALMAN_TOL;} From 962447876696e364b450ef300d029c04739eab9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 3 Feb 2014 16:22:42 +0100 Subject: [PATCH 094/162] Exit gracefully if an external function name is used as a model local variable. Closes #599 --- ParsingDriver.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 58311c0e..ba7bf5e6 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -342,6 +342,9 @@ ParsingDriver::add_expression_variable(string *name) || mod_file->symbol_table.getType(*name) == eLogTrend) error("Variable " + *name + " not allowed outside model declaration, because it is a trend variable."); + if (mod_file->symbol_table.getType(*name) == eExternalFunction) + error("Symbol '" + *name + "' is the name of a MATLAB/Octave function, and cannot be used as a variable."); + int symb_id = mod_file->symbol_table.getID(*name); expr_t id = data_tree->AddVariable(symb_id); From 9e962e7a93af7c57368f8298cd4b546b414a6170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 4 Feb 2014 14:49:06 +0100 Subject: [PATCH 095/162] Add new options consider_all_endogenous and consider_only_observed. Closes #336 --- DynareBison.yy | 7 ++++++- DynareFlex.ll | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index e1b38aa1..5e148252 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -89,6 +89,7 @@ class ParsingDriver; %token BVAR_PRIOR_MU BVAR_PRIOR_OMEGA BVAR_PRIOR_TAU BVAR_PRIOR_TRAIN %token BVAR_REPLIC BYTECODE ALL_VALUES_REQUIRED %token CALIB_SMOOTHER CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF CYCLE_REDUCTION LOGARITHMIC_REDUCTION +%token CONSIDER_ALL_ENDOGENOUS CONSIDER_ONLY_OBSERVED %token DATAFILE FILE DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT EXTENDED_PATH ENDOGENOUS_PRIOR %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME @@ -1622,7 +1623,9 @@ estimation_options : o_datafile | o_geweke_interval | o_mcmc_jumping_covariance | o_irf_plot_threshold - | o_posterior_max_subsample_draws + | o_posterior_max_subsample_draws + | o_consider_all_endogenous + | o_consider_only_observed ; list_optim_option : QUOTED_STRING COMMA QUOTED_STRING @@ -2746,6 +2749,8 @@ o_mcmc_jumping_covariance : MCMC_JUMPING_COVARIANCE EQUAL HESSIAN { driver.option_str("MCMC_jumping_covariance", $3); } ; o_irf_plot_threshold : IRF_PLOT_THRESHOLD EQUAL non_negative_number { driver.option_num("impulse_responses.plot_threshold", $3); }; +o_consider_all_endogenous : CONSIDER_ALL_ENDOGENOUS { driver.option_str("endo_vars_for_moment_computations_in_estimation", "all_endogenous_variables"); }; +o_consider_only_observed : CONSIDER_ONLY_OBSERVED { driver.option_str("endo_vars_for_moment_computations_in_estimation", "only_observed_variables"); }; range : symbol ':' symbol { diff --git a/DynareFlex.ll b/DynareFlex.ll index 457d1ffd..a30b9eaf 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -522,6 +522,8 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 solver_periods {return token::SOLVER_PERIODS;} endogenous_prior {return token::ENDOGENOUS_PRIOR;} long_name {return token::LONG_NAME;} +consider_all_endogenous {return token::CONSIDER_ALL_ENDOGENOUS;} +consider_only_observed {return token::CONSIDER_ONLY_OBSERVED;} [\$][^$]*[\$] { From 58d71bb44e7bb7d326ee44c889fd684238e09cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 10 Feb 2014 19:22:54 +0100 Subject: [PATCH 096/162] Implement syntaxes for moment_calibration and irf_calibration. Ref #267 --- DynareBison.yy | 62 ++++++++++++++++++++++++++++++++++++++-- DynareFlex.ll | 2 ++ ParsingDriver.cc | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ ParsingDriver.hh | 14 ++++++++- Shocks.cc | 46 +++++++++++++++++++++++++++++- Shocks.hh | 40 +++++++++++++++++++++++++- 6 files changed, 232 insertions(+), 6 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 5e148252..47e6129c 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -100,13 +100,13 @@ class ParsingDriver; %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID HYBRID %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT %token INT_NUMBER -%token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS IRF_PLOT_THRESHOLD +%token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS IRF_PLOT_THRESHOLD IRF_CALIBRATION %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LOGDATA LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN -%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE +%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS @@ -176,7 +176,7 @@ class ParsingDriver; %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf named_var %type change_type_arg -%type change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt +%type change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt calibration_range %type vec_int_elem vec_int_1 vec_int vec_int_number %type prior_pdf prior_distribution %% @@ -266,6 +266,8 @@ statement : parameters | calib_smoother | extended_path | model_diagnostics + | moment_calibration + | irf_calibration ; dsample : DSAMPLE INT_NUMBER ';' @@ -2304,6 +2306,60 @@ model_diagnostics : MODEL_DIAGNOSTICS ';' { driver.model_diagnostics(); } ; +calibration_range : '[' signed_number_w_inf signed_number_w_inf ']' + { + $$ = new vector(); + $$->push_back($2); + $$->push_back($3); + } + | '[' signed_number_w_inf COMMA signed_number_w_inf ']' + { + $$ = new vector(); + $$->push_back($2); + $$->push_back($4); + } + | PLUS + { + $$ = new vector(); + $$->push_back(new string("0")); + $$->push_back(new string("inf")); + } + | MINUS + { + $$ = new vector(); + $$->push_back(new string("-inf")); + $$->push_back(new string("0")); + } + ; + +moment_calibration : MOMENT_CALIBRATION ';' moment_calibration_list END ';' + { driver.end_moment_calibration(); } + ; + +moment_calibration_list : moment_calibration_item + | moment_calibration_list moment_calibration_item + ; + +moment_calibration_item : symbol COMMA symbol COMMA calibration_range ';' + { driver.add_moment_calibration_item($1, $3, new string("0"), $5); } + | symbol COMMA symbol '(' signed_integer ')' COMMA calibration_range ';' + { driver.add_moment_calibration_item($1, $3, $5, $8); } + ; + +irf_calibration : IRF_CALIBRATION ';' irf_calibration_list END ';' + { driver.end_irf_calibration(); } + ; + +irf_calibration_list : irf_calibration_item + | irf_calibration_list irf_calibration_item + ; + +irf_calibration_item : symbol COMMA symbol COMMA calibration_range ';' + { driver.add_irf_calibration_item($1, new string("1"), $3, $5); } + | symbol '(' INT_NUMBER ')' COMMA symbol COMMA calibration_range ';' + { driver.add_irf_calibration_item($1, $3, $6, $8); } + ; + o_dr_algo : DR_ALGO EQUAL INT_NUMBER { if (*$3 == string("0")) driver.warning("dr_algo option is now deprecated, and may be removed in a future version of Dynare"); diff --git a/DynareFlex.ll b/DynareFlex.ll index a30b9eaf..68e4f018 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -191,6 +191,8 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 homotopy_setup {BEGIN DYNARE_BLOCK; return token::HOMOTOPY_SETUP;} conditional_forecast_paths {BEGIN DYNARE_BLOCK; return token::CONDITIONAL_FORECAST_PATHS;} svar_identification {BEGIN DYNARE_BLOCK; return token::SVAR_IDENTIFICATION;} +moment_calibration {BEGIN DYNARE_BLOCK; return token::MOMENT_CALIBRATION;} +irf_calibration {BEGIN DYNARE_BLOCK; return token::IRF_CALIBRATION;} /* For the semicolon after an "end" keyword */ ; {return Dynare::parser::token_type (yytext[0]);} diff --git a/ParsingDriver.cc b/ParsingDriver.cc index ba7bf5e6..b7960aac 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2571,3 +2571,77 @@ ParsingDriver::add_parallel_local_file(string *filename) delete filename; } +void +ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector *range) +{ + MomentCalibration::Constraint c; + + check_symbol_existence(*endo1); + c.endo1 = mod_file->symbol_table.getID(*endo1); + if (mod_file->symbol_table.getType(*endo1) != eEndogenous) + error("Variable " + *endo1 + " is not an endogenous."); + delete endo1; + + check_symbol_existence(*endo2); + c.endo2 = mod_file->symbol_table.getID(*endo2); + if (mod_file->symbol_table.getType(*endo2) != eEndogenous) + error("Variable " + *endo2 + " is not an endogenous."); + delete endo2; + + c.lag = abs(atoi(lag->c_str())); + delete lag; + + assert(range->size() == 2); + c.lower_bound = *((*range)[0]); + c.upper_bound = *((*range)[1]); + delete (*range)[0]; + delete (*range)[1]; + delete range; + + moment_calibration_constraints.push_back(c); +} + +void ParsingDriver::end_moment_calibration() +{ + mod_file->addStatement(new MomentCalibration(moment_calibration_constraints, + mod_file->symbol_table)); + moment_calibration_constraints.clear(); +} + +void +ParsingDriver::add_irf_calibration_item(string *endo, string *period, string *exo, vector *range) +{ + IrfCalibration::Constraint c; + + check_symbol_existence(*endo); + c.endo = mod_file->symbol_table.getID(*endo); + if (mod_file->symbol_table.getType(*endo) != eEndogenous) + error("Variable " + *endo + " is not an endogenous."); + delete endo; + + c.period = atoi(period->c_str()); + delete period; + + check_symbol_existence(*exo); + c.exo = mod_file->symbol_table.getID(*exo); + if (mod_file->symbol_table.getType(*exo) != eExogenous) + error("Variable " + *endo + " is not an exogenous."); + delete exo; + + assert(range->size() == 2); + c.lower_bound = *((*range)[0]); + c.upper_bound = *((*range)[1]); + delete (*range)[0]; + delete (*range)[1]; + delete range; + + irf_calibration_constraints.push_back(c); +} + +void ParsingDriver::end_irf_calibration() +{ + mod_file->addStatement(new IrfCalibration(irf_calibration_constraints, + mod_file->symbol_table)); + irf_calibration_constraints.clear(); +} + diff --git a/ParsingDriver.hh b/ParsingDriver.hh index e856a2e0..853078cc 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -152,6 +152,10 @@ private: HistValStatement::hist_values_t hist_values; //! Temporary storage for homotopy_setup blocks HomotopyStatement::homotopy_values_t homotopy_values; + //! Temporary storage for moment_calibration + MomentCalibration::constraints_t moment_calibration_constraints; + //! Temporary storage for irf_calibration + IrfCalibration::constraints_t irf_calibration_constraints; //! Temporary storage for svar_identification blocks SvarIdentificationStatement::svar_identification_restrictions_t svar_ident_restrictions; //! Temporary storage for mapping the equation number to the restrictions within an svar_identification block @@ -651,6 +655,14 @@ public: void model_diagnostics(); //! Processing the parallel_local_files option void add_parallel_local_file(string *filename); + //! Add an item of a moment_calibration statement + void add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector *range); + //! End a moment_calibration statement + void end_moment_calibration(); + //! Add an item of an irf_calibration statement + void add_irf_calibration_item(string *endo, string *period, string *exo, vector *range); + //! End a moment_calibration statement + void end_irf_calibration(); }; #endif // ! PARSING_DRIVER_HH diff --git a/Shocks.cc b/Shocks.cc index 73032fbb..1d25d4b1 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -386,3 +386,47 @@ ConditionalForecastPathsStatement::writeOutput(ostream &output, const string &ba k++; } } + +MomentCalibration::MomentCalibration(const constraints_t &constraints_arg, + const SymbolTable &symbol_table_arg) + : constraints(constraints_arg), symbol_table(symbol_table_arg) +{ +} + +void +MomentCalibration::writeOutput(ostream &output, const string &basename) const +{ + output << "options_.moment_calibration = {" << endl; + for (size_t i = 0; i < constraints.size(); i++) + { + const Constraint &c = constraints[i]; + output << "'" << symbol_table.getName(c.endo1) << "', " + << "'" << symbol_table.getName(c.endo2) << "', " + << c.lag << ", " + << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" + << endl; + } + output << "};" << endl; +} + +IrfCalibration::IrfCalibration(const constraints_t &constraints_arg, + const SymbolTable &symbol_table_arg) + : constraints(constraints_arg), symbol_table(symbol_table_arg) +{ +} + +void +IrfCalibration::writeOutput(ostream &output, const string &basename) const +{ + output << "options_.irf_calibration = {" << endl; + for (size_t i = 0; i < constraints.size(); i++) + { + const Constraint &c = constraints[i]; + output << "'" << symbol_table.getName(c.endo) << "', " + << "'" << symbol_table.getName(c.exo) << "', " + << c.period << ", " + << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" + << endl; + } + output << "};" << endl; +} diff --git a/Shocks.hh b/Shocks.hh index 68d8eee2..17f6bb85 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -97,4 +97,42 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class MomentCalibration : public Statement +{ +public: + struct Constraint + { + int endo1, endo2; + int lag; + string lower_bound, upper_bound; + }; + typedef vector constraints_t; +private: + constraints_t constraints; + const SymbolTable &symbol_table; +public: + MomentCalibration(const constraints_t &constraints_arg, + const SymbolTable &symbol_table_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class IrfCalibration : public Statement +{ +public: + struct Constraint + { + int endo, period; + int exo; + string lower_bound, upper_bound; + }; + typedef vector constraints_t; +private: + constraints_t constraints; + const SymbolTable &symbol_table; +public: + IrfCalibration(const constraints_t &constraints_arg, + const SymbolTable &symbol_table_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + #endif From dea58be55872eb7d78f7f4fed39f7eb5bcb39b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 17 Feb 2014 17:52:37 +0100 Subject: [PATCH 097/162] Fix size of M_.endo_histval. Should fix crashes on models with leads/lags of more than one on exogenous. Closes #617 --- NumericalInitialization.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 4e28a492..4e2fc618 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -269,7 +269,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const output << "%" << endl << "% HISTVAL instructions" << endl << "%" << endl - << "M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_lag);" << endl; + << "M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_endo_lag);" << endl; for (hist_values_t::const_iterator it = hist_values.begin(); it != hist_values.end(); it++) @@ -305,7 +305,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const int tsid = symbol_table.getTypeSpecificID(symb_id) + 1; if (type == eEndogenous) - output << "M_.endo_histval( " << tsid << ", M_.maximum_lag + " << lag << ") = "; + output << "M_.endo_histval( " << tsid << ", M_.maximum_endo_lag + " << lag << ") = "; else if (type == eExogenous) output << "oo_.exo_simul( M_.maximum_lag + " << lag << ", " << tsid << " ) = "; else if (type != eExogenousDet) From 516491941d14ec27559572c9a3d9934c9372bc09 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sat, 24 Dec 2011 16:58:16 +0100 Subject: [PATCH 098/162] ms-dsge: add number_of_lags and parameters option to front end --- DynareBison.yy | 6 +++++- DynareFlex.ll | 1 + ParsingDriver.cc | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index 47e6129c..aab4a95a 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -153,7 +153,7 @@ class ParsingDriver; %token SBVAR TREND_VAR DEFLATOR GROWTH_FACTOR MS_IRF MS_VARIANCE_DECOMPOSITION %token MS_ESTIMATION MS_SIMULATION MS_COMPUTE_MDD MS_COMPUTE_PROBABILITIES MS_FORECAST %token SVAR_IDENTIFICATION EQUATION EXCLUSION LAG UPPER_CHOLESKY LOWER_CHOLESKY MONTHLY QUARTERLY -%token MARKOV_SWITCHING CHAIN DURATION NUMBER_OF_REGIMES +%token MARKOV_SWITCHING CHAIN DURATION NUMBER_OF_REGIMES NUMBER_OF_LAGS %token SVAR COEFF COEFFICIENTS VARIANCES CONSTANTS EQUATIONS %token EXTERNAL_FUNCTION EXT_FUNC_NAME EXT_FUNC_NARGS FIRST_DERIV_PROVIDED SECOND_DERIV_PROVIDED %token SELECTED_VARIABLES_ONLY COVA_COMPUTE SIMULATION_FILE_TAG FILE_TAG @@ -844,6 +844,8 @@ ms_options : o_chain | o_duration | o_restrictions | o_number_of_regimes + | o_number_of_lags + | o_parameters ; svar : SVAR '(' svar_options_list ')' ';' @@ -2700,6 +2702,8 @@ o_duration : DURATION EQUAL non_negative_number { driver.option_num("ms.duration",$3); } ; o_number_of_regimes : NUMBER_OF_REGIMES EQUAL INT_NUMBER { driver.option_num("ms.number_of_regimes",$3); }; +o_number_of_lags : NUMBER_OF_LAGS EQUAL INT_NUMBER { driver.option_num("ms.number_of_lags",$3); }; +o_parameters : PARAMETERS EQUAL '[' symbol_list ']' { driver.option_symbol_list("ms.parameters"); }; o_coefficients : COEFFICIENTS { driver.option_str("ms.coefficients","svar_coefficients"); }; o_variances : VARIANCES { driver.option_str("ms.variances","svar_variances"); }; o_equations : EQUATIONS EQUAL vec_int diff --git a/DynareFlex.ll b/DynareFlex.ll index 68e4f018..ab9770ab 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -602,6 +602,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 upper_cholesky {return token::UPPER_CHOLESKY;} lower_cholesky {return token::LOWER_CHOLESKY;} chain {return token::CHAIN;} +number_of_lags {return token::NUMBER_OF_LAGS;} number_of_regimes {return token::NUMBER_OF_REGIMES;} duration {return token::DURATION;} coefficients {return token::COEFFICIENTS;} diff --git a/ParsingDriver.cc b/ParsingDriver.cc index b7960aac..c105f81c 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1138,6 +1138,15 @@ ParsingDriver::option_symbol_list(const string &name_option) error("Variables passed to irf_shocks must be exogenous. Caused by: " + *it); } + if (name_option.compare("ms.parameters")==0) + { + vector parameters = symbol_list.get_symbols(); + for (vector::const_iterator it = parameters.begin(); + it != parameters.end(); it++) + if (mod_file->symbol_table.getType(*it) != eParameter) + error("Variables passed to the parameters option of the markov_switching statement must be parameters. Caused by: " + *it); + } + options_list.symbol_list_options[name_option] = symbol_list; symbol_list.clear(); } From 053ee41ac024f0e54b3434dfbdcdf231c49de83d Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 6 Aug 2013 15:20:36 -0400 Subject: [PATCH 099/162] ms-dsge: create C driver file without statements Conflicts: preprocessor/DynareMain2.cc preprocessor/ModFile.cc preprocessor/ModFile.hh preprocessor/Statement.hh --- ComputingTasks.cc | 3 + DynamicModel.cc | 142 ++++++++++++++++++++++++++++++++++++++++++++++ DynamicModel.hh | 2 + DynareMain2.cc | 10 +++- ModFile.cc | 64 ++++++++++++++++++++- ModFile.hh | 5 ++ Statement.hh | 14 +++++ SymbolList.hh | 2 + SymbolTable.cc | 72 +++++++++++++++++++++++ SymbolTable.hh | 2 + 10 files changed, 312 insertions(+), 4 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 8f549dbf..fcbda923 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1612,6 +1612,9 @@ MarkovSwitchingStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo } } } + + if (options_list.symbol_list_options.find("ms.parameters") != options_list.symbol_list_options.end()) + mod_file_struct.ms_dsge_present = true; } void diff --git a/DynamicModel.cc b/DynamicModel.cc index 87239eea..c0363d3c 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2956,6 +2956,148 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de } +void +DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const +{ + /* Writing initialisation for M_.lead_lag_incidence matrix + M_.lead_lag_incidence is a matrix with as many columns as there are + endogenous variables and as many rows as there are periods in the + models (nbr of rows = M_.max_lag+M_.max_lead+1) + + The matrix elements are equal to zero if a variable isn't present in the + model at a given period. + */ + + vector state_var, state_equ; + output << endl + << "int lead_lag_incidence[" + << max_lag+max_lead+1 <<"][" + << symbol_table.endo_nbr() << "];" << endl; + // Loop on endogenous variables + int nstatic = 0, + nfwrd = 0, + npred = 0, + nboth = 0; + for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) + { + int sstatic = 1, + sfwrd = 0, + spred = 0, + sboth = 0; + // Loop on periods + for (int lag = -max_endo_lag; lag <= max_endo_lead; lag++) + { + // Print variableID if exists with current period, otherwise print 0 + try + { + int varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag); + output << "lead_lag_incidence[" << lag + max_endo_lag << "][" << endoID << "] = " + << getDynJacobianCol(varID) << ";" << endl; + if (lag == -1) + { + sstatic = 0; + spred = 1; + } + else if (lag == 1) + { + if (spred == 1) + { + sboth = 1; + spred = 0; + } + else + { + sstatic = 0; + sfwrd = 1; + } + } + } + catch (UnknownDerivIDException &e) + { + output << "lead_lag_incidence[" << lag + max_endo_lag << "][" << endoID << "] = 0;" << endl; + } + } + nstatic += sstatic; + nfwrd += sfwrd; + npred += spred; + nboth += sboth; + } + output << "int nstatic = " << nstatic << ";" << endl + << "int nfwrd = " << nfwrd << ";" << endl + << "int npred = " << npred << ";" << endl + << "int nboth = " << nboth << ";" << endl; + for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) + { + // Loop on periods + for (int lag = -max_endo_lag; lag < 0; lag++) + { + // Print variableID if exists with current period, otherwise print 0 + try + { + getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag); + if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end()) + state_var.push_back(variable_reordered[endoID]); + } + catch (UnknownDerivIDException &e) + { + } + } + } + + // Writing initialization for some other variables + output << endl + << "int state_var[" << state_var.size() << "] = {"; + for (size_t i = 0; i < state_var.size(); i++) + if (i+1 == state_var.size()) + output << state_var[i]; + else + output << state_var[i] << ", "; + output << "};" << endl; + + output << endl + << "int maximum_lag = " << max_lag << ";" << endl + << "int maximum_lead = " << max_lead << ";" << endl; + + if (symbol_table.endo_nbr()) + output << endl + << "int maximum_endo_lag = " << max_endo_lag << ";" << endl + << "int maximum_endo_lead = " << max_endo_lead << ";" << endl + << "double steady_state[" << symbol_table.endo_nbr() << "];" << endl; + + if (symbol_table.exo_nbr()) + output << endl + << "int maximum_exo_lag = " << max_exo_lag << ";" << endl + << "int maximum_exo_lead = " << max_exo_lead << ";" << endl + << "double exo_steady_state[" << symbol_table.exo_nbr() << "];" << endl; + + if (symbol_table.exo_det_nbr()) + output << endl + << "int maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl + << "int maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl + << "double exo_det_steady_state[" << symbol_table.exo_det_nbr() << "];" << endl; + + if (symbol_table.param_nbr()) + output << endl + << "double params[" << symbol_table.param_nbr() << "];" << endl; + + // Write number of non-zero derivatives + // Use -1 if the derivatives have not been computed + output << endl + << "int NNZDerivatives[3];" << endl + << "NNZDerivatives[0] = " << NNZDerivatives[0] << ";" << endl; + if (order > 1) + { + output << "NNZDerivatives[1] = " << NNZDerivatives[1] << ";" << endl; + if (order > 2) + output << "NNZDerivatives[2] = " << NNZDerivatives[2] << ";" << endl; + else + output << "NNZDerivatives[2] = -1;" << endl; + } + else + output << "NNZDerivatives[1] = -1;" << endl + << "NNZDerivatives[2] = -1;" << endl; +} + map >, expr_t> DynamicModel::collect_first_order_derivatives_endogenous() { diff --git a/DynamicModel.hh b/DynamicModel.hh index 07716efe..e23816a0 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -213,6 +213,8 @@ public: const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode); //! Writes model initialization and lead/lag incidence matrix to output void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + //! Writes model initialization and lead/lag incidence matrix to C output + void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; //! Adds informations for simulation in a binary file void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, diff --git a/DynareMain2.cc b/DynareMain2.cc index 66ade0c8..c1704989 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -56,7 +56,10 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm mod_file->computingPass(no_tmp_terms); // Write outputs - mod_file->writeOutputFiles(basename, clear_all, no_log, no_warn, console, nograph, nointeractive, config_file + if (mod_file->c_driver) + mod_file->writeCOutputFiles(basename); + else + mod_file->writeOutputFiles(basename, clear_all, no_log, no_warn, console, nograph, nointeractive, config_file #if defined(_WIN32) || defined(__CYGWIN32__) , cygwin, msvc #endif @@ -64,6 +67,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm delete mod_file; - cout << "Preprocessing completed." << endl - << "Starting MATLAB/Octave computing." << endl; + cout << "Preprocessing completed." << endl; + if (!mod_file->use_dll) + cout << "Starting MATLAB/Octave computing." << endl; } diff --git a/ModFile.cc b/ModFile.cc index 2c44e3f2..7571da2d 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -40,7 +40,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg) linear(false), block(false), byte_code(false), use_dll(false), no_static(false), differentiate_forward_vars(false), nonstationary_variables(false), ramsey_policy_orig_eqn_nbr(0), - warnings(warnings_arg) + warnings(warnings_arg), c_driver(false) { } @@ -444,6 +444,9 @@ ModFile::transformPass(bool nostrict) << "greater than or equal to the number of observed variables." << endl; exit(EXIT_FAILURE); } + + if (mod_file_struct.ms_dsge_present) + c_driver = true; } void @@ -797,3 +800,62 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b cout << "done" << endl; } + +void +ModFile::writeCOutputFiles(const string &basename) const +{ + // Erase possible remnants of previous runs + string dynfile = basename + "_dynamic.m"; + unlink(dynfile.c_str()); + + dynfile = basename + "_dynamic.c"; + unlink(dynfile.c_str()); + + dynfile = basename + "_dynamic_mex.c"; + unlink(dynfile.c_str()); + + string statfile = basename + "_static.m"; + unlink(statfile.c_str()); + + statfile = basename + "_static.c"; + unlink(statfile.c_str()); + + statfile = basename + "_static_mex.c"; + unlink(statfile.c_str()); + + string filename = "cdriver.cc"; + unlink(filename.c_str()); + + ofstream mDriverCFile; + mDriverCFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDriverCFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + mDriverCFile << "/*" << endl + << " * " << filename << " : Driver file for MS-DSGE code" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << endl + << " */" << endl + << endl + << "#include \"ms_dsge_c_driver.hh\"" << endl + << endl + << "int main()" << endl + << "{" << endl; + + // Write basic info + symbol_table.writeCOutput(mDriverCFile); + + dynamic_model.writeCOutput(mDriverCFile, basename, false, false, true, mod_file_struct.order_option, mod_file_struct.estimation_present); + + mDriverCFile << "}" << endl; + mDriverCFile.close(); + + dynamic_model.writeDynamicFile(basename, false, false, true, mod_file_struct.order_option); + if (!no_static) + static_model.writeStaticFile(basename, false, false, true); +} diff --git a/ModFile.hh b/ModFile.hh index cdbca445..1f8aa0c9 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -98,6 +98,9 @@ public: /*! (i.e. option parallel_local_files of model block) */ vector parallel_local_files; + //! Output C driver file as opposed to Matlab/Octave driver file + bool c_driver; + private: //! List of statements vector statements; @@ -137,6 +140,8 @@ public: , bool cygwin, bool msvc #endif ) const; + //! Writes C output files only => No further Matlab processing + void writeCOutputFiles(const string &basename) const; }; #endif // ! MOD_FILE_HH diff --git a/Statement.hh b/Statement.hh index 7cd7b921..40e37818 100644 --- a/Statement.hh +++ b/Statement.hh @@ -105,6 +105,20 @@ public: set parameters_within_shocks_values; //! Set of estimated parameters (stored as symbol ids) set estimated_parameters; + //! Whether there is a prior statement present + bool prior_statement_present; + //! Whether there is a std prior statement present + bool std_prior_statement_present; + //! Whether there is a corr prior statement present + bool corr_prior_statement_present; + //! Whether there is a options statement present + bool options_statement_present; + //! Whether there is a std options statement present + bool std_options_statement_present; + //! Whether there is a corr options statement present + bool corr_options_statement_present; + //! Whether a Markov Switching DSGE is present + bool ms_dsge_present; }; class Statement diff --git a/SymbolList.hh b/SymbolList.hh index 45c7023f..82f0b794 100644 --- a/SymbolList.hh +++ b/SymbolList.hh @@ -43,6 +43,8 @@ public: void clear(); //! Get a copy of the string vector vector get_symbols() const { return symbols; }; + //! Is Empty + int empty() const { return symbols.empty(); }; }; #endif diff --git a/SymbolTable.cc b/SymbolTable.cc index 1c8652e5..9f670e7f 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -286,6 +286,78 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) } } +void +SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) +{ + if (!frozen) + throw NotYetFrozenException(); + + output << endl + << "map exo_names, exo_det_names, endo_names, param_names;" << endl; + + output << endl + << "int exo_nbr = " << exo_nbr() << ";" << endl; + if (exo_nbr() > 0) + for (int id = 0; id < exo_nbr(); id++) + output << "exo_names[\"" << getName(exo_ids[id]) << "\"] = " << id << ";" << endl; + + output << endl + << "int exo_det_nbr = " << exo_det_nbr() << ";" << endl; + if (exo_det_nbr() > 0) + for (int id = 0; id < exo_det_nbr(); id++) + output << "exo_det_names[\"" << getName(exo_det_ids[id]) << "\"] = " << id << " ;" << endl; + + output << endl + << "int endo_nbr = " << endo_nbr() << ";" << endl + << "int orig_endo_nbr = " << orig_endo_nbr() << ";" << endl; + if (endo_nbr() > 0) + for (int id = 0; id < endo_nbr(); id++) + output << "endo_names[\"" << getName(endo_ids[id]) << "\"] = " << id << ";" << endl; + + output << endl + << "int param_nbr = " << param_nbr() << ";" << endl; + if (param_nbr() > 0) + for (int id = 0; id < param_nbr(); id++) + output << "param_names[\"" << getName(param_ids[id]) << "\"] = " << id << ";" << endl; + + // Write the auxiliary variable table + output << endl + << "vector aux_vars;" << endl; + if (aux_vars.size() > 0) + for (int i = 0; i < (int) aux_vars.size(); i++) + { + output << "aux_vars_t av" << i << ";" << endl; + output << "av" << i << ".endo_index = " << getTypeSpecificID(aux_vars[i].get_symb_id()) << ";" << endl + << "av" << i << ".type = " << aux_vars[i].get_type() << ";" << endl; + switch (aux_vars[i].get_type()) + { + case avEndoLead: + case avExoLead: + case avExpectation: + case avMultiplier: + break; + case avEndoLag: + case avExoLag: + output << "av" << i << ".orig_index = " << getTypeSpecificID(aux_vars[i].get_orig_symb_id()) << ";" << endl + << "av" << i << ".orig_lead_lag = " << aux_vars[i].get_orig_lead_lag() << ";" << endl; + break; + } + output << "aux_vars.push_back(" << "av" << i << ");" << endl; + } + + output << endl + << "vector predetermined_variables, varobs;" << endl; + if (predeterminedNbr() > 0) + for (set::const_iterator it = predetermined_variables.begin(); + it != predetermined_variables.end(); it++) + output << "predetermined_variables.push_back(" << getTypeSpecificID(*it) << ");" << endl; + + if (observedVariablesNbr() > 0) + for (vector::const_iterator it = varobs.begin(); + it != varobs.end(); it++) + output << "varobs.push_back(" << getTypeSpecificID(*it) << ");" << endl; +} + int SymbolTable::addLeadAuxiliaryVarInternal(bool endo, int index) throw (FrozenException) { diff --git a/SymbolTable.hh b/SymbolTable.hh index 49c20855..c8716014 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -276,6 +276,8 @@ public: inline int orig_endo_nbr() const throw (NotYetFrozenException); //! Write output of this class void writeOutput(ostream &output) const throw (NotYetFrozenException); + //! Write C output of this class + void writeCOutput(ostream &output) const throw (NotYetFrozenException); //! Mark a symbol as predetermined variable void markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException); //! Test if a given symbol is a predetermined variable From e1cebde83ae7c4168506ff3587b6660bad707195 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sun, 25 Dec 2011 19:03:26 +0100 Subject: [PATCH 100/162] ms-dsge: add param initialization to C output --- ModFile.cc | 9 +++++++++ NumericalInitialization.cc | 10 ++++++++++ NumericalInitialization.hh | 1 + Statement.cc | 5 +++++ Statement.hh | 1 + 5 files changed, 26 insertions(+) diff --git a/ModFile.cc b/ModFile.cc index 7571da2d..a5ab0a42 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -852,6 +852,15 @@ ModFile::writeCOutputFiles(const string &basename) const dynamic_model.writeCOutput(mDriverCFile, basename, false, false, true, mod_file_struct.order_option, mod_file_struct.estimation_present); + mDriverCFile << "/*" << endl + << " * Writing statements" << endl + << " */" << endl; + + // Print statements + for (vector::const_iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->writeCOutput(mDriverCFile, basename); + mDriverCFile << "}" << endl; mDriverCFile.close(); diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 4e2fc618..0d6bcecc 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -50,6 +50,16 @@ InitParamStatement::writeOutput(ostream &output, const string &basename) const output << symbol_table.getName(symb_id) << " = M_.params( " << id << " );\n"; } +void +InitParamStatement::writeCOutput(ostream &output, const string &basename) +{ + int id = symbol_table.getTypeSpecificID(symb_id); + output << "params[ " << id << " ] = "; + param_value->writeOutput(output); + output << ";" << endl; + output << "double " << symbol_table.getName(symb_id) << " = params[ " << id << " ];" << endl; +} + void InitParamStatement::fillEvalContext(eval_context_t &eval_context) const { diff --git a/NumericalInitialization.hh b/NumericalInitialization.hh index f9214410..b03ab0d2 100644 --- a/NumericalInitialization.hh +++ b/NumericalInitialization.hh @@ -41,6 +41,7 @@ public: const SymbolTable &symbol_table_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); //! Fill eval context with parameter value void fillEvalContext(eval_context_t &eval_context) const; }; diff --git a/Statement.cc b/Statement.cc index f990f611..3ce955d6 100644 --- a/Statement.cc +++ b/Statement.cc @@ -63,6 +63,11 @@ Statement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &wa { } +void +Statement::writeCOutput(ostream &output, const string &basename) +{ +} + void Statement::computingPass() { diff --git a/Statement.hh b/Statement.hh index 40e37818..6abda8fc 100644 --- a/Statement.hh +++ b/Statement.hh @@ -138,6 +138,7 @@ public: \param basename is the name of the modfile (without extension) which can be used to build auxiliary files */ virtual void writeOutput(ostream &output, const string &basename) const = 0; + virtual void writeCOutput(ostream &output, const string &basename); }; class NativeStatement : public Statement From bc27f79d0661c8574cd126691f095509a3ae6e8f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sun, 8 Jan 2012 15:55:50 +0100 Subject: [PATCH 101/162] ms-dsge: aesthetic fix --- SymbolTable.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SymbolTable.cc b/SymbolTable.cc index 9f670e7f..abcd3c67 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -293,7 +293,7 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) throw NotYetFrozenException(); output << endl - << "map exo_names, exo_det_names, endo_names, param_names;" << endl; + << "map exo_names, exo_det_names, endo_names, param_names;" << endl; output << endl << "int exo_nbr = " << exo_nbr() << ";" << endl; From 8a50eee858a6f5a40d841e6754868e942bfb933c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sun, 8 Jan 2012 15:57:40 +0100 Subject: [PATCH 102/162] ms-dsge: change types of output for better packaging --- DynamicModel.cc | 77 ++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index c0363d3c..57317e47 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2970,9 +2970,8 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d vector state_var, state_equ; output << endl - << "int lead_lag_incidence[" - << max_lag+max_lead+1 <<"][" - << symbol_table.endo_nbr() << "];" << endl; + << "vector > lead_lag_incidence;" << endl + << "vector variable_presence;" << endl; // Loop on endogenous variables int nstatic = 0, nfwrd = 0, @@ -2980,6 +2979,7 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d nboth = 0; for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) { + output << "variable_presence.clear();" << endl; int sstatic = 1, sfwrd = 0, spred = 0, @@ -2991,58 +2991,52 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d try { int varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag); - output << "lead_lag_incidence[" << lag + max_endo_lag << "][" << endoID << "] = " - << getDynJacobianCol(varID) << ";" << endl; + output << "variable_presence.push_back(" << getDynJacobianCol(varID) << ");" << endl; if (lag == -1) { sstatic = 0; spred = 1; } else if (lag == 1) - { - if (spred == 1) - { - sboth = 1; - spred = 0; - } - else - { - sstatic = 0; - sfwrd = 1; - } - } + if (spred == 1) + { + sboth = 1; + spred = 0; + } + else + { + sstatic = 0; + sfwrd = 1; + } } catch (UnknownDerivIDException &e) { - output << "lead_lag_incidence[" << lag + max_endo_lag << "][" << endoID << "] = 0;" << endl; + output << "variable_presence.push_back(0);" << endl; } } nstatic += sstatic; nfwrd += sfwrd; npred += spred; nboth += sboth; + output << "lead_lag_incidence.push_back(variable_presence);" << endl; } output << "int nstatic = " << nstatic << ";" << endl << "int nfwrd = " << nfwrd << ";" << endl << "int npred = " << npred << ";" << endl << "int nboth = " << nboth << ";" << endl; for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) - { - // Loop on periods - for (int lag = -max_endo_lag; lag < 0; lag++) + // Loop on periods + for (int lag = -max_endo_lag; lag < 0; lag++) + // Print variableID if exists with current period, otherwise print 0 + try + { + getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag); + if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end()) + state_var.push_back(variable_reordered[endoID]); + } + catch (UnknownDerivIDException &e) { - // Print variableID if exists with current period, otherwise print 0 - try - { - getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag); - if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end()) - state_var.push_back(variable_reordered[endoID]); - } - catch (UnknownDerivIDException &e) - { - } } - } // Writing initialization for some other variables output << endl @@ -3076,26 +3070,25 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d << "int maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl << "double exo_det_steady_state[" << symbol_table.exo_det_nbr() << "];" << endl; - if (symbol_table.param_nbr()) - output << endl - << "double params[" << symbol_table.param_nbr() << "];" << endl; + output << endl + << "map params;" << endl; // Write number of non-zero derivatives // Use -1 if the derivatives have not been computed output << endl - << "int NNZDerivatives[3];" << endl - << "NNZDerivatives[0] = " << NNZDerivatives[0] << ";" << endl; + << "vector NNZDerivatives;" << endl + << "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl; if (order > 1) { - output << "NNZDerivatives[1] = " << NNZDerivatives[1] << ";" << endl; + output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl; if (order > 2) - output << "NNZDerivatives[2] = " << NNZDerivatives[2] << ";" << endl; + output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl; else - output << "NNZDerivatives[2] = -1;" << endl; + output << "NNZDerivatives.push_back(-1);" << endl; } else - output << "NNZDerivatives[1] = -1;" << endl - << "NNZDerivatives[2] = -1;" << endl; + output << "NNZDerivatives.push_back(-1);" << endl + << "NNZDerivatives.push_back(-1);" << endl; } map >, expr_t> From fa5020134845e3edcc0a8db8675a0086db699f45 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sun, 8 Jan 2012 16:35:09 +0100 Subject: [PATCH 103/162] ms-dsge: remove unnecessary output --- NumericalInitialization.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 0d6bcecc..6acbd79b 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -47,7 +47,6 @@ InitParamStatement::writeOutput(ostream &output, const string &basename) const output << "M_.params( " << id << " ) = "; param_value->writeOutput(output); output << ";" << endl; - output << symbol_table.getName(symb_id) << " = M_.params( " << id << " );\n"; } void From 1424f34eed3bc10d81b0e815b5286c9e6795f0c2 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 6 Aug 2013 15:29:11 -0400 Subject: [PATCH 104/162] ms-dsge: write C output Conflicts: preprocessor/ComputingTasks.cc preprocessor/ComputingTasks.hh --- ComputingTasks.cc | 204 ++++++++++++++++++++++++++++++++++++++++++++++ ComputingTasks.hh | 13 +++ ModFile.cc | 20 ++++- 3 files changed, 233 insertions(+), 4 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index fcbda923..542cb55a 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1652,6 +1652,57 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) c << itR->first.second << ", " << itR->second << "]};" << endl; } +void +MarkovSwitchingStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl; + + OptionsList::num_options_t::const_iterator it = + options_list.num_options.find("ms.chain"); + assert(it != options_list.num_options.end()); + output << "chain = " << it->second << ";" << endl; + + it = options_list.num_options.find("ms.number_of_regimes"); + assert(it != options_list.num_options.end()); + output << "number_of_regimes = " << it->second << ";" << endl; + + it = options_list.num_options.find("ms.number_of_lags"); + if (it != options_list.num_options.end()) + output << "number_of_lags = " << it->second << ";" << endl + << "number_of_lags_was_passed = true;" << endl; + else + output << "number_of_lags_was_passed = false;" << endl; + + it = options_list.num_options.find("ms.duration"); + assert(it != options_list.num_options.end()); + output << "duration.clear();" << endl; + using namespace boost; + vector tokenizedDomain; + split(tokenizedDomain, it->second, is_any_of("[ ]"), token_compress_on); + for (vector::iterator itvs = tokenizedDomain.begin(); + itvs != tokenizedDomain.end(); itvs++ ) + if (!itvs->empty()) + output << "duration.push_back(" << *itvs << ");" << endl; + + OptionsList::symbol_list_options_t::const_iterator itsl = + options_list.symbol_list_options.find("ms.parameters"); + assert(itsl != options_list.symbol_list_options.end()); + vector parameters = itsl->second.get_symbols(); + output << "parameters.clear();" << endl; + for (vector::iterator itp = parameters.begin(); + itp != parameters.end(); itp++ ) + output << "parameters.push_back(param_names[\"" << *itp << "\"]);" << endl; + + output << "restriction_map.clear();" << endl; + for (map , double >::iterator itrm = restriction_map.begin(); + itrm != restriction_map.end(); itrm++) + output << "restriction_map[make_pair(" << itrm->first.first << "," + << itrm->first.second << ")] = " << itrm->second << ";" << endl; + + output << "msdsgeinfo->addMarkovSwitching(new MarkovSwitching(" << endl + << " chain, number_of_regimes, number_of_lags, number_of_lags_was_passed, parameters, duration, restriction_map));" << endl; +} + SvarStatement::SvarStatement(const OptionsList &options_list_arg) : options_list(options_list_arg) { @@ -1950,6 +2001,14 @@ BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsoli } } +bool +BasicPriorStatement::is_structural_innovation(const SymbolType symb_type) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + return true; + return false; +} + void BasicPriorStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const { @@ -2007,6 +2066,73 @@ BasicPriorStatement::writePriorOutput(ostream &output, string &lhs_field, const writeCommonOutput(output, lhs_field); } +void +BasicPriorStatement::writeCVarianceOption(ostream &output) const +{ + output << "variance = "; + if (variance) + variance->writeOutput(output); + else + output << "numeric_limits::quiet_NaN()"; + output << ";" << endl; +} + +void +BasicPriorStatement::writeCDomain(ostream &output) const +{ + output << "domain.clear();" << endl; + OptionsList::num_options_t::const_iterator it_num = options_list.num_options.find("domain"); + if (it_num != options_list.num_options.end()) + { + using namespace boost; + vector tokenizedDomain; + split(tokenizedDomain, it_num->second, is_any_of("[ ]"), token_compress_on); + for (vector::iterator it = tokenizedDomain.begin(); + it != tokenizedDomain.end(); it++ ) + if (!it->empty()) + output << "domain.push_back(" << *it << ");" << endl; + } +} + +void +BasicPriorStatement::writeCOutputHelper(ostream &output, const string &field) const +{ + OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + if (itn != options_list.num_options.end()) + output << field << " = " << itn->second << ";" << endl; + else + output << field << " = " << "numeric_limits::quiet_NaN();" << endl; +} + +void +BasicPriorStatement::writeCShape(ostream &output) const +{ + output << "shape = "; + switch (prior_shape) + { + case eBeta: + output << "\"beta\";" << endl; + break; + case eGamma: + output << "\"gamma\";" << endl; + break; + case eNormal: + output << "\"normal\";" << endl; + break; + case eInvGamma: + output << "\"inv_gamma\";" << endl; + break; + case eUniform: + output << "\"uniform\";" << endl; + break; + case eInvGamma2: + output << "\"inv_gamma2\";" << endl; + break; + case eNoShape: + assert(prior_shape != eNoShape); + } +} + PriorStatement::PriorStatement(const string &name_arg, const string &subsample_name_arg, const PriorDistributions &prior_shape_arg, @@ -2026,6 +2152,23 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const writePriorOutput(output, lhs_field, ""); } +void +PriorStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = param_names[\""<< name << "\"];" << endl; + writeCShape(output); + writeCOutputHelper(output, "mean"); + writeCOutputHelper(output, "mode"); + writeCOutputHelper(output, "stdev"); + writeCVarianceOption(output); + writeCDomain(output); + + output << "msdsgeinfo->addPrior(new ModFilePrior(" << endl + << " index, shape, mean, mode, stdev, variance, domain));" << endl; + +} + StdPriorStatement::StdPriorStatement(const string &name_arg, const string &subsample_name_arg, const PriorDistributions &prior_shape_arg, @@ -2050,6 +2193,31 @@ StdPriorStatement::writeOutput(ostream &output, const string &basename) const writePriorOutput(output, lhs_field, ""); } +void +StdPriorStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = "; + if (is_structural_innovation(symbol_table.getType(name))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name << "\"];" << endl; + + writeCShape(output); + writeCOutputHelper(output, "mean"); + writeCOutputHelper(output, "mode"); + writeCOutputHelper(output, "stdev"); + writeCVarianceOption(output); + writeCDomain(output); + + if (is_structural_innovation(symbol_table.getType(name))) + output << "msdsgeinfo->addStructuralInnovationPrior(new ModFileStructuralInnovationPrior("; + else + output << "msdsgeinfo->addMeasurementErrorPrior(new ModFileMeasurementErrorPrior("; + output << endl << " index, shape, mean, mode, stdev, variance, domain));" << endl; +} + CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2, const string &subsample_name_arg, const PriorDistributions &prior_shape_arg, @@ -2111,6 +2279,42 @@ PriorEqualStatement::PriorEqualStatement(const string &to_declaration_type_arg, { } +void +CorrPriorStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = "; + if (is_structural_innovation(symbol_table.getType(name))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name << "\"];" << endl; + + output << "index1 = "; + if (is_structural_innovation(symbol_table.getType(name1))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name1 << "\"];" << endl; + + writeCShape(output); + writeCOutputHelper(output, "mean"); + writeCOutputHelper(output, "mode"); + writeCOutputHelper(output, "stdev"); + writeCVarianceOption(output); + writeCDomain(output); + + if (is_structural_innovation(symbol_table.getType(name))) + output << "msdsgeinfo->addStructuralInnovationCorrPrior(new ModFileStructuralInnovationCorrPrior("; + else + output << "msdsgeinfo->addMeasurementErrorCorrPrior(new ModFileMeasurementErrorCorrPrior("; + output << endl <<" index, index1, shape, mean, mode, stdev, variance, domain));" << endl; +} + +BasicOptionsStatement::~BasicOptionsStatement() +{ +} + void PriorEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { diff --git a/ComputingTasks.hh b/ComputingTasks.hh index feddfbbc..c76728fe 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -571,6 +571,7 @@ public: MarkovSwitchingStatement(const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class SvarStatement : public Statement @@ -658,6 +659,15 @@ protected: void writeCommonOutput(ostream &output, const string &lhs_field) const; void writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const; void writePriorOutput(ostream &output, string &lhs_field, const string &name2) const; + bool is_structural_innovation(const SymbolType symb_type) const; + void writePriorIndex(ostream &output, const string &lhs_field) const; + void writeVarianceOption(ostream &output, const string &lhs_field) const; + void writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const; + void writeShape(ostream &output, const string &lhs_field) const; + void writeCOutputHelper(ostream &output, const string &field) const; + void writeCShape(ostream &output) const; + void writeCVarianceOption(ostream &output) const; + void writeCDomain(ostream &output) const; }; class PriorStatement : public BasicPriorStatement @@ -669,6 +679,7 @@ public: const expr_t &variance_arg, const OptionsList &options_list_arg); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class StdPriorStatement : public BasicPriorStatement @@ -683,6 +694,7 @@ public: const OptionsList &options_list_arg, const SymbolTable &symbol_table_arg); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class CorrPriorStatement : public BasicPriorStatement @@ -727,6 +739,7 @@ public: void get_base_name(const SymbolType symb_type, string &lhs_field) const; virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class BasicOptionsStatement : public Statement diff --git a/ModFile.cc b/ModFile.cc index a5ab0a42..bb2a45fb 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -823,7 +823,7 @@ ModFile::writeCOutputFiles(const string &basename) const statfile = basename + "_static_mex.c"; unlink(statfile.c_str()); - string filename = "cdriver.cc"; + string filename = "preprocessorOutput.cc"; unlink(filename.c_str()); ofstream mDriverCFile; @@ -839,12 +839,12 @@ ModFile::writeCOutputFiles(const string &basename) const << " *" << endl << " * Warning : this file is generated automatically by Dynare" << endl << " * from model file (.mod)" << endl - << endl << " */" << endl << endl << "#include \"ms_dsge_c_driver.hh\"" << endl << endl - << "int main()" << endl + << "MsDsgeInfo *" << endl + << "preprocessorOutput()" << endl << "{" << endl; // Write basic info @@ -854,13 +854,25 @@ ModFile::writeCOutputFiles(const string &basename) const mDriverCFile << "/*" << endl << " * Writing statements" << endl - << " */" << endl; + << " */" << endl + << "/* prior args*/" << endl + << "MsDsgeInfo *msdsgeinfo = new MsDsgeInfo(exo_names, exo_det_names, endo_names, param_names, params, aux_vars, predetermined_variables, varobs, lead_lag_incidence, NNZDerivatives);" << endl + << "int index, index1;" << endl + << "string shape;" << endl + << "double mean, mode, stdev, variance;" << endl + << "vector domain;" << endl + << "/* markov_switching args*/" << endl + << "int chain, number_of_regimes, number_of_lags, number_of_lags_was_passed;" << endl + << "vector parameters;" << endl + << "vector duration;" << endl + << "restriction_map_t restriction_map;" << endl << endl; // Print statements for (vector::const_iterator it = statements.begin(); it != statements.end(); it++) (*it)->writeCOutput(mDriverCFile, basename); + mDriverCFile << "return msdsgeinfo;" << endl; mDriverCFile << "}" << endl; mDriverCFile.close(); From 109045a97ba96ed879138f6c62a22cffa0405d3c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 6 Aug 2013 15:44:04 -0400 Subject: [PATCH 105/162] ms-dsge: add options statement Conflicts: preprocessor/ComputingTasks.cc preprocessor/ComputingTasks.hh preprocessor/ParsingDriver.cc --- ComputingTasks.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++- ComputingTasks.hh | 7 +++++ ModFile.cc | 4 ++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 542cb55a..fc368d8e 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2166,7 +2166,6 @@ PriorStatement::writeCOutput(ostream &output, const string &basename) output << "msdsgeinfo->addPrior(new ModFilePrior(" << endl << " index, shape, mean, mode, stdev, variance, domain));" << endl; - } StdPriorStatement::StdPriorStatement(const string &name_arg, @@ -2415,6 +2414,14 @@ BasicOptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConso { } +bool +BasicOptionsStatement::is_structural_innovation(const SymbolType symb_type) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + return true; + return false; +} + void BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const { @@ -2442,6 +2449,16 @@ BasicOptionsStatement::writeCommonOutputHelper(ostream &output, const string &fi output << lhs_field << "." << field << " = " << itn->second << ";" << endl; } +void +BasicOptionsStatement::writeCOutputHelper(ostream &output, const string &field) const +{ + OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + if (itn != options_list.num_options.end()) + output << field << " = " << itn->second << ";" << endl; + else + output << field << " = " << "numeric_limits::quiet_NaN();" << endl; +} + void BasicOptionsStatement::writeOptionsOutput(ostream &output, string &lhs_field, const string &name2) const { @@ -2473,6 +2490,15 @@ OptionsStatement::writeOutput(ostream &output, const string &basename) const writeOptionsOutput(output, lhs_field, ""); } +void +OptionsStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = param_names[\""<< name << "\"];" << endl; + writeCOutputHelper(output, "init"); + output << "msdsgeinfo->addOption(new ModFileOption(index, init));" << endl; +} + StdOptionsStatement::StdOptionsStatement(const string &name_arg, const string &subsample_name_arg, const OptionsList &options_list_arg, @@ -2495,6 +2521,26 @@ StdOptionsStatement::writeOutput(ostream &output, const string &basename) const writeOptionsOutput(output, lhs_field, ""); } +void +StdOptionsStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = "; + if (is_structural_innovation(symbol_table.getType(name))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name << "\"];" << endl; + + writeCOutputHelper(output, "init"); + + if (is_structural_innovation(symbol_table.getType(name))) + output << "msdsgeinfo->addStructuralInnovationOption(new ModFileStructuralInnovationOption("; + else + output << "msdsgeinfo->addMeasurementErrorOption(new ModFileMeasurementErrorOption("; + output << "index, init));" << endl; +} + CorrOptionsStatement::CorrOptionsStatement(const string &name_arg1, const string &name_arg2, const string &subsample_name_arg, const OptionsList &options_list_arg, @@ -2698,3 +2744,30 @@ ModelDiagnosticsStatement::writeOutput(ostream &output, const string &basename) { output << "model_diagnostics(M_,options_,oo_);" << endl; } + +void +CorrOptionsStatement::writeCOutput(ostream &output, const string &basename) +{ + output << endl + << "index = "; + if (is_structural_innovation(symbol_table.getType(name))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name << "\"];" << endl; + + output << "index1 = "; + if (is_structural_innovation(symbol_table.getType(name1))) + output << "exo_names"; + else + output << "endo_names"; + output << "[\""<< name1 << "\"];" << endl; + + writeCOutputHelper(output, "init"); + + if (is_structural_innovation(symbol_table.getType(name))) + output << "msdsgeinfo->addStructuralInnovationCorrOption(new ModFileStructuralInnovationCorrOption("; + else + output << "msdsgeinfo->addMeasurementErrorCorrOption(new ModFileMeasurementErrorCorrOption("; + output << "index, index1, init));" << endl; +} diff --git a/ComputingTasks.hh b/ComputingTasks.hh index c76728fe..cefa8487 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -758,6 +758,10 @@ protected: void writeOptionsOutput(ostream &output, string &lhs_field, const string &name2) const; void writeCommonOutput(ostream &output, const string &lhs_field) const; void writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const; + bool is_structural_innovation(const SymbolType symb_type) const; + void writeOptionsIndex(ostream &output, const string &lhs_field) const; + void writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const; + void writeCOutputHelper(ostream &output, const string &field) const; }; class OptionsStatement : public BasicOptionsStatement @@ -765,6 +769,7 @@ class OptionsStatement : public BasicOptionsStatement public: OptionsStatement(const string &name_arg, const string &subsample_name_arg, const OptionsList &options_list_arg); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class StdOptionsStatement : public BasicOptionsStatement @@ -777,6 +782,7 @@ public: const OptionsList &options_list_arg, const SymbolTable &symbol_table_arg); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class CorrOptionsStatement : public BasicOptionsStatement @@ -825,6 +831,7 @@ class ModelDiagnosticsStatement : public Statement public: ModelDiagnosticsStatement(); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; #endif diff --git a/ModFile.cc b/ModFile.cc index bb2a45fb..48b76cb4 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -865,7 +865,9 @@ ModFile::writeCOutputFiles(const string &basename) const << "int chain, number_of_regimes, number_of_lags, number_of_lags_was_passed;" << endl << "vector parameters;" << endl << "vector duration;" << endl - << "restriction_map_t restriction_map;" << endl << endl; + << "restriction_map_t restriction_map;" << endl + << "/* options args*/" << endl + << "double init;" << endl << endl; // Print statements for (vector::const_iterator it = statements.begin(); From 0e469dc6e8f9db5906eba52da81d516d1c766926 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 23 Jan 2012 11:57:05 -0500 Subject: [PATCH 106/162] pretty end to dynare processing --- ModFile.cc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ModFile.cc b/ModFile.cc index 48b76cb4..6239db39 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -881,4 +881,35 @@ ModFile::writeCOutputFiles(const string &basename) const dynamic_model.writeDynamicFile(basename, false, false, true, mod_file_struct.order_option); if (!no_static) static_model.writeStaticFile(basename, false, false, true); + + + // Write informational m file + ofstream mOutputFile; + + if (basename.size()) + { + string fname(basename); + fname += ".m"; + mOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!mOutputFile.is_open()) + { + cerr << "ERROR: Can't open file " << fname + << " for writing" << endl; + exit(EXIT_FAILURE); + } + } + else + { + cerr << "ERROR: Missing file name" << endl; + exit(EXIT_FAILURE); + } + + mOutputFile << "%" << endl + << "% Status : informational m file" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl + << "disp('The following C file was successfully created:');" << endl + << "ls preprocessorOutput.cc" << endl << endl; + mOutputFile.close(); } From 96513df4bf1a43430bbadab5357b837128f40064 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 6 Aug 2013 16:01:03 -0400 Subject: [PATCH 107/162] rebase fixes --- ComputingTasks.cc | 4 ---- ComputingTasks.hh | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index fc368d8e..d189e34f 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2310,10 +2310,6 @@ CorrPriorStatement::writeCOutput(ostream &output, const string &basename) output << endl <<" index, index1, shape, mean, mode, stdev, variance, domain));" << endl; } -BasicOptionsStatement::~BasicOptionsStatement() -{ -} - void PriorEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { diff --git a/ComputingTasks.hh b/ComputingTasks.hh index cefa8487..8882f2ce 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -712,6 +712,7 @@ public: const SymbolTable &symbol_table_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class PriorEqualStatement : public Statement @@ -739,7 +740,6 @@ public: void get_base_name(const SymbolType symb_type, string &lhs_field) const; virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; - virtual void writeCOutput(ostream &output, const string &basename); }; class BasicOptionsStatement : public Statement @@ -797,6 +797,7 @@ public: const SymbolTable &symbol_table_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; + virtual void writeCOutput(ostream &output, const string &basename); }; class OptionsEqualStatement : public Statement @@ -831,7 +832,6 @@ class ModelDiagnosticsStatement : public Statement public: ModelDiagnosticsStatement(); virtual void writeOutput(ostream &output, const string &basename) const; - virtual void writeCOutput(ostream &output, const string &basename); }; #endif From 2e7afdb22ba75481b6b4b48cd4f6186e4c3c01dc Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Tue, 13 Aug 2013 21:16:02 +0200 Subject: [PATCH 108/162] extended-preprocessor: adding line options output= and cuda; starting to change ModFile::c_driver mechanism --- DynareMain.cc | 32 +++++- DynareMain2.cc | 7 +- DynareOutput.hh | 12 +++ ExternalFiles.cc | 259 +++++++++++++++++++++++++++++++++++++++++++++++ ExternalFiles.hh | 37 +++++++ ModFile.cc | 23 +++++ ModFile.hh | 3 + 7 files changed, 367 insertions(+), 6 deletions(-) create mode 100644 DynareOutput.hh create mode 100644 ExternalFiles.cc create mode 100644 ExternalFiles.hh diff --git a/DynareMain.cc b/DynareMain.cc index 4e8709b2..383ffa08 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -29,6 +29,7 @@ #include "macro/MacroDriver.hh" #include +#include "DynareOutput.hh" /* Prototype for second part of main function Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be @@ -36,7 +37,7 @@ */ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict + bool parallel_test, bool nostrict, OutputType output_mode, bool cuda #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif @@ -47,7 +48,7 @@ usage() { cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]" << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test] " - << " [-D[=]] [nostrict]" + << " [-D[=]] [nostrict] [-double=dynamic|first|second|third] [cuda]" #if defined(_WIN32) || defined(__CYGWIN32__) << " [cygwin] [msvc]" #endif @@ -95,6 +96,8 @@ main(int argc, char **argv) bool parallel_test = false; bool nostrict = false; map defines; + OutputType output_mode = none; + bool cuda = false; // Parse options for (int arg = 2; arg < argc; arg++) @@ -189,6 +192,29 @@ main(int argc, char **argv) defines[key] = "1"; } } + else if (strlen(argv[arg]) >= 6 && !strncmp(argv[arg], "output", 6)) + { + if (strlen(argv[arg]) <= 7 || argv[arg][6] != '=') + { + cerr << "Incorrect syntax for ouput option" << endl; + usage(); + } + if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 7, "dynamic", 7)) + output_mode = dynamic; + else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "first", 5)) + output_mode = first; + else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 7, "second", 6)) + output_mode = second; + else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "third", 5)) + output_mode = third; + else + { + cerr << "Incorrect syntax for ouput option" << endl; + usage(); + } + } + else if (!strcmp(argv[arg], "cuda")) + cuda = true; else { cerr << "Unknown option: " << argv[arg] << endl; @@ -229,7 +255,7 @@ main(int argc, char **argv) // Do the rest main2(macro_output, basename, debug, clear_all, no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive, - parallel, parallel_config_file, cluster_name, parallel_slave_open_mode, parallel_test, nostrict + parallel, parallel_config_file, cluster_name, parallel_slave_open_mode, parallel_test, nostrict, output_mode, cuda #if defined(_WIN32) || defined(__CYGWIN32__) , cygwin, msvc #endif diff --git a/DynareMain2.cc b/DynareMain2.cc index c1704989..cb9beac5 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -22,11 +22,12 @@ #include "ParsingDriver.hh" #include "ModFile.hh" #include "ConfigFile.hh" +#include "DynareOutput.hh" void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict + bool parallel_test, bool nostrict, OutputType output_mode, bool cuda #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif @@ -56,8 +57,8 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm mod_file->computingPass(no_tmp_terms); // Write outputs - if (mod_file->c_driver) - mod_file->writeCOutputFiles(basename); + if (output_mode != none) + mod_file->writeExternalFiles(basename, output_mode, cuda); else mod_file->writeOutputFiles(basename, clear_all, no_log, no_warn, console, nograph, nointeractive, config_file #if defined(_WIN32) || defined(__CYGWIN32__) diff --git a/DynareOutput.hh b/DynareOutput.hh new file mode 100644 index 00000000..4e884f6b --- /dev/null +++ b/DynareOutput.hh @@ -0,0 +1,12 @@ +#ifndef _DYNARE_OUTPUT_HH +#define _DYNARE_OUTPUT_HH + +enum OutputType + { + none, // outputs files for Matlab/Octave processing + dynamic, // outputs _dynamic.cc and related files + first, // outputs _first_derivatives and related files + second, // outputs _first_derivatives, _second_derivatives.cc and related files + third, // outputs _first_derivatives, _second_derivatives.cc, _third_derivatives.cc and related files + }; +#endif diff --git a/ExternalFiles.cc b/ExternalFiles.cc new file mode 100644 index 00000000..9bfd42ea --- /dev/null +++ b/ExternalFiles.cc @@ -0,0 +1,259 @@ +/* + * Copyright (C) 2006-2013 Dynare Team + * + * This file is part of Dynare. + * + * Dynare is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dynare is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dynare. If not, see . + */ + +#input "ExternalFiles.hh" + +ExternalFiles::writeModelCCconst string &basename, bool cuda) +{ + ofstream mOutputFile; + + if (basename.size()) + { + string fname(basename); + fname += ".cc"; + cOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!cOutputFile.is_open()) + { + cerr << "ERROR: Can't open file " << fname + << " for writing" << endl; + exit(EXIT_FAILURE); + } + } + else + { + cerr << "ERROR: Missing file name" << endl; + exit(EXIT_FAILURE); + } + + cOutputFile << "%" << endl + << "% Status : dynare_model class initializing function " << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file " << baseline << "(.mod)" << endl << endl; + + cOutputFile << "#include \"dynare_model.hh\"" << endl << endl; + + cOutputFile << "dynare_model::dynare_model(void)" << endl + << "{" << endl + << " model_name = \""<< basename << "\";" << endl; + + symbol_table.writeOutputCC(hOutputFile); + + // Initialize M_.Sigma_e and M_.H + mOutputFile << "M_.Sigma_e = zeros(" << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ");" << endl; + + if (mod_file_struct.calibrated_measurement_errors) + mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl; + else + mOutputFile << "M_.H = 0;" << endl; + + if (linear == 1) + mOutputFile << "options_.linear = 1;" << endl; + + mOutputFile << "options_.block=" << block << ";" << endl + << "options_.bytecode=" << byte_code << ";" << endl + << "options_.use_dll=" << use_dll << ";" << endl; + + if (parallel_local_files.size() > 0) + { + mOutputFile << "options_.parallel_info.local_files = {" << endl; + for (size_t i = 0; i < parallel_local_files.size(); i++) + { + size_t j = parallel_local_files[i].find_last_of("/\\"); + if (j == string::npos) + mOutputFile << "'', '" << parallel_local_files[i] << "';" << endl; + else + mOutputFile << "'" << parallel_local_files[i].substr(0, j+1) << "', '" + << parallel_local_files[i].substr(j+1, string::npos) << "';" << endl; + } + mOutputFile << "};" << endl; + } + + config_file.writeCluster(mOutputFile); + + if (byte_code) + mOutputFile << "if exist('bytecode') ~= 3" << endl + << " error('DYNARE: Can''t find bytecode DLL. Please compile it or remove the ''bytecode'' option.')" << endl + << "end" << endl; + + // Erase possible remnants of previous runs + string dynfile = basename + "_dynamic.m"; + unlink(dynfile.c_str()); + + string statfile = basename + "_static.m"; + unlink(statfile.c_str()); + + string steadystatefile = basename + "_steadystate2.m"; + unlink(steadystatefile.c_str()); + + if (!use_dll) + { + mOutputFile << "erase_compiled_function('" + basename + "_static');" << endl; + mOutputFile << "erase_compiled_function('" + basename + "_dynamic');" << endl; + } + +#if defined(_WIN32) || defined(__CYGWIN32__) + // If using USE_DLL with MSVC, check that the user didn't use a function not supported by MSVC (because MSVC doesn't comply with C99 standard) + if (use_dll && msvc) + { + if (dynamic_model.isUnaryOpUsed(oAcosh)) + { + cerr << "ERROR: acosh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; + exit(EXIT_FAILURE); + } + if (dynamic_model.isUnaryOpUsed(oAsinh)) + { + cerr << "ERROR: asinh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; + exit(EXIT_FAILURE); + } + if (dynamic_model.isUnaryOpUsed(oAtanh)) + { + cerr << "ERROR: atanh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; + exit(EXIT_FAILURE); + } + if (dynamic_model.isTrinaryOpUsed(oNormcdf)) + { + cerr << "ERROR: normcdf() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; + exit(EXIT_FAILURE); + } + } +#endif + + // Compile the dynamic MEX file for use_dll option + if (use_dll) + { + mOutputFile << "if ~exist('OCTAVE_VERSION')" << endl; + // Some mex commands are enclosed in an eval(), because otherwise it will make Octave fail +#if defined(_WIN32) || defined(__CYGWIN32__) + if (msvc) + // MATLAB/Windows + Microsoft Visual C++ + mOutputFile << " eval('mex -O LINKFLAGS=\"$LINKFLAGS /export:Dynamic\" " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LINKFLAGS=\"$LINKFLAGS /export:Static\" " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; + else if (cygwin) + // MATLAB/Windows + Cygwin g++ + mOutputFile << " eval('mex -O PRELINK_CMDS1=\"echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Dynamic >> mex.def\" " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O PRELINK_CMDS1=\"echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Static >> mex.def\" " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; + else + mOutputFile << " error('When using the USE_DLL option, you must give either ''cygwin'' or ''msvc'' option to the ''dynare'' command')" << endl; +#else +# ifdef __linux__ + // MATLAB/Linux + mOutputFile << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; +# else // MacOS + // MATLAB/MacOS + mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_static.c " << basename << "_static_mex.c')" << endl; +# endif +#endif + mOutputFile << "else" << endl // Octave + << " mex " << basename << "_dynamic.c " << basename << "_dynamic_mex.c" << endl + << " mex " << basename << "_static.c " << basename << "_static_mex.c" << endl + << "end" << endl; + } + + // Add path for block option with M-files + if (block && !byte_code) + mOutputFile << "addpath " << basename << ";" << endl; + + if (mod_file_struct.ramsey_policy_present) + mOutputFile << "M_.orig_eq_nbr = " << ramsey_policy_orig_eqn_nbr << ";" << endl; + + if (dynamic_model.equation_number() > 0) + { + dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + if (!no_static) + static_model.writeOutput(mOutputFile, block); + } + + // Print statements + for (vector::const_iterator it = statements.begin(); + it != statements.end(); it++) + { + (*it)->writeOutput(mOutputFile, basename); + + /* Special treatment for initval block: insert initial values for the + auxiliary variables and initialize exo det */ + InitValStatement *ivs = dynamic_cast(*it); + if (ivs != NULL) + { + static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); + ivs->writeOutputPostInit(mOutputFile); + } + + // Special treatment for endval block: insert initial values for the auxiliary variables + EndValStatement *evs = dynamic_cast(*it); + if (evs != NULL) + static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); + + // Special treatment for load params and steady state statement: insert initial values for the auxiliary variables + LoadParamsAndSteadyStateStatement *lpass = dynamic_cast(*it); + if (lpass && !no_static) + static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); + } + + // Remove path for block option with M-files + if (block && !byte_code) + mOutputFile << "rmpath " << basename << ";" << endl; + + mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl; + + config_file.writeEndParallel(mOutputFile); + + mOutputFile << endl << endl + << "disp(['Total computing time : ' dynsec2hms(toc) ]);" << endl; + + if (!no_warn) + { + if (warnings.countWarnings() > 0) + mOutputFile << "disp('Note: " << warnings.countWarnings() << " warning(s) encountered in the preprocessor')" << endl; + + mOutputFile << "if ~isempty(lastwarn)" << endl + << " disp('Note: warning(s) encountered in MATLAB/Octave code')" << endl + << "end" << endl; + } + + + if (!no_log) + mOutputFile << "diary off" << endl; + + mOutputFile.close(); + + // Create static and dynamic files + if (dynamic_model.equation_number() > 0) + { + if (!no_static) + { + static_model.writeStaticFile(basename, block, byte_code, use_dll); + static_model.writeParamsDerivativesFile(basename); + } + + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + dynamic_model.writeParamsDerivativesFile(basename); + } + + // Create steady state file + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_policy_present); + + cout << "done" << endl; +} diff --git a/ExternalFiles.hh b/ExternalFiles.hh new file mode 100644 index 00000000..ee0f6cf9 --- /dev/null +++ b/ExternalFiles.hh @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2006-2013 Dynare Team + * + * This file is part of Dynare. + * + * Dynare is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dynare is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dynare. If not, see . + */ + +#ifndef _EXTERNAL_FILES_HH +#define _EXTERNAL_FILES_HH + +using namespace std; + +#include + +#include "ModFile.hh" + + +class ExternalFiles +{ +public: + void writeHeaders(const string &basename, bool cuda); +}; + + +#endif diff --git a/ModFile.cc b/ModFile.cc index 6239db39..aa23a422 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -913,3 +913,26 @@ ModFile::writeCOutputFiles(const string &basename) const << "ls preprocessorOutput.cc" << endl << endl; mOutputFile.close(); } + +void +ModFile::writeExternalFiles(const string &basename, OutputType output, bool cuda) const +{ + ExternalFiles::writeHeaders(basename, cuda); + ExternalFiles::writeModelCC(cuda); + steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_policy_present, cuda); + static_model.writeStaticFile(basename, block, byte_code, use_dll); + static_model.writeParamsDerivativesFile(basename); + static_model.writeAuxVarInitvalCC(mOutputFile, oMatlabOutsideModel); + + dynamic_model.writeResiduals(basename, cuda); + dynamic_model.writeParamsDerivativesFile(basename, cuda); + dynamic_model.writeFirstDerivatives(basename, cuda); + + if (output == second) + dynamic_model.writeSecondDerivatives(basename, cuda); + else if (output == third) + { + dynamic_model.writeSecondDerivatives(basename, cuda); + dynamic_model.writeThirdDerivatives(basename, cuda); + } +} diff --git a/ModFile.hh b/ModFile.hh index 1f8aa0c9..741dc531 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -35,6 +35,8 @@ using namespace std; #include "ExternalFunctionsTable.hh" #include "ConfigFile.hh" #include "WarningConsolidation.hh" +#include "DynareOutput.hh" +#include "ExternalFiles.hh" //! The abstract representation of a "mod" file class ModFile @@ -142,6 +144,7 @@ public: ) const; //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; + void writeExternalFiles(const string &basename, OutputType output, bool cuda) const; }; #endif // ! MOD_FILE_HH From 71824b3d4c5ffa2da3e6603cf9bf4e810c364cfc Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Wed, 14 Aug 2013 21:35:06 +0200 Subject: [PATCH 109/162] extended-preprocessor: finished removing c_driver mechanism --- ModFile.cc | 5 +---- ModFile.hh | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index aa23a422..954e02b6 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -40,7 +40,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg) linear(false), block(false), byte_code(false), use_dll(false), no_static(false), differentiate_forward_vars(false), nonstationary_variables(false), ramsey_policy_orig_eqn_nbr(0), - warnings(warnings_arg), c_driver(false) + warnings(warnings_arg) { } @@ -444,9 +444,6 @@ ModFile::transformPass(bool nostrict) << "greater than or equal to the number of observed variables." << endl; exit(EXIT_FAILURE); } - - if (mod_file_struct.ms_dsge_present) - c_driver = true; } void diff --git a/ModFile.hh b/ModFile.hh index 741dc531..5e4f01de 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -100,9 +100,6 @@ public: /*! (i.e. option parallel_local_files of model block) */ vector parallel_local_files; - //! Output C driver file as opposed to Matlab/Octave driver file - bool c_driver; - private: //! List of statements vector statements; From 6e0ef026ac786173bb01b090b076186214c75bf4 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Tue, 17 Sep 2013 15:11:57 +0200 Subject: [PATCH 110/162] write C++ version of model functions without temporary terms --- DynamicModel.cc | 356 +++++++++++++++++++++++++++++++++++++++++++- DynamicModel.hh | 16 ++ DynareMain.cc | 8 +- DynareMain2.cc | 2 +- ExprNode.cc | 11 +- ExprNode.hh | 3 +- ExternalFiles.hh | 2 + ModFile.cc | 71 ++++----- ModFile.hh | 3 +- ModelTree.hh | 1 - SteadyStateModel.cc | 55 +++++++ SteadyStateModel.hh | 1 + 12 files changed, 476 insertions(+), 53 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 57317e47..48f665ee 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2089,8 +2089,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : oMatlabDynamicModel); deriv_node_temp_terms_t tef_terms; - writeModelLocalVariables(model_output, output_type, tef_terms); - + writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms); writeModelEquations(model_output, output_type); @@ -4419,3 +4418,356 @@ DynamicModel::dynamicOnlyEquationsNbr() const return eqs.size(); } +void +DynamicModel::writeFirstDerivativesCC(const string &basename, bool cuda) const +{ + string filename = basename + "_first_derivatives.cc"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes first order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // Write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void FirstDerivatives(const 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; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + // Writing Jacobian + for (first_derivatives_t::const_iterator it = first_derivatives.begin(); + it != first_derivatives.end(); it++) + { + int eq = it->first.first; + int var = it->first.second; + expr_t d1 = it->second; + + jacobianHelper(mDynamicModelFile, eq, getDynJacobianCol(var), oCDynamicModel); + mDynamicModelFile << "="; + // oCstaticModel makes reference to the static variables + d1->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + } + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +// using compressed sparse row format (CSR) +void +DynamicModel::writeSecondDerivativesCC_csr(const string &basename, bool cuda) const +{ + + string filename = basename + "_second_derivatives.cc"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes second order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void SecondDerivatives(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, int *row_ptr, int *col_ptr, double *value)" << endl + << "{" << endl; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + // Indexing derivatives in column order + vector d_order(NNZDerivatives[1]); + OrderByLinearAddress obla(NNZDerivatives[1]); + int counter = 0; + int dynHessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + for (second_derivatives_t::const_iterator it = second_derivatives.begin(); + it != second_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + + int col_nb = id1 * dynJacobianColsNbr + id2; + int col_nb_sym = id2 * dynJacobianColsNbr + id1; + + obla.linear_address[counter] = col_nb + eq*dynHessianColsNbr; + d_order[counter] = counter; + ++counter; + if (id1 != id2) + { + obla.linear_address[counter] = col_nb_sym + eq*dynHessianColsNbr; + d_order[counter] = counter; + ++counter; + } + } + sort(d_order.begin(), d_order.end(), obla); + + // Writing Hessian + vector row_ptr(equations.size()); + fill(row_ptr.begin(),row_ptr.end(),0.0); + int k = 0; // Keep the order of a 2nd derivative + for (second_derivatives_t::const_iterator it = second_derivatives.begin(); + it != second_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second; + expr_t d2 = it->second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + + int col_nb = id1 * dynJacobianColsNbr + id2; + int col_nb_sym = id2 * dynJacobianColsNbr + id1; + + row_ptr[eq]++; + mDynamicModelFile << "col_ptr[" << d_order[k] << "] " + << "=" << col_nb << ";" << endl; + mDynamicModelFile << "value[" << d_order[k] << "] = "; + // oCstaticModel makes reference to the static variables + d2->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + + k++; + + // Treating symetric elements + if (id1 != id2) + { + row_ptr[eq]++; + mDynamicModelFile << "col_ptr[" << d_order[k] << "] " + << "=" << col_nb_sym << ";" << endl; + mDynamicModelFile << "value[" << d_order[k] << "] = "; + // oCstaticModel makes reference to the static variables + d2->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + + k++; + } + } + + // row_ptr must point to the relative address of the first element of the row + int cumsum = 0; + mDynamicModelFile << "row_ptr = [ 0"; + for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) + { + cumsum += *it; + mDynamicModelFile << ", " << cumsum; + } + mDynamicModelFile << "];" << endl; + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +void +DynamicModel::writeThirdDerivativesCC_csr(const string &basename, bool cuda) const +{ + string filename = basename + "_third_derivatives.cc"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes third order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // Write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void ThirdDerivatives(const 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; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + // Indexing derivatives in column order + vector d_order(NNZDerivatives[1]); + OrderByLinearAddress obla(NNZDerivatives[1]); + int counter = 0; + int dynHessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + for (third_derivatives_t::const_iterator it = third_derivatives.begin(); + it != third_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second.first; + int var3 = it->first.second.second.second; + expr_t d3 = it->second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + int id3 = getDynJacobianCol(var3); + + // Reference column number for the g3 matrix + int ref_col = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; + + sparseHelper(3, mDynamicModelFile, k, 0, oCDynamicModel); + mDynamicModelFile << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k, 1, oCDynamicModel); + mDynamicModelFile << "=" << ref_col + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); + mDynamicModelFile << "="; + // oCstaticModel makes reference to the static variables + d3->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + set cols; + cols.insert(id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2); + cols.insert(id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3); + cols.insert(id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1); + cols.insert(id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2); + cols.insert(id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1); + + int k2 = 1; // Keeps the offset of the permutation relative to k + for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) + if (*it2 != ref_col) + { + sparseHelper(3, mDynamicModelFile, k+k2, 0, oCDynamicModel); + mDynamicModelFile << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k+k2, 1, oCDynamicModel); + mDynamicModelFile << "=" << *it2 + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k+k2, 2, oCDynamicModel); + mDynamicModelFile << "="; + sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); + mDynamicModelFile << ";" << endl; + + k2++; + } + k += k2; + } + + // Writing third derivatives + int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr; + int k = 0; // Keep the line of a 3rd derivative in v3 + for (third_derivatives_t::const_iterator it = third_derivatives.begin(); + it != third_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second.first; + int var3 = it->first.second.second.second; + expr_t d3 = it->second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + int id3 = getDynJacobianCol(var3); + + // Reference column number for the g3 matrix + int ref_col = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; + + sparseHelper(3, mDynamicModelFile, k, 0, oCDynamicModel); + mDynamicModelFile << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k, 1, oCDynamicModel); + mDynamicModelFile << "=" << ref_col + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); + mDynamicModelFile << "="; + // oCstaticModel makes reference to the static variables + d3->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + set cols; + cols.insert(id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2); + cols.insert(id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3); + cols.insert(id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1); + cols.insert(id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2); + cols.insert(id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1); + + int k2 = 1; // Keeps the offset of the permutation relative to k + for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) + if (*it2 != ref_col) + { + sparseHelper(3, mDynamicModelFile, k+k2, 0, oCDynamicModel); + mDynamicModelFile << "=" << eq + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k+k2, 1, oCDynamicModel); + mDynamicModelFile << "=" << *it2 + 1 << ";" << endl; + + sparseHelper(3, mDynamicModelFile, k+k2, 2, oCDynamicModel); + mDynamicModelFile << "="; + sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); + mDynamicModelFile << ";" << endl; + + k2++; + } + k += k2; + } + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +OrderByLinearAddress::OrderByLinearAddress(int size) +{ + linear_address.resize(size); +} + +bool OrderByLinearAddress::operator()(const int i1, const int i2) const +{ + return linear_address[i1] < linear_address[i2]; +} + diff --git a/DynamicModel.hh b/DynamicModel.hh index e23816a0..720e23ea 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -223,6 +223,12 @@ public: void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const; //! Writes file containing parameters derivatives void writeParamsDerivativesFile(const string &basename) const; + //! Writes CC file containing first order derivatives of model evaluated at steady state + void writeFirstDerivativesCC(const string &basename, bool cuda) const; + //! Writes CC file containing second order derivatives of model evaluated at steady state (compressed sparse column) + void writeSecondDerivativesCC_csr(const string &basename, bool cuda) const; + //! Writes CC file containing third order derivatives of model evaluated at steady state (compressed sparse column) + void writeThirdDerivativesCC_csr(const string &basename, bool cuda) const; //! Converts to static model (only the equations) /*! It assumes that the static model given in argument has just been allocated */ void toStatic(StaticModel &static_model) const; @@ -467,4 +473,14 @@ public: bool isModelLocalVariableUsed() const; }; +//! Class to re-order derivatives for various sparse storage formats +class OrderByLinearAddress +{ +public: + //! vector of linear addresses in derivatives matrices + vector linear_address; + OrderByLinearAddress(int size); + //! Order by linear address in a matrix + bool operator()(const int i1, const int i2) const; +}; #endif diff --git a/DynareMain.cc b/DynareMain.cc index 383ffa08..633f5f97 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -199,13 +199,15 @@ main(int argc, char **argv) cerr << "Incorrect syntax for ouput option" << endl; usage(); } + // we don't want temp terms in CC functions + no_tmp_terms = true; if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 7, "dynamic", 7)) output_mode = dynamic; - else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "first", 5)) + else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "first", 5)) output_mode = first; - else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 7, "second", 6)) + else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 7, "second", 6)) output_mode = second; - else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "third", 5)) + else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "third", 5)) output_mode = third; else { diff --git a/DynareMain2.cc b/DynareMain2.cc index cb9beac5..44416b32 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -54,7 +54,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm mod_file->evalAllExpressions(warn_uninit); // Do computations - mod_file->computingPass(no_tmp_terms); + mod_file->computingPass(no_tmp_terms, output_mode); // Write outputs if (output_mode != none) diff --git a/ExprNode.cc b/ExprNode.cc index 2fff674b..6639416f 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -673,6 +673,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oSteadyStateFile: output << "ys_(" << tsid + 1 << ")"; break; + case oCSteadyStateFile: + output << "ys_[" << tsid << "]"; + break; default: cerr << "VariableNode::writeOutput: should not reach this point" << endl; exit(EXIT_FAILURE); @@ -715,6 +718,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oSteadyStateFile: output << "exo_(" << i << ")"; break; + case oCSteadyStateFile: + output << "exo_[" << i - 1 << "]"; + break; default: cerr << "VariableNode::writeOutput: should not reach this point" << endl; exit(EXIT_FAILURE); @@ -757,6 +763,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oSteadyStateFile: output << "exo_(" << i << ")"; break; + case oCSteadyStateFile: + output << "exo_[" << i - 1 << "]"; + break; default: cerr << "VariableNode::writeOutput: should not reach this point" << endl; exit(EXIT_FAILURE); @@ -4376,7 +4385,7 @@ ExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType output_typ const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const { - if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile) + if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile || output_type == oCSteadyStateFile) { output << datatree.symbol_table.getName(symb_id) << "("; writeExternalFunctionArguments(output, output_type, temporary_terms, tef_terms); diff --git a/ExprNode.hh b/ExprNode.hh index aebdead2..fd53a62d 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -73,7 +73,8 @@ enum ExprNodeOutputType oMatlabDynamicSteadyStateOperator, //!< Matlab code, dynamic model, inside a steady state operator oMatlabDynamicSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model, inside a steady state operator oCDynamicSteadyStateOperator, //!< C code, dynamic model, inside a steady state operator - oSteadyStateFile //!< Matlab code, in the generated steady state file + oSteadyStateFile, //!< Matlab code, in the generated steady state file + oCSteadyStateFile //!< C code, in the generated steady state file }; #define IS_MATLAB(output_type) ((output_type) == oMatlabStaticModel \ diff --git a/ExternalFiles.hh b/ExternalFiles.hh index ee0f6cf9..7e34ab9d 100644 --- a/ExternalFiles.hh +++ b/ExternalFiles.hh @@ -30,7 +30,9 @@ using namespace std; class ExternalFiles { public: + void eraseFiles(const string &basename); void writeHeaders(const string &basename, bool cuda); + void writeModelCC(const string &basename, bool cuda); }; diff --git a/ModFile.cc b/ModFile.cc index 954e02b6..5725a6dc 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -447,7 +447,7 @@ ModFile::transformPass(bool nostrict) } void -ModFile::computingPass(bool no_tmp_terms) +ModFile::computingPass(bool no_tmp_terms, OutputType output) { // Mod file may have no equation (for example in a standalone BVAR estimation) if (dynamic_model.equation_number() > 0) @@ -493,8 +493,14 @@ ModFile::computingPass(bool no_tmp_terms) cerr << "ERROR: Incorrect order option..." << endl; exit(EXIT_FAILURE); } - bool hessian = mod_file_struct.order_option >= 2 || mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation; - bool thirdDerivatives = mod_file_struct.order_option == 3 || mod_file_struct.estimation_analytic_derivation; + bool hessian = mod_file_struct.order_option >= 2 + || mod_file_struct.identification_present + || mod_file_struct.estimation_analytic_derivation + || output == second + || output == third; + bool thirdDerivatives = mod_file_struct.order_option == 3 + || mod_file_struct.estimation_analytic_derivation + || output == third; bool paramsDerivatives = mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation; dynamic_model.computingPass(true, hessian, thirdDerivatives, paramsDerivatives, global_eval_context, no_tmp_terms, block, use_dll, byte_code); } @@ -799,29 +805,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b } void -ModFile::writeCOutputFiles(const string &basename) const +ModFile::writeModelCC(const string &basename, bool cuda) const { - // Erase possible remnants of previous runs - string dynfile = basename + "_dynamic.m"; - unlink(dynfile.c_str()); - - dynfile = basename + "_dynamic.c"; - unlink(dynfile.c_str()); - - dynfile = basename + "_dynamic_mex.c"; - unlink(dynfile.c_str()); - - string statfile = basename + "_static.m"; - unlink(statfile.c_str()); - - statfile = basename + "_static.c"; - unlink(statfile.c_str()); - - statfile = basename + "_static_mex.c"; - unlink(statfile.c_str()); - - string filename = "preprocessorOutput.cc"; - unlink(filename.c_str()); + string filename = basename + ".cc"; ofstream mDriverCFile; mDriverCFile.open(filename.c_str(), ios::out | ios::binary); @@ -847,8 +833,6 @@ ModFile::writeCOutputFiles(const string &basename) const // Write basic info symbol_table.writeCOutput(mDriverCFile); - dynamic_model.writeCOutput(mDriverCFile, basename, false, false, true, mod_file_struct.order_option, mod_file_struct.estimation_present); - mDriverCFile << "/*" << endl << " * Writing statements" << endl << " */" << endl @@ -875,11 +859,6 @@ ModFile::writeCOutputFiles(const string &basename) const mDriverCFile << "}" << endl; mDriverCFile.close(); - dynamic_model.writeDynamicFile(basename, false, false, true, mod_file_struct.order_option); - if (!no_static) - static_model.writeStaticFile(basename, false, false, true); - - // Write informational m file ofstream mOutputFile; @@ -914,22 +893,28 @@ ModFile::writeCOutputFiles(const string &basename) const void ModFile::writeExternalFiles(const string &basename, OutputType output, bool cuda) const { - ExternalFiles::writeHeaders(basename, cuda); - ExternalFiles::writeModelCC(cuda); + writeModelCC(basename, cuda); steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_policy_present, cuda); - static_model.writeStaticFile(basename, block, byte_code, use_dll); - static_model.writeParamsDerivativesFile(basename); - static_model.writeAuxVarInitvalCC(mOutputFile, oMatlabOutsideModel); - dynamic_model.writeResiduals(basename, cuda); - dynamic_model.writeParamsDerivativesFile(basename, cuda); - dynamic_model.writeFirstDerivatives(basename, cuda); + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + + if (!no_static) + static_model.writeStaticFile(basename, false, false, true); + + + // static_model.writeStaticCFile(basename, block, byte_code, use_dll); + // static_model.writeParamsDerivativesFileCC(basename, cuda); + // static_model.writeAuxVarInitvalCC(mOutputFile, oMatlabOutsideModel, cuda); + + // dynamic_model.writeResidualsCC(basename, cuda); + // dynamic_model.writeParamsDerivativesFileCC(basename, cuda); + dynamic_model.writeFirstDerivativesCC(basename, cuda); if (output == second) - dynamic_model.writeSecondDerivatives(basename, cuda); + dynamic_model.writeSecondDerivativesCC_csr(basename, cuda); else if (output == third) { - dynamic_model.writeSecondDerivatives(basename, cuda); - dynamic_model.writeThirdDerivatives(basename, cuda); + dynamic_model.writeSecondDerivativesCC_csr(basename, cuda); + dynamic_model.writeThirdDerivativesCC_csr(basename, cuda); } } diff --git a/ModFile.hh b/ModFile.hh index 5e4f01de..9301283c 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -123,7 +123,7 @@ public: void transformPass(bool nostrict); //! Execute computations /*! \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */ - void computingPass(bool no_tmp_terms); + void computingPass(bool no_tmp_terms, OutputType output); //! Writes Matlab/Octave output files /*! \param basename The base name used for writing output files. Should be the name of the mod file without its extension @@ -141,6 +141,7 @@ public: ) const; //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; + void writeModelCC(const string &basename, bool cuda) const; void writeExternalFiles(const string &basename, OutputType output, bool cuda) const; }; diff --git a/ModelTree.hh b/ModelTree.hh index ffdb02ac..4d161daf 100644 --- a/ModelTree.hh +++ b/ModelTree.hh @@ -161,7 +161,6 @@ protected: void computeThirdDerivatives(const set &vars); //! Computes derivatives of the Jacobian and Hessian w.r. to parameters void computeParamsDerivatives(); - //! Write derivative of an equation w.r. to a variable void writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; //! Computes temporary terms (for all equations and derivatives) diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index a8770c78..d84b4f77 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -145,3 +145,58 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_polic << "end" << endl; } +void +SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const +{ + string filename = basename + "_steadystate.cc"; + + ofstream output; + output.open(filename.c_str(), ios::out | ios::binary); + if (!output.is_open()) + { + cerr << "ERROR: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + if (cuda) + output << "__global__ "; + + output << "void steadystate(" + << "const double *exo_, const double *params_, double *ys_, int *info)" << endl + << "// Steady state file generated by Dynare preprocessor" << endl + << "{" << endl + << " info = 0;" << endl; + + if (recursive_order.size() == 0) + { + output << " return;" << endl + << "}" << endl; + return; + } + + for (size_t i = 0; i < recursive_order.size(); i++) + { + const vector &symb_ids = recursive_order[i]; + output << " "; + if (symb_ids.size() > 1) + output << "["; + for (size_t j = 0; j < symb_ids.size(); j++) + { + variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[j], 0)); + assert(it != variable_node_map.end()); + dynamic_cast(it->second)->writeOutput(output, oSteadyStateFile); + if (j < symb_ids.size()-1) + output << ","; + } + if (symb_ids.size() > 1) + output << "]"; + + output << "="; + def_table.find(symb_ids)->second->writeOutput(output, oSteadyStateFile); + output << ";" << endl; + } + output << " // Auxiliary equations" << endl; + static_model.writeAuxVarInitval(output, oSteadyStateFile); + output << "}" << endl; +} + diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index cbb206af..d2184ffc 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -49,6 +49,7 @@ public: \param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ void writeSteadyStateFile(const string &basename, bool ramsey_policy) const; + void writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const; }; #endif From 17d017727604f45a8eb005626b49ccbfc2748186 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 13 Oct 2013 17:49:10 +0200 Subject: [PATCH 111/162] returning sparse matrix in compressed format --- DynamicModel.cc | 237 +++++++++++++++++------------------------------- DynamicModel.hh | 24 +++-- 2 files changed, 98 insertions(+), 163 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 48f665ee..8ecbef31 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -4511,10 +4511,8 @@ DynamicModel::writeSecondDerivativesCC_csr(const string &basename, bool cuda) co deriv_node_temp_terms_t tef_terms; // Indexing derivatives in column order - vector d_order(NNZDerivatives[1]); - OrderByLinearAddress obla(NNZDerivatives[1]); - int counter = 0; - int dynHessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + vector D; + int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; for (second_derivatives_t::const_iterator it = second_derivatives.begin(); it != second_derivatives.end(); it++) { @@ -4526,61 +4524,32 @@ DynamicModel::writeSecondDerivativesCC_csr(const string &basename, bool cuda) co int id2 = getDynJacobianCol(var2); int col_nb = id1 * dynJacobianColsNbr + id2; - int col_nb_sym = id2 * dynJacobianColsNbr + id1; - obla.linear_address[counter] = col_nb + eq*dynHessianColsNbr; - d_order[counter] = counter; - ++counter; + derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); + D.push_back(deriv); if (id1 != id2) { - obla.linear_address[counter] = col_nb_sym + eq*dynHessianColsNbr; - d_order[counter] = counter; - ++counter; + col_nb = id2 * dynJacobianColsNbr + id1; + derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); + D.push_back(deriv); } } - sort(d_order.begin(), d_order.end(), obla); + sort(D.begin(), D.end(), derivative_less_than() ); // Writing Hessian vector row_ptr(equations.size()); fill(row_ptr.begin(),row_ptr.end(),0.0); - int k = 0; // Keep the order of a 2nd derivative - for (second_derivatives_t::const_iterator it = second_derivatives.begin(); - it != second_derivatives.end(); it++) + int k = 0; + for(vector::const_iterator it = D.begin(); it != D.end(); ++it) { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second; - expr_t d2 = it->second; - - int id1 = getDynJacobianCol(var1); - int id2 = getDynJacobianCol(var2); - - int col_nb = id1 * dynJacobianColsNbr + id2; - int col_nb_sym = id2 * dynJacobianColsNbr + id1; - - row_ptr[eq]++; - mDynamicModelFile << "col_ptr[" << d_order[k] << "] " - << "=" << col_nb << ";" << endl; - mDynamicModelFile << "value[" << d_order[k] << "] = "; + row_ptr[it->row_nbr]++; + mDynamicModelFile << "col_ptr[" << k << "] " + << "=" << it->col_nbr << ";" << endl; + mDynamicModelFile << "value[" << k << "] = "; // oCstaticModel makes reference to the static variables - d2->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); mDynamicModelFile << ";" << endl; - k++; - - // Treating symetric elements - if (id1 != id2) - { - row_ptr[eq]++; - mDynamicModelFile << "col_ptr[" << d_order[k] << "] " - << "=" << col_nb_sym << ";" << endl; - mDynamicModelFile << "value[" << d_order[k] << "] = "; - // oCstaticModel makes reference to the static variables - d2->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); - mDynamicModelFile << ";" << endl; - - k++; - } } // row_ptr must point to the relative address of the first element of the row @@ -4634,11 +4603,9 @@ DynamicModel::writeThirdDerivativesCC_csr(const string &basename, bool cuda) con // this is always empty here, but needed by d1->writeOutput deriv_node_temp_terms_t tef_terms; - // Indexing derivatives in column order - vector d_order(NNZDerivatives[1]); - OrderByLinearAddress obla(NNZDerivatives[1]); - int counter = 0; - int dynHessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + vector D; + int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + int thirdDerivativesColsNbr = hessianColsNbr*dynJacobianColsNbr; for (third_derivatives_t::const_iterator it = third_derivatives.begin(); it != third_derivatives.end(); it++) { @@ -4646,114 +4613,81 @@ DynamicModel::writeThirdDerivativesCC_csr(const string &basename, bool cuda) con int var1 = it->first.second.first; int var2 = it->first.second.second.first; int var3 = it->first.second.second.second; - expr_t d3 = it->second; int id1 = getDynJacobianCol(var1); int id2 = getDynJacobianCol(var2); int id3 = getDynJacobianCol(var3); - // Reference column number for the g3 matrix - int ref_col = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; - - sparseHelper(3, mDynamicModelFile, k, 0, oCDynamicModel); - mDynamicModelFile << "=" << eq + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k, 1, oCDynamicModel); - mDynamicModelFile << "=" << ref_col + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); - mDynamicModelFile << "="; - // oCstaticModel makes reference to the static variables - d3->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); - mDynamicModelFile << ";" << endl; - - // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) - set cols; - cols.insert(id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2); - cols.insert(id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3); - cols.insert(id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1); - cols.insert(id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2); - cols.insert(id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1); - - int k2 = 1; // Keeps the offset of the permutation relative to k - for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) - if (*it2 != ref_col) - { - sparseHelper(3, mDynamicModelFile, k+k2, 0, oCDynamicModel); - mDynamicModelFile << "=" << eq + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k+k2, 1, oCDynamicModel); - mDynamicModelFile << "=" << *it2 + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k+k2, 2, oCDynamicModel); - mDynamicModelFile << "="; - sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); - mDynamicModelFile << ";" << endl; - - k2++; - } - k += k2; + // Reference column number for the g3 matrix (with symmetrical derivatives) + vector cols; + long unsigned int col_nb = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; + int thirdDColsNbr = hessianColsNbr*dynJacobianColsNbr; + derivative deriv(col_nb + eq*thirdDColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + col_nb = id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + } } - // Writing third derivatives - int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr; - int k = 0; // Keep the line of a 3rd derivative in v3 - for (third_derivatives_t::const_iterator it = third_derivatives.begin(); - it != third_derivatives.end(); it++) + sort(D.begin(), D.end(), derivative_less_than() ); + + vector row_ptr(equations.size()); + fill(row_ptr.begin(),row_ptr.end(),0.0); + int k = 0; + for(vector::const_iterator it = D.begin(); it != D.end(); ++it) { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second.first; - int var3 = it->first.second.second.second; - expr_t d3 = it->second; - - int id1 = getDynJacobianCol(var1); - int id2 = getDynJacobianCol(var2); - int id3 = getDynJacobianCol(var3); - - // Reference column number for the g3 matrix - int ref_col = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; - - sparseHelper(3, mDynamicModelFile, k, 0, oCDynamicModel); - mDynamicModelFile << "=" << eq + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k, 1, oCDynamicModel); - mDynamicModelFile << "=" << ref_col + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); - mDynamicModelFile << "="; + row_ptr[it->row_nbr]++; + mDynamicModelFile << "col_ptr[" << k << "] " + << "=" << it->col_nbr << ";" << endl; + mDynamicModelFile << "value[" << k << "] = "; // oCstaticModel makes reference to the static variables - d3->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); mDynamicModelFile << ";" << endl; - - // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) - set cols; - cols.insert(id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2); - cols.insert(id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3); - cols.insert(id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1); - cols.insert(id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2); - cols.insert(id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1); - - int k2 = 1; // Keeps the offset of the permutation relative to k - for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) - if (*it2 != ref_col) - { - sparseHelper(3, mDynamicModelFile, k+k2, 0, oCDynamicModel); - mDynamicModelFile << "=" << eq + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k+k2, 1, oCDynamicModel); - mDynamicModelFile << "=" << *it2 + 1 << ";" << endl; - - sparseHelper(3, mDynamicModelFile, k+k2, 2, oCDynamicModel); - mDynamicModelFile << "="; - sparseHelper(3, mDynamicModelFile, k, 2, oCDynamicModel); - mDynamicModelFile << ";" << endl; - - k2++; - } - k += k2; + k++; } + // row_ptr must point to the relative address of the first element of the row + int cumsum = 0; + mDynamicModelFile << "row_ptr = [ 0"; + for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) + { + cumsum += *it; + mDynamicModelFile << ", " << cumsum; + } + mDynamicModelFile << "];" << endl; + mDynamicModelFile << "}" << endl; writePowerDeriv(mDynamicModelFile, true); @@ -4761,13 +4695,4 @@ DynamicModel::writeThirdDerivativesCC_csr(const string &basename, bool cuda) con } -OrderByLinearAddress::OrderByLinearAddress(int size) -{ - linear_address.resize(size); -} - -bool OrderByLinearAddress::operator()(const int i1, const int i2) const -{ - return linear_address[i1] < linear_address[i2]; -} diff --git a/DynamicModel.hh b/DynamicModel.hh index 720e23ea..8de6bd39 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -473,14 +473,24 @@ public: bool isModelLocalVariableUsed() const; }; -//! Class to re-order derivatives for various sparse storage formats -class OrderByLinearAddress +//! Classes to re-order derivatives for various sparse storage formats +class derivative { public: - //! vector of linear addresses in derivatives matrices - vector linear_address; - OrderByLinearAddress(int size); - //! Order by linear address in a matrix - bool operator()(const int i1, const int i2) const; + long unsigned int linear_address; + long unsigned int col_nbr; + unsigned int row_nbr; + expr_t value; + derivative(long unsigned int arg1, long unsigned int arg2, int arg3, expr_t arg4): + linear_address(arg1), col_nbr(arg2), row_nbr(arg3), value(arg4) {}; +}; + +class derivative_less_than +{ +public: + bool operator()(const derivative & d1, const derivative & d2) const + { + return d1.linear_address < d2.linear_address; + } }; #endif From 86c3a2b8622a842ac671b1dd2aee01df311bf68d Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sat, 19 Oct 2013 15:45:42 +0200 Subject: [PATCH 112/162] Revert "ms-dsge: remove unnecessary output" This reverts commit 2568e87b4394961fed1da7d2337d6cae637b4166. --- NumericalInitialization.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 6acbd79b..0d6bcecc 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -47,6 +47,7 @@ InitParamStatement::writeOutput(ostream &output, const string &basename) const output << "M_.params( " << id << " ) = "; param_value->writeOutput(output); output << ";" << endl; + output << symbol_table.getName(symb_id) << " = M_.params( " << id << " );\n"; } void From 1cfaddcd448075b3fde942adf3dd9f229aa3482b Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 20 Oct 2013 14:57:10 +0200 Subject: [PATCH 113/162] extended_preprocessor: renaming files/class and bug fixing --- ModFile.cc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 5725a6dc..432778b5 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -824,9 +824,9 @@ ModFile::writeModelCC(const string &basename, bool cuda) const << " * from model file (.mod)" << endl << " */" << endl << endl - << "#include \"ms_dsge_c_driver.hh\"" << endl + << "#include \"dynare_cpp_driver.hh\"" << endl << endl - << "MsDsgeInfo *" << endl + << "DynareInfo *" << endl << "preprocessorOutput()" << endl << "{" << endl; @@ -837,7 +837,6 @@ ModFile::writeModelCC(const string &basename, bool cuda) const << " * Writing statements" << endl << " */" << endl << "/* prior args*/" << endl - << "MsDsgeInfo *msdsgeinfo = new MsDsgeInfo(exo_names, exo_det_names, endo_names, param_names, params, aux_vars, predetermined_variables, varobs, lead_lag_incidence, NNZDerivatives);" << endl << "int index, index1;" << endl << "string shape;" << endl << "double mean, mode, stdev, variance;" << endl @@ -848,15 +847,19 @@ ModFile::writeModelCC(const string &basename, bool cuda) const << "vector duration;" << endl << "restriction_map_t restriction_map;" << endl << "/* options args*/" << endl - << "double init;" << endl << endl; + << "double init;" << endl + << "vector< vector > lead_lag_incidence;" << endl + << "vector NNZDerivatives;" << endl + << "vector params(param_nbr);" << endl << endl; // Print statements for (vector::const_iterator it = statements.begin(); it != statements.end(); it++) (*it)->writeCOutput(mDriverCFile, basename); - mDriverCFile << "return msdsgeinfo;" << endl; - mDriverCFile << "}" << endl; + mDriverCFile << "DynareInfo *model_info = new DynareInfo(exo_names, exo_det_names, endo_names, param_names, params, aux_vars, predetermined_variables, varobs, lead_lag_incidence, NNZDerivatives);" << endl + << "return model_info;" << endl + << "}" << endl; mDriverCFile.close(); // Write informational m file From 6bee53c658265b122dfa183f2844f4dc40f82b88 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Mon, 21 Oct 2013 16:17:53 +0200 Subject: [PATCH 114/162] extended_preprocessor: fixed computation of steady state out of steady_state_model --- ExprNode.hh | 5 ++++- SteadyStateModel.cc | 28 ++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/ExprNode.hh b/ExprNode.hh index fd53a62d..c77c8cbc 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -86,7 +86,10 @@ enum ExprNodeOutputType || (output_type) == oMatlabDynamicSparseSteadyStateOperator \ || (output_type) == oSteadyStateFile) -#define IS_C(output_type) ((output_type) == oCDynamicModel || (output_type) == oCStaticModel || (output_type) == oCDynamicSteadyStateOperator) +#define IS_C(output_type) ((output_type) == oCDynamicModel \ + || (output_type) == oCStaticModel \ + || (output_type) == oCDynamicSteadyStateOperator \ + || (output_type) == oCSteadyStateFile) #define IS_LATEX(output_type) ((output_type) == oLatexStaticModel \ || (output_type) == oLatexDynamicModel \ diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index d84b4f77..2128f1bc 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -158,14 +158,16 @@ SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_pol exit(EXIT_FAILURE); } + output << "#include " << endl; + if (cuda) output << "__global__ "; output << "void steadystate(" - << "const double *exo_, const double *params_, double *ys_, int *info)" << endl + << "const double *exo_, const double *params, double *ys_, int *info)" << endl << "// Steady state file generated by Dynare preprocessor" << endl << "{" << endl - << " info = 0;" << endl; + << " *info = 0;" << endl; if (recursive_order.size() == 0) { @@ -179,24 +181,18 @@ SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_pol const vector &symb_ids = recursive_order[i]; output << " "; if (symb_ids.size() > 1) - output << "["; - for (size_t j = 0; j < symb_ids.size(); j++) - { - variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[j], 0)); - assert(it != variable_node_map.end()); - dynamic_cast(it->second)->writeOutput(output, oSteadyStateFile); - if (j < symb_ids.size()-1) - output << ","; - } - if (symb_ids.size() > 1) - output << "]"; - + std::cout << "Error: in C, multiple returns are not permitted in steady_state_model" << std::endl; + variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[0], 0)); + assert(it != variable_node_map.end()); + if (it->second->get_type() == eModFileLocalVariable) + output << "double "; + dynamic_cast(it->second)->writeOutput(output, oCSteadyStateFile); output << "="; - def_table.find(symb_ids)->second->writeOutput(output, oSteadyStateFile); + def_table.find(symb_ids)->second->writeOutput(output, oCSteadyStateFile); output << ";" << endl; } output << " // Auxiliary equations" << endl; - static_model.writeAuxVarInitval(output, oSteadyStateFile); + static_model.writeAuxVarInitval(output, oCSteadyStateFile); output << "}" << endl; } From 416b7b849f9e2065eb3fec261c7819afa28a7871 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Tue, 22 Oct 2013 18:31:59 +0200 Subject: [PATCH 115/162] extended-preprocessor: adding back writing of model local variables (#) --- DynamicModel.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 8ecbef31..8804551d 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2089,7 +2089,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : oMatlabDynamicModel); deriv_node_temp_terms_t tef_terms; - + writeModelLocalVariables(model_output, output_type, tef_terms); + writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms); writeModelEquations(model_output, output_type); From 3d11273a61835d364adab685205672e069cbd156 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 3 Nov 2013 18:05:38 +0100 Subject: [PATCH 116/162] extended-preprocessor: update test example1 --- DynamicModel.cc | 123 +++++++++--------------------------------------- ModFile.cc | 33 ++++--------- SymbolTable.cc | 14 ++---- 3 files changed, 34 insertions(+), 136 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 8804551d..1a5de8b9 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2959,124 +2959,43 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de void DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const { - /* Writing initialisation for M_.lead_lag_incidence matrix - M_.lead_lag_incidence is a matrix with as many columns as there are - endogenous variables and as many rows as there are periods in the - models (nbr of rows = M_.max_lag+M_.max_lead+1) - - The matrix elements are equal to zero if a variable isn't present in the - model at a given period. - */ - - vector state_var, state_equ; - output << endl - << "vector > lead_lag_incidence;" << endl - << "vector variable_presence;" << endl; + int lag_presence[3]; // Loop on endogenous variables - int nstatic = 0, - nfwrd = 0, - npred = 0, - nboth = 0; for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) { - output << "variable_presence.clear();" << endl; - int sstatic = 1, - sfwrd = 0, - spred = 0, - sboth = 0; + int varID; // Loop on periods - for (int lag = -max_endo_lag; lag <= max_endo_lead; lag++) - { - // Print variableID if exists with current period, otherwise print 0 + for (int lag = 0; lag <= 2; lag++) + { + lag_presence[lag] = 1; try { - int varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag); - output << "variable_presence.push_back(" << getDynJacobianCol(varID) << ");" << endl; - if (lag == -1) - { - sstatic = 0; - spred = 1; - } - else if (lag == 1) - if (spred == 1) - { - sboth = 1; - spred = 0; - } - else - { - sstatic = 0; - sfwrd = 1; - } + varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); } catch (UnknownDerivIDException &e) { - output << "variable_presence.push_back(0);" << endl; + lag_presence[lag] = 0; } } - nstatic += sstatic; - nfwrd += sfwrd; - npred += spred; - nboth += sboth; - output << "lead_lag_incidence.push_back(variable_presence);" << endl; + if (lag_presence[0] == 1) + if (lag_presence[2] == 1) + output << "zeta_mixed.push_back(" << endoID << ");" << endl; + else + output << "zeta_back.push_back(" << endoID << ");" << endl; + else if (lag_presence[2] == 1) + output << "zeta_fwrd.push_back(" << endoID << ");" << endl; + else + output << "zeta_static.push_back(" << endoID << ");" << endl; + } - output << "int nstatic = " << nstatic << ";" << endl - << "int nfwrd = " << nfwrd << ";" << endl - << "int npred = " << npred << ";" << endl - << "int nboth = " << nboth << ";" << endl; - for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) - // Loop on periods - for (int lag = -max_endo_lag; lag < 0; lag++) - // Print variableID if exists with current period, otherwise print 0 - try - { - getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag); - if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end()) - state_var.push_back(variable_reordered[endoID]); - } - catch (UnknownDerivIDException &e) - { - } - - // Writing initialization for some other variables - output << endl - << "int state_var[" << state_var.size() << "] = {"; - for (size_t i = 0; i < state_var.size(); i++) - if (i+1 == state_var.size()) - output << state_var[i]; - else - output << state_var[i] << ", "; - output << "};" << endl; - - output << endl - << "int maximum_lag = " << max_lag << ";" << endl - << "int maximum_lead = " << max_lead << ";" << endl; - - if (symbol_table.endo_nbr()) - output << endl - << "int maximum_endo_lag = " << max_endo_lag << ";" << endl - << "int maximum_endo_lead = " << max_endo_lead << ";" << endl - << "double steady_state[" << symbol_table.endo_nbr() << "];" << endl; - - if (symbol_table.exo_nbr()) - output << endl - << "int maximum_exo_lag = " << max_exo_lag << ";" << endl - << "int maximum_exo_lead = " << max_exo_lead << ";" << endl - << "double exo_steady_state[" << symbol_table.exo_nbr() << "];" << endl; - - if (symbol_table.exo_det_nbr()) - output << endl - << "int maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl - << "int maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl - << "double exo_det_steady_state[" << symbol_table.exo_det_nbr() << "];" << endl; - - output << endl - << "map params;" << endl; + output << "nstatic = zeta_static.size();" << endl + << "nfwrd = zeta_fwrd.size();" << endl + << "nback = zeta_back.size();" << endl + << "nmixed = zeta_mixed.size();" << endl; // Write number of non-zero derivatives // Use -1 if the derivatives have not been computed output << endl - << "vector NNZDerivatives;" << endl << "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl; if (order > 1) { diff --git a/ModFile.cc b/ModFile.cc index 432778b5..b58b6e65 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -826,40 +826,27 @@ ModFile::writeModelCC(const string &basename, bool cuda) const << endl << "#include \"dynare_cpp_driver.hh\"" << endl << endl - << "DynareInfo *" << endl - << "preprocessorOutput()" << endl + << "DynareInfo::DynareInfo(void)" << endl << "{" << endl; // Write basic info symbol_table.writeCOutput(mDriverCFile); - mDriverCFile << "/*" << endl - << " * Writing statements" << endl - << " */" << endl - << "/* prior args*/" << endl - << "int index, index1;" << endl - << "string shape;" << endl - << "double mean, mode, stdev, variance;" << endl - << "vector domain;" << endl - << "/* markov_switching args*/" << endl - << "int chain, number_of_regimes, number_of_lags, number_of_lags_was_passed;" << endl - << "vector parameters;" << endl - << "vector duration;" << endl - << "restriction_map_t restriction_map;" << endl - << "/* options args*/" << endl - << "double init;" << endl - << "vector< vector > lead_lag_incidence;" << endl - << "vector NNZDerivatives;" << endl - << "vector params(param_nbr);" << endl << endl; + mDriverCFile << endl << "params.resize(param_nbr);" << endl; + + if (dynamic_model.equation_number() > 0) + { + dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + // if (!no_static) + // static_model.writeCOutput(mOutputFile, block); + } // Print statements for (vector::const_iterator it = statements.begin(); it != statements.end(); it++) (*it)->writeCOutput(mDriverCFile, basename); - mDriverCFile << "DynareInfo *model_info = new DynareInfo(exo_names, exo_det_names, endo_names, param_names, params, aux_vars, predetermined_variables, varobs, lead_lag_incidence, NNZDerivatives);" << endl - << "return model_info;" << endl - << "}" << endl; + mDriverCFile << "}" << endl; mDriverCFile.close(); // Write informational m file diff --git a/SymbolTable.cc b/SymbolTable.cc index abcd3c67..21b406b8 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -293,23 +293,19 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) throw NotYetFrozenException(); output << endl - << "map exo_names, exo_det_names, endo_names, param_names;" << endl; - - output << endl - << "int exo_nbr = " << exo_nbr() << ";" << endl; + << "exo_nbr = " << exo_nbr() << ";" << endl; if (exo_nbr() > 0) for (int id = 0; id < exo_nbr(); id++) output << "exo_names[\"" << getName(exo_ids[id]) << "\"] = " << id << ";" << endl; output << endl - << "int exo_det_nbr = " << exo_det_nbr() << ";" << endl; + << "exo_det_nbr = " << exo_det_nbr() << ";" << endl; if (exo_det_nbr() > 0) for (int id = 0; id < exo_det_nbr(); id++) output << "exo_det_names[\"" << getName(exo_det_ids[id]) << "\"] = " << id << " ;" << endl; output << endl - << "int endo_nbr = " << endo_nbr() << ";" << endl - << "int orig_endo_nbr = " << orig_endo_nbr() << ";" << endl; + << "endo_nbr = " << endo_nbr() << ";" << endl; if (endo_nbr() > 0) for (int id = 0; id < endo_nbr(); id++) output << "endo_names[\"" << getName(endo_ids[id]) << "\"] = " << id << ";" << endl; @@ -321,8 +317,6 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) output << "param_names[\"" << getName(param_ids[id]) << "\"] = " << id << ";" << endl; // Write the auxiliary variable table - output << endl - << "vector aux_vars;" << endl; if (aux_vars.size() > 0) for (int i = 0; i < (int) aux_vars.size(); i++) { @@ -345,8 +339,6 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) output << "aux_vars.push_back(" << "av" << i << ");" << endl; } - output << endl - << "vector predetermined_variables, varobs;" << endl; if (predeterminedNbr() > 0) for (set::const_iterator it = predetermined_variables.begin(); it != predetermined_variables.end(); it++) From dab2dde8af11019737d869160a0e8e4bb9f7ae48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 24 Feb 2014 15:37:55 +0100 Subject: [PATCH 117/162] Small improvements to external functions code. --- DataTree.hh | 8 +++++++- ExprNode.cc | 24 ++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/DataTree.hh b/DataTree.hh index 3540a86d..9ef440f8 100644 --- a/DataTree.hh +++ b/DataTree.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -68,10 +68,16 @@ protected: binary_op_node_map_t binary_op_node_map; typedef map, expr_t>, TrinaryOpcode>, TrinaryOpNode *> trinary_op_node_map_t; trinary_op_node_map_t trinary_op_node_map; + + // (arguments, symb_id) -> ExternalFunctionNode typedef map, int>, ExternalFunctionNode *> external_function_node_map_t; external_function_node_map_t external_function_node_map; + + // ((arguments, deriv_idx), symb_id) -> FirstDerivExternalFunctionNode typedef map, int>, int>, FirstDerivExternalFunctionNode *> first_deriv_external_function_node_map_t; first_deriv_external_function_node_map_t first_deriv_external_function_node_map; + + // ((arguments, (deriv_idx1, deriv_idx2)), symb_id) -> SecondDerivExternalFunctionNode typedef map, pair >, int>, SecondDerivExternalFunctionNode *> second_deriv_external_function_node_map_t; second_deriv_external_function_node_map_t second_deriv_external_function_node_map; diff --git a/ExprNode.cc b/ExprNode.cc index 6639416f..11d23a6d 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2013 Dynare Team + * Copyright (C) 2007-2014 Dynare Team * * This file is part of Dynare. * @@ -4935,12 +4935,10 @@ FirstDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType return; } - int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id); + const int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id); assert(first_deriv_symb_id != eExtFunSetButNoNameProvided); - int tmpIndx = inputIndex; - if (IS_C(output_type)) - tmpIndx = tmpIndx - 1; + const int tmpIndx = inputIndex - ARRAY_SUBSCRIPT_OFFSET(output_type); if (first_deriv_symb_id == symb_id) output << "TEFD_" << getIndxInTefTerms(symb_id, tef_terms) @@ -4982,15 +4980,14 @@ FirstDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &inst int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id); assert(first_deriv_symb_id != eExtFunSetButNoNameProvided); - int tmpIndx = inputIndex; if (!lhs_rhs) { - FLDTEFD_ fldtefd(getIndxInTefTerms(symb_id, tef_terms), tmpIndx); + FLDTEFD_ fldtefd(getIndxInTefTerms(symb_id, tef_terms), inputIndex); fldtefd.write(CompileCode, instruction_number); } else { - FSTPTEFD_ fstptefd(getIndxInTefTerms(symb_id, tef_terms), tmpIndx); + FSTPTEFD_ fstptefd(getIndxInTefTerms(symb_id, tef_terms), inputIndex); fstptefd.write(CompileCode, instruction_number); } } @@ -5193,16 +5190,11 @@ SecondDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType return; } - int second_deriv_symb_id = datatree.external_functions_table.getSecondDerivSymbID(symb_id); + const int second_deriv_symb_id = datatree.external_functions_table.getSecondDerivSymbID(symb_id); assert(second_deriv_symb_id != eExtFunSetButNoNameProvided); - int tmpIndex1 = inputIndex1; - int tmpIndex2 = inputIndex2; - if (IS_C(output_type)) - { - tmpIndex1 = tmpIndex1 - 1; - tmpIndex2 = tmpIndex2 - 1; - } + const int tmpIndex1 = inputIndex1 - ARRAY_SUBSCRIPT_OFFSET(output_type); + const int tmpIndex2 = inputIndex2 - ARRAY_SUBSCRIPT_OFFSET(output_type); int indx = getIndxInTefTerms(symb_id, tef_terms); if (second_deriv_symb_id == symb_id) From eaf02155cb89586cc53e16185052853345382bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 24 Feb 2014 16:40:28 +0100 Subject: [PATCH 118/162] Remove some limitations of steady state model: - allow recursive definitions (Closes #554) - allow a variable to be defined twice, simply emit a warning (Ref #556) --- ModFile.cc | 4 ++-- SteadyStateModel.cc | 44 +++++++++++++++++++++----------------------- SteadyStateModel.hh | 8 ++++---- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index b58b6e65..51b85532 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2013 Dynare Team + * Copyright (C) 2006-2014 Dynare Team * * This file is part of Dynare. * @@ -113,7 +113,7 @@ ModFile::checkPass() (*it)->checkPass(mod_file_struct, warnings); // Check the steady state block - steady_state_model.checkPass(mod_file_struct.ramsey_policy_present); + steady_state_model.checkPass(mod_file_struct.ramsey_policy_present, warnings); // If order option has not been set, default to 2 if (!mod_file_struct.order_option) diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 2128f1bc..fa39c8e5 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2011 Dynare Team + * Copyright (C) 2010-2014 Dynare Team * * This file is part of Dynare. * @@ -39,8 +39,7 @@ SteadyStateModel::addDefinition(int symb_id, expr_t expr) // Add the variable vector v; v.push_back(symb_id); - recursive_order.push_back(v); - def_table[v] = expr; + def_table.push_back(make_pair(v, expr)); } void @@ -53,33 +52,32 @@ SteadyStateModel::addMultipleDefinitions(const vector &symb_ids, expr_t exp || symbol_table.getType(symb_ids[i]) == eModFileLocalVariable || symbol_table.getType(symb_ids[i]) == eParameter); } - recursive_order.push_back(symb_ids); - def_table[symb_ids] = expr; + def_table.push_back(make_pair(symb_ids, expr)); } void -SteadyStateModel::checkPass(bool ramsey_policy) const +SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) const { + if (ramsey_policy) + return; + vector so_far_defined; - for (size_t i = 0; i < recursive_order.size(); i++) + for (size_t i = 0; i < def_table.size(); i++) { - const vector &symb_ids = recursive_order[i]; + const vector &symb_ids = def_table[i].first; // Check that symbols are not already defined for (size_t j = 0; j < symb_ids.size(); j++) if (find(so_far_defined.begin(), so_far_defined.end(), symb_ids[j]) != so_far_defined.end()) - { - cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl; - exit(EXIT_FAILURE); - } - + warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl; + // Check that expression has no undefined symbol if (!ramsey_policy) { set used_symbols; - expr_t expr = def_table.find(symb_ids)->second; + const expr_t &expr = def_table[i].second; expr->collectVariables(eEndogenous, used_symbols); expr->collectVariables(eModFileLocalVariable, used_symbols); for (set::const_iterator it = used_symbols.begin(); @@ -87,7 +85,7 @@ SteadyStateModel::checkPass(bool ramsey_policy) const if (find(so_far_defined.begin(), so_far_defined.end(), *it) == so_far_defined.end()) { - cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(*it) + cerr << "ERROR: in the 'steady_state_model' block, variable '" << symbol_table.getName(*it) << "' is undefined in the declaration of variable '" << symbol_table.getName(symb_ids[0]) << "'" << endl; exit(EXIT_FAILURE); } @@ -100,7 +98,7 @@ SteadyStateModel::checkPass(bool ramsey_policy) const void SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_policy) const { - if (recursive_order.size() == 0) + if (def_table.size() == 0) return; string filename = basename + "_steadystate2.m"; @@ -118,9 +116,9 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_polic << "% Steady state generated by Dynare preprocessor" << endl << " info = 0;" << endl; - for (size_t i = 0; i < recursive_order.size(); i++) + for (size_t i = 0; i < def_table.size(); i++) { - const vector &symb_ids = recursive_order[i]; + const vector &symb_ids = def_table[i].first; output << " "; if (symb_ids.size() > 1) output << "["; @@ -136,7 +134,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_polic output << "]"; output << "="; - def_table.find(symb_ids)->second->writeOutput(output, oSteadyStateFile); + def_table[i].second->writeOutput(output, oSteadyStateFile); output << ";" << endl; } output << " % Auxiliary equations" << endl; @@ -169,16 +167,16 @@ SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_pol << "{" << endl << " *info = 0;" << endl; - if (recursive_order.size() == 0) + if (def_table.size() == 0) { output << " return;" << endl << "}" << endl; return; } - for (size_t i = 0; i < recursive_order.size(); i++) + for (size_t i = 0; i < def_table.size(); i++) { - const vector &symb_ids = recursive_order[i]; + const vector &symb_ids = def_table[i].first; output << " "; if (symb_ids.size() > 1) std::cout << "Error: in C, multiple returns are not permitted in steady_state_model" << std::endl; @@ -188,7 +186,7 @@ SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_pol output << "double "; dynamic_cast(it->second)->writeOutput(output, oCSteadyStateFile); output << "="; - def_table.find(symb_ids)->second->writeOutput(output, oCSteadyStateFile); + def_table[i].second->writeOutput(output, oCSteadyStateFile); output << ";" << endl; } output << " // Auxiliary equations" << endl; diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index d2184ffc..b5686157 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2011 Dynare Team + * Copyright (C) 2010-2014 Dynare Team * * This file is part of Dynare. * @@ -22,13 +22,13 @@ #include "DataTree.hh" #include "StaticModel.hh" +#include "WarningConsolidation.hh" class SteadyStateModel : public DataTree { private: //! Associates a set of symbol IDs (the variable(s) assigned in a given statement) to an expression (their assigned value) - map, expr_t> def_table; - vector > recursive_order; + vector, expr_t> > def_table; //! Reference to static model (for writing auxiliary equations) const StaticModel &static_model; @@ -43,7 +43,7 @@ public: /*! \param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then disable the check on the recursivity of the declarations */ - void checkPass(bool ramsey_policy) const; + void checkPass(bool ramsey_policy, WarningConsolidation &warnings) const; //! Write the steady state file /*! \param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values From cb9d603edc3e7c81d549a512c82c4e0c80b73ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 24 Feb 2014 17:27:19 +0100 Subject: [PATCH 119/162] Add a warning if some endogenous is not present in steady_state_model block. Closes #556 --- SteadyStateModel.cc | 9 +++++++++ SymbolTable.cc | 22 +++++++++++++++++++++- SymbolTable.hh | 6 +++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index fa39c8e5..9f166527 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -93,6 +93,15 @@ SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) copy(symb_ids.begin(), symb_ids.end(), back_inserter(so_far_defined)); } + + set orig_endogs = symbol_table.getOrigEndogenous(); + for (set::const_iterator it = orig_endogs.begin(); + it != orig_endogs.end(); ++it) + { + if (find(so_far_defined.begin(), so_far_defined.end(), *it) + == so_far_defined.end()) + warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(*it) << "' is not assigned a value" << endl; + } } void diff --git a/SymbolTable.cc b/SymbolTable.cc index 21b406b8..97b98430 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -591,3 +591,23 @@ SymbolTable::getEndogenous() const endogs.insert(it->second); return endogs; } + +bool +SymbolTable::isAuxiliaryVariable(int symb_id) const +{ + for (int i = 0; i < aux_vars.size(); i++) + if (aux_vars[i].get_symb_id() == symb_id) + return true; + return false; +} + +set +SymbolTable::getOrigEndogenous() const +{ + set origendogs; + for (symbol_table_type::const_iterator it = symbol_table.begin(); + it != symbol_table.end(); it++) + if (getType(it->second) == eEndogenous && !isAuxiliaryVariable(it->second)) + origendogs.insert(it->second); + return origendogs; +} diff --git a/SymbolTable.hh b/SymbolTable.hh index c8716014..8e0903e5 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -297,6 +297,10 @@ public: set getExogenous() const; //! Get list of endogenous variables set getEndogenous() const; + //! Is a given symbol an auxiliary variable + bool isAuxiliaryVariable(int symb_id) const; + //! Get list of endogenous variables without aux vars + set getOrigEndogenous() const; }; inline bool From 90d0bafac9837fb4fc1c8594701c0646785a96bb Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 25 Feb 2014 11:32:01 +0100 Subject: [PATCH 120/162] preprocessor: include avDiffForward in switch statement to supress clang warning --- SymbolTable.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/SymbolTable.cc b/SymbolTable.cc index 97b98430..00152684 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -329,6 +329,7 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) case avExoLead: case avExpectation: case avMultiplier: + case avDiffForward: break; case avEndoLag: case avExoLag: From d9f78ae33649572bed698ed476ef181a40b0705d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 25 Feb 2014 15:01:16 +0100 Subject: [PATCH 121/162] Remove obsolete files. --- ExternalFiles.cc | 259 ----------------------------------------------- ExternalFiles.hh | 39 ------- ModFile.hh | 1 - 3 files changed, 299 deletions(-) delete mode 100644 ExternalFiles.cc delete mode 100644 ExternalFiles.hh diff --git a/ExternalFiles.cc b/ExternalFiles.cc deleted file mode 100644 index 9bfd42ea..00000000 --- a/ExternalFiles.cc +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright (C) 2006-2013 Dynare Team - * - * This file is part of Dynare. - * - * Dynare is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dynare is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dynare. If not, see . - */ - -#input "ExternalFiles.hh" - -ExternalFiles::writeModelCCconst string &basename, bool cuda) -{ - ofstream mOutputFile; - - if (basename.size()) - { - string fname(basename); - fname += ".cc"; - cOutputFile.open(fname.c_str(), ios::out | ios::binary); - if (!cOutputFile.is_open()) - { - cerr << "ERROR: Can't open file " << fname - << " for writing" << endl; - exit(EXIT_FAILURE); - } - } - else - { - cerr << "ERROR: Missing file name" << endl; - exit(EXIT_FAILURE); - } - - cOutputFile << "%" << endl - << "% Status : dynare_model class initializing function " << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file " << baseline << "(.mod)" << endl << endl; - - cOutputFile << "#include \"dynare_model.hh\"" << endl << endl; - - cOutputFile << "dynare_model::dynare_model(void)" << endl - << "{" << endl - << " model_name = \""<< basename << "\";" << endl; - - symbol_table.writeOutputCC(hOutputFile); - - // Initialize M_.Sigma_e and M_.H - mOutputFile << "M_.Sigma_e = zeros(" << symbol_table.exo_nbr() << ", " - << symbol_table.exo_nbr() << ");" << endl; - - if (mod_file_struct.calibrated_measurement_errors) - mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " - << symbol_table.observedVariablesNbr() << ");" << endl; - else - mOutputFile << "M_.H = 0;" << endl; - - if (linear == 1) - mOutputFile << "options_.linear = 1;" << endl; - - mOutputFile << "options_.block=" << block << ";" << endl - << "options_.bytecode=" << byte_code << ";" << endl - << "options_.use_dll=" << use_dll << ";" << endl; - - if (parallel_local_files.size() > 0) - { - mOutputFile << "options_.parallel_info.local_files = {" << endl; - for (size_t i = 0; i < parallel_local_files.size(); i++) - { - size_t j = parallel_local_files[i].find_last_of("/\\"); - if (j == string::npos) - mOutputFile << "'', '" << parallel_local_files[i] << "';" << endl; - else - mOutputFile << "'" << parallel_local_files[i].substr(0, j+1) << "', '" - << parallel_local_files[i].substr(j+1, string::npos) << "';" << endl; - } - mOutputFile << "};" << endl; - } - - config_file.writeCluster(mOutputFile); - - if (byte_code) - mOutputFile << "if exist('bytecode') ~= 3" << endl - << " error('DYNARE: Can''t find bytecode DLL. Please compile it or remove the ''bytecode'' option.')" << endl - << "end" << endl; - - // Erase possible remnants of previous runs - string dynfile = basename + "_dynamic.m"; - unlink(dynfile.c_str()); - - string statfile = basename + "_static.m"; - unlink(statfile.c_str()); - - string steadystatefile = basename + "_steadystate2.m"; - unlink(steadystatefile.c_str()); - - if (!use_dll) - { - mOutputFile << "erase_compiled_function('" + basename + "_static');" << endl; - mOutputFile << "erase_compiled_function('" + basename + "_dynamic');" << endl; - } - -#if defined(_WIN32) || defined(__CYGWIN32__) - // If using USE_DLL with MSVC, check that the user didn't use a function not supported by MSVC (because MSVC doesn't comply with C99 standard) - if (use_dll && msvc) - { - if (dynamic_model.isUnaryOpUsed(oAcosh)) - { - cerr << "ERROR: acosh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; - exit(EXIT_FAILURE); - } - if (dynamic_model.isUnaryOpUsed(oAsinh)) - { - cerr << "ERROR: asinh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; - exit(EXIT_FAILURE); - } - if (dynamic_model.isUnaryOpUsed(oAtanh)) - { - cerr << "ERROR: atanh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; - exit(EXIT_FAILURE); - } - if (dynamic_model.isTrinaryOpUsed(oNormcdf)) - { - cerr << "ERROR: normcdf() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead." << endl; - exit(EXIT_FAILURE); - } - } -#endif - - // Compile the dynamic MEX file for use_dll option - if (use_dll) - { - mOutputFile << "if ~exist('OCTAVE_VERSION')" << endl; - // Some mex commands are enclosed in an eval(), because otherwise it will make Octave fail -#if defined(_WIN32) || defined(__CYGWIN32__) - if (msvc) - // MATLAB/Windows + Microsoft Visual C++ - mOutputFile << " eval('mex -O LINKFLAGS=\"$LINKFLAGS /export:Dynamic\" " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LINKFLAGS=\"$LINKFLAGS /export:Static\" " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; - else if (cygwin) - // MATLAB/Windows + Cygwin g++ - mOutputFile << " eval('mex -O PRELINK_CMDS1=\"echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Dynamic >> mex.def\" " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O PRELINK_CMDS1=\"echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Static >> mex.def\" " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; - else - mOutputFile << " error('When using the USE_DLL option, you must give either ''cygwin'' or ''msvc'' option to the ''dynare'' command')" << endl; -#else -# ifdef __linux__ - // MATLAB/Linux - mOutputFile << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; -# else // MacOS - // MATLAB/MacOS - mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " - << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " - << basename << "_static.c " << basename << "_static_mex.c')" << endl; -# endif -#endif - mOutputFile << "else" << endl // Octave - << " mex " << basename << "_dynamic.c " << basename << "_dynamic_mex.c" << endl - << " mex " << basename << "_static.c " << basename << "_static_mex.c" << endl - << "end" << endl; - } - - // Add path for block option with M-files - if (block && !byte_code) - mOutputFile << "addpath " << basename << ";" << endl; - - if (mod_file_struct.ramsey_policy_present) - mOutputFile << "M_.orig_eq_nbr = " << ramsey_policy_orig_eqn_nbr << ";" << endl; - - if (dynamic_model.equation_number() > 0) - { - dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); - if (!no_static) - static_model.writeOutput(mOutputFile, block); - } - - // Print statements - for (vector::const_iterator it = statements.begin(); - it != statements.end(); it++) - { - (*it)->writeOutput(mOutputFile, basename); - - /* Special treatment for initval block: insert initial values for the - auxiliary variables and initialize exo det */ - InitValStatement *ivs = dynamic_cast(*it); - if (ivs != NULL) - { - static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); - ivs->writeOutputPostInit(mOutputFile); - } - - // Special treatment for endval block: insert initial values for the auxiliary variables - EndValStatement *evs = dynamic_cast(*it); - if (evs != NULL) - static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); - - // Special treatment for load params and steady state statement: insert initial values for the auxiliary variables - LoadParamsAndSteadyStateStatement *lpass = dynamic_cast(*it); - if (lpass && !no_static) - static_model.writeAuxVarInitval(mOutputFile, oMatlabOutsideModel); - } - - // Remove path for block option with M-files - if (block && !byte_code) - mOutputFile << "rmpath " << basename << ";" << endl; - - mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl; - - config_file.writeEndParallel(mOutputFile); - - mOutputFile << endl << endl - << "disp(['Total computing time : ' dynsec2hms(toc) ]);" << endl; - - if (!no_warn) - { - if (warnings.countWarnings() > 0) - mOutputFile << "disp('Note: " << warnings.countWarnings() << " warning(s) encountered in the preprocessor')" << endl; - - mOutputFile << "if ~isempty(lastwarn)" << endl - << " disp('Note: warning(s) encountered in MATLAB/Octave code')" << endl - << "end" << endl; - } - - - if (!no_log) - mOutputFile << "diary off" << endl; - - mOutputFile.close(); - - // Create static and dynamic files - if (dynamic_model.equation_number() > 0) - { - if (!no_static) - { - static_model.writeStaticFile(basename, block, byte_code, use_dll); - static_model.writeParamsDerivativesFile(basename); - } - - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); - dynamic_model.writeParamsDerivativesFile(basename); - } - - // Create steady state file - steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_policy_present); - - cout << "done" << endl; -} diff --git a/ExternalFiles.hh b/ExternalFiles.hh deleted file mode 100644 index 7e34ab9d..00000000 --- a/ExternalFiles.hh +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2006-2013 Dynare Team - * - * This file is part of Dynare. - * - * Dynare is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dynare is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dynare. If not, see . - */ - -#ifndef _EXTERNAL_FILES_HH -#define _EXTERNAL_FILES_HH - -using namespace std; - -#include - -#include "ModFile.hh" - - -class ExternalFiles -{ -public: - void eraseFiles(const string &basename); - void writeHeaders(const string &basename, bool cuda); - void writeModelCC(const string &basename, bool cuda); -}; - - -#endif diff --git a/ModFile.hh b/ModFile.hh index 9301283c..6616ae26 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -36,7 +36,6 @@ using namespace std; #include "ConfigFile.hh" #include "WarningConsolidation.hh" #include "DynareOutput.hh" -#include "ExternalFiles.hh" //! The abstract representation of a "mod" file class ModFile From 9a0dddef311f38692ae78b525d5fb96bfb1ed942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 25 Feb 2014 15:53:14 +0100 Subject: [PATCH 122/162] Rename OutputType to FileOutputType (to distinguish it from ExprOutputType). Also rename DynareOutput.hh to FileOutputType.hh. Include the latter in dist tarball, and add copyright/license header. --- DynareMain.cc | 8 ++++---- DynareMain2.cc | 6 +++--- DynareOutput.hh | 12 ------------ FileOutputType.hh | 31 +++++++++++++++++++++++++++++++ Makefile.am | 3 ++- ModFile.cc | 4 ++-- ModFile.hh | 8 ++++---- 7 files changed, 46 insertions(+), 26 deletions(-) delete mode 100644 DynareOutput.hh create mode 100644 FileOutputType.hh diff --git a/DynareMain.cc b/DynareMain.cc index 633f5f97..1f99d874 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -29,7 +29,7 @@ #include "macro/MacroDriver.hh" #include -#include "DynareOutput.hh" +#include "FileOutputType.hh" /* Prototype for second part of main function Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be @@ -37,7 +37,7 @@ */ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict, OutputType output_mode, bool cuda + bool parallel_test, bool nostrict, FileOutputType output_mode, bool cuda #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif @@ -96,7 +96,7 @@ main(int argc, char **argv) bool parallel_test = false; bool nostrict = false; map defines; - OutputType output_mode = none; + FileOutputType output_mode = none; bool cuda = false; // Parse options diff --git a/DynareMain2.cc b/DynareMain2.cc index 44416b32..208f1e40 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2013 Dynare Team + * Copyright (C) 2008-2014 Dynare Team * * This file is part of Dynare. * @@ -22,12 +22,12 @@ #include "ParsingDriver.hh" #include "ModFile.hh" #include "ConfigFile.hh" -#include "DynareOutput.hh" +#include "FileOutputType.hh" void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict, OutputType output_mode, bool cuda + bool parallel_test, bool nostrict, FileOutputType output_mode, bool cuda #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif diff --git a/DynareOutput.hh b/DynareOutput.hh deleted file mode 100644 index 4e884f6b..00000000 --- a/DynareOutput.hh +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef _DYNARE_OUTPUT_HH -#define _DYNARE_OUTPUT_HH - -enum OutputType - { - none, // outputs files for Matlab/Octave processing - dynamic, // outputs _dynamic.cc and related files - first, // outputs _first_derivatives and related files - second, // outputs _first_derivatives, _second_derivatives.cc and related files - third, // outputs _first_derivatives, _second_derivatives.cc, _third_derivatives.cc and related files - }; -#endif diff --git a/FileOutputType.hh b/FileOutputType.hh new file mode 100644 index 00000000..8f4e681f --- /dev/null +++ b/FileOutputType.hh @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2014 Dynare Team + * + * This file is part of Dynare. + * + * Dynare is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dynare is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dynare. If not, see . + */ + +#ifndef _FILE_OUTPUT_TYPE_HH +#define _FILE_OUTPUT_TYPE_HH + +enum FileOutputType + { + none, // outputs files for Matlab/Octave processing + dynamic, // outputs _dynamic.cc and related files + first, // outputs _first_derivatives and related files + second, // outputs _first_derivatives, _second_derivatives.cc and related files + third, // outputs _first_derivatives, _second_derivatives.cc, _third_derivatives.cc and related files + }; +#endif diff --git a/Makefile.am b/Makefile.am index a9cecac8..91112aef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,7 +52,8 @@ dynare_m_SOURCES = \ SteadyStateModel.hh \ SteadyStateModel.cc \ WarningConsolidation.hh \ - WarningConsolidation.cc + WarningConsolidation.cc \ + FileOutputType.hh # The -I. is for dynare_m_CPPFLAGS = $(BOOST_CPPFLAGS) -I. diff --git a/ModFile.cc b/ModFile.cc index 51b85532..ff352c62 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -447,7 +447,7 @@ ModFile::transformPass(bool nostrict) } void -ModFile::computingPass(bool no_tmp_terms, OutputType output) +ModFile::computingPass(bool no_tmp_terms, FileOutputType output) { // Mod file may have no equation (for example in a standalone BVAR estimation) if (dynamic_model.equation_number() > 0) @@ -881,7 +881,7 @@ ModFile::writeModelCC(const string &basename, bool cuda) const } void -ModFile::writeExternalFiles(const string &basename, OutputType output, bool cuda) const +ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const { writeModelCC(basename, cuda); steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_policy_present, cuda); diff --git a/ModFile.hh b/ModFile.hh index 6616ae26..387fda17 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2013 Dynare Team + * Copyright (C) 2006-2014 Dynare Team * * This file is part of Dynare. * @@ -35,7 +35,7 @@ using namespace std; #include "ExternalFunctionsTable.hh" #include "ConfigFile.hh" #include "WarningConsolidation.hh" -#include "DynareOutput.hh" +#include "FileOutputType.hh" //! The abstract representation of a "mod" file class ModFile @@ -122,7 +122,7 @@ public: void transformPass(bool nostrict); //! Execute computations /*! \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */ - void computingPass(bool no_tmp_terms, OutputType output); + void computingPass(bool no_tmp_terms, FileOutputType output); //! Writes Matlab/Octave output files /*! \param basename The base name used for writing output files. Should be the name of the mod file without its extension @@ -141,7 +141,7 @@ public: //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; void writeModelCC(const string &basename, bool cuda) const; - void writeExternalFiles(const string &basename, OutputType output, bool cuda) const; + void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const; }; #endif // ! MOD_FILE_HH From b1d7f8f223a6e86764d71785230245d12e2eb049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 25 Feb 2014 16:00:38 +0100 Subject: [PATCH 123/162] Fix bug in external functions introduced in 711a4f62. --- ExprNode.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index 11d23a6d..84a0e0d1 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -4938,7 +4938,7 @@ FirstDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType const int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id); assert(first_deriv_symb_id != eExtFunSetButNoNameProvided); - const int tmpIndx = inputIndex - ARRAY_SUBSCRIPT_OFFSET(output_type); + const int tmpIndx = inputIndex - 1 + ARRAY_SUBSCRIPT_OFFSET(output_type); if (first_deriv_symb_id == symb_id) output << "TEFD_" << getIndxInTefTerms(symb_id, tef_terms) @@ -5193,8 +5193,8 @@ SecondDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType const int second_deriv_symb_id = datatree.external_functions_table.getSecondDerivSymbID(symb_id); assert(second_deriv_symb_id != eExtFunSetButNoNameProvided); - const int tmpIndex1 = inputIndex1 - ARRAY_SUBSCRIPT_OFFSET(output_type); - const int tmpIndex2 = inputIndex2 - ARRAY_SUBSCRIPT_OFFSET(output_type); + const int tmpIndex1 = inputIndex1 - 1 + ARRAY_SUBSCRIPT_OFFSET(output_type); + const int tmpIndex2 = inputIndex2 - 1 + ARRAY_SUBSCRIPT_OFFSET(output_type); int indx = getIndxInTefTerms(symb_id, tef_terms); if (second_deriv_symb_id == symb_id) From 2ab9614fadf3baa99ba643d4e54a5e164203fc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 25 Feb 2014 17:58:13 +0100 Subject: [PATCH 124/162] steady_state_model: fix bug introduced in 8fddca92 - don't emit warnings if the steady_state_model block is not used - the ramsey_policy check is spurious, there is another one deeper --- SteadyStateModel.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 9f166527..6f33c7df 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -58,7 +58,7 @@ SteadyStateModel::addMultipleDefinitions(const vector &symb_ids, expr_t exp void SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) const { - if (ramsey_policy) + if (def_table.size() == 0) return; vector so_far_defined; From c2cec94af83205850772ed066fb4662e3949e026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 28 Feb 2014 18:01:03 +0100 Subject: [PATCH 125/162] Verify types of the symbols in estimated_params{,_init,_bounds} statements. Closes #624 --- ParsingDriver.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index c105f81c..4264b2a9 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -1223,8 +1223,23 @@ ParsingDriver::add_estimated_params_element() if (estim_params.name != "dsge_prior_weight") { check_symbol_existence(estim_params.name); - if (estim_params.name2.size() > 0) - check_symbol_existence(estim_params.name2); + SymbolType type = mod_file->symbol_table.getType(estim_params.name); + switch (estim_params.type) + { + case 1: + if (type != eEndogenous && type != eExogenous) + error(estim_params.name + " must be an endogenous or an exogenous variable"); + break; + case 2: + check_symbol_is_parameter(&estim_params.name); + break; + case 3: + check_symbol_existence(estim_params.name2); + SymbolType type2 = mod_file->symbol_table.getType(estim_params.name2); + if ((type != eEndogenous && type != eExogenous) || type != type2) + error(estim_params.name + " and " + estim_params.name2 + " must either be both endogenous variables or both exogenous"); + break; + } } estim_params_list.push_back(estim_params); estim_params.init(*data_tree); From d43a2bce0956f2d4ce5b915ae9cd6c59185236f6 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 6 Mar 2014 10:06:55 +0100 Subject: [PATCH 126/162] Add option mh_conf_sig Fixes #598 --- DynareBison.yy | 5 ++++- DynareFlex.ll | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index aab4a95a..3ce406b7 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -104,7 +104,7 @@ class ParsingDriver; %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF %token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR LOGDATA LYAPUNOV %token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT -%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS +%token MFS MH_CONF_SIG MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION %token NAME @@ -1561,6 +1561,7 @@ estimation_options : o_datafile | o_nodisplay | o_graph_format | o_conf_sig + | o_mh_conf_sig | o_mh_replic | o_mh_drop | o_mh_jscale @@ -2175,6 +2176,7 @@ dynare_sensitivity_option : o_gsa_identification | o_nodisplay | o_graph_format | o_conf_sig + | o_mh_conf_sig | o_loglinear | o_mode_file | o_load_ident_files @@ -2471,6 +2473,7 @@ o_subsample_name : symbol EQUAL date_expr ':' date_expr { driver.set_subsample_name_equal_to_date_range($1, $3, $5); } ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; +o_mh_conf_sig : MH_CONF_SIG EQUAL non_negative_number { driver.option_num("mh_conf_sig", $3); }; o_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("mh_replic", $3); }; o_posterior_max_subsample_draws : POSTERIOR_MAX_SUBSAMPLE_DRAWS EQUAL INT_NUMBER { driver.option_num("posterior_max_subsample_draws", $3); }; o_mh_drop : MH_DROP EQUAL non_negative_number { driver.option_num("mh_drop", $3); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index ab9770ab..9c040444 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -262,6 +262,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 print {return token::PRINT;} noprint {return token::NOPRINT;} conf_sig {return token::CONF_SIG;} +mh_conf_sig {return token::MH_CONF_SIG;} mh_replic {return token::MH_REPLIC;} mh_drop {return token::MH_DROP;} mh_jscale {return token::MH_JSCALE;} From 46bcfea9e5b0c7ceaa583d322f31c778b47f8f4f Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Fri, 7 Mar 2014 17:18:06 +0100 Subject: [PATCH 127/162] adding ramsey_model to Dynare grammar --- DynareBison.yy | 21 ++++++++++++++++++++- DynareFlex.ll | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3ce406b7..500b8d14 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -119,7 +119,7 @@ class ParsingDriver; %token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD %token SMOOTHER SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS %token STDERR STEADY STOCH_SIMUL SURPRISE SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME -%token TEX RAMSEY_POLICY PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL +%token TEX RAMSEY_MODEL RAMSEY_POLICY PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL %token TEX_NAME %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED %token VALUES VAR VAREXO VAREXO_DET VAROBS PREDETERMINED_VARIABLES @@ -232,6 +232,7 @@ statement : parameters | model_comparison | model_info | planner_objective + | ramsey_model | ramsey_policy | discretionary_policy | bvar_density @@ -1778,6 +1779,16 @@ mc_filename_list : filename planner_objective : PLANNER_OBJECTIVE { driver.begin_planner_objective(); } hand_side { driver.end_planner_objective($3); } ';'; +ramsey_model : RAMSEY_MODEL ';' + { driver.ramsey_model(); } + | RAMSEY_MODEL '(' ramsey_model_options_list ')' ';' + { driver.ramsey_model(); } + | RAMSEY_MODEL symbol_list ';' + { driver.ramsey_model(); } + | RAMSEY_MODEL '(' ramsey_model_options_list ')' symbol_list ';' + { driver.ramsey_model(); } + ; + ramsey_policy : RAMSEY_POLICY ';' { driver.ramsey_policy(); } | RAMSEY_POLICY '(' ramsey_policy_options_list ')' ';' @@ -1807,6 +1818,14 @@ discretionary_policy_options : ramsey_policy_options | o_dp_maxit; ; +ramsey_model_options_list : ramsey_model_options_list COMMA ramsey_model_options + | ramsey_model_options + ; + +ramsey_model_options : o_planner_discount + | o_instruments + ; + ramsey_policy_options_list : ramsey_policy_options_list COMMA ramsey_policy_options | ramsey_policy_options ; diff --git a/DynareFlex.ll b/DynareFlex.ll index 9c040444..5cc35434 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -137,6 +137,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 dsample {BEGIN DYNARE_STATEMENT; return token::DSAMPLE;} Sigma_e {BEGIN DYNARE_STATEMENT; sigma_e = 1; return token::SIGMA_E;} planner_objective {BEGIN DYNARE_STATEMENT; return token::PLANNER_OBJECTIVE;} +ramsey_model {BEGIN DYNARE_STATEMENT; return token::RAMSEY_MODEL;} ramsey_policy {BEGIN DYNARE_STATEMENT; return token::RAMSEY_POLICY;} discretionary_policy {BEGIN DYNARE_STATEMENT; return token::DISCRETIONARY_POLICY;} identification {BEGIN DYNARE_STATEMENT; return token::IDENTIFICATION;} From bb67c635994a187f1242fd4ca7be09f1abae84e8 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sat, 8 Mar 2014 09:55:00 +0100 Subject: [PATCH 128/162] adding ramsey_model to ParsingDriver --- ParsingDriver.cc | 12 +++++++++++- ParsingDriver.hh | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 4264b2a9..3a46428f 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -214,7 +214,7 @@ ParsingDriver::declare_optimal_policy_discount_factor_parameter(expr_t exprnode) string *optimalParName_declare = new string("optimal_policy_discount_factor"); string *optimalParName_init = new string("optimal_policy_discount_factor"); if (mod_file->symbol_table.exists(*optimalParName_declare)) - error("Symbol optimal_policy_discount_factor is needed by Dynare when using an ramsey_policy or a discretionary_policy statement"); + error("Symbol optimal_policy_discount_factor is needed by Dynare when using a ramsey_model, a ramsey_policy or a discretionary_policy statement"); declare_parameter(optimalParName_declare, NULL); init_param(optimalParName_init, exprnode); } @@ -1775,6 +1775,16 @@ ParsingDriver::end_planner_objective(expr_t expr) reset_data_tree(); } +void +ParsingDriver::ramsey_model() +{ + if (!mod_file->symbol_table.exists("optimal_policy_discount_factor")) + declare_optimal_policy_discount_factor_parameter(data_tree->One); + mod_file->addStatement(new RamseyModelStatement(symbol_list, options_list)); + symbol_list.clear(); + options_list.clear(); +} + void ParsingDriver::ramsey_policy() { diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 853078cc..24b47ffc 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -493,6 +493,8 @@ public: void begin_planner_objective(); //! End a planner objective statement void end_planner_objective(expr_t expr); + //! Ramsey model statement + void ramsey_model(); //! Ramsey policy statement void ramsey_policy(); //! Discretionary policy statement From 6364e74a3ef4665b21c9bf0ff6908cd969e7a17d Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sat, 8 Mar 2014 10:05:25 +0100 Subject: [PATCH 129/162] adding ramsey_model to ComputingTasks --- ComputingTasks.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ ComputingTasks.hh | 12 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index d189e34f..2c48c5a4 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -160,6 +160,50 @@ ForecastStatement::writeOutput(ostream &output, const string &basename) const output << "info = dyn_forecast(var_list_,'simul');" << endl; } +RamseyModelStatement::RamseyModelStatement(const SymbolList &symbol_list_arg, + const OptionsList &options_list_arg) : + symbol_list(symbol_list_arg), + options_list(options_list_arg) +{ +} + +void +RamseyModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + mod_file_struct.ramsey_model_present = true; + + /* Fill in option_order of mod_file_struct + Since ramsey model needs one further order of derivation (for example, for 1st order + approximation, it needs 2nd derivatives), we add 1 to the order declared by user */ + OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order"); + if (it != options_list.num_options.end()) + { + int order = atoi(it->second.c_str()); + if (order > 2) + { + cerr << "ERROR: ramsey_model: order > 2 is not implemented" << endl; + exit(EXIT_FAILURE); + } + mod_file_struct.order_option = max(mod_file_struct.order_option, order + 1); + } + + // Fill in mod_file_struct.partial_information + it = options_list.num_options.find("partial_information"); + if (it != options_list.num_options.end() && it->second == "1") + mod_file_struct.partial_information = true; + + // Option k_order_solver (implicit when order >= 3) + it = options_list.num_options.find("k_order_solver"); + if ((it != options_list.num_options.end() && it->second == "1") + || mod_file_struct.order_option >= 3) + mod_file_struct.k_order_solver = true; +} + +void +RamseyModelStatement::writeOutput(ostream &output, const string &basename) const +{ +} + RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg, const OptionsList &options_list_arg) : symbol_list(symbol_list_arg), diff --git a/ComputingTasks.hh b/ComputingTasks.hh index 8882f2ce..fde8b668 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -91,6 +91,18 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class RamseyModelStatement : public Statement +{ +private: + const SymbolList symbol_list; + const OptionsList options_list; +public: + RamseyModelStatement(const SymbolList &symbol_list_arg, + const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + class RamseyPolicyStatement : public Statement { private: From 8d042b812173b831153bcc42d8706b241fd742be Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 9 Mar 2014 09:52:44 +0100 Subject: [PATCH 130/162] finishing ramsey_model implementation --- ComputingTasks.cc | 8 ++++++++ ModFile.cc | 40 ++++++++++++++++++++-------------------- ModFile.hh | 2 +- Statement.cc | 1 + Statement.hh | 2 ++ SteadyStateModel.cc | 8 ++++---- SteadyStateModel.hh | 10 +++++----- 7 files changed, 41 insertions(+), 30 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 2c48c5a4..74b541f5 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -202,6 +202,10 @@ RamseyModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsol void RamseyModelStatement::writeOutput(ostream &output, const string &basename) const { + // options_.ramsey_policy indicates that a Ramsey model is present in the *.mod file + // this affects the computation of the steady state that uses a special algorithm + // It should probably rather be a M_ field, but we leave it in options_ for historical reason + output << "options_.ramsey_policy = 1;\n"; } RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg, @@ -214,6 +218,10 @@ RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg, void RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { + // ramsey_model_present indicates that the model is augmented with the FOC of the planner problem + mod_file_struct.ramsey_model_present = true; + // ramsey_policy_present indicates that ramsey_policy instruction for computation of first order approximation + // of a stochastic Ramsey problem if present in the *.mod file mod_file_struct.ramsey_policy_present = true; /* Fill in option_order of mod_file_struct diff --git a/ModFile.cc b/ModFile.cc index ff352c62..98ee8d7d 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -39,7 +39,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg) steady_state_model(symbol_table, num_constants, external_functions_table, static_model), linear(false), block(false), byte_code(false), use_dll(false), no_static(false), differentiate_forward_vars(false), - nonstationary_variables(false), ramsey_policy_orig_eqn_nbr(0), + nonstationary_variables(false), ramsey_model_orig_eqn_nbr(0), warnings(warnings_arg) { } @@ -113,7 +113,7 @@ ModFile::checkPass() (*it)->checkPass(mod_file_struct, warnings); // Check the steady state block - steady_state_model.checkPass(mod_file_struct.ramsey_policy_present, warnings); + steady_state_model.checkPass(mod_file_struct.ramsey_model_present, warnings); // If order option has not been set, default to 2 if (!mod_file_struct.order_option) @@ -136,12 +136,12 @@ ModFile::checkPass() exit(EXIT_FAILURE); } - if (((mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) + if (((mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present) && !mod_file_struct.planner_objective_present) - || (!(mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) + || (!(mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present) && mod_file_struct.planner_objective_present)) { - cerr << "ERROR: A planner_objective statement must be used with a ramsey_policy or a discretionary_policy statement and vice versa." << endl; + cerr << "ERROR: A planner_objective statement must be used with a ramsey_model, a ramsey_policy or a discretionary_policy statement and vice versa." << endl; exit(EXIT_FAILURE); } @@ -246,9 +246,9 @@ ModFile::checkPass() } if (dynamic_model.staticOnlyEquationsNbr() > 0 && - (mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present)) + (mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present)) { - cerr << "ERROR: marking equations as [static] or [dynamic] is not possible with ramsey_policy or discretionary_policy" << endl; + cerr << "ERROR: marking equations as [static] or [dynamic] is not possible with ramsey_model, ramsey_policy or discretionary_policy" << endl; exit(EXIT_FAILURE); } @@ -330,7 +330,7 @@ ModFile::transformPass(bool nostrict) dynamic_model.removeTrendVariableFromEquations(); } - if (mod_file_struct.ramsey_policy_present) + if (mod_file_struct.ramsey_model_present) { StaticModel *planner_objective = NULL; for (vector::iterator it = statements.begin(); it != statements.end(); it++) @@ -340,7 +340,7 @@ ModFile::transformPass(bool nostrict) planner_objective = pos->getPlannerObjective(); } assert(planner_objective != NULL); - ramsey_policy_orig_eqn_nbr = dynamic_model.equation_number(); + ramsey_model_orig_eqn_nbr = dynamic_model.equation_number(); /* clone the model then clone the new equations back to the original because @@ -395,11 +395,11 @@ ModFile::transformPass(bool nostrict) /* Enforce the same number of equations and endogenous, except in three cases: - - ramsey_policy is used + - ramsey_model, ramsey_policy or discretionary_policy is used - a BVAR command is used and there is no equation (standalone BVAR estimation) - nostrict option is passed and there are more endogs than equations (dealt with before freeze) */ - if (!(mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) + if (!(mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present) && !(mod_file_struct.bvar_present && dynamic_model.equation_number() == 0) && (dynamic_model.equation_number() != symbol_table.endo_nbr())) { @@ -419,11 +419,11 @@ ModFile::transformPass(bool nostrict) exit(EXIT_FAILURE); } - if (!mod_file_struct.ramsey_policy_present) + if (!mod_file_struct.ramsey_model_present) cout << "Found " << dynamic_model.equation_number() << " equation(s)." << endl; else { - cout << "Found " << ramsey_policy_orig_eqn_nbr << " equation(s)." << endl; + cout << "Found " << ramsey_model_orig_eqn_nbr << " equation(s)." << endl; cout << "Found " << dynamic_model.equation_number() << " FOC equation(s) for Ramsey Problem." << endl; } @@ -461,7 +461,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output) { if (mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present || mod_file_struct.calib_smoother_present) static_model.set_cutoff_to_zero(); @@ -476,7 +476,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output) if (mod_file_struct.simul_present || mod_file_struct.check_present || mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present || mod_file_struct.calib_smoother_present) { if (mod_file_struct.simul_present) @@ -485,7 +485,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output) { if (mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present - || mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present + || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present || mod_file_struct.calib_smoother_present) dynamic_model.set_cutoff_to_zero(); if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3) @@ -714,8 +714,8 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b if (block && !byte_code) mOutputFile << "addpath " << basename << ";" << endl; - if (mod_file_struct.ramsey_policy_present) - mOutputFile << "M_.orig_eq_nbr = " << ramsey_policy_orig_eqn_nbr << ";" << endl; + if (mod_file_struct.ramsey_model_present) + mOutputFile << "M_.orig_eq_nbr = " << ramsey_model_orig_eqn_nbr << ";" << endl; if (dynamic_model.equation_number() > 0) { @@ -799,7 +799,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b } // Create steady state file - steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_policy_present); + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present); cout << "done" << endl; } @@ -884,7 +884,7 @@ void ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const { writeModelCC(basename, cuda); - steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_policy_present, cuda); + steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_model_present, cuda); dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); diff --git a/ModFile.hh b/ModFile.hh index 387fda17..aca8ef9d 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -93,7 +93,7 @@ public: eval_context_t global_eval_context; //! Stores the original number of equations in the model_block - int ramsey_policy_orig_eqn_nbr; + int ramsey_model_orig_eqn_nbr; //! Stores the list of extra files to be transefered during a parallel run /*! (i.e. option parallel_local_files of model block) */ diff --git a/Statement.cc b/Statement.cc index 3ce955d6..4331c4dc 100644 --- a/Statement.cc +++ b/Statement.cc @@ -29,6 +29,7 @@ ModFileStructure::ModFileStructure() : osr_present(false), osr_params_present(false), optim_weights_present(false), + ramsey_model_present(false), ramsey_policy_present(false), discretionary_policy_present(false), planner_objective_present(false), diff --git a/Statement.hh b/Statement.hh index 6abda8fc..9f4a3c04 100644 --- a/Statement.hh +++ b/Statement.hh @@ -48,6 +48,8 @@ public: bool osr_params_present; //! Whether an optim weight statement is present bool optim_weights_present; + //! Whether a ramsey_model statement is present + bool ramsey_model_present; //! Whether a ramsey_policy statement is present bool ramsey_policy_present; //! Whether a discretionary_objective statement is present diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 6f33c7df..f1069642 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -56,7 +56,7 @@ SteadyStateModel::addMultipleDefinitions(const vector &symb_ids, expr_t exp } void -SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) const +SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) const { if (def_table.size() == 0) return; @@ -74,7 +74,7 @@ SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl; // Check that expression has no undefined symbol - if (!ramsey_policy) + if (!ramsey_model) { set used_symbols; const expr_t &expr = def_table[i].second; @@ -105,7 +105,7 @@ SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) } void -SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_policy) const +SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model) const { if (def_table.size() == 0) return; @@ -153,7 +153,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_polic } void -SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const +SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const { string filename = basename + "_steadystate.cc"; diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index b5686157..817f84f5 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -41,15 +41,15 @@ public: void addMultipleDefinitions(const vector &symb_ids, expr_t expr); //! Checks that definitions are in a recursive order, and that no variable is declared twice /*! - \param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then disable the check on the recursivity of the declarations + \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then disable the check on the recursivity of the declarations */ - void checkPass(bool ramsey_policy, WarningConsolidation &warnings) const; + void checkPass(bool ramsey_model, WarningConsolidation &warnings) const; //! Write the steady state file /*! - \param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values + \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ - void writeSteadyStateFile(const string &basename, bool ramsey_policy) const; - void writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const; + void writeSteadyStateFile(const string &basename, bool ramsey_model) const; + void writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const; }; #endif From 380df90748bbeb7ecfaeb36d2b5f198dab66882d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 11 Mar 2014 15:31:08 +0100 Subject: [PATCH 131/162] Fix output of external functions (and their derivatives) in LaTeX. --- ExprNode.cc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index 84a0e0d1..7a03ee96 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -4385,9 +4385,12 @@ ExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType output_typ const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const { - if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile || output_type == oCSteadyStateFile) + if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile + || output_type == oCSteadyStateFile || IS_LATEX(output_type)) { - output << datatree.symbol_table.getName(symb_id) << "("; + string name = IS_LATEX(output_type) ? datatree.symbol_table.getTeXName(symb_id) + : datatree.symbol_table.getName(symb_id); + output << name << "("; writeExternalFunctionArguments(output, output_type, temporary_terms, tef_terms); output << ")"; return; @@ -4924,6 +4927,15 @@ FirstDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType { assert(output_type != oMatlabOutsideModel); + if (IS_LATEX(output_type)) + { + output << "\\frac{\\partial " << datatree.symbol_table.getTeXName(symb_id) + << "}{\\partial " << inputIndex << "}("; + writeExternalFunctionArguments(output, output_type, temporary_terms, tef_terms); + output << ")"; + return; + } + // If current node is a temporary term temporary_terms_t::const_iterator it = temporary_terms.find(const_cast(this)); if (it != temporary_terms.end()) @@ -5179,6 +5191,15 @@ SecondDerivExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType { assert(output_type != oMatlabOutsideModel); + if (IS_LATEX(output_type)) + { + output << "\\frac{\\partial^2 " << datatree.symbol_table.getTeXName(symb_id) + << "}{\\partial " << inputIndex1 << "\\partial " << inputIndex2 << "}("; + writeExternalFunctionArguments(output, output_type, temporary_terms, tef_terms); + output << ")"; + return; + } + // If current node is a temporary term temporary_terms_t::const_iterator it = temporary_terms.find(const_cast(this)); if (it != temporary_terms.end()) From 0612bca00fa58e90152c6363011479d387dd4ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 11 Mar 2014 17:12:46 +0100 Subject: [PATCH 132/162] Remove spurious test. --- ExprNode.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index 7a03ee96..fcf19ec5 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -4320,9 +4320,8 @@ ExternalFunctionNode::composeDerivatives(const vector &dargs) { vector dNodes; for (int i = 0; i < (int) dargs.size(); i++) - if (dargs.at(i) != 0) - dNodes.push_back(datatree.AddTimes(dargs.at(i), - datatree.AddFirstDerivExternalFunctionNode(symb_id, arguments, i+1))); + dNodes.push_back(datatree.AddTimes(dargs.at(i), + datatree.AddFirstDerivExternalFunctionNode(symb_id, arguments, i+1))); expr_t theDeriv = datatree.Zero; for (vector::const_iterator it = dNodes.begin(); it != dNodes.end(); it++) @@ -4911,9 +4910,8 @@ FirstDerivExternalFunctionNode::composeDerivatives(const vector &dargs) { vector dNodes; for (int i = 0; i < (int) dargs.size(); i++) - if (dargs.at(i) != 0) - dNodes.push_back(datatree.AddTimes(dargs.at(i), - datatree.AddSecondDerivExternalFunctionNode(symb_id, arguments, inputIndex, i+1))); + dNodes.push_back(datatree.AddTimes(dargs.at(i), + datatree.AddSecondDerivExternalFunctionNode(symb_id, arguments, inputIndex, i+1))); expr_t theDeriv = datatree.Zero; for (vector::const_iterator it = dNodes.begin(); it != dNodes.end(); it++) theDeriv = datatree.AddPlus(theDeriv, *it); From 140db861d57ee0cc6af55f579671f3bd6f4c6419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 11 Mar 2014 17:33:46 +0100 Subject: [PATCH 133/162] Implement cloneDynamic() for {First,Second}DerivExternalFunction. Since the method was not implement, the method of the parent class was used. This was leading to wrong results in the context of Ramsey Policy (basically derivative operators on external functions were dropped from FOCs). --- ExprNode.cc | 22 ++++++++++++++++++++++ ExprNode.hh | 2 ++ 2 files changed, 24 insertions(+) diff --git a/ExprNode.cc b/ExprNode.cc index fcf19ec5..5620b11a 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -5140,6 +5140,17 @@ FirstDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCo } } +expr_t +FirstDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const +{ + vector dynamic_arguments; + for (vector::const_iterator it = arguments.begin(); + it != arguments.end(); it++) + dynamic_arguments.push_back((*it)->cloneDynamic(dynamic_datatree)); + return dynamic_datatree.AddFirstDerivExternalFunctionNode(symb_id, dynamic_arguments, + inputIndex); +} + SecondDerivExternalFunctionNode::SecondDerivExternalFunctionNode(DataTree &datatree_arg, int top_level_symb_id_arg, const vector &arguments_arg, @@ -5336,3 +5347,14 @@ SecondDerivExternalFunctionNode::writeExternalFunctionOutput(ostream &output, Ex output << ");" << endl; } } + +expr_t +SecondDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const +{ + vector dynamic_arguments; + for (vector::const_iterator it = arguments.begin(); + it != arguments.end(); it++) + dynamic_arguments.push_back((*it)->cloneDynamic(dynamic_datatree)); + return dynamic_datatree.AddSecondDerivExternalFunctionNode(symb_id, dynamic_arguments, + inputIndex1, inputIndex2); +} diff --git a/ExprNode.hh b/ExprNode.hh index c77c8cbc..c0de01b8 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -874,6 +874,7 @@ public: bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const; + virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const; }; class SecondDerivExternalFunctionNode : public ExternalFunctionNode @@ -899,6 +900,7 @@ public: virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; + virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const; }; #endif From ca7fe60252adf7549a7c96dc0875f0c10ed7e79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 11 Mar 2014 17:38:47 +0100 Subject: [PATCH 134/162] Cosmetic renaming for consistency between DataTree method names. --- DataTree.cc | 6 +++--- DataTree.hh | 4 ++-- ExprNode.cc | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/DataTree.cc b/DataTree.cc index 629365fb..f95ab9ca 100644 --- a/DataTree.cc +++ b/DataTree.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -512,7 +512,7 @@ DataTree::AddExternalFunction(int symb_id, const vector &arguments) } expr_t -DataTree::AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector &arguments, int input_index) +DataTree::AddFirstDerivExternalFunction(int top_level_symb_id, const vector &arguments, int input_index) { assert(symbol_table.getType(top_level_symb_id) == eExternalFunction); @@ -526,7 +526,7 @@ DataTree::AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector< } expr_t -DataTree::AddSecondDerivExternalFunctionNode(int top_level_symb_id, const vector &arguments, int input_index1, int input_index2) +DataTree::AddSecondDerivExternalFunction(int top_level_symb_id, const vector &arguments, int input_index1, int input_index2) { assert(symbol_table.getType(top_level_symb_id) == eExternalFunction); diff --git a/DataTree.hh b/DataTree.hh index 9ef440f8..859556c5 100644 --- a/DataTree.hh +++ b/DataTree.hh @@ -208,9 +208,9 @@ public: //! Adds an external function node expr_t AddExternalFunction(int symb_id, const vector &arguments); //! Adds an external function node for the first derivative of an external function - expr_t AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector &arguments, int input_index); + expr_t AddFirstDerivExternalFunction(int top_level_symb_id, const vector &arguments, int input_index); //! Adds an external function node for the second derivative of an external function - expr_t AddSecondDerivExternalFunctionNode(int top_level_symb_id, const vector &arguments, int input_index1, int input_index2); + expr_t AddSecondDerivExternalFunction(int top_level_symb_id, const vector &arguments, int input_index1, int input_index2); //! Checks if a given symbol is used somewhere in the data tree bool isSymbolUsed(int symb_id) const; //! Checks if a given unary op is used somewhere in the data tree diff --git a/ExprNode.cc b/ExprNode.cc index 5620b11a..d6df8496 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -4321,7 +4321,7 @@ ExternalFunctionNode::composeDerivatives(const vector &dargs) vector dNodes; for (int i = 0; i < (int) dargs.size(); i++) dNodes.push_back(datatree.AddTimes(dargs.at(i), - datatree.AddFirstDerivExternalFunctionNode(symb_id, arguments, i+1))); + datatree.AddFirstDerivExternalFunction(symb_id, arguments, i+1))); expr_t theDeriv = datatree.Zero; for (vector::const_iterator it = dNodes.begin(); it != dNodes.end(); it++) @@ -4911,7 +4911,7 @@ FirstDerivExternalFunctionNode::composeDerivatives(const vector &dargs) vector dNodes; for (int i = 0; i < (int) dargs.size(); i++) dNodes.push_back(datatree.AddTimes(dargs.at(i), - datatree.AddSecondDerivExternalFunctionNode(symb_id, arguments, inputIndex, i+1))); + datatree.AddSecondDerivExternalFunction(symb_id, arguments, inputIndex, i+1))); expr_t theDeriv = datatree.Zero; for (vector::const_iterator it = dNodes.begin(); it != dNodes.end(); it++) theDeriv = datatree.AddPlus(theDeriv, *it); @@ -5147,8 +5147,8 @@ FirstDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) dynamic_arguments.push_back((*it)->cloneDynamic(dynamic_datatree)); - return dynamic_datatree.AddFirstDerivExternalFunctionNode(symb_id, dynamic_arguments, - inputIndex); + return dynamic_datatree.AddFirstDerivExternalFunction(symb_id, dynamic_arguments, + inputIndex); } SecondDerivExternalFunctionNode::SecondDerivExternalFunctionNode(DataTree &datatree_arg, @@ -5355,6 +5355,6 @@ SecondDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) dynamic_arguments.push_back((*it)->cloneDynamic(dynamic_datatree)); - return dynamic_datatree.AddSecondDerivExternalFunctionNode(symb_id, dynamic_arguments, - inputIndex1, inputIndex2); + return dynamic_datatree.AddSecondDerivExternalFunction(symb_id, dynamic_arguments, + inputIndex1, inputIndex2); } From 8882f809c3563e8f106f3373cad3403a0ba27017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 13 Mar 2014 11:22:00 +0100 Subject: [PATCH 135/162] Introduce a new abstract class for external function nodes. This fixes the following bugs: - 1st and 2nd ext fcn deriv nodes were incorrectly added to DataTree::external_function_node_map, because of the chaining of constructors. - the following methods of FirstDerivExternalFunctionNode were not overloaded: toStatic(), buildSimilarExternalFunctionNode() - the following methods of SecondDerivExternalFunctionNode were not overloaded: toStatic(), buildSimilarExternalFunctionNode(), compile(), compileExternalFunctionOutput() --- DataTree.hh | 1 + DynamicModel.cc | 4 +- ExprNode.cc | 154 +++++++++++++++++++++++++++++++++--------------- ExprNode.hh | 118 +++++++++++++++++++++++++------------ ModelTree.cc | 4 +- StaticModel.cc | 8 +-- 6 files changed, 196 insertions(+), 93 deletions(-) diff --git a/DataTree.hh b/DataTree.hh index 859556c5..19712e9b 100644 --- a/DataTree.hh +++ b/DataTree.hh @@ -44,6 +44,7 @@ class DataTree friend class UnaryOpNode; friend class BinaryOpNode; friend class TrinaryOpNode; + friend class AbstractExternalFunctionNode; friend class ExternalFunctionNode; friend class FirstDerivExternalFunctionNode; friend class SecondDerivExternalFunctionNode; diff --git a/DynamicModel.cc b/DynamicModel.cc index 1a5de8b9..bd2adaed 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -460,7 +460,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const for (temporary_terms_t::const_iterator it = v_temporary_terms[block][i].begin(); it != v_temporary_terms[block][i].end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->writeExternalFunctionOutput(output, local_output_type, tt2, tef_terms); output << " " << sps; @@ -1181,7 +1181,7 @@ DynamicModel::writeModelEquationsCode_Block(string &file_name, const string &bin for (temporary_terms_t::const_iterator it = v_temporary_terms[block][i].begin(); it != v_temporary_terms[block][i].end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->compileExternalFunctionOutput(code_file, instruction_number, false, tt2, map_idx, true, false, tef_terms); FNUMEXPR_ fnumexpr(TemporaryTerm, (int) (map_idx.find((*it)->idx)->second)); diff --git a/ExprNode.cc b/ExprNode.cc index d6df8496..f435107a 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -4274,19 +4274,26 @@ TrinaryOpNode::isInStaticForm() const return arg1->isInStaticForm() && arg2->isInStaticForm() && arg3->isInStaticForm(); } -ExternalFunctionNode::ExternalFunctionNode(DataTree &datatree_arg, - int symb_id_arg, - const vector &arguments_arg) : +AbstractExternalFunctionNode::AbstractExternalFunctionNode(DataTree &datatree_arg, + int symb_id_arg, + const vector &arguments_arg) : ExprNode(datatree_arg), symb_id(symb_id_arg), arguments(arguments_arg) +{ +} + +ExternalFunctionNode::ExternalFunctionNode(DataTree &datatree_arg, + int symb_id_arg, + const vector &arguments_arg) : + AbstractExternalFunctionNode(datatree_arg, symb_id_arg, arguments_arg) { // Add myself to the external function map datatree.external_function_node_map[make_pair(arguments, symb_id)] = this; } void -ExternalFunctionNode::prepareForDerivation() +AbstractExternalFunctionNode::prepareForDerivation() { if (preparedForDerivation) return; @@ -4306,7 +4313,7 @@ ExternalFunctionNode::prepareForDerivation() } expr_t -ExternalFunctionNode::computeDerivative(int deriv_id) +AbstractExternalFunctionNode::computeDerivative(int deriv_id) { assert(datatree.external_functions_table.getNargs(symb_id) > 0); vector dargs; @@ -4330,7 +4337,7 @@ ExternalFunctionNode::composeDerivatives(const vector &dargs) } expr_t -ExternalFunctionNode::getChainRuleDerivative(int deriv_id, const map &recursive_variables) +AbstractExternalFunctionNode::getChainRuleDerivative(int deriv_id, const map &recursive_variables) { assert(datatree.external_functions_table.getNargs(symb_id) > 0); vector dargs; @@ -4349,9 +4356,9 @@ ExternalFunctionNode::computeTemporaryTerms(map &reference_count, } void -ExternalFunctionNode::writeExternalFunctionArguments(ostream &output, ExprNodeOutputType output_type, - const temporary_terms_t &temporary_terms, - deriv_node_temp_terms_t &tef_terms) const +AbstractExternalFunctionNode::writeExternalFunctionArguments(ostream &output, ExprNodeOutputType output_type, + const temporary_terms_t &temporary_terms, + deriv_node_temp_terms_t &tef_terms) const { for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4364,9 +4371,9 @@ ExternalFunctionNode::writeExternalFunctionArguments(ostream &output, ExprNodeOu } void -ExternalFunctionNode::writePrhs(ostream &output, ExprNodeOutputType output_type, - const temporary_terms_t &temporary_terms, - deriv_node_temp_terms_t &tef_terms, const string &ending) const +AbstractExternalFunctionNode::writePrhs(ostream &output, ExprNodeOutputType output_type, + const temporary_terms_t &temporary_terms, + deriv_node_temp_terms_t &tef_terms, const string &ending) const { output << "mxArray *prhs"<< ending << "[nrhs"<< ending << "];" << endl; int i = 0; @@ -4411,10 +4418,10 @@ ExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType output_typ } unsigned int -ExternalFunctionNode::compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, - bool lhs_rhs, const temporary_terms_t &temporary_terms, - const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, - deriv_node_temp_terms_t &tef_terms) const +AbstractExternalFunctionNode::compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const { for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4602,7 +4609,7 @@ ExternalFunctionNode::computeTemporaryTerms(map &reference_count, } void -ExternalFunctionNode::collectDynamicVariables(SymbolType type_arg, set > &result) const +AbstractExternalFunctionNode::collectDynamicVariables(SymbolType type_arg, set > &result) const { for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4610,9 +4617,9 @@ ExternalFunctionNode::collectDynamicVariables(SymbolType type_arg, set(this)); + temporary_terms_t::const_iterator it = temporary_terms.find(const_cast(this)); if (it != temporary_terms.end()) temporary_terms_inuse.insert(idx); else @@ -4624,13 +4631,13 @@ ExternalFunctionNode::collectTemporary_terms(const temporary_terms_t &temporary_ } double -ExternalFunctionNode::eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException) +AbstractExternalFunctionNode::eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException) { throw EvalExternalFunctionException(); } pair -ExternalFunctionNode::normalizeEquation(int var_endo, vector > > &List_of_Op_RHS) const +AbstractExternalFunctionNode::normalizeEquation(int var_endo, vector > > &List_of_Op_RHS) const { vector > V_arguments; vector V_expr_t; @@ -4669,7 +4676,7 @@ ExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const } int -ExternalFunctionNode::maxEndoLead() const +AbstractExternalFunctionNode::maxEndoLead() const { int val = 0; for (vector::const_iterator it = arguments.begin(); @@ -4679,7 +4686,7 @@ ExternalFunctionNode::maxEndoLead() const } int -ExternalFunctionNode::maxExoLead() const +AbstractExternalFunctionNode::maxExoLead() const { int val = 0; for (vector::const_iterator it = arguments.begin(); @@ -4689,7 +4696,7 @@ ExternalFunctionNode::maxExoLead() const } int -ExternalFunctionNode::maxEndoLag() const +AbstractExternalFunctionNode::maxEndoLag() const { int val = 0; for (vector::const_iterator it = arguments.begin(); @@ -4699,7 +4706,7 @@ ExternalFunctionNode::maxEndoLag() const } int -ExternalFunctionNode::maxExoLag() const +AbstractExternalFunctionNode::maxExoLag() const { int val = 0; for (vector::const_iterator it = arguments.begin(); @@ -4709,7 +4716,7 @@ ExternalFunctionNode::maxExoLag() const } int -ExternalFunctionNode::maxLead() const +AbstractExternalFunctionNode::maxLead() const { int val = 0; for (vector::const_iterator it = arguments.begin(); @@ -4719,7 +4726,7 @@ ExternalFunctionNode::maxLead() const } expr_t -ExternalFunctionNode::decreaseLeadsLags(int n) const +AbstractExternalFunctionNode::decreaseLeadsLags(int n) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4728,7 +4735,7 @@ ExternalFunctionNode::decreaseLeadsLags(int n) const } expr_t -ExternalFunctionNode::decreaseLeadsLagsPredeterminedVariables() const +AbstractExternalFunctionNode::decreaseLeadsLagsPredeterminedVariables() const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4737,7 +4744,7 @@ ExternalFunctionNode::decreaseLeadsLagsPredeterminedVariables() const } expr_t -ExternalFunctionNode::substituteEndoLeadGreaterThanTwo(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const +AbstractExternalFunctionNode::substituteEndoLeadGreaterThanTwo(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4746,7 +4753,7 @@ ExternalFunctionNode::substituteEndoLeadGreaterThanTwo(subst_table_t &subst_tabl } expr_t -ExternalFunctionNode::substituteEndoLagGreaterThanTwo(subst_table_t &subst_table, vector &neweqs) const +AbstractExternalFunctionNode::substituteEndoLagGreaterThanTwo(subst_table_t &subst_table, vector &neweqs) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4755,7 +4762,7 @@ ExternalFunctionNode::substituteEndoLagGreaterThanTwo(subst_table_t &subst_table } expr_t -ExternalFunctionNode::substituteExoLead(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const +AbstractExternalFunctionNode::substituteExoLead(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4764,7 +4771,7 @@ ExternalFunctionNode::substituteExoLead(subst_table_t &subst_table, vector &neweqs) const +AbstractExternalFunctionNode::substituteExoLag(subst_table_t &subst_table, vector &neweqs) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4773,7 +4780,7 @@ ExternalFunctionNode::substituteExoLag(subst_table_t &subst_table, vector &neweqs, bool partial_information_model) const +AbstractExternalFunctionNode::substituteExpectation(subst_table_t &subst_table, vector &neweqs, bool partial_information_model) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4782,7 +4789,7 @@ ExternalFunctionNode::substituteExpectation(subst_table_t &subst_table, vector &subset, subst_table_t &subst_table, vector &neweqs) const +AbstractExternalFunctionNode::differentiateForwardVars(const vector &subset, subst_table_t &subst_table, vector &neweqs) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4797,7 +4804,7 @@ ExternalFunctionNode::buildSimilarExternalFunctionNode(vector &alt_args, } bool -ExternalFunctionNode::alreadyWrittenAsTefTerm(int the_symb_id, deriv_node_temp_terms_t &tef_terms) const +AbstractExternalFunctionNode::alreadyWrittenAsTefTerm(int the_symb_id, deriv_node_temp_terms_t &tef_terms) const { deriv_node_temp_terms_t::const_iterator it = tef_terms.find(make_pair(the_symb_id, arguments)); if (it != tef_terms.end()) @@ -4806,7 +4813,7 @@ ExternalFunctionNode::alreadyWrittenAsTefTerm(int the_symb_id, deriv_node_temp_t } int -ExternalFunctionNode::getIndxInTefTerms(int the_symb_id, deriv_node_temp_terms_t &tef_terms) const throw (UnknownFunctionNameAndArgs) +AbstractExternalFunctionNode::getIndxInTefTerms(int the_symb_id, deriv_node_temp_terms_t &tef_terms) const throw (UnknownFunctionNameAndArgs) { deriv_node_temp_terms_t::const_iterator it = tef_terms.find(make_pair(the_symb_id, arguments)); if (it != tef_terms.end()) @@ -4815,19 +4822,19 @@ ExternalFunctionNode::getIndxInTefTerms(int the_symb_id, deriv_node_temp_terms_t } bool -ExternalFunctionNode::isNumConstNodeEqualTo(double value) const +AbstractExternalFunctionNode::isNumConstNodeEqualTo(double value) const { return false; } bool -ExternalFunctionNode::isVariableNodeEqualTo(SymbolType type_arg, int variable_id, int lag_arg) const +AbstractExternalFunctionNode::isVariableNodeEqualTo(SymbolType type_arg, int variable_id, int lag_arg) const { return false; } bool -ExternalFunctionNode::containsEndogenous(void) const +AbstractExternalFunctionNode::containsEndogenous(void) const { bool result = false; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4836,7 +4843,7 @@ ExternalFunctionNode::containsEndogenous(void) const } expr_t -ExternalFunctionNode::replaceTrendVar() const +AbstractExternalFunctionNode::replaceTrendVar() const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4845,7 +4852,7 @@ ExternalFunctionNode::replaceTrendVar() const } expr_t -ExternalFunctionNode::detrend(int symb_id, bool log_trend, expr_t trend) const +AbstractExternalFunctionNode::detrend(int symb_id, bool log_trend, expr_t trend) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4854,7 +4861,7 @@ ExternalFunctionNode::detrend(int symb_id, bool log_trend, expr_t trend) const } expr_t -ExternalFunctionNode::removeTrendLeadLag(map trend_symbols_map) const +AbstractExternalFunctionNode::removeTrendLeadLag(map trend_symbols_map) const { vector arguments_subst; for (vector::const_iterator it = arguments.begin(); it != arguments.end(); it++) @@ -4863,7 +4870,7 @@ ExternalFunctionNode::removeTrendLeadLag(map trend_symbols_map) con } bool -ExternalFunctionNode::isInStaticForm() const +AbstractExternalFunctionNode::isInStaticForm() const { for (vector::const_iterator it = arguments.begin(); it != arguments.end(); ++it) if (!(*it)->isInStaticForm()) @@ -4876,7 +4883,7 @@ FirstDerivExternalFunctionNode::FirstDerivExternalFunctionNode(DataTree &datatre int top_level_symb_id_arg, const vector &arguments_arg, int inputIndex_arg) : - ExternalFunctionNode(datatree_arg, top_level_symb_id_arg, arguments_arg), + AbstractExternalFunctionNode(datatree_arg, top_level_symb_id_arg, arguments_arg), inputIndex(inputIndex_arg) { // Add myself to the first derivative external function map @@ -5151,12 +5158,29 @@ FirstDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const inputIndex); } +expr_t +FirstDerivExternalFunctionNode::buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const +{ + return alt_datatree.AddFirstDerivExternalFunction(symb_id, alt_args, inputIndex); +} + +expr_t +FirstDerivExternalFunctionNode::toStatic(DataTree &static_datatree) const +{ + vector static_arguments; + for (vector::const_iterator it = arguments.begin(); + it != arguments.end(); it++) + static_arguments.push_back((*it)->toStatic(static_datatree)); + return static_datatree.AddFirstDerivExternalFunction(symb_id, static_arguments, + inputIndex); +} + SecondDerivExternalFunctionNode::SecondDerivExternalFunctionNode(DataTree &datatree_arg, int top_level_symb_id_arg, const vector &arguments_arg, int inputIndex1_arg, int inputIndex2_arg) : - ExternalFunctionNode(datatree_arg, top_level_symb_id_arg, arguments_arg), + AbstractExternalFunctionNode(datatree_arg, top_level_symb_id_arg, arguments_arg), inputIndex1(inputIndex1_arg), inputIndex2(inputIndex2_arg) { @@ -5187,7 +5211,8 @@ SecondDerivExternalFunctionNode::computeTemporaryTerms(map &referen } expr_t -SecondDerivExternalFunctionNode::computeDerivative(int deriv_id) +SecondDerivExternalFunctionNode::composeDerivatives(const vector &dargs) + { cerr << "ERROR: third order derivatives of external functions are not implemented" << endl; exit(EXIT_FAILURE); @@ -5358,3 +5383,40 @@ SecondDerivExternalFunctionNode::cloneDynamic(DataTree &dynamic_datatree) const return dynamic_datatree.AddSecondDerivExternalFunction(symb_id, dynamic_arguments, inputIndex1, inputIndex2); } + +expr_t +SecondDerivExternalFunctionNode::buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const +{ + return alt_datatree.AddSecondDerivExternalFunction(symb_id, alt_args, inputIndex1, inputIndex2); +} + +expr_t +SecondDerivExternalFunctionNode::toStatic(DataTree &static_datatree) const +{ + vector static_arguments; + for (vector::const_iterator it = arguments.begin(); + it != arguments.end(); it++) + static_arguments.push_back((*it)->toStatic(static_datatree)); + return static_datatree.AddSecondDerivExternalFunction(symb_id, static_arguments, + inputIndex1, inputIndex2); +} + +void +SecondDerivExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const +{ + cerr << "SecondDerivExternalFunctionNode::compile: not implemented." << endl; + exit(EXIT_FAILURE); +} + +void +SecondDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const +{ + cerr << "SecondDerivExternalFunctionNode::compileExternalFunctionOutput: not implemented." << endl; + exit(EXIT_FAILURE); +} diff --git a/ExprNode.hh b/ExprNode.hh index c0de01b8..b008333c 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2013 Dynare Team + * Copyright (C) 2007-2014 Dynare Team * * This file is part of Dynare. * @@ -125,7 +125,7 @@ class ExprNode friend class UnaryOpNode; friend class BinaryOpNode; friend class TrinaryOpNode; - friend class ExternalFunctionNode; + friend class AbstractExternalFunctionNode; private: //! Computes derivative w.r. to a derivation ID (but doesn't store it in derivatives map) /*! You shoud use getDerivative() to get the benefit of symbolic a priori and of caching */ @@ -771,11 +771,11 @@ public: }; //! External function node -class ExternalFunctionNode : public ExprNode +class AbstractExternalFunctionNode : public ExprNode { private: virtual expr_t computeDerivative(int deriv_id); - virtual expr_t composeDerivatives(const vector &dargs); + virtual expr_t composeDerivatives(const vector &dargs) = 0; protected: //! Thrown when trying to access an unknown entry in external_function_node_map class UnknownFunctionNameAndArgs @@ -789,10 +789,69 @@ protected: int getIndxInTefTerms(int the_symb_id, deriv_node_temp_terms_t &tef_terms) const throw (UnknownFunctionNameAndArgs); //! Helper function to write output arguments of any given external function void writeExternalFunctionArguments(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; +public: + AbstractExternalFunctionNode(DataTree &datatree_arg, int symb_id_arg, + const vector &arguments_arg); + virtual void prepareForDerivation(); + virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const = 0; + virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0; + virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, + const temporary_terms_t &temporary_terms, + deriv_node_temp_terms_t &tef_terms) const = 0; + virtual void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const = 0; + virtual void computeTemporaryTerms(map &reference_count, + temporary_terms_t &temporary_terms, + map > &first_occurence, + int Curr_block, + vector< vector > &v_temporary_terms, + int equation) const = 0; + virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; + virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; + virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); + unsigned int compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const; + + virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const = 0; + virtual expr_t toStatic(DataTree &static_datatree) const = 0; + virtual pair normalizeEquation(int symb_id_endo, vector > > &List_of_Op_RHS) const; + virtual expr_t getChainRuleDerivative(int deriv_id, const map &recursive_variables); + virtual int maxEndoLead() const; + virtual int maxExoLead() const; + virtual int maxEndoLag() const; + virtual int maxExoLag() const; + virtual int maxLead() const; + virtual expr_t decreaseLeadsLags(int n) const; + virtual expr_t substituteEndoLeadGreaterThanTwo(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const; + virtual expr_t substituteEndoLagGreaterThanTwo(subst_table_t &subst_table, vector &neweqs) const; + virtual expr_t substituteExoLead(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const; + virtual expr_t substituteExoLag(subst_table_t &subst_table, vector &neweqs) const; + virtual expr_t substituteExpectation(subst_table_t &subst_table, vector &neweqs, bool partial_information_model) const; + virtual expr_t buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const = 0; + virtual expr_t decreaseLeadsLagsPredeterminedVariables() const; + virtual expr_t differentiateForwardVars(const vector &subset, subst_table_t &subst_table, vector &neweqs) const; + virtual bool isNumConstNodeEqualTo(double value) const; + virtual bool containsEndogenous(void) const; + virtual bool isVariableNodeEqualTo(SymbolType type_arg, int variable_id, int lag_arg) const; + virtual void writePrhs(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms, const string &ending) const; + virtual expr_t replaceTrendVar() const; + virtual expr_t detrend(int symb_id, bool log_trend, expr_t trend) const; + virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const = 0; + virtual expr_t removeTrendLeadLag(map trend_symbols_map) const; + virtual bool isInStaticForm() const; +}; + +class ExternalFunctionNode : public AbstractExternalFunctionNode +{ +private: + virtual expr_t composeDerivatives(const vector &dargs); public: ExternalFunctionNode(DataTree &datatree_arg, int symb_id_arg, const vector &arguments_arg); - virtual void prepareForDerivation(); virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, @@ -808,44 +867,13 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void collectDynamicVariables(SymbolType type_arg, set > &result) const; - virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; - virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); - unsigned int compileExternalFunctionArguments(ostream &CompileCode, unsigned int &instruction_number, - bool lhs_rhs, const temporary_terms_t &temporary_terms, - const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, - deriv_node_temp_terms_t &tef_terms) const; - virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const; virtual expr_t toStatic(DataTree &static_datatree) const; - virtual pair normalizeEquation(int symb_id_endo, vector > > &List_of_Op_RHS) const; - virtual expr_t getChainRuleDerivative(int deriv_id, const map &recursive_variables); - virtual int maxEndoLead() const; - virtual int maxExoLead() const; - virtual int maxEndoLag() const; - virtual int maxExoLag() const; - virtual int maxLead() const; - virtual expr_t decreaseLeadsLags(int n) const; - virtual expr_t substituteEndoLeadGreaterThanTwo(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const; - virtual expr_t substituteEndoLagGreaterThanTwo(subst_table_t &subst_table, vector &neweqs) const; - virtual expr_t substituteExoLead(subst_table_t &subst_table, vector &neweqs, bool deterministic_model) const; - virtual expr_t substituteExoLag(subst_table_t &subst_table, vector &neweqs) const; - virtual expr_t substituteExpectation(subst_table_t &subst_table, vector &neweqs, bool partial_information_model) const; virtual expr_t buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const; - virtual expr_t decreaseLeadsLagsPredeterminedVariables() const; - virtual expr_t differentiateForwardVars(const vector &subset, subst_table_t &subst_table, vector &neweqs) const; - virtual bool isNumConstNodeEqualTo(double value) const; - virtual bool containsEndogenous(void) const; - virtual bool isVariableNodeEqualTo(SymbolType type_arg, int variable_id, int lag_arg) const; - virtual void writePrhs(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms, const string &ending) const; - virtual expr_t replaceTrendVar() const; - virtual expr_t detrend(int symb_id, bool log_trend, expr_t trend) const; virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const; - virtual expr_t removeTrendLeadLag(map trend_symbols_map) const; - virtual bool isInStaticForm() const; }; -class FirstDerivExternalFunctionNode : public ExternalFunctionNode +class FirstDerivExternalFunctionNode : public AbstractExternalFunctionNode { private: const int inputIndex; @@ -874,15 +902,17 @@ public: bool lhs_rhs, const temporary_terms_t &temporary_terms, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, deriv_node_temp_terms_t &tef_terms) const; + virtual expr_t toStatic(DataTree &static_datatree) const; + virtual expr_t buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const; virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const; }; -class SecondDerivExternalFunctionNode : public ExternalFunctionNode +class SecondDerivExternalFunctionNode : public AbstractExternalFunctionNode { private: const int inputIndex1; const int inputIndex2; - virtual expr_t computeDerivative(int deriv_id); + virtual expr_t composeDerivatives(const vector &dargs); public: SecondDerivExternalFunctionNode(DataTree &datatree_arg, int top_level_symb_id_arg, @@ -897,9 +927,19 @@ public: vector< vector > &v_temporary_terms, int equation) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; + virtual void compile(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; + virtual void compileExternalFunctionOutput(ostream &CompileCode, unsigned int &instruction_number, + bool lhs_rhs, const temporary_terms_t &temporary_terms, + const map_idx_t &map_idx, bool dynamic, bool steady_dynamic, + deriv_node_temp_terms_t &tef_terms) const; + virtual expr_t toStatic(DataTree &static_datatree) const; + virtual expr_t buildSimilarExternalFunctionNode(vector &alt_args, DataTree &alt_datatree) const; virtual expr_t cloneDynamic(DataTree &dynamic_datatree) const; }; diff --git a/ModelTree.cc b/ModelTree.cc index 5daf09df..a83ea06f 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -1113,7 +1113,7 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, ostream &output, for (temporary_terms_t::const_iterator it = tt.begin(); it != tt.end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->writeExternalFunctionOutput(output, output_type, tt2, tef_terms); if (IS_C(output_type)) @@ -1144,7 +1144,7 @@ ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_n for (temporary_terms_t::const_iterator it = tt.begin(); it != tt.end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) { (*it)->compileExternalFunctionOutput(code_file, instruction_number, false, tt2, map_idx, dynamic, steady_dynamic, tef_terms); } diff --git a/StaticModel.cc b/StaticModel.cc index d82efaed..ab11ca9a 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -286,7 +286,7 @@ StaticModel::writeModelEquationsOrdered_M(const string &static_basename) const for (temporary_terms_t::const_iterator it = v_temporary_terms[block][i].begin(); it != v_temporary_terms[block][i].end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->writeExternalFunctionOutput(output, local_output_type, tt2, tef_terms); output << " " << sps; @@ -658,7 +658,7 @@ StaticModel::writeModelEquationsCode_Block(const string file_name, const string for (temporary_terms_t::const_iterator it = v_temporary_terms[block][i].begin(); it != v_temporary_terms[block][i].end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->compileExternalFunctionOutput(code_file, instruction_number, false, tt2, map_idx, false, false, tef_terms); FNUMEXPR_ fnumexpr(TemporaryTerm, (int) (map_idx.find((*it)->idx)->second)); @@ -851,7 +851,7 @@ StaticModel::writeModelEquationsCode_Block(const string file_name, const string for (temporary_terms_t::const_iterator it = v_temporary_terms_local[block][i].begin(); it != v_temporary_terms_local[block][i].end(); it++) { - if (dynamic_cast(*it) != NULL) + if (dynamic_cast(*it) != NULL) (*it)->compileExternalFunctionOutput(code_file, instruction_number, false, tt3, map_idx2[block], false, false, tef_terms2); FNUMEXPR_ fnumexpr(TemporaryTerm, (int) (map_idx2[block].find((*it)->idx)->second)); From bd2405e2926a17057fd706ff2dd174ab95aa9e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 13 Mar 2014 12:26:03 +0100 Subject: [PATCH 136/162] Fix crash when the model contains 1st or 2nd ext fcn deriv nodes. In the case where the external function computes itself its derivatives, the TEF terms were not always created before being used, hence leading to a preprocessor crash. This problem could only happen with ramsey_policy, because otherwise the model does not contain derivatives of external functions. --- ExprNode.cc | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index f435107a..d7f7bd4d 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -5018,7 +5018,17 @@ FirstDerivExternalFunctionNode::writeExternalFunctionOutput(ostream &output, Exp int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id); assert(first_deriv_symb_id != eExtFunSetButNoNameProvided); - if (first_deriv_symb_id == symb_id || alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms)) + /* For a node with derivs provided by the user function, call the method + on the non-derived node */ + if (first_deriv_symb_id == symb_id) + { + expr_t parent = datatree.AddExternalFunction(symb_id, arguments); + parent->writeExternalFunctionOutput(output, output_type, temporary_terms, + tef_terms); + return; + } + + if (alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms)) return; if (IS_C(output_type)) @@ -5285,8 +5295,17 @@ SecondDerivExternalFunctionNode::writeExternalFunctionOutput(ostream &output, Ex int second_deriv_symb_id = datatree.external_functions_table.getSecondDerivSymbID(symb_id); assert(second_deriv_symb_id != eExtFunSetButNoNameProvided); - if (alreadyWrittenAsTefTerm(second_deriv_symb_id, tef_terms) - || second_deriv_symb_id == symb_id) + /* For a node with derivs provided by the user function, call the method + on the non-derived node */ + if (second_deriv_symb_id == symb_id) + { + expr_t parent = datatree.AddExternalFunction(symb_id, arguments); + parent->writeExternalFunctionOutput(output, output_type, temporary_terms, + tef_terms); + return; + } + + if (alreadyWrittenAsTefTerm(second_deriv_symb_id, tef_terms)) return; if (IS_C(output_type)) From 892b565fc8eebd0ffe71169ddb58af32c4085a49 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 14 Mar 2014 10:21:42 +0100 Subject: [PATCH 137/162] Fix comments in header of dynamic and static files Columns and rows were confused --- DynamicModel.cc | 12 ++++++------ StaticModel.cc | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 1a5de8b9..a1b8c31c 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -1525,14 +1525,14 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const << "% residual [M_.endo_nbr by 1] double vector of residuals of the dynamic model equations in order of " << endl << "% declaration of the equations" << endl << "% g1 [M_.endo_nbr by #dynamic variables] double Jacobian matrix of the dynamic model equations;" << endl - << "% columns: equations in order of declaration" << endl - << "% rows: variables in order stored in M_.lead_lag_incidence" << endl + << "% rows: equations in order of declaration" << endl + << "% columns: variables in order stored in M_.lead_lag_incidence" << endl << "% g2 [M_.endo_nbr by (#dynamic variables)^2] double Hessian matrix of the dynamic model equations;" << endl - << "% columns: equations in order of declaration" << endl - << "% rows: variables in order stored in M_.lead_lag_incidence" << endl + << "% rows: equations in order of declaration" << endl + << "% columns: variables in order stored in M_.lead_lag_incidence" << endl << "% g3 [M_.endo_nbr by (#dynamic variables)^3] double Third order derivative matrix of the dynamic model equations;" << endl - << "% columns: equations in order of declaration" << endl - << "% rows: variables in order stored in M_.lead_lag_incidence" << endl + << "% rows: equations in order of declaration" << endl + << "% columns: variables in order stored in M_.lead_lag_incidence" << endl << "%" << endl << "%" << endl << "% Warning : this file is generated automatically by Dynare" << endl diff --git a/StaticModel.cc b/StaticModel.cc index d82efaed..ba1f695d 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -1161,11 +1161,11 @@ StaticModel::writeStaticMFile(const string &func_name) const << "% residual [M_.endo_nbr by 1] double vector of residuals of the static model equations " << endl << "% in order of declaration of the equations" << endl << "% g1 [M_.endo_nbr by M_.endo_nbr] double Jacobian matrix of the static model equations;" << endl - << "% columns: equations in order of declaration" << endl - << "% rows: variables in declaration order" << endl + << "% columns: variables in declaration order" << endl + << "% rows: equations in order of declaration" << endl << "% g2 [M_.endo_nbr by (M_.endo_nbr)^2] double Hessian matrix of the static model equations;" << endl - << "% columns: equations in order of declaration" << endl - << "% rows: variables in declaration order" << endl + << "% columns: variables in declaration order" << endl + << "% rows: equations in order of declaration" << endl << "% g3 [M_.endo_nbr by (M_.endo_nbr)^3] double Third derivatives matrix of the static model equations;" << endl << "% columns: variables in declaration order" << endl << "% rows: equations in order of declaration" << endl From 42487be43c035c35b36ea74f4889e3af7b2a60c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 17 Mar 2014 16:45:43 +0100 Subject: [PATCH 138/162] Change fields in options_ for {irf,moment}_calibration. Ref #267 --- Shocks.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Shocks.cc b/Shocks.cc index 1d25d4b1..d9af2d05 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -396,7 +396,7 @@ MomentCalibration::MomentCalibration(const constraints_t &constraints_arg, void MomentCalibration::writeOutput(ostream &output, const string &basename) const { - output << "options_.moment_calibration = {" << endl; + output << "options_.endogenous_prior_restrictions.moment = {" << endl; for (size_t i = 0; i < constraints.size(); i++) { const Constraint &c = constraints[i]; @@ -418,7 +418,7 @@ IrfCalibration::IrfCalibration(const constraints_t &constraints_arg, void IrfCalibration::writeOutput(ostream &output, const string &basename) const { - output << "options_.irf_calibration = {" << endl; + output << "options_.endogenous_prior_restrictions.irf = {" << endl; for (size_t i = 0; i < constraints.size(); i++) { const Constraint &c = constraints[i]; From 8507409d97598c5f4ec5c75887d1463095602f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 17 Mar 2014 18:10:44 +0100 Subject: [PATCH 139/162] Allow ranges in lags/periods specification of {irf,moment}_calibration. Ref #267 --- DynareBison.yy | 30 ++++++++++++++++++++++++++++++ ParsingDriver.cc | 12 ++++++------ ParsingDriver.hh | 4 ++-- Shocks.cc | 4 ++-- Shocks.hh | 6 +++--- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 500b8d14..aafe4022 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -175,6 +175,7 @@ class ParsingDriver; %type filename symbol vec_of_vec_value vec_value_list date_expr %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf named_var +%type integer_range signed_integer_range %type change_type_arg %type change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt calibration_range %type vec_int_elem vec_int_1 vec_int vec_int_number @@ -2367,6 +2368,8 @@ moment_calibration_item : symbol COMMA symbol COMMA calibration_range ';' { driver.add_moment_calibration_item($1, $3, new string("0"), $5); } | symbol COMMA symbol '(' signed_integer ')' COMMA calibration_range ';' { driver.add_moment_calibration_item($1, $3, $5, $8); } + | symbol COMMA symbol '(' signed_integer_range ')' COMMA calibration_range ';' + { driver.add_moment_calibration_item($1, $3, $5, $8); } ; irf_calibration : IRF_CALIBRATION ';' irf_calibration_list END ';' @@ -2381,6 +2384,8 @@ irf_calibration_item : symbol COMMA symbol COMMA calibration_range ';' { driver.add_irf_calibration_item($1, new string("1"), $3, $5); } | symbol '(' INT_NUMBER ')' COMMA symbol COMMA calibration_range ';' { driver.add_irf_calibration_item($1, $3, $6, $8); } + | symbol '(' integer_range ')' COMMA symbol COMMA calibration_range ';' + { driver.add_irf_calibration_item($1, $3, $6, $8); } ; o_dr_algo : DR_ALGO EQUAL INT_NUMBER { @@ -2842,6 +2847,31 @@ range : symbol ':' symbol $$ = $1; }; +integer_range : INT_NUMBER ':' INT_NUMBER + { + $1->append(":"); + $1->append(*$3); + delete $3; + $$ = $1; + }; + +signed_integer_range : signed_integer ':' signed_integer + { + $1->append(":"); + $1->append(*$3); + delete $3; + $$ = $1; + } + | MINUS '(' signed_integer ':' signed_integer ')' + { + $3->insert(0, "-("); + $3->append(":"); + $3->append(*$5); + delete $5; + $3->append(")"); + $$ = $3; + }; + vec_int_number : INT_NUMBER { $$ = new vector(); $$->push_back(atoi((*$1).c_str())); delete $1; }; vec_int_elem : vec_int_number diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 3a46428f..72e3c22c 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2606,7 +2606,7 @@ ParsingDriver::add_parallel_local_file(string *filename) } void -ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector *range) +ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string *lags, vector *range) { MomentCalibration::Constraint c; @@ -2622,8 +2622,8 @@ ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string error("Variable " + *endo2 + " is not an endogenous."); delete endo2; - c.lag = abs(atoi(lag->c_str())); - delete lag; + c.lags = *lags; + delete lags; assert(range->size() == 2); c.lower_bound = *((*range)[0]); @@ -2643,7 +2643,7 @@ void ParsingDriver::end_moment_calibration() } void -ParsingDriver::add_irf_calibration_item(string *endo, string *period, string *exo, vector *range) +ParsingDriver::add_irf_calibration_item(string *endo, string *periods, string *exo, vector *range) { IrfCalibration::Constraint c; @@ -2653,8 +2653,8 @@ ParsingDriver::add_irf_calibration_item(string *endo, string *period, string *ex error("Variable " + *endo + " is not an endogenous."); delete endo; - c.period = atoi(period->c_str()); - delete period; + c.periods = *periods; + delete periods; check_symbol_existence(*exo); c.exo = mod_file->symbol_table.getID(*exo); diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 24b47ffc..dec73dfe 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -658,11 +658,11 @@ public: //! Processing the parallel_local_files option void add_parallel_local_file(string *filename); //! Add an item of a moment_calibration statement - void add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector *range); + void add_moment_calibration_item(string *endo1, string *endo2, string *lags, vector *range); //! End a moment_calibration statement void end_moment_calibration(); //! Add an item of an irf_calibration statement - void add_irf_calibration_item(string *endo, string *period, string *exo, vector *range); + void add_irf_calibration_item(string *endo, string *periods, string *exo, vector *range); //! End a moment_calibration statement void end_irf_calibration(); }; diff --git a/Shocks.cc b/Shocks.cc index d9af2d05..c6b51ee4 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -402,7 +402,7 @@ MomentCalibration::writeOutput(ostream &output, const string &basename) const const Constraint &c = constraints[i]; output << "'" << symbol_table.getName(c.endo1) << "', " << "'" << symbol_table.getName(c.endo2) << "', " - << c.lag << ", " + << c.lags << ", " << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" << endl; } @@ -424,7 +424,7 @@ IrfCalibration::writeOutput(ostream &output, const string &basename) const const Constraint &c = constraints[i]; output << "'" << symbol_table.getName(c.endo) << "', " << "'" << symbol_table.getName(c.exo) << "', " - << c.period << ", " + << c.periods << ", " << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" << endl; } diff --git a/Shocks.hh b/Shocks.hh index 17f6bb85..ef68b78e 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -103,7 +103,7 @@ public: struct Constraint { int endo1, endo2; - int lag; + string lags; string lower_bound, upper_bound; }; typedef vector constraints_t; @@ -121,9 +121,9 @@ class IrfCalibration : public Statement public: struct Constraint { - int endo, period; + int endo; int exo; - string lower_bound, upper_bound; + string periods, lower_bound, upper_bound; }; typedef vector constraints_t; private: From 51e8a70bf48bbb5696e918141c9d58b1a10db76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 24 Mar 2014 16:31:26 +0100 Subject: [PATCH 140/162] Fix call to "mex" helper under MATLAB 8.3 (R2014a) and GNU/Linux. There is a new LINKOPTS variable which governs the list of symbols to be exported, so we set it to an empty value to have all symbols exported. Previously we were overloading the LDFLAGS which was containing the list of exported symbols. --- ModFile.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 98ee8d7d..0a5df469 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -687,8 +687,13 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b #else # ifdef __linux__ // MATLAB/Linux - mOutputFile << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl; + mOutputFile << " if matlab_ver_less_than('8.3')" << endl + << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl + << " else" << endl + << " eval('mex -O LINKEXPORT='''' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LINKEXPORT='''' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl + << " end" << endl; # else // MacOS // MATLAB/MacOS mOutputFile << " if matlab_ver_less_than('8.1')" << endl; From 39e9bfceeb536ecf36ae75f240f3308994480267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 24 Mar 2014 17:05:58 +0100 Subject: [PATCH 141/162] Remove last preprocessor information message. First, the current logic is buggy, because mod_file->use_dll is undefined at this point (mod_file having been deleted above). More fundamentally, the preprocessor is now evolving into something to be used without MATLAB/Octave, so this statement is now inaccurate in some use cases. --- DynareMain2.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/DynareMain2.cc b/DynareMain2.cc index 208f1e40..2f0f30a3 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -69,6 +69,4 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm delete mod_file; cout << "Preprocessing completed." << endl; - if (!mod_file->use_dll) - cout << "Starting MATLAB/Octave computing." << endl; } From 2515d772abeabecea045672089d5b8c7f16a907a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 25 Mar 2014 10:42:27 +0100 Subject: [PATCH 142/162] changes related to #641 for os x --- ModFile.cc | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 0a5df469..fd5b4715 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -696,17 +696,22 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b << " end" << endl; # else // MacOS // MATLAB/MacOS - mOutputFile << " if matlab_ver_less_than('8.1')" << endl; - mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + mOutputFile << " if matlab_ver_less_than('8.3')" << endl + << " if matlab_ver_less_than('8.1')" << endl + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " - << basename << "_static.c " << basename << "_static_mex.c')" << endl; - mOutputFile << " else" << endl; - mOutputFile << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_static.c " << basename << "_static_mex.c')" << endl + << " else" << endl + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl - << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " - << basename << "_static.c " << basename << "_static_mex.c')" << endl; - mOutputFile << " end" << endl; + << " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\$ARCHS -Wl,-syslibroot,\\$MW_SDKROOT -mmacosx-version-min=\\$MACOSX_DEPLOYMENT_TARGET -bundle'' " + << basename << "_static.c " << basename << "_static_mex.c')" << endl + << " end" << endl + << " else" << endl + << " eval('mex -O LINKEXPORT='''' " << basename << "_dynamic.c " << basename << "_dynamic_mex.c')" << endl + << " eval('mex -O LINKEXPORT='''' " << basename << "_static.c "<< basename << "_static_mex.c')" << endl + << " end" << endl; # endif #endif mOutputFile << "else" << endl // Octave From 66baffaee5a5b88418a7ab6598e7f187fdda68fb Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 25 Mar 2014 15:42:33 +0100 Subject: [PATCH 143/162] add missing semicolon --- ComputingTasks.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index 74b541f5..afcab1a6 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -1897,9 +1897,9 @@ SubsamplesStatement::writeOutput(ostream &output, const string &basename) const output << "estimation_info.subsamples(subsamples_indx).range_index(" << map_indx << ") = {'" << it->first << "'};" << endl << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date1 = " - << it->second.first << endl + << it->second.first << ";" << endl << "estimation_info.subsamples(subsamples_indx).range(" << map_indx << ").date2 = " - << it->second.second << endl; + << it->second.second << ";" << endl; // Initialize associated subsample substructures in estimation_info const SymbolType symb_type = symbol_table.getType(name1); From 491e70ab7be74543f2ebc384845bc3b48c020162 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 25 Mar 2014 12:48:34 +0100 Subject: [PATCH 144/162] dmm/estimation: add dirichlet prior shape front end, #642 --- CodeInterpreter.hh | 5 +++-- ComputingTasks.cc | 3 +++ DynareBison.yy | 4 +++- DynareFlex.ll | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CodeInterpreter.hh b/CodeInterpreter.hh index e4e3e103..d39567b8 100644 --- a/CodeInterpreter.hh +++ b/CodeInterpreter.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2013 Dynare Team + * Copyright (C) 2007-2014 Dynare Team * * This file is part of Dynare. * @@ -245,7 +245,8 @@ enum PriorDistributions eInvGamma = 4, eInvGamma1 = 4, eUniform = 5, - eInvGamma2 = 6 + eInvGamma2 = 6, + eDirichlet = 7 }; struct Block_contain_type diff --git a/ComputingTasks.cc b/ComputingTasks.cc index afcab1a6..e11a55f9 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2180,6 +2180,9 @@ BasicPriorStatement::writeCShape(ostream &output) const case eInvGamma2: output << "\"inv_gamma2\";" << endl; break; + case eDirichlet: + output << "\"dirichlet\";" << endl; + break; case eNoShape: assert(prior_shape != eNoShape); } diff --git a/DynareBison.yy b/DynareBison.yy index aafe4022..b367f830 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -144,7 +144,7 @@ class ParsingDriver; %token VLISTLOG VLISTPER %token RESTRICTION RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST %token DUMMY_OBS NSTATES INDXSCALESSTATES NO_BAYESIAN_PRIOR SPECIFICATION SIMS_ZHA -%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE PRIOR PRIOR_VARIANCE HESSIAN IDENTITY_MATRIX +%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM EPS PDF FIG DR NONE PRIOR PRIOR_VARIANCE HESSIAN IDENTITY_MATRIX DIRICHLET %token GSIG2_LMDM Q_DIAG FLAT_PRIOR NCSK NSTD %token INDXPARR INDXOVR INDXAP APBAND INDXIMF IMFBAND INDXFORE FOREBAND INDXGFOREHAT INDXGIMFHAT %token INDXESTIMA INDXGDLS EQ_MS FILTER_COVARIANCE FILTER_DECOMPOSITION @@ -1285,6 +1285,8 @@ prior_distribution : BETA { $$ = eUniform; } | INV_GAMMA2 { $$ = eInvGamma2; } + | DIRICHLET + { $$ = eDirichlet; } ; prior_pdf : BETA_PDF diff --git a/DynareFlex.ll b/DynareFlex.ll index 5cc35434..2f212c66 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -372,6 +372,10 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 yylval->string_val = new string(yytext); return token::INV_GAMMA2; } +dirichlet { + yylval->string_val = new string(yytext); + return token::DIRICHLET; +} normal { yylval->string_val = new string(yytext); return token::NORMAL; From 8c2e9b3c02219d8854123e0a006085bd0fb34cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 3 Apr 2014 15:05:20 +0200 Subject: [PATCH 145/162] Improvements to smoother2histval (ref #594) - fix handling of auxiliary variables related to lagged endogenous - add preprocessor interface - add histval_file - add tests (for smoother2histval with outfile, and for histval_file) --- ComputingTasks.cc | 12 ++++++++++++ ComputingTasks.hh | 11 ++++++++++- DynareBison.yy | 39 +++++++++++++++++++++++++++++++++----- DynareFlex.ll | 8 +++++++- NumericalInitialization.cc | 11 +++++++++++ NumericalInitialization.hh | 11 ++++++++++- ParsingDriver.cc | 13 +++++++++++++ ParsingDriver.hh | 3 +++ 8 files changed, 100 insertions(+), 8 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index e11a55f9..acf039f8 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2822,3 +2822,15 @@ CorrOptionsStatement::writeCOutput(ostream &output, const string &basename) output << "msdsgeinfo->addMeasurementErrorCorrOption(new ModFileMeasurementErrorCorrOption("; output << "index, index1, init));" << endl; } + +Smoother2histvalStatement::Smoother2histvalStatement(const OptionsList &options_list_arg) : + options_list(options_list_arg) +{ +} + +void +Smoother2histvalStatement::writeOutput(ostream &output, const string &basename) const +{ + options_list.writeOutput(output, "options_smoother2histval"); + output << "smoother2histval(options_smoother2histval);" << endl; +} diff --git a/ComputingTasks.hh b/ComputingTasks.hh index fde8b668..c04f2f70 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -846,4 +846,13 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class Smoother2histvalStatement : public Statement +{ +private: + const OptionsList options_list; +public: + Smoother2histvalStatement(const OptionsList &options_list_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + #endif diff --git a/DynareBison.yy b/DynareBison.yy index b367f830..bedda79b 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -97,8 +97,8 @@ class ParsingDriver; %token DEFAULT FIXED_POINT %token FORECAST K_ORDER_SOLVER INSTRUMENTS SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN %token GAMMA_PDF GRAPH GRAPH_FORMAT CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD -%token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID HYBRID -%token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT +%token HISTVAL HISTVAL_FILE HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID HYBRID +%token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT INFILE INVARS %token INT_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS IRF_PLOT_THRESHOLD IRF_CALIBRATION %token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS TOLF @@ -110,14 +110,14 @@ class ParsingDriver; %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS -%token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED -%token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE +%token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS +%token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PERIOD PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE %token PRINT PRIOR_MC PRIOR_TRUNC PRIOR_MODE PRIOR_MEAN POSTERIOR_MODE POSTERIOR_MEAN POSTERIOR_MEDIAN PRUNING %token QUOTED_STRING %token QZ_CRITERIUM QZ_ZERO_THRESHOLD FULL DSGE_VAR DSGE_VARLAG DSGE_PRIOR_WEIGHT TRUNCATE %token RELATIVE_IRF REPLIC SIMUL_REPLIC RPLOT SAVE_PARAMS_AND_STEADY_STATE PARAMETER_UNCERTAINTY %token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD -%token SMOOTHER SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS +%token SMOOTHER SMOOTHER2HISTVAL SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS %token STDERR STEADY STOCH_SIMUL SURPRISE SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME %token TEX RAMSEY_MODEL RAMSEY_POLICY PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL %token TEX_NAME @@ -270,6 +270,8 @@ statement : parameters | model_diagnostics | moment_calibration | irf_calibration + | smoother2histval + | histval_file ; dsample : DSAMPLE INT_NUMBER ';' @@ -623,6 +625,10 @@ histval_list : histval_list histval_elem histval_elem : symbol '(' signed_integer ')' EQUAL expression ';' { driver.hist_val($1, $3, $6); }; +histval_file : HISTVAL_FILE '(' FILENAME EQUAL filename ')' ';' + { driver.histval_file($5); } + ; + model_options : BLOCK { driver.block(); } | o_cutoff | o_mfs @@ -2390,6 +2396,23 @@ irf_calibration_item : symbol COMMA symbol COMMA calibration_range ';' { driver.add_irf_calibration_item($1, $3, $6, $8); } ; +smoother2histval : SMOOTHER2HISTVAL ';' + { driver.smoother2histval(); } + | SMOOTHER2HISTVAL '(' smoother2histval_options_list ')' ';' + { driver.smoother2histval(); } + ; + +smoother2histval_options_list : smoother2histval_option COMMA smoother2histval_options_list + | smoother2histval_option + ; + +smoother2histval_option : o_infile + | o_invars + | o_period + | o_outfile + | o_outvars + ; + o_dr_algo : DR_ALGO EQUAL INT_NUMBER { if (*$3 == string("0")) driver.warning("dr_algo option is now deprecated, and may be removed in a future version of Dynare"); @@ -2841,6 +2864,12 @@ o_irf_plot_threshold : IRF_PLOT_THRESHOLD EQUAL non_negative_number { driver.opt o_consider_all_endogenous : CONSIDER_ALL_ENDOGENOUS { driver.option_str("endo_vars_for_moment_computations_in_estimation", "all_endogenous_variables"); }; o_consider_only_observed : CONSIDER_ONLY_OBSERVED { driver.option_str("endo_vars_for_moment_computations_in_estimation", "only_observed_variables"); }; +o_infile : INFILE EQUAL filename { driver.option_str("infile", $3); }; +o_invars : INVARS EQUAL '(' symbol_list ')' { driver.option_symbol_list("invars"); }; +o_period : PERIOD EQUAL INT_NUMBER { driver.option_num("period", $3); }; +o_outfile : OUTFILE EQUAL filename { driver.option_str("outfile", $3); }; +o_outvars : OUTVARS EQUAL '(' symbol_list ')' { driver.option_symbol_list("outvars"); }; + range : symbol ':' symbol { $1->append(":"); diff --git a/DynareFlex.ll b/DynareFlex.ll index 2f212c66..eafa637e 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -146,6 +146,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 bvar_forecast {BEGIN DYNARE_STATEMENT; return token::BVAR_FORECAST; } dynare_sensitivity {BEGIN DYNARE_STATEMENT; return token::DYNARE_SENSITIVITY;} initval_file {BEGIN DYNARE_STATEMENT; return token::INITVAL_FILE;} +histval_file {BEGIN DYNARE_STATEMENT; return token::HISTVAL_FILE;} forecast {BEGIN DYNARE_STATEMENT; return token::FORECAST;} shock_decomposition {BEGIN DYNARE_STATEMENT; return token::SHOCK_DECOMPOSITION;} sbvar {BEGIN DYNARE_STATEMENT; return token::SBVAR;} @@ -166,6 +167,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 calib_smoother { BEGIN DYNARE_STATEMENT; return token::CALIB_SMOOTHER; } model_diagnostics {BEGIN DYNARE_STATEMENT; return token::MODEL_DIAGNOSTICS;} extended_path {BEGIN DYNARE_STATEMENT; return token::EXTENDED_PATH;} +smoother2histval {BEGIN DYNARE_STATEMENT; return token::SMOOTHER2HISTVAL;} ; { if (!sigma_e) @@ -532,7 +534,11 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 long_name {return token::LONG_NAME;} consider_all_endogenous {return token::CONSIDER_ALL_ENDOGENOUS;} consider_only_observed {return token::CONSIDER_ONLY_OBSERVED;} - +infile {return token::INFILE;} +invars {return token::INVARS;} +period {return token::PERIOD;} +outfile {return token::OUTFILE;} +outvars {return token::OUTVARS;} [\$][^$]*[\$] { strtok(yytext+1, "$"); diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 0d6bcecc..c328c988 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -341,6 +341,17 @@ InitvalFileStatement::writeOutput(ostream &output, const string &basename) const << "initvalf('" << filename << "');" << endl; } +HistvalFileStatement::HistvalFileStatement(const string &filename_arg) : + filename(filename_arg) +{ +} + +void +HistvalFileStatement::writeOutput(ostream &output, const string &basename) const +{ + output << "histvalf('" << filename << "');" << endl; +} + HomotopyStatement::HomotopyStatement(const homotopy_values_t &homotopy_values_arg, const SymbolTable &symbol_table_arg) : homotopy_values(homotopy_values_arg), diff --git a/NumericalInitialization.hh b/NumericalInitialization.hh index b03ab0d2..c61d41e1 100644 --- a/NumericalInitialization.hh +++ b/NumericalInitialization.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -122,6 +122,15 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class HistvalFileStatement : public Statement +{ +private: + const string filename; +public: + HistvalFileStatement(const string &filename_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + class HomotopyStatement : public Statement { public: diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 72e3c22c..fe48e41a 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2679,3 +2679,16 @@ void ParsingDriver::end_irf_calibration() irf_calibration_constraints.clear(); } +void +ParsingDriver::smoother2histval() +{ + mod_file->addStatement(new Smoother2histvalStatement(options_list)); + options_list.clear(); +} + +void +ParsingDriver::histval_file(string *filename) +{ + mod_file->addStatement(new HistvalFileStatement(*filename)); + delete filename; +} diff --git a/ParsingDriver.hh b/ParsingDriver.hh index dec73dfe..46493879 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -665,6 +665,9 @@ public: void add_irf_calibration_item(string *endo, string *periods, string *exo, vector *range); //! End a moment_calibration statement void end_irf_calibration(); + + void smoother2histval(); + void histval_file(string *filename); }; #endif // ! PARSING_DRIVER_HH From cd063ce3fe757dc458c19800029ffcaa027c8a98 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sat, 15 Mar 2014 21:07:09 +0100 Subject: [PATCH 146/162] extended-preprocessor: starting a C interface (not finished) --- DynamicModel.cc | 105 +++++++++++++++++++++++++++++++++++++++--- DynamicModel.hh | 8 ++-- ModFile.cc | 108 +++++++++++++++++++++++++++++++++++++------- ModFile.hh | 2 +- SteadyStateModel.cc | 4 +- SteadyStateModel.hh | 2 +- SymbolTable.cc | 99 +++++++++++++++++++++++++++++++++++++++- SymbolTable.hh | 2 + 8 files changed, 300 insertions(+), 30 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index b34057b0..415b9b27 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2958,6 +2958,99 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de void DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const +{ + int lag_presence[3]; + // Loop on endogenous variables + vector zeta_back, zeta_mixed, zeta_fwrd, zeta_static; + for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) + { + int varID; + // Loop on periods + for (int lag = 0; lag <= 2; lag++) + { + lag_presence[lag] = 1; + try + { + varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); + } + catch (UnknownDerivIDException &e) + { + lag_presence[lag] = 0; + } + } + if (lag_presence[0] == 1) + if (lag_presence[2] == 1) + zeta_mixed.push_back(endoID); + else + zeta_back.push_back(endoID); + else if (lag_presence[2] == 1) + zeta_fwrd.push_back(endoID); + else + zeta_static.push_back(endoID); + + } + output << "nstatic = " << zeta_static.size() << ";" << endl + << "nfwrd = " << zeta_fwrd.size() << ";" << endl + << "nback = " << zeta_back.size() << ";" << endl + << "nmixed = " << zeta_mixed.size() << ";" << endl; + output << "zeta_static[" << zeta_static.size() << "] = {"; + for (vector::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i) + { + if ( i != zeta_static.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "zeta_back[" << zeta_back.size() << "] = {"; + for (vector::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i) + { + if ( i != zeta_back.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "zeta_fwrd[" << zeta_fwrd.size() << "] = {"; + for (vector::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i) + { + if ( i != zeta_fwrd.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "zeta_mixed[" << zeta_mixed.size() << "] = {"; + for (vector::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i) + { + if ( i != zeta_mixed.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + // Write number of non-zero derivatives + // Use -1 if the derivatives have not been computed + output << "int *NNZDerivatives[3] = {"; + switch (order) + { + case 0: + output << NNZDerivativs[0] << ",-1,-1};" << endl; + break; + case 1: + output << NNZDerivativs[0] << "," << NNZDerivatives[1] << ",-1};" << endl; + break; + case 2: + output << NNZDerivativs[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl; + break; + default: + cerr << "Order larger than 3 not implemented" << endl; + exit(EXIT_FAILURE); + } +} + +void +DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const { int lag_presence[3]; // Loop on endogenous variables @@ -4339,9 +4432,9 @@ DynamicModel::dynamicOnlyEquationsNbr() const } void -DynamicModel::writeFirstDerivativesCC(const string &basename, bool cuda) const +DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const { - string filename = basename + "_first_derivatives.cc"; + string filename = basename + "_first_derivatives.c"; ofstream mDynamicModelFile, mDynamicMexFile; mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); @@ -4396,10 +4489,10 @@ DynamicModel::writeFirstDerivativesCC(const string &basename, bool cuda) const // using compressed sparse row format (CSR) void -DynamicModel::writeSecondDerivativesCC_csr(const string &basename, bool cuda) const +DynamicModel::writeSecondDerivativesC_csr(const string &basename, bool cuda) const { - string filename = basename + "_second_derivatives.cc"; + string filename = basename + "_second_derivatives.c"; ofstream mDynamicModelFile, mDynamicMexFile; mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); @@ -4490,9 +4583,9 @@ DynamicModel::writeSecondDerivativesCC_csr(const string &basename, bool cuda) co } void -DynamicModel::writeThirdDerivativesCC_csr(const string &basename, bool cuda) const +DynamicModel::writeThirdDerivativesC_csr(const string &basename, bool cuda) const { - string filename = basename + "_third_derivatives.cc"; + string filename = basename + "_third_derivatives.c"; ofstream mDynamicModelFile, mDynamicMexFile; mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); diff --git a/DynamicModel.hh b/DynamicModel.hh index 8de6bd39..e1ee490b 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -215,6 +215,8 @@ public: void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; //! Writes model initialization and lead/lag incidence matrix to C output void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + //! Writes model initialization and lead/lag incidence matrix to Cpp output + void writeCCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; //! Adds informations for simulation in a binary file void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, @@ -224,11 +226,11 @@ public: //! Writes file containing parameters derivatives void writeParamsDerivativesFile(const string &basename) const; //! Writes CC file containing first order derivatives of model evaluated at steady state - void writeFirstDerivativesCC(const string &basename, bool cuda) const; + void writeFirstDerivativesC(const string &basename, bool cuda) const; //! Writes CC file containing second order derivatives of model evaluated at steady state (compressed sparse column) - void writeSecondDerivativesCC_csr(const string &basename, bool cuda) const; + void writeSecondDerivativesC_csr(const string &basename, bool cuda) const; //! Writes CC file containing third order derivatives of model evaluated at steady state (compressed sparse column) - void writeThirdDerivativesCC_csr(const string &basename, bool cuda) const; + void writeThirdDerivativesC_csr(const string &basename, bool cuda) const; //! Converts to static model (only the equations) /*! It assumes that the static model given in argument has just been allocated */ void toStatic(StaticModel &static_model) const; diff --git a/ModFile.cc b/ModFile.cc index fd5b4715..7e73f550 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -815,9 +815,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b } void -ModFile::writeModelCC(const string &basename, bool cuda) const +ModFile::writeModelC(const string &basename, bool cuda) const { - string filename = basename + ".cc"; + string filename = basename + ".c"; ofstream mDriverCFile; mDriverCFile.open(filename.c_str(), ios::out | ios::binary); @@ -828,15 +828,15 @@ ModFile::writeModelCC(const string &basename, bool cuda) const } mDriverCFile << "/*" << endl - << " * " << filename << " : Driver file for MS-DSGE code" << endl + << " * " << filename << " : Driver file for Dynare C code" << endl << " *" << endl << " * Warning : this file is generated automatically by Dynare" << endl << " * from model file (.mod)" << endl << " */" << endl << endl - << "#include \"dynare_cpp_driver.hh\"" << endl + << "#include \"dynare_driver.h\"" << endl << endl - << "DynareInfo::DynareInfo(void)" << endl + << "struct" << endl << "{" << endl; // Write basic info @@ -856,7 +856,7 @@ ModFile::writeModelCC(const string &basename, bool cuda) const it != statements.end(); it++) (*it)->writeCOutput(mDriverCFile, basename); - mDriverCFile << "}" << endl; + mDriverCFile << "} DynareInfo;" << endl; mDriverCFile.close(); // Write informational m file @@ -886,6 +886,82 @@ ModFile::writeModelCC(const string &basename, bool cuda) const << "% Warning : this file is generated automatically by Dynare" << endl << "% from model file (.mod)" << endl << endl << "disp('The following C file was successfully created:');" << endl + << "ls preprocessorOutput.c" << endl << endl; + mOutputFile.close(); +} + +void +ModFile::writeModelCC(const string &basename, bool cuda) const +{ + string filename = basename + ".cc"; + + ofstream mDriverCFile; + mDriverCFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDriverCFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + mDriverCFile << "/*" << endl + << " * " << filename << " : Driver file for Dynare C++ code" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << " */" << endl + << endl + << "#include \"dynare_cpp_driver.hh\"" << endl + << endl + << "DynareInfo::DynareInfo(void)" << endl + << "{" << endl; + + // Write basic info + symbol_table.writeCCOutput(mDriverCFile); + + mDriverCFile << endl << "params.resize(param_nbr);" << endl; + + if (dynamic_model.equation_number() > 0) + { + dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + // if (!no_static) + // static_model.writeCOutput(mOutputFile, block); + } + + // Print statements + for (vector::const_iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->writeCOutput(mDriverCFile, basename); + + mDriverCFile << "};" << endl; + mDriverCFile.close(); + + // Write informational m file + ofstream mOutputFile; + + if (basename.size()) + { + string fname(basename); + fname += ".m"; + mOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!mOutputFile.is_open()) + { + cerr << "ERROR: Can't open file " << fname + << " for writing" << endl; + exit(EXIT_FAILURE); + } + } + else + { + cerr << "ERROR: Missing file name" << endl; + exit(EXIT_FAILURE); + } + + mOutputFile << "%" << endl + << "% Status : informational m file" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl + << "disp('The following C++ file was successfully created:');" << endl << "ls preprocessorOutput.cc" << endl << endl; mOutputFile.close(); } @@ -893,8 +969,8 @@ ModFile::writeModelCC(const string &basename, bool cuda) const void ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const { - writeModelCC(basename, cuda); - steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_model_present, cuda); + writeModelC(basename, cuda); + steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present, cuda); dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); @@ -903,18 +979,18 @@ ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool // static_model.writeStaticCFile(basename, block, byte_code, use_dll); - // static_model.writeParamsDerivativesFileCC(basename, cuda); - // static_model.writeAuxVarInitvalCC(mOutputFile, oMatlabOutsideModel, cuda); + // static_model.writeParamsDerivativesFileC(basename, cuda); + // static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda); - // dynamic_model.writeResidualsCC(basename, cuda); - // dynamic_model.writeParamsDerivativesFileCC(basename, cuda); - dynamic_model.writeFirstDerivativesCC(basename, cuda); + // dynamic_model.writeResidualsC(basename, cuda); + // dynamic_model.writeParamsDerivativesFileC(basename, cuda); + dynamic_model.writeFirstDerivativesC(basename, cuda); if (output == second) - dynamic_model.writeSecondDerivativesCC_csr(basename, cuda); + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); else if (output == third) { - dynamic_model.writeSecondDerivativesCC_csr(basename, cuda); - dynamic_model.writeThirdDerivativesCC_csr(basename, cuda); + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); + dynamic_model.writeThirdDerivativesC_csr(basename, cuda); } } diff --git a/ModFile.hh b/ModFile.hh index aca8ef9d..9885c6e6 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -140,7 +140,7 @@ public: ) const; //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; - void writeModelCC(const string &basename, bool cuda) const; + void writeModelC(const string &basename, bool cuda) const; void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const; }; diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index f1069642..1c590a98 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -153,9 +153,9 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model } void -SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const +SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const { - string filename = basename + "_steadystate.cc"; + string filename = basename + "_steadystate.c"; ofstream output; output.open(filename.c_str(), ios::out | ios::binary); diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index 817f84f5..f8bb051f 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -49,7 +49,7 @@ public: \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ void writeSteadyStateFile(const string &basename, bool ramsey_model) const; - void writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const; + void writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const; }; #endif diff --git a/SymbolTable.cc b/SymbolTable.cc index 00152684..4e207cfe 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -288,6 +288,103 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) void SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) +{ + if (!frozen) + throw NotYetFrozenException(); + + output << endl + << "int exo_nbr = " << exo_nbr() << ";" << endl; + if (exo_nbr() > 0) + { + output << "char *exo_names[" << exo_nbr() << "];" << endl; + for (int id = 0; id < exo_nbr(); id++) + output << "exo_names[" << id << "] = \"" << getName(exo_ids[id]) << "\";" << endl; + } + + output << endl + << "int exo_det_nbr = " << exo_det_nbr() << ";" << endl; + if (exo_det_nbr() > 0) + { + output << "char *exo_det_names[" << exo_det_nbr() << "];" << endl; + for (int id = 0; id < exo_det_nbr(); id++) + output << "exo_det_names[" << id << "] = \"" << getName(exo_det_ids[id]) << "\";" << endl; + } + + output << endl + << "int endo_nbr = " << endo_nbr() << ";" << endl; + if (endo_nbr() > 0) + { + output << "char *endo_names[" << endo_nbr() << "];" << endl; + for (int id = 0; id < endo_nbr(); id++) + output << "endo_names[" << id << "] = \"" << getName(endo_ids[id]) << "\";" << endl; + } + + output << endl + << "int param_nbr = " << param_nbr() << ";" << endl; + if (param_nbr() > 0) + { + output << "char *param_names[" << param_nbr() << "];" << endl; + for (int id = 0; id < param_nbr(); id++) + output << "param_names[" << id << "] = \"" << getName(param_ids[id]) << "\";" << endl; + } + + // Write the auxiliary variable table + output << "int aux_var_nbr = " << aux_vars.size() << ";" << endl; + if (aux_vars.size() > 0) + { + output << "struct aux_vars_t *av[" << aux_vars.size() << "];" << endl; + for (int i = 0; i < (int) aux_vars.size(); i++) + { + output << "av[" << i << "].endo_index = " << getTypeSpecificID(aux_vars[i].get_symb_id()) << ";" << endl + << "av[" << i << "].type = " << aux_vars[i].get_type() << ";" << endl; + switch (aux_vars[i].get_type()) + { + case avEndoLead: + case avExoLead: + case avExpectation: + case avMultiplier: + case avDiffForward: + break; + case avEndoLag: + case avExoLag: + output << "av[" << i << "].orig_index = " << getTypeSpecificID(aux_vars[i].get_orig_symb_id()) << ";" << endl + << "av[" << i << "].orig_lead_lag = " << aux_vars[i].get_orig_lead_lag() << ";" << endl; + break; + } + } + } + + output << "int predeterminedNbr = " << predeterminedNbr() << ";" << endl; + if (predeterminedNbr() > 0) + { + output << "int predetermined_variables[" << predeterminedNbr() << "] = {"; + for (set::const_iterator it = predetermined_variables.begin(); + it != predetermined_variables.end(); it++) + { + if ( it != predetermined_variables.begin() ) + output << ","; + output << getTypeSpecificID(*it); + } + output << "};" << endl; + } + + output << "int observedVariablesNbr = " << observedVariablesNbr() << ";" << endl; + if (observedVariablesNbr() > 0) + { + output << "int varobs[" << observedVariablesNbr() << "] = {"; + for (vector::const_iterator it = varobs.begin(); + it != varobs.end(); it++) + { + if ( it != varobs.begin() ) + output << ","; + output << getTypeSpecificID(*it); + } + output << "};" << endl; + } +} + +void +SymbolTable::writeCCOutput(ostream &output) const throw (NotYetFrozenException) { if (!frozen) throw NotYetFrozenException(); @@ -596,7 +693,7 @@ SymbolTable::getEndogenous() const bool SymbolTable::isAuxiliaryVariable(int symb_id) const { - for (int i = 0; i < aux_vars.size(); i++) + for (int i = 0; i < (int) aux_vars.size(); i++) if (aux_vars[i].get_symb_id() == symb_id) return true; return false; diff --git a/SymbolTable.hh b/SymbolTable.hh index 8e0903e5..3e4c47e1 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -278,6 +278,8 @@ public: void writeOutput(ostream &output) const throw (NotYetFrozenException); //! Write C output of this class void writeCOutput(ostream &output) const throw (NotYetFrozenException); + //! Write CC output of this class + void writeCCOutput(ostream &output) const throw (NotYetFrozenException); //! Mark a symbol as predetermined variable void markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException); //! Test if a given symbol is a predetermined variable From 0908b0a50a0cb18d3c0d561a929b277c992552bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Mon, 7 Apr 2014 14:21:28 +0200 Subject: [PATCH 147/162] Fix compilation error introduced in 0983ca78. --- DynamicModel.cc | 6 +++--- ModFile.hh | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 415b9b27..4408a5dd 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -3035,13 +3035,13 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d switch (order) { case 0: - output << NNZDerivativs[0] << ",-1,-1};" << endl; + output << NNZDerivatives[0] << ",-1,-1};" << endl; break; case 1: - output << NNZDerivativs[0] << "," << NNZDerivatives[1] << ",-1};" << endl; + output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl; break; case 2: - output << NNZDerivativs[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl; + output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl; break; default: cerr << "Order larger than 3 not implemented" << endl; diff --git a/ModFile.hh b/ModFile.hh index 9885c6e6..d558b9aa 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -141,6 +141,7 @@ public: //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; void writeModelC(const string &basename, bool cuda) const; + void writeModelCC(const string &basename, bool cuda) const; void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const; }; From f06da421cdd611db0c17b1a974f50b68c531a571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 8 Apr 2014 17:43:58 +0200 Subject: [PATCH 148/162] Store deterministic shocks information in M_. Previously, deterministic shock information was not store in M_, but rather processed on the fly to create oo_.exo_simul and oo_.exo_det_simul. This had several bad consequences, like for example #35. Closes #271 --- Shocks.cc | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/Shocks.cc b/Shocks.cc index c6b51ee4..e7c059d5 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -36,13 +36,13 @@ void AbstractShocksStatement::writeDetShocks(ostream &output) const { int exo_det_length = 0; + int counter = 1; for (det_shocks_t::const_iterator it = det_shocks.begin(); it != det_shocks.end(); it++) { int id = symbol_table.getTypeSpecificID(it->first) + 1; bool exo_det = (symbol_table.getType(it->first) == eExogenousDet); - int set_shocks_index = ((int) mshocks) + 2 * ((int) exo_det); for (size_t i = 0; i < it->second.size(); i++) { @@ -50,20 +50,19 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const const int &period2 = it->second[i].period2; const expr_t value = it->second[i].value; - if (period1 == period2) - { - output << "set_shocks(" << set_shocks_index << "," << period1 - << ", " << id << ", "; - value->writeOutput(output); - output << ");" << endl; - } - else - { - output << "set_shocks(" << set_shocks_index << "," << period1 - << ":" << period2 << ", " << id << ", "; - value->writeOutput(output); - output << ");" << endl; - } + output << "M_.det_shocks(" << counter << ").exo_det=" << (int) exo_det + << ";" << endl + << "M_.det_shocks(" << counter << ").exo_id=" << id + << ";" << endl + << "M_.det_shocks(" << counter << ").multiplicative=" << (int) mshocks + << ";" << endl + << "M_.det_shocks(" << counter << ").periods=" << period1 + << ":" << period2 << ";" << endl + << "M_.det_shocks(" << counter << ").value=("; + value->writeOutput(output); + output << ");" << endl; + + counter++; if (exo_det && (period2 > exo_det_length)) exo_det_length = period2; @@ -93,9 +92,6 @@ ShocksStatement::writeOutput(ostream &output, const string &basename) const << "% SHOCKS instructions" << endl << "%" << endl; - // Write instruction that initializes a shock - output << "make_ex_;" << endl; - writeDetShocks(output); writeVarAndStdShocks(output); writeCovarAndCorrShocks(output); @@ -313,9 +309,6 @@ MShocksStatement::writeOutput(ostream &output, const string &basename) const << "% MSHOCKS instructions" << endl << "%" << endl; - // Write instruction that initializes a shock - output << "make_ex_;" << endl; - writeDetShocks(output); } From e040e4d0d63cb2bc2bf57b8e998c342f0e348041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 8 Apr 2014 17:53:10 +0200 Subject: [PATCH 149/162] Remove workaround preventing the use of "shocks" before "endval". It now works correctly since 82b31156. Closes #35 --- ComputingTasks.cc | 3 --- NumericalInitialization.cc | 6 ------ Shocks.cc | 10 ---------- Shocks.hh | 1 - Statement.cc | 3 +-- Statement.hh | 7 +------ 6 files changed, 2 insertions(+), 28 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index acf039f8..d3777117 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -95,9 +95,6 @@ void SimulStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { mod_file_struct.simul_present = true; - - // The following is necessary to allow shocks+endval+simul in a loop - mod_file_struct.shocks_present_but_simul_not_yet = false; } void diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index c328c988..3df225fc 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -239,12 +239,6 @@ EndValStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati if (endogs.size() > 0 || exogs.size() > 0) exit(EXIT_FAILURE); - - if (mod_file_struct.shocks_present_but_simul_not_yet) - { - cerr << "ERROR: Putting a \"shocks\" block before an \"endval\" block is not permitted. Please swap the two blocks. This limitation will be removed in a future release of Dynare." << endl; - exit(EXIT_FAILURE); - } } void diff --git a/Shocks.cc b/Shocks.cc index e7c059d5..fef8bda4 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -200,9 +200,6 @@ ShocksStatement::writeCovarAndCorrShocks(ostream &output) const void ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { - // Workaround for trac ticket #35 - mod_file_struct.shocks_present_but_simul_not_yet = true; - /* Error out if variables are not of the right type. This must be done here and not at parsing time (see #448). Also Determine if there is a calibrated measurement error */ @@ -312,13 +309,6 @@ MShocksStatement::writeOutput(ostream &output, const string &basename) const writeDetShocks(output); } -void -MShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) -{ - // Workaround for trac ticket #35 - mod_file_struct.shocks_present_but_simul_not_yet = true; -} - ConditionalForecastPathsStatement::ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg) : paths(paths_arg), path_length(-1) diff --git a/Shocks.hh b/Shocks.hh index ef68b78e..6f893d2b 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -83,7 +83,6 @@ public: MShocksStatement(const det_shocks_t &det_shocks_arg, const SymbolTable &symbol_table_arg); virtual void writeOutput(ostream &output, const string &basename) const; - virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); }; class ConditionalForecastPathsStatement : public Statement diff --git a/Statement.cc b/Statement.cc index 4331c4dc..688883cc 100644 --- a/Statement.cc +++ b/Statement.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2013 Dynare Team + * Copyright (C) 2006-2014 Dynare Team * * This file is part of Dynare. * @@ -40,7 +40,6 @@ ModFileStructure::ModFileStructure() : identification_present(false), estimation_analytic_derivation(false), partial_information(false), - shocks_present_but_simul_not_yet(false), histval_present(false), k_order_solver(false), calibrated_measurement_errors(false), diff --git a/Statement.hh b/Statement.hh index 9f4a3c04..0f583865 100644 --- a/Statement.hh +++ b/Statement.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2013 Dynare Team + * Copyright (C) 2006-2014 Dynare Team * * This file is part of Dynare. * @@ -72,11 +72,6 @@ public: bool estimation_analytic_derivation; //! Whether the option partial_information is given to stoch_simul/estimation/osr/ramsey_policy bool partial_information; - //! Whether a shocks or mshocks block has been parsed and no simul command yet run - /*! Used for the workaround for trac ticket #35. When a simul command is - seen, this flag is cleared in order to allow a sequence - shocks+endval+simul in a loop */ - bool shocks_present_but_simul_not_yet; //! Whether a histval bloc is present /*! Used for the workaround for trac ticket #157 */ bool histval_present; From ff17a871dd76560f3ed37cbba3cdf07254f4ab1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 9 Apr 2014 15:23:20 +0200 Subject: [PATCH 150/162] Multiple "shocks" block are cumulative. This was the default behavior before commit 82b31156. This commit reintroduces this behavior. --- ModFile.cc | 3 +++ Shocks.cc | 20 +++++++------------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 7e73f550..f5c20837 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -585,6 +585,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b << "M_.Correlation_matrix = eye(" << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ");" << endl; + // Initialize M_.det_shocks + mOutputFile << "M_.det_shocks = [];" << endl; + if (mod_file_struct.calibrated_measurement_errors) mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " << symbol_table.observedVariablesNbr() << ");" << endl diff --git a/Shocks.cc b/Shocks.cc index fef8bda4..25adbdee 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -36,7 +36,6 @@ void AbstractShocksStatement::writeDetShocks(ostream &output) const { int exo_det_length = 0; - int counter = 1; for (det_shocks_t::const_iterator it = det_shocks.begin(); it != det_shocks.end(); it++) @@ -50,19 +49,14 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const const int &period2 = it->second[i].period2; const expr_t value = it->second[i].value; - output << "M_.det_shocks(" << counter << ").exo_det=" << (int) exo_det - << ";" << endl - << "M_.det_shocks(" << counter << ").exo_id=" << id - << ";" << endl - << "M_.det_shocks(" << counter << ").multiplicative=" << (int) mshocks - << ";" << endl - << "M_.det_shocks(" << counter << ").periods=" << period1 - << ":" << period2 << ";" << endl - << "M_.det_shocks(" << counter << ").value=("; + output << "M_.det_shocks = [ M_.det_shocks;" << endl + << "struct('exo_det'," << (int) exo_det + << ",'exo_id'," << id + << ",'multiplicative'," << (int) mshocks + << ",'periods'," << period1 << ":" << period2 + << ",'value',"; value->writeOutput(output); - output << ");" << endl; - - counter++; + output << ") ];" << endl; if (exo_det && (period2 > exo_det_length)) exo_det_length = period2; From d44cb3af93b2a0e6793e03ab7f0c0b8419972c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 9 Apr 2014 17:57:17 +0200 Subject: [PATCH 151/162] Introduce two new command: perfect_foresight_{setup,solver}. simul is now simply an alias for perfect_foresight_setup+perfect_foresight_solver. --- ComputingTasks.cc | 35 +++++++++++++++++++++++++++++++++-- ComputingTasks.hh | 19 +++++++++++++++++++ DynareBison.yy | 43 ++++++++++++++++++++++++++++++++++++------- DynareFlex.ll | 2 ++ ModFile.cc | 14 +++++++------- ParsingDriver.cc | 15 +++++++++++++++ ParsingDriver.hh | 2 ++ Statement.cc | 2 +- Statement.hh | 4 ++-- 9 files changed, 117 insertions(+), 19 deletions(-) diff --git a/ComputingTasks.cc b/ComputingTasks.cc index d3777117..e9292f90 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -94,14 +94,45 @@ SimulStatement::SimulStatement(const OptionsList &options_list_arg) : void SimulStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { - mod_file_struct.simul_present = true; + mod_file_struct.perfect_foresight_solver_present = true; } void SimulStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); - output << "simul();\n"; + output << "perfect_foresight_setup;" << endl + << "perfect_foresight_solver;" << endl; +} + +PerfectForesightSetupStatement::PerfectForesightSetupStatement(const OptionsList &options_list_arg) : + options_list(options_list_arg) +{ +} + +void +PerfectForesightSetupStatement::writeOutput(ostream &output, const string &basename) const +{ + options_list.writeOutput(output); + output << "perfect_foresight_setup;" << endl; +} + +PerfectForesightSolverStatement::PerfectForesightSolverStatement(const OptionsList &options_list_arg) : + options_list(options_list_arg) +{ +} + +void +PerfectForesightSolverStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + mod_file_struct.perfect_foresight_solver_present = true; +} + +void +PerfectForesightSolverStatement::writeOutput(ostream &output, const string &basename) const +{ + options_list.writeOutput(output); + output << "perfect_foresight_solver;" << endl; } StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg, diff --git a/ComputingTasks.hh b/ComputingTasks.hh index c04f2f70..b1a009ff 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -58,6 +58,25 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class PerfectForesightSetupStatement : public Statement +{ +private: + const OptionsList options_list; +public: + PerfectForesightSetupStatement(const OptionsList &options_list_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class PerfectForesightSolverStatement : public Statement +{ +private: + const OptionsList options_list; +public: + PerfectForesightSolverStatement(const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + class ModelInfoStatement : public Statement { private: diff --git a/DynareBison.yy b/DynareBison.yy index bedda79b..3449cc6f 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -112,6 +112,7 @@ class ParsingDriver; %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS %token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS %token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PERIOD PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE +%token PERFECT_FORESIGHT_SETUP PERFECT_FORESIGHT_SOLVER %token PRINT PRIOR_MC PRIOR_TRUNC PRIOR_MODE PRIOR_MEAN POSTERIOR_MODE POSTERIOR_MEAN POSTERIOR_MEDIAN PRUNING %token QUOTED_STRING %token QZ_CRITERIUM QZ_ZERO_THRESHOLD FULL DSGE_VAR DSGE_VARLAG DSGE_PRIOR_WEIGHT TRUNCATE @@ -272,6 +273,8 @@ statement : parameters | irf_calibration | smoother2histval | histval_file + | perfect_foresight_setup + | perfect_foresight_solver ; dsample : DSAMPLE INT_NUMBER ';' @@ -970,6 +973,37 @@ model_info_options_list : model_info_options_list COMMA model_info_options ; model_info_options : +perfect_foresight_setup : PERFECT_FORESIGHT_SETUP ';' + { driver.perfect_foresight_setup(); } + | PERFECT_FORESIGHT_SETUP '(' perfect_foresight_setup_options_list ')' ';' + { driver.perfect_foresight_setup(); } + ; + +perfect_foresight_setup_options_list : perfect_foresight_setup_options_list COMMA perfect_foresight_setup_options + | perfect_foresight_setup_options + ; + +perfect_foresight_setup_options : o_periods + | o_datafile + ; + +perfect_foresight_solver : PERFECT_FORESIGHT_SOLVER ';' + { driver.perfect_foresight_solver(); } + | PERFECT_FORESIGHT_SOLVER '(' perfect_foresight_solver_options_list ')' ';' + { driver.perfect_foresight_solver(); } + ; + +perfect_foresight_solver_options_list : perfect_foresight_solver_options_list COMMA perfect_foresight_solver_options + | perfect_foresight_solver_options + ; + +perfect_foresight_solver_options : o_stack_solve_algo + | o_markowitz + | o_minimal_solving_periods + | o_simul_maxit + | o_endogenous_terminal_period + ; + simul : SIMUL ';' { driver.simul(); } | SIMUL '(' simul_options_list ')' ';' @@ -980,13 +1014,8 @@ simul_options_list : simul_options_list COMMA simul_options | simul_options ; -simul_options : o_periods - | o_datafile - | o_stack_solve_algo - | o_markowitz - | o_minimal_solving_periods - | o_simul_maxit - | o_endogenous_terminal_period +simul_options : perfect_foresight_setup_options + | perfect_foresight_solver_options ; external_function : EXTERNAL_FUNCTION '(' external_function_options_list ')' ';' diff --git a/DynareFlex.ll b/DynareFlex.ll index eafa637e..4834afee 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -168,6 +168,8 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 model_diagnostics {BEGIN DYNARE_STATEMENT; return token::MODEL_DIAGNOSTICS;} extended_path {BEGIN DYNARE_STATEMENT; return token::EXTENDED_PATH;} smoother2histval {BEGIN DYNARE_STATEMENT; return token::SMOOTHER2HISTVAL;} +perfect_foresight_setup {BEGIN DYNARE_STATEMENT; return token::PERFECT_FORESIGHT_SETUP;} +perfect_foresight_solver {BEGIN DYNARE_STATEMENT; return token::PERFECT_FORESIGHT_SOLVER;} ; { if (!sigma_e) diff --git a/ModFile.cc b/ModFile.cc index f5c20837..7f31e86f 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -129,7 +129,7 @@ ModFile::checkPass() // Allow empty model only when doing a standalone BVAR estimation if (dynamic_model.equation_number() == 0 && (mod_file_struct.check_present - || mod_file_struct.simul_present + || mod_file_struct.perfect_foresight_solver_present || stochastic_statement_present)) { cerr << "ERROR: At least one model equation must be declared!" << endl; @@ -153,9 +153,9 @@ ModFile::checkPass() exit(EXIT_FAILURE); } - if (mod_file_struct.simul_present && stochastic_statement_present) + if (mod_file_struct.perfect_foresight_solver_present && stochastic_statement_present) { - cerr << "ERROR: A .mod file cannot contain both a simul command and one of {stoch_simul, estimation, osr, ramsey_policy, discretionary_policy}" << endl; + cerr << "ERROR: A .mod file cannot contain both one of {perfect_foresight_solver,simul} and one of {stoch_simul, estimation, osr, ramsey_policy, discretionary_policy}. This is not possible: one cannot mix perfect foresight context with stochastic context in the same file." << endl; exit(EXIT_FAILURE); } @@ -407,9 +407,9 @@ ModFile::transformPass(bool nostrict) exit(EXIT_FAILURE); } - if (symbol_table.exo_det_nbr() > 0 && mod_file_struct.simul_present) + if (symbol_table.exo_det_nbr() > 0 && mod_file_struct.perfect_foresight_solver_present) { - cerr << "ERROR: A .mod file cannot contain both a simul command and varexo_det declaration (all exogenous variables are deterministic in this case)" << endl; + cerr << "ERROR: A .mod file cannot contain both one of {perfect_foresight_solver,simul} and varexo_det declaration (all exogenous variables are deterministic in this case)" << endl; exit(EXIT_FAILURE); } @@ -473,13 +473,13 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output) false, paramsDerivatives, block, byte_code); } // Set things to compute for dynamic model - if (mod_file_struct.simul_present || mod_file_struct.check_present + if (mod_file_struct.perfect_foresight_solver_present || mod_file_struct.check_present || mod_file_struct.stoch_simul_present || mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present || mod_file_struct.calib_smoother_present) { - if (mod_file_struct.simul_present) + if (mod_file_struct.perfect_foresight_solver_present) dynamic_model.computingPass(true, false, false, false, global_eval_context, no_tmp_terms, block, use_dll, byte_code); else { diff --git a/ParsingDriver.cc b/ParsingDriver.cc index fe48e41a..cda88042 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2692,3 +2692,18 @@ ParsingDriver::histval_file(string *filename) mod_file->addStatement(new HistvalFileStatement(*filename)); delete filename; } + + +void +ParsingDriver::perfect_foresight_setup() +{ + mod_file->addStatement(new PerfectForesightSetupStatement(options_list)); + options_list.clear(); +} + +void +ParsingDriver::perfect_foresight_solver() +{ + mod_file->addStatement(new PerfectForesightSolverStatement(options_list)); + options_list.clear(); +} diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 46493879..6218776e 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -668,6 +668,8 @@ public: void smoother2histval(); void histval_file(string *filename); + void perfect_foresight_setup(); + void perfect_foresight_solver(); }; #endif // ! PARSING_DRIVER_HH diff --git a/Statement.cc b/Statement.cc index 688883cc..50d8ccef 100644 --- a/Statement.cc +++ b/Statement.cc @@ -23,7 +23,7 @@ ModFileStructure::ModFileStructure() : check_present(false), steady_present(false), - simul_present(false), + perfect_foresight_solver_present(false), stoch_simul_present(false), estimation_present(false), osr_present(false), diff --git a/Statement.hh b/Statement.hh index 0f583865..5bdade39 100644 --- a/Statement.hh +++ b/Statement.hh @@ -36,8 +36,8 @@ public: bool check_present; //! Whether steady is present bool steady_present; - //! Whether a simul statement is present - bool simul_present; + //! Whether a perfect_foresight_solver/simul statement is present + bool perfect_foresight_solver_present; //! Whether a stoch_simul statement is present bool stoch_simul_present; //! Whether an estimation statement is present From bd892547916b90ff908117c47ab7657a34cb9ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 9 Apr 2014 18:33:24 +0200 Subject: [PATCH 152/162] Cosmetic change for clearer code. --- ModFile.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ModFile.cc b/ModFile.cc index 7f31e86f..27299dea 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -585,9 +585,6 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b << "M_.Correlation_matrix = eye(" << symbol_table.exo_nbr() << ", " << symbol_table.exo_nbr() << ");" << endl; - // Initialize M_.det_shocks - mOutputFile << "M_.det_shocks = [];" << endl; - if (mod_file_struct.calibrated_measurement_errors) mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " << symbol_table.observedVariablesNbr() << ");" << endl @@ -597,6 +594,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b mOutputFile << "M_.H = 0;" << endl << "M_.Correlation_matrix_ME = 1;" << endl; + // Initialize M_.det_shocks + mOutputFile << "M_.det_shocks = [];" << endl; + if (linear == 1) mOutputFile << "options_.linear = 1;" << endl; From 27c2396a81c2758916707cc3dce73c90661ca15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 10 Apr 2014 11:29:11 +0200 Subject: [PATCH 153/162] Simplify MATLAB output of symbol lists. --- SymbolList.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/SymbolList.cc b/SymbolList.cc index af4c1911..dca1410e 100644 --- a/SymbolList.cc +++ b/SymbolList.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2010 Dynare Team + * Copyright (C) 2003-2014 Dynare Team * * This file is part of Dynare. * @@ -28,13 +28,15 @@ SymbolList::addSymbol(const string &symbol) void SymbolList::writeOutput(const string &varname, ostream &output) const { - output << varname << "=[];" << endl; + output << varname << " = char("; for (vector::const_iterator it = symbols.begin(); - it != symbols.end(); it++) - if (it == symbols.begin()) - output << varname << " = '" << *it << "';" << endl; - else - output << varname << " = char(" << varname << ", '" << *it << "');" << endl; + it != symbols.end(); ++it) + { + if (it != symbols.begin()) + output << ","; + output << "'" << *it << "'"; + } + output << ");" << endl; } void From 75dbe4afde19fe1b9c82f1486e19e7679c8ad665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 10 Apr 2014 11:43:26 +0200 Subject: [PATCH 154/162] Bugfix: M_.sigma_e_is_diagonal could be wrong if there were several shocks blocks. --- ModFile.cc | 3 +++ Shocks.cc | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ModFile.cc b/ModFile.cc index 27299dea..649440c9 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -594,6 +594,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b mOutputFile << "M_.H = 0;" << endl << "M_.Correlation_matrix_ME = 1;" << endl; + // May be later modified by a shocks block + mOutputFile << "M_.sigma_e_is_diagonal = 1;" << endl; + // Initialize M_.det_shocks mOutputFile << "M_.det_shocks = [];" << endl; diff --git a/Shocks.cc b/Shocks.cc index 25adbdee..8373d9d9 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -89,9 +89,14 @@ ShocksStatement::writeOutput(ostream &output, const string &basename) const writeDetShocks(output); writeVarAndStdShocks(output); writeCovarAndCorrShocks(output); + + /* M_.sigma_e_is_diagonal is initialized to 1 by ModFile.cc. + If there are no off-diagonal elements, and we are not in overwrite mode, + then we don't reset it to 1, since there might be previous shocks blocks + with off-diagonal elements. */ if (covar_shocks.size()+corr_shocks.size() > 0) output << "M_.sigma_e_is_diagonal = 0;" << endl; - else + else if (overwrite) output << "M_.sigma_e_is_diagonal = 1;" << endl; } From 164441287585983e98f73dc231eb41143f6d0df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 10 Apr 2014 11:54:49 +0200 Subject: [PATCH 155/162] New option "overwrite" to "shocks" and "mshocks". Tells that a new block overwrite the previous ones instead of being cumulative. --- DynareBison.yy | 10 ++++-- DynareFlex.ll | 1 + ParsingDriver.cc | 8 ++--- ParsingDriver.hh | 4 +-- Shocks.cc | 82 +++++++++++++++++++++++++++++++++++++----------- Shocks.hh | 11 +++++-- 6 files changed, 85 insertions(+), 31 deletions(-) diff --git a/DynareBison.yy b/DynareBison.yy index 3449cc6f..c986eac7 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -110,7 +110,7 @@ class ParsingDriver; %token NAME %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS -%token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS +%token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS OVERWRITE %token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PERIOD PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE %token PERFECT_FORESIGHT_SETUP PERFECT_FORESIGHT_SOLVER %token PRINT PRIOR_MC PRIOR_TRUNC PRIOR_MODE PRIOR_MEAN POSTERIOR_MODE POSTERIOR_MEAN POSTERIOR_MEDIAN PRUNING @@ -769,7 +769,9 @@ comma_hand_side : hand_side pound_expression: '#' symbol EQUAL hand_side ';' { driver.declare_and_init_model_local_variable($2, $4); }; -shocks : SHOCKS ';' shock_list END ';' { driver.end_shocks(); }; +shocks : SHOCKS ';' shock_list END ';' { driver.end_shocks(false); } + | SHOCKS '(' OVERWRITE ')' ';' shock_list END ';' { driver.end_shocks(true); } + ; shock_list : shock_list shock_elem | shock_elem @@ -873,7 +875,9 @@ svar_options : o_coefficients | o_chain ; -mshocks : MSHOCKS ';' mshock_list END ';' { driver.end_mshocks(); }; +mshocks : MSHOCKS ';' mshock_list END ';' { driver.end_mshocks(false); } + | MSHOCKS '(' OVERWRITE ')' ';' mshock_list END ';' { driver.end_mshocks(true); } + ; mshock_list : mshock_list det_shock_elem | det_shock_elem diff --git a/DynareFlex.ll b/DynareFlex.ll index 4834afee..cc1fffd7 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -613,6 +613,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 exclusion {return token::EXCLUSION;} lag {return token::LAG;} coeff {return token::COEFF;} +overwrite {return token::OVERWRITE;} upper_cholesky {return token::UPPER_CHOLESKY;} lower_cholesky {return token::LOWER_CHOLESKY;} chain {return token::CHAIN;} diff --git a/ParsingDriver.cc b/ParsingDriver.cc index cda88042..70ebe871 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -620,9 +620,9 @@ ParsingDriver::begin_model() } void -ParsingDriver::end_shocks() +ParsingDriver::end_shocks(bool overwrite) { - mod_file->addStatement(new ShocksStatement(det_shocks, var_shocks, std_shocks, + mod_file->addStatement(new ShocksStatement(overwrite, det_shocks, var_shocks, std_shocks, covar_shocks, corr_shocks, mod_file->symbol_table)); det_shocks.clear(); var_shocks.clear(); @@ -632,9 +632,9 @@ ParsingDriver::end_shocks() } void -ParsingDriver::end_mshocks() +ParsingDriver::end_mshocks(bool overwrite) { - mod_file->addStatement(new MShocksStatement(det_shocks, mod_file->symbol_table)); + mod_file->addStatement(new MShocksStatement(overwrite, det_shocks, mod_file->symbol_table)); det_shocks.clear(); } diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 6218776e..a747e896 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -327,9 +327,9 @@ public: //! Begin a model block void begin_model(); //! Writes a shocks statement - void end_shocks(); + void end_shocks(bool overwrite); //! Writes a mshocks statement - void end_mshocks(); + void end_mshocks(bool overwrite); //! Adds a deterministic chock or a path element inside a conditional_forecast_paths block void add_det_shock(string *var, bool conditional_forecast); //! Adds a std error chock diff --git a/Shocks.cc b/Shocks.cc index 8373d9d9..d0aecd96 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -24,9 +24,11 @@ #include "Shocks.hh" AbstractShocksStatement::AbstractShocksStatement(bool mshocks_arg, + bool overwrite_arg, const det_shocks_t &det_shocks_arg, const SymbolTable &symbol_table_arg) : mshocks(mshocks_arg), + overwrite(overwrite_arg), det_shocks(det_shocks_arg), symbol_table(symbol_table_arg) { @@ -65,13 +67,14 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const output << "M_.exo_det_length = " << exo_det_length << ";\n"; } -ShocksStatement::ShocksStatement(const det_shocks_t &det_shocks_arg, +ShocksStatement::ShocksStatement(bool overwrite_arg, + const det_shocks_t &det_shocks_arg, const var_and_std_shocks_t &var_shocks_arg, const var_and_std_shocks_t &std_shocks_arg, const covar_and_corr_shocks_t &covar_shocks_arg, const covar_and_corr_shocks_t &corr_shocks_arg, const SymbolTable &symbol_table_arg) : - AbstractShocksStatement(false, det_shocks_arg, symbol_table_arg), + AbstractShocksStatement(false, overwrite_arg, det_shocks_arg, symbol_table_arg), var_shocks(var_shocks_arg), std_shocks(std_shocks_arg), covar_shocks(covar_shocks_arg), @@ -86,6 +89,26 @@ ShocksStatement::writeOutput(ostream &output, const string &basename) const << "% SHOCKS instructions" << endl << "%" << endl; + if (overwrite) + { + output << "M_.det_shocks = [];" << endl; + + output << "M_.Sigma_e = zeros(" << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ");" << endl + << "M_.Correlation_matrix = eye(" << symbol_table.exo_nbr() << ", " + << symbol_table.exo_nbr() << ");" << endl; + + if (has_calibrated_measurement_errors()) + output << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl + << "M_.Correlation_matrix_ME = eye(" << symbol_table.observedVariablesNbr() << ", " + << symbol_table.observedVariablesNbr() << ");" << endl; + else + output << "M_.H = 0;" << endl + << "M_.Correlation_matrix_ME = 1;" << endl; + + } + writeDetShocks(output); writeVarAndStdShocks(output); writeCovarAndCorrShocks(output); @@ -212,11 +235,7 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati << symbol_table.getName(it->first) << "' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable" << endl; exit(EXIT_FAILURE); } - - if (symbol_table.isObservedVariable(it->first)) - mod_file_struct.calibrated_measurement_errors = true; } - for (var_and_std_shocks_t::const_iterator it = std_shocks.begin(); it != std_shocks.end(); it++) @@ -228,9 +247,6 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati << symbol_table.getName(it->first) << "' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable" << endl; exit(EXIT_FAILURE); } - - if (symbol_table.isObservedVariable(it->first)) - mod_file_struct.calibrated_measurement_errors = true; } for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin(); @@ -249,10 +265,6 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati << symbol_table.getName(symb_id2) << "'is not allowed; covariances can only be specified for exogenous or observed endogenous variables of same type" << endl; exit(EXIT_FAILURE); } - - if (symbol_table.isObservedVariable(symb_id1) - || symbol_table.isObservedVariable(symb_id2)) - mod_file_struct.calibrated_measurement_errors = true; } for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin(); @@ -271,12 +283,11 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati << symbol_table.getName(symb_id2) << "'is not allowed; correlations can only be specified for exogenous or observed endogenous variables of same type" << endl; exit(EXIT_FAILURE); } - - if (symbol_table.isObservedVariable(it->first.first) - || symbol_table.isObservedVariable(it->first.second)) - mod_file_struct.calibrated_measurement_errors = true; } + // Determine if there is a calibrated measurement error + mod_file_struct.calibrated_measurement_errors |= has_calibrated_measurement_errors(); + // Fill in mod_file_struct.parameters_with_shocks_values (related to #469) for (var_and_std_shocks_t::const_iterator it = var_shocks.begin(); it != var_shocks.end(); ++it) @@ -290,11 +301,41 @@ ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidati for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin(); it != corr_shocks.end(); ++it) it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values); + } -MShocksStatement::MShocksStatement(const det_shocks_t &det_shocks_arg, +bool +ShocksStatement::has_calibrated_measurement_errors() const +{ + for (var_and_std_shocks_t::const_iterator it = var_shocks.begin(); + it != var_shocks.end(); it++) + if (symbol_table.isObservedVariable(it->first)) + return true; + + for (var_and_std_shocks_t::const_iterator it = std_shocks.begin(); + it != std_shocks.end(); it++) + if (symbol_table.isObservedVariable(it->first)) + return true; + + for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin(); + it != covar_shocks.end(); it++) + if (symbol_table.isObservedVariable(it->first.first) + || symbol_table.isObservedVariable(it->first.second)) + return true; + + for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin(); + it != corr_shocks.end(); it++) + if (symbol_table.isObservedVariable(it->first.first) + || symbol_table.isObservedVariable(it->first.second)) + return true; + + return false; +} + +MShocksStatement::MShocksStatement(bool overwrite_arg, + const det_shocks_t &det_shocks_arg, const SymbolTable &symbol_table_arg) : - AbstractShocksStatement(true, det_shocks_arg, symbol_table_arg) + AbstractShocksStatement(true, overwrite_arg, det_shocks_arg, symbol_table_arg) { } @@ -305,6 +346,9 @@ MShocksStatement::writeOutput(ostream &output, const string &basename) const << "% MSHOCKS instructions" << endl << "%" << endl; + if (overwrite) + output << "M_.det_shocks = [];" << endl; + writeDetShocks(output); } diff --git a/Shocks.hh b/Shocks.hh index 6f893d2b..6ab43a64 100644 --- a/Shocks.hh +++ b/Shocks.hh @@ -45,11 +45,13 @@ public: protected: //! Is this statement a "mshocks" statement ? (instead of a "shocks" statement) const bool mshocks; + //! Does this "shocks" statement replace the previous ones? + const bool overwrite; const det_shocks_t det_shocks; const SymbolTable &symbol_table; void writeDetShocks(ostream &output) const; - AbstractShocksStatement(bool mshocks_arg, + AbstractShocksStatement(bool mshocks_arg, bool overwrite_arg, const det_shocks_t &det_shocks_arg, const SymbolTable &symbol_table_arg); }; @@ -66,8 +68,10 @@ private: void writeVarAndStdShocks(ostream &output) const; void writeCovarOrCorrShock(ostream &output, covar_and_corr_shocks_t::const_iterator &it, bool corr) const; void writeCovarAndCorrShocks(ostream &output) const; + bool has_calibrated_measurement_errors() const; public: - ShocksStatement(const det_shocks_t &det_shocks_arg, + ShocksStatement(bool overwrite_arg, + const det_shocks_t &det_shocks_arg, const var_and_std_shocks_t &var_shocks_arg, const var_and_std_shocks_t &std_shocks_arg, const covar_and_corr_shocks_t &covar_shocks_arg, @@ -80,7 +84,8 @@ public: class MShocksStatement : public AbstractShocksStatement { public: - MShocksStatement(const det_shocks_t &det_shocks_arg, + MShocksStatement(bool overwrite_arg, + const det_shocks_t &det_shocks_arg, const SymbolTable &symbol_table_arg); virtual void writeOutput(ostream &output, const string &basename) const; }; From a64786707d3a8e8798d3a374115da61592d2a175 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Thu, 10 Apr 2014 16:35:31 +0200 Subject: [PATCH 156/162] left over change for external API --- ModFile.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ModFile.hh b/ModFile.hh index d558b9aa..f6809361 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -141,6 +141,8 @@ public: //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; void writeModelC(const string &basename, bool cuda) const; + //! Writes Cpp output files only => No further Matlab processing + void writeCCOutputFiles(const string &basename) const; void writeModelCC(const string &basename, bool cuda) const; void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const; }; From 578da1e0bc16437ac0245ab3f1bfad4b55c287bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 10 Apr 2014 16:38:39 +0200 Subject: [PATCH 157/162] Perfect foresight solver now uses a homotopy technique by default. This commit introduces a "no_homotopy" option to restore the old behavior. Ref #220 --- DynareBison.yy | 4 +++- DynareFlex.ll | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DynareBison.yy b/DynareBison.yy index c986eac7..c2cb723e 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -108,7 +108,7 @@ class ParsingDriver; %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION %token NAME -%token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS +%token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS NO_HOMOTOPY %token NOGRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS %token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS OVERWRITE %token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERFECT_FORESIGHT PERIODS PERIOD PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE @@ -1006,6 +1006,7 @@ perfect_foresight_solver_options : o_stack_solve_algo | o_minimal_solving_periods | o_simul_maxit | o_endogenous_terminal_period + | o_no_homotopy ; simul : SIMUL ';' @@ -2896,6 +2897,7 @@ o_mcmc_jumping_covariance : MCMC_JUMPING_COVARIANCE EQUAL HESSIAN o_irf_plot_threshold : IRF_PLOT_THRESHOLD EQUAL non_negative_number { driver.option_num("impulse_responses.plot_threshold", $3); }; o_consider_all_endogenous : CONSIDER_ALL_ENDOGENOUS { driver.option_str("endo_vars_for_moment_computations_in_estimation", "all_endogenous_variables"); }; o_consider_only_observed : CONSIDER_ONLY_OBSERVED { driver.option_str("endo_vars_for_moment_computations_in_estimation", "only_observed_variables"); }; +o_no_homotopy : NO_HOMOTOPY { driver.option_num("no_homotopy", "1"); }; o_infile : INFILE EQUAL filename { driver.option_str("infile", $3); }; o_invars : INVARS EQUAL '(' symbol_list ')' { driver.option_symbol_list("invars"); }; diff --git a/DynareFlex.ll b/DynareFlex.ll index cc1fffd7..6ab6faab 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -608,6 +608,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 planner_discount {return token::PLANNER_DISCOUNT;} calibration {return token::CALIBRATION;} irf_plot_threshold {return token::IRF_PLOT_THRESHOLD;} +no_homotopy {return token::NO_HOMOTOPY;} equation {return token::EQUATION;} exclusion {return token::EXCLUSION;} From 03833cee2fddf94f2cad2d58192aec045a24bfcf Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 18 Apr 2014 16:08:39 +0200 Subject: [PATCH 158/162] preprocessor: don't substitute 'dates' when preceded by an underscore --- Statement.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Statement.cc b/Statement.cc index 50d8ccef..70889c6b 100644 --- a/Statement.cc +++ b/Statement.cc @@ -83,7 +83,7 @@ NativeStatement::writeOutput(ostream &output, const string &basename) const { using namespace boost::xpressive; string date_regex = "(-?\\d+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4]\\d|5[0-2])))"; - sregex regex_lookbehind = sregex::compile("(? Date: Sun, 20 Apr 2014 11:12:12 +0200 Subject: [PATCH 159/162] changed line options for extended preprocessor, adding language option renamed FileOutputType.hh as ExtendedPreprocessorTypes.hh updated Makefile.am --- DynareMain.cc | 39 ++++++++++++++----- DynareMain2.cc | 4 +- ...putType.hh => ExtendedPreprocessorTypes.hh | 22 ++++++++--- Makefile.am | 2 +- ModFile.hh | 2 +- 5 files changed, 50 insertions(+), 19 deletions(-) rename FileOutputType.hh => ExtendedPreprocessorTypes.hh (57%) diff --git a/DynareMain.cc b/DynareMain.cc index 1f99d874..01648868 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -29,7 +29,7 @@ #include "macro/MacroDriver.hh" #include -#include "FileOutputType.hh" +#include "ExtendedPreprocessorTypes.hh" /* Prototype for second part of main function Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be @@ -37,7 +37,7 @@ */ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict, FileOutputType output_mode, bool cuda + bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType lang #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif @@ -48,7 +48,7 @@ usage() { cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]" << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test] " - << " [-D[=]] [nostrict] [-double=dynamic|first|second|third] [cuda]" + << " [-D[=]] [nostrict] [output=dynamic|first|second|third] [language=C|C++]" #if defined(_WIN32) || defined(__CYGWIN32__) << " [cygwin] [msvc]" #endif @@ -97,7 +97,7 @@ main(int argc, char **argv) bool nostrict = false; map defines; FileOutputType output_mode = none; - bool cuda = false; + LanguageOutputType language = matlab; // Parse options for (int arg = 2; arg < argc; arg++) @@ -199,8 +199,6 @@ main(int argc, char **argv) cerr << "Incorrect syntax for ouput option" << endl; usage(); } - // we don't want temp terms in CC functions - no_tmp_terms = true; if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 7, "dynamic", 7)) output_mode = dynamic; else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "first", 5)) @@ -215,8 +213,31 @@ main(int argc, char **argv) usage(); } } - else if (!strcmp(argv[arg], "cuda")) - cuda = true; + else if (strlen(argv[arg]) >= 8 && !strncmp(argv[arg], "language", 8)) + { + if (strlen(argv[arg]) <= 9 || argv[arg][6] != '=') + { + cerr << "Incorrect syntax for language option" << endl; + usage(); + } + // we don't want temp terms in external functions + no_tmp_terms = true; + if (strlen(argv[arg]) == 10 && !strncmp(argv[arg] + 9, "C", 1)) + language = c; + else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 9, "C++", 3)) + language = cpp; + else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 9, "cuda", 4)) + language = cuda; + else if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 9, "julia", 5)) + language = julia; + else if (strlen(argv[arg]) == 15 && !strncmp(argv[arg] + 9, "python", 6)) + language = python; + else + { + cerr << "Incorrect syntax for ouput option" << endl; + usage(); + } + } else { cerr << "Unknown option: " << argv[arg] << endl; @@ -257,7 +278,7 @@ main(int argc, char **argv) // Do the rest main2(macro_output, basename, debug, clear_all, no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive, - parallel, parallel_config_file, cluster_name, parallel_slave_open_mode, parallel_test, nostrict, output_mode, cuda + parallel, parallel_config_file, cluster_name, parallel_slave_open_mode, parallel_test, nostrict, output_mode, language #if defined(_WIN32) || defined(__CYGWIN32__) , cygwin, msvc #endif diff --git a/DynareMain2.cc b/DynareMain2.cc index 2f0f30a3..60a06064 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -22,12 +22,12 @@ #include "ParsingDriver.hh" #include "ModFile.hh" #include "ConfigFile.hh" -#include "FileOutputType.hh" +#include "ExtendedPreprocessorTypes.hh" void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict, FileOutputType output_mode, bool cuda + bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType lang #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif diff --git a/FileOutputType.hh b/ExtendedPreprocessorTypes.hh similarity index 57% rename from FileOutputType.hh rename to ExtendedPreprocessorTypes.hh index 8f4e681f..d9a210e7 100644 --- a/FileOutputType.hh +++ b/ExtendedPreprocessorTypes.hh @@ -17,15 +17,25 @@ * along with Dynare. If not, see . */ -#ifndef _FILE_OUTPUT_TYPE_HH -#define _FILE_OUTPUT_TYPE_HH +#ifndef _EXTENDED_PREPROCESSOR_TYPES_HH +#define _EXTENDED_PREPROCESSOR_TYPES_HH enum FileOutputType { none, // outputs files for Matlab/Octave processing - dynamic, // outputs _dynamic.cc and related files - first, // outputs _first_derivatives and related files - second, // outputs _first_derivatives, _second_derivatives.cc and related files - third, // outputs _first_derivatives, _second_derivatives.cc, _third_derivatives.cc and related files + dynamic, // outputs _dynamic.* and related files + first, // outputs _first_derivatives.* and related files + second, // outputs _first_derivatives.*, _second_derivatives.* and related files + third, // outputs _first_derivatives.*, _second_derivatives.*, _third_derivatives.* and related files + }; + +enum LanguageOutputType + { + matlab, // outputs files for Matlab/Octave processing + c, // outputs files for C + cpp, // outputs files for C++ + cuda, // outputs files for CUDA (not yet implemented) + julia, // outputs files for Julia (not yet implemented) + python, // outputs files for Python (not yet implemented) (not yet implemented) }; #endif diff --git a/Makefile.am b/Makefile.am index 91112aef..fc33ec33 100644 --- a/Makefile.am +++ b/Makefile.am @@ -53,7 +53,7 @@ dynare_m_SOURCES = \ SteadyStateModel.cc \ WarningConsolidation.hh \ WarningConsolidation.cc \ - FileOutputType.hh + ExtendedPreprocessorTypes.hh # The -I. is for dynare_m_CPPFLAGS = $(BOOST_CPPFLAGS) -I. diff --git a/ModFile.hh b/ModFile.hh index f6809361..8b22f2db 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -35,7 +35,7 @@ using namespace std; #include "ExternalFunctionsTable.hh" #include "ConfigFile.hh" #include "WarningConsolidation.hh" -#include "FileOutputType.hh" +#include "ExtendedPreprocessorTypes.hh" //! The abstract representation of a "mod" file class ModFile From b32bf860553c4480b041dbe859c2d146b1d561f9 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 20 Apr 2014 21:23:32 +0200 Subject: [PATCH 160/162] fixing bugs in commit 182c27f --- DynareMain.cc | 4 ++-- DynareMain2.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DynareMain.cc b/DynareMain.cc index 01648868..7fed1e78 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -215,7 +215,7 @@ main(int argc, char **argv) } else if (strlen(argv[arg]) >= 8 && !strncmp(argv[arg], "language", 8)) { - if (strlen(argv[arg]) <= 9 || argv[arg][6] != '=') + if (strlen(argv[arg]) <= 9 || argv[arg][8] != '=') { cerr << "Incorrect syntax for language option" << endl; usage(); @@ -234,7 +234,7 @@ main(int argc, char **argv) language = python; else { - cerr << "Incorrect syntax for ouput option" << endl; + cerr << "Incorrect syntax for language option" << endl; usage(); } } diff --git a/DynareMain2.cc b/DynareMain2.cc index 60a06064..8f82a059 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -27,7 +27,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, const string ¶llel_config_file, const string &cluster_name, bool parallel_slave_open_mode, - bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType lang + bool parallel_test, bool nostrict, FileOutputType output_mode, LanguageOutputType language #if defined(_WIN32) || defined(__CYGWIN32__) , bool cygwin, bool msvc #endif @@ -58,7 +58,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool no_tm // Write outputs if (output_mode != none) - mod_file->writeExternalFiles(basename, output_mode, cuda); + mod_file->writeExternalFiles(basename, output_mode, language); else mod_file->writeOutputFiles(basename, clear_all, no_log, no_warn, console, nograph, nointeractive, config_file #if defined(_WIN32) || defined(__CYGWIN32__) From cffe121a373756453b37edf876b80785bfab11f3 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Mon, 21 Apr 2014 19:57:40 +0200 Subject: [PATCH 161/162] fixing extended-preprocessor C++ API and test case --- DynamicModel.cc | 147 ---------------- ExternalFiles.cc | 403 ++++++++++++++++++++++++++++++++++++++++++++ Makefile.am | 3 +- ModFile.cc | 179 -------------------- ModFile.hh | 9 +- SteadyStateModel.cc | 5 +- SteadyStateModel.hh | 2 +- 7 files changed, 413 insertions(+), 335 deletions(-) create mode 100644 ExternalFiles.cc diff --git a/DynamicModel.cc b/DynamicModel.cc index 4408a5dd..00cb9925 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -2956,153 +2956,6 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de } -void -DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const -{ - int lag_presence[3]; - // Loop on endogenous variables - vector zeta_back, zeta_mixed, zeta_fwrd, zeta_static; - for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) - { - int varID; - // Loop on periods - for (int lag = 0; lag <= 2; lag++) - { - lag_presence[lag] = 1; - try - { - varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); - } - catch (UnknownDerivIDException &e) - { - lag_presence[lag] = 0; - } - } - if (lag_presence[0] == 1) - if (lag_presence[2] == 1) - zeta_mixed.push_back(endoID); - else - zeta_back.push_back(endoID); - else if (lag_presence[2] == 1) - zeta_fwrd.push_back(endoID); - else - zeta_static.push_back(endoID); - - } - output << "nstatic = " << zeta_static.size() << ";" << endl - << "nfwrd = " << zeta_fwrd.size() << ";" << endl - << "nback = " << zeta_back.size() << ";" << endl - << "nmixed = " << zeta_mixed.size() << ";" << endl; - output << "zeta_static[" << zeta_static.size() << "] = {"; - for (vector::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i) - { - if ( i != zeta_static.begin() ) - output << ","; - output << *i; - } - output << "};" << endl; - - output << "zeta_back[" << zeta_back.size() << "] = {"; - for (vector::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i) - { - if ( i != zeta_back.begin() ) - output << ","; - output << *i; - } - output << "};" << endl; - - output << "zeta_fwrd[" << zeta_fwrd.size() << "] = {"; - for (vector::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i) - { - if ( i != zeta_fwrd.begin() ) - output << ","; - output << *i; - } - output << "};" << endl; - - output << "zeta_mixed[" << zeta_mixed.size() << "] = {"; - for (vector::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i) - { - if ( i != zeta_mixed.begin() ) - output << ","; - output << *i; - } - output << "};" << endl; - - // Write number of non-zero derivatives - // Use -1 if the derivatives have not been computed - output << "int *NNZDerivatives[3] = {"; - switch (order) - { - case 0: - output << NNZDerivatives[0] << ",-1,-1};" << endl; - break; - case 1: - output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl; - break; - case 2: - output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl; - break; - default: - cerr << "Order larger than 3 not implemented" << endl; - exit(EXIT_FAILURE); - } -} - -void -DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const -{ - int lag_presence[3]; - // Loop on endogenous variables - for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) - { - int varID; - // Loop on periods - for (int lag = 0; lag <= 2; lag++) - { - lag_presence[lag] = 1; - try - { - varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); - } - catch (UnknownDerivIDException &e) - { - lag_presence[lag] = 0; - } - } - if (lag_presence[0] == 1) - if (lag_presence[2] == 1) - output << "zeta_mixed.push_back(" << endoID << ");" << endl; - else - output << "zeta_back.push_back(" << endoID << ");" << endl; - else if (lag_presence[2] == 1) - output << "zeta_fwrd.push_back(" << endoID << ");" << endl; - else - output << "zeta_static.push_back(" << endoID << ");" << endl; - - } - output << "nstatic = zeta_static.size();" << endl - << "nfwrd = zeta_fwrd.size();" << endl - << "nback = zeta_back.size();" << endl - << "nmixed = zeta_mixed.size();" << endl; - - // Write number of non-zero derivatives - // Use -1 if the derivatives have not been computed - output << endl - << "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl; - if (order > 1) - { - output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl; - if (order > 2) - output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl; - else - output << "NNZDerivatives.push_back(-1);" << endl; - } - else - output << "NNZDerivatives.push_back(-1);" << endl - << "NNZDerivatives.push_back(-1);" << endl; -} - map >, expr_t> DynamicModel::collect_first_order_derivatives_endogenous() { diff --git a/ExternalFiles.cc b/ExternalFiles.cc new file mode 100644 index 00000000..76dc3d30 --- /dev/null +++ b/ExternalFiles.cc @@ -0,0 +1,403 @@ +/* + * Copyright (C) 2006-2013 Dynare Team + * + * This file is part of Dynare. + * + * Dynare is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Dynare is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Dynare. If not, see . + */ +#include "ModFile.hh" +#include "DynamicModel.hh" +#include "StaticModel.hh" +#include "SteadyStateModel.hh" + +void +ModFile::writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const +{ + switch(language) + { + case c: + writeExternalFilesC(basename, output); + break; + case cpp: + writeExternalFilesCC(basename, output); + break; + default: + cerr << "This case shouldn't happen. Contact the authors of Dynare" << endl; + exit(EXIT_FAILURE); + } +} + +// C interface + +void +ModFile::writeExternalFilesC(const string &basename, FileOutputType output) const +{ + writeModelC(basename); + steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present); + + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + + if (!no_static) + static_model.writeStaticFile(basename, false, false, true); + + + // static_model.writeStaticCFile(basename, block, byte_code, use_dll); + // static_model.writeParamsDerivativesFileC(basename, cuda); + // static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda); + + // dynamic_model.writeResidualsC(basename, cuda); + // dynamic_model.writeParamsDerivativesFileC(basename, cuda); + dynamic_model.writeFirstDerivativesC(basename, cuda); + + if (output == second) + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); + else if (output == third) + { + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); + dynamic_model.writeThirdDerivativesC_csr(basename, cuda); + } +} + +void +ModFile::writeModelC(const string &basename) const +{ + string filename = basename + ".c"; + + ofstream mDriverCFile; + mDriverCFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDriverCFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + mDriverCFile << "/*" << endl + << " * " << filename << " : Driver file for Dynare C code" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << " */" << endl + << endl + << "#include \"dynare_driver.h\"" << endl + << endl + << "struct" << endl + << "{" << endl; + + // Write basic info + symbol_table.writeCOutput(mDriverCFile); + + mDriverCFile << endl << "params.resize(param_nbr);" << endl; + + if (dynamic_model.equation_number() > 0) + { + dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + // if (!no_static) + // static_model.writeCOutput(mOutputFile, block); + } + + // Print statements + for (vector::const_iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->writeCOutput(mDriverCFile, basename); + + mDriverCFile << "} DynareInfo;" << endl; + mDriverCFile.close(); + + // Write informational m file + ofstream mOutputFile; + + if (basename.size()) + { + string fname(basename); + fname += ".m"; + mOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!mOutputFile.is_open()) + { + cerr << "ERROR: Can't open file " << fname + << " for writing" << endl; + exit(EXIT_FAILURE); + } + } + else + { + cerr << "ERROR: Missing file name" << endl; + exit(EXIT_FAILURE); + } + + mOutputFile << "%" << endl + << "% Status : informational m file" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl + << "disp('The following C file was successfully created:');" << endl + << "ls preprocessorOutput.c" << endl << endl; + mOutputFile.close(); +} + + +void +DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const +{ + int lag_presence[3]; + // Loop on endogenous variables + vector zeta_back, zeta_mixed, zeta_fwrd, zeta_static; + for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) + { + int varID; + // Loop on periods + for (int lag = 0; lag <= 2; lag++) + { + lag_presence[lag] = 1; + try + { + varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); + } + catch (UnknownDerivIDException &e) + { + lag_presence[lag] = 0; + } + } + if (lag_presence[0] == 1) + if (lag_presence[2] == 1) + zeta_mixed.push_back(endoID); + else + zeta_back.push_back(endoID); + else if (lag_presence[2] == 1) + zeta_fwrd.push_back(endoID); + else + zeta_static.push_back(endoID); + + } + output << "size_t nstatic = " << zeta_static.size() << ";" << endl + << "size_t nfwrd = " << zeta_fwrd.size() << ";" << endl + << "size_t nback = " << zeta_back.size() << ";" << endl + << "size_t nmixed = " << zeta_mixed.size() << ";" << endl; + output << "size_t zeta_static[" << zeta_static.size() << "] = {"; + for (vector::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i) + { + if ( i != zeta_static.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "size_t zeta_back[" << zeta_back.size() << "] = {"; + for (vector::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i) + { + if ( i != zeta_back.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "size_t zeta_fwrd[" << zeta_fwrd.size() << "] = {"; + for (vector::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i) + { + if ( i != zeta_fwrd.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + output << "size_t zeta_mixed[" << zeta_mixed.size() << "] = {"; + for (vector::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i) + { + if ( i != zeta_mixed.begin() ) + output << ","; + output << *i; + } + output << "};" << endl; + + // Write number of non-zero derivatives + // Use -1 if the derivatives have not been computed + output << "int *NNZDerivatives[3] = {"; + switch (order) + { + case 0: + output << NNZDerivatives[0] << ",-1,-1};" << endl; + break; + case 1: + output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl; + break; + case 2: + output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl; + break; + default: + cerr << "Order larger than 3 not implemented" << endl; + exit(EXIT_FAILURE); + } +} + +// C++ interface +void +ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) const +{ + writeModelCC(basename); + steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present); + + dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); + + if (!no_static) + static_model.writeStaticFile(basename, false, false, true); + + + // static_model.writeStaticCFile(basename, block, byte_code, use_dll); + // static_model.writeParamsDerivativesFileC(basename, cuda); + // static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda); + + // dynamic_model.writeResidualsC(basename, cuda); + // dynamic_model.writeParamsDerivativesFileC(basename, cuda); + dynamic_model.writeFirstDerivativesC(basename, cuda); + + if (output == second) + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); + else if (output == third) + { + dynamic_model.writeSecondDerivativesC_csr(basename, cuda); + dynamic_model.writeThirdDerivativesC_csr(basename, cuda); + } +} + +void +ModFile::writeModelCC(const string &basename) const +{ + string filename = basename + ".cc"; + + ofstream mDriverCFile; + mDriverCFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDriverCFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + mDriverCFile << "/*" << endl + << " * " << filename << " : Driver file for Dynare C++ code" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << " */" << endl + << endl + << "#include \"dynare_cpp_driver.hh\"" << endl + << endl + << "DynareInfo::DynareInfo(void)" << endl + << "{" << endl; + + // Write basic info + symbol_table.writeCCOutput(mDriverCFile); + + mDriverCFile << endl << "params.resize(param_nbr);" << endl; + + if (dynamic_model.equation_number() > 0) + { + dynamic_model.writeCCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); + // if (!no_static) + // static_model.writeCOutput(mOutputFile, block); + } + + // Print statements + for (vector::const_iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->writeCOutput(mDriverCFile, basename); + + mDriverCFile << "};" << endl; + mDriverCFile.close(); + + // Write informational m file + ofstream mOutputFile; + + if (basename.size()) + { + string fname(basename); + fname += ".m"; + mOutputFile.open(fname.c_str(), ios::out | ios::binary); + if (!mOutputFile.is_open()) + { + cerr << "ERROR: Can't open file " << fname + << " for writing" << endl; + exit(EXIT_FAILURE); + } + } + else + { + cerr << "ERROR: Missing file name" << endl; + exit(EXIT_FAILURE); + } + + mOutputFile << "%" << endl + << "% Status : informational m file" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl + << "disp('The following C++ file was successfully created:');" << endl + << "ls preprocessorOutput.cc" << endl << endl; + mOutputFile.close(); +} + + +void +DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const +{ + int lag_presence[3]; + // Loop on endogenous variables + for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) + { + int varID; + // Loop on periods + for (int lag = 0; lag <= 2; lag++) + { + lag_presence[lag] = 1; + try + { + varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); + } + catch (UnknownDerivIDException &e) + { + lag_presence[lag] = 0; + } + } + if (lag_presence[0] == 1) + if (lag_presence[2] == 1) + output << "zeta_mixed.push_back(" << endoID << ");" << endl; + else + output << "zeta_back.push_back(" << endoID << ");" << endl; + else if (lag_presence[2] == 1) + output << "zeta_fwrd.push_back(" << endoID << ");" << endl; + else + output << "zeta_static.push_back(" << endoID << ");" << endl; + + } + output << "nstatic = zeta_static.size();" << endl + << "nfwrd = zeta_fwrd.size();" << endl + << "nback = zeta_back.size();" << endl + << "nmixed = zeta_mixed.size();" << endl; + + // Write number of non-zero derivatives + // Use -1 if the derivatives have not been computed + output << endl + << "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl; + if (order > 1) + { + output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl; + if (order > 2) + output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl; + else + output << "NNZDerivatives.push_back(-1);" << endl; + } + else + output << "NNZDerivatives.push_back(-1);" << endl + << "NNZDerivatives.push_back(-1);" << endl; +} + + diff --git a/Makefile.am b/Makefile.am index fc33ec33..5b8118a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -53,7 +53,8 @@ dynare_m_SOURCES = \ SteadyStateModel.cc \ WarningConsolidation.hh \ WarningConsolidation.cc \ - ExtendedPreprocessorTypes.hh + ExtendedPreprocessorTypes.hh \ + ExternalFiles.cc # The -I. is for dynare_m_CPPFLAGS = $(BOOST_CPPFLAGS) -I. diff --git a/ModFile.cc b/ModFile.cc index 649440c9..314fd504 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -820,183 +820,4 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b cout << "done" << endl; } -void -ModFile::writeModelC(const string &basename, bool cuda) const -{ - string filename = basename + ".c"; - ofstream mDriverCFile; - mDriverCFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDriverCFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - - mDriverCFile << "/*" << endl - << " * " << filename << " : Driver file for Dynare C code" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model file (.mod)" << endl - << " */" << endl - << endl - << "#include \"dynare_driver.h\"" << endl - << endl - << "struct" << endl - << "{" << endl; - - // Write basic info - symbol_table.writeCOutput(mDriverCFile); - - mDriverCFile << endl << "params.resize(param_nbr);" << endl; - - if (dynamic_model.equation_number() > 0) - { - dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); - // if (!no_static) - // static_model.writeCOutput(mOutputFile, block); - } - - // Print statements - for (vector::const_iterator it = statements.begin(); - it != statements.end(); it++) - (*it)->writeCOutput(mDriverCFile, basename); - - mDriverCFile << "} DynareInfo;" << endl; - mDriverCFile.close(); - - // Write informational m file - ofstream mOutputFile; - - if (basename.size()) - { - string fname(basename); - fname += ".m"; - mOutputFile.open(fname.c_str(), ios::out | ios::binary); - if (!mOutputFile.is_open()) - { - cerr << "ERROR: Can't open file " << fname - << " for writing" << endl; - exit(EXIT_FAILURE); - } - } - else - { - cerr << "ERROR: Missing file name" << endl; - exit(EXIT_FAILURE); - } - - mOutputFile << "%" << endl - << "% Status : informational m file" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl - << "disp('The following C file was successfully created:');" << endl - << "ls preprocessorOutput.c" << endl << endl; - mOutputFile.close(); -} - -void -ModFile::writeModelCC(const string &basename, bool cuda) const -{ - string filename = basename + ".cc"; - - ofstream mDriverCFile; - mDriverCFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDriverCFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - - mDriverCFile << "/*" << endl - << " * " << filename << " : Driver file for Dynare C++ code" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model file (.mod)" << endl - << " */" << endl - << endl - << "#include \"dynare_cpp_driver.hh\"" << endl - << endl - << "DynareInfo::DynareInfo(void)" << endl - << "{" << endl; - - // Write basic info - symbol_table.writeCCOutput(mDriverCFile); - - mDriverCFile << endl << "params.resize(param_nbr);" << endl; - - if (dynamic_model.equation_number() > 0) - { - dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present); - // if (!no_static) - // static_model.writeCOutput(mOutputFile, block); - } - - // Print statements - for (vector::const_iterator it = statements.begin(); - it != statements.end(); it++) - (*it)->writeCOutput(mDriverCFile, basename); - - mDriverCFile << "};" << endl; - mDriverCFile.close(); - - // Write informational m file - ofstream mOutputFile; - - if (basename.size()) - { - string fname(basename); - fname += ".m"; - mOutputFile.open(fname.c_str(), ios::out | ios::binary); - if (!mOutputFile.is_open()) - { - cerr << "ERROR: Can't open file " << fname - << " for writing" << endl; - exit(EXIT_FAILURE); - } - } - else - { - cerr << "ERROR: Missing file name" << endl; - exit(EXIT_FAILURE); - } - - mOutputFile << "%" << endl - << "% Status : informational m file" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl - << "disp('The following C++ file was successfully created:');" << endl - << "ls preprocessorOutput.cc" << endl << endl; - mOutputFile.close(); -} - -void -ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const -{ - writeModelC(basename, cuda); - steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present, cuda); - - dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); - - if (!no_static) - static_model.writeStaticFile(basename, false, false, true); - - - // static_model.writeStaticCFile(basename, block, byte_code, use_dll); - // static_model.writeParamsDerivativesFileC(basename, cuda); - // static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda); - - // dynamic_model.writeResidualsC(basename, cuda); - // dynamic_model.writeParamsDerivativesFileC(basename, cuda); - dynamic_model.writeFirstDerivativesC(basename, cuda); - - if (output == second) - dynamic_model.writeSecondDerivativesC_csr(basename, cuda); - else if (output == third) - { - dynamic_model.writeSecondDerivativesC_csr(basename, cuda); - dynamic_model.writeThirdDerivativesC_csr(basename, cuda); - } -} diff --git a/ModFile.hh b/ModFile.hh index 8b22f2db..a561af08 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -138,13 +138,16 @@ public: , bool cygwin, bool msvc #endif ) const; + // Functions located in ExternalFiles.cc + void writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const; + void writeExternalFilesC(const string &basename, FileOutputType output) const; + void writeExternalFilesCC(const string &basename, FileOutputType output) const; //! Writes C output files only => No further Matlab processing void writeCOutputFiles(const string &basename) const; - void writeModelC(const string &basename, bool cuda) const; + void writeModelC(const string &basename) const; //! Writes Cpp output files only => No further Matlab processing void writeCCOutputFiles(const string &basename) const; - void writeModelCC(const string &basename, bool cuda) const; - void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const; + void writeModelCC(const string &basename) const; }; #endif // ! MOD_FILE_HH diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 1c590a98..1c811c52 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -153,7 +153,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model } void -SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const +SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const { string filename = basename + "_steadystate.c"; @@ -167,9 +167,6 @@ SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_mode output << "#include " << endl; - if (cuda) - output << "__global__ "; - output << "void steadystate(" << "const double *exo_, const double *params, double *ys_, int *info)" << endl << "// Steady state file generated by Dynare preprocessor" << endl diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index f8bb051f..9a28c430 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -49,7 +49,7 @@ public: \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ void writeSteadyStateFile(const string &basename, bool ramsey_model) const; - void writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const; + void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; }; #endif From 8d3876e55b6a0bcdf03c020a00529fa59d90cd29 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Thu, 24 Apr 2014 20:54:38 +0200 Subject: [PATCH 162/162] putting functions related to extended preprocessor in ExternalFiles.cc --- DynamicModel.cc | 277 ------------------------------------ DynamicModel.hh | 22 +-- ExternalFiles.cc | 335 +++++++++++++++++++++++++++++++++++++++++++- SteadyStateModel.cc | 48 ------- SteadyStateModel.hh | 1 + 5 files changed, 344 insertions(+), 339 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 00cb9925..7dfc7d7e 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -4284,281 +4284,4 @@ DynamicModel::dynamicOnlyEquationsNbr() const return eqs.size(); } -void -DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const -{ - string filename = basename + "_first_derivatives.c"; - ofstream mDynamicModelFile, mDynamicMexFile; - - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "/*" << endl - << " * " << filename << " : Computes first order derivatives of the model for Dynare" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model " << basename << "(.mod)" << endl - << " */" << endl - << "#include " << endl; - - mDynamicModelFile << "#include " << endl; - - mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl - << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; - - // Write function definition if oPowerDeriv is used - writePowerDerivCHeader(mDynamicModelFile); - - mDynamicModelFile << "void FirstDerivatives(const 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; - - // this is always empty here, but needed by d1->writeOutput - deriv_node_temp_terms_t tef_terms; - - // Writing Jacobian - for (first_derivatives_t::const_iterator it = first_derivatives.begin(); - it != first_derivatives.end(); it++) - { - int eq = it->first.first; - int var = it->first.second; - expr_t d1 = it->second; - - jacobianHelper(mDynamicModelFile, eq, getDynJacobianCol(var), oCDynamicModel); - mDynamicModelFile << "="; - // oCstaticModel makes reference to the static variables - d1->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); - mDynamicModelFile << ";" << endl; - } - - mDynamicModelFile << "}" << endl; - - writePowerDeriv(mDynamicModelFile, true); - mDynamicModelFile.close(); - -} - -// using compressed sparse row format (CSR) -void -DynamicModel::writeSecondDerivativesC_csr(const string &basename, bool cuda) const -{ - - string filename = basename + "_second_derivatives.c"; - ofstream mDynamicModelFile, mDynamicMexFile; - - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "/*" << endl - << " * " << filename << " : Computes second order derivatives of the model for Dynare" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model " << basename << "(.mod)" << endl - << " */" << endl - << "#include " << endl; - - mDynamicModelFile << "#include " << endl; - - mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl - << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; - - // write function definition if oPowerDeriv is used - writePowerDerivCHeader(mDynamicModelFile); - - mDynamicModelFile << "void SecondDerivatives(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, int *row_ptr, int *col_ptr, double *value)" << endl - << "{" << endl; - - // this is always empty here, but needed by d1->writeOutput - deriv_node_temp_terms_t tef_terms; - - // Indexing derivatives in column order - vector D; - int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; - for (second_derivatives_t::const_iterator it = second_derivatives.begin(); - it != second_derivatives.end(); it++) - { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second; - - int id1 = getDynJacobianCol(var1); - int id2 = getDynJacobianCol(var2); - - int col_nb = id1 * dynJacobianColsNbr + id2; - - derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - if (id1 != id2) - { - col_nb = id2 * dynJacobianColsNbr + id1; - derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - } - } - sort(D.begin(), D.end(), derivative_less_than() ); - - // Writing Hessian - vector row_ptr(equations.size()); - fill(row_ptr.begin(),row_ptr.end(),0.0); - int k = 0; - for(vector::const_iterator it = D.begin(); it != D.end(); ++it) - { - row_ptr[it->row_nbr]++; - mDynamicModelFile << "col_ptr[" << k << "] " - << "=" << it->col_nbr << ";" << endl; - mDynamicModelFile << "value[" << k << "] = "; - // oCstaticModel makes reference to the static variables - it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); - mDynamicModelFile << ";" << endl; - k++; - } - - // row_ptr must point to the relative address of the first element of the row - int cumsum = 0; - mDynamicModelFile << "row_ptr = [ 0"; - for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) - { - cumsum += *it; - mDynamicModelFile << ", " << cumsum; - } - mDynamicModelFile << "];" << endl; - - mDynamicModelFile << "}" << endl; - - writePowerDeriv(mDynamicModelFile, true); - mDynamicModelFile.close(); - -} - -void -DynamicModel::writeThirdDerivativesC_csr(const string &basename, bool cuda) const -{ - string filename = basename + "_third_derivatives.c"; - ofstream mDynamicModelFile, mDynamicMexFile; - - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "/*" << endl - << " * " << filename << " : Computes third order derivatives of the model for Dynare" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model " << basename << "(.mod)" << endl - << " */" << endl - << "#include " << endl; - - mDynamicModelFile << "#include " << endl; - - mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl - << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; - - // Write function definition if oPowerDeriv is used - writePowerDerivCHeader(mDynamicModelFile); - - mDynamicModelFile << "void ThirdDerivatives(const 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; - - // this is always empty here, but needed by d1->writeOutput - deriv_node_temp_terms_t tef_terms; - - vector D; - int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; - int thirdDerivativesColsNbr = hessianColsNbr*dynJacobianColsNbr; - for (third_derivatives_t::const_iterator it = third_derivatives.begin(); - it != third_derivatives.end(); it++) - { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second.first; - int var3 = it->first.second.second.second; - - int id1 = getDynJacobianCol(var1); - int id2 = getDynJacobianCol(var2); - int id3 = getDynJacobianCol(var3); - - // Reference column number for the g3 matrix (with symmetrical derivatives) - vector cols; - long unsigned int col_nb = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; - int thirdDColsNbr = hessianColsNbr*dynJacobianColsNbr; - derivative deriv(col_nb + eq*thirdDColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - cols.push_back(col_nb); - col_nb = id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2; - if (find(cols.begin(),cols.end(),col_nb) == cols.end()) - { - derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - cols.push_back(col_nb); - } - col_nb = id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3; - if (find(cols.begin(),cols.end(),col_nb) == cols.end()) - { - derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - cols.push_back(col_nb); - } - col_nb = id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1; - if (find(cols.begin(),cols.end(),col_nb) == cols.end()) - { - derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - cols.push_back(col_nb); - } - col_nb = id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2; - if (find(cols.begin(),cols.end(),col_nb) == cols.end()) - { - derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - cols.push_back(col_nb); - } - col_nb = id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1; - if (find(cols.begin(),cols.end(),col_nb) == cols.end()) - { - derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); - D.push_back(deriv); - } - } - - sort(D.begin(), D.end(), derivative_less_than() ); - - vector row_ptr(equations.size()); - fill(row_ptr.begin(),row_ptr.end(),0.0); - int k = 0; - for(vector::const_iterator it = D.begin(); it != D.end(); ++it) - { - row_ptr[it->row_nbr]++; - mDynamicModelFile << "col_ptr[" << k << "] " - << "=" << it->col_nbr << ";" << endl; - mDynamicModelFile << "value[" << k << "] = "; - // oCstaticModel makes reference to the static variables - it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); - mDynamicModelFile << ";" << endl; - k++; - } - - // row_ptr must point to the relative address of the first element of the row - int cumsum = 0; - mDynamicModelFile << "row_ptr = [ 0"; - for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) - { - cumsum += *it; - mDynamicModelFile << ", " << cumsum; - } - mDynamicModelFile << "];" << endl; - - mDynamicModelFile << "}" << endl; - - writePowerDeriv(mDynamicModelFile, true); - mDynamicModelFile.close(); - -} - diff --git a/DynamicModel.hh b/DynamicModel.hh index e1ee490b..c752fe0b 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -213,10 +213,6 @@ public: const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode); //! Writes model initialization and lead/lag incidence matrix to output void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; - //! Writes model initialization and lead/lag incidence matrix to C output - void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; - //! Writes model initialization and lead/lag incidence matrix to Cpp output - void writeCCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; //! Adds informations for simulation in a binary file void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, @@ -225,12 +221,6 @@ public: void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const; //! Writes file containing parameters derivatives void writeParamsDerivativesFile(const string &basename) const; - //! Writes CC file containing first order derivatives of model evaluated at steady state - void writeFirstDerivativesC(const string &basename, bool cuda) const; - //! Writes CC file containing second order derivatives of model evaluated at steady state (compressed sparse column) - void writeSecondDerivativesC_csr(const string &basename, bool cuda) const; - //! Writes CC file containing third order derivatives of model evaluated at steady state (compressed sparse column) - void writeThirdDerivativesC_csr(const string &basename, bool cuda) const; //! Converts to static model (only the equations) /*! It assumes that the static model given in argument has just been allocated */ void toStatic(StaticModel &static_model) const; @@ -473,6 +463,18 @@ public: return (-1); }; bool isModelLocalVariableUsed() const; + + // in ExternalFiles.cc + //! Writes model initialization and lead/lag incidence matrix to C output + void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + //! Writes model initialization and lead/lag incidence matrix to Cpp output + void writeCCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + //! Writes C file containing first order derivatives of model evaluated at steady state + void writeFirstDerivativesC(const string &basename, bool cuda) const; + //! Writes C file containing second order derivatives of model evaluated at steady state (compressed sparse column) + void writeSecondDerivativesC_csr(const string &basename, bool cuda) const; + //! Writes C file containing third order derivatives of model evaluated at steady state (compressed sparse column) + void writeThirdDerivativesC_csr(const string &basename, bool cuda) const; }; //! Classes to re-order derivatives for various sparse storage formats diff --git a/ExternalFiles.cc b/ExternalFiles.cc index 76dc3d30..9ac2ba72 100644 --- a/ExternalFiles.cc +++ b/ExternalFiles.cc @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with Dynare. If not, see . */ +#include #include "ModFile.hh" #include "DynamicModel.hh" #include "StaticModel.hh" @@ -154,14 +155,13 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d vector zeta_back, zeta_mixed, zeta_fwrd, zeta_static; for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) { - int varID; // Loop on periods for (int lag = 0; lag <= 2; lag++) { lag_presence[lag] = 1; try { - varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); + getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); } catch (UnknownDerivIDException &e) { @@ -239,7 +239,335 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d } } +void +DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const +{ + string filename = basename + "_first_derivatives.c"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes first order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // Write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void FirstDerivatives(const 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; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + // Writing Jacobian + for (first_derivatives_t::const_iterator it = first_derivatives.begin(); + it != first_derivatives.end(); it++) + { + int eq = it->first.first; + int var = it->first.second; + expr_t d1 = it->second; + + jacobianHelper(mDynamicModelFile, eq, getDynJacobianCol(var), oCDynamicModel); + mDynamicModelFile << "="; + // oCstaticModel makes reference to the static variables + d1->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + } + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +// using compressed sparse row format (CSR) +void +DynamicModel::writeSecondDerivativesC_csr(const string &basename, bool cuda) const +{ + + string filename = basename + "_second_derivatives.c"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes second order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void SecondDerivatives(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, int *row_ptr, int *col_ptr, double *value)" << endl + << "{" << endl; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + // Indexing derivatives in column order + vector D; + int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + for (second_derivatives_t::const_iterator it = second_derivatives.begin(); + it != second_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + + int col_nb = id1 * dynJacobianColsNbr + id2; + + derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + if (id1 != id2) + { + col_nb = id2 * dynJacobianColsNbr + id1; + derivative deriv(col_nb + eq*hessianColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + } + } + sort(D.begin(), D.end(), derivative_less_than() ); + + // Writing Hessian + vector row_ptr(equations.size()); + fill(row_ptr.begin(),row_ptr.end(),0.0); + int k = 0; + for(vector::const_iterator it = D.begin(); it != D.end(); ++it) + { + row_ptr[it->row_nbr]++; + mDynamicModelFile << "col_ptr[" << k << "] " + << "=" << it->col_nbr << ";" << endl; + mDynamicModelFile << "value[" << k << "] = "; + // oCstaticModel makes reference to the static variables + it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + k++; + } + + // row_ptr must point to the relative address of the first element of the row + int cumsum = 0; + mDynamicModelFile << "row_ptr = [ 0"; + for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) + { + cumsum += *it; + mDynamicModelFile << ", " << cumsum; + } + mDynamicModelFile << "];" << endl; + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +void +DynamicModel::writeThirdDerivativesC_csr(const string &basename, bool cuda) const +{ + string filename = basename + "_third_derivatives.c"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes third order derivatives of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // Write function definition if oPowerDeriv is used + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void ThirdDerivatives(const 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; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + vector D; + int hessianColsNbr = dynJacobianColsNbr*dynJacobianColsNbr; + int thirdDerivativesColsNbr = hessianColsNbr*dynJacobianColsNbr; + for (third_derivatives_t::const_iterator it = third_derivatives.begin(); + it != third_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second.first; + int var3 = it->first.second.second.second; + + int id1 = getDynJacobianCol(var1); + int id2 = getDynJacobianCol(var2); + int id3 = getDynJacobianCol(var3); + + // Reference column number for the g3 matrix (with symmetrical derivatives) + vector cols; + long unsigned int col_nb = id1 * hessianColsNbr + id2 * dynJacobianColsNbr + id3; + int thirdDColsNbr = hessianColsNbr*dynJacobianColsNbr; + derivative deriv(col_nb + eq*thirdDColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + col_nb = id1 * hessianColsNbr + id3 * dynJacobianColsNbr + id2; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id2 * hessianColsNbr + id1 * dynJacobianColsNbr + id3; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id2 * hessianColsNbr + id3 * dynJacobianColsNbr + id1; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id3 * hessianColsNbr + id1 * dynJacobianColsNbr + id2; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + cols.push_back(col_nb); + } + col_nb = id3 * hessianColsNbr + id2 * dynJacobianColsNbr + id1; + if (find(cols.begin(),cols.end(),col_nb) == cols.end()) + { + derivative deriv(col_nb + eq*thirdDerivativesColsNbr,col_nb,eq,it->second); + D.push_back(deriv); + } + } + + sort(D.begin(), D.end(), derivative_less_than() ); + + vector row_ptr(equations.size()); + fill(row_ptr.begin(),row_ptr.end(),0.0); + int k = 0; + for(vector::const_iterator it = D.begin(); it != D.end(); ++it) + { + row_ptr[it->row_nbr]++; + mDynamicModelFile << "col_ptr[" << k << "] " + << "=" << it->col_nbr << ";" << endl; + mDynamicModelFile << "value[" << k << "] = "; + // oCstaticModel makes reference to the static variables + it->value->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + mDynamicModelFile << ";" << endl; + k++; + } + + // row_ptr must point to the relative address of the first element of the row + int cumsum = 0; + mDynamicModelFile << "row_ptr = [ 0"; + for (vector::iterator it=row_ptr.begin(); it != row_ptr.end(); ++it) + { + cumsum += *it; + mDynamicModelFile << ", " << cumsum; + } + mDynamicModelFile << "];" << endl; + + mDynamicModelFile << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + +void +SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const +{ + string filename = basename + "_steadystate.c"; + + ofstream output; + output.open(filename.c_str(), ios::out | ios::binary); + if (!output.is_open()) + { + cerr << "ERROR: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + output << "#include " << endl; + + output << "void steadystate(" + << "const double *exo_, const double *params, double *ys_, int *info)" << endl + << "// Steady state file generated by Dynare preprocessor" << endl + << "{" << endl + << " *info = 0;" << endl; + + if (def_table.size() == 0) + { + output << " return;" << endl + << "}" << endl; + return; + } + + for (size_t i = 0; i < def_table.size(); i++) + { + const vector &symb_ids = def_table[i].first; + output << " "; + if (symb_ids.size() > 1) + std::cout << "Error: in C, multiple returns are not permitted in steady_state_model" << std::endl; + variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[0], 0)); + assert(it != variable_node_map.end()); + if (it->second->get_type() == eModFileLocalVariable) + output << "double "; + dynamic_cast(it->second)->writeOutput(output, oCSteadyStateFile); + output << "="; + def_table[i].second->writeOutput(output, oCSteadyStateFile); + output << ";" << endl; + } + output << " // Auxiliary equations" << endl; + static_model.writeAuxVarInitval(output, oCSteadyStateFile); + output << "}" << endl; +} + + +// // C++ interface +// void ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) const { @@ -353,14 +681,13 @@ DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_ // Loop on endogenous variables for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++) { - int varID; // Loop on periods for (int lag = 0; lag <= 2; lag++) { lag_presence[lag] = 1; try { - varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); + getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1); } catch (UnknownDerivIDException &e) { diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 1c811c52..d19a7f38 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -152,51 +152,3 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model << "end" << endl; } -void -SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const -{ - string filename = basename + "_steadystate.c"; - - ofstream output; - output.open(filename.c_str(), ios::out | ios::binary); - if (!output.is_open()) - { - cerr << "ERROR: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - - output << "#include " << endl; - - output << "void steadystate(" - << "const double *exo_, const double *params, double *ys_, int *info)" << endl - << "// Steady state file generated by Dynare preprocessor" << endl - << "{" << endl - << " *info = 0;" << endl; - - if (def_table.size() == 0) - { - output << " return;" << endl - << "}" << endl; - return; - } - - for (size_t i = 0; i < def_table.size(); i++) - { - const vector &symb_ids = def_table[i].first; - output << " "; - if (symb_ids.size() > 1) - std::cout << "Error: in C, multiple returns are not permitted in steady_state_model" << std::endl; - variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[0], 0)); - assert(it != variable_node_map.end()); - if (it->second->get_type() == eModFileLocalVariable) - output << "double "; - dynamic_cast(it->second)->writeOutput(output, oCSteadyStateFile); - output << "="; - def_table[i].second->writeOutput(output, oCSteadyStateFile); - output << ";" << endl; - } - output << " // Auxiliary equations" << endl; - static_model.writeAuxVarInitval(output, oCSteadyStateFile); - output << "}" << endl; -} - diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index 9a28c430..64b78573 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -49,6 +49,7 @@ public: \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ void writeSteadyStateFile(const string &basename, bool ramsey_model) const; + // in ExternalFiles.cc void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; };