DynamicModel.hh 29.6 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
2
 * Copyright (C) 2003-2018 Dynare Team
sebastien's avatar
sebastien committed
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/>.
 */

#ifndef _DYNAMICMODEL_HH
#define _DYNAMICMODEL_HH

using namespace std;

25
#include <fstream>
26
27

#include <boost/filesystem.hpp>
28
#include <boost/crc.hpp>
29

sebastien's avatar
sebastien committed
30
31
32
33
34
#include "StaticModel.hh"

//! Stores a dynamic model
class DynamicModel : public ModelTree
{
35
36
37
38
39
public:
  //! A reference to the trend component model table
  TrendComponentModelTable &trend_component_model_table;
  //! A reference to the VAR model table
  VarModelTable &var_model_table;
sebastien's avatar
sebastien committed
40
private:
41
42
  constexpr static double zero_band{1e-8};

43
  //! Stores equations declared as [static]
44
  /*! They will be used in the conversion to StaticModel to replace equations marked as [dynamic] */
45
46
  vector<BinaryOpNode *> static_only_equations;

47
48
49
  //! Stores line numbers of equations declared as [static]
  vector<int> static_only_equations_lineno;

50
  //! Stores the equation tags of equations declared as [static]
51
  vector<vector<pair<string, string>>> static_only_equations_equation_tags;
52

53
  using deriv_id_table_t = map<pair<int, int>, int>;
54
55
56
  //! Maps a pair (symbol_id, lag) to a deriv ID
  deriv_id_table_t deriv_id_table;
  //! Maps a deriv ID to a pair (symbol_id, lag)
57
  vector<pair<int, int>> inv_deriv_id_table;
58
59
60
61
62
63

  //! Maps a deriv_id to the column index of the dynamic Jacobian
  /*! Contains only endogenous, exogenous and exogenous deterministic */
  map<int, int> dyn_jacobian_cols_table;

