From a1a51befcd34a09f6a793951b8c1e94f77328a56 Mon Sep 17 00:00:00 2001 From: Hubertus Tummescheit Date: Fri, 18 Sep 2026 08:57:35 -0400 Subject: [PATCH 1/2] Enable the FMIODE2 Jacobian by default for Radau5ODE and RodasODE AssimuloFMIAlg turns `with_jacobian` on by default only when the FMU provides directional derivatives, or (for FMUs without them, with at least PYFMI_JACOBIAN_LIMIT states) when the solver is CVode. Radau5ODE and RodasODE were excluded and therefore fell back to their own dense finite-difference Jacobian: nx rhs evaluations per Jacobian, and RodasODE needs a Jacobian every step. FMIODE2's Jacobian uses the dependency structure with CPR colouring, i.e. a handful of rhs evaluations per Jacobian, and both solvers accept it (dense, or CSC for Radau5ODE's SPARSE linear solver). Measured on Modelica.Fluid.Examples.BranchingDynamicPipes (82 states, no directional derivatives, rtol 1e-4): Radau5ODE 5.5 s -> 3.3 s, and RodasODE completes where it previously failed (the failing FMU evaluation happened at one of rodas.f's own finite-difference perturbation points). The automatic SPARSE selection for large, sparse systems stays limited to the solvers that have a `linear_solver` option (CVode, Radau5ODE); RodasODE would reject it. Users can still pass with_jacobian=False. Co-Authored-By: Claude Opus 5 (1M context) --- src/pyfmi/fmi_algorithm_drivers.py | 9 +++++++-- tests/test_fmi2.py | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pyfmi/fmi_algorithm_drivers.py b/src/pyfmi/fmi_algorithm_drivers.py index dae2fa3d..9f62d95c 100644 --- a/src/pyfmi/fmi_algorithm_drivers.py +++ b/src/pyfmi/fmi_algorithm_drivers.py @@ -43,6 +43,8 @@ PYFMI_JACOBIAN_LIMIT = 10 PYFMI_JACOBIAN_SPARSE_SIZE_LIMIT = 100 PYFMI_JACOBIAN_SPARSE_NNZ_LIMIT = 0.15 #In percentage +PYFMI_JACOBIAN_SOLVERS = ("CVode", "Radau5ODE", "RodasODE") # get with_jacobian by default (see below) +PYFMI_SPARSE_JACOBIAN_SOLVERS = ("CVode", "Radau5ODE") # ... and support linear_solver = "SPARSE" class FMIResult(JMResultBase): def __init__(self, model=None, result_file_name=None, solver=None, @@ -547,9 +549,12 @@ def _set_options(self): self.with_jacobian = True else: fnbr, _ = self.model.get_ode_sizes() - if fnbr >= PYFMI_JACOBIAN_LIMIT and solver == "CVode": + # Solvers that evaluate the Jacobian themselves by dense finite differences + # (nx rhs calls per Jacobian) benefit from the structure-aware (coloured) + # Jacobian of FMIODE2 just like CVode; RodasODE needs one every step. + if fnbr >= PYFMI_JACOBIAN_LIMIT and solver in PYFMI_JACOBIAN_SOLVERS: self.with_jacobian = True - if fnbr >= PYFMI_JACOBIAN_SPARSE_SIZE_LIMIT: + if fnbr >= PYFMI_JACOBIAN_SPARSE_SIZE_LIMIT and solver in PYFMI_SPARSE_JACOBIAN_SOLVERS: try: self.solver_options["linear_solver"] except KeyError: diff --git a/tests/test_fmi2.py b/tests/test_fmi2.py index 1264bba2..f08642cf 100644 --- a/tests/test_fmi2.py +++ b/tests/test_fmi2.py @@ -312,7 +312,14 @@ def run_case(expected, default="Default"): model.get_ode_sizes = lambda: (PYFMI_JACOBIAN_LIMIT+1, 0) run_case(True) + # solvers with their own dense finite-difference Jacobian get the coloured one too opts["solver"] = "Radau5ODE" + run_case(True) + + opts["solver"] = "RodasODE" + run_case(True) + + opts["solver"] = "LSODAR" run_case(False) opts["solver"] = "CVode" From 6e8ddea3a89d1e908c813c3fb2fc7a9f37021de7 Mon Sep 17 00:00:00 2001 From: Hubertus Tummescheit Date: Sat, 19 Sep 2026 13:30:38 -0400 Subject: [PATCH 2/2] Radau5ODE: default the Jacobian-reuse threshold 'thet' to 0.1 with the PyFMI Jacobian Radau5 recomputes the Jacobian after every accepted step in which Newton's contraction rate exceeded THET (Assimulo option 'thet', default 1e-3), which on stiff FMU models is nearly every step: 1493 Jacobian evaluations for 1629 steps on a 141-state vehicle model, 1473 for 1783 steps on a 226-state plant. CVode on the same models needs 124 and 454. With the PyFMI Jacobian each evaluation costs 100-240 rhs calls, so this dominates the run. Hairer's documentation recommends raising THET to about 0.1 when Jacobian evaluations are costly. Measured at rtol 1e-6 with correct event counts and unchanged accuracy (deviation vs an rtol 1e-8 reference 1e-5 .. 4e-4): model thet 1e-3 -> 0.1 -> 0.5 -> 0.9 (Jacobians / solver time) vehicle, 141 st. 1493 / 379 s -> 1242 / 347 s -> 669 / 226 s -> 654 / 219 s plant, 226 st. 1473 / 444 s -> 1379 / 499 s* -> 1177 / 420 s -> 1189 / 399 s data center, 86 272 -> 213 (6.8 -> 6.6 s); event-dominated small models unchanged (* under parallel load; counts are the measure) 'Radau5ODE_options["thet"]' is now "Default": 0.1 when 'with_jacobian' is in effect, otherwise Assimulo's 1e-3; an explicit value is passed through. Co-Authored-By: Claude Opus 5 --- src/pyfmi/fmi_algorithm_drivers.py | 28 +++++++++++++++++++++++++++- tests/test_fmi2.py | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/pyfmi/fmi_algorithm_drivers.py b/src/pyfmi/fmi_algorithm_drivers.py index 9f62d95c..abad7e79 100644 --- a/src/pyfmi/fmi_algorithm_drivers.py +++ b/src/pyfmi/fmi_algorithm_drivers.py @@ -46,6 +46,13 @@ PYFMI_JACOBIAN_SOLVERS = ("CVode", "Radau5ODE", "RodasODE") # get with_jacobian by default (see below) PYFMI_SPARSE_JACOBIAN_SOLVERS = ("CVode", "Radau5ODE") # ... and support linear_solver = "SPARSE" +# Radau5ODE recomputes the Jacobian after every step in which Newton's contraction rate +# exceeded 'thet' (Hairer's THET, Assimulo default 1e-3, i.e. after nearly every step). +# With the PyFMI Jacobian (100-240 rhs evaluations each on 140-320 state FMUs) a looser +# threshold is worth it: measured -42 % solver time on a 141-state vehicle model and +# -10 % on a 226-state plant, correct events, unchanged accuracy, neutral on small models. +PYFMI_RADAU5_THET_WITH_JACOBIAN = 0.1 + class FMIResult(JMResultBase): def __init__(self, model=None, result_file_name=None, solver=None, result_data=None, options=None, status=0, detailed_timings=None): @@ -209,6 +216,19 @@ class AssimuloFMIAlgOptions(OptionBase): iter -- The iteration method. Can be either 'Newton' or 'FixedPoint' Default: 'Newton' + + Options for Radau5ODE:: + + rtol, atol, maxh -- + As for CVode. + + thet -- + Newton contraction rate above which the Jacobian is recomputed + after an accepted step (0 < thet < 1). Assimulo's default 1e-3 + recomputes it after almost every step; with the PyFMI Jacobian + ('with_jacobian' in effect) the default is 0.1, which halves the + Jacobian evaluations on models where Newton converges slowly. + Default: "Default" (0.1 with the PyFMI Jacobian, else Assimulo's 1e-3) """ def __init__(self, *args, **kw): _defaults= { @@ -231,7 +251,7 @@ def __init__(self, *args, **kw): 'extra_equations':None, 'CVode_options':{'discr':'BDF','iter':'Newton', 'atol':"Default",'rtol':"Default","maxh":"Default",'external_event_detection':False}, - 'Radau5ODE_options':{'atol':"Default",'rtol':"Default","maxh":"Default"}, + 'Radau5ODE_options':{'atol':"Default",'rtol':"Default","maxh":"Default","thet":"Default"}, 'RungeKutta34_options':{'atol':"Default",'rtol':"Default"}, 'Dopri5_options':{'atol':"Default",'rtol':"Default", "maxh":"Default"}, 'RodasODE_options':{'atol':"Default",'rtol':"Default", "maxh":"Default"}, @@ -628,6 +648,12 @@ def _set_solver_options(self): if "usejac" in solver_options and fnbr == 0: solver_options["usejac"] = False + if "thet" in solver_options and isinstance(solver_options["thet"], str) and solver_options["thet"] == "Default": + if self.with_jacobian: + solver_options["thet"] = PYFMI_RADAU5_THET_WITH_JACOBIAN + else: + del solver_options["thet"] # Assimulo's own default + if "maxh" in solver_options and solver_options["maxh"] == "Default": if self.options["ncp"] == 0: solver_options["maxh"] = 0.0 diff --git a/tests/test_fmi2.py b/tests/test_fmi2.py index f08642cf..131d8319 100644 --- a/tests/test_fmi2.py +++ b/tests/test_fmi2.py @@ -330,6 +330,26 @@ def run_case(expected, default="Default"): opts["with_jacobian"] = True run_case(True, True) + def test_radau5_thet_default(self): + """Radau5ODE 'thet': 0.1 with the PyFMI Jacobian, Assimulo's default without, user value wins.""" + model = Dummy_FMUModelME2([], os.path.join(file_path, "files", "FMUs", "XML", "ME2.0", "NoState.Example1.fmu"), _connect_dll=False) + opts = model.simulate_options() + opts["solver"] = "Radau5ODE" + opts["result_handling"] = None + model.get_ode_sizes = lambda: (PYFMI_JACOBIAN_LIMIT + 1, 0) + + def run_case(expected_thet, with_jacobian="Default", thet="Default"): + model.reset() + opts["with_jacobian"] = with_jacobian + opts["Radau5ODE_options"]["thet"] = thet + alg = NoSolveAlg(0.0, 1.0, (), model, opts) + assert alg.simulator.thet == pytest.approx(expected_thet), alg.simulator.thet + + run_case(0.1) # with_jacobian by default for this size and solver + run_case(1e-3, with_jacobian=False) # Assimulo's default + run_case(0.5, thet=0.5) + run_case(0.5, with_jacobian=False, thet=0.5) + def test_sparse_option(self): def run_case(expected_jacobian, expected_sparse, fnbr=0, nnz={}, set_sparse=False):