2008-01-11 14:42:14 +01:00
/*
2009-02-27 13:19:25 +01:00
* Copyright ( C ) 2003 - 2009 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>
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 ,
2007-03-09 18:27:46 +01:00
const NodeID 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
{
}
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 ;
2009-02-27 13:19:25 +01:00
output < < symbol_table . getName ( symb_id ) < < " = M_.params( " < < id < < " ); \n " ;
2008-12-19 11:24:31 +01:00
}
2009-02-27 13:19:25 +01:00
void
InitParamStatement : : fillEvalContext ( eval_context_type & 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
}
2006-12-12 12:54:30 +01:00
InitOrEndValStatement : : InitOrEndValStatement ( const init_values_type & init_values_arg ,
const SymbolTable & symbol_table_arg ) :
init_values ( init_values_arg ) ,
symbol_table ( symbol_table_arg )
2006-11-05 00:31:17 +01:00
{
}
2009-02-27 13:19:25 +01:00
void
InitOrEndValStatement : : fillEvalContext ( eval_context_type & eval_context ) const
{
2009-12-16 18:13:23 +01:00
for ( init_values_type : : const_iterator it = init_values . begin ( ) ;
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
}
}
}
2006-12-12 12:54:30 +01:00
void
InitOrEndValStatement : : writeInitValues ( ostream & output ) const
2006-11-05 00:31:17 +01:00
{
2009-12-16 18:13:23 +01:00
for ( init_values_type : : const_iterator it = init_values . begin ( ) ;
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 ;
2007-03-09 18:27:46 +01:00
const NodeID 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
}
}
2006-12-12 12:54:30 +01:00
InitValStatement : : InitValStatement ( const init_values_type & init_values_arg ,
2006-12-18 12:29:10 +01:00
const SymbolTable & symbol_table_arg ) :
InitOrEndValStatement ( init_values_arg , symbol_table_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
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
{
output < < " oo_.endo_simul=[oo_.steady_state*ones(1,M_.maximum_lag)]; " < < endl
< < " if M_.exo_nbr > 0; " < < endl
< < " \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
}
2006-12-12 12:54:30 +01:00
EndValStatement : : EndValStatement ( const init_values_type & init_values_arg ,
const SymbolTable & symbol_table_arg ) :
InitOrEndValStatement ( init_values_arg , symbol_table_arg )
2006-11-05 00:31:17 +01:00
{
}
2009-10-29 18:16:10 +01:00
void
EndValStatement : : checkPass ( ModFileStructure & mod_file_struct )
{
if ( mod_file_struct . shocks_present )
{
cerr < < " ERROR: Putting a \" shocks \" block before an \" endval \" block is not permitted. Please swap the two blocks. This limitation will be removed in the next major release of Dynare. " < < endl ;
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
2008-12-05 16:34:26 +01: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
}
2006-12-12 12:54:30 +01:00
HistValStatement : : HistValStatement ( const hist_values_type & hist_values_arg ,
const SymbolTable & symbol_table_arg ) :
hist_values ( hist_values_arg ) ,
symbol_table ( symbol_table_arg )
2006-11-05 00:31:17 +01:00
{
}
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
< < " % " < < endl ;
2006-11-05 00:31:17 +01:00
2009-12-16 18:13:23 +01:00
for ( hist_values_type : : const_iterator it = hist_values . begin ( ) ;
it ! = hist_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 . first ;
2006-12-12 12:54:30 +01:00
const int & lag = it - > first . second ;
2007-03-09 18:27:46 +01:00
const NodeID 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 )
2009-02-27 13:19:25 +01:00
output < < " oo_.endo_simul( " < < tsid < < " , M_.maximum_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
}
2007-11-26 17:31:48 +01:00
HomotopyStatement : : HomotopyStatement ( const homotopy_values_type & 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
2009-12-16 18:13:23 +01:00
for ( homotopy_values_type : : const_iterator it = homotopy_values . begin ( ) ;
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 ;
2007-11-26 17:31:48 +01:00
const NodeID expression1 = it - > second . first ;
const NodeID expression2 = it - > second . second ;
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 ,
2009-02-27 13:19:25 +01:00
const SymbolTable & symbol_table_arg ) :
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
{
2009-03-24 17:29:58 +01:00
cerr < < " WARNING: Unknown symbol " < < symb_name < < " in " < < filename < < endl ;
}
}
}
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
LoadParamsAndSteadyStateStatement : : fillEvalContext ( eval_context_type & eval_context ) 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
eval_context [ it - > first ] = atof ( it - > second . c_str ( ) ) ;
}