v4 parser:

* added an embryonic checking pass after parsing
* modified the decision of which derivatives to compute:
  - if a simul statement is present, compute the jacobian vs endogenous variables only, and no hessian
  - if a stoch_simul/estimation/olr/osr statement is present, compute the jacobian vs all variables, plus the hessian
  - if the two previous conditions are simultaneously fulfilled, abort with an error
  - if none of these two conditions are fulfilled, abort with an error (nothing to compute)
* fixed a minor bug with the size of the jacobian matrix when computing it only vs endogenous variables


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1120 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
sebastien 2006-12-15 11:44:58 +00:00
parent 47ae595059
commit 73d3e17540
10 changed files with 113 additions and 42 deletions

View File

@ -41,6 +41,12 @@ SimulStatement::SimulStatement(const OptionsList &options_list_arg) :
{ {
} }
void
SimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.simul_present = true;
}
void void
SimulStatement::writeOutput(ostream &output) const SimulStatement::writeOutput(ostream &output) const
{ {
@ -55,6 +61,12 @@ StochSimulStatement::StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_
{ {
} }
void
StochSimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.stoch_simul_or_similar_present = true;
}
void void
StochSimulStatement::writeOutput(ostream &output) const StochSimulStatement::writeOutput(ostream &output) const
{ {
@ -70,6 +82,12 @@ EstimationStatement::EstimationStatement(const TmpSymbolTable &tmp_symbol_table_
{ {
} }
void
EstimationStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.stoch_simul_or_similar_present = true;
}
void void
EstimationStatement::writeOutput(ostream &output) const EstimationStatement::writeOutput(ostream &output) const
{ {
@ -494,6 +512,12 @@ OsrStatement::OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg,
{ {
} }
void
OsrStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.stoch_simul_or_similar_present = true;
}
void void
OsrStatement::writeOutput(ostream &output) const OsrStatement::writeOutput(ostream &output) const
{ {
@ -521,6 +545,12 @@ OlrStatement::OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg,
{ {
} }
void
OlrStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.stoch_simul_or_similar_present = true;
}
void void
OlrStatement::writeOutput(ostream &output) const OlrStatement::writeOutput(ostream &output) const
{ {

View File

@ -46,6 +46,9 @@ main(int argc, char** argv)
// Launch parsing // Launch parsing
ModFile *mod_file = p.parse(argv[1]); ModFile *mod_file = p.parse(argv[1]);
// Run checking pass
mod_file->checkPass();
// FIXME // FIXME
string basename = argv[1]; string basename = argv[1];
basename.erase(basename.size() - 4, 4); basename.erase(basename.size() - 4, 4);

View File

@ -3,7 +3,7 @@
ModFile::ModFile() : symbol_table(model_parameters), ModFile::ModFile() : symbol_table(model_parameters),
model_tree(symbol_table, model_parameters, num_constants), model_tree(symbol_table, model_parameters, num_constants),
order(-1), linear(-1) linear(false)
{ {
} }
@ -20,6 +20,37 @@ ModFile::addStatement(Statement *st)
statements.push_back(st); statements.push_back(st);
} }
void
ModFile::checkPass()
{
for(vector<Statement *>::iterator it = statements.begin();
it != statements.end(); it++)
(*it)->checkPass(mod_file_struct);
if (!mod_file_struct.simul_present
&& !mod_file_struct.stoch_simul_or_similar_present)
{
cerr << "Error: nothing to compute! you must use one of {simul, stoch_simul, estimation, olr, osr}" << endl;
exit(-1);
}
if (mod_file_struct.simul_present
&& mod_file_struct.stoch_simul_or_similar_present)
{
cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, olr, osr}" << endl;
exit(-1);
}
// Set things to compute
if (mod_file_struct.simul_present)
model_tree.computeJacobian = true;
else
{
model_tree.computeJacobianExo = true;
model_tree.computeHessian = true;
}
}
void void
ModFile::writeOutputFiles(const string &basename, bool clear_all) ModFile::writeOutputFiles(const string &basename, bool clear_all)
{ {
@ -92,7 +123,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all)
if (linear == 1) if (linear == 1)
mOutputFile << "options_.linear = 1;" << endl; mOutputFile << "options_.linear = 1;" << endl;
model_tree.writeOutput(mOutputFile, basename, order, linear); model_tree.writeOutput(mOutputFile, basename);
// Print statements // Print statements
for(vector<Statement *>::iterator it = statements.begin(); for(vector<Statement *>::iterator it = statements.begin();

View File

@ -40,9 +40,9 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg,
DataTree(symbol_table_arg, mod_param_arg), DataTree(symbol_table_arg, mod_param_arg),
mod_param(mod_param_arg), mod_param(mod_param_arg),
num_constants(num_constants_arg), num_constants(num_constants_arg),
computeHessian(false),
computeJacobian(false), computeJacobian(false),
computeJacobianExo(false) computeJacobianExo(false),
computeHessian(false)
{ {
} }
@ -964,7 +964,10 @@ string ModelTree::setDynamicModel(void)
cout << "done \n"; cout << "done \n";
} }
int nrows = mod_param.eq_nbr; int nrows = mod_param.eq_nbr;
int nvars = mod_param.var_endo_nbr + mod_param.exo_nbr + mod_param.exo_det_nbr; int nvars = mod_param.var_endo_nbr;
if (computeJacobianExo)
nvars += mod_param.exo_nbr + mod_param.exo_det_nbr;
if (offset == 1) if (offset == 1)
{ {
DynamicOutput << "global M_ it_\n"; DynamicOutput << "global M_ it_\n";
@ -1386,20 +1389,8 @@ inline int ModelTree::optimize(NodeID node)
} }
void void
ModelTree::writeOutput(ostream &output, const string &basename, int order, int linear) ModelTree::writeOutput(ostream &output, const string &basename)
{ {
// Setting flags to compute what is necessary
if (order == 1 || linear == 1)
{
computeJacobianExo = true;
computeJacobian = false;
}
else if (order != -1 && linear != -1)
{
computeHessian = true;
computeJacobianExo = true;
}
ModelInitialization(output); ModelInitialization(output);
if (computeHessian) if (computeHessian)

View File

@ -497,7 +497,6 @@ ParsingDriver::steady()
{ {
mod_file->addStatement(new SteadyStatement(options_list)); mod_file->addStatement(new SteadyStatement(options_list));
options_list.clear(); options_list.clear();
mod_file->model_tree.computeJacobian = true;
} }
void void
@ -527,9 +526,6 @@ ParsingDriver::option_num(const string &name_option, const string &opt)
error("option " + name_option + " declared twice"); error("option " + name_option + " declared twice");
options_list.num_options[name_option] = opt; options_list.num_options[name_option] = opt;
if (name_option == "order")
mod_file->order = atoi(opt.c_str());
} }
void void
@ -552,7 +548,7 @@ ParsingDriver::option_str(const string &name_option, const string &opt)
void void
ParsingDriver::linear() ParsingDriver::linear()
{ {
mod_file->linear = 1; mod_file->linear = true;
} }
void void
@ -582,13 +578,6 @@ void ParsingDriver::rplot()
void ParsingDriver::stoch_simul() void ParsingDriver::stoch_simul()
{ {
// If order and linear not set, then set them to default values
if (mod_file->order == -1)
mod_file->order = 2;
if (mod_file->linear == -1)
mod_file->linear = 0;
mod_file->addStatement(new StochSimulStatement(*tmp_symbol_table, options_list)); mod_file->addStatement(new StochSimulStatement(*tmp_symbol_table, options_list));
tmp_symbol_table->clear(); tmp_symbol_table->clear();
options_list.clear(); options_list.clear();
@ -599,7 +588,6 @@ ParsingDriver::simul()
{ {
mod_file->addStatement(new SimulStatement(options_list)); mod_file->addStatement(new SimulStatement(options_list));
options_list.clear(); options_list.clear();
mod_file->model_tree.computeJacobian = true;
} }
void void
@ -607,7 +595,6 @@ ParsingDriver::check()
{ {
mod_file->addStatement(new CheckStatement(options_list)); mod_file->addStatement(new CheckStatement(options_list));
options_list.clear(); options_list.clear();
mod_file->model_tree.computeJacobian = true;
} }
void void
@ -626,7 +613,6 @@ ParsingDriver::estimated_params()
{ {
mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table)); mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table));
estim_params_list.clear(); estim_params_list.clear();
mod_file->model_tree.computeJacobianExo = true;
} }
void void
@ -784,7 +770,6 @@ ParsingDriver::run_osr()
mod_file->addStatement(new OsrStatement(*tmp_symbol_table, options_list)); mod_file->addStatement(new OsrStatement(*tmp_symbol_table, options_list));
tmp_symbol_table->clear(); tmp_symbol_table->clear();
options_list.clear(); options_list.clear();
mod_file->model_tree.computeJacobianExo = true;
} }
void void

View File

@ -1,9 +1,20 @@
#include "Statement.hh" #include "Statement.hh"
ModFileStructure::ModFileStructure() :
simul_present(false),
stoch_simul_or_similar_present(false)
{
}
Statement::~Statement() Statement::~Statement()
{ {
} }
void
Statement::checkPass(ModFileStructure &mod_file_struct)
{
}
NativeStatement::NativeStatement(const string &native_statement_arg) : NativeStatement::NativeStatement(const string &native_statement_arg) :
native_statement(native_statement_arg) native_statement(native_statement_arg)
{ {

View File

@ -31,6 +31,7 @@ private:
const OptionsList options_list; const OptionsList options_list;
public: public:
SimulStatement(const OptionsList &options_list_arg); SimulStatement(const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const; virtual void writeOutput(ostream &output) const;
}; };
@ -42,6 +43,7 @@ private:
public: public:
StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg, StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg); const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const; virtual void writeOutput(ostream &output) const;
}; };
@ -92,6 +94,7 @@ private:
public: public:
EstimationStatement(const TmpSymbolTable &tmp_symbol_table_arg, EstimationStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg); const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const; virtual void writeOutput(ostream &output) const;
}; };
@ -156,6 +159,7 @@ private:
public: public:
OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg, OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg); const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const; virtual void writeOutput(ostream &output) const;
}; };
@ -167,6 +171,7 @@ private:
public: public:
OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg, OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg); const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const; virtual void writeOutput(ostream &output) const;
}; };

