<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

#include "bmi.h"
#include "globals.h"


#define return_on_error(stmt) { \
  const int status = (stmt); \
  if (status != BMI_SUCCESS) \
  return status; \
}


int cem_initialize (void);
int cem_update (void);
int cem_update_until (int);
int cem_finalize (void);


int update_frac (void *, double);


static int
initialize (const char *config_file, void ** handle) {
  CemModel *self = NULL;
  int status;

  if (!handle)
    return BMI_FAILURE;

  self = malloc (sizeof (CemModel));

  status = cem_initialize ();

  *handle = self;

  if (status == 0)
    return BMI_SUCCESS;
  else
    return BMI_FAILURE;
}


int
update (void *self)
{
  if (cem_update () == 0)
    return BMI_SUCCESS;
  else
    return BMI_FAILURE;
}


int
update_frac (void *self, double f)
{
  if (f>0) {
    double dt;

    get_time_step (self, &dt);

    TimeStep = f * dt;

    update (self);

    TimeStep = dt;
  }

  return BMI_SUCCESS;
}


int
update_until (void *self, double t)
{
  {
    double dt;
    double now;

    get_time_step (self, &dt);
    gt_current_time (self, &now);

    {
      int n;
      const double n_steps = (t - now) / dt;
      const int n_full_steps = (int)n_steps;
      for (n=0; n<n_full_steps; n++) {
        update (self);
      }

      update_frac (self, n_steps - n_full_steps);
    }
  }

  return BMI_SUCCESS;
}


int
finalize (void *self)
{
  if (self) {
    cem_finalize ();
  }

  return BMI_SUCCESS;
}


int
get_var_grid(void *self, const char *name, int *grid_id) {
  if (strcmp (name, "sea_water__depth") == 0 ||
      strcmp (name, "land_surface__elevation") == 0) {
    *grid_id = 0;
    return BMI_SUCCESS;
  }
  else {
    *grid_id = -1;
    return BMI_FAILURE;
  }
}


int
get_var_type (void *self, const char *long_var_name, char * type)
{
  if (strcmp (long_var_name, "sea_water__depth") == 0 ||
      strcmp (long_var_name, "land_surface__elevation") == 0) {
    *type = BMI_VAR_TYPE_DOUBLE;
    return BMI_SUCCESS;
  }
  else {
    *type = BMI_VAR_TYPE_UNKNOWN;
    return BMI_FAILURE;
  }
}


int
get_var_units (void *self, const char *long_var_name, char * units)
{
  if (strcmp (long_var_name, "sea_water__depth") == 0 ||
      strcmp (long_var_name, "land_surface__elevation") == 0) {
    strncpy (units, "meter", BMI_MAX_UNITS_NAME);
    return BMI_SUCCESS;
  }
  else {
    units[0] = '\0';
    return BMI_FAILURE;
  }
}


static int
get_var_itemsize(void *self, const char *name, int *itemsize) {
  if (strcmp (name, "sea_water__depth") == 0 ||
      strcmp (name, "land_surface__elevation") == 0) {
    *itemsize = sizeof(double);
    return BMI_SUCCESS;
  } else {
    *itemsize = 0;
    return BMI_FAILURE;
  }
}



static int
get_var_nbytes(void *self, const char *name, int *nbytes) {
  int id, size, itemsize;

  return_on_error(get_var_grid(self, name, &id));
  return_on_error(get_grid_size(self, id, &size));
  return_on_error(get_var_itemsize(self, name, &itemsize));

  *nbytes = itemsize * size;

  return BMI_SUCCESS;
}


int
get_grid_rank (void *self, int grid_id, int * rank)
{
  if (grid_id == 0) {
    *rank = 2;
    return BMI_SUCCESS;
  }
  else {
    *rank = -1;
    return BMI_FAILURE;
  }
}


int
get_grid_size (void *self, int grid_id, int * size)
{
  if (grid_id == 0) {
    *size = Xmax * Ymax;
    return BMI_SUCCESS;
  }
  else {
    *size = -1;
    return BMI_FAILURE;
  }
}


