From d8bd315f0eef6e3dc719e91e59e452ea98b505ff Mon Sep 17 00:00:00 2001
From: KyleKlenk <kyle.c.klenk@gmail.com>
Date: Thu, 1 Dec 2022 14:28:18 -0600
Subject: [PATCH] fixed issue with the forcing step not being correct

---
 bin/submission_one.sh                         |  4 +-
 .../cpp_code/file_access_actor.cpp            | 68 ++-----------------
 .../fortran_code/write_to_netcdf.f90          | 41 +++++++++++
 .../actors/hru_actor/cpp_code/hru_actor.cpp   | 31 +++++++--
 build/summa                                   |  1 +
 laugh_tests                                   |  1 +
 6 files changed, 76 insertions(+), 70 deletions(-)
 create mode 160000 build/summa
 create mode 160000 laugh_tests

diff --git a/bin/submission_one.sh b/bin/submission_one.sh
index eaa562f..62d4570 100755
--- a/bin/submission_one.sh
+++ b/bin/submission_one.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 #SBATCH --cpus-per-task=1
 #SBATCH --time=01:00:00
-#SBATCH --mem=1G
+#SBATCH --mem=4G
 #SBATCH --job-name=Summa-Actors
 #SBATCH --mail-user=kyle.klenk@usask.ca
 #SBATCH --mail-type=ALL
@@ -10,4 +10,4 @@
 
 /globalhome/kck540/HPC/Summa-Projects/Summa-Actors/bin/summaMain -g 1 -n 10 -c /project/gwf/gwf_cmt/kck540/domain_NorthAmerica/summa_actors_input/Summa_Actors_Settings.json
 
-/globalhome/kck540/HPC/Summa-Projects/Summa-Actors/build/summa/bin/summa.exe -g 1 10 -m /project/gwf/gwf_cmt/kck540/domain_NorthAmerica/summa_actors_input/fileManager.txt
\ No newline at end of file
+# /globalhome/kck540/HPC/Summa-Projects/Summa-Actors/build/summa/bin/summa.exe -g 1 10 -m /project/gwf/gwf_cmt/kck540/domain_NorthAmerica/summa_actors_input/fileManager.txt
\ No newline at end of file
diff --git a/build/source/actors/file_access_actor/cpp_code/file_access_actor.cpp b/build/source/actors/file_access_actor/cpp_code/file_access_actor.cpp
index 71059a7..0971954 100644
--- a/build/source/actors/file_access_actor/cpp_code/file_access_actor.cpp
+++ b/build/source/actors/file_access_actor/cpp_code/file_access_actor.cpp
@@ -203,11 +203,11 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr
             set_var_dlength(diag_struct, hru_output->handle_diag_struct);
             set_var_dlength(flux_struct, hru_output->handle_flux_struct);
             // basin-average structures
-            set_var_d(bpar_struct, hru_output->handle_attr_struct);
+            set_var_d(bpar_struct, hru_output->handle_bpar_struct);
             set_var_dlength(bvar_struct, hru_output->handle_bvar_struct);
             // ancillary data structures
             set_var_d(dpar_struct, hru_output->handle_dpar_struct);
-            set_var_i(finalize_stats, hru_output->handle_finalize_stats);
+            set_flagVec(finalize_stats, hru_output->handle_finalize_stats);
             set_var_i(output_timestep, hru_output->handle_output_timestep);
 
             std::optional<int> partition_index;
