From 6076ce84ef63c7411ed619c8c737f4558a984839 Mon Sep 17 00:00:00 2001
From: KyleKlenk <kyle.c.klenk@gmail.com>
Date: Tue, 4 Apr 2023 09:51:55 -0600
Subject: [PATCH] Fix HRU restart feature:

HRU can now restart with timestep halved.
---
 .gitignore                                    |  1 +
 .../file_access_actor/output_container.hpp    |  2 ++
 .../cpp_code/output_container.cpp             | 20 +++++++++++++++----
 .../writeOutputFromOutputStructure.f90        |  2 +-
 .../actors/hru_actor/cpp_code/hru_actor.cpp   |  1 +
 build/source/dshare/var_lookup.f90            |  4 ++--
 build/source/engine/checkStruc.f90            |  5 ++++-
 build/summa_old                               |  1 -
 8 files changed, 27 insertions(+), 9 deletions(-)
 delete mode 160000 build/summa_old

diff --git a/.gitignore b/.gitignore
index c2dafa7..da41099 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,3 +24,4 @@ bin/plot_resuduial.py
 bin/state.png
 bin/submission_script_array.sh
 build/source/testing/containers/output_container/out.txt
+build/summa_old
diff --git a/build/includes/file_access_actor/output_container.hpp b/build/includes/file_access_actor/output_container.hpp
index 4f0873e..ee64191 100644
--- a/build/includes/file_access_actor/output_container.hpp
+++ b/build/includes/file_access_actor/output_container.hpp
@@ -17,7 +17,9 @@
 class Output_Partition {
   private:
     int start_local_gru_index; // The index of the first GRU in the partition
+    int end_local_gru_index; // The index of the last GRU in the partition
     int num_local_grus; // The number of GRUs in the partition
+    int num_active_grus; // The number of GRUs that have not failed
     int num_timesteps_simulation; // The number of timesteps in the simulation
     int num_stored_timesteps; // The number of timesteps held within the partition
 
diff --git a/build/source/actors/file_access_actor/cpp_code/output_container.cpp b/build/source/actors/file_access_actor/cpp_code/output_container.cpp
index ba2a68a..d02e45f 100644
--- a/build/source/actors/file_access_actor/cpp_code/output_container.cpp
+++ b/build/source/actors/file_access_actor/cpp_code/output_container.cpp
@@ -9,6 +9,8 @@ Output_Partition::Output_Partition(int start_local_gru_index, int num_local_grus
     this->num_local_grus = num_local_grus;
     this->num_timesteps_simulation = num_timesteps_simulation;
     this->num_stored_timesteps = num_stored_timesteps;
+    this->end_local_gru_index = start_local_gru_index + num_local_grus - 1;
+    this->num_active_grus = num_local_grus;
 
 }
 
@@ -21,12 +23,11 @@ void Output_Partition::setGRUReadyToWrite(caf::actor gru_actor) {
 }
 
 bool Output_Partition::isReadyToWrite() {
-    return (this->ready_to_write_list.size() + this->failed_gru_index_list.size()) 
-        == this->num_local_grus;
+    return this->ready_to_write_list.size() == this->num_active_grus;
 }
 
 int Output_Partition::getMaxGRUIndex() {
-    return this->start_local_gru_index + this->num_local_grus - 1;
+    return this->end_local_gru_index;
 }
 
 int Output_Partition::getNumStoredTimesteps() {
@@ -58,11 +59,22 @@ void Output_Partition::resetReadyToWriteList() {
 }
 
 void Output_Partition::addFailedGRUIndex(int local_gru_index) {
+
+    // Special case where the failing GRU is the last or first GRU in the partition
+    // This will affect writing of output if a failed GRU is the last or first GRU
+    if (local_gru_index == this->end_local_gru_index) {
+        this->end_local_gru_index -= 1;
+    } else if (local_gru_index == this->start_local_gru_index) {
+        this->start_local_gru_index += 1;
+    }
+
+    this->num_active_grus -= 1;
+
     this->failed_gru_index_list.push_back(local_gru_index);
 }
 
 int Output_Partition::getNumActiveGRUs() {
-    return this->num_local_grus - this->failed_gru_index_list.size();
+    return this->num_active_grus;
 }
 
 int Output_Partition::getNumLocalGRUs() {
diff --git a/build/source/actors/file_access_actor/fortran_code/writeOutputFromOutputStructure.f90 b/build/source/actors/file_access_actor/fortran_code/writeOutputFromOutputStructure.f90
index 42380d2..484e90e 100644
--- a/build/source/actors/file_access_actor/fortran_code/writeOutputFromOutputStructure.f90
+++ b/build/source/actors/file_access_actor/fortran_code/writeOutputFromOutputStructure.f90
@@ -407,9 +407,9 @@ subroutine writeScalar(ncid, outputTimestep, outputTimestepUpdate, nSteps, minGR
       if (outputTimeStepUpdate(iFreq) /= stepCounter ) then
         print*, "ERROR Missmatch in Steps - stat doubleVec"
         print*, "   outputTimeStepUpdate(iFreq) = ", outputTimeStepUpdate(iFreq)
-        print*, "   outputTimeStepUpdate", outputTimeStepUpdate
         print*, "   stepCounter = ", stepCounter
         print*, "   iFreq = ", iFreq
+
         return
       endif
     class default; err=20; message=trim(message)//'stats must be scalarv and of type gru_hru_doubleVec'; return
diff --git a/build/source/actors/hru_actor/cpp_code/hru_actor.cpp b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp
index b9ad0d1..89108d4 100644
--- a/build/source/actors/hru_actor/cpp_code/hru_actor.cpp
+++ b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp
@@ -115,6 +115,7 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
                 self->state.num_steps_until_write--;
 
                 err = Run_HRU(self); // Simulate a Timestep
+
                 if (err != 0) {
                     aout(self) << "Error: HRU_Actor - Run_HRU - HRU = " << self->state.indxHRU << 
                         " - indxGRU = " << self->state.indxGRU << " - refGRU = "<< self->state.refGRU << std::endl;
diff --git a/build/source/dshare/var_lookup.f90 b/build/source/dshare/var_lookup.f90
index 4b430ca..3a530ad 100755
--- a/build/source/dshare/var_lookup.f90
+++ b/build/source/dshare/var_lookup.f90
@@ -438,7 +438,7 @@ MODULE var_lookup
   integer(i4b)    :: scalarVolLatHt_fus              = integerMissing ! volumetric latent heat of fusion     (J m-3)
   ! number of function evaluations
   integer(i4b)    :: numFluxCalls                    = integerMissing ! number of flux calls (-)
-  integer(i4b)    :: wallClockTime                   = integerMissing ! wall clock time (s)
+!   integer(i4b)    :: wallClockTime                   = integerMissing ! wall clock time (s)
  endtype iLook_diag
 
  ! ***********************************************************************************************************
@@ -823,7 +823,7 @@ MODULE var_lookup
                                                                          51, 52, 53, 54, 55, 56, 57, 58, 59, 60,&
                                                                          61, 62, 63, 64, 65, 66, 67, 68, 69, 70,&
                                                                          71, 72, 73, 74, 75, 76, 77, 78, 79, 80,&
-                                                                         81, 82, 83, 84)
+                                                                         81, 82, 83)
  ! named variables: model fluxes
  type(iLook_flux),    public,parameter :: iLookFLUX     =iLook_flux    (  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,&
                                                                          11, 12, 13, 14, 15, 16, 17, 18, 19, 20,&
diff --git a/build/source/engine/checkStruc.f90 b/build/source/engine/checkStruc.f90
index 772f448..d048e2d 100755
--- a/build/source/engine/checkStruc.f90
+++ b/build/source/engine/checkStruc.f90
@@ -89,7 +89,10 @@ contains
   ! check that the length of the lookup structure matches the number of variables in the data structure
   call split_line(longString,words,err,cmessage) ! convert the long character string to a vector of "words"
   if(err/=0)then; message=trim(message)//trim(cmessage); return; end if
-  if(size(words)/=structInfo(iStruct)%nVar)then; err=20; message=trim(message)//'unexpected number of elements'; return; end if
+  if(size(words)/=structInfo(iStruct)%nVar)then; err=20; message=trim(message)//'unexpected number of elements'; 
+    print*, 'size(words) = ', size(words)
+    print*, 'structInfo(iStruct)%nVar = ', structInfo(iStruct)%nVar
+  return; end if
   ! check that the elements in the lookup structure are sequential integers (1,2,3,...,n)
   do ix=1,structInfo(iStruct)%nVar
    read(words(ix),*) ixTest  ! convert character to integer; store in ixTest
diff --git a/build/summa_old b/build/summa_old
deleted file mode 160000
index 3640d3a..0000000
--- a/build/summa_old
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 3640d3a30a438396480f8f115a51139bc4d8708e
-- 
GitLab