Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align get set #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 50 additions & 29 deletions models/c/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ BMI_API int initialize(const char *config_file)
{
current = 0;
sprintf(msg, "initializing with %s", config_file);
_log(INFO, msg);
_log(LEVEL_INFO, msg);
return 0;
}


BMI_API int update(double dt){
sprintf(msg, "updating from %f with %f", current, (dt != -1 ? dt : timestep));
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
current = current + (dt != -1 ? dt : timestep);
return 0;
}

BMI_API int finalize()
{
_log(INFO, "finalizing c model");
_log(LEVEL_INFO, "finalizing c model");
return 0;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ BMI_API void get_var_name(int n, char *name)
strncpy(name, "", MAXSTRINGLEN);
}
sprintf(msg, "getting variable %d -> %s", n, name);
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
}


Expand All @@ -111,27 +111,27 @@ BMI_API void get_var_rank(const char *name, int *rank)
if (strcmp(name, "arr1") == 0)
{
*rank = 1;
}
}
else if (strcmp(name, "arr2") == 0)
{
*rank = 2;
}
}
else if (strcmp(name, "arr3") == 0)
{
*rank = 3;
}
else
}
else
{
*rank = 0;
}
sprintf(msg, "variable %s has rank %d", name, *rank);
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
}


BMI_API void get_var_shape(const char *name, int shape[MAXDIMS])
{

int rank = 0;
int i;
for (i = 0; i < MAXDIMS; i++) {
Expand All @@ -143,25 +143,25 @@ BMI_API void get_var_shape(const char *name, int shape[MAXDIMS])
if (strcmp(name, "arr1") == 0)
{
shape[0] = 3;
}
}
else if (strcmp(name, "arr2") == 0)
{
shape[0] = 2;
shape[1] = 3;

}
}
else if (strcmp(name, "arr3") == 0)
{
shape[0] = 2;
shape[1] = 2;
shape[2] = 3;
}
else
}
else
{

}
sprintf(msg, "variable %s has shape %d", name, *shape);
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
}

BMI_API void get_var_type(const char *name, char *type)
Expand All @@ -170,47 +170,49 @@ BMI_API void get_var_type(const char *name, char *type)
if (strcmp(name, "arr1") == 0)
{
strncpy(type, "double", MAXSTRINGLEN);
}
}
else if (strcmp(name, "arr2") == 0)
{
strncpy(type, "int", MAXSTRINGLEN);
}
}
else if (strcmp(name, "arr3") == 0)
{
strncpy(type, "bool", MAXSTRINGLEN);
}
else
}
else
{
strncpy(type, "", MAXSTRINGLEN);
}
sprintf(msg, "variable %s has type %s", name, type);
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
}

BMI_API void get_var(const char *name, void **ptr)
BMI_API int get_value_ptr(const char *name, void **ptr)
{
/* The value referenced to by ptr is the memory address of arr1 */
if (strcmp(name, "arr1") == 0)
{
*ptr = &arr1;
}
}
else if (strcmp(name, "arr2") == 0)
{
*ptr = &arr2;
}
}
else if (strcmp(name, "arr3") == 0)
{
*ptr = &arr3;
}
else
}
else
{
*ptr = NULL;
return 1;
}
sprintf(msg, "variable %s is at location %p", name, *ptr);
_log(DEBUG, msg);
_log(LEVEL_DEBUG, msg);
return 0;
}

BMI_API void set_var(const char *name, const void *ptr)
BMI_API int set_value(const char *name, const void *ptr)
{
if (strcmp(name, "arr1") == 0)
{
Expand All @@ -224,6 +226,10 @@ BMI_API void set_var(const char *name, const void *ptr)
{
memcpy(arr3, ptr, sizeof(arr3));
}
else {
return 1;
}
return 0;
}

BMI_API void set_var_slice(const char *name, const int *start, const int *count, const void *ptr)
Expand All @@ -241,11 +247,26 @@ BMI_API void set_var_slice(const char *name, const int *start, const int *count,
}
}

BMI_API int set_value_at_indices(const char *name, int *inds, int len, void *ptr)
{
if (strcmp(name, "arr1") == 0) {
for(int i = 0; i < len; ++i)
{
/* unravel index here... */
int idx = inds[i];
arr1[idx] = ((int*) ptr)[i];
}
return 0;
}
return 1;
}


BMI_API void set_logger(Logger callback)
{
char *msg = "Logger attached to c model.";
logger = callback;
_log(INFO, msg);
_log(LEVEL_INFO, msg);
}

/* private log function, which logs to the logging callback */
Expand Down
129 changes: 65 additions & 64 deletions models/cpp/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ double timestep = 1;
// define some arrays for exchange
double arr1[3] = { 3, 2, 1};
int arr2[2][3] =
{
{
{ 3, 2, 1},
{ 6, 4, 2}
};
};
bool arr3[2][2][3] =
{
{
{
{ true, false, false},
{ false, true, false}
{ true, false, false},
{ false, true, false}
},
{
{ false, false, false},
{ false, true, false}
{ false, false, false},
{ false, true, false}
}
};
};

/* Store callback */
Logger logger = NULL;
Expand All @@ -35,67 +35,68 @@ void _log(Level level, std::string msg);


extern "C" {
BMI_API int initialize(const char *config_file)
{
BMI_API int initialize(const char *config_file)
{
std::ostringstream msg;
msg << "initializing with " << config_file;
_log(INFO, msg.str());
return 0;
}

BMI_API int update(double dt)
{
std::ostringstream msg;
msg << "updating from " << current << " with dt: " << (dt != -1 ? dt : timestep);
_log(DEBUG, msg.str());
current += (dt != -1 ? dt : timestep);
return 0;
}

BMI_API int finalize()
{
return 0;
}

BMI_API void get_start_time(double *t)
{
*t = 0;
}

BMI_API void get_end_time(double *t)
{
*t = 10;
}

BMI_API void get_current_time(double *t)
{
*t = current;
}

BMI_API void get_time_step(double *dt)
{
*dt = timestep;
}

BMI_API void get_var(const char *name, void **ptr)
{
/* The value referenced to by ptr is the memory address of arr1 */
*ptr = &arr1;
}

BMI_API void set_logger(Logger callback)
{
Level level = INFO;
std::string msg = "Logging attached to cxx model";
logger = callback;
logger(level, msg.c_str());
}
_log(LEVEL_INFO, msg.str());
return 0;
}

BMI_API int update(double dt)
{
std::ostringstream msg;
msg << "updating from " << current << " with dt: " << (dt != -1 ? dt : timestep);
_log(LEVEL_DEBUG, msg.str());
current += (dt != -1 ? dt : timestep);
return 0;
}

BMI_API int finalize()
{
return 0;
}

BMI_API void get_start_time(double *t)
{
*t = 0;
}

BMI_API void get_end_time(double *t)
{
*t = 10;
}

BMI_API void get_current_time(double *t)
{
*t = current;
}

BMI_API void get_time_step(double *dt)
{
*dt = timestep;
}

BMI_API int get_value_ptr(const char *name, void **ptr)
{
/* The value referenced to by ptr is the memory address of arr1 */
*ptr = &arr1;
return 0;
}

BMI_API void set_logger(Logger callback)
{
Level level = LEVEL_INFO;
std::string msg = "Logging attached to cxx model";
logger = callback;
logger(level, msg.c_str());
}
}

void _log(Level level, std::string msg) {
if (logger != NULL) {
logger(level, msg.c_str());
}
if (logger != NULL) {
logger(level, msg.c_str());
}
}

// placeholder function, all dll's need a main.. in windows only
Expand Down
Loading