  //! Maximum lag and lead over all types of variables (positive values)
sebastien's avatar
sebastien committed
64
  /*! Set by computeDerivIDs() */
65
  int max_lag{0}, max_lead{0};
66
  //! Maximum lag and lead over endogenous variables (positive values)
sebastien's avatar
sebastien committed
67
  /*! Set by computeDerivIDs() */
68
  int max_endo_lag{0}, max_endo_lead{0};
69
  //! Maximum lag and lead over exogenous variables (positive values)
sebastien's avatar
sebastien committed
70
  /*! Set by computeDerivIDs() */
71
  int max_exo_lag{0}, max_exo_lead{0};
72
  //! Maximum lag and lead over deterministic exogenous variables (positive values)
sebastien's avatar
sebastien committed
73
  /*! Set by computeDerivIDs() */
74
  int max_exo_det_lag{0}, max_exo_det_lead{0};
75
  //! Maximum lag and lead over all types of variables (positive values) of original model
76
  int max_lag_orig{0}, max_lead_orig{0}, max_lag_with_diffs_expanded_orig{0};
77
  //! Maximum lag and lead over endogenous variables (positive values) of original model
78
  int max_endo_lag_orig{0}, max_endo_lead_orig{0};
79
  //! Maximum lag and lead over exogenous variables (positive values) of original model
80
  int max_exo_lag_orig{0}, max_exo_lead_orig{0};
81
  //! Maximum lag and lead over deterministic exogenous variables (positive values) of original model
82
  int max_exo_det_lag_orig{0}, max_exo_det_lead_orig{0};
83

84
85
  //! Cross reference information
  map<int, ExprNode::EquationInfo> xrefs;
86
87
88
89
  map<pair<int, int>, set<int>> xref_param;
  map<pair<int, int>, set<int>> xref_endo;
  map<pair<int, int>, set<int>> xref_exo;
  map<pair<int, int>, set<int>> xref_exo_det;
90

91
92
93
  //! Nonzero equations in the Hessian
  map<int, string> nonzero_hessian_eqs;

94
  //! Number of columns of dynamic jacobian
sebastien's avatar
sebastien committed
95
  /*! Set by computeDerivID()s and computeDynJacobianCols() */
96
  int dynJacobianColsNbr{0};
97
  //! Temporary terms for block decomposed models
98
  vector< vector<temporary_terms_t>> v_temporary_terms;
99

100
  vector<temporary_terms_inuse_t> v_temporary_terms_inuse;
101

102
103
  //! Store the derivatives or the chainrule derivatives:map<tuple<equation, variable, lead_lag>, expr_t>
  using first_chain_rule_derivatives_t = map<tuple<int, int, int>, expr_t>;
104
  first_chain_rule_derivatives_t first_chain_rule_derivatives;
105

sebastien's avatar
sebastien committed
106
  //! Writes dynamic model file (Matlab version)
107
  void writeDynamicMFile(const string &basename) const;
Houtan Bastani's avatar
Houtan Bastani committed
108
109
  //! Writes dynamic model file (Julia version)
  void writeDynamicJuliaFile(const string &dynamic_basename) const;
sebastien's avatar
sebastien committed
110
111
  //! Writes dynamic model file (C version)
  /*! \todo add third derivatives handling */
112
  void writeDynamicCFile(const string &basename, const int order) const;
sebastien's avatar
sebastien committed
113
  //! Writes dynamic model file when SparseDLL option is on
114
  void writeSparseDynamicMFile(const string &basename) const;
sebastien's avatar
sebastien committed
115
116
  //! Writes the dynamic model equations and its derivatives
  /*! \todo add third derivatives handling in C output */
Houtan Bastani's avatar
Houtan Bastani committed
117
  void writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const;
118
119
  void writeDynamicModel(const string &basename, bool use_dll, bool julia) const;
  void writeDynamicModel(const string &basename, ostream &DynamicOutput, bool use_dll, bool julia) const;
sebastien's avatar
sebastien committed
120
  //! Writes the Block reordred structure of the model in M output
121
  void writeModelEquationsOrdered_M(const string &basename) const;
sebastien's avatar
sebastien committed
122
  //! Writes the code of the Block reordred structure of the model in virtual machine bytecode
123
  void writeModelEquationsCode_Block(const string &basename, const map_idx_t &map_idx, const bool linear_decomposition) const;
124
  //! Writes the code of the model in virtual machine bytecode
125
  void writeModelEquationsCode(const string &basename, const map_idx_t &map_idx) const;
126

127
128
129
  void writeSetAuxiliaryVariables(const string &basename, const bool julia) const;
  void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const;

sebastien's avatar
sebastien committed
130
131
132
133
134
  //! Computes jacobian and prepares for equation normalization
  /*! Using values from initval/endval blocks and parameter initializations:
    - computes the jacobian for the model w.r. to contemporaneous variables
    - removes edges of the incidence matrix when derivative w.r. to the corresponding variable is too close to zero (below the cutoff)
  */
135
  //void evaluateJacobian(const eval_context_t &eval_context, jacob_map *j_m, bool dynamic);
136
137

  //! return a map on the block jacobian
138
  map<tuple<int, int, int, int, int>, int> get_Derivatives(int block);
139
  //! Computes chain rule derivatives of the Jacobian w.r. to endogenous variables
140
  void computeChainRuleJacobian(blocks_derivatives_t &blocks_derivatives);
141

sebastien's avatar
sebastien committed
142
  string reform(string name) const;
143
  map_idx_t map_idx;
sebastien's avatar
sebastien committed
144

145
  //! sorts the temporary terms in the blocks order
146
  void computeTemporaryTermsOrdered();
147
148
149

  //! creates a mapping from the index of temporary terms to a natural index
  void computeTemporaryTermsMapping();
sebastien's avatar
sebastien committed
150
  //! Write derivative code of an equation w.r. to a variable
151
  void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, int lag, const map_idx_t &map_idx) const;
152
  //! Write chain rule derivative code of an equation w.r. to a variable
153
  void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int var, int lag, const map_idx_t &map_idx) const;
sebastien's avatar
sebastien committed
154

155
  //! Get the type corresponding to a derivation ID
156
  SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override;
157
  //! Get the lag corresponding to a derivation ID
158
  int getLagByDerivID(int deriv_id) const noexcept(false) override;
