StaticModel.hh 14.2 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
2
 * Copyright (C) 2003-2015 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
  //! Temporary terms for the file containing parameters dervicatives
34
  temporary_terms_t params_derivs_temporary_terms;
35

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

39 40 41
  //! local temporary terms for block decomposed models
  vector<vector<temporary_terms_t> > v_temporary_terms_local;

42
  vector<temporary_terms_inuse_t> v_temporary_terms_inuse;
43

44
  typedef map< pair< int, pair< int, int> >, expr_t> first_chain_rule_derivatives_t;
45
  first_chain_rule_derivatives_t first_chain_rule_derivatives;
46

47
  //! Writes static model file (standard Matlab version)
48 49
  void writeStaticMFile(const string &static_basename) const;

50 51 52
  //! Writes static model file (C version)
  void writeStaticCFile(const string &func_name) const;

Houtan Bastani's avatar
Houtan Bastani committed
53
  //! Writes static model file (Julia version)
54
  void writeStaticJuliaFile(const string &basename) const;
Houtan Bastani's avatar
Houtan Bastani committed
55

56
  //! Writes the static model equations and its derivatives
Houtan Bastani's avatar
Houtan Bastani committed
57
  void writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) const;
58

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

62 63
  //! Writes the Block reordred structure of the model in M output
  void writeModelEquationsOrdered_M(const string &dynamic_basename) const;
64

65
  //! Writes the code of the Block reordred structure of the model in virtual machine bytecode
66
  void writeModelEquationsCode_Block(const string file_name, const string bin_basename, map_idx_t map_idx, vector<map_idx_t> map_idx2) const;
67 68

  //! Writes the code of the model in virtual machine bytecode
69
  void writeModelEquationsCode(const string file_name, const string bin_basename, map_idx_t map_idx) const;
70

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

78
  map_idx_t map_idx;
sebastien's avatar
sebastien committed
79

80 81
  vector<map_idx_t> map_idx2;

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

87
  //! Write derivative code of an equation w.r. to a variable
88
  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;
89
  //! Write chain rule derivative code of an equation w.r. to a variable
90
  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;
91

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

  //! Write chain rule derivative of a recursive equation w.r. to a variable
108
  void writeChainRuleDerivative(ostream &output, int eq, int var, int lag, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const;
109 110 111 112 113 114 115 116

  //! 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;

117
  //! Vector describing equations: BlockSimulationType, if BlockSimulationType == EVALUATE_s then a expr_t on the new normalized equation
118
  equation_type_and_normalized_equation_t equation_type_and_normalized_equation;
119 120

  //! for each block contains pair< Simulation_Type, pair < Block_Size, Recursive_part_Size > >
121
  block_type_firstequation_size_mfs_t block_type_firstequation_size_mfs;
122 123

  //! for all blocks derivatives description
124
  blocks_derivatives_t blocks_derivatives;
125 126

  //! The jacobian without the elements below the cutoff
127
  dynamic_jacob_map_t dynamic_jacobian;
128 129 130 131

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

132 133
  //! Map the derivatives for a block pair<lag, make_pair(make_pair(eq, var)), expr_t>
  typedef map<pair< int, pair<int, int> >, expr_t> derivative_t;
134
  //! Vector of derivative for each blocks
135
  vector<derivative_t> derivative_endo, derivative_other_endo, derivative_exo, derivative_exo_det;
136 137

  //!List for each block and for each lag-leag all the other endogenous variables and exogenous variables
138 139 140
  typedef set<int> var_t;
  typedef map<int, var_t> lag_var_t;
  vector<lag_var_t> other_endo_block, exo_block, exo_det_block;
141

142 143
  //! for each block described the number of static, forward, backward and mixed variables in the block
  /*! pair< pair<static, forward>, pair<backward,mixed> > */
144
  vector<pair< pair<int, int>, pair<int, int> > > block_col_type;
145 146 147 148 149 150

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

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

sebastien's avatar
sebastien committed
154
public:
155
  StaticModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants, ExternalFunctionsTable &external_functions_table_arg);
156 157

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

160 161 162 163
  //! 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
164
    \param hessian whether 2nd derivatives w.r. to exo, exo_det and endo should be computed
165
    \param paramsDerivatives whether 2nd derivatives w.r. to a pair (endo/exo/exo_det, parameter) should be computed
166
  */
167
  void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatices, bool paramsDerivatives, bool block, bool bytecode);
168

169 170
  //! Adds informations for simulation in a binary file for a block decomposed model
  void Write_Inf_To_Bin_File_Block(const string &static_basename, const string &bin_basename, const int &num,
171
                                   int &u_count_int, bool &file_open) const;
172

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

176
  //! Writes file containing static parameters derivatives
177
  void writeParamsDerivativesFile(const string &basename, bool julia) const;
178

179 180 181
  //! Writes LaTeX file with the equations of the static model
  void writeLatexFile(const string &basename) const;

182 183
  //! 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
184

