Global variables

All topics on coding 4Dscript in Enterprise Dynamics.
Post Reply
SullivanM
Posts: 3
Joined: Sunday 05 June, 2011 - 14:47

Global variables

Post by SullivanM »

Hello,

I'm struggling with major issues. Last night, I linked my model with excel in order to write down different time measurements that I'm taking throughout a simulation run. The different kind of measurements belong to certain columns, but one simulation run is written down in on row only. So before the run starts, I'd like to go down one row.

I have implemented this by using the global variable "row". I declared the variable in the atom-editor in my model, in the section "Events" and "Init", in one of the sources.

Code: Select all

dim([Row],vbvalue,5)
There's one source in my model, that only produces one product per run in the very first second of the run. That's where I put the code in trigger on exit:

Code: Select all

Row:=row+1
Now, even though I'm using the variable "row" in many other atoms, there was never a problem last night. But when I started the program this morning, first of all an error appeared:

Code: Select all

3 Compile error: "Row" is not a valid expression. Reference: Excelwrite(Row,3,(time/60))
4 Could not set expression to attribute 'EntryTrigger' on atom 'Source'.


And second of all, every single "excelwrite-code" is not working properly either. So in the end, after one run is over, only a few times are written down in row. It seems like one or two excel-write codes are working correctly, but the rest of the 3/4 are not. Moreover, it's written down in row 5 instead of row 6.

I really need some help on this. Thanks!!
MarvinH
Posts: 93
Joined: Tuesday 25 January, 2011 - 11:07
Contact:

Re: Global variables

Post by MarvinH »

Hello!

The first problem you mention has to do with the way a model is created in Enterprise Dynamics. The model is created from the first atom in the model tree downwards. I assume that your source that produces one product (let's call it SourceA) is positioned above (with respect to the order in the model tree) the Source on which the variable is declared in your model (let's call this SourceB). So when the model is loaded, first the exit trigger on SourceA is set, but because SourceB is not loaded yet, the expression Row is not known yet and the exit trigger is not set correctly.

To solve this, make sure that the atoms on which global variables are declared are positioned above atoms on which is refered to these global variables. I think that when you modify your model in this way, the other problems will be solved as well.

The best way to do this is by use of a dedicated atom which you can call "Definitions". Put this atom on rank 1 of the model, and declare all global variables as used in the model OnInit of this atom. Because this atom is loaded first, all other atoms will load without errors.

Good luck!

Kind regards,

Marvin
SullivanM
Posts: 3
Joined: Sunday 05 June, 2011 - 14:47

Re: Global variables

Post by SullivanM »

Thank you, everything is fixed. One more question: I'd like to evaluate the Avgtime of a container, staying in an unpacker via Experiment wizard. But if I select the unpacker and type in Avgstay(cs), it only shows me how long each product on the container is staying until it is unpacked!
menno
Posts: 45
Joined: Tuesday 29 March, 2011 - 16:01

Re: Global variables

Post by menno »

Hello,

AvgStay(cs) calculates the average staytime of all atoms that have been in Unpack. Internally, the Unpack atom, directly unpacks all atoms out of the container and releases them all when the cycletime is over. For example: if cycletime = 10 sec, and a containter holds 8 products, then the avgstay of unpack will be (10 sec/9 products(=8prod+1cont)) = 1.111 sec.

So if we want to know the average staytime of only the container, then we need to know the average cycletime of the Unpack. The calculate this, we must know two things:
1. the number of container that has passed through
2. the cumulative cycletime
These can be updated on the entry and exit trigger by means of labels (which must be set to 0 on reset!).

A way to do this is:

Code: Select all

{Entrytrigger Unpack:}
label([staytimecontainer],c) := time

{Exittrigger Unpack:}
label([staytimecontainer],c) := time - label([staytimecontainer],c) ,
label([cumulativestaytimecontainer],c) := label([cumulativestaytimecontainer],c) + label([staytimecontainer],c) ,
Inc(label([numberofcontainers],c) ),

