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 2b633826ef4d52ea68cd892695af06aeb3e11cfc..03c7e93507ce59e5671f1239db82353510ea42bf 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 @@ -37,4 +37,8 @@ extern "C" { void overwriteParam(int* num_gru, int* err); void readParamFileAccessActor(int* start_gru, int* num_gru, int* err); + + void writeParamToNetCDF(void* handle_ncid, int* index_gru, int* index_hru, + void* handle_attr_struct, void* handle_type_struct, void* handle_mpar_struct, + void* handle_bpar_struct, int* err); } diff --git a/build/includes/global/auxilary.hpp b/build/includes/global/auxilary.hpp index 294940087d54722ff9b9f7953c4ac167d6ec7486..0a4c8fe220943c3dad402d3c7a96c947a09f0e9f 100644 --- a/build/includes/global/auxilary.hpp +++ b/build/includes/global/auxilary.hpp @@ -1,6 +1,17 @@ #pragma once - - +// Setters +void set_flagVec(std::vector<int>& arr_i, void* handle); +void set_var_i(std::vector<int>& arr_i, void* handle); +void set_var_d(std::vector<double> &arr_d, void* handle); +void set_var_i8(std::vector<long int>& arr_i, void* handle); +void set_i8length(std::vector<long int> &arr_i8length, void* handle); +void set_ilength(std::vector<int> &arr_ilength, void* handle); +void set_dlength(std::vector<double> &arr_dlength, void* handle); +void set_var_flagVec(std::vector<std::vector<int> > &mat, void* handle); +void set_var_ilength(std::vector<std::vector<int> > &mat, void* handle); +void set_var_i8length(std::vector<std::vector<long int> > &mat, void* handle); +void set_var_dlength(std::vector<std::vector<double> > &mat, void *handle); +// Getters std::vector<int> get_flagVec(void* handle); std::vector<int> get_var_i(void* handle); std::vector<double> get_var_d(void* handle); diff --git a/build/makefile_sundials b/build/makefile_sundials index 15a8c726b56692c9e6e67f7e86b0b3fde4b0c7b3..842265e9caed5accf63c4c0e850e0903c1b55544 100644 --- a/build/makefile_sundials +++ b/build/makefile_sundials @@ -165,7 +165,8 @@ SUMMA_FILEACCESS_INTERFACE = \ deallocateOutputStruc.f90 \ cppwrap_fileAccess.f90 \ read_attribute_all_hru.f90 \ - read_param_all_hru.f90 + read_param_all_hru.f90 \ + file_access_actor.f90 FILEACCESS_INTERFACE = $(patsubst %, $(FILE_ACCESS_DIR)/%, $(SUMMA_FILEACCESS_INTERFACE)) diff --git a/build/source/actors/file_access_actor/cppwrap_fileAccess.f90 b/build/source/actors/file_access_actor/cppwrap_fileAccess.f90 index 3b2dee5a4c898cecaede7a6e3950fb360918a741..7089a578692bc1d8d2f267351367d82223a725d9 100644 --- a/build/source/actors/file_access_actor/cppwrap_fileAccess.f90 +++ b/build/source/actors/file_access_actor/cppwrap_fileAccess.f90 @@ -343,6 +343,9 @@ subroutine FileAccessActor_WriteOutput(& end subroutine + + + subroutine FileAccessActor_DeallocateStructures(handle_forcFileInfo, handle_ncid) bind(C,name="FileAccessActor_DeallocateStructures") USE netcdf_util_module,only:nc_file_close USE globalData,only:structInfo ! information on the data structures diff --git a/build/source/actors/file_access_actor/file_access_actor.cpp b/build/source/actors/file_access_actor/file_access_actor.cpp index ad27a3bbb9200540c2f3b1d841d017a2fff39ef0..d1003deb85b6b409d4345be08f46ea0127cef791 100644 --- a/build/source/actors/file_access_actor/file_access_actor.cpp +++ b/build/source/actors/file_access_actor/file_access_actor.cpp @@ -7,6 +7,7 @@ #include "message_atoms.hpp" #include "global.hpp" #include "json.hpp" +#include "auxilary.hpp" using json = nlohmann::json; @@ -52,13 +53,28 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int startGRU } }, - [=](write_param, int indxGRU, int indxHRU) { - int err; - err = 0; - Write_HRU_Param(self->state.handle_ncid, &indxGRU, &indxHRU, &err); - if (err != 0) { - aout(self) << "ERROR: Write_HRU_PARAM -- For HRU = " << indxHRU << "\n"; - } + [=](write_param, int index_gru, int index_hru, std::vector<double> attr_struct, + std::vector<int> type_struct, std::vector<std::vector<double>> mpar_struct, + std::vector<double> bpar_struct) { + int err = 0; + 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(); + + 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); + + writeParamToNetCDF(self->state.handle_ncid, &index_gru, &index_hru, + handle_attr_struct, handle_type_struct, handle_mpar_struct, + handle_bpar_struct, &err); + + // Write_HRU_Param(self->state.handle_ncid, &index_gru, &index_hru, &err); + // if (err != 0) { + // aout(self) << "ERROR: Write_HRU_PARAM -- For HRU = " << index_hru << "\n"; + // } }, [=](access_forcing, int currentFile, caf::actor refToRespondTo) { @@ -128,10 +144,10 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int startGRU int err; int returnMessage = 9999; - err = writeOutput(self, indxGRU, indxHRU, numStepsToWrite, returnMessage, refToRespondTo); - if (err != 0) { - aout(self) << "FILE_ACCESS_ACTOR - ERROR Writing Output \n"; - } + // err = writeOutput(self, indxGRU, indxHRU, numStepsToWrite, returnMessage, refToRespondTo); + // if (err != 0) { + // aout(self) << "FILE_ACCESS_ACTOR - ERROR Writing Output \n"; + // } }, [=](read_and_write, int indxGRU, int indxHRU, int numStepsToWrite, int currentFile, @@ -142,9 +158,9 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int startGRU if (err != 0) aout(self) << "\nERROR: FILE_ACCESS_ACTOR - READING_FORCING FAILED\n"; - err = writeOutput(self, indxGRU, indxHRU, numStepsToWrite, currentFile, refToRespondTo); - if (err != 0) - aout(self) << "FILE_ACCESS_ACTOR - ERROR Writing Output \n"; + // err = writeOutput(self, indxGRU, indxHRU, numStepsToWrite, currentFile, refToRespondTo); + // if (err != 0) + // aout(self) << "FILE_ACCESS_ACTOR - ERROR Writing Output \n"; }, [=](run_failure, int indxGRU) { @@ -212,17 +228,11 @@ behavior file_access_actor(stateful_actor<file_access_state>* self, int startGRU std::vector<double> bparSturct, std::vector<std::vector<double>> bvarStruct, // ancillary data structures - std::vector<double> dparStruct) { - aout(self) << "Receieved HRU Data\n"; - - - - - - - + std::vector<double> dparStruct, + std::vector<int> finalizeStats) { + aout(self) << "Receieved HRU Data\n"; }, @@ -352,35 +362,39 @@ int write(stateful_actor<file_access_state>* self, int listIndex) { return 0; } -int writeOutput(stateful_actor<file_access_state>* self, int indxGRU, int indxHRU, - int numStepsToWrite, int returnMessage, caf::actor actorRef) { - self->state.file_access_timing.updateStartPoint("write_duration"); - - if (debug) { - aout(self) << "Recieved Write Request From GRU: " << indxGRU << "\n"; - } - int err = 0; - int listIndex = self->state.output_manager->addActor(actorRef, indxGRU, returnMessage, numStepsToWrite); - if (self->state.output_manager->isFull(listIndex)) { - if (debug) { - aout(self) << "List with Index " << listIndex << " is full and ready to write\n"; - aout(self) << "Minimum GRU Index = " << self->state.output_manager->getMinIndex(listIndex) << "\n"; - aout(self) << "Maximum GRU Index = " << self->state.output_manager->getMaxIndex(listIndex) << "\n"; - } - - err = write(self, listIndex); - - } else { - if (debug) { - aout(self) << "List with Index " << listIndex << " is not full yet waiting to write\n"; - aout(self) << "Size of list is " << self->state.output_manager->getSize(listIndex) << "\n"; - } - } +// int writeOutput(stateful_actor<file_access_state>* self, ) { + +// } + +// int writeOutput(stateful_actor<file_access_state>* self, int indxGRU, int indxHRU, +// int numStepsToWrite, int returnMessage, caf::actor actorRef) { +// self->state.file_access_timing.updateStartPoint("write_duration"); + +// if (debug) { +// aout(self) << "Recieved Write Request From GRU: " << indxGRU << "\n"; +// } +// int err = 0; +// int listIndex = self->state.output_manager->addActor(actorRef, indxGRU, returnMessage, numStepsToWrite); +// if (self->state.output_manager->isFull(listIndex)) { +// if (debug) { +// aout(self) << "List with Index " << listIndex << " is full and ready to write\n"; +// aout(self) << "Minimum GRU Index = " << self->state.output_manager->getMinIndex(listIndex) << "\n"; +// aout(self) << "Maximum GRU Index = " << self->state.output_manager->getMaxIndex(listIndex) << "\n"; +// } + +// err = write(self, listIndex); + +// } else { +// if (debug) { +// aout(self) << "List with Index " << listIndex << " is not full yet waiting to write\n"; +// aout(self) << "Size of list is " << self->state.output_manager->getSize(listIndex) << "\n"; +// } +// } - self->state.file_access_timing.updateEndPoint("write_duration"); - return err; +// self->state.file_access_timing.updateEndPoint("write_duration"); +// return err; -} +// } int readForcing(stateful_actor<file_access_state>* self, int currentFile) { // Check if we have already loaded this file diff --git a/build/source/actors/file_access_actor/file_access_actor.f90 b/build/source/actors/file_access_actor/file_access_actor.f90 new file mode 100644 index 0000000000000000000000000000000000000000..5af004c2983ee4d1da4c5154442f269e06163ba5 --- /dev/null +++ b/build/source/actors/file_access_actor/file_access_actor.f90 @@ -0,0 +1,98 @@ +module file_access_actor +USE, intrinsic :: iso_c_binding +USE nrtype +USE data_types + +implicit none +public::writeParamToNetCDF +public::writeDataToNetCDF +public::writeBasinToNetCDF +public::writeTimeToNetCDF + +contains + +! Subroutine that writes data from the HRU actor to be written to netcdf +subroutine writeParamToNetCDF(handle_ncid, & + index_gru, & + index_hru, & + handle_attr_struct, & + handle_type_struct, & + handle_mpar_struct, & + handle_bpar_struct, & + err) bind(C, name="writeParamToNetCDF") + USE globalData,only:attr_meta,type_meta,mpar_meta,bpar_meta ! meta structures + USE globalData,only:gru_struc + USE writeOutput_module,only:writeParm + USE globalData,only:structInfo ! information on the data structures + implicit none + ! dummy variables + type(c_ptr), intent(in), value :: handle_ncid ! ncid of the output file + integer(c_int),intent(in) :: index_gru ! index of GRU in gru_struc + integer(c_int),intent(in) :: index_hru ! index of HRU in gru_struc + 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_mpar_struct + type(c_ptr), intent(in), value :: handle_bpar_struct + integer(c_int),intent(out) :: err + ! local variables pointers + type(var_i), pointer :: ncid + type(var_d), pointer :: attr_struct + type(var_i), pointer :: type_struct + type(var_dlength), pointer :: mpar_struct + type(var_d),pointer :: bpar_struct + ! local variables + integer(i4b) :: iStruct + character(LEN=256) :: cmessage + character(LEN=256) :: message + ! --------------------------------------------------------------------------------------- + ! * Convert From C++ to Fortran + ! --------------------------------------------------------------------------------------- + message="file_access_actor.f90 - writeParamToNetCDF" + call c_f_pointer(handle_ncid, ncid) + call c_f_pointer(handle_attr_struct, attr_struct) + call c_f_pointer(handle_type_struct, type_struct) + call c_f_pointer(handle_mpar_struct, mpar_struct) + call c_f_pointer(handle_bpar_struct, bpar_struct) + + do iStruct=1,size(structInfo) + select case(trim(structInfo(iStruct)%structName)) + case('attr'); call writeParm(ncid,gru_struc(index_gru)%hruInfo(index_hru)%hru_ix, & + attr_struct,attr_meta,err,cmessage) + case('type'); call writeParm(ncid,gru_struc(index_gru)%hruInfo(index_hru)%hru_ix, & + type_struct,type_meta,err,cmessage) + case('mpar'); call writeParm(ncid,gru_struc(index_gru)%hruInfo(index_hru)%hru_ix, & + mpar_struct,mpar_meta,err,cmessage) + end select + if(err/=0)then + message=trim(message)//trim(cmessage)//'['//trim(structInfo(iStruct)%structName)//']' + print*, message + return + endif + end do + + ! write GRU parameters + call writeParm(ncid,index_gru,bpar_struct,bpar_meta,err,cmessage) + if(err/=0)then + message=trim(message)//trim(cmessage) + print*, message + return + endif + +end subroutine writeParamToNetCDF + +subroutine writeDataToNetCDF() bind(C, name="writeDataToNetCDF") +implicit none +end subroutine writeDataToNetCDF + +subroutine writeBasinToNetCDF() bind(C, name="writeBasinToNetCDF") +implicit none +end subroutine writeBasinToNetCDF + +subroutine writeTimeToNetCDF() bind(C, name="writeTimeToNetCDF") +implicit none +end subroutine writeTimeToNetCDF + + + + +end module file_access_actor \ No newline at end of file diff --git a/build/source/actors/global/auxiliary.cpp b/build/source/actors/global/auxiliary.cpp index 745f156b8fa556e1b45a61d3945abde9d7231dce..060fce1ed47a5965a8687e6dff6a692bad845e11 100644 --- a/build/source/actors/global/auxiliary.cpp +++ b/build/source/actors/global/auxiliary.cpp @@ -9,35 +9,35 @@ * These are all of the functions to get the Fortran data types into C * / /*************** SET DATA **************/ -void set_flagVec(const std::vector<int>& arr_i, void* handle) { +void set_flagVec(std::vector<int>& arr_i, void* handle) { set_data_flagVec(handle, &arr_i[0], arr_i.size()); } -void set_var_i(const std::vector<int>& arr_i, void* handle) { +void set_var_i(std::vector<int>& arr_i, void* handle) { set_data_var_i(handle, &arr_i[0], arr_i.size()); } -void set_var_d(const std::vector<double> &arr_d, void* handle) { +void set_var_d(std::vector<double> &arr_d, void* handle) { set_data_var_d(handle, &arr_d[0], arr_d.size()); } -void set_var_i8(const std::vector<long int>& arr_i, void* handle) { - ::set_data_var_i8(handle, &arr_i[0], arr_i.size()); +void set_var_i8(std::vector<long int>& arr_i, void* handle) { + set_data_var_i8(handle, &arr_i[0], arr_i.size()); } -void set_i8length(const std::vector<long int> &arr_i8length, void* handle) { +void set_i8length(std::vector<long int> &arr_i8length, void* handle) { set_data_i8length(handle, &arr_i8length[0], arr_i8length.size()); } -void set_ilength(const std::vector<int> &arr_ilength, void* handle) { +void set_ilength(std::vector<int> &arr_ilength, void* handle) { set_data_ilength(handle, &arr_ilength[0], arr_ilength.size()); } -void set_dlength(const std::vector<double> &arr_dlength, void* handle) { +void set_dlength(std::vector<double> &arr_dlength, void* handle) { set_data_dlength(handle, &arr_dlength[0], arr_dlength.size()); } -void set_var_flagVec(const std::vector<std::vector<int> > &mat, void* handle) { +void set_var_flagVec(std::vector<std::vector<int> > &mat, void* handle) { size_t num_row = mat.size(); std::vector<int> num_col( num_row ); @@ -54,7 +54,7 @@ void set_var_flagVec(const std::vector<std::vector<int> > &mat, void* handle) { set_data_var_flagVec(handle, &array[0], num_row, &num_col[0], num_elements); } -void set_var_ilength(const std::vector<std::vector<int> > &mat, void* handle) { +void set_var_ilength(std::vector<std::vector<int> > &mat, void* handle) { size_t num_row = mat.size(); std::vector<int> num_col( num_row ); @@ -71,7 +71,7 @@ void set_var_ilength(const std::vector<std::vector<int> > &mat, void* handle) { set_data_var_ilength(handle, &array[0], num_row, &num_col[0], num_elements); } -void set_var_i8length(const std::vector<std::vector<long int> > &mat, void* handle) { +void set_var_i8length(std::vector<std::vector<long int> > &mat, void* handle) { size_t num_row = mat.size(); std::vector<int> num_col( num_row ); @@ -88,7 +88,7 @@ void set_var_i8length(const std::vector<std::vector<long int> > &mat, void* hand set_data_var_i8length(handle, &array[0], num_row, &num_col[0], num_elements); } -void set_var_dlength(const std::vector<std::vector<double> > &mat, void *handle) { +void set_var_dlength(std::vector<std::vector<double> > &mat, void *handle) { size_t num_row = mat.size(); std::vector<int> num_col( num_row ); diff --git a/build/source/actors/hru_actor/hru_actor.cpp b/build/source/actors/hru_actor/hru_actor.cpp index 812f3345bc7076a54703aa86afb10fdfbaba4e09..467c59e2d573d5f00ff0320f2ea24de757e424b3 100644 --- a/build/source/actors/hru_actor/hru_actor.cpp +++ b/build/source/actors/hru_actor/hru_actor.cpp @@ -66,13 +66,19 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, err = 0; // Write Paramaters to OutputStruc - Write_Param_C(&self->state.indxGRU, &self->state.indxHRU, - self->state.handle_attrStruct, self->state.handle_typeStruct, - self->state.handle_mparStruct, self->state.handle_bparStruct, - &err); + // Write_Param_C(&self->state.indxGRU, &self->state.indxHRU, + // self->state.handle_attrStruct, self->state.handle_typeStruct, + // self->state.handle_mparStruct, self->state.handle_bparStruct, + // &err); + std::vector<double> attr_struct_array = get_var_d(self->state.handle_attrStruct); + std::vector<int> type_struct_array = get_var_i(self->state.handle_typeStruct); + std::vector<std::vector<double>> mpar_struct_array = get_var_dlength(self->state.handle_mparStruct); + std::vector<double> bpar_struct_array = get_var_d(self->state.handle_bparStruct); // ask file_access_actor to write paramaters - self->send(self->state.file_access_actor, write_param_v, self->state.indxGRU, self->state.indxHRU); + self->send(self->state.file_access_actor, write_param_v, + self->state.indxGRU, self->state.indxHRU, attr_struct_array, + type_struct_array, mpar_struct_array, bpar_struct_array); self->send(self->state.file_access_actor, access_forcing_v, self->state.iFile, self); self->state.hru_timing.updateEndPoint("total_duration"); }, @@ -172,6 +178,8 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, // ancillary data structures std::vector<double> dpar_struct_array = get_var_d(self->state.handle_dparStruct); + std::vector<int> finalize_stats_array = get_flagVec(self->state.handle_finalizeStats); + self->send(self->state.file_access_actor, serialized_hru_data_v, // Statistic Structures forc_stat_array, @@ -196,7 +204,8 @@ behavior hru_actor(stateful_actor<hru_state>* self, int refGRU, int indxGRU, bpar_struct_array, bvar_struct_array, // ancillary data structures - dpar_struct_array); + dpar_struct_array, + finalize_stats_array); },