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


Python ExprEvaluator.set方法代码示例

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


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

示例1: test_reparse_on_scope_change

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import set [as 别名]
    def test_reparse_on_scope_change(self):
        self.top.comp.x = 99.5
        self.top.comp.y = -3.14

        ex = ExprEvaluator('comp.x', self.top)
        self.assertEqual(99.5, ex.evaluate())
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.scope = self.top.a
        try:
            ex.set(0.5)
        except AttributeError as err:
            self.assertEqual(str(err), "a: object has no attribute 'comp.x'")
        else:
            self.fail("AttributeError expected")
        self.assertEqual(new_text(ex), "scope.get('comp.x')")
        self.assertEqual(99.5, ex.evaluate(self.top)) # set scope back to self.top
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.text = 'comp.y'
        try:
            ex.evaluate(self.top.a)
        except AttributeError as err:
            self.assertEqual(str(err), "can't evaluate expression 'comp.y':"
                             " a: 'A' object has no attribute 'comp'")
        else:
            self.fail("AttributeError expected")
        ex.scope = self.top
        ex.set(11.1)
        self.assertEqual(11.1, self.top.comp.y)
        self.assertEqual(new_text(ex), "scope.get('comp.y')")
开发者ID:TwanvdBorgh,项目名称:OpenMDAO-Framework,代码行数:33,代码来源:test_evalexpr.py

示例2: test_multi_object

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import set [as 别名]
 def test_multi_object(self):
     # verify that expressions with multiple objects raise a reasonable error message
     # when a set is attempted.
     try:
         ex = ExprEvaluator('comp.x+comp.x', self.top)
         ex.set(1)
     except ValueError, err:
         self.assertEqual(str(err),
             "expression 'comp.x+comp.x' can't be set to a value")
开发者ID:TwanvdBorgh,项目名称:OpenMDAO-Framework,代码行数:11,代码来源:test_evalexpr.py

示例3: SimAcceleration

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import set [as 别名]
class SimAcceleration(Driver):
    """ Simulation of vehicle acceleration performance. This is a specialized
    simulation driver whose workflow should consist of a Vehicle assembly, and
    whose connections are as follows:
    
    Connection Inputs
    velocity_str: str
        Variable location for vehicle velocity.
    
    throttle_str: str
        Variable location for vehicle throttle position.
    
    gear_str: str
        Variable location for vehicle gear position.
    
    acceleration_str: str
        Variable location for vehicle acceleration.
    
    overspeed_str: str
        Variable location for vehicle overspeed.
    
    Simulation Inputs
    end_speed: float
        Ending speed for the simulation (default 60 mph)
    
    timestep: float
        Simulation time step (default .01)
        
    Outputs
    accel_time: float
        Time to perform the acceleration test.
    """
    
    velocity_str = Str(iotype='in',
                       desc='Location of vehicle input: velocity.')
    throttle_str = Str(iotype='in',
                       desc='Location of vehicle input: throttle.')
    gear_str = Str(iotype='in',
                       desc='Location of vehicle input: current_gear.')
    acceleration_str = Str(iotype='in',
                       desc='Location of vehicle output: acceleration.')
    overspeed_str = Str(iotype='in',
                       desc='Location of vehicle output: overspeed.')

    end_speed = Float(60.0, iotype='in', units='mi/h',
                      desc='Simulation final speed')
    timestep = Float(0.1, iotype='in', units='s', 
                     desc='Simulation time step size')
    
    accel_time = Float(0.0, iotype='out', units='s',
                       desc = 'Acceleration time')
    
    def __init__(self):
        super(SimAcceleration, self).__init__()
        
        self._velocity_str_expr = None
        self._throttle_str_expr = None
        self._gear_str_expr = None
        self._acceleration_str_expr = None
        self._overspeed_str_expr = None
    
    def _velocity_str_changed(self, oldval, newval):
        self._velocity_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _throttle_str_changed(self, oldval, newval):
        self._throttle_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _gear_str_changed(self, oldval, newval):
        self._gear_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _acceleration_str_changed(self, oldval, newval):
        self._acceleration_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _overspeed_str_changed(self, oldval, newval):
        self._overspeed_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def execute(self):
        """ Simulate the vehicle model at full throttle."""
        
        # Set initial throttle, gear, and velocity
        time = 0.0
        velocity = 0.0
        throttle = 1.0
        gear = 1
        
        while velocity < self.end_speed:
            
            self._velocity_str_expr.set(velocity)
            self._throttle_str_expr.set(throttle)
            self._gear_str_expr.set(gear)
            self.run_iteration()
            
            acceleration = self._acceleration_str_expr.evaluate(self.parent)
            overspeed = self._overspeed_str_expr.evaluate(self.parent)
            
            # If the next gear can produce more torque, let's shift.
            if gear < 5:
                self._gear_str_expr.set(gear+1)
                self.run_iteration()
            
