StaticModel.hh 15.4 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
Houtan Bastani's avatar
Houtan Bastani committed
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
 *
 * 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/>.
 */

20 21 22 23 24 25
#ifndef _STATIC_MODEL_HH
#define _STATIC_MODEL_HH

using namespace std;

#include <fstream>
sebastien's avatar
sebastien committed
26 27 28

#include "ModelTree.hh"

29
//! Stores a static model, as derived from the "model" block when leads and lags have been removed
sebastien's avatar
sebastien committed
30 31 32
class StaticModel : public ModelTree
{
private:
33
  //! global temporary terms for block decomposed models
34
  vector<vector<temporary_terms_t>> v_temporary_terms;
35

36
  //! local temporary terms for block decomposed models
37
  vector<vector<temporary_terms_t>> v_temporary_terms_local;
38

39
  vector<temporary_terms_inuse_t> v_temporary_terms_inuse;
40

41
  using first_chain_rule_derivatives_t = map< pair< int, pair< int, int>>, expr_t>;
42
  first_chain_rule_derivatives_t first_chain_rule_derivatives;
43

44
  //! Writes static model file (standard Matlab version)
45
  void writeStaticMFile(const string &basename) const;
46

47
  //! Writes static model file (C version)
48
  void writeStaticCFile(const string &basename) const;
49

Houtan Bastani's avatar
Houtan Bastani committed
50
  //! Writes static model file (Julia version)
51
  void writeStaticJuliaFile(const string &basename) const;
Houtan Bastani's avatar
Houtan Bastani committed
52

53
  //! Writes the static model equations and its derivatives
Houtan Bastani's avatar
Houtan Bastani committed
54
  void writeStaticModel(const string &basename, ostream &StaticOutput, bool use_dll, bool julia) const;
55

56 57
  //! Writes the static function calling the block to solve (Matlab version)
  void writeStaticBlockMFSFile(const string &basename) const;
sebastien's avatar
sebastien committed
58

59
  //! Writes the Block reordred structure of the model in M output
60
  void writeModelEquationsOrdered_M(const string &basename) const;
61

62
  //! Writes the code of the Block reordred structure of the model in virtual machine bytecode
63
  void writeModelEquationsCode_Block(const string &basename, map_idx_t map_idx, vector<map_idx_t> map_idx2) const;
64 65

  //! Writes the code of the model in virtual machine bytecode
66
  void writeModelEquationsCode(const string &basename, map_idx_t map_idx) const;
67

68 69 70 71 72
  //! 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)
  */
73
  void evaluateJacobian(const eval_context_t &eval_context, jacob_map_t *j_m, bool dynamic);
74

75
  map_idx_t map_idx;
sebastien's avatar
sebastien committed
76

77 78
  vector<map_idx_t> map_idx2;

79
  //! sorts the temporary terms in the blocks order
80
  void computeTemporaryTermsOrdered();
81
  //! creates a mapping from the index of temporary terms to a natural index
82
  void computeTemporaryTermsMapping(temporary_terms_t &temporary_terms, map_idx_t &map_idx);
83

84
  //! Write derivative code of an equation w.r. to a variable
85
  void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, map_idx_t &map_idx, temporary_terms_t temporary_terms) const;
86
  //! Write chain rule derivative code of an equation w.r. to a variable
87
  void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int var, int lag, map_idx_t &map_idx, temporary_terms_t temporary_terms) const;
88

89
  //! Get the type corresponding to a derivation ID
90
  SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override;
91
  //! Get the lag corresponding to a derivation ID
92
  int getLagByDerivID(int deriv_id) const noexcept(false) override;
93
  //! Get the symbol ID corresponding to a derivation ID
94
  int getSymbIDByDerivID(int deriv_id) const noexcept(false) override;
95 96 97
  //! Compute the column indices of the static Jacobian
  void computeStatJacobianCols();
  //! return a map on the block jacobian
98
  map<pair<pair<int, pair<int, int>>, pair<int, int>>, int> get_Derivatives(int block);
99
  //! Computes chain rule derivatives of the Jacobian w.r. to endogenous variables
100
  void computeChainRuleJacobian(blocks_derivatives_t &blocks_derivatives);
101
  //! Collect only the first derivatives
102
  map<pair<int, pair<int, int>>, expr_t> collect_first_order_derivatives_endogenous();
103 104 105 106 107 108 109 110

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

protected:
  //! Indicate if the temporary terms are computed for the overall model (true) or not (false). Default value true
  bool global_temporary_terms;

111
  //! Vector describing equations: BlockSimulationType, if BlockSimulationType == EVALUATE_s then a expr_t on the new normalized equation
112
  equation_type_and_normalized_equation_t equation_type_and_normalized_equation;
113

114
  //! for each block contains pair< Simulation_Type, pair < Block_Size, Recursive_part_Size >>
