2008-01-11 14:42:14 +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/>.
|
|
|
|
*/
|
|
|
|
|
2007-01-09 20:00:05 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
2006-11-28 12:56:02 +01:00
|
|
|
#include "ModFile.hh"
|
2006-12-12 12:54:30 +01:00
|
|
|
#include "Interface.hh"
|
2006-11-28 12:56:02 +01:00
|
|
|
|
2007-03-09 18:27:46 +01:00
|
|
|
ModFile::ModFile() : expressions_tree(symbol_table, num_constants),
|
|
|
|
model_tree(symbol_table, num_constants),
|
2006-12-15 12:44:58 +01:00
|
|
|
linear(false)
|
2006-11-28 12:56:02 +01:00
|
|
|
{
|
|
|
|
}
|
2006-12-12 12:54:30 +01:00
|
|
|
|
|
|
|
ModFile::~ModFile()
|
|
|
|
{
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModFile::addStatement(Statement *st)
|
|
|
|
{
|
|
|
|
statements.push_back(st);
|
|
|
|
}
|
|
|
|
|
2006-12-15 12:44:58 +01:00
|
|
|
void
|
|
|
|
ModFile::checkPass()
|
|
|
|
{
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
(*it)->checkPass(mod_file_struct);
|
|
|
|
|
2007-07-16 18:47:09 +02: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
|
|
|
|
|| mod_file_struct.stoch_simul_or_similar_present))
|
|
|
|
{
|
|
|
|
cerr << "Error: you must declare at least one model equation!" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2006-12-15 12:44:58 +01:00
|
|
|
if (mod_file_struct.simul_present
|
|
|
|
&& mod_file_struct.stoch_simul_or_similar_present)
|
|
|
|
{
|
2007-07-16 18:47:09 +02:00
|
|
|
cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, osr, ramsey_policy}" << endl;
|
2006-12-15 12:44:58 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
2006-12-15 18:31:31 +01:00
|
|
|
}
|
2006-12-15 12:44:58 +01:00
|
|
|
|
2006-12-15 18:31:31 +01:00
|
|
|
void
|
|
|
|
ModFile::computingPass()
|
|
|
|
{
|
2007-07-16 18:47:09 +02:00
|
|
|
// Mod file may have no equation (for example in a standalone BVAR estimation)
|
|
|
|
if (model_tree.equation_number() > 0)
|
2006-12-15 12:44:58 +01:00
|
|
|
{
|
2007-07-16 18:47:09 +02:00
|
|
|
// Set things to compute
|
|
|
|
if (mod_file_struct.simul_present)
|
|
|
|
model_tree.computeJacobian = true;
|
|
|
|
else
|
2007-02-08 12:56:46 +01:00
|
|
|
{
|
2007-07-16 18:47:09 +02:00
|
|
|
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;
|
2007-02-08 12:56:46 +01:00
|
|
|
}
|
2006-12-15 18:31:31 +01:00
|
|
|
|
2007-07-16 18:47:09 +02:00
|
|
|
model_tree.computingPass(global_eval_context);
|
|
|
|
}
|
2006-12-19 00:28:52 +01:00
|
|
|
|
|
|
|
for(vector<Statement *>::iterator it = statements.begin();
|
|
|
|
it != statements.end(); it++)
|
|
|
|
(*it)->computingPass();
|
2006-12-15 12:44:58 +01:00
|
|
|
}
|
|
|
|
|
2006-12-12 12:54:30 +01:00
|
|
|
void
|
2007-03-06 18:14:35 +01:00
|
|
|
ModFile::writeOutputFiles(const string &basename, bool clear_all) const
|
2006-12-12 12:54:30 +01:00
|
|
|
{
|
|
|
|
ofstream mOutputFile;
|
|
|
|
|
|
|
|
if (basename.size())
|
|
|
|
{
|
|
|
|
string fname(basename);
|
|
|
|
fname += interfaces::function_file_extension();
|
|
|
|
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
|
|
|
if (!mOutputFile.is_open())
|
|
|
|
{
|
|
|
|
cerr << "Error: Can't open file " << fname
|
|
|
|
<< " for writing" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Error: Missing file name" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutputFile << interfaces::comment() << endl;
|
|
|
|
mOutputFile << interfaces::comment() << "Status : main Dynare file " << endl;
|
|
|
|
mOutputFile << interfaces::comment() << endl;
|
|
|
|
mOutputFile << interfaces::comment() << "Warning : this file is generated automatically by Dynare" << endl;
|
|
|
|
mOutputFile << interfaces::comment() << " from model file (.mod)" << endl << endl;
|
|
|
|
|
|
|
|
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;
|
|
|
|
mOutputFile << interfaces::comment() << endl;
|
|
|
|
mOutputFile << interfaces::comment() << "Some global variables initialisation" << endl;
|
|
|
|
mOutputFile << interfaces::comment() << endl;
|
|
|
|
mOutputFile << "global_initialization;" << endl;
|
|
|
|
mOutputFile << "diary off;" << endl << "warning off;" << endl << endl;
|
|
|
|
mOutputFile << interfaces::delete_file(basename + ".log") << ";" << endl;
|
|
|
|
mOutputFile << "warning on;" << endl << "warning backtrace;" << endl;
|
|
|
|
mOutputFile << "logname_ = '" << basename << ".log';" << endl;
|
|
|
|
mOutputFile << "diary '" << basename << ".log';" << endl;
|
|
|
|
|
2007-07-16 18:47:09 +02:00
|
|
|
|
|
|
|
if (model_tree.equation_number() > 0)
|
2006-12-12 12:54:30 +01:00
|
|
|
{
|
2007-07-16 18:47:09 +02:00
|
|
|
if (model_tree.mode == eDLLMode)
|
|
|
|
{
|
|
|
|
mOutputFile << "if ";
|
|
|
|
mOutputFile << interfaces::file_exist(basename + "_static.c") << endl;
|
|
|
|
mOutputFile << " clear " << basename << "_static" << endl;
|
|
|
|
mOutputFile << " " << interfaces::compile(basename +"_static.c") << endl;
|
|
|
|
mOutputFile << "end" << endl;
|
|
|
|
mOutputFile << "if ";
|
|
|
|
mOutputFile << interfaces::file_exist(basename + "_dynamic.c") << endl;
|
|
|
|
mOutputFile << " clear " << basename << "_dynamic" << endl;
|
|
|
|
mOutputFile << " " + interfaces::compile(basename+"_dynamic.c") << endl;
|
|
|
|
mOutputFile << "end" << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mOutputFile << "erase_compiled_function('" + basename +"_static');" << endl;
|
|
|
|
mOutputFile << "erase_compiled_function('" + basename +"_dynamic');" << endl;
|
|
|
|
mOutputFile << interfaces::load_model_function_files(basename);
|
|
|
|
}
|
2006-12-12 12:54:30 +01:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:47:09 +02:00
|
|
|
cout << "Processing outputs ..." << endl;
|
|
|
|
|
2006-12-12 12:54:30 +01:00
|
|
|
symbol_table.writeOutput(mOutputFile);
|
|
|
|
|
|
|
|
if (linear == 1)
|
|
|
|
mOutputFile << "options_.linear = 1;" << endl;
|
|
|
|
|
2007-07-16 18:47:09 +02:00
|
|
|
if (model_tree.equation_number() > 0)
|
|
|
|
{
|
|
|
|
model_tree.writeOutput(mOutputFile);
|
|
|
|
model_tree.writeStaticFile(basename);
|
|
|
|
model_tree.writeDynamicFile(basename);
|
|
|
|
}
|
2006-12-12 12:54:30 +01:00
|
|
|
|
|
|
|
// Print statements
|
2007-01-09 20:00:05 +01:00
|
|
|
for(vector<Statement *>::const_iterator it = statements.begin();
|
2006-12-12 12:54:30 +01:00
|
|
|
it != statements.end(); it++)
|
2006-12-19 00:28:52 +01:00
|
|
|
(*it)->writeOutput(mOutputFile, basename);
|
2006-12-12 12:54:30 +01:00
|
|
|
|
|
|
|
mOutputFile << "save('" << basename << "_results', 'oo_');" << endl;
|
|
|
|
mOutputFile << "diary off" << endl;
|
|
|
|
|
|
|
|
mOutputFile << endl << "disp(['Total computing time : ' sec2hms(round(toc)) ]);" << endl;
|
|
|
|
mOutputFile.close();
|
|
|
|
}
|