Skip to content

Commit

Permalink
fixed bug related to +1 in the AET rootzone memory allocation, remove…
Browse files Browse the repository at this point in the history
…d unused variables num_cells and soil_layer_depths.
  • Loading branch information
ajkhattak committed Aug 30, 2024
1 parent 3f43749 commit e59aeb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
45 changes: 18 additions & 27 deletions src/bmi_cfe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,10 @@ int read_init_config_cfe(const char* config_file, cfe_state_struct* model)
return BMI_FAILURE;
}

model->soil_reservoir.soil_layer_depths_m = malloc(sizeof(double) * (model->soil_reservoir.n_soil_layers + 1));
model->soil_reservoir.soil_layer_depths_m = malloc(sizeof(double) * (model->soil_reservoir.n_soil_layers));
copy = soil_layer_depths_string_val;

i = 1;
i = 0;
while((value = strsep(&copy, ",")) != NULL) {
model->soil_reservoir.soil_layer_depths_m[i++] = strtod(value, NULL);
}
Expand All @@ -1224,7 +1224,7 @@ int read_init_config_cfe(const char* config_file, cfe_state_struct* model)

//Check that the last depth read in from the cfe config file (soil_layer_depths) matches the total depth (soil_params.depth)
//from the cfe config file.
if(model->NWM_soil_params.D != model->soil_reservoir.soil_layer_depths_m[model->soil_reservoir.n_soil_layers]){
if(model->NWM_soil_params.D != model->soil_reservoir.soil_layer_depths_m[model->soil_reservoir.n_soil_layers - 1]){
printf("WARNING: soil_params.depth is not equal to the last soil layer depth in the CFE config file! Aborting...\n");
return BMI_FAILURE;
}
Expand All @@ -1234,10 +1234,10 @@ int read_init_config_cfe(const char* config_file, cfe_state_struct* model)
// Calculate the thickness (delta) of each soil layer
if (is_aet_rootzone_set == TRUE ) {
model->soil_reservoir.is_aet_rootzone = TRUE;
model->soil_reservoir.delta_soil_layer_depth_m = malloc(sizeof(double) * (model->soil_reservoir.n_soil_layers + 1));
model->soil_reservoir.delta_soil_layer_depth_m = malloc(sizeof(double) * (model->soil_reservoir.n_soil_layers));
double previous_depth = 0;
double current_depth = 0;
for (int i=1; i <= model->soil_reservoir.n_soil_layers; i++) {
for (int i=0; i <model->soil_reservoir.n_soil_layers; i++) {
current_depth = model->soil_reservoir.soil_layer_depths_m[i];
if (current_depth <= previous_depth)
printf("WARNING: soil depths may be out of order. One or more soil layer depths is less than or equal to the previous layer. Check CFE config file.\n");
Expand Down Expand Up @@ -1474,7 +1474,7 @@ static int Initialize (Bmi *self, const char *file)
cfe_bmi_data_ptr->soil_reservoir.ice_fraction_xinanjiang = 0.0;

if (cfe_bmi_data_ptr->soil_reservoir.is_aet_rootzone == TRUE)
cfe_bmi_data_ptr->soil_reservoir.smc_profile = malloc(sizeof(double)*cfe_bmi_data_ptr->soil_reservoir.n_soil_layers + 1);
cfe_bmi_data_ptr->soil_reservoir.smc_profile = malloc(sizeof(double)*cfe_bmi_data_ptr->soil_reservoir.n_soil_layers);
else
cfe_bmi_data_ptr->soil_reservoir.smc_profile = malloc(sizeof(double)*1);

Expand Down Expand Up @@ -1786,9 +1786,9 @@ 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) {
if (strcmp(name, "soil_moisture_profile") == 0) {
cfe_state_struct *cfe_ptr;
item_count = ((cfe_state_struct *)(self->data))->soil_reservoir.n_soil_layers + 1;
item_count = ((cfe_state_struct *)(self->data))->soil_reservoir.n_soil_layers;
}
break;
}
Expand Down Expand Up @@ -2065,15 +2065,8 @@ 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) {
cfe_state_struct *cfe_ptr;
cfe_ptr = (cfe_state_struct *) self->data;
*dest = (void*)&cfe_ptr->soil_reservoir.n_soil_layers;
return BMI_SUCCESS;
}
/**********Parameter Derived from config file - root zone adjusted AET development - rlm -ajk ***********/

//--------------Root zone adjusted AET development -rlm -ajk -------
if (strcmp (name, "soil_moisture_profile") == 0) {
*dest = (void *) ((cfe_state_struct *)(self->data))->soil_reservoir.smc_profile;
return BMI_SUCCESS;
Expand Down Expand Up @@ -2139,17 +2132,15 @@ static int Set_value_at_indices (Bmi *self, const char *name, int * inds, int le
if (self->get_var_itemsize(self, name, &itemsize) == BMI_FAILURE)
return BMI_FAILURE;

{ /* Copy the data */
size_t i;
size_t offset;
char * ptr;
// iterate over the source pointer, src, by itemsize byte chunks
// and set the destination pointer, dest, to the value in src
// based on the linear offset provided by inds[i]
for (i=0, ptr=(char*)src; i<len; i++, ptr+=itemsize) {
offset = inds[i] * itemsize;
memcpy ((char*)dest + offset, ptr, itemsize);
}
size_t i;
size_t offset;
char * ptr;
// iterate over the source pointer, src, by itemsize byte chunks
// and set the destination pointer, dest, to the value in src
// based on the linear offset provided by inds[i]
for (i=0, ptr=(char*)src; i<len; i++, ptr+=itemsize) {
offset = inds[i] * itemsize;
memcpy ((char*)dest + offset, ptr, itemsize);
}

return BMI_SUCCESS;
Expand Down
5 changes: 2 additions & 3 deletions src/main_cfe_aorc_pet_rz_aet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ void pass_smc_from_coupler_to_cfe(Bmi *cfe_bmi_model,BmiSoilMoistureProfile coup
coupler_bmi.SetValue("soil_moisture_layered",&smc_layers[0]);
coupler_bmi.Update();
}
double *smct = new double[n_soil_layers + 1];
coupler_bmi.GetValue("soil_moisture_profile",&smct[1]);
double *smct = new double[n_soil_layers];
coupler_bmi.GetValue("soil_moisture_profile",&smct[0]);
cfe_bmi_model->set_value(cfe_bmi_model, "soil_moisture_profile", &smct[0]);

}


/***************************************************************
Function to pass the forcing data from AORC to PET using BMI.
This requires a lot of getters and setters,
Expand Down

0 comments on commit e59aeb8

Please sign in to comment.