Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nonlinearsystem): Fix codegen issue for vector parameters #3382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SebastianM-C
Copy link
Contributor

Co-authored-by: Aayush Sabharwal [email protected]

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@ChrisRackauckas
Copy link
Member

test? What does this solve?

@SebastianM-C
Copy link
Contributor Author

This solves a codegen issue for SCCNonlinearProblems, where vector parameters show up in registered functions.
The problem is that with DestructuredArgs, we get codegen that just unpacks the elements of the vector parameter

__mtk_arg_2 = ___mtkparameters___[1]
var"vol1₊rho_0[1]" = __mtk_arg_2[1]
var"vol1₊rho_0[2]" = __mtk_arg_2[2]
var"vol2₊rho_0[1]" = __mtk_arg_2[3]
var"vol2₊rho_0[2]" = __mtk_arg_2[4]
...

but the entire vector variable (e.g. vol1₊rho_0) is not available, so if there is something that needs the entire vector, like a registered function, then we get an undefined variable error.

As for the test, I've been trying to get one based on the existing

import ModelingToolkitStandardLibrary.Blocks as B
import ModelingToolkitStandardLibrary.Mechanical.Translational as T
import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC
@testset "Caching of subexpressions of different types" begin
liquid_pressure(rho, rho_0, bulk) = (rho / rho_0 - 1) * bulk
gas_pressure(rho, rho_0, p_gas, rho_gas) = rho * ((0 - p_gas) / (rho_0 - rho_gas))
full_pressure(rho, rho_0, bulk, p_gas, rho_gas) = ifelse(
rho >= rho_0, liquid_pressure(rho, rho_0, bulk),
gas_pressure(rho, rho_0, p_gas, rho_gas))
@component function Volume(;
#parameters
area,
direction = +1,
x_int,
name)
pars = @parameters begin
area = area
x_int = x_int
rho_0 = 1000
bulk = 1e9
p_gas = -1000
rho_gas = 1
end
vars = @variables begin
x(t) = x_int
dx(t), [guess = 0]
p(t), [guess = 0]
f(t), [guess = 0]
rho(t), [guess = 0]
m(t), [guess = 0]
dm(t), [guess = 0]
end
systems = @named begin
port = IC.HydraulicPort()
flange = T.MechanicalPort()
end
eqs = [
# connectors
port.p ~ p
port.dm ~ dm
flange.v * direction ~ dx
flange.f * direction ~ -f
# differentials
D(x) ~ dx
D(m) ~ dm
# physics
p ~ full_pressure(rho, rho_0, bulk, p_gas, rho_gas)
f ~ p * area
m ~ rho * x * area]
return ODESystem(eqs, t, vars, pars; name, systems)
end
systems = @named begin
fluid = IC.HydraulicFluid(; bulk_modulus = 1e9)
src1 = IC.Pressure(;)
src2 = IC.Pressure(;)
vol1 = Volume(; area = 0.01, direction = +1, x_int = 0.1)
vol2 = Volume(; area = 0.01, direction = +1, x_int = 0.1)
mass = T.Mass(; m = 10)
sin1 = B.Sine(; frequency = 0.5, amplitude = +0.5e5, offset = 10e5)
sin2 = B.Sine(; frequency = 0.5, amplitude = -0.5e5, offset = 10e5)
end
eqs = [connect(fluid, src1.port)
connect(fluid, src2.port)
connect(src1.port, vol1.port)
connect(src2.port, vol2.port)
connect(vol1.flange, mass.flange, vol2.flange)
connect(src1.p, sin1.output)
connect(src2.p, sin2.output)]
initialization_eqs = [mass.s ~ 0.0
mass.v ~ 0.0]
@mtkbuild sys = ODESystem(eqs, t, [], []; systems, initialization_eqs)
prob = ODEProblem(sys, [], (0, 5))
sol = solve(prob)
@test SciMLBase.successful_retcode(sol)
end
and make rho_0 a 1 element vector and register the full_pressure or liquid_pressure/gas_pressure functions, but I'm running into other issues (differentiation wrt array expression).

@SebastianM-C
Copy link
Contributor Author

Rebased on master and added test.

@ChrisRackauckas
Copy link
Member

Looks like it's failing?

@SebastianM-C
Copy link
Contributor Author

The failures seem to be due to aliasing, I think we can re-trigger

@SebastianM-C
Copy link
Contributor Author

Looks like the fix was already merged, this just adds the test

Copy link
Member

@AayushSabharwal AayushSabharwal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I "accidentally fixed" this in #3390, so this PR adds the test

@ChrisRackauckas
Copy link
Member

Test fails though? Needs to rebase?

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.

3 participants