VENSIM DLL calls from Matlab

Use this forum to post Vensim related questions.
Post Reply
rlsmith5
Junior Member
Posts: 14
Joined: Thu Apr 11, 2013 1:49 pm
Vensim version: DSS

VENSIM DLL calls from Matlab

Post by rlsmith5 »

I am using Matlab as an experimentation platform and desire to interface to my Vensim SD model (in blackbox fashion) using the Vensim DLL. I have been successful in loading and confirming the simulation model execution using the Vensim DLL. However, I have been unsuccessful in getting information returned from the executed model; for example, the 'vensim_get_info' and 'vensim_get_data' dll calls through the Matlab calllib function do not return good results.

I am using Vensim DSS 6.3 (32-bit) and Matlab 2014a (32-bit).

I have included below an example of the Matlab code attempting to interface with the RABFOX.vpm model provided in the /dll subdirectory. I expected that the pointers for rvalPtr1 and tvalPtr1 would provide me with results, or reference to variables rpop and time, respectively; however, I am only left with an array of zeros as initially generated. My knowledge string/pointer handling in Matlab is low. I attempted to follow questions by others on the forum - in particular, Olivia's questions regarding similar type inquiry - but I could not get a good result.

I would appreciate some guidance and suggestions. Thanks, Raymond

code attached:

Code: Select all

%% MATLAB to VENSIM by-way-of DDL

clear all
%*********************LOAD THE MODEL *************************

if not(libisloaded('VenDLL32'))
    addpath('C:\Users\Raymond\Documents\MATLAB\project_vensimdll');
    hfile = ['C:\Users\Public\Vensim\dll\vendll.h'];
    loadlibrary('VenDLL32',hfile);
end

% Display Vensim DDL function calls and signatures
libfunctionsview VenDLL32

%% *********************EXECUTE SIMULATION *************************
% check calllib returns to verify initiation

str=['SPECIAL>LOADMODEL|C:\Users\Public\Vensim\dll\RABFOX.vpm'];
calllib('VenDLL32','vensim_check_status')
calllib('VenDLL32','vensim_command',str) 
calllib('VenDLL32','vensim_start_simulation',0,0,1) 
calllib('VenDLL32','vensim_finish_simulation')
 
%% *********************RETRIEVE DATA *************************

time = zeros(250);
rpop = zeros(250);

rvalPtr1 = libpointer('singlePtr', rpop);
tvalPtr1 = libpointer('singlePtr', time);
tpoints1 = calllib('VenDLL32','vensim_get_data','RABFOX.vdf','Rabbit Population','time',rvalPtr1(1),tvalPtr1(1),250);

rvalPtr1(1).Value
tvalPtr1(1).Value
%******************** UNLOAD VENSIM DLL ********************
%% Now unload the library 

unloadlibrary VenDLL32

%  end of command statements
Raymond Smith
North Carolina State University
Administrator
Super Administrator
Posts: 4589
Joined: Wed Mar 05, 2003 3:10 am

Re: VENSIM DLL calls from Matlab

Post by Administrator »

I'm not familiar with Matlab.

What happens if you try
tpoints1 = calllib('VenDLL32','vensim_get_data','RABFOX.vdf','Rabbit Population','time',rvalPtr,tvalPtr1,250);

also try
rvalPtr1 = libpointer('single', rpop);
tvalPtr1 = libpointer('single', time);
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
rlsmith5
Junior Member
Posts: 14
Joined: Thu Apr 11, 2013 1:49 pm
Vensim version: DSS

Re: VENSIM DLL calls from Matlab

Post by rlsmith5 »

Investigating the suggestions:

(A) What happens if you try
tpoints1 = calllib('VenDLL32','vensim_get_data','RABFOX.vdf','Rabbit Population','time',rvalPtr,tvalPtr1,250);

Result:
No discernible change in the generated output from previous instances where 'rvalPtr1(1)' and 'tvalPtr1(1)' were used. The result was that all entries remain assigned to zero value, where we should see time advance and the population demonstrate oscillation.

(B) also try
rvalPtr1 = libpointer('single', rpop);
tvalPtr1 = libpointer('single', time);

Result:
Generates an "Error in libpointer" that indicates the parameter must be scalar (previously no errors).


Using the signature for 'Functions in library VenDLL32' presented in Matlab, the vensim_get_data function has the following arguments and returns:

Return type:
[int32, cstring, cstring, cstring, singlePtr, singlePtr]
Name:
vensim_get_data
Arguments:
(cstring, cstring, cstring, singlePtr, singlePtr, int32)

The argument data type 'singlePtr' should have the equivalent Matlab type of 'single', as suggested above - but this did not work.

Any ideas or similar examples that might work?

Thanks, Raymond
Raymond Smith
North Carolina State University
Administrator
Super Administrator
Posts: 4589
Joined: Wed Mar 05, 2003 3:10 am

Re: VENSIM DLL calls from Matlab

Post by Administrator »

Could the "250" be being passed as a long instead of int32? What happens if you define it as a variable before passing it to the function?
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
rlsmith5
Junior Member
Posts: 14
Joined: Thu Apr 11, 2013 1:49 pm
Vensim version: DSS

Re: VENSIM DLL calls from Matlab

Post by rlsmith5 »

A new week and the challenge continues .... I have spent quite a bit of time trying to figure this out without success. I have attached the most recent code below and a description of what I have learned thus far.

Currently it appears that the problem may be the handling of the reference pointers in Matlab. I do not have strong Matlab skills but after reviewing others code and the references it is not obvious why I can not obtain the resultant data. I took the example Vensim model "RABFOX.mdl" and modified it with an initial rabbit population of 543 and a model start time of 3, in order to better reveal if any information was being passed. Conclusion, it appears that the initial time and rabbit population are obtained, however, all forward moving times are not. In addition, the 'vensim_get_data' command does return values for the cstring_params return parameters, and similarly the singlePtr_vars (again only for the initial time values). I have provided of the results to the similar farther down below.

Matlab Code

Code: Select all

clear all
%*********************LOAD THE MODEL *************************

if not(libisloaded('VenDLL32'))
    addpath('C:\Users\Raymond\Documents\MATLAB\project_vensimdll');
    hfile = ['C:\Users\Public\Vensim\dll\vendll.h'];
    loadlibrary('VenDLL32',hfile);
end

% Display Vensim DDL function calls and signatures
libfunctionsview VenDLL32

%% *********************EXECUTE SIMULATION  *************************
% check calllib returns to verify initiation
str=['SPECIAL>LOADMODEL|C:\Users\Public\Vensim\dll\myRABFOX1234.vpm'];
calllib('VenDLL32','vensim_check_status')
calllib('VenDLL32','vensim_command',str) 
calllib('VenDLL32','vensim_start_simulation',0,0,1) 
calllib('VenDLL32','vensim_finish_simulation')


%% *********************RETRIEVE DATA  *************************

tnum = 250;
timepts = int32(tnum);

mytime = zeros(1,tnum);
mytimePtr1 = libpointer('singlePtr', mytime);

myrabpop = zeros(1,tnum);
myrabpopPtr1 = libpointer('singlePtr', myrabpop);

% vensim get data command
[int32_status, cstring_vdf, cstring_var1, cstring_var2, singlePtr_var1, ... 
    singlePtr_var2] = calllib('VenDLL32','vensim_get_data','CURRENT.vdf', ...
    'Rabbit Population','time',myrabpopPtr1(1),mytimePtr1(1),timepts);

myrabpopPtr1(1).Value;
mytimePtr1(1).Value;
Matlab Pseudo Results:

Code: Select all


int32_status =      1
cstring_vdf =       CURRENT.vdf
cstring_var1 =     Rabbit Population
cstring_var2 =     time

singlePtr_var1'  = 
   543
     0
     0
     0
    ...
     0

singlePtr_var2'  =
     3
     0
     0
     0
    ...
     0

myrabpopPtr1(1).Value'   =
   543
     0
     0
     0
    ...
     0

mytimePtr1(1).Value'  =
     3
     0
     0
     0
    ...
     0

end of file 
Raymond Smith
North Carolina State University
Administrator
Super Administrator
Posts: 4589
Joined: Wed Mar 05, 2003 3:10 am

Re: VENSIM DLL calls from Matlab

Post by Administrator »

Given you get singlePtr_var1' = 543, it looks like things are working correctly. But you said you only get it for time = 0, and now I can see why.

Code: Select all

calllib('VenDLL32','vensim_start_simulation',0,0,1) 
calllib('VenDLL32','vensim_finish_simulation')
All this does is start and stop the sim at time = 0. You need to call vensim_continue_simulation as well to step it forward. Or use a venapp command instead.

