;; ;; NineML IVP code generator for Octave/MLton. ;; ;; ;; Copyright 2010-2011 Ivan Raikov and the Okinawa Institute of ;; Science and Technology. ;; ;; 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 ;; . ;; (module 9ML-ivp-octave-mlton (ivp-octave-mlton) (import scheme chicken ) (import (only files make-pathname pathname-directory pathname-file absolute-pathname? ) (only data-structures conc alist-ref intersperse) (only posix current-directory) (only srfi-1 filter list-index) (only srfi-13 string-concatenate)) (require-extension setup-api datatype signal-diagram 9ML-repr 9ML-ivp-mlton) (define nl "\n") (define (octave/mlton-m ivp-id ivar hvar dvars ic imax log-path adir) (let* ((states (cons ivar dvars)) (parameters (filter (lambda (x) (not (member (car x) states))) ic))) `(,(sprintf "addpath (~S);~%" adir ) ,(sprintf "i = 1; t = 0; tmax = ~A;~%" imax) ,(sprintf "~A_open();~%" ivp-id) ,(sprintf "[state parameters] = ~A_initial();~%" ivp-id) ,(sprintf "N = size(state)(1);~%" ) ,(sprintf "log = repmat(state,[1 ((tmax-t+1)/(parameters(~A)))]);~%" (+ 1 (list-index (lambda (x) (eq? (car x) hvar)) parameters))) ,(sprintf "tic;~%") ,(sprintf "while (t < tmax) ~%") ,(sprintf " ~A_run(state,parameters,log,i);~%" ivp-id) ,(sprintf " t = state(1);~%" ) ,(sprintf " i = i+1;~%") ,(sprintf "endwhile~%") ,(sprintf "toc;~%") ,(sprintf "~A_close();~%" ivp-id) ,(sprintf "log = resize(log,[N (i-1)]);~%" ) ,(sprintf "data = log';~%" ) ,(sprintf "save (\"-ascii\", ~S, \"data\");~%" log-path) )) ) (define (mlton-clib-oct-cc ivp-id N P) `(,(sprintf #< #include "~A_clib.h" void print_usage (void) { } EOF ivp-id) ,(sprintf #< if v then setReal(p,i,1.0) else setReal(p,i,0.0)) val getBoolean = (fn (p,i) => if Real.==(getReal(p,i),1.0) then true else false) EOF ) (define (mlton-clib-exports ivp-id ivar dvars ic) `( ,(sprintf "val e = _export \"~A_clib_initial\" public: (MLton.Pointer.t -> unit) -> unit;~%~%" ivp-id) ,(sprintf "val _ = e (fn(p) => (coutputstate (p,initial)))~%~%") ,(sprintf "val e = _export \"~A_clib_parameters\" public: (MLton.Pointer.t -> unit) -> unit;~%~%" ivp-id) ,(sprintf "val _ = e (fn(p) => (coutputparameters (p,initial)))~%~%") ,(sprintf "val e = _export \"~A_clib_run1\" public: (MLton.Pointer.t * MLton.Pointer.t -> unit) -> unit;~%~%" ivp-id) ,(sprintf "val _ = e (run1(Model.~A,coutputstate))~%~%" ivp-id) )) (define (mlton-clib-run1 ivar dvars ic) (let* ((states (cons ivar dvars)) (sic (cons (cons ivar (alist-ref ivar ic)) (append (filter (lambda (x) (member (car x) dvars)) ic) (filter (lambda (x) (not (member (car x) states))) ic)))) (iv (lambda (vars) (let recur ((vars vars) (sindex 0) (pindex 0) (ax '())) (if (null? vars) (reverse ax) (let ((x (car vars))) (let ((n (car x)) (v (cdr x))) (let ((get (cond ((number? v) "getReal") ((boolean? v) "getBoolean") (else "")))) (let ((asgn (if (member n states) (sprintf "~A=(~A(s,~A))" n get sindex) (sprintf "~A=(~A(p,~A))" n get pindex))) (sindex (if (member n states) (+ 1 sindex) sindex)) (pindex (if (member n states) pindex (+ 1 pindex)))) (recur (cdr vars) sindex pindex (cons asgn ax)) ))) )) ))) (input (string-append "{" (string-concatenate (intersperse (iv sic) ",")) "}")) (ov (lambda (vars) (let recur ((vars vars) (pindex 0) (ax '())) (if (null? vars) (reverse ax) (let ((x (car vars))) (let ((n (car x)) (v (cdr x))) (let ((get (cond ((number? v) "getReal") ((boolean? v) "getBoolean") (else "")))) (let ((asgn (if (member n states) (sprintf "~A=(#~A(~A))" n n "nstate" ) (sprintf "~A=(~A(p,~A))" n get pindex))) (pindex (if (member n states) pindex (+ 1 pindex)))) (recur (cdr vars) pindex (cons asgn ax)) ))) )) ))) (nstate1 (let recur ((vars sic) (pindex 0) (ax '())) (string-append "{" (string-concatenate (intersperse (ov sic) ",")) "}")))) (sprintf #<