159
  //! Get the symbol ID corresponding to a derivation ID
160
  int getSymbIDByDerivID(int deriv_id) const noexcept(false) override;
161
  //! Compute the column indices of the dynamic Jacobian
162
  void computeDynJacobianCols(bool jacobianExo);
163
164
  //! Computes derivatives of the Jacobian w.r. to trend vars and tests that they are equal to zero
  void testTrendDerivativesEqualToZero(const eval_context_t &eval_context);
165
  //! Collect only the first derivatives
166
  map<tuple<int, int, int>, expr_t> collect_first_order_derivatives_endogenous();
167

sebastien's avatar
sebastien committed
168
169
170
171
  //! Allocates the derivation IDs for all dynamic variables of the model
  /*! Also computes max_{endo,exo}_{lead_lag}, and initializes dynJacobianColsNbr to the number of dynamic endos */
  void computeDerivIDs();

172
173
174
  //! Collecte the derivatives w.r. to endogenous of the block, to endogenous of previouys blocks and to exogenous
  void collect_block_first_order_derivatives();

175
176
177
  //! Collecte the informations about exogenous, deterministic exogenous and endogenous from the previous block for each block
  void collectBlockVariables();

178
  //! Factorized code for substitutions of leads/lags
179
180
181
182
  /*! \param[in] type determines which type of variables is concerned
    \param[in] deterministic_model whether we are in a deterministic model (only for exogenous leads/lags)
    \param[in] subset variables to which to apply the transformation (only for diff of forward vars)
  */
183
  void substituteLeadLagInternal(AuxVarType type, bool deterministic_model, const vector<string> &subset, bool nopreprocessoroutput);
184

185
  //! Indicate if the temporary terms are computed for the overall model (true) or not (false). Default value true
186
  bool global_temporary_terms{true};
187

188
  //! Vector describing equations: BlockSimulationType, if BlockSimulationType == EVALUATE_s then a expr_t on the new normalized equation
189
  equation_type_and_normalized_equation_t equation_type_and_normalized_equation;
190

191
  //! for each block contains pair< Simulation_Type, pair < Block_Size, Recursive_part_Size >>
192
  block_type_firstequation_size_mfs_t block_type_firstequation_size_mfs;
193
194

  //! for all blocks derivatives description
195
  blocks_derivatives_t blocks_derivatives;
196
197

  //! The jacobian without the elements below the cutoff
198
  dynamic_jacob_map_t dynamic_jacobian;
199
200
201
202

  //! Vector indicating if the block is linear in endogenous variable (true) or not (false)
  vector<bool> blocks_linear;

203
204
  //! Map the derivatives for a block tuple<lag, eq, var>
  using derivative_t = map<tuple<int, int, int>, expr_t>;
205
  //! Vector of derivative for each blocks
206
  vector<derivative_t> derivative_endo, derivative_other_endo, derivative_exo, derivative_exo_det;
207

208
  //!List for each block and for each lag-lead all the other endogenous variables and exogenous variables
209
  using var_t = set<int>;
210
  using lag_var_t = map<int, var_t>;
211
  vector<lag_var_t> other_endo_block, exo_block, exo_det_block;
212

213
  //!List for each block the exogenous variables
214
  vector<pair<var_t, int>> block_var_exo;
215

216
  map< int, map<int, int>> block_exo_index, block_det_exo_index, block_other_endo_index;
217
218

  //! for each block described the number of static, forward, backward and mixed variables in the block
219
220
  /*! tuple<static, forward, backward, mixed> */
  vector<tuple<int, int, int, int>> block_col_type;
221

222
  //! Help computeXrefs to compute the reverse references (i.e. param->eqs, endo->eqs, etc)
223
  void computeRevXref(map<pair<int, int>, set<int>> &xrefset, const set<pair<int, int>> &eiref, int eqn);
224
225

  //! Write reverse cross references
226
  void writeRevXrefs(ostream &output, const map<pair<int, int>, set<int>> &xrefmap, const string &type) const;
227

228
  //! List for each variable its block number and its maximum lag and lead inside the block
229
  vector<tuple<int, int, int>> variable_block_lead_lag;
