Skip to content

Clean up bmi_cem.c#20

Merged
mcflugen merged 9 commits intov0from
mcflugen/clean-up-bmi-cem
May 28, 2025
Merged

Clean up bmi_cem.c#20
mcflugen merged 9 commits intov0from
mcflugen/clean-up-bmi-cem

Conversation

@mcflugen
Copy link
Contributor

As with #19, I've cleaned up the bmi_cem.c to use a table of variables that lists variable names and metadata.

@mcflugen mcflugen requested a review from Copilot May 27, 2025 22:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR cleans up bmi_cem.c by replacing repetitive hard-coded metadata with a centralized table of variable information. The key changes include introducing the VarInfo structure with an array of variable definitions, refactoring getter functions to use the lookup table, and consolidating grid and variable metadata handling.

Comments suppressed due to low confidence (1)

bmi_cem.c:309

  • After setting *grid = 2 for matched variable names, the code unconditionally resets *grid to -1 and returns BMI_FAILURE. Consider returning BMI_SUCCESS immediately after setting *grid appropriately to avoid overriding the valid assignment.
*grid = -1;

@mcflugen mcflugen force-pushed the mcflugen/clean-up-bmi-cem branch from 1d2fb4c to b953aa7 Compare May 27, 2025 22:53
@mcflugen mcflugen requested a review from Copilot May 27, 2025 23:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Refactors bmi_cem.c to centralize variable metadata in a VarInfo table and simplifies per-variable lookups.

  • Adds VarInfo struct and variables[] table with name/units/type/itemsize/location
  • Introduces find_variable helper and replaces long if-else chains for type/units/itemsize/location
  • Leaves per-variable logic in get_var_grid, get_var_nbytes, and get_value_ptr, still manually matching names
Comments suppressed due to low confidence (1)

bmi_cem.c:21

  • [nitpick] The array name variables is very generic. Consider renaming it to something more descriptive like var_info_table or bmi_var_info_list.
const VarInfo variables[] = {

/* Implement this: Add model-specific includes */
#include "cem_model.h"


Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Add a brief comment above the VarInfo typedef explaining its purpose (e.g. “// VarInfo holds metadata for each BMI variable”).

Suggested change
// VarInfo holds metadata for each BMI variable, including its name, units, type, size, and location.

Copilot uses AI. Check for mistakes.
type[0] = '\0';
return BMI_FAILURE;
}
strncpy(type, var->type, BMI_MAX_UNITS_NAME);
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

The BMI_MAX_UNITS_NAME macro is intended for unit strings. Use the appropriate max‐length macro for type names (e.g. BMI_MAX_TYPE_NAME) to avoid truncation or confusion.

Suggested change
strncpy(type, var->type, BMI_MAX_UNITS_NAME);
strncpy(type, var->type, BMI_MAX_TYPE_NAME);

Copilot uses AI. Check for mistakes.
bmi_cem.c Outdated
Comment on lines 299 to 308
if (
strcmp(name, "land_surface_water_sediment~bedload__mass_flow_rate") == 0 ||
strcmp(name, "land_surface__elevation") == 0 ||
strcmp(name, "sea_water__depth") == 0
) {
*grid = 2;
} else {
fprintf(stderr, "bad grid. returning %d", BMI_FAILURE);
*grid = -1;
return BMI_FAILURE;
return BMI_SUCCESS;
}
return BMI_SUCCESS;
*grid = -1;
return BMI_FAILURE;
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

The get_var_grid function still hard‐codes a subset of variable names. Consider adding a grid_id field to VarInfo and using find_variable to eliminate this manual matching and keep metadata in one place.

Copilot uses AI. Check for mistakes.
return BMI_FAILURE;
}

if (
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Similarly, get_var_nbytes duplicates name-based logic to pick size sources. You could extend VarInfo to include grid and size logic (or a callback) to avoid repeating these conditionals.

Copilot uses AI. Check for mistakes.
@mcflugen mcflugen force-pushed the mcflugen/clean-up-bmi-cem branch from b953aa7 to 6de5fbb Compare May 27, 2025 23:25
@mcflugen mcflugen merged commit c899a7a into v0 May 28, 2025
5 checks passed
@mcflugen mcflugen deleted the mcflugen/clean-up-bmi-cem branch May 28, 2025 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant