2008-01-11 14:42:14 +01:00
/*
2011-02-04 17:27:33 +01:00
* Copyright ( C ) 2006 - 2011 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/>.
*/
2008-06-28 13:20:45 +02:00
# include <cstdlib>
2007-01-09 20:00:05 +01:00
# include <iostream>
# include <fstream>
2008-12-19 11:24:31 +01:00
# include <typeinfo>
2011-03-21 18:40:57 +01:00
# include <cassert>
2006-11-28 12:56:02 +01:00
# include "ModFile.hh"
2010-10-25 18:20:58 +02:00
# include "ConfigFile.hh"
2011-03-21 18:40:57 +01:00
# include "ComputingTasks.hh"
2006-11-28 12:56:02 +01:00
2010-02-22 17:33:38 +01:00
ModFile : : ModFile ( ) : expressions_tree ( symbol_table , num_constants , external_functions_table ) ,
dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
2010-10-15 19:05:16 +02:00
trend_dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
2011-03-21 18:40:57 +01:00
ramsey_FOC_equations_dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
2010-04-23 18:39:07 +02:00
static_model ( symbol_table , num_constants , external_functions_table ) ,
2010-04-27 17:04:52 +02:00
steady_state_model ( symbol_table , num_constants , external_functions_table , static_model ) ,
2009-09-02 15:36:56 +02:00
linear ( false ) , block ( false ) , byte_code ( false ) ,
2011-03-21 18:40:57 +01:00
use_dll ( false ) , no_static ( false ) , nonstationary_variables ( false ) ,
ramsey_policy_orig_eqn_nbr ( 0 )
2006-11-28 12:56:02 +01:00
{
}
2006-12-12 12:54:30 +01:00
ModFile : : ~ ModFile ( )
{
2009-12-16 18:13:23 +01:00
for ( vector < Statement * > : : iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
2006-12-12 12:54:30 +01:00
delete ( * it ) ;
}
2008-12-19 11:24:31 +01:00
void
2009-11-06 19:31:03 +01:00
ModFile : : evalAllExpressions ( bool warn_uninit )
2008-12-19 11:24:31 +01:00
{
2009-02-27 13:19:25 +01:00
cout < < " Evaluating expressions... " ;
2008-12-31 20:29:17 +01:00
2009-02-27 13:19:25 +01:00
// Loop over all statements, and fill global eval context if relevant
2009-12-16 18:13:23 +01:00
for ( vector < Statement * > : : const_iterator it = statements . begin ( ) ; it ! = statements . end ( ) ; it + + )
2008-12-19 11:24:31 +01:00
{
2009-02-27 13:19:25 +01:00
InitParamStatement * ips = dynamic_cast < InitParamStatement * > ( * it ) ;
if ( ips )
ips - > fillEvalContext ( global_eval_context ) ;
InitOrEndValStatement * ies = dynamic_cast < InitOrEndValStatement * > ( * it ) ;
if ( ies )
ies - > fillEvalContext ( global_eval_context ) ;
LoadParamsAndSteadyStateStatement * lpass = dynamic_cast < LoadParamsAndSteadyStateStatement * > ( * it ) ;
if ( lpass )
lpass - > fillEvalContext ( global_eval_context ) ;
2008-12-19 11:24:31 +01:00
}
2009-02-27 13:19:25 +01:00
// Evaluate model local variables
2009-04-14 16:39:53 +02:00
dynamic_model . fillEvalContext ( global_eval_context ) ;
2009-02-27 13:19:25 +01:00
cout < < " done " < < endl ;
// Check if some symbols are not initialized, and give them a zero value then
2009-12-16 18:13:23 +01:00
for ( int id = 0 ; id < = symbol_table . maxID ( ) ; id + + )
2008-12-19 11:24:31 +01:00
{
2009-02-27 13:19:25 +01:00
SymbolType type = symbol_table . getType ( id ) ;
if ( ( type = = eEndogenous | | type = = eExogenous | | type = = eExogenousDet
2009-12-16 18:13:23 +01:00
| | type = = eParameter | | type = = eModelLocalVariable )
2009-02-27 13:19:25 +01:00
& & global_eval_context . find ( id ) = = global_eval_context . end ( ) )
2008-12-19 11:24:31 +01:00
{
2009-11-06 19:31:03 +01:00
if ( warn_uninit )
cerr < < " WARNING: can't find a numeric initial value for " < < symbol_table . getName ( id ) < < " , using zero " < < endl ;
2009-02-27 13:19:25 +01:00
global_eval_context [ id ] = 0 ;
2008-12-19 11:24:31 +01:00
}
}
}
2006-12-12 12:54:30 +01:00
void
ModFile : : addStatement ( Statement * st )
{
statements . push_back ( st ) ;
}
2010-06-24 15:07:15 +02:00
void
ModFile : : addStatementAtFront ( Statement * st )
{
statements . insert ( statements . begin ( ) , st ) ;
}
2006-12-15 12:44:58 +01:00
void
ModFile : : checkPass ( )
{
2009-12-16 18:13:23 +01:00
for ( vector < Statement * > : : iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
2006-12-15 12:44:58 +01:00
( * it ) - > checkPass ( mod_file_struct ) ;
2010-05-31 17:43:17 +02:00
// Check the steady state block
steady_state_model . checkPass ( mod_file_struct . ramsey_policy_present ) ;
2008-04-24 12:55:26 +02:00
// If order option has not been set, default to 2
if ( ! mod_file_struct . order_option )
mod_file_struct . order_option = 2 ;
2008-09-29 12:16:13 +02:00
bool stochastic_statement_present = mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present
| | mod_file_struct . osr_present
2011-03-17 15:15:13 +01:00
| | mod_file_struct . ramsey_policy_present
| | mod_file_struct . discretionary_policy_present ;
2008-09-29 12:16:13 +02:00
2007-07-16 18:47:09 +02:00
// Allow empty model only when doing a standalone BVAR estimation
2009-04-14 16:39:53 +02:00
if ( dynamic_model . equation_number ( ) = = 0
2007-07-16 18:47:09 +02:00
& & ( mod_file_struct . check_present
| | mod_file_struct . simul_present
2008-09-29 12:16:13 +02:00
| | stochastic_statement_present ) )
{
cerr < < " ERROR: At least one model equation must be declared! " < < endl ;
2008-10-29 16:33:16 +01:00
exit ( EXIT_FAILURE ) ;
2008-09-29 12:16:13 +02:00
}
2011-03-17 15:15:13 +01:00
if ( ( ( mod_file_struct . ramsey_policy_present | | mod_file_struct . discretionary_policy_present )
& & ! mod_file_struct . planner_objective_present )
| | ( ! ( mod_file_struct . ramsey_policy_present | | mod_file_struct . discretionary_policy_present )
& & mod_file_struct . planner_objective_present ) )
2011-03-03 16:03:21 +01:00
{
2011-03-17 15:15:13 +01:00
cerr < < " ERROR: A planner_objective statement must be used with a ramsey_policy or a discretionary_policy statement and vice versa. " < < endl ;
2011-03-03 16:03:21 +01:00
exit ( EXIT_FAILURE ) ;
}
2011-03-03 16:30:53 +01:00
if ( ( mod_file_struct . osr_present & & ( ! mod_file_struct . osr_params_present | | ! mod_file_struct . optim_weights_present ) )
| | ( ( ! mod_file_struct . osr_present | | ! mod_file_struct . osr_params_present ) & & mod_file_struct . optim_weights_present )
| | ( ( ! mod_file_struct . osr_present | | ! mod_file_struct . optim_weights_present ) & & mod_file_struct . osr_params_present ) )
{
cerr < < " ERROR: The osr statement must be used with osr_params and optim_weights. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2009-04-29 13:35:44 +02:00
if ( mod_file_struct . simul_present & & stochastic_statement_present )
2007-07-16 18:47:09 +02:00
{
2011-03-17 15:15:13 +01:00
cerr < < " ERROR: A .mod file cannot contain both a simul command and one of {stoch_simul, estimation, osr, ramsey_policy, discretionary_policy} " < < endl ;
2008-10-29 16:33:16 +01:00
exit ( EXIT_FAILURE ) ;
2007-07-16 18:47:09 +02:00
}
2010-12-17 18:34:23 +01:00
if ( mod_file_struct . k_order_solver & & byte_code )
2009-12-09 12:50:58 +01:00
{
2010-12-17 18:34:23 +01:00
cerr < < " ERROR: 'k_order_solver' (which is implicit if order >= 3), is not yet compatible with 'bytecode'. " < < endl ;
2009-12-09 12:50:58 +01:00
exit ( EXIT_FAILURE ) ;
}
2009-09-02 15:36:56 +02:00
if ( use_dll & & ( block | | byte_code ) )
{
cerr < < " ERROR: In 'model' block, 'use_dll' option is not compatible with 'block' or 'bytecode' " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2011-10-14 14:35:32 +02:00
if ( block | | byte_code )
if ( dynamic_model . isModelLocalVariableUsed ( ) )
{
cerr < < " ERROR: In 'model' block, 'block' or 'bytecode' options are not yet compatible with pound expressions " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2011-02-04 16:25:38 +01:00
if ( ( stochastic_statement_present | | mod_file_struct . check_present | | mod_file_struct . steady_present ) & & no_static )
2010-01-08 12:06:25 +01:00
{
2011-03-17 15:15:13 +01:00
cerr < < " ERROR: no_static option is incompatible with stoch_simul, estimation, osr, ramsey_policy, discretionary_policy, steady and check commands " < < endl ;
2010-01-08 12:06:25 +01:00
exit ( EXIT_FAILURE ) ;
}
2010-02-22 17:33:38 +01:00
2010-06-11 21:19:30 +02:00
if ( mod_file_struct . dsge_var_estimated )
if ( ! mod_file_struct . dsge_prior_weight_in_estimated_params )
{
cerr < < " ERROR: When estimating a DSGE-VAR model and estimating the weight of the prior, dsge_prior_weight must "
< < " be referenced in the estimated_params block. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2010-06-21 18:40:36 +02:00
if ( symbol_table . exists ( " dsge_prior_weight " ) )
{
if ( symbol_table . getType ( " dsge_prior_weight " ) ! = eParameter )
{
cerr < < " ERROR: dsge_prior_weight may only be used as a parameter. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
else
cout < < " WARNING: When estimating a DSGE-Var, declaring dsge_prior_weight as a parameter is deprecated. "
< < " The preferred method is to do this via the dsge_var option in the estimation statement. " < < endl ;
if ( mod_file_struct . dsge_var_estimated | | ! mod_file_struct . dsge_var_calibrated . empty ( ) )
{
cerr < < " ERROR: dsge_prior_weight can either be declared as a parameter (deprecated) or via the dsge_var option "
< < " to the estimation statement (preferred), but not both. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( ! mod_file_struct . dsge_prior_weight_initialized & & ! mod_file_struct . dsge_prior_weight_in_estimated_params )
{
cerr < < " ERROR: If dsge_prior_weight is declared as a parameter, it must either be initialized or placed in the "
< < " estimated_params block. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( mod_file_struct . dsge_prior_weight_initialized & & mod_file_struct . dsge_prior_weight_in_estimated_params )
{
cerr < < " ERROR: dsge_prior_weight cannot be both initalized and estimated. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
2010-06-11 21:19:30 +02:00
if ( mod_file_struct . dsge_prior_weight_in_estimated_params )
2010-06-21 18:40:36 +02:00
if ( ! mod_file_struct . dsge_var_estimated & & ! mod_file_struct . dsge_var_calibrated . empty ( ) )
2010-06-11 21:19:30 +02:00
{
2010-06-21 18:40:36 +02:00
cerr < < " ERROR: If dsge_prior_weight is in the estimated_params block, the prior weight cannot be calibrated "
2010-06-11 21:19:30 +02:00
< < " via the dsge_var option in the estimation statement. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2010-06-21 18:40:36 +02:00
else if ( ! mod_file_struct . dsge_var_estimated & & ! symbol_table . exists ( " dsge_prior_weight " ) )
{
cerr < < " ERROR: If dsge_prior_weight is in the estimated_params block, it must either be declared as a parameter "
< < " (deprecated) or the dsge_var option must be passed to the estimation statement (preferred). " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2009-09-30 17:10:31 +02:00
}
void
ModFile : : transformPass ( )
{
2009-11-09 12:03:18 +01:00
if ( symbol_table . predeterminedNbr ( ) > 0 )
dynamic_model . transformPredeterminedVariables ( ) ;
2009-11-07 19:37:11 +01:00
2011-03-21 18:36:38 +01:00
// Create auxiliary vars for Expectation operator
dynamic_model . substituteExpectation ( mod_file_struct . partial_information ) ;
2010-10-15 19:05:16 +02:00
if ( nonstationary_variables )
{
dynamic_model . detrendEquations ( ) ;
dynamic_model . cloneDynamic ( trend_dynamic_model ) ;
dynamic_model . removeTrendVariableFromEquations ( ) ;
}
2011-03-21 18:40:57 +01:00
if ( mod_file_struct . ramsey_policy_present )
{
StaticModel * planner_objective = NULL ;
for ( vector < Statement * > : : iterator it = statements . begin ( ) ; it ! = statements . end ( ) ; it + + )
{
PlannerObjectiveStatement * pos = dynamic_cast < PlannerObjectiveStatement * > ( * it ) ;
if ( pos ! = NULL )
planner_objective = pos - > getPlannerObjective ( ) ;
}
2011-03-29 18:18:32 +02:00
assert ( planner_objective ! = NULL ) ;
2011-03-21 18:40:57 +01:00
ramsey_policy_orig_eqn_nbr = dynamic_model . equation_number ( ) ;
/*
clone the model then clone the new equations back to the original because
we have to call computeDerivIDs ( in computeRamseyPolicyFOCs and computingPass )
*/
dynamic_model . cloneDynamic ( ramsey_FOC_equations_dynamic_model ) ;
2011-03-29 18:18:32 +02:00
ramsey_FOC_equations_dynamic_model . computeRamseyPolicyFOCs ( * planner_objective ) ;
2011-03-21 18:40:57 +01:00
ramsey_FOC_equations_dynamic_model . replaceMyEquations ( dynamic_model ) ;
}
2009-09-30 17:10:31 +02:00
if ( mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present
| | mod_file_struct . osr_present
2011-03-17 15:15:13 +01:00
| | mod_file_struct . ramsey_policy_present
| | mod_file_struct . discretionary_policy_present )
2009-09-30 17:10:31 +02:00
{
2010-08-18 13:45:07 +02:00
// In stochastic models, create auxiliary vars for leads and lags greater than 2, on both endos and exos
dynamic_model . substituteEndoLeadGreaterThanTwo ( false ) ;
dynamic_model . substituteExoLead ( false ) ;
dynamic_model . substituteEndoLagGreaterThanTwo ( false ) ;
dynamic_model . substituteExoLag ( false ) ;
}
else
{
// In deterministic models, create auxiliary vars for leads and lags endogenous greater than 2, only on endos (useless on exos)
dynamic_model . substituteEndoLeadGreaterThanTwo ( true ) ;
dynamic_model . substituteEndoLagGreaterThanTwo ( true ) ;
2009-09-30 17:10:31 +02:00
}
2009-09-02 15:36:56 +02:00
2010-06-21 18:40:36 +02:00
if ( mod_file_struct . dsge_var_estimated | | ! mod_file_struct . dsge_var_calibrated . empty ( ) )
2010-06-11 21:19:30 +02:00
try
{
2010-06-21 18:40:36 +02:00
int sid = symbol_table . addSymbol ( " dsge_prior_weight " , eParameter ) ;
if ( ! mod_file_struct . dsge_var_calibrated . empty ( ) )
2010-06-24 15:07:15 +02:00
addStatementAtFront ( new InitParamStatement ( sid ,
2010-11-25 13:45:35 +01:00
expressions_tree . AddNonNegativeConstant ( mod_file_struct . dsge_var_calibrated ) ,
2010-06-24 15:07:15 +02:00
symbol_table ) ) ;
2010-06-11 21:19:30 +02:00
}
catch ( SymbolTable : : AlreadyDeclaredException & e )
{
cerr < < " ERROR: dsge_prior_weight should not be declared as a model variable / parameter "
< < " when the dsge_var option is passed to the estimation statement. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2009-02-27 13:19:25 +01:00
// Freeze the symbol table
symbol_table . freeze ( ) ;
2008-10-17 14:52:08 +02:00
/*
Enforce the same number of equations and endogenous , except in two cases :
- ramsey_policy is used
- a BVAR command is used and there is no equation ( standalone BVAR estimation )
*/
2011-06-18 06:53:00 +02:00
if ( ! ( mod_file_struct . ramsey_policy_present | | mod_file_struct . discretionary_policy_present )
2009-11-26 18:25:04 +01:00
& & ! ( mod_file_struct . bvar_present & & dynamic_model . equation_number ( ) = = 0 )
2009-04-14 16:39:53 +02:00
& & ( dynamic_model . equation_number ( ) ! = symbol_table . endo_nbr ( ) ) )
2006-12-15 12:44:58 +01:00
{
2009-04-14 16:39:53 +02:00
cerr < < " ERROR: There are " < < dynamic_model . equation_number ( ) < < " equations but " < < symbol_table . endo_nbr ( ) < < " endogenous variables! " < < endl ;
2008-10-29 16:33:16 +01:00
exit ( EXIT_FAILURE ) ;
2006-12-15 12:44:58 +01:00
}
2011-03-21 18:40:57 +01:00
2011-03-18 12:47:06 +01:00
if ( symbol_table . exo_det_nbr ( ) > 0 & & mod_file_struct . simul_present )
{
cerr < < " ERROR: A .mod file cannot contain both a simul command and varexo_det declaration (all exogenous variables are deterministic in this case) " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2011-03-21 18:40:57 +01:00
if ( ! mod_file_struct . ramsey_policy_present )
cout < < " Found " < < dynamic_model . equation_number ( ) < < " equation(s). " < < endl ;
else
{
cout < < " Found " < < ramsey_policy_orig_eqn_nbr < < " equation(s). " < < endl ;
cout < < " Found " < < dynamic_model . equation_number ( ) < < " FOC equation(s) for Ramsey Problem. " < < endl ;
}
2010-06-11 21:19:30 +02:00
2010-06-21 18:40:36 +02:00
if ( symbol_table . exists ( " dsge_prior_weight " ) )
2010-06-11 21:19:30 +02:00
if ( mod_file_struct . bayesian_irf_present )
{
if ( symbol_table . exo_nbr ( ) ! = symbol_table . observedVariablesNbr ( ) )
{
2010-06-21 18:40:36 +02:00
cerr < < " ERROR: When estimating a DSGE-Var and the bayesian_irf option is passed to the estimation "
< < " statement, the number of shocks must equal the number of observed variables. " < < endl ;
2010-06-11 21:19:30 +02:00
exit ( EXIT_FAILURE ) ;
}
}
else
if ( symbol_table . exo_nbr ( ) < symbol_table . observedVariablesNbr ( ) )
{
2010-06-21 18:40:36 +02:00
cerr < < " ERROR: When estimating a DSGE-Var, the number of shocks must be "
< < " greater than or equal to the number of observed variables. " < < endl ;
2010-06-11 21:19:30 +02:00
exit ( EXIT_FAILURE ) ;
}
2006-12-15 18:31:31 +01:00
}
2006-12-15 12:44:58 +01:00
2006-12-15 18:31:31 +01:00
void
2008-10-29 16:10:51 +01:00
ModFile : : computingPass ( bool no_tmp_terms )
2006-12-15 18:31:31 +01:00
{
2007-07-16 18:47:09 +02:00
// Mod file may have no equation (for example in a standalone BVAR estimation)
2009-04-14 16:39:53 +02:00
if ( dynamic_model . equation_number ( ) > 0 )
2006-12-15 12:44:58 +01:00
{
2010-10-15 19:05:16 +02:00
if ( nonstationary_variables )
trend_dynamic_model . runTrendTest ( global_eval_context ) ;
2009-04-14 16:39:53 +02:00
// Compute static model and its derivatives
2009-12-16 14:21:31 +01:00
dynamic_model . toStatic ( static_model ) ;
2011-02-04 16:25:38 +01:00
if ( ! no_static )
2010-01-22 11:03:29 +01:00
{
2011-06-18 17:53:50 +02:00
if ( mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_policy_present | | mod_file_struct . identification_present )
static_model . set_cutoff_to_zero ( ) ;
2011-08-18 17:23:09 +02:00
if ( mod_file_struct . identification_present )
static_model . computingPass ( global_eval_context , no_tmp_terms , true , block , byte_code ) ;
else
static_model . computingPass ( global_eval_context , no_tmp_terms , false , block , byte_code ) ;
2010-01-22 11:03:29 +01:00
}
2009-04-14 16:39:53 +02:00
// Set things to compute for dynamic model
2011-05-03 11:28:43 +02:00
if ( mod_file_struct . simul_present | | mod_file_struct . check_present
| | mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_policy_present | | mod_file_struct . identification_present )
2007-02-08 12:56:46 +01:00
{
2009-09-02 18:44:15 +02:00
if ( mod_file_struct . simul_present )
2010-11-25 16:04:56 +01:00
dynamic_model . computingPass ( true , false , false , false , global_eval_context , no_tmp_terms , block , use_dll , byte_code ) ;
2009-09-02 18:44:15 +02:00
else
2007-07-16 18:47:09 +02:00
{
2011-06-18 17:53:50 +02:00
if ( mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_policy_present | | mod_file_struct . identification_present )
dynamic_model . set_cutoff_to_zero ( ) ;
2009-09-02 18:44:15 +02:00
if ( mod_file_struct . order_option < 1 | | mod_file_struct . order_option > 3 )
{
cerr < < " ERROR: Incorrect order option... " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2009-10-01 10:03:40 +02:00
bool hessian = mod_file_struct . order_option > = 2 | | mod_file_struct . identification_present ;
2009-09-02 18:44:15 +02:00
bool thirdDerivatives = mod_file_struct . order_option = = 3 ;
bool paramsDerivatives = mod_file_struct . identification_present ;
2011-01-31 09:58:13 +01:00
dynamic_model . computingPass ( true , hessian , thirdDerivatives , paramsDerivatives , global_eval_context , no_tmp_terms , block , use_dll , byte_code ) ;
2007-07-16 18:47:09 +02:00
}
2007-02-08 12:56:46 +01:00
}
2011-05-03 11:28:43 +02:00
else // No computing task requested, compute derivatives up to 2nd order by default
dynamic_model . computingPass ( true , true , false , false , global_eval_context , no_tmp_terms , block , use_dll , byte_code ) ;
2007-07-16 18:47:09 +02:00
}
2009-04-20 12:48:54 +02:00
2009-12-16 18:13:23 +01:00
for ( vector < Statement * > : : iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
2006-12-19 00:28:52 +01:00
( * it ) - > computingPass ( ) ;
2006-12-15 12:44:58 +01:00
}
2006-12-12 12:54:30 +01:00
void
2010-10-28 11:19:11 +02:00
ModFile : : writeOutputFiles ( const string & basename , bool clear_all , bool console , const ConfigFile & config_file
2009-12-11 15:46:21 +01:00
# if defined(_WIN32) || defined(__CYGWIN32__)
2009-12-16 18:13:23 +01:00
, bool cygwin , bool msvc
2009-12-11 15:46:21 +01:00
# endif
2009-12-16 18:13:23 +01:00
) const
2006-12-12 12:54:30 +01:00
{
ofstream mOutputFile ;
2009-10-06 10:56:02 +02:00
2006-12-12 12:54:30 +01:00
if ( basename . size ( ) )
{
string fname ( basename ) ;
2008-06-16 18:33:28 +02:00
fname + = " .m " ;
2006-12-12 12:54:30 +01:00
mOutputFile . open ( fname . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! mOutputFile . is_open ( ) )
{
2008-09-29 12:16:13 +02:00
cerr < < " ERROR: Can't open file " < < fname
2006-12-12 12:54:30 +01:00
< < " for writing " < < endl ;
2008-10-29 16:33:16 +01:00
exit ( EXIT_FAILURE ) ;
2006-12-12 12:54:30 +01:00
}
}
else
{
2008-09-29 12:16:13 +02:00
cerr < < " ERROR: Missing file name " < < endl ;
2008-10-29 16:33:16 +01:00
exit ( EXIT_FAILURE ) ;
2006-12-12 12:54:30 +01:00
}
2008-06-16 18:33:28 +02:00
mOutputFile < < " % " < < endl
< < " % Status : main Dynare file " < < endl
< < " % " < < endl
< < " % Warning : this file is generated automatically by Dynare " < < endl
< < " % from model file (.mod) " < < endl < < endl ;
2006-12-12 12:54:30 +01:00
if ( clear_all )
mOutputFile < < " clear all " < < endl ;
2009-04-30 15:05:21 +02:00
mOutputFile < < " tic; " < < endl
2009-12-01 17:30:56 +01:00
< < " global M_ oo_ options_ ys0_ ex0_ " < < endl
2009-04-30 15:05:21 +02:00
< < " options_ = []; " < < endl
< < " M_.fname = ' " < < basename < < " '; " < < endl
< < " % " < < endl
< < " % Some global variables initialization " < < endl
< < " % " < < endl
< < " global_initialization; " < < endl
< < " diary off; " < < endl
< < " logname_ = ' " < < basename < < " .log'; " < < endl
2010-03-29 12:44:20 +02:00
< < " if exist(logname_, 'file') " < < endl
< < " delete(logname_) " < < endl
< < " end " < < endl
< < " diary(logname_) " < < endl ;
2009-04-30 15:05:21 +02:00
2010-10-28 11:19:11 +02:00
if ( console )
mOutputFile < < " options_.console_mode = 1; " < < endl ;
2008-12-24 16:49:01 +01:00
cout < < " Processing outputs ... " ;
2007-07-16 18:47:09 +02:00
2006-12-12 12:54:30 +01:00
symbol_table . writeOutput ( mOutputFile ) ;
2010-04-14 15:03:41 +02:00
// Initialize M_.Sigma_e and M_.H
mOutputFile < < " M_.Sigma_e = zeros( " < < symbol_table . exo_nbr ( ) < < " , "
< < symbol_table . exo_nbr ( ) < < " ); " < < endl ;
if ( mod_file_struct . calibrated_measurement_errors )
mOutputFile < < " M_.H = zeros( " < < symbol_table . observedVariablesNbr ( ) < < " , "
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl ;
else
mOutputFile < < " M_.H = 0; " < < endl ;
2006-12-12 12:54:30 +01:00
if ( linear = = 1 )
mOutputFile < < " options_.linear = 1; " < < endl ;
2009-09-02 15:36:56 +02:00
mOutputFile < < " options_.block= " < < block < < " ; " < < endl
2009-12-14 12:06:29 +01:00
< < " options_.bytecode= " < < byte_code < < " ; " < < endl
< < " options_.use_dll= " < < use_dll < < " ; " < < endl ;
2009-09-02 15:36:56 +02:00
2010-10-25 18:20:58 +02:00
config_file . writeCluster ( mOutputFile ) ;
2009-09-03 18:34:15 +02:00
if ( byte_code )
mOutputFile < < " if exist('bytecode') ~= 3 " < < endl
< < " error('DYNARE: Can''t find bytecode DLL. Please compile it or remove the ''bytecode'' option.') " < < endl
< < " end " < < endl ;
2009-09-02 15:36:56 +02:00
// Erase possible remnants of previous runs
2011-04-28 10:41:43 +02:00
string dynfile = basename + " _dynamic.m " ;
unlink ( dynfile . c_str ( ) ) ;
2009-09-02 15:36:56 +02:00
2011-04-28 10:41:43 +02:00
string statfile = basename + " _static.m " ;
unlink ( statfile . c_str ( ) ) ;
2009-09-02 15:36:56 +02:00
if ( ! use_dll )
2011-08-18 12:44:11 +02:00
{
mOutputFile < < " erase_compiled_function(' " + basename + " _static'); " < < endl ;
mOutputFile < < " erase_compiled_function(' " + basename + " _dynamic'); " < < endl ;
}
2009-09-02 15:36:56 +02:00
2010-02-23 16:56:15 +01:00
# if defined(_WIN32) || defined(__CYGWIN32__)
// If using USE_DLL with MSVC, check that the user didn't use a function not supported by MSVC (because MSVC doesn't comply with C99 standard)
if ( use_dll & & msvc )
{
if ( dynamic_model . isUnaryOpUsed ( oAcosh ) )
{
cerr < < " ERROR: acosh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( dynamic_model . isUnaryOpUsed ( oAsinh ) )
{
cerr < < " ERROR: asinh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( dynamic_model . isUnaryOpUsed ( oAtanh ) )
{
cerr < < " ERROR: atanh() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( dynamic_model . isTrinaryOpUsed ( oNormcdf ) )
{
cerr < < " ERROR: normcdf() function is not supported with USE_DLL option and MSVC compiler; use Cygwin compiler instead. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
# endif
2009-09-02 15:36:56 +02:00
// Compile the dynamic MEX file for use_dll option
if ( use_dll )
2009-12-11 15:46:21 +01:00
{
mOutputFile < < " if ~exist('OCTAVE_VERSION') " < < endl ;
// Some mex commands are enclosed in an eval(), because otherwise it will make Octave fail
# if defined(_WIN32) || defined(__CYGWIN32__)
if ( msvc )
2011-08-18 12:44:11 +02:00
// MATLAB/Windows + Microsoft Visual C++
mOutputFile < < " eval('mex -O LINKFLAGS= \" $LINKFLAGS /export:Dynamic \" " < < basename < < " _dynamic.c') " < < endl
< < " eval('mex -O LINKFLAGS= \" $LINKFLAGS /export:Dynamic \" " < < basename < < " _static.c') " < < endl ;
2009-12-11 15:46:21 +01:00
else if ( cygwin )
2011-08-18 12:44:11 +02:00
// MATLAB/Windows + Cygwin g++
mOutputFile < < " eval('mex -O PRELINK_CMDS1= \" echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Dynamic >> mex.def \" " < < basename < < " _dynamic.c') " < < endl
< < " eval('mex -O PRELINK_CMDS1= \" echo EXPORTS > mex.def & echo mexFunction >> mex.def & echo Dynamic >> mex.def \" " < < basename < < " _static.c') " < < endl ;
2009-12-11 15:46:21 +01:00
else
2010-02-17 13:14:43 +01:00
mOutputFile < < " error('When using the USE_DLL option, you must give either ''cygwin'' or ''msvc'' option to the ''dynare'' command') " < < endl ;
2009-12-11 15:46:21 +01:00
# else
2009-12-11 16:34:47 +01:00
# ifdef __linux__
2011-08-18 12:44:11 +02:00
// MATLAB/Linux
mOutputFile < < " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " < < basename < < " _dynamic.c') " < < endl
< < " eval('mex -O LDFLAGS=''-pthread -shared -Wl,--no-undefined'' " < < basename < < " _static.c') " < < endl ;
2009-12-11 16:34:47 +01:00
# else // MacOS
2011-08-18 12:44:11 +02:00
// MATLAB/MacOS
mOutputFile < < " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\ $ARCHS -Wl,-syslibroot, \\ $SDKROOT -mmacosx-version-min= \\ $MACOSX_DEPLOYMENT_TARGET -bundle'' " < < basename < < " _dynamic.c') " < < endl
< < " eval('mex -O LDFLAGS=''-Wl,-twolevel_namespace -undefined error -arch \\ $ARCHS -Wl,-syslibroot, \\ $SDKROOT -mmacosx-version-min= \\ $MACOSX_DEPLOYMENT_TARGET -bundle'' " < < basename < < " _static.c') " < < endl ;
2009-12-11 16:34:47 +01:00
# endif
2009-12-11 15:46:21 +01:00
# endif
2009-12-18 14:35:50 +01:00
mOutputFile < < " else " < < endl // Octave
< < " if ~octave_ver_less_than('3.2.0') " < < endl // Workaround for bug in Octave >= 3.2, see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=550823
< < " sleep(2) " < < endl
< < " end " < < endl
< < " mex " < < basename < < " _dynamic.c " < < endl
2011-08-18 12:44:11 +02:00
< < " mex " < < basename < < " _static.c " < < endl
2009-12-16 18:13:23 +01:00
< < " end " < < endl ;
2009-12-11 15:46:21 +01:00
}
2009-09-02 15:36:56 +02:00
// Add path for block option with M-files
if ( block & & ! byte_code )
mOutputFile < < " addpath " < < basename < < " ; " < < endl ;
2011-03-21 18:40:57 +01:00
if ( mod_file_struct . ramsey_policy_present )
mOutputFile < < " M_.orig_eq_nbr = " < < ramsey_policy_orig_eqn_nbr < < " ; " < < endl ;
2009-04-14 16:39:53 +02:00
if ( dynamic_model . equation_number ( ) > 0 )
2007-07-16 18:47:09 +02:00
{
2011-09-20 14:18:31 +02:00
dynamic_model . writeOutput ( mOutputFile , basename , block , byte_code , use_dll , mod_file_struct . order_option , mod_file_struct . estimation_present ) ;
2010-09-17 11:23:00 +02:00
if ( ! no_static )
2011-03-28 11:19:10 +02:00
static_model . writeOutput ( mOutputFile , block ) ;
2007-07-16 18:47:09 +02:00
}
2006-12-12 12:54:30 +01:00
// Print statements
2009-12-16 18:13:23 +01:00
for ( vector < Statement * > : : const_iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
2009-09-30 17:10:31 +02:00
{
( * it ) - > writeOutput ( mOutputFile , basename ) ;
// Special treatment for initval block: insert initial values for the auxiliary variables
InitValStatement * ivs = dynamic_cast < InitValStatement * > ( * it ) ;
if ( ivs ! = NULL )
{
2010-04-27 17:04:52 +02:00
static_model . writeAuxVarInitval ( mOutputFile , oMatlabOutsideModel ) ;
2009-09-30 17:10:31 +02:00
ivs - > writeOutputPostInit ( mOutputFile ) ;
}
// Special treatment for load params and steady state statement: insert initial values for the auxiliary variables
LoadParamsAndSteadyStateStatement * lpass = dynamic_cast < LoadParamsAndSteadyStateStatement * > ( * it ) ;
2010-01-08 12:06:25 +01:00
if ( lpass & & ! no_static )
2010-04-27 17:04:52 +02:00
static_model . writeAuxVarInitval ( mOutputFile , oMatlabOutsideModel ) ;
2009-09-30 17:10:31 +02:00
}
2006-12-12 12:54:30 +01:00
2009-09-02 15:36:56 +02:00
// Remove path for block option with M-files
if ( block & & ! byte_code )
mOutputFile < < " rmpath " < < basename < < " ; " < < endl ;
2008-12-05 12:27:28 +01:00
2010-10-25 18:20:58 +02:00
mOutputFile < < " save(' " < < basename < < " _results.mat', 'oo_', 'M_', 'options_'); " < < endl ;
config_file . writeEndParallel ( mOutputFile ) ;
mOutputFile < < " diary off " < < endl
2009-07-07 16:20:48 +02:00
< < endl < < " disp(['Total computing time : ' dynsec2hms(toc) ]); " < < endl ;
2006-12-12 12:54:30 +01:00
mOutputFile . close ( ) ;
2009-04-30 15:05:21 +02:00
2009-07-07 16:20:48 +02:00
// Create static and dynamic files
if ( dynamic_model . equation_number ( ) > 0 )
{
2010-01-08 12:06:25 +01:00
if ( ! no_static )
2011-08-18 12:44:11 +02:00
static_model . writeStaticFile ( basename , block , byte_code , use_dll ) ;
2009-12-16 14:21:31 +01:00
2011-05-03 11:28:43 +02:00
dynamic_model . writeDynamicFile ( basename , block , byte_code , use_dll , mod_file_struct . order_option ) ;
dynamic_model . writeParamsDerivativesFile ( basename ) ;
2009-07-07 16:20:48 +02:00
}
2010-04-23 18:39:07 +02:00
// Create steady state file
2010-05-31 17:43:17 +02:00
steady_state_model . writeSteadyStateFile ( basename , mod_file_struct . ramsey_policy_present ) ;
2010-04-23 18:39:07 +02:00
2009-04-30 15:05:21 +02:00
cout < < " done " < < endl ;
2006-12-12 12:54:30 +01:00
}