示例#1
0
    def setup(self):
        self.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add_subsystem('pz',
                           IndepVarComp('z', np.array([5.0, 2.0])),
                           promotes=['z'])

        sub = self.add_subsystem('sub',
                                 Group(),
                                 promotes=[
                                     'x', 'z', 'y1', 'state_eq.y2_actual',
                                     'state_eq.y2_command', 'd1.y2', 'd2.y2'
                                 ])

        subgrp = sub.add_subsystem(
            'state_eq_group',
            Group(),
            promotes=['state_eq.y2_actual', 'state_eq.y2_command'])
        subgrp.add_subsystem('state_eq', StateConnection())

        sub.add_subsystem('d1',
                          SellarDis1withDerivatives(),
                          promotes=['x', 'z', 'y1'])
        sub.add_subsystem('d2',
                          SellarDis2withDerivatives(),
                          promotes=['z', 'y1'])

        self.connect('state_eq.y2_command', 'd1.y2')
        self.connect('d2.y2', 'state_eq.y2_actual')

        self.add_subsystem('obj_cmp',
                           ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                    z=np.array([0.0, 0.0]),
                                    x=0.0,
                                    y1=0.0,
                                    y2=0.0),
                           promotes=['x', 'z', 'y1', 'obj'])
        self.connect('d2.y2', 'obj_cmp.y2')

        self.add_subsystem('con_cmp1',
                           ExecComp('con1 = 3.16 - y1'),
                           promotes=['con1', 'y1'])
        self.add_subsystem('con_cmp2',
                           ExecComp('con2 = y2 - 24.0'),
                           promotes=['con2'])
        self.connect('d2.y2', 'con_cmp2.y2')

        nl = self.options['nonlinear_solver']
        self.nonlinear_solver = nl() if inspect.isclass(nl) else nl
        if self.options['nl_atol']:
            self.nonlinear_solver.options['atol'] = self.options['nl_atol']
        if self.options['nl_maxiter']:
            self.nonlinear_solver.options['maxiter'] = self.options[
                'nl_maxiter']

        ln = self.options['linear_solver']
        self.linear_solver = ln() if inspect.isclass(ln) else ln
        if self.options['ln_atol']:
            self.linear_solver.options['atol'] = self.options['ln_atol']
        if self.options['ln_maxiter']:
            self.linear_solver.options['maxiter'] = self.options['ln_maxiter']
示例#2
0
    def test_add(self):

        group = Group()
        comp = ExecComp('y=x*2.0')
        group.add('mycomp', comp)

        subs = list(group.subsystems())
        self.assertEqual(len(subs), 1)
        self.assertEqual(subs[0], comp)
        self.assertEqual(subs[0].name, 'mycomp')

        comp2 = ExecComp('y=x*2.0')
        group.add("nextcomp", comp2)

        subs = list(group.subsystems())
        self.assertEqual(len(subs), 2)
        self.assertEqual(subs[0], comp)
        self.assertEqual(subs[1], comp2)

        with self.assertRaises(RuntimeError) as cm:
            group.add('mycomp', comp)

        expected_msg = "Group '' already contains a subsystem with name 'mycomp'."

        self.assertEqual(str(cm.exception), expected_msg)
示例#3
0
    def __init__(self):
        super(Diamond, self).__init__()

        self.add_subsystem('iv', IndepVarComp('x', 2.0))

        self.add_subsystem('c1', ExecComp([
            'y1 = 2.0*x1**2',
            'y2 = 3.0*x1'
        ]))

        sub = self.add_subsystem('sub', ParallelGroup())
        sub.add_subsystem('c2', ExecComp('y1 = 0.5*x1'))
        sub.add_subsystem('c3', ExecComp('y1 = 3.5*x1'))

        self.add_subsystem('c4', ExecComp([
            'y1 = x1 + 2.0*x2',
            'y2 = 3.0*x1 - 5.0*x2'
        ]))

        # make connections
        self.connect('iv.x', 'c1.x1')

        self.connect('c1.y1', 'sub.c2.x1')
        self.connect('c1.y2', 'sub.c3.x1')

        self.connect('sub.c2.y1', 'c4.x1')
        self.connect('sub.c3.y1', 'c4.x2')
示例#4
0
    def __init__(self):
        super(SellarStateConnection, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['*'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*'])

        sub = self.add('sub', Group(), promotes=['*'])
        sub.ln_solver = ScipyGMRES()

        subgrp = sub.add('state_eq_group', Group(), promotes=['*'])
        subgrp.ln_solver = ScipyGMRES()
        subgrp.add('state_eq', StateConnection())

        sub.add('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1'])
        sub.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1'])

        self.connect('state_eq.y2_command', 'd1.y2')
        self.connect('d2.y2', 'state_eq.y2_actual')

        self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                     z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
                 promotes=['x', 'z', 'y1', 'obj'])
        self.connect('d2.y2', 'obj_cmp.y2')

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2'])
        self.connect('d2.y2', 'con_cmp2.y2')

        self.nl_solver = Newton()
示例#5
0
    def test_conflicting_promotions(self):
        # verify we get an error if we have conflicting promotions
        root = Group()

        # promoting G1.x will create an implicit connection to G3.x
        # this is a conflict because G3.x (aka G3.C4.x) is already connected
        # to G3.C3.x
        G2 = root.add('G2', Group())
        G2.add('C1', ParamComp('x', 5.), promotes=['x'])

        G1 = G2.add('G1', Group(), promotes=['x'])
        G1.add('C2', ExecComp('y=x*2.0'), promotes=['x'])

        G3 = root.add('G3', Group(), promotes=['x'])
        G3.add('C3', ExecComp('y=x*2.0'), promotes=['y'])  # promoting y
        G3.add('C4', ExecComp('y=x*2.0'),
               promotes=['x', 'y'])  # promoting y again.. BAD

        prob = Problem(root)

        try:
            prob.setup(check=False)
        except Exception as error:
            msg = "Promoted name 'G3.y' matches multiple unknowns: ['G3.C3.y', 'G3.C4.y']"
            self.assertEqual(text_type(error), msg)
        else:
            self.fail("Error expected")
示例#6
0
    def test_conflicting_connections(self):
        # verify we get an error if we have conflicting implicit and explicit connections
        root = Group()

        # promoting G1.x will create an implicit connection to G3.x
        # this is a conflict because G3.x (aka G3.C4.x) is already connected
        # to G3.C3.x
        G2 = root.add('G2', Group(), promotes=['x'])  # BAD PROMOTE
        G2.add('C1', ParamComp('x', 5.), promotes=['x'])

        G1 = G2.add('G1', Group(), promotes=['x'])
        G1.add('C2', ExecComp('y=x*2.0'), promotes=['x'])

        G3 = root.add('G3', Group(), promotes=['x'])
        G3.add('C3', ExecComp('y=x*2.0'))
        G3.add('C4', ExecComp('y=x*2.0'), promotes=['x'])

        root.connect('G2.G1.C2.y', 'G3.C3.x')
        G3.connect('C3.y', 'x')

        prob = Problem(root)

        try:
            prob.setup(check=False)
        except Exception as error:
            msg = "Target 'G3.C4.x' is connected to multiple unknowns: ['G2.C1.x', 'G3.C3.y']"
            self.assertEqual(text_type(error), msg)
        else:
            self.fail("Error expected")
示例#7
0
    def __init__(self):
        super(SellarDerivativesGrouped, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['*'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*'])

        mda = self.add('mda', Group(), promotes=['*'])
        mda.ln_solver = ScipyGMRES()
        mda.add('d1', SellarDis1withDerivatives(), promotes=['*'])
        mda.add('d2', SellarDis2withDerivatives(), promotes=['*'])

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0,
                          y1=0.0,
                          y2=0.0),
                 promotes=['*'])

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['*'])

        mda.nl_solver = NLGaussSeidel()
        mda.d1.fd_options['force_fd'] = True
        mda.d2.fd_options['force_fd'] = True

        self.ln_solver = ScipyGMRES()
示例#8
0
文件: sellar.py 项目: Js775/OpenMDAO
    def setup(self):
        self.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        self.mda = mda = self.add_subsystem('mda', Group(), promotes=['x', 'z', 'y1', 'y2'])
        mda.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2'])
        mda.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])

        self.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                               z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
                           promotes=['obj', 'x', 'z', 'y1', 'y2'])

        self.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1'])
        self.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2'])

        nl = self.options['nonlinear_solver']
        self.nonlinear_solver = nl() if inspect.isclass(nl) else nl
        if self.options['nl_atol']:
            self.nonlinear_solver.options['atol'] = self.options['nl_atol']
        if self.options['nl_maxiter']:
            self.nonlinear_solver.options['maxiter'] = self.options['nl_maxiter']

        ln = self.options['linear_solver']
        self.linear_solver = ln() if inspect.isclass(ln) else ln
        if self.options['ln_atol']:
            self.linear_solver.options['atol'] = self.options['ln_atol']
        if self.options['ln_maxiter']:
            self.linear_solver.options['maxiter'] = self.options['ln_maxiter']
示例#9
0
    def test_inp_inp_promoted_no_src(self):
        p = Problem(root=Group())
        root = p.root

        G1 = root.add("G1", Group())
        G2 = G1.add("G2", Group())
        C1 = G2.add("C1", ExecComp('y=x*2.0'))
        C2 = G2.add("C2", ExecComp('y=x*2.0'))

        G3 = root.add("G3", Group())
        G4 = G3.add("G4", Group())
        C3 = G4.add("C3", ExecComp('y=x*2.0'), promotes=['x'])
        C4 = G4.add("C4", ExecComp('y=x*2.0'), promotes=['x'])

        stream = cStringIO()
        checks = p.setup(out_stream=stream)
        self.assertEqual(checks['dangling_params'],
                         ['G1.G2.C1.x', 'G1.G2.C2.x', 'G3.G4.x'])
        self.assertEqual(checks['no_connect_comps'],
                         ['G1.G2.C1', 'G1.G2.C2', 'G3.G4.C3', 'G3.G4.C4'])
        self.assertEqual(p._dangling['G3.G4.x'],
                         set(['G3.G4.C3.x', 'G3.G4.C4.x']))

        # setting promoted name should set both params mapped to that name since the
        # params are dangling.
        p['G3.G4.x'] = 999.
        self.assertEqual(p.root.G3.G4.C3.params['x'], 999.)
        self.assertEqual(p.root.G3.G4.C4.params['x'], 999.)
示例#10
0
    def test_simple_array_comp(self):

        prob = Problem()
        root = prob.root = Group()

        root.add('p1', ParamComp('x', np.zeros([2])), promotes=['*'])
        root.add('comp', SimpleArrayComp(), promotes=['*'])
        root.add('con',
                 ExecComp('c = y - 20.0',
                          c=np.array([0.0, 0.0]),
                          y=np.array([0.0, 0.0])),
                 promotes=['*'])
        root.add('obj',
                 ExecComp('o = y[0]', y=np.array([0.0, 0.0])),
                 promotes=['*'])

        prob.driver = pyOptSparseDriver()
        prob.driver.add_param('x', low=-50.0, high=50.0)

        prob.driver.add_objective('o')
        prob.driver.add_constraint('c', ctype='eq')

        prob.setup(check=False)
        prob.run()

        obj = prob['o']
        assert_rel_error(self, obj, 20.0, 1e-6)
    def __init__(self):
        super(SellarStateConnection, self).__init__()

        self.add('px', ParamComp('x', 1.0), promotes=['*'])
        self.add('pz', ParamComp('z', np.array([5.0, 2.0])), promotes=['*'])

        self.add('state_eq', StateConnection())
        self.add('d1', SellarDis1(), promotes=['x', 'z', 'y1'])
        self.add('d2', SellarDis2(), promotes=['z', 'y1'])

        self.connect('state_eq.y2_command', 'd1.y2')
        self.connect('d2.y2', 'state_eq.y2_actual')

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0,
                          d1=0.0,
                          d2=0.0),
                 promotes=['x', 'z', 'y1', 'obj'])
        self.connect('d2.y2', 'obj_cmp.y2')

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2'])
        self.connect('d2.y2', 'con_cmp2.y2')

        self.nl_solver = Newton()
示例#12
0
    def setup(self):
        self.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add_subsystem('pz',
                           IndepVarComp('z', np.array([5.0, 2.0])),
                           promotes=['z'])

        cycle = self.add_subsystem('cycle',
                                   Group(),
                                   promotes=['x', 'z', 'y1', 'y2'])
        cycle.add_subsystem('d1',
                            SellarDis1(),
                            promotes=['x', 'z', 'y1', 'y2'])
        cycle.add_subsystem('d2', SellarDis2(), promotes=['z', 'y1', 'y2'])

        self.add_subsystem('obj_cmp',
                           ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                    z=np.array([0.0, 0.0]),
                                    x=0.0),
                           promotes=['x', 'z', 'y1', 'y2', 'obj'])

        self.add_subsystem('con_cmp1',
                           ExecComp('con1 = 3.16 - y1'),
                           promotes=['con1', 'y1'])
        self.add_subsystem('con_cmp2',
                           ExecComp('con2 = y2 - 24.0'),
                           promotes=['con2', 'y2'])

        self.nonlinear_solver = NonlinearBlockGS()

        self.nonlinear_solver = self.options['nonlinear_solver']
        if self.options['nl_atol']:
            self.nonlinear_solver.options['atol'] = self.options['nl_atol']
        if self.options['nl_maxiter']:
            self.nonlinear_solver.options['maxiter'] = self.options[
                'nl_maxiter']
示例#13
0
    def __init__(self):
        super(SellarNoDerivatives, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        cycle = self.add('cycle', Group(), promotes=['x', 'z', 'y1', 'y2'])
        cycle.ln_solver = ScipyGMRES()
        cycle.add('d1', SellarDis1(), promotes=['x', 'z', 'y1', 'y2'])
        cycle.add('d2', SellarDis2(), promotes=['z', 'y1', 'y2'])

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0),
                 promotes=['x', 'z', 'y1', 'y2', 'obj'])

        self.add('con_cmp1',
                 ExecComp('con1 = 3.16 - y1'),
                 promotes=['con1', 'y1'])
        self.add('con_cmp2',
                 ExecComp('con2 = y2 - 24.0'),
                 promotes=['con2', 'y2'])

        self.nl_solver = NLGaussSeidel()
        self.cycle.d1.deriv_options['type'] = 'fd'
        self.cycle.d2.deriv_options['type'] = 'fd'
示例#14
0
            def setup(self):
                self.add_subsystem('indep_var_comp', IndepVarComp('x'), promotes=['*'])
                self.add_subsystem('Cy', ExecComp('y=2*x'), promotes=['*'])
                self.add_subsystem('Cc', ExecComp('c=x+2'), promotes=['*'])

                self.add_design_var('x')
                self.add_constraint('c', lower=-3.)
示例#15
0
    def test_simple_array_comp2D(self):

        prob = Problem()
        root = prob.root = Group()

        root.add('p1', ParamComp('x', np.zeros((2, 2))), promotes=['*'])
        root.add('comp', ArrayComp2D(), promotes=['*'])
        root.add('con',
                 ExecComp('c = y - 20.0',
                          c=np.zeros((2, 2)),
                          y=np.zeros((2, 2))),
                 promotes=['*'])
        root.add('obj',
                 ExecComp('o = y[0, 0]', y=np.zeros((2, 2))),
                 promotes=['*'])

        prob.driver = ScipyOptimizer()
        prob.driver.options['optimizer'] = 'SLSQP'
        prob.driver.add_param('x', low=-50.0, high=50.0)

        prob.driver.add_objective('o')
        prob.driver.add_constraint('c', ctype='eq')
        prob.driver.options['disp'] = False

        prob.setup(check=False)
        prob.run()

        obj = prob['o']
        assert_rel_error(self, obj, 20.0, 1e-6)
