feat (exm): sincos example

This commit is contained in:
Henri Saudubray 2025-04-29 15:33:57 +02:00
parent 9a0d22e880
commit d398989ece
Signed by: hms
GPG key ID: 7065F57ED8856128
6 changed files with 87 additions and 58 deletions

View file

@ -11,13 +11,14 @@ let inplace = ref false
let steps = ref 1
let model = ref ""
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 hasmodel = 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 hasmodel = ref false
let modelargs = ref []
let set_model s =
if !hasmodel then raise (Arg.Bad "Too many arguments.") else
model := s; hasmodel := true
if !hasmodel then modelargs := s :: !modelargs
else model := s; hasmodel := true
let opts = [
"-sample", Arg.Int (gt0i sample), "n \tSample count (default=10)";
@ -37,10 +38,13 @@ let output = Output.print !sample
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
| "ball" -> Ball.bouncing_ball ()
| "vdp" -> Vdp.van_der_pol ()
| _ -> raise (Arg.Bad (Format.sprintf "Unknown model: %s." !model))
| "ball" -> Ball.bouncing_ball
| "vdp" -> Vdp.van_der_pol
| "sincos" -> Sincos.sinus_cosinus
| _ -> eprintf "Unknown model: %s\n" !model; exit 2
let m = try m !modelargs with Invalid_argument s -> eprintf "%s\n" s; exit 2
let state = if !inplace then (module State.InPlaceSimState : State.SimState)
else (module State.FunctionalSimState : State.SimState)

View file

@ -1,7 +1,4 @@
let debug = ref false
let fmt () = if !debug then Format.std_formatter
else Format.make_formatter (fun _ _ _ -> ()) (fun () -> ())
let print = Format.fprintf (fmt ()) "%s"
let print s = if !debug then Format.printf "%s\n" s else ()

View file

@ -82,8 +82,6 @@ module LazySim (S : SimState) =
(** Run the model on multiple inputs. *)
let run_on_n model solver inputs use =
ignore @@ List.fold_left (fun (DNode sim) i ->
Common.Debug.print
(Format.sprintf "New input: %.10e\t%.10e\n" i.start i.length);
let state = match sim.step sim.state (Some i) with
| None, s -> s | _ -> assert false in
let rec loop (DNode s) =
@ -185,8 +183,6 @@ module GreedySim (S : SimState) =
(** Run the model on multiple inputs. *)
let run_on_n model solver inputs use =
let o, _ = List.fold_left (fun (acc, DNode sim) i ->
Common.Debug.print
(Format.sprintf "new input: %.10e\t%.10e\n" i.start i.length);
let o, state = sim.step sim.state i in
o::acc, DNode { sim with state }) ([], run model solver) inputs in
List.iter use (List.concat (List.rev o))