This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
178a284
commit 5853724
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
simulation/modelica/external_functions/extObj_ticket3446.mo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package TestMyExternalObj | ||
class MyExternalObj | ||
extends ExternalObject; | ||
function constructor | ||
input Integer size=3; | ||
output MyExternalObj outMyExternalObj; | ||
|
||
external "C" outMyExternalObj=initMyExternalObj(size); | ||
annotation(Include=" | ||
#include <stdio.h> | ||
#include <stdlib.h> /* for Linux malloc and exit */ | ||
#include <string.h> | ||
void* initMyExternalObj(int size) | ||
{ | ||
int i=0; | ||
double *extObj = (double*)malloc(size*sizeof(double)); | ||
if(extObj == NULL) | ||
printf(\"Not enough memory\"); | ||
for(i=0; i<size; i++) | ||
if(i < 2) | ||
extObj[i] = 1.0; | ||
else | ||
extObj[i] = extObj[i-1]+extObj[i-2]; | ||
return (void*)extObj; | ||
}"); | ||
end constructor; | ||
|
||
function destructor | ||
input MyExternalObj inMyExternalObj; | ||
|
||
external "C" closeMyExternalObj(inMyExternalObj) ; | ||
annotation(Include=" | ||
#include <stdio.h> | ||
#include <stdlib.h> /* for Linux malloc and exit */ | ||
#include <string.h> | ||
/* Destructor */ | ||
void closeMyExternalObj(void *object) | ||
{ | ||
/* Release storage */ | ||
double *extObj = (double*)object; | ||
if (object == NULL) | ||
return; | ||
free(extObj); | ||
}"); | ||
end destructor; | ||
end MyExternalObj; | ||
|
||
function readFromMyExternalObj | ||
input MyExternalObj extObj; | ||
input Integer i; | ||
output Real y; | ||
|
||
external "C" y=readFromMyExternalObj(extObj, i); | ||
annotation(Include=" | ||
#include <stdio.h> | ||
#include <stdlib.h> /* for Linux malloc and exit */ | ||
#include <string.h> | ||
double readFromMyExternalObj(void* object, int i) | ||
{ | ||
double *extObj = (double*)object; | ||
return extObj[i-1]; | ||
}"); | ||
end readFromMyExternalObj; | ||
|
||
model Test | ||
parameter Integer size = 5; | ||
final parameter Integer size_ = size; | ||
parameter MyExternalObj MyExtObj=MyExternalObj(size_); | ||
parameter Real p1 = readFromMyExternalObj(MyExtObj, 1); | ||
Real p2 = readFromMyExternalObj(MyExtObj, 2); | ||
Real p3 = readFromMyExternalObj(MyExtObj, 3); | ||
Real p4 = readFromMyExternalObj(MyExtObj, 4); | ||
Real p5 = readFromMyExternalObj(MyExtObj, 5); | ||
end Test; | ||
end TestMyExternalObj; |
32 changes: 32 additions & 0 deletions
32
simulation/modelica/external_functions/extObj_ticket3446.mos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// name: extObj_ticket3446 | ||
// keywords: initialization, external object | ||
// status: correct | ||
|
||
loadFile("extObj_ticket3446.mo"); getErrorString(); | ||
simulate(TestMyExternalObj.Test); getErrorString(); | ||
val(p1, 0.0); getErrorString(); | ||
val(p2, 0.0); getErrorString(); | ||
val(p3, 0.0); getErrorString(); | ||
val(p4, 0.0); getErrorString(); | ||
val(p5, 0.0); getErrorString(); | ||
|
||
// Result: | ||
// true | ||
// "" | ||
// record SimulationResult | ||
// resultFile = "TestMyExternalObj.Test_res.mat", | ||
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'TestMyExternalObj.Test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''", | ||
// messages = "" | ||
// end SimulationResult; | ||
// "" | ||
// 1.0 | ||
// "" | ||
// 1.0 | ||
// "" | ||
// 2.0 | ||
// "" | ||
// 3.0 | ||
// "" | ||
// 5.0 | ||
// "" | ||
// endResult |