Skip to content

Commit

Permalink
Added stand_alone to init() and init_discharge, and edited to allocat…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
Ben-Choat authored and madMatchstick committed Jan 12, 2024
1 parent d28a857 commit 3600ba7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/topmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/bmi_topmodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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 )
Expand Down Expand Up @@ -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);
}
Expand Down
37 changes: 23 additions & 14 deletions src/topmodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3600ba7

Please sign in to comment.