For Loop in Server

All topics on coding 4Dscript in Enterprise Dynamics.
Post Reply
Annaberth
Posts: 3
Joined: Friday 12 July, 2013 - 14:11

For Loop in Server

Post by Annaberth »

Good afternoon,

I have a question about bij 4Dscript code in one of my servers. The quality of the products in the server should be checked, however the time until the check should be divided into time steps in order to do a good quality prediction, therefore I introduced the For(..) function. This seems to work, because when I press the button 'OK' it is accepted, but when I start running the model I get the following message: ‘Error at “Do”: Access violation at address 0000000. Read of address 00000000.’

Maybe it is not possible to do a Do(..) function in a For(..) function?
Can anyone help me out?
I past the code I developed beneath.

Many thanks in advance!
Annaberth

Code: Select all

Do(   
   Label([time at manufacturer],i) := Label([end manufacturer time],i) - Label([creation time],i),     
   For( 
       i := 0,
       i < (Round(Label([time at manufacturer],i)/(60*(Data(12,1))))),
       Inc(i),      
       Do(
          Label([Pack permeability],i)  := (Data(13,1)) * exp((Data(14,1))*1000/(Data(15,1))) * ((1/(Data(16,1))) - (1/(Data(1,1)))),           
          Label([O2 influx],i) := Label([Pack permeability],i) * (Data(17,1)) * ((Data(18,1)) - (Data(13,1))) / (Data(19,1)),           
          Label([O2 consumption],i) := (((2 * exp(-12)) * (exp(0.1069) * (Data(1,1))) * (Data(13,1))) / ((7.52555/100)+(Data(13,1)))) * 24 * 0.25,                   
          Label([O2 concentration],i) := (Data(11,1)) + Label([O2 influx],i) - Label([O2 consumption],i),           
          Label([visual quality manufacturer],i) := (Label([initial quality],i))+(-0.53356)*exp((73778.9/8.314)*((1/280.15)-(1/Normal((Data(1,1)),(Data(2,1)))))*exp(-5.90263*(0.0275-( Label([O2 concentration],i))))*(Label([time at manufacturer],i)/86400))     
       )
   )
)
BartC
Posts: 47
Joined: Monday 05 November, 2012 - 17:05

Re: For Loop in Server

Post by BartC »

Hi Annaberth,

Although common in other programming language, "i" can't be used in ED for a loop. In ED "i" is a referral (pointer) to the involved atom, e.g. the product at the entry/exittrigger of a server. At the for loop you are trying to set the pointer "i" to the value 0, which will give you the error you mentioned. Instead, make a local variable and use that one as the counter:

Code: Select all

Do(
  var([valCounter], vbValue),
  
  Label([time at manufacturer],i) := Label([end manufacturer time],i) - Label([creation time],i),     
  For( 
    valCounter := 0,
    valCounter < (Round(Label([time at manufacturer],i)/(60*(Data(12,1))))),
    Inc(valCounter),      
    Do(
      Label([Pack permeability],i)  := (Data(13,1)) * exp((Data(14,1))*1000/(Data(15,1))) * ((1/(Data(16,1))) - (1/(Data(1,1)))),           
      Label([O2 influx],i) := Label([Pack permeability],i) * (Data(17,1)) * ((Data(18,1)) - (Data(13,1))) / (Data(19,1)),           
      Label([O2 consumption],i) := (((2 * exp(-12)) * (exp(0.1069) * (Data(1,1))) * (Data(13,1))) / ((7.52555/100)+(Data(13,1)))) * 24 * 0.25,                   
      Label([O2 concentration],i) := (Data(11,1)) + Label([O2 influx],i) - Label([O2 consumption],i),           
      Label([visual quality manufacturer],i) := (Label([initial quality],i))+(-0.53356)*exp((73778.9/8.314)*((1/280.15)-(1/Normal((Data(1,1)),(Data(2,1)))))*exp(-5.90263*(0.0275-( Label([O2 concentration],i))))*(Label([time at manufacturer],i)/86400))     
    )
  )
)
Regards,

Bart
Annaberth
Posts: 3
Joined: Friday 12 July, 2013 - 14:11

Re: For Loop in Server

Post by Annaberth »

It works!

I have only one question left. The label "O2% package manufacturer new" should be updated every loop. However, this is not done... I get for O2% the first value and not the value of 4 times one hour.

The idea is that a product is for example 4 hours at the manufacturer. This time is divided in tsteps of half an hour (so eight tsteps). In the Do(..) of the For(..) loop the O2% should be updated every tstep.

