From 22fbfb0fbae569177fb4702352ce7110fa4b0f96 Mon Sep 17 00:00:00 2001
From: normann <normann@dynare.org>
Date: Thu, 22 Apr 2021 14:52:29 +0200
Subject: [PATCH] Allows to write M_.NNZDerivatives_objective in the driver.m
 file Writes the number of non-zero derivatives of the planner objective
 function NNZDerivatives_objective in the driver.m file. Useful for the
 k-order approximations of welfare.

---
 src/ComputingTasks.cc |  4 ++++
 src/ModelTree.hh      | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc
index 00f31006..8616766f 100644
--- a/src/ComputingTasks.cc
+++ b/src/ComputingTasks.cc
@@ -2175,6 +2175,10 @@ PlannerObjectiveStatement::computingPass()
 void
 PlannerObjectiveStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
+  output << "M_.NNZDerivatives_objective = [";
+  for (int i=1; i < static_cast<int>(model_tree.getNNZDerivatives().size()); i++)
+    output << (i > model_tree.getComputedDerivsOrder() ? -1 : model_tree.getNNZDerivatives()[i]) << ";";
+  output << "];";
   model_tree.writeStaticFile(basename + ".objective", false, false, false, "", {}, {}, false);
 }
 
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index e075e612..6cfdfcf6 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -455,6 +455,20 @@ public:
     return equation_tags.getTagsByEqn(eq);
   }
 
+  //! Returns the vector of non-zero derivative counts
+  inline const vector<int> &
+  getNNZDerivatives() const
+  {
+    return NNZDerivatives;
+  }
+
+  //!Returns the maximum order of computed derivatives
+  inline int
+  getComputedDerivsOrder() const
+  {
+    return computed_derivs_order;
+  }
+
   inline static string
   BlockSim(BlockSimulationType type)
   {
-- 
GitLab