@@ -215,38 +215,10 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr
             if (partition_index.has_value()) {
                 // We have a partition to write
                 std::vector<std::vector<std::shared_ptr<hru_output_handles>>> hru_output_from_vector = getOutputHandlesFromPartition(partition_index.value(), self->state.output_partitions); 
+                
                 for (int hru = 0; hru < self->state.output_partitions[partition_index.value()]->hru_info_and_data.size(); hru++) {
                     for(int timestep = 0; timestep < self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data.size(); timestep++) {
-                    //     writeBasinToNetCDF(self->state.handle_ncid, &self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->index_gru,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_finalize_stats, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_output_timestep, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_bvar_stat,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_bvar_struct, &err);
-                                
-                    //     writeTimeToNetCDF(self->state.handle_ncid,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_finalize_stats, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_output_timestep, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_time_struct, &err);
-                        
-                    //     writeDataToNetCDF(self->state.handle_ncid, &self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->index_gru,
-                    //         &self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->index_hru,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_finalize_stats, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_forc_stat, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_forc_struct,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_prog_stat, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_prog_struct, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_diag_stat, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_diag_struct, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_flux_stat, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_flux_struct,
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_indx_stat, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_indx_struct, 
-                    //         self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->output_data[timestep]->handle_output_timestep,
-                    //         &err);
-                    // }
-                                    
-                // for (int hru = 0; hru < hru_output_from_vector.size(); hru++) {
-                //     for (int timestep = 0; timestep < hru_output_from_vector[hru].size(); timestep++) {
+
                         writeBasinToNetCDF(self->state.handle_ncid, &self->state.output_partitions[partition_index.value()]->hru_info_and_data[hru]->index_gru,
                             hru_output_from_vector[hru][timestep]->handle_finalize_stats, 
                             hru_output_from_vector[hru][timestep]->handle_output_timestep, 
@@ -273,41 +245,9 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr
                             hru_output_from_vector[hru][timestep]->handle_indx_struct, 
                             hru_output_from_vector[hru][timestep]->handle_output_timestep,
                             &err);
-                    // }
                     }
                 }
                 
-                
-                // for (int hru = 0; hru < hru_output_from_vector.size(); hru++) {
-                //     for (int timestep = 0; timestep < hru_output_from_vector[hru].size(); timestep++) {
-                //         writeBasinToNetCDF(self->state.handle_ncid, &index_gru,
-                //             hru_output_from_vector[hru][timestep]->handle_finalize_stats, 
-                //             hru_output_from_vector[hru][timestep]->handle_output_timestep, 
-                //             hru_output_from_vector[hru][timestep]->handle_bvar_stat,
-                //             hru_output_from_vector[hru][timestep]->handle_bvar_struct, &err);
-
-                //         writeTimeToNetCDF(self->state.handle_ncid,
-                //             hru_output_from_vector[hru][timestep]->handle_finalize_stats, 
-                //             hru_output_from_vector[hru][timestep]->handle_output_timestep, 
-                //             hru_output_from_vector[hru][timestep]->handle_time_struct, &err);
-
-                //         writeDataToNetCDF(self->state.handle_ncid, &index_gru, &index_hru,
-                //             hru_output_from_vector[hru][timestep]->handle_finalize_stats, 
-                //             hru_output_from_vector[hru][timestep]->handle_forc_stat, 
-                //             hru_output_from_vector[hru][timestep]->handle_forc_struct,
-                //             hru_output_from_vector[hru][timestep]->handle_prog_stat, 
-                //             hru_output_from_vector[hru][timestep]->handle_prog_struct, 
-                //             hru_output_from_vector[hru][timestep]->handle_diag_stat, 
-                //             hru_output_from_vector[hru][timestep]->handle_diag_struct, 
-                //             hru_output_from_vector[hru][timestep]->handle_flux_stat, 
-                //             hru_output_from_vector[hru][timestep]->handle_flux_struct,
-                //             hru_output_from_vector[hru][timestep]->handle_indx_stat, 
-                //             hru_output_from_vector[hru][timestep]->handle_indx_struct, 
-                //             hru_output_from_vector[hru][timestep]->handle_output_timestep,
-                //             &err);
-                //     }
-                // }
-
                 clearOutputPartition(self->state.output_partitions[partition_index.value()]);
                 updateSimulationTimestepsRemaining(self->state.output_partitions[partition_index.value()]);
                 updateNumTimeForPartition(self->state.output_partitions[partition_index.value()]);                 
diff --git a/build/source/actors/file_access_actor/fortran_code/write_to_netcdf.f90 b/build/source/actors/file_access_actor/fortran_code/write_to_netcdf.f90
index da07bb1..7ab98ae 100644
--- a/build/source/actors/file_access_actor/fortran_code/write_to_netcdf.f90
+++ b/build/source/actors/file_access_actor/fortran_code/write_to_netcdf.f90
@@ -9,6 +9,42 @@ public::writeDataToNetCDF
 public::writeBasinToNetCDF
 public::writeTimeToNetCDF
 
+! type var_dlength_ptr
+!   type(var_dlength), pointer
+! type(var_dlength)     forc_stat_all_grus
+! type(var_dlength)     prog_stat_all_grus
+! type(var_dlength)     diag_stat_all_grus
+! type(var_dlength)     flux_stat_all_grus
+! type(var_dlength)     indx_stat_all_grus
+! type(var_dlength)     bvar_stat_all_grus
+
+! type(var_i)           time_struct
+! type(var_d)           forc_struct
+! type(var_d)           attr_struct
+! type(var_i)           type_struct
+! type(var_i8)          id_struct
+
+! type(var_ilength)     indx_struct
+! type(var_dlength)     mpar_struct
+! type(var_dlength)     prog_struct
+! type(var_dlength)     diag_struct
+! type(var_dlength)     flux_struct
+
+! type(var_d)           bpar_struct
+! type(var_dlength)     bvar_struct
+
+! type(var_d)           dpar_struct
+! type(flagVec)         finalize_stats
+! type(var_i)           output_timesteps_all_grus
+
+
+
+! type(flagVec),     pointer, dimension(:,:), allocatable, public :: finalize_stats_all_grus
+! type(var_i),       pointer, dimension(:,:), allocatable, public :: output_timesteps_all_grus
+! type(var_dlength), pointer, dimension(:,:), allocatable, public :: bvar_stats_all_grus
+! type(var_dlength), pointer, dimension(:,:), allocatable, public :: bvar_struct_all_grus
+! type(var_i),       pointer, dimension(:,:), allocatable, public :: time_struct_all_grus
+
 contains
 
 ! Subroutine that writes data from the HRU actor to be written to netcdf
@@ -195,6 +231,11 @@ subroutine writeDataToNetCDF(handle_ncid,          &
   end do  ! (looping through structures)
 end subroutine writeDataToNetCDF
 
+! subroutine setOutputStructure(index_gru, index_timestep, 
+!   handle_finalize_stats, handle_output_timestep, handle_output_timestep, )
+
+! end subroutine setOutputStructure
+
 subroutine writeBasinToNetCDF(handle_ncid, index_gru, handle_finalize_stats, &
   handle_output_timestep, handle_bvar_stat, handle_bvar_struct, err) bind(C, name="writeBasinToNetCDF")
   USE writeOutput_module,only:writeBasin
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 4fa9f0c..6221265 100644
--- a/build/source/actors/hru_actor/cpp_code/hru_actor.cpp
+++ b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp
@@ -121,6 +121,11 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
             }
 
             while(self->state.num_steps_until_write > 0) {
+                if (self->state.forcingStep > self->state.stepsInCurrentFFile) {
+                    self->send(self->state.file_access_actor, access_forcing_v, self->state.iFile+1, self);
+                    break;
+                }
+
                 self->state.num_steps_until_write--;
                 err = Run_HRU(self); // Simulate a Timestep
 
@@ -139,10 +144,6 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
                     break;
                 }
 
-                if (self->state.forcingStep > self->state.stepsInCurrentFFile) {
-                    self->send(self->state.file_access_actor, access_forcing_v, self->state.iFile+1, self);
-                    break;
-                }
             }
             
             self->state.hru_timing.updateEndPoint("total_duration");
