Release SPHLite 0.1.0 framework
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
#include "sphlite_c_api.h"
|
||||
#include "sphlite_fortran_mangle.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern void SPHLITE_FC_sphft_initialize(int *ierr);
|
||||
extern void SPHLITE_FC_sphft_finalize(int *ierr);
|
||||
extern void SPHLITE_FC_sphft_get_version(
|
||||
int *major, int *minor, int *patch, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_get_last_error_chars(
|
||||
int *code, int *chars, const int *capacity, int *actual, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_read_inp_chars(
|
||||
const int *chars, const int *count, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_build_model(int *ierr);
|
||||
extern void SPHLITE_FC_sphft_estimate_stable_dt(
|
||||
double *dtcrit, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_advance_explicit(
|
||||
const double *dt, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_run_explicit(int *ierr);
|
||||
extern void SPHLITE_FC_sphft_get_current_time(
|
||||
double *time, int *step, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_get_particle_count(
|
||||
int *count, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_write_csv_chars(
|
||||
const int *chars, const int *count, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_write_restart_chars(
|
||||
const int *chars, const int *count, int *ierr);
|
||||
extern void SPHLITE_FC_sphft_read_restart_chars(
|
||||
const int *chars, const int *count, int *ierr);
|
||||
|
||||
static int sphlite_call_path(
|
||||
const char *path,
|
||||
void (*fortran_call)(const int *chars, const int *count, int *ierr))
|
||||
{
|
||||
size_t i;
|
||||
size_t length;
|
||||
int count;
|
||||
int ierr;
|
||||
int *chars;
|
||||
|
||||
if (path == NULL) {
|
||||
return -1;
|
||||
}
|
||||
length = strlen(path);
|
||||
if (length == 0 || length > 1024) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
chars = (int *)malloc(length * sizeof(int));
|
||||
if (chars == NULL) {
|
||||
return -1;
|
||||
}
|
||||
for (i = 0; i < length; ++i) {
|
||||
chars[i] = (unsigned char)path[i];
|
||||
}
|
||||
count = (int)length;
|
||||
ierr = 0;
|
||||
fortran_call(chars, &count, &ierr);
|
||||
free(chars);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_initialize(void)
|
||||
{
|
||||
int ierr = 0;
|
||||
SPHLITE_FC_sphft_initialize(&ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_finalize(void)
|
||||
{
|
||||
int ierr = 0;
|
||||
SPHLITE_FC_sphft_finalize(&ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_get_version(int *major, int *minor, int *patch)
|
||||
{
|
||||
int ierr = 0;
|
||||
if (major == NULL || minor == NULL || patch == NULL) {
|
||||
return -1;
|
||||
}
|
||||
SPHLITE_FC_sphft_get_version(major, minor, patch, &ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_get_last_error(int *code, char *message, int message_capacity)
|
||||
{
|
||||
int ierr = 0;
|
||||
int actual = 0;
|
||||
int *chars;
|
||||
int i;
|
||||
|
||||
if (code == NULL || message == NULL || message_capacity <= 0) {
|
||||
return -1;
|
||||
}
|
||||
chars = (int *)malloc((size_t)message_capacity * sizeof(int));
|
||||
if (chars == NULL) {
|
||||
return -1;
|
||||
}
|
||||
SPHLITE_FC_sphft_get_last_error_chars(code, chars, &message_capacity, &actual, &ierr);
|
||||
for (i = 0; i < message_capacity; ++i) {
|
||||
if (chars[i] == 0) {
|
||||
message[i] = '\0';
|
||||
break;
|
||||
}
|
||||
message[i] = (char)chars[i];
|
||||
}
|
||||
message[message_capacity - 1] = '\0';
|
||||
free(chars);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_read_inp(const char *filename)
|
||||
{
|
||||
return sphlite_call_path(
|
||||
filename, SPHLITE_FC_sphft_read_inp_chars);
|
||||
}
|
||||
|
||||
int sphlite_build_model(void)
|
||||
{
|
||||
int ierr = 0;
|
||||
SPHLITE_FC_sphft_build_model(&ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_estimate_stable_dt(double *dtcrit)
|
||||
{
|
||||
int ierr = 0;
|
||||
if (dtcrit == NULL) {
|
||||
return -1;
|
||||
}
|
||||
SPHLITE_FC_sphft_estimate_stable_dt(dtcrit, &ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_advance_explicit(double dt)
|
||||
{
|
||||
int ierr = 0;
|
||||
SPHLITE_FC_sphft_advance_explicit(&dt, &ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_run_explicit(void)
|
||||
{
|
||||
int ierr = 0;
|
||||
SPHLITE_FC_sphft_run_explicit(&ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_get_current_time(double *time, int *step)
|
||||
{
|
||||
int ierr = 0;
|
||||
if (time == NULL || step == NULL) {
|
||||
return -1;
|
||||
}
|
||||
SPHLITE_FC_sphft_get_current_time(time, step, &ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_get_particle_count(int *count)
|
||||
{
|
||||
int ierr = 0;
|
||||
if (count == NULL) {
|
||||
return -1;
|
||||
}
|
||||
SPHLITE_FC_sphft_get_particle_count(count, &ierr);
|
||||
return ierr;
|
||||
}
|
||||
|
||||
int sphlite_write_csv(const char *filename)
|
||||
{
|
||||
return sphlite_call_path(
|
||||
filename, SPHLITE_FC_sphft_write_csv_chars);
|
||||
}
|
||||
|
||||
int sphlite_write_restart(const char *filename)
|
||||
{
|
||||
return sphlite_call_path(
|
||||
filename, SPHLITE_FC_sphft_write_restart_chars);
|
||||
}
|
||||
|
||||
int sphlite_read_restart(const char *filename)
|
||||
{
|
||||
return sphlite_call_path(
|
||||
filename, SPHLITE_FC_sphft_read_restart_chars);
|
||||
}
|
||||
Reference in New Issue
Block a user