Beispiel #1
0
 def setUp(self):
     self.top = set_as_top(Assembly())
     self.top.add('driver', SLSQPdriver())
     self.top.add('comp', OptRosenSuzukiComponent())
     self.top.driver.workflow.add('comp')
     self.top.driver.iprint = 0
     self.top.driver.differentiator = FiniteDifference()
 def configure(self):
     self.add('comp1', MyComp())
     self.add('comp2', MyComp())
     driver = self.add('driver', SLSQPdriver())
     driver.add_parameter('comp1.x', low=-10, high=[10, 10])
     driver.add_parameter('comp2.x', low=-10, high=10)
     driver.add_objective('comp1.y[0] + comp2.y[1]')
    def test_cascade_opt(self):

        raise SkipTest(
            "We currently don't allow a component instance in multiple workflows."
        )

        top = set_as_top(Assembly())

        eq = ['f = (x-3)**2 + x*y + (y+4)**2 - 3']
        deriv = ['df_dx = 2.0*x - 6.0 + y', 'df_dy = 2.0*y + 8.0 + x']
        top.add('comp', ExecCompWithDerivatives(eq, deriv))
        top.add('driver', SimpleDriver())
        top.add('opt1', SLSQPdriver())
        top.add('opt2', SLSQPdriver())

        top.opt1.workflow.add(['comp'])
        top.opt2.workflow.add(['comp'])
        top.driver.workflow.add(['opt1', 'opt2'])

        top.opt1.add_parameter('comp.x', low=-100, high=100)
        top.opt1.add_parameter('comp.y', low=-100, high=100)
        top.opt1.add_objective('comp.f')
        top.opt1.maxiter = 2
        top.opt2.add_parameter('comp.x', low=-100, high=100)
        top.opt2.add_parameter('comp.y', low=-100, high=100)
        top.opt2.add_objective('comp.f')
        top.opt2.maxiter = 50

        top.run()

        assert_rel_error(self, top.comp.x, 6.666309, 0.01)
        assert_rel_error(self, top.comp.y, -7.333026, 0.01)

        J = top.driver.calc_gradient(inputs=['comp.x', 'comp.y'],
                                     outputs=['comp.f'])
        edges = top.driver.workflow._edges
        print edges
        self.assertEqual(set(edges['@in0']),
                         set(['~opt1.comp|x', '~opt2.comp|x']))
        self.assertEqual(set(edges['@in1']),
                         set(['~opt1.comp|y', '~opt2.comp|y']))
        self.assertEqual(set(edges['~opt1.comp|f']), set(['@out0']))
        self.assertEqual(set(edges['~opt2.comp|f']), set(['@out0']))
Beispiel #4
0
 def test_replace2(self):
     top = set_as_top(Simulation())
     top.replace('preproc', ScalingPreProc())
     for comp in top.driver.workflow:
         n = comp.name
     top.replace('postproc', ScalingPostProc())
     top.replace('driver', SLSQPdriver())
     for comp in top.driver.workflow:
         n = comp.name
     top.replace('comp', Assembly())
     for comp in top.driver.workflow:
         n = comp.name
Beispiel #5
0
    def configure(self):
        """ Creates a new Assembly containing a chain of Paraboloid components"""

        self.add("paraboloid", Paraboloid())
        self.add("optimizer", SLSQPdriver())

        self.optimizer.add_parameter("paraboloid.x", low='-50', high='50')
        self.optimizer.add_parameter("paraboloid.y", low='-50', high='50')

        self.optimizer.add_objective("paraboloid.f_xy")

        self.optimizer.workflow.add('paraboloid')
    def test_ActuatorDisk(self):
        self.assertEqual(self.top.get('ad.Cp'), 0.)

        self.top.run()

        self.assertEqual(self.top.get('ad.Cp'), 0.5)

        self.top.replace('driver', SLSQPdriver())
        self.top.driver.add_parameter('ad.a', low=0, high=1)
        self.top.driver.add_objective('-ad.Cp')

        self.top.run()

        assert_rel_error(self, self.top.ad.a, 0.333, 0.005)
        assert_rel_error(self, self.top.ad.Cp, 0.593, 0.005)  # Betz Limit
    def test_AutoBEM_Opt(self):
        # perform optimization
        self.top.replace('driver', SLSQPdriver())

        self.top.driver.add_parameter('b.chord_hub', low=.1, high=2)
        self.top.driver.add_parameter('b.chord_tip', low=.1, high=2)
        self.top.driver.add_parameter('b.rpm', low=20, high=300)
        self.top.driver.add_parameter('b.twist_hub', low=-5, high=50)
        self.top.driver.add_parameter('b.twist_tip', low=-5, high=50)
        # self.top.driver.add_parameter('b.r_tip',     low=1, high=10)

        self.top.driver.add_objective('-b.data.Cp')

        self.top.run()

        assert_rel_error(self, self.top.b.data.Cp, 0.57, 0.01)