示例#16
0
    def setup(self):
        self.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        self.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2'])
        self.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])

        self.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                           z=np.array([0.0, 0.0]), x=0.0),
                           promotes=['obj', 'x', 'z', 'y1', 'y2'])

        self.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1'])
        self.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2'])

        self.nonlinear_solver = self.metadata['nonlinear_solver']
        if self.metadata['nl_atol']:
            self.nonlinear_solver.options['atol'] = self.metadata['nl_atol']
        if self.metadata['nl_maxiter']:
            self.nonlinear_solver.options['maxiter'] = self.metadata['nl_maxiter']

        self.linear_solver = self.metadata['linear_solver']
        if self.metadata['ln_atol']:
            self.linear_solver.options['atol'] = self.metadata['ln_atol']
        if self.metadata['ln_maxiter']:
            self.linear_solver.options['maxiter'] = self.metadata['ln_maxiter']
示例#17
0
    def __init__(self):
        super(SellarDerivatives, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        self.add('d1',
                 SellarDis1withDerivatives(),
                 promotes=['x', 'z', 'y1', 'y2'])
        self.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0),
                 promotes=['obj', 'x', 'z', 'y1', 'y2'])

        self.add('con_cmp1',
                 ExecComp('con1 = 3.16 - y1'),
                 promotes=['con1', 'y1'])
        self.add('con_cmp2',
                 ExecComp('con2 = y2 - 24.0'),
                 promotes=['con2', 'y2'])

        self.nl_solver = NLGaussSeidel()
        self.ln_solver = ScipyGMRES()
示例#18
0
    def test_inp_inp_promoted_w_explicit_src(self):
        p = Problem(root=Group())
        root = p.root

        G1 = root.add("G1", Group())
        G2 = G1.add("G2", Group(), promotes=['x'])
        C1 = G2.add("C1", ExecComp('y=x*2.0'))
        C2 = G2.add("C2", ParamComp('x', 1.0), promotes=['x'])

        G3 = root.add("G3", Group())
        G4 = G3.add("G4", Group(), promotes=['x'])
        C3 = G4.add("C3", ExecComp('y=x*2.0'), promotes=['x'])
        C4 = G4.add("C4", ExecComp('y=x*2.0'), promotes=['x'])

        p.root.connect('G1.x', 'G3.x')
        p.setup(check=False)

        self.assertEqual(p._dangling['G1.G2.C1.x'], set(['G1.G2.C1.x']))
        self.assertEqual(len(p._dangling), 1)

        # setting promoted name will set the value into the unknowns, but will
        # not propagate it to the params. That will happen during run().
        p['G1.x'] = 999.
        self.assertEqual(p.root.G3.G4.C3.params['x'], 0.)
        self.assertEqual(p.root.G3.G4.C4.params['x'], 0.)

        p.run()
        self.assertEqual(p.root.G3.G4.C3.params['x'], 999.)
        self.assertEqual(p.root.G3.G4.C4.params['x'], 999.)
示例#19
0
    def test_unconnected_param_access_with_promotes(self):
        prob = Problem(root=Group())
        G1 = prob.root.add('G1', Group())
        G2 = G1.add('G2', Group(), promotes=['x'])
        C1 = G2.add('C1', ExecComp(['y=2.0*x', 'z=x*x-2.0']), promotes=['x'])
        C2 = G2.add('C2', ExecComp(['y=2.0*x', 'z=x*x-2.0']))
        G2.connect('C1.y', 'C2.x')

        # ignore warning about the unconnected param
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("ignore")
            prob.setup(check=False)

        prob.run()

        # still must use absolute naming to find params even if they're
        # promoted.  Promoted names for params can refer to more than one param.
        C1.params['x'] = 2.
        self.assertEqual(prob['G1.x'], 2.0)
        self.assertEqual(prob.root.G1.G2.C1.params['x'], 2.0)
        prob['G1.x'] = 99.
        self.assertEqual(C1.params['x'], 99.)
        prob['G1.x'] = 12.
        self.assertEqual(C1.params['x'], 12.)

        prob['G1.x'] = 17.

        self.assertEqual(prob.root.G1.G2.C1.params['x'], 17.0)
        prob.run()
示例#20
0
    def __init__(self):
        super(FanOut, self).__init__()

        self.add('p', ParamComp('x', 1.0))
        self.add('comp1', ExecComp(['y=3.0*x']))
        self.add('comp2', ExecComp(['y=-2.0*x']))
        self.add('comp3', ExecComp(['y=5.0*x']))

        self.connect("comp1.y", "comp2.x")
        self.connect("comp1.y", "comp3.x")
        self.connect("p.x", "comp1.x")
