From 615f2ae322a179eeaf9e2eb48cb9d813751bae04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 20 Oct 2010 14:47:03 +0200 Subject: [PATCH] Histval block: - fail when user tries to initialize a variable at a period which is not in the model, and make the error message explicit - correctly handle exogenous variables in stochastic mode, which are also substituted with an aux var - added test files --- DataTree.cc | 11 +++++++++++ DataTree.hh | 3 +++ NumericalInitialization.cc | 36 +++++++++++++++++++++--------------- ParsingDriver.cc | 10 +++++++++- SymbolTable.cc | 6 +++--- SymbolTable.hh | 22 +++++++++++++++++----- 6 files changed, 64 insertions(+), 24 deletions(-) diff --git a/DataTree.cc b/DataTree.cc index 24621f33..e6ae3fcd 100644 --- a/DataTree.cc +++ b/DataTree.cc @@ -596,3 +596,14 @@ DataTree::isSecondDerivExternalFunctionUsed(int symb_id) const return false; } + +int +DataTree::minLagForSymbol(int symb_id) const +{ + int r = 0; + for (variable_node_map_t::const_iterator it = variable_node_map.begin(); + it != variable_node_map.end(); ++it) + if (it->first.first == symb_id && it->first.second < r) + r = it->first.second; + return r; +} diff --git a/DataTree.hh b/DataTree.hh index 648c62c7..814284a5 100644 --- a/DataTree.hh +++ b/DataTree.hh @@ -208,6 +208,9 @@ public: bool isFirstDerivExternalFunctionUsed(int symb_id) const; //! Checks if a given second derivative external function is used somewhere in the data tree bool isSecondDerivExternalFunctionUsed(int symb_id) const; + //! Returns the minimum lag (as a negative number) of the given symbol in the whole data tree (and not only in the equations !!) + /*! Returns 0 if the symbol is not used */ + int minLagForSymbol(int symb_id) const; //! Thrown when trying to access an unknown variable by deriv_id class UnknownDerivIDException { diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index ceccb973..e15f2724 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -193,21 +193,27 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const const expr_t expression = it->second; SymbolType type = symbol_table.getType(symb_id); - if (type == eEndogenous && lag < 0) - { - const int new_symb_id = symbol_table.searchAuxiliaryVars(symb_id,lag); - if (new_symb_id != -1) - { - symb_id = new_symb_id; - lag = 0; - } - else if (symbol_table.AuxVarsSize() > 0) - { - cerr << "Histval: this variable doesn't exist with such a lag in the model" << endl; - exit(EXIT_FAILURE); - } - - } + + // For a lag greater than 1, lookup for auxiliary variable + if ((type == eEndogenous || type == eExogenous) && lag < 0) + { + try + { + symb_id = symbol_table.searchAuxiliaryVars(symb_id, lag); + lag = 0; + } + catch (SymbolTable::SearchFailedException &e) + { + if (type == eEndogenous) + { + cerr << "HISTVAL: internal error of Dynare, please contact the developers"; + exit(EXIT_FAILURE); + } + // We don't fail for exogenous, because they are not replaced by + // auxiliary variables in deterministic mode. + } + } + int tsid = symbol_table.getTypeSpecificID(symb_id) + 1; if (type == eEndogenous) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index f0652beb..a719334a 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include "ParsingDriver.hh" #include "Statement.hh" @@ -392,11 +393,18 @@ ParsingDriver::hist_val(string *name, string *lag, expr_t rhs) if (type != eEndogenous && type != eExogenous && type != eExogenousDet) - error("hist_val: " + *name + " should be an endogenous or exogenous variable"); + error("histval: " + *name + " should be an endogenous or exogenous variable"); int ilag = atoi(lag->c_str()); pair key(symb_id, ilag); + if (mod_file->dynamic_model.minLagForSymbol(symb_id) > ilag - 1) + { + ostringstream s; + s << ilag-1; + error("histval: variable " + *name + " does not appear in the model with the lag " + s.str() + " (see the reference manual for the timing convention in 'histval')"); + } + if (hist_values.find(key) != hist_values.end()) error("hist_val: (" + *name + ", " + *lag + ") declared twice"); diff --git a/SymbolTable.cc b/SymbolTable.cc index 7043de83..aba5c399 100644 --- a/SymbolTable.cc +++ b/SymbolTable.cc @@ -368,13 +368,13 @@ SymbolTable::addExpectationAuxiliaryVar(int information_set, int index, const st } int -SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const +SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const throw (SearchFailedException) { - for (int i=0; i < aux_vars.size();++i) + for (size_t i = 0; i < aux_vars.size(); i++) if ((aux_vars[i].get_type() == avEndoLag || aux_vars[i].get_type() == avExoLag) && aux_vars[i].get_orig_symb_id() == orig_symb_id && aux_vars[i].get_orig_lead_lag() == orig_lead_lag) return aux_vars[i].get_symb_id(); - return -1; + throw SearchFailedException(orig_symb_id, orig_lead_lag); } void diff --git a/SymbolTable.hh b/SymbolTable.hh index ad0579cd..0925c5ea 100644 --- a/SymbolTable.hh +++ b/SymbolTable.hh @@ -47,8 +47,8 @@ class AuxVarInfo private: int symb_id; //!< Symbol ID of the auxiliary variable aux_var_t type; //!< Its type - int orig_symb_id; //!< Symbol ID of the endo of the original model represented by this aux var. Not used for avEndoLead - int orig_lead_lag; //!< Lead/lag of the endo of the original model represented by this aux var. Not used for avEndoLead + int orig_symb_id; //!< Symbol ID of the endo of the original model represented by this aux var. Only used for avEndoLag and avExoLag. + int orig_lead_lag; //!< Lead/lag of the endo of the original model represented by this aux var. Only used for avEndoLag and avExoLag. string expectation_information_set_name; //!< Stores 'full' or 'varobs' for avExpectationRIS. Not used otherwise. public: AuxVarInfo(int symb_id_arg, aux_var_t type_arg, int orig_symb_id, int orig_lead_lag, string expectation_information_set_name_arg); @@ -161,6 +161,16 @@ public: class NotYetFrozenException { }; + //! Thrown when searchAuxiliaryVars() failed + class SearchFailedException + { + public: + int orig_symb_id, orig_lead_lag; + SearchFailedException(int orig_symb_id_arg, int orig_lead_lag_arg) : orig_symb_id(orig_symb_id_arg), + orig_lead_lag(orig_lead_lag_arg) + { + } + }; private: //! Factorized code for adding aux lag variables @@ -204,11 +214,13 @@ public: \return the symbol ID of the new symbol */ int addExpectationAuxiliaryVar(int information_set, int index, const string &information_set_name) throw (FrozenException); - //! Searches auxiliary variables by symbol_id and lead_lag + //! Searches auxiliary variables which are substitutes for a given symbol_id and lead/lag /*! - \return the symbol ID of the auxiliary variable and -1 if not found + The search is only performed among auxiliary variables of endo/exo lag. + \return the symbol ID of the auxiliary variable + Throws an exception if match not found. */ - int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const; + int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const throw (SearchFailedException); //! Returns the number of auxiliary variables int AuxVarsSize() const {return aux_vars.size();}; //! Tests if symbol already exists