115
  block_type_firstequation_size_mfs_t block_type_firstequation_size_mfs;
116 117

  //! for all blocks derivatives description
118
  blocks_derivatives_t blocks_derivatives;
119 120

  //! The jacobian without the elements below the cutoff
121
  dynamic_jacob_map_t dynamic_jacobian;
122 123 124 125

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

126
  //! Map the derivatives for a block pair<lag, make_pair(make_pair(eq, var)), expr_t>
127
  using derivative_t = map<pair< int, pair<int, int>>, expr_t>;
128
  //! Vector of derivative for each blocks
129
  vector<derivative_t> derivative_endo, derivative_other_endo, derivative_exo, derivative_exo_det;
130 131

  //!List for each block and for each lag-leag all the other endogenous variables and exogenous variables
132
  using var_t = set<int>;
133
  using lag_var_t = map<int, var_t>;
134
  vector<lag_var_t> other_endo_block, exo_block, exo_det_block;
135

136
  //! for each block described the number of static, forward, backward and mixed variables in the block
137 138
  /*! pair< pair<static, forward>, pair<backward,mixed>> */
  vector<pair< pair<int, int>, pair<int, int>>> block_col_type;
139 140

  //! List for each variable its block number and its maximum lag and lead inside the block
141
  vector<pair<int, pair<int, int>>> variable_block_lead_lag;
142 143 144
  //! List for each equation its block number
  vector<int> equation_block;

145
  //!Maximum lead and lag for each block on endogenous of the block, endogenous of the previous blocks, exogenous and deterministic exogenous
146
  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;
147

Houtan Bastani's avatar
Houtan Bastani committed
148
  //! Helper functions for writeStaticModel
149 150
  void writeStaticModelHelper(const string &basename,
                              const string &name, const string &retvalname,
Houtan Bastani's avatar
Houtan Bastani committed
151 152 153 154 155
                              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;
  void writeWrapperFunctions(const string &basename, const string &ending) const;
156 157 158 159

  //! Create a legacy *_static.m file for Matlab/Octave not yet using the temporary terms array interface
  void writeStaticMatlabCompatLayer(const string &name) const;

Houtan Bastani's avatar
Houtan Bastani committed
160 161
  void writeStaticModel(ostream &DynamicOutput, bool use_dll, bool julia) const;
  void writeStaticModel(const string &dynamic_basename, bool use_dll, bool julia) const;
sebastien's avatar
sebastien committed
162
public:
163 164
  StaticModel(SymbolTable &symbol_table_arg,
              NumericalConstants &num_constants,
165
              ExternalFunctionsTable &external_functions_table_arg);
166 167

  //! Writes information on block decomposition when relevant
168
  void writeOutput(ostream &output, bool block) const;
169

170 171 172 173
  //! Execute computations (variable sorting + derivation)
  /*!
    \param eval_context evaluation context for normalization
    \param no_tmp_terms if true, no temporary terms will be computed in the static files
174
    \param hessian whether 2nd derivatives w.r. to exo, exo_det and endo should be computed
175
    \param paramsDerivsOrder order of derivatives w.r. to a pair (endo/exo/exo_det, parameter) to be computed
176
  */
177
  void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatices, int paramsDerivsOrder, bool block, bool bytecode, const bool nopreprocessoroutput);
178

179
  //! Adds informations for simulation in a binary file for a block decomposed model
180
  void Write_Inf_To_Bin_File_Block(const string &basename, const int &num,
181
                                   int &u_count_int, bool &file_open) const;
182

sebastien's avatar
sebastien committed
183
  //! Writes static model file
184
  void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, bool julia) const;
185

186 187 188
  //! Write JSON Output (used by PlannerObjectiveStatement)
  void writeJsonOutput(ostream &output) const;

189
  //! Write JSON representation of static model
190
  void writeJsonComputingPassOutput(ostream &output, bool writeDetails) const;
191 192

  //! Writes file containing static parameters derivatives
193
  void writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) const;
194

195
  //! Writes file containing static parameters derivatives
196
  void writeParamsDerivativesFile(const string &basename, bool julia) const;
197

198
  //! Writes LaTeX file with the equations of the static model
199
  void writeLatexFile(const string &basename, const bool write_equation_tags) const;
200

201 202
  //! Writes initializations in oo_.steady_state or steady state file for the auxiliary variables
  void writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const;
sebastien's avatar
sebastien committed
203

204
  //! Writes definition of the auxiliary variables in a .m or .jl file
205 206
  void writeSetAuxiliaryVariables(const string &basename, const bool julia) const;
  void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const;
207
  void writeLatexAuxVarRecursiveDefinitions(ostream &output) const;
208
  void writeJsonAuxVarRecursiveDefinitions(ostream &output) const;
209

210 211 212 213
  //! To ensure that no exogenous is present in the planner objective
  //! See #1264
  bool exoPresentInEqs() const;