示例#21
0
    def setUp(self):
        self.p = Problem(root=Group())
        root = self.p.root

        self.G1 = root.add("G1", Group())
        self.G2 = self.G1.add("G2", Group())
        self.C1 = self.G2.add("C1", ExecComp('y=x*2.0'))
        self.C2 = self.G2.add("C2", ParamComp('x', 1.0))

        self.G3 = root.add("G3", Group())
        self.G4 = self.G3.add("G4", Group())
        self.C3 = self.G4.add("C3", ExecComp('y=x*2.0'))
        self.C4 = self.G4.add("C4", ExecComp('y=x*2.0'))
示例#22
0
    def __init__(self):
        super(FanIn, self).__init__()

        self.add('p1', ParamComp('x1', 1.0))
        self.add('p2', ParamComp('x2', 1.0))
        self.add('comp1', ExecComp(['y=-2.0*x']))
        self.add('comp2', ExecComp(['y=5.0*x']))
        self.add('comp3', ExecComp(['y=3.0*x1+7.0*x2']))

        self.connect("comp1.y", "comp3.x1")
        self.connect("comp2.y", "comp3.x2")
        self.connect("p1.x1", "comp1.x")
        self.connect("p2.x2", "comp2.x")
示例#23
0
 def test_input_input_explicit_conns_w_conn(self):
     prob = Problem(root=Group())
     root = prob.root
     root.add('p1', ParamComp('x', 1.0))
     root.add('c1', ExecComp('y = x*2.0'))
     root.add('c2', ExecComp('y = x*3.0'))
     root.connect('c1.x', 'c2.x')
     root.connect('p1.x', 'c2.x')
     prob.setup(check=False)
     prob.run()
     self.assertEqual(root.connections['c1.x'], 'p1.x')
     self.assertEqual(root.connections['c2.x'], 'p1.x')
     self.assertEqual(len(root.connections), 2)
示例#24
0
    def test_simple_Jacobian(self):

        # Tests that we can correctly handle user-defined Jacobians.

        empty = {}
        params = {'x': 0.0}
        unknowns = {'y': 0.0}
        mycomp = ExecComp(['y=2.0*x'])
        mycomp._jacobian_cache = mycomp.jacobian(params, unknowns, empty)

        # Forward

        dparams = {}
        dparams['x'] = np.array([3.1])
        dresids = {}
        dresids['y'] = np.array([0.0])

        mycomp.apply_linear(empty, empty, dparams, empty, dresids, 'fwd')

        self.assertEqual(dresids['y'], 6.2)

        # Reverse

        dparams = {}
        dparams['x'] = np.array([0.0])
        dresids = {}
        dresids['y'] = np.array([3.1])

        mycomp.apply_linear(empty, empty, dparams, empty, dresids, 'rev')

        self.assertEqual(dparams['x'], 6.2)
示例#25
0
    def test_input_input_explicit_conns_no_conn(self):
        prob = Problem(root=Group())
        root = prob.root
        root.add('p1', ParamComp('x', 1.0))
        root.add('c1', ExecComp('y = x*2.0'))
        root.add('c2', ExecComp('y = x*3.0'))
        root.connect('c1.x', 'c2.x')

        # ignore warning about the unconnected params
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("ignore")
            prob.setup(check=False)

        prob.run()
        self.assertEqual(root.connections, {})
示例#26
0
    def test_multiple_connect(self):
        root = Group()
        C1 = root.add('C1', ExecComp('y=x*2.0'))
        C2 = root.add('C2', ExecComp('y=x*2.0'))
        C3 = root.add('C3', ExecComp('y=x*2.0'))

        root.connect('C1.y', ['C2.x', 'C3.x'])

        root._setup_paths('')
        params_dict, unknowns_dict = root._setup_variables()

        # verify we get correct connection information
        connections = root._get_explicit_connections()
        expected_connections = {'C2.x': ['C1.y'], 'C3.x': ['C1.y']}
        self.assertEqual(connections, expected_connections)
