Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted_BMI_set_get_functions #127

Merged
merged 6 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 47 additions & 63 deletions src/bmi_cfe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,6 @@ static int Get_var_units (Bmi *self, const char *name, char * units)
return BMI_FAILURE;
}


static int Get_var_nbytes (Bmi *self, const char *name, int * nbytes)
{
int item_size;
Expand All @@ -1787,6 +1786,10 @@ static int Get_var_nbytes (Bmi *self, const char *name, int * nbytes)
for (i = 0; i < INPUT_VAR_NAME_COUNT; i++) {
if (strcmp(name, input_var_names[i]) == 0) {
item_count = input_var_item_count[i];
if (strcmp(name, "soil_moisture_profile") == 0 || strcmp(name, "soil_layer_depths_m") == 0) {
cfe_state_struct *cfe_ptr;
item_count = ((cfe_state_struct *)(self->data))->soil_reservoir.n_soil_layers + 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this +1?

Copy link
Contributor Author

@ajkhattak ajkhattak Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this +1 is actually a bug. I followed the concept from the initialization stage (here), what looks to me is that they tried to start the index at 1 (see), but the BMI Set_value does not work this way, so we need to fix it.

}
break;
}
}
Expand All @@ -1798,10 +1801,19 @@ static int Get_var_nbytes (Bmi *self, const char *name, int * nbytes)
}
}
}
if (item_count < 1) {
for (i = 0; i < PARAM_VAR_NAME_COUNT; i++) {
if (strcmp(name, param_var_names[i]) == 0) {
item_count = 1; // all of the calibratable parameters are (and will be?) scalars - AJK
break;
}
}
}
if (item_count < 1)
item_count = ((cfe_state_struct *) self->data)->num_timesteps;

*nbytes = item_size * item_count;

return BMI_SUCCESS;
}

Expand Down Expand Up @@ -2054,7 +2066,7 @@ static int Get_value_ptr (Bmi *self, const char *name, void **dest)