#.........这里部分代码省略.........
开发者ID:JustinSGray,项目名称:OpenMDAO-Framework,代码行数:103,代码来源:driving_sim.py

示例4: SimEconomy

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

#.........这里部分代码省略.........
        self._throttle_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _gear_str_changed(self, oldval, newval):
        self._gear_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _acceleration_str_changed(self, oldval, newval):
        self._acceleration_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _fuel_burn_str_changed(self, oldval, newval):
        self._fuel_burn_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _overspeed_str_changed(self, oldval, newval):
        self._overspeed_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def _underspeed_str_changed(self, oldval, newval):
        self._underspeed_str_expr = ExprEvaluator(newval, scope=self.parent)
    
    def execute(self):
        """ Simulate the vehicle over a velocity profile."""
        
        # Set initial throttle, gear, and velocity
        throttle = 1.0
        gear = 1
        time1 = 0.0
        velocity1 = 0.0
        
        profile_stream = resource_stream('openmdao.examples.enginedesign',
                                         self.profilename)
        profile_reader = reader(profile_stream, delimiter=',')
        
        distance = 0.0
        fuelburn = 0.0
        
        self._gear_str_expr.set(gear)
        
        for row in profile_reader:
            
            time2 = float(row[0])
            velocity2 = float(row[1])
            converged = 0
            
            command_accel = (velocity2-velocity1)/(time2-time1)
            
            #------------------------------------------------------------
            # Choose the correct Gear
            #------------------------------------------------------------

            # First, if speed is less than 10 mph, put it in first gear.
            # Note: some funky gear ratios might not like this.
            # So, it's a hack for now.
            
            if velocity1 < self.shiftpoint1:
                gear = 1
                self._gear_str_expr.set(gear)
                
            # Find out min and max accel in current gear.
            
            throttle = self.throttle_min
            self._velocity_str_expr.set(velocity1)
            self._throttle_str_expr.set(throttle)
            gear = self._findgear(velocity1, throttle, gear)                    
            acceleration = self._acceleration_str_expr.evaluate(self.parent)
            accel_min = convert_units(acceleration, 'm/(s*s)', 'mi/(h*s)')
            
            # Upshift if commanded accel is less than closed-throttle accel
            # The net effect of this will often be a shift to a higher gear
开发者ID:JustinSGray,项目名称:OpenMDAO-Framework,代码行数:70,代码来源:driving_sim.py

示例5: test_set_evaluate

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import set [as 别名]
    def test_set_evaluate(self):
        ex = ExprEvaluator('comp.x', self.top)
        self.assertEqual(3.14, ex.evaluate())

        ex.set(75.4)
        self.assertEqual(75.4, self.top.comp.x)

        self.top.comp.contlist = [A(), A(), A()]
        self.top.comp.contlist[1].a1d = [4]*5
        ex = ExprEvaluator('comp.contlist[1].a1d[3]', self.top)
        self.assertEqual(ex.evaluate(), 4)

        ex.set(123)
        self.assertEqual(ex.evaluate(), 123)

        ex = ExprEvaluator("comp.contlist[1].some_funct(3,5,'sub')", self.top)
        self.assertEqual(ex.evaluate(), -2)

        ex = ExprEvaluator("comp.get_cont(1).some_funct(3,5,'add')", self.top)
        self.assertEqual(ex.evaluate(), 8)

        ex = ExprEvaluator("comp.get_cont(1).a1d[2]", self.top)
        self.assertEqual(ex.evaluate(), 4)

        ex = ExprEvaluator("a2d[1][0]", self.top.a)
        self.assertEqual(ex.evaluate(), 2.)
        ex.set(7.)
        self.assertEqual(self.top.a.a2d[1][0], 7.)

        ex = ExprEvaluator("a2d[1,0]", self.top.a)
        self.assertEqual(ex.evaluate(), 7.)
        ex.set(11.)
        self.assertEqual(self.top.a.a2d[1][0], 11.)

        ex = ExprEvaluator("a2d[1]", self.top.a)

        self.assertTrue(all(ex.evaluate() == array([11., 3.])))
        ex.set([0.1, 0.2])
        self.assertTrue(all(self.top.a.a2d[1] == array([0.1, 0.2])))


        self.top.comp.cont = A()

        ex = ExprEvaluator("comp.cont.a2d[1][0]", self.top)
        self.assertEqual(ex.evaluate(), 2.)
        ex.set(7.)
        self.assertEqual(self.top.comp.cont.a2d[1][0], 7.)

        ex = ExprEvaluator("comp.cont.a2d[1,0]", self.top)
        self.assertEqual(ex.evaluate(), 7.)
        ex.set(11.)
        self.assertEqual(self.top.comp.cont.a2d[1][0], 11.)

        # try a numpy function
        try:
            import numpy
        except ImportError:
            pass
        else:
            ex = ExprEvaluator("numpy.eye(2)", self.top.a)
            val = ex.evaluate()

            self.assertTrue((val == numpy.eye(2)).all())

        ex = ExprEvaluator("comp.get_cont(1).a1d", self.top)
        self.assertEqual(list(ex.evaluate()), [4, 4, 4, 123, 4])

        ex = ExprEvaluator("comp.get_attrib('get_cont')(1).a1d", self.top)
        self.assertEqual(list(ex.evaluate()), [4, 4, 4, 123, 4])
