Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TemporarySubsystemManager unexpectedly activates ConstraintData children of indexed constraint #3734

@dallan-keylogic

Description

@dallan-keylogic

Summary

I'm attempting to perform index reduction for DAE systems using IDAES models. I've determined the best way to convert a differential variable to an algebraic variable is to first discretize the system and then deactivate the associated discretization constraint, e.g., m.fs.unit.control_volume.material_accumulation_disc_eq[:, "Liq", "H2O"].deactivate(). I understand this to be the method most compatible with templatization.

Upon attempting to solve the reduced system with PETSc, I encountered a degrees of freedom problem when calling the integrator. After doing some digging, the problem seems to be with using the TemporarySubsystemManager to deactivate an indexed constraint, e.g., m.fs.unit.control_volume.material_accumulation_disc_eq. When the indexed constraint is deactivated using the context manager, upon exiting the context manager all its ConstraintData children are activated, regardless of whether or not they were active upon entering the context manager.

Steps to reproduce the issue

The following script reproduces the issue. m.con[3] is deactivated before entering the TemporarySubsystemManager but is activated upon exit of the context manager.

# example.py
import pyomo.environ as pyo
from pyomo.util.subsystems import TemporarySubsystemManager

m = pyo.ConcreteModel()

m.x = pyo.Var([1,2,3])

@m.Constraint([1,2,3])
def con(b, i):
    return b.x[i] == i

m.con[3].deactivate()

print(m.con[3].active)

with TemporarySubsystemManager(to_deactivate=[m.con]):
    print("foo")

print(m.con[3].active)
False
foo
True

I expect the output to be

False
foo
False

A workaround for this problem is to have the context manager deactivate the ConstraintData children instead of the indexed constraint directly:

# example.py
import pyomo.environ as pyo
from pyomo.util.subsystems import TemporarySubsystemManager

m = pyo.ConcreteModel()

m.x = pyo.Var([1,2,3])

@m.Constraint([1,2,3])
def con(b, i):
    return b.x[i] == i

m.con[3].deactivate()

print(m.con[3].active)

with TemporarySubsystemManager(to_deactivate=[condata for condata in m.con.values()]):
    print("foo")

print(m.con[3].active)
False
foo
False

Information on your system

Pyomo version: 6.9.4
Python version: 3.10.18
Operating system: Windows 10
How Pyomo was installed (PyPI, conda, source): IDAES developer installation
Solver (if applicable): n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions