2009-04-14 16:39:53 +02:00
|
|
|
/*
|
2011-02-04 17:27:33 +01:00
|
|
|
* Copyright (C) 2003-2011 Dynare Team
|
2009-04-14 16:39:53 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
#ifndef _STATIC_MODEL_HH
|
|
|
|
#define _STATIC_MODEL_HH
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <fstream>
|
2009-04-14 16:39:53 +02:00
|
|
|
|
|
|
|
#include "ModelTree.hh"
|
|
|
|
|
2010-04-23 18:39:07 +02:00
|
|
|
//! Stores a static model, as derived from the "model" block when leads and lags have been removed
|
2009-04-14 16:39:53 +02:00
|
|
|
class StaticModel : public ModelTree
|
|
|
|
{
|
|
|
|
private:
|
2009-12-16 14:21:31 +01:00
|
|
|
typedef map<pair<int, int>, int> deriv_id_table_t;
|
|
|
|
//! 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)
|
|
|
|
vector<pair<int, int> > inv_deriv_id_table;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Temporary terms for the file containing parameters dervicatives
|
2010-09-16 19:00:48 +02:00
|
|
|
temporary_terms_t params_derivs_temporary_terms;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2010-10-11 19:21:32 +02:00
|
|
|
//! global temporary terms for block decomposed models
|
2010-09-16 19:00:48 +02:00
|
|
|
vector<vector<temporary_terms_t> > v_temporary_terms;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2010-10-11 19:21:32 +02:00
|
|
|
//! local temporary terms for block decomposed models
|
|
|
|
vector<vector<temporary_terms_t> > v_temporary_terms_local;
|
|
|
|
|
2010-09-16 19:00:48 +02:00
|
|
|
vector<temporary_terms_inuse_t> v_temporary_terms_inuse;
|
2009-07-10 16:22:40 +02:00
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef map< pair< int, pair< int, int> >, expr_t> first_chain_rule_derivatives_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
first_chain_rule_derivatives_t first_chain_rule_derivatives;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2009-07-07 17:58:17 +02:00
|
|
|
//! Writes static model file (standard Matlab version)
|
2009-12-16 14:21:31 +01:00
|
|
|
void writeStaticMFile(const string &static_basename) const;
|
|
|
|
|
2011-08-18 12:44:11 +02:00
|
|
|
//! Writes static model file (C version)
|
|
|
|
void writeStaticCFile(const string &func_name) const;
|
|
|
|
|
|
|
|
//! Writes the static model equations and its derivatives
|
|
|
|
void writeStaticModel(ostream &StaticOutput, bool use_dll) const;
|
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Writes the static function calling the block to solve (Matlab version)
|
|
|
|
void writeStaticBlockMFSFile(const string &basename) const;
|
2009-04-14 16:39:53 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Writes the Block reordred structure of the model in M output
|
|
|
|
void writeModelEquationsOrdered_M(const string &dynamic_basename) const;
|
2009-06-30 17:07:09 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Writes the code of the Block reordred structure of the model in virtual machine bytecode
|
2010-10-11 19:21:32 +02:00
|
|
|
void writeModelEquationsCode_Block(const string file_name, const string bin_basename, map_idx_t map_idx, vector<map_idx_t> map_idx2) const;
|
2010-01-22 11:03:29 +01:00
|
|
|
|
|
|
|
//! Writes the code of the model in virtual machine bytecode
|
2010-09-16 19:00:48 +02:00
|
|
|
void writeModelEquationsCode(const string file_name, const string bin_basename, map_idx_t map_idx) const;
|
2010-01-22 11:03:29 +01:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! 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)
|
|
|
|
*/
|
2010-09-16 19:00:48 +02:00
|
|
|
void evaluateJacobian(const eval_context_t &eval_context, jacob_map_t *j_m, bool dynamic);
|
2009-07-10 16:22:40 +02:00
|
|
|
|
2010-09-16 19:00:48 +02:00
|
|
|
map_idx_t map_idx;
|
2009-04-28 19:11:48 +02:00
|
|
|
|
2010-10-11 19:21:32 +02:00
|
|
|
vector<map_idx_t> map_idx2;
|
|
|
|
|
2010-01-22 11:03:29 +01:00
|
|
|
//! sorts the temporary terms in the blocks order
|
2009-12-16 14:21:31 +01:00
|
|
|
void computeTemporaryTermsOrdered();
|
2010-01-22 11:03:29 +01:00
|
|
|
//! creates a mapping from the index of temporary terms to a natural index
|
2010-10-11 19:21:32 +02:00
|
|
|
void computeTemporaryTermsMapping(temporary_terms_t &temporary_terms, map_idx_t &map_idx);
|
2010-01-22 11:03:29 +01:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Write derivative code of an equation w.r. to a variable
|
2010-10-11 19:21:32 +02:00
|
|
|
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;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Write chain rule derivative code of an equation w.r. to a variable
|
2010-10-11 19:21:32 +02:00
|
|
|
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;
|
2009-04-27 19:15:14 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! 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
|
2010-09-16 19:00:48 +02:00
|
|
|
void computeChainRuleJacobian(blocks_derivatives_t &blocks_derivatives);
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Collect only the first derivatives
|
2010-09-16 19:18:45 +02:00
|
|
|
map<pair<int, pair<int, int> >, expr_t> collect_first_order_derivatives_endogenous();
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! Write chain rule derivative of a recursive equation w.r. to a variable
|
2010-09-16 19:00:48 +02:00
|
|
|
void writeChainRuleDerivative(ostream &output, int eq, int var, int lag, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! 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;
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
//! Vector describing equations: BlockSimulationType, if BlockSimulationType == EVALUATE_s then a expr_t on the new normalized equation
|
2010-09-16 19:00:48 +02:00
|
|
|
equation_type_and_normalized_equation_t equation_type_and_normalized_equation;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! for each block contains pair< Simulation_Type, pair < Block_Size, Recursive_part_Size > >
|
2010-09-16 19:00:48 +02:00
|
|
|
block_type_firstequation_size_mfs_t block_type_firstequation_size_mfs;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! for all blocks derivatives description
|
2010-09-16 19:00:48 +02:00
|
|
|
blocks_derivatives_t blocks_derivatives;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! The jacobian without the elements below the cutoff
|
2010-09-16 19:00:48 +02:00
|
|
|
dynamic_jacob_map_t dynamic_jacobian;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! Vector indicating if the block is linear in endogenous variable (true) or not (false)
|
|
|
|
vector<bool> blocks_linear;
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
//! 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;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Vector of derivative for each blocks
|
2010-09-16 19:00:48 +02:00
|
|
|
vector<derivative_t> derivative_endo, derivative_other_endo, derivative_exo, derivative_exo_det;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//!List for each block and for each lag-leag all the other endogenous variables and exogenous variables
|
2010-09-16 19:00:48 +02:00
|
|
|
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;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2010-07-23 11:20:24 +02:00
|
|
|
//! for each block described the number of static, forward, backward and mixed variables in the block
|
|
|
|
/*! pair< pair<static, forward>, pair<backward,mixed> > */
|
2011-02-04 16:25:38 +01:00
|
|
|
vector<pair< pair<int, int>, pair<int, int> > > block_col_type;
|
2010-07-23 11:20:24 +02:00
|
|
|
|
|
|
|
//! 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;
|
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//!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;
|
2009-07-13 15:44:46 +02:00
|
|
|
|
2009-04-14 16:39:53 +02:00
|
|
|
public:
|
2010-02-22 17:33:38 +01:00
|
|
|
StaticModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants, ExternalFunctionsTable &external_functions_table_arg);
|
2009-07-07 16:20:48 +02:00
|
|
|
|
|
|
|
//! Writes information on block decomposition when relevant
|
2009-08-29 17:04:11 +02:00
|
|
|
void writeOutput(ostream &output, bool block) const;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! 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
|
2011-12-28 11:47:49 +01:00
|
|
|
\param hessian whether 2nd derivatives w.r. to exo, exo_det and endo should be computed
|
2009-12-16 14:21:31 +01:00
|
|
|
*/
|
2010-09-16 19:00:48 +02:00
|
|
|
void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool block, bool bytecode);
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2010-01-22 11:03:29 +01:00
|
|
|
//! 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,
|
2011-02-04 16:25:38 +01:00
|
|
|
int &u_count_int, bool &file_open) const;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2009-04-14 16:39:53 +02:00
|
|
|
//! Writes static model file
|
2011-08-18 12:44:11 +02:00
|
|
|
void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll) const;
|
2009-04-17 18:26:23 +02:00
|
|
|
|
2009-04-30 15:14:33 +02:00
|
|
|
//! Writes LaTeX file with the equations of the static model
|
|
|
|
void writeLatexFile(const string &basename) const;
|
|
|
|
|
2010-04-27 17:04:52 +02:00
|
|
|
//! Writes initializations in oo_.steady_state or steady state file for the auxiliary variables
|
|
|
|
void writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const;
|
2009-09-30 17:10:31 +02:00
|
|
|
|
2011-07-24 20:52:03 +02:00
|
|
|
//! Writes definition of the auxiliary variables in a M file
|
|
|
|
void writeAuxVarRecursiveDefinitions(const string &basename) const;
|
|
|
|
|
2009-04-17 18:26:23 +02:00
|
|
|
virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
|
2009-12-16 14:21:31 +01:00
|
|
|
|
|
|
|
//! Return the number of blocks
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getNbBlocks() const
|
|
|
|
{
|
|
|
|
return (block_type_firstequation_size_mfs.size());
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Determine the simulation type of each block
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual BlockSimulationType
|
|
|
|
getBlockSimulationType(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_type_firstequation_size_mfs[block_number].first.first);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the first equation number of a block
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getBlockFirstEquation(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_type_firstequation_size_mfs[block_number].first.second);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the size of the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getBlockSize(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_type_firstequation_size_mfs[block_number].second.first);
|
|
|
|
};
|
2010-07-23 11:20:24 +02:00
|
|
|
//! 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;
|
|
|
|
}
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the number of feedback variable of the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getBlockMfs(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_type_firstequation_size_mfs[block_number].second.second);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the maximum lag in a block
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getBlockMaxLag(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_lag_lead[block_number].first);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the maximum lead in a block
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual unsigned int
|
|
|
|
getBlockMaxLead(int block_number) const
|
|
|
|
{
|
|
|
|
return (block_lag_lead[block_number].second);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the type of equation (equation_number) belonging to the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
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);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return true if the equation has been normalized
|
2009-12-16 18:13:23 +01:00
|
|
|
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);
|
|
|
|
};
|
2010-09-16 19:18:45 +02:00
|
|
|
//! 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
|
2009-12-16 18:13:23 +01:00
|
|
|
{
|
|
|
|
return (equations[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]]);
|
|
|
|
};
|
2010-09-16 19:18:45 +02:00
|
|
|
//! 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
|
2009-12-16 18:13:23 +01:00
|
|
|
{
|
|
|
|
return (equation_type_and_normalized_equation[equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]].second);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the original number of equation equation_number belonging to the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual int
|
|
|
|
getBlockEquationID(int block_number, int equation_number) const
|
|
|
|
{
|
|
|
|
return (equation_reordered[block_type_firstequation_size_mfs[block_number].first.second+equation_number]);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the original number of variable variable_number belonging to the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual int
|
|
|
|
getBlockVariableID(int block_number, int variable_number) const
|
|
|
|
{
|
|
|
|
return (variable_reordered[block_type_firstequation_size_mfs[block_number].first.second+variable_number]);
|
|
|
|
};
|
2010-07-23 11:20:24 +02:00
|
|
|
//! 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;
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the position of equation_number in the block number belonging to the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
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);
|
|
|
|
};
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Return the position of variable_number in the block number belonging to the block block_number
|
2009-12-16 18:13:23 +01:00
|
|
|
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);
|
|
|
|
};
|
2010-07-23 11:20:24 +02:00
|
|
|
//! 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;
|
|
|
|
};
|
2009-04-14 16:39:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|