c language in vensim

Use this forum to post Vensim related questions.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

c language in vensim

Post by Anna »

I have already seen Chapter 8 of the DSS reference about external function.I find all external function are return one value.If I want to return many values ,I know to use the way that "call by address" in c language.Can Vensim support the "call by address" capability?That is what I ask!
Thank you!
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

Hi,

Vensim does support calling by address. Look at the PSUM and MATRIX_INVERT functions in the file VENEXT.C (located in 'comp' sub-directory under the Vensim installation).

PSUM passes a vector of values to the external function library, MATRIX_INVERT passes a vector and returns a vector.

Hope this helps.

Tony.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

By the way,I want to ask something in the venext.c,for example :
{"PSUM"," {vector} , {nelm} ,{nelmlimit} ",3,1,PSUM_FUNC,0,0,0,0}
^^ ^^^^^^
What do the "1" and "0" mean?
Thanks a lot! Tony

Anna^^
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

[quote]Originally posted by Anna
By the way,I want to ask something in the venext.c,for example :
{"PSUM"," {vector} , {nelm} ,{nelmlimit} ",3,1,PSUM_FUNC,0,0,0,0},


What do the "1" and "0, 0,0,0" mean?
Thanks a lot! Tony

Anna^^

[Edited on 3-29-2006 by Anna]
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

You need to look in VENSIM.H for information on that.

The relevant parts are below. Hope it helps.

Tony.

/**************************************************************************
Part 3

fuction prototypes and structure definitions specific to external functions

***************************************************************************/

/* this is used as a convenient way to store function info */
typedef struct _fnct_desc {
char *sym ; /* function names - as it will be used in Vensim */
char *argument_desc ; /* description of arguments use in equation editor */
unsigned char num_args ; /* 0 - 255 */
unsigned char num_vector ; /* for manipulating vectors or changing the
value of model variables */
unsigned short func_index ; /* the index for the user function 0-32000 */
char num_loop ; /* the number of user managed loops - for defining
more than one element of an array or matrix - use -1 to signal
a constant definition function and -2 a data definition function */
unsigned char modify ; /* flag to indicate that function may change its
its inputs - only makes sense if num_vector > 0 */
unsigned char num_literal ;
unsigned char num_lookup ;
} FUNC_DESC ;
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

That lot is much easier to read in a monospaced font. Just take a look at vensim.h and do a search for fnct_des.

All the best,

Tony.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

I wrote a simple function called "test_func"in the venext.c to test if their vales(x and y) exchang.(their value will defind in vensim_dss)
But its have many errors .
These sentences are what I added:
--------------------------------------
#define test_FUNC 23
double test(double x,double y);
{"test","{x}, {y} ",2,0,test_FUNC,0,0,0,0},
case test_FUNC :
rval = test(val[0].val,val[1].literal) ;
break ;
void ex(double *,double *)
double test(double x,double y )
{
ex(&x,&y) ;
return y ;
}
void ex(double *a,double *b)
{
double temp ;
temp= *a ;
*a= *b ;
*b= temp ;
}
--------------------------------------------
I give the venext.c and take a look ,please.
Thank you!

[Edited on 4-11-2006 by Anna]
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

What were the error messages?
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Oh ,it 's ok after I modify "double" to "int " and "test_func" to "main_func".But I feel strange why the function's name chang to "main_func" ,it will be ok.The function's name should be named as I wish ,don't they?
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

read input file

Post by Anna »

I do a test in venext.c to check if vensim can open and read external file.I build a function to do this thing.It body is fllowing below :(There is only one value in in.dat)
====================================
505 int VCTypeCount(int m)
506 {
507 int in ;
508 FILE *IN ;
509 if((IN=fopen("In.dat","r"))==NULL)
510 {
511 printf("can't open file\n");
512 exit(0);
513 }
514 fscanf(IN, "%d\n", &in);
515
516 return (m+in);
517
518 }
======================================
The error messages are:
venext.c<508>:error c2059:syntax error:';'
venext.c<509>:error c2059:syntax error:'='
venext.c<514>:error c2059:syntax error:','
venext.c<514>:error c2143:syntax error:missing'}'before'string'
venext.c<514>:error c2143:syntax error:missing'{'before'string'