185 186
  //! Writes definition of the auxiliary variables in a .m or .jl file
  void writeAuxVarRecursiveDefinitions(const string &basename, const bool julia) const;
187

188
  virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
189
  virtual void addAllParamDerivId(set<int> &deriv_id_set);
190 191

  //! Return the number of blocks
192 193 194 195 196
  virtual unsigned int
  getNbBlocks() const
  {
    return (block_type_firstequation_size_mfs.size());
  };
197
  //! Determine the simulation type of each block
198 199 200 201 202
  virtual BlockSimulationType
  getBlockSimulationType(int block_number) const
  {
    return (block_type_firstequation_size_mfs[block_number].first.first);
  };
203
  //! Return the first equation number of a block
204 205 206 207 208
  virtual unsigned int
  getBlockFirstEquation(int block_number) const
  {
    return (block_type_firstequation_size_mfs[block_number].first.second);
  };
209
  //! Return the size of the block block_number
210 211 212 213 214
  virtual unsigned int
  getBlockSize(int block_number) const
  {
    return (block_type_firstequation_size_mfs[block_number].second.first);
  };
215 216 217 218 219 220 221 222 223 224
  //! Return the number of exogenous variable in the block block_number
  virtual unsigned int getBlockExoSize(int block_number) const
  {
    return 0;
  };
  //! Return the number of colums in the jacobian matrix for exogenous variable in the block block_number
  virtual unsigned int getBlockExoColSize(int block_number) const
  {
    return 0;
  }
225
  //! Return the number of feedback variable of the block block_number
226 227 228 229 230
  virtual unsigned int
  getBlockMfs(int block_number) const
  {
    return (block_type_firstequation_size_mfs[block_number].second.second);
  };
231
  //! Return the maximum lag in a block
232 233 234 235 236
  virtual unsigned int
  getBlockMaxLag(int block_number) const
  {
    return (block_lag_lead[block_number].first);
  };
237
  //! Return the maximum lead in a block
238 239 240 241 242
  virtual unsigned int
  getBlockMaxLead(int block_number) const
  {
    return (block_lag_lead[block_number].second);
  };
243
  //! Return the type of equation (equation_number) belonging to the block block_number
244 245 246 247 248
  virtual EquationType
  getBlockEquationType(int block_number, int equation_number) const
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].first);
  };
249
  //! Return true if the equation has been normalized
250 251 252 253 254
  virtual bool
  isBlockEquationRenormalized(int block_number, int equation_number) const
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].first == E_EVALUATE_S);
  };
255 256 257
  //! Return the expr_t of the equation equation_number belonging to the block block_number
  virtual expr_t
  getBlockEquationExpr(int block_number, int equation_number) const
258 259 260
  {
    return (equations[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]]);
  };
261 262 263
  //! Return the expr_t of the renormalized equation equation_number belonging to the block block_number
  virtual expr_t
  getBlockEquationRenormalizedExpr(int block_number, int equation_number) const
264 265 266
  {
    return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].second);
  };
267
  //! Return the original number of equation equation_number belonging to the block block_number
268 269 270 271 272
  virtual int
  getBlockEquationID(int block_number, int equation_number) const
  {
    return (equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]);
  };
273
  //! Return the original number of variable variable_number belonging to the block block_number
274 275 276 277 278
  virtual int
  getBlockVariableID(int block_number, int variable_number) const
  {
    return (variable_reordered[block_type_firstequation_size_mfs[block_number].first.second+variable_number]);
  };
279 280 281 282 283 284
  //! Return the original number of the exogenous variable varexo_number belonging to the block block_number
  virtual int
  getBlockVariableExoID(int block_number, int variable_number) const
  {
    return 0;
  };
285
  //! Return the position of equation_number in the block number belonging to the block block_number
286 287 288 289 290
  virtual int
  getBlockInitialEquationID(int block_number, int equation_number) const
  {
    return ((int) inv_equation_reordered[equation_number] - (int) block_type_firstequation_size_mfs[block_number].first.second);
  };
291
  //! Return the position of variable_number in the block number belonging to the block block_number
292 293 294 295 296
  virtual int
  getBlockInitialVariableID(int block_number, int variable_number) const
  {
    return ((int) inv_variable_reordered[variable_number] - (int) block_type_firstequation_size_mfs[block_number].first.second);
  };
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  //! Return the position of variable_number in the block number belonging to the block block_number
  virtual int
  getBlockInitialExogenousID(int block_number, int variable_number) const
  {
    return -1;
  };
  //! Return the position of the deterministic exogenous variable_number in the block number belonging to the block block_number
  virtual int
  getBlockInitialDetExogenousID(int block_number, int variable_number) const
  {
    return -1;
  };
  //! Return the position of the other endogenous variable_number in the block number belonging to the block block_number
  virtual int
  getBlockInitialOtherEndogenousID(int block_number, int variable_number) const
  {
    return -1;
  };
sebastien's avatar
sebastien committed
315 316 317
};

#endif