-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt.scm
2448 lines (2090 loc) · 90.8 KB
/
salt.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;
;; Hybrid dynamical systems modeling.
;;
;; Copyright 2015-2018 Ivan Raikov.
;;
;; This implementation follows the work of Tom Short and his Julia
;; Sims.jl library, which is in turn based on David Broman's MKL
;; simulator and the work of George Giorgidze and Henrik Nilsson in
;; functional hybrid modeling.
;;
;; Following Sims.jl, a nodal formulation is used based on David
;; Broman's thesis:
;;
;; David Broman. Meta-Languages and Semantics for Equation-Based
;; Modeling and Simulation. PhD thesis, Thesis No 1333. Department of
;; Computer and Information Science, Linköping University, Sweden,
;; 2010.
;; http://www.bromans.com/david/publ/thesis-2010-david-broman.pdf
;;
;; The type elaboration and environment management routines are based
;; on ideas and code from the Scheme dynamic type inferencer by Andrew
;; Wright.
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; A full copy of the GPL license can be found at
;; <http://www.gnu.org/licenses/>.
;;
(module salt
(
parse elaborate simcreate codegen-ODE codegen-ODE/ML codegen-ODE/C
math-constant-env math-binop-env math-unop-env
model-quantities model-units
verbose add-trace clear-trace
empty-env env-lookup extend-env-with-binding env->list
gen-binding binding-key binding-value
astdecls? make-astdecls astdecls-decls
equation-set-model
equation-set-definitions
equation-set-discrete-definitions
equation-set-parameters
equation-set-fields
equation-set-externals
equation-set-externalevs
equation-set-equations
equation-set-initial
equation-set-conditions
equation-set-pos-responses
equation-set-neg-responses
equation-set-functions
equation-set-nodemap
equation-set-regimemap
equation-set-dimenv
constant parameter
codegen-primop codegen-expr codegen-const-expr codegen-ODE
name/ML binding->ML stmt->ML value->ML codegen-ODE/ML
model-time
)
(import scheme (chicken base)
matchable datatype lalr-driver mathh unitconv with-units fmt fmt-c mathh-consts
(only srfi-1 first last zip fold fold-right filter filter-map list-tabulate concatenate every delete-duplicates drop-right lset-union)
(only srfi-4 list->s32vector)
(only srfi-13 string-null? string-concatenate string-map string<)
(only (chicken string) ->string conc)
(only (chicken sort) sort topological-sort)
(only (chicken format) fprintf sprintf)
(only (chicken pretty-print) pp)
(only (chicken port) with-output-to-port port-position port-name)
(only (chicken condition) print-error-message)
(chicken memory representation)
)
(include "units.scm")
(include "parser.scm")
(include "env.scm")
(include "codegen.scm")
(define (s+ . rst) (string-concatenate (map ->string rst)))
(define (salt:warning x . rest)
(let loop ((port (open-output-string)) (objs (cons x rest)))
(if (null? objs)
(begin
(newline port)
(print-error-message (get-output-string port)
(current-error-port) "salt warning"))
(begin (display (car objs) port)
(display " " port)
(loop port (cdr objs))))))
(define (salt:error x . rest)
(let ((port (open-output-string)))
(if (port? x)
(begin
(display "[" port)
(display (port-name x) port)
(display "] " port)))
(let loop ((objs (if (port? x) rest (cons x rest))))
(if (null? objs)
(begin
(newline port)
(error 'salt (get-output-string port)))
(let ((obj (car objs)))
(if (procedure? obj)
(with-output-to-port port obj)
(begin
(display obj port)
(display " " port)))
(loop (cdr objs)))))))
(define verbose (make-parameter 0))
(define trace-locations (make-parameter '()))
(define (add-trace loc)
(if (not (member loc (trace-locations)))
(trace-locations (cons loc (trace-locations)))))
(define (clear-trace loc)
(trace-locations '()))
(define (d loc fstr . args)
(let ([port (current-error-port)])
(if (positive? (verbose))
(begin
(fprintf port "~A: " loc)
(apply fprintf port fstr args)
(flush-output port) ) )))
(define (trace loc fstr . args)
(let ([port (current-error-port)])
(if (member loc (trace-locations))
(begin
(fprintf port "~A: " loc)
(apply fprintf port fstr args)
(flush-output port) ) )))
(define syntactic-keywords
'(function if cond and or let else))
(define-record-type variable
(make-variable name label value history dim)
variable?
(name variable-name)
(label variable-label)
(value variable-value)
(history variable-history)
(dim variable-dim)
)
(define-record-printer (variable x out)
(fprintf out "#(variable ~S label=~S value=~S history=~S dim=~A)"
(variable-name x)
(variable-label x)
(variable-value x)
(variable-history x)
(variable-dim x)
))
(define (unknown value label dim)
(make-variable (gensym 'u) label value #t dim))
(define-record-type free-variable
(make-free-variable name)
free-variable?
(name free-variable-name)
)
(define-record-printer (free-variable x out)
(fprintf out "#(free ~S)"
(free-variable-name x)
))
(define-record-type derivative-variable
(make-derivative-variable parent)
derivative-variable?
(parent derivative-variable-parent)
)
(define-record-printer (derivative-variable x out)
(fprintf out "#(deriv ~S)"
(derivative-variable-parent x)
))
(define-record-type discrete-variable
(make-discrete-variable name label value dim)
discrete-variable?
(name discrete-variable-name)
(label discrete-variable-label)
(value discrete-variable-value)
(dim discrete-variable-dim)
)
(define (discrete value label dim)
(make-discrete-variable (gensym 'd) label value dim))
(define-record-printer (discrete-variable x out)
(fprintf out "#(discrete ~S (~a) [~A] = ~A)"
(discrete-variable-name x)
(discrete-variable-label x)
(discrete-variable-dim x)
(discrete-variable-value x)
))
(define-record-type regime-variable
(make-regime-variable name)
regime-variable?
(name regime-variable-name)
)
(define-record-printer (regime-variable x out)
(fprintf out "#(regime-variable ~S)"
(regime-variable-name x)
))
(define-record-type reduce-variable
(make-reduce-variable op parent)
reduce-variable?
(op reduce-variable-op)
(parent reduce-variable-parent)
)
(define-record-printer (reduce-variable x out)
(fprintf out "#(reduce ~S ~S)"
(reduce-variable-op x)
(reduce-variable-parent x)
))
(define-record-type parameter
(make-system-parameter name label value dim)
parameter?
(name parameter-name)
(label parameter-label)
(value parameter-value)
(dim parameter-dim)
)
(define-record-printer (parameter x out)
(fprintf out "#(parameter ~S (~A) [~A] = ~A)"
(parameter-name x)
(parameter-dim x)
(parameter-label x)
(parameter-value x)
))
(define-record-type field
(make-field name label value dim)
field?
(name field-name)
(label field-label)
(value field-value)
(dim field-dim)
)
(define-record-printer (field x out)
(fprintf out "#(field ~S (~A) [~A] = ~A)"
(field-name x)
(field-dim x)
(field-label x)
(field-value x)
))
(define-record-type declared-constant
(make-declared-constant label content)
declared-constant?
(label declared-constant-label)
(content declared-constant-content)
)
(define-record-type constant
(make-constant type value unit)
constant?
(type constant-type)
(value constant-value)
(unit constant-unit)
)
(define-record-printer (constant x out)
(if (constant-unit x)
(fprintf out "#(constant ~S = ~A ~A)"
(constant-type x)
(constant-value x)
(constant-unit x)
)
(fprintf out "#(constant ~S = ~A)"
(constant-type x)
(constant-value x)
)))
(define-record-type external
(make-external name label initial dim order)
external?
(name external-name)
(label external-label)
(initial external-initial)
(dim external-dim)
(order external-order)
)
(define-record-printer (external x out)
(fprintf out "#(external ~S (~A) [~A] = ~A order: ~A)"
(external-name x)
(external-dim x)
(external-label x)
(external-initial x)
(or (external-order x) "none")
))
(define-record-type externalev
(make-externalev name label initial dim order link)
externalev?
(name externalev-name)
(label externalev-label)
(initial externalev-initial)
(dim externalev-dim)
(order externalev-order)
(link externalev-link)
)
(define-record-printer (externalev x out)
(fprintf out "#(externalev ~S (~A) [~A] = ~A order: ~A link: ~A)"
(externalev-name x)
(externalev-label x)
(externalev-dim x)
(externalev-initial x)
(externalev-order x)
(or (externalev-link x) "none")
))
(define-record-type left-var
(make-left-var u )
left-var?
(u left-var-u)
)
(define-record-printer (left-var x out)
(fprintf out "#(left-var ~S)"
(left-var-u x)
))
(define-record-type ref-var
(make-make-ref-var u idx )
ref-var?
(u ref-var-u)
(idx ref-var-idx)
)
(define-record-printer (ref-var x out)
(fprintf out "#(ref-var ~S [~A])"
(ref-var-u x)
(ref-var-idx x)
))
(define-record-type pair-arg
(make-pair-arg fst snd )
pair-arg?
(fst pair-arg-fst)
(snd pair-arg-snd)
)
(define-record-type null-arg
(make-null-arg)
null-arg?
)
(define-record-printer (pair-arg x out)
(fprintf out "(~A . ~A)"
(pair-arg-fst x)
(pair-arg-snd x)
))
(define-record-type pair-formal
(make-pair-formal fst snd )
pair-formal?
(fst pair-formal-fst)
(snd pair-formal-snd)
)
(define-record-printer (pair-formal x out)
(fprintf out "<~A . ~A>"
(pair-formal-fst x)
(pair-formal-snd x)
))
(define-record-type null-formal
(make-null-formal )
null-formal?
)
(define-record-type var-def
(make-var-def sym )
var-def?
(sym var-def-sym)
)
(define-record-printer (var-def x out)
(fprintf out "#(var-def ~S)"
(var-def-sym x)
))
(define-record-type function
(make-function name formals body)
function?
(name function-name)
(formals function-formals)
(body function-body))
(define-record-printer (function x out)
(fprintf out "#(function ~S (~S) = ~S)"
(function-name x)
(function-formals x)
(function-body x)
))
(define-record-type equation
(make-eq pattern rhs)
equation?
(pattern equation-pattern)
(rhs equation-rhs))
(define-record-printer (equation x out)
(fprintf out "#(equation ~S = ~A)"
(equation-pattern x)
(equation-rhs x)
))
(define-record-type initial-equation
(make-init variable expr)
initial-equation?
(variable equation-variable)
(expr equation-expr))
(define-record-type event
(make-event name condition pos neg)
event?
(name event-name)
(condition event-condition)
(pos event-pos)
(neg event-neg))
(define-record-printer (event x out)
(fprintf out "#")
(pp
`(event
(name=,(event-name x))
(condition=,(event-condition x))
(pos=,(event-pos x))
(neg=,(event-neg x))
)
out))
(define-record-type evcondition
(make-evcondition name expr)
evcondition?
(name evcondition-name)
(expr evcondition-expr))
(define-record-printer (evcondition x out)
(fprintf out "#")
(pp
`(evcondition
(name=,(evcondition-name x))
(expr=,(evcondition-expr x))
)
out))
(define-record-type evresponse
(make-evresponse name expr)
evresponse?
(name evresponse-name)
(expr evresponse-expr))
(define-record-printer (evresponse x out)
(fprintf out "#")
(pp
`(evresponse
(name=,(evresponse-name x))
(expr=,(evresponse-expr x))
)
out))
(define-record-type extevlink
(make-extevlink src dst)
extevlink?
(src extevlink-src)
(dst extevlink-dst))
(define-record-printer (extevlink x out)
(fprintf out "#")
(pp
`(extevlink
(src=,(extevlink-src x))
(dst=,(extevlink-dst x))
)
out))
(define-record-type transition
(make-transition event target condition response)
transition?
(event transition-event)
(target transition-target)
(condition transition-condition)
(response transition-response)
)
(define-record-type structural-event
(make-structural-event name label regime transition )
structural-event?
(name structural-event-name)
(label structural-event-label)
(regime structural-event-regime)
(transition structural-event-transition)
)
(define-record-type astdecls
(make-astdecls decls)
astdecls?
(decls astdecls-decls)
)
(define (expr-value x)
(cond ((variable? x) (variable-value x))
((ref-var? x) (vector-ref (ref-var-u x) (ref-var-idx x)))
((list? x) (eval `(,(car x) . ,(map expr-value x))))
(else x)))
(define (variable-identity x)
(cond ((variable? x)
(variable-name x))
((derivative-variable? x)
(variable-identity (derivative-variable-parent x)))
((discrete-variable? x)
(discrete-variable-name x))
((regime-variable? x)
(regime-variable-name x))
((reduce-variable? x)
(variable-identity (reduce-variable-parent x)))
((external? x)
(external-name x))
(else (error 'variable-identity "invalid argument" x))
))
(define (reinit e x y)
(cond ((left-var? x) `(signal.reinit ,e ,x ,y))
((variable? x) (reinit e (make-left-var x) y))
((ref-var? x) (reinit e (make-left-var x) y))
((derivative-variable? x)
(reinit e (make-left-var (derivative-variable-parent x)) y))
((discrete-variable? x)
(reinit e (make-left-var x) y))
((regime-variable? x)
(reinit e (make-left-var x) y))
((reduce-variable? x)
(reinit e (make-left-var (reduce-variable-parent x)) y))
(else (error 'reinit "invaliusing using using using d argument to reinit" x))
))
(define math-constant-env
(fold (lambda (k v env)
(let ((binding (gen-binding k v)))
(extend-env-with-binding env binding)))
empty-env
`(UNITZERO
E 1/E E^2 E^PI/4 LOG2E LOG10E LN2 LN3 LNPI LN10 1/LN2 1/LN10 PI PI/2
PI/4 1/PI 2/PI 2/SQRTPI SQRTPI PI^2 DEGREE SQRT2 1/SQRT2 SQRT3 SQRT5
SQRT10 CUBERT2 CUBERT3 4THRT2 GAMMA1/2 GAMMA1/3 GAMMA2/3 PHI LNPHI
1/LNPHI EULER E^EULER SIN1 COS1 ZETA3)
(cons (make-constant 'number 0.0 'unitbottom)
(map (lambda (x) (make-constant 'number x unitless))
(list e 1/e e^2 e^pi/4 log2e log10e ln2 ln3 lnpi ln10 1/ln2 1/ln10 pi pi/2
pi/4 1/pi 2/pi 2/sqrtpi sqrtpi pi^2 degree sqrt2 1/sqrt2 sqrt3 sqrt5
sqrt10 cubert2 cubert3 4thrt2 gamma1/2 gamma1/3 gamma2/3 phi lnphi
1/lnphi euler e^euler sin1 cos1 zeta3)))
))
(define math-binop-env
(let ((function-names `(+ - * / pow max min == < <= > >= random.unifrange))
(op-names `(signal.add
signal.sub signal.mul signal.div signal.pow signal.max signal.min signal.eqnum
signal.lt signal.lte signal.gt signal.gte
random.unifrange)))
(fold (lambda (k v env)
(let ((binding (gen-binding k v)))
(extend-env-with-binding env binding)))
empty-env
function-names
(map (lambda (f fn)
(make-function
(make-free-variable f)
(make-pair-formal (make-var-def 'a) (make-pair-formal (make-var-def 'b) (make-null-formal)))
`(signal.call ,fn ,(make-pair-arg (make-var-def 'a) (make-pair-arg (make-var-def 'b) (make-null-arg))))
))
function-names
op-names))
))
(define math-unop-env
(let ((function-names
`(neg abs atan asin acos sin cos exp ln
sqrt tan cosh sinh tanh hypot gamma lgamma log10 log2 log1p ldexp cube
round ceiling floor
heaviside random.exponential))
(op-names
`(signal.neg signal.abs signal.atan signal.asin signal.acos signal.sin signal.cos signal.exp signal.ln
signal.sqrt signal.tan signal.cosh signal.sinh signal.tanh signal.hypot signal.gamma signal.lgamma signal.log10
signal.log2 signal.log1p signal.ldexp signal.cube
signal.round signal.ceiling signal.floor
signal.heaviside
random.exponential)))
(fold (lambda (k v env)
(let ((binding (gen-binding k v)))
(extend-env-with-binding env binding)))
empty-env
function-names
(map (lambda (f fn)
(make-function (make-free-variable f)
(make-pair-formal (make-var-def 'a) (make-null-formal))
`(signal.call ,fn ,(make-pair-arg (make-var-def 'a) (make-null-arg))))
)
function-names
op-names)
))
)
(define math-zop-env
(let ((function-names
`(random.uniform))
(op-names
`(random.uniform)))
(fold (lambda (k v env)
(let ((binding (gen-binding k v)))
(extend-env-with-binding env binding)))
empty-env
function-names
(map (lambda (f fn)
(make-function (make-free-variable f)
(make-null-formal)
`(signal.call ,fn ,(make-null-arg)))
)
function-names
op-names)
))
)
;; A representation of a flattened model, normally created with
;; `elaborate(model)`. The code generation procedures take an
;; elaborated model as input.
;;
;; Contains the hierarchical equations, flattened equations, flattened
;; initial equations, events, event response functions, and a map of
;; nodes.
(define-record-type equation-set
(make-equation-set model definitions discrete-definitions parameters fields externals externalevs
equations initial conditions pos-responses neg-responses
functions nodemap regimemap dimenv)
equation-set?
(model equation-set-model)
(definitions equation-set-definitions)
(discrete-definitions equation-set-discrete-definitions)
(parameters equation-set-parameters)
(fields equation-set-fields)
(externals equation-set-externals)
(externalevs equation-set-externalevs)
(equations equation-set-equations)
(initial equation-set-initial)
(conditions equation-set-conditions)
(pos-responses equation-set-pos-responses)
(neg-responses equation-set-neg-responses)
(functions equation-set-functions)
(nodemap equation-set-nodemap)
(regimemap equation-set-regimemap)
(dimenv equation-set-dimenv)
)
(define-record-printer (equation-set x out)
(fprintf out "#")
(pp
`(equation-set
(definitions=,(equation-set-definitions x))
(discrete-definitions=,(equation-set-discrete-definitions x))
(parameters=,(equation-set-parameters x))
(fields=,(equation-set-fields x))
(externals=,(equation-set-externals x))
(externalevs=,(equation-set-externalevs x))
(equations=,(equation-set-equations x))
(functions=,(equation-set-functions x))
(conditions=,(equation-set-conditions x))
(pos-responses=,(equation-set-pos-responses x))
(neg-responses=,(equation-set-neg-responses x))
(nodemap=,(equation-set-nodemap x))
(regimemap=,(equation-set-regimemap x)))
out))
;; runtime representation of a simulation object
(define-record-type simruntime
(make-simruntime eqset cindexmap dindexmap evindexmap rindexmap extindexmap extevindexmap
parameters fields defs discrete-defs external-defs externalev-defs externalev-links
eqblock condblock posresp negresp)
simruntime?
(eqset simruntime-eqset)
(cindexmap simruntime-cindexmap)
(dindexmap simruntime-dindexmap)
(evindexmap simruntime-evindexmap)
(rindexmap simruntime-rindexmap)
(extindexmap simruntime-extindexmap)
(extevindexmap simruntime-extevindexmap)
(parameters simruntime-parameters)
(fields simruntime-fields)
(defs simruntime-definitions)
(discrete-defs simruntime-discrete-definitions)
(external-defs simruntime-external-definitions)
(externalev-defs simruntime-externalev-definitions)
(externalev-links simruntime-externalev-links)
(eqblock simruntime-eqblock)
(condblock simruntime-condblock)
(posresp simruntime-posresp)
(negresp simruntime-negresp)
)
(define-record-printer (simruntime x out)
(fprintf out "#")
(pp `(simruntime
(cindexmap=,(simruntime-cindexmap x))
(dindexmap=,(simruntime-dindexmap x))
(evindexmap=,(simruntime-evindexmap x))
(extindexmap=,(simruntime-extindexmap x))
(extevindexmap=,(simruntime-extevindexmap x))
(parameters=,(simruntime-parameters x))
(fields=,(simruntime-fields x))
(defs=,(simruntime-definitions x))
(external-defs=,(simruntime-external-definitions x))
(externalev-defs=,(simruntime-externalev-definitions x))
(externalev-links=,(simruntime-externalev-links x))
(eqblock=,(simruntime-eqblock x))
(evblock=,(simruntime-condblock x))
(posresp=,(simruntime-posresp x))
(negresp=,(simruntime-negresp x))
)
out)
)
(define-record-printer (astdecls x out)
(fprintf out "#")
(pp `(ast . ,(astdecls-decls x)) out))
(define model-time (unknown (make-constant 'number 0.0 millisecond) 't millisecond))
(define (map-equations f eqs)
(map
(match-lambda*
[($ salt#initial-equation s expr)
(make-init s (f expr))]
[($ salt#equation s expr)
(make-eq s (f expr))]
[eqn (error 'map-equation "unknown equation type" eqn)])
eqs))
(define (replace-fixed expr)
(map
(lambda (x)
(cond ((variable? x) (or (variable-value x) x))
((list? x) `(,(car x) . ,(map replace-fixed (cdr x))))
(else x)))
expr))
;; Equation parsing
(define default-parse-env
(fold (lambda (ext-env env) (extend-env-with-env env ext-env))
empty-env
(list math-constant-env
math-binop-env
math-unop-env
math-zop-env)
))
(define (parse decls #!key (env default-parse-env))
(letrec
((parse0
(lambda (decls)
(d 'parse "decls = ~A~%" decls)
(map (lambda (x)
(cond
((astdecls? x) x)
(else (parse-declaration env x))
))
decls))
))
(make-astdecls (parse0 decls)))
)
;; Expression resolution
(define (resolve expr env-stack)
(trace 'resolve "expr: ~A env-stack: ~A~%"
expr (env-stack-show env-stack))
(let recur ((e expr))
(trace 'resolve "e: ~A~%" e)
(cond
((free-variable? e)
(let ((assoc-var-def (env-stack-lookup (free-variable-name e) env-stack)))
(trace 'resolve "free-var: e = ~A assoc-var-def = ~A~%" (free-variable-name e) assoc-var-def)
(if assoc-var-def
(let recur ((resval (binding-value assoc-var-def)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1))))
(let ((assoc-unit (assv (free-variable-name e) (model-units))))
(trace 'resolve "free-var: e = ~A assoc-unit = ~A~%" (free-variable-name e) assoc-unit)
(if assoc-unit
(make-constant 'number 1.0 (cdr assoc-unit))
e)))
))
((derivative-variable? e)
(make-derivative-variable (resolve (derivative-variable-parent e) env-stack)))
((variable? e)
(let ((name (variable-name e))
(label (variable-label e))
(value (let recur ((resval (variable-value e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(history (variable-history e))
(dim (variable-dim e)))
(make-variable name label value history dim)))
((discrete-variable? e)
(let ((name (discrete-variable-name e))
(label (discrete-variable-label e))
(value (let recur ((resval (discrete-variable-value e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(dim (discrete-variable-dim e)))
(make-discrete-variable name label value dim)))
((parameter? e)
(let ((name (parameter-name e))
(label (parameter-label e))
(value (let recur ((resval (parameter-value e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(dim (parameter-dim e)))
(make-system-parameter name label value dim)))
((field? e)
(let ((name (field-name e))
(label (field-label e))
(value (let recur ((resval (field-value e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(dim (field-dim e)))
(make-field name label value dim)))
((reduce-variable? e)
(make-reduce-variable
(resolve (reduce-variable-op e) env-stack)
(resolve (reduce-variable-parent e) env-stack)))
((external? e)
(let ((name (external-name e))
(label (external-label e))
(initial (let recur ((resval (external-initial e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(dim (external-dim e))
(order (external-order e)))
(make-external name label initial dim order)))
((externalev? e)
(let ((name (externalev-name e))
(label (externalev-label e))
(initial (let recur ((resval (externalev-initial e)))
(let ((resval1 (resolve resval env-stack)))
(if (equal? resval resval1) resval
(recur resval1)))))
(dim (externalev-dim e))
(order (externalev-order e))
(link (externalev-link e))
)
(make-externalev name label initial dim order link)))
((pair-arg? e)
(make-pair-arg (recur (pair-arg-fst e))
(recur (pair-arg-snd e))))
((function? e)
(let ((name (function-name e))
(formals (function-formals e))
(body (function-body e)))
(let* ((env-stack1 (push-env-stack
(function-formal-env formals empty-env)
env-stack))
(body1 (resolve body env-stack1)))