1 | |
---|
2 | structure Sim{{group.name}} = |
---|
3 | struct |
---|
4 | |
---|
5 | fun putStrLn out str = |
---|
6 | (TextIO.output (out, str); |
---|
7 | TextIO.output (out, "\n")) |
---|
8 | |
---|
9 | fun putStr out str = |
---|
10 | (TextIO.output (out, str)) |
---|
11 | |
---|
12 | |
---|
13 | fun showBoolean b = (if b then "1" else "0") |
---|
14 | |
---|
15 | |
---|
16 | fun showReal n = |
---|
17 | let open StringCvt |
---|
18 | open Real |
---|
19 | in |
---|
20 | (if n < 0.0 then "-" else "") ^ (fmt (FIX (SOME 12)) (abs n)) |
---|
21 | end |
---|
22 | |
---|
23 | |
---|
24 | exception Index |
---|
25 | |
---|
26 | fun start (tmax, N, S, D, DQ, Pn, initial_vector, psr_initial_vector, frun, fresponse, ftime, fspikes, finfo, out) = |
---|
27 | |
---|
28 | let |
---|
29 | val I0 = RTensor.new([N,1],0.0) |
---|
30 | |
---|
31 | val sub = Unsafe.Real64Array.sub |
---|
32 | val isub = Unsafe.IntArray.sub |
---|
33 | val update = Unsafe.Real64Array.update |
---|
34 | |
---|
35 | |
---|
36 | fun netrun (DQ,state_vector,psr_state_vector) = |
---|
37 | let |
---|
38 | val t = ftime state_vector |
---|
39 | val prio = {{group.name}}.TEQ.delayPriority t |
---|
40 | |
---|
41 | val (I,DQ') = {{group.name}}.TEQ.nextEvent' |
---|
42 | (fn (prio',_) => prio' <= prio, |
---|
43 | fn (_,v) => SOME v, |
---|
44 | NONE, |
---|
45 | DQ) |
---|
46 | |
---|
47 | val (W',psr_state_vector_i) = fresponse I psr_state_vector |
---|
48 | |
---|
49 | val state_vector_i = frun W' state_vector |
---|
50 | |
---|
51 | val t_i = ftime state_vector_i |
---|
52 | |
---|
53 | val (spike_i, log_spike_i) = fspikes state_vector_i |
---|
54 | |
---|
55 | val _ = finfo (state_vector_i, out) |
---|
56 | |
---|
57 | val W'' = if (List.null spike_i) |
---|
58 | then NONE |
---|
59 | else SOME |
---|
60 | (List.map |
---|
61 | (fn (W) => |
---|
62 | let |
---|
63 | val T = Real64Array.array (N, 0.0) |
---|
64 | in |
---|
65 | (List.app |
---|
66 | (fn (i,nv) => |
---|
67 | let |
---|
68 | val sl = SparseMatrix.slice (W,1,i) |
---|
69 | in |
---|
70 | SparseMatrix.sliceAppi |
---|
71 | (fn (j,x) => update (T,j,Real.+ (Real.* (nv,x), sub(T,j)))) |
---|
72 | sl |
---|
73 | end) |
---|
74 | spike_i; |
---|
75 | RTensor.fromArray ([N,1], T)) |
---|
76 | end) |
---|
77 | S) |
---|
78 | |
---|
79 | val _ = if not (List.null log_spike_i) |
---|
80 | then (putStr out ((showReal t_i) ^ " "); |
---|
81 | TensorFile.intListLineWrite out (List.map (fn (i,n) => (i+1)) log_spike_i)) |
---|
82 | else (putStrLn out ("# " ^ (showReal t_i))) |
---|
83 | |
---|
84 | |
---|
85 | val DQ'' = case W'' of |
---|
86 | SOME W => |
---|
87 | let |
---|
88 | val T = RTensor.new ([N,1],t_i) |
---|
89 | in |
---|
90 | {{group.name}}.TEQ.addEvent ((Real.+ (t_i,D),(T,W)), DQ') |
---|
91 | end |
---|
92 | | NONE => DQ' |
---|
93 | in |
---|
94 | if t_i > tmax |
---|
95 | then (putStrLn out "# All done!"; state_vector_i) |
---|
96 | else netrun (DQ'',state_vector_i,psr_state_vector_i) |
---|
97 | end |
---|
98 | in |
---|
99 | netrun (DQ,initial_vector,psr_initial_vector) |
---|
100 | end |
---|
101 | |
---|
102 | fun timing (action) = |
---|
103 | let |
---|
104 | val timer = Timer.startCPUTimer () |
---|
105 | val result = action () |
---|
106 | val times = Timer.checkCPUTimer timer |
---|
107 | in |
---|
108 | (result, Time.+ (#usr times, #sys times)) |
---|
109 | end |
---|
110 | |
---|
111 | fun main (name,args) = |
---|
112 | let |
---|
113 | open {{group.name}} |
---|
114 | fun finfo (_,out) = () |
---|
115 | in |
---|
116 | (let |
---|
117 | val _ = putStrLn TextIO.stdOut ("starting fprojection...") |
---|
118 | val (S,t) = timing fprojection |
---|
119 | val _ = putStrLn TextIO.stdOut ("fprojection took " ^ (Time.toString t) ^ " s") |
---|
120 | val out = TextIO.openOut (label ^ ".dat") |
---|
121 | in |
---|
122 | List.app (fn (s) => putStrLn out ("# " ^ s)) |
---|
123 | ([ |
---|
124 | label, |
---|
125 | {% for p in dict (group.properties) %} |
---|
126 | ("{{p.name}} = {{p.value.exprML}}"){% if not loop.last %},{% endif %} |
---|
127 | {% endfor %} |
---|
128 | ]); |
---|
129 | let |
---|
130 | val _ = putStrLn TextIO.stdOut ("starting simulation...") |
---|
131 | val (_,t) = timing (fn () => start (1200.0, N, S, D, DQ, Pn, initial, psr_initial, frun, fresponse, ftime, fspikes, finfo, out)) |
---|
132 | in |
---|
133 | putStrLn TextIO.stdOut ("simulation took " ^ (Time.toString t) ^ " s") |
---|
134 | end; |
---|
135 | |
---|
136 | TextIO.flushOut (out); |
---|
137 | TextIO.closeOut (out) |
---|
138 | end) |
---|
139 | end |
---|
140 | |
---|
141 | |
---|
142 | end |
---|
143 | |
---|
144 | val _ = let val name = CommandLine.name() |
---|
145 | val args = CommandLine.arguments() |
---|
146 | val env = Posix.ProcEnv.environ() |
---|
147 | in |
---|
148 | Sim{{group.name}}.main (name, args) |
---|
149 | end |
---|