示例#27
0
    def test_auto_order2(self):
        # this tests the auto ordering when we have a cycle that is the full graph.
        p = Problem(root=Group())
        root = p.root
        C1 = root.add("C1", ExecComp('y=x*2.0'))
        C2 = root.add("C2", ExecComp('y=x*2.0'))
        C3 = root.add("C3", ExecComp('y=x*2.0'))

        root.connect('C1.y', 'C3.x')
        root.connect('C3.y', 'C2.x')
        root.connect('C2.y', 'C1.x')

        p.setup(check=False)

        self.assertEqual(p.root.list_auto_order(), ['C1', 'C3', 'C2'])
示例#28
0
    def __init__(self):
        super(ExampleGroup, self).__init__()

        self.G2 = self.add('G2', Group())
        self.C1 = self.G2.add('C1', IndepVarComp('x', 5.))

        self.G1 = self.G2.add('G1', Group())
        self.C2 = self.G1.add('C2', ExecComp('y=x*2.0', x=3., y=5.5))

        self.G3 = self.add('G3', Group())
        self.C3 = self.G3.add('C3', ExecComp('y=x*2.0', x=3., y=5.5))
        self.C4 = self.G3.add('C4', ExecComp('y=x*2.0', x=3., y=5.5))

        self.G2.connect('C1.x', 'G1.C2.x')
        self.connect('G2.G1.C2.y', 'G3.C3.x')
        self.G3.connect('C3.y', 'C4.x')
示例#29
0
    def test_simple_paraboloid_equality(self):

        prob = Problem()
        root = prob.root = Group()

        root.add('p1', ParamComp('x', 50.0), promotes=['*'])
        root.add('p2', ParamComp('y', 50.0), promotes=['*'])
        root.add('comp', Paraboloid(), promotes=['*'])
        root.add('con', ExecComp('c = 15.0 - x + y'), promotes=['*'])

        prob.driver = ScipyOptimizer()
        prob.driver.options['optimizer'] = 'SLSQP'
        prob.driver.options['tol'] = 1.0e-8
        prob.driver.add_param('x', low=-50.0, high=50.0)
        prob.driver.add_param('y', low=-50.0, high=50.0)

        prob.driver.add_objective('f_xy')
        prob.driver.add_constraint('c', ctype='ineq')
        prob.driver.options['disp'] = False

        prob.setup(check=False)
        prob.run()

        # Minimum should be at (7.166667, -7.833334)
        assert_rel_error(self, prob['x'], 7.16667, 1e-6)
        assert_rel_error(self, prob['y'], -7.833334, 1e-6)
示例#30
0
    def __init__(self):
        super(FanInGrouped, self).__init__()

        self.add('p1', IndepVarComp('x1', 1.0))
        self.add('p2', IndepVarComp('x2', 1.0))
        self.add('p3', IndepVarComp('x3', 1.0))
        sub = self.add('sub', ParallelGroup())

        sub.add('comp1', ExecComp(['y=-2.0*x']))
        sub.add('comp2', ExecComp(['y=5.0*x']))
        self.add('comp3', ExecComp(['y=3.0*x1+7.0*x2']))

        self.connect("sub.comp1.y", "comp3.x1")
        self.connect("sub.comp2.y", "comp3.x2")
        self.connect("p1.x1", "sub.comp1.x")
        self.connect("p2.x2", "sub.comp2.x")
    def test_simple_Jacobian(self):

        # Tests that we can correctly handle user-defined Jacobians.

        empty = {}
        params = {'x': 0.0}
        unknowns = {'y': 0.0}
        mycomp = ExecComp(['y=2.0*x'])
        mycomp._jacobian_cache = mycomp.jacobian(params, unknowns, empty)

        # Forward

        dparams = {}
        dparams['x'] = np.array([3.1])
        dresids = {}
        dresids['y'] = np.array([0.0])

        mycomp.apply_linear(empty, empty, dparams, empty,
                            dresids, 'fwd')

        self.assertEqual(dresids['y'], 6.2)

        # Reverse

        dparams = {}
        dparams['x'] = np.array([0.0])
        dresids = {}
        dresids['y'] = np.array([3.1])

        mycomp.apply_linear(empty, empty, dparams, empty,
                            dresids, 'rev')

        self.assertEqual(dparams['x'], 6.2)