How can I turn an MDL or a VPMX file into a C/C++ file

Use this forum to post Vensim related questions.
Post Reply
n.secco
Junior Member
Posts: 7
Joined: Wed Apr 26, 2023 2:47 pm
Vensim version: PLE

How can I turn an MDL or a VPMX file into a C/C++ file

Post by n.secco »

I have been given an .mdl file and a .vpmx file, and I have to turn one of them into a C/C++ equivalent file. Which is the best way to do that? Is it using sdverywhere or is there a better way? Because sdeverywhere gives me some errors...
Administrator
Super Administrator
Posts: 4613
Joined: Wed Mar 05, 2003 3:10 am

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by Administrator »

I'm assuming you want to put your model on the web? As an academic PLE user, Forio is an option as well as SD everywhere. PLE is mainly for learning how to build Vensim models, it's not really designed to be capable of publishing to the web.

Vensim DSS does have the capability to compile to web assembly directly.
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
n.secco
Junior Member
Posts: 7
Joined: Wed Apr 26, 2023 2:47 pm
Vensim version: PLE

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by n.secco »

Yes we want to make a web application, more specifically a videogame, but we want to only use free software, that is why Forio and Vensim DSS are not viable options.
So the idea is to convert the model into C/C++ and insert it into Unreal Engine that is based on C++ and is good for videogames.
So far I have tried to use sdeverywhere but I have had 2 major problems.

FIRST PROBLEM

I tried to create the C++ code by using the following command

sde build mymodel.mdl

but I got the following errors:

Code: Select all

mymodel.c:266:16: warning: implicit declaration of function '_RANDOM_NORMAL' is invalid in C99 [-Wimplicit-function-declaration]
  _pinknoise = _RANDOM_NORMAL(-1000000.0, 1000000.0, _noisemean, _noisestddev, _noiseseed);
               ^
mymodel.c:268:17: warning: implicit declaration of function '_GET_TIME_VALUE' is invalid in C99 [-Wimplicit-function-declaration]
  _noiseseedt = _GET_TIME_VALUE(2.0, 0.0, 10.0);
                ^
mymodel.c:270:10: warning: implicit declaration of function '_RANDOM_UNIFORM' is invalid in C99 [-Wimplicit-function-declaration]
  _ri1 = _RANDOM_UNIFORM(0.0, 1.0, _noiseseedt);
         ^
mymodel.c:409:114: warning: implicit declaration of function '_RANDOM_NORMAL' is invalid in C99 [-Wimplicit-function-declaration]
  _random_number_generator = (pow(((2.0 - (_time_step / _noisecrrtime)) / (_time_step / _noisecrrtime)), 0.5)) * _RANDOM_NORMAL(-1000000.0, 1000000.0, _noisemean, _noisestddev, _noiseseed);

I worked around this problem by implementing the functions _RANDOM_NORMAL, _GET_TIME_VALUE and _RANDOM_UNIFORM by myself.

SECOND PROBLEM

The second problem is that I don't know how to call the functions that have been generated by sde.
After implementing the missing functions I got a code which compiled correctly. So I took a look at the C/C++ file representing my model and I found the following auto generated functions: initConstants, initLevels, evalLevels, evalAux and other functions that are called by these ones.
I noticed the the variables that I am interested in are calculated in the evalLevels and evalAux functions. Therefore I organized my code in the following manner:

Code: Select all

//I start out by calling the functions with a name starting with "init..."
//because usually they are the first to be called
initConstants();
initLevels();

//In the following line I set my inputs using a function (setF) that I have defined and implemented
setF(f1, f2, f3);
//Then I try to go on with the simulation by incrementing the auto generated _time variable defined in model.cpp
for (int month = initial_month; month < initial_month + 12; month++) {
	//The setTime function, that I have defined and implemented, modifies the _time variable
	setTime(month);
	evalLevels();//Here I calculate the levels
}
evalAux();//Here I calculate the auxiliaries
//In the following line I get the results by calling a function that I have defined and implemented (getOutputStructure) 
//which returns only the variables that I am interested in
Output output = getOutputStructure();
The results that I got are not exactly as expected therefore there must be some problems in this sequence of calls...
tomfid
Administrator
Posts: 3816
Joined: Wed May 24, 2006 4:54 am

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by tomfid »

The Vensim DSS publication to webassembly doesn't require any software after the initial publication. Your application runs in the client browser as pure html/javascript/wasm.
n.secco
Junior Member
Posts: 7
Joined: Wed Apr 26, 2023 2:47 pm
Vensim version: PLE

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by n.secco »

Tomfid I can't use Vensim DSS because I can only use free software
Administrator
Super Administrator
Posts: 4613
Joined: Wed Mar 05, 2003 3:10 am

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by Administrator »

n.secco wrote: Mon Jun 12, 2023 7:18 am Tomfid I can't use Vensim DSS because I can only use free software
SDE is your only option then. You'll need to contact the SDE folks for help with that.
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: 4613
Joined: Wed Mar 05, 2003 3:10 am

Re: How can I turn an MDL or a VPMX file into a C/C++ file

Post by Administrator »

n.secco wrote: Thu Jun 08, 2023 1:37 pm Yes we want to make a web application, more specifically a videogame, but we want to only use free software, that is why Forio and Vensim DSS are not viable options.
One note. Vensim PLE is only free for academic use. If you use it for any other reason, you need to purchase a licence.
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