------------------------------------------
------------------------------------------
----- A System Matrix Implementation -----
----- using Sparse Matrix Methods,   -----
----- and Energy Flow Processing     -----
-----                                -----
----- By John Ringland               -----
----- 2004/11/21                     -----
------------------------------------------
------------------------------------------

-- this file uses sparse matrix energy flow methods
-- to implement an ergodic pure metric sysWrapped SMN framework

--with trace
include ergodicSysMSMN.e

---------------------------------
---------------------------------
-----  Test Model of a Two  -----
-----  Tiered NAND system   -----
---------------------------------
---------------------------------

sequence st, sm, sv, model, cUpdate, result
object is

function NAND(sequence in)
    -- in is a pair of cData bools; either zero or one
    return not (in[1]*in[2])
end function

integer NAND_id
NAND_id = routine_id("NAND")
 
-- input selector
is = {1,1}

st = {NAND_id,NAND_id,NAND_id}

-- constructing the Metric System Matrix element by element
sm = sparse_SM(7,7)
sm = set_SM(sm, {1,2}, is)
sm = set_SM(sm, {1,3}, is)
sm = set_SM(sm, {2,4}, is)
sm = set_SM(sm, {2,5}, is)
sm = set_SM(sm, {3,6}, is)
sm = set_SM(sm, {3,7}, is)

-- constructing the final State Vector
sv = {0,0,0,1,1,1,0} -- a=1, b=1, c=1, d=0

-- flagging the initial inputs for processing
cUpdate = {4,5,6,7}

-- combining all of the above into a single system model
model = {st,sm,sv,cUpdate}

puts(1,"\n\nTwo Tiered NAND System\n\n")
puts(1,"initial model\n")
? model

puts(1,"first iteration\n")
result = step_Model(model, 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nsecond iteration\n")
result = step_Model(result[model_], 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nthird iteration\n")
result = step_Model(result[model_], 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nall iterations at once\n")
result = step_Model(model, 100)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nthen change b=0\n")
result[model_][SV_][5] = 0
result[model_][CU_] = {5}
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nfirst iteration\n")
result = step_Model(result[model_], 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nsecond iteration\n")
result = step_Model(result[model_], 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]
puts(1,"\nthird iteration\n")
result = step_Model(result[model_], 1)
puts(1,"State Vector:    ")
? result[model_][SV_]
puts(1,"cUpdate:         ")
? result[model_][CU_]


constant ITERATIONS = 10000
atom t0, loop_overhead, t

t0 = time()
for i = 1 to ITERATIONS do
    -- time an empty loop
end for
loop_overhead = time() - t0
    
t0 = time()
for i = 1 to ITERATIONS do
    result = step_Model(model, 3)
end for
t = (time() - t0 - loop_overhead)/ITERATIONS
printf(1,"\n\nAveraged over %d cycles the three iteration process took %f seconds,\n",{ITERATIONS,t} )
printf(1, "that is %f iterations per second.\n", 1/t)

www.Anandavala.info