label([avgstaytimecontainer],c):=label([cumulativestaytimecontainer],c) /label([numberofcontainers],c)

In the experiment wizard, you must then not select AvgStay(cs), but define your one variable

Code: Select all

label([avgstaytimecontainer],cs) 
With this code it should work. Do not forget to reset your label values!
SullivanM
Posts: 3
Joined: Sunday 05 June, 2011 - 14:47

Re: Global variables

Post by SullivanM »

Oh I see that's why! I tried out your suggestion and it's working perfectly now! I didn't know how AvgStay is really working, so this helps a lot!

Thanks for your help!
CyrusMMXI
Posts: 9
Joined: Friday 04 November, 2011 - 11:51

Re: Global variables

Post by CyrusMMXI »

MarvinH wrote:Hello!

The first problem you mention has to do with the way a model is created in Enterprise Dynamics. The model is created from the first atom in the model tree downwards. I assume that your source that produces one product (let's call it SourceA) is positioned above (with respect to the order in the model tree) the Source on which the variable is declared in your model (let's call this SourceB). So when the model is loaded, first the exit trigger on SourceA is set, but because SourceB is not loaded yet, the expression Row is not known yet and the exit trigger is not set correctly.

To solve this, make sure that the atoms on which global variables are declared are positioned above atoms on which is refered to these global variables. I think that when you modify your model in this way, the other problems will be solved as well.

The best way to do this is by use of a dedicated atom which you can call "Definitions". Put this atom on rank 1 of the model, and declare all global variables as used in the model OnInit of this atom. Because this atom is loaded first, all other atoms will load without errors.

Good luck!

Kind regards,

Marvin
Marvin! Would you please conduct me how can I change the rank of the atom in which the global variable is defined?
MarvinH
Posts: 93
Joined: Tuesday 25 January, 2011 - 11:07
Contact:

Re: Global variables

Post by MarvinH »

Hello!

To change the rank of an atom in the Model Tree, you can use the Interact window (short cut Shift + F6). Now select the atom of which you want to change the rank within the tree, and place the following code in the Interact window:

Code: Select all

SetRank(1, TreeAtom)
The function TreeAtom refers to the atom selected in the Model Tree. 1 is the new rank of the selected atom within the container it is in. Here you can use any value, depending on your desired new position. Please refer to the Help file (F1) for more info on these functions. To change the container the selected atom is in, please refer to the MoveAtom function in the Help file.

Be sure the refresh the Model Tree (press F5 after activating the Model Tree) after executing the code in the Interact window.

Good luck!
CyrusMMXI
Posts: 9
Joined: Friday 04 November, 2011 - 11:51

Re: Global variables

Post by CyrusMMXI »

Thanks Marvin! It worked, however I have defined the Global Variables in an Initialize atom so that this atom can be used in other models conveniently. 1st would you please let me know if I have made the right choice to use Initialize to define Global Variables there? 2nd Is there any way to code this Rank Setting of the Initialize atom to 1 in the Model Tree automatically and without having the user to interfere and set its rank to 1? I want to let my friends use this Initialize atom and its codes in their own models without having to set the rank manually.
MarvinH
Posts: 93
Joined: Tuesday 25 January, 2011 - 11:07
Contact:

Re: Global variables

Post by MarvinH »

Hello!
CyrusMMXI wrote:1st would you please let me know if I have made the right choice to use Initialize to define Global Variables there?
Yes, the Initialize atom is perfect for defining global variables, as it can be seen as the initialization of the model.
CyrusMMXI wrote:2nd Is there any way to code this Rank Setting of the Initialize atom to 1 in the Model Tree automatically and without having the user to interfere and set its rank to 1?
When you are using the Initialize atom, and the checkbox "Execute on reset" is checked, then the code is executed everytime you reset your model. You can use the code I posted earlier about setting the rank of the atom, but then not using the TreeAtom but the atom on which the code is executed itself, by use of the reference function "c". So on the initialization code, use:

Code: Select all

SetRank(1, c)
Good luck!
Post Reply