From cb67e9ed70e31ce3d5c17b7bfdcd6887df711fb1 Mon Sep 17 00:00:00 2001
From: KyleKlenk <kyle.c.klenk@gmail.com>
Date: Thu, 24 Mar 2022 09:50:48 -0600
Subject: [PATCH] Added configuration file input to HRUActor

---
 build/source/actors/HRU.h         | 12 ++++++++
 build/source/actors/HRUActor.h    | 49 +++++++++++++++++++++++++------
 build/source/actors/JobActor.h    |  6 ++--
 config/Summa_Actors_Settings.json |  5 ++++
 4 files changed, 61 insertions(+), 11 deletions(-)

diff --git a/build/source/actors/HRU.h b/build/source/actors/HRU.h
index dfd48c3..6a7ac6e 100644
--- a/build/source/actors/HRU.h
+++ b/build/source/actors/HRU.h
@@ -12,6 +12,8 @@
 #include <sys/resource.h>
 #include <chrono>
 #include <iostream>
+#include "json.hpp"
+
 using namespace caf;
 
 struct hru_state {
@@ -76,6 +78,9 @@ struct hru_state {
     int         forcingStep = 1;    // index of current time step in current forcing file
     int         iFile = 1;          // index of current forcing file from forcing file list
     int         dt_init_factor = 1; // factor of dt_init (coupled_em)
+    bool        printOutput;
+    int         outputFrequency;
+
 
     // Julian Day variables
     double      fracJulDay;
@@ -101,6 +106,13 @@ struct hru_state {
 
 };
 
+/**
+ * @brief Get the settings from the settings JSON file
+ * 
+ * @param self Actor State
+ * @param configPath Path to the directory that contains the settings file
+ */
+void parseSettings(stateful_actor<hru_state>* self, std::string configPath);
 /**
  Function to initalize the HRU for running
  */
diff --git a/build/source/actors/HRUActor.h b/build/source/actors/HRUActor.h
index a1ed5d5..04f015a 100644
--- a/build/source/actors/HRUActor.h
+++ b/build/source/actors/HRUActor.h
@@ -2,6 +2,8 @@
 #define HRUActor_H_
 
 #include "HRU.h"
+using json = nlohmann::json;
+
 
 /**
  * @brief HRU Actor is reponsible for carrying out the computation component of SUMMA
@@ -12,7 +14,8 @@
  * @param parent 
  * @return behavior 
  */
-behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, 
+behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
+    std::string configPath,
     caf::actor file_access_actor, int outputStrucSize, caf::actor parent) {
     // Timing Information
     self->state.start = std::chrono::high_resolution_clock::now();
@@ -31,7 +34,7 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
     // OutputStructure Size (how many timesteps we can compute before we need to write)
     self->state.outputStrucSize = outputStrucSize;
 
-    // init counters 
+    // initialize counters 
     self->state.timestep   = 1;
     self->state.outputStep = 1;
 
@@ -154,6 +157,41 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
      *********************************************************************************************************/
 }
 
+void parseSettings(stateful_actor<hru_state>* self, std::string configPath) {
+    json settings;
+    std::string SummaActorsSettings = "/Summa_Actors_Settings.json";
+    std::ifstream settings_file(configPath + SummaActorsSettings);
+    settings_file >> settings;
+    settings_file.close();
+
+    if (settings.find("HRUActor") != settings.end()) {
+        json HRUActorConfig = settings["HRUActor"];
+        // find if we want to print output to stdout
+        if (HRUActorConfig.find("printOutput") != HRUActorConfig.end()) {
+            self->state.printOutput = HRUActorConfig["printOutput"];
+        } else {
+            aout(self) << "Error finding printOutput in JSON File - Reverting to default value\n";
+            self->state.printOutput = true;
+        }
+
+        if (self->state.printOutput) {
+            // get the frequency in number of timesteps we want to print the output
+            if(HRUActorConfig.find("outputFrequency") != settings.end()) {
+                self->state.outputFrequency = HRUActorConfig["outputFrequency"];
+            } else {
+                aout(self) << "Error finding outputFrequency in JSON File - Reverting to default value\n";
+                self->state.outputFrequency = 10000;
+            }
+        }
+    } else {
+        aout(self) << "Error finding HRUActor in JSON File - Reverting to default values for HRUs\n";
+        self->state.printOutput = true;
+        self->state.outputFrequency = 10000;
+    }
+
+
+}
+
 void Initialize_HRU(stateful_actor<hru_state>* self) {
     self->state.initStart = std::chrono::high_resolution_clock::now();
     // aout(self) << "Initalizing HRU" << std::endl;
@@ -376,13 +414,6 @@ void finalizeTimeVars(stateful_actor<hru_state>* self) {
     self->state.end = std::chrono::high_resolution_clock::now();
     self->state.duration += std::chrono::duration_cast<std::chrono::seconds>
         (self->state.end - self->state.start).count();
-
-    // // Output the timing information
-    // aout(self) << "DONE:" << self->state.refGRU << ":duration = " << self->state.duration << std::endl;
-    // aout(self) << "DONE:" << self->state.refGRU << ":initDuration = " << self->state.initDuration << std::endl;
-    // aout(self) << "DONE:" << self->state.refGRU << ":forcingDuration = " << self->state.forcingDuration.count() << std::endl;
-    // aout(self) << "DONE:" << self->state.refGRU << ":runPhysicsDuration = " << self->state.runPhysicsDuration.count() << std::endl;
-    // aout(self) << "DONE:" << self->state.refGRU << ":writeOutputDuration = " << self->state.writeOutputDuration.count() << std::endl;
 }
 
 void deallocateHRUStructures(stateful_actor<hru_state>* self) {
diff --git a/build/source/actors/JobActor.h b/build/source/actors/JobActor.h
index f2d9e06..d1583af 100644
--- a/build/source/actors/JobActor.h
+++ b/build/source/actors/JobActor.h
@@ -212,7 +212,8 @@ void initJob(stateful_actor<job_state>* self) {
 void initalizeGRU(stateful_actor<job_state>* self) {
     int startGRU = self->state.GRUList.size() + self->state.startGRU;
     int indexGRU = self->state.GRUList.size() + 1; // Fortran reference starts at 1
-    auto gru = self->spawn(hru_actor, startGRU, indexGRU, self->state.file_access_actor, 
+    auto gru = self->spawn(hru_actor, startGRU, indexGRU, 
+        self->state.configPath, self->state.file_access_actor, 
         self->state.outputStrucSize, self);
     self->state.GRUList.push_back(new GRUinfo(startGRU, indexGRU, gru, 
         self->state.dt_init_start_factor, self->state.maxRunAttempts));
@@ -235,7 +236,8 @@ void restartFailures(stateful_actor<job_state>* self) {
             gru->updateFailed();
             self->send(self->state.file_access_actor, reset_outputCounter_v, gru->getIndxGRU());
             gru->updateDt_init();
-            auto newGRU = self->spawn(hru_actor, gru->getRefGRU(), gru->getIndxGRU(), self->state.file_access_actor, 
+            auto newGRU = self->spawn(hru_actor, gru->getRefGRU(), gru->getIndxGRU(), 
+                self->state.configPath,self->state.file_access_actor, 
                 self->state.outputStrucSize, self);
             gru->updateGRU(newGRU);
             gru->updateCurrentAttempt();
diff --git a/config/Summa_Actors_Settings.json b/config/Summa_Actors_Settings.json
index 1c21859..53358e7 100644
--- a/config/Summa_Actors_Settings.json
+++ b/config/Summa_Actors_Settings.json
@@ -8,5 +8,10 @@
         "FileManagerPath": "/project/gwf/gwf_cmt/kck540/domain_NorthAmerica/settings/SUMMA/fileManager.txt",
         "outputCSV": false,
         "csvPath": ""
+    },
+
+    "HRUActor": {
+        "printOutput": true,
+        "outputFrequency": 25000
     }
 }
\ No newline at end of file
-- 
GitLab