230
231
232
  //! List for each equation its block number
  vector<int> equation_block;

233
  //! Used for var_expectation and var_model
234
  map<string, set<int>> var_expectation_functions_to_write;
235

236
  //! Used for pac_expectation operator
237
  set<const PacExpectationNode *> pac_expectation_info; // PacExpectationNode pointers
238

239
  //!Maximum lead and lag for each block on endogenous of the block, endogenous of the previous blocks, exogenous and deterministic exogenous
240
  vector<pair<int, int>> endo_max_leadlag_block, other_endo_max_leadlag_block, exo_max_leadlag_block, exo_det_max_leadlag_block, max_leadlag_block;
241

Houtan Bastani's avatar
Houtan Bastani committed
242
  void writeWrapperFunctions(const string &name, const string &ending) const;
243
244
  void writeDynamicModelHelper(const string &basename,
                               const string &name, const string &retvalname,
Houtan Bastani's avatar
Houtan Bastani committed
245
246
247
248
249
250
                               const string &name_tt, size_t ttlen,
                               const string &previous_tt_name,
                               const ostringstream &init_s,
                               const ostringstream &end_s,
                               const ostringstream &s, const ostringstream &s_tt) const;

251
  //! Create a legacy *_dynamic.m file for Matlab/Octave not yet using the temporary terms array interface
252
  void writeDynamicMatlabCompatLayer(const string &basename) const;
253

254
255
256
257
  void getEquationNumbersFromTags(vector<int> &eqnumber, set<string> &eqtags) const;

  void findPacExpectationEquationNumbers(vector<int> &eqnumber) const;

258
259
260
261
262
  //! Internal helper for the copy constructor and assignment operator
  /*! Copies all the structures that contain ExprNode*, by the converting the
      pointers into their equivalent in the new tree */
  void copyHelper(const DynamicModel &m);

sebastien's avatar
sebastien committed
263
public:
Houtan Bastani's avatar
Houtan Bastani committed
264
265
266
267
268
  DynamicModel(SymbolTable &symbol_table_arg,
               NumericalConstants &num_constants_arg,
               ExternalFunctionsTable &external_functions_table_arg,
               TrendComponentModelTable &trend_component_model_table_arg,
               VarModelTable &var_model_table_arg);
269
270
271
272
273

  DynamicModel(const DynamicModel &m);
  DynamicModel(DynamicModel &&) = delete;
  DynamicModel & operator=(const DynamicModel &m);
  DynamicModel & operator=(DynamicModel &&) = delete;
274
275
276
277
278
279
280

  //! Compute cross references
  void computeXrefs();

  //! Write cross references
  void writeXrefs(ostream &output) const;

sebastien's avatar
sebastien committed
281
  //! Execute computations (variable sorting + derivation)
282
283
  /*!
    \param jacobianExo whether derivatives w.r. to exo and exo_det should be in the Jacobian (derivatives w.r. to endo are always computed)
284
    \param derivsOrder order of derivatives w.r. to exo, exo_det and endo should be computed (implies jacobianExo = true when order >= 2)
285
    \param paramsDerivsOrder order of derivatives w.r. to a pair (endo/exo/exo_det, parameter) to be computed (>0 implies jacobianExo = true)
286
287
288
    \param eval_context evaluation context for normalization
    \param no_tmp_terms if true, no temporary terms will be computed in the dynamic files
  */
289
290
  void computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsOrder,
                     const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode, bool nopreprocessoroutput, bool linear_decomposition);
sebastien's avatar
sebastien committed
291
  //! Writes model initialization and lead/lag incidence matrix to output
292
  void writeOutput(ostream &output, const string &basename, bool block, bool linear_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present, bool compute_xrefs, bool julia) const;
293

Houtan Bastani's avatar
Houtan Bastani committed
294
295
296
  //! Write JSON AST
  void writeJsonAST(ostream &output) const;

297
298
299
  //! Write JSON Output
  void writeJsonOutput(ostream &output) const;

300
301
302
  //! Write JSON Output representation of original dynamic model
  void writeJsonOriginalModelOutput(ostream &output) const;