Should I add this the var "tstep" somewhere in the Do code?
Now, it is not updating...

The Data(11,1) in the line beneath should become "O2% package manufacturer new" after every loop.
Label([Ml O2 package manufacturer],i) := Data(11,1) * 1200,

Code: Select all

Do(   
   var([tstep], vbValue),
   Label([time at manufacturer],i) := Label([end manufacturer time],i) - Label([creation time],i),     
   For(
       timestep := 0,
       timestep < (Round(Label([time at manufacturer],i)/(60*(Data(12,1))))),
       Inc(timestep),     
       Do(
          Label([Pack permeability manufacturer],i)  := (Data(13,1)) * exp((((Data(14,1))*1000)/(Data(15,1))) * ((1/(Data(16,1))) - (1/(Data(1,1))))),           
          Label([O2 influx manufacturer],i) := Label([Pack permeability manufacturer],i) * (Data(17,1)) * ((Data(18,1)) - (Data(11,1))) / (Data(19,1)), 
          Label([Vmax manufacturer],i) := (2*(Power(10,-12)) * (exp(0.1069 * (Data(1,1))))),
          Label([O2 consumption manufacturer],i) := (Label([Vmax manufacturer],i) * (Data(11,1)))/((7.52555/100)+(Data(11,1))),
          Label([Pack consumption manufacturer],i) := Label([O2 consumption manufacturer],i) * 24 * 0.25,   
          Label([Balance O2 manufactuer per hour],i) := ((Label([O2 influx manufacturer],i) - Label([Pack consumption manufacturer],i))/24),
          Label([Ml O2 package manufacturer],i) := Data(11,1) * 1200,
          Label([Ml O2 package manufacturer new],i) :=  Label([Ml O2 package manufacturer],i) + Label([Balance O2 manufactuer per hour],i), 
          Label([O2% package manufacturer new], i) :=  (Label([Ml O2 package manufacturer new],i) *100)/1200
       ) 
   )
)
Annaberth
Posts: 3
Joined: Friday 12 July, 2013 - 14:11

Re: For Loop in Server

Post by Annaberth »

Excuse me! This is the good code:

Code: Select all

Do(   
   var([timestep], vbValue),
   Label([time at manufacturer],i) := Label([end manufacturer time],i) - Label([creation time],i),     
   For(
       timestep := 0,
       timestep < (Round(Label([time at manufacturer],i)/(60*(Data(12,1))))),
       Inc(timestep),     
       Do(
          Label([Pack permeability manufacturer],i)  := (Data(13,1)) * exp((((Data(14,1))*1000)/(Data(15,1))) * ((1/(Data(16,1))) - (1/(Data(1,1))))),           
          Label([O2 influx manufacturer],i) := Label([Pack permeability manufacturer],i) * (Data(17,1)) * ((Data(18,1)) - (Data(11,1))) / (Data(19,1)), 
          Label([Vmax manufacturer],i) := (2*(Power(10,-12)) * (exp(0.1069 * (Data(1,1))))),
          Label([O2 consumption manufacturer],i) := (Label([Vmax manufacturer],i) * (Data(11,1)))/((7.52555/100)+(Data(11,1))),
          Label([Pack consumption manufacturer],i) := Label([O2 consumption manufacturer],i) * 24 * 0.25,   
          Label([Balance O2 manufactuer per hour],i) := ((Label([O2 influx manufacturer],i) - Label([Pack consumption manufacturer],i))/24),
          Label([Ml O2 package manufacturer],i) := Data(11,1) * 1200,
          Label([Ml O2 package manufacturer new],i) :=  Label([Ml O2 package manufacturer],i) + Label([Balance O2 manufactuer per hour],i), 
          Label([O2% package manufacturer new], i) :=  (Label([Ml O2 package manufacturer new],i) *100)/1200
       ) 
   )
)
BartC
Posts: 47
Joined: Monday 05 November, 2012 - 17:05

Re: For Loop in Server

Post by BartC »

Hi Annaberth,

So if I understand you right, you want to set the cell Data(11, 1) to the value of the "Ml O2 package manufacturer new" label?

Since you already declared aliases for your table, the function SetData should exist in your model. By adding the following line beneath "Label([Ml O2 package manufacturer new],i) := Label([Ml O2 package manufacturer],i) + Label([Balance O2 manufactuer per hour],i),", you can update your table:

Code: Select all

SetData(11, 1, Label([Ml O2 package manufacturer new],i))
Regards,

Bart
Post Reply