﻿ MODELS > Examples > Models Residents

# Models Resident Functions and Variables

1. Resident functions

This example illustrates all built-in functions in MODELS

MODEL numfun

VAR a1, a2

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

write("*** Begin results of model 'numfun' ***")

a1:=1; a2:=-2

write('abs(2)           =2        :', abs(a2))

write('sqrt(2)          =1.414... :', sqrt(2*a1))

write('exp(1)           =e        :', exp(1))

write('ln(e)            =1        :', ln(exp(1)))

write('log10(100)       =2        :', log10(100))

write('log2(4)          =2        :', log2(4))

write('recip(-2)        =-0.5     :', recip(a2))

write('factorial(5)     =120      :', factorial(5))

write('trunc(5.5)       =5        :', trunc(5.5))

write('fract(5.5)       =0.5      :', fract(5.5))

write('round(5.5)       =6        :', round(5.5))

write('sign(-1)         =-1       :', sign(a2+3-2))

write('deg(pi)          =180      :', deg(pi))

write('random                     :', random())

write('cos(-pi/4)       =0.707... :', cos(-pi/4))

write('tan(pi/4)        =1        :', tan(pi/4))

write('asin(sin(pi/2))  =pi/2     :', asin(sin(pi/2)))

write('acos(cos(pi/2))  =pi/2     :', acos(cos(pi/2)))

write('atan(0.5)        =0.4636...:', atan(0.5))

write('sinh(pi/2)       =2.3012...:', sinh(pi/2))

write('cosh(pi/2)       =2.5091...:', cosh(pi/2))

write('tanh(pi/2)       =0.9171...:', tanh(pi/2))

--write(' :', asinh(xpr1))

--write(' :', acosh(xpr1))

--write(' :', atanh(xpr1))

write('atan2(-1,1)      =-pi/4    :', atan2(-1,1))

write('binom(5,3)       =10       :', binom(5,3))

write('permut(5,3)      =60       :', permut(5,3))

write('min(1,-2,-2,-8)  =-8       :', min(a1,a2,a2,-8))

write('max(9,8,7)       =9        :', max(9,8,7))

write('norm(2,2,sqrt(8))=4        :', norm(2,-2,sqrt(8)))

write('bool(-4)         =0        :', bool(-4))

write('1 :', and(1,1,1))

write('0 :', and(-1,1,1))

write('0 :', and(1,-2,1))

write('0 :', and(1,1,-3))

write('0 :', and(-4,-5,1))

write('0 :', and(-4,1,-3))

write('0 :', and(1,-2,-1))

write('0 :', and(0,0,0))

write('1 :', or(1,1,1))

write('1 :', or(-1,1,1))

write('1 :', or(1,-2,1))

write('1 :', or(1,1,-3))

write('1 :', or(-4,-5,1))

write('1 :', or(-4,1,-3))

write('1 :', or(1,-2,-1))

write('0 :', or(0,0,0))

write('0 :', nand(1,1,1))

write('1 :', nand(-1,1,1))

write('1 :', nand(1,-2,1))

write('1 :', nand(1,1,-3))

write('1 :', nand(-4,-5,1))

write('1 :', nand(-4,1,-3))

write('1 :', nand(1,-2,-1))

write('1 :', nand(0,0,0))

write('0 :', nor(1,1,1))

write('0 :', nor(-1,1,1))

write('0 :', nor(1,-2,1))

write('0 :', nor(1,1,-3))

write('0 :', nor(-4,-5,1))

write('0 :', nor(-4,1,-3))

write('0 :', nor(1,-2,-1))

write('1 :', nor(0,0,0))

write('0 :', xor(1,1,1))

write('0 :', xor(-1,1,1))

write('0 :', xor(1,-2,1))

write('0 :', xor(1,1,-3))

write('1 :', xor(-4,-5,1))

write('1 :', xor(-4,1,-3))

write('1 :', xor(1,-2,-1))

write('0 :', xor(0,0,0))

write("*** End results of model 'numfun' ***")

ENDIF

ENDEXEC

ENDMODEL

2. Resident time variables

This example illustrates the value of the simulation variables in MODELS.

MODEL simvar

VAR x -- a model expects at least one variable

EXEC

write(' ')

write("*** In model 'simvar' at t=", t, " ***")

write("'prevtime'= time of previous execution of this model    : ", prevtime)

write("'simtime' = present time inside this model              : ", simtime)

write("'endtime' = present time outside this model             : ", endtime)

write("'fullstep'= interval since last call to this model      : ", fullstep)

