feat: solvers and ball example

This commit is contained in:
Henri Saudubray 2025-04-25 13:57:53 +02:00
parent cc099c02e7
commit e07f165494
Signed by: hms
GPG key ID: 7065F57ED8856128
27 changed files with 1483 additions and 290 deletions

View file

@ -1,4 +1,49 @@
open Hsim.Types
let _x : 'a value = { start = 0.; length = 0.; u = (fun _ -> 0) }
let () = print_endline "Hello, World!"
open Hsim
open Examples
open Types
let sample = ref 10
let stop = ref 30.0
let debug = ref false
let doc_sample = "n \tSample count [10]"
let doc_stop = "n \tStop time [10.0]"
let doc_debug = "\tPrint debug information"
let opts = [
"-sample", Arg.Set_int sample, doc_sample;
"-stop", Arg.Set_float stop, doc_stop;
"-debug", Arg.Set debug, doc_debug
]
let errmsg = "Usage: " ^ Sys.executable_name ^ " [OPTIONS]\nOptions are:"
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)