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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .pylint/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ disable=no-self-argument,
arguments-differ,
abstract-method,
arguments-renamed,
unused-argument, # w have cases where a general high level method passes arguments that simpler methods do not need
unused-argument, # we have cases where a general high level method passes arguments that simpler methods do not need
redefined-outer-name,
anomalous-backslash-in-string,
unspecified-encoding,
Expand All @@ -29,6 +29,7 @@ disable=no-self-argument,
too-many-lines,
use-implicit-booleaness-not-comparison, # implicit booleaness can be hard to follow
consider-using-f-string, # should fix, but a lot of effort
no-member, # Triggers many false positives from Pyomo due to diamond structure

[IMPORTS]
ignored-modules=cvalsim,almsim
Expand Down
2 changes: 1 addition & 1 deletion docs/reference_guides/core/var_like_expr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ In these cases, it is possible that a user might mistake the `Expression` for a
.. autoclass:: IndexedVarLikeExpression
:members:

.. autoclass:: _GeneralVarLikeExpressionData
.. autoclass:: VarLikeExpressionData
:members:

6 changes: 3 additions & 3 deletions idaes/apps/caprese/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""
import enum
from pyomo.environ import SolverFactory
from pyomo.core.base.var import _GeneralVarData
from pyomo.core.base.var import VarData


class ControlInitOption(enum.Enum):
Expand Down Expand Up @@ -94,7 +94,7 @@ def validate_list_of_vardata(varlist):
if not isinstance(varlist, list):
raise TypeError("Not a list of VarData")
for var in varlist:
if not isinstance(var, _GeneralVarData):
if not isinstance(var, VarData):
raise TypeError("Not a list of VarData")
return varlist

Expand All @@ -107,7 +107,7 @@ def validate_list_of_vardata_value_tuples(varvaluelist):
raise TypeError("Item in list is not a tuple")
if not len(item) == 2:
raise ValueError("Tuple in list does not have correct length")
if not isinstance(item[0], _GeneralVarData):
if not isinstance(item[0], VarData):
raise TypeError("First entry is not a VarData")
item = (item[0], float(item[1]))
return varvaluelist
Expand Down
13 changes: 1 addition & 12 deletions idaes/apps/caprese/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@
""" Block-like object meant for controller models.
"""

import idaes.logger as idaeslog
from idaes.apps.caprese.util import initialize_by_element_in_range
from idaes.apps.caprese.common.config import (
ControlPenaltyType,
)
from idaes.apps.caprese.common.config import VariableCategory as VC
from idaes.apps.caprese.categorize import (
categorize_dae_variables,
CATEGORY_TYPE_MAP,
)

from idaes.apps.caprese.nmpc_var import (
NmpcVar,
DiffVar,
AlgVar,
InputVar,
Expand All @@ -35,22 +29,17 @@
)
from idaes.apps.caprese.dynamic_block import (
_DynamicBlockData,
IndexedDynamicBlock,
DynamicBlock,
)
from idaes.core.util.model_statistics import degrees_of_freedom

from pyomo.environ import (
Objective,
check_optimal_termination,
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


Expand Down
9 changes: 4 additions & 5 deletions idaes/apps/caprese/dynamic_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 2 additions & 21 deletions idaes/apps/caprese/tests/test_dynamic_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -62,10 +46,7 @@
AlgVar,
InputVar,
FixedVar,
MeasuredVar,
)
import idaes.logger as idaeslog
import random
import pytest

__author__ = "Robert Parker"
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions idaes/apps/caprese/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from pyomo.dae.flatten import flatten_dae_components
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.constraint import ConstraintData
from pyomo.core.base.block import BlockData

