Using Vensim as a DDE Client

Use this forum to post Vensim related questions.
Post Reply
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Using Vensim as a DDE Client

Post by alazar »

Hi,

I would like to try out the DDE Client functionality, but the shipped example files of Vensim DSS (within the dde directory) are problematic to use. Three files are provided for this example: vddeext.c, vddeext.mdl and vddeext.xls.

The vddeext.mdl file does not work. When I click on 'Check model' or 'Simulate', Vensim returns with an error message saying "Syntax error." and point to the bracket of the topmost external function: ADD VECTOR(vector x[sub] ,vector y[sub],3)

If I modify the code and only the simpler external function called 'ADD' remains in the code, Vensim still returns with the same error message and pointing to the variables within the bracket of the 'ADD' function: ADD(x,y)

It is clear that the above functions are detailed in the vddeext.c file. It is relatively straightforward what is coded in the C file. Since the vddeext.mdl code does not include any reference to the vddeext.c file, I suspect that this external library file has to be loaded into Vensim first (Tools -> Options -> Startup). By reading the Vensim online documentation, I suspect that this C sourced vddeext.c file should be compiled into a .dll file, and this .dll file should be loaded into Vensim.

Could anyone, please, clarify:
- Is there anything wrong with the syntax of the vddeext.mdl code? Anything missing, like the declaration of the types of the variables?
- How to use the vddeext.c within Vensim?
- If the dll conversion is necessary, could you please give me some tips how to do it?

Many thanks in advance,

Attila
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Using Vensim as a DDE Client

Post by Administrator »

- Is there anything wrong with the syntax of the vddeext.mdl code? Anything missing, like the declaration of the types of the variables?
Nothing wrong with the syntax. Because you have not (yet) compiled the C code to a DLL, the model is missing functions hence the error reports.
- How to use the vddeext.c within Vensim?
You need to compile this and create a DLL. Microsoft Visual Studio is probably the easiest to use.
- If the dll conversion is necessary, could you please give me some tips how to do it?
If you are unfamiliar with creating DLLs, then maybe DDE is not the best option for you.

It might be better to explain exactly what you need to do and what experience of programming you have. Using the Vensim DLL might be a better option, it will certainly be much faster than DDE and if you have experience of VBA, much easier too.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Dear Tony,

Thanks for the info. Then it is as I suspected that DLL compilation is needed first. I wanted check this before I spend too much time to learn how to do it.

Let me explain my project. I have to link 4-5 process-based biophysical models with a socio-economic model in a dynamic environment. These process-based models work at different temporal and spatial scales, therefore, these have to be run separately. Some of the models will run in their original form, others will be simplified for this purpose. The simple models might be possible to include in the main Vensim model. I want to include feedbacks in the system (human decision), therefore, I need to have the socio-economic model (the Vensim model) in the centre of the application, which receives information from the bio-physical models (VB, Matlab), and at the same time, it modifies the inputs of the biophysical models at the next timestep (if needed – depending on the human behaviour) or when the gaming parameters have changed by the user.
Vensim seemed originally a very good candidate for this, because (i) it can communicate with other applications (VB and Matlab), (ii) the model interface can be simplified for stakeholders, (iii) it has a gaming option, when the stakeholders can modify some of the assumptions or forcing functions along the simulation, and finally, (iv) the model can be packaged and freely distributed to project partners and key stakeholders by using the Vensim Model Viewer. I have to check it with my bosses, but I believe that the free distribution is quite important as stakeholder engagement is a central part within my project.

I am a user of Visual basic, Fortran and Matlab languages. I also have started learning C++ some years ago, but I never used it, so my C++ knowledge is very basic. The Microsoft Visual Studio is already installed on my computer.

I agree that using Vensim DLL might be easier for me, but I do not think that it fits my purpose, because then the command centre is in Excel and in this case, Vensim will need to communicate with the other models through the Excel. This is why I wanted to try out the DDE Client example to see how it works, how quick the model run is and how complicated to set it up for my own purposes. To be honest, it does not look very complicated (apart from the dll creation bit). However, I do not know how this affects the user interface. Can it be run through Venapp? Would this model remain distributable?

I also have some concerns with the Vensim DLL, because the online guide says that “Vensim DLL may not be redistributed.” What does it mean?

Can a DDE be set up in between two Vensim model? (i.e. if I manage to program one of the bio-physical models (which operates at a different time step) in Vensim; the main Vensim program would initiate the run of this second Vensim, which will return a value to the main program after the simulation)

Many thanks for your kind advice!

Attila
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: Using Vensim as a DDE Client

Post by tomfid »

You need a separate Redistribution License to distribute the .dll, or have users install the Model Reader. We can give you a price quote if needed.

