2009-04-14 16:39:53 +02:00
/*
2018-03-27 17:14:30 +02:00
* Copyright ( C ) 2003 - 2018 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
# include <iostream>
# include <cmath>
2009-04-14 16:47:57 +02:00
# include <cstdlib>
2009-04-27 19:15:14 +02:00
# include <cassert>
2009-12-16 14:21:31 +01:00
# include <cstdio>
# include <cerrno>
2009-04-28 19:11:48 +02:00
# include <algorithm>
2009-12-16 14:21:31 +01:00
2018-06-27 15:01:31 +02:00
# include <boost/filesystem.hpp>
# include "StaticModel.hh"
2009-04-28 19:11:48 +02:00
2009-12-16 14:21:31 +01:00
StaticModel : : StaticModel ( SymbolTable & symbol_table_arg ,
2010-02-22 17:33:38 +01:00
NumericalConstants & num_constants_arg ,
2018-09-14 17:04:06 +02:00
ExternalFunctionsTable & external_functions_table_arg ) :
ModelTree ( symbol_table_arg , num_constants_arg , external_functions_table_arg ) ,
2011-06-22 11:56:07 +02:00
global_temporary_terms ( true )
2009-12-16 14:21:31 +01:00
{
}
2009-04-14 16:47:57 +02:00
2009-12-16 14:21:31 +01:00
void
2010-10-11 19:21:32 +02:00
StaticModel : : 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
{
2018-07-17 18:34:07 +02:00
auto it = first_derivatives . find ( { eq , getDerivID ( symbol_table . getID ( SymbolType : : endogenous , symb_id ) , 0 ) } ) ;
2009-12-16 14:21:31 +01:00
if ( it ! = first_derivatives . end ( ) )
2010-07-23 11:20:24 +02:00
( it - > second ) - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
2009-12-16 14:21:31 +01:00
else
{
FLDZ_ fldz ;
2010-07-23 11:20:24 +02:00
fldz . write ( code_file , instruction_number ) ;
2009-12-16 14:21:31 +01:00
}
}
2009-04-14 16:39:53 +02:00
2009-12-16 14:21:31 +01:00
void
2010-10-11 19:21:32 +02:00
StaticModel : : compileChainRuleDerivative ( ofstream & code_file , unsigned int & instruction_number , int eqr , int varr , int lag , map_idx_t & map_idx , temporary_terms_t temporary_terms ) const
2009-12-16 14:21:31 +01:00
{
2018-06-04 16:36:46 +02:00
auto it = first_chain_rule_derivatives . find ( { eqr , { varr , lag } } ) ;
2009-12-16 14:21:31 +01:00
if ( it ! = first_chain_rule_derivatives . end ( ) )
2010-07-23 11:20:24 +02:00
( it - > second ) - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
2009-12-16 14:21:31 +01:00
else
{
FLDZ_ fldz ;
2010-07-23 11:20:24 +02:00
fldz . write ( code_file , instruction_number ) ;
2009-12-16 14:21:31 +01:00
}
}
void
StaticModel : : computeTemporaryTermsOrdered ( )
{
2018-06-04 14:17:36 +02:00
map < expr_t , pair < int , int > > first_occurence ;
2010-09-16 19:18:45 +02:00
map < expr_t , int > reference_count ;
2009-12-16 14:21:31 +01:00
BinaryOpNode * eq_node ;
2010-09-16 19:00:48 +02:00
first_derivatives_t : : const_iterator it ;
first_chain_rule_derivatives_t : : const_iterator it_chr ;
2009-12-16 14:21:31 +01:00
ostringstream tmp_s ;
v_temporary_terms . clear ( ) ;
map_idx . clear ( ) ;
unsigned int nb_blocks = getNbBlocks ( ) ;
2018-06-04 14:17:36 +02:00
v_temporary_terms = vector < vector < temporary_terms_t > > ( nb_blocks ) ;
v_temporary_terms_local = vector < vector < temporary_terms_t > > ( nb_blocks ) ;
2009-12-16 14:21:31 +01:00
2010-09-16 19:00:48 +02:00
v_temporary_terms_inuse = vector < temporary_terms_inuse_t > ( nb_blocks ) ;
2009-12-16 14:21:31 +01:00
2010-10-11 19:21:32 +02:00
map_idx2 = vector < map_idx_t > ( nb_blocks ) ;
2009-12-16 14:21:31 +01:00
temporary_terms . clear ( ) ;
2010-10-11 19:21:32 +02:00
//local temporay terms
for ( unsigned int block = 0 ; block < nb_blocks ; block + + )
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
map < expr_t , int > reference_count_local ;
reference_count_local . clear ( ) ;
2018-06-04 14:17:36 +02:00
map < expr_t , pair < int , int > > first_occurence_local ;
2010-10-11 19:21:32 +02:00
first_occurence_local . clear ( ) ;
temporary_terms_t temporary_terms_l ;
temporary_terms_l . clear ( ) ;
2009-12-16 14:21:31 +01:00
2010-10-11 19:21:32 +02:00
unsigned int block_size = getBlockSize ( block ) ;
unsigned int block_nb_mfs = getBlockMfs ( block ) ;
unsigned int block_nb_recursives = block_size - block_nb_mfs ;
v_temporary_terms_local [ block ] = vector < temporary_terms_t > ( block_size ) ;
for ( unsigned int i = 0 ; i < block_size ; i + + )
{
if ( i < block_nb_recursives & & isBlockEquationRenormalized ( block , i ) )
getBlockEquationRenormalizedExpr ( block , i ) - > computeTemporaryTerms ( reference_count_local , temporary_terms_l , first_occurence_local , block , v_temporary_terms_local , i ) ;
else
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
eq_node - > computeTemporaryTerms ( reference_count_local , temporary_terms_l , first_occurence_local , block , v_temporary_terms_local , i ) ;
2009-12-16 14:21:31 +01:00
}
}
2010-10-11 19:21:32 +02:00
for ( block_derivatives_equation_variable_laglead_nodeid_t : : const_iterator it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
{
expr_t id = it - > second . second ;
id - > computeTemporaryTerms ( reference_count_local , temporary_terms_l , first_occurence_local , block , v_temporary_terms_local , block_size - 1 ) ;
}
set < int > temporary_terms_in_use ;
temporary_terms_in_use . clear ( ) ;
v_temporary_terms_inuse [ block ] = temporary_terms_in_use ;
computeTemporaryTermsMapping ( temporary_terms_l , map_idx2 [ block ] ) ;
2009-12-16 14:21:31 +01:00
}
2010-10-11 19:21:32 +02:00
// global temporay terms
for ( unsigned int block = 0 ; block < nb_blocks ; block + + )
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
// Compute the temporary terms reordered
unsigned int block_size = getBlockSize ( block ) ;
unsigned int block_nb_mfs = getBlockMfs ( block ) ;
unsigned int block_nb_recursives = block_size - block_nb_mfs ;
v_temporary_terms [ block ] = vector < temporary_terms_t > ( block_size ) ;
for ( unsigned int i = 0 ; i < block_size ; i + + )
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
if ( i < block_nb_recursives & & isBlockEquationRenormalized ( block , i ) )
getBlockEquationRenormalizedExpr ( block , i ) - > computeTemporaryTerms ( reference_count , temporary_terms , first_occurence , block , v_temporary_terms , i ) ;
else
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
eq_node - > computeTemporaryTerms ( reference_count , temporary_terms , first_occurence , block , v_temporary_terms , i ) ;
2009-12-16 14:21:31 +01:00
}
}
2010-10-11 19:21:32 +02:00
for ( block_derivatives_equation_variable_laglead_nodeid_t : : const_iterator it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
expr_t id = it - > second . second ;
id - > computeTemporaryTerms ( reference_count , temporary_terms , first_occurence , block , v_temporary_terms , block_size - 1 ) ;
}
}
for ( unsigned int block = 0 ; block < nb_blocks ; block + + )
{
// Collecte the temporary terms reordered
unsigned int block_size = getBlockSize ( block ) ;
unsigned int block_nb_mfs = getBlockMfs ( block ) ;
unsigned int block_nb_recursives = block_size - block_nb_mfs ;
set < int > temporary_terms_in_use ;
for ( unsigned int i = 0 ; i < block_size ; i + + )
{
if ( i < block_nb_recursives & & isBlockEquationRenormalized ( block , i ) )
getBlockEquationRenormalizedExpr ( block , i ) - > collectTemporary_terms ( temporary_terms , temporary_terms_in_use , block ) ;
else
2009-12-16 14:21:31 +01:00
{
2010-10-11 19:21:32 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
eq_node - > collectTemporary_terms ( temporary_terms , temporary_terms_in_use , block ) ;
2009-12-16 14:21:31 +01:00
}
}
2010-10-11 19:21:32 +02:00
for ( block_derivatives_equation_variable_laglead_nodeid_t : : const_iterator it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
{
expr_t id = it - > second . second ;
id - > collectTemporary_terms ( temporary_terms , temporary_terms_in_use , block ) ;
}
for ( int i = 0 ; i < ( int ) getBlockSize ( block ) ; i + + )
2018-06-04 15:03:26 +02:00
for ( auto it = v_temporary_terms [ block ] [ i ] . begin ( ) ;
2010-10-11 19:21:32 +02:00
it ! = v_temporary_terms [ block ] [ i ] . end ( ) ; it + + )
( * it ) - > collectTemporary_terms ( temporary_terms , temporary_terms_in_use , block ) ;
v_temporary_terms_inuse [ block ] = temporary_terms_in_use ;
2009-12-16 14:21:31 +01:00
}
2010-10-11 19:21:32 +02:00
computeTemporaryTermsMapping ( temporary_terms , map_idx ) ;
2010-01-22 11:03:29 +01:00
}
void
2010-10-11 19:21:32 +02:00
StaticModel : : 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
// Add a mapping form node ID to temporary terms order
2009-12-16 18:13:23 +01:00
int j = 0 ;
2018-06-04 12:26:16 +02:00
for ( auto temporary_term : temporary_terms )
map_idx [ temporary_term - > idx ] = j + + ;
2009-12-16 14:21:31 +01:00
}
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeModelEquationsOrdered_M ( const string & basename ) const
2009-12-16 18:13:23 +01:00
{
string tmp_s , sps ;
ostringstream tmp_output , tmp1_output , global_output ;
2018-06-04 12:52:14 +02:00
expr_t lhs = nullptr , rhs = nullptr ;
2009-12-16 18:13:23 +01:00
BinaryOpNode * eq_node ;
2010-09-16 19:18:45 +02:00
map < expr_t , int > reference_count ;
2010-09-16 19:00:48 +02:00
temporary_terms_t local_temporary_terms ;
2009-12-16 18:13:23 +01:00
ofstream output ;
vector < int > feedback_variables ;
2010-12-10 11:50:27 +01:00
deriv_node_temp_terms_t tef_terms ;
2009-12-16 18:13:23 +01:00
ExprNodeOutputType local_output_type ;
2009-12-16 14:21:31 +01:00
2018-09-05 18:27:13 +02:00
local_output_type = ExprNodeOutputType : : matlabStaticModelSparse ;
2009-12-16 18:13:23 +01:00
if ( global_temporary_terms )
2010-09-16 16:57:35 +02:00
local_temporary_terms = temporary_terms ;
2009-12-16 18:13:23 +01:00
//----------------------------------------------------------------------
//For each block
for ( unsigned int block = 0 ; block < getNbBlocks ( ) ; block + + )
{
//recursive_variables.clear();
feedback_variables . clear ( ) ;
//For a block composed of a single equation determines wether we have to evaluate or to solve the equation
BlockSimulationType simulation_type = getBlockSimulationType ( block ) ;
unsigned int block_size = getBlockSize ( block ) ;
unsigned int block_mfs = getBlockMfs ( block ) ;
unsigned int block_recursive = block_size - block_mfs ;
tmp1_output . str ( " " ) ;
2018-06-27 15:01:31 +02:00
tmp1_output < < packageDir ( basename + " .block " ) < < " /static_ " < < block + 1 < < " .m " ;
2018-06-27 15:12:12 +02:00
output . open ( tmp1_output . str ( ) , ios : : out | ios : : binary ) ;
2009-12-16 18:13:23 +01:00
output < < " % \n " ;
output < < " % " < < tmp1_output . str ( ) < < " : Computes static model for Dynare \n " ;
output < < " % \n " ;
output < < " % Warning : this file is generated automatically by Dynare \n " ;
output < < " % from model file (.mod) \n \n " ;
output < < " %/ \n " ;
if ( simulation_type = = EVALUATE_BACKWARD | | simulation_type = = EVALUATE_FORWARD )
2018-06-27 15:01:31 +02:00
output < < " function y = static_ " < < block + 1 < < " (y, x, params) \n " ;
2009-12-16 18:13:23 +01:00
else
2018-06-27 15:01:31 +02:00
output < < " function [residual, y, g1] = static_ " < < block + 1 < < " (y, x, params) \n " ;
2009-12-16 18:13:23 +01:00
BlockType block_type ;
if ( simulation_type = = SOLVE_FORWARD_COMPLETE | | simulation_type = = SOLVE_BACKWARD_COMPLETE )
block_type = SIMULTANS ;
else if ( ( simulation_type = = SOLVE_FORWARD_SIMPLE | | simulation_type = = SOLVE_BACKWARD_SIMPLE
| | simulation_type = = EVALUATE_BACKWARD | | simulation_type = = EVALUATE_FORWARD )
& & getBlockFirstEquation ( block ) < prologue )
block_type = PROLOGUE ;
else if ( ( simulation_type = = SOLVE_FORWARD_SIMPLE | | simulation_type = = SOLVE_BACKWARD_SIMPLE
| | simulation_type = = EVALUATE_BACKWARD | | simulation_type = = EVALUATE_FORWARD )
& & getBlockFirstEquation ( block ) > = equations . size ( ) - epilogue )
block_type = EPILOGUE ;
else
block_type = SIMULTANS ;
output < < " % //////////////////////////////////////////////////////////////////////// " < < endl
< < " % // " < < string ( " Block " ) . substr ( int ( log10 ( block + 1 ) ) ) < < block + 1 < < " " < < BlockType0 ( block_type )
< < " // " < < endl
< < " % // Simulation type "
< < BlockSim ( simulation_type ) < < " // " < < endl
< < " % //////////////////////////////////////////////////////////////////////// " < < endl ;
output < < " global options_; " < < endl ;
//The Temporary terms
if ( simulation_type ! = EVALUATE_BACKWARD & & simulation_type ! = EVALUATE_FORWARD )
2010-10-22 16:20:28 +02:00
output < < " g1 = spalloc( " < < block_mfs < < " , " < < block_mfs < < " , " < < derivative_endo [ block ] . size ( ) < < " ); " < < endl ;
2009-12-16 18:13:23 +01:00
if ( v_temporary_terms_inuse [ block ] . size ( ) )
{
tmp_output . str ( " " ) ;
2018-06-04 12:26:16 +02:00
for ( int it : v_temporary_terms_inuse [ block ] )
tmp_output < < " T " < < it ;
2009-12-16 18:13:23 +01:00
output < < " global " < < tmp_output . str ( ) < < " ; \n " ;
}
if ( simulation_type ! = EVALUATE_BACKWARD & & simulation_type ! = EVALUATE_FORWARD )
output < < " residual=zeros( " < < block_mfs < < " ,1); \n " ;
// The equations
2018-03-27 17:14:30 +02:00
temporary_terms_idxs_t temporary_terms_idxs ;
2009-12-16 18:13:23 +01:00
for ( unsigned int i = 0 ; i < block_size ; i + + )
{
if ( ! global_temporary_terms )
local_temporary_terms = v_temporary_terms [ block ] [ i ] ;
2010-09-16 19:00:48 +02:00
temporary_terms_t tt2 ;
2009-12-16 18:13:23 +01:00
tt2 . clear ( ) ;
if ( v_temporary_terms [ block ] . size ( ) )
{
output < < " " < < " % //Temporary variables " < < endl ;
2018-06-04 12:26:16 +02:00
for ( auto it : v_temporary_terms [ block ] [ i ] )
2009-12-16 18:13:23 +01:00
{
2018-06-04 12:52:14 +02:00
if ( dynamic_cast < AbstractExternalFunctionNode * > ( it ) ! = nullptr )
2018-06-04 12:26:16 +02:00
it - > writeExternalFunctionOutput ( output , local_output_type , tt2 , { } , tef_terms ) ;
2010-12-10 11:50:27 +01:00
2009-12-16 18:13:23 +01:00
output < < " " < < sps ;
2018-06-04 12:26:16 +02:00
it - > writeOutput ( output , local_output_type , local_temporary_terms , { } , tef_terms ) ;
2009-12-16 18:13:23 +01:00
output < < " = " ;
2018-06-04 12:26:16 +02:00
it - > writeOutput ( output , local_output_type , tt2 , { } , tef_terms ) ;
2009-12-16 18:13:23 +01:00
// Insert current node into tt2
2018-06-04 12:26:16 +02:00
tt2 . insert ( it ) ;
2009-12-16 18:13:23 +01:00
output < < " ; " < < endl ;
}
}
int variable_ID = getBlockVariableID ( block , i ) ;
int equation_ID = getBlockEquationID ( block , i ) ;
EquationType equ_type = getBlockEquationType ( block , i ) ;
2018-07-17 18:34:07 +02:00
string sModel = symbol_table . getName ( symbol_table . getID ( SymbolType : : endogenous , variable_ID ) ) ;
2010-09-16 19:18:45 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
2009-12-16 18:13:23 +01:00
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
tmp_output . str ( " " ) ;
2018-05-28 11:56:34 +02:00
lhs - > writeOutput ( tmp_output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
switch ( simulation_type )
{
case EVALUATE_BACKWARD :
case EVALUATE_FORWARD :
evaluation :
output < < " % equation " < < getBlockEquationID ( block , i ) + 1 < < " variable : " < < sModel
< < " ( " < < variable_ID + 1 < < " ) " < < c_Equation_Type ( equ_type ) < < endl ;
output < < " " ;
if ( equ_type = = E_EVALUATE )
{
output < < tmp_output . str ( ) ;
output < < " = " ;
2018-05-28 11:56:34 +02:00
rhs - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
}
else if ( equ_type = = E_EVALUATE_S )
{
output < < " % " < < tmp_output . str ( ) ;
output < < " = " ;
if ( isBlockEquationRenormalized ( block , i ) )
{
2018-05-28 11:56:34 +02:00
rhs - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
output < < " \n " ;
tmp_output . str ( " " ) ;
2010-09-16 19:18:45 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationRenormalizedExpr ( block , i ) ;
2009-12-16 18:13:23 +01:00
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2018-05-28 11:56:34 +02:00
lhs - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
output < < " = " ;
2018-05-28 11:56:34 +02:00
rhs - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
}
}
else
{
2017-06-28 15:06:54 +02:00
cerr < < " Type mismatch for equation " < < equation_ID + 1 < < " \n " ;
2009-12-16 18:13:23 +01:00
exit ( EXIT_FAILURE ) ;
}
output < < " ; \n " ;
break ;
case SOLVE_BACKWARD_SIMPLE :
case SOLVE_FORWARD_SIMPLE :
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
if ( i < block_recursive )
goto evaluation ;
feedback_variables . push_back ( variable_ID ) ;
output < < " % equation " < < equation_ID + 1 < < " variable : " < < sModel
< < " ( " < < variable_ID + 1 < < " ) " < < c_Equation_Type ( equ_type ) < < endl ;
output < < " " < < " residual( " < < i + 1 - block_recursive < < " ) = ( " ;
goto end ;
default :
end :
output < < tmp_output . str ( ) ;
output < < " ) - ( " ;
2018-05-28 11:56:34 +02:00
rhs - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2009-12-16 18:13:23 +01:00
output < < " ); \n " ;
}
}
// The Jacobian if we have to solve the block
if ( simulation_type = = SOLVE_BACKWARD_SIMPLE | | simulation_type = = SOLVE_FORWARD_SIMPLE
| | simulation_type = = SOLVE_BACKWARD_COMPLETE | | simulation_type = = SOLVE_FORWARD_COMPLETE )
output < < " " < < sps < < " % Jacobian " < < endl ;
switch ( simulation_type )
{
case SOLVE_BACKWARD_SIMPLE :
case SOLVE_FORWARD_SIMPLE :
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
2018-06-04 15:03:26 +02:00
for ( auto it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
2009-12-16 18:13:23 +01:00
{
unsigned int eq = it - > first . first ;
unsigned int var = it - > first . second ;
unsigned int eqr = getBlockEquationID ( block , eq ) ;
unsigned int varr = getBlockVariableID ( block , var ) ;
2010-09-16 19:18:45 +02:00
expr_t id = it - > second . second ;
2009-12-16 18:13:23 +01:00
output < < " g1( " < < eq + 1 - block_recursive < < " , " < < var + 1 - block_recursive < < " ) = " ;
2018-05-28 11:56:34 +02:00
id - > writeOutput ( output , local_output_type , local_temporary_terms , { } ) ;
2018-07-17 18:34:07 +02:00
output < < " ; % variable= " < < symbol_table . getName ( symbol_table . getID ( SymbolType : : endogenous , varr ) )
2009-12-16 18:13:23 +01:00
< < " ( " < < 0
< < " ) " < < varr + 1
< < " , equation= " < < eqr + 1 < < endl ;
}
break ;
default :
break ;
}
2010-12-17 14:21:35 +01:00
output < < " end " < < endl ;
2009-12-16 18:13:23 +01:00
output . close ( ) ;
}
}
2009-12-16 14:21:31 +01:00
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeModelEquationsCode ( const string & basename , map_idx_t map_idx ) const
2010-01-22 11:03:29 +01:00
{
ostringstream tmp_output ;
ofstream code_file ;
2010-07-23 11:20:24 +02:00
unsigned int instruction_number = 0 ;
2010-01-22 11:03:29 +01:00
bool file_open = false ;
2018-06-27 15:01:31 +02:00
boost : : filesystem : : create_directories ( basename + " /model/bytecode " ) ;
string main_name = basename + " /model/bytecode/static.cod " ;
2018-06-27 15:12:12 +02:00
code_file . open ( main_name , ios : : out | ios : : binary | ios : : ate ) ;
2010-01-22 11:03:29 +01:00
if ( ! code_file . is_open ( ) )
{
2017-03-01 12:44:31 +01:00
cerr < < " Error : Can't open file \" " < < main_name < < " \" for writing " < < endl ;
2010-01-22 11:03:29 +01:00
exit ( EXIT_FAILURE ) ;
}
int count_u ;
int u_count_int = 0 ;
2018-06-27 15:01:31 +02:00
Write_Inf_To_Bin_File ( basename + " /model/bytecode/static.bin " , u_count_int , file_open , false , symbol_table . endo_nbr ( ) ) ;
2010-01-22 11:03:29 +01:00
file_open = true ;
//Temporary variables declaration
2010-10-11 19:21:32 +02:00
FDIMST_ fdimst ( temporary_terms . size ( ) ) ;
fdimst . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
FBEGINBLOCK_ fbeginblock ( symbol_table . endo_nbr ( ) ,
SOLVE_FORWARD_COMPLETE ,
0 ,
symbol_table . endo_nbr ( ) ,
variable_reordered ,
equation_reordered ,
false ,
symbol_table . endo_nbr ( ) ,
0 ,
0 ,
2010-07-23 11:20:24 +02:00
u_count_int ,
symbol_table . endo_nbr ( )
2010-01-22 11:03:29 +01:00
) ;
2010-07-23 11:20:24 +02:00
fbeginblock . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
// Add a mapping form node ID to temporary terms order
int j = 0 ;
2018-06-04 12:26:16 +02:00
for ( auto temporary_term : temporary_terms )
map_idx [ temporary_term - > idx ] = j + + ;
2010-07-23 11:20:24 +02:00
compileTemporaryTerms ( code_file , instruction_number , temporary_terms , map_idx , false , false ) ;
2010-01-22 11:03:29 +01:00
2010-07-23 11:20:24 +02:00
compileModelEquations ( code_file , instruction_number , temporary_terms , map_idx , false , false ) ;
2010-01-22 11:03:29 +01:00
FENDEQU_ fendequ ;
2010-07-23 11:20:24 +02:00
fendequ . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
2010-10-18 17:28:21 +02:00
// Get the current code_file position and jump if eval = true
streampos pos1 = code_file . tellp ( ) ;
FJMPIFEVAL_ fjmp_if_eval ( 0 ) ;
fjmp_if_eval . write ( code_file , instruction_number ) ;
int prev_instruction_number = instruction_number ;
2018-06-04 14:17:36 +02:00
vector < vector < pair < int , int > > > derivatives ;
2010-01-22 11:03:29 +01:00
derivatives . resize ( symbol_table . endo_nbr ( ) ) ;
count_u = symbol_table . endo_nbr ( ) ;
2018-06-04 12:26:16 +02:00
for ( const auto & first_derivative : first_derivatives )
2010-01-22 11:03:29 +01:00
{
2018-06-04 12:26:16 +02:00
int deriv_id = first_derivative . first . second ;
2018-07-17 18:34:07 +02:00
if ( getTypeByDerivID ( deriv_id ) = = SymbolType : : endogenous )
2010-01-22 11:03:29 +01:00
{
2018-06-04 12:26:16 +02:00
expr_t d1 = first_derivative . second ;
unsigned int eq = first_derivative . first . first ;
2010-01-22 11:03:29 +01:00
int symb = getSymbIDByDerivID ( deriv_id ) ;
unsigned int var = symbol_table . getTypeSpecificID ( symb ) ;
2010-01-22 17:42:08 +01:00
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , eq , var ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
if ( ! derivatives [ eq ] . size ( ) )
derivatives [ eq ] . clear ( ) ;
2018-06-04 16:36:46 +02:00
derivatives [ eq ] . emplace_back ( var , count_u ) ;
2010-01-22 11:03:29 +01:00
2010-07-23 11:20:24 +02:00
d1 - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
2010-01-22 11:03:29 +01:00
FSTPSU_ fstpsu ( count_u ) ;
2010-07-23 11:20:24 +02:00
fstpsu . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
count_u + + ;
}
}
for ( int i = 0 ; i < symbol_table . endo_nbr ( ) ; i + + )
{
FLDR_ fldr ( i ) ;
2010-07-23 11:20:24 +02:00
fldr . write ( code_file , instruction_number ) ;
2010-11-20 16:06:07 +01:00
if ( derivatives [ i ] . size ( ) )
2010-01-22 11:03:29 +01:00
{
2018-06-04 14:17:36 +02:00
for ( vector < pair < int , int > > : : const_iterator it = derivatives [ i ] . begin ( ) ;
2011-02-04 16:25:38 +01:00
it ! = derivatives [ i ] . end ( ) ; it + + )
2010-01-22 11:03:29 +01:00
{
2010-11-20 16:06:07 +01:00
FLDSU_ fldsu ( it - > second ) ;
fldsu . write ( code_file , instruction_number ) ;
2018-07-18 15:12:00 +02:00
FLDSV_ fldsv { static_cast < int > ( SymbolType : : endogenous ) , static_cast < unsigned int > ( it - > first ) } ;
2010-11-20 16:06:07 +01:00
fldsv . write ( code_file , instruction_number ) ;
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : times ) } ;
2010-07-23 11:20:24 +02:00
fbinary . write ( code_file , instruction_number ) ;
2010-11-20 16:06:07 +01:00
if ( it ! = derivatives [ i ] . begin ( ) )
{
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : plus ) } ;
2010-11-20 16:06:07 +01:00
fbinary . write ( code_file , instruction_number ) ;
}
2010-01-22 11:03:29 +01:00
}
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : minus ) } ;
2010-11-20 16:06:07 +01:00
fbinary . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
}
FSTPSU_ fstpsu ( i ) ;
2010-07-23 11:20:24 +02:00
fstpsu . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
}
2010-10-18 17:28:21 +02:00
// Get the current code_file position and jump = true
streampos pos2 = code_file . tellp ( ) ;
FJMP_ fjmp ( 0 ) ;
fjmp . write ( code_file , instruction_number ) ;
// Set code_file position to previous JMPIFEVAL_ and set the number of instructions to jump
streampos pos3 = code_file . tellp ( ) ;
code_file . seekp ( pos1 ) ;
FJMPIFEVAL_ fjmp_if_eval1 ( instruction_number - prev_instruction_number ) ;
fjmp_if_eval1 . write ( code_file , instruction_number ) ;
code_file . seekp ( pos3 ) ;
2011-02-04 16:25:38 +01:00
prev_instruction_number = instruction_number ;
2010-10-18 17:28:21 +02:00
temporary_terms_t tt2 ;
tt2 . clear ( ) ;
temporary_terms_t tt3 ;
tt3 . clear ( ) ;
// The Jacobian if we have to solve the block determinsitic bloc
2018-06-04 12:26:16 +02:00
for ( const auto & first_derivative : first_derivatives )
2010-10-18 17:28:21 +02:00
{
2018-06-04 12:26:16 +02:00
int deriv_id = first_derivative . first . second ;
2018-07-17 18:34:07 +02:00
if ( getTypeByDerivID ( deriv_id ) = = SymbolType : : endogenous )
2010-10-18 17:28:21 +02:00
{
2018-06-04 12:26:16 +02:00
expr_t d1 = first_derivative . second ;
unsigned int eq = first_derivative . first . first ;
2010-10-18 17:28:21 +02:00
int symb = getSymbIDByDerivID ( deriv_id ) ;
unsigned int var = symbol_table . getTypeSpecificID ( symb ) ;
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , eq , var ) ;
fnumexpr . write ( code_file , instruction_number ) ;
if ( ! derivatives [ eq ] . size ( ) )
derivatives [ eq ] . clear ( ) ;
2018-06-04 16:36:46 +02:00
derivatives [ eq ] . emplace_back ( var , count_u ) ;
2010-10-18 17:28:21 +02:00
d1 - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
2011-02-04 16:25:38 +01:00
FSTPG2_ fstpg2 ( eq , var ) ;
2010-10-18 17:28:21 +02:00
fstpg2 . write ( code_file , instruction_number ) ;
}
}
// Set codefile position to previous JMP_ and set the number of instructions to jump
pos1 = code_file . tellp ( ) ;
code_file . seekp ( pos2 ) ;
FJMP_ fjmp1 ( instruction_number - prev_instruction_number ) ;
fjmp1 . write ( code_file , instruction_number ) ;
code_file . seekp ( pos1 ) ;
2010-01-22 11:03:29 +01:00
FENDBLOCK_ fendblock ;
2010-07-23 11:20:24 +02:00
fendblock . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
FEND_ fend ;
2010-07-23 11:20:24 +02:00
fend . write ( code_file , instruction_number ) ;
2010-01-22 11:03:29 +01:00
code_file . close ( ) ;
}
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeModelEquationsCode_Block ( const string & basename , map_idx_t map_idx , vector < map_idx_t > map_idx2 ) const
2009-12-16 18:13:23 +01:00
{
struct Uff_l
2009-12-16 14:21:31 +01:00
{
2009-12-16 18:13:23 +01:00
int u , var , lag ;
Uff_l * pNext ;
} ;
2009-12-16 14:21:31 +01:00
2009-12-16 18:13:23 +01:00
struct Uff
{
Uff_l * Ufl , * Ufl_First ;
} ;
int i , v ;
string tmp_s ;
ostringstream tmp_output ;
ofstream code_file ;
2010-07-23 11:20:24 +02:00
unsigned int instruction_number = 0 ;
2018-06-04 12:52:14 +02:00
expr_t lhs = nullptr , rhs = nullptr ;
2009-12-16 18:13:23 +01:00
BinaryOpNode * eq_node ;
Uff Uf [ symbol_table . endo_nbr ( ) ] ;
2010-09-16 19:18:45 +02:00
map < expr_t , int > reference_count ;
2009-12-16 18:13:23 +01:00
vector < int > feedback_variables ;
2010-12-10 11:50:27 +01:00
deriv_node_temp_terms_t tef_terms ;
2009-12-16 18:13:23 +01:00
bool file_open = false ;
2018-06-27 15:01:31 +02:00
boost : : filesystem : : create_directories ( basename + " /model/bytecode " ) ;
string main_name = basename + " /model/bytecode/static.cod " ;
2018-06-27 15:12:12 +02:00
code_file . open ( main_name , ios : : out | ios : : binary | ios : : ate ) ;
2009-12-16 18:13:23 +01:00
if ( ! code_file . is_open ( ) )
{
2017-03-01 12:44:31 +01:00
cerr < < " Error : Can't open file \" " < < main_name < < " \" for writing " < < endl ;
2009-12-16 18:13:23 +01:00
exit ( EXIT_FAILURE ) ;
}
//Temporary variables declaration
2010-10-11 19:21:32 +02:00
FDIMST_ fdimst ( temporary_terms . size ( ) ) ;
fdimst . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
for ( unsigned int block = 0 ; block < getNbBlocks ( ) ; block + + )
{
feedback_variables . clear ( ) ;
if ( block > 0 )
{
FENDBLOCK_ fendblock ;
2010-07-23 11:20:24 +02:00
fendblock . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
}
int count_u ;
int u_count_int = 0 ;
BlockSimulationType simulation_type = getBlockSimulationType ( block ) ;
unsigned int block_size = getBlockSize ( block ) ;
unsigned int block_mfs = getBlockMfs ( block ) ;
unsigned int block_recursive = block_size - block_mfs ;
if ( simulation_type = = SOLVE_TWO_BOUNDARIES_SIMPLE | | simulation_type = = SOLVE_TWO_BOUNDARIES_COMPLETE
| | simulation_type = = SOLVE_BACKWARD_COMPLETE | | simulation_type = = SOLVE_FORWARD_COMPLETE )
{
2018-06-27 15:01:31 +02:00
Write_Inf_To_Bin_File_Block ( basename , block , u_count_int , file_open ) ;
2009-12-16 18:13:23 +01:00
file_open = true ;
}
FBEGINBLOCK_ fbeginblock ( block_mfs ,
simulation_type ,
getBlockFirstEquation ( block ) ,
block_size ,
variable_reordered ,
equation_reordered ,
blocks_linear [ block ] ,
symbol_table . endo_nbr ( ) ,
0 ,
0 ,
2010-07-23 11:20:24 +02:00
u_count_int ,
2011-02-04 16:25:38 +01:00
/*symbol_table.endo_nbr()*/ block_size
2009-12-16 18:13:23 +01:00
) ;
2010-10-11 19:21:32 +02:00
2010-07-23 11:20:24 +02:00
fbeginblock . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
2010-12-31 16:46:09 +01:00
// Get the current code_file position and jump if eval = true
streampos pos1 = code_file . tellp ( ) ;
FJMPIFEVAL_ fjmp_if_eval ( 0 ) ;
fjmp_if_eval . write ( code_file , instruction_number ) ;
int prev_instruction_number = instruction_number ;
2009-12-16 18:13:23 +01:00
for ( i = 0 ; i < ( int ) block_size ; i + + )
{
//The Temporary terms
2010-09-16 19:00:48 +02:00
temporary_terms_t tt2 ;
2009-12-16 18:13:23 +01:00
tt2 . clear ( ) ;
if ( v_temporary_terms [ block ] . size ( ) )
{
2018-06-04 12:26:16 +02:00
for ( auto it : v_temporary_terms [ block ] [ i ] )
2009-12-16 18:13:23 +01:00
{
2018-06-04 12:52:14 +02:00
if ( dynamic_cast < AbstractExternalFunctionNode * > ( it ) ! = nullptr )
2018-06-04 12:26:16 +02:00
it - > compileExternalFunctionOutput ( code_file , instruction_number , false , tt2 , map_idx , false , false , tef_terms ) ;
2010-12-10 11:50:27 +01:00
2018-06-04 12:26:16 +02:00
FNUMEXPR_ fnumexpr ( TemporaryTerm , ( int ) ( map_idx . find ( it - > idx ) - > second ) ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2018-06-04 12:26:16 +02:00
it - > compile ( code_file , instruction_number , false , tt2 , map_idx , false , false , tef_terms ) ;
FSTPST_ fstpst ( ( int ) ( map_idx . find ( it - > idx ) - > second ) ) ;
2010-07-23 11:20:24 +02:00
fstpst . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
// Insert current node into tt2
2018-06-04 12:26:16 +02:00
tt2 . insert ( it ) ;
2009-12-16 18:13:23 +01:00
}
}
2010-10-11 19:21:32 +02:00
// The equations
2009-12-16 18:13:23 +01:00
int variable_ID , equation_ID ;
EquationType equ_type ;
switch ( simulation_type )
{
evaluation :
case EVALUATE_BACKWARD :
case EVALUATE_FORWARD :
equ_type = getBlockEquationType ( block , i ) ;
2010-01-22 17:42:08 +01:00
{
FNUMEXPR_ fnumexpr ( ModelEquation , getBlockEquationID ( block , i ) ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-01-22 17:42:08 +01:00
}
2009-12-16 18:13:23 +01:00
if ( equ_type = = E_EVALUATE )
{
2010-09-16 19:18:45 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
2009-12-16 18:13:23 +01:00
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-07-23 11:20:24 +02:00
rhs - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
lhs - > compile ( code_file , instruction_number , true , temporary_terms , map_idx , false , false ) ;
2009-12-16 18:13:23 +01:00
}
else if ( equ_type = = E_EVALUATE_S )
{
2010-09-16 19:18:45 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationRenormalizedExpr ( block , i ) ;
2009-12-16 18:13:23 +01:00
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-07-23 11:20:24 +02:00
rhs - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
lhs - > compile ( code_file , instruction_number , true , temporary_terms , map_idx , false , false ) ;
2009-12-16 18:13:23 +01:00
}
break ;
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
if ( i < ( int ) block_recursive )
goto evaluation ;
variable_ID = getBlockVariableID ( block , i ) ;
equation_ID = getBlockEquationID ( block , i ) ;
feedback_variables . push_back ( variable_ID ) ;
2018-06-04 12:52:14 +02:00
Uf [ equation_ID ] . Ufl = nullptr ;
2009-12-16 18:13:23 +01:00
goto end ;
default :
end :
2010-01-22 17:42:08 +01:00
FNUMEXPR_ fnumexpr ( ModelEquation , getBlockEquationID ( block , i ) ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-09-16 19:18:45 +02:00
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
2009-12-16 18:13:23 +01:00
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-07-23 11:20:24 +02:00
lhs - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
rhs - > compile ( code_file , instruction_number , false , temporary_terms , map_idx , false , false ) ;
2009-12-16 18:13:23 +01:00
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : minus ) } ;
2010-07-23 11:20:24 +02:00
fbinary . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
FSTPR_ fstpr ( i - block_recursive ) ;
2010-07-23 11:20:24 +02:00
fstpr . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
}
}
FENDEQU_ fendequ ;
2010-07-23 11:20:24 +02:00
fendequ . write ( code_file , instruction_number ) ;
2010-12-10 11:50:27 +01:00
2009-12-16 18:13:23 +01:00
// The Jacobian if we have to solve the block
if ( simulation_type ! = EVALUATE_BACKWARD
& & simulation_type ! = EVALUATE_FORWARD )
{
switch ( simulation_type )
{
case SOLVE_BACKWARD_SIMPLE :
case SOLVE_FORWARD_SIMPLE :
2010-01-22 17:42:08 +01:00
{
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , 0 , 0 ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-01-22 17:42:08 +01:00
}
2010-10-11 19:21:32 +02:00
compileDerivative ( code_file , instruction_number , getBlockEquationID ( block , 0 ) , getBlockVariableID ( block , 0 ) , map_idx , temporary_terms ) ;
2009-12-16 14:21:31 +01:00
{
2009-12-16 18:13:23 +01:00
FSTPG_ fstpg ( 0 ) ;
2010-07-23 11:20:24 +02:00
fstpg . write ( code_file , instruction_number ) ;
2009-12-16 14:21:31 +01:00
}
2009-12-16 18:13:23 +01:00
break ;
2009-12-16 14:21:31 +01:00
2009-12-16 18:13:23 +01:00
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
count_u = feedback_variables . size ( ) ;
2018-06-04 15:03:26 +02:00
for ( auto it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
2009-12-16 18:13:23 +01:00
{
unsigned int eq = it - > first . first ;
unsigned int var = it - > first . second ;
unsigned int eqr = getBlockEquationID ( block , eq ) ;
unsigned int varr = getBlockVariableID ( block , var ) ;
2010-04-28 16:03:32 +02:00
if ( eq > = block_recursive & & var > = block_recursive )
2009-12-16 18:13:23 +01:00
{
if ( ! Uf [ eqr ] . Ufl )
{
Uf [ eqr ] . Ufl = ( Uff_l * ) malloc ( sizeof ( Uff_l ) ) ;
Uf [ eqr ] . Ufl_First = Uf [ eqr ] . Ufl ;
}
else
{
Uf [ eqr ] . Ufl - > pNext = ( Uff_l * ) malloc ( sizeof ( Uff_l ) ) ;
Uf [ eqr ] . Ufl = Uf [ eqr ] . Ufl - > pNext ;
}
2018-06-04 12:52:14 +02:00
Uf [ eqr ] . Ufl - > pNext = nullptr ;
2009-12-16 18:13:23 +01:00
Uf [ eqr ] . Ufl - > u = count_u ;
Uf [ eqr ] . Ufl - > var = varr ;
2010-01-22 17:42:08 +01:00
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , eqr , varr ) ;
2010-07-23 11:20:24 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-10-11 19:21:32 +02:00
compileChainRuleDerivative ( code_file , instruction_number , eqr , varr , 0 , map_idx , temporary_terms ) ;
2009-12-16 18:13:23 +01:00
FSTPSU_ fstpsu ( count_u ) ;
2010-07-23 11:20:24 +02:00
fstpsu . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
count_u + + ;
}
}
for ( i = 0 ; i < ( int ) block_size ; i + + )
{
if ( i > = ( int ) block_recursive )
{
FLDR_ fldr ( i - block_recursive ) ;
2010-07-23 11:20:24 +02:00
fldr . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
FLDZ_ fldz ;
2010-07-23 11:20:24 +02:00
fldz . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
v = getBlockEquationID ( block , i ) ;
for ( Uf [ v ] . Ufl = Uf [ v ] . Ufl_First ; Uf [ v ] . Ufl ; Uf [ v ] . Ufl = Uf [ v ] . Ufl - > pNext )
{
FLDSU_ fldsu ( Uf [ v ] . Ufl - > u ) ;
2010-07-23 11:20:24 +02:00
fldsu . write ( code_file , instruction_number ) ;
2018-07-18 15:12:00 +02:00
FLDSV_ fldsv { static_cast < int > ( SymbolType : : endogenous ) , static_cast < unsigned int > ( Uf [ v ] . Ufl - > var ) } ;
2010-07-23 11:20:24 +02:00
fldsv . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : times ) } ;
2010-07-23 11:20:24 +02:00
fbinary . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
FCUML_ fcuml ;
2010-07-23 11:20:24 +02:00
fcuml . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
}
Uf [ v ] . Ufl = Uf [ v ] . Ufl_First ;
while ( Uf [ v ] . Ufl )
{
Uf [ v ] . Ufl_First = Uf [ v ] . Ufl - > pNext ;
free ( Uf [ v ] . Ufl ) ;
Uf [ v ] . Ufl = Uf [ v ] . Ufl_First ;
}
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : minus ) } ;
2010-07-23 11:20:24 +02:00
fbinary . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
FSTPSU_ fstpsu ( i - block_recursive ) ;
2010-07-23 11:20:24 +02:00
fstpsu . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
}
}
break ;
default :
break ;
}
}
2010-10-11 19:21:32 +02:00
// Get the current code_file position and jump = true
streampos pos2 = code_file . tellp ( ) ;
FJMP_ fjmp ( 0 ) ;
fjmp . write ( code_file , instruction_number ) ;
// Set code_file position to previous JMPIFEVAL_ and set the number of instructions to jump
streampos pos3 = code_file . tellp ( ) ;
code_file . seekp ( pos1 ) ;
FJMPIFEVAL_ fjmp_if_eval1 ( instruction_number - prev_instruction_number ) ;
fjmp_if_eval1 . write ( code_file , instruction_number ) ;
code_file . seekp ( pos3 ) ;
2011-02-04 16:25:38 +01:00
prev_instruction_number = instruction_number ;
2010-10-11 19:21:32 +02:00
temporary_terms_t tt2 ;
tt2 . clear ( ) ;
temporary_terms_t tt3 ;
tt3 . clear ( ) ;
2010-12-31 16:46:09 +01:00
deriv_node_temp_terms_t tef_terms2 ;
2010-10-11 19:21:32 +02:00
for ( i = 0 ; i < ( int ) block_size ; i + + )
{
if ( v_temporary_terms_local [ block ] . size ( ) )
{
2018-06-04 15:03:26 +02:00
for ( auto it = v_temporary_terms_local [ block ] [ i ] . begin ( ) ;
2010-10-11 19:21:32 +02:00
it ! = v_temporary_terms_local [ block ] [ i ] . end ( ) ; it + + )
{
2018-06-04 12:52:14 +02:00
if ( dynamic_cast < AbstractExternalFunctionNode * > ( * it ) ! = nullptr )
2010-12-31 16:46:09 +01:00
( * it ) - > compileExternalFunctionOutput ( code_file , instruction_number , false , tt3 , map_idx2 [ block ] , false , false , tef_terms2 ) ;
2010-12-10 11:50:27 +01:00
2017-06-14 07:01:31 +02:00
FNUMEXPR_ fnumexpr ( TemporaryTerm , ( int ) ( map_idx2 [ block ] . find ( ( * it ) - > idx ) - > second ) ) ;
2010-10-11 19:21:32 +02:00
fnumexpr . write ( code_file , instruction_number ) ;
2010-12-10 11:50:27 +01:00
( * it ) - > compile ( code_file , instruction_number , false , tt3 , map_idx2 [ block ] , false , false , tef_terms ) ;
2017-06-14 07:01:31 +02:00
FSTPST_ fstpst ( ( int ) ( map_idx2 [ block ] . find ( ( * it ) - > idx ) - > second ) ) ;
2010-10-11 19:21:32 +02:00
fstpst . write ( code_file , instruction_number ) ;
// Insert current node into tt2
tt3 . insert ( * it ) ;
tt2 . insert ( * it ) ;
}
}
// The equations
int variable_ID , equation_ID ;
EquationType equ_type ;
switch ( simulation_type )
{
evaluation_l :
case EVALUATE_BACKWARD :
case EVALUATE_FORWARD :
equ_type = getBlockEquationType ( block , i ) ;
{
FNUMEXPR_ fnumexpr ( ModelEquation , getBlockEquationID ( block , i ) ) ;
fnumexpr . write ( code_file , instruction_number ) ;
}
if ( equ_type = = E_EVALUATE )
{
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-12-31 16:46:09 +01:00
rhs - > compile ( code_file , instruction_number , false , tt2 , map_idx2 [ block ] , false , false ) ;
lhs - > compile ( code_file , instruction_number , true , tt2 , map_idx2 [ block ] , false , false ) ;
2010-10-11 19:21:32 +02:00
}
else if ( equ_type = = E_EVALUATE_S )
{
eq_node = ( BinaryOpNode * ) getBlockEquationRenormalizedExpr ( block , i ) ;
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-12-31 16:46:09 +01:00
rhs - > compile ( code_file , instruction_number , false , tt2 , map_idx2 [ block ] , false , false ) ;
lhs - > compile ( code_file , instruction_number , true , tt2 , map_idx2 [ block ] , false , false ) ;
2010-10-11 19:21:32 +02:00
}
break ;
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
if ( i < ( int ) block_recursive )
goto evaluation_l ;
variable_ID = getBlockVariableID ( block , i ) ;
equation_ID = getBlockEquationID ( block , i ) ;
feedback_variables . push_back ( variable_ID ) ;
2018-06-04 12:52:14 +02:00
Uf [ equation_ID ] . Ufl = nullptr ;
2010-10-11 19:21:32 +02:00
goto end_l ;
default :
end_l :
FNUMEXPR_ fnumexpr ( ModelEquation , getBlockEquationID ( block , i ) ) ;
fnumexpr . write ( code_file , instruction_number ) ;
eq_node = ( BinaryOpNode * ) getBlockEquationExpr ( block , i ) ;
lhs = eq_node - > get_arg1 ( ) ;
rhs = eq_node - > get_arg2 ( ) ;
2010-12-31 16:46:09 +01:00
lhs - > compile ( code_file , instruction_number , false , tt2 , map_idx2 [ block ] , false , false ) ;
rhs - > compile ( code_file , instruction_number , false , tt2 , map_idx2 [ block ] , false , false ) ;
2010-10-11 19:21:32 +02:00
2018-07-18 16:18:26 +02:00
FBINARY_ fbinary { static_cast < int > ( BinaryOpcode : : minus ) } ;
2010-10-11 19:21:32 +02:00
fbinary . write ( code_file , instruction_number ) ;
FSTPR_ fstpr ( i - block_recursive ) ;
fstpr . write ( code_file , instruction_number ) ;
}
}
FENDEQU_ fendequ_l ;
fendequ_l . write ( code_file , instruction_number ) ;
// The Jacobian if we have to solve the block determinsitic bloc
switch ( simulation_type )
{
case SOLVE_BACKWARD_SIMPLE :
case SOLVE_FORWARD_SIMPLE :
{
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , 0 , 0 ) ;
fnumexpr . write ( code_file , instruction_number ) ;
}
2011-02-04 16:25:38 +01:00
compileDerivative ( code_file , instruction_number , getBlockEquationID ( block , 0 ) , getBlockVariableID ( block , 0 ) , map_idx2 [ block ] , tt2 /*temporary_terms*/ ) ;
2010-10-11 19:21:32 +02:00
{
2011-02-04 16:25:38 +01:00
FSTPG2_ fstpg2 ( 0 , 0 ) ;
2010-10-11 19:21:32 +02:00
fstpg2 . write ( code_file , instruction_number ) ;
}
break ;
case EVALUATE_BACKWARD :
case EVALUATE_FORWARD :
case SOLVE_BACKWARD_COMPLETE :
case SOLVE_FORWARD_COMPLETE :
count_u = feedback_variables . size ( ) ;
2018-06-04 15:03:26 +02:00
for ( auto it = blocks_derivatives [ block ] . begin ( ) ; it ! = ( blocks_derivatives [ block ] ) . end ( ) ; it + + )
2010-10-11 19:21:32 +02:00
{
unsigned int eq = it - > first . first ;
unsigned int var = it - > first . second ;
unsigned int eqr = getBlockEquationID ( block , eq ) ;
unsigned int varr = getBlockVariableID ( block , var ) ;
FNUMEXPR_ fnumexpr ( FirstEndoDerivative , eqr , varr , 0 ) ;
fnumexpr . write ( code_file , instruction_number ) ;
2011-02-04 16:25:38 +01:00
compileChainRuleDerivative ( code_file , instruction_number , eqr , varr , 0 , map_idx2 [ block ] , tt2 /*temporary_terms*/ ) ;
2010-10-11 19:21:32 +02:00
2011-02-04 16:25:38 +01:00
FSTPG2_ fstpg2 ( eq , var ) ;
2010-10-11 19:21:32 +02:00
fstpg2 . write ( code_file , instruction_number ) ;
}
break ;
default :
break ;
}
// Set codefile position to previous JMP_ and set the number of instructions to jump
pos1 = code_file . tellp ( ) ;
code_file . seekp ( pos2 ) ;
FJMP_ fjmp1 ( instruction_number - prev_instruction_number ) ;
fjmp1 . write ( code_file , instruction_number ) ;
code_file . seekp ( pos1 ) ;
2009-12-16 18:13:23 +01:00
}
FENDBLOCK_ fendblock ;
2010-07-23 11:20:24 +02:00
fendblock . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
FEND_ fend ;
2010-07-23 11:20:24 +02:00
fend . write ( code_file , instruction_number ) ;
2009-12-16 18:13:23 +01:00
code_file . close ( ) ;
}
2009-12-16 14:21:31 +01:00
void
2018-06-27 15:01:31 +02:00
StaticModel : : Write_Inf_To_Bin_File_Block ( const string & basename , const int & num ,
2011-02-04 16:25:38 +01:00
int & u_count_int , bool & file_open ) const
2009-12-16 18:13:23 +01:00
{
int j ;
std : : ofstream SaveCode ;
2018-06-27 15:01:31 +02:00
string filename = basename + " /model/bytecode/static.bin " ;
2009-12-16 18:13:23 +01:00
if ( file_open )
2018-06-27 15:01:31 +02:00
SaveCode . open ( filename , ios : : out | ios : : in | ios : : binary | ios : : ate ) ;
2009-12-16 18:13:23 +01:00
else
2018-06-27 15:01:31 +02:00
SaveCode . open ( filename , ios : : out | ios : : binary ) ;
2009-12-16 18:13:23 +01:00
if ( ! SaveCode . is_open ( ) )
{
2018-06-27 15:01:31 +02:00
cerr < < " Error : Can't open file " < < filename < < " for writing " < < endl ;
2009-12-16 18:13:23 +01:00
exit ( EXIT_FAILURE ) ;
}
u_count_int = 0 ;
unsigned int block_size = getBlockSize ( num ) ;
unsigned int block_mfs = getBlockMfs ( num ) ;
unsigned int block_recursive = block_size - block_mfs ;
2018-06-04 15:03:26 +02:00
for ( auto it = blocks_derivatives [ num ] . begin ( ) ; it ! = ( blocks_derivatives [ num ] ) . end ( ) ; it + + )
2009-12-16 18:13:23 +01:00
{
unsigned int eq = it - > first . first ;
unsigned int var = it - > first . second ;
int lag = 0 ;
2010-04-28 16:03:32 +02:00
if ( eq > = block_recursive & & var > = block_recursive )
2009-12-16 18:13:23 +01:00
{
int v = eq - block_recursive ;
SaveCode . write ( reinterpret_cast < char * > ( & v ) , sizeof ( v ) ) ;
int varr = var - block_recursive ;
SaveCode . write ( reinterpret_cast < char * > ( & varr ) , sizeof ( varr ) ) ;
SaveCode . write ( reinterpret_cast < char * > ( & lag ) , sizeof ( lag ) ) ;
int u = u_count_int + block_mfs ;
SaveCode . write ( reinterpret_cast < char * > ( & u ) , sizeof ( u ) ) ;
u_count_int + + ;
}
}
for ( j = block_recursive ; j < ( int ) block_size ; j + + )
{
unsigned int varr = getBlockVariableID ( num , j ) ;
SaveCode . write ( reinterpret_cast < char * > ( & varr ) , sizeof ( varr ) ) ;
}
for ( j = block_recursive ; j < ( int ) block_size ; j + + )
{
unsigned int eqr = getBlockEquationID ( num , j ) ;
SaveCode . write ( reinterpret_cast < char * > ( & eqr ) , sizeof ( eqr ) ) ;
}
SaveCode . close ( ) ;
}
2009-12-16 14:21:31 +01:00
2018-06-04 14:17:36 +02:00
map < pair < int , pair < int , int > > , expr_t >
2009-12-16 14:21:31 +01:00
StaticModel : : collect_first_order_derivatives_endogenous ( )
2009-04-14 16:39:53 +02:00
{
2018-06-04 14:17:36 +02:00
map < pair < int , pair < int , int > > , expr_t > endo_derivatives ;
2018-06-04 12:26:16 +02:00
for ( auto & first_derivative : first_derivatives )
2009-12-16 14:21:31 +01:00
{
2018-07-17 18:34:07 +02:00
if ( getTypeByDerivID ( first_derivative . first . second ) = = SymbolType : : endogenous )
2009-12-16 14:21:31 +01:00
{
2018-06-04 12:26:16 +02:00
int eq = first_derivative . first . first ;
int var = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( first_derivative . first . second ) ) ;
2009-12-16 14:21:31 +01:00
int lag = 0 ;
2018-06-04 16:36:46 +02:00
endo_derivatives [ { eq , { var , lag } } ] = first_derivative . second ;
2009-12-16 14:21:31 +01:00
}
}
2009-12-16 18:13:23 +01:00
return endo_derivatives ;
2009-12-16 14:21:31 +01:00
}
void
2017-09-28 14:52:10 +02:00
StaticModel : : computingPass ( const eval_context_t & eval_context , bool no_tmp_terms , bool hessian , bool thirdDerivatives , int paramsDerivsOrder , bool block , bool bytecode , const bool nopreprocessoroutput )
2009-12-16 14:21:31 +01:00
{
2011-06-22 11:34:25 +02:00
initializeVariablesAndEquations ( ) ;
2016-03-18 14:55:14 +01:00
vector < BinaryOpNode * > neweqs ;
2016-03-21 11:51:48 +01:00
for ( unsigned int eq = 0 ; eq < equations . size ( ) - aux_equations . size ( ) ; eq + + )
{
expr_t eq_tmp = equations [ eq ] - > substituteStaticAuxiliaryVariable ( ) ;
neweqs . push_back ( dynamic_cast < BinaryOpNode * > ( eq_tmp - > toStatic ( * this ) ) ) ;
}
2016-03-18 14:55:14 +01:00
2018-06-04 12:26:16 +02:00
for ( auto & aux_equation : aux_equations )
2016-03-21 20:42:35 +01:00
{
2018-06-04 12:26:16 +02:00
expr_t eq_tmp = aux_equation - > substituteStaticAuxiliaryDefinition ( ) ;
2016-03-21 20:42:35 +01:00
neweqs . push_back ( dynamic_cast < BinaryOpNode * > ( eq_tmp - > toStatic ( * this ) ) ) ;
}
2017-06-14 07:01:31 +02:00
2016-03-18 14:55:14 +01:00
equations . clear ( ) ;
2017-06-14 07:01:31 +02:00
copy ( neweqs . begin ( ) , neweqs . end ( ) , back_inserter ( equations ) ) ;
2009-12-16 18:13:23 +01:00
// Compute derivatives w.r. to all endogenous, and possibly exogenous and exogenous deterministic
2009-12-16 14:21:31 +01:00
set < int > vars ;
2009-12-16 18:13:23 +01:00
for ( int i = 0 ; i < symbol_table . endo_nbr ( ) ; i + + )
2016-03-21 11:51:48 +01:00
{
2018-07-17 18:34:07 +02:00
int id = symbol_table . getID ( SymbolType : : endogenous , i ) ;
2016-03-21 20:42:35 +01:00
// if (!symbol_table.isAuxiliaryVariableButNotMultiplier(id))
vars . insert ( getDerivID ( id , 0 ) ) ;
2017-06-14 07:01:31 +02:00
}
2009-12-16 14:21:31 +01:00
// Launch computations
2017-09-28 14:52:10 +02:00
if ( ! nopreprocessoroutput )
cout < < " Computing static model derivatives: " < < endl
< < " - order 1 " < < endl ;
2009-12-16 14:21:31 +01:00
first_derivatives . clear ( ) ;
computeJacobian ( vars ) ;
if ( hessian )
{
2017-09-28 14:52:10 +02:00
if ( ! nopreprocessoroutput )
cout < < " - order 2 " < < endl ;
2009-12-16 14:21:31 +01:00
computeHessian ( vars ) ;
}
2013-11-22 21:09:04 +01:00
if ( thirdDerivatives )
{
2017-09-28 14:52:10 +02:00
if ( ! nopreprocessoroutput )
cout < < " - order 3 " < < endl ;
2013-11-22 21:09:04 +01:00
computeThirdDerivatives ( vars ) ;
}
2016-05-18 12:26:19 +02:00
if ( paramsDerivsOrder > 0 )
2012-11-29 18:07:48 +01:00
{
2017-09-28 14:52:10 +02:00
if ( ! nopreprocessoroutput )
cout < < " - derivatives of Jacobian/Hessian w.r. to parameters " < < endl ;
2016-05-18 12:26:19 +02:00
computeParamsDerivatives ( paramsDerivsOrder ) ;
2012-11-29 18:07:48 +01:00
if ( ! no_tmp_terms )
computeParamsDerivativesTemporaryTerms ( ) ;
}
2009-12-16 14:21:31 +01:00
if ( block )
{
2010-09-16 19:00:48 +02:00
jacob_map_t contemporaneous_jacobian , static_jacobian ;
2010-07-23 11:20:24 +02:00
vector < unsigned int > n_static , n_forward , n_backward , n_mixed ;
2009-12-16 14:21:31 +01:00
// for each block contains pair<Size, Feddback_variable>
2018-06-04 14:17:36 +02:00
vector < pair < int , int > > blocks ;
2009-12-16 14:21:31 +01:00
evaluateAndReduceJacobian ( eval_context , contemporaneous_jacobian , static_jacobian , dynamic_jacobian , cutoff , false ) ;
2009-12-21 11:29:21 +01:00
computeNonSingularNormalization ( contemporaneous_jacobian , cutoff , static_jacobian , dynamic_jacobian ) ;
2009-12-16 14:21:31 +01:00
2010-09-16 17:51:50 +02:00
computePrologueAndEpilogue ( static_jacobian , equation_reordered , variable_reordered ) ;
2009-12-16 14:21:31 +01:00
2018-06-04 14:17:36 +02:00
map < pair < int , pair < int , int > > , expr_t > first_order_endo_derivatives = collect_first_order_derivatives_endogenous ( ) ;
2009-12-16 14:21:31 +01:00
2010-09-16 17:51:50 +02:00
equation_type_and_normalized_equation = equationTypeDetermination ( first_order_endo_derivatives , variable_reordered , equation_reordered , mfs ) ;
2009-12-16 14:21:31 +01:00
2017-09-28 14:52:10 +02:00
if ( ! nopreprocessoroutput )
cout < < " Finding the optimal block decomposition of the model ... \n " ;
2009-12-16 14:21:31 +01:00
2010-07-23 11:20:24 +02:00
lag_lead_vector_t equation_lag_lead , variable_lag_lead ;
computeBlockDecompositionAndFeedbackVariablesForEachBlock ( static_jacobian , dynamic_jacobian , equation_reordered , variable_reordered , blocks , equation_type_and_normalized_equation , false , false , mfs , inv_equation_reordered , inv_variable_reordered , equation_lag_lead , variable_lag_lead , n_static , n_forward , n_backward , n_mixed ) ;
2009-12-16 14:21:31 +01:00
2018-09-21 17:13:19 +02:00
block_type_firstequation_size_mfs = reduceBlocksAndTypeDetermination ( dynamic_jacobian , blocks , equation_type_and_normalized_equation , variable_reordered , equation_reordered , n_static , n_forward , n_backward , n_mixed , block_col_type , false ) ;
2009-12-16 14:21:31 +01:00
printBlockDecomposition ( blocks ) ;
computeChainRuleJacobian ( blocks_derivatives ) ;
blocks_linear = BlockLinear ( blocks_derivatives , variable_reordered ) ;
collect_block_first_order_derivatives ( ) ;
global_temporary_terms = true ;
if ( ! no_tmp_terms )
computeTemporaryTermsOrdered ( ) ;
}
else
2010-01-22 11:03:29 +01:00
{
2018-09-25 15:56:52 +02:00
computeTemporaryTerms ( true , no_tmp_terms ) ;
if ( bytecode & & ! no_tmp_terms )
computeTemporaryTermsMapping ( temporary_terms , map_idx ) ;
2010-01-22 11:03:29 +01:00
}
2009-04-14 16:39:53 +02:00
}
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeStaticMFile ( const string & basename ) const
2009-04-14 16:39:53 +02:00
{
2018-06-27 15:01:31 +02:00
writeStaticModel ( basename , false , false ) ;
2018-03-27 17:14:30 +02:00
}
2009-12-16 14:21:31 +01:00
2018-03-27 17:14:30 +02:00
void
StaticModel : : writeWrapperFunctions ( const string & basename , const string & ending ) const
{
string name ;
if ( ending = = " g1 " )
2018-06-27 15:01:31 +02:00
name = " static_resid_g1 " ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g2 " )
2018-06-27 15:01:31 +02:00
name = " static_resid_g1_g2 " ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g3 " )
2018-06-27 15:01:31 +02:00
name = " static_resid_g1_g2_g3 " ;
2018-03-27 17:14:30 +02:00
2018-06-27 15:01:31 +02:00
string filename = packageDir ( basename ) + " / " + name + " .m " ;
2009-12-16 14:21:31 +01:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2009-12-16 14:21:31 +01:00
if ( ! output . is_open ( ) )
{
2018-03-27 17:14:30 +02:00
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( ending = = " g1 " )
2018-05-23 17:32:50 +02:00
output < < " function [residual, g1] = " < < name < < " (T, y, x, params, T_flag) " < < endl
< < " % function [residual, g1] = " < < name < < " (T, y, x, params, T_flag) " < < endl ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g2 " )
2018-05-23 17:32:50 +02:00
output < < " function [residual, g1, g2] = " < < name < < " (T, y, x, params, T_flag) " < < endl
< < " % function [residual, g1, g2] = " < < name < < " (T, y, x, params, T_flag) " < < endl ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g3 " )
2018-05-23 17:32:50 +02:00
output < < " function [residual, g1, g2, g3] = " < < name < < " (T, y, x, params, T_flag) " < < endl
< < " % function [residual, g1, g2, g3] = " < < name < < " (T, y, x, params, T_flag) " < < endl ;
2018-03-27 17:14:30 +02:00
output < < " % " < < endl
< < " % Wrapper function automatically created by Dynare " < < endl
2018-05-23 17:32:50 +02:00
< < " % " < < endl
< < endl
< < " if T_flag " < < endl
2018-06-27 15:01:31 +02:00
< < " T = " < < basename < < " .static_ " < < ending < < " _tt(T, y, x, params); " < < endl
2018-05-23 17:32:50 +02:00
< < " end " < < endl ;
2018-03-27 17:14:30 +02:00
if ( ending = = " g1 " )
2018-06-27 15:01:31 +02:00
output < < " residual = " < < basename < < " .static_resid(T, y, x, params, false); " < < endl
< < " g1 = " < < basename < < " .static_g1(T, y, x, params, false); " < < endl ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g2 " )
2018-06-27 15:01:31 +02:00
output < < " [residual, g1] = " < < basename < < " .static_resid_g1(T, y, x, params, false); " < < endl
< < " g2 = " < < basename < < " .static_g2(T, y, x, params, false); " < < endl ;
2018-03-27 17:14:30 +02:00
else if ( ending = = " g3 " )
2018-06-27 15:01:31 +02:00
output < < " [residual, g1, g2] = " < < basename < < " .static_resid_g1_g2(T, y, x, params, false); " < < endl
< < " g3 = " < < basename < < " .static_g3(T, y, x, params, false); " < < endl ;
2018-03-27 17:14:30 +02:00
output < < endl < < " end " < < endl ;
output . close ( ) ;
}
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeStaticModelHelper ( const string & basename ,
const string & name , const string & retvalname ,
2018-03-27 17:14:30 +02:00
const string & name_tt , size_t ttlen ,
const string & previous_tt_name ,
const ostringstream & init_s , const ostringstream & end_s ,
const ostringstream & s , const ostringstream & s_tt ) const
{
2018-06-27 15:01:31 +02:00
string filename = packageDir ( basename ) + " / " + name_tt + " .m " ;
2018-03-27 17:14:30 +02:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2018-03-27 17:14:30 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
2009-12-16 14:21:31 +01:00
exit ( EXIT_FAILURE ) ;
}
2018-03-27 17:14:30 +02:00
output < < " function T = " < < name_tt < < " (T, y, x, params) " < < endl
< < " % function T = " < < name_tt < < " (T, y, x, params) " < < endl
2009-07-07 16:20:48 +02:00
< < " % " < < endl
2018-03-27 17:14:30 +02:00
< < " % File created by Dynare Preprocessor from .mod file " < < endl
2009-07-07 16:20:48 +02:00
< < " % " < < endl
2018-03-27 17:14:30 +02:00
< < " % Inputs: " < < endl
< < " % T [#temp variables by 1] double vector of temporary terms to be filled by function " < < endl
< < " % y [M_.endo_nbr by 1] double vector of endogenous variables in declaration order " < < endl
< < " % x [M_.exo_nbr by 1] double vector of exogenous variables in declaration order " < < endl
< < " % params [M_.param_nbr by 1] double vector of parameter values in declaration order " < < endl
2013-07-28 11:32:14 +02:00
< < " % " < < endl
2018-03-27 17:14:30 +02:00
< < " % Output: " < < endl
< < " % T [#temp variables by 1] double vector of temporary terms " < < endl
< < " % " < < endl < < endl
< < " assert(length(T) >= " < < ttlen < < " ); " < < endl
< < endl ;
if ( ! previous_tt_name . empty ( ) )
2018-06-27 15:01:31 +02:00
output < < " T = " < < basename < < " . " < < previous_tt_name < < " (T, y, x, params); " < < endl < < endl ;
2018-03-27 17:14:30 +02:00
output < < s_tt . str ( ) < < endl
< < " end " < < endl ;
output . close ( ) ;
2018-06-27 15:01:31 +02:00
filename = packageDir ( basename ) + " / " + name + " .m " ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2018-03-27 17:14:30 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
output < < " function " < < retvalname < < " = " < < name < < " (T, y, x, params, T_flag) " < < endl
< < " % function " < < retvalname < < " = " < < name < < " (T, y, x, params, T_flag) " < < endl
2013-07-28 11:32:14 +02:00
< < " % " < < endl
2018-03-27 17:14:30 +02:00
< < " % File created by Dynare Preprocessor from .mod file " < < endl
2017-06-14 07:01:31 +02:00
< < " % " < < endl
2018-03-27 17:14:30 +02:00
< < " % Inputs: " < < endl
< < " % T [#temp variables by 1] double vector of temporary terms to be filled by function " < < endl
< < " % y [M_.endo_nbr by 1] double vector of endogenous variables in declaration order " < < endl
< < " % x [M_.exo_nbr by 1] double vector of exogenous variables in declaration order " < < endl
< < " % params [M_.param_nbr by 1] double vector of parameter values in declaration order " < < endl
< < " % to evaluate the model " < < endl
< < " % T_flag boolean boolean flag saying whether or not to calculate temporary terms " < < endl
< < " % " < < endl
< < " % Output: " < < endl
< < " % " < < retvalname < < endl
< < " % " < < endl < < endl ;
if ( ! name_tt . empty ( ) )
output < < " if T_flag " < < endl
2018-06-27 15:01:31 +02:00
< < " T = " < < basename < < " . " < < name_tt < < " (T, y, x, params); " < < endl
2018-03-27 17:14:30 +02:00
< < " end " < < endl ;
output < < init_s . str ( ) < < endl
< < s . str ( )
< < end_s . str ( ) < < endl
< < " end " < < endl ;
2011-08-18 12:44:11 +02:00
output . close ( ) ;
}
2018-05-23 16:10:26 +02:00
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeStaticMatlabCompatLayer ( const string & basename ) const
2018-05-23 16:10:26 +02:00
{
2018-06-27 15:01:31 +02:00
string filename = packageDir ( basename ) + " /static.m " ;
2018-05-23 16:10:26 +02:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2018-05-23 16:10:26 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
int ntt = temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) + temporary_terms_g2 . size ( ) + temporary_terms_g3 . size ( ) ;
2018-06-27 15:01:31 +02:00
output < < " function [residual, g1, g2, g3] = static(y, x, params) " < < endl
2018-05-23 16:10:26 +02:00
< < " T = NaN( " < < ntt < < " , 1); " < < endl
< < " if nargout <= 1 " < < endl
2018-06-27 15:01:31 +02:00
< < " residual = " < < basename < < " .static_resid(T, y, x, params, true); " < < endl
2018-05-23 16:10:26 +02:00
< < " elseif nargout == 2 " < < endl
2018-06-27 15:01:31 +02:00
< < " [residual, g1] = " < < basename < < " .static_resid_g1(T, y, x, params, true); " < < endl
2018-05-23 16:10:26 +02:00
< < " elseif nargout == 3 " < < endl
2018-06-27 15:01:31 +02:00
< < " [residual, g1, g2] = " < < basename < < " .static_resid_g1_g2(T, y, x, params, true); " < < endl
2018-05-23 16:10:26 +02:00
< < " else " < < endl
2018-06-27 15:01:31 +02:00
< < " [residual, g1, g2, g3] = " < < basename < < " .static_resid_g1_g2_g3(T, y, x, params, true); " < < endl
2018-05-23 16:10:26 +02:00
< < " end " < < endl
< < " end " < < endl ;
output . close ( ) ;
}
2011-08-18 12:44:11 +02:00
void
2015-07-27 15:33:38 +02:00
StaticModel : : writeStaticModel ( ostream & StaticOutput , bool use_dll , bool julia ) const
2011-08-18 12:44:11 +02:00
{
2018-03-27 17:14:30 +02:00
writeStaticModel ( " " , StaticOutput , use_dll , julia ) ;
}
void
StaticModel : : writeStaticModel ( const string & basename , bool use_dll , bool julia ) const
{
ofstream StaticOutput ;
writeStaticModel ( basename , StaticOutput , use_dll , julia ) ;
}
void
StaticModel : : writeStaticModel ( const string & basename ,
ostream & StaticOutput , bool use_dll , bool julia ) const
{
ostringstream model_tt_output ; // Used for storing model temp vars
ostringstream model_output ; // Used for storing model equations
ostringstream jacobian_tt_output ; // Used for storing jacobian temp vars
ostringstream jacobian_output ; // Used for storing jacobian equations
ostringstream hessian_tt_output ; // Used for storing Hessian temp vars
ostringstream hessian_output ; // Used for storing Hessian equations
ostringstream third_derivatives_tt_output ; // Used for storing third order derivatives temp terms
ostringstream third_derivatives_output ; // Used for storing third order derivatives equations
2015-08-20 14:41:15 +02:00
ostringstream for_sym ;
2018-03-27 17:14:30 +02:00
2018-09-05 18:27:13 +02:00
ExprNodeOutputType output_type = ( use_dll ? ExprNodeOutputType : : CStaticModel :
julia ? ExprNodeOutputType : : juliaStaticModel : ExprNodeOutputType : : matlabStaticModel ) ;
2009-07-07 16:20:48 +02:00
2011-08-18 12:44:11 +02:00
deriv_node_temp_terms_t tef_terms ;
2018-03-27 17:14:30 +02:00
temporary_terms_t temp_term_union ;
2018-06-04 12:26:16 +02:00
for ( auto it : temporary_terms_mlv )
temp_term_union . insert ( it . first ) ;
2018-03-27 17:14:30 +02:00
writeModelLocalVariableTemporaryTerms ( temp_term_union , temporary_terms_mlv ,
model_tt_output , output_type , tef_terms ) ;
2018-05-24 19:29:53 +02:00
writeTemporaryTerms ( temporary_terms_res ,
temp_term_union ,
2018-05-28 15:23:15 +02:00
temporary_terms_idxs ,
2018-03-27 17:14:30 +02:00
model_tt_output , output_type , tef_terms ) ;
temp_term_union . insert ( temporary_terms_res . begin ( ) , temporary_terms_res . end ( ) ) ;
2018-05-24 19:29:53 +02:00
writeModelEquations ( model_output , output_type , temp_term_union ) ;
2009-04-14 16:39:53 +02:00
2013-11-22 21:09:04 +01:00
int nrows = equations . size ( ) ;
int JacobianColsNbr = symbol_table . endo_nbr ( ) ;
int hessianColsNbr = JacobianColsNbr * JacobianColsNbr ;
2009-04-14 16:39:53 +02:00
// Write Jacobian w.r. to endogenous only
2015-09-03 13:50:02 +02:00
if ( ! first_derivatives . empty ( ) )
2018-03-27 17:14:30 +02:00
{
2018-05-24 19:29:53 +02:00
writeTemporaryTerms ( temporary_terms_g1 ,
temp_term_union ,
2018-05-28 15:23:15 +02:00
temporary_terms_idxs ,
2018-03-27 17:14:30 +02:00
jacobian_tt_output , output_type , tef_terms ) ;
temp_term_union . insert ( temporary_terms_g1 . begin ( ) , temporary_terms_g1 . end ( ) ) ;
}
2018-06-04 12:26:16 +02:00
for ( const auto & first_derivative : first_derivatives )
2009-04-14 16:39:53 +02:00
{
2018-06-05 15:34:34 +02:00
int eq , var ;
tie ( eq , var ) = first_derivative . first ;
2018-06-04 12:26:16 +02:00
expr_t d1 = first_derivative . second ;
2018-06-05 15:34:34 +02:00
int symb_id = getSymbIDByDerivID ( var ) ;
2009-04-14 16:39:53 +02:00
2011-08-18 12:44:11 +02:00
jacobianHelper ( jacobian_output , eq , symbol_table . getTypeSpecificID ( symb_id ) , output_type ) ;
jacobian_output < < " = " ;
2018-03-27 17:14:30 +02:00
d1 - > writeOutput ( jacobian_output , output_type ,
2018-05-24 19:29:53 +02:00
temp_term_union , temporary_terms_idxs , tef_terms ) ;
2011-08-18 12:44:11 +02:00
jacobian_output < < " ; " < < endl ;
2009-04-14 16:39:53 +02:00
}
2009-07-07 16:20:48 +02:00
int g2ncols = symbol_table . endo_nbr ( ) * symbol_table . endo_nbr ( ) ;
2011-08-18 12:44:11 +02:00
// Write Hessian w.r. to endogenous only (only if 2nd order derivatives have been computed)
2015-09-03 13:50:02 +02:00
if ( ! second_derivatives . empty ( ) )
2009-04-20 12:48:54 +02:00
{
2018-05-24 19:29:53 +02:00
writeTemporaryTerms ( temporary_terms_g2 ,
temp_term_union ,
2018-05-28 15:23:15 +02:00
temporary_terms_idxs ,
2018-03-27 17:14:30 +02:00
hessian_tt_output , output_type , tef_terms ) ;
temp_term_union . insert ( temporary_terms_g2 . begin ( ) , temporary_terms_g2 . end ( ) ) ;
int k = 0 ; // Keep the line of a 2nd derivative in v2
2018-06-04 12:26:16 +02:00
for ( const auto & second_derivative : second_derivatives )
2018-03-27 17:14:30 +02:00
{
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 ;
tie ( eq , var1 , var2 ) = second_derivative . first ;
2018-06-04 12:26:16 +02:00
expr_t d2 = second_derivative . second ;
2009-04-20 12:48:54 +02:00
2018-06-05 15:34:34 +02:00
int symb_id1 = getSymbIDByDerivID ( var1 ) ;
int symb_id2 = getSymbIDByDerivID ( var2 ) ;
2018-03-27 17:14:30 +02:00
int tsid1 = symbol_table . getTypeSpecificID ( symb_id1 ) ;
int tsid2 = symbol_table . getTypeSpecificID ( symb_id2 ) ;
2009-07-07 16:20:48 +02:00
2018-03-27 17:14:30 +02:00
int col_nb = tsid1 * symbol_table . endo_nbr ( ) + tsid2 ;
int col_nb_sym = tsid2 * symbol_table . endo_nbr ( ) + tsid1 ;
2009-06-25 11:56:41 +02:00
2018-09-05 18:27:13 +02:00
if ( output_type = = ExprNodeOutputType : : juliaStaticModel )
2018-03-27 17:14:30 +02:00
{
for_sym < < " g2[ " < < eq + 1 < < " , " < < col_nb + 1 < < " ] " ;
hessian_output < < " @inbounds " < < for_sym . str ( ) < < " = " ;
2018-05-28 11:59:14 +02:00
d2 - > writeOutput ( hessian_output , output_type , temp_term_union , temporary_terms_idxs , tef_terms ) ;
2018-03-27 17:14:30 +02:00
hessian_output < < endl ;
}
else
{
sparseHelper ( 2 , hessian_output , k , 0 , output_type ) ;
hessian_output < < " = " < < eq + 1 < < " ; " < < endl ;
2011-08-18 12:44:11 +02:00
2018-03-27 17:14:30 +02:00
sparseHelper ( 2 , hessian_output , k , 1 , output_type ) ;
hessian_output < < " = " < < col_nb + 1 < < " ; " < < endl ;
2011-08-18 12:44:11 +02:00
2018-03-27 17:14:30 +02:00
sparseHelper ( 2 , hessian_output , k , 2 , output_type ) ;
hessian_output < < " = " ;
2018-05-28 11:59:14 +02:00
d2 - > writeOutput ( hessian_output , output_type , temp_term_union , temporary_terms_idxs , tef_terms ) ;
2018-03-27 17:14:30 +02:00
hessian_output < < " ; " < < endl ;
2011-08-18 12:44:11 +02:00
2018-03-27 17:14:30 +02:00
k + + ;
}
2015-08-20 12:18:19 +02:00
2018-03-27 17:14:30 +02:00
// Treating symetric elements
if ( symb_id1 ! = symb_id2 )
2018-09-05 18:27:13 +02:00
if ( output_type = = ExprNodeOutputType : : juliaStaticModel )
2018-03-27 17:14:30 +02:00
hessian_output < < " @inbounds g2[ " < < eq + 1 < < " , " < < col_nb_sym + 1 < < " ] = "
< < for_sym . str ( ) < < endl ;
else
{
sparseHelper ( 2 , hessian_output , k , 0 , output_type ) ;
hessian_output < < " = " < < eq + 1 < < " ; " < < endl ;
2015-08-20 12:18:19 +02:00
2018-03-27 17:14:30 +02:00
sparseHelper ( 2 , hessian_output , k , 1 , output_type ) ;
hessian_output < < " = " < < col_nb_sym + 1 < < " ; " < < endl ;
2015-08-20 12:18:19 +02:00
2018-03-27 17:14:30 +02:00
sparseHelper ( 2 , hessian_output , k , 2 , output_type ) ;
hessian_output < < " = " ;
sparseHelper ( 2 , hessian_output , k - 1 , 2 , output_type ) ;
hessian_output < < " ; " < < endl ;
2015-08-20 12:18:19 +02:00
2018-03-27 17:14:30 +02:00
k + + ;
}
}
2011-08-18 12:44:11 +02:00
}
2009-04-14 16:39:53 +02:00
2013-11-22 21:09:04 +01:00
// Writing third derivatives
2015-09-03 13:50:02 +02:00
if ( ! third_derivatives . empty ( ) )
2013-11-22 21:09:04 +01:00
{
2018-05-24 19:29:53 +02:00
writeTemporaryTerms ( temporary_terms_g3 ,
temp_term_union ,
2018-05-28 15:23:15 +02:00
temporary_terms_idxs ,
2018-03-27 17:14:30 +02:00
third_derivatives_tt_output , output_type , tef_terms ) ;
temp_term_union . insert ( temporary_terms_g3 . begin ( ) , temporary_terms_g3 . end ( ) ) ;
int k = 0 ; // Keep the line of a 3rd derivative in v3
2018-06-04 12:26:16 +02:00
for ( const auto & third_derivative : third_derivatives )
2015-08-20 14:41:15 +02:00
{
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 , var3 ;
tie ( eq , var1 , var2 , var3 ) = third_derivative . first ;
2018-06-04 12:26:16 +02:00
expr_t d3 = third_derivative . second ;
2013-11-22 21:09:04 +01:00
2018-03-27 17:14:30 +02:00
int id1 = getSymbIDByDerivID ( var1 ) ;
int id2 = getSymbIDByDerivID ( var2 ) ;
int id3 = getSymbIDByDerivID ( var3 ) ;
2013-11-22 21:09:04 +01:00
2018-03-27 17:14:30 +02:00
// Reference column number for the g3 matrix
int ref_col = id1 * hessianColsNbr + id2 * JacobianColsNbr + id3 ;
2013-11-22 21:09:04 +01:00
2018-09-05 18:27:13 +02:00
if ( output_type = = ExprNodeOutputType : : juliaStaticModel )
2018-03-27 17:14:30 +02:00
{
for_sym < < " g3[ " < < eq + 1 < < " , " < < ref_col + 1 < < " ] " ;
third_derivatives_output < < " @inbounds " < < for_sym . str ( ) < < " = " ;
2018-05-28 11:59:14 +02:00
d3 - > writeOutput ( third_derivatives_output , output_type , temp_term_union , temporary_terms_idxs , tef_terms ) ;
2018-03-27 17:14:30 +02:00
third_derivatives_output < < endl ;
}
2015-08-20 14:41:15 +02:00
else
{
2018-03-27 17:14:30 +02:00
sparseHelper ( 3 , third_derivatives_output , k , 0 , output_type ) ;
2015-08-20 14:41:15 +02:00
third_derivatives_output < < " = " < < eq + 1 < < " ; " < < endl ;
2013-11-22 21:09:04 +01:00
2018-03-27 17:14:30 +02:00
sparseHelper ( 3 , third_derivatives_output , k , 1 , output_type ) ;
third_derivatives_output < < " = " < < ref_col + 1 < < " ; " < < endl ;
2013-11-22 21:09:04 +01:00
2015-08-20 14:41:15 +02:00
sparseHelper ( 3 , third_derivatives_output , k , 2 , output_type ) ;
2018-03-27 17:14:30 +02:00
third_derivatives_output < < " = " ;
2018-05-28 11:59:14 +02:00
d3 - > writeOutput ( third_derivatives_output , output_type , temp_term_union , temporary_terms_idxs , tef_terms ) ;
2015-08-20 14:41:15 +02:00
third_derivatives_output < < " ; " < < endl ;
}
2018-03-27 17:14:30 +02:00
// Compute the column numbers for the 5 other permutations of (id1,id2,id3)
// and store them in a set (to avoid duplicates if two indexes are equal)
set < int > cols ;
cols . insert ( id1 * hessianColsNbr + id3 * JacobianColsNbr + id2 ) ;
cols . insert ( id2 * hessianColsNbr + id1 * JacobianColsNbr + id3 ) ;
cols . insert ( id2 * hessianColsNbr + id3 * JacobianColsNbr + id1 ) ;
cols . insert ( id3 * hessianColsNbr + id1 * JacobianColsNbr + id2 ) ;
cols . insert ( id3 * hessianColsNbr + id2 * JacobianColsNbr + id1 ) ;
int k2 = 1 ; // Keeps the offset of the permutation relative to k
2018-06-04 12:26:16 +02:00
for ( int col : cols )
if ( col ! = ref_col )
2018-09-05 18:27:13 +02:00
if ( output_type = = ExprNodeOutputType : : juliaStaticModel )
2018-06-04 12:26:16 +02:00
third_derivatives_output < < " @inbounds g3[ " < < eq + 1 < < " , " < < col + 1 < < " ] = "
2018-03-27 17:14:30 +02:00
< < for_sym . str ( ) < < endl ;
else
{
sparseHelper ( 3 , third_derivatives_output , k + k2 , 0 , output_type ) ;
third_derivatives_output < < " = " < < eq + 1 < < " ; " < < endl ;
sparseHelper ( 3 , third_derivatives_output , k + k2 , 1 , output_type ) ;
2018-06-04 12:26:16 +02:00
third_derivatives_output < < " = " < < col + 1 < < " ; " < < endl ;
2018-03-27 17:14:30 +02:00
sparseHelper ( 3 , third_derivatives_output , k + k2 , 2 , output_type ) ;
third_derivatives_output < < " = " ;
sparseHelper ( 3 , third_derivatives_output , k , 2 , output_type ) ;
third_derivatives_output < < " ; " < < endl ;
k2 + + ;
}
k + = k2 ;
}
2013-11-22 21:09:04 +01:00
}
2018-09-05 18:27:13 +02:00
if ( output_type = = ExprNodeOutputType : : matlabStaticModel )
2011-08-18 12:44:11 +02:00
{
2016-12-28 14:02:50 +01:00
// Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
2016-12-30 11:26:56 +01:00
map < string , string > tmp_paren_vars ;
2017-01-05 15:19:13 +01:00
bool message_printed = false ;
fixNestedParenthesis ( model_output , tmp_paren_vars , message_printed ) ;
2018-03-27 17:14:30 +02:00
fixNestedParenthesis ( model_tt_output , tmp_paren_vars , message_printed ) ;
2017-01-05 15:19:13 +01:00
fixNestedParenthesis ( jacobian_output , tmp_paren_vars , message_printed ) ;
2018-03-27 17:14:30 +02:00
fixNestedParenthesis ( jacobian_tt_output , tmp_paren_vars , message_printed ) ;
2017-01-05 15:19:13 +01:00
fixNestedParenthesis ( hessian_output , tmp_paren_vars , message_printed ) ;
2018-03-27 17:14:30 +02:00
fixNestedParenthesis ( hessian_tt_output , tmp_paren_vars , message_printed ) ;
2017-01-05 15:19:13 +01:00
fixNestedParenthesis ( third_derivatives_output , tmp_paren_vars , message_printed ) ;
2018-03-27 17:14:30 +02:00
fixNestedParenthesis ( third_derivatives_tt_output , tmp_paren_vars , message_printed ) ;
ostringstream init_output , end_output ;
init_output < < " residual = zeros( " < < equations . size ( ) < < " , 1); " ;
end_output < < " if ~isreal(residual) " < < endl
< < " residual = real(residual)+imag(residual).^2; " < < endl
< < " end " ;
2018-06-27 15:01:31 +02:00
writeStaticModelHelper ( basename , " static_resid " , " residual " , " static_resid_tt " ,
2018-05-24 19:29:53 +02:00
temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) ,
2018-03-27 17:14:30 +02:00
" " , init_output , end_output ,
model_output , model_tt_output ) ;
init_output . str ( string ( ) ) ;
init_output . clear ( ) ;
end_output . str ( string ( ) ) ;
end_output . clear ( ) ;
init_output < < " g1 = zeros( " < < equations . size ( ) < < " , " < < symbol_table . endo_nbr ( ) < < " ); " ;
end_output < < " if ~isreal(g1) " < < endl
< < " g1 = real(g1)+2*imag(g1); " < < endl
< < " end " ;
2018-06-27 15:01:31 +02:00
writeStaticModelHelper ( basename , " static_g1 " , " g1 " , " static_g1_tt " ,
2018-05-24 19:29:53 +02:00
temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) ,
2018-06-27 15:01:31 +02:00
" static_resid_tt " ,
2018-03-27 17:14:30 +02:00
init_output , end_output ,
jacobian_output , jacobian_tt_output ) ;
2018-06-27 15:01:31 +02:00
writeWrapperFunctions ( basename , " g1 " ) ;
2018-03-27 17:14:30 +02:00
init_output . str ( string ( ) ) ;
init_output . clear ( ) ;
end_output . str ( string ( ) ) ;
end_output . clear ( ) ;
2011-08-18 12:44:11 +02:00
if ( second_derivatives . size ( ) )
2018-03-27 17:14:30 +02:00
{
init_output < < " v2 = zeros( " < < NNZDerivatives [ 1 ] < < " ,3); " ;
end_output < < " g2 = sparse(v2(:,1),v2(:,2),v2(:,3), " < < equations . size ( ) < < " , " < < g2ncols < < " ); " ;
}
2011-08-18 12:44:11 +02:00
else
2018-03-27 17:14:30 +02:00
init_output < < " g2 = sparse([],[],[], " < < equations . size ( ) < < " , " < < g2ncols < < " ); " ;
2018-06-27 15:01:31 +02:00
writeStaticModelHelper ( basename , " static_g2 " , " g2 " , " static_g2_tt " ,
2018-05-24 19:29:53 +02:00
temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( )
+ temporary_terms_g2 . size ( ) ,
2018-06-27 15:01:31 +02:00
" static_g1_tt " ,
2018-03-27 17:14:30 +02:00
init_output , end_output ,
hessian_output , hessian_tt_output ) ;
2018-06-27 15:01:31 +02:00
writeWrapperFunctions ( basename , " g2 " ) ;
2018-03-27 17:14:30 +02:00
init_output . str ( string ( ) ) ;
init_output . clear ( ) ;
end_output . str ( string ( ) ) ;
end_output . clear ( ) ;
2013-11-22 21:09:04 +01:00
int ncols = hessianColsNbr * JacobianColsNbr ;
if ( third_derivatives . size ( ) )
2018-03-27 17:14:30 +02:00
{
init_output < < " v3 = zeros( " < < NNZDerivatives [ 2 ] < < " ,3); " ;
end_output < < " g3 = sparse(v3(:,1),v3(:,2),v3(:,3), " < < nrows < < " , " < < ncols < < " ); " ;
}
else
init_output < < " g3 = sparse([],[],[], " < < nrows < < " , " < < ncols < < " ); " ;
2018-06-27 15:01:31 +02:00
writeStaticModelHelper ( basename , " static_g3 " , " g3 " , " static_g3_tt " ,
2018-05-24 19:29:53 +02:00
temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( )
+ temporary_terms_g2 . size ( ) + temporary_terms_g3 . size ( ) ,
2018-06-27 15:01:31 +02:00
" static_g2_tt " ,
2018-03-27 17:14:30 +02:00
init_output , end_output ,
third_derivatives_output , third_derivatives_tt_output ) ;
2018-06-27 15:01:31 +02:00
writeWrapperFunctions ( basename , " g3 " ) ;
2018-05-23 16:10:26 +02:00
2018-06-27 15:01:31 +02:00
writeStaticMatlabCompatLayer ( basename ) ;
2015-07-27 15:33:38 +02:00
}
2018-09-05 18:27:13 +02:00
else if ( output_type = = ExprNodeOutputType : : CStaticModel )
2011-08-18 12:44:11 +02:00
{
2018-09-25 19:15:22 +02:00
StaticOutput < < " void static_resid_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T) " < < endl
2011-08-18 12:44:11 +02:00
< < " { " < < endl
2018-05-25 15:48:55 +02:00
< < model_tt_output . str ( )
2018-09-25 19:15:22 +02:00
< < " } " < < endl
< < endl
< < " void static_resid(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *residual) " < < endl
< < " { " < < endl
< < " double lhs, rhs; " < < endl
2011-08-18 12:44:11 +02:00
< < model_output . str ( )
2018-09-25 19:15:22 +02:00
< < " } " < < endl
2015-09-04 14:58:14 +02:00
< < endl
2018-09-25 19:15:22 +02:00
< < " void static_g1_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T) " < < endl
< < " { " < < endl
2018-05-25 15:48:55 +02:00
< < jacobian_tt_output . str ( )
2018-09-25 19:15:22 +02:00
< < " } " < < endl
< < endl
< < " void static_g1(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *g1) " < < endl
< < " { " < < endl
2011-08-18 12:44:11 +02:00
< < jacobian_output . str ( )
2018-09-25 19:15:22 +02:00
< < " } " < < endl
< < endl
< < " void static_g2_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T) " < < endl
< < " { " < < endl
< < hessian_tt_output . str ( )
< < " } " < < endl
< < endl
< < " void static_g2(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *v2) " < < endl
< < " { " < < endl
< < hessian_output . str ( )
< < " } " < < endl ;
2011-08-18 12:44:11 +02:00
}
2015-08-18 15:41:27 +02:00
else
{
2018-03-27 17:14:30 +02:00
string filename = basename + " Static.jl " ;
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2018-03-27 17:14:30 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2015-08-18 15:41:27 +02:00
2018-03-27 17:14:30 +02:00
output < < " module " < < basename < < " Static " < < endl
< < " # " < < endl
< < " # NB: this file was automatically generated by Dynare " < < endl
< < " # from " < < basename < < " .mod " < < endl
< < " # " < < endl
< < " using Utils " < < endl < < endl
2018-03-28 17:50:58 +02:00
< < " export tmp_nbr, static!, staticResid!, staticG1!, staticG2!, staticG3! " < < endl < < endl
2018-03-27 17:14:30 +02:00
< < " #= " < < endl
< < " # The comments below apply to all functions contained in this module # " < < endl
< < " NB: The arguments contained on the first line of the function " < < endl
< < " definition are those that are modified in place " < < endl < < endl
< < " ## Exported Functions ## " < < endl
< < " static! : Wrapper function; computes residuals, Jacobian, Hessian, " < < endl
2018-03-29 11:31:25 +02:00
< < " and third order derivatives matroces depending on the arguments provided " < < endl
2018-03-27 17:14:30 +02:00
< < " staticResid! : Computes the static model residuals " < < endl
< < " staticG1! : Computes the static model Jacobian " < < endl
< < " staticG2! : Computes the static model Hessian " < < endl
< < " staticG3! : Computes the static model third derivatives " < < endl < < endl
2018-03-29 11:31:25 +02:00
< < " ## Exported Variables ## " < < endl
< < " tmp_nbr : Vector{Int}(4) respectively the number of temporary variables " < < endl
< < " for the residuals, g1, g2 and g3. " < < endl < < endl
2018-03-27 17:14:30 +02:00
< < " ## Local Functions ## " < < endl
< < " staticResidTT! : Computes the static model temporary terms for the residuals " < < endl
< < " staticG1TT! : Computes the static model temporary terms for the Jacobian " < < endl
< < " staticG2TT! : Computes the static model temporary terms for the Hessian " < < endl
< < " staticG3TT! : Computes the static model temporary terms for the third derivatives " < < endl < < endl
< < " ## Function Arguments ## " < < endl
2018-03-29 11:31:25 +02:00
< < " T : Vector{Float64}(num_temp_terms) temporary terms " < < endl
< < " y : Vector{Float64}(model_.endo_nbr) endogenous variables in declaration order " < < endl
< < " x : Vector{Float64}(model_.exo_nbr) exogenous variables in declaration order " < < endl
< < " params : Vector{Float64}(model_.param) parameter values in declaration order " < < endl
< < " residual : Vector{Float64}(model_.eq_nbr) residuals of the static model equations " < < endl
< < " in order of declaration of the equations. Dynare may prepend auxiliary equations, " < < endl
< < " see model.aux_vars " < < endl
< < " g1 : Matrix{Float64}(model.eq_nbr,model_.endo_nbr) Jacobian matrix of the static model equations " < < endl
< < " The columns and rows respectively correspond to the variables in declaration order and the " < < endl
< < " equations in order of declaration " < < endl
< < " g2 : spzeros(model.eq_nbr, model_.endo^2) Hessian matrix of the static model equations " < < endl
< < " The columns and rows respectively correspond to the variables in declaration order and the " < < endl
< < " equations in order of declaration " < < endl
< < " g3 : spzeros(model.eq_nbr, model_.endo^3) Third order derivatives matrix of the static model equations " < < endl
< < " The columns and rows respectively correspond to the variables in declaration order and the " < < endl
< < " equations in order of declaration " < < endl < < endl
< < " ## Remarks ## " < < endl
< < " [1] The size of `T`, ie the value of `num_temp_terms`, depends on the version of the static model called. The number of temporary variables " < < endl
< < " used for the different returned objects (residuals, jacobian, hessian or third order derivatives) is given by the elements in `tmp_nbr` " < < endl
< < " exported vector. The first element is the number of temporaries used for the computation of the residuals, the second element is the " < < endl
< < " number of temporaries used for the evaluation of the jacobian matrix, etc. If one calls the version of the static model computing the " < < endl
< < " residuals, and the jacobian and hessian matrices, then `T` must have at least `sum(tmp_nbr[1:3])` elements. " < < endl
2018-03-27 17:14:30 +02:00
< < " =# " < < endl < < endl ;
2018-03-28 17:50:58 +02:00
// Write the number of temporary terms
output < < " tmp_nbr = zeros(Int,4) " < < endl
2018-05-24 19:29:53 +02:00
< < " tmp_nbr[1] = " < < temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) < < " # Number of temporary terms for the residuals " < < endl
< < " tmp_nbr[2] = " < < temporary_terms_g1 . size ( ) < < " # Number of temporary terms for g1 (jacobian) " < < endl
< < " tmp_nbr[3] = " < < temporary_terms_g2 . size ( ) < < " # Number of temporary terms for g2 (hessian) " < < endl
< < " tmp_nbr[4] = " < < temporary_terms_g3 . size ( ) < < " # Number of temporary terms for g3 (third order derivates) " < < endl < < endl ;
2018-03-28 17:50:58 +02:00
2018-03-27 17:14:30 +02:00
// staticResidTT!
output < < " function staticResidTT!(T::Vector{Float64}, " < < endl
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}) " < < endl
2018-05-24 19:29:53 +02:00
< < " @assert length(T) >= " < < temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) < < endl
2018-03-27 17:14:30 +02:00
< < model_tt_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// static!
output < < " function staticResid!(T::Vector{Float64}, residual::Vector{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T0_flag::Bool) " < < endl
2018-03-27 17:14:30 +02:00
< < " @assert length(y) == " < < symbol_table . endo_nbr ( ) < < endl
< < " @assert length(x) == " < < symbol_table . exo_nbr ( ) < < endl
< < " @assert length(params) == " < < symbol_table . param_nbr ( ) < < endl
< < " @assert length(residual) == " < < equations . size ( ) < < endl
2018-04-10 09:45:40 +02:00
< < " if T0_flag " < < endl
2018-03-27 17:14:30 +02:00
< < " staticResidTT!(T, y, x, params) " < < endl
< < " end " < < endl
< < model_output . str ( )
< < " if ~isreal(residual) " < < endl
< < " residual = real(residual)+imag(residual).^2; " < < endl
< < " end " < < endl
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG1TT!
output < < " function staticG1TT!(T::Vector{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T0_flag::Bool) " < < endl
< < " if T0_flag " < < endl
< < " staticResidTT!(T, y, x, params) " < < endl
< < " end " < < endl
2018-03-27 17:14:30 +02:00
< < jacobian_tt_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG1!
output < < " function staticG1!(T::Vector{Float64}, g1::Matrix{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T1_flag::Bool, T0_flag::Bool) " < < endl
2018-03-27 17:14:30 +02:00
< < " @assert length(T) >= "
2018-05-24 19:29:53 +02:00
< < temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) < < endl
2018-03-27 17:14:30 +02:00
< < " @assert size(g1) == ( " < < equations . size ( ) < < " , " < < symbol_table . endo_nbr ( ) < < " ) " < < endl
< < " @assert length(y) == " < < symbol_table . endo_nbr ( ) < < endl
< < " @assert length(x) == " < < symbol_table . exo_nbr ( ) < < endl
< < " @assert length(params) == " < < symbol_table . param_nbr ( ) < < endl
2018-04-10 09:45:40 +02:00
< < " if T1_flag " < < endl
< < " staticG1TT!(T, y, x, params, T0_flag) " < < endl
2018-03-27 17:14:30 +02:00
< < " end " < < endl
< < " fill!(g1, 0.0) " < < endl
< < jacobian_output . str ( )
< < " if ~isreal(g1) " < < endl
< < " g1 = real(g1)+2*imag(g1); " < < endl
< < " end " < < endl
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG2TT!
output < < " function staticG2TT!(T::Vector{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T1_flag::Bool, T0_flag::Bool) " < < endl
< < " if T1_flag " < < endl
< < " staticG1TT!(T, y, x, params, TO_flag) " < < endl
< < " end " < < endl
2018-03-27 17:14:30 +02:00
< < hessian_tt_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG2!
output < < " function staticG2!(T::Vector{Float64}, g2::Matrix{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T2_flag::Bool, T1_flag::Bool, T0_flag::Bool) " < < endl
2018-03-27 17:14:30 +02:00
< < " @assert length(T) >= "
2018-05-24 19:29:53 +02:00
< < temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) + temporary_terms_g2 . size ( ) < < endl
2018-03-27 17:14:30 +02:00
< < " @assert size(g2) == ( " < < equations . size ( ) < < " , " < < g2ncols < < " ) " < < endl
< < " @assert length(y) == " < < symbol_table . endo_nbr ( ) < < endl
< < " @assert length(x) == " < < symbol_table . exo_nbr ( ) < < endl
< < " @assert length(params) == " < < symbol_table . param_nbr ( ) < < endl
2018-04-10 09:45:40 +02:00
< < " if T2_flag " < < endl
< < " staticG2TT!(T, y, x, params, T1_flag, T0_flag) " < < endl
2018-03-27 17:14:30 +02:00
< < " end " < < endl
< < " fill!(g2, 0.0) " < < endl
< < hessian_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG3TT!
output < < " function staticG3TT!(T::Vector{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T2_flag::Bool, T1_flag::Bool, T0_flag::Bool) " < < endl
< < " if T2_flag " < < endl
< < " staticG2TT!(T, y, x, params, T1_flag, T0_flag) " < < endl
< < " end " < < endl
2018-03-27 17:14:30 +02:00
< < third_derivatives_tt_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// staticG3!
2015-08-20 11:32:26 +02:00
int ncols = hessianColsNbr * JacobianColsNbr ;
2018-03-27 17:14:30 +02:00
output < < " function staticG3!(T::Vector{Float64}, g3::Matrix{Float64}, " < < endl
2018-04-10 09:45:40 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}, T3_flag::Bool, T2_flag::Bool, T1_flag::Bool, T0_flag::Bool) " < < endl
2018-03-27 17:14:30 +02:00
< < " @assert length(T) >= "
2018-05-24 19:29:53 +02:00
< < temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) + temporary_terms_g2 . size ( ) + temporary_terms_g3 . size ( ) < < endl
2018-03-27 17:14:30 +02:00
< < " @assert size(g3) == ( " < < nrows < < " , " < < ncols < < " ) " < < endl
< < " @assert length(y) == " < < symbol_table . endo_nbr ( ) < < endl
< < " @assert length(x) == " < < symbol_table . exo_nbr ( ) < < endl
< < " @assert length(params) == " < < symbol_table . param_nbr ( ) < < endl
2018-04-10 09:45:40 +02:00
< < " if T3_flag " < < endl
< < " staticG3TT!(T, y, x, params, T2_flag, T1_flag, T0_flag) " < < endl
2018-03-27 17:14:30 +02:00
< < " end " < < endl
< < " fill!(g3, 0.0) " < < endl
< < third_derivatives_output . str ( )
< < " return nothing " < < endl
< < " end " < < endl < < endl ;
// static!
2018-04-10 09:45:40 +02:00
output < < " function static!(T::Vector{Float64}, residual::Vector{Float64}, " < < endl
2018-03-29 22:23:38 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}) " < < endl
2018-03-27 17:14:30 +02:00
< < " staticResid!(T, residual, y, x, params, true) " < < endl
< < " return nothing " < < endl
< < " end " < < endl
< < endl
< < " function static!(T::Vector{Float64}, residual::Vector{Float64}, g1::Matrix{Float64}, " < < endl
2018-03-29 22:23:38 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}) " < < endl
2018-03-27 17:14:30 +02:00
< < " staticG1!(T, g1, y, x, params, true) " < < endl
< < " staticResid!(T, residual, y, x, params, false) " < < endl
< < " return nothing " < < endl
< < " end " < < endl
< < endl
2018-04-10 09:45:40 +02:00
< < " function static!(T::Vector{Float64}, g1::Matrix{Float64}, " < < endl
2018-03-29 22:23:38 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}) " < < endl
2018-04-10 09:45:40 +02:00
< < " staticG1!(T, g1, y, x, params, true, false) " < < endl
2018-03-27 17:14:30 +02:00
< < " return nothing " < < endl
< < " end " < < endl
< < endl
2018-04-10 09:45:40 +02:00
< < " function static!(T::Vector{Float64}, residual::Vector{Float64}, g1::Matrix{Float64}, g2::Matrix{Float64}, " < < endl
2018-03-29 22:23:38 +02:00
< < " y::Vector{Float64}, x::Vector{Float64}, params::Vector{Float64}) " < < endl
2018-04-10 09:45:40 +02:00
< < " staticG2!(T, g2, y, x, params, true) " < < endl
2018-03-27 17:14:30 +02:00
< < " staticG1!(T, g1, y, x, params, false) " < < endl
< < " staticResid!(T, residual, y, x, params, false) " < < endl
< < " return nothing " < < endl
2018-04-10 09:45:40 +02:00
< < " end " < < endl < < endl
2018-03-27 17:14:30 +02:00
< < " end " < < endl ;
output . close ( ) ;
2015-08-18 15:41:27 +02:00
}
2011-08-18 12:44:11 +02:00
}
void
2018-06-27 15:01:31 +02:00
StaticModel : : writeStaticCFile ( const string & basename ) const
2011-08-18 12:44:11 +02:00
{
// Writing comments and function definition command
2018-06-27 15:01:31 +02:00
boost : : filesystem : : create_directories ( basename + " /model/src " ) ;
string filename = basename + " /model/src/static.c " ;
string filename_mex = basename + " /model/src/static_mex.c " ;
2011-08-18 12:44:11 +02:00
2018-09-25 19:15:22 +02:00
int ntt = temporary_terms_mlv . size ( ) + temporary_terms_res . size ( ) + temporary_terms_g1 . size ( ) + temporary_terms_g2 . size ( ) + temporary_terms_g3 . size ( ) ;
2011-08-18 12:44:11 +02:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2011-08-18 12:44:11 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
output < < " /* " < < endl
< < " * " < < filename < < " : Computes static model for Dynare " < < endl
< < " * " < < endl
< < " * Warning : this file is generated automatically by Dynare " < < endl
< < " * from model file (.mod) " < < endl < < endl
< < " */ " < < endl
2018-10-02 16:50:07 +02:00
# if defined(_WIN32) || defined(__CYGWIN32__)
2016-10-17 14:59:07 +02:00
< < " #ifdef _MSC_VER " < < endl
< < " #define _USE_MATH_DEFINES " < < endl
< < " #endif " < < endl
# endif
2011-12-22 14:55:57 +01:00
< < " #include <math.h> " < < endl ;
if ( external_functions_table . get_total_number_of_unique_model_block_external_functions ( ) )
// External Matlab function, implies Static function will call mex
output < < " #include \" mex.h \" " < < endl ;
else
output < < " #include <stdlib.h> " < < endl ;
output < < " #define max(a, b) (((a) > (b)) ? (a) : (b)) " < < endl
2011-08-18 12:44:11 +02:00
< < " #define min(a, b) (((a) > (b)) ? (b) : (a)) " < < endl ;
2018-07-18 16:18:26 +02:00
// Write function definition if BinaryOpcode::powerDeriv is used
2011-08-18 12:44:11 +02:00
writePowerDerivCHeader ( output ) ;
2016-10-14 16:11:44 +02:00
writeNormcdfCHeader ( output ) ;
2011-08-18 12:44:11 +02:00
2018-09-25 19:15:22 +02:00
output < < endl ;
2011-08-18 12:44:11 +02:00
// Writing the function body
2015-07-27 15:33:38 +02:00
writeStaticModel ( output , true , false ) ;
2018-09-25 19:15:22 +02:00
output < < endl ;
2011-08-18 12:44:11 +02:00
2016-10-14 14:30:14 +02:00
writePowerDeriv ( output ) ;
2016-10-14 16:11:44 +02:00
writeNormcdf ( output ) ;
2011-12-22 14:55:57 +01:00
output . close ( ) ;
2018-06-27 15:12:12 +02:00
output . open ( filename_mex , ios : : out | ios : : binary ) ;
2011-12-22 14:55:57 +01:00
if ( ! output . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < filename_mex < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2011-08-18 12:44:11 +02:00
// Writing the gateway routine
2011-12-22 14:55:57 +01:00
output < < " /* " < < endl
< < " * " < < filename_mex < < " : The gateway routine used to call the Static function "
< < " located in " < < filename < < endl
< < " * " < < endl
< < " * Warning : this file is generated automatically by Dynare " < < endl
< < " * from model file (.mod) " < < endl < < endl
2018-09-25 19:15:22 +02:00
< < " */ " < < endl
< < endl
< < " #include <stdlib.h> " < < endl
< < " #include \" mex.h \" " < < endl
< < endl
< < " const int ntt = " < < ntt < < " ; " < < endl
< < " void static_resid_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T); " < < endl
< < " void static_resid(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *residual); " < < endl
< < " void static_g1_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T); " < < endl
< < " void static_g1(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *g1); " < < endl
< < " void static_g2_tt(const double *y, const double *x, int nb_row_x, const double *params, double *T); " < < endl
< < " void static_g2(const double *y, const double *x, int nb_row_x, const double *params, const double *T, double *v2); " < < endl
2011-08-18 12:44:11 +02:00
< < " void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) " < < endl
< < " { " < < endl
< < " /* Create a pointer to the input matrix y. */ " < < endl
2018-09-25 19:15:22 +02:00
< < " double *y = mxGetPr(prhs[0]); " < < endl
2011-08-18 12:44:11 +02:00
< < endl
< < " /* Create a pointer to the input matrix x. */ " < < endl
2018-09-25 19:15:22 +02:00
< < " double *x = mxGetPr(prhs[1]); " < < endl
2011-08-18 12:44:11 +02:00
< < endl
< < " /* Create a pointer to the input matrix params. */ " < < endl
2018-09-25 19:15:22 +02:00
< < " double *params = mxGetPr(prhs[2]); " < < endl
2011-08-18 12:44:11 +02:00
< < endl
< < " /* Gets number of rows of matrix x. */ " < < endl
2018-09-25 19:15:22 +02:00
< < " int nb_row_x = mxGetM(prhs[1]); " < < endl
< < endl
< < " double *T = (double *) malloc(sizeof(double)*ntt); "
2011-08-18 12:44:11 +02:00
< < endl
< < " if (nlhs >= 1) " < < endl
< < " { " < < endl
< < " /* Set the output pointer to the output matrix residual. */ " < < endl
< < " plhs[0] = mxCreateDoubleMatrix( " < < equations . size ( ) < < " ,1, mxREAL); " < < endl
2018-09-25 19:15:22 +02:00
< < " double *residual = mxGetPr(plhs[0]); " < < endl
< < " static_resid_tt(y, x, nb_row_x, params, T); " < < endl
< < " static_resid(y, x, nb_row_x, params, T, residual); " < < endl
2011-08-18 12:44:11 +02:00
< < " } " < < endl
< < endl
< < " if (nlhs >= 2) " < < endl
< < " { " < < endl
< < " /* Set the output pointer to the output matrix g1. */ " < < endl
< < " plhs[1] = mxCreateDoubleMatrix( " < < equations . size ( ) < < " , " < < symbol_table . endo_nbr ( ) < < " , mxREAL); " < < endl
2018-09-25 19:15:22 +02:00
< < " double *g1 = mxGetPr(plhs[1]); " < < endl
< < " static_g1_tt(y, x, nb_row_x, params, T); " < < endl
< < " static_g1(y, x, nb_row_x, params, T, g1); " < < endl
2011-08-18 12:44:11 +02:00
< < " } " < < endl
< < endl
< < " if (nlhs >= 3) " < < endl
< < " { " < < endl
< < " /* Set the output pointer to the output matrix v2. */ " < < endl
< < " plhs[2] = mxCreateDoubleMatrix( " < < NNZDerivatives [ 1 ] < < " , " < < 3
< < " , mxREAL); " < < endl
2018-09-25 19:15:22 +02:00
< < " double *v2 = mxGetPr(plhs[2]); " < < endl
< < " static_g2_tt(y, x, nb_row_x, params, T); " < < endl
< < " static_g2(y, x, nb_row_x, params, T, v2); " < < endl
2011-08-18 12:44:11 +02:00
< < " } " < < endl
< < endl
2018-09-25 19:15:22 +02:00
< < " free(T); " < < endl
< < " } " < < endl ;
2009-07-07 16:20:48 +02:00
output . close ( ) ;
2009-04-14 16:39:53 +02:00
}
2009-12-16 14:21:31 +01:00
void
2015-07-27 15:59:13 +02:00
StaticModel : : writeStaticJuliaFile ( const string & basename ) const
2015-07-27 15:33:38 +02:00
{
2018-03-27 17:14:30 +02:00
writeStaticModel ( basename , false , true ) ;
2015-07-27 15:33:38 +02:00
}
void
2015-07-27 15:59:13 +02:00
StaticModel : : writeStaticFile ( const string & basename , bool block , bool bytecode , bool use_dll , bool julia ) const
2009-12-16 18:13:23 +01:00
{
if ( block & & bytecode )
2018-06-27 15:01:31 +02:00
writeModelEquationsCode_Block ( basename , map_idx , map_idx2 ) ;
2010-01-22 11:03:29 +01:00
else if ( ! block & & bytecode )
2018-06-27 15:01:31 +02:00
writeModelEquationsCode ( basename , map_idx ) ;
2009-12-16 18:13:23 +01:00
else if ( block & & ! bytecode )
{
2018-06-27 15:01:31 +02:00
writeModelEquationsOrdered_M ( basename ) ;
2009-12-16 18:13:23 +01:00
writeStaticBlockMFSFile ( basename ) ;
}
2017-06-14 07:01:31 +02:00
else if ( use_dll )
2011-08-18 12:44:11 +02:00
writeStaticCFile ( basename ) ;
2015-07-27 15:59:13 +02:00
else if ( julia )
writeStaticJuliaFile ( basename ) ;
2009-12-16 18:13:23 +01:00
else
writeStaticMFile ( basename ) ;
2016-04-04 17:11:03 +02:00
writeSetAuxiliaryVariables ( basename , julia ) ;
2009-12-16 18:13:23 +01:00
}
2009-04-27 19:15:14 +02:00
2016-08-12 11:50:57 +02:00
bool
StaticModel : : exoPresentInEqs ( ) const
{
2018-06-04 12:26:16 +02:00
for ( auto equation : equations )
if ( equation - > containsExogenous ( ) )
2016-08-12 11:50:57 +02:00
return true ;
return false ;
}
2009-04-27 19:15:14 +02:00
void
2009-12-16 14:21:31 +01:00
StaticModel : : writeStaticBlockMFSFile ( const string & basename ) const
2009-04-27 19:15:14 +02:00
{
2018-06-27 15:01:31 +02:00
string filename = packageDir ( basename ) + " /static.m " ;
2009-04-27 19:15:14 +02:00
2009-12-16 14:21:31 +01:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2009-12-16 14:21:31 +01:00
if ( ! output . is_open ( ) )
2009-04-27 19:15:14 +02:00
{
2009-12-16 14:21:31 +01:00
cerr < < " ERROR: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
2009-04-27 19:15:14 +02:00
}
2018-06-27 15:01:31 +02:00
output < < " function [residual, g1, y, var_index] = static(nblock, y, x, params) " < < endl
2009-12-16 14:21:31 +01:00
< < " residual = []; " < < endl
< < " g1 = []; " < < endl
2010-09-17 16:53:27 +02:00
< < " var_index = []; \n " < < endl
2009-12-16 14:21:31 +01:00
< < " switch nblock " < < endl ;
2009-06-30 17:07:09 +02:00
2009-12-16 14:21:31 +01:00
unsigned int nb_blocks = getNbBlocks ( ) ;
2009-06-30 17:07:09 +02:00
2009-12-16 18:13:23 +01:00
for ( int b = 0 ; b < ( int ) nb_blocks ; b + + )
2009-06-30 17:07:09 +02:00
{
2009-12-16 14:21:31 +01:00
set < int > local_var ;
2009-04-27 19:15:14 +02:00
2009-12-16 14:21:31 +01:00
output < < " case " < < b + 1 < < endl ;
2009-04-27 19:15:14 +02:00
2009-12-16 14:21:31 +01:00
BlockSimulationType simulation_type = getBlockSimulationType ( b ) ;
2009-12-16 18:13:23 +01:00
if ( simulation_type = = EVALUATE_BACKWARD | | simulation_type = = EVALUATE_FORWARD )
2010-09-17 16:53:27 +02:00
{
2018-06-27 15:01:31 +02:00
output < < " y_tmp = " < < basename < < " .block.static_ " < < b + 1 < < " (y, x, params); \n " ;
2010-09-17 16:53:27 +02:00
ostringstream tmp ;
2011-02-04 16:25:38 +01:00
for ( int i = 0 ; i < ( int ) getBlockSize ( b ) ; i + + )
2010-09-17 16:53:27 +02:00
tmp < < " " < < getBlockVariableID ( b , i ) + 1 ;
output < < " var_index = [ " < < tmp . str ( ) < < " ]; \n " ;
output < < " residual = y(var_index) - y_tmp(var_index); \n " ;
output < < " y = y_tmp; \n " ;
}
2009-12-16 18:13:23 +01:00
else
2018-06-27 15:01:31 +02:00
output < < " [residual, y, g1] = " < < basename < < " .block.static_ " < < b + 1 < < " (y, x, params); \n " ;
2010-09-17 16:53:27 +02:00
2009-04-28 19:11:48 +02:00
}
2009-12-16 14:21:31 +01:00
output < < " end " < < endl
< < " end " < < endl ;
output . close ( ) ;
}
2009-04-28 19:11:48 +02:00
2009-12-16 14:21:31 +01:00
void
StaticModel : : writeOutput ( ostream & output , bool block ) const
{
2018-03-28 18:53:02 +02:00
output < < " M_.static_tmp_nbr = zeros(4,1); % Number of temporaries used for the static model " < < endl
2018-05-24 19:29:53 +02:00
< < " M_.static_tmp_nbr(1) = " < < temporary_terms_res . size ( ) < < " ; % Number of temporaries used for the evaluation of the residuals " < < endl
< < " M_.static_tmp_nbr(2) = " < < temporary_terms_g1 . size ( ) < < " ; % Number of temporaries used for the evaluation of g1 (jacobian) " < < endl
< < " M_.static_tmp_nbr(3) = " < < temporary_terms_g2 . size ( ) < < " ; % Number of temporaries used for the evaluation of g2 (hessian) " < < endl
< < " M_.static_tmp_nbr(4) = " < < temporary_terms_g3 . size ( ) < < " ; % Number of temporaries used for the evaluation of g3 (third order derivatives) " < < endl ;
2018-03-28 18:53:02 +02:00
2009-12-16 14:21:31 +01:00
if ( ! block )
return ;
2009-06-30 17:07:09 +02:00
2009-12-16 14:21:31 +01:00
unsigned int nb_blocks = getNbBlocks ( ) ;
2012-06-06 16:36:56 +02:00
for ( int b = 0 ; b < ( int ) nb_blocks ; b + + )
{
BlockSimulationType simulation_type = getBlockSimulationType ( b ) ;
unsigned int block_size = getBlockSize ( b ) ;
ostringstream tmp_s , tmp_s_eq ;
tmp_s . str ( " " ) ;
tmp_s_eq . str ( " " ) ;
2012-06-08 19:00:57 +02:00
for ( unsigned int i = 0 ; i < block_size ; i + + )
2012-06-06 16:36:56 +02:00
{
tmp_s < < " " < < getBlockVariableID ( b , i ) + 1 ;
tmp_s_eq < < " " < < getBlockEquationID ( b , i ) + 1 ;
}
output < < " block_structure_stat.block( " < < b + 1 < < " ).Simulation_Type = " < < simulation_type < < " ; \n " ;
output < < " block_structure_stat.block( " < < b + 1 < < " ).endo_nbr = " < < block_size < < " ; \n " ;
output < < " block_structure_stat.block( " < < b + 1 < < " ).mfs = " < < getBlockMfs ( block ) < < " ; \n " ;
output < < " block_structure_stat.block( " < < b + 1 < < " ).equation = [ " < < tmp_s_eq . str ( ) < < " ]; \n " ;
output < < " block_structure_stat.block( " < < b + 1 < < " ).variable = [ " < < tmp_s . str ( ) < < " ]; \n " ;
}
output < < " M_.block_structure_stat.block = block_structure_stat.block; \n " ;
string cst_s ;
int nb_endo = symbol_table . endo_nbr ( ) ;
output < < " M_.block_structure_stat.variable_reordered = [ " ;
for ( int i = 0 ; i < nb_endo ; i + + )
output < < " " < < variable_reordered [ i ] + 1 ;
output < < " ]; \n " ;
output < < " M_.block_structure_stat.equation_reordered = [ " ;
for ( int i = 0 ; i < nb_endo ; i + + )
output < < " " < < equation_reordered [ i ] + 1 ;
output < < " ]; \n " ;
2017-06-14 07:01:31 +02:00
2012-06-06 16:36:56 +02:00
map < pair < int , int > , int > row_incidence ;
2018-06-04 12:26:16 +02:00
for ( const auto & first_derivative : first_derivatives )
2012-06-06 16:36:56 +02:00
{
2018-06-04 12:26:16 +02:00
int deriv_id = first_derivative . first . second ;
2018-07-17 18:34:07 +02:00
if ( getTypeByDerivID ( deriv_id ) = = SymbolType : : endogenous )
2012-06-06 16:36:56 +02:00
{
2018-06-04 12:26:16 +02:00
int eq = first_derivative . first . first ;
2012-06-06 16:36:56 +02:00
int symb = getSymbIDByDerivID ( deriv_id ) ;
int var = symbol_table . getTypeSpecificID ( symb ) ;
//int lag = getLagByDerivID(deriv_id);
2018-06-04 16:36:46 +02:00
row_incidence [ { eq , var } ] = 1 ;
2012-06-06 16:36:56 +02:00
}
}
output < < " M_.block_structure_stat.incidence.sparse_IM = [ " ;
for ( map < pair < int , int > , int > : : const_iterator it = row_incidence . begin ( ) ; it ! = row_incidence . end ( ) ; it + + )
{
output < < it - > first . first + 1 < < " " < < it - > first . second + 1 < < " ; \n " ;
}
output < < " ]; \n " ;
2009-04-28 19:11:48 +02:00
}
2009-12-16 14:21:31 +01:00
SymbolType
2018-06-04 12:50:53 +02:00
StaticModel : : getTypeByDerivID ( int deriv_id ) const noexcept ( false )
2009-12-16 14:21:31 +01:00
{
2012-11-29 18:07:48 +01:00
if ( deriv_id < symbol_table . endo_nbr ( ) )
2018-07-17 18:34:07 +02:00
return SymbolType : : endogenous ;
2012-11-29 18:07:48 +01:00
else if ( deriv_id < symbol_table . endo_nbr ( ) + symbol_table . param_nbr ( ) )
2018-07-17 18:34:07 +02:00
return SymbolType : : parameter ;
2012-11-29 18:07:48 +01:00
else
throw UnknownDerivIDException ( ) ;
2009-04-27 19:15:14 +02:00
}
2009-04-30 15:14:33 +02:00
2009-12-16 14:21:31 +01:00
int
2018-06-04 12:50:53 +02:00
StaticModel : : getLagByDerivID ( int deriv_id ) const noexcept ( false )
2009-04-30 15:14:33 +02:00
{
2009-12-16 14:21:31 +01:00
return 0 ;
2009-04-30 15:14:33 +02:00
}
2009-06-30 17:07:09 +02:00
2009-12-16 14:21:31 +01:00
int
2018-06-04 12:50:53 +02:00
StaticModel : : getSymbIDByDerivID ( int deriv_id ) const noexcept ( false )
2009-06-30 17:07:09 +02:00
{
2012-11-29 18:07:48 +01:00
if ( deriv_id < symbol_table . endo_nbr ( ) )
2018-07-17 18:34:07 +02:00
return symbol_table . getID ( SymbolType : : endogenous , deriv_id ) ;
2012-11-29 18:07:48 +01:00
else if ( deriv_id < symbol_table . endo_nbr ( ) + symbol_table . param_nbr ( ) )
2018-07-17 18:34:07 +02:00
return symbol_table . getID ( SymbolType : : parameter , deriv_id - symbol_table . endo_nbr ( ) ) ;
2012-11-29 18:07:48 +01:00
else
throw UnknownDerivIDException ( ) ;
2009-06-30 17:07:09 +02:00
}
2009-12-16 14:21:31 +01:00
int
2018-06-04 12:50:53 +02:00
StaticModel : : getDerivID ( int symb_id , int lag ) const noexcept ( false )
2009-06-30 17:07:09 +02:00
{
2018-07-17 18:34:07 +02:00
if ( symbol_table . getType ( symb_id ) = = SymbolType : : endogenous )
2012-11-29 18:07:48 +01:00
return symbol_table . getTypeSpecificID ( symb_id ) ;
2018-07-17 18:34:07 +02:00
else if ( symbol_table . getType ( symb_id ) = = SymbolType : : parameter )
2012-11-29 18:07:48 +01:00
return symbol_table . getTypeSpecificID ( symb_id ) + symbol_table . endo_nbr ( ) ;
2009-12-16 14:21:31 +01:00
else
return - 1 ;
}
2009-06-30 17:07:09 +02:00
2012-11-29 18:07:48 +01:00
void
StaticModel : : addAllParamDerivId ( set < int > & deriv_id_set )
{
for ( int i = 0 ; i < symbol_table . param_nbr ( ) ; i + + )
deriv_id_set . insert ( i + symbol_table . endo_nbr ( ) ) ;
}
2018-06-04 14:17:36 +02:00
map < pair < pair < int , pair < int , int > > , pair < int , int > > , int >
2009-12-16 14:21:31 +01:00
StaticModel : : get_Derivatives ( int block )
{
2018-06-04 14:17:36 +02:00
map < pair < pair < int , pair < int , int > > , pair < int , int > > , int > Derivatives ;
2009-12-16 14:21:31 +01:00
Derivatives . clear ( ) ;
int block_size = getBlockSize ( block ) ;
int block_nb_recursive = block_size - getBlockMfs ( block ) ;
int lag = 0 ;
2009-12-16 18:13:23 +01:00
for ( int eq = 0 ; eq < block_size ; eq + + )
2009-06-30 17:07:09 +02:00
{
2009-12-16 14:21:31 +01:00
int eqr = getBlockEquationID ( block , eq ) ;
2009-12-16 18:13:23 +01:00
for ( int var = 0 ; var < block_size ; var + + )
2009-06-30 17:07:09 +02:00
{
2009-12-16 14:21:31 +01:00
int varr = getBlockVariableID ( block , var ) ;
2018-06-04 16:36:46 +02:00
if ( dynamic_jacobian . find ( { lag , { eqr , varr } } ) ! = dynamic_jacobian . end ( ) )
2009-12-16 14:21:31 +01:00
{
bool OK = true ;
2018-06-04 16:36:46 +02:00
map < pair < pair < int , pair < int , int > > , pair < int , int > > , int > : : const_iterator its = Derivatives . find ( { { lag , { eq , var } } , { eqr , varr } } ) ;
2009-12-16 18:13:23 +01:00
if ( its ! = Derivatives . end ( ) )
2009-12-16 14:21:31 +01:00
{
2009-12-16 18:13:23 +01:00
if ( its - > second = = 2 )
OK = false ;
2009-12-16 14:21:31 +01:00
}
2009-06-30 17:07:09 +02:00
2009-12-16 18:13:23 +01:00
if ( OK )
2009-12-16 14:21:31 +01:00
{
2010-04-28 16:03:32 +02:00
if ( getBlockEquationType ( block , eq ) = = E_EVALUATE_S & & eq < block_nb_recursive )
2009-12-16 14:21:31 +01:00
//It's a normalized equation, we have to recompute the derivative using chain rule derivative function
2018-06-04 16:36:46 +02:00
Derivatives [ { { lag , { eq , var } } , { eqr , varr } } ] = 1 ;
2009-12-16 14:21:31 +01:00
else
//It's a feedback equation we can use the derivatives
2018-06-04 16:36:46 +02:00
Derivatives [ { { lag , { eq , var } } , { eqr , varr } } ] = 0 ;
2009-12-16 14:21:31 +01:00
}
2009-12-16 18:13:23 +01:00
if ( var < block_nb_recursive )
2009-12-16 14:21:31 +01:00
{
int eqs = getBlockEquationID ( block , var ) ;
2009-12-16 18:13:23 +01:00
for ( int vars = block_nb_recursive ; vars < block_size ; vars + + )
2009-12-16 14:21:31 +01:00
{
int varrs = getBlockVariableID ( block , vars ) ;
//A new derivative needs to be computed using the chain rule derivative function (a feedback variable appears in a recursive equation)
2018-06-04 16:36:46 +02:00
if ( Derivatives . find ( { { lag , { var , vars } } , { eqs , varrs } } ) ! = Derivatives . end ( ) )
Derivatives [ { { lag , { eq , vars } } , { eqr , varrs } } ] = 2 ;
2009-12-16 14:21:31 +01:00
}
}
}
2009-06-30 17:07:09 +02:00
}
}
2009-12-16 14:21:31 +01:00
2009-12-16 18:13:23 +01:00
return ( Derivatives ) ;
2009-06-30 17:07:09 +02:00
}
2009-07-07 16:20:48 +02:00
void
2010-09-16 19:00:48 +02:00
StaticModel : : computeChainRuleJacobian ( blocks_derivatives_t & blocks_derivatives )
2009-07-07 16:20:48 +02:00
{
2010-09-16 19:18:45 +02:00
map < int , expr_t > recursive_variables ;
2009-12-16 14:21:31 +01:00
unsigned int nb_blocks = getNbBlocks ( ) ;
2010-09-16 19:00:48 +02:00
blocks_derivatives = blocks_derivatives_t ( nb_blocks ) ;
2009-12-16 18:13:23 +01:00
for ( unsigned int block = 0 ; block < nb_blocks ; block + + )
2009-07-07 16:20:48 +02:00
{
2010-09-16 19:00:48 +02:00
block_derivatives_equation_variable_laglead_nodeid_t tmp_derivatives ;
2009-12-16 14:21:31 +01:00
recursive_variables . clear ( ) ;
BlockSimulationType simulation_type = getBlockSimulationType ( block ) ;
int block_size = getBlockSize ( block ) ;
int block_nb_mfs = getBlockMfs ( block ) ;
int block_nb_recursives = block_size - block_nb_mfs ;
2010-04-28 16:03:32 +02:00
if ( simulation_type = = SOLVE_TWO_BOUNDARIES_COMPLETE | | simulation_type = = SOLVE_TWO_BOUNDARIES_SIMPLE )
2009-07-10 16:22:40 +02:00
{
2010-09-16 19:00:48 +02:00
blocks_derivatives . push_back ( block_derivatives_equation_variable_laglead_nodeid_t ( 0 ) ) ;
2009-12-16 18:13:23 +01:00
for ( int i = 0 ; i < block_nb_recursives ; i + + )
2009-12-16 14:21:31 +01:00
{
if ( getBlockEquationType ( block , i ) = = E_EVALUATE_S )
2018-07-17 18:34:07 +02:00
recursive_variables [ getDerivID ( symbol_table . getID ( SymbolType : : endogenous , getBlockVariableID ( block , i ) ) , 0 ) ] = getBlockEquationRenormalizedExpr ( block , i ) ;
2009-12-16 14:21:31 +01:00
else
2018-07-17 18:34:07 +02:00
recursive_variables [ getDerivID ( symbol_table . getID ( SymbolType : : endogenous , getBlockVariableID ( block , i ) ) , 0 ) ] = getBlockEquationExpr ( block , i ) ;
2009-12-16 14:21:31 +01:00
}
2018-06-04 14:17:36 +02:00
map < pair < pair < int , pair < int , int > > , pair < int , int > > , int > Derivatives = get_Derivatives ( block ) ;
map < pair < pair < int , pair < int , int > > , pair < int , int > > , int > : : const_iterator it = Derivatives . begin ( ) ;
2009-12-16 18:13:23 +01:00
for ( int i = 0 ; i < ( int ) Derivatives . size ( ) ; i + + )
2009-07-10 16:22:40 +02:00
{
2009-12-16 14:21:31 +01:00
int Deriv_type = it - > second ;
2018-06-04 14:17:36 +02:00
pair < pair < int , pair < int , int > > , pair < int , int > > it_l ( it - > first ) ;
2009-12-16 14:21:31 +01:00
it + + ;
int lag = it_l . first . first ;
int eq = it_l . first . second . first ;
int var = it_l . first . second . second ;
int eqr = it_l . second . first ;
int varr = it_l . second . second ;
2009-12-16 18:13:23 +01:00
if ( Deriv_type = = 0 )
2018-07-17 18:34:07 +02:00
first_chain_rule_derivatives [ { eqr , { varr , lag } } ] = first_derivatives [ { eqr , getDerivID ( symbol_table . getID ( SymbolType : : endogenous , varr ) , lag ) } ] ;
2009-12-16 14:21:31 +01:00
else if ( Deriv_type = = 1 )
2018-07-17 18:34:07 +02:00
first_chain_rule_derivatives [ { eqr , { varr , lag } } ] = ( equation_type_and_normalized_equation [ eqr ] . second ) - > getChainRuleDerivative ( getDerivID ( symbol_table . getID ( SymbolType : : endogenous , varr ) , lag ) , recursive_variables ) ;
2009-12-16 14:21:31 +01:00
else if ( Deriv_type = = 2 )
2009-07-10 16:22:40 +02:00
{
2009-12-16 18:13:23 +01:00
if ( getBlockEquationType ( block , eq ) = = E_EVALUATE_S & & eq < block_nb_recursives )
2018-07-17 18:34:07 +02:00
first_chain_rule_derivatives [ { eqr , { varr , lag } } ] = ( equation_type_and_normalized_equation [ eqr ] . second ) - > getChainRuleDerivative ( getDerivID ( symbol_table . getID ( SymbolType : : endogenous , varr ) , lag ) , recursive_variables ) ;
2009-12-16 14:21:31 +01:00
else
2018-07-17 18:34:07 +02:00
first_chain_rule_derivatives [ { eqr , { varr , lag } } ] = equations [ eqr ] - > getChainRuleDerivative ( getDerivID ( symbol_table . getID ( SymbolType : : endogenous , varr ) , lag ) , recursive_variables ) ;
2009-07-10 16:22:40 +02:00
}
2018-06-04 16:36:46 +02:00
tmp_derivatives . emplace_back ( make_pair ( eq , var ) , make_pair ( lag , first_chain_rule_derivatives [ make_pair ( eqr , make_pair ( varr , lag ) ) ] ) ) ;
2009-07-10 16:22:40 +02:00
}
}
2010-10-11 19:21:32 +02:00
else
2009-07-07 16:20:48 +02:00
{
2010-09-16 19:00:48 +02:00
blocks_derivatives . push_back ( block_derivatives_equation_variable_laglead_nodeid_t ( 0 ) ) ;
2009-12-16 18:13:23 +01:00
for ( int i = 0 ; i < block_nb_recursives ; i + + )
2009-07-07 17:58:17 +02:00
{
2009-12-16 14:21:31 +01:00
if ( getBlockEquationType ( block , i ) = = E_EVALUATE_S )
2018-07-17 18:34:07 +02:00
recursive_variables [ getDerivID ( symbol_table . getID ( SymbolType : : endogenous , getBlockVariableID ( block , i ) ) , 0 ) ] = getBlockEquationRenormalizedExpr ( block , i ) ;
2009-12-16 14:21:31 +01:00
else
2018-07-17 18:34:07 +02:00
recursive_variables [ getDerivID ( symbol_table . getID ( SymbolType : : endogenous , getBlockVariableID ( block , i ) ) , 0 ) ] = getBlockEquationExpr ( block , i ) ;
2009-12-16 14:21:31 +01:00
}
2009-12-16 18:13:23 +01:00
for ( int eq = block_nb_recursives ; eq < block_size ; eq + + )
2009-12-16 14:21:31 +01:00
{
int eqr = getBlockEquationID ( block , eq ) ;
2009-12-16 18:13:23 +01:00
for ( int var = block_nb_recursives ; var < block_size ; var + + )
2009-12-16 14:21:31 +01:00
{
int varr = getBlockVariableID ( block , var ) ;
2018-07-17 18:34:07 +02:00
expr_t d1 = equations [ eqr ] - > getChainRuleDerivative ( getDerivID ( symbol_table . getID ( SymbolType : : endogenous , varr ) , 0 ) , recursive_variables ) ;
2009-12-16 14:21:31 +01:00
if ( d1 = = Zero )
continue ;
2018-06-04 16:36:46 +02:00
first_chain_rule_derivatives [ { eqr , { varr , 0 } } ] = d1 ;
tmp_derivatives . emplace_back ( make_pair ( eq , var ) , make_pair ( 0 , first_chain_rule_derivatives [ make_pair ( eqr , make_pair ( varr , 0 ) ) ] ) ) ;
2009-12-16 14:21:31 +01:00
}
2009-07-07 17:58:17 +02:00
}
2009-07-07 16:20:48 +02:00
}
2009-12-16 14:21:31 +01:00
blocks_derivatives [ block ] = tmp_derivatives ;
2009-07-07 16:20:48 +02:00
}
}
void
2009-12-16 14:21:31 +01:00
StaticModel : : collect_block_first_order_derivatives ( )
2009-07-07 16:20:48 +02:00
{
2009-12-16 14:21:31 +01:00
//! vector for an equation or a variable indicates the block number
vector < int > equation_2_block , variable_2_block ;
unsigned int nb_blocks = getNbBlocks ( ) ;
equation_2_block = vector < int > ( equation_reordered . size ( ) ) ;
variable_2_block = vector < int > ( variable_reordered . size ( ) ) ;
2009-12-16 18:13:23 +01:00
for ( unsigned int block = 0 ; block < nb_blocks ; block + + )
2009-07-07 16:20:48 +02:00
{
2009-12-16 14:21:31 +01:00
unsigned int block_size = getBlockSize ( block ) ;
2009-12-16 18:13:23 +01:00
for ( unsigned int i = 0 ; i < block_size ; i + + )
2009-12-16 14:21:31 +01:00
{
equation_2_block [ getBlockEquationID ( block , i ) ] = block ;
variable_2_block [ getBlockVariableID ( block , i ) ] = block ;
}
2009-07-07 16:20:48 +02:00
}
2010-09-16 19:00:48 +02:00
derivative_endo = vector < derivative_t > ( nb_blocks ) ;
2018-06-04 16:36:46 +02:00
endo_max_leadlag_block = vector < pair < int , int > > ( nb_blocks , { 0 , 0 } ) ;
max_leadlag_block = vector < pair < int , int > > ( nb_blocks , { 0 , 0 } ) ;
2018-06-04 12:26:16 +02:00
for ( auto & first_derivative : first_derivatives )
2009-07-13 15:44:46 +02:00
{
2018-06-04 12:26:16 +02:00
int eq = first_derivative . first . first ;
int var = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( first_derivative . first . second ) ) ;
2009-12-16 14:21:31 +01:00
int lag = 0 ;
int block_eq = equation_2_block [ eq ] ;
int block_var = variable_2_block [ var ] ;
2018-06-04 16:36:46 +02:00
max_leadlag_block [ block_eq ] = { 0 , 0 } ;
max_leadlag_block [ block_eq ] = { 0 , 0 } ;
endo_max_leadlag_block [ block_eq ] = { 0 , 0 } ;
endo_max_leadlag_block [ block_eq ] = { 0 , 0 } ;
2010-09-16 19:00:48 +02:00
derivative_t tmp_derivative ;
lag_var_t lag_var ;
2018-07-17 18:34:07 +02:00
if ( getTypeByDerivID ( first_derivative . first . second ) = = SymbolType : : endogenous & & block_eq = = block_var )
2009-12-16 14:21:31 +01:00
{
tmp_derivative = derivative_endo [ block_eq ] ;
2018-07-17 18:34:07 +02:00
tmp_derivative [ { lag , { eq , var } } ] = first_derivatives [ { eq , getDerivID ( symbol_table . getID ( SymbolType : : endogenous , var ) , lag ) } ] ;
2009-12-16 14:21:31 +01:00
derivative_endo [ block_eq ] = tmp_derivative ;
}
2009-07-13 15:44:46 +02:00
}
}
2009-12-16 14:21:31 +01:00
void
2017-08-24 15:35:10 +02:00
StaticModel : : writeLatexFile ( const string & basename , bool write_equation_tags ) const
2009-12-16 18:13:23 +01:00
{
2018-09-05 18:27:13 +02:00
writeLatexModelFile ( basename + " _static " , ExprNodeOutputType : : latexStaticModel , write_equation_tags ) ;
2009-12-16 18:13:23 +01:00
}
2009-07-07 17:58:17 +02:00
2009-09-30 17:10:31 +02:00
void
2010-04-27 17:04:52 +02:00
StaticModel : : writeAuxVarInitval ( ostream & output , ExprNodeOutputType output_type ) const
2009-09-30 17:10:31 +02:00
{
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
2009-09-30 17:10:31 +02:00
{
2018-06-04 12:26:16 +02:00
dynamic_cast < ExprNode * > ( aux_equation ) - > writeOutput ( output , output_type ) ;
2009-09-30 17:10:31 +02:00
output < < " ; " < < endl ;
}
}
2011-07-24 20:52:03 +02:00
2017-06-14 07:01:31 +02:00
void
StaticModel : : writeSetAuxiliaryVariables ( const string & basename , const bool julia ) const
2011-07-24 20:52:03 +02:00
{
2017-08-29 10:58:39 +02:00
ostringstream output_func_body ;
2018-09-05 18:27:13 +02:00
writeAuxVarRecursiveDefinitions ( output_func_body , ExprNodeOutputType : : matlabStaticModel ) ;
2017-08-29 10:58:39 +02:00
if ( output_func_body . str ( ) . empty ( ) )
return ;
2017-06-14 07:01:31 +02:00
2018-06-27 15:01:31 +02:00
string func_name = julia ? basename + " _set_auxiliary_variables " : " set_auxiliary_variables " ;
string filename = julia ? func_name + " .jl " : packageDir ( basename ) + " / " + func_name + " .m " ;
2015-08-17 15:36:18 +02:00
string comment = julia ? " # " : " % " ;
2011-07-24 20:52:03 +02:00
ofstream output ;
2018-06-27 15:12:12 +02:00
output . open ( filename , ios : : out | ios : : binary ) ;
2011-07-24 20:52:03 +02:00
if ( ! output . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
output < < " function y = " < < func_name + " (y, x, params) " < < endl
2015-08-17 15:36:18 +02:00
< < comment < < endl
< < comment < < " Status : Computes static model for Dynare " < < endl
< < comment < < endl
< < comment < < " Warning : this file is generated automatically by Dynare " < < endl
2017-08-29 10:58:39 +02:00
< < comment < < " from model file (.mod) " < < endl < < endl
< < output_func_body . str ( ) ;
2018-03-02 18:39:16 +01:00
output . close ( ) ;
2016-04-04 17:11:03 +02:00
}
2015-05-12 16:36:03 +02:00
2016-04-04 17:11:03 +02:00
void
StaticModel : : writeAuxVarRecursiveDefinitions ( ostream & output , ExprNodeOutputType output_type ) const
{
deriv_node_temp_terms_t tef_terms ;
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
if ( dynamic_cast < ExprNode * > ( aux_equation ) - > containsExternalFunction ( ) )
2018-09-05 18:27:13 +02:00
dynamic_cast < ExprNode * > ( aux_equation ) - > writeExternalFunctionOutput ( output , ExprNodeOutputType : : matlabStaticModel , { } , { } , tef_terms ) ;
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
2011-07-24 20:52:03 +02:00
{
2018-06-04 12:26:16 +02:00
dynamic_cast < ExprNode * > ( aux_equation - > substituteStaticAuxiliaryDefinition ( ) ) - > writeOutput ( output , output_type ) ;
2011-07-24 20:52:03 +02:00
output < < " ; " < < endl ;
}
}
2012-11-29 18:07:48 +01:00
2017-08-30 11:32:01 +02:00
void
StaticModel : : writeLatexAuxVarRecursiveDefinitions ( ostream & output ) const
{
deriv_node_temp_terms_t tef_terms ;
temporary_terms_t temporary_terms ;
2018-03-27 17:14:30 +02:00
temporary_terms_idxs_t temporary_terms_idxs ;
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
if ( dynamic_cast < ExprNode * > ( aux_equation ) - > containsExternalFunction ( ) )
2018-09-05 18:27:13 +02:00
dynamic_cast < ExprNode * > ( aux_equation ) - > writeExternalFunctionOutput ( output , ExprNodeOutputType : : latexStaticModel ,
2018-03-27 17:14:30 +02:00
temporary_terms , temporary_terms_idxs , tef_terms ) ;
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
2017-08-30 11:32:01 +02:00
{
output < < " \\ begin{dmath} " < < endl ;
2018-09-05 18:27:13 +02:00
dynamic_cast < ExprNode * > ( aux_equation - > substituteStaticAuxiliaryDefinition ( ) ) - > writeOutput ( output , ExprNodeOutputType : : latexStaticModel ) ;
2017-08-30 11:32:01 +02:00
output < < endl < < " \\ end{dmath} " < < endl ;
}
}
2017-10-16 17:24:55 +02:00
void
StaticModel : : writeJsonAuxVarRecursiveDefinitions ( ostream & output ) const
{
deriv_node_temp_terms_t tef_terms ;
temporary_terms_t temporary_terms ;
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
if ( dynamic_cast < ExprNode * > ( aux_equation ) - > containsExternalFunction ( ) )
2017-10-16 17:24:55 +02:00
{
vector < string > efout ;
2018-06-04 12:26:16 +02:00
dynamic_cast < ExprNode * > ( aux_equation ) - > writeJsonExternalFunctionOutput ( efout ,
2017-10-16 17:24:55 +02:00
temporary_terms ,
tef_terms ,
false ) ;
for ( vector < string > : : const_iterator it = efout . begin ( ) ; it ! = efout . end ( ) ; it + + )
{
if ( it ! = efout . begin ( ) )
output < < " , " ;
output < < * it ;
}
}
2018-06-04 12:26:16 +02:00
for ( auto aux_equation : aux_equations )
2017-10-16 17:24:55 +02:00
{
output < < " , { \" lhs \" : \" " ;
2018-06-04 12:26:16 +02:00
aux_equation - > get_arg1 ( ) - > writeJsonOutput ( output , temporary_terms , tef_terms , false ) ;
2017-10-16 17:24:55 +02:00
output < < " \" , \" rhs \" : \" " ;
2018-06-04 12:26:16 +02:00
dynamic_cast < BinaryOpNode * > ( aux_equation - > substituteStaticAuxiliaryDefinition ( ) ) - > get_arg2 ( ) - > writeJsonOutput ( output , temporary_terms , tef_terms , false ) ;
2017-10-16 17:24:55 +02:00
output < < " \" } " ;
}
}
2012-11-29 18:07:48 +01:00
void
2015-07-28 12:29:42 +02:00
StaticModel : : writeParamsDerivativesFile ( const string & basename , bool julia ) const
2012-11-29 18:07:48 +01:00
{
if ( ! residuals_params_derivatives . size ( )
& & ! residuals_params_second_derivatives . size ( )
& & ! jacobian_params_derivatives . size ( )
& & ! jacobian_params_second_derivatives . size ( )
& & ! hessian_params_derivatives . size ( ) )
return ;
2018-09-05 18:27:13 +02:00
ExprNodeOutputType output_type = ( julia ? ExprNodeOutputType : : juliaStaticModel : ExprNodeOutputType : : matlabStaticModel ) ;
2015-07-28 12:29:42 +02:00
2017-01-05 17:31:48 +01:00
ostringstream model_local_vars_output ; // Used for storing model local vars
ostringstream model_output ; // Used for storing model
ostringstream jacobian_output ; // Used for storing jacobian equations
ostringstream hessian_output ; // Used for storing Hessian equations
ostringstream hessian1_output ; // Used for storing Hessian equations
ostringstream third_derivs_output ; // Used for storing third order derivatives equations
ostringstream third_derivs1_output ; // Used for storing third order derivatives equations
2012-11-29 18:07:48 +01:00
deriv_node_temp_terms_t tef_terms ;
2018-05-28 15:23:15 +02:00
writeTemporaryTerms ( params_derivs_temporary_terms , { } , params_derivs_temporary_terms_idxs , model_output , output_type , tef_terms ) ;
2012-11-29 18:07:48 +01:00
2018-06-04 12:26:16 +02:00
for ( const auto & residuals_params_derivative : residuals_params_derivatives )
2012-11-29 18:07:48 +01:00
{
2018-06-05 15:34:34 +02:00
int eq , param ;
tie ( eq , param ) = residuals_params_derivative . first ;
2018-06-04 12:26:16 +02:00
expr_t d1 = residuals_params_derivative . second ;
2012-11-29 18:07:48 +01:00
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
2017-01-05 17:31:48 +01:00
jacobian_output < < " rp " < < LEFT_ARRAY_SUBSCRIPT ( output_type )
< < eq + 1 < < " , " < < param_col
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " ;
2018-05-28 15:23:15 +02:00
d1 - > writeOutput ( jacobian_output , output_type , params_derivs_temporary_terms , params_derivs_temporary_terms_idxs , tef_terms ) ;
2017-01-05 17:31:48 +01:00
jacobian_output < < " ; " < < endl ;
2012-11-29 18:07:48 +01:00
}
2018-06-04 12:26:16 +02:00
for ( const auto & jacobian_params_derivative : jacobian_params_derivatives )
2012-11-29 18:07:48 +01:00
{
2018-06-05 15:34:34 +02:00
int eq , var , param ;
tie ( eq , var , param ) = jacobian_params_derivative . first ;
2018-06-04 12:26:16 +02:00
expr_t d2 = jacobian_params_derivative . second ;
2012-11-29 18:07:48 +01:00
int var_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var ) ) + 1 ;
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
2017-01-05 17:31:48 +01:00
hessian_output < < " gp " < < LEFT_ARRAY_SUBSCRIPT ( output_type )
< < eq + 1 < < " , " < < var_col < < " , " < < param_col
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " ;
2018-05-28 15:23:15 +02:00
d2 - > writeOutput ( hessian_output , output_type , params_derivs_temporary_terms , params_derivs_temporary_terms_idxs , tef_terms ) ;
2017-01-05 17:31:48 +01:00
hessian_output < < " ; " < < endl ;
2012-11-29 18:07:48 +01:00
}
int i = 1 ;
2018-06-05 15:34:34 +02:00
for ( const auto & it : residuals_params_second_derivatives )
2012-11-29 18:07:48 +01:00
{
2018-06-05 15:34:34 +02:00
int eq , param1 , param2 ;
tie ( eq , param1 , param2 ) = it . first ;
expr_t d2 = it . second ;
2012-11-29 18:07:48 +01:00
int param1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param1 ) ) + 1 ;
int param2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param2 ) ) + 1 ;
2017-01-05 17:31:48 +01:00
hessian1_output < < " rpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,1 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < eq + 1 < < " ; " < < endl
< < " rpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,2 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < param1_col < < " ; " < < endl
< < " rpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,3 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < param2_col < < " ; " < < endl
< < " rpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,4 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " ;
2018-05-28 15:23:15 +02:00
d2 - > writeOutput ( hessian1_output , output_type , params_derivs_temporary_terms , params_derivs_temporary_terms_idxs , tef_terms ) ;
2017-01-05 17:31:48 +01:00
hessian1_output < < " ; " < < endl ;
2018-06-05 15:34:34 +02:00
i + + ;
2012-11-29 18:07:48 +01:00
}
i = 1 ;
2018-06-05 15:34:34 +02:00
for ( const auto & it : jacobian_params_second_derivatives )
2012-11-29 18:07:48 +01:00
{
2018-06-05 15:34:34 +02:00
int eq , var , param1 , param2 ;
tie ( eq , var , param1 , param2 ) = it . first ;
expr_t d2 = it . second ;
2012-11-29 18:07:48 +01:00
int var_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var ) ) + 1 ;
int param1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param1 ) ) + 1 ;
int param2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param2 ) ) + 1 ;
2017-01-05 17:31:48 +01:00
third_derivs_output < < " gpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,1 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < eq + 1 < < " ; " < < endl
< < " gpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,2 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < var_col < < " ; " < < endl
< < " gpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,3 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < param1_col < < " ; " < < endl
< < " gpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,4 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < param2_col < < " ; " < < endl
< < " gpp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,5 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " ;
2018-05-28 15:23:15 +02:00
d2 - > writeOutput ( third_derivs_output , output_type , params_derivs_temporary_terms , params_derivs_temporary_terms_idxs , tef_terms ) ;
2017-01-05 17:31:48 +01:00
third_derivs_output < < " ; " < < endl ;
2018-06-05 15:34:34 +02:00
i + + ;
2012-11-29 18:07:48 +01:00
}
i = 1 ;
2018-06-05 15:34:34 +02:00
for ( const auto & it : hessian_params_derivatives )
2012-11-29 18:07:48 +01:00
{
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 , param ;
tie ( eq , var1 , var2 , param ) = it . first ;
expr_t d2 = it . second ;
2012-11-29 18:07:48 +01:00
int var1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var1 ) ) + 1 ;
int var2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var2 ) ) + 1 ;
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
2017-01-05 17:31:48 +01:00
third_derivs1_output < < " hp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,1 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < eq + 1 < < " ; " < < endl
< < " hp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,2 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < var1_col < < " ; " < < endl
< < " hp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,3 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < var2_col < < " ; " < < endl
< < " hp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,4 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " < < param_col < < " ; " < < endl
< < " hp " < < LEFT_ARRAY_SUBSCRIPT ( output_type ) < < i < < " ,5 "
< < RIGHT_ARRAY_SUBSCRIPT ( output_type ) < < " = " ;
2018-05-28 15:23:15 +02:00
d2 - > writeOutput ( third_derivs1_output , output_type , params_derivs_temporary_terms , params_derivs_temporary_terms_idxs , tef_terms ) ;
2017-01-05 17:31:48 +01:00
third_derivs1_output < < " ; " < < endl ;
2018-06-05 15:34:34 +02:00
i + + ;
2012-11-29 18:07:48 +01:00
}
2017-01-05 17:31:48 +01:00
ofstream paramsDerivsFile ;
2018-06-27 15:01:31 +02:00
string filename = julia ? basename + " StaticParamsDerivs.jl " : packageDir ( basename ) + " /static_params_derivs.m " ;
2018-06-27 15:12:12 +02:00
paramsDerivsFile . open ( filename , ios : : out | ios : : binary ) ;
2017-01-05 17:31:48 +01:00
if ( ! paramsDerivsFile . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( ! julia )
{
// Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
map < string , string > tmp_paren_vars ;
bool message_printed = false ;
fixNestedParenthesis ( model_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( model_local_vars_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( jacobian_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( hessian_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( hessian1_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( third_derivs_output , tmp_paren_vars , message_printed ) ;
fixNestedParenthesis ( third_derivs1_output , tmp_paren_vars , message_printed ) ;
2018-06-27 15:01:31 +02:00
paramsDerivsFile < < " function [rp, gp, rpp, gpp, hp] = static_params_derivs(y, x, params) " < < endl
2017-01-05 17:31:48 +01:00
< < " % " < < endl
< < " % Status : Computes derivatives of the static model with respect to the parameters " < < endl
< < " % " < < endl
< < " % Inputs : " < < endl
< < " % y [M_.endo_nbr by 1] double vector of endogenous variables in declaration order " < < endl
< < " % x [M_.exo_nbr by 1] double vector of exogenous variables in declaration order " < < endl
< < " % params [M_.param_nbr by 1] double vector of parameter values in declaration order " < < endl
< < " % " < < endl
< < " % Outputs: " < < endl
< < " % rp [M_.eq_nbr by #params] double Jacobian matrix of static model equations with respect to parameters " < < endl
< < " % Dynare may prepend or append auxiliary equations, see M_.aux_vars " < < endl
< < " % gp [M_.endo_nbr by M_.endo_nbr by #params] double Derivative of the Jacobian matrix of the static model equations with respect to the parameters " < < endl
< < " % rows: variables in declaration order " < < endl
< < " % rows: equations in order of declaration " < < endl
< < " % rpp [#second_order_residual_terms by 4] double Hessian matrix of second derivatives of residuals with respect to parameters; " < < endl
< < " % rows: respective derivative term " < < endl
< < " % 1st column: equation number of the term appearing " < < endl
< < " % 2nd column: number of the first parameter in derivative " < < endl
< < " % 3rd column: number of the second parameter in derivative " < < endl
< < " % 4th column: value of the Hessian term " < < endl
< < " % gpp [#second_order_Jacobian_terms by 5] double Hessian matrix of second derivatives of the Jacobian with respect to the parameters; " < < endl
< < " % rows: respective derivative term " < < endl
< < " % 1st column: equation number of the term appearing " < < endl
< < " % 2nd column: column number of variable in Jacobian of the static model " < < endl
< < " % 3rd column: number of the first parameter in derivative " < < endl
< < " % 4th column: number of the second parameter in derivative " < < endl
< < " % 5th column: value of the Hessian term " < < endl
< < " % " < < endl
< < " % " < < endl
< < " % Warning : this file is generated automatically by Dynare " < < endl
< < " % from model file (.mod) " < < endl < < endl
2018-05-28 15:23:15 +02:00
< < " T = NaN( " < < params_derivs_temporary_terms_idxs . size ( ) < < " ,1); " < < endl
2017-01-05 17:31:48 +01:00
< < model_local_vars_output . str ( )
< < model_output . str ( )
2017-02-24 11:20:54 +01:00
< < " rp = zeros( " < < equations . size ( ) < < " , "
2017-01-05 17:31:48 +01:00
< < symbol_table . param_nbr ( ) < < " ); " < < endl
< < jacobian_output . str ( )
2017-02-24 11:20:54 +01:00
< < " gp = zeros( " < < equations . size ( ) < < " , " < < symbol_table . endo_nbr ( ) < < " , "
2017-01-05 17:31:48 +01:00
< < symbol_table . param_nbr ( ) < < " ); " < < endl
< < hessian_output . str ( )
< < " if nargout >= 3 " < < endl
< < " rpp = zeros( " < < residuals_params_second_derivatives . size ( ) < < " ,4); " < < endl
< < hessian1_output . str ( )
< < " gpp = zeros( " < < jacobian_params_second_derivatives . size ( ) < < " ,5); " < < endl
< < third_derivs_output . str ( )
< < " end " < < endl
< < " if nargout >= 5 " < < endl
< < " hp = zeros( " < < hessian_params_derivatives . size ( ) < < " ,5); " < < endl
< < third_derivs1_output . str ( )
< < " end " < < endl
< < " end " < < endl ;
}
else
paramsDerivsFile < < " module " < < basename < < " StaticParamsDerivs " < < endl
< < " # " < < endl
< < " # NB: this file was automatically generated by Dynare " < < endl
< < " # from " < < basename < < " .mod " < < endl
< < " # " < < endl
< < " export params_derivs " < < endl < < endl
< < " function params_derivs(y, x, params) " < < endl
< < model_local_vars_output . str ( )
< < model_output . str ( )
2017-02-24 11:20:54 +01:00
< < " rp = zeros( " < < equations . size ( ) < < " , "
2017-01-05 17:31:48 +01:00
< < symbol_table . param_nbr ( ) < < " ); " < < endl
< < jacobian_output . str ( )
2017-02-24 11:20:54 +01:00
< < " gp = zeros( " < < equations . size ( ) < < " , " < < symbol_table . endo_nbr ( ) < < " , "
2017-01-05 17:31:48 +01:00
< < symbol_table . param_nbr ( ) < < " ); " < < endl
< < hessian_output . str ( )
< < " rpp = zeros( " < < residuals_params_second_derivatives . size ( ) < < " ,4); " < < endl
< < hessian1_output . str ( )
< < " gpp = zeros( " < < jacobian_params_second_derivatives . size ( ) < < " ,5); " < < endl
< < third_derivs_output . str ( )
< < " hp = zeros( " < < hessian_params_derivatives . size ( ) < < " ,5); " < < endl
< < third_derivs1_output . str ( )
< < " (rp, gp, rpp, gpp, hp) " < < endl
< < " end " < < endl
< < " end " < < endl ;
2012-11-29 18:07:48 +01:00
paramsDerivsFile . close ( ) ;
}
2017-02-20 12:18:11 +01:00
2017-02-27 15:40:34 +01:00
void
StaticModel : : writeJsonOutput ( ostream & output ) const
{
writeJsonModelEquations ( output , false ) ;
}
2017-02-20 12:18:11 +01:00
void
2017-03-02 18:34:18 +01:00
StaticModel : : writeJsonComputingPassOutput ( ostream & output , bool writeDetails ) const
2017-02-20 12:18:11 +01:00
{
ostringstream model_local_vars_output ; // Used for storing model local vars
ostringstream model_output ; // Used for storing model
ostringstream jacobian_output ; // Used for storing jacobian equations
ostringstream hessian_output ; // Used for storing Hessian equations
ostringstream third_derivatives_output ; // Used for storing third order derivatives equations
deriv_node_temp_terms_t tef_terms ;
temporary_terms_t temp_term_union = temporary_terms_res ;
temporary_terms_t temp_term_union_m_1 ;
string concat = " " ;
writeJsonModelLocalVariables ( model_local_vars_output , tef_terms ) ;
writeJsonTemporaryTerms ( temporary_terms_res , temp_term_union_m_1 , model_output , tef_terms , concat ) ;
2017-02-27 15:40:34 +01:00
model_output < < " , " ;
2017-02-20 12:18:11 +01:00
writeJsonModelEquations ( model_output , true ) ;
int nrows = equations . size ( ) ;
int JacobianColsNbr = symbol_table . endo_nbr ( ) ;
int hessianColsNbr = JacobianColsNbr * JacobianColsNbr ;
// Write Jacobian w.r. to endogenous only
temp_term_union_m_1 = temp_term_union ;
temp_term_union . insert ( temporary_terms_g1 . begin ( ) , temporary_terms_g1 . end ( ) ) ;
concat = " jacobian " ;
writeJsonTemporaryTerms ( temp_term_union , temp_term_union_m_1 , jacobian_output , tef_terms , concat ) ;
jacobian_output < < " , \" jacobian \" : { "
< < " \" nrows \" : " < < nrows
< < " , \" ncols \" : " < < JacobianColsNbr
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = first_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = first_derivatives . end ( ) ; it + + )
{
if ( it ! = first_derivatives . begin ( ) )
jacobian_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var ;
tie ( eq , var ) = it - > first ;
2017-03-02 18:34:18 +01:00
int symb_id = getSymbIDByDerivID ( var ) ;
int col = symbol_table . getTypeSpecificID ( symb_id ) ;
2017-02-20 12:18:11 +01:00
expr_t d1 = it - > second ;
2017-03-02 18:34:18 +01:00
if ( writeDetails )
2017-06-29 12:42:28 +02:00
jacobian_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
jacobian_output < < " { \" row \" : " < < eq + 1 ;
2017-06-29 12:42:28 +02:00
jacobian_output < < " , \" col \" : " < < col + 1 ;
if ( writeDetails )
jacobian_output < < " , \" var \" : \" " < < symbol_table . getName ( symb_id ) < < " \" " ;
jacobian_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d1 - > writeJsonOutput ( jacobian_output , temp_term_union , tef_terms ) ;
jacobian_output < < " \" } " < < endl ;
}
jacobian_output < < " ]} " ;
int g2ncols = symbol_table . endo_nbr ( ) * symbol_table . endo_nbr ( ) ;
// Write Hessian w.r. to endogenous only (only if 2nd order derivatives have been computed)
temp_term_union_m_1 = temp_term_union ;
temp_term_union . insert ( temporary_terms_g2 . begin ( ) , temporary_terms_g2 . end ( ) ) ;
concat = " hessian " ;
writeJsonTemporaryTerms ( temp_term_union , temp_term_union_m_1 , hessian_output , tef_terms , concat ) ;
hessian_output < < " , \" hessian \" : { "
< < " \" nrows \" : " < < equations . size ( )
< < " , \" ncols \" : " < < g2ncols
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = second_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = second_derivatives . end ( ) ; it + + )
{
if ( it ! = second_derivatives . begin ( ) )
hessian_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 ;
tie ( eq , var1 , var2 ) = it - > first ;
int symb_id1 = getSymbIDByDerivID ( var1 ) ;
int symb_id2 = getSymbIDByDerivID ( var2 ) ;
2017-02-20 12:18:11 +01:00
expr_t d2 = it - > second ;
2017-03-02 18:34:18 +01:00
int tsid1 = symbol_table . getTypeSpecificID ( symb_id1 ) ;
int tsid2 = symbol_table . getTypeSpecificID ( symb_id2 ) ;
int col = tsid1 * symbol_table . endo_nbr ( ) + tsid2 ;
int col_sym = tsid2 * symbol_table . endo_nbr ( ) + tsid1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
hessian_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
hessian_output < < " { \" row \" : " < < eq + 1 ;
hessian_output < < " , \" col \" : [ " < < col + 1 ;
2017-06-29 12:42:28 +02:00
if ( writeDetails )
hessian_output < < " , \" var1 \" : \" " < < symbol_table . getName ( symb_id1 ) < < " \" "
< < " , \" var2 \" : \" " < < symbol_table . getName ( symb_id2 ) < < " \" " ;
2017-03-02 18:34:18 +01:00
if ( symb_id1 ! = symb_id2 )
hessian_output < < " , " < < col_sym + 1 ;
hessian_output < < " ] "
2017-02-20 12:18:11 +01:00
< < " , \" val \" : \" " ;
d2 - > writeJsonOutput ( hessian_output , temp_term_union , tef_terms ) ;
hessian_output < < " \" } " < < endl ;
}
hessian_output < < " ]} " ;
// Writing third derivatives
temp_term_union_m_1 = temp_term_union ;
temp_term_union . insert ( temporary_terms_g3 . begin ( ) , temporary_terms_g3 . end ( ) ) ;
concat = " third_derivatives " ;
writeJsonTemporaryTerms ( temp_term_union , temp_term_union_m_1 , third_derivatives_output , tef_terms , concat ) ;
third_derivatives_output < < " , \" third_derivative \" : { "
< < " \" nrows \" : " < < equations . size ( )
< < " , \" ncols \" : " < < hessianColsNbr * JacobianColsNbr
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = third_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = third_derivatives . end ( ) ; it + + )
{
if ( it ! = third_derivatives . begin ( ) )
third_derivatives_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 , var3 ;
tie ( eq , var1 , var2 , var3 ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d3 = it - > second ;
2017-03-02 18:34:18 +01:00
if ( writeDetails )
2017-06-29 12:42:28 +02:00
third_derivatives_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
third_derivatives_output < < " { \" row \" : " < < eq + 1 ;
int id1 = getSymbIDByDerivID ( var1 ) ;
int id2 = getSymbIDByDerivID ( var2 ) ;
int id3 = getSymbIDByDerivID ( var3 ) ;
set < int > cols ;
cols . insert ( id1 * hessianColsNbr + id2 * JacobianColsNbr + id3 ) ;
cols . insert ( id1 * hessianColsNbr + id3 * JacobianColsNbr + id2 ) ;
cols . insert ( id2 * hessianColsNbr + id1 * JacobianColsNbr + id3 ) ;
cols . insert ( id2 * hessianColsNbr + id3 * JacobianColsNbr + id1 ) ;
cols . insert ( id3 * hessianColsNbr + id1 * JacobianColsNbr + id2 ) ;
cols . insert ( id3 * hessianColsNbr + id2 * JacobianColsNbr + id1 ) ;
third_derivatives_output < < " , \" col \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it2 = cols . begin ( ) ; it2 ! = cols . end ( ) ; it2 + + )
2017-03-02 18:34:18 +01:00
{
if ( it2 ! = cols . begin ( ) )
third_derivatives_output < < " , " ;
third_derivatives_output < < * it2 + 1 ;
}
2017-06-29 12:42:28 +02:00
third_derivatives_output < < " ] " ;
if ( writeDetails )
third_derivatives_output < < " , \" var1 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var1 ) ) < < " \" "
< < " , \" var2 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var2 ) ) < < " \" "
< < " , \" var3 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var3 ) ) < < " \" " ;
third_derivatives_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d3 - > writeJsonOutput ( third_derivatives_output , temp_term_union , tef_terms ) ;
third_derivatives_output < < " \" } " < < endl ;
}
third_derivatives_output < < " ]} " ;
2017-03-02 18:34:18 +01:00
if ( writeDetails )
2017-06-29 12:42:28 +02:00
output < < " \" static_model \" : { " ;
2017-03-02 18:34:18 +01:00
else
2017-06-29 12:42:28 +02:00
output < < " \" static_model_simple \" : { " ;
2017-06-14 07:01:31 +02:00
output < < model_local_vars_output . str ( )
2017-02-20 12:18:11 +01:00
< < " , " < < model_output . str ( )
< < " , " < < jacobian_output . str ( )
< < " , " < < hessian_output . str ( )
< < " , " < < third_derivatives_output . str ( )
< < " } " ;
}
void
2017-03-02 18:34:18 +01:00
StaticModel : : writeJsonParamsDerivativesFile ( ostream & output , bool writeDetails ) const
2017-02-20 12:18:11 +01:00
{
if ( ! residuals_params_derivatives . size ( )
& & ! residuals_params_second_derivatives . size ( )
& & ! jacobian_params_derivatives . size ( )
& & ! jacobian_params_second_derivatives . size ( )
& & ! hessian_params_derivatives . size ( ) )
return ;
ostringstream model_local_vars_output ; // Used for storing model local vars
ostringstream model_output ; // Used for storing model temp vars and equations
ostringstream jacobian_output ; // Used for storing jacobian equations
ostringstream hessian_output ; // Used for storing Hessian equations
ostringstream hessian1_output ; // Used for storing Hessian equations
ostringstream third_derivs_output ; // Used for storing third order derivatives equations
ostringstream third_derivs1_output ; // Used for storing third order derivatives equations
deriv_node_temp_terms_t tef_terms ;
writeJsonModelLocalVariables ( model_local_vars_output , tef_terms ) ;
temporary_terms_t temp_terms_empty ;
string concat = " all " ;
writeJsonTemporaryTerms ( params_derivs_temporary_terms , temp_terms_empty , model_output , tef_terms , concat ) ;
jacobian_output < < " \" deriv_wrt_params \" : { "
< < " \" neqs \" : " < < equations . size ( )
< < " , \" nparamcols \" : " < < symbol_table . param_nbr ( )
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = residuals_params_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = residuals_params_derivatives . end ( ) ; it + + )
{
if ( it ! = residuals_params_derivatives . begin ( ) )
jacobian_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , param ;
tie ( eq , param ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d1 = it - > second ;
2017-03-02 18:34:18 +01:00
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
jacobian_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
jacobian_output < < " { \" row \" : " < < eq + 1 ;
2017-06-29 12:42:28 +02:00
if ( writeDetails )
jacobian_output < < " , \" param_col \" : " < < param_col ;
jacobian_output < < " , \" param \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param ) ) < < " \" " ;
jacobian_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d1 - > writeJsonOutput ( jacobian_output , params_derivs_temporary_terms , tef_terms ) ;
jacobian_output < < " \" } " < < endl ;
}
jacobian_output < < " ]} " ;
hessian_output < < " \" deriv_jacobian_wrt_params \" : { "
< < " \" neqs \" : " < < equations . size ( )
< < " , \" nvarcols \" : " < < symbol_table . endo_nbr ( )
< < " , \" nparamcols \" : " < < symbol_table . param_nbr ( )
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = jacobian_params_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = jacobian_params_derivatives . end ( ) ; it + + )
{
if ( it ! = jacobian_params_derivatives . begin ( ) )
hessian_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var , param ;
tie ( eq , var , param ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d2 = it - > second ;
2017-03-02 18:34:18 +01:00
int var_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var ) ) + 1 ;
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
hessian_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
hessian_output < < " { \" row \" : " < < eq + 1 ;
2017-06-29 12:42:28 +02:00
if ( writeDetails )
hessian_output < < " , \" var \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var ) ) < < " \" "
< < " , \" param \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param ) ) < < " \" " ;
2017-03-02 18:34:18 +01:00
hessian_output < < " , \" var_col \" : " < < var_col
< < " , \" param_col \" : " < < param_col
2017-02-20 12:18:11 +01:00
< < " , \" val \" : \" " ;
d2 - > writeJsonOutput ( hessian_output , params_derivs_temporary_terms , tef_terms ) ;
hessian_output < < " \" } " < < endl ;
}
hessian_output < < " ]} " ;
hessian1_output < < " \" second_deriv_residuals_wrt_params \" : { "
< < " \" nrows \" : " < < equations . size ( )
< < " , \" nparam1cols \" : " < < symbol_table . param_nbr ( )
< < " , \" nparam2cols \" : " < < symbol_table . param_nbr ( )
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = residuals_params_second_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = residuals_params_second_derivatives . end ( ) ; + + it )
{
if ( it ! = residuals_params_second_derivatives . begin ( ) )
hessian1_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , param1 , param2 ;
tie ( eq , param1 , param2 ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d2 = it - > second ;
2017-03-02 18:34:18 +01:00
int param1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param1 ) ) + 1 ;
int param2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param2 ) ) + 1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
hessian1_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
hessian1_output < < " { \" row \" : " < < eq + 1 ;
2017-06-29 12:42:28 +02:00
2017-03-02 18:34:18 +01:00
hessian1_output < < " , \" param1_col \" : " < < param1_col
2017-06-29 12:42:28 +02:00
< < " , \" param2_col \" : " < < param2_col ;
if ( writeDetails )
hessian1_output < < " , \" param1 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param1 ) ) < < " \" "
< < " , \" param2 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param2 ) ) < < " \" " ;
hessian1_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d2 - > writeJsonOutput ( hessian1_output , params_derivs_temporary_terms , tef_terms ) ;
hessian1_output < < " \" } " < < endl ;
}
hessian1_output < < " ]} " ;
third_derivs_output < < " \" second_deriv_jacobian_wrt_params \" : { "
< < " \" neqs \" : " < < equations . size ( )
< < " , \" nvarcols \" : " < < symbol_table . endo_nbr ( )
< < " , \" nparam1cols \" : " < < symbol_table . param_nbr ( )
< < " , \" nparam2cols \" : " < < symbol_table . param_nbr ( )
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = jacobian_params_second_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = jacobian_params_second_derivatives . end ( ) ; + + it )
{
if ( it ! = jacobian_params_second_derivatives . begin ( ) )
third_derivs_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var , param1 , param2 ;
tie ( eq , var , param1 , param2 ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d2 = it - > second ;
2017-03-02 18:34:18 +01:00
int var_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var ) ) + 1 ;
int param1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param1 ) ) + 1 ;
int param2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param2 ) ) + 1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
third_derivs_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
third_derivs_output < < " { \" row \" : " < < eq + 1 ;
third_derivs_output < < " , \" var_col \" : " < < var_col
< < " , \" param1_col \" : " < < param1_col
2017-06-29 12:42:28 +02:00
< < " , \" param2_col \" : " < < param2_col ;
if ( writeDetails )
third_derivs_output < < " , \" var \" : \" " < < symbol_table . getName ( var ) < < " \" "
< < " , \" param1 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param1 ) ) < < " \" "
< < " , \" param2 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param2 ) ) < < " \" " ;
third_derivs_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d2 - > writeJsonOutput ( third_derivs_output , params_derivs_temporary_terms , tef_terms ) ;
third_derivs_output < < " \" } " < < endl ;
}
third_derivs_output < < " ]} " < < endl ;
third_derivs1_output < < " \" derivative_hessian_wrt_params \" : { "
< < " \" neqs \" : " < < equations . size ( )
< < " , \" nvar1cols \" : " < < symbol_table . endo_nbr ( )
< < " , \" nvar2cols \" : " < < symbol_table . endo_nbr ( )
< < " , \" nparamcols \" : " < < symbol_table . param_nbr ( )
< < " , \" entries \" : [ " ;
2018-06-04 15:03:26 +02:00
for ( auto it = hessian_params_derivatives . begin ( ) ;
2017-02-20 12:18:11 +01:00
it ! = hessian_params_derivatives . end ( ) ; + + it )
{
if ( it ! = hessian_params_derivatives . begin ( ) )
third_derivs1_output < < " , " ;
2018-06-05 15:34:34 +02:00
int eq , var1 , var2 , param ;
tie ( eq , var1 , var2 , param ) = it - > first ;
2017-02-20 12:18:11 +01:00
expr_t d2 = it - > second ;
2017-03-02 18:34:18 +01:00
int var1_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var1 ) ) + 1 ;
int var2_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( var2 ) ) + 1 ;
int param_col = symbol_table . getTypeSpecificID ( getSymbIDByDerivID ( param ) ) + 1 ;
if ( writeDetails )
2017-06-29 12:42:28 +02:00
third_derivs1_output < < " { \" eq \" : " < < eq + 1 ;
2017-03-02 18:34:18 +01:00
else
third_derivs1_output < < " { \" row \" : " < < eq + 1 ;
2017-06-29 12:42:28 +02:00
2017-03-02 18:34:18 +01:00
third_derivs1_output < < " , \" var1_col \" : " < < var1_col
< < " , \" var2_col \" : " < < var2_col
2017-06-29 12:42:28 +02:00
< < " , \" param_col \" : " < < param_col ;
if ( writeDetails )
third_derivs1_output < < " , \" var1 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var1 ) ) < < " \" "
< < " , \" var2 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( var2 ) ) < < " \" "
< < " , \" param1 \" : \" " < < symbol_table . getName ( getSymbIDByDerivID ( param ) ) < < " \" " ;
third_derivs1_output < < " , \" val \" : \" " ;
2017-02-20 12:18:11 +01:00
d2 - > writeJsonOutput ( third_derivs1_output , params_derivs_temporary_terms , tef_terms ) ;
third_derivs1_output < < " \" } " < < endl ;
}
third_derivs1_output < < " ]} " < < endl ;
2017-03-02 18:34:18 +01:00
if ( writeDetails )
2017-06-29 12:42:28 +02:00
output < < " \" static_model_params_derivative \" : { " ;
2017-03-02 18:34:18 +01:00
else
2017-06-29 12:42:28 +02:00
output < < " \" static_model_params_derivatives_simple \" : { " ;
2017-03-02 18:34:18 +01:00
output < < model_local_vars_output . str ( )
2017-02-20 12:18:11 +01:00
< < " , " < < model_output . str ( )
< < " , " < < jacobian_output . str ( )
< < " , " < < hessian_output . str ( )
< < " , " < < hessian1_output . str ( )
< < " , " < < third_derivs_output . str ( )
< < " , " < < third_derivs1_output . str ( )
< < " } " ;
}