External Functions

Use this forum to post Vensim related questions.
Post Reply
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

External Functions

Post by aksdax »

Hi,
I am trying to build a dll using the venext.c
however when I run the dll in vensim it does not pass double values.
I write the value passed in a text file.
every time a double is passed it write the value passed as 0.0000
but when I pass a literal/string it is passed properly.
e.g if i pass cosine(.5) the value of x in the dll is 0.00
I have attached the venext.c file.
I have not made any changes to the venext.c file provided by vensim package.
I compile the venext.c in visual studio 6. I also tried using the VENEXT6.dsw and compiled the project yet the values passes are 0.
the only code i have added is
FILE* f;
double y=0;
f=fopen("B:\\test.txt","w");
y=x;
fprintf (f, "y = %f",y);
fclose(f);

Am I supposed to add any library files for double precision while compiling the project ?
I tried adding VENSIMDP.lib in the project.

[Edited on 8-2-2008 by aksdax]
Attachments

[The extension t.c has been deactivated and can no longer be displayed.]

bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

If you are not using the double precision version of Vensim it probably makes more sense to simply use float instead of double. If you really want to pass and use doubles then you can't use %f you will need to use %lf since %f is for a float.
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

Post by aksdax »

Still using %lf does not help printing the right values.
Float or double nither of them is being passed. It still prints 0.00000
I tried compling the DLL using the batch file provided but still the DLL behaves the same.
I have tried re installing the software but it does not help.
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

Post by aksdax »

I tried passing int as well as float still the result is the same.
Do I need to add any library files in the project while build it.
e.g. vensim.lib
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

Actually I just tried your code and it leaves after a the simulation the line y = 100.000000 in the text file as would be expected using the sample model. If I use a model with just 1 variable

blah=cosine(0.5)

then the file contains

y = 0.500000

Of course all calls to cosine at all times will overwrite the file so make sure there is only 1 variable in the model when testing. You might try opening the DLL you create in a debugger to better figure out what is going on.
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

Post by aksdax »

ok so the dll works may be there is some problem with the installed copy of vensim. I will try it on some other pc.

Thank you
Akshay D.
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

Post by aksdax »

Hi,
I have tried to install vensim on another PC and I re-complied the dll and I ran the debugger I am posting the screenshot of the debugger. you can see that the values passed by vensim is 0.00000 for both the functions cosine() and inrange()
The code and the visual project is the one which is provided by you guys I have not modified any code.

[Edited on 8-6-2008 by aksdax]

[Edited on 8-6-2008 by aksdax]

[Edited on 8-6-2008 by aksdax]

[Edited on 8-6-2008 by aksdax]
Attachments
untitledw.JPG
untitledw.JPG (179.88 KiB) Viewed 4352 times
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

other file

Post by aksdax »

Attachments
untitled(2).JPG
untitled(2).JPG (181.17 KiB) Viewed 4352 times
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

Create a new and empty model that runs from time 0 to 1 and in that model add a single variable

test = cosine(1.234)

then run that model and see what appears in the debugger.
aksdax
Junior Member
Posts: 11
Joined: Wed Jul 02, 2008 6:31 am

Post by aksdax »

Hi,
Still the result is the same. I generally create just one variable and try and test my function. Could it be that the copy that i have is corrupt ?
everything else works just fine....
I could understand if only my code did not work or the dll i create does not work but in this case the code and dll created from your project also does not work :(
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

Using the DLL you created from the sample open the extern.mdl model in the same directory and simulate it. Assuming this works look at the variable called "cos time" and see what it does. It should be a wave between -1 and 1 with period 2 pi. If you get this behavior then x is being passed correctly.

Note to make a graph of "cos time" you will need to select it into the workbench using the Variable tab of the control panel as it does not appear on the sketch view.
Post Reply