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

Separate out external object files #79

Open
wants to merge 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ model ExternalObjectTableInFunction
extends Icons.TestCase;

class MyTable
// Note that this is a copy of ModelicaCompliance.Functions.ExternalObjects.ExternalTable

Choose a reason for hiding this comment

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

I can't see that class, did you mean this?

Suggested change
// Note that this is a copy of ModelicaCompliance.Functions.ExternalObjects.ExternalTable
// Note that this is a copy of ModelicaCompliance.Functions.ExternalObjects.ExternalObjectTable

// In real code it should just be a common implementation,
// but these tests are designed to be self-contained
extends ExternalObject;

function constructor
input Real[:] vals;
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;

Expand All @@ -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
Expand Down
70 changes: 0 additions & 70 deletions ModelicaCompliance/Resources/Include/ExtObj.c

This file was deleted.

15 changes: 15 additions & 0 deletions ModelicaCompliance/Resources/Include/ExtObjClose.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "ExtObj.h"

#include <stdio.h>
Copy link

@henrikt-ma henrikt-ma Oct 1, 2024

Choose a reason for hiding this comment

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

Why is stdio.h included everywhere?

#include <stdlib.h> /* for Linux malloc and exit */
#include <string.h>
#include "ModelicaUtilities.h"

/* Destructor for MyTable. */
void closeMyTable(void *object)
{
MyTable *table = (MyTable*)object;
if (object == NULL) return;
free(table->array);
free(table);
}
30 changes: 30 additions & 0 deletions ModelicaCompliance/Resources/Include/ExtObjInit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "ExtObj.h"

#include <stdio.h>
#include <stdlib.h> /* for Linux malloc and exit */
#include <string.h>
#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;
}
31 changes: 31 additions & 0 deletions ModelicaCompliance/Resources/Include/ExtObjInterpolate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ExtObj.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#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;
}