diff --git a/build/includes/file_access_actor/file_access_actor.hpp b/build/includes/file_access_actor/file_access_actor.hpp index c988af78ed36d9269febb16d4d19edf6917037bc..e2f239ca18408e313fdc3c4d6aec83d174dd2836 100644 --- a/build/includes/file_access_actor/file_access_actor.hpp +++ b/build/includes/file_access_actor/file_access_actor.hpp @@ -37,7 +37,38 @@ struct hru_output_handles { // ancillary data structures void* handle_dpar_struct = new_handle_var_d(); void* handle_finalize_stats = new_handle_var_i(); - void* handle_output_timestep = new_handle_var_i(); + void* handle_output_timestep = new_handle_var_i(); + + // Destructor + ~hru_output_handles(){ + // Statistic Structures + delete_handle_var_dlength(handle_forc_stat); + delete_handle_var_dlength(handle_prog_stat); + delete_handle_var_dlength(handle_diag_stat); + delete_handle_var_dlength(handle_flux_stat); + delete_handle_var_dlength(handle_indx_stat); + delete_handle_var_dlength(handle_bvar_stat); + // primary data structures (scalars) + delete_handle_var_i(handle_time_struct); + delete_handle_var_d(handle_forc_struct); + delete_handle_var_d(handle_attr_struct); + delete_handle_var_i(handle_type_struct); + delete_handle_var_i8(handle_id_struct); + // primary data structures (variable length vectors) + delete_handle_var_ilength(handle_indx_struct); + delete_handle_var_dlength(handle_mpar_struct); + delete_handle_var_dlength(handle_prog_struct); + delete_handle_var_dlength(handle_diag_struct); + delete_handle_var_dlength(handle_flux_struct); + // basin-average structures + delete_handle_var_d(handle_bpar_struct); + delete_handle_var_dlength(handle_bvar_struct); + // ancillary data structures + delete_handle_var_d(handle_dpar_struct); + delete_handle_var_i(handle_finalize_stats); + delete_handle_var_i(handle_output_timestep); + } + }; struct file_access_state { @@ -65,14 +96,14 @@ struct file_access_state { // Variables for hanlding attributes file int attribute_ncid; int num_var_in_attributes_file; - std::vector<void*> attr_structs_for_hrus; - std::vector<void*> type_structs_for_hrus; - std::vector<void*> id_structs_for_hrus; + std::vector<std::vector<double>> attr_structs_for_hrus; + std::vector<std::vector<int>> type_structs_for_hrus; + std::vector<std::vector<long int>> id_structs_for_hrus; // Variables for handling parameters file - std::vector<void*> mpar_structs_for_hrus; - std::vector<void*> bpar_structs_for_hrus; - std::vector<void*> dpar_structs_for_hrus; + std::vector<std::vector<std::vector<double>>> mpar_structs_for_hrus; + std::vector<std::vector<double>> bpar_structs_for_hrus; + std::vector<std::vector<double>> dpar_structs_for_hrus; int dpar_array_size; int bpar_array_size; diff --git a/build/includes/global/fortran_data_types.hpp b/build/includes/global/fortran_data_types.hpp index e6833991804632b599eac275b055d8c48deeaf13..1a0ed5412432c522e267f5071dc8e9cba1d4d1be 100644 --- a/build/includes/global/fortran_data_types.hpp +++ b/build/includes/global/fortran_data_types.hpp @@ -98,5 +98,6 @@ extern "C" { // zLookup void* new_handle_z_lookup(); + void* delete_handle_z_lookup(void* handle); } \ No newline at end of file diff --git a/build/includes/hru_actor/hru_actor.hpp b/build/includes/hru_actor/hru_actor.hpp index 48b8aa336d8350b2e71843233a5227029336bc73..684b24af6f297eb26af20b7b411bcf237350476c 100644 --- a/build/includes/hru_actor/hru_actor.hpp +++ b/build/includes/hru_actor/hru_actor.hpp @@ -10,6 +10,7 @@ + namespace caf { struct hru_state { // Actor References @@ -54,15 +55,13 @@ struct hru_state { // ancillary data structures void *handle_dparStruct = new_handle_var_d(); // default model parameters // sundials type - void *handle_lookupStruct = new_handle_z_lookup(); - - - // Local hru data - void *handle_ncid = new_handle_var_i(); // output file ids + void *handle_lookupStruct = new_handle_z_lookup(); + // Counter variables void *handle_statCounter = new_handle_var_i(); void *handle_outputTimeStep = new_handle_var_i(); void *handle_resetStats = new_handle_flagVec(); void *handle_finalizeStats = new_handle_flagVec(); + // Time variables void *handle_oldTime = new_handle_var_i(); void *handle_refTime = new_handle_var_i(); void *handle_finshTime = new_handle_var_i(); @@ -86,12 +85,50 @@ struct hru_state { // Settings HRU_Actor_Settings hru_actor_settings; - - - int err = 0; // error conotrol - + // error control + int err = 0; TimingInfo hru_timing; - + + ~hru_state() { + // statistics structures + delete_handle_var_dlength(handle_forcStat); + delete_handle_var_dlength(handle_progStat); + delete_handle_var_dlength(handle_diagStat); + delete_handle_var_dlength(handle_fluxStat); + delete_handle_var_dlength(handle_indxStat); + delete_handle_var_dlength(handle_bvarStat); + // primary data structures (scalars) + delete_handle_var_i(handle_timeStruct); + delete_handle_var_d(handle_forcStruct); + delete_handle_var_d(handle_attrStruct); + delete_handle_var_i(handle_typeStruct); + delete_handle_var_i8(handle_idStruct); + // primary data structures (variable length vectors) + delete_handle_var_ilength(handle_indxStruct); + delete_handle_var_dlength(handle_mparStruct); + delete_handle_var_dlength(handle_progStruct); + delete_handle_var_dlength(handle_diagStruct); + delete_handle_var_dlength(handle_fluxStruct); + // basin-average structures + delete_handle_var_d(handle_bparStruct); + delete_handle_var_dlength(handle_bvarStruct); + // ancillary data structures + delete_handle_var_d(handle_dparStruct); + // sundials type + delete_handle_z_lookup(handle_lookupStruct); + // counter variables + delete_handle_var_i(handle_statCounter); + delete_handle_var_i(handle_outputTimeStep); + delete_handle_flagVec(handle_resetStats); + delete_handle_flagVec(handle_finalizeStats); + // time variables + delete_handle_var_i(handle_oldTime); + delete_handle_var_i(handle_refTime); + delete_handle_var_i(handle_finshTime); + delete_handle_var_i(handle_startTime); + + + } }; behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, 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 a1103df843db9c77390f3a87c772b10d404318e6..29308d0790383f3d4cd4e3021b534a244733139b 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 @@ -46,20 +46,17 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr self->state.file_access_timing.updateStartPoint("write_duration"); - // create structures to populate in Fortran - void *handle_attr_struct = new_handle_var_d(); - void *handle_type_struct = new_handle_var_i(); - void *handle_mpar_struct = new_handle_var_dlength(); - void *handle_bpar_struct = new_handle_var_d(); // populate the newly created Fortran structures - set_var_d(attr_struct, handle_attr_struct); - set_var_i(type_struct, handle_type_struct); - set_var_dlength(mpar_struct, handle_mpar_struct); - set_var_d(bpar_struct, handle_bpar_struct); + set_var_d(attr_struct, self->state.output_handles.handle_attr_struct); + set_var_i(type_struct, self->state.output_handles.handle_type_struct); + set_var_dlength(mpar_struct, self->state.output_handles.handle_mpar_struct); + set_var_d(bpar_struct, self->state.output_handles.handle_bpar_struct); // write the populated data to netCDF writeParamToNetCDF(self->state.handle_ncid, &index_gru, &index_hru, - handle_attr_struct, handle_type_struct, handle_mpar_struct, - handle_bpar_struct, &err); + self->state.output_handles.handle_attr_struct, + self->state.output_handles.handle_type_struct, + self->state.output_handles.handle_mpar_struct, + self->state.output_handles.handle_bpar_struct, &err); self->state.file_access_timing.updateEndPoint("write_duration"); @@ -127,24 +124,16 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr }, [=] (get_attributes_params, int ref_gru, caf::actor actor_to_respond) { - // Find the correct attribute file we loaded in - void* handle_attr_struct = self->state.attr_structs_for_hrus[ref_gru-1]; - std::vector<double> attr_struct_to_send = get_var_d(handle_attr_struct); - - void* handle_type_struct = self->state.type_structs_for_hrus[ref_gru-1]; - std::vector<int> type_struct_to_send = get_var_i(handle_type_struct); - - void* handle_id_struct = self->state.id_structs_for_hrus[ref_gru-1]; - std::vector<long int> id_struct_to_send = get_var_i8(handle_id_struct); - - void* handle_bpar_struct = self->state.bpar_structs_for_hrus[ref_gru-1]; - std::vector<double> bpar_struct_to_send = get_var_d(handle_bpar_struct); - - void* handle_dpar_struct = self->state.dpar_structs_for_hrus[ref_gru-1]; - std::vector<double> dpar_struct_to_send = get_var_d(handle_dpar_struct); + + // From Attributes File + std::vector<double> attr_struct_to_send = self->state.attr_structs_for_hrus[ref_gru-1]; + std::vector<int> type_struct_to_send = self->state.type_structs_for_hrus[ref_gru-1]; + std::vector<long int> id_struct_to_send = self->state.id_structs_for_hrus[ref_gru-1]; - void* handle_mpar_struct = self->state.mpar_structs_for_hrus[ref_gru-1]; - std::vector<std::vector<double>> mpar_struct_to_send = get_var_dlength(handle_mpar_struct); + // From Parameters File + std::vector<double> bpar_struct_to_send = self->state.bpar_structs_for_hrus[ref_gru-1]; + std::vector<double> dpar_struct_to_send = self->state.dpar_structs_for_hrus[ref_gru-1]; + std::vector<std::vector<double>> mpar_struct_to_send = self->state.mpar_structs_for_hrus[ref_gru-1]; self->send(actor_to_respond, get_attributes_params_v, attr_struct_to_send, type_struct_to_send, id_struct_to_send, bpar_struct_to_send, @@ -349,10 +338,19 @@ void readAttributes(stateful_actor<file_access_state>* self) { readAttributeFromNetCDF(&self->state.attribute_ncid, &index_gru, &index_hru, &self->state.num_var_in_attributes_file, handle_attr_struct, handle_type_struct, handle_id_struct, &err); - - self->state.attr_structs_for_hrus.push_back(handle_attr_struct); - self->state.type_structs_for_hrus.push_back(handle_type_struct); - self->state.id_structs_for_hrus.push_back(handle_id_struct); + + // attr struct + std::vector<double> attr_struct_to_push = get_var_d(handle_attr_struct); + self->state.attr_structs_for_hrus.push_back(attr_struct_to_push); + delete_handle_var_d(handle_attr_struct); + // type struct + std::vector<int> type_struct_to_push = get_var_i(handle_type_struct); + self->state.type_structs_for_hrus.push_back(type_struct_to_push); + delete_handle_var_i(handle_type_struct); + // id struct + std::vector<long int> id_struct_to_push = get_var_i8(handle_id_struct); + self->state.id_structs_for_hrus.push_back(id_struct_to_push); + delete_handle_var_i8(handle_id_struct); } closeAttributeFile(&self->state.attribute_ncid, &err); @@ -379,6 +377,7 @@ void readParameters(stateful_actor<file_access_state>* self) { for (int index_gru = 1; index_gru < self->state.num_gru + 1; index_gru++) { std::vector<double> dpar_array(self->state.dpar_array_size); + void* handle_type_struct = new_handle_var_i(); void* handle_dpar_struct = new_handle_var_d(); void* handle_mpar_struct = new_handle_var_dlength(); void* handle_bpar_struct = new_handle_var_d(); @@ -387,8 +386,11 @@ void readParameters(stateful_actor<file_access_state>* self) { allocateParamStructures(&index_gru, &index_hru, handle_dpar_struct, handle_mpar_struct, handle_bpar_struct, &err); + // need to convert attr_struct to FORTRAN format + set_var_i(self->state.type_structs_for_hrus[index_gru-1], handle_type_struct); + overwriteParam(&index_gru, &index_hru, - self->state.type_structs_for_hrus[index_gru-1], + handle_type_struct, handle_dpar_struct, handle_mpar_struct, handle_bpar_struct, @@ -403,15 +405,27 @@ void readParameters(stateful_actor<file_access_state>* self) { &err); } - self->state.dpar_structs_for_hrus.push_back(handle_dpar_struct); - self->state.mpar_structs_for_hrus.push_back(handle_mpar_struct); - self->state.bpar_structs_for_hrus.push_back(handle_bpar_struct); + // type_struct + delete_handle_var_i(handle_type_struct); + + // dpar_struct + std::vector<double> dpar_struct_to_push = get_var_d(handle_dpar_struct); + self->state.dpar_structs_for_hrus.push_back(dpar_struct_to_push); + delete_handle_var_d(handle_dpar_struct); + // mpar_struct + std::vector<std::vector<double>> mpar_struct_to_push = get_var_dlength(handle_mpar_struct); + self->state.mpar_structs_for_hrus.push_back(mpar_struct_to_push); + delete_handle_var_dlength(handle_mpar_struct); + // bpar_struct + std::vector<double> bpar_struct_to_push = get_var_d(handle_bpar_struct); + self->state.bpar_structs_for_hrus.push_back(bpar_struct_to_push); + delete_handle_var_d(handle_bpar_struct); } closeParamFile(&self->state.param_ncid, &err); } void cleanup(stateful_actor<file_access_state>* self) { - + } } // end namespace \ No newline at end of file diff --git a/build/source/actors/global/cppwrap_datatypes.f90 b/build/source/actors/global/cppwrap_datatypes.f90 index 021fdfca2806d7926e782243c53a0a6b793047cb..decf8edfb9f0e5e0f275d54c90891e00bbc5b757 100644 --- a/build/source/actors/global/cppwrap_datatypes.f90 +++ b/build/source/actors/global/cppwrap_datatypes.f90 @@ -1096,6 +1096,15 @@ function new_handle_z_lookup() result(handle) bind(C, name="new_handle_z_lookup" allocate(p) handle = c_loc(p) end function + +subroutine delete_handle_z_lookup(handle) bind(C, name="delete_handle_z_lookup") + type(c_ptr), intent(in), value :: handle + type(zLookup), pointer :: p + + call c_f_pointer(handle, p) + deallocate(p) +end subroutine + end module cppwrap_datatypes 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 b3a399229b7d62a3ffa209f38e57955eea18f0f0..83967b42e487b4605c4e0ee5492a8452f3a679d5 100644 --- a/build/source/actors/hru_actor/cpp_code/hru_actor.cpp +++ b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp @@ -212,7 +212,7 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, self->state.hru_timing.getDuration("run_physics_duration").value_or(-1.0), self->state.hru_timing.getDuration("write_output_duration").value_or(-1.0)); - deallocateHRUStructures(self); + // deallocateHRUStructures(self); self->quit(); return; @@ -426,35 +426,36 @@ bool check_HRU(stateful_actor<hru_state>* self, int err) { void deallocateHRUStructures(stateful_actor<hru_state>* self) { - DeallocateStructures(self->state.handle_forcStat, - self->state.handle_progStat, - self->state.handle_diagStat, - self->state.handle_fluxStat, - self->state.handle_indxStat, - self->state.handle_bvarStat, - self->state.handle_timeStruct, - self->state.handle_forcStruct, - self->state.handle_attrStruct, - self->state.handle_typeStruct, - self->state.handle_idStruct, - self->state.handle_indxStruct, - self->state.handle_mparStruct, - self->state.handle_progStruct, - self->state.handle_diagStruct, - self->state.handle_fluxStruct, - self->state.handle_bparStruct, - self->state.handle_bvarStruct, - self->state.handle_dparStruct, - self->state.handle_startTime, - self->state.handle_finshTime, - self->state.handle_refTime, - self->state.handle_oldTime, - self->state.handle_ncid, - self->state.handle_statCounter, - self->state.handle_outputTimeStep, - self->state.handle_resetStats, - self->state.handle_finalizeStats, - &self->state.err); + // DeallocateStructures( + // self->state.handle_forcStat, + // self->state.handle_progStat, + // self->state.handle_diagStat, + // self->state.handle_fluxStat, + // self->state.handle_indxStat, + // self->state.handle_bvarStat, + // self->state.handle_timeStruct, + // self->state.handle_forcStruct, + // self->state.handle_attrStruct, + // self->state.handle_typeStruct, + // self->state.handle_idStruct, + // self->state.handle_indxStruct, + // self->state.handle_mparStruct, + // self->state.handle_progStruct, + // self->state.handle_diagStruct, + // self->state.handle_fluxStruct, + // self->state.handle_bparStruct, + // self->state.handle_bvarStruct, + // self->state.handle_dparStruct, + // self->state.handle_startTime, + // self->state.handle_finshTime, + // self->state.handle_refTime, + // self->state.handle_oldTime, + // self->state.handle_ncid, + // self->state.handle_statCounter, + // self->state.handle_outputTimeStep, + // self->state.handle_resetStats, + // self->state.handle_finalizeStats, + // &self->state.err); } void printOutput(stateful_actor<hru_state>* self) { diff --git a/build/source/actors/hru_actor/fortran_code/hru_actor.f90 b/build/source/actors/hru_actor/fortran_code/hru_actor.f90 index 45ea3489d84f4cb8946daa40bb2c97d59455a559..40c55ad56468a019eca848bbde5862050d5c9821 100644 --- a/build/source/actors/hru_actor/fortran_code/hru_actor.f90 +++ b/build/source/actors/hru_actor/fortran_code/hru_actor.f90 @@ -11,28 +11,12 @@ USE data_types,only:& implicit none -public::getSummaVariableInfo -! public::setParamAndAttr public::prepareOutput public::updateCounters contains -! subroutine setParamAndAttr(handle_attrStruct, handle_attrStruct, handle_idStruct, & -! handle_bparStruct, handle_dparStruct, attr_array, type_array, id_array, bpar_array) bind(C, name="setParamAndAttr") -! implicit none - - - - -! end subroutine - -subroutine getSummaVariableInfo(var_type, var_fortran_index, data_struct) bind(C, name="getSummaVariableInfo") - integer(c_int) :: var_type - integer(c_int) :: var_fortran_index - type(c_ptr) :: data_struct - -end subroutine getSummaVariableInfo - +! Prepare structure for being sent off to the file access actor +! call set alarms and calc stats subroutine prepareOutput(& modelTimeStep, & ! statistics variables @@ -228,6 +212,8 @@ subroutine prepareOutput(& end subroutine prepareOutput + +! Update all of the counters for time and output step subroutine updateCounters(handle_timeStruct, handle_statCounter, handle_outputTimeStep, & handle_resetStats, handle_oldTime, handle_finalizeStats) bind(C, name="updateCounters") USE globalData,only:startWrite,endWrite,elapsedWrite diff --git a/build/source/dshare/data_types.f90 b/build/source/dshare/data_types.f90 index b5f9e94a4a404345c7c2a50825efd6dbc0c1d525..60c2fcab61de25e9d70b62a8f717437382d4b737 100755 --- a/build/source/dshare/data_types.f90 +++ b/build/source/dshare/data_types.f90 @@ -424,24 +424,6 @@ endtype var_time_ilength type(vLookup),allocatable :: z(:) ! z(:)%var(:)%lookup(:) endtype zLookup -type, public :: summa_output_type - - - ! define the primary data structures (scalars) - type(gru_hru_double),allocatable :: attrStruct(:) ! x%gru(:)%hru(:)%var(:) -- local attributes for each HRU, DOES NOT CHANGE OVER TIMESTEPS - type(gru_hru_int),allocatable :: typeStruct(:) ! x%gru(:)%hru(:)%var(:)%tim(:) -- local classification of soil veg etc. for each HRU, DOES NOT CHANGE OVER TIMESTEPS - type(gru_hru_int8),allocatable :: idStruct(:) ! x%gru(:)%hru(:)%var(:) - - ! define the primary data structures (variable length vectors) - type(gru_hru_doubleVec),allocatable :: mparStruct(:) ! x%gru(:)%hru(:)%var(:)%dat -- model parameters, DOES NOT CHANGE OVER TIMESTEPS TODO: MAYBE - ! define the basin-average structures - type(gru_double),allocatable :: bparStruct(:) ! x%gru(:)%var(:) -- basin-average parameters, DOES NOT CHANGE OVER TIMESTEPS - - ! define the ancillary data structures - type(gru_hru_double),allocatable :: dparStruct(:) ! x%gru(:)%hru(:)%var(:) - - -end type summa_output_type END MODULE data_types diff --git a/build/source/dshare/globalData.f90 b/build/source/dshare/globalData.f90 index 937ab0c9a9e3b214d9ef8e78adbc6742280cc2e4..41670da6396bc3ffc92d83181e81194e09c88847 100755 --- a/build/source/dshare/globalData.f90 +++ b/build/source/dshare/globalData.f90 @@ -308,7 +308,6 @@ MODULE globalData !!!!!!!!!!!!!!!!!!GLOBAL DATA STRUCTURES THAT ARE MANAGED BY FILEACCESSACTOR !!!!!!!!!!!!!!!!!!!!!!!!!!!!! type(var_forc),allocatable,save,public :: forcingDataStruct(:) ! forcingDataStruct(:)%var(:)%dataFromFile(:,:) type(dlength),allocatable,save,public :: vecTime(:) - type(summa_output_type),allocatable,save,public :: outputStructure(:) ! summa_OutputStructure(iFile)%struc%var(:)%dat(nTimeSteps) logical(lgt),allocatable,save,public :: failedHRUs(:) ! list of true and false values to indicate if an HRU has failed !!!!!!!!!!!!!!!!!!GLOBAL DATA STRUCTURES THAT ARE MANAGED BY FILEACCESSACTOR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!