int
get_grid_shape (void *self, int grid_id, int * shape)
{
  if (grid_id == 0) {
    shape[0] = Xmax;
    shape[1] = Ymax;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


int
get_grid_spacing (void *self, int grid_id, double * spacing)
{
  if (grid_id == 0) {
    spacing[0] = CellWidth;
    spacing[1] = CellWidth;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


int
get_grid_origin (void *self, int grid_id, double * origin)
{
  if (grid_id == 0) {
    origin[0] = 0.;
    origin[1] = 0.;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


int
get_grid_type (void *self, int grid_id, char * type)
{
  if (grid_id == 0) {
    *type = BMI_GRID_TYPE_UNIFORM;
    return BMI_SUCCESS;
  }
  else {
    *type = BMI_GRID_TYPE_UNKNOWN;
    return BMI_FAILURE;
  }
}


int
get_value (void *self, const char *long_var_name, void *dest)
{
  void *src = NULL;

  if (get_value_ptr (self, long_var_name, &src) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, src, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


int
get_value_ptr (BMI_CEM_Model *self, const char *long_var_name, void **dest)
{
  void *data = NULL;

  if (strcmp (long_var_name, "sea_water__depth")==0)
    data = (void*) ShelfDepth[0];
  else if (strcmp (long_var_name, "land_surface__elevation") == 0)
    data = (void*) Topography[0];
  else if (strcmp (long_var_name, "sea_surface_water_wave__significant_height")==0)
    data = (void*) Hsig[0];
  else if (strcmp (long_var_name, "sea_surface_water_wave__azimuth_angle_of_group_velocity")==0)
    data = (void*) Dir[0];

  *dest = data;

  return (data == NULL)?BMI_FAILURE:BMI_SUCCESS;
}


int
get_value_at_indices (void *self, const char *long_var_name, void *dest, int * inds, int len)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    double * to = (double*) dest;
    for (i=0; i<len; i++) {
      to[i] = src[inds[i]];
    }
  }

  return BMI_SUCCESS;
}


int
get_double (void *self, const char *long_var_name, double *dest)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, src, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


int
get_double_ptr (void *self, const char *long_var_name, double **dest)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  *dest = src;

  return BMI_SUCCESS;
}


int
get_double_at_indices (void *self, const char *long_var_name, double *dest, int * inds, int len)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    for (i=0; i<len; i++) {
      dest[i] = src[inds[i]];
    }
  }

  return BMI_SUCCESS;
}


int
set_value (BMI_CEM_Model *self, const char *long_var_name, void *array)
{
  void * dest = NULL;

  if (get_value_ptr (self, long_var_name, &dest) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, array, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


int
set_value_at_indices (void *self, const char *long_var_name, int * inds, int len, void *src)
{
  double * dest = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    double * from = src;
    for (i=0; i<len; i++) {
      dest[inds[i]] = from[i];
    }
  }

  return BMI_SUCCESS;
}


int
set_double (void *self, const char *long_var_name, double *array)
{
  double * dest;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, array, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


int
set_double_at_indices (void *self, const char *long_var_name, int * inds, int len, double *src)
{
  double * dest;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    for (i=0; i<len; i++) {
      dest[inds[i]] = src[i];
    }
  }

  return BMI_SUCCESS;
}


int
get_component_name (void *self, char * name)
{
  strncpy (name, "Coastline Evolution Model", BMI_MAX_COMPONENT_NAME);
  return BMI_SUCCESS;
}


const char *input_var_names[BMI_CEM_INPUT_VAR_NAME_COUNT] = {
  "sea_surface_water_wave__significant_height",
  "sea_surface_water_wave__azimuth_angle_of_group_velocity"
};

int
get_input_var_names (void *self, char ** names)
{
  int i;
  for (i=0; i<BMI_CEM_INPUT_VAR_NAME_COUNT; i++) {
    strncpy (names[i], input_var_names[i], BMI_MAX_VAR_NAME);
  }
  return BMI_SUCCESS;
}


const char *output_var_names[BMI_CEM_OUTPUT_VAR_NAME_COUNT] = {
  "sea_water__depth",
  "land_surface__elevation"
};


int
get_output_var_names (void *self, char ** names)
{
  int i;
  for (i=0; i<BMI_CEM_OUTPUT_VAR_NAME_COUNT; i++) {
    strncpy (names[i], output_var_names[i], BMI_MAX_VAR_NAME);
  }
  return BMI_SUCCESS;
}


int
get_start_time (void *self, double * time)
{
  if (time) {
    *time = 0.;
    return BMI_SUCCESS;
  }
  else {
    return BMI_FAILURE;
  }
}


int
get_end_time (void *self, double * time)
{
  *time = StopAfter;
  return BMI_SUCCESS;
}


int
get_current_time (void *self, double * time)
{
  *time = CurrentTimeStep * TimeStep;
  return BMI_SUCCESS;
}


int
get_time_step (void *self, double * dt)
{
  *dt = TimeStep;
  return BMI_SUCCESS;
}


int
get_time_units (void *self, char * units)
{
  strncpy (units, "d", BMI_MAX_UNITS_NAME);
  return BMI_SUCCESS;
}


BMI_Model*
register_bmi_cem(BMI_Model *model)
{
    model->self = NULL;

    model->initialize = initialize;
    model->update = update;
    model->update_until = update_until;
    model->update_frac = update_frac;
    model->finalize = finalize;
    model->run_model = NULL;

    model->get_component_name = get_component_name;
    model->get_input_var_name_count = get_input_var_name_count;
    model->get_output_var_name_count = get_output_var_name_count;
    model->get_input_var_names = get_input_var_names;
    model->get_output_var_names = get_output_var_names;

    model->get_var_grid = get_var_grid;
    model->get_var_type = get_var_type;
    model->get_var_units = get_var_units;
    model->get_var_nbytes = get_var_nbytes;
    model->get_current_time = get_current_time;
    model->get_start_time = get_start_time;<commit_msg>Make all BMI functions static.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

#include "bmi.h"
#include "globals.h"


#define return_on_error(stmt) { \
  const int status = (stmt); \
  if (status != BMI_SUCCESS) \
  return status; \
}


int cem_initialize (void);
int cem_update (void);
int cem_update_until (int);
int cem_finalize (void);


int update_frac (void *, double);


static int
initialize (const char *config_file, void ** handle) {
  CemModel *self = NULL;
  int status;

  if (!handle)
    return BMI_FAILURE;

  self = malloc (sizeof (CemModel));

  status = cem_initialize ();

  *handle = self;

  if (status == 0)
    return BMI_SUCCESS;
  else
    return BMI_FAILURE;
}


static int
update (void *self)
{
  if (cem_update () == 0)
    return BMI_SUCCESS;
  else
    return BMI_FAILURE;
}


static int
update_frac (void *self, double f)
{
  if (f>0) {
    double dt;

    get_time_step (self, &dt);

    TimeStep = f * dt;

    update (self);

    TimeStep = dt;
  }

  return BMI_SUCCESS;
}


static int
update_until (void *self, double t)
{
  {
    double dt;
    double now;

    get_time_step (self, &dt);
    gt_current_time (self, &now);

    {
      int n;
      const double n_steps = (t - now) / dt;
      const int n_full_steps = (int)n_steps;
      for (n=0; n<n_full_steps; n++) {
        update (self);
      }

      update_frac (self, n_steps - n_full_steps);
    }
  }

  return BMI_SUCCESS;
}


static int
finalize (void *self)
{
  if (self) {
    cem_finalize ();
  }

  return BMI_SUCCESS;
}


static int
get_var_grid(void *self, const char *name, int *grid_id) {
  if (strcmp (name, "sea_water__depth") == 0 ||
      strcmp (name, "land_surface__elevation") == 0) {
    *grid_id = 0;
    return BMI_SUCCESS;
  }
  else {
    *grid_id = -1;
    return BMI_FAILURE;
  }
}


static int
get_var_type (void *self, const char *long_var_name, char * type)
{
  if (strcmp (long_var_name, "sea_water__depth") == 0 ||
      strcmp (long_var_name, "land_surface__elevation") == 0) {
    *type = BMI_VAR_TYPE_DOUBLE;
    return BMI_SUCCESS;
  }
  else {
    *type = BMI_VAR_TYPE_UNKNOWN;
    return BMI_FAILURE;
  }
}


static int
get_var_units (void *self, const char *long_var_name, char * units)
{
  if (strcmp (long_var_name, "sea_water__depth") == 0 ||
      strcmp (long_var_name, "land_surface__elevation") == 0) {
    strncpy (units, "meter", BMI_MAX_UNITS_NAME);
    return BMI_SUCCESS;
  }
  else {
    units[0] = '\0';
    return BMI_FAILURE;
  }
}


static int
get_var_itemsize(void *self, const char *name, int *itemsize) {
  if (strcmp (name, "sea_water__depth") == 0 ||
      strcmp (name, "land_surface__elevation") == 0) {
    *itemsize = sizeof(double);
    return BMI_SUCCESS;
  } else {
    *itemsize = 0;
    return BMI_FAILURE;
  }
}



static int
get_var_nbytes(void *self, const char *name, int *nbytes) {
  int id, size, itemsize;

  return_on_error(get_var_grid(self, name, &id));
  return_on_error(get_grid_size(self, id, &size));
  return_on_error(get_var_itemsize(self, name, &itemsize));

  *nbytes = itemsize * size;

  return BMI_SUCCESS;
}


static int
get_grid_rank (void *self, int grid_id, int * rank)
{
  if (grid_id == 0) {
    *rank = 2;
    return BMI_SUCCESS;
  }
  else {
    *rank = -1;
    return BMI_FAILURE;
  }
}


static int
get_grid_size (void *self, int grid_id, int * size)
{
  if (grid_id == 0) {
    *size = Xmax * Ymax;
    return BMI_SUCCESS;
  }
  else {
    *size = -1;
    return BMI_FAILURE;
  }
}


static int
get_grid_shape (void *self, int grid_id, int * shape)
{
  if (grid_id == 0) {
    shape[0] = Xmax;
    shape[1] = Ymax;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


static int
get_grid_spacing (void *self, int grid_id, double * spacing)
{
  if (grid_id == 0) {
    spacing[0] = CellWidth;
    spacing[1] = CellWidth;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


static int
get_grid_origin (void *self, int grid_id, double * origin)
{
  if (grid_id == 0) {
    origin[0] = 0.;
    origin[1] = 0.;
    return BMI_SUCCESS;
  } else {
    return BMI_FAILURE;
  }
}


static int
get_grid_type (void *self, int grid_id, char * type)
{
  if (grid_id == 0) {
    *type = BMI_GRID_TYPE_UNIFORM;
    return BMI_SUCCESS;
  }
  else {
    *type = BMI_GRID_TYPE_UNKNOWN;
    return BMI_FAILURE;
  }
}


static int
get_value (void *self, const char *long_var_name, void *dest)
{
  void *src = NULL;

  if (get_value_ptr (self, long_var_name, &src) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, src, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


static int
get_value_ptr (BMI_CEM_Model *self, const char *long_var_name, void **dest)
{
  void *data = NULL;

  if (strcmp (long_var_name, "sea_water__depth")==0)
    data = (void*) ShelfDepth[0];
  else if (strcmp (long_var_name, "land_surface__elevation") == 0)
    data = (void*) Topography[0];
  else if (strcmp (long_var_name, "sea_surface_water_wave__significant_height")==0)
    data = (void*) Hsig[0];
  else if (strcmp (long_var_name, "sea_surface_water_wave__azimuth_angle_of_group_velocity")==0)
    data = (void*) Dir[0];

  *dest = data;

  return (data == NULL)?BMI_FAILURE:BMI_SUCCESS;
}


static int
get_value_at_indices (void *self, const char *long_var_name, void *dest, int * inds, int len)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    double * to = (double*) dest;
    for (i=0; i<len; i++) {
      to[i] = src[inds[i]];
    }
  }

  return BMI_SUCCESS;
}


static int
get_double (void *self, const char *long_var_name, double *dest)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, src, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


static int
get_double_ptr (void *self, const char *long_var_name, double **dest)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  *dest = src;

  return BMI_SUCCESS;
}


static int
get_double_at_indices (void *self, const char *long_var_name, double *dest, int * inds, int len)
{
  double *src = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&src) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    for (i=0; i<len; i++) {
      dest[i] = src[inds[i]];
    }
  }

  return BMI_SUCCESS;
}


static int
set_value (BMI_CEM_Model *self, const char *long_var_name, void *array)
{
  void * dest = NULL;

  if (get_value_ptr (self, long_var_name, &dest) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, array, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


static int
set_value_at_indices (void *self, const char *long_var_name, int * inds, int len, void *src)
{
  double * dest = NULL;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    double * from = src;
    for (i=0; i<len; i++) {
      dest[inds[i]] = from[i];
    }
  }

  return BMI_SUCCESS;
}


static int
set_double (void *self, const char *long_var_name, double *array)
{
  double * dest;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  memcpy (dest, array, sizeof (double) * Xmax * Ymax * 2);

  return BMI_SUCCESS;
}


static int
set_double_at_indices (void *self, const char *long_var_name, int * inds, int len, double *src)
{
  double * dest;

  if (get_value_ptr (self, long_var_name, (void*)&dest) == BMI_FAILURE)
    return BMI_FAILURE;

  { /* Copy the data */
    int i;
    for (i=0; i<len; i++) {
      dest[inds[i]] = src[i];
    }
  }

  return BMI_SUCCESS;
}


static int
get_component_name (void *self, char * name)
{
  strncpy (name, "Coastline Evolution Model", BMI_MAX_COMPONENT_NAME);
  return BMI_SUCCESS;
}


const char *input_var_names[BMI_CEM_INPUT_VAR_NAME_COUNT] = {
  "sea_surface_water_wave__significant_height",
  "sea_surface_water_wave__azimuth_angle_of_group_velocity"
};

static int
get_input_var_names (void *self, char ** names)
{
  int i;
  for (i=0; i<BMI_CEM_INPUT_VAR_NAME_COUNT; i++) {
    strncpy (names[i], input_var_names[i], BMI_MAX_VAR_NAME);
  }
  return BMI_SUCCESS;
}


const char *output_var_names[BMI_CEM_OUTPUT_VAR_NAME_COUNT] = {
  "sea_water__depth",
  "land_surface__elevation"
};


static int
get_output_var_names (void *self, char ** names)
{
  int i;
  for (i=0; i<BMI_CEM_OUTPUT_VAR_NAME_COUNT; i++) {
    strncpy (names[i], output_var_names[i], BMI_MAX_VAR_NAME);
  }
  return BMI_SUCCESS;
}


static int
get_start_time (void *self, double * time)
{
  if (time) {
    *time = 0.;
    return BMI_SUCCESS;
  }
  else {
    return BMI_FAILURE;
  }
}


static int
get_end_time (void *self, double * time)
{
  *time = StopAfter;
  return BMI_SUCCESS;
}


static int
get_current_time (void *self, double * time)
{
  *time = CurrentTimeStep * TimeStep;
  return BMI_SUCCESS;
}


static int
get_time_step (void *self, double * dt)
{
  *dt = TimeStep;
  return BMI_SUCCESS;
}


static int
get_time_units (void *self, char * units)
{
  strncpy (units, "d", BMI_MAX_UNITS_NAME);
  return BMI_SUCCESS;
}


BMI_Model*
register_bmi_cem(BMI_Model *model)
{
    model->self = NULL;

    model->initialize = initialize;
    model->update = update;
    model->update_until = update_until;
    model->update_frac = update_frac;
    model->finalize = finalize;
    model->run_model = NULL;

    model->get_component_name = get_component_name;
    model->get_input_var_name_count = get_input_var_name_count;
    model->get_output_var_name_count = get_output_var_name_count;
    model->get_input_var_names = get_input_var_names;
    model->get_output_var_names = get_output_var_names;

    model->get_var_grid = get_var_grid;
    model->get_var_type = get_var_type;
    model->get_var_units = get_var_units;
    model->get_var_nbytes = get_var_nbytes;
    model->get_current_time = get_current_time;
    model->get_start_time = get_start_time;
{'fraction_non_alphanumeric': 0.12832167832167832, 'fraction_numerical': 0.005294705294705295, 'mean_word_length': 3.5082188696239585, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '540850', 'n_tokens_mistral': 8117, 'n_tokens_neox': 7744, 'n_words': 1864}