Beispiel #8
0
 def test_replace2(self):
     top = set_as_top(Simulation())
     top.replace('preproc', ScalingPreProc())
     top.get_attributes(False)
     top.preproc.get_attributes(False)
     for comp in top.driver.workflow:
         n = comp.name
     top.replace('postproc', ScalingPostProc())
     top.postproc.get_attributes(False)
     top.replace('driver', SLSQPdriver())
     top.driver.get_attributes(False)
     for comp in top.driver.workflow:
         n = comp.name
     top.replace('comp', Assembly())
     top.driver.get_attributes(False)
     top.comp.get_attributes(False)
     top.get_attributes(False)
     top.get_dataflow()
     for comp in top.driver.workflow:
         n = comp.name
Beispiel #9
0
    def test_initial_run(self):
        # Test the fix that added a run_iteration top of the
        #   start_iteration method
        class MyComp(Component):

            x = Float(0.0, iotype='in', low=-10, high=10)
            xx = Float(0.0, iotype='in', low=-10, high=10)
            f_x = Float(iotype='out')
            y = Float(iotype='out')

            def execute(self):
                if self.xx != 1.0:
                    self.raise_exception("Lazy", RuntimeError)
                self.f_x = 2.0*self.x
                self.y = self.x

        @add_delegate(HasParameters)
        class SpecialDriver(Driver):

            implements(IHasParameters)

            def execute(self):
                self.set_parameters([1.0])

        top = set_as_top(Assembly())
        top.add('comp', MyComp())
        top.add('driver', SLSQPdriver())
        top.add('subdriver', SpecialDriver())
        top.driver.workflow.add('subdriver')
        top.subdriver.workflow.add('comp')

        top.subdriver.add_parameter('comp.xx')
        top.driver.add_parameter('comp.x')
        top.driver.add_constraint('comp.y > 1.0')
        top.driver.add_objective('comp.f_x')

        top.run()
            'comp.x[0]**2+comp.x[0]+comp.x[1]**2-comp.x[1]+comp.x[2]**2+comp.x[2]+comp.x[3]**2-comp.x[3] < 8',
            'comp.x[0]**2-comp.x[0]+2*comp.x[1]**2+comp.x[2]**2+2*comp.x[3]**2-comp.x[3] < 10',
            '2*comp.x[0]**2+2*comp.x[0]+comp.x[1]**2-comp.x[1]+comp.x[2]**2-comp.x[3] < 5'])
        self.recorders = [ListCaseRecorder()]


class NestedSimulation(Assembly):
    def configure(self):
        self.add('sim', Simulation())
        self.driver.workflow.add('sim')


if __name__ == '__main__':
    sim = set_as_top(Simulation())
    flow = sim.get_dataflow()
    for item in flow['connections']:
        print 'connection:', item
    for item in flow['parameters']:
        print 'parameter:', item
    for item in flow['constraints']:
        print 'constraint:', item
    for item in flow['objectives']:
        print 'objective:', item
    sim.run()
    print 'objective', sim.comp.opt_objective, sim.driver.eval_objective()
    for i in range(len(sim.preproc.x_in)):
        print 'design_var', i, sim.comp.opt_design_vars[i], sim.preproc.x_in[i]

    sim.replace('preproc', ScalingPreProc())
    sim.replace('driver', SLSQPdriver())
 def configure(self):
     self.add('paraboloid', ArrayParaboloid())
     driver = self.add('driver', SLSQPdriver())
     driver.add_objective('paraboloid.f_x')
     driver.workflow.add('paraboloid')