Skip to content

Add shared RK4 solvers for scalar and vector ODE initial-value problems - #313

Draft
PawelTroka wants to merge 1 commit into
masterfrom
codex/ode-solvers-20261030
Draft

PawelTroka wants to merge 1 commit into
masterfrom
codex/ode-solvers-20261030

Conversation

@PawelTroka

@PawelTroka PawelTroka commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Problem

Refs #45. Computator.NET has shared integration, differentiation and root-finding methods, but it does not expose an ordinary differential equation initial-value solver through TSL. For example, this scalar problem has the analytic answer 7, but currently fails compilation because DifferentialEquation is unknown:

DifferentialEquation.rungeKutta4((t,u) => 2*t,3.0,0.0,2.0,21)[20]

Change

Expose the existing, pinned Math.NET Numerics 5.0.0 classical fourth-order Runge–Kutta implementation through one shared DifferentialEquation class, with scalar and real-vector overloads:

var values = DifferentialEquation.rungeKutta4(
    (t,u) => u, 1.0, 0.0, 1.0, 101);
// values[100] approximates exp(1).

var states = DifferentialEquation.rungeKutta4(
    (t,state) => vector(state[1],-state[0]),
    vector(1.0,0.0), 0.0, 2*PI, 401);
// Position/velocity of y'' = -y after one period: approximately (1,0).
  • samples counts stored states including both endpoints; default 101, accepted range 2–1,000,001. Descending intervals work. Equal bounds repeat the initial state without calling the derivative.
  • Validate finite inputs and representable RK4 time stages. Reject nonfinite computed states/derivatives and wrong-sized system derivatives; propagate callback exceptions.
  • Copy vector state at callback boundaries and copy derivative outputs so callbacks cannot overwrite earlier samples or stages by mutating inputs or reusing a result buffer.
  • Compile the implementation once into Engine, exclude it from Core compilation, and use the shared TSL alias for portable and desktop evaluator paths. No new dependency or duplicated emitted ToCode algorithm is introduced.
  • Add scripting completion, one authored reference article covering both reflected overloads, a worked example, and a linked guide in the documentation directory and canonical solution.

This is a fixed-step initial-value method. Adaptive error control, stiff solvers, event detection, boundary-value solving and a dedicated desktop ODE panel remain outside this change. The guide explains convergence checks, floating-point limits and the dependence of memory/cost on system dimension and callback behavior.

Validation

Published Git tree 1bd9b590b5f37155973fa53344f0c9220e1d6f51 exactly matches the validated local tree, based on master fe1aba49b5f9538355ffaaa3d1683f69a23d2d9c.

Fresh local runs used .NET SDK 10.0.403, Release, Linux x64:

  • Before: the six OdeExecutionTests regressions fail on the unchanged implementation with CS0103: The name 'DifferentialEquation' does not exist in the current context.
  • After: the full Engine suite passes 1,472/1,472, with 34 ODE cases and no failures/skips. The tests cover analytic scalar solutions at every sample, fourth-order convergence, coupled oscillator state, reverse/zero-length intervals, vector ownership, validation and failure behavior, actual TSL expressions/scripts, and custom-definition isolation.
  • Reference Python suite: 61/61 pass.
  • Deterministic reference --check: passes. All 1,220 existing article blocks are byte-identical; one article is added for two overloads. The local inventory combines the authenticated baseline Windows inventory with the new type's output from the production reflection/signature source, using a portable UI shell. Native catalogue coverage remains part of Windows CI.
  • Windows-targeted Computator.NET.Core.Tests cross-build: succeeds with 0 errors, plus two existing unused-field warnings in ToolStripRadioButtonMenuItem. It includes new tests for legacy/precompiled evaluator paths and production completion/reference attachment. Those native Windows tests have not been executed on Linux. Native Windows/Mono acceptance and the platform-specific Cake gate remain CI/reviewer checks.

Commands used (with dotnet resolved to SDK 10.0.403 and the local NuGet cache):

# Baseline reproduction, with the six execution tests added before implementation:
dotnet test Computator.NET.Engine.Tests/Computator.NET.Engine.Tests.csproj \
  -c Release --filter FullyQualifiedName~OdeExecutionTests \
  -m:1 -nodeReuse:false -p:UseSharedCompilation=false

# Complete portable suite after implementation:
dotnet test Computator.NET.Engine.Tests/Computator.NET.Engine.Tests.csproj \
  -c Release -m:1 -nodeReuse:false -p:UseSharedCompilation=false

python3 -m unittest discover -s build/reference -p 'test_*.py'
python3 build/reference/generate.py \
  --inventory /tmp/computator-recovered-ode-inventory.json \
  --output Computator.NET.Core/Static/functions.xml --check

dotnet build Computator.NET.Core.Tests/Computator.NET.Core.Tests.csproj \
  -c Release -p:EnableWindowsTargeting=true \
  -m:1 -nodeReuse:false -p:UseSharedCompilation=false

Relationship to existing work

Open pull requests, GitHub issues and the current shared engine functionality were inspected before selecting this work. No existing open PR provides the ODE API requested by #45. This branch is independent of the concurrent charting/platform, parsing, secant, translation, special-function, integration-count and Euclidean-algorithm changes. It deliberately reuses the established Engine/TSL/reference architecture and leaves those implementations untouched.

Draft for owner review and native CI. No merge performed.

Combined compatibility check — 2026-10-30

Also tested the final contributions in #311, #313, #314 and #315 together with the seven existing maintenance PRs (#304–#310), using the existing #312 candidate 189d28c0ec7acbf77d380f14f95ef450e7bfbd84 as the local integration base.

  • Complete combined Engine Release suite: 1,819 passed, zero failed/skipped.
  • Combined Python reference-authoring suite: 76 passed.
  • Windows-targeted Core.Tests and Desktop.Tests cross-builds: passed, zero errors, with existing warnings.
  • Deterministic reference check: passed, covering 1,251 articles and 1,351 completion items.

Eight shared paths required composition resolutions. These preserved all Euclid/ODE/polynomial aliases, completion and authoring registrations; Debye/unitary documentation; and Find/Replace/printing methods, test-source links and documentation entries. No feature behavior or numerical algorithm needed changing to combine the contributions. The independently published PRs remain based on master.

Combined local tree: 63225a973ea2883974a91ab26afcdc83d866e77b, SDK 10.0.403, Linux x64. The reference check used the authenticated master inventory plus current production reflection for changed entries through a portable UI shell. Native Windows tests, full native catalog execution and printer acceptance remain pending. The existing #312 PR was not changed by this local verification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant