2008-01-11 14:42:14 +01:00
/*
2015-02-16 08:31:30 +01:00
* Copyright ( C ) 2006 - 2015 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>
2012-05-08 16:02:22 +02:00
# ifndef _WIN32
# include <unistd.h>
# endif
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
2013-02-26 16:50:05 +01:00
ModFile : : ModFile ( WarningConsolidation & warnings_arg )
: expressions_tree ( symbol_table , num_constants , external_functions_table ) ,
2015-02-16 08:31:30 +01:00
original_model ( symbol_table , num_constants , external_functions_table ) ,
2013-02-26 16:50:05 +01:00
dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
trend_dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
ramsey_FOC_equations_dynamic_model ( symbol_table , num_constants , external_functions_table ) ,
static_model ( symbol_table , num_constants , external_functions_table ) ,
steady_state_model ( symbol_table , num_constants , external_functions_table , static_model ) ,
linear ( false ) , block ( false ) , byte_code ( false ) , use_dll ( false ) , no_static ( false ) ,
2013-04-25 18:09:31 +02:00
differentiate_forward_vars ( false ) ,
2014-10-13 17:25:08 +02:00
nonstationary_variables ( false ) , orig_eqn_nbr ( 0 ) , ramsey_eqn_nbr ( 0 ) ,
2015-07-30 14:40:03 +02:00
param_used_with_lead_lag ( false ) , warnings ( warnings_arg )
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 )
2012-01-23 16:52:27 +01:00
warnings < < " 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 + + )
2012-01-23 16:52:27 +01:00
( * it ) - > checkPass ( mod_file_struct , warnings ) ;
2006-12-15 12:44:58 +01:00
2010-05-31 17:43:17 +02:00
// Check the steady state block
2014-03-09 09:52:44 +01:00
steady_state_model . checkPass ( mod_file_struct . ramsey_model_present , warnings ) ;
2010-05-31 17:43:17 +02:00
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 ;
2015-07-30 14:40:03 +02:00
param_used_with_lead_lag = dynamic_model . ParamUsedWithLeadLag ( ) ;
if ( param_used_with_lead_lag )
warnings < < " WARNING: A parameter was used with a lead or a lag in the model block " < < endl ;
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
2013-10-28 14:22:23 +01:00
| | mod_file_struct . discretionary_policy_present
| | mod_file_struct . calib_smoother_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
2014-04-09 17:57:17 +02:00
| | mod_file_struct . perfect_foresight_solver_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
}
2015-09-03 17:04:34 +02:00
if ( ( mod_file_struct . ramsey_model_present | | mod_file_struct . ramsey_policy_present )
& & mod_file_struct . discretionary_policy_present )
{
cerr < < " ERROR: You cannot use the discretionary_policy command when you use either rasmey_model or ramsey_policy and vice versa " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2014-03-09 09:52:44 +01:00
if ( ( ( mod_file_struct . ramsey_model_present | | mod_file_struct . discretionary_policy_present )
2011-03-17 15:15:13 +01:00
& & ! mod_file_struct . planner_objective_present )
2014-03-09 09:52:44 +01:00
| | ( ! ( mod_file_struct . ramsey_model_present | | mod_file_struct . discretionary_policy_present )
2011-03-17 15:15:13 +01:00
& & mod_file_struct . planner_objective_present ) )
2011-03-03 16:03:21 +01:00
{
2014-03-09 09:52:44 +01:00
cerr < < " ERROR: A planner_objective statement must be used with a ramsey_model, 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 ) ;
}
2014-04-09 17:57:17 +02:00
if ( mod_file_struct . perfect_foresight_solver_present & & stochastic_statement_present )
2007-07-16 18:47:09 +02:00
{
2014-04-09 17:57:17 +02:00
cerr < < " ERROR: A .mod file cannot contain both one of {perfect_foresight_solver,simul} and one of {stoch_simul, estimation, osr, ramsey_policy, discretionary_policy}. This is not possible: one cannot mix perfect foresight context with stochastic context in the same file. " < < 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
2012-01-23 16:52:27 +01:00
warnings < < " 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 ;
2010-06-21 18:40:36 +02:00
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 ) ;
}
2013-04-11 17:07:39 +02:00
if ( dynamic_model . staticOnlyEquationsNbr ( ) ! = dynamic_model . dynamicOnlyEquationsNbr ( ) )
{
cerr < < " ERROR: the number of equations marked [static] must be equal to the number of equations marked [dynamic] " < < endl ;
exit ( EXIT_FAILURE ) ;
}
if ( dynamic_model . staticOnlyEquationsNbr ( ) > 0 & &
2014-03-09 09:52:44 +01:00
( mod_file_struct . ramsey_model_present | | mod_file_struct . discretionary_policy_present ) )
2013-04-11 17:07:39 +02:00
{
2014-03-09 09:52:44 +01:00
cerr < < " ERROR: marking equations as [static] or [dynamic] is not possible with ramsey_model, ramsey_policy or discretionary_policy " < < endl ;
2013-04-11 17:07:39 +02:00
exit ( EXIT_FAILURE ) ;
}
2013-05-17 14:14:15 +02:00
if ( stochastic_statement_present & &
( dynamic_model . isUnaryOpUsed ( oSign )
| | dynamic_model . isUnaryOpUsed ( oAbs )
| | dynamic_model . isBinaryOpUsed ( oMax )
| | dynamic_model . isBinaryOpUsed ( oMin )
| | dynamic_model . isBinaryOpUsed ( oGreater )
| | dynamic_model . isBinaryOpUsed ( oLess )
| | dynamic_model . isBinaryOpUsed ( oGreaterEqual )
| | dynamic_model . isBinaryOpUsed ( oLessEqual )
| | dynamic_model . isBinaryOpUsed ( oEqualEqual )
| | dynamic_model . isBinaryOpUsed ( oDifferent ) ) )
warnings < < " WARNING: you are using a function (max, min, abs, sign) or an operator (<, >, <=, >=, ==, !=) which is unsuitable for a stochastic context; see the reference manual, section about \" Expressions \" , for more details. " < < endl ;
2013-11-29 14:50:58 +01:00
// Test if some estimated parameters are used within the values of shocks
// statements (see issue #469)
set < int > parameters_intersect ;
set_intersection ( mod_file_struct . parameters_within_shocks_values . begin ( ) ,
mod_file_struct . parameters_within_shocks_values . end ( ) ,
mod_file_struct . estimated_parameters . begin ( ) ,
mod_file_struct . estimated_parameters . end ( ) ,
inserter ( parameters_intersect , parameters_intersect . begin ( ) ) ) ;
if ( parameters_intersect . size ( ) > 0 )
{
cerr < < " ERROR: some estimated parameters ( " ;
for ( set < int > : : const_iterator it = parameters_intersect . begin ( ) ;
it ! = parameters_intersect . end ( ) ; )
{
cerr < < symbol_table . getName ( * it ) ;
if ( + + it ! = parameters_intersect . end ( ) )
cerr < < " , " ;
}
cerr < < " ) also appear in the expressions defining the variance/covariance matrix of shocks; this is not allowed. " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2013-11-29 16:03:15 +01:00
// Check if some exogenous is not used in the model block
set < int > unusedExo = dynamic_model . findUnusedExogenous ( ) ;
2015-03-06 09:49:11 +01:00
if ( unusedExo . size ( ) > 0 )
2013-11-29 16:03:15 +01:00
{
warnings < < " WARNING: some exogenous ( " ;
for ( set < int > : : const_iterator it = unusedExo . begin ( ) ;
it ! = unusedExo . end ( ) ; )
{
warnings < < symbol_table . getName ( * it ) ;
if ( + + it ! = unusedExo . end ( ) )
warnings < < " , " ;
}
2015-06-09 16:42:51 +02:00
warnings < < " ) are declared but not used in the model. This may lead to crashes or unexpected behaviour. " < < endl ;
2013-11-29 16:03:15 +01:00
}
2009-09-30 17:10:31 +02:00
}
void
2013-09-13 20:37:31 +02:00
ModFile : : transformPass ( bool nostrict )
2009-09-30 17:10:31 +02:00
{
2015-02-16 08:31:30 +01:00
// Save the original model (must be done before any model transformations by preprocessor)
dynamic_model . cloneDynamic ( original_model ) ;
2013-09-13 20:37:31 +02:00
if ( nostrict )
{
2013-11-29 15:32:49 +01:00
set < int > unusedEndogs = dynamic_model . findUnusedEndogenous ( ) ;
2013-09-13 20:37:31 +02:00
for ( set < int > : : iterator it = unusedEndogs . begin ( ) ; it ! = unusedEndogs . end ( ) ; it + + )
{
symbol_table . changeType ( * it , eUnusedEndogenous ) ;
warnings < < " WARNING: ' " < < symbol_table . getName ( * it )
< < " ' not used in model block, removed by nostrict command-line option " < < endl ;
}
}
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 ( ) ;
}
2014-10-13 17:16:41 +02:00
orig_eqn_nbr = dynamic_model . equation_number ( ) ;
2014-03-09 09:52:44 +01:00
if ( mod_file_struct . ramsey_model_present )
2011-03-21 18:40:57 +01:00
{
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
/*
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 ) ;
2014-10-13 17:25:08 +02:00
ramsey_eqn_nbr = dynamic_model . equation_number ( ) - orig_eqn_nbr ;
2011-03-21 18:40:57 +01:00
}
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
2013-10-28 14:22:23 +01:00
| | mod_file_struct . discretionary_policy_present
| | mod_file_struct . calib_smoother_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
2013-04-25 18:09:31 +02:00
if ( differentiate_forward_vars )
2013-05-17 16:51:34 +02:00
dynamic_model . differentiateForwardVars ( differentiate_forward_vars_subset ) ;
2013-04-25 18:09:31 +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
/*
2013-09-13 20:37:31 +02:00
Enforce the same number of equations and endogenous , except in three cases :
2014-03-09 09:52:44 +01:00
- ramsey_model , ramsey_policy or discretionary_policy is used
2008-10-17 14:52:08 +02:00
- a BVAR command is used and there is no equation ( standalone BVAR estimation )
2013-09-13 20:37:31 +02:00
- nostrict option is passed and there are more endogs than equations ( dealt with before freeze )
2008-10-17 14:52:08 +02:00
*/
2014-03-09 09:52:44 +01:00
if ( ! ( mod_file_struct . ramsey_model_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 )
2015-06-04 14:16:52 +02:00
& & ! ( mod_file_struct . occbin_option )
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
2014-04-09 17:57:17 +02:00
if ( symbol_table . exo_det_nbr ( ) > 0 & & mod_file_struct . perfect_foresight_solver_present )
2011-03-18 12:47:06 +01:00
{
2014-04-09 17:57:17 +02:00
cerr < < " ERROR: A .mod file cannot contain both one of {perfect_foresight_solver,simul} and varexo_det declaration (all exogenous variables are deterministic in this case) " < < endl ;
2011-03-18 12:47:06 +01:00
exit ( EXIT_FAILURE ) ;
}
2011-03-21 18:40:57 +01:00
2013-02-07 11:52:59 +01:00
if ( mod_file_struct . ramsey_policy_present & & symbol_table . exo_det_nbr ( ) > 0 )
{
cerr < < " ERROR: ramsey_policy is incompatible with deterministic exogenous variables " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2014-07-30 17:28:45 +02:00
if ( mod_file_struct . identification_present & & symbol_table . exo_det_nbr ( ) > 0 )
{
cerr < < " ERROR: identification is incompatible with deterministic exogenous variables " < < endl ;
exit ( EXIT_FAILURE ) ;
}
2014-03-09 09:52:44 +01:00
if ( ! mod_file_struct . ramsey_model_present )
2011-03-21 18:40:57 +01:00
cout < < " Found " < < dynamic_model . equation_number ( ) < < " equation(s). " < < endl ;
else
{
2014-10-13 17:16:41 +02:00
cout < < " Found " < < orig_eqn_nbr < < " equation(s). " < < endl ;
2011-03-21 18:40:57 +01:00
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
2014-02-25 15:53:14 +01:00
ModFile : : computingPass ( bool no_tmp_terms , FileOutputType output )
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
{
2013-10-29 11:46:54 +01:00
if ( nonstationary_variables )
trend_dynamic_model . runTrendTest ( global_eval_context ) ;
2010-10-15 19:05:16 +02:00
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 )
2015-05-10 18:16:11 +02:00
{
if ( mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_model_present | | mod_file_struct . identification_present
| | mod_file_struct . calib_smoother_present )
static_model . set_cutoff_to_zero ( ) ;
const bool static_hessian = mod_file_struct . identification_present
| | mod_file_struct . estimation_analytic_derivation ;
const bool paramsDerivatives = mod_file_struct . identification_present
| | mod_file_struct . estimation_analytic_derivation ;
static_model . computingPass ( global_eval_context , no_tmp_terms , static_hessian ,
false , paramsDerivatives , block , byte_code ) ;
}
2009-04-14 16:39:53 +02:00
// Set things to compute for dynamic model
2014-04-09 17:57:17 +02:00
if ( mod_file_struct . perfect_foresight_solver_present | | mod_file_struct . check_present
2015-05-10 18:16:11 +02:00
| | mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_model_present | | mod_file_struct . identification_present
| | mod_file_struct . calib_smoother_present )
{
if ( mod_file_struct . perfect_foresight_solver_present )
dynamic_model . computingPass ( true , false , false , false , global_eval_context , no_tmp_terms , block , use_dll , byte_code ) ;
else
{
if ( mod_file_struct . stoch_simul_present
| | mod_file_struct . estimation_present | | mod_file_struct . osr_present
| | mod_file_struct . ramsey_model_present | | mod_file_struct . identification_present
| | mod_file_struct . calib_smoother_present )
dynamic_model . set_cutoff_to_zero ( ) ;
if ( mod_file_struct . order_option < 1 | | mod_file_struct . order_option > 3 )
{
cerr < < " ERROR: Incorrect order option... " < < endl ;
exit ( EXIT_FAILURE ) ;
}
bool hessian = mod_file_struct . order_option > = 2
| | mod_file_struct . identification_present
| | mod_file_struct . estimation_analytic_derivation
| | output = = second
| | output = = third ;
bool thirdDerivatives = mod_file_struct . order_option = = 3
| | mod_file_struct . estimation_analytic_derivation
| | output = = third ;
bool paramsDerivatives = mod_file_struct . identification_present | | mod_file_struct . estimation_analytic_derivation ;
dynamic_model . computingPass ( true , hessian , thirdDerivatives , paramsDerivatives , global_eval_context , no_tmp_terms , block , use_dll , byte_code ) ;
}
}
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
2015-05-28 11:42:12 +02:00
ModFile : : writeOutputFiles ( const string & basename , bool clear_all , bool clear_global , bool no_log , bool no_warn ,
bool console , bool nograph , bool nointeractive , const ConfigFile & config_file ,
bool check_model_changes , bool minimal_workspace
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 ( ) )
{
2015-06-30 12:12:49 +02:00
cerr < < " ERROR: Can't open file " < < fname < < " 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
2015-06-30 12:12:49 +02:00
< < " % Status : main Dynare file " < < endl
2008-06-16 18:33:28 +02:00
< < " % " < < endl
< < " % Warning : this file is generated automatically by Dynare " < < endl
< < " % from model file (.mod) " < < endl < < endl ;
2006-12-12 12:54:30 +01:00
2013-02-26 16:50:05 +01:00
if ( no_warn )
mOutputFile < < " warning off " < < endl ; // This will be executed *after* function warning_config()
2006-12-12 12:54:30 +01:00
if ( clear_all )
2015-09-30 22:52:48 +02:00
mOutputFile < < " if isoctave || matlab_ver_less_than('8.6') " < < endl
< < " clear all " < < endl
< < " else " < < endl
< < " clearvars -global " < < endl
2015-11-12 14:38:16 +01:00
< < " clear_persistent_variables(fileparts(which('dynare')), false) " < < endl
2015-09-30 22:52:48 +02:00
< < " end " < < endl ;
2014-12-30 21:51:33 +01:00
else if ( clear_global )
2015-04-04 19:22:49 +02:00
mOutputFile < < " clear M_ options_ oo_ estim_params_ bayestopt_ dataset_ dataset_info estimation_info ys0_ ex0_; " < < endl ;
2009-04-30 15:05:21 +02:00
2015-10-10 11:13:13 +02:00
mOutputFile < < " tic0 = tic; " < < endl
2014-05-14 15:54:15 +02:00
< < " % Save empty dates and dseries objects in memory. " < < endl
< < " dates('initialize'); " < < endl
< < " dseries('initialize'); " < < endl
< < " % Define global variables. " < < endl
2015-04-04 19:22:49 +02:00
< < " global M_ options_ oo_ estim_params_ bayestopt_ dataset_ dataset_info estimation_info ys0_ ex0_ " < < endl
2009-04-30 15:05:21 +02:00
< < " options_ = []; " < < endl
< < " M_.fname = ' " < < basename < < " '; " < < endl
2015-04-30 12:35:33 +02:00
< < " M_.dynare_version = ' " < < PACKAGE_VERSION < < " '; " < < endl
< < " oo_.dynare_version = ' " < < PACKAGE_VERSION < < " '; " < < endl
< < " options_.dynare_version = ' " < < PACKAGE_VERSION < < " '; " < < endl
2009-04-30 15:05:21 +02:00
< < " % " < < endl
< < " % Some global variables initialization " < < endl
2012-06-06 16:07:59 +02:00
< < " % " < < endl ;
config_file . writeHooks ( mOutputFile ) ;
mOutputFile < < " global_initialization; " < < endl
2012-04-20 18:15:02 +02:00
< < " diary off; " < < endl ;
if ( ! no_log )
2013-03-18 13:44:04 +01:00
mOutputFile < < " diary(' " < < basename < < " .log'); " < < endl ;
2009-04-30 15:05:21 +02:00
2015-05-28 11:42:12 +02:00
if ( minimal_workspace )
mOutputFile < < " options_.minimal_workspace = 1; " < < endl ;
2010-10-28 11:19:11 +02:00
if ( console )
2012-11-12 16:34:18 +01:00
mOutputFile < < " options_.console_mode = 1; " < < endl
< < " options_.nodisplay = 1; " < < endl ;
2013-07-10 11:03:48 +02:00
if ( nograph )
mOutputFile < < " options_.nograph = 1; " < < endl ;
2013-07-10 12:02:12 +02:00
if ( nointeractive )
mOutputFile < < " options_.nointeractive = 1; " < < endl ;
2015-07-30 14:40:03 +02:00
if ( param_used_with_lead_lag )
mOutputFile < < " M_.parameter_used_with_lead_lag = true; " < < endl ;
2015-05-11 08:52:50 +02:00
cout < < " Processing outputs ... " < < endl ;
2007-07-16 18:47:09 +02:00
2006-12-12 12:54:30 +01:00
symbol_table . writeOutput ( mOutputFile ) ;
2013-10-28 15:29:41 +01:00
// Initialize M_.Sigma_e, M_.Correlation_matrix, M_.H, and M_.Correlation_matrix_ME
2010-04-14 15:03:41 +02:00
mOutputFile < < " M_.Sigma_e = zeros( " < < symbol_table . exo_nbr ( ) < < " , "
2013-10-28 15:29:41 +01:00
< < symbol_table . exo_nbr ( ) < < " ); " < < endl
< < " M_.Correlation_matrix = eye( " < < symbol_table . exo_nbr ( ) < < " , "
2010-04-14 15:03:41 +02:00
< < symbol_table . exo_nbr ( ) < < " ); " < < endl ;
if ( mod_file_struct . calibrated_measurement_errors )
mOutputFile < < " M_.H = zeros( " < < symbol_table . observedVariablesNbr ( ) < < " , "
2013-10-28 15:29:41 +01:00
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl
< < " M_.Correlation_matrix_ME = eye( " < < symbol_table . observedVariablesNbr ( ) < < " , "
2010-04-14 15:03:41 +02:00
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl ;
else
2013-10-28 15:29:41 +01:00
mOutputFile < < " M_.H = 0; " < < endl
< < " M_.Correlation_matrix_ME = 1; " < < endl ;
2010-04-14 15:03:41 +02:00
2014-04-10 11:43:26 +02:00
// May be later modified by a shocks block
mOutputFile < < " M_.sigma_e_is_diagonal = 1; " < < endl ;
2014-04-09 18:33:24 +02:00
// Initialize M_.det_shocks
mOutputFile < < " M_.det_shocks = []; " < < 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
2013-05-31 14:47:28 +02:00
if ( parallel_local_files . size ( ) > 0 )
{
mOutputFile < < " options_.parallel_info.local_files = { " < < endl ;
for ( size_t i = 0 ; i < parallel_local_files . size ( ) ; i + + )
{
size_t j = parallel_local_files [ i ] . find_last_of ( " / \\ " ) ;
if ( j = = string : : npos )
mOutputFile < < " '', ' " < < parallel_local_files [ i ] < < " '; " < < endl ;
else
mOutputFile < < " ' " < < parallel_local_files [ i ] . substr ( 0 , j + 1 ) < < " ', ' "
< < parallel_local_files [ i ] . substr ( j + 1 , string : : npos ) < < " '; " < < endl ;
}
mOutputFile < < " }; " < < endl ;
}
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 ;
2015-05-10 18:16:11 +02:00
bool hasModelChanged = ! dynamic_model . isChecksumMatching ( basename ) ;
2015-05-10 20:29:03 +02:00
if ( ! check_model_changes )
hasModelChanged = true ;
2015-05-10 18:16:11 +02:00
if ( hasModelChanged )
{
// Erase possible remnants of previous runs
unlink ( ( basename + " _dynamic.m " ) . c_str ( ) ) ;
unlink ( ( basename + " _dynamic.cod " ) . c_str ( ) ) ;
unlink ( ( basename + " _dynamic.bin " ) . c_str ( ) ) ;
2009-09-02 15:36:56 +02:00
2015-05-10 18:16:11 +02:00
unlink ( ( basename + " _static.m " ) . c_str ( ) ) ;
unlink ( ( basename + " _static.cod " ) . c_str ( ) ) ;
unlink ( ( basename + " _static.bin " ) . c_str ( ) ) ;
2011-11-25 15:11:02 +01:00
2015-05-10 18:16:11 +02:00
unlink ( ( basename + " _steadystate2.m " ) . c_str ( ) ) ;
unlink ( ( basename + " _set_auxiliary_variables.m " ) . 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
2015-05-10 20:29:03 +02:00
// When check_model_changes is true, don't force compile if MEX is fresher than source
2009-09-02 15:36:56 +02:00
if ( use_dll )
2009-12-11 15:46:21 +01:00
{
# if defined(_WIN32) || defined(__CYGWIN32__)
if ( msvc )
2011-08-18 12:44:11 +02:00
// MATLAB/Windows + Microsoft Visual C++
2015-05-10 20:29:03 +02:00
mOutputFile < < " dyn_mex('msvc', ' " < < basename < < " ', " < < ! check_model_changes < < " ) " < < endl ;
2009-12-11 15:46:21 +01:00
else if ( cygwin )
2011-08-18 12:44:11 +02:00
// MATLAB/Windows + Cygwin g++
2015-05-10 20:29:03 +02:00
mOutputFile < < " dyn_mex('cygwin', ' " < < basename < < " ', " < < ! check_model_changes < < " ) " < < 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
2015-05-10 20:29:03 +02:00
// other configurations
mOutputFile < < " dyn_mex('', ' " < < basename < < " ', " < < ! check_model_changes < < " ) " < < endl ;
2009-12-11 15:46:21 +01:00
# endif
}
2009-09-02 15:36:56 +02:00
// Add path for block option with M-files
if ( block & & ! byte_code )
mOutputFile < < " addpath " < < basename < < " ; " < < endl ;
2014-10-13 17:17:50 +02:00
mOutputFile < < " M_.orig_eq_nbr = " < < orig_eqn_nbr < < " ; " < < endl
2014-10-13 17:25:08 +02:00
< < " M_.eq_nbr = " < < dynamic_model . equation_number ( ) < < " ; " < < endl
< < " M_.ramsey_eq_nbr = " < < ramsey_eqn_nbr < < " ; " < < endl ;
2011-03-21 18:40:57 +01:00
2009-04-14 16:39:53 +02:00
if ( dynamic_model . equation_number ( ) > 0 )
2007-07-16 18:47:09 +02:00
{
2015-07-29 14:59:09 +02:00
dynamic_model . writeOutput ( mOutputFile , basename , block , byte_code , use_dll , mod_file_struct . order_option , mod_file_struct . estimation_present , false ) ;
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
{
2015-05-28 11:42:12 +02:00
( * it ) - > writeOutput ( mOutputFile , basename , minimal_workspace ) ;
2009-09-30 17:10:31 +02:00
2013-04-22 17:29:59 +02:00
/* Special treatment for initval block: insert initial values for the
auxiliary variables and initialize exo det */
2009-09-30 17:10:31 +02:00
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 ) ;
}
2013-04-22 17:29:59 +02:00
// Special treatment for endval block: insert initial values for the auxiliary variables
EndValStatement * evs = dynamic_cast < EndValStatement * > ( * it ) ;
if ( evs ! = NULL )
static_model . writeAuxVarInitval ( mOutputFile , oMatlabOutsideModel ) ;
2009-09-30 17:10:31 +02:00
// 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
2013-11-13 11:29:27 +01:00
mOutputFile < < " save(' " < < basename < < " _results.mat', 'oo_', 'M_', 'options_'); " < < endl
< < " if exist('estim_params_', 'var') == 1 " < < endl
< < " save(' " < < basename < < " _results.mat', 'estim_params_', '-append'); " < < endl < < " end " < < endl
< < " if exist('bayestopt_', 'var') == 1 " < < endl
< < " save(' " < < basename < < " _results.mat', 'bayestopt_', '-append'); " < < endl < < " end " < < endl
< < " if exist('dataset_', 'var') == 1 " < < endl
< < " save(' " < < basename < < " _results.mat', 'dataset_', '-append'); " < < endl < < " end " < < endl
< < " if exist('estimation_info', 'var') == 1 " < < endl
2014-12-17 11:09:46 +01:00
< < " save(' " < < basename < < " _results.mat', 'estimation_info', '-append'); " < < endl < < " end " < < endl
< < " if exist('oo_recursive_', 'var') == 1 " < < endl
< < " save(' " < < basename < < " _results.mat', 'oo_recursive_', '-append'); " < < endl < < " end " < < endl ;
2010-10-25 18:20:58 +02:00
config_file . writeEndParallel ( mOutputFile ) ;
2012-01-23 16:52:27 +01:00
mOutputFile < < endl < < endl
2015-10-10 11:13:13 +02:00
< < " disp(['Total computing time : ' dynsec2hms(toc(tic0)) ]); " < < endl ;
2012-01-23 16:52:27 +01:00
2013-02-26 16:50:05 +01:00
if ( ! no_warn )
{
2013-03-05 12:53:37 +01:00
if ( warnings . countWarnings ( ) > 0 )
mOutputFile < < " disp('Note: " < < warnings . countWarnings ( ) < < " warning(s) encountered in the preprocessor') " < < endl ;
mOutputFile < < " if ~isempty(lastwarn) " < < endl
2013-02-26 16:50:05 +01:00
< < " disp('Note: warning(s) encountered in MATLAB/Octave code') " < < endl
< < " end " < < endl ;
}
2012-01-23 16:52:27 +01:00
2012-04-20 18:15:02 +02:00
if ( ! no_log )
mOutputFile < < " diary off " < < endl ;
2006-12-12 12:54:30 +01:00
mOutputFile . close ( ) ;
2009-04-30 15:05:21 +02:00
2015-05-10 18:16:11 +02:00
if ( hasModelChanged )
2009-07-07 16:20:48 +02:00
{
2015-05-10 18:16:11 +02:00
// Create static and dynamic files
if ( dynamic_model . equation_number ( ) > 0 )
{
if ( ! no_static )
{
2015-07-27 15:59:13 +02:00
static_model . writeStaticFile ( basename , block , byte_code , use_dll , false ) ;
2015-07-28 12:29:42 +02:00
static_model . writeParamsDerivativesFile ( basename , false ) ;
2015-05-10 18:16:11 +02:00
}
2009-12-16 14:21:31 +01:00
2015-07-27 17:02:51 +02:00
dynamic_model . writeDynamicFile ( basename , block , byte_code , use_dll , mod_file_struct . order_option , false ) ;
2015-07-28 14:59:55 +02:00
dynamic_model . writeParamsDerivativesFile ( basename , false ) ;
2015-05-10 18:16:11 +02:00
}
2010-04-23 18:39:07 +02:00
2015-05-10 18:16:11 +02:00
// Create steady state file
2015-07-28 17:27:56 +02:00
steady_state_model . writeSteadyStateFile ( basename , mod_file_struct . ramsey_model_present , false ) ;
2015-05-10 18:16:11 +02:00
}
2015-07-21 17:47:56 +02:00
2009-04-30 15:05:21 +02:00
cout < < " done " < < endl ;
2006-12-12 12:54:30 +01:00
}
2015-07-21 17:26:08 +02:00
void
ModFile : : writeExternalFiles ( const string & basename , FileOutputType output , LanguageOutputType language ) const
{
switch ( language )
{
case c :
writeExternalFilesC ( basename , output ) ;
break ;
case cpp :
writeExternalFilesCC ( basename , output ) ;
break ;
2015-07-21 17:47:56 +02:00
case julia :
writeExternalFilesJulia ( basename , output ) ;
break ;
2015-07-21 17:26:08 +02:00
default :
cerr < < " This case shouldn't happen. Contact the authors of Dynare " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
void
ModFile : : writeExternalFilesC ( const string & basename , FileOutputType output ) const
{
writeModelC ( basename ) ;
steady_state_model . writeSteadyStateFileC ( basename , mod_file_struct . ramsey_model_present ) ;
2015-07-27 17:02:51 +02:00
dynamic_model . writeDynamicFile ( basename , block , byte_code , use_dll , mod_file_struct . order_option , false ) ;
2015-07-21 17:26:08 +02:00
if ( ! no_static )
2015-07-27 15:59:13 +02:00
static_model . writeStaticFile ( basename , false , false , true , false ) ;
2015-07-21 17:26:08 +02:00
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
// static_model.writeParamsDerivativesFileC(basename, cuda);
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
2015-08-24 12:37:04 +02:00
dynamic_model . writeResidualsC ( basename , cuda ) ;
2015-07-21 17:26:08 +02:00
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
dynamic_model . writeFirstDerivativesC ( basename , cuda ) ;
if ( output = = second )
dynamic_model . writeSecondDerivativesC_csr ( basename , cuda ) ;
else if ( output = = third )
{
dynamic_model . writeSecondDerivativesC_csr ( basename , cuda ) ;
dynamic_model . writeThirdDerivativesC_csr ( basename , cuda ) ;
}
}
void
ModFile : : writeModelC ( const string & basename ) const
{
string filename = basename + " .c " ;
ofstream mDriverCFile ;
mDriverCFile . open ( filename . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! mDriverCFile . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
mDriverCFile < < " /* " < < endl
< < " * " < < filename < < " : Driver file for Dynare C code " < < endl
< < " * " < < endl
< < " * Warning : this file is generated automatically by Dynare " < < endl
< < " * from model file (.mod) " < < endl
< < " */ " < < endl
< < endl
< < " #include \" dynare_driver.h \" " < < endl
< < endl
< < " struct " < < endl
< < " { " < < endl ;
// Write basic info
symbol_table . writeCOutput ( mDriverCFile ) ;
mDriverCFile < < endl < < " params.resize(param_nbr); " < < endl ;
if ( dynamic_model . equation_number ( ) > 0 )
{
dynamic_model . writeCOutput ( mDriverCFile , basename , block , byte_code , use_dll , mod_file_struct . order_option , mod_file_struct . estimation_present ) ;
// if (!no_static)
// static_model.writeCOutput(mOutputFile, block);
}
// Print statements
for ( vector < Statement * > : : const_iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
( * it ) - > writeCOutput ( mDriverCFile , basename ) ;
mDriverCFile < < " } DynareInfo; " < < endl ;
mDriverCFile . close ( ) ;
// Write informational m file
ofstream mOutputFile ;
if ( basename . size ( ) )
{
string fname ( basename ) ;
fname + = " .m " ;
mOutputFile . open ( fname . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! mOutputFile . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < fname
< < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
else
{
cerr < < " ERROR: Missing file name " < < endl ;
exit ( EXIT_FAILURE ) ;
}
mOutputFile < < " % " < < endl
< < " % Status : informational m file " < < endl
< < " % " < < endl
< < " % Warning : this file is generated automatically by Dynare " < < endl
< < " % from model file (.mod) " < < endl < < endl
< < " disp('The following C file was successfully created:'); " < < endl
< < " ls preprocessorOutput.c " < < endl < < endl ;
mOutputFile . close ( ) ;
}
void
ModFile : : writeExternalFilesCC ( const string & basename , FileOutputType output ) const
{
writeModelCC ( basename ) ;
steady_state_model . writeSteadyStateFileC ( basename , mod_file_struct . ramsey_model_present ) ;
2015-07-27 17:02:51 +02:00
dynamic_model . writeDynamicFile ( basename , block , byte_code , use_dll , mod_file_struct . order_option , false ) ;
2015-07-21 17:26:08 +02:00
if ( ! no_static )
2015-07-27 15:59:13 +02:00
static_model . writeStaticFile ( basename , false , false , true , false ) ;
2015-07-21 17:26:08 +02:00
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
// static_model.writeParamsDerivativesFileC(basename, cuda);
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
// dynamic_model.writeResidualsC(basename, cuda);
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
2015-08-24 12:37:04 +02:00
dynamic_model . writeResidualsC ( basename , cuda ) ;
2015-08-27 10:00:27 +02:00
dynamic_model . writeFirstDerivativesC_csr ( basename , cuda ) ;
2015-07-21 17:26:08 +02:00
if ( output = = second )
dynamic_model . writeSecondDerivativesC_csr ( basename , cuda ) ;
else if ( output = = third )
{
dynamic_model . writeSecondDerivativesC_csr ( basename , cuda ) ;
dynamic_model . writeThirdDerivativesC_csr ( basename , cuda ) ;
}
}
void
ModFile : : writeModelCC ( const string & basename ) const
{
string filename = basename + " .cc " ;
ofstream mDriverCFile ;
mDriverCFile . open ( filename . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! mDriverCFile . is_open ( ) )
{
cerr < < " Error: Can't open file " < < filename < < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
mDriverCFile < < " /* " < < endl
< < " * " < < filename < < " : Driver file for Dynare C++ code " < < endl
< < " * " < < endl
< < " * Warning : this file is generated automatically by Dynare " < < endl
< < " * from model file (.mod) " < < endl
< < " */ " < < endl
< < endl
< < " #include \" dynare_cpp_driver.hh \" " < < endl
< < endl
< < " DynareInfo::DynareInfo(void) " < < endl
< < " { " < < endl ;
// Write basic info
symbol_table . writeCCOutput ( mDriverCFile ) ;
mDriverCFile < < endl < < " params.resize(param_nbr); " < < endl ;
if ( dynamic_model . equation_number ( ) > 0 )
{
dynamic_model . writeCCOutput ( mDriverCFile , basename , block , byte_code , use_dll , mod_file_struct . order_option , mod_file_struct . estimation_present ) ;
// if (!no_static)
// static_model.writeCOutput(mOutputFile, block);
}
// Print statements
for ( vector < Statement * > : : const_iterator it = statements . begin ( ) ;
it ! = statements . end ( ) ; it + + )
( * it ) - > writeCOutput ( mDriverCFile , basename ) ;
mDriverCFile < < " }; " < < endl ;
mDriverCFile . close ( ) ;
// Write informational m file
ofstream mOutputFile ;
if ( basename . size ( ) )
{
string fname ( basename ) ;
fname + = " .m " ;
mOutputFile . open ( fname . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! mOutputFile . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < fname
< < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
else
{
cerr < < " ERROR: Missing file name " < < endl ;
exit ( EXIT_FAILURE ) ;
}
mOutputFile < < " % " < < endl
< < " % Status : informational m file " < < endl
< < " % " < < endl
< < " % Warning : this file is generated automatically by Dynare " < < endl
< < " % from model file (.mod) " < < endl < < endl
< < " disp('The following C++ file was successfully created:'); " < < endl
< < " ls preprocessorOutput.cc " < < endl < < endl ;
mOutputFile . close ( ) ;
}
2015-07-21 17:47:56 +02:00
void
ModFile : : writeExternalFilesJulia ( const string & basename , FileOutputType output ) const
{
ofstream jlOutputFile ;
if ( basename . size ( ) )
{
string fname ( basename ) ;
fname + = " .jl " ;
jlOutputFile . open ( fname . c_str ( ) , ios : : out | ios : : binary ) ;
if ( ! jlOutputFile . is_open ( ) )
{
cerr < < " ERROR: Can't open file " < < fname
< < " for writing " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
else
{
cerr < < " ERROR: Missing file name " < < endl ;
exit ( EXIT_FAILURE ) ;
}
jlOutputFile < < " module " < < basename < < endl
2015-08-21 12:00:47 +02:00
< < " # " < < endl
< < " # NB: this file was automatically generated by Dynare " < < endl
< < " # from " < < basename < < " .mod " < < endl
< < " # " < < endl
2015-07-23 17:32:09 +02:00
< < " using DynareModel " < < endl
2015-07-29 11:07:36 +02:00
< < " using DynareOptions " < < endl
2015-07-29 14:59:09 +02:00
< < " using DynareOutput " < < endl
2015-07-23 17:32:09 +02:00
< < " using Utils " < < endl
2015-07-27 15:59:13 +02:00
< < " using " < < basename < < " Static " < < endl
2015-08-19 10:59:30 +02:00
< < " using " < < basename < < " Dynamic " < < endl
< < " using " < < basename < < " SteadyState2 " < < endl < < endl
2015-07-29 13:31:42 +02:00
< < " export model " < < endl ;
2015-07-29 14:59:09 +02:00
// Write Output
jlOutputFile < < endl
< < " output = dynare_output() " < < endl
< < " output.dynare_version = \" " < < PACKAGE_VERSION < < " \" " < < endl ;
2015-07-29 13:31:42 +02:00
// Write Options
jlOutputFile < < endl
2015-07-29 11:07:36 +02:00
< < " options = dynare_options() " < < endl
2015-07-29 13:31:42 +02:00
< < " options.dynare_version = \" " < < PACKAGE_VERSION < < " \" " < < endl ;
if ( linear = = 1 )
jlOutputFile < < " options.linear = true " < < endl ;
// Write Model
jlOutputFile < < endl
2015-07-29 11:35:10 +02:00
< < " model = dynare_model() " < < endl
< < " model.fname = \" " < < basename < < " \" " < < endl
< < " model.dynare_version = \" " < < PACKAGE_VERSION < < " \" " < < endl
< < " model.sigma_e = zeros(Float64, " < < symbol_table . exo_nbr ( ) < < " , "
2015-07-24 12:33:28 +02:00
< < symbol_table . exo_nbr ( ) < < " ) " < < endl
2015-07-29 11:35:10 +02:00
< < " model.correlation_matrix = ones(Float64, " < < symbol_table . exo_nbr ( ) < < " , "
2015-07-24 12:33:28 +02:00
< < symbol_table . exo_nbr ( ) < < " ) " < < endl
2015-07-29 11:35:10 +02:00
< < " model.orig_eq_nbr = " < < orig_eqn_nbr < < endl
< < " model.eq_nbr = " < < dynamic_model . equation_number ( ) < < endl
< < " model.ramsey_eq_nbr = " < < ramsey_eqn_nbr < < endl ;
2015-07-21 17:47:56 +02:00
2015-07-24 12:33:28 +02:00
if ( mod_file_struct . calibrated_measurement_errors )
2015-07-29 11:35:10 +02:00
jlOutputFile < < " model.h = zeros(Float64, "
2015-07-24 12:33:28 +02:00
< < symbol_table . observedVariablesNbr ( ) < < " , "
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl
2015-07-29 11:35:10 +02:00
< < " model.correlation_matrix_me = ones(Float64, "
2015-07-24 12:33:28 +02:00
< < symbol_table . observedVariablesNbr ( ) < < " , "
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl ;
else
2015-07-29 11:35:10 +02:00
jlOutputFile < < " model.h = zeros(Float64, 1, 1) " < < endl
< < " model.correlation_matrix_me = ones(Float64, 1, 1) " < < endl ;
2015-07-24 12:33:28 +02:00
cout < < " Processing outputs ... " < < endl ;
2015-07-21 17:47:56 +02:00
symbol_table . writeJuliaOutput ( jlOutputFile ) ;
2015-07-27 15:33:38 +02:00
if ( dynamic_model . equation_number ( ) > 0 )
2015-07-27 17:02:51 +02:00
{
2015-07-29 14:59:09 +02:00
dynamic_model . writeOutput ( jlOutputFile , basename , false , false , false ,
mod_file_struct . order_option ,
mod_file_struct . estimation_present , true ) ;
2015-07-27 17:02:51 +02:00
if ( ! no_static )
2015-07-28 12:29:42 +02:00
{
static_model . writeStaticFile ( basename , false , false , false , true ) ;
static_model . writeParamsDerivativesFile ( basename , true ) ;
}
2015-07-28 14:59:55 +02:00
dynamic_model . writeDynamicFile ( basename , block , byte_code , use_dll ,
mod_file_struct . order_option , true ) ;
dynamic_model . writeParamsDerivativesFile ( basename , true ) ;
2015-07-27 17:02:51 +02:00
}
2015-07-28 17:27:56 +02:00
steady_state_model . writeSteadyStateFile ( basename , mod_file_struct . ramsey_model_present , true ) ;
2015-07-27 15:33:38 +02:00
2015-08-19 11:28:25 +02:00
jlOutputFile < < " model.static = " < < basename < < " Static.static! " < < endl
< < " model.dynamic = " < < basename < < " Dynamic.dynamic! " < < endl
< < " model.steady_state = " < < basename < < " SteadyState2.steady_state! " < < endl
2015-07-28 17:32:34 +02:00
< < " try " < < endl
< < " using " < < basename < < " StaticParamsDerivs " < < endl
2015-07-29 11:35:10 +02:00
< < " model.static_params_derivs = " < < basename
2015-07-28 17:48:42 +02:00
< < " StaticParamsDerivs.params_derivs " < < endl
2015-07-28 17:32:34 +02:00
< < " catch " < < endl
< < " end " < < endl
< < " try " < < endl
< < " using " < < basename < < " DynamicParamsDerivs " < < endl
2015-07-29 11:35:10 +02:00
< < " model.dynamic_params_derivs = " < < basename
2015-07-28 17:48:42 +02:00
< < " DynamicParamsDerivs.params_derivs " < < endl
2015-07-28 17:32:34 +02:00
< < " catch " < < endl
< < " end " < < endl
2015-07-27 15:59:13 +02:00
< < " end " < < endl ;
2015-07-21 17:47:56 +02:00
jlOutputFile . close ( ) ;
2015-07-27 15:33:38 +02:00
cout < < " done " < < endl ;
2015-07-21 17:47:56 +02:00
}