from idaes.core.util.model_statistics import degrees_of_freedom
from idaes.core.util.dyn_utils import (
Expand Down Expand Up @@ -187,9 +187,9 @@ def initialize_by_element_in_range(
if was_originally_active[id(comp)]:
comp.activate()
if not time_linking_vars:
if isinstance(comp, _ConstraintData):
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(
Expand Down
10 changes: 5 additions & 5 deletions idaes/apps/matopt/opt/pyomo_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logging = getModelLogger("MatOptModel")

from pyomo.environ import *
from pyomo.core.base.var import _GeneralVarData
from pyomo.core.base.var import VarData
from pyomo.core.expr.numeric_expr import (
MonomialTermExpression,
SumExpression,
Expand Down Expand Up @@ -43,13 +43,13 @@ def getLB(e):
"""
# Future work: use Pyomo function to achieve this functionality
# return compute_bounds_on_expr(e)[0]
if isinstance(e, _GeneralVarData):
if isinstance(e, VarData):
if e.is_fixed():
return value(e)
else:
return e.lb
elif isinstance(e, MonomialTermExpression):
assert isinstance(e.args[1], pyomo.core.base.var._GeneralVarData), (
assert isinstance(e.args[1], pyomo.core.base.var.VarData), (
"This code relies on the assumption that the only variable "
"in a monomial expression is the second argument"
)
Expand Down Expand Up @@ -93,13 +93,13 @@ def getUB(e):
"""
# Future work: use Pyomo function to achieve this functionality
# return compute_bounds_on_expr(e)[1]
if isinstance(e, _GeneralVarData):
if isinstance(e, VarData):
if e.is_fixed():
return value(e)
else:
return e.ub
elif isinstance(e, MonomialTermExpression):
assert isinstance(e.args[1], pyomo.core.base.var._GeneralVarData), (
assert isinstance(e.args[1], pyomo.core.base.var.VarData), (
"This code relies on the assumption that the only variable "
"in a monomial expression is the second argument"
)
Expand Down
6 changes: 3 additions & 3 deletions idaes/core/base/process_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions idaes/core/base/property_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

# Import Pyomo libraries
from pyomo.environ import Set, value, Var, Expression, Constraint, Reference
from pyomo.core.base.var import _VarData
from pyomo.core.base.expression import _ExpressionData
from pyomo.core.base.var import VarData
from pyomo.core.base.expression import ExpressionData
from pyomo.common.config import ConfigBlock, ConfigValue, Bool
from pyomo.common.formatting import tabular_writer
from pyomo.network import Port
Expand Down Expand Up @@ -399,7 +399,7 @@ def report(self, index=(0), true_state=False, dof=False, ostream=None, prefix=""
ostream.write("\n" + "-" * max_str_length + "\n")
ostream.write(f"{prefix}{tab}State Report")

if any(isinstance(v, _VarData) for k, v in stream_attributes.items()):
if any(isinstance(v, VarData) for k, v in stream_attributes.items()):
ostream.write("\n" * 2)
ostream.write(f"{prefix}{tab}Variables: \n\n")
tabular_writer(
Expand All @@ -408,13 +408,13 @@ def report(self, index=(0), true_state=False, dof=False, ostream=None, prefix=""
(
(k, v)
for k, v in stream_attributes.items()
if isinstance(v, _VarData)
if isinstance(v, VarData)
),
("Value", "Fixed", "Bounds"),
lambda k, v: ["{:#.5g}".format(value(v)), v.fixed, v.bounds],
)

if any(isinstance(v, _ExpressionData) for k, v in stream_attributes.items()):
if any(isinstance(v, ExpressionData) for k, v in stream_attributes.items()):
ostream.write("\n" * 2)
ostream.write(f"{prefix}{tab}Expressions: \n\n")
tabular_writer(
Expand All @@ -423,7 +423,7 @@ def report(self, index=(0), true_state=False, dof=False, ostream=None, prefix=""
(
(k, v)
for k, v in stream_attributes.items()
if isinstance(v, _ExpressionData)
if isinstance(v, ExpressionData)
),
("Value",),
lambda k, v: ["{:#.5g}".format(value(v))],
Expand Down
Loading
Loading