ModelTree.hh 6.15 KB
Newer Older
1
2
#ifndef _MODELTREE_HH
#define _MODELTREE_HH
sebastien's avatar
sebastien committed
3
4
5

using namespace std;

6
7
#include <string>
#include <vector>
sebastien's avatar
sebastien committed
8
#include <map>
sebastien's avatar
sebastien committed
9
#include <ostream>
sebastien's avatar
sebastien committed
10

11
12
13
#include "SymbolTable.hh"
#include "NumericalConstants.hh"
#include "DataTree.hh"
sebastien's avatar
sebastien committed
14
#include "BlockTriangular.hh"
sebastien's avatar
sebastien committed
15

sebastien's avatar
sebastien committed
16
17
18
19
20
21
22
23
24
25
26
#define LCC_COMPILE 0
#define GCC_COMPILE 1

//! The three in which ModelTree can work
enum ModelTreeMode
  {
    eStandardMode, //!< Standard mode (static and dynamic files in Matlab)
    eDLLMode,      //!< DLL mode (static and dynamic files in C)
    eSparseDLLMode //!< Sparse DLL mode (static file in Matlab, dynamic file in C with block decomposition plus a binary file)
  };

sebastien's avatar
sebastien committed
27
//! Stores a model's equations and derivatives
28
29
class ModelTree : public DataTree
{
sebastien's avatar
sebastien committed
30
31
32
33
34
35
36
37
38
private:
  //! Stores declared equations
  vector<BinaryOpNode *> equations;

  typedef map<pair<int, int>, NodeID> first_derivatives_type;
  //! First order derivatives
  /*! First index is equation number, second is variable w.r. to which is computed the derivative.
    Only non-null derivatives are stored in the map.
    Variable indexes used are those of the variable_table, before sorting.
39
  */
sebastien's avatar
sebastien committed
40
  first_derivatives_type first_derivatives;
41

sebastien's avatar
sebastien committed
42
43
44
45
46
47
48
49
50
  typedef map<pair<int, pair<int, int> >, NodeID> second_derivatives_type;
  //! Second order derivatives
  /*! First index is equation number, second and third are variables w.r. to which is computed the derivative.
    Only non-null derivatives are stored in the map.
    Contains only second order derivatives where var1 >= var2 (for obvious symmetry reasons).
    Variable indexes used are those of the variable_table, before sorting.
  */
  second_derivatives_type second_derivatives;

sebastien's avatar
sebastien committed
51
52
53
54
55
56
57
58
59
  typedef map<pair<int, pair<int, pair<int, int> > >, NodeID> third_derivatives_type;
  //! Third order derivatives
  /*! First index is equation number, second, third and fourth are variables w.r. to which is computed the derivative.
    Only non-null derivatives are stored in the map.
    Contains only third order derivatives where var1 >= var2 >= var3 (for obvious symmetry reasons).
    Variable indexes used are those of the variable_table, before sorting.
  */
  third_derivatives_type third_derivatives;

sebastien's avatar
sebastien committed
60
61
62
63
64
  //! Temporary terms (those which will be noted Txxxx)
  temporary_terms_type temporary_terms;

  //! Computes derivatives of ModelTree
  void derive(int order);
sebastien's avatar
sebastien committed
65
66
  //! Write derivative of an equation w.r. to a variable
  void writeDerivative(ostream &output, int eq, int var, int lag, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
sebastien's avatar
sebastien committed
67
68
  //! Computes temporary terms
  void computeTemporaryTerms(int order);
sebastien's avatar
sebastien committed
69
  void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock);
sebastien's avatar
sebastien committed
70
  //! Writes temporary terms
sebastien's avatar
sebastien committed
71
  void writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) const;
sebastien's avatar
sebastien committed
72
  //! Writes model local variables
73
  /*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */
sebastien's avatar
sebastien committed
74
  void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const;
sebastien's avatar
sebastien committed
75
  //! Writes model equations
sebastien's avatar
sebastien committed
76
  void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const;
sebastien's avatar
sebastien committed
77
  //! Writes the static model equations and its derivatives
sebastien's avatar
sebastien committed
78
  /*! \todo handle hessian in C output */
sebastien's avatar
sebastien committed
79
  void writeStaticModel(ostream &StaticOutput) const;
sebastien's avatar
sebastien committed
80
  //! Writes the dynamic model equations and its derivatives
sebastien's avatar
sebastien committed
81
  /*! \todo add third derivatives handling in C output */
