From 90d8b579cc31bc325eeea4357576af933a9f3135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Thu, 26 Jul 2018 17:39:55 +0200 Subject: [PATCH] Fix incorrect recursive ordering of aux vars with diff not used at current period Ensure that all diff operators appear once with their argument at current period (i.e. maxLag=0). If it is not the case, generate the corresponding expressions. This is necessary to avoid lags of more than one in the auxiliary equation, which would then be modified by subsequent transformations (removing lags > 1), which in turn would break the recursive ordering of auxiliary equations. See McModelTeam/McModelProject/issues/95 for an example. --- src/DynamicModel.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 70d3485f..56314494 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -5445,6 +5445,26 @@ DynamicModel::substituteDiff(StaticModel &static_model, ExprNode::subst_table_t for (const auto & equation : equations) equation->findDiffNodes(static_model, diff_table); + /* Ensure that all diff operators appear once with their argument at current + period (i.e. maxLag=0). + If it is not the case, generate the corresponding expressions. + This is necessary to avoid lags of more than one in the auxiliary + equation, which would then be modified by subsequent transformations + (removing lags > 1), which in turn would break the recursive ordering + of auxiliary equations. See McModelTeam/McModelProject/issues/95 */ + for (auto &it : diff_table) + { + auto iterator_arg_max_lag = it.second.rbegin(); + int arg_max_lag = iterator_arg_max_lag->first; + expr_t arg_max_expr = iterator_arg_max_lag->second; + while (arg_max_lag < 0) + { + arg_max_lag++; + arg_max_expr = arg_max_expr->decreaseLeadsLags(-1); + it.second[arg_max_lag] = arg_max_expr; + } + } + // Substitute in model local variables vector<BinaryOpNode *> neweqs; for (auto & it : local_variables_table) -- GitLab