From 6a31ba4b626f036bfba862642775b7b61c78c364 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 3 Dec 2021 15:48:22 +0100
Subject: [PATCH] PAC: minor simplification in the matching of the growth
 expression

Simple variable nodes are already correctly matched by
ExprNode::matchLinearCombinationOfVariables().

Also improve the related documentation.
---
 src/ExprNode.hh |  4 +++-
 src/SubModel.cc | 10 ++--------
 src/SubModel.hh |  3 +++
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/ExprNode.hh b/src/ExprNode.hh
index 83f32744..17bf0405 100644
--- a/src/ExprNode.hh
+++ b/src/ExprNode.hh
@@ -632,7 +632,9 @@ public:
     parameter in a term, param_id == -1.
     Can throw a MatchFailureException.
     if `variable_obligatory_in_each_term` is true, then every part of the linear combination must contain a variable;
-    otherwise, if `variable_obligatory_in_each_term`, then any linear combination of constant/variable/param is matched
+    otherwise, if `variable_obligatory_in_each_term` is false, then any linear
+    combination of constant/variable/param is matched (and variable_id == -1
+    for terms without a variable).
   */
   vector<tuple<int, int, int, double>> matchLinearCombinationOfVariables(bool variable_obligatory_in_each_term = true) const;
 
diff --git a/src/SubModel.cc b/src/SubModel.cc
index dbfaf0e1..28b09016 100644
--- a/src/SubModel.cc
+++ b/src/SubModel.cc
@@ -830,14 +830,8 @@ PacModelTable::transformPass(ExprNode::subst_table_t &diff_subst_table,
           }
         catch (ExprNode::MatchFailureException &e)
           {
-            auto gv = dynamic_cast<const VariableNode *>(growth[name]);
-            if (gv)
-              growth_info[name].emplace_back(gv->symb_id, gv->lag, -1, 1);
-            else
-              {
-                cerr << "Pac growth must be a linear combination of variables" << endl;
-                exit(EXIT_FAILURE);
-              }
+            cerr << "ERROR: PAC growth must be a linear combination of variables" << endl;
+            exit(EXIT_FAILURE);
           }
 
       // Collect some information about PAC models
diff --git a/src/SubModel.hh b/src/SubModel.hh
index f3a7921e..d45f29c7 100644
--- a/src/SubModel.hh
+++ b/src/SubModel.hh
@@ -199,6 +199,9 @@ private:
   map<string, string> discount;
   // The growth expressions belong to the main dynamic_model from the ModFile instance
   map<string, expr_t> growth, original_growth;
+  /* Information about the structure of growth expressions (which must be a
+     linear combination of variables).
+     Each tuple represents a term: (endo_id, lag, param_id, constant) */
   map<string, vector<tuple<int, int, int, double>>> growth_info;
 
   /* Stores the name of the PAC equation associated to the model.
-- 
GitLab