Skip to content

Commit 049476b

Browse files
committed
test(bmi_c): update bmi C test model code to implement the mass balance protocol semantics
1 parent 64c5758 commit 049476b

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

extern/test_bmi_c/include/bmi_test_bmi_c.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef BMI_TEST_BMI_C_H
22
#define BMI_TEST_BMI_C_H
33

4+
#define NGEN_MASS_IN "ngen::mass_in"
5+
#define NGEN_MASS_OUT "ngen::mass_out"
6+
#define NGEN_MASS_STORED "ngen::mass_stored"
7+
#define NGEN_MASS_LEAKED "ngen::mass_leaked"
8+
49
#if defined(__cplusplus)
510
extern "C" {
611
#endif

extern/test_bmi_c/include/test_bmi_c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ struct test_bmi_c_model {
3131
int param_var_1;
3232
double param_var_2;
3333
double* param_var_3;
34+
35+
double mass_stored; // Mass balance variable, for testing purposes
36+
double mass_leaked; //Mass balance variable, for testing purposes
3437
};
3538
typedef struct test_bmi_c_model test_bmi_c_model;
3639

extern/test_bmi_c/src/bmi_test_bmi_c.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define INPUT_VAR_NAME_COUNT 2
1010
#define OUTPUT_VAR_NAME_COUNT 2
1111
#define PARAM_VAR_NAME_COUNT 3
12+
#define MASS_BALANCE_VAR_NAME_COUNT 4
1213

1314
// Don't forget to update Get_value/Get_value_at_indices (and setter) implementation if these are adjusted
1415
static const char *output_var_names[OUTPUT_VAR_NAME_COUNT] = { "OUTPUT_VAR_1", "OUTPUT_VAR_2" };
@@ -34,6 +35,13 @@ static const int param_var_item_count[PARAM_VAR_NAME_COUNT] = { 1, 1, 2 };
3435
static const char *param_var_grids[PARAM_VAR_NAME_COUNT] = { 0, 0, 0 };
3536
static const char *param_var_locations[PARAM_VAR_NAME_COUNT] = { "node", "node", "node" };
3637

38+
static const char *mass_balance_var_names[MASS_BALANCE_VAR_NAME_COUNT] = { NGEN_MASS_IN, NGEN_MASS_OUT, NGEN_MASS_STORED, NGEN_MASS_LEAKED};
39+
static const char *mass_balance_var_types[MASS_BALANCE_VAR_NAME_COUNT] = { "double", "double", "double", "double"};
40+
static const char *mass_balance_var_units[MASS_BALANCE_VAR_NAME_COUNT] = { "m", "m", "m", "m" };
41+
static const int mass_balance_var_item_count[MASS_BALANCE_VAR_NAME_COUNT] = { 1, 1, 1, 1};
42+
static const char *mass_balance_var_grids[MASS_BALANCE_VAR_NAME_COUNT] = { 0, 0, 0, 0 };
43+
static const char *mass_balance_var_locations[MASS_BALANCE_VAR_NAME_COUNT] = { "node", "node", "node", "node" };
44+
3745
static int Finalize (Bmi *self)
3846
{
3947
// Function assumes everything that is needed is retrieved from the model before Finalize is called.
@@ -387,6 +395,23 @@ static int Get_value_ptr (Bmi *self, const char *name, void **dest)
387395
*dest = ((test_bmi_c_model *)(self->data))->param_var_3;
388396
return BMI_SUCCESS;
389397
}
398+
399+
if (strcmp (name, NGEN_MASS_IN) == 0) {
400+
*dest = ((test_bmi_c_model *)(self->data))->input_var_1;
401+
return BMI_SUCCESS;
402+
}
403+
if (strcmp (name, NGEN_MASS_OUT) == 0) {
404+
*dest = ((test_bmi_c_model *)(self->data))->output_var_1;
405+
return BMI_SUCCESS;
406+
}
407+
if (strcmp (name, NGEN_MASS_STORED) == 0) {
408+
*dest = &((test_bmi_c_model *)(self->data))->mass_stored;
409+
return BMI_SUCCESS;
410+
}
411+
if (strcmp (name, NGEN_MASS_LEAKED) == 0) {
412+
*dest = &((test_bmi_c_model *)(self->data))->mass_leaked;
413+
return BMI_SUCCESS;
414+
}
390415
return BMI_FAILURE;
391416
}
392417

@@ -483,6 +508,14 @@ static int Get_var_nbytes (Bmi *self, const char *name, int * nbytes)
483508
}
484509
}
485510
}
511+
if (item_count < 1) {
512+
for (i = 0; i < MASS_BALANCE_VAR_NAME_COUNT; i++) {
513+
if (strcmp(name, mass_balance_var_names[i]) == 0) {
514+
item_count = mass_balance_var_item_count[i];
515+
break;
516+
}
517+
}
518+
}
486519
if (item_count < 1)
487520
item_count = ((test_bmi_c_model *) self->data)->num_time_steps;
488521

@@ -515,6 +548,13 @@ static int Get_var_type (Bmi *self, const char *name, char * type)
515548
return BMI_SUCCESS;
516549
}
517550
}
551+
// Finally check to see if in mass balance array
552+
for (i = 0; i < MASS_BALANCE_VAR_NAME_COUNT; i++) {
553+
if (strcmp(name, mass_balance_var_names[i]) == 0) {
554+
snprintf(type, BMI_MAX_TYPE_NAME, "%s", mass_balance_var_types[i]);
555+
return BMI_SUCCESS;
556+
}
557+
}
518558
// If we get here, it means the variable name wasn't recognized
519559
type[0] = '\0';
520560
return BMI_FAILURE;
@@ -538,6 +578,13 @@ static int Get_var_units (Bmi *self, const char *name, char * units)
538578
return BMI_SUCCESS;
539579
}
540580
}
581+
//Check for mass balance
582+
for (i = 0; i < MASS_BALANCE_VAR_NAME_COUNT; i++) {
583+
if (strcmp(name, mass_balance_var_names[i]) == 0) {
584+
snprintf(units, BMI_MAX_UNITS_NAME, "%s", mass_balance_var_units[i]);
585+
return BMI_SUCCESS;
586+
}
587+
}
541588
// If we get here, it means the variable name wasn't recognized
542589
units[0] = '\0';
543590
return BMI_FAILURE;
@@ -560,6 +607,9 @@ static int Initialize (Bmi *self, const char *file)
560607
else
561608
model = (test_bmi_c_model *) self->data;
562609

610+
model->mass_stored = 0.0;
611+
model->mass_leaked = 0.0;
612+
563613
if (read_init_config(file, model) == BMI_FAILURE)
564614
return BMI_FAILURE;
565615

extern/test_bmi_c/src/test_bmi_c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ extern int run(test_bmi_c_model* model, long dt)
1919
}
2020
model->current_model_time += (double)dt;
2121

22+
model->mass_stored = *model->output_var_1 - *model->input_var_1;
23+
model->mass_leaked = 0;
2224
return 0;
2325
}

0 commit comments

Comments
 (0)