303
304
305
  //! Write JSON Output representation of model info (useful stuff from M_)
  void writeJsonDynamicModelInfo(ostream &output) const;

306
  //! Write JSON Output representation of dynamic model after computing pass
307
  void writeJsonComputingPassOutput(ostream &output, bool writeDetails) const;
308
309

  //! Write JSON prams derivatives file
310
  void writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) const;
311

312
313
  //! Write cross reference output if the xref maps have been filed
  void writeJsonXrefs(ostream &output) const;
314
  void writeJsonXrefsHelper(ostream &output, const map<pair<int, int>, set<int>> &xrefs) const;
315

316
317
  //! Print equations that have non-zero second derivatives
  void printNonZeroHessianEquations(ostream &output) const;
318

319
320
  //! Set the equations that have non-zero second derivatives
  void setNonZeroHessianEquations(map<int, string> &eqs);
321

322
  //! Fill Autoregressive Matrix for var_model/trend_component_model
323
  void fillAutoregressiveMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ARr, bool is_trend_component_model) const;
Houtan Bastani's avatar
Houtan Bastani committed
324

325
  //! Fill Error Component Matrix for trend_component_model
326
  void fillErrorComponentMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ECr, ExprNode::subst_table_t &diff_subst_table) const;
327

328
329
330
  //! Fill the Trend Component Model Table
  void fillTrendComponentModelTable() const;
  void fillTrendComponentModelTableFromOrigModel(StaticModel &static_model) const;
331
  void fillTrendComponentmodelTableAREC(ExprNode::subst_table_t &diff_subst_table) const;
332

Houtan Bastani's avatar
Houtan Bastani committed
333
334
335
336
  //! Fill the Var Model Table
  void fillVarModelTable() const;
  void fillVarModelTableFromOrigModel(StaticModel &static_model) const;

337
  //! Update the rhs references in the var model and trend component tables
338
339
340
  //! after substitution of auxiliary variables and find the trend variables
  //! in the trend_component model
  void updateVarAndTrendModel() const;
341

Houtan Bastani's avatar
Houtan Bastani committed
342
343
344
345
  //! Add aux equations (and aux variables) for variables declared in var_model
  //! at max order if they don't already exist
  void addEquationsForVar();

346
347
  //! Get Pac equation parameter info
  void walkPacParameters();
348
  //! Add var_model info to pac_expectation nodes
Houtan Bastani's avatar
Houtan Bastani committed
349
  void fillPacExpectationVarInfo(string &pac_model_name,
350
                                 vector<int> &lhs,
Houtan Bastani's avatar
Houtan Bastani committed
351
                                 int max_lag,
352
                                 int pac_max_lag,
Houtan Bastani's avatar
Houtan Bastani committed
353
354
355
                                 vector<bool> &nonstationary,
                                 int growth_symb_id);

356
357
358
  //! Substitutes pac_expectation operator
  void substitutePacExpectation();

sebastien's avatar
sebastien committed
359
  //! Adds informations for simulation in a binary file
360
  void Write_Inf_To_Bin_File_Block(const string &basename,
361
                                   const int &num, int &u_count_int, bool &file_open, bool is_two_boundaries, const bool linear_decomposition) const;
sebastien's avatar
sebastien committed
362
  //! Writes dynamic model file
363
  void writeDynamicFile(const string &basename, bool block, bool linear_decomposition, bool bytecode, bool use_dll, const string &mexext, const boost::filesystem::path &matlabroot, const boost::filesystem::path &dynareroot, int order, bool julia) const;
sebastien's avatar
sebastien committed
364
  //! Writes file containing parameters derivatives
365
  void writeParamsDerivativesFile(const string &basename, bool julia) const;
MichelJuillard's avatar
MichelJuillard committed
366

367
368
369
370
  //! Converts to nonlinear model (only the equations)
  /*! It assumes that the nonlinear model given in argument has just been allocated */
  void toNonlinearPart(DynamicModel &non_linear_equations_dynamic_model) const;

371
  //! Find endogenous variables not used in model
372
  set<int> findUnusedEndogenous();
373
374
  //! Find exogenous variables not used in model
  set<int> findUnusedExogenous();
375

376
377
378
  //! Set the max leads/lags of the original model
  void setLeadsLagsOrig();

379
  //! Replaces model equations with derivatives of Lagrangian w.r.t. endogenous
380
  void computeRamseyPolicyFOCs(const StaticModel &static_model, const bool nopreprocessoroutput);
Houtan Bastani's avatar
Houtan Bastani committed
381
  //! Replaces the model equations in dynamic_model with those in this model
382
383
  void replaceMyEquations(DynamicModel &dynamic_model) const;

384
  //! Adds an equation marked as [static]
385
  void addStaticOnlyEquation(expr_t eq, int lineno, const vector<pair<string, string>> &eq_tags);
386
387
388

  //! Returns number of static only equations
  size_t staticOnlyEquationsNbr() const;
389

390
391
392
  //! Returns number of dynamic only equations
  size_t dynamicOnlyEquationsNbr() const;

393
  //! Writes LaTeX file with the equations of the dynamic model
394
  void writeLatexFile(const string &basename, const bool write_equation_tags) const;
395

396
  //! Writes LaTeX file with the equations of the dynamic model (for the original model)
397
  void writeLatexOriginalFile(const string &basename, const bool write_equation_tags) const;
398

399
400
401
  int getDerivID(int symb_id, int lag) const noexcept(false) override;
  int getDynJacobianCol(int deriv_id) const noexcept(false) override;
  void addAllParamDerivId(set<int> &deriv_id_set) override;
402

403
  //! Returns true indicating that this is a dynamic model
404
405
  bool
  isDynamic() const override
406
407
408
  {
    return true;
  };
sebastien's avatar
sebastien committed
409

410
411
412
  //! Drive test of detrended equations
  void runTrendTest(const eval_context_t &eval_context);

413
  //! Transforms the model by removing all leads greater or equal than 2 on endos
sebastien's avatar
sebastien committed
414
  /*! Note that this can create new lags on endos and exos */
415
  void substituteEndoLeadGreaterThanTwo(bool deterministic_model, bool nopreprocessoroutput);
sebastien's avatar
sebastien committed
416

417
  //! Transforms the model by removing all lags greater or equal than 2 on endos
418
  void substituteEndoLagGreaterThanTwo(bool deterministic_model, bool nopreprocessoroutput);
419

sebastien's avatar
sebastien committed
420
421
  //! Transforms the model by removing all leads on exos
  /*! Note that this can create new lags on endos and exos */
422
  void substituteExoLead(bool deterministic_model, bool nopreprocessoroutput);
sebastien's avatar
sebastien committed
423
424

  //! Transforms the model by removing all lags on exos
425
  void substituteExoLag(bool deterministic_model, bool nopreprocessoroutput);
sebastien's avatar
sebastien committed
426

427
  //! Transforms the model by removing all UnaryOpcode::expectation
428
  void substituteExpectation(bool partial_information_model, bool nopreprocessoroutput);
429

430
431
432
  //! Transforms the model by decreasing the lead/lag of predetermined variables in model equations by one
  void transformPredeterminedVariables();

433
434
435
  //! Transforms the model by removing trends specified by the user
  void detrendEquations();

436
437
438
  //! Substitutes adl operator
  void substituteAdl();

439
  //! Creates aux vars for all unary operators
440
  void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, bool nopreprocessoroutput);
441

442
  //! Creates aux vars for certain unary operators: originally implemented for support of VARs
443
  void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, set<string> &eq_tags, bool nopreprocessoroutput);
444

445
  //! Creates aux vars for certain unary operators: originally implemented for support of VARs
446
  void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, vector<int> &eqnumbers, bool nopreprocessoroutput);
447

448
  //! Substitutes diff operator
449
  void substituteDiff(StaticModel &static_model, diff_table_t &diff_table, ExprNode::subst_table_t &diff_subst_table, bool nopreprocessoroutput);
Houtan Bastani's avatar
Houtan Bastani committed
450

451
452
453
  //! Substitute VarExpectation operators
  void substituteVarExpectation(const map<string, expr_t> &subst_table);