It's possible to run a Vensim model from another Vensim model by creating an external function that calls the .dll. I've done it, and could probably find and post some code, but it's complex.

It might be cleaner to write a master controller application in Java or VB or something like that, and have it manage the timing and information flows among all the components.
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Thanks for the info and suggestion! I'll think about it.

Attila
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Sorry. One last question.

I have managed to build the dll file based on the shipped vensim.h and vddext.c files by using Microsoft Visual Studio 2010.
However, when I load the dll file into Vensim, it creates an error message saying: "External function version mismatch need version 51050 (got -1)". I do not understand why this message is created since the EXTERNAL_VERSION is defined as 51050 in the vensim.h header file, which is included during the compilation process.

I have also tried to re-create the venext.dll file based on the venext.c and vensim.h files, but it resulted in the same error message. However, the venext.dll file, what is shipped with the Vensim DSS program works fine when loaded.

Could you, please, advise me what goes wrong with this version_info call? Has the external version number changed?
Many thanks!

Attila
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Using Vensim as a DDE Client

Post by Administrator »

Are you using the .DEF file to make sure the functions that Vensim calls are exported from the DLL ok?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: Using Vensim as a DDE Client

Post by tomfid »

Also, what version # are you using? I think the -1 error result actually indicates something other than a version mismatch, but I'd have to know what you're working with to be sure.
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Dear both,

I did not include the .def file, because it generates 100 errors within Visual Studio (e.g. unrecognised token, identifier "module" is undefined, declaration has no storage class, ...), and the user guide was unclear what to include and what is not necessary. So maybe this missing file causes the error. I have to admit that even the .c code caused ~18 errors until I have fixed them. However, I am not sure that I can handle the error messages associated with the .def file.

Which version number you were asking about? The version number of Visual Studio 2010 is 10.0.40219.1 SP1REL. The version number of my DSS is Vesion 6.0b.

Thanks for your help!
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Using Vensim as a DDE Client

Post by Administrator »

In Visual Studio, go to project properties. Expand the "Configuration properties", then select "Linker" and select "input". In here there is a space for "Module definition file", this is where you list the .DEF file.

The .DEF file tells the linker to make certain functions available from the DLL. If the function to tell Vensim the version number is not made available, Vensim cannot call it and reports a -1 instead of the proper version number.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Sorry for the trouble, but this still does not work.
I have now included the .def file in the Linker\Input\module Definition File' space. I also added the Vensim.lib file to the 'Linker\Input\Additional dependencies' space, because without it, the compiler generated a large number of errors.
When both were added, the compiler finished the job without any error or warnings. However, when I loaded this dll file into Vensim, I have got the same error message : "need version 51050 (got -1)".

Do you have any other thoughts what goes wrong?
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: Using Vensim as a DDE Client

Post by tomfid »

I think another thing that can cause the version -1 error is a missing resource file - be sure that you're running the version of the Vensim .dll that installs to your system, not a copy of the dll elsewhere.
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Using Vensim as a DDE Client

Post by Administrator »

Try the attached zip file. I've created a Visual Studio 2010 project that builds.

You will need to change the path to the vensim.h file in vddeext.c. Apart from that it should be good to go.

Tony.
Attachments
dde.zip
(304.75 KiB) Downloaded 325 times
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
alazar
Junior Member
Posts: 7
Joined: Mon Mar 25, 2013 10:25 pm
Vensim version: DSS

Re: Using Vensim as a DDE Client

Post by alazar »

Dear Tony,

Thank you very much for the Visual Studio project, much appreciated!
Yes, if I change the path, it works, and creates a dll what is accepted by Vensim. I found interesting that the dll is created despite the errors in the .c file. I also do not understand why the .c file is considered in the compilation, because I did not find any reference to this file in the Visual Studio settings. Anyway, the dll was accepted by Vensim.

But, when I have tried to run the dde client Vensim application (vddeext.mdl), the program fails. The error message says:
ERROR: DDE failure sending data during ADD_VECTOR.
ERROR: DDE failure sending data during ADD.

I used your folder structure, and I suspect that MS Excel blocks the data connection (Microsoft Office Professional Plus 2010, Version: 14.0.6129.5000 (32-bit)). Therefore, I have tried a number of tricks to get it work, but none helped:
- Uninstalled Vensim and reinstalled it with the default folder path settings.
- Eliminate the errors in the .c file (char type errors). (same result with and without errors)
- Enabled the use of R1C1 reference style (Excel options > Formulas)
- Enabled all data connections (Trust Center Settings >External Content)
- Disabled Safe Mode (Trust Centre Settings > Active X Settings)

Please let me know what else I can try to get the connection work. Thanks!
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Using Vensim as a DDE Client

Post by Administrator »

I'm looking into it now. The connection is made ok, but for some reason the data is not sending.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Post Reply