Overflow error in `mtkcompile` for friction model with 32 nodes

Aperta
#172 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
julia
Ambito
backend

Direzione di ricerca

The reproducer calls mtkcompile on a generated friction model network and reports an OverflowError at 32 or more nodes; start by running the script with the listed ModelingToolkit version to confirm the threshold. The issue points to ModelingToolkit PR #2089 as possibly related, so inspect that change and the state selection code reached during compilation. Done means identifying and fixing the overflow so the reproducer compiles at the reported node counts.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A network of friction model fails to compile with node counts 32 or larger, it seems to be an integer that overflows. With 31 it works fine.

copilot-authored script to reproduce and monkeypatch to use BigInt below.

ModelingToolkit version 11.45.5, Julia 1.13.2.

Maybe ModelingToolkit #2089: Fix Barreiss algorithm is related in some way?

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@connector function ContactPort(; name)
    variables = @variables begin
        velocity(t)
        force(t), [connect = Flow]
    end
    return System(Equation[], t, variables, []; name)
end

function contact_model(; name)
    parameters = @parameters stiffness = 1.0 rate_coefficient = 1.0
    variables = @variables begin
        deformation(t) = 0.0
        plastic_slip(t) = 0.0
        accumulated_slip(t) = 0.0
        resistance(t)
        capacity(t)
        positive_rate(t)
        negative_rate(t)
        slip_rate(t)
    end
    @named port = ContactPort()
    equations = [
        resistance ~ stiffness * deformation,
        capacity ~ 1 + exp(-accumulated_slip),
        positive_rate ~ (resistance - capacity + abs(resistance - capacity)) /
                         (2rate_coefficient),
        negative_rate ~ (-resistance - capacity + abs(-resistance - capacity)) /
                         (2rate_coefficient),
        slip_rate ~ positive_rate - negative_rate,
        D(deformation) ~ port.velocity - slip_rate,
        D(plastic_slip) ~ slip_rate,
        D(accumulated_slip) ~ positive_rate + negative_rate,
        port.force ~ resistance,
    ]
    return compose(System(equations, t, variables, parameters; name), port)
end

function velocity_source(; name)
    @named port = ContactPort()
    return compose(System([port.velocity ~ -1.0], t; name), port)
end

function contact_network(contact_count)
    @named source = velocity_source()
    contacts = [contact_model(; name = Symbol(:contact, index)) for index in 1:contact_count]
    equations = [connect(source.port, [contact.port for contact in contacts]...)]
    return compose(System(equations, t; name = :contact_network), source, contacts...)
end

function reproduce_main()
    for contact_count in (1, 30, 31, 32, 45, 60)
        try
            elapsed = @elapsed mtkcompile(contact_network(contact_count))
            println("PASS contacts=", contact_count, " compile_s=", elapsed)
        catch exception
            println("FAIL contacts=", contact_count, " ", sprint(showerror, exception))
            exception isa OverflowError || rethrow()
        end
    end
end

function enable_bigint_probe!()
    @eval ModelingToolkit.StateSelection begin
        function aag_bareiss!(structure, original::CLIL.SparseMatrixCLIL{Int, Int})
            pristine = copy(original)
            try
                return invoke(aag_bareiss!, Tuple{Any, CLIL.SparseMatrixCLIL},
                              structure, original)
            catch exception
                exception isa OverflowError || rethrow()
            end
            reduced, solvable, ranks = aag_bareiss!(structure,
                convert(CLIL.SparseMatrixCLIL{BigInt, Int}, pristine))
            maximum_coefficient = maximum(
                (maximum(abs, row; init = big(0)) for row in reduced.row_vals);
                init = big(0))
            println("BIGINT_RETRY_OK rows=", size(pristine, 1),
                    " max_reduced_coefficient=", maximum_coefficient)
            return convert(CLIL.SparseMatrixCLIL{Int, Int}, reduced), solvable, ranks
        end
    end
end

if "--bigint-probe" in ARGS
    enable_bigint_probe!()
end

if abspath(PROGRAM_FILE) == @__FILE__
    reproduce_main()
end
Lingua principale
Julia
Stelle
8
Fork
14
Merge medio
8g 8h
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaComputing/StateSelection.jl

Tutte le issue di JuliaComputing/StateSelection.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.