View File

@ -26,17 +26,20 @@ public:
NumericalConstants num_constants; NumericalConstants num_constants;
//! Model equations and their derivatives //! Model equations and their derivatives
ModelTree model_tree; ModelTree model_tree;
//! Option order
int order;
//! Option linear //! Option linear
int linear; bool linear;
private: private:
//! List of statements //! List of statements
vector<Statement *> statements; vector<Statement *> statements;
//! Structure of the mod file
ModFileStructure mod_file_struct;
public: public:
//! Add a statement //! Add a statement
void addStatement(Statement *st); void addStatement(Statement *st);
//! Do some checking and fills mod_file_struct
void checkPass();
//! Writes Matlab/Scilab output files //! Writes Matlab/Scilab output files
/*! /*!
\param basename The base name used for writing output files. Should be the name of the mod file without its extension \param basename The base name used for writing output files. Should be the name of the mod file without its extension

View File

@ -60,8 +60,6 @@ private :
/*! Gets expression of part of model tree */ /*! Gets expression of part of model tree */
inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1); inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1);
inline int optimize(NodeID id); inline int optimize(NodeID id);
/*! When Hessian is writen this flag is set to true */
bool computeHessian;
/*! Opens output M files (1st and 2nd derivatives) */ /*! Opens output M files (1st and 2nd derivatives) */
void OpenMFiles(std::string iModelFileName1, std::string iModelFileName2 = ""); void OpenMFiles(std::string iModelFileName1, std::string iModelFileName2 = "");
/*! Opens output C files (1st and 2nd derivatives) */ /*! Opens output C files (1st and 2nd derivatives) */
@ -92,12 +90,14 @@ public:
ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants); ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants);
//! Destructor //! Destructor
~ModelTree(); ~ModelTree();
//! When Jacobian (vs endogenous) is written this flag is set to true //! Whether Jacobian (vs endogenous) should be written
bool computeJacobian; bool computeJacobian;
//! When Jacobian (vs endogenous and exogenous) is written this flag is set to true //! Whether Jacobian (vs endogenous and exogenous) should be written
bool computeJacobianExo; bool computeJacobianExo;
//! Whether Hessian (vs endogenous and exogenous) should be written
bool computeHessian;
//! Writes model initialization to output and uses basename for dumping model static/dynamic files //! Writes model initialization to output and uses basename for dumping model static/dynamic files
void writeOutput(std::ostream &output, const std::string &basename, int order, int linear); void writeOutput(std::ostream &output, const std::string &basename);
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#endif #endif

View File

@ -7,10 +7,22 @@ using namespace std;
#include <string> #include <string>
#include <map> #include <map>
class ModFileStructure
{
public:
ModFileStructure();
//! Whether a simul statement is present
bool simul_present;
//! Whether a stoch_simul, estimation, olr, osr statement is present
bool stoch_simul_or_similar_present;
};
class Statement class Statement
{ {
public: public:
virtual ~Statement(); virtual ~Statement();
//! Do some internal check, and fill the ModFileStructure class
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output) const = 0; virtual void writeOutput(ostream &output) const = 0;
}; };