feat: cleanup

This commit is contained in:
Henri Saudubray 2025-04-25 14:25:55 +02:00
parent e07f165494
commit c867859cce
Signed by: hms
GPG key ID: 7065F57ED8856128
3 changed files with 27 additions and 18 deletions

View file

@ -1,7 +1,6 @@
open Hsim
open Examples
open Types
let sample = ref 10
let stop = ref 30.0
@ -23,27 +22,11 @@ let () =
try Arg.parse (Arg.align opts) (fun _ -> ()) errmsg
with _ -> exit 2
let print_samples n { start; length; u } =
if !debug then begin
if length <= 0.0 then
Format.printf "\nD : %.20e\n\n" start
else Format.printf "\nC : %.20e to %.20e\n\n" start (start +. length);
end;
Format.printf "%.20e\t% .20e\n" start (u 0.0).{0};
if length <= 0.0 then ()
else let step = length /. (float_of_int n) in
let rec loop i =
if i > n then ()
else let t = float_of_int i *. step in
(Format.printf "%.20e\t" (start +. t); Format.printf "% .20e\n" (u t).{0};
loop (i+1)) in
loop 1
let () =
let csolver = StatefulRK45.Functional.csolve in
let zsolver = StatefulZ.Functional.zsolve in
let solver = Solver.solver_c csolver zsolver in
let model = Ball.bouncing_ball () in
let open Sim.LazySim(State.FunctionalSimState) in
run_until model (Solver.solver_from_c solver) !stop (print_samples !sample)
run_until model (Solver.solver_from_c solver) !stop (Output.print !sample)

21
src/bin/output.ml Normal file
View file

@ -0,0 +1,21 @@
open Hsim.Types
let print_entry t y =
let n = Bigarray.Array1.dim y in
let rec loop i =
if i = n then ()
else (Printf.printf "\t% .10e" y.{i}; loop (i+1)) in
Printf.printf "% .10e" t;
loop 0;
Printf.printf "\n";
flush stdout
let print samples { start; length; u } =
let step = length /. (float_of_int samples) in
let rec loop i =
if i > samples then ()
else let t = float_of_int i *. step in
(print_entry (start +. t) (u t); loop (i+1)) in
loop 0

View file

@ -6,6 +6,7 @@ open State
module LazySim (S : SimState) =
struct
(** "Lazy" simulation of a model with an appropriate solver. *)
let run
(HNode model : ('p, 'a, 'b, 'y, 'yder, 'zin, 'zout) hnode)
(DNode solver : ('y, 'yder, 'zin, 'zout) solver)
@ -64,6 +65,8 @@ module LazySim (S : SimState) =
S.update ms ss (S.set_idle s) in
DNode { state; step; reset }
(** Run the model on the given input until the end of the input or until the
model stops answering. *)
let run_on model solver input use =
let DNode sim = run model solver in
let state = match sim.step sim.state (Some input) with
@ -75,6 +78,8 @@ module LazySim (S : SimState) =
| Some o -> use o; loop (DNode { s with state }) in
loop (DNode { sim with state })
(** Run the model autonomously until [length], or until the model stops
answering. *)
let run_until model solver length =
run_on model solver { start = 0.0; length; u = fun _ -> () }