Kronrod error when using QuadGK w/ symbolic inputs

I am running a script where I’m calculating some values using ModelingToolkit with some code that I’ve developed. I register my functions using the @register_symbolic macro, and these functions work on their own through regular use and test. But when I run the following line to create an equation for ModelingToolkit, I get this error:

julia> eqs = [stream_enthalpy(stream1; T=Tval) - stream_enthalpy(stream1) ~ stream_delta_heat_capacity(stream1, Tval)]
ERROR: MethodError: no method matching kronrod(::Type{Num}, ::Int64)
The function kronrod exists, but no method is defined for this combination of argument types.

Closest candidates are:
kronrod(::Any, ::Integer, ::Real, ::Real; rtol, quad)
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/weightedgauss.jl:90
kronrod(::Type{T}, ::Integer) where T<:AbstractFloat
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/gausskronrod.jl:337
kronrod(::AbstractMatrix{<:Real}, ::Integer, ::Real, ::Pair{<:Tuple{Real, Real}, <:Tuple{Real, Real}})
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/gausskronrod.jl:411

Stacktrace:
[1] macro expansion
@ ~/.julia/packages/QuadGK/7rND3/src/gausskronrod.jl:618 [inlined]
[2] _cachedrule(::Type{Num}, n::Int64)
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/gausskronrod.jl:618
[3] cachedrule(::Type{Num}, n::Int64)
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/gausskronrod.jl:626
[4] do_quadgk(f::ProcessCalc.var"#39#43"{…}, s::Tuple{…}, n::Int64, atol::Nothing, rtol::Nothing, maxevals::Int64, nrm::typeof(LinearAlgebra.norm), _segbuf::Nothing, eval_segbuf::Nothing)
@ QuadGK ~/.julia/packages/QuadGK/7rND3/src/adapt.jl:19
[5] #50
@ ~/.julia/packages/QuadGK/7rND3/src/api.jl:83 [inlined]
[6] handle_infinities
@ ~/.julia/packages/QuadGK/7rND3/src/adapt.jl:189 [inlined]
[7] quadgk#49
@ ~/.julia/packages/QuadGK/7rND3/src/api.jl:82 [inlined]
[8] quadgk
@ ~/.julia/packages/QuadGK/7rND3/src/api.jl:80 [inlined]
[9] quadgk
@ ~/.julia/packages/QuadGK/7rND3/src/api.jl:77 [inlined]
[10] stream_delta_heat_capacity(stream::FlowStream{RK{…}, Nothing}, outlet_temperature::Num)
@ ProcessCalc ~/.julia/dev/ProcessCalc/src/base/streams.jl:359
[11] top-level scope
@ REPL[23]:1
Some type information was truncated. Use show(err) to see complete types.

Does anyone have any ideas? It doesn’t seem like the error is coming from my code.

To give some extra context, this is what Claude 3.7 is giving me on this:

This is occurring because:

  • You’re trying to use stream_delta_heat_capacity with a symbolic variable (Tval of type Num from Symbolics.jl) as the outlet temperature

  • Inside stream_delta_heat_capacity, the quadgk function is trying to perform numerical integration with this symbolic variable

  • The kronrod function (used internally by quadgk) doesn’t have a method defined for symbolic types

This is not an issue with your code specifically, but rather a limitation in how these packages interact. The QuadGK package is designed for numerical integration with concrete floating-point numbers, not symbolic variables.

I’d like to use QuadGK in my functions, but for some reason it doesn’t seem like variables are registering properly.

Please don’t post AI-generated answers, in general. People are on this forum to talk to humans.

Your stream_delta_heat_capacity is calling quadgk to do an integral. But quadgk is for numerical integration, it won’t work on symbolic variables.

It’s not clear what you want it to do here. Do you want a symbolic integral? Or are you hoping that ModelingToolkit will generate some code that calls quadgk?

I have a numerical function “stream_delta_heat_capacity” that is calling QuadGK as part of it. It’s just integrating from the inlet to the outlet temperature.

I put together another function that overloads this function:

function symbolic_delta_heat_capacity(stream::FlowStream, T_out::Num)
  # Create a function that can be called with numerical values
  function delta_heat_capacity_numeric(T_val)
    return stream_delta_heat_capacity(stream, Float64(T_val))
  end

  return delta_heat_capacity_numeric(value(T_out))
end

This resolves the QuadGK issue, and allows the code to run by casting the variable to a Float64 type.

I’m not sure why posting AI-generated content is not helpful. You’re still conversing with a human – I’m just trying to provide additional context. This error was strange because it was happening two libraries down in the stack, not in code that I had written or understand.

1 Like

For reference, you would use function registration on the call to QuadGK.jl to make this happen. The documentation for this is:

1 Like