454
455
456
  //! Return max lag of pac equation
  int getPacMaxLag(const string &pac_model_name) const;

Houtan Bastani's avatar
Houtan Bastani committed
457
  //! Table to undiff LHS variables for pac vector z
458
  vector<int> getUndiffLHSForPac(const string &aux_model_name,
459
                                 ExprNode::subst_table_t &diff_subst_table) const;
460

461
462
463
  //! Transforms the model by replacing trend variables with a 1
  void removeTrendVariableFromEquations();

464
  //! Transforms the model by creating aux vars for the diff of forward vars
465
  /*! If subset is empty, does the transformation for all fwrd vars; otherwise
466
    restrict it to the vars in subset */
467
  void differentiateForwardVars(const vector<string> &subset, bool nopreprocessoroutput);
468

sebastien's avatar
sebastien committed
469
  //! Fills eval context with values of model local variables and auxiliary variables
470
  void fillEvalContext(eval_context_t &eval_context) const;
471

472
473
  auto getStaticOnlyEquationsInfo() const { return make_tuple(static_only_equations, static_only_equations_lineno, static_only_equations_equation_tags); };

474
  //! Return the number of blocks
475
476
  unsigned int
  getNbBlocks() const override
477
478
479
  {
    return (block_type_firstequation_size_mfs.size());
  };
480
  //! Determine the simulation type of each block
481
482
  BlockSimulationType
  getBlockSimulationType(int block_number) const override
483
  {
484
    return (get<0>(block_type_firstequation_size_mfs[block_number]));
485
  };
486
  //! Return the first equation number of a block
487
488
  unsigned int
  getBlockFirstEquation(int block_number) const override
489
  {
490
    return (get<1>(block_type_firstequation_size_mfs[block_number]));
491
  };
492
  //! Return the size of the block block_number
493
494
  unsigned int
  getBlockSize(int block_number) const override
495
  {
496
    return (get<2>(block_type_firstequation_size_mfs[block_number]));
497
  };
498
  //! Return the number of exogenous variable in the block block_number
499
500
  unsigned int
  getBlockExoSize(int block_number) const override
501
502
503
504
  {
    return (block_var_exo[block_number].first.size());
  };
  //! Return the number of colums in the jacobian matrix for exogenous variable in the block block_number
505
506
  unsigned int
  getBlockExoColSize(int block_number) const override
507
508
509
  {
    return (block_var_exo[block_number].second);
  };
510
  //! Return the number of feedback variable of the block block_number
511
512
  unsigned int
  getBlockMfs(int block_number) const override
513
  {
514
    return (get<3>(block_type_firstequation_size_mfs[block_number]));
515
  };
516
  //! Return the maximum lag in a block
517
518
  unsigned int
  getBlockMaxLag(int block_number) const override
519
520
521
  {
    return (block_lag_lead[block_number].first);
  };
522
  //! Return the maximum lead in a block
523
524
  unsigned int
  getBlockMaxLead(int block_number) const override
525
526
527
  {
    return (block_lag_lead[block_number].second);
  };
528
  //! Return the type of equation (equation_number) belonging to the block block_number
529
530
  EquationType
  getBlockEquationType(int block_number, int equation_number) const override
531
  {
532
    return (equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first);
533
  };
534
  //! Return true if the equation has been normalized
535
536
  bool
  isBlockEquationRenormalized(int block_number, int equation_number) const override
537
  {
538
    return (equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].first == E_EVALUATE_S);
539
  };
540
  //! Return the expr_t of the equation equation_number belonging to the block block_number
541
542
  expr_t
  getBlockEquationExpr(int block_number, int equation_number) const override
543
  {
544
    return (equations[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]]);
545
  };
546
  //! Return the expr_t of the renormalized equation equation_number belonging to the block block_number
547
548
  expr_t
  getBlockEquationRenormalizedExpr(int block_number, int equation_number) const override
549
  {
550
    return (equation_type_and_normalized_equation[equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]].second);
551
  };
552
  //! Return the original number of equation equation_number belonging to the block block_number
553
554
  int
  getBlockEquationID(int block_number, int equation_number) const override
