2008-02-03 11:28:36 +01:00
/*
2009-01-21 15:39:24 +01:00
* Copyright ( C ) 2003 - 2009 Dynare Team
2008-02-03 11:28:36 +01: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/>.
*/
# ifndef _MODELTREE_HH
# define _MODELTREE_HH
using namespace std ;
# include <string>
# include <vector>
# include <map>
# include <ostream>
# include "DataTree.hh"
//! The three in which ModelTree can work
enum ModelTreeMode
{
eStandardMode , //!< Standard mode (static and dynamic files in Matlab)
eSparseMode , //!< Sparse mode (static file in Matlab, dynamic file in Matlab with block decomposition)
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)
} ;
2009-04-14 16:39:53 +02:00
//! Shared code for static and dynamic models
2008-02-03 11:28:36 +01:00
class ModelTree : public DataTree
{
2009-04-14 16:39:53 +02:00
protected :
2008-02-03 11:28:36 +01:00
//! 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 .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
first_derivatives_type first_derivatives ;
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 ) .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
second_derivatives_type second_derivatives ;
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 ) .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
third_derivatives_type third_derivatives ;
//! Temporary terms (those which will be noted Txxxx)
temporary_terms_type temporary_terms ;
2009-04-20 12:48:54 +02:00
//! Computes 1st derivatives
/*! \param vars the derivation IDs w.r. to which compute the derivatives */
void computeJacobian ( const set < int > & vars ) ;
//! Computes 2nd derivatives
/*! \param vars the derivation IDs w.r. to which derive the 1st derivatives */
void computeHessian ( const set < int > & vars ) ;
//! Computes 3rd derivatives
/*! \param vars the derivation IDs w.r. to which derive the 2nd derivatives */
void computeThirdDerivatives ( const set < int > & vars ) ;
2008-02-03 11:28:36 +01:00
//! Write derivative of an equation w.r. to a variable
2009-02-27 13:19:25 +01:00
void writeDerivative ( ostream & output , int eq , int symb_id , int lag , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
2009-04-20 12:48:54 +02:00
//! Computes temporary terms (for all equations and derivatives)
void computeTemporaryTerms ( ) ;
2008-02-03 11:28:36 +01:00
//! Writes temporary terms
2009-04-20 15:58:15 +02:00
void writeTemporaryTerms ( const temporary_terms_type & tt , ostream & output , ExprNodeOutputType output_type ) const ;
2008-02-03 11:28:36 +01:00
//! Writes model local variables
/*! 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 */
void writeModelLocalVariables ( ostream & output , ExprNodeOutputType output_type ) const ;
//! Writes model equations
void writeModelEquations ( ostream & output , ExprNodeOutputType output_type ) const ;
//! Writes either (i+1,j+1) or [i+j*n_i] whether we are in Matlab or C mode
void matrixHelper ( ostream & output , int eq_nb , int col_nb , ExprNodeOutputType output_type ) const ;
public :
ModelTree ( SymbolTable & symbol_table_arg , NumericalConstants & num_constants ) ;
//! Mode in which the ModelTree is supposed to work (Matlab, DLL or SparseDLL)
ModelTreeMode mode ;
//! Declare a node as an equation of the model
void addEquation ( NodeID eq ) ;
2008-10-17 19:21:22 +02:00
//! Returns the number of equations in the model
2008-02-03 11:28:36 +01:00
int equation_number ( ) const ;
} ;
# endif