External Function

Use this forum to post Vensim related questions.
Post Reply
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

External Function

Post by khalediwm »

HI
I am trying to work out an optimization problem, by solving systems of non-linear equations. I know that I have to use external function facilities. But I have 27 nonlinear equations to solve. So, I may have more than 27 optimized variables & parameters to return in Vensim ( in every time step to use for the next time step), instead of a single objective function values.
1) Do you have any suggestion on this issue?
2) Is it OK, if I use Java instead of C/C++ in developing external function coding?
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Post by tomfid »

1) Check to be sure that you can't solve your problem with the FIND ZERO function. If you need to go the external function route, consider hooking up some external library to do most of the work. I once connected Vensim to LINDO (an LP solver) for a similar purpose.

2) External functions have to be compiled in .dlls. It's probably possible to wrap Java to work this way, but there would be a lot of overhead. I'd stick to C.

Tom
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Hi Tom
Thanks for your suggestion. However, I am still not clear, how can I get more than one variable from the external function, as the usual way of calling an external function is Y= Externalfunction (x1,x2,...xn). In case of having all the route, how should I modify my equation. In the conventional way I may able to get only the value of Y.

Thanking you in advance
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Post by tomfid »

You can write an external function that returns an array. I don't have the documentation in front of me, but I think if you look at the example external function library you'll find an applicable example.

The call from Vensim would probably look like:

val : (v1-v27)
myResult[val] = MYFUNC( ... )
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Hi Tom
Thanks for your suggestion. I will try to implement it.
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Post by tomfid »

OK, good luck. If you share more details on what you're trying to accomplish, you might get additional comments.

Tom
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Hi
I am trying to use, external functions of VENSIM to solve nonlinear series of equation, which consist of 26 variables and 33 constants. I used Visual Studio 2008 and IMSL library function to calculate the roots. The program works fine in Visual Studio environment. However, I am having problem while implementing the same algorithm in VENEXT.C file. I am attaching that file, so that you can have a look. I actually created the *.dll file to call from Vensim, as user defined function. I am able to call that function (SLVSYSNONLINREQN) but couldn’t simulate because of floating point error. I am bit unclear about the following points:

1) Under “3-Grouping of functions in a structure- see venext.h “ , it is given as “COSINE (x), 1,0,cos_Func,0,0”. I couldn’t able to find Venext.h file, therefore confused about the different values under cosine function and may did some mistake

2) For non linear equation solution I am using,” Solve_Sys_of_Nonlinear_Equations’ external function and this function also calling “fcn” function. The other possibility could be such that the first function is not able to pass the value to the second one.

I am not bit lost, as I tried to resolve the issue in different ways but none of those succeeded. Could anyone please help me to solve my problem or at least anybody could give me an example where such nonlinear equations are dealt with?
Attachments

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

khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

another file
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 »

The FUNC_DESC structure is in vensim.h (it used to be venext.h but that is no longer used).

To do what you want I would actually recommend that you map the 33 parameters to a small number of arrays (possibly just 1) in Vensim and then just pass the arrays.

The function closest to what you want is probably MATRIX_INPLACE_INVERT which takes one array and makes changes to that (the function is marked as changing its arguments which is important for equation ordering).

Do be careful about the form in which information is passed. Regular arguments are just passed as float (or double in the double precision version) but vectors are passed as structure pointers. To change a value you will need to pass the argument as an array - even if it is a scalar.
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Thanks Bob, for your advice and I will try my best.

Could you please let me know, how to write an external function, with such arrays, as I mentioned to you before that I am not clear about the values under "COSINE" function? I don't know how you choose those 0 and 1 ?

If you have any example which works with arrays and used as external function, please let me have a chance to look in to that.
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

This is the function to use as a prototype:

{"MATRIX_INPLACE_INVERT", // function name
" {matrix} ", // argument list used in equation editor
1, // the number of arguments the function takes in Vensim
1, // the number of vector arguments (these will be first)
INPLACE_INVERT_FUNC, // the function index passed to vensim_external
0, // number of user loops
1, // modification flag - this function will change its arguments
0, // the number of literal arguments (these precede lookups and vectors
0 // the number of Lookups passed (these precede vectors)
},

This passes a vector that it modifies, which is what you want from your description.
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Hi, I just faced another problem regarding external function. Previously my external function was working fine. However, I just transfered all my files to my new PC (Windows7, 64 bit). I coppied the actual VENSIM.DLL file and then mentioned the path in 'startup', but I am not able to see my external functions! Can anybody tell me what is going on?
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

The message that I am getting, "? The external functions in I:\VENSIM.DLL -could not be loaded. Do you want to try loading again next time?"

I tried with clean & reform option but it's not working.
Administrator
Super Administrator
Posts: 4592
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

Are you sure the path to the DLL is correct?
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Yes, I am. I then copied this dll file to another location (thinking that it may not be able to access the root directory) and changed the path, but the same message is appearing! Any suggestion?
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

If VENSIM.DLL is a file that you created and that was working on your previous computer it should still work under Windows 7. There might, however, be a permissions issue. Make sure that the current user has permissions to work with this file. It is also possible that it is being blocked as a potential security threat by a firewall.

If you still have the old computer the best test would be top copy your VENSIM.DLL file back and see is it still works on that computer.
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Thanks Bob
In the old mechine it works fine. However, I just followed all your instructions to resolve the issue with my new PC but still I am having the same problem. Any more idea or suggestion?
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

right click on Vensim and select run as administrator. If that allows you to use your external function dll it is an access control issue. You will need to get a manifest setitng the DLL to asInvoker - this is pretty straightforward to do using Visual Studio 2010, otherwise you will need to the manifest tool from the Microsoft website and figure out how to use it.

If this is not the problem my guess is that the dll got corrupted somehow.
khalediwm
Member
Posts: 42
Joined: Sun Mar 07, 2010 9:58 am

Post by khalediwm »

Thanks bob for your kind reply.
However, I followed your advice to run Vensim as administrator but I am still having the same trouble. Then I created a new VENSIM.DLL file and checked in my older PC which works fine, but it's not working in my new machine. So, there could be 2 issues as per my view:
1) I have installed Vensim 5.10d in my new computer but my old computer has Vensim 5.10C. could it be an issue?
2) I am using Visual studio 2008 to create dll file, is it a problem for Windows 7?
Administrator
Super Administrator
Posts: 4592
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

There is a tool that ships with Visual Studio called dependency checker. If you copy this to the Windows 7 machine, and then open the DLL in dependency checker, it will list what additional DLLs are required by your VENSIM.DLL.

I suspect it may depend on other DLLs that are not on Windows 7.

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

Post by bob@vensim.com »

The external function version changed with 5.10d. You will need to rebuild it with the new vensim.h file that comes with 5.10d. If this is the problem you should be getting an error message about external function version mismatch. Are you?
Post Reply