From 3a5a395e8cfdacc2034bc66b912dcf2313f8834d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 10 Apr 2014 11:43:26 +0200
Subject: [PATCH] Bugfix: M_.sigma_e_is_diagonal could be wrong if there were
 several shocks blocks.

(manually cherry picked from commit df0db38b3ca812dacd7d9c68b5abdc1fc5fa848c)
---
 preprocessor/ModFile.cc | 3 +++
 preprocessor/Shocks.cc  | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index ac3fd136eb..c78702c005 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -588,6 +588,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
     mOutputFile << "M_.H = 0;" << endl
                 << "M_.Correlation_matrix_ME = 1;" << endl;
 
+  // May be later modified by a shocks block
+  mOutputFile << "M_.sigma_e_is_diagonal = 1;" << endl;
+
   if (linear == 1)
     mOutputFile << "options_.linear = 1;" << endl;
 
diff --git a/preprocessor/Shocks.cc b/preprocessor/Shocks.cc
index 73032fbb54..a6cdf5fdc7 100644
--- a/preprocessor/Shocks.cc
+++ b/preprocessor/Shocks.cc
@@ -99,10 +99,12 @@ ShocksStatement::writeOutput(ostream &output, const string &basename) const
   writeDetShocks(output);
   writeVarAndStdShocks(output);
   writeCovarAndCorrShocks(output);
+
+  /* M_.sigma_e_is_diagonal is initialized to 1 by ModFile.cc. If there are no
+     off-diagonal elements, then we don't reset it to 1, since there might be
+     previous shocks blocks with off-diagonal elements. */
   if (covar_shocks.size()+corr_shocks.size() > 0)
     output << "M_.sigma_e_is_diagonal = 0;" << endl;
-  else
-    output << "M_.sigma_e_is_diagonal = 1;" << endl;
 }
 
 void
-- 
GitLab