write("'timestep'= present size of time step in this model     : ", timestep)

write("'minstep' = limit on minimum size of time step          : ", minstep)

write("'maxstep' = limit on maximum size of time step          : ", maxstep)

write("'starttime'= time of beginning of simulation     : ", starttime)

write("'stoptime' = time of end of simulation           : ", stoptime)

write("'startstep'= outermost time step of simulation   : ", startstep)

ENDEXEC

ENDMODEL

2. Resident time functions

The last example illustrates the time-dependent functions in MODELS.

MODEL simul_functions

VAR    y, y1, y2, y3, y4, y5, t1, t2, t3, t4, t5

y11, y22, y33, y55, t11, t22, t33, t55

HISTORY y {dflt:t*t +10*t +16} -- default history of y

EXEC

y:=t*t +10*t +16

IF t=timestep THEN

t1:=t-timestep/2

t2:=t1-timestep

t3:=t2-timestep

t4:=t3-timestep

t5:=t1+timestep

t11:=prevtime

t22:=t11-timestep

t33:=t22-timestep

t55:=t+timestep

y4:=histval(y, t4) -- always uses history function

y3:=histval(y, t3)

y2:=histval(y, t2)

y1:=histval(y, t1)

y5:=histval(y, t5)

y11:=prevval(y)

y22:=histval(y, t22)

y33:=histval(y, t33)

y55:=histval(y, t55)

write("*** In model simul_functions at t= ", t, ' ***')

write(" ")

write("y(t) = t*t +10*t +16")

write("   y(", t4, ")=", y4)

write("   y(", t33, "  )=", y33)

write("   y(", t3, ")=", y3)

write("   y(", t22, "  )=", y22)

write("   y(", t2, ")=", y2)

write("   y(", t11, "  )=", y11)

write("   y(", t1, ")=", y1)

write("   y(", t , "  )=", y)

write("   y(", t5, ")=", y5)

write("   y(", t55, "  )=", y55)

write(" ")

write("backval(y, ", t4, ",  0)   : ", backval(y, t4,  0)) -- no interpol.

write("backval(y, ", t3, ",  0)   : ", backval(y, t3,  0)) -- no interpol.

write("backval(y, ", t22, ", 0)   : ", backval(y, t22, 0)) -- no interpol.

write("backval(y, ", t2, ",  0)   : ", backval(y, t2,  0)) -- no interpol.

write("backval(y, ", t11, ", 0)   : ", backval(y, t11, 0)) -- no interpol.

write("backval(y, ", t1, ",  0)   : ", backval(y, t1,  0)) -- no interpol.

write("backval(y, ", t,  ",  0)   : ", backval(y, t ,  0)) -- no interpol.

write("backval(y, ", t5, ",  0)   : ", backval(y, t5,  0)) -- no interpol.

write(" ")

write("backval(y, ", t4,  ")      : ", backval(y, t4 ))     -- linear     "  "

write("backval(y, ", t3,  ")      : ", backval(y, t3 ))     -- linear     "  "

write("backval(y, ", t22, ")      : ", backval(y, t22))     -- linear     "  "

write("backval(y, ", t2,  ")      : ", backval(y, t2 ))     -- linear     "  "

write("backval(y, ", t11, ")      : ", backval(y, t11))     -- linear     "  "

write("backval(y, ", t1,  ")      : ", backval(y, t1 ))     -- linear     "  "

write("backval(y, ", t ,  ")      : ", backval(y, t  ))     -- linear     "  "

write("backval(y, ", t5,  ")      : ", backval(y, t5 ))     -- linear     "  "

write(" ")

write("backval(y, ", t4,  ", 2)   : ", backval(y, t4,  2)) -- quadratic  "  "

write("backval(y, ", t3,  ", 2)   : ", backval(y, t3,  2)) -- quadratic  "  "

write("backval(y, ", t22, ", 2)   : ", backval(y, t22, 2)) -- quadratic  "  "

write("backval(y, ", t2,  ", 2)   : ", backval(y, t2,  2)) -- quadratic  "  "

write("backval(y, ", t11, ", 2)   : ", backval(y, t11, 2)) -- quadratic  "  "

write("backval(y, ", t1,  ", 2)   : ", backval(y, t1,  2)) -- quadratic  "  "

write("backval(y, ", t ,  ", 2)   : ", backval(y, t ,  2)) -- quadratic  "  "

write("backval(y, ", t5,  ", 2)   : ", backval(y, t5,  2)) -- quadratic  "  "

write(" ")

write("predval(y, ", t1,  ", 0)   : ", predval(y, t1,  0))

write("predval(y, ", t ,  ", 0)   : ", predval(y, t ,  0))

write("predval(y, ", t5,  ", 0)   : ", predval(y, t5,  0))

write("predval(y, ", t55, ", 0)   : ", predval(y, t55, 0))

write(" ")

write("predval(y, ", t1,  ")      : ", predval(y, t1) )

write("predval(y, ", t ,  ")      : ", predval(y, t ) )

write("predval(y, ", t5,  ")      : ", predval(y, t5) )

write("predval(y, ", t55, ")      : ", predval(y, t55))

write(" ")

write("predval(y, ", t1,  ", 2)   : ", predval(y, t1,  2))

write("predval(y, ", t ,  ", 2)   : ", predval(y, t ,  2))

write("predval(y, ", t5,  ", 2)   : ", predval(y, t5,  2))

write("predval(y, ", t55, ", 2)   : ", predval(y, t55, 2))

write(" ")

write("backtime(y, ", y4,  ", 0)   : ", backtime(y, y4,  0))

write("backtime(y, ", y33, ", 0)   : ", backtime(y, y33, 0))

write("backtime(y, ", y3,  ", 0)   : ", backtime(y, y3,  0))

write("backtime(y, ", y22, ", 0)   : ", backtime(y, y22, 0))

write("backtime(y, ", y2,  ", 0)   : ", backtime(y, y2,  0))

write("backtime(y, ", y11, ", 0)   : ", backtime(y, y11, 0))

write("backtime(y, ", y1,  ", 0)   : ", backtime(y, y1,  0))

write("backtime(y, ", y ,  ", 0)   : ", backtime(y, y ,  0))

write("backtime(y, ", y5,  ", 0)   : ", backtime(y, y5,  0))

write(" ")

write("backtime(y, ", y4,  ")      : ", backtime(y, y4) )

write("backtime(y, ", y33, ")      : ", backtime(y, y33))

write("backtime(y, ", y3,  ")      : ", backtime(y, y3) )

write("backtime(y, ", y22, ")      : ", backtime(y, y22))

write("backtime(y, ", y2,  ")      : ", backtime(y, y2) )

write("backtime(y, ", y11, ")      : ", backtime(y, y11))

write("backtime(y, ", y1,  ")      : ", backtime(y, y1) )

write("backtime(y, ", y ,  ")      : ", backtime(y, y ) )

write("backtime(y, ", y5,  ")      : ", backtime(y, y5) )

write(" ")

write("backtime(y, ", y4,  ", 2)   : ", backtime(y, y4,  2))

write("backtime(y, ", y33, ", 2)   : ", backtime(y, y33, 2))

write("backtime(y, ", y3,  ", 2)   : ", backtime(y, y3,  2))

write("backtime(y, ", y22, ", 2)   : ", backtime(y, y22, 2))

write("backtime(y, ", y2,  ", 2)   : ", backtime(y, y2,  2))

write("backtime(y, ", y11, ", 2)   : ", backtime(y, y11, 2))

write("backtime(y, ", y1,  ", 2)   : ", backtime(y, y1,  2))

write("backtime(y, ", y ,  ", 2)   : ", backtime(y, y ,  2))

write("backtime(y, ", y5,  ", 2)   : ", backtime(y, y5,  2))

write(" ")

write("predtime(y, ", y1,  ", 0)   : ", predtime(y, y1,  0))

write("predtime(y, ", y ,  ", 0)   : ", predtime(y, y ,  0))

write("predtime(y, ", y5,  ", 0)   : ", predtime(y, y5,  0))

write("predtime(y, ", y55, ", 0)   : ", predtime(y, y55, 0))

write(" ")

write("predtime(y, ", y1,  ")      : ", predtime(y, y1) )

write("predtime(y, ", y ,  ")      : ", predtime(y, y ) )

write("predtime(y, ", y5,  ")      : ", predtime(y, y5) )

write("predtime(y, ", y55, ")      : ", predtime(y, y55))

write(" ")

write("predtime(y, ", y1,  ", 2)   : ", predtime(y, y1,  2))

write("predtime(y, ", y ,  ", 2)   : ", predtime(y, y ,  2))

write("predtime(y, ", y5,  ", 2)   : ", predtime(y, y5,  2))

write("predtime(y, ", y55, ", 2)   : ", predtime(y, y55, 2))

ENDIF

ENDEXEC

ENDMODEL