From 4ae6df494d3793f8486ac84cbf3fa9e70bf43f31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 26 Sep 2022 13:07:18 +0200
Subject: [PATCH] JSON: add dynamic_tmp_nbr and static_tmp_nbr
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As a consequence, remove the “tmp_nbr” variable from the Julia modules, since
it is now redundant.
---
 src/DynamicModel.cc | 22 ++++++++++------------
 src/StaticModel.cc  | 21 ++++++++++-----------
 2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index ff55ca34..a8eac146 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -735,7 +735,7 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const
          << "#     from " << basename << ".mod" << endl
          << "#" << endl
          << "using StatsFuns" << endl << endl
-         << "export tmp_nbr, dynamic!, dynamicResid!, dynamicG1!, dynamicG2!, dynamicG3!" << endl << endl
+         << "export dynamic!, dynamicResid!, dynamicG1!, dynamicG2!, dynamicG3!" << endl << endl
          << "#=" << endl
          << "# The comments below apply to all functions contained in this module #" << endl
          << "  NB: The arguments contained on the first line of the function" << endl
@@ -747,9 +747,6 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const
          << "  dynamicG1!    : Computes the dynamic model Jacobian" << endl
          << "  dynamicG2!    : Computes the dynamic model Hessian" << endl
          << "  dynamicG3!    : Computes the dynamic model third derivatives" << endl << endl
-         << "## Exported Variables ##" << endl
-         << "  tmp_nbr       : Vector{Int}(4) respectively the number of temporary variables" << endl
-         << "                  for the residuals, g1, g2 and g3." << endl << endl
          << "## Local Functions ##" << endl
          << "  dynamicResidTT! : Computes the dynamic model temporary terms for the residuals" << endl
          << "  dynamicG1TT!    : Computes the dynamic model temporary terms for the Jacobian" << endl
@@ -781,13 +778,6 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const
          << "      residuals, the jacobian and hessian matrices, then `T` must have at least `sum(tmp_nbr[1:3])` elements." << endl
          << "=#" << endl << endl;
 
-  // Write the number of temporary terms
-  output << "tmp_nbr = zeros(Int,4)" << endl
-         << "tmp_nbr[1] = " << temporary_terms_derivatives[0].size() << "# Number of temporary terms for the residuals" << endl
-         << "tmp_nbr[2] = " << temporary_terms_derivatives[1].size() << "# Number of temporary terms for g1 (jacobian)" << endl
-         << "tmp_nbr[3] = " << temporary_terms_derivatives[2].size() << "# Number of temporary terms for g2 (hessian)" << endl
-         << "tmp_nbr[4] = " << temporary_terms_derivatives[3].size() << "# Number of temporary terms for g3 (third order derivates)" << endl << endl;
-
   // dynamicResidTT!
   output << "function dynamicResidTT!(T::Vector{<: Real}," << endl
          << "                         y::Vector{<: Real}, x::Matrix{<: Real}, "
@@ -4664,7 +4654,15 @@ DynamicModel::writeJsonOutput(ostream &output) const
   writeJsonAST(output);
   output << ", ";
   writeJsonVariableMapping(output);
-  output << ", ";
+  output << R"(, "dynamic_tmp_nbr": [)";
+  for (bool printed_something {false};
+       const auto &tts : temporary_terms_derivatives)
+    {
+      if (exchange(printed_something, true))
+        output << ", ";
+      output << tts.size();
+    }
+  output << "], ";
   writeJsonSparseIndicesHelper<true>(output);
 }
 
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 83da54b9..b9faf9d0 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -622,7 +622,7 @@ StaticModel::writeStaticJuliaFile(const string &basename) const
          << "#     from " << basename << ".mod" << endl
          << "#" << endl
          << "using StatsFuns" << endl << endl
-         << "export tmp_nbr, static!, staticResid!, staticG1!, staticG2!, staticG3!" << endl << endl
+         << "export static!, staticResid!, staticG1!, staticG2!, staticG3!" << endl << endl
          << "#=" << endl
          << "# The comments below apply to all functions contained in this module #" << endl
          << "  NB: The arguments contained on the first line of the function" << endl
@@ -634,9 +634,6 @@ StaticModel::writeStaticJuliaFile(const string &basename) const
          << "  staticG1!    : Computes the static model Jacobian" << endl
          << "  staticG2!    : Computes the static model Hessian" << endl
          << "  staticG3!    : Computes the static model third derivatives" << endl << endl
-         << "## Exported Variables ##" << endl
-         << "  tmp_nbr      : Vector{Int}(4) respectively the number of temporary variables" << endl
-         << "                 for the residuals, g1, g2 and g3." << endl << endl
          << "## Local Functions ##" << endl
          << "  staticResidTT! : Computes the static model temporary terms for the residuals" << endl
          << "  staticG1TT!    : Computes the static model temporary terms for the Jacobian" << endl
@@ -667,13 +664,6 @@ StaticModel::writeStaticJuliaFile(const string &basename) const
          << "      residuals, and the jacobian and hessian matrices, then `T` must have at least `sum(tmp_nbr[1:3])` elements." << endl
          << "=#" << endl << endl;
 
-  // Write the number of temporary terms
-  output << "tmp_nbr = zeros(Int,4)" << endl
-         << "tmp_nbr[1] = " << temporary_terms_derivatives[0].size() << "# Number of temporary terms for the residuals" << endl
-         << "tmp_nbr[2] = " << temporary_terms_derivatives[1].size() << "# Number of temporary terms for g1 (jacobian)" << endl
-         << "tmp_nbr[3] = " << temporary_terms_derivatives[2].size() << "# Number of temporary terms for g2 (hessian)" << endl
-         << "tmp_nbr[4] = " << temporary_terms_derivatives[3].size() << "# Number of temporary terms for g3 (third order derivates)" << endl << endl;
-
   // staticResidTT!
   output << "function staticResidTT!(T::Vector{<: Real}," << endl
          << "                        y::Vector{<: Real}, x::Vector{<: Real}, params::Vector{<: Real})" << endl
@@ -1310,6 +1300,15 @@ StaticModel::writeJsonAuxVarRecursiveDefinitions(ostream &output) const
 void
 StaticModel::writeJsonOutput(ostream &output) const
 {
+  output << R"("static_tmp_nbr": [)";
+  for (bool printed_something {false};
+       const auto &tts : temporary_terms_derivatives)
+    {
+      if (exchange(printed_something, true))
+        output << ", ";
+      output << tts.size();
+    }
+  output << "], ";
   writeJsonSparseIndicesHelper<false>(output);
 }
 
-- 
GitLab