File: static_fine_atoms.cc

package info (click to toggle)
dynare 5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,852 kB
  • sloc: cpp: 94,481; ansic: 28,551; pascal: 14,532; sh: 5,453; objc: 4,671; yacc: 4,442; makefile: 2,923; lex: 1,612; python: 677; ruby: 469; lisp: 156; xml: 22
file content (218 lines) | stat: -rw-r--r-- 6,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright © 2006 Ondra Kamenik
 * Copyright © 2019 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 <https://www.gnu.org/licenses/>.
 */

#include "utils/cc/exception.hh"

#include "static_fine_atoms.hh"
#include "parser_exception.hh"

using namespace ogp;

void
StaticFineAtoms::import_atoms(const FineAtoms &fa, OperationTree &otree, Tintintmap &tmap)
{
  StaticAtoms::import_atoms(fa, otree, tmap);

  // we just need to put parameters, endovars, and exovars to
  // respective vectors, the names are already in the storage

  // parameters
  auto &fa_params = fa.get_params();
  for (const auto &fa_param : fa_params)
    register_param(fa_param);

  // endogenous
  auto &fa_endovars = fa.get_endovars();
  for (const auto &fa_endovar : fa_endovars)
    register_endo(fa_endovar);

  // exogenous
  auto &fa_exovars = fa.get_exovars();
  for (const auto &fa_exovar : fa_exovars)
    register_exo(fa_exovar);

  parsing_finished();
}

void
StaticFineAtoms::import_atoms(const FineAtoms &fa, OperationTree &otree, Tintintmap &tmap,
                              const char *dummy)
{
  StaticAtoms::import_atoms(fa, otree, tmap);

  // we just need to put parameters, endovars, and exovars to
  // respective vectors, the names are already in the storage

  // parameters
  auto &fa_params = fa.get_params();
  for (const auto &fa_param : fa_params)
    register_param(fa_param);

  // endogenous
  auto &fa_endovars = fa.get_endovars();
  for (unsigned int i = 0; i < fa_endovars.size(); i++)
    register_endo(fa_endovars[fa.y2outer_endo()[i]]);

  // exogenous
  auto &fa_exovars = fa.get_exovars();
  for (unsigned int i = 0; i < fa_exovars.size(); i++)
    register_exo(fa_exovars[fa.y2outer_exo()[i]]);

  parsing_finished();
}

int
StaticFineAtoms::check_variable(const string &name) const
{
  if (!varnames.query(name))
    throw ParserException(string("Variable <")+name+"> not declared.", 0);
  return index(name);
}

void
StaticFineAtoms::parsing_finished()
{
  // build der_atoms, and endo_atoms_map and exo_atoms_map
  der_atoms.clear();
  endo_atoms_map.clear();
  exo_atoms_map.clear();

  // go through all endo and exo insert tree indices, ignore names
  // whose tree index is -1 (those which are not referenced)
  for (auto &endovar : endovars)
    {
      int t = index(endovar);
      if (t != -1)
        {
          endo_atoms_map.push_back(der_atoms.size());
          der_atoms.push_back(t);
        }
    }
  for (auto &exovar : exovars)
    {
      int t = index(exovar);
      if (t != -1)
        {
          exo_atoms_map.push_back(der_atoms.size());
          der_atoms.push_back(t);
        }
    }
}

int
StaticFineAtoms::name2outer_param(const string &name) const
{
  auto it = param_outer_map.find(name);
  if (it == param_outer_map.end())
    throw ogu::Exception(__FILE__, __LINE__,
                         "Name is not a parameter in StaticFineAtoms::name2outer_param");
  return it->second;
}

int
StaticFineAtoms::name2outer_endo(const string &name) const
{
  auto it = endo_outer_map.find(name);
  if (it == endo_outer_map.end())
    throw ogu::Exception(__FILE__, __LINE__,
                         "Name is not an endogenous variable in StaticFineAtoms::name2outer_endo");
  return it->second;
}

int
StaticFineAtoms::name2outer_exo(const string &name) const
{
  auto it = exo_outer_map.find(name);
  if (it == exo_outer_map.end())
    throw ogu::Exception(__FILE__, __LINE__,
                         "Name is not an exogenous variable in StaticFineAtoms::name2outer_exo");
  return it->second;
}

void
StaticFineAtoms::register_uniq_endo(string name)
{
  if (varnames.query(name))
    throw ogp::ParserException(string("Endogenous variable <")+name+"> is not unique.", 0);
  varnames.insert(name);
  register_endo(std::move(name));
}

void
StaticFineAtoms::register_uniq_exo(string name)
{
  if (varnames.query(name))
    throw ogp::ParserException(string("Exogenous variable <")+name+"> is not unique.", 0);
  varnames.insert(name);
  register_exo(std::move(name));
}

void
StaticFineAtoms::register_uniq_param(string name)
{
  if (varnames.query(name))
    throw ogp::ParserException(string("Parameter <")+name+"> is not unique.", 0);
  varnames.insert(name);
  register_param(std::move(name));
}

void
StaticFineAtoms::print() const
{
  StaticAtoms::print();
  std::cout << "endo atoms map:\n";
  for (unsigned int i = 0; i < endo_atoms_map.size(); i++)
    std::cout << i << u8" → " << endo_atoms_map[i] << "\n";
  std::cout << "exo atoms map:\n";
  for (unsigned int i = 0; i < exo_atoms_map.size(); i++)
    std::cout << i << u8" → " << exo_atoms_map[i] << "\n";
  std::cout << "der atoms:\n";
  for (unsigned int i = 0; i < der_atoms.size(); i++)
    std::cout << i << "\t" << der_atoms[i] << "\n";
}

void
StaticFineAtoms::register_endo(string name)
{
  if (!varnames.query(name))
    throw ogp::ParserException(string("Endogenous variable <")
                               +name+"> not found in storage.", 0);
  endovars.push_back(name);
  endo_outer_map.emplace(std::move(name), endovars.size()-1);
}

void
StaticFineAtoms::register_exo(string name)
{
  if (!varnames.query(name))
    throw ogp::ParserException(string("Exogenous variable <")
                               +name+"> not found in storage.", 0);
  exovars.push_back(name);
  exo_outer_map.emplace(std::move(name), exovars.size()-1);
}

void
StaticFineAtoms::register_param(string name)
{
  if (!varnames.query(name))
    throw ogp::ParserException(string("Parameter <")+name+"> not found in storage.", 0);
  params.push_back(name);
  param_outer_map.emplace(std::move(name), params.size()-1);
}