214 215
  int getDerivID(int symb_id, int lag) const noexcept(false) override;
  void addAllParamDerivId(set<int> &deriv_id_set) override;
216 217

  //! Return the number of blocks
218 219
  unsigned int
  getNbBlocks() const override
220 221 222
  {
    return (block_type_firstequation_size_mfs.size());
  };
223
  //! Determine the simulation type of each block
224 225
  BlockSimulationType
  getBlockSimulationType(int block_number) const override
226 227 228
  {
    return (block_type_firstequation_size_mfs[block_number].first.first);
  };
229
  //! Return the first equation number of a block
230 231
  unsigned int
  getBlockFirstEquation(int block_number) const override
232 233 234
  {
    return (block_type_firstequation_size_mfs[block_number].first.second);
  };
235
  //! Return the size of the block block_number
236 237
  unsigned int
  getBlockSize(int block_number) const override
238 239 240
  {
    return (block_type_firstequation_size_mfs[block_number].second.first);
  };
241
  //! Return the number of exogenous variable in the block block_number
242 243
  unsigned int
  getBlockExoSize(int block_number) const override
244 245 246 247
  {
    return 0;
  };
  //! Return the number of colums in the jacobian matrix for exogenous variable in the block block_number
248 249
  unsigned int
  getBlockExoColSize(int block_number) const override
250 251 252
  {
    return 0;
  }
253
  //! Return the number of feedback variable of the block block_number
254 255
  unsigned int
  getBlockMfs(int block_number) const override
256 257 258
  {
    return (block_type_firstequation_size_mfs[block_number].second.second);
  };
259
  //! Return the maximum lag in a block
260 261
  unsigned int
  getBlockMaxLag(int block_number) const override
262 263 264
  {
    return (block_lag_lead[block_number].first);
  };
265
  //! Return the maximum lead in a block
266 267
  unsigned int
  getBlockMaxLead(int block_number) const override
268 269 270
  {
    return (block_lag_lead[block_number].second);
  };
271
  //! Return the type of equation (equation_number) belonging to the block block_number
272 273
  EquationType
  getBlockEquationType(int block_number, int equation_number) const override
274 275 276
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].first);
  };
277
  //! Return true if the equation has been normalized
278 279
  bool
  isBlockEquationRenormalized(int block_number, int equation_number) const override
280 281 282
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].first == E_EVALUATE_S);
  };
283
  //! Return the expr_t of the equation equation_number belonging to the block block_number
284 285
  expr_t
  getBlockEquationExpr(int block_number, int equation_number) const override
286 287 288
  {
    return (equations[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]]);
  };
289
  //! Return the expr_t of the renormalized equation equation_number belonging to the block block_number
290 291
  expr_t
  getBlockEquationRenormalizedExpr(int block_number, int equation_number) const override
292 293 294
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].second);
  };
295
  //! Return the original number of equation equation_number belonging to the block block_number
296 297
  int
  getBlockEquationID(int block_number, int equation_number) const override
298 299 300
  {
    return (equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]);
  };
301
  //! Return the original number of variable variable_number belonging to the block block_number
302 303
  int
  getBlockVariableID(int block_number, int variable_number) const override
304 305 306
  {
    return (variable_reordered[block_type_firstequation_size_mfs[block_number].first.second+variable_number]);
  };
307
  //! Return the original number of the exogenous variable varexo_number belonging to the block block_number
308 309
  int
  getBlockVariableExoID(int block_number, int variable_number) const override
310 311 312
  {
    return 0;
  };
313
  //! Return the position of equation_number in the block number belonging to the block block_number
314 315
  int
  getBlockInitialEquationID(int block_number, int equation_number) const override
316 317 318
  {
    return ((int) inv_equation_reordered[equation_number] - (int) block_type_firstequation_size_mfs[block_number].first.second);
  };
319
  //! Return the position of variable_number in the block number belonging to the block block_number
320 321
  int
  getBlockInitialVariableID(int block_number, int variable_number) const override
322 323 324
  {
    return ((int) inv_variable_reordered[variable_number] - (int) block_type_firstequation_size_mfs[block_number].first.second);
  };
325
  //! Return the position of variable_number in the block number belonging to the block block_number
326 327
  int
  getBlockInitialExogenousID(int block_number, int variable_number) const override
328 329 330 331
  {
    return -1;
  };
  //! Return the position of the deterministic exogenous variable_number in the block number belonging to the block block_number
332 333
  int
  getBlockInitialDetExogenousID(int block_number, int variable_number) const override
334 335 336 337
  {
    return -1;
  };
  //! Return the position of the other endogenous variable_number in the block number belonging to the block block_number
338 339
  int
  getBlockInitialOtherEndogenousID(int block_number, int variable_number) const override
340 341 342
  {
    return -1;
  };
sebastien's avatar
sebastien committed
343 344 345
};

#endif