feat: runtime as library
This commit is contained in:
parent
8f6320b30e
commit
dc8d941b84
24 changed files with 184 additions and 111 deletions
9
exm/builtins/dune
Normal file
9
exm/builtins/dune
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(env
|
||||
(dev
|
||||
(flags
|
||||
(:standard -w -a))))
|
||||
|
||||
(executable
|
||||
(public_name examples.exe)
|
||||
(name main)
|
||||
(libraries std))
|
||||
93
exm/builtins/main.ml
Normal file
93
exm/builtins/main.ml
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
|
||||
open Hsim
|
||||
open Solvers
|
||||
open Common
|
||||
open Types
|
||||
|
||||
let sample = ref 1
|
||||
let stop = ref 10.0
|
||||
let accel = ref false
|
||||
let inplace = ref false
|
||||
let sundials = ref false
|
||||
let speed = ref false
|
||||
let steps = ref 1
|
||||
let model = ref None
|
||||
let minstep = ref None
|
||||
let maxstep = ref None
|
||||
let mintol = ref None
|
||||
let maxtol = ref None
|
||||
let no_print = ref false
|
||||
|
||||
let gt0i v i = v := if i <= 0 then 1 else i
|
||||
let gt0f v f = v := if f <= 0.0 then 1.0 else f
|
||||
let opt r s = r := Some s
|
||||
let modelargs = ref []
|
||||
|
||||
let set_model s =
|
||||
match !model with
|
||||
| None -> model := Some s
|
||||
| Some _ -> modelargs := s :: !modelargs
|
||||
|
||||
let opts = [
|
||||
"-sample", Arg.Int (gt0i sample), "n \tSample count (default=10)";
|
||||
"-stop", Arg.Float (gt0f stop), "n \tStop time (default=10.0)";
|
||||
"-debug", Arg.Set Debug.debug, "\tPrint debug information";
|
||||
"-accelerate", Arg.Set accel, "\tConcatenate continuous functions";
|
||||
"-sundials", Arg.Set sundials, "\tUse sundials (doesn't support -accelerate)";
|
||||
"-inplace", Arg.Set inplace, "\tUse imperative solvers";
|
||||
"-steps", Arg.Int (gt0i steps), "n \tSplit into [n] steps (default=1)";
|
||||
"-speed", Arg.Set speed, "\tLog the step length";
|
||||
"-minstep", Arg.String (opt minstep), "\tSet minimum solver step length";
|
||||
"-maxstep", Arg.String (opt maxstep), "\tSet maximum solver step length";
|
||||
"-mintol", Arg.String (opt mintol), "\tSet minimum solver tolerance";
|
||||
"-maxtol", Arg.String (opt maxtol), "\tSet maximum solver tolerance";
|
||||
"-no-print", Arg.Set no_print, "\tDo not print output values";
|
||||
]
|
||||
|
||||
let errmsg = "Usage: " ^ Sys.executable_name ^ " [OPTIONS] MODEL\nOptions are:"
|
||||
|
||||
let () = try Arg.parse (Arg.align opts) set_model errmsg with _ -> exit 2
|
||||
|
||||
let args = List.rev !modelargs
|
||||
|
||||
let m =
|
||||
try match !model with
|
||||
| None -> Format.eprintf "Missing model\n"; exit 2
|
||||
| Some "ball" -> Ball.init args
|
||||
| Some "vdp" -> Vdp.init args
|
||||
| Some "sincos" -> Sincos.init args
|
||||
| Some "sqrt" -> Sqrt.init args
|
||||
| Some "sin1x" -> Sin1x.init args
|
||||
| Some "sin1xd" -> Sin1x_der.init args
|
||||
| Some s -> Format.eprintf "Unknown model: %s\n" s; exit 2
|
||||
with Invalid_argument s -> Format.eprintf "%s\n" s; exit 2
|
||||
|
||||
let st = if !inplace then (module State.InPlaceSimState : State.SimState)
|
||||
else (module State.FunctionalSimState : State.SimState)
|
||||
|
||||
let output =
|
||||
if !no_print then Hsim.Utils.ignore
|
||||
else if !speed then Std.Output.print_h
|
||||
else Std.Output.print (* Output.ignore *)
|
||||
|
||||
let sim =
|
||||
if !sundials then
|
||||
let open StatefulSundials in
|
||||
let c = if !inplace then InPlace.csolve else Functional.csolve in
|
||||
let open StatefulZ in
|
||||
let z = if !inplace then InPlace.zsolve else Functional.zsolve in
|
||||
let s = Solver.solver c (d_of_dc z) in
|
||||
let open Sim.Sim(val st) in
|
||||
run_until_n (output !sample (run m s))
|
||||
else
|
||||
let open StatefulRK45 in
|
||||
let c = if !inplace then InPlace.csolve else Functional.csolve in
|
||||
let open StatefulZ in
|
||||
let z = if !inplace then InPlace.zsolve else Functional.zsolve in
|
||||
let s = Solver.solver_c c z in
|
||||
let open Sim.Sim(val st) in
|
||||
let n = if !accel then accelerate m s else run m (d_of_dc s) in
|
||||
run_until_n (output !sample n)
|
||||
|
||||
let () = sim !stop !steps ignore
|
||||
|
||||
13
exm/dune
13
exm/dune
|
|
@ -1,13 +0,0 @@
|
|||
(env
|
||||
(dev
|
||||
(flags
|
||||
(:standard -w -a))))
|
||||
|
||||
(library
|
||||
(name examples)
|
||||
(libraries hsim solvers std))
|
||||
|
||||
; (executable
|
||||
; (name ballz_main))
|
||||
|
||||
(include_subdirs unqualified)
|
||||
|
|
@ -2,7 +2,7 @@ let g = 9.81
|
|||
let y0 = 0.0
|
||||
let y'0 = 10.0
|
||||
|
||||
let hybrid ball (y0, y'0) = (y, y', z) where
|
||||
let hybrid ball () = (y, y', z) where
|
||||
rec der y = y' init y0
|
||||
and der y' = -. g init y'0 reset z -> -0.8 *. (last y')
|
||||
and z = up(-. y)
|
||||
|
|
@ -10,7 +10,7 @@ let hybrid ball (y0, y'0) = (y, y', z) where
|
|||
let hybrid main () =
|
||||
let der t = 1.0 init 0.0 in
|
||||
let s = period(0.01) in
|
||||
let (y, y', z) = ball (y0, y'0) in
|
||||
let (y, y', z) = ball () in
|
||||
present z | s -> (
|
||||
print_float t;
|
||||
print_string "\t";
|
||||
|
|
|
|||
|
|
@ -1,6 +1,16 @@
|
|||
(env
|
||||
(dev
|
||||
(flags
|
||||
(:standard -w -a))))
|
||||
|
||||
(rule
|
||||
(targets ballz.ml ballz.zci ballz_main.ml)
|
||||
(targets ballz.ml ballz.zci)
|
||||
(deps
|
||||
(:zl ballz.zls))
|
||||
(action
|
||||
(run zeluc -s main -o ballz_main %{zl})))
|
||||
(run zeluc %{zl})))
|
||||
|
||||
(executable
|
||||
(public_name ball.exe)
|
||||
(name main)
|
||||
(libraries std))
|
||||
|
|
|
|||
6
exm/zelus/ballz/main.ml
Normal file
6
exm/zelus/ballz/main.ml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
open Std
|
||||
|
||||
let input _ = ()
|
||||
let output (now, (y, _, _)) = Format.printf "%.10e\t%.10e\n" now y
|
||||
let () = Runtime.go input Ballz.ball output
|
||||
|
|
@ -1,6 +1,16 @@
|
|||
(env
|
||||
(dev
|
||||
(flags
|
||||
(:standard -w -a))))
|
||||
|
||||
(rule
|
||||
(targets sincosz_main.ml sincosz.ml sincosz.zci)
|
||||
(targets sincosz.ml sincosz.zci)
|
||||
(deps
|
||||
(:zl sincosz.zls))
|
||||
(action
|
||||
(run zeluc -s f -o sincosz_main %{zl})))
|
||||
(run zeluc %{zl})))
|
||||
|
||||
(executable
|
||||
(public_name sincos.exe)
|
||||
(name main)
|
||||
(libraries std))
|
||||
|
|
|
|||
6
exm/zelus/sincos/main.ml
Normal file
6
exm/zelus/sincos/main.ml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
open Std
|
||||
|
||||
let input _ = ()
|
||||
let output (now, (sin, cos)) = Format.printf "%.10e\t%.10e\t%.10e\n" now sin cos
|
||||
let () = Runtime.go input Sincosz.g output
|
||||
21
exm/zelus/sincos/ztypes.ml
Normal file
21
exm/zelus/sincos/ztypes.ml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
include Std
|
||||
include Ztypes
|
||||
include Solvers
|
||||
|
||||
module type IGNORE = sig end
|
||||
module Defaultsolver : IGNORE = struct end
|
||||
|
||||
module Zlsrun = struct
|
||||
module Make (S : IGNORE) = struct
|
||||
let go s =
|
||||
let s = Lift.lift_hsim s in
|
||||
let open Hsim in
|
||||
let state = (module State.InPlaceSimState : State.SimState) in
|
||||
let solver =
|
||||
Solver.solver (StatefulSundials.InPlace.csolve)
|
||||
(Types.d_of_dc StatefulZ.InPlace.zsolve) in
|
||||
let open Sim.Sim(val state) in
|
||||
()
|
||||
(* run_until_n (Utils.ignore 0 (run s solver)) 30. 1 ignore *)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue