2008-01-11 14:42:14 +01:00
/*
2014-02-17 17:52:37 +01:00
* Copyright ( C ) 2003 - 2014 Dynare Team
2008-01-11 14:42:14 +01:00
*
* This file is part of Dynare .
*
* Dynare is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Dynare is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Dynare . If not , see < http : //www.gnu.org/licenses/>.
*/
2009-07-06 12:36:36 +02:00
# include <iostream>
# include <fstream>
2012-01-23 16:52:27 +01:00
# include <sstream>
2009-03-24 17:29:58 +01:00
# include <cstdlib>
2006-11-28 12:56:02 +01:00
2009-03-24 17:29:58 +01:00
# include "NumericalInitialization.hh"
2009-02-27 13:19:25 +01:00
InitParamStatement : : InitParamStatement ( int symb_id_arg ,
2010-09-16 19:18:45 +02:00
const expr_t param_value_arg ,
2006-12-12 12:54:30 +01:00
const SymbolTable & symbol_table_arg ) :
2009-02-27 13:19:25 +01:00
symb_id ( symb_id_arg ) ,
2006-12-12 12:54:30 +01:00
param_value ( param_value_arg ) ,
symbol_table ( symbol_table_arg )
2006-11-05 00:31:17 +01:00
{
}
2010-06-21 18:40:36 +02:00
void
2012-01-23 16:52:27 +01:00
InitParamStatement : : checkPass ( ModFileStructure & mod_file_struct , WarningConsolidation & warnings )
2010-06-21 18:40:36 +02:00
{
if ( symbol_table . getName ( symb_id ) = = " dsge_prior_weight " )
mod_file_struct . dsge_prior_weight_initialized = true ;
}
2006-12-12 12:54:30 +01:00
void
2006-12-19 00:28:52 +01:00
InitParamStatement : : writeOutput ( ostream & output , const string & basename ) const
2006-11-05 00:31:17 +01:00
{
2009-02-27 13:19:25 +01:00
int id = symbol_table . getTypeSpecificID ( symb_id ) + 1 ;
2007-03-09 18:27:46 +01:00
output < < " M_.params( " < < id < < " ) = " ;
param_value - > writeOutput ( output ) ;
output < < " ; " < < endl ;
2013-10-19 15:45:42 +02:00
output < < symbol_table . getName ( symb_id ) < < " = M_.params( " < < id < < " ); \n " ;
2008-12-19 11:24:31 +01:00
}
2011-12-25 19:03:26 +01:00
void
InitParamStatement : : writeCOutput ( ostream & output , const string & basename )
{
int id = symbol_table . getTypeSpecificID ( symb_id ) ;
output < < " params[ " < < id < < " ] = " ;
param_value - > writeOutput ( output ) ;
output < < " ; " < < endl ;
output < < " double " < < symbol_table . getName ( symb_id ) < < " = params[ " < < id < < " ]; " < < endl ;
}
2009-02-27 13:19:25 +01:00
void
2010-09-16 19:00:48 +02:00
InitParamStatement : : fillEvalContext ( eval_context_t & eval_context ) const
2008-12-19 11:24:31 +01:00
{
2009-02-27 13:19:25 +01:00
try
{
eval_context [ symb_id ] = param_value - > eval ( eval_context ) ;
}
2009-12-16 18:13:23 +01:00
catch ( ExprNode : : EvalException & e )
2009-02-27 13:19:25 +01:00
{
// Do nothing
}
2008-12-19 11:24:31 +01:00
}
2010-09-16 19:00:48 +02:00
InitOrEndValStatement : : InitOrEndValStatement ( const init_values_t & init_values_arg ,
2012-10-31 15:23:02 +01:00
const SymbolTable & symbol_table_arg ,
const bool & all_values_required_arg ) :
2006-12-12 12:54:30 +01:00
init_values ( init_values_arg ) ,
2012-10-31 15:23:02 +01:00
symbol_table ( symbol_table_arg ) ,
all_values_required ( all_values_required_arg )
2006-11-05 00:31:17 +01:00
{
}
2009-02-27 13:19:25 +01:00
void
2010-09-16 19:00:48 +02:00
InitOrEndValStatement : : fillEvalContext ( eval_context_t & eval_context ) const
2009-02-27 13:19:25 +01:00
{
2010-09-16 19:00:48 +02:00
for ( init_values_t : : const_iterator it = init_values . begin ( ) ;
2009-12-16 18:13:23 +01:00
it ! = init_values . end ( ) ; it + + )
2009-02-27 13:19:25 +01:00
{
try
{
eval_context [ it - > first ] = ( it - > second ) - > eval ( eval_context ) ;
}
2009-12-16 18:13:23 +01:00
catch ( ExprNode : : EvalException & e )
2009-02-27 13:19:25 +01:00
{
// Do nothing
}
}
}
2012-10-31 15:23:02 +01:00
set < int >
InitOrEndValStatement : : getUninitializedVariables ( SymbolType type )
{
set < int > unused ;
if ( ! all_values_required )
return unused ;
if ( type = = eEndogenous )
unused = symbol_table . getEndogenous ( ) ;
else if ( type = = eExogenous )
unused = symbol_table . getExogenous ( ) ;
else
{
cerr < < " ERROR: Shouldn't arrive here. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
set < int > : : iterator sit ;
for ( init_values_t : : const_iterator it = init_values . begin ( ) ;
it ! = init_values . end ( ) ; it + + )
{
sit = unused . find ( it - > first ) ;
if ( sit ! = unused . end ( ) )
unused . erase ( sit ) ;
}
return unused ;
}
2006-12-12 12:54:30 +01:00
void
InitOrEndValStatement : : writeInitValues ( ostream & output ) const
2006-11-05 00:31:17 +01:00
{
2010-09-16 19:00:48 +02:00
for ( init_values_t : : const_iterator it = init_values . begin ( ) ;
2009-12-16 18:13:23 +01:00
it ! = init_values . end ( ) ; it + + )
2006-11-05 00:31:17 +01:00
{
2009-02-27 13:19:25 +01:00
const int symb_id = it - > first ;
2010-09-16 19:18:45 +02:00
const expr_t expression = it - > second ;
2006-12-12 12:54:30 +01:00
2009-02-27 13:19:25 +01:00
SymbolType type = symbol_table . getType ( symb_id ) ;
int tsid = symbol_table . getTypeSpecificID ( symb_id ) + 1 ;
2006-12-12 12:54:30 +01:00
if ( type = = eEndogenous )
2007-03-09 18:27:46 +01:00
output < < " oo_.steady_state " ;
2006-12-12 12:54:30 +01:00
else if ( type = = eExogenous )
2007-03-09 18:27:46 +01:00
output < < " oo_.exo_steady_state " ;
2006-12-12 12:54:30 +01:00
else if ( type = = eExogenousDet )
2007-03-09 18:27:46 +01:00
output < < " oo_.exo_det_steady_state " ;
2009-02-27 13:19:25 +01:00
output < < " ( " < < tsid < < " ) = " ;
2007-03-09 18:27:46 +01:00
expression - > writeOutput ( output ) ;
output < < " ; " < < endl ;
2006-11-05 00:31:17 +01:00
}
}
2010-09-16 19:00:48 +02:00
InitValStatement : : InitValStatement ( const init_values_t & init_values_arg ,
2012-10-31 15:23:02 +01:00
const SymbolTable & symbol_table_arg ,
const bool & all_values_required_arg ) :
InitOrEndValStatement ( init_values_arg , symbol_table_arg , all_values_required_arg )
2006-11-05 00:31:17 +01:00
{
2006-12-12 12:54:30 +01:00
}
2006-11-05 00:31:17 +01:00
2012-10-31 15:23:02 +01:00
void
InitValStatement : : checkPass ( ModFileStructure & mod_file_struct , WarningConsolidation & warnings )
{
set < int > exogs = getUninitializedVariables ( eExogenous ) ;
set < int > endogs = getUninitializedVariables ( eEndogenous ) ;
if ( endogs . size ( ) > 0 )
{
cerr < < " ERROR: You have not set the following endogenous variables in initval: " ;
for ( set < int > : : const_iterator it = endogs . begin ( ) ; it ! = endogs . end ( ) ; it + + )
cerr < < " " < < symbol_table . getName ( * it ) ;
cerr < < endl ;
}
if ( exogs . size ( ) > 0 )
{
cerr < < " ERROR: You have not set the following exogenous variables in initval: " ;
for ( set < int > : : const_iterator it = exogs . begin ( ) ; it ! = exogs . end ( ) ; it + + )
cerr < < " " < < symbol_table . getName ( * it ) ;
cerr < < endl ;
}
if ( endogs . size ( ) > 0 | | exogs . size ( ) > 0 )
exit ( EXIT_FAILURE ) ;
}
2006-12-12 12:54:30 +01:00
void
2006-12-19 00:28:52 +01:00
InitValStatement : : writeOutput ( ostream & output , const string & basename ) const
2006-12-12 12:54:30 +01:00
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
< < " % INITVAL instructions " < < endl
< < " % " < < endl ;
2006-11-05 00:31:17 +01:00
// Writing initval block to set initial values for variables
2008-12-05 16:34:26 +01:00
output < < " options_.initval_file = 0; " < < endl ;
2006-11-28 12:56:02 +01:00
2006-12-12 12:54:30 +01:00
writeInitValues ( output ) ;
2009-09-30 17:10:31 +02:00
}
2006-11-05 00:31:17 +01:00
2009-09-30 17:10:31 +02:00
void
InitValStatement : : writeOutputPostInit ( ostream & output ) const
{
2011-12-28 11:11:06 +01:00
output < < " if M_.exo_nbr > 0; " < < endl
2009-09-30 17:10:31 +02:00
< < " \t oo_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state']; " < < endl
< < " end; " < < endl
< < " if M_.exo_det_nbr > 0; " < < endl
< < " \t oo_.exo_det_simul = [ones(M_.maximum_lag,1)*oo_.exo_det_steady_state']; " < < endl
< < " end; " < < endl ;
2006-11-05 00:31:17 +01:00
}
2010-09-16 19:00:48 +02:00
EndValStatement : : EndValStatement ( const init_values_t & init_values_arg ,
2012-10-31 15:23:02 +01:00
const SymbolTable & symbol_table_arg ,
const bool & all_values_required_arg ) :
InitOrEndValStatement ( init_values_arg , symbol_table_arg , all_values_required_arg )
2006-11-05 00:31:17 +01:00
{
}
2009-10-29 18:16:10 +01:00
void
2012-01-23 16:52:27 +01:00
EndValStatement : : checkPass ( ModFileStructure & mod_file_struct , WarningConsolidation & warnings )
2009-10-29 18:16:10 +01:00
{
2012-10-31 15:23:02 +01:00
set < int > exogs = getUninitializedVariables ( eExogenous ) ;
set < int > endogs = getUninitializedVariables ( eEndogenous ) ;
if ( endogs . size ( ) > 0 )
{
cerr < < " ERROR: You have not set the following endogenous variables in endval: " ;
for ( set < int > : : const_iterator it = endogs . begin ( ) ; it ! = endogs . end ( ) ; it + + )
cerr < < " " < < symbol_table . getName ( * it ) ;
cerr < < endl ;
}
if ( exogs . size ( ) > 0 )
{
cerr < < " ERROR: You have not set the following exogenous variables in endval: " ;
for ( set < int > : : const_iterator it = exogs . begin ( ) ; it ! = exogs . end ( ) ; it + + )
cerr < < " " < < symbol_table . getName ( * it ) ;
cerr < < endl ;
}
if ( endogs . size ( ) > 0 | | exogs . size ( ) > 0 )
exit ( EXIT_FAILURE ) ;
2012-07-30 17:01:20 +02:00
if ( mod_file_struct . shocks_present_but_simul_not_yet )
2009-10-29 18:16:10 +01:00
{
2012-07-30 17:01:20 +02:00
cerr < < " ERROR: Putting a \" shocks \" block before an \" endval \" block is not permitted. Please swap the two blocks. This limitation will be removed in a future release of Dynare. " < < endl ;
2009-10-29 18:16:10 +01:00
exit ( EXIT_FAILURE ) ;
}
}
2006-12-12 12:54:30 +01:00
void
2006-12-19 00:28:52 +01:00
EndValStatement : : writeOutput ( ostream & output , const string & basename ) const
2006-11-05 00:31:17 +01:00
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
< < " % ENDVAL instructions " < < endl
< < " % " < < endl ;
2006-11-05 00:31:17 +01:00
// Writing endval block to set terminal values for variables
2008-06-16 18:33:28 +02:00
output < < " ys0_= oo_.steady_state; " < < endl
2013-05-27 17:31:41 +02:00
< < " ex0_ = oo_.exo_steady_state; " < < endl ;
2006-11-05 00:31:17 +01:00
2006-12-12 12:54:30 +01:00
writeInitValues ( output ) ;
2006-11-05 00:31:17 +01:00
}
2010-09-16 19:00:48 +02:00
HistValStatement : : HistValStatement ( const hist_values_t & hist_values_arg ,
2006-12-12 12:54:30 +01:00
const SymbolTable & symbol_table_arg ) :
hist_values ( hist_values_arg ) ,
symbol_table ( symbol_table_arg )
2006-11-05 00:31:17 +01:00
{
}
2011-02-04 12:38:12 +01:00
void
2012-01-23 16:52:27 +01:00
HistValStatement : : checkPass ( ModFileStructure & mod_file_struct , WarningConsolidation & warnings )
2011-02-04 12:38:12 +01:00
{
mod_file_struct . histval_present = true ;
}
2006-12-12 12:54:30 +01:00
void
2006-12-19 00:28:52 +01:00
HistValStatement : : writeOutput ( ostream & output , const string & basename ) const
2006-11-05 00:31:17 +01:00
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
< < " % HISTVAL instructions " < < endl
2010-09-10 11:43:48 +02:00
< < " % " < < endl
2014-02-17 17:52:37 +01:00
< < " M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_endo_lag); " < < endl ;
2006-11-05 00:31:17 +01:00
2010-09-16 19:00:48 +02:00
for ( hist_values_t : : const_iterator it = hist_values . begin ( ) ;
2009-12-16 18:13:23 +01:00
it ! = hist_values . end ( ) ; it + + )
2006-11-05 00:31:17 +01:00
{
2010-09-10 11:43:48 +02:00
int symb_id = it - > first . first ;
int lag = it - > first . second ;
2010-09-16 19:18:45 +02:00
const expr_t expression = it - > second ;
2006-12-12 12:54:30 +01:00
2009-02-27 13:19:25 +01:00
SymbolType type = symbol_table . getType ( symb_id ) ;
2010-10-20 14:47:03 +02:00
2010-10-20 14:55:28 +02:00
// For a lag greater than 1 on endo, or for any exo, lookup for auxiliary variable
if ( ( type = = eEndogenous & & lag < 0 ) | | type = = eExogenous )
2010-10-20 14:47:03 +02:00
{
try
{
2010-10-20 14:55:28 +02:00
// This function call must remain the 1st statement in this block
2010-10-20 14:47:03 +02:00
symb_id = symbol_table . searchAuxiliaryVars ( symb_id , lag ) ;
lag = 0 ;
2011-01-13 18:18:57 +01:00
type = eEndogenous ;
2010-10-20 14:47:03 +02:00
}
catch ( SymbolTable : : SearchFailedException & e )
{
if ( type = = eEndogenous )
{
cerr < < " HISTVAL: internal error of Dynare, please contact the developers " ;
exit ( EXIT_FAILURE ) ;
}
// We don't fail for exogenous, because they are not replaced by
// auxiliary variables in deterministic mode.
}
}
2009-02-27 13:19:25 +01:00
int tsid = symbol_table . getTypeSpecificID ( symb_id ) + 1 ;
2006-12-12 12:54:30 +01:00
if ( type = = eEndogenous )
2014-02-17 17:52:37 +01:00
output < < " M_.endo_histval( " < < tsid < < " , M_.maximum_endo_lag + " < < lag < < " ) = " ;
2006-12-12 12:54:30 +01:00
else if ( type = = eExogenous )
2009-02-27 13:19:25 +01:00
output < < " oo_.exo_simul( M_.maximum_lag + " < < lag < < " , " < < tsid < < " ) = " ;
2006-12-12 12:54:30 +01:00
else if ( type ! = eExogenousDet )
2009-02-27 13:19:25 +01:00
output < < " oo_.exo_det_simul( M_.maximum_lag + " < < lag < < " , " < < tsid < < " ) = " ;
2007-03-09 18:27:46 +01:00
expression - > writeOutput ( output ) ;
output < < " ; " < < endl ;
2006-11-05 00:31:17 +01:00
}
}
2007-11-26 17:31:48 +01:00
2008-04-14 11:17:15 +02:00
InitvalFileStatement : : InitvalFileStatement ( const string & filename_arg ) :
filename ( filename_arg )
2008-04-14 09:22:10 +02:00
{
}
2008-04-14 11:17:15 +02:00
void
InitvalFileStatement : : writeOutput ( ostream & output , const string & basename ) const
2008-04-14 09:22:10 +02:00
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
< < " % INITVAL_FILE statement " < < endl
< < " % " < < endl
< < " options_.initval_file = 1; " < < endl
< < " initvalf(' " < < filename < < " '); " < < endl ;
2008-04-14 09:22:10 +02:00
}
2010-09-16 19:00:48 +02:00
HomotopyStatement : : HomotopyStatement ( const homotopy_values_t & homotopy_values_arg ,
2008-03-31 18:19:16 +02:00
const SymbolTable & symbol_table_arg ) :
2007-11-26 17:31:48 +01:00
homotopy_values ( homotopy_values_arg ) ,
symbol_table ( symbol_table_arg )
{
}
void
HomotopyStatement : : writeOutput ( ostream & output , const string & basename ) const
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
< < " % HOMOTOPY_SETUP instructions " < < endl
< < " % " < < endl
2008-03-31 18:19:16 +02:00
< < " options_.homotopy_values = []; " < < endl ;
2007-11-26 17:31:48 +01:00
2010-09-16 19:00:48 +02:00
for ( homotopy_values_t : : const_iterator it = homotopy_values . begin ( ) ;
2009-12-16 18:13:23 +01:00
it ! = homotopy_values . end ( ) ; it + + )
2007-11-26 17:31:48 +01:00
{
2009-02-27 13:19:25 +01:00
const int & symb_id = it - > first ;
2010-09-16 19:18:45 +02:00
const expr_t expression1 = it - > second . first ;
const expr_t expression2 = it - > second . second ;
2007-11-26 17:31:48 +01:00
2009-02-27 13:19:25 +01:00
const SymbolType type = symbol_table . getType ( symb_id ) ;
const int tsid = symbol_table . getTypeSpecificID ( symb_id ) + 1 ;
2007-11-26 17:31:48 +01:00
2009-02-27 13:19:25 +01:00
output < < " options_.homotopy_values = vertcat(options_.homotopy_values, [ " < < type < < " , " < < tsid < < " , " ;
2008-04-03 18:22:52 +02:00
if ( expression1 ! = NULL )
expression1 - > writeOutput ( output ) ;
else
output < < " NaN " ;
2007-11-26 17:31:48 +01:00
output < < " , " ;
expression2 - > writeOutput ( output ) ;
2008-03-31 18:19:16 +02:00
output < < " ]); " < < endl ;
2007-11-26 17:31:48 +01:00
}
}
2009-02-27 13:19:25 +01:00
SaveParamsAndSteadyStateStatement : : SaveParamsAndSteadyStateStatement ( const string & filename_arg ) :
filename ( filename_arg )
{
}
void
SaveParamsAndSteadyStateStatement : : writeOutput ( ostream & output , const string & basename ) const
{
output < < " save_params_and_steady_state(' " < < filename < < " '); " < < endl ;
}
2009-03-24 17:29:58 +01:00
LoadParamsAndSteadyStateStatement : : LoadParamsAndSteadyStateStatement ( const string & filename ,
2012-01-23 16:52:27 +01:00
const SymbolTable & symbol_table_arg ,
WarningConsolidation & warnings ) :
2009-03-24 17:29:58 +01:00
symbol_table ( symbol_table_arg )
2009-02-27 13:19:25 +01:00
{
2009-04-20 17:54:19 +02:00
cout < < " Reading " < < filename < < " . " < < endl ;
2009-02-27 13:19:25 +01:00
2009-03-24 17:29:58 +01:00
ifstream f ;
f . open ( filename . c_str ( ) , ios : : in ) ;
2009-06-10 16:45:41 +02:00
if ( f . fail ( ) )
2009-03-24 17:29:58 +01:00
{
cerr < < " ERROR: Can't open " < < filename < < endl ;
exit ( EXIT_FAILURE ) ;
}
2009-02-27 13:19:25 +01:00
2009-12-16 18:13:23 +01:00
while ( true )
2009-02-27 13:19:25 +01:00
{
2009-03-24 17:29:58 +01:00
string symb_name , value ;
f > > symb_name > > value ;
if ( f . eof ( ) )
break ;
2009-02-27 13:19:25 +01:00
try
{
2009-03-24 17:29:58 +01:00
int symb_id = symbol_table . getID ( symb_name ) ;
content [ symb_id ] = value ;
2009-02-27 13:19:25 +01:00
}
2009-12-16 18:13:23 +01:00
catch ( SymbolTable : : UnknownSymbolNameException & e )
2009-02-27 13:19:25 +01:00
{
2012-01-23 16:52:27 +01:00
warnings < < " WARNING: Unknown symbol " < < symb_name < < " in " < < filename < < endl ;
2009-03-24 17:29:58 +01:00
}
}
2012-01-18 14:30:43 +01:00
f . close ( ) ;
2009-03-24 17:29:58 +01:00
}
void
LoadParamsAndSteadyStateStatement : : writeOutput ( ostream & output , const string & basename ) const
{
2009-12-16 18:13:23 +01:00
for ( map < int , string > : : const_iterator it = content . begin ( ) ;
it ! = content . end ( ) ; it + + )
2009-03-24 17:29:58 +01:00
{
2009-12-16 18:13:23 +01:00
switch ( symbol_table . getType ( it - > first ) )
2009-03-24 17:29:58 +01:00
{
case eParameter :
output < < " M_.params " ;
break ;
case eEndogenous :
output < < " oo_.steady_state " ;
break ;
case eExogenous :
output < < " oo_.exo_steady_state " ;
break ;
case eExogenousDet :
output < < " oo_.exo_det_steady_state " ;
break ;
default :
cerr < < " ERROR: Unsupported variable type for " < < symbol_table . getName ( it - > first ) < < " in load_params_and_steady_state " < < endl ;
exit ( EXIT_FAILURE ) ;
2009-02-27 13:19:25 +01:00
}
2009-03-24 17:29:58 +01:00
int tsid = symbol_table . getTypeSpecificID ( it - > first ) + 1 ;
output < < " ( " < < tsid < < " ) = " < < it - > second < < " ; " < < endl ;
2009-02-27 13:19:25 +01:00
}
}
2009-03-24 17:29:58 +01:00
void
2010-09-16 19:00:48 +02:00
LoadParamsAndSteadyStateStatement : : fillEvalContext ( eval_context_t & eval_context ) const
2009-03-24 17:29:58 +01:00
{
2009-12-16 18:13:23 +01:00
for ( map < int , string > : : const_iterator it = content . begin ( ) ;
it ! = content . end ( ) ; it + + )
2009-03-24 17:29:58 +01:00
eval_context [ it - > first ] = atof ( it - > second . c_str ( ) ) ;
}