@@ -155,6 +156,7 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU,
             self->state.iFile = iFile;
             self->state.stepsInCurrentFFile = num_forcing_steps_in_iFile;
             setTimeZoneOffset(&self->state.iFile, &self->state.tmZoneOffsetFracDay, &err);
+            self->state.forcingStep = 1;
             self->send(self, run_hru_v);
         },
 
@@ -248,12 +250,32 @@ int Run_HRU(stateful_actor<hru_state>* self) {
                    self->state.handle_forcStruct, 
                    &self->state.iFile,
                    &self->state.err);
+    if (self->state.err != 0) {
+        aout(self) << "Error: HRU_Actor - ReadForcingHRU - HRU = " << self->state.indxHRU <<
+        " - indxGRU = " << self->state.indxGRU << " - refGRU = " << self->state.refGRU << std::endl;
+        aout(self) << "Forcing Step = " << self->state.forcingStep << std::endl;
+        aout(self) << "Timestep = " << self->state.timestep << std::endl;
+        aout(self) << "iFile = " << self->state.iFile << std::endl;
+        aout(self) << "Steps in Forcing File = " << self->state.stepsInCurrentFFile << std::endl;
+        self->quit();
+        return -1;
+    }
 
     computeTimeForcingHRU(self->state.handle_timeStruct,
                           self->state.handle_forcStruct, 
                           &self->state.fracJulDay,
                           &self->state.yearLength,
                           &self->state.err);
+    if (self->state.err != 0) {
+        aout(self) << "Error: HRU_Actor - ComputeTimeForcingHRU - HRU = " << self->state.indxHRU <<
+        " - indxGRU = " << self->state.indxGRU << " - refGRU = " << self->state.refGRU << std::endl;
+        aout(self) << "Forcing Step = " << self->state.forcingStep << std::endl;
+        aout(self) << "Timestep = " << self->state.timestep << std::endl;
+        aout(self) << "iFile = " << self->state.iFile << std::endl;
+        aout(self) << "Steps in Forcing File = " << self->state.stepsInCurrentFFile << std::endl;
+        self->quit();
+        return -1;
+    }
 
     if (self->state.err != 0) { 
         aout(self) << "*********************************************************\n";
@@ -304,6 +326,7 @@ int Run_HRU(stateful_actor<hru_state>* self) {
         aout(self) << "Error: RunPhysics - HRU = " << self->state.indxHRU << 
             " - indxGRU = " << self->state.indxGRU << " - refGRU = " << self->state.refGRU <<
             " - Timestep = " << self->state.timestep << std::endl;
+        self->quit();
         return 20;
     }
     self->state.hru_timing.updateEndPoint("run_physics_duration");
diff --git a/build/summa b/build/summa
new file mode 160000
index 0000000..fa9adf8
--- /dev/null
+++ b/build/summa
@@ -0,0 +1 @@
+Subproject commit fa9adf808229a45085defdc2bb8ef05836b9b3aa
diff --git a/laugh_tests b/laugh_tests
new file mode 160000
index 0000000..a1dfea1
--- /dev/null
+++ b/laugh_tests
@@ -0,0 +1 @@
+Subproject commit a1dfea1dfbcd128b6ee666cbe0ddbc2785b43fb4
-- 
GitLab