From 3600ba7871632d3fbeb1c2f9f2415bc2a342218c Mon Sep 17 00:00:00 2001 From: Ben Choat Date: Fri, 22 Sep 2023 15:23:28 -0700 Subject: [PATCH] Added stand_alone to init() and init_discharge, and edited to allocate Q differently depending on if stand alon emode or not. Seeing discrepancies between master stand_alone results and current stand_alone results. Differences exist before these edits. --- include/topmodel.h | 4 ++-- src/bmi_topmodel.c | 12 ++++++------ src/topmodel.c | 37 +++++++++++++++++++++++-------------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/include/topmodel.h b/include/topmodel.h index d9bc6b1..af5030e 100755 --- a/include/topmodel.h +++ b/include/topmodel.h @@ -38,11 +38,11 @@ extern void init_water_balance( double **deficit_local, double **deficit_root_zone, double *sbar, double *bal); -extern void init_discharge_array(int *num_delay, double *Q0, double area, +extern void init_discharge_array(int stand_alone, int *num_delay, double *Q0, double area, int *num_time_delay_histo_ords, double **time_delay_histogram, double **Q); -extern void init(FILE *in_param_fptr, FILE *output_fptr, char *subcat, +extern void init(FILE *in_param_fptr, FILE *output_fptr, char *subcat, int stand_alone, int num_channels, int num_topodex_values, int yes_print_output, double area, double **time_delay_histogram, double *cum_dist_area_with_dist, double dt, diff --git a/src/bmi_topmodel.c b/src/bmi_topmodel.c index bce0e81..a41bdf3 100755 --- a/src/bmi_topmodel.c +++ b/src/bmi_topmodel.c @@ -302,9 +302,8 @@ int init_config(const char* config_file, topmodel_model* model) &model->cum_dist_area_with_dist,&model->tl,&model->dist_from_outlet); fclose(model->subcat_fptr); - - - init(model->params_fptr,model->output_fptr,model->subcat,model->num_channels,model->num_topodex_values, + + init(model->params_fptr,model->output_fptr,model->subcat,model->stand_alone, model->num_channels, model->num_topodex_values, model->yes_print_output,model->area,&model->time_delay_histogram,model->cum_dist_area_with_dist, model->dt,model->tl,model->dist_from_outlet,&model->num_time_delay_histo_ords,&model->num_delay, &model->szm,&model->t0,&model->chv,&model->rv,&model->td, &model->srmax, @@ -473,6 +472,7 @@ static int Update_until (Bmi *self, double t) static int Finalize (Bmi *self) { + if (self){ topmodel_model* model = (topmodel_model *)(self->data); @@ -494,9 +494,9 @@ static int Finalize (Bmi *self) // framework-controlled mode. //----------------------------------------------------------- if (model->stand_alone == TRUE){ - results(model->output_fptr,model->out_hyd_fptr,model->nstep, + results(model->output_fptr,model->out_hyd_fptr,model->nstep, model->Qobs, model->Q, model->yes_print_output); - } + } } if( model->Q != NULL ) @@ -1045,7 +1045,7 @@ static int Set_value (Bmi *self, const char *name, void *array) &topmodel->num_delay, &topmodel->time_delay_histogram); free(tch); // Reinitialise discharge array - init_discharge_array(&topmodel->num_delay, &topmodel->Q0, topmodel->area, + init_discharge_array(topmodel->stand_alone, &topmodel->num_delay, &topmodel->Q0, topmodel->area, &topmodel->num_time_delay_histo_ords, &topmodel->time_delay_histogram, &topmodel->Q); } diff --git a/src/topmodel.c b/src/topmodel.c index 16f17bb..af8f42f 100755 --- a/src/topmodel.c +++ b/src/topmodel.c @@ -142,8 +142,6 @@ extern void topmod(FILE *output_fptr, int nstep, int num_topodex_values, /* BMI Adaption: current_time_step, *sump, *sumae, *sumq, stand_alone added as function input parameters */ -//shift Q array to align current time step -shift_Q(Q, num_delay + num_time_delay_histo_ords); double ex[num_topodex_values+1]; //+1 to maintin 1 based array indexing //NJF TODO consider warning on all program limits here since this is essentially @@ -175,8 +173,15 @@ if(yes_print_output==TRUE && current_time_step==1) /* BMI Adaption: Set iteration to bmi's current_time_step (standalone) or 1 (framework) Counter++ is handled by bmi's update()*/ -if (stand_alone == TRUE)it=current_time_step; -else it=1; +if (stand_alone == TRUE) { + it=current_time_step; +} +else { + it=1; + //shift Q array to align current time step + shift_Q(Q, num_delay + num_time_delay_histo_ords); +} + *qof=0.0; *quz=0.0; @@ -718,6 +723,7 @@ extern void calc_time_delay_histogram(int num_channels, double area, /** * Function to (re)initialize discharge array + * @params[in] stand_alone, int, 0 for running with ngen and 1 for running in stand alone mode * @params[in] num_delay, pointer of type int, number of time steps lag (delay) in channel within * catchment to outlet. Output from calc_time_delay_histogram * @params[in] Q0, pointer of type double, initial subsurface flow per unit area @@ -730,18 +736,20 @@ extern void calc_time_delay_histogram(int num_channels, double area, * * @params[out] Q, pointer of type double and length num_delay+num_time_delay_histo_ords, simulated discharge */ - -extern void init_discharge_array(int *num_delay, double *Q0, double area, +extern void init_discharge_array(int stand_alone, int *num_delay, double *Q0, double area, int *num_time_delay_histo_ords, double **time_delay_histogram, double **Q) { - //if Q is already allocated, need to free and re-compute the required size - if(*Q != NULL){ - free(*Q); - *Q = NULL; + // if not in stand alone mode, then reallocate Q + if(stand_alone != TRUE) { + // if Q is already allocated, need to free and re-compute the required size + if(*Q != NULL){ + free(*Q); + *Q = NULL; + } + //*Q = calloc(*num_delay + *num_time_delay_histo_ords + 1, sizeof(double)); + d_alloc(Q, *num_delay + *num_time_delay_histo_ords); } - //*Q = calloc(*num_delay + *num_time_delay_histo_ords + 1, sizeof(double)); - d_alloc(Q, *num_delay + *num_time_delay_histo_ords); // declare local variables double sum; @@ -841,6 +849,7 @@ extern void init_water_balance( * @params[in] in_param_fptr, FILE pointer, file with parameters (e.g., params.dat) * @params[in] output_fptr, FILE pointer, file to which output will be written (e.g., topmod-cat.out) * @params[in] subcat, pointer of type char, name of subcatchment, read in from in_param_fptr file + * @params[in] stand_alone, int, 0 for running with ngen and 1 for running in stand alone mode * @params[in] num_channels, int, defined in subcat.dat file * @params[in] num_topodex_values, int, number of topodex histogram values * (i.e., number of A/TANB ordinates) @@ -902,7 +911,7 @@ extern void init_water_balance( * @params[out] bal, pointer of type double, residual of water balance * */ -extern void init(FILE *in_param_fptr, FILE *output_fptr, char *subcat, +extern void init(FILE *in_param_fptr, FILE *output_fptr, char *subcat, int stand_alone, int num_channels, int num_topodex_values, int yes_print_output, double area, double **time_delay_histogram, double *cum_dist_area_with_dist, double dt, @@ -974,7 +983,7 @@ if(yes_print_output==TRUE) // Reinitialise discharge array -init_discharge_array(num_delay, Q0, area, +init_discharge_array(stand_alone, num_delay, Q0, area, num_time_delay_histo_ords, time_delay_histogram, Q);