diff --git a/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTable.mo b/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTable.mo index 13e2354..f9de98f 100644 --- a/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTable.mo +++ b/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTable.mo @@ -11,14 +11,14 @@ model ExternalObjectTable output MyTable outTable; external "C" outTable = initMyTable(vals, size(vals, 1)); - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjInit.c\""); end constructor; function destructor input MyTable inTable; external "C" closeMyTable(inTable) ; - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjClose.c\""); end destructor; end MyTable; @@ -28,7 +28,7 @@ model ExternalObjectTable output Real y; external "C" y=interpolateMyTable(interpolTable,u) ; - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjInterpolate.c\""); end interpolateMyTable; MyTable myTable = MyTable({5, 3, 1, 9, 6}); diff --git a/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTableInFunction.mo b/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTableInFunction.mo index 76398d5..c17bc1a 100644 --- a/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTableInFunction.mo +++ b/ModelicaCompliance/Functions/ExternalObjects/ExternalObjectTableInFunction.mo @@ -4,6 +4,9 @@ model ExternalObjectTableInFunction extends Icons.TestCase; class MyTable + // Note that this is a copy of ModelicaCompliance.Functions.ExternalObjects.ExternalTable + // In real code it should just be a common implementation, + // but these tests are designed to be self-contained extends ExternalObject; function constructor @@ -11,14 +14,14 @@ model ExternalObjectTableInFunction output MyTable outTable; external "C" outTable = initMyTable(vals, size(vals, 1)); - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjInit.c\""); end constructor; function destructor input MyTable inTable; external "C" closeMyTable(inTable) ; - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjClose.c\""); end destructor; end MyTable; @@ -28,7 +31,7 @@ model ExternalObjectTableInFunction output Real y; external "C" y=interpolateMyTable(interpolTable,u) ; - annotation(Include="#include \"ExtObj.c\""); + annotation(Include="#include \"ExtObjInterpolate.c\""); end interpolateMyTable; function interpolateMyTableFunc diff --git a/ModelicaCompliance/Resources/Include/ExtObj.c b/ModelicaCompliance/Resources/Include/ExtObj.c deleted file mode 100644 index 8213a76..0000000 --- a/ModelicaCompliance/Resources/Include/ExtObj.c +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef EXTOBJ_C -#define EXTOBJ_C - -#include -#include /* for Linux malloc and exit */ -#include -#include -#include "ModelicaUtilities.h" - -#include "ExtObj.h" - -/* Constructor for MyTable. */ -void* initMyTable(double *table_data, size_t table_size) -{ - MyTable *table = (MyTable*)malloc(sizeof(MyTable)); - - if (table == NULL) { - ModelicaError("Not enough memory.\n"); - return 0; /* Not reachable */ - } - - table->array = (double*)malloc(table_size * sizeof(double)); - - if(!table->array) { - free(table); - ModelicaError("Error allocating array in MyTable.\n"); - return 0; /* Not reachable */ - } - - memcpy(table->array, table_data, table_size * sizeof(double)); - table->size = table_size; - - return (void*)table; -} - -/* Destructor for MyTable. */ -void closeMyTable(void *object) -{ - MyTable *table = (MyTable*)object; - if (object == NULL) return; - free(table->array); - free(table); -} - -/* Interpolates between two adjacent values in the table. */ -double interpolateMyTable(void *object, double u) -{ - MyTable *table = (MyTable*)object; - double ip, fp, val1, val2; - long idx; - - /* Use modf to break u into integer and fraction part. */ - fp = modf(u, &ip); - /* Use the integer part of u to get the table index for the first value.*/ - idx = (long) (ip + 0.5); - - if(u < 0.0 || idx >= table->size - 1) { - ModelicaFormatError("%f is outside the table interval.\n", u); - return 0; /* Not reachable */ - } - - /* Fetch the two values from the table and use the fraction part of u to - * interpolate between them. */ - val1 = table->array[idx]; - val2 = table->array[idx + 1]; - - return val1 * (1.0 - fp) + val2 * fp; -} - -#endif /*EXTOBJ_C*/ diff --git a/ModelicaCompliance/Resources/Include/ExtObjClose.c b/ModelicaCompliance/Resources/Include/ExtObjClose.c new file mode 100644 index 0000000..130c606 --- /dev/null +++ b/ModelicaCompliance/Resources/Include/ExtObjClose.c @@ -0,0 +1,15 @@ +#include "ExtObj.h" + +#include +#include /* for Linux malloc and exit */ +#include +#include "ModelicaUtilities.h" + +/* Destructor for MyTable. */ +void closeMyTable(void *object) +{ + MyTable *table = (MyTable*)object; + if (object == NULL) return; + free(table->array); + free(table); +} diff --git a/ModelicaCompliance/Resources/Include/ExtObjInit.c b/ModelicaCompliance/Resources/Include/ExtObjInit.c new file mode 100644 index 0000000..ee38c0b --- /dev/null +++ b/ModelicaCompliance/Resources/Include/ExtObjInit.c @@ -0,0 +1,30 @@ +#include "ExtObj.h" + +#include +#include /* for Linux malloc and exit */ +#include +#include "ModelicaUtilities.h" + +/* Constructor for MyTable. */ +void* initMyTable(double *table_data, size_t table_size) +{ + MyTable *table = (MyTable*)malloc(sizeof(MyTable)); + + if (table == NULL) { + ModelicaError("Not enough memory.\n"); + return 0; /* Not reachable */ + } + + table->array = (double*)malloc(table_size * sizeof(double)); + + if(!table->array) { + free(table); + ModelicaError("Error allocating array in MyTable.\n"); + return 0; /* Not reachable */ + } + + memcpy(table->array, table_data, table_size * sizeof(double)); + table->size = table_size; + + return (void*)table; +} diff --git a/ModelicaCompliance/Resources/Include/ExtObjInterpolate.c b/ModelicaCompliance/Resources/Include/ExtObjInterpolate.c new file mode 100644 index 0000000..61b60f5 --- /dev/null +++ b/ModelicaCompliance/Resources/Include/ExtObjInterpolate.c @@ -0,0 +1,31 @@ +#include "ExtObj.h" + +#include +#include +#include +#include "ModelicaUtilities.h" + +/* Interpolates between two adjacent values in the table. */ +double interpolateMyTable(void *object, double u) +{ + MyTable *table = (MyTable*)object; + double ip, fp, val1, val2; + long idx; + + /* Use modf to break u into integer and fraction part. */ + fp = modf(u, &ip); + /* Use the integer part of u to get the table index for the first value.*/ + idx = (long) (ip + 0.5); + + if(u < 0.0 || idx >= table->size - 1) { + ModelicaFormatError("%f is outside the table interval.\n", u); + return 0; /* Not reachable */ + } + + /* Fetch the two values from the table and use the fraction part of u to + * interpolate between them. */ + val1 = table->array[idx]; + val2 = table->array[idx + 1]; + + return val1 * (1.0 - fp) + val2 * fp; +}