当前位置: 首页>>代码示例>>Python>>正文


Python ExprEvaluator.name_changed方法代码示例

本文整理汇总了Python中openmdao.main.expreval.ExprEvaluator.name_changed方法的典型用法代码示例。如果您正苦于以下问题:Python ExprEvaluator.name_changed方法的具体用法?Python ExprEvaluator.name_changed怎么用?Python ExprEvaluator.name_changed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openmdao.main.expreval.ExprEvaluator的用法示例。


在下文中一共展示了ExprEvaluator.name_changed方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Constraint

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import name_changed [as 别名]

#.........这里部分代码省略.........
        else:
            first = self.lhs.text
            second = self.rhs.text

        first_zero = False
        try:
            f = float(first)
        except Exception:
            pass
        else:
            if f == 0:
                first_zero = True

        second_zero = False
        try:
            f = float(second)
        except Exception:
            pass
        else:
            if f == 0:
                second_zero = True

        if first_zero:
            newexpr = "-(%s)" % second
        elif second_zero:
            newexpr = first
        else:
            newexpr = '%s-(%s)' % (first, second)

        return ExprEvaluator(newexpr, scope)

    def copy(self):
        """ Returns a copy of our self. """
        return Constraint(str(self.lhs), self.comparator, str(self.rhs),
                          scope=self.lhs.scope, jacs=self.jacs)

    def evaluate(self, scope):
        """Returns the value of the constraint as a sequence."""
        vname = self.pcomp_name + '.out0'
        try:
            system = getattr(scope,self.pcomp_name)._system
            info = system.vec['u']._info[scope.name2collapsed[vname]]
            # if a pseudocomp output is marked as hidden, that means that
            # it's really a residual, but it's mapped in the vector to
            # the corresponding state, so don't pull that value because
            # we want the actual residual value
            if info.hide: # it's a residual so pull from f vector
                return -system.vec['f'][scope.name2collapsed[vname]]
            else:
                return info.view
        except (KeyError, AttributeError):
            pass

        val = getattr(scope, self.pcomp_name).out0

        if isinstance(val, ndarray):
            return val.flatten()
        else:
            return [val]

    def get_referenced_compnames(self):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        if isinstance(self.rhs, float):
            return self.lhs.get_referenced_compnames()
        else:
            return self.lhs.get_referenced_compnames().union(
                                            self.rhs.get_referenced_compnames())

    def get_referenced_varpaths(self, copy=True, refs=False):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        if isinstance(self.rhs, float):
            return self.lhs.get_referenced_varpaths(copy=copy, refs=refs)
        else:
            return self.lhs.get_referenced_varpaths(copy=copy, refs=refs).union(
                    self.rhs.get_referenced_varpaths(copy=copy, refs=refs))

    def check_resolve(self):
        """Returns True if this constraint has no unresolved references."""
        return self.lhs.check_resolve() and self.rhs.check_resolve()

    def get_unresolved(self):
        return list(set(self.lhs.get_unresolved()).union(self.rhs.get_unresolved()))

    def name_changed(self, old, new):
        """Update expressions if necessary when an object is renamed."""
        self.rhs.name_changed(old, new)
        self.lhs.name_changed(old, new)

    def __str__(self):
        return ' '.join((str(self.lhs), self.comparator, str(self.rhs)))

    def __eq__(self, other):
        if not isinstance(other, Constraint):
            return False
        return (self.lhs, self.comparator, self.rhs) == \
               (other.lhs, other.comparator, other.rhs)
开发者ID:ChanChiChoi,项目名称:OpenMDAO-Framework,代码行数:104,代码来源:hasconstraints.py

示例2: Constraint2Sided

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import name_changed [as 别名]
class Constraint2Sided(Constraint):
    """ Object that stores info for a double-sided constraint. """

    def __init__(self, lhs, center, rhs, comparator, scope, jacs=None):
        self.lhs = ExprEvaluator(lhs, scope=scope)
        unresolved_vars = self.lhs.get_unresolved()

        self._pseudo = None
        self.pcomp_name = None

        if unresolved_vars:
            msg = "Left hand side of constraint '{0}' has invalid variables {1}"
            expression = ' '.join((lhs, comparator, center, comparator,
                                   rhs))

            raise ExprEvaluator._invalid_expression_error(unresolved_vars,
                                                          expr=expression,
                                                          msg=msg)
        self.center = ExprEvaluator(center, scope=scope)
        unresolved_vars = self.center.get_unresolved()

        if unresolved_vars:
            msg = "Center of constraint '{0}' has invalid variables {1}"
            expression = ' '.join((lhs, comparator, center, comparator,
                                   rhs))

            raise ExprEvaluator._invalid_expression_error(unresolved_vars,
                                                          expr=expression,
                                                          msg=msg)
        self.rhs = ExprEvaluator(rhs, scope=scope)
        unresolved_vars = self.rhs.get_unresolved()

        if unresolved_vars:
            msg = "Right hand side of constraint '{0}' has invalid variables {1}"
            expression = ' '.join((lhs, comparator, center, comparator,
                                   rhs))

            raise ExprEvaluator._invalid_expression_error(unresolved_vars,
                                                          expr=expression,
                                                          msg=msg)
        self.comparator = comparator
        self._size = None

        # Linear flag: constraints are nonlinear by default
        self.linear = False

        self.low = self.lhs.evaluate()
        self.high = self.rhs.evaluate()

        # User-defined jacobian function
        self.jacs = jacs

        self._create_pseudo()

    def _create_pseudo(self):
        """Create our pseudo component."""
        scope = self.lhs.scope
        refs = list(self.center.ordered_refs())
        pseudo_class = PseudoComponent

        # look for a<var1<b
        if len(refs) == 1 and self.center.text == refs[0]:
            pseudo_class = SimpleEQ0PComp

        self._pseudo = pseudo_class(scope,
                                    self.center,
                                    pseudo_type='constraint',
                                    subtype='inequality',
                                    exprobject=self)

        self.pcomp_name = self._pseudo.name

    def _combined_expr(self):
        """Only need the center expression
        """
        return self.center

    def copy(self):
        """ Returns a copy of our self. """
        return Constraint2Sided(str(self.lhs), str(self.center), str(self.rhs),
                          self.comparator, scope=self.lhs.scope,
                          jacs=self.jacs)

    def get_referenced_compnames(self):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        return self.center.get_referenced_compnames()

    def get_referenced_varpaths(self, copy=True, refs=False):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        return self.center.get_referenced_varpaths(copy=copy, refs=refs)

    def name_changed(self, old, new):
        """Update expressions if necessary when an object is renamed."""
        self.rhs.name_changed(old, new)
        self.lhs.name_changed(old, new)
        self.center.name_changed(old, new)
#.........这里部分代码省略.........
开发者ID:ChanChiChoi,项目名称:OpenMDAO-Framework,代码行数:103,代码来源:hasconstraints.py


注:本文中的openmdao.main.expreval.ExprEvaluator.name_changed方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。