SteadyStateModel.cc 5.24 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2011 Dynare Team
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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"

25
26
SteadyStateModel::SteadyStateModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants, ExternalFunctionsTable &external_functions_table_arg, const StaticModel &static_model_arg) :
  DataTree(symbol_table_arg, num_constants, external_functions_table), static_model(static_model_arg)
27
28
29
30
{
}

void
31
SteadyStateModel::addDefinition(int symb_id, expr_t expr)
32
{
33
34
  AddVariable(symb_id); // Create the variable node to be used in write method

35
  assert(symbol_table.getType(symb_id) == eEndogenous
36
37
         || symbol_table.getType(symb_id) == eModFileLocalVariable
         || symbol_table.getType(symb_id) == eParameter);
38
39

  // Add the variable
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  vector<int> v;
  v.push_back(symb_id);
  recursive_order.push_back(v);
  def_table[v] = expr;
}

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);
    }
  recursive_order.push_back(symb_ids);
  def_table[symb_ids] = expr;
58
59
60
}

void
61
62
SteadyStateModel::checkPass(bool ramsey_policy) const
{
63
64
65
  vector<int> so_far_defined;

  for (size_t i = 0; i < recursive_order.size(); i++)
66
    {
67
68
69
70
71
72
73
74
75
76
      const vector<int> &symb_ids = recursive_order[i];

      // 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())
          {
            cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl;
            exit(EXIT_FAILURE);
          }
77
78
79
80
81

      // Check that expression has no undefined symbol
      if (!ramsey_policy)
        {
          set<pair<int, int> > used_symbols;
82
          expr_t expr = def_table.find(symb_ids)->second;
83
84
          expr->collectVariables(eEndogenous, used_symbols);
          expr->collectVariables(eModFileLocalVariable, used_symbols);
85
86
          for (set<pair<int, int> >::const_iterator it = used_symbols.begin();
               it != used_symbols.end(); ++it)
87
88
            if (find(so_far_defined.begin(), so_far_defined.end(), it->first)
                == so_far_defined.end())
89
              {
90
91
                cerr << "ERROR: in the 'steady_state' block, variable '" << symbol_table.getName(it->first)
                     << "' is undefined in the declaration of variable '" << symbol_table.getName(symb_ids[0]) << "'" << endl;
92
93
94
                exit(EXIT_FAILURE);
              }
        }
95
96

      copy(symb_ids.begin(), symb_ids.end(), back_inserter(so_far_defined));
97
98
99
100
101
    }
}

void
SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_policy) const
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
  if (recursive_order.size() == 0)
    return;

  string filename = basename + "_steadystate.m";

  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);
    }

116
117
118
119
120
121
  output << "function [ys_, check_] = " << basename << "_steadystate(";
  if (ramsey_policy)
    output << "ys_";
  else
    output << "ys_orig_";
  output << ", exo_)" << endl
122
123
         << "% Steady state generated by Dynare preprocessor" << endl;
  if (!ramsey_policy)
124
    output << "    ys_=zeros(" << symbol_table.endo_nbr() << ",1);" << endl;
125
  output << "    global M_" << endl;
126

127
  for (size_t i = 0; i < recursive_order.size(); i++)
128
    {
129
      const vector<int> &symb_ids = recursive_order[i];
130
      output << "    ";
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      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());
          dynamic_cast<ExprNode *>(it->second)->writeOutput(output, oSteadyStateFile);
          if (j < symb_ids.size()-1)
            output << ",";
        }
      if (symb_ids.size() > 1)
        output << "]";

      output << "=";
      def_table.find(symb_ids)->second->writeOutput(output, oSteadyStateFile);
146
147
      output << ";" << endl;
    }
148
149
  output << "    % Auxiliary equations" << endl;
  static_model.writeAuxVarInitval(output, oSteadyStateFile);
150
151
152
153
  output << "    check_=0;" << endl
         << "end" << endl;
}