2010-04-23 18:39:07 +02:00
|
|
|
/*
|
2016-05-04 16:04:44 +02:00
|
|
|
* Copyright (C) 2010-2016 Dynare Team
|
2010-04-23 18:39:07 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "SteadyStateModel.hh"
|
|
|
|
|
2015-10-30 18:13:37 +01:00
|
|
|
SteadyStateModel::SteadyStateModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg, ExternalFunctionsTable &external_functions_table_arg, const StaticModel &static_model_arg) :
|
|
|
|
DataTree(symbol_table_arg, num_constants_arg, external_functions_table_arg), static_model(static_model_arg)
|
2010-04-23 18:39:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-16 19:18:45 +02:00
|
|
|
SteadyStateModel::addDefinition(int symb_id, expr_t expr)
|
2010-04-23 18:39:07 +02:00
|
|
|
{
|
2011-01-26 19:55:01 +01:00
|
|
|
AddVariable(symb_id); // Create the variable node to be used in write method
|
|
|
|
|
2010-04-23 18:39:07 +02:00
|
|
|
assert(symbol_table.getType(symb_id) == eEndogenous
|
2010-06-03 15:59:35 +02:00
|
|
|
|| symbol_table.getType(symb_id) == eModFileLocalVariable
|
|
|
|
|| symbol_table.getType(symb_id) == eParameter);
|
2010-04-23 18:39:07 +02:00
|
|
|
|
|
|
|
// Add the variable
|
2011-01-26 19:55:01 +01:00
|
|
|
vector<int> v;
|
|
|
|
v.push_back(symb_id);
|
2014-02-24 16:40:28 +01:00
|
|
|
def_table.push_back(make_pair(v, expr));
|
2011-01-26 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SteadyStateModel::addMultipleDefinitions(const vector<int> &symb_ids, expr_t expr)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < symb_ids.size(); i++)
|
|
|
|
{
|
|
|
|
AddVariable(symb_ids[i]); // Create the variable nodes to be used in write method
|
|
|
|
assert(symbol_table.getType(symb_ids[i]) == eEndogenous
|
|
|
|
|| symbol_table.getType(symb_ids[i]) == eModFileLocalVariable
|
|
|
|
|| symbol_table.getType(symb_ids[i]) == eParameter);
|
|
|
|
}
|
2014-02-24 16:40:28 +01:00
|
|
|
def_table.push_back(make_pair(symb_ids, expr));
|
2010-04-23 18:39:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-09 09:52:44 +01:00
|
|
|
SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) const
|
2010-05-31 17:43:17 +02:00
|
|
|
{
|
2014-02-25 17:58:13 +01:00
|
|
|
if (def_table.size() == 0)
|
2014-02-24 16:40:28 +01:00
|
|
|
return;
|
|
|
|
|
2011-01-26 19:55:01 +01:00
|
|
|
vector<int> so_far_defined;
|
|
|
|
|
2014-02-24 16:40:28 +01:00
|
|
|
for (size_t i = 0; i < def_table.size(); i++)
|
2010-05-31 17:43:17 +02:00
|
|
|
{
|
2014-02-24 16:40:28 +01:00
|
|
|
const vector<int> &symb_ids = def_table[i].first;
|
2011-01-26 19:55:01 +01:00
|
|
|
|
|
|
|
// 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())
|
2014-02-24 16:40:28 +01:00
|
|
|
warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl;
|
|
|
|
|
2010-05-31 17:43:17 +02:00
|
|
|
// Check that expression has no undefined symbol
|
2014-03-09 09:52:44 +01:00
|
|
|
if (!ramsey_model)
|
2010-05-31 17:43:17 +02:00
|
|
|
{
|
2013-11-29 15:32:49 +01:00
|
|
|
set<int> used_symbols;
|
2014-02-24 16:40:28 +01:00
|
|
|
const expr_t &expr = def_table[i].second;
|
2010-05-31 17:43:17 +02:00
|
|
|
expr->collectVariables(eEndogenous, used_symbols);
|
|
|
|
expr->collectVariables(eModFileLocalVariable, used_symbols);
|
2013-11-29 15:32:49 +01:00
|
|
|
for (set<int>::const_iterator it = used_symbols.begin();
|
2011-02-04 16:25:38 +01:00
|
|
|
it != used_symbols.end(); ++it)
|
2013-11-29 15:32:49 +01:00
|
|
|
if (find(so_far_defined.begin(), so_far_defined.end(), *it)
|
2011-01-26 19:55:01 +01:00
|
|
|
== so_far_defined.end())
|
2010-05-31 17:43:17 +02:00
|
|
|
{
|
2014-02-24 16:40:28 +01:00
|
|
|
cerr << "ERROR: in the 'steady_state_model' block, variable '" << symbol_table.getName(*it)
|
2011-01-26 19:55:01 +01:00
|
|
|
<< "' is undefined in the declaration of variable '" << symbol_table.getName(symb_ids[0]) << "'" << endl;
|
2010-05-31 17:43:17 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2011-01-26 19:55:01 +01:00
|
|
|
|
|
|
|
copy(symb_ids.begin(), symb_ids.end(), back_inserter(so_far_defined));
|
2010-05-31 17:43:17 +02:00
|
|
|
}
|
2014-02-24 17:27:19 +01:00
|
|
|
|
|
|
|
set<int> orig_endogs = symbol_table.getOrigEndogenous();
|
|
|
|
for (set<int>::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;
|
|
|
|
}
|
2010-05-31 17:43:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-28 17:27:56 +02:00
|
|
|
SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const
|
2010-04-23 18:39:07 +02:00
|
|
|
{
|
2014-02-24 16:40:28 +01:00
|
|
|
if (def_table.size() == 0)
|
2010-04-23 18:39:07 +02:00
|
|
|
return;
|
|
|
|
|
2015-07-28 17:27:56 +02:00
|
|
|
string filename = julia ? basename + "SteadyState2.jl" : basename + "_steadystate2.m";
|
2010-04-23 18:39:07 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:27:56 +02:00
|
|
|
ExprNodeOutputType output_type = (julia ? oJuliaSteadyStateFile : oSteadyStateFile);
|
|
|
|
|
|
|
|
if (!julia)
|
|
|
|
output << "function [ys_, params, info] = " << basename << "_steadystate2("
|
|
|
|
<< "ys_, exo_, params)" << endl
|
|
|
|
<< "% Steady state generated by Dynare preprocessor" << endl
|
|
|
|
<< " info = 0;" << endl;
|
|
|
|
else
|
2015-08-21 12:00:47 +02:00
|
|
|
output << "module " << basename << "SteadyState2" << endl
|
|
|
|
<< "#" << endl
|
|
|
|
<< "# NB: this file was automatically generated by Dynare" << endl
|
|
|
|
<< "# from " << basename << ".mod" << endl
|
|
|
|
<< "#" << endl
|
2015-08-19 11:28:25 +02:00
|
|
|
<< "export steady_state!" << endl << endl
|
2015-08-19 12:11:08 +02:00
|
|
|
<< "function steady_state!(ys_::Vector{Float64}, exo_::Vector{Float64}, "
|
|
|
|
<< "params::Vector{Float64})" << endl;
|
2010-04-23 18:39:07 +02:00
|
|
|
|
2014-02-24 16:40:28 +01:00
|
|
|
for (size_t i = 0; i < def_table.size(); i++)
|
2010-04-23 18:39:07 +02:00
|
|
|
{
|
2014-02-24 16:40:28 +01:00
|
|
|
const vector<int> &symb_ids = def_table[i].first;
|
2010-04-23 18:39:07 +02:00
|
|
|
output << " ";
|
2011-01-26 19:55:01 +01:00
|
|
|
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());
|
2015-07-28 17:27:56 +02:00
|
|
|
dynamic_cast<ExprNode *>(it->second)->writeOutput(output, output_type);
|
2011-01-26 19:55:01 +01:00
|
|
|
if (j < symb_ids.size()-1)
|
|
|
|
output << ",";
|
|
|
|
}
|
|
|
|
if (symb_ids.size() > 1)
|
|
|
|
output << "]";
|
|
|
|
|
|
|
|
output << "=";
|
2015-07-28 17:27:56 +02:00
|
|
|
def_table[i].second->writeOutput(output, output_type);
|
2010-04-23 18:39:07 +02:00
|
|
|
output << ";" << endl;
|
|
|
|
}
|
2015-07-28 17:27:56 +02:00
|
|
|
if (!julia)
|
|
|
|
output << " % Auxiliary equations" << endl;
|
|
|
|
else
|
|
|
|
output << " # Auxiliary equations" << endl;
|
2016-04-04 17:11:03 +02:00
|
|
|
static_model.writeAuxVarRecursiveDefinitions(output, output_type);
|
2015-07-28 17:27:56 +02:00
|
|
|
|
2015-08-18 16:31:30 +02:00
|
|
|
if (!julia)
|
|
|
|
output << " check_=0;" << endl;
|
|
|
|
|
2015-07-28 17:27:56 +02:00
|
|
|
output << "end" << endl;
|
|
|
|
if (julia)
|
|
|
|
output << "end" << endl;
|
2010-04-23 18:39:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:26:08 +02:00
|
|
|
void
|
|
|
|
SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const
|
|
|
|
{
|
|
|
|
string filename = basename + "_steadystate.c";
|
2013-09-17 15:11:57 +02:00
|
|
|
|
2015-07-21 17:26:08 +02:00
|
|
|
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 <math.h>" << 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<int> &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<ExprNode *>(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;
|
|
|
|
}
|