/**************************************************************************************************/
/**********Parameter Derived from config file - root zone adjusted AET development - rlm ***********/
if (strcmp (name, "soil__num_cells") == 0) {
if (strcmp (name, "soil_num_cells") == 0) {
cfe_state_struct *cfe_ptr;
cfe_ptr = (cfe_state_struct *) self->data;
*dest = (void*)&cfe_ptr->soil_reservoir.n_soil_layers;
Expand Down Expand Up @@ -2116,49 +2128,45 @@ static int Get_value (Bmi *self, const char *name, void *dest)
return Get_value_at_indices(self, name, dest, inds, 1);
}


static int Set_value_at_indices (Bmi *self, const char *name, int * inds, int len, void *src)
{
if (len < 1)
return BMI_FAILURE;
void * dest = NULL;
int itemsize = 0;

// Get "adjusted_index" for variable
int adjusted_index = Get_adjusted_index_for_variable(name);
if (adjusted_index < 0)
if (self->get_value_ptr(self, name, &dest) == BMI_FAILURE)
return BMI_FAILURE;

int var_item_size;
int status = Get_var_itemsize(self, name, &var_item_size);
if (status == BMI_FAILURE)
if (self->get_var_itemsize(self, name, &itemsize) == BMI_FAILURE)
return BMI_FAILURE;

// For now, all variables are non-array scalar values, with only 1 item of type double

// Thus, there is only ever one value to return (len must be 1) and it must always be from index 0
// ajk: modifying it to work with soil moisture column for rootzone depth based AET
if (strcmp(name, "soil_moisture_profile") == 0 || strcmp(name, "soil_layer_depths_m") == 0) { //Adding soil layer depths since they will be needed for root zone adjusted AET estimations -rlm
{ /* Copy the data */
ajkhattak marked this conversation as resolved.
Show resolved Hide resolved
size_t i;
size_t offset;
char * ptr;
for (i=0, ptr=(char*)src; i<len; i++, ptr+=itemsize) {
ajkhattak marked this conversation as resolved.
Show resolved Hide resolved
offset = inds[i] * itemsize;
memcpy ((char*)dest + offset, ptr, itemsize);
}
}

len = ((cfe_state_struct *)(self->data))->soil_reservoir.n_soil_layers + 1;
void *ptr = NULL; //(double*) malloc (sizeof (double)* len);
status = Get_value_ptr(self, name, &ptr);
return BMI_SUCCESS;

}

if (status == BMI_FAILURE)
return BMI_FAILURE;

memcpy(ptr, src, var_item_size * len);
static int Set_value (Bmi *self, const char *name, void *src)
{

return BMI_SUCCESS;
void * dest = NULL;
int nbytes = 0;

}
else if (len > 1 || inds[0] != 0)
if (self->get_value_ptr(self, name, &dest) == BMI_FAILURE)
return BMI_FAILURE;

void* ptr;
status = Get_value_ptr(self, name, &ptr);
if (status == BMI_FAILURE)
if (self->get_var_nbytes(self, name, &nbytes) == BMI_FAILURE)
return BMI_FAILURE;
memcpy(ptr, src, var_item_size * len);

memcpy (dest, src, nbytes);

if (strcmp (name, "maxsmc") == 0 || strcmp (name, "alpha_fc") == 0 || strcmp (name, "wltsmc") == 0 || strcmp (name, "maxsmc") == 0 || strcmp (name, "b") == 0 || strcmp (name, "slope") == 0 || strcmp (name, "satpsi") == 0 || strcmp (name, "Klf") == 0 || strcmp (name, "satdk") == 0){

Expand All @@ -2173,48 +2181,24 @@ static int Set_value_at_indices (Bmi *self, const char *name, int * inds, int le

if (strcmp (name, "N_nash_subsurface") == 0) {
cfe_state_struct* cfe_ptr = (cfe_state_struct *) self->data;
if( cfe_ptr->nash_storage_subsurface != NULL ) free(cfe_ptr->nash_storage_subsurface);
cfe_ptr->nash_storage_subsurface = malloc(sizeof(double) * cfe_ptr->N_nash_subsurface);
if( cfe_ptr->nash_storage_subsurface == NULL ) return BMI_FAILURE;
for (j = 0; j < cfe_ptr->N_nash_subsurface; j++)

if( cfe_ptr->nash_storage_subsurface != NULL )
free(cfe_ptr->nash_storage_subsurface);
cfe_ptr->nash_storage_subsurface = malloc(sizeof(double) * cfe_ptr->N_nash_subsurface);

if( cfe_ptr->nash_storage_subsurface == NULL )
return BMI_FAILURE;

for (j = 0; j < cfe_ptr->N_nash_subsurface; j++)
cfe_ptr->nash_storage_subsurface[j] = 0.0;
}

if (strcmp (name, "storage_max_m") == 0) {
cfe_state_struct* cfe_ptr = (cfe_state_struct *) self->data;
cfe_ptr->gw_reservoir.storage_m = cfe_ptr->gw_reservoir.gw_storage * cfe_ptr->gw_reservoir.storage_max_m;
}

return BMI_SUCCESS;
}


static int Set_value (Bmi *self, const char *name, void *array)
{
// Avoid using set value, call instead set_value_at_index
// Use nested call to "by index" version

// Here, for now at least, we know all the variables are scalar, so
int inds[] = {0};

// Then we can just ...
return Set_value_at_indices(self, name, inds, 1, array);


/* This is the sample code from read the docs
void * dest = NULL;
int nbytes = 0;

if (self->get_value_ptr(self, name, &dest) == BMI_FAILURE)
return BMI_FAILURE;

if (self->get_var_nbytes(self, name, &nbytes) == BMI_FAILURE)
return BMI_FAILURE;

memcpy (dest, array, nbytes);


return BMI_SUCCESS;
*/
}


Expand Down
12 changes: 6 additions & 6 deletions src/main_cfe_aorc_pet_rz_aet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,20 @@ int
Initializing the BMI model for CFE and AORC and Freeze-thaw model
************************************************************************/

printf("Initializeing BMI CFE model\n");
printf("Initializing BMI CFE model\n");
const char *cfg_file_cfe = argv[1];
cfe_bmi_model->initialize(cfe_bmi_model, cfg_file_cfe);

printf("Initializeing BMI AORC model\n");
printf("Initializing BMI AORC model\n");
const char *cfg_file_aorc = argv[2];
printf("AORC config file %s\n", cfg_file_aorc);
aorc_bmi_model->initialize(aorc_bmi_model, cfg_file_aorc);

printf("Initializeing BMI PET model\n");
printf("Initializing BMI PET model\n");
const char *cfg_file_pet = argv[3];
pet_bmi_model->initialize(pet_bmi_model, cfg_file_pet);

printf("Initializeing BMI Coupler model\n");
printf("Initializing BMI Coupler model\n");
const char *cfg_file_coupler = argv[4];
coupler_bmi.Initialize(cfg_file_coupler);

Expand Down Expand Up @@ -231,7 +231,7 @@ int
/************************************************************************
Now loop through time and call the models with the intermediate get/set
************************************************************************/
printf("looping through and calling updata\n");
printf("looping through and calling update \n");
if (cfe->verbosity > 0)
print_cfe_flux_header();
for (int i = 0; i < 24862; i++){
Expand Down Expand Up @@ -270,7 +270,7 @@ int
printf("PET value from PET is %8.9lf\n", pet->pet_m_per_s);
printf("PET value from CFE is %8.9lf\n", cfe->et_struct.potential_et_m_per_s);
}

cfe_bmi_model->update(cfe_bmi_model); // Update model 2

if (cfe->verbosity > 0)
Expand Down
Loading