2008-02-03 11:28:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-10-17 19:15:23 +02:00
|
|
|
#ifndef _BLOCKTRIANGULAR_HH
|
|
|
|
#define _BLOCKTRIANGULAR_HH
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
#include <string>
|
2008-12-24 16:49:01 +01:00
|
|
|
#include "CodeInterpreter.hh"
|
2008-02-03 11:28:36 +01:00
|
|
|
#include "ExprNode.hh"
|
|
|
|
#include "SymbolTable.hh"
|
2009-06-05 16:45:23 +02:00
|
|
|
//#include "ModelNormalization.hh"
|
|
|
|
//#include "ModelBlocks.hh"
|
2008-11-14 21:12:59 +01:00
|
|
|
#include "IncidenceMatrix.hh"
|
2009-05-18 07:02:20 +02:00
|
|
|
#include "ModelTree.hh"
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2008-11-14 17:07:47 +01:00
|
|
|
|
|
|
|
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Sparse matrix of double to store the values of the Jacobian
|
2008-02-03 11:28:36 +01:00
|
|
|
typedef map<pair<int ,int >,double> jacob_map;
|
|
|
|
|
2009-05-16 00:41:51 +02:00
|
|
|
typedef vector<pair<BlockSimulationType, pair<int, int> > > t_type;
|
|
|
|
|
2009-06-05 16:45:23 +02:00
|
|
|
//! Vector describing equations: BlockSimulationType, if BlockSimulationType == EVALUATE_s then a NodeID on the new normalized equation
|
|
|
|
typedef vector<pair<EquationType, NodeID > > t_etype;
|
|
|
|
|
|
|
|
//! Vector describing variables: max_lag in the block, max_lead in the block
|
|
|
|
typedef vector<pair< int, int> > t_vtype;
|
2008-12-24 16:49:01 +01:00
|
|
|
|
2009-07-06 12:36:36 +02:00
|
|
|
typedef set<int> temporary_terms_inuse_type;
|
|
|
|
|
2009-07-21 17:50:12 +02:00
|
|
|
typedef vector<pair< pair<int, pair<int, int> >, pair<int, int> > > chain_rule_derivatives_type;
|
2009-07-10 17:05:09 +02:00
|
|
|
|
2009-07-06 12:36:36 +02:00
|
|
|
//! For one lead/lag of one block, stores mapping of information between original model and block-decomposed model
|
|
|
|
struct IM_compact
|
|
|
|
{
|
|
|
|
int size, u_init, u_finish, nb_endo, nb_other_endo, size_exo, size_other_endo;
|
|
|
|
int *u, *us, *Var, *Equ, *Var_Index, *Equ_Index, *Exogenous, *Exogenous_Index, *Equ_X, *Equ_X_Index;
|
|
|
|
int *u_other_endo, *Var_other_endo, *Equ_other_endo, *Var_Index_other_endo, *Equ_Index_other_endo;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! One block of the model
|
|
|
|
struct Block
|
|
|
|
{
|
|
|
|
int Size, Sized, nb_exo, nb_exo_det, nb_other_endo, Nb_Recursives;
|
|
|
|
BlockType Type;
|
|
|
|
BlockSimulationType Simulation_Type;
|
|
|
|
int Max_Lead, Max_Lag, Nb_Lead_Lag_Endo;
|
|
|
|
int Max_Lag_Endo, Max_Lead_Endo;
|
|
|
|
int Max_Lag_Other_Endo, Max_Lead_Other_Endo;
|
|
|
|
int Max_Lag_Exo, Max_Lead_Exo;
|
|
|
|
bool is_linear;
|
|
|
|
int *Equation, *Own_Derivative;
|
|
|
|
EquationType *Equation_Type;
|
|
|
|
NodeID *Equation_Normalized;
|
|
|
|
int *Variable, *Other_Endogenous, *Exogenous;
|
|
|
|
temporary_terms_type **Temporary_Terms_in_Equation;
|
|
|
|
//temporary_terms_type *Temporary_terms;
|
|
|
|
temporary_terms_inuse_type *Temporary_InUse;
|
|
|
|
IM_compact *IM_lead_lag;
|
2009-07-21 17:50:12 +02:00
|
|
|
chain_rule_derivatives_type *Chain_Rule_Derivatives;
|
2009-07-06 12:36:36 +02:00
|
|
|
int Code_Start, Code_Length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! The set of all blocks of the model
|
|
|
|
struct Model_Block
|
|
|
|
{
|
|
|
|
int Size, Periods;
|
|
|
|
Block* Block_List;
|
|
|
|
//int *in_Block_Equ, *in_Block_Var, *in_Equ_of_Block, *in_Var_of_Block;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Creates the incidence matrix, computes prologue & epilogue, normalizes the model and computes the block decomposition
|
2008-02-03 11:28:36 +01:00
|
|
|
class BlockTriangular
|
|
|
|
{
|
2009-05-13 01:03:40 +02:00
|
|
|
private:
|
|
|
|
//! Find equations and endogenous variables belonging to the prologue and epilogue of the model
|
|
|
|
void Prologue_Epilogue(bool* IM, int &prologue, int &epilogue, int n, vector<int> &Index_Var_IM, vector<int> &Index_Equ_IM, bool* IM0);
|
|
|
|
//! Allocates and fills the Model structure describing the content of each block
|
2009-07-10 17:05:09 +02:00
|
|
|
void Allocate_Block(int size, int *count_Equ, int count_Block, BlockType type, BlockSimulationType SimType, Model_Block *ModelBlock, t_etype &Equation_Type, int recurs_Size, vector<int> &Index_Var_IM, vector<int> &Index_Equ_IM);
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Finds a matching between equations and endogenous variables
|
2009-08-25 11:43:01 +02:00
|
|
|
bool Compute_Normalization(bool *IM, int equation_number, int prologue, int epilogue, int verbose, bool *IM0, vector<int> &Index_Var_IM) const;
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Decomposes into recurive blocks the non purely recursive equations and determines for each block the minimum feedback variables
|
2009-08-25 11:43:01 +02:00
|
|
|
void Compute_Block_Decomposition_and_Feedback_Variables_For_Each_Block(bool *IM, int nb_var, int prologue, int epilogue, vector<int> &Index_Equ_IM, vector<int> &Index_Var_IM, vector<pair<int, int> > &blocks, t_etype &Equation_Type, bool verbose_, bool select_feedback_variable, int mfs) const;
|
2009-06-05 16:45:23 +02:00
|
|
|
//! determines the type of each equation of the model (could be evaluated or need to be solved)
|
2009-08-25 11:43:01 +02:00
|
|
|
t_etype Equation_Type_determination(vector<BinaryOpNode *> &equations, map<pair<int, pair<int, int> >, NodeID> &first_order_endo_derivatives, vector<int> &Index_Var_IM, vector<int> &Index_Equ_IM, int mfs);
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Tries to merge the consecutive blocks in a single block and determine the type of each block: recursive, simultaneous, ...
|
2009-07-10 17:05:09 +02:00
|
|
|
t_type Reduce_Blocks_and_type_determination(int prologue, int epilogue, vector<pair<int, int> > &blocks, vector<BinaryOpNode *> &equations, t_etype &Equation_Type, vector<int> &Index_Var_IM, vector<int> &Index_Equ_IM);
|
2009-06-05 16:45:23 +02:00
|
|
|
//! Compute for each variable its maximum lead and lag in its block
|
2009-07-10 17:05:09 +02:00
|
|
|
t_vtype Get_Variable_LeadLag_By_Block(vector<int > &components_set, int nb_blck_sim, int prologue, int epilogue, t_vtype &equation_lead_lag) const;
|
2008-02-03 11:28:36 +01:00
|
|
|
public:
|
2009-06-05 16:45:23 +02:00
|
|
|
SymbolTable &symbol_table;
|
|
|
|
/*Blocks blocks;
|
|
|
|
Normalization normalization;*/
|
|
|
|
IncidenceMatrix incidencematrix;
|
|
|
|
NumericalConstants &num_const;
|
|
|
|
DataTree *Normalized_Equation;
|
|
|
|
BlockTriangular(SymbolTable &symbol_table_arg, NumericalConstants &num_const_arg);
|
|
|
|
~BlockTriangular();
|
2009-05-13 01:03:40 +02:00
|
|
|
//! Frees the Model structure describing the content of each block
|
|
|
|
void Free_Block(Model_Block* ModelBlock) const;
|
2009-06-05 16:45:23 +02:00
|
|
|
|
2009-07-21 17:50:12 +02:00
|
|
|
map<pair<pair<int, pair<int, int> >, pair<int, int> >, int> get_Derivatives(Model_Block *ModelBlock, int Blck);
|
2009-06-05 16:45:23 +02:00
|
|
|
|
|
|
|
|
2009-08-25 11:43:01 +02:00
|
|
|
void Normalize_and_BlockDecompose_Static_0_Model(jacob_map &j_m, vector<BinaryOpNode *> &equations, t_etype &V_Equation_Type, map<pair<int, pair<int, int> >, NodeID> &first_order_endo_derivatives, int mfs, double cutoff);
|
|
|
|
void Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int &prologue, int &epilogue, vector<int> &Index_Var_IM, vector<int> &Index_Equ_IM, bool* IM_0 , jacob_map &j_m, vector<BinaryOpNode *> &equations, t_etype &equation_simulation_type, map<pair<int, pair<int, int> >, NodeID> &first_order_endo_derivatives, bool dynamic, int mfs, double cutoff);
|
2009-05-13 01:03:40 +02:00
|
|
|
vector<int> Index_Equ_IM;
|
|
|
|
vector<int> Index_Var_IM;
|
2008-02-03 11:28:36 +01:00
|
|
|
int prologue, epilogue;
|
|
|
|
bool bt_verbose;
|
|
|
|
Model_Block* ModelBlock;
|
2008-11-14 17:07:47 +01:00
|
|
|
int periods;
|
2008-02-03 11:28:36 +01:00
|
|
|
inline static std::string BlockType0(int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return ("SIMULTANEOUS TIME SEPARABLE ");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return ("PROLOGUE ");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
return ("EPILOGUE ");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
return ("SIMULTANEOUS TIME UNSEPARABLE");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ("UNKNOWN ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
inline static std::string BlockSim(int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2008-10-19 17:44:08 +02:00
|
|
|
case EVALUATE_FORWARD:
|
2009-06-05 16:45:23 +02:00
|
|
|
//case EVALUATE_FORWARD_R:
|
2008-11-14 17:07:47 +01:00
|
|
|
return ("EVALUATE FORWARD ");
|
2008-02-03 11:28:36 +01:00
|
|
|
break;
|
|
|
|
case EVALUATE_BACKWARD:
|
2009-06-05 16:45:23 +02:00
|
|
|
//case EVALUATE_BACKWARD_R:
|
2008-02-03 11:28:36 +01:00
|
|
|
return ("EVALUATE BACKWARD ");
|
|
|
|
break;
|
2008-10-19 17:44:08 +02:00
|
|
|
case SOLVE_FORWARD_SIMPLE:
|
2008-11-14 17:07:47 +01:00
|
|
|
return ("SOLVE FORWARD SIMPLE ");
|
2008-02-03 11:28:36 +01:00
|
|
|
break;
|
|
|
|
case SOLVE_BACKWARD_SIMPLE:
|
|
|
|
return ("SOLVE BACKWARD SIMPLE ");
|
|
|
|
break;
|
|
|
|
case SOLVE_TWO_BOUNDARIES_SIMPLE:
|
|
|
|
return ("SOLVE TWO BOUNDARIES SIMPLE ");
|
|
|
|
break;
|
2008-10-19 17:44:08 +02:00
|
|
|
case SOLVE_FORWARD_COMPLETE:
|
2008-11-14 17:07:47 +01:00
|
|
|
return ("SOLVE FORWARD COMPLETE ");
|
2008-02-03 11:28:36 +01:00
|
|
|
break;
|
|
|
|
case SOLVE_BACKWARD_COMPLETE:
|
|
|
|
return ("SOLVE BACKWARD COMPLETE ");
|
|
|
|
break;
|
|
|
|
case SOLVE_TWO_BOUNDARIES_COMPLETE:
|
|
|
|
return ("SOLVE TWO BOUNDARIES COMPLETE");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ("UNKNOWN ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2009-05-16 00:41:51 +02:00
|
|
|
inline static std::string c_Equation_Type(int type)
|
|
|
|
{
|
2009-08-25 11:43:01 +02:00
|
|
|
char c_Equation_Type[4][13]=
|
2009-05-16 00:41:51 +02:00
|
|
|
{
|
|
|
|
"E_UNKNOWN ",
|
|
|
|
"E_EVALUATE ",
|
|
|
|
"E_EVALUATE_S",
|
|
|
|
"E_SOLVE "
|
|
|
|
};
|
|
|
|
return(c_Equation_Type[type]);
|
|
|
|
};
|
2008-02-03 11:28:36 +01:00
|
|
|
};
|
|
|
|
#endif
|