开发者ID:TwanvdBorgh,项目名称:OpenMDAO-Framework,代码行数:71,代码来源:test_evalexpr.py

示例6: test_set_evaluate

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import set [as 别名]
    def test_set_evaluate(self):
        ex = ExprEvaluator('comp.x', self.top)
        self.assertEqual(3.14, ex.evaluate())

        ex.set(75.4)
        self.assertEqual(75.4, self.top.comp.x)
        
        self.top.comp.contlist = [A(), A(), A()]
        self.top.comp.contlist[1].a1d = [4]*5
        ex = ExprEvaluator('comp.contlist[1].a1d[3]', self.top)
        self.assertEqual(ex.evaluate(), 4)
        
        ex.set(123)
        self.assertEqual(ex.evaluate(), 123)
        
        ex = ExprEvaluator("comp.contlist[1].some_funct(3,5,'sub')", self.top)
        self.assertEqual(ex.evaluate(), -2)
        
        ex = ExprEvaluator("comp.get_cont(1).some_funct(3,5,'add')", self.top)
        self.assertEqual(ex.evaluate(), 8)
        
        ex = ExprEvaluator("comp.get_cont(1).a1d[2]", self.top)
        self.assertEqual(ex.evaluate(), 4)
        
        ex = ExprEvaluator("a2d[1][0]", self.top.a)
        self.assertEqual(ex.evaluate(), 2.)
        ex.set(7.)
        self.assertEqual(self.top.a.a2d[1][0], 7.)
        
        ex = ExprEvaluator("a2d[1,0]", self.top.a)
        self.assertEqual(ex.evaluate(), 7.)
        ex.set(11.)
        self.assertEqual(self.top.a.a2d[1][0], 11.)
        
        ex = ExprEvaluator("a2d[1]", self.top.a)
        self.assertTrue(all(ex.evaluate() == numpy.array([11.,3.])))
        ex.set([0.1,0.2])
        self.assertTrue(all(self.top.a.a2d[1] == numpy.array([0.1,0.2])))
        
        self.top.comp.cont = A()
        
        ex = ExprEvaluator("comp.cont.a2d[1][0]", self.top)
        self.assertEqual(ex.evaluate(), 2.)
        ex.set(7.)
        self.assertEqual(self.top.comp.cont.a2d[1][0], 7.)
        
        ex = ExprEvaluator("comp.cont.a2d[1,0]", self.top)
        self.assertEqual(ex.evaluate(), 7.)
        ex.set(11.)
        self.assertEqual(self.top.comp.cont.a2d[1][0], 11.)
        
        ex = ExprEvaluator("comp.get_cont(1).a1d", self.top)
        self.assertTrue(all(ex.evaluate() == numpy.array([4,4,4,123,4])))
        
        ex = ExprEvaluator("comp.get_attr('get_cont')(1).a1d", self.top)
        self.assertTrue(all(ex.evaluate() == numpy.array([4,4,4,123,4])))
        
        # try an expression that's a simple assignment
        ex = ExprEvaluator("f = 10.333", self.top.a)
        self.assertEqual(ex.evaluate(), None)
        self.assertEqual(self.top.a.f, 10.333)
开发者ID:drousis,项目名称:OpenMDAO-Framework,代码行数:63,代码来源:test_evalexpr.py


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