venext.c<514>:error c2059:syntax error:'<unknow>'
venext.c<514>:error c2059:syntax error:'}'
venext.c<516>:error c2059:syntax error:'return'
venext.c<518>:error c2059:syntax error:'}'

venext.c<632>:error c2371:syntax error:'lu_decomposition':redefinition;different basic type
=======================================
Can't it use "fopen" to open and read other file in vensim?
That's so strange that I don't chang 'lu_decomposition' anymore.

=========================================
Because I must add a program about neural network which was written by c language,I do somthing to test vensim's ability fist .

[Edited on 4-5-2006 by Anna]

[Edited on 4-5-2006 by Anna]
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

Those are basic C errors, nothing to do with Vensim at all.

If you want to email the C/C++ workspace to me I will see if it compiles ok. My direct email is tony@ventanasystems.co.uk.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

I can't find where I should modify it upstairs.
I don't know where I written wrong.I email the file to you
and thanks for your help.
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

I have removed some of the things you don't need and attached it to this message. It compiles fine using Microsoft Visual C++.

What compiler are you using?

Tony.

[Edited on 5-4-2006 by Administrator]

[Edited on 5-4-2006 by Administrator]
Attachments

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

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

Post by Administrator »

ok, try the attached Microsoft Visual C++ project. It creates the DLL called Fej1023.dll, load this in Vensim and you will have access to your external functions.

Let us know how you get on.

Tony.
Attachments
Fej1023.zip
(22.86 KiB) Downloaded 314 times
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Thank you for all what you do ,I got the "fej1023.dll".
Then ,I open new a dll project call "dlltest" and put the venext.c and venext.def added to the project.I tried to make a new dll but the error message was"d:\dlltest\venext.c(872) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe."
Strangly,I could got dll when I cpmplie the project you gave me but I couldn't get it when I cpmplie the project I made .
I just told to you the condiction and you can ignore it if you think that was nothing.
I have already written the program about neural network.Next week,I will try to add it in the venext.c.
I hope that it will be ok to make vensim_dss run.
God bless me .
Thank you,Tony.
bob@vensim.com
Senior Member
Posts: 1107
Joined: Wed Mar 12, 2003 2:46 pm

Post by bob@vensim.com »

Hi Anna,

The problems you are having are not related to Vensim but only to the C development environment you are using.

Having said that if you have a projec that is working just add your code to that project by editing venext.c, or perhaps adding in other source files. If you really want to get the project you created working you might check the Microsoft help forums.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Wu..Wu...

Post by Anna »

When I add my neural network (it run well in vc++) and modify somewhere to suit the model that I will build in the future.
There are 102 errors .My God!
I don't know why it had errors because somewhere that errors mean I thought their were be fine.@@
I email something for you.Please take a look.
Thank you!
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Sorry , I modify the venext.c again.

[Edited on 4-11-2006 by Anna]
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

These are not Vensim problems you are having. They are just syntax errors.

One thing you need to change is in the call to vensim_external.

case network_FUNC :
rval = network((int)val[0].val ,(int)val[1].val ,(int)val[2].val ,(int)val[3].val ,(int)val[4].val ,(int)val[5].val ,(int)val[6].val,(int)val[7].val ,(int)val[8].val ,(int)val[9].val ,(int)val[10].val ,(int)val[1].val ,(int)val[2].val,(int)val[3].val ,(int)val[4].val ,(int)val[5].val ,(int)val[6].val,(int)val[7].val,(int)val[8].val,(int)val[19].val) ;
break ;

I assume that is correct.

Other than that, there are some errors in "network" (this is not standard 'C') such as
in[layers_num[0]]={a,b,c,d,e,agw,aggw,bgw,bggw,cgw,cggw,dgw,dggw,egw,eggw,aio,bio,cio,dio,eio};//¨úINPUT¸ê®Æ

and some brackets missing in places.

Hope this helps.

