feat: a lot of stuff

This commit is contained in:
Henri Saudubray 2025-05-12 14:50:10 +02:00
parent dd6152833f
commit 6cec3d6c5d
Signed by: hms
GPG key ID: 7065F57ED8856128
22 changed files with 476 additions and 276 deletions

View file

@ -1,15 +1,17 @@
open Hsim
open Types
open Solvers
open Examples
open Common
open Types
let sample = ref 10
let stop = ref 30.0
let greedy = ref false
let inplace = ref false
let steps = ref 1
let model = ref None
let sample = ref 10
let stop = ref 30.0
let greedy = ref false
let inplace = ref false
let sundials = ref false
let steps = ref 1
let model = ref None
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
@ -21,38 +23,60 @@ let set_model 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";
"-greedy", Arg.Set greedy, "\tUse greedy simulation";
"-inplace", Arg.Set inplace, "\tUse greedy simulation";
"-steps", Arg.Int (gt0i steps), "n \tSplit into [n] steps (default=1)";
"-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";
"-greedy", Arg.Set greedy, "\tUse greedy simulation";
"-sundials", Arg.Set sundials, "\tUse sundials (not compatible with greedy)";
"-inplace", Arg.Set inplace, "\tUse imperative solvers";
"-steps", Arg.Int (gt0i steps), "n \tSplit into [n] steps (default=1)";
]
let errmsg = "Usage: " ^ Sys.executable_name ^ " [OPTIONS] MODEL\nOptions are:"
let () = try Arg.parse (Arg.align opts) set_model errmsg with _ -> exit 2
let output = Output.print !sample
let m =
match !model with
| None -> Format.eprintf "Missing model\n"; exit 2
| Some "ball" -> (module Ball : Model.Model)
| Some "vdp" -> (module Vdp)
| Some "sincos" -> (module Sincos)
| Some "sqrt" -> (module Sqrt)
| Some s -> Format.eprintf "Unknown model: %s\n" s; exit 2
let c = StatefulRK45.(if !inplace then InPlace.csolve else Functional.csolve)
let z = StatefulZ.(Functional.zsolve)
let s = Solver.solver_c c z
open Format
let m = match !model with
| None -> eprintf "Missing model\n"; exit 2
| Some "ball" -> Ball.bouncing_ball
| Some "vdp" -> Vdp.van_der_pol
| Some "sincos" -> Sincos.sinus_cosinus
| Some "sqrt" -> Sqrt.sqrt
| Some s -> eprintf "Unknown model: %s\n" s; exit 2
let m = try m !modelargs with Invalid_argument s -> eprintf "%s\n" s; exit 2
let z = if !inplace then (module StatefulZ.InPlace : Zsolver.ZsolverC)
else (module StatefulZ.Functional : Zsolver.ZsolverC)
let state = if !inplace then (module State.InPlaceSimState : State.SimState)
else (module State.FunctionalSimState : State.SimState)
let sim = if !greedy
then let open Sim.GreedySim(val state) in run_until_n m s
else let open Sim.LazySim(val state) in run_until_n m (d_of_dc s)
let st = if !inplace then (module State.InPlaceSimState : State.SimState)
else (module State.FunctionalSimState : State.SimState)
let () = sim !stop !steps output
let () =
(* if !sundials then *)
(* if !greedy then *)
(* (Format.eprintf "Sundials does not support greedy simulation\n"; exit 2) *)
(* else *)
(* let open StatefulSundials in *)
(* let c = if !inplace then (module InPlace : Csolver.Csolver) *)
(* else (module Functional : Csolver.Csolver) in *)
(* let open (val c) in let open (val z) in *)
(* let s = Solver.solver csolve (d_of_dc zsolve) in *)
(* let open Sim.LazySim(val st) in run_until_n m s *)
(* else *)
let open (val m) in
let m = init !modelargs in
let open StatefulRK45 in
let c = if !inplace then (module InPlace : Csolver.CsolverC)
else (module Functional : Csolver.CsolverC) in
let open (val c) in let open (val z) in
let s = Solver.solver_c csolve zsolve in
let sim = if !greedy then let open Sim.GreedySim(val st) in run_until_n m s
else let open Sim.LazySim(val st) in run_until_n m (d_of_dc s) in
let open Solver in
let HNode { init; csize; zsize; fder; fzer; cget; _ } = m in
let state = init () in
let init = cget state in
let ivp = { size=csize; fder=(fun _ -> fder state ()); init; stop=1.0 } in
let zc = { size=zsize; fzer=(fun _ -> fzer state ()); init } in
sim !stop !steps ((), (ivp, zc)) (Output.print !sample)