﻿ MODELS > Examples > Models History & Init

# Models History & Init

1. History

This example illustrates various ways to assign history values to variables.This can be done in three ways:

in HISTORY x {dflt:2.3+t}

in INIT histdef(x):=2.3+t

in USE HISTORY x:=2.3+t . This is not supported by ATPDraw!!

The function histval(x, t) can be used to calculate the history value of x at time t.

BEGIN NEW DATA CASE                                              { HISTORY.DAT }

C deltat    tmax    xopt    copt  epsiln  tolmat  tstart

1.      1.

C  print  points  connec     s-s  minmax    vary   again    plot

1       1       0       0       0       0       0       0

C ==============================================================================

MODELS

MODEL history_assign     -- illustrates assignment and use of history in MODELS

VAR  y[1..5] -- declare variables y[1], y[2], ... y[5]

HISTORY y[1..5] {flt: -66} -- default history of y[..]

-- used if HISTORY is not passed in USE statement.

INIT -- to define values and history functions, privately inside the model

IF histval(y[2],0)=-66 THEN histdef(y[2]):=10*t ENDIF

-- the above line uses histval() to access the history function of y[2]

-- and assigns it a different function if some condition is true

histdef(y[5]) := histdef(y[4]) +1

-- the above line defines the history of y[5] as dependent of

--  the history function already defined for y[4]

ENDINIT

EXEC

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

FOR k:=-10 to 0 DO

wriite('Values of y[1..5] at t=', k, ' : ',

histval(y[1],k), ', ', histval(y[2],k), ', ', histval(y[3],k), ', ',

histval(y[4],k), ', ', histval(y[5],k)

)

ENDFOR

ENDIF

ENDEXEC

ENDMODEL

--Not supported in ATPDraw:

USE history_assign AS history_assign

HISTORY y[3..4]:=[t/2, t*t +3*t -4]  -- assign history functions to y[3], y[4]

ENDUSE

ENDMODELS

C ==============================================================================

C BRANCH CARDS

SRCE                       10.

BLANK CARD ENDING BRANCHES

BLANK CARD ENDING SWITCHES

14SRCE           1.0       50.

BLANK CARD ENDING SOURCES

SRCE

BLANK CARD ENDING NODE VOLTAGE REQUEST

BLANK CARD ENDING PLOT

BEGIN NEW DATA CASE

BLANK

2. Init

This example illustrates how to use the histval function in INIT to override the HISTORY definitions.

MODEL inival -- illustrates the 3 priority levels for initialization in MODELS:

-- 1) the default history in the model itself,

--     which can be modified by 2) and 3)

-- 2) the history assigned in the USE statement using that model,

--     which can be modified by 3)

-- 3) the history assigned in the INIT section of the model

VAR a, b, ref

HISTORY a {dflt:0}, b {dflt:0} -- declare that a and b require history functions

-- and assign default history functions

INIT

b:=100                       -- overwrite value of b prior to first execution

write('a(t) at t=', t-2*timestep, ' : ', histval(a, t-2*timestep) )

write('a(t) at t=',   t-timestep, ' : ', histval(a, t-timestep) )

write('a(t) at t=',   t, ' : ', histval(a, t) )

write(' ')

write('b(t) at t=', t-2*timestep, ' : ', histval(b, t-2*timestep) )

write('b(t) at t=',   t-timestep, ' : ', histval(b, t-timestep) )

write('b(t) at t=',   t, ' : ', histval(b, t) )

ENDINIT

EXEC

diffeq(1|D0 +1|D1)|a :=b

ref := 100 - 200*exp(-t)

ENDEXEC

ENDMODEL