From a74830efca5c9e4c18d7d4d03da9782fc699ce10 Mon Sep 17 00:00:00 2001 From: Kyle <kyle.c.klenk@gmail.com> Date: Fri, 28 Oct 2022 12:35:42 -0600 Subject: [PATCH] Attribute fix is producing the same results as the older version of summa-actors --- .../file_access_actor/file_access_actor.hpp | 11 +- .../file_access_actor_subroutine_wrappers.hpp | 13 ++- .../hru_actor_subroutine_wrappers.hpp | 8 +- build/makefile_sundials | 4 +- .../cpp_code/file_access_actor.cpp | 81 +++++++++----- .../fortran_code/cppwrap_fileAccess.f90 | 26 ----- .../fortran_code/deallocateOutputStruc.f90 | 19 ---- .../fortran_code/read_attribute.f90 | 75 ++++++++++--- .../fortran_code/read_param.f90 | 102 ++++++++++++------ .../hru_actor/{ => cpp_code}/hru_actor.cpp | 31 +++--- .../{ => fortran_code}/cppwrap_hru.f90 | 0 .../{ => fortran_code}/hru_actor.f90 | 12 +++ .../hru_actor/serialize_data_structure.cpp | 2 - build/source/driver/SummaActors_setup.f90 | 20 ---- build/source/driver/init_hru_actor.f90 | 44 +------- 15 files changed, 240 insertions(+), 208 deletions(-) rename build/source/actors/hru_actor/{ => cpp_code}/hru_actor.cpp (95%) rename build/source/actors/hru_actor/{ => fortran_code}/cppwrap_hru.f90 (100%) rename build/source/actors/hru_actor/{ => fortran_code}/hru_actor.f90 (98%) delete mode 100644 build/source/actors/hru_actor/serialize_data_structure.cpp diff --git a/build/includes/file_access_actor/file_access_actor.hpp b/build/includes/file_access_actor/file_access_actor.hpp index 200532b..162b207 100644 --- a/build/includes/file_access_actor/file_access_actor.hpp +++ b/build/includes/file_access_actor/file_access_actor.hpp @@ -30,14 +30,15 @@ struct file_access_state { // Variables for hanlding attributes file int attribute_ncid; int num_var_in_attributes_file; - std::vector<std::vector<double>> attr_arrays_for_hrus; - std::vector<std::vector<int>> type_arrays_for_hrus; - std::vector<std::vector<long int>> id_arrays_for_hrus; + std::vector<void*> attr_structs_for_hrus; + std::vector<void*> type_structs_for_hrus; + std::vector<void*> id_structs_for_hrus; // Variables for handling parameters file std::vector<void*> mpar_structs_for_hrus; - std::vector<std::vector<double>> bpar_arrays_for_hrus; - std::vector<std::vector<double>> dpar_arrays_for_hrus; + std::vector<void*> bpar_structs_for_hrus; + std::vector<void*> dpar_structs_for_hrus; + int dpar_array_size; int bpar_array_size; int type_array_size; diff --git a/build/includes/file_access_actor/file_access_actor_subroutine_wrappers.hpp b/build/includes/file_access_actor/file_access_actor_subroutine_wrappers.hpp index 134f00d..5bbfe72 100644 --- a/build/includes/file_access_actor/file_access_actor_subroutine_wrappers.hpp +++ b/build/includes/file_access_actor/file_access_actor_subroutine_wrappers.hpp @@ -58,6 +58,9 @@ extern "C" { // Attributes Files + void allocateAttributeStructures(int* index_gru, int* index_hru, void* handle_attr_struct, + void* handle_type_struct, void* handle_id_struct, int* err); + void openAttributeFile(int* att_ncid, int* err); void getNumVarAttr(int* attr_ncid, int* num_var_attr, int* err); @@ -69,16 +72,18 @@ extern "C" { // Parameters File + void allocateParamStructures(int* index_gru, int* index_hru, void* handle_dpar_struct, + void* handle_mpar_struct, void* handle_bpar_struct, int* err); void openParamFile(int* param_ncid, bool* param_file_exists, int* err); void getNumVarParam(int* param_ncid, int* num_var_param, int* err); void closeParamFile(int* param_ncid, int* err); void getParamSizes(int* dpar_array_size, int* bpar_array_size, int* type_array_size); - void overwriteParam(int* index_gru, int* index_hru, int* num_var_attr, - void* type_array, void* dpar_array, void* handle_mpar_struct, void* bpar_array, - int* err); + void overwriteParam(int* index_gru, int* index_hru, + void* handle_type_struct, void* handle_dpar_struct, void* handle_mpar_struct, + void* handle_bpar_struct, int* err); void readParamFromNetCDF(int* param_ncid, int* index_gru, int* index_hru, int* start_index_gru, - int* num_var_param, int* bpar_array_size, void* handle_mpar_struct, void* bpar_array, int* err); + int* num_var_param, void* handle_mpar_struct, void* _handle_bpar_struct, int* err); } diff --git a/build/includes/hru_actor/hru_actor_subroutine_wrappers.hpp b/build/includes/hru_actor/hru_actor_subroutine_wrappers.hpp index 05440fc..c98529a 100644 --- a/build/includes/hru_actor/hru_actor_subroutine_wrappers.hpp +++ b/build/includes/hru_actor/hru_actor_subroutine_wrappers.hpp @@ -7,13 +7,11 @@ extern "C" { // Statistics Structures void* forcStat, void* progStat, void* diagStat, void* fluxStat, void* indxStat, void* bvarStat, // Primary Data Structures (scalars) - void* timeStruct, void* forcStruct, void* attrStruct, void* typeStruct, void* idStruct, + void* timeStruct, void* forcStruct, // primary data structures (variable length vectors) - void* indxStruct, void* mparStruct, void* progStruct, void* diagStruct, void* fluxStruct, + void* indxStruct, void* progStruct, void* diagStruct, void* fluxStruct, // basin-average structures - void* bparStruct, void* bvarStruct, - // ancillary data structures - void* dparStruct, + void* bvarStruct, // local HRU data void* startTime, void* finshTime, void* refTime, void* oldTime, int* err); diff --git a/build/makefile_sundials b/build/makefile_sundials index 60f38af..4153042 100644 --- a/build/makefile_sundials +++ b/build/makefile_sundials @@ -59,7 +59,7 @@ ENGINE_DIR = $(F_KORE_DIR)/engine ACTORS_DIR = $(F_KORE_DIR)/actors JOB_ACTOR_DIR = $(ACTORS_DIR)/job_actor FILE_ACCESS_DIR = $(ACTORS_DIR)/file_access_actor/fortran_code -HRU_ACTOR_DIR = $(ACTORS_DIR)/hru_actor +HRU_ACTOR_DIR = $(ACTORS_DIR)/hru_actor/fortran_code GRU_ACTOR_DIR = $(ACTORS_DIR)/gru_actor # utilities @@ -307,7 +307,7 @@ FORCING_FILE_INFO = $(SOURCE_DIR)/file_access_actor/cpp_code/forcing_file_info.c OUTPUT_MANAGER = $(SOURCE_DIR)/file_access_actor/cpp_code/output_manager.cpp HRU_ACTOR_INCLUDES = -I$(INCLUDE_DIR)/hru_actor -HRU_ACTOR = $(SOURCE_DIR)/hru_actor/hru_actor.cpp +HRU_ACTOR = $(SOURCE_DIR)/hru_actor/cpp_code/hru_actor.cpp MAIN = $(F_KORE_DIR)/actors/main.cpp 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 5f56b7d..c0c452f 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 @@ -130,20 +130,32 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int start_gr [=] (get_attributes, int ref_gru, caf::actor actor_to_respond) { // ref_gru will always be 1 index too high (FORTRAN arrays start at 1) - std::vector<double> attr_array_to_send = self->state.attr_arrays_for_hrus[ref_gru-1]; - std::vector<int> type_array_to_send = self->state.type_arrays_for_hrus[ref_gru-1]; - std::vector<long int> id_array_to_send = self->state.id_arrays_for_hrus[ref_gru-1]; - std::vector<double> bpar_array_to_send = self->state.bpar_arrays_for_hrus[ref_gru-1]; - std::vector<double> dpar_array_to_send = self->state.dpar_arrays_for_hrus[ref_gru-1]; + // std::vector<double> attr_array_to_send = self->state.attr_arrays_for_hrus[ref_gru-1]; + // std::vector<int> type_array_to_send = self->state.type_arrays_for_hrus[ref_gru-1]; + // std::vector<long int> id_array_to_send = self->state.id_arrays_for_hrus[ref_gru-1]; + // std::vector<double> bpar_array_to_send = self->state.bpar_arrays_for_hrus[ref_gru-1]; + // std::vector<double> dpar_array_to_send = self->state.dpar_arrays_for_hrus[ref_gru-1]; + 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_mpar_struct = self->state.mpar_structs_for_hrus[ref_gru-1]; + 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); - std::vector<std::vector<double>> mpar_array_to_send = get_var_dlength(handle_mpar_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); + 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); - self->send(actor_to_respond, get_attributes_v, attr_array_to_send, - type_array_to_send, id_array_to_send, bpar_array_to_send, - dpar_array_to_send, mpar_array_to_send); + self->send(actor_to_respond, get_attributes_v, attr_struct_to_send, + type_struct_to_send, id_struct_to_send, bpar_struct_to_send, + dpar_struct_to_send, mpar_struct_to_send); }, @@ -391,25 +403,29 @@ int readForcing(stateful_actor<file_access_state>* self, int currentFile) { void readAttributes(stateful_actor<file_access_state>* self) { int err = 0; openAttributeFile(&self->state.attribute_ncid, &err); + getNumVarAttr(&self->state.attribute_ncid, &self->state.num_var_in_attributes_file, &err); + for (int index_gru = 1; index_gru < self->state.num_gru + 1; index_gru++) { - std::vector<double> attr_array(self->state.num_var_in_attributes_file); - std::vector<int> type_array(self->state.num_var_in_attributes_file); - std::vector<long int> id_array(self->state.num_var_in_attributes_file); - + void* handle_attr_struct = new_handle_var_d(); + void* handle_type_struct = new_handle_var_i(); + void* handle_id_struct = new_handle_var_i8(); int index_hru = 1; + + allocateAttributeStructures(&index_gru, &index_hru, handle_attr_struct, handle_type_struct, + handle_id_struct, &err); + readAttributeFromNetCDF(&self->state.attribute_ncid, &index_gru, &index_hru, - &self->state.num_var_in_attributes_file, &attr_array[0], &type_array[0], - &id_array[0], &err); + &self->state.num_var_in_attributes_file, handle_attr_struct, handle_type_struct, + handle_id_struct, &err); - self->state.attr_arrays_for_hrus.push_back(attr_array); - self->state.type_arrays_for_hrus.push_back(type_array); - self->state.id_arrays_for_hrus.push_back(id_array); + 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); } closeAttributeFile(&self->state.attribute_ncid, &err); - } void readParameters(stateful_actor<file_access_state>* self) { @@ -433,24 +449,33 @@ 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_mpar_struct = new_handle_var_dlength(); + void* handle_dpar_struct = new_handle_var_d(); + void* handle_mpar_struct = new_handle_var_dlength(); + void* handle_bpar_struct = new_handle_var_d(); std::vector<double> bpar_array(self->state.dpar_array_size); + allocateParamStructures(&index_gru, &index_hru, handle_dpar_struct, + handle_mpar_struct, handle_bpar_struct, &err); - overwriteParam(&index_gru, &index_hru, &self->state.num_var_in_param_file, - &self->state.attr_arrays_for_hrus[index_gru-1][0], - &dpar_array[0], handle_mpar_struct, &bpar_array[0], &err); + overwriteParam(&index_gru, &index_hru, + self->state.type_structs_for_hrus[index_gru-1], + handle_dpar_struct, + handle_mpar_struct, + handle_bpar_struct, + &err); if (self->state.param_file_exists) { readParamFromNetCDF(&self->state.param_ncid, &index_gru, &index_hru, - &self->state.start_gru, &self->state.num_var_in_param_file, - &self->state.bpar_array_size, handle_mpar_struct, &bpar_array[0], + &self->state.start_gru, + &self->state.num_var_in_param_file, + handle_mpar_struct, + handle_bpar_struct, &err); } - self->state.dpar_arrays_for_hrus.push_back(dpar_array); + 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_arrays_for_hrus.push_back(bpar_array); + self->state.bpar_structs_for_hrus.push_back(handle_bpar_struct); } closeParamFile(&self->state.param_ncid, &err); diff --git a/build/source/actors/file_access_actor/fortran_code/cppwrap_fileAccess.f90 b/build/source/actors/file_access_actor/fortran_code/cppwrap_fileAccess.f90 index 07fa7c8..86d95d1 100644 --- a/build/source/actors/file_access_actor/fortran_code/cppwrap_fileAccess.f90 +++ b/build/source/actors/file_access_actor/fortran_code/cppwrap_fileAccess.f90 @@ -12,7 +12,6 @@ module cppwrap_fileAccess implicit none public::ffile_info_C public::mDecisions_C - public::Init_OutputStruct public::initFailedHRUTracker public::FileAccessActor_ReadForcing @@ -165,27 +164,6 @@ subroutine resetOutputCounter(indxGRU) bind(C, name="resetOutputCounter") end subroutine resetOutputCounter -subroutine Init_OutputStruct(handle_forcFileInfo, maxSteps, nGRU, err) bind(C, name="Init_OutputStruct") - USE summaActors_initOutputStruct,only:initalizeOutput - USE globalData,only:outputStructure - - implicit none - type(c_ptr), intent(in), value :: handle_forcFileInfo - integer(c_int), intent(in) :: maxSteps - integer(c_int), intent(in) :: nGRU - integer(c_int), intent(inout) :: err - - ! local Variables - type(file_info_array), pointer :: forcFileInfo - call c_f_pointer(handle_forcFileInfo, forcFileInfo) - - if (allocated(outputStructure))then - print*, "Already Allocated" - else - call initalizeOutput(forcFileInfo,maxSteps,nGRU,err) - endif - -end subroutine Init_OutputStruct subroutine FileAccessActor_ReadForcing(handle_forcFileInfo, currentFile, stepsInFile, startGRU, numGRU, err) bind(C,name="FileAccessActor_ReadForcing") USE access_forcing_module,only:access_forcingFile @@ -211,7 +189,6 @@ subroutine FileAccessActor_DeallocateStructures(handle_forcFileInfo, handle_ncid USE globalData,only:structInfo ! information on the data structures USE globalData,only:outputTimeStep USE globalData,only:failedHRUs - USE summaActors_deallocateOuptutStruct,only:deallocateOutputStruc implicit none type(c_ptr),intent(in), value :: handle_forcFileInfo type(c_ptr),intent(in), value :: handle_ncid @@ -239,9 +216,6 @@ subroutine FileAccessActor_DeallocateStructures(handle_forcFileInfo, handle_ncid deallocate(outputTimeStep) deallocate(ncid) deallocate(failedHRUs) - - call deallocateOutputStruc(err) - end subroutine FileAccessActor_DeallocateStructures diff --git a/build/source/actors/file_access_actor/fortran_code/deallocateOutputStruc.f90 b/build/source/actors/file_access_actor/fortran_code/deallocateOutputStruc.f90 index f287e3f..c034166 100644 --- a/build/source/actors/file_access_actor/fortran_code/deallocateOutputStruc.f90 +++ b/build/source/actors/file_access_actor/fortran_code/deallocateOutputStruc.f90 @@ -1,27 +1,8 @@ module summaActors_deallocateOuptutStruct USE nrtype implicit none - public::deallocateOutputStruc contains -subroutine deallocateOutputStruc(err) - USE globalData,only:outputStructure - implicit none - integer(i4b), intent(inout) :: err - - err = 0 - ! id - call deallocateData_output(outputStructure(1)%idStruct(1)); deallocate(outputStructure(1)%idStruct) - ! attr - call deallocateData_output(outputStructure(1)%attrStruct(1)); deallocate(outputStructure(1)%attrStruct) - ! type - call deallocateData_output(outputStructure(1)%typeStruct(1)); deallocate(outputStructure(1)%typeStruct) - ! mpar - call deallocateData_output(outputStructure(1)%mparStruct(1)); deallocate(outputStructure(1)%mparStruct) - ! bpar - call deallocateData_output(outputStructure(1)%bparStruct(1)); deallocate(outputStructure(1)%bparStruct) - -end subroutine deallocateOutputStruc subroutine deallocateData_output(dataStruct) USE data_types,only:gru_hru_time_doubleVec, & diff --git a/build/source/actors/file_access_actor/fortran_code/read_attribute.f90 b/build/source/actors/file_access_actor/fortran_code/read_attribute.f90 index 7a0aba8..4215a8c 100644 --- a/build/source/actors/file_access_actor/fortran_code/read_attribute.f90 +++ b/build/source/actors/file_access_actor/fortran_code/read_attribute.f90 @@ -4,6 +4,7 @@ USE nrtype implicit none private +public::allocateAttributeStructures public::openAttributeFile public::getNumVarAttr public::closeAttributeFile @@ -11,6 +12,45 @@ public::readAttributeFromNetCDF contains +subroutine allocateAttributeStructures(index_gru, index_hru, & ! indexes into gru_struc + handle_attr_struct, handle_type_struct, handle_id_struct, err) bind(C, name="allocateAttributeStructures") + USE data_types,only:var_d, var_i, var_i8 + USE globalData,only:gru_struc + USE globalData,only:attr_meta,type_meta,id_meta + USE allocspace_module,only:allocLocal + implicit none + integer(c_int),intent(in) :: index_gru + integer(c_int),intent(in) :: index_hru + type(c_ptr), intent(in), value :: handle_attr_struct + type(c_ptr), intent(in), value :: handle_type_struct + type(c_ptr), intent(in), value :: handle_id_struct + integer(c_int), intent(out) :: err + type(var_d), pointer :: attr_struct + type(var_i), pointer :: type_struct + type(var_i8), pointer :: id_struct + integer(i4b) :: nSoil + integer(i4b) :: nSnow + character(len=256) :: message + ! --------------------------------------------------------------------------------------- + ! * Convert From C++ to Fortran + ! --------------------------------------------------------------------------------------- + call c_f_pointer(handle_attr_struct, attr_struct) + call c_f_pointer(handle_type_struct, type_struct) + call c_f_pointer(handle_id_struct, id_struct) + ! Start subroutine + err=0; message="read_attribute.f90 - allocateAttributeStructures" + + nSnow = gru_struc(index_gru)%hruInfo(index_hru)%nSnow + nSoil = gru_struc(index_gru)%hruInfo(index_hru)%nSoil + + call allocLocal(attr_meta,attr_struct,nSnow,nSoil,err,message); + call allocLocal(type_meta,type_struct,nSnow,nSoil,err,message); + call allocLocal(id_meta,id_struct,nSnow,nSoil,err,message); + if(err/=0)then; message=trim(message); print*, message; return; endif; + +end subroutine allocateAttributeStructures + + subroutine openAttributeFile(attr_ncid, err) bind(C, name="openAttributeFile") USE netcdf USE netcdf_util_module,only:nc_file_open ! open netcdf file @@ -79,7 +119,7 @@ end subroutine closeAttributeFile ! Read in the local attributes for an HRU subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & - attr_array, type_array, id_array, err) bind(C, name="readAttributeFromNetCDF") + handle_attr_struct, handle_type_struct, handle_id_struct, err) bind(C, name="readAttributeFromNetCDF") ! netcdf utilities USE netcdf USE netcdf_util_module,only:nc_file_open ! open netcdf file @@ -92,6 +132,8 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & ! Information to make up the attributes file USE summaActors_FileManager,only:SETTINGS_PATH ! define path to settings files (e.g., parameters, soil and veg. tables) USE summaActors_FileManager,only:LOCAL_ATTRIBUTES ! name of model initial attributes file + ! Fortran Data Type Structures + USE data_types,only:var_d, var_i, var_i8 implicit none ! indexes into gru_struc integer(c_int), intent(in) :: ncid @@ -100,23 +142,24 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & ! number of variables from the netCDF file integer(c_int), intent(in) :: num_var ! data structures to populate - real(c_double), intent(out) :: attr_array(num_var) - integer(c_int), intent(out) :: type_array(num_var) - integer(c_long),intent(out) :: id_array(num_var) + type(c_ptr), intent(in), value :: handle_attr_struct + type(c_ptr), intent(in), value :: handle_type_struct + type(c_ptr), intent(in), value :: handle_id_struct ! error control integer(c_int), intent(out) :: err - ! local variables integer(i4b) :: iVar ! loop through varibles in the netcdf file integer(i4b) :: varType ! type of variable (categorica, numerical, idrelated) integer(i4b) :: varIndx ! index of variable within its data structure - + ! Fortran structures + type(var_d), pointer :: attr_struct + type(var_i), pointer :: type_struct + type(var_i8), pointer :: id_struct ! check structures - to verify input integer(i4b) :: iCheck ! index of an attribute name logical(lgt),allocatable :: checkType(:) ! vector to check if we have all desired categorical values logical(lgt),allocatable :: checkId(:) ! vector to check if we have all desired IDs logical(lgt),allocatable :: checkAttr(:) ! vector to check if we have all desired local attributes - ! netcdf variables character(LEN=nf90_max_name) :: varName ! character array of netcdf variable name integer(i4b),parameter :: categorical=101 ! named variable to denote categorical data @@ -125,12 +168,14 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & integer(i4b) :: categorical_var(1) ! temporary categorical variable from local attributes netcdf file real(rkind) :: numeric_var(1) ! temporary numeric variable from local attributes netcdf file integer(8) :: idrelated_var(1) ! temporary ID related variable from local attributes netcdf file - - character(len=256) :: attr_file ! attributes file name character(len=256) :: message - - + ! --------------------------------------------------------------------------------------- + ! * Convert From C++ to Fortran + ! --------------------------------------------------------------------------------------- + call c_f_pointer(handle_attr_struct, attr_struct) + call c_f_pointer(handle_type_struct, type_struct) + call c_f_pointer(handle_id_struct, id_struct) err=0; message="read_attribute_file_access_actor - read_attribute.f90" @@ -186,7 +231,7 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & print*, message return end if - type_array(varIndx) = categorical_var(1) + type_struct%var(varIndx) = categorical_var(1) ! ** ID related data case('hruId') @@ -211,7 +256,7 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & print*, message return end if - id_array(varIndx) = idrelated_var(1) + id_struct%var(varIndx) = idrelated_var(1) ! ** numerical data @@ -236,7 +281,7 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & print*, message return end if - attr_array(varIndx) = numeric_var(1) + attr_struct%var(varIndx) = numeric_var(1) ! for mapping varibles, do nothing (information read above) @@ -258,7 +303,7 @@ subroutine readAttributeFromNetCDF(ncid, index_gru, index_hru, num_var, & ! check that the variable was not found in the attribute file if(.not. checkAttr(varIndx)) then write(*,*) NEW_LINE('A')//'INFO: aspect not found in the input attribute file, continuing ...'//NEW_LINE('A') - attr_array(varIndx) = nr_realMissing ! populate variable with out-of-range value, used later + attr_struct%var(varIndx) = nr_realMissing ! populate variable with out-of-range value, used later checkAttr(varIndx) = .true. endif diff --git a/build/source/actors/file_access_actor/fortran_code/read_param.f90 b/build/source/actors/file_access_actor/fortran_code/read_param.f90 index eaaca26..52263b1 100644 --- a/build/source/actors/file_access_actor/fortran_code/read_param.f90 +++ b/build/source/actors/file_access_actor/fortran_code/read_param.f90 @@ -3,6 +3,7 @@ module read_param_module USE nrtype implicit none private + public::allocateParamStructures public::openParamFile public::getNumVarParam public::closeParamFile @@ -10,6 +11,49 @@ module read_param_module public::overwriteParam public::readParamFromNetCDF contains +subroutine allocateParamStructures(index_gru, index_hru, handle_dpar_struct, & + handle_mpar_struct, handle_bpar_struct, err) bind(C, name="allocateParamStructures") + + USE globalData,only:mpar_meta,bpar_meta + USE globalData,only:gru_struc + USE data_types,only:var_dlength,var_d + USE allocspace_module,only:allocLocal + + implicit none + integer(c_int),intent(in) :: index_gru + integer(c_int),intent(in) :: index_hru + type(c_ptr),intent(in),value :: handle_dpar_struct + type(c_ptr),intent(in),value :: handle_mpar_struct + type(c_ptr),intent(in),value :: handle_bpar_struct + integer(c_int),intent(out) :: err + + type(var_d), pointer :: dpar_struct + type(var_dlength), pointer :: mpar_struct + type(var_d), pointer :: bpar_struct + + integer(i4b) :: nSnow + integer(i4b) :: nSoil + + character(len=256) :: message + + ! --------------------------------------------------------------------------------------- + ! * Convert From C++ to Fortran + ! --------------------------------------------------------------------------------------- + call c_f_pointer(handle_dpar_struct, dpar_struct) + call c_f_pointer(handle_mpar_struct, mpar_struct) + call c_f_pointer(handle_bpar_struct, bpar_struct) + ! start of subroutine + err=0; message="read_attribute.f90 - allocateAttributeStructures" + + nSnow = gru_struc(index_gru)%hruInfo(index_hru)%nSnow + nSoil = gru_struc(index_gru)%hruInfo(index_hru)%nSoil + + ! initalize the structure with allocatable components + call allocLocal(mpar_meta,dpar_struct,nSnow,nSoil,err,message); + call allocLocal(mpar_meta,mpar_struct,nSnow,nSoil,err,message); + call allocLocal(bpar_meta,bpar_struct,nSnow=0,nSoil=0,err=err,message=message); + if(err/=0)then; message=trim(message); print*, message; return; endif; +end subroutine subroutine openParamFile(param_ncid, param_file_exists, err) bind(C, name="openParamFile") USE netcdf @@ -112,37 +156,37 @@ subroutine getParamSizes(dpar_array_size, bpar_array_size, type_array_size) bind end subroutine getParamSizes -subroutine overwriteParam(index_gru, index_hru, num_var_attr, type_array, & - dpar_array, handle_mpar_struct, bpar_array, err) bind(C, name="overwriteParam") +subroutine overwriteParam(index_gru, index_hru, handle_type_struct, & + handle_dpar_struct, handle_mpar_struct, handle_bpar_struct, err) bind(C, name="overwriteParam") USE var_lookup,only:maxvarMpar ! model parameters: maximum number variables USE var_lookup,only:maxvarBpar ! model parameters: maximum number variables USE var_lookup,only:iLookTYPE ! named variables to index elements of the data vectors - ! global data USE globalData,only:gru_struc USE globalData,only:localParFallback ! local column default parameters USE globalData,only:basinParFallback ! basin-average default parameter - USE globalData,only:mpar_meta - USE data_types,only:var_dlength + USE data_types,only:var_dlength,var_i,var_d USE pOverwrite_module,only:pOverwrite ! module to overwrite default parameter values with info from the Noah tables USE allocspace_module,only:allocLocal - - implicit none integer(c_int),intent(in) :: index_gru integer(c_int),intent(in) :: index_hru - integer(c_int),intent(in) :: num_var_attr ! size of type array - ! arrays - integer(c_int),intent(in) :: type_array(num_var_attr) - real(c_double),intent(out) :: dpar_array(maxvarMpar) + ! structures + type(c_ptr),intent(in),value :: handle_type_struct + type(c_ptr),intent(in),value :: handle_dpar_struct type(c_ptr),intent(in),value :: handle_mpar_struct - real(c_double),intent(out) :: bpar_array(maxvarBpar) + type(c_ptr),intent(in),value :: handle_bpar_struct + ! error control integer(c_int), intent(out) :: err ! local variables + type(var_i),pointer :: type_struct ! model parameters + type(var_d),pointer :: dpar_struct ! model parameters type(var_dlength),pointer :: mpar_struct ! model parameters + type(var_d),pointer :: bpar_struct ! model parameters + integer(i4b) :: iVar integer(i4b) :: iDat @@ -150,44 +194,41 @@ subroutine overwriteParam(index_gru, index_hru, num_var_attr, type_array, & ! --------------------------------------------------------------------------------------- ! * Convert From C++ to Fortran ! --------------------------------------------------------------------------------------- + call c_f_pointer(handle_type_struct, type_struct) + call c_f_pointer(handle_dpar_struct, dpar_struct) call c_f_pointer(handle_mpar_struct, mpar_struct) + call c_f_pointer(handle_bpar_struct, bpar_struct) ! Start subroutine err=0; message="read_param.f90 - overwriteParam" - ! initalize the structure with allocatable components - call allocLocal(mpar_meta,mpar_struct, & - gru_struc(index_gru)%hruInfo(index_hru)%nSnow,& - gru_struc(index_gru)%hruInfo(index_hru)%nSoil,& - err,message); if(err/=0)then; message=trim(message); print*, message; return; endif; - ! Set the basin parameters with the default values do ivar=1, size(localParFallback) - dpar_array(iVar) = localParFallback(iVar)%default_val + dpar_struct%var(iVar) = localParFallback(iVar)%default_val end do - call pOverwrite(type_array(iLookTYPE%vegTypeIndex), & ! vegetation category - type_array(iLookTYPE%soilTypeIndex),& ! soil category - dpar_array(:),err,message) ! default model parameters + call pOverwrite(type_struct%var(iLookTYPE%vegTypeIndex), & ! vegetation category + type_struct%var(iLookTYPE%soilTypeIndex),& ! soil category + dpar_struct%var(:),err,message) ! default model parameters do ivar=1, size(localParFallback) do iDat=1, size(mpar_struct%var(iVar)%dat) - mpar_struct%var(iVar)%dat(iDat) = dpar_array(iVar) + mpar_struct%var(iVar)%dat(iDat) = dpar_struct%var(iVar) end do end do do iVar=1, size(basinParFallback) - bpar_array(iVar) = basinParFallback(iVar)%default_val + bpar_struct%var(iVar) = basinParFallback(iVar)%default_val end do end subroutine overwriteParam subroutine readParamFromNetCDF(param_ncid, index_gru, index_hru, start_index_gru, & - num_vars, bpar_array_size, handle_mpar_struct, bpar_array, err) bind(C, name="readParamFromNetCDF") + num_vars, handle_mpar_struct, handle_bpar_struct, err) bind(C, name="readParamFromNetCDF") USE netcdf USE netcdf_util_module,only:netcdf_err ! netcdf error handling function - USE data_types,only:var_dlength + USE data_types,only:var_dlength,var_d USE get_ixname_module,only:get_ixparam,get_ixbpar ! access function to find index of elements in structure USE globalData,only:index_map,gru_struc ! mapping from global HRUs to the elements in the data structures @@ -200,12 +241,12 @@ subroutine readParamFromNetCDF(param_ncid, index_gru, index_hru, start_index_gru integer(c_int),intent(in) :: index_hru integer(c_int),intent(in) :: start_index_gru integer(c_int),intent(in) :: num_vars - integer(c_int),intent(in) :: bpar_array_size - type(c_ptr), intent(in),value :: handle_mpar_struct - real(c_double), intent(out) :: bpar_array(bpar_array_size) + type(c_ptr),intent(in),value :: handle_mpar_struct + type(c_ptr),intent(in),value :: handle_bpar_struct integer(c_int), intent(out) :: err ! define local variables type(var_dlength),pointer :: mpar_struct ! model parameters + type(var_d),pointer :: bpar_struct ! model parameters character(len=256) :: message ! error message character(len=1024) :: cmessage ! error message for downwind routine @@ -231,6 +272,7 @@ subroutine readParamFromNetCDF(param_ncid, index_gru, index_hru, start_index_gru ! * Convert From C++ to Fortran ! --------------------------------------------------------------------------------------- call c_f_pointer(handle_mpar_struct, mpar_struct) + call c_f_pointer(handle_bpar_struct, bpar_struct) err=0; message="read_param.f90 - readParamFromNetCDF/" @@ -342,7 +384,7 @@ subroutine readParamFromNetCDF(param_ncid, index_gru, index_hru, start_index_gru ! read parameter data netcdf_index = start_index_gru + index_gru - 1 - err=nf90_get_var(param_ncid, ivarid, bpar_array(ixParam), start=(/netcdf_index/)) + err=nf90_get_var(param_ncid, ivarid, bpar_struct%var(ixParam), start=(/netcdf_index/)) if(err/=0)then message=trim(message)//trim(cmessage) print*, message diff --git a/build/source/actors/hru_actor/hru_actor.cpp b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp similarity index 95% rename from build/source/actors/hru_actor/hru_actor.cpp rename to build/source/actors/hru_actor/cpp_code/hru_actor.cpp index e8a4fa0..cc3bb61 100644 --- a/build/source/actors/hru_actor/hru_actor.cpp +++ b/build/source/actors/hru_actor/cpp_code/hru_actor.cpp @@ -54,10 +54,9 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, initHRU(&self->state.indxGRU, &self->state.num_steps, self->state.handle_lookupStruct, 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_bvarStat, self->state.handle_timeStruct, self->state.handle_forcStruct, self->state.handle_indxStruct, + self->state.handle_progStruct, self->state.handle_diagStruct, self->state.handle_fluxStruct, + self->state.handle_bvarStruct, self->state.handle_startTime, self->state.handle_finshTime, self->state.handle_refTime,self->state.handle_oldTime, &self->state.err); if (self->state.err != 0) { aout(self) << "Error: HRU_Actor - Initialize - HRU = " << self->state.indxHRU << @@ -72,16 +71,25 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, self->state.hru_timing.updateEndPoint("total_duration"); - self->send(self, start_hru_v); return { // Starts the HRU and tells it to ask for data from the file_access_actor - [=](get_attributes, std::vector<double> attr_array, std::vector<int> type_array, - std::vector<long int> id_array, std::vector<double> bpar_array, - std::vector<double> dpar_array, std::vector<std::vector<double>> mpar_array) { + [=](get_attributes, std::vector<double> attr_struct, std::vector<int> type_struct, + std::vector<long int> id_struct, std::vector<double> bpar_struct, + std::vector<double> dpar_struct, std::vector<std::vector<double>> mpar_struct) { - aout(self) << "Received Attribute Information \n"; - + int err = 0; + set_var_d(attr_struct, self->state.handle_attrStruct); + set_var_i(type_struct, self->state.handle_typeStruct); + set_var_i8(id_struct, self->state.handle_idStruct); + set_var_d(bpar_struct, self->state.handle_bparStruct); + set_var_d(dpar_struct, self->state.handle_dparStruct); + set_var_dlength(mpar_struct, self->state.handle_mparStruct); + + Initialize_HRU(self); + + self->send(self, start_hru_v); + }, @@ -246,9 +254,6 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, void Initialize_HRU(stateful_actor<hru_state>* self) { self->state.hru_timing.updateStartPoint("init_duration"); - - - // Need to send a message to the file_access_actor for the data setupHRUParam(&self->state.indxHRU, diff --git a/build/source/actors/hru_actor/cppwrap_hru.f90 b/build/source/actors/hru_actor/fortran_code/cppwrap_hru.f90 similarity index 100% rename from build/source/actors/hru_actor/cppwrap_hru.f90 rename to build/source/actors/hru_actor/fortran_code/cppwrap_hru.f90 diff --git a/build/source/actors/hru_actor/hru_actor.f90 b/build/source/actors/hru_actor/fortran_code/hru_actor.f90 similarity index 98% rename from build/source/actors/hru_actor/hru_actor.f90 rename to build/source/actors/hru_actor/fortran_code/hru_actor.f90 index f9a1d8d..45ea348 100644 --- a/build/source/actors/hru_actor/hru_actor.f90 +++ b/build/source/actors/hru_actor/fortran_code/hru_actor.f90 @@ -12,8 +12,20 @@ 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 diff --git a/build/source/actors/hru_actor/serialize_data_structure.cpp b/build/source/actors/hru_actor/serialize_data_structure.cpp deleted file mode 100644 index 1a1d432..0000000 --- a/build/source/actors/hru_actor/serialize_data_structure.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "serialize_data_structure.hpp" - diff --git a/build/source/driver/SummaActors_setup.f90 b/build/source/driver/SummaActors_setup.f90 index ded0cf6..9075fd5 100755 --- a/build/source/driver/SummaActors_setup.f90 +++ b/build/source/driver/SummaActors_setup.f90 @@ -188,16 +188,6 @@ subroutine setupHRUParam(& ! get the maximum number of layers maxLayers = gru_struc(1)%hruInfo(1)%nSoil + maxSnowLayers - ! ***************************************************************************** - ! *** read local attributes for each HRU - ! ***************************************************************************** - ! call read_attribute(indxHRU,indxGRU,attrStruct,typeStruct,idStruct,err,cmessage) - ! if(err/=0)then - ! message=trim(message)//trim(cmessage) - ! print*, message - ! return - ! endif - ! define monthly fraction of green vegetation greenVegFrac_monthly = (/0.01_dp, 0.02_dp, 0.03_dp, 0.07_dp, 0.50_dp, 0.90_dp, 0.95_dp, 0.96_dp, 0.65_dp, 0.24_dp, 0.11_dp, 0.02_dp/) @@ -214,16 +204,6 @@ subroutine setupHRUParam(& return end select - - ! ***************************************************************************** - ! *** read trial model parameter values for each HRU, and populate initial data structures - ! ***************************************************************************** - call read_param(indxHRU,indxGRU,mparStruct,bparStruct,dparStruct,err) - if(err/=0)then - message=trim(message)//trim(cmessage) - print*, message - return - endif ! ***************************************************************************** ! *** compute derived model variables that are pretty much constant for the basin as a whole ! ***************************************************************************** diff --git a/build/source/driver/init_hru_actor.f90 b/build/source/driver/init_hru_actor.f90 index 0ca9869..b8837de 100755 --- a/build/source/driver/init_hru_actor.f90 +++ b/build/source/driver/init_hru_actor.f90 @@ -55,20 +55,13 @@ contains ! primary data structures (scalars) handle_timeStruct, & ! model time data handle_forcStruct, & ! model forcing data - handle_attrStruct, & ! local attributes for each HRU - handle_typeStruct, & ! local classification of soil veg etc. for each HRU - handle_idStruct, & ! ! primary data structures (variable length vectors) handle_indxStruct, & ! model indices - handle_mparStruct, & ! model parameters handle_progStruct, & ! model prognostic (state) variables handle_diagStruct, & ! model diagnostic variables handle_fluxStruct, & ! model fluxes ! basin-average structures - handle_bparStruct, & ! basin-average parameters handle_bvarStruct, & ! basin-average variables - ! ancillary data structures - handle_dparStruct, & ! default model parameters ! local HRU data structures handle_startTime, & ! start time for the model simulation handle_finshTime, & ! end time for the model simulation @@ -115,20 +108,14 @@ contains ! primary data structures (scalars) type(c_ptr), intent(in), value :: handle_timeStruct ! model time data type(c_ptr), intent(in), value :: handle_forcStruct ! model forcing data - type(c_ptr), intent(in), value :: handle_attrStruct ! local attributes for each HRU - type(c_ptr), intent(in), value :: handle_typeStruct ! local classification of soil veg etc. for each HRU - type(c_ptr), intent(in), value :: handle_idStruct ! ! primary data structures (variable length vectors) type(c_ptr), intent(in), value :: handle_indxStruct ! model indices - type(c_ptr), intent(in), value :: handle_mparStruct ! model parameters type(c_ptr), intent(in), value :: handle_progStruct ! model prognostic (state) variables type(c_ptr), intent(in), value :: handle_diagStruct ! model diagnostic variables type(c_ptr), intent(in), value :: handle_fluxStruct ! model fluxes ! basin-average structures - type(c_ptr), intent(in), value :: handle_bparStruct ! basin-average parameters type(c_ptr), intent(in), value :: handle_bvarStruct ! basin-average variables ! ancillary data structures - type(c_ptr), intent(in), value :: handle_dparStruct ! default model parameters ! local hru data structures type(c_ptr), intent(in), value :: handle_startTime ! start time for the model simulation type(c_ptr), intent(in), value :: handle_finshTime ! end time for the model simulation @@ -148,20 +135,13 @@ contains ! primary data structures (scalars) type(var_i),pointer :: timeStruct ! model time data type(var_d),pointer :: forcStruct ! model forcing data - type(var_d),pointer :: attrStruct ! local attributes for each HRU - type(var_i),pointer :: typeStruct ! local classification of soil veg etc. for each HRU - type(var_i8),pointer :: idStruct ! ! primary data structures (variable length vectors) type(var_ilength),pointer :: indxStruct ! model indices - type(var_dlength),pointer :: mparStruct ! model parameters type(var_dlength),pointer :: progStruct ! model prognostic (state) variables type(var_dlength),pointer :: diagStruct ! model diagnostic variables type(var_dlength),pointer :: fluxStruct ! model fluxes ! basin-average structures - type(var_d),pointer :: bparStruct ! basin-average parameters type(var_dlength),pointer :: bvarStruct ! basin-average variables - ! ancillary data structures - type(var_d),pointer :: dparStruct ! default model parameters ! local HRU data structures type(var_i),pointer :: startTime_hru ! start time for the model simulation type(var_i),pointer :: finishTime_hru ! end time for the model simulation @@ -185,17 +165,11 @@ contains call c_f_pointer(handle_bvarStat, bvarStat) call c_f_pointer(handle_timeStruct, timeStruct) call c_f_pointer(handle_forcStruct, forcStruct) - call c_f_pointer(handle_attrStruct, attrStruct) - call c_f_pointer(handle_typeStruct, typeStruct) - call c_f_pointer(handle_idStruct, idStruct) call c_f_pointer(handle_indxStruct, indxStruct) - call c_f_pointer(handle_mparStruct, mparStruct) call c_f_pointer(handle_progStruct, progStruct) call c_f_pointer(handle_diagStruct, diagStruct) call c_f_pointer(handle_fluxStruct, fluxStruct) - call c_f_pointer(handle_bparStruct, bparStruct) call c_f_pointer(handle_bvarStruct, bvarStruct) - call c_f_pointer(handle_dparStruct, dparStruct) call c_f_pointer(handle_startTime, startTime_hru) call c_f_pointer(handle_finshTime, finishTime_hru) call c_f_pointer(handle_refTime, refTime_hru) @@ -249,15 +223,15 @@ contains select case(trim(structInfo(iStruct)%structName)) case('time'); call allocLocal(time_meta,timeStruct,err=err,message=cmessage) ! model forcing data case('forc'); call allocLocal(forc_meta,forcStruct,nSnow,nSoil,err,cmessage); ! model forcing data - case('attr'); call allocLocal(attr_meta,attrStruct,nSnow,nSoil,err,cmessage); ! local attributes for each HRU - case('type'); call allocLocal(type_meta,typeStruct,nSnow,nSoil,err,cmessage); ! classification of soil veg etc. - case('id' ); call allocLocal(id_meta,idStruct,nSnow,nSoil,err,cmessage); ! local values of hru and gru IDs - case('mpar'); call allocLocal(mpar_meta,mparStruct,nSnow,nSoil,err,cmessage); ! model parameters + case('attr'); cycle ! set by file_access_actor + case('type'); cycle ! set by file_access_actor + case('id' ); cycle ! set by file_access_actor + case('mpar'); cycle ! set by file_access_actor case('indx'); call allocLocal(indx_meta,indxStruct,nSnow,nSoil,err,cmessage); ! model variables case('prog'); call allocLocal(prog_meta,progStruct,nSnow,nSoil,err,cmessage); ! model prognostic (state) variables case('diag'); call allocLocal(diag_meta,diagStruct,nSnow,nSoil,err,cmessage); ! model diagnostic variables case('flux'); call allocLocal(flux_meta,fluxStruct,nSnow,nSoil,err,cmessage); ! model fluxes - case('bpar'); call allocLocal(bpar_meta,bparStruct,nSnow=0,nSoil=0,err=err,message=cmessage); ! basin-average params + case('bpar'); cycle ! set by file_access_actor case('bvar'); call allocLocal(bvar_meta,bvarStruct,nSnow=0,nSoil=0,err=err,message=cmessage); ! basin-average variables case('lookup'); call allocLocal(lookup_meta,lookupStruct,err=err,message=cmessage) ! basin-average variables case('deriv'); cycle @@ -271,14 +245,6 @@ contains endif end do ! looping through data structures - ! allocate space for default model parameters - ! NOTE: This is done here, rather than in the loop above, because dpar is not one of the "standard" data structures - call allocLocal(mpar_meta,dparStruct,nSnow,nSoil,err,cmessage); ! default model parameters - if(err/=0)then - message=trim(message)//trim(cmessage)//' [problem allocating dparStruct]' - print*, message - return - endif ! ***************************************************************************** ! *** allocate space for output statistics data structures -- GitLab