sebastien's avatar
sebastien committed
82
83
  void writeDynamicModel(ostream &DynamicOutput) const;
  void writeModelEquationsOrdered(ostream &output, Model_Block *ModelBlock) const;
sebastien's avatar
sebastien committed
84
  //! Writes static model file (Matlab version)
sebastien's avatar
sebastien committed
85
  void writeStaticMFile(const string &static_basename) const;
sebastien's avatar
sebastien committed
86
  //! Writes static model file (C version)
sebastien's avatar
sebastien committed
87
  void writeStaticCFile(const string &static_basename) const;
sebastien's avatar
sebastien committed
88
  //! Writes dynamic model file (Matlab version)
sebastien's avatar
sebastien committed
89
  void writeDynamicMFile(const string &dynamic_basename) const;
sebastien's avatar
sebastien committed
90
  //! Writes dynamic model file (C version)
sebastien's avatar
sebastien committed
91
  /*! \todo add third derivatives handling */
sebastien's avatar
sebastien committed
92
  void writeDynamicCFile(const string &dynamic_basename) const;
sebastien's avatar
sebastien committed
93
94
95
96
  //! Writes dynamic model header file when SparseDLL option is on
  void writeSparseDLLDynamicHFile(const string &dynamic_basename) const;
  //! Writes dynamic model file when SparseDLL option is on
  void writeSparseDLLDynamicCFileAndBinFile(const string &dynamic_basename, const string &bin_basename) const;
sebastien's avatar
sebastien committed
97
98
  void evaluateJacobian(const eval_context_type &eval_context);
  void BlockLinear(Model_Block *ModelBlock);
sebastien's avatar
sebastien committed
99
  string reform(string name) const;
sebastien's avatar
sebastien committed
100
101

public:
sebastien's avatar
sebastien committed
102
  ModelTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants);
sebastien's avatar
sebastien committed
103
104
105
106
  //! Mode in which the ModelTree is supposed to work (Matlab, DLL or SparseDLL)
  ModelTreeMode mode;
  //! Type of compiler used in matlab for SPARSE_DLL option: 0 = LCC or 1 = GCC
  int compiler;
sebastien's avatar
sebastien committed
107
108
  //! Absolute value under which a number is considered to be zero
  double cutoff;
sebastien's avatar
sebastien committed
109

sebastien's avatar
sebastien committed
110
111
  //! Declare a node as an equation of the model
  void addEquation(NodeID eq);
sebastien's avatar
sebastien committed
112
113
  //! Do some checking
  void checkPass() const;
sebastien's avatar
sebastien committed
114
  //! Whether dynamic Jacobian (w.r. to endogenous) should be written
sebastien's avatar
sebastien committed
115
  bool computeJacobian;
sebastien's avatar
sebastien committed
116
  //! Whether dynamic Jacobian (w.r. to endogenous and exogenous) should be written
sebastien's avatar
sebastien committed
117
  bool computeJacobianExo;
sebastien's avatar
sebastien committed
118
  //! Whether dynamic Hessian (w.r. to endogenous and exogenous) should be written
sebastien's avatar
sebastien committed
119
  bool computeHessian;
sebastien's avatar
sebastien committed
120
121
  //! Whether static Hessian (w.r. to endogenous only) should be written
  bool computeStaticHessian;
sebastien's avatar
sebastien committed
122
123
  //! Whether dynamic third order derivatives (w.r. to endogenous and exogenous) should be written
  bool computeThirdDerivatives;
sebastien's avatar
sebastien committed
124
  //! Execute computations (variable sorting + derivation)
sebastien's avatar
sebastien committed
125
  /*! You must set computeJacobian, computeJacobianExo, computeHessian, computeStaticHessian and computeThirdDerivatives to correct values before calling this function */
sebastien's avatar
sebastien committed
126
  void computingPass(const eval_context_type &eval_context);
sebastien's avatar
sebastien committed
127
  //! Writes model initialization and lead/lag incidence matrix to output
sebastien's avatar
sebastien committed
128
  void writeOutput(ostream &output) const;
sebastien's avatar
sebastien committed
129
  //! Writes static model file
sebastien's avatar
sebastien committed
130
  void writeStaticFile(const string &basename) const;
sebastien's avatar
sebastien committed
131
  //! Writes dynamic model file
sebastien's avatar
sebastien committed
132
  void writeDynamicFile(const string &basename) const;
sebastien's avatar
sebastien committed
133
134
135
  //! Complete set to block decompose the model
  BlockTriangular block_triangular;
  int equation_number() const;
136
};
sebastien's avatar
sebastien committed
137

138
#endif