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

Renaming components #1402

Merged
merged 10 commits into from
May 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Renaming Block classes
  • Loading branch information
Andrew Lee committed May 2, 2024
commit 2e89daf6c71d46e13d8292c593a9db7044c3bb3a
3 changes: 0 additions & 3 deletions idaes/apps/caprese/controller.py
Original file line number Diff line number Diff line change
@@ -46,11 +46,8 @@
Constraint,
Block,
)
from pyomo.core.base.block import _BlockData
from pyomo.common.collections import ComponentMap
from pyomo.core.base.range import remainder
from pyomo.dae.set_utils import deactivate_model_at
from pyomo.dae.flatten import flatten_dae_components
from pyomo.core.base.indexed_component import UnindexedComponent_set


9 changes: 4 additions & 5 deletions idaes/apps/caprese/dynamic_block.py
Original file line number Diff line number Diff line change
@@ -49,16 +49,15 @@
Suffix,
)
from pyomo.core.base.initializer import Initializer
from pyomo.core.base.block import _BlockData, SubclassOf
from pyomo.core.base.block import BlockData, SubclassOf
from pyomo.core.base.indexed_component import UnindexedComponent_set
from pyomo.common.collections import ComponentMap
from pyomo.common.config import ConfigDict, ConfigValue
from pyomo.core.base.range import remainder
from pyomo.dae.set_utils import deactivate_model_at
from pyomo.dae.flatten import flatten_dae_components


class _DynamicBlockData(_BlockData):
class _DynamicBlockData(BlockData):
"""This class adds methods and data structures that are useful
for working with dynamic models. These include methods for
initialization and references to time-indexed variables.
@@ -290,7 +289,7 @@ def _add_category_references(self):
# to add time in a rule without a long messy line of super().__setattr__.
def add_time(self):
# Do this because I can't add a reference to a set
super(_BlockData, self).__setattr__("time", self.time)
super(BlockData, self).__setattr__("time", self.time)

def set_sample_time(self, sample_time, tolerance=1e-8):
"""Validates and sets sample time"""
@@ -802,7 +801,7 @@ def _getitem_when_not_present(self, idx):
block.mod = self._init_model(parent, idx)

if self._init_time is not None:
super(_BlockData, block).__setattr__("time", self._init_time(parent, idx))
super(BlockData, block).__setattr__("time", self._init_time(parent, idx))

if self._init_inputs is not None:
block._inputs = self._init_inputs(parent, idx)
23 changes: 2 additions & 21 deletions idaes/apps/caprese/tests/test_dynamic_block.py
Original file line number Diff line number Diff line change
@@ -14,27 +14,11 @@
"""

import pyomo.environ as pyo
import pyomo.dae as dae
import pyomo.network as pyn
from pyomo.common.collections import ComponentSet
from pyomo.core.expr.visitor import identify_variables
from pyomo.util.calc_var_value import calculate_variable_from_constraint
from pyomo.core.base.block import _BlockData, SubclassOf
from pyomo.core.base.block import BlockData, SubclassOf
from pyomo.dae.flatten import flatten_dae_components

from idaes.core import (
FlowsheetBlock,
MaterialBalanceType,
EnergyBalanceType,
MomentumBalanceType,
)
from idaes.core.util.model_statistics import (
degrees_of_freedom,
activated_equalities_generator,
unfixed_variables_generator,
)
from idaes.core.util.initialization import initialize_by_time_element
from idaes.core.util.exceptions import ConfigurationError
from idaes.apps.caprese.tests.test_simple_model import (
make_model,
make_small_model,
@@ -62,10 +46,7 @@
AlgVar,
InputVar,
FixedVar,
MeasuredVar,
)
import idaes.logger as idaeslog
import random
import pytest

__author__ = "Robert Parker"
@@ -234,7 +215,7 @@ def dynamic_block_rule(b, i):
# Won't be obvious that these attrs need to be set if
# constructing from a rule
b.mod = model
super(_BlockData, b).__setattr__("time", time)
super(BlockData, b).__setattr__("time", time)
b._inputs = inputs
b._measurements = measurements