555
  {
556
    return (equation_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+equation_number]);
557
  };
558
  //! Return the original number of variable variable_number belonging to the block block_number
559
560
  int
  getBlockVariableID(int block_number, int variable_number) const override
561
  {
562
    return (variable_reordered[get<1>(block_type_firstequation_size_mfs[block_number])+variable_number]);
563
  };
564
  //! Return the original number of the exogenous variable varexo_number belonging to the block block_number
565
566
  int
  getBlockVariableExoID(int block_number, int variable_number) const override
567
  {
568
    auto it = exo_block[block_number].find(variable_number);
569
570
    return (it->first);
  };
571
  //! Return the position of equation_number in the block number belonging to the block block_number
572
573
  int
  getBlockInitialEquationID(int block_number, int equation_number) const override
574
  {
575
    return ((int) inv_equation_reordered[equation_number] - (int) get<1>(block_type_firstequation_size_mfs[block_number]));
576
  };
577
  //! Return the position of variable_number in the block number belonging to the block block_number
578
579
  int
  getBlockInitialVariableID(int block_number, int variable_number) const override
580
  {
581
    return ((int) inv_variable_reordered[variable_number] - (int) get<1>(block_type_firstequation_size_mfs[block_number]));
582
  };
583
584
585
586
  //! Return the block number containing the endogenous variable variable_number
  int
  getBlockVariableID(int variable_number) const
  {
587
    return (get<0>(variable_block_lead_lag[variable_number]));
588
589
  };
  //! Return the position of the exogenous variable_number in the block number belonging to the block block_number
590
591
  int
  getBlockInitialExogenousID(int block_number, int variable_number) const override
592
  {
593
    auto it = block_exo_index.find(block_number);
594
595
    if (it != block_exo_index.end())
      {
596
        auto it1 = it->second.find(variable_number);
597
        if (it1 != it->second.end())
598
599
600
601
602
603
604
605
          return it1->second;
        else
          return -1;
      }
    else
      return (-1);
  };
  //! Return the position of the deterministic exogenous variable_number in the block number belonging to the block block_number
606
607
  int
  getBlockInitialDetExogenousID(int block_number, int variable_number) const override
608
  {
609
    auto it = block_det_exo_index.find(block_number);
610
611
    if (it != block_det_exo_index.end())
      {
612
        auto it1 = it->second.find(variable_number);
613
        if (it1 != it->second.end())
614
615
616
617
618
619
620
621
          return it1->second;
        else
          return -1;
      }
    else
      return (-1);
  };
  //! Return the position of the other endogenous variable_number in the block number belonging to the block block_number
622
623
  int
  getBlockInitialOtherEndogenousID(int block_number, int variable_number) const override
624
  {
625
    auto it = block_other_endo_index.find(block_number);
626
627
    if (it != block_other_endo_index.end())
      {
628
        auto it1 = it->second.find(variable_number);
629
        if (it1 != it->second.end())
630
631
632
633
634
635
636
          return it1->second;
        else
          return -1;
      }
    else
      return (-1);
  };
637
  bool isModelLocalVariableUsed() const;
638

639
640
641
  //! Returns true if a parameter was used in the model block with a lead or lag
  bool ParamUsedWithLeadLag() const;

642
  bool isChecksumMatching(const string &basename, bool block) const;
sebastien's avatar
sebastien committed
643
644
};

645
//! Classes to re-order derivatives for various sparse storage formats
646
class derivative
647
648
{
public:
649
650
651
652
  long unsigned int linear_address;
  long unsigned int col_nbr;
  unsigned int row_nbr;
  expr_t value;
653
654
655
656
  derivative(long unsigned int arg1, long unsigned int arg2, int arg3, expr_t arg4) :
    linear_address(arg1), col_nbr(arg2), row_nbr(arg3), value(arg4)
  {
  };
657
658
659
660
661
};

class derivative_less_than
{
public:
662
663
  bool
  operator()(const derivative &d1, const derivative &d2) const
664
665
666
  {
    return d1.linear_address < d2.linear_address;
  }
667
};
sebastien's avatar
sebastien committed
668
#endif