2008-02-03 11:28:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006-2008 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-06-28 13:20:45 +02:00
|
|
|
#include <cstdlib>
|
2008-02-03 11:28:36 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "ModFile.hh"
|
|
|
|
|
|
|
|
ModFile::ModFile() : expressions_tree(symbol_table, num_constants),
|
|
|
|
model_tree(symbol_table, num_constants),
|
|
|
|
linear(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ModFile::~ModFile()
|
|
|
|
{
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModFile::addStatement(Statement *st)
|
|
|
|
{
|
|
|
|
statements.push_back(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModFile::checkPass()
|
|
|
|
{
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
(*it)->checkPass(mod_file_struct);
|
|
|
|
|
2008-04-24 12:55:26 +02:00
|
|
|
// If order option has not been set, default to 2
|
|
|
|
if (!mod_file_struct.order_option)
|
|
|
|
mod_file_struct.order_option = 2;
|
|
|
|
|
2008-09-29 12:16:13 +02:00
|
|
|
bool stochastic_statement_present = mod_file_struct.stoch_simul_present
|
|
|
|
|| mod_file_struct.estimation_present
|
|
|
|
|| mod_file_struct.forecast_present
|
|
|
|
|| mod_file_struct.osr_present
|
|
|
|
|| mod_file_struct.ramsey_policy_present;
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
// Allow empty model only when doing a standalone BVAR estimation
|
|
|
|
if (model_tree.equation_number() == 0
|
|
|
|
&& (mod_file_struct.check_present
|
|
|
|
|| mod_file_struct.simul_present
|
2008-09-29 12:16:13 +02:00
|
|
|
|| stochastic_statement_present))
|
|
|
|
{
|
|
|
|
cerr << "ERROR: At least one model equation must be declared!" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod_file_struct.simul_present && stochastic_statement_present)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2008-09-29 12:16:13 +02:00
|
|
|
cerr << "ERROR: A .mod file cannot contain both a simul command and one of {stoch_simul, estimation, forecast, osr, ramsey_policy}" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2008-10-17 14:52:08 +02:00
|
|
|
/*
|
|
|
|
Enforce the same number of equations and endogenous, except in two cases:
|
|
|
|
- ramsey_policy is used
|
|
|
|
- a BVAR command is used and there is no equation (standalone BVAR estimation)
|
|
|
|
*/
|
|
|
|
if (!mod_file_struct.ramsey_policy_present
|
|
|
|
&& !((mod_file_struct.bvar_density_present || mod_file_struct.bvar_forecast_present)
|
|
|
|
&& model_tree.equation_number() == 0)
|
|
|
|
&& (model_tree.equation_number() != symbol_table.endo_nbr))
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2008-09-29 12:16:13 +02:00
|
|
|
cerr << "ERROR: There are " << model_tree.equation_number() << " equations but " << symbol_table.endo_nbr << " endogenous variables!" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModFile::computingPass()
|
|
|
|
{
|
|
|
|
// Mod file may have no equation (for example in a standalone BVAR estimation)
|
|
|
|
if (model_tree.equation_number() > 0)
|
|
|
|
{
|
|
|
|
// Set things to compute
|
|
|
|
if (mod_file_struct.simul_present)
|
|
|
|
model_tree.computeJacobian = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3)
|
|
|
|
{
|
|
|
|
cerr << "Incorrect order option..." << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
model_tree.computeJacobianExo = true;
|
|
|
|
if (mod_file_struct.order_option >= 2)
|
|
|
|
model_tree.computeHessian = true;
|
|
|
|
if (mod_file_struct.order_option == 3)
|
|
|
|
model_tree.computeThirdDerivatives = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
model_tree.computingPass(global_eval_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
(*it)->computingPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModFile::writeOutputFiles(const string &basename, bool clear_all) const
|
|
|
|
{
|
|
|
|
ofstream mOutputFile;
|
|
|
|
|
|
|
|
if (basename.size())
|
|
|
|
{
|
|
|
|
string fname(basename);
|
2008-06-16 18:33:28 +02:00
|
|
|
fname += ".m";
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
|
|
|
if (!mOutputFile.is_open())
|
|
|
|
{
|
2008-09-29 12:16:13 +02:00
|
|
|
cerr << "ERROR: Can't open file " << fname
|
2008-02-03 11:28:36 +01:00
|
|
|
<< " for writing" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-29 12:16:13 +02:00
|
|
|
cerr << "ERROR: Missing file name" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << "%" << endl
|
|
|
|
<< "% Status : main Dynare file " << endl
|
|
|
|
<< "%" << endl
|
|
|
|
<< "% Warning : this file is generated automatically by Dynare" << endl
|
|
|
|
<< "% from model file (.mod)" << endl << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
if (clear_all)
|
|
|
|
mOutputFile << "clear all" << endl;
|
|
|
|
mOutputFile << "tic;" << endl;
|
|
|
|
mOutputFile << "global M_ oo_ exedet_ exdet_ recur_ recurs_ " << endl;
|
|
|
|
mOutputFile << "global options_ endval_" << endl;
|
|
|
|
mOutputFile << "global ys0_ recurs0_ ex0_ ct_" << endl;
|
|
|
|
mOutputFile << "options_ = [];" << endl;
|
|
|
|
mOutputFile << "M_.fname = '" << basename << "';" << endl;
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << "%" << endl;
|
|
|
|
mOutputFile << "% Some global variables initialization" << endl;
|
|
|
|
mOutputFile << "%" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << "global_initialization;" << endl;
|
2008-06-24 20:20:48 +02:00
|
|
|
mOutputFile << "diary off;" << endl
|
|
|
|
<< "warning_old_state = warning;" << endl
|
|
|
|
<< "warning off;" << endl
|
|
|
|
<< "delete " << basename << ".log;" << endl
|
|
|
|
<< "warning warning_old_state" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << "logname_ = '" << basename << ".log';" << endl;
|
2008-06-24 20:20:48 +02:00
|
|
|
mOutputFile << "diary " << basename << ".log" << endl;
|
2008-08-25 17:06:36 +02:00
|
|
|
mOutputFile << "options_.model_mode = " << model_tree.mode << ";\n";
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (model_tree.equation_number() > 0)
|
|
|
|
{
|
|
|
|
if (model_tree.mode == eDLLMode)
|
|
|
|
{
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << "if exist('" << basename << "_static.c')" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << " clear " << basename << "_static" << endl;
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << " mex -O " << basename << "_static.c" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << "end" << endl;
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << "if exist('" << basename << "_dynamic.c')" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << " clear " << basename << "_dynamic" << endl;
|
2008-06-16 18:33:28 +02:00
|
|
|
mOutputFile << " mex -O " << basename << "_dynamic.c" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << "end" << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mOutputFile << "erase_compiled_function('" + basename +"_static');" << endl;
|
|
|
|
mOutputFile << "erase_compiled_function('" + basename +"_dynamic');" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Processing outputs ..." << endl;
|
|
|
|
|
|
|
|
symbol_table.writeOutput(mOutputFile);
|
|
|
|
|
|
|
|
if (linear == 1)
|
|
|
|
mOutputFile << "options_.linear = 1;" << endl;
|
|
|
|
|
|
|
|
if (model_tree.equation_number() > 0)
|
|
|
|
{
|
|
|
|
model_tree.writeOutput(mOutputFile);
|
|
|
|
model_tree.writeStaticFile(basename);
|
|
|
|
model_tree.writeDynamicFile(basename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print statements
|
|
|
|
for(vector<Statement *>::const_iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
(*it)->writeOutput(mOutputFile, basename);
|
|
|
|
|
2008-06-24 20:20:48 +02:00
|
|
|
mOutputFile << "save('" << basename << "_results.mat', 'oo_');" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile << "diary off" << endl;
|
|
|
|
|
2008-09-26 18:26:21 +02:00
|
|
|
mOutputFile << endl << "disp(['Total computing time : ' dynsec2hms(toc) ]);" << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
mOutputFile.close();
|
|
|
|
}
|