Tony.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Thank you Tony and Bob.Now ,I have 7errors:

1.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(208) : error C2065: 'NETWORK_FUNC' : undeclared identifier
2.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(209) : error C2065: 'VC_FUNC' : undeclared identifier
-------------------------------------------------------------------------
*Does The function's name was named as I wish?Do I think right?
==========================================
3.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(208) : error C2099: initializer is not a constant
4.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(209) : error C2099: initializer is not a constant
5.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(524) : error C2051: case expression not constant
6.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(527) : error C2051: case expression not constant
7.D:\reserch\model\Fej1023\Fej1023\VENEXT.C(1102) : error C2059: syntax error : '{'
----------------------------------------------------------------------------
I really don't know how to modify thease parts .
*case NETWORK_FUNC :
rval = NETWORK((int)val[0].val ,(int)val[1].val ,(int)val[2].val ,(int)val[3].val ,(int)val[4].val ,(int)val[5].val ,(int)val[6].val,(int)val[7].val ,(int)val[8].val ,(int)val[9].val ,(int)val[10].val ,(int)val[11].val ,(int)val[12].val,(int)val[13].val ,(int)val[14].val ,(int)val[15].val ,(int)val[16].val,(int)val[17].val,(int)val[18].val,(int)val[19].val) ;
break ;
-----------------------------------------------------------------
*My "network " 20 inputs are all double number.Shouldn't I modify it as:
rval = NETWORK(val[0].val ,val[1].val ,val[2].val ,val[3].val ,val[4].val ,val[5].val ,val[6].val,val[7].val ,val[8].val ,val[9].val ,val[10].val ,val[11].val ,val[12].val,val[13].val ,val[14].val ,val[15].val ,val[16].val,val[17].val,val[18].val,val[19].val) ;
-----------------------------------------------------------
*( I tried it but error was the same.)
in[20]={a,b,c,d,e,agw,aggw,bgw,bggw,cgw,cggw,dgw,dggw,egw,eggw,aio,bio,cio,dio,eio};
The sentence declare the 20 inputs are an array called "in"
Why it iwas wrong?

[Edited on 4-11-2006 by Anna]

[Edited on 4-11-2006 by Anna]
Attachments

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

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

Post by Administrator »

All the errors you see are just very basic syntax errors. You need to get some help from a C/C++ programmer.

I have attached a C++ project workspace that compiles. I cannot help any more on this though as you really need some programming help.

Tony.
Attachments
Fej1023.zip
(78 KiB) Downloaded 253 times
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Thank you for you to help me modify the file.
My model can't run when I added it. I email to you .Can you help me to check?
I don't know about any c++.Actuality,This was my first time to know what is the dll file and to write neural nework by c .
Our lab usually use Fortran and I often feel upset and impotent last several months because all many laboratory use vensim in Taiwan don't touch this part.
:( Help me ,please~~~ ><
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Sorry,I know you are busy .
I 'm very worried about vensim model can't do I want by adding the network.Can I have any message from you?
Do you get my email on Tuesday?
:(
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

I found where was the problem!

Post by Anna »

I found some little problems and modify it .Now "NETWORK" which I made is fine but VC is not fine.
Then I did some tests to test where were the problems happen?
Because I use VC_FUNCTION 15 times(15 variables will use it) in my model.I tried to reserve one variable and others were deleted,then if model run successfully ,I add one variable in the model to test again.
I found model run ok when it just have 5 variables,but when it was added sixth to fifteenth variables,the error happened.
It means that if I give 6~15 number as parameters ,the VC function will not be fine.
Where places in the VC function I sould modify ?
My model and dll is too big to be attached on the web.
I email for you.
Thank you.

[Edited on 4-18-2006 by Anna]

[Edited on 4-18-2006 by Anna]

[Edited on 4-18-2006 by Anna]

[Edited on 4-18-2006 by Anna]
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

I found one thing again.When I added the part model which use NETWORK_FUNC to attempt to combine them . The VC_function just can be fine when it accept "int 0 " in its body.So strangly!I email to you again.
Post Reply