SciML / SciML/ModelingToolkitStandardLibrary.jl

FirstOrder(lowpass=false): documented transfer function differs from implementation for k != 1

Open
#515 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Julia
Stars
171
Forks
49
Avg merge
15h 50m
Merged PRs (30d)
10

Description

Describe the bug 🐞

The documented transfer function for FirstOrder(lowpass=false) differs from the implementation when k ≠
1.

The docstring gives (sT + 1 - k)/(sT + 1), but the component equations imply k*sT/(sT + 1).

These agree at the default k=1. For k=2, their DC gains are −1 and 0, respectively.

Expected behavior

The documentation and implementation should describe the same transfer function. I’m unsure which
behavior is intended.

Minimal Reproducible Example 👇

using ModelingToolkit, Symbolics
  using ModelingToolkit: t_nounits as t, D_nounits as D
  using ModelingToolkitStandardLibrary.Blocks: FirstOrder, Constant

  @named input = Constant()
  @named filter = FirstOrder(lowpass=false)
  @named model = System(
      [connect(input.output, filter.input)], t;
      systems=[input, filter],
  )

  eqs = equations(expand_connections(model))
  rate = only(eq.rhs for eq in eqs if isequal(eq.lhs, D(filter.x)))
  output = only(
      eq.rhs for eq in eqs
      if isequal(eq.lhs, filter.y) &&
         any(v -> isequal(v, filter.x), Symbolics.get_variables(eq.rhs))
  )

  values = Dict(filter.u => 1, filter.x => 2, filter.k => 2, filter.T => 1)
  evaluate(expr) = Symbolics.unwrap_const(Symbolics.unwrap(
      Symbolics.simplify(Symbolics.substitute(expr, values))
  ))

  @show evaluate(rate)    # 0: the state is stationary
  @show evaluate(output)  # 0: actual output
  @show (1 - 2) * 1       # -1: documented DC gain times input

Error & Stacktrace ⚠️

No exception occurs. The discrepancy is in the output:

evaluate(rate) = 0
evaluate(output) = 0
(1 - 2) * 1 = -1

Environment (please complete the following information):

Julia 1.11.9. Relevant entries from using Pkg; Pkg.status():

⌃ [961ee093] ModelingToolkit v11.20.0
⌅ [16a59e39] ModelingToolkitStandardLibrary v2.29.4
⌃ [0c5d862f] Symbolics v7.18.1

Additional context

Found using our formal property prover system.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FirstOrder docstring at src/Blocks/continuous.jl#L116-L122 and its component equations at #L157-L160. Run the provided Julia reproducer to confirm the differing transfer functions, then determine which behavior is intended and make the documentation and implementation agree; verify that the k=1 and k≠1 cases match.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.