What happens if you run the following? It should run a complete simulation.

Code: Select all

str=['SPECIAL>LOADMODEL|C:\Users\Public\Vensim\dll\myRABFOX1234.vpm'];
calllib('VenDLL32','vensim_check_status')
calllib('VenDLL32','vensim_command',str) 
str=['MENU>RUN|o'];
calllib('VenDLL32','vensim_command',str)
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
rlsmith5
Junior Member
Posts: 14
Joined: Thu Apr 11, 2013 1:49 pm
Vensim version: DSS

Re: VENSIM DLL calls from Matlab

Post by rlsmith5 »

Your suggestion has resolved the problem and now everything works as expected. I sincerely appreciate your efforts to troubleshoot this issue.

I am attaching below a clean copy of the Matlab code that has been proven to work and can be accessed by others using the RABFOX.vpm model installed with the Vensim DSS v6.3 software under the the DLL subdirectory.

Thank you, Raymond

Code: Select all

%% MATLAB to VENSIM by-way-of DDL

% The code below documents a successful example using the Vensim DDL
% from Vensim DSS v6.3 32-bit in conjunction with calls from Matlab 
% R2014a 32-bit.
%
% The SD model 'RABFOX.vpm' can be found in the Vensim DSS subdir DLL.
%
% I appreciate the helpful bread crumbs left in Olivia's posts a few 
% years ago, and the help troubleshooting the vensim calls this is example 
% by the forum administrator (Tony)!
%
% Raymond Smith, North Carolina State University, March 16, 2015

clear all
%*********************LOAD THE MODEL *************************

if not(libisloaded('VenDLL32'))
    addpath('C:\Users\Raymond\Documents\MATLAB\project_vensimdll');
    hfile = ['C:\Users\Public\Vensim\dll\vendll.h'];
    loadlibrary('VenDLL32',hfile);
end

% Display Vensim DDL function calls and signatures
libfunctionsview VenDLL32

%% *********************EXECUTE SIMULATION *************************
% check calllib returns to verify initiation

str=['SPECIAL>LOADMODEL|C:\Users\Public\Vensim\dll\RABFOX.vpm'];
calllib('VenDLL32','vensim_check_status')
calllib('VenDLL32','vensim_command',str) 
str=['MENU>RUN|o'];
calllib('VenDLL32','vensim_command',str)

 
%% *********************RETRIEVE DATA *************************
% retrieve data from the post simulation execution

tnum = 201;
timepts = int32(tnum);

mytime = zeros(1,tnum);
mytimePtr1 = libpointer('singlePtr', mytime);

myrabpop = zeros(1,tnum);
myrabpopPtr1 = libpointer('singlePtr', myrabpop);

% vensim get data command
[int32_status, cstring_vdf, cstring_var1, cstring_var2, singlePtr_var1, ... 
    singlePtr_var2] = calllib('VenDLL32','vensim_get_data','CURRENT.vdf', ...
    'Rabbit Population','time',myrabpopPtr1(1),mytimePtr1(1),timepts);

myrabpopPtr1(1).Value'
mytimePtr1(1).Value'

%% ******************** UNLOAD VENSIM DLL ********************
% unload the library 

unloadlibrary VenDLL32

%  end of command statements
Raymond Smith
North Carolina State University
Administrator
Super Administrator
Posts: 4589
Joined: Wed Mar 05, 2003 3:10 am

Re: VENSIM DLL calls from Matlab

Post by Administrator »

Do you mind if I include this in the Vensim help system?
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
rlsmith5
Junior Member
Posts: 14
Joined: Thu Apr 11, 2013 1:49 pm
Vensim version: DSS

Re: VENSIM DLL calls from Matlab

Post by rlsmith5 »

You are welcome to include it in the Vensim help system - and make modifications as appropriate. Thank you for your help!
Raymond Smith
North Carolina State University
khairahnazurah
Newbie
Posts: 1
Joined: Fri Mar 10, 2017 4:45 am
Vensim version: PLE

Re: VENSIM DLL calls from Matlab

Post by khairahnazurah »

is this possible in vensim DSS 6.2?
tomfid
Administrator
Posts: 3808
Joined: Wed May 24, 2006 4:54 am

Re: VENSIM DLL calls from Matlab

Post by tomfid »

Should be - nothing has changed.
Post Reply