4 changes: 2 additions & 2 deletions idaes/apps/caprese/util.py
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
from pyomo.dae.set_utils import is_in_block_indexed_by
from pyomo.core.expr.visitor import identify_variables
from pyomo.core.base.constraint import _ConstraintData
from pyomo.core.base.block import _BlockData
from pyomo.core.base.block import BlockData

from idaes.core.util.model_statistics import degrees_of_freedom
from idaes.core.util.dyn_utils import (
@@ -189,7 +189,7 @@ def initialize_by_element_in_range(
if not time_linking_vars:
if isinstance(comp, _ConstraintData):
con_list.append(comp)
elif isinstance(comp, _BlockData):
elif isinstance(comp, BlockData):
# Active here should be independent of whether block
# was active
con_list.extend(
6 changes: 3 additions & 3 deletions idaes/core/base/process_base.py
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@

from pandas import DataFrame

from pyomo.core.base.block import _BlockData
from pyomo.core.base.block import BlockData
from pyomo.common.formatting import tabular_writer
from pyomo.environ import Block
from pyomo.gdp import Disjunct
@@ -75,7 +75,7 @@ class MaterialFlowBasis(Enum):


@declare_process_block_class("ProcessBaseBlock")
class ProcessBlockData(_BlockData):
class ProcessBlockData(BlockData):
"""
Base class for most IDAES process models and classes.

@@ -97,7 +97,7 @@ def __init__(self, component):
Initialize a ProcessBlockData object.

Args:
component(Block): container Block instance to which this _BlockData
component(Block): container Block instance to which this BlockData
belongs.

Returns:
26 changes: 13 additions & 13 deletions idaes/core/base/tests/test_control_volume_1d.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
from pyomo.util.check_units import assert_units_consistent, assert_units_equivalent
from pyomo.dae import ContinuousSet, DerivativeVar
from pyomo.common.config import ConfigBlock
from pyomo.core.base.constraint import _GeneralConstraintData
from pyomo.core.base.constraint import ConstraintData
from idaes.core import (
ControlVolume1DBlock,
FlowsheetBlockData,
@@ -1125,7 +1125,7 @@ def test_add_material_balances_default():
for j in m.fs.pp.component_list:
with pytest.raises(KeyError):
assert m.fs.cv.material_balances[0, 0, p, j]
assert type(m.fs.cv.material_balances[0, 1, p, j]) is _GeneralConstraintData
assert type(m.fs.cv.material_balances[0, 1, p, j]) is ConstraintData

assert_units_consistent(m)

@@ -1310,7 +1310,7 @@ def test_add_phase_component_balances_default():
for j in m.fs.pp.component_list:
with pytest.raises(KeyError):
assert m.fs.cv.material_balances[0, 0, p, j]
assert type(m.fs.cv.material_balances[0, 1, p, j]) is _GeneralConstraintData
assert type(m.fs.cv.material_balances[0, 1, p, j]) is ConstraintData

assert_units_consistent(m)

@@ -1342,7 +1342,7 @@ def test_add_phase_component_balances_default_FFD():
for j in m.fs.pp.component_list:
with pytest.raises(KeyError):
assert m.fs.cv.material_balances[0, 1, p, j]
assert type(m.fs.cv.material_balances[0, 0, p, j]) is _GeneralConstraintData
assert type(m.fs.cv.material_balances[0, 0, p, j]) is ConstraintData

assert_units_consistent(m)

@@ -1988,7 +1988,7 @@ def test_add_total_component_balances_default():
for j in m.fs.pp.component_list:
with pytest.raises(KeyError):
assert m.fs.cv.material_balances[0, 0, j]
assert type(m.fs.cv.material_balances[0, 1, j]) is _GeneralConstraintData
assert type(m.fs.cv.material_balances[0, 1, j]) is ConstraintData

assert_units_consistent(m)

@@ -2020,7 +2020,7 @@ def test_add_total_component_balances_default_FFD():
for j in m.fs.pp.component_list:
with pytest.raises(KeyError):
assert m.fs.cv.material_balances[0, 1, j]
assert type(m.fs.cv.material_balances[0, 0, j]) is _GeneralConstraintData
assert type(m.fs.cv.material_balances[0, 0, j]) is ConstraintData

assert_units_consistent(m)

@@ -2658,7 +2658,7 @@ def test_add_total_element_balances_default():
for j in m.fs.pp.element_list:
with pytest.raises(KeyError):
assert m.fs.cv.element_balances[0, 0, j]
assert type(m.fs.cv.element_balances[0, 1, j]) is _GeneralConstraintData
assert type(m.fs.cv.element_balances[0, 1, j]) is ConstraintData

assert_units_consistent(m)

@@ -2690,7 +2690,7 @@ def test_add_total_element_balances_default_FFD():
for j in m.fs.pp.element_list:
with pytest.raises(KeyError):
assert m.fs.cv.element_balances[0, 1, j]
assert type(m.fs.cv.element_balances[0, 0, j]) is _GeneralConstraintData
assert type(m.fs.cv.element_balances[0, 0, j]) is ConstraintData

assert_units_consistent(m)

@@ -3036,7 +3036,7 @@ def test_add_energy_balances_default():

with pytest.raises(KeyError):
assert m.fs.cv.enthalpy_balances[0, 0]
assert type(m.fs.cv.enthalpy_balances[0, 1]) is _GeneralConstraintData
assert type(m.fs.cv.enthalpy_balances[0, 1]) is ConstraintData

assert_units_consistent(m)

@@ -3072,7 +3072,7 @@ def test_add_total_enthalpy_balances_default():

with pytest.raises(KeyError):
assert m.fs.cv.enthalpy_balances[0, 0]
assert type(m.fs.cv.enthalpy_balances[0, 1]) is _GeneralConstraintData
assert type(m.fs.cv.enthalpy_balances[0, 1]) is ConstraintData

assert_units_consistent(m)

@@ -3106,7 +3106,7 @@ def test_add_total_enthalpy_balances_default_FFD():

with pytest.raises(KeyError):
assert m.fs.cv.enthalpy_balances[0, 1]
assert type(m.fs.cv.enthalpy_balances[0, 0]) is _GeneralConstraintData
assert type(m.fs.cv.enthalpy_balances[0, 0]) is ConstraintData

assert_units_consistent(m)

@@ -3468,7 +3468,7 @@ def test_add_total_pressure_balances_default():

with pytest.raises(KeyError):
assert m.fs.cv.pressure_balance[0, 0]
assert type(m.fs.cv.pressure_balance[0, 1]) is _GeneralConstraintData
assert type(m.fs.cv.pressure_balance[0, 1]) is ConstraintData

assert_units_consistent(m)

@@ -3501,7 +3501,7 @@ def test_add_total_pressure_balances_default_FFD():

with pytest.raises(KeyError):
assert m.fs.cv.pressure_balance[0, 1]
assert type(m.fs.cv.pressure_balance[0, 0]) is _GeneralConstraintData
assert type(m.fs.cv.pressure_balance[0, 0]) is ConstraintData

assert_units_consistent(m)

4 changes: 2 additions & 2 deletions idaes/core/surrogate/surrogate_block.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

import collections

from pyomo.core.base.block import declare_custom_block, _BlockData
from pyomo.core.base.block import declare_custom_block, BlockData
from pyomo.environ import Var, Set

import idaes.logger as idaeslog
@@ -26,7 +26,7 @@


@declare_custom_block(name="SurrogateBlock")
class SurrogateBlockData(_BlockData):
class SurrogateBlockData(BlockData):
# pylint: disable=W0235
def __init__(self, component):
"""
6 changes: 3 additions & 3 deletions idaes/core/util/expr_doc.py
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
from pyomo.environ import ExternalFunction, Var, Expression, value, units as pu
from pyomo.core.base.constraint import _ConstraintData, Constraint
from pyomo.core.base.expression import _ExpressionData
from pyomo.core.base.block import _BlockData
from pyomo.core.base.block import BlockData
from pyomo.core.expr.visitor import StreamBasedExpressionVisitor
from pyomo.core.expr.numeric_expr import ExternalFunctionExpression
from pyomo.core import expr as EXPR, native_types
@@ -308,7 +308,7 @@ def document_constraints(

Args:
comp: A Pyomo component to document in {_ConstraintData, _ExpressionData,
_BlockData}.
BlockData}.
doc: True adds a documentation table for each constraint or expression.
Due to the way symbols are semi-automatiaclly generated, the
exact symbol definitions may be unique to each constraint or
@@ -357,7 +357,7 @@ def document_constraints(
s = rf"$${comp.lower} \le {latex_expr} \le {comp.upper}$$"
if doc:
s += f"\n{d['where']}"
elif isinstance(comp, _BlockData):
elif isinstance(comp, BlockData):
cs = []
for c in comp.component_data_objects(Constraint, descend_into=descend_into):
if not c.active:
18 changes: 9 additions & 9 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@
NPV_UnaryFunctionExpression,
NumericExpression,
)
from pyomo.core.base.block import _BlockData
from pyomo.core.base.block import BlockData
from pyomo.core.base.var import _GeneralVarData, _VarData
from pyomo.core.base.constraint import _ConstraintData
from pyomo.repn.standard_repn import ( # pylint: disable=no-name-in-module
@@ -427,10 +427,10 @@ class DiagnosticsToolbox:

"""

def __init__(self, model: _BlockData, **kwargs):
def __init__(self, model: BlockData, **kwargs):
# TODO: In future may want to generalise this to accept indexed blocks
# However, for now some of the tools do not support indexed blocks
if not isinstance(model, _BlockData):
if not isinstance(model, BlockData):
raise TypeError(
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
@@ -1541,10 +1541,10 @@ class SVDToolbox:

"""

def __init__(self, model: _BlockData, **kwargs):
def __init__(self, model: BlockData, **kwargs):
# TODO: In future may want to generalise this to accept indexed blocks
# However, for now some of the tools do not support indexed blocks
if not isinstance(model, _BlockData):
if not isinstance(model, BlockData):
raise TypeError(
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
@@ -1986,7 +1986,7 @@ class DegeneracyHunter2:
def __init__(self, model, **kwargs):
# TODO: In future may want to generalise this to accept indexed blocks
# However, for now some of the tools do not support indexed blocks
if not isinstance(model, _BlockData):
if not isinstance(model, BlockData):
raise TypeError(
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
@@ -3086,7 +3086,7 @@ class IpoptConvergenceAnalysis:
def __init__(self, model, **kwargs):
# TODO: In future may want to generalise this to accept indexed blocks
# However, for now some of the tools do not support indexed blocks
if not isinstance(model, _BlockData):
if not isinstance(model, BlockData):
raise TypeError(
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
@@ -3535,7 +3535,7 @@ def set_bounds_from_valid_range(component, descend_into=True):
if component.is_indexed():
for k in component:
set_bounds_from_valid_range(component[k])
elif isinstance(component, _BlockData):
elif isinstance(component, BlockData):
for i in component.component_data_objects(
ctype=[Var, Param], descend_into=descend_into
):
@@ -3576,7 +3576,7 @@ def list_components_with_values_outside_valid_range(component, descend_into=True
comp_list.extend(
list_components_with_values_outside_valid_range(component[k])
)
elif isinstance(component, _BlockData):
elif isinstance(component, BlockData):
for i in component.component_data_objects(
ctype=[Var, Param], descend_into=descend_into
):
Loading
Oops, something went wrong.