vensim DLL with R

Use this forum to post Vensim related questions.
Post Reply
MEresearch
Junior Member
Posts: 4
Joined: Fri Jul 08, 2016 2:51 am
Vensim version: DSS

vensim DLL with R

Post by MEresearch »

I am trying to link my Vensim model with (32-bit) R using the DLL. One reason for this is so that I can perform some analysis of the model, but I also need to link my vensim model with some non systems dynamics models.

I naively tried to just use the R command

Code: Select all

dyn.load('vendll32')
but that didn't work (it didn't give any error messages, just did not have any libraries available afterwards). I am not sure what else to try. Has anyone had any success with this?

Alternatively, I think it is possible to call C or C++ code from R (there are available packages, but I haven't tested them out myself), so any minimal working examples of C or C++ code to load/run the DLL would be useful too. Thanks!

Postscript: I am running VensimDSS 6.4B, Windows 7, Visual Studio 2015 (with visual C++), and R version 3.2.3.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: vensim DLL with R

Post by Administrator »

I've never used R, but will see what I can do.

It's a shame it's called "R", as finding anything relevant on Google is next to impossible.
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
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: vensim DLL with R

Post by Administrator »

Can R actually call 3rd party DLLs? The documentation seems to suggest the answer is no. This page also suggests it's not possible.
http://stackoverflow.com/questions/6510 ... a-dll-in-r

From what I've can figure out, you create a DLL from R code which can then be called from R.
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: 3811
Joined: Wed May 24, 2006 4:54 am

Re: vensim DLL with R

Post by tomfid »

I'm pretty sure this has been done, but I don't recall where. Unfortunately "R" is hard to google. I'll ask around. It should be straightforward to write an R package that wraps Vensim, if it hasn't been done.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: vensim DLL with R

Post by Administrator »

tomfid wrote:It should be straightforward to write an R package that wraps Vensim, if it hasn't been done.
I tried this earlier, it fails at the first hurdle (will not load the DLL). Both x32 and x64 failed.

But I'm sure someone mentioned it in the past. But I could be getting confused with Python. Or possibly an automatic export of Vensim data so it can be used in R.
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
LAUJJL
Senior Member
Posts: 1427
Joined: Fri May 23, 2003 10:09 am
Vensim version: DSS

Re: vensim DLL with R

Post by LAUJJL »

Hi

Jim Duggan (member of this forum as JDuggan on the J list of members) seems to know R quite well. He is teaching a workshop at the next conference on R and publishes too a paper on the bass diffusion written in R studio.

You may try to contact him, by this forum or by his e-mail address that can be found in his conference paper.

Best regards.

JJ
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: vensim DLL with R

Post by tomfid »

The auto-export part is easy if you're using DSS, because you can run a postprocess command script to automatically export the dataset. Also, there are a lot more options for formatting in the latest release, including tidy data.
MEresearch
Junior Member
Posts: 4
Joined: Fri Jul 08, 2016 2:51 am
Vensim version: DSS

Re: vensim DLL with R

Post by MEresearch »

Thanks for all your helpful and prompt replies. I just composed a long message but when I clicked submit my login had timed out and I lost it all! So I will try to remember what I had written.

Firstly, I totally agree with the "R" as a name issue. It makes it SO difficult to search for relevant help.
Administrator wrote:I tried this earlier, it fails at the first hurdle (will not load the DLL). Both x32 and x64 failed.
I get a different issue. I can load the vendll32.dll using dyn.load() in R. I can then check that this is loaded using getLoadedDLLs(), which confirms that vendll32 is loaded, and that Dynamic.Lookup=TRUE. (I have installed Rtools developer tool package, which may make a difference here.)
However, if I then try to use any of the C commands in the dll using the .C( ) command in R (for example .C("vensim_command","FILE>EXISTS|RABFOX.vpm",PACKAGE="vendll32") ) it crashes R: "R encountered a fatal error. The session was terminated." It is definitely interacting with the DLL though, as if I use a command that is not in there is comes back with: "nonexistent_vensim_command" not available for .C() for package "vendll32".
tomfid wrote:It should be straightforward to write an R package that wraps Vensim, if it hasn't been done.
I think this might be the way to go. I don't think I have the know-how to do this unfortunately. I will try contacting Jim Duggan, as he has just published a book called "Systems Dynamics in R", so may have worked out what can and can't be done.

Big picture, I am wanting to set values for model parameters (read cin file), run the vensim model for a year then get values at the end of the year (at double-precision), use those values as inputs to a model written in R code, then use values the R code calculates to set values for the Vensim model and then run it for another year. I tried running the model one year at a time and using the final time values of stocks as initial conditions for the next year's run, but due to the scaling in my model the exported values being only 6sf accuracy led to very different behaviour. This led me to the conclusion that I need to run the model in gaming mode in 1 year intervals, using get_dpval and set_val. This means I can't use the batch command file approach.

I should note that I am not wed to R, so if there is an implementation in Python that would work for me I think. I know I can call R code from Python, so I could use Python as the bridge/integrator between the models. I haven't found any python code for this in my searches yet. Anyone have any links to where this has been done?
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: vensim DLL with R

Post by tomfid »

Ugh. Usually if you hit the "back" button (ignoring the login request) it's all there still, and you can copy it before logging in. Sorry.

You might try vensim_be_quiet, which takes a simple int argument, to see whether the issue is the argument format. (It looks like you'd have to pass the argument via as.integer(0).)
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: vensim DLL with R

Post by tomfid »

It seems like a strong candidate theory is that your command string is not getting passed as a c-format null terminated char*. It looks like as.raw() or as.character() might be the appropriate way to do that, but I don't see much good documentation on .c() (but again, the search problem in spades).
MEresearch
Junior Member
Posts: 4
Joined: Fri Jul 08, 2016 2:51 am
Vensim version: DSS

Re: vensim DLL with R

Post by MEresearch »

For some reason when I hit back it only had the first two lines of the message. Oh well, lesson learned!

I have tried as.integer(0) as the input for the .C() call but it still causes R to crash.

I was going to try to just get it running from some c code, but the Vensim/DLL directory that came with my install doesn't have a C or C++ example. The closest I can find to what I think I'd need are the venext.c and vensim.c files, so I'm going to try to work out what they do?! (I don't actually know the difference between C and C++ and have never written any code that needs compiling, so the odds of it working are not great.)

ps: I used to live in Bozeman (was a postdoc at MSU)
MEresearch
Junior Member
Posts: 4
Joined: Fri Jul 08, 2016 2:51 am
Vensim version: DSS

Re: vensim DLL with R

Post by MEresearch »

As an update, I have taken the easy way out (for now) and used python:

Code: Select all

from ctypes import *
mydll = windll.LoadLibrary("vendll32")
print mydll.vensim_check_status() #check status, which outputs 6 at the beginning
output=mydll.vensim_command("SPECIAL>LOADMODEL|RABFOX.vpm")
print("file loaded successfully" if output==1 else "error loading file") #check file loaded ok
print mydll.vensim_check_status() #check status, which should now output 0 
mydll.vensim_command("MENU>RUN|o")
(following Matlab demo file in Vensim Download)
It all seems to be working so far! Now to try to get it working on my model file and planned setup instead of the demo one...
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: vensim DLL with R

Post by Administrator »

MEresearch wrote:I was going to try to just get it running from some c code, but the Vensim/DLL directory that came with my install doesn't have a C or C++ example.
It does, C:\Users\Public\Vensim\DLL\msvc60.
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