1 | [[tags:egg]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | == salt |
---|
5 | |
---|
6 | A language and code generation tool for equation-based modeling and simulations. |
---|
7 | |
---|
8 | == Introduction |
---|
9 | |
---|
10 | SALT is an implementation of a domain-specific language for |
---|
11 | equation-based models. This implementation follows the work of Tom |
---|
12 | Short and the Julia Sims.jl library |
---|
13 | [[https://github.com/tshort/Sims.jl]], which is in turn based on |
---|
14 | David Broman's MKL simulator and the work of George Giorgidze and |
---|
15 | Henrik Nilsson in functional hybrid modeling. Following Sims.jl, a |
---|
16 | nodal formulation is used based on David Broman's thesis |
---|
17 | [[http://www.bromans.com/david/publ/thesis-2010-david-broman.pdf]]: |
---|
18 | |
---|
19 | David Broman. Meta-Languages and Semantics for |
---|
20 | Equation-Based Modeling and Simulation. PhD thesis, Thesis No |
---|
21 | 1333. Department of Computer and Information Science, LinkÃ¶ping |
---|
22 | University, Sweden,; 2010. |
---|
23 | |
---|
24 | == Library procedures |
---|
25 | |
---|
26 | <procedure>parse :: DECLARATIONS -> ASTDECLS</procedure> |
---|
27 | Parses equation declarations in the syntax described in the next section, and returns an abstract syntax tree structure. |
---|
28 | |
---|
29 | <procedure>elaborate :: ASTDECLS -> EQUATIION-SET</procedure> |
---|
30 | Performs flattening of the given declarations and returns an equation set. The main steps in flattening are: |
---|
31 | |
---|
32 | * Creation of a name resolution environment (parameters,fields,externals,constants,variables,functions). |
---|
33 | * Replacing of fixed initial values. |
---|
34 | * Flattening models and populating equation, definition, function lists. |
---|
35 | * Populating list of initials. |
---|
36 | * Populating event list from event and structural event definitions. |
---|
37 | * Handles structural events. |
---|
38 | |
---|
39 | <procedure>simcreate :: EQUATION-SET -> SIMRUNTIME</procedure> |
---|
40 | Given an equation set, creates a simulation runtime representation. |
---|
41 | |
---|
42 | <procedure>codegen-ODE :: SIMRUNTIME -> ODE LIST</procedure> |
---|
43 | Given a simulation runtime representation, creates an abstract code representation aimed at ODE solvers. |
---|
44 | |
---|
45 | <procedure>codegen-ODE/ML</procedure> |
---|
46 | Given a simulation runtime representation, creates a code representation aimed at ODE solvers in the Standard ML language. |
---|
47 | |
---|
48 | == Model description language |
---|
49 | |
---|
50 | === Definitions |
---|
51 | |
---|
52 | Definitions serve to define unknowns (state variables), parameters (constants during integration), and units of measurement. |
---|
53 | |
---|
54 | (define millivolt = unit Potential (1e-3 * volt)) |
---|
55 | |
---|
56 | (define Vinit = parameter (dim Potential) -65.0 * millivolt) |
---|
57 | |
---|
58 | (define v = unknown (dim Potential) -65.0 * mV) |
---|
59 | |
---|
60 | === Equations |
---|
61 | |
---|
62 | Equations serve to define differential and algebraic equations. |
---|
63 | |
---|
64 | ((der(u)) = (s - u) / tau) |
---|
65 | ((s) = b * ((v - a) ^ 3)) |
---|
66 | |
---|
67 | === Events |
---|
68 | |
---|
69 | (event (v - Vthreshold) |
---|
70 | ((v := Vreset)) |
---|
71 | ) |
---|
72 | |
---|
73 | == Examples |
---|
74 | |
---|
75 | <enscript highlight="scheme"> |
---|
76 | |
---|
77 | ;; Van der Pol oscillator |
---|
78 | (define vdp |
---|
79 | (parse |
---|
80 | `( |
---|
81 | (define x = unknown -0.25) |
---|
82 | (define y = unknown 1.0) |
---|
83 | ((der(x)) = (1 - y ^ 2) * x - y ) |
---|
84 | ((der(y)) = x) |
---|
85 | ) |
---|
86 | )) |
---|
87 | |
---|
88 | |
---|
89 | ;; Izhikevich Fast Spiking neuron |
---|
90 | (define izhfs |
---|
91 | (parse |
---|
92 | `( |
---|
93 | (define millivolt = unit Potential (1e-3 * volt)) |
---|
94 | |
---|
95 | (define Isyn = parameter (dim Current) 0.0 * nA) |
---|
96 | (define Iext = parameter (dim Current) 400.0 * nA) |
---|
97 | |
---|
98 | (define k = parameter 1.0) |
---|
99 | (define Vinit = parameter (dim Potential) -65.0 * millivolt) |
---|
100 | (define Vpeak = parameter (dim Potential) 25.0 * mV) |
---|
101 | (define Vt = parameter (dim Potential) -55.0 * mV) |
---|
102 | (define Vr = parameter (dim Potential) -40.0 * mV) |
---|
103 | (define Vb = parameter (dim Potential) -55.0 * mV) |
---|
104 | (define Cm = parameter (dim Capacitance) 20.0 * uF) |
---|
105 | |
---|
106 | (define FS_a = parameter 0.2) |
---|
107 | (define FS_b = parameter (dim Current) 0.025 * nA) |
---|
108 | (define FS_c = parameter (dim Potential) -45.0 * mV) |
---|
109 | (define FS_U = parameter (dim Current) FS_b * (Vinit / mV)) |
---|
110 | |
---|
111 | (define v = unknown (dim Potential) -65.0 * mV) |
---|
112 | (define u = unknown (dim Current) FS_U) |
---|
113 | (define s = unknown (dim Current) 0.0 * nA) |
---|
114 | |
---|
115 | ((der(v)) = (((k * (v - Vr) * (v - Vt) / millivolt) + (((- u) + Iext) * megaohm)) / Cm) / megaohm) |
---|
116 | ((der(u)) = (FS_a * (s - u)) / ms) |
---|
117 | ((s) = FS_b * ((v - Vb) / mV) ^ 3) |
---|
118 | |
---|
119 | |
---|
120 | (event (v - Vpeak) |
---|
121 | ((v := FS_c) |
---|
122 | (u := u) |
---|
123 | (s := s) |
---|
124 | ) |
---|
125 | ) |
---|
126 | )) |
---|
127 | ) |
---|
128 | |
---|
129 | |
---|
130 | ;; A routine to generate and compile Standard ML code |
---|
131 | (define (compile-model name model #!key (solver 'rk4b) (compile #f) (dir "tests")) |
---|
132 | (pp model) |
---|
133 | |
---|
134 | (define elab (elaborate model)) |
---|
135 | (print "elaborate is done") |
---|
136 | (pp elab) |
---|
137 | |
---|
138 | (define sim (simcreate elab)) |
---|
139 | (pp sim) |
---|
140 | (pp (codegen-ODE sim solver)) |
---|
141 | (let* ((sml-path (make-pathname dir (string-append (->string name) ".sml"))) |
---|
142 | (mlb-path (make-pathname dir (string-append (->string name) "_run.mlb"))) |
---|
143 | (port (open-output-file sml-path))) |
---|
144 | (codegen-ODE/ML sim out: port solver: solver libs: '(interp)) |
---|
145 | (close-output-port port) |
---|
146 | (if compile |
---|
147 | (run (mlton -mlb-path-var ,(sprintf "'SALT_HOME ~A'" SALT-DIR) |
---|
148 | -mlb-path-var ,(sprintf "'RK_LIB $(SALT_HOME)/sml-lib/rk'") |
---|
149 | -mlb-path-var ,(sprintf "'DYNAMICS_LIB $(SALT_HOME)/sml-lib/dynamics'") |
---|
150 | ,mlb-path)))) |
---|
151 | |
---|
152 | ) |
---|
153 | |
---|
154 | (compile-model 'vdp vdp) |
---|
155 | (compile-model 'izhfs izhfs) |
---|
156 | |
---|
157 | </enscript> |
---|
158 | |
---|
159 | == Version history |
---|
160 | |
---|
161 | * 0.21 : Added step size controller |
---|
162 | * 0.20 : Support for precise event time interpolation and adaptive solvers |
---|
163 | * 0.5 : Support for using assigned quantities in external init equations |
---|
164 | * 0.1 : Initial release |
---|
165 | |
---|
166 | == License |
---|
167 | |
---|
168 | Copyright 2015-2017 Ivan Raikov |
---|
169 | |
---|
170 | This program is free software: you can redistribute it and/or modify |
---|
171 | it under the terms of the GNU General Public License as published by |
---|
172 | the Free Software Foundation, either version 3 of the License, or (at |
---|
173 | your option) any later version. |
---|
174 | |
---|
175 | This program is distributed in the hope that it will be useful, but |
---|
176 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
177 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
178 | General Public License for more details. |
---|
179 | |
---|
180 | A full copy of the GPL license can be found at http://www.gnu.org/licenses/. |
---|