コード例 #1
0
    def test_branch_output_in_opaque_system(self):

        # This test replicates a bug where an interior output was missing in
        # an opaque system.

        top = set_as_top(Assembly())
        top.add('nest', Assembly())
        top.nest.add('comp1', ExecComp(['y=7.0*x1']))
        top.nest.add('comp2', ExecComp(['y=5.0*x1 + 2.0*x2']))
        top.driver.workflow.add(['nest'])
        top.nest.driver.workflow.add(['comp1', 'comp2'])

        top.nest.add('x1', Float(3.0, iotype='in'))
        top.nest.add('y2', Float(3.0, iotype='out'))
        top.nest.connect('comp1.y', 'comp2.x2')
        top.nest.connect('x1', 'comp1.x1')
        top.nest.connect('x1', 'comp2.x1')
        top.nest.create_passthrough('comp1.y')
        top.nest.connect('comp2.y', 'y2')

        top.run()

        J = top.driver.calc_gradient(inputs=['nest.x1'],
                                     outputs=['nest.y', 'nest.y2'],
                                     mode='forward')

        assert_rel_error(self, J[0, 0], 7.0, .001)
        assert_rel_error(self, J[1, 0], 19.0, .001)
コード例 #2
0
    def setUp(self):
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_csv-')
        os.chdir(self.tempdir)

        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        top.comp1.add('a_string', Str("Hello',;','", iotype='out'))
        top.comp1.add('a_array', Array(array([1.0, 3.0, 5.5]), iotype='out'))
        top.comp1.add('x_array', Array(array([1.0, 1.0, 1.0]), iotype='in'))
        top.comp1.add('b_bool', Bool(False, iotype='in'))
        top.comp1.add('vt', VarTree(DumbVT(), iotype='out'))
        top.driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z', 'comp1.a_string', 'comp1.a_array[2]']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i + 0.1), ('comp1.y', i * 2 + .1),
                      ('comp1.x_array[1]', 99.88), ('comp1.b_bool', True)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(sorted(outputs))

        self.filename = "openmdao_test_csv_case_iterator.csv"
コード例 #3
0
    def test_smarter_nondifferentiable_blocks(self):

        top = set_as_top(Assembly())
        top.add(
            'comp1',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.add('comp2', ExecComp(['y=2.0*x + 3.0*x2', 'y2=4.0*x + 5.0*x2']))
        top.add(
            'comp3',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.add('comp4', ExecComp(['y=2.0*x + 3.0*x2']))
        top.add(
            'comp5',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.driver.workflow.add(['comp1', 'comp2', 'comp3', 'comp4', 'comp5'])

        top.connect('comp1.y', 'comp2.x')
        top.connect('comp2.y', 'comp3.x')
        top.connect('comp2.y2', 'comp4.x')
        top.connect('comp3.y', 'comp4.x2')
        top.connect('comp4.y', 'comp5.x')

        top.run()

        J = top.driver.workflow.calc_gradient(inputs=['comp1.x'],
                                              outputs=['comp5.y'],
                                              mode='forward')

        pa1 = top.driver.workflow._derivative_graph.node['~0']['pa_object']
        self.assertTrue('comp1' not in pa1.comps)
        self.assertTrue('comp2' in pa1.comps)
        self.assertTrue('comp3' in pa1.comps)
        self.assertTrue('comp4' in pa1.comps)
        self.assertTrue('comp5' not in pa1.comps)
        self.assertTrue(pa1.comps == pa1.itercomps)

        top.replace(
            'comp4',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))

        top.run()

        top.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=['comp1.x'],
                                              outputs=['comp5.y'],
                                              mode='forward')

        pa1 = top.driver.workflow._derivative_graph.node['~0']['pa_object']
        self.assertTrue('comp1' not in pa1.comps)
        self.assertTrue('comp2' in pa1.comps)
        self.assertTrue('comp3' not in pa1.comps)
        self.assertTrue('comp4' not in pa1.comps)
        self.assertTrue('comp5' not in pa1.comps)
        self.assertTrue(pa1.comps == pa1.itercomps)
コード例 #4
0
 def _create_assembly(self, dbname, drivertype):
     asm = Assembly()
     driver = asm.add('driver', drivertype())
     asm.add('comp1', ExecComp(exprs=['z=x+y']))
     asm.add('comp2', ExecComp(exprs=['z=x+y']))
     asm.connect('comp1.z', 'comp2.x')
     driver.workflow.add(['comp1', 'comp2'])
     driver.recorders = [DBCaseRecorder(dbname, append=True)]
     return asm
コード例 #5
0
    def test_smarter_nondifferentiable_blocks(self):

        top = set_as_top(Assembly())
        top.add(
            'comp1',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.add('comp2', ExecComp(['y=2.0*x + 3.0*x2', 'y2=4.0*x + 5.0*x2']))
        top.add(
            'comp3',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.add('comp4', ExecComp(['y=2.0*x + 3.0*x2']))
        top.add(
            'comp5',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))
        top.driver.workflow.add(['comp1', 'comp2', 'comp3', 'comp4', 'comp5'])

        top.connect('comp1.y', 'comp2.x')
        top.connect('comp2.y', 'comp3.x')
        top.connect('comp2.y2', 'comp4.x')
        top.connect('comp3.y', 'comp4.x2')
        top.connect('comp4.y', 'comp5.x')

        top.run()

        J = top.driver.calc_gradient(inputs=['comp1.x'],
                                     outputs=['comp5.y'],
                                     mode='forward')

        self.assertTrue(len(top.driver.workflow._system.subsystems()) == 4)
        comp_list = simple_node_iter(
            top.driver.workflow._system.subsystems()[2]._nodes)
        self.assertTrue(len(comp_list) == 3)
        self.assertTrue('comp2' in comp_list)
        self.assertTrue('comp3' in comp_list)
        self.assertTrue('comp4' in comp_list)

        top.replace(
            'comp4',
            ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'],
                                    ['dy_dx = 2.0', 'dy_dx2 = 3.0']))

        top.run()

        J = top.driver.calc_gradient(inputs=['comp1.x'],
                                     outputs=['comp5.y'],
                                     mode='forward')

        self.assertTrue(len(top.driver.workflow._system.subsystems()) == 6)
        comp_list = simple_node_iter(
            top.driver.workflow._system.subsystems()[2]._nodes)
        self.assertTrue(len(comp_list) == 1)
        self.assertTrue('comp2' in comp_list)
コード例 #6
0
    def setUp(self):
        self.top = set_as_top(Assembly())

        self.top.add('comp1', ExecComp(exprs=['z=x+y']))
        self.top.add('comp2', ExecComp(exprs=['z=x+1']))
        self.top.connect('comp1.z', 'comp2.x')

        self.top.add('driver', SensitivityDriver())
        self.top.driver.workflow.add(['comp1', 'comp2'])
        self.top.driver.add_parameter(['comp1.x'], low=-100, high=100)
        self.top.driver.add_objective('comp1.z')
        self.top.driver.add_objective('comp2.z')
コード例 #7
0
 def setUp(self):
     self.top = top = set_as_top(Assembly())
     driver = top.add('driver', SimpleCaseIterDriver())
     top.add('comp1', ExecComp(exprs=['z=x+y']))
     top.add('comp2', ExecComp(exprs=['z=x+1']))
     top.connect('comp1.z', 'comp2.x')
     driver.workflow.add(['comp1', 'comp2'])
     
     # now create some Cases
     outputs = ['comp1.z', 'comp2.z']
     cases = []
     for i in range(10):
         inputs = [('comp1.x', i), ('comp1.y', i*2)]
         cases.append(Case(inputs=inputs, outputs=outputs, label='case%s'%i))
     driver.iterator = ListCaseIterator(cases)
コード例 #8
0
    def test_baseline_case(self):

        top = set_as_top(Assembly())
        top.add('comp1', ExecCompWithDerivatives(['y1=2.0*x',
                                                  'y2=3.0*x',
                                                  'y3=4.0*x',
                                                  'y4=5.0*x'],
                                                 ['dy1_dx = 2.0',
                                                  'dy2_dx = 3.0',
                                                  'dy3_dx = 4.0',
                                                  'dy4_dx = 5.0']))

        top.add('comp2', ExecComp(['y = 2.0*x1 + x2 + x3 + x4']))
        top.driver.workflow.add(['comp1', 'comp2'])
        top.connect('comp1.y1', 'comp2.x1')
        top.connect('comp1.y2', 'comp2.x2')
        top.connect('comp1.y3', 'comp2.x3')
        top.connect('comp1.y4', 'comp2.x4')

        top.run()
        self.assertEqual(top.comp2.exec_count, 1)
        top.driver.gradient_options.directional_fd = True

        J = top.driver.calc_gradient(inputs=['comp1.x'],
                                     outputs=['comp2.y'])

        # Ran 1 times, not 4.
        self.assertEqual(top.comp2.exec_count, 2)
        assert_rel_error(self, J[0, 0], 16.0, 0.0001)

        top.driver.gradient_options.directional_fd = False

        J = top.driver.calc_gradient(inputs=['comp1.x'],
                                     outputs=['comp2.y'])

        # Ran 4 times (4 fd steps).
        self.assertEqual(top.comp2.exec_count, 6)
        assert_rel_error(self, J[0, 0], 16.0, 0.0001)

        # Next, test param / obj

        top.add('driver', SimpleDriver())
        top.driver.add_parameter('comp1.x', low=-1000, high=1000)
        top.driver.add_objective('comp2.y')
        top.run()
        self.assertEqual(top.comp2.exec_count, 7)

        top.driver.gradient_options.directional_fd = True

        J = top.driver.calc_gradient()

        # Ran 1 more times.
        self.assertEqual(top.comp2.exec_count, 8)
        assert_rel_error(self, J[0, 0], 16.0, 0.0001)

        J = top.driver.calc_gradient(mode='fd')

        # Ran 1 more times (full model fd, 2 edges).
        self.assertEqual(top.comp2.exec_count, 9)
        assert_rel_error(self, J[0, 0], 16.0, 0.0001)
コード例 #9
0
    def test_connected_input_as_parameter(self):
        self.top.add('comp2', ExecComp(exprs=['c=x+y', 'd=x-y']))
        self.top.driver.add_parameter('comp2.x', low=-99.0, high=99.9)

        try:
            self.top.connect('comp.c', 'comp2.x')
        except RuntimeError as err:
            msg = "Can't connect 'comp.c' to 'comp2.x' because" + \
                  " the target is a Parameter in driver 'driver'."
        else:
            self.fail("Exception expected")

        # try with parameter group
        self.top.driver.clear_parameters()
        self.top.driver.add_parameter(('comp2.x', 'comp2.y'),
                                      low=-99.0,
                                      high=99.9)

        try:
            self.top.connect('comp.c', 'comp2.x')
        except RuntimeError as err:
            msg = "Can't connect 'comp.c' to 'comp2.x' because" + \
                  " the target is a Parameter in driver 'driver'."
        else:
            self.fail("Exception expected")
コード例 #10
0
 def setUp(self):
     self.top = set_as_top(Assembly())
     self.top.add('driver', SimpleDriver())
     self.top.add('driver1', MyDriver())
     self.top.add('driver2', MyDriver())
     self.top.add('comp', ExecComp(exprs=['z=a+b+c+d']))
     self.top.driver.workflow.add(['driver1', 'driver2'])
コード例 #11
0
 def test_mixed_use(self):
     # Connect to one element of array and use another element as parameter.
     self.top.comp.x1d = [1, 2, 3]
     self.top.add('exec_comp', ExecComp(exprs=['c=x+y', 'd=x-y']))
     self.top.driver.workflow.add('exec_comp')
     self.top.connect('exec_comp.c', 'comp.x1d[0]')
     self.top.driver.add_parameter('comp.x1d[1]', low=-10, high=10, start=1)
     self.top.run()
コード例 #12
0
 def setUp(self):
     self.asm = set_as_top(Assembly())
     self.asm.add('comp1', ExecComp(exprs=['c=a+b', 'd=a-b']))
     self.asm.add('driver', MyMultiDriver())
     self.asm.comp1.a = 1
     self.asm.comp1.b = 2
     self.asm.comp1.c = 3
     self.asm.comp1.d = -1
コード例 #13
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            inputs = [('comp1.x', i), ('comp1.y', i * 2)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #14
0
    def test_execcomp(self):
        exp1 = ['y1 = 2.0*x1 + x2*x2', 'y2 = 3.0*x1*x2']
        self.top.add('comp1', ExecComp(exp1))

        self.top.comp1.x1 = 3.0
        self.top.comp1.x2 = 5.0
        self.top.comp1.run()

        self.assertEqual(self.top.comp1.y1, 31.0)
        self.assertEqual(self.top.comp1.y2, 45.0)
コード例 #15
0
    def test_nested(self):
        asm3 = Assembly()
        asm3.add('comp1', ExecComp(exprs=['z=x+y']))
        driver = asm3.add('driver', SLSQPdriver())
        driver.workflow.add('comp1')
        driver.add_parameter('comp1.y', low=-1, high=1, start=0)
        driver.add_objective('comp1.z')
        driver.add_constraint('comp1.z >= 0')
        asm3.create_passthrough('comp1.x')
        asm3.create_passthrough('comp1.z')

        asm2 = Assembly()
        asm2.add('comp1', ExecComp(exprs=['z=x+y']))
        asm2.add('asm3', asm3)
        asm2.connect('comp1.z', 'asm3.x')
        driver = asm2.add('driver', SLSQPdriver())
        driver.workflow.add(('comp1', 'asm3'))
        driver.add_parameter('comp1.y', low=-1, high=1, start=0)
        driver.add_objective('asm3.z')
        driver.add_constraint('comp1.z >= 0')
        asm2.create_passthrough('comp1.x')
        asm2.create_passthrough('asm3.z')

        asm1 = set_as_top(Assembly())
        asm1.add('comp1', ExecComp(exprs=['z=x+y']))
        asm1.add('asm2', asm2)
        asm1.connect('comp1.z', 'asm2.x')
        driver = asm1.add('driver', SLSQPdriver())
        driver.workflow.add(('comp1', 'asm2'))
        driver.add_parameter('comp1.y', low=-1, high=1, start=0)
        driver.add_objective('asm2.z')
        driver.add_constraint('comp1.z >= 0')

        sout = StringIO()
        asm1.recorders = [JSONCaseRecorder(sout)]
        asm1.run()

        if os.environ.get('REGEN_JSON_FILES'):
            with open('nested.new', 'w') as out:
                out.write(sout.getvalue())
        verify_json(self, sout, 'nested.json')
コード例 #16
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        comp1 = top.add('comp1', ExecComp(exprs=['z=x*y+100']))
        comp2 = top.add('comp2', ExecComp(exprs=['z=x*.4']))
        top.connect('comp1.z', 'comp2.x')
        driver = top.add('driver', SimpleCaseIterDriver())
        driver.workflow.add([comp1, comp2])

        plotter = top.add('plotter', XYplotter())
        plotter.title = "Foobar"
        #plotter.add_line(y="comp1.z", line_type='bo-')
        plotter.add_line(x="comp1.x", y="comp1.z", line_type='bo-')
        plotter.add_line(x='comp1.x', y="comp2.z", line_type='rD-', label='blah')

        # now create some Cases
        outputs = [('comp1.z', None, None), ('comp2.z', None, None)]
        cases = []
        for i in range(10):
            inputs = [('comp1.x', None, i), ('comp1.y', None, i*2)]
            cases.append(Case(inputs=inputs, outputs=outputs, ident='case%s'%i))
        driver.iterator = ListCaseIterator(cases)
コード例 #17
0
 def setUp(self):
     self.top = top = set_as_top(Assembly())
     driver = top.add('driver', SimpleCaseIterDriver())
     top.add('comp1', ExecComp(exprs=['z=x+y']))
     top.add('comp2', ExecComp(exprs=['z=x+1']))
     top.connect('comp1.z', 'comp2.x')
     top.comp1.add('a_string', Str("Hello',;','", iotype='out'))
     top.comp1.add('a_array', Array(array([1.0, 3.0, 5.5]), iotype='out'))
     top.comp1.add('x_array', Array(array([1.0, 1.0, 1.0]), iotype='in'))
     top.comp1.add('vt', Slot(DumbVT, iotype='out'))
     top.comp1.vt = DumbVT()
     driver.workflow.add(['comp1', 'comp2'])
     
     # now create some Cases
     outputs = ['comp1.z', 'comp2.z', 'comp1.a_string', 'comp1.a_array[2]']
     cases = []
     for i in range(10):
         inputs = [('comp1.x', i+0.1), ('comp1.y', i*2 + .1), ('comp1.x_array[1]', 99.88)]
         cases.append(Case(inputs=inputs, outputs=outputs, label='case%s'%i))
     driver.iterator = ListCaseIterator(cases)
     
     self.filename = "openmdao_test_csv_case_iterator.csv"
コード例 #18
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.comp1.add('a_dict', Dict({}, iotype='in'))
        top.comp1.add('a_list', List([], iotype='in'))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i), ('comp1.y', i * 2),
                      ('comp1.a_dict', {
                          'a': 'b'
                      }), ('comp1.a_list', ['a', 'b'])]
            cases.append(Case(inputs=inputs, outputs=outputs))
        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #19
0
    def test_serial_under_par(self):

        class MyDriver(SimpleDriver):

            implements(ISolver)

            def execute(self):
               # Direct uvec setting
                uvec = self._system.vec['u']
                #print uvec.keys()
                # Only can interact with the var that is in our node
                for num in [1.0, 2.0, 3.0]:
                    if 'comp1.x' in uvec:
                        uvec['comp1.x'] = num
                        #print "SETTING", 'comp1.x', uvec['comp1.x']
                    if 'comp2.x' in uvec:
                        uvec['comp2.x'] = num
                        #print "SETTING", 'comp2.x', uvec['comp2.x']

                    self.run_iteration()

            def requires_derivs(self):
                return False

        top = set_as_top(Assembly())
        top.add('driver', MyDriver())
        top.add('comp1', ExecComp(['y = 2.0*x']))
        top.add('comp2', ExecComp(['y = 1.0*x']))
        top.driver.workflow.add(['comp1', 'comp2'])
        top.driver.add_parameter('comp1.x', low=-100, high=100)
        top.driver.add_parameter('comp2.x', low=-100, high=100)
        top.driver.add_constraint('comp1.y = comp2.x')
        top.driver.add_constraint('comp2.y = comp1.x')

        top.run()

        self.assertTrue(top.comp1.x==3.0)
        self.assertTrue(top.comp2.x==3.0)
コード例 #20
0
    def test_broadcast_input_to_opaque_system(self):

        # This test replicates a bug when finite differencing a boundary variable that
        # broadcasts to multiple places.

        top = set_as_top(Assembly())
        top.add('comp1', ExecComp(['y=7.0*x1']))
        top.add('comp2', ExecComp(['y=5.0*x1 + 2.0*x2']))
        top.add('driver', SimpleDriver())
        top.driver.workflow.add(['comp1', 'comp2'])

        top.add('x1', Float(3.0, iotype='in'))
        top.connect('comp1.y', 'comp2.x2')
        top.connect('x1', 'comp1.x1')
        top.connect('x1', 'comp2.x1')

        top.run()

        J = top.driver.calc_gradient(inputs=['x1'],
                                     outputs=['comp2.y'],
                                     mode='forward')

        assert_rel_error(self, J[0, 0], 19.0, .001)
コード例 #21
0
    def test_set_boundary_params(self):
        self.top = set_as_top(Assembly())
        self.top.add('driver', MyDriver())
        self.top.add('comp', ExecComp(exprs=['c=x+y', 'd=x-y']))
        self.top.driver.workflow.add('comp')
        self.top.create_passthrough('comp.x')
        self.top.create_passthrough('comp.c')
        self.top.connect('x', 'comp.y')

        self.top.driver.add_parameter(('x'), low=0., high=1e99)
        self.top.x = 22.0
        self.top.run()
        self.assertEqual(self.top.x, 22.)
        self.assertEqual(self.top.comp.y, 22.)
コード例 #22
0
 def test_errors(self):
     a = set_as_top(Assembly())
     comp = a.add('comp', ExecComp(exprs=["f=x"]))
     driver = a.add('driver', Brent())
     driver.add_parameter('comp.x')
     driver.add_constraint('comp.f=0')
     comp.n = 1.0
     comp.c = 0
     driver.lower_bound = 1.0
     try:
         a.run()
     except Exception as err:
         self.assertEqual(str(err), "driver: bounds (low=1.0, high=100.0) do not bracket a root")
     else:
         self.fail("Exception expected")
コード例 #23
0
    def test_simplest(self):

        top = set_as_top(Assembly())
        top.add('comp', ExecComp(['y=4.0*x']))
        top.driver.workflow.add('comp')

        top.run()
        top.driver.gradient_options.directional_fd = True

        J = top.driver.workflow.calc_gradient(inputs=['comp.x'],
                                              outputs=['comp.y'])

        assert_rel_error(self, J[0, 0], 4.0, 0.0001)

        top.driver.gradient_options.fd_form = 'backward'

        top.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=['comp.x'],
                                              outputs=['comp.y'])

        assert_rel_error(self, J[0, 0], 4.0, 0.0001)

        top.driver.gradient_options.fd_form = 'central'

        top.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=['comp.x'],
                                              outputs=['comp.y'])

        assert_rel_error(self, J[0, 0], 4.0, 0.0001)

        top.driver.gradient_options.fd_form = 'complex_step'

        top.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=['comp.x'],
                                              outputs=['comp.y'])

        assert_rel_error(self, J[0, 0], 4.0, 0.0001)

        top.driver.gradient_options.directional_fd = True
        top.driver.workflow.config_changed()
        try:
            J = top.driver.workflow.calc_gradient(inputs=['comp.x'],
                                                  outputs=['comp.y'],
                                                  mode='adjoint')
        except RuntimeError, err:
            msg = ": Directional derivatives can only be used with forward "
            msg += "mode."
            self.assertEqual(str(err), msg)
コード例 #24
0
    def test_brent_converge(self):

        a = set_as_top(Assembly())
        comp = a.add('comp', ExecComp(exprs=["f=a * x**n + b * x - c"]))
        comp.n = 77.0/27.0
        comp.a = 1.0
        comp.b = 1.0
        comp.c = 10.0

        driver = a.add('driver', Brent())
        driver.add_parameter('comp.x', 0, 100)
        driver.add_constraint('comp.f=0')

        a.run()

        assert_rel_error(self, a.comp.x, 2.06720359226, .0001)
        assert_rel_error(self, a.comp.f, 0, .0001)

        self.assertTrue(has_interface(driver, ISolver))
コード例 #25
0
    def test_connected_input_as_parameter(self):
        self.top.add('comp2', ExecComp(exprs=['c=x+y', 'd=x-y']))
        self.top.driver.add_parameter('comp2.x', low=-99.0, high=99.9)

        code = "self.top.connect('comp.c', 'comp2.x')"
        assert_raises(
            self, code, globals(), locals(), RuntimeError,
            ": Can't connect 'comp.c' to 'comp2.x': : destination"
            " 'comp2.x' is a Parameter in driver 'driver'.")

        # try with parameter group
        self.top.driver.clear_parameters()
        self.top.driver.add_parameter(('comp2.x', 'comp2.y'),
                                      low=-99.0,
                                      high=99.9)
        code = "self.top.connect('comp.c', 'comp2.x')"
        assert_raises(
            self, code, globals(), locals(), RuntimeError,
            ": Can't connect 'comp.c' to 'comp2.x': : destination"
            " 'comp2.x' is a Parameter in driver 'driver'.")
コード例 #26
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', CaseIteratorDriver())
        top.add('comp1', TExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i), ('comp1.y', i * 2)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)

        self.tempdir = tempfile.mkdtemp(prefix='test_jsonrecorder-')
コード例 #27
0
    def test_general_solver(self):

        a = set_as_top(Assembly())
        comp = a.add('comp', ExecComp(exprs=["f=a * x**n + b * x - c"]))
        comp.n = 77.0 / 27.0
        comp.a = 1.0
        comp.b = 1.0
        comp.c = 10.0
        comp.x = 0.0

        driver = a.add('driver', NewtonSolver())
        driver.workflow.add('comp')

        driver.add_parameter('comp.x', 0, 100)
        driver.add_constraint('comp.f=0')
        self.top.driver.gradient_options.fd_step = 0.01
        self.top.driver.gradient_options.fd_step_type = 'relative'

        a.run()

        assert_rel_error(self, a.comp.x, 2.06720359226, .0001)
        assert_rel_error(self, a.comp.f, 0, .0001)
コード例 #28
0
    def test_connected_input_as_parameter(self):
        self.top.add('comp2', ExecComp(exprs=['c=x+y', 'd=x-y']))
        self.top.driver.add_parameter('comp2.x', low=-99.0, high=99.9)

        self.top.connect('comp.c', 'comp2.x')
        assert_raises(
            self, "self.top.run()", globals(), locals(), RuntimeError,
            ": The following parameters collide with connected inputs: comp2.x in driver"
        )

        self.top.disconnect('comp.c', 'comp2.x')

        # try with parameter group
        self.top.driver.clear_parameters()
        self.top.driver.add_parameter(('comp2.x', 'comp2.y'),
                                      low=-99.0,
                                      high=99.9)
        self.top.connect('comp.c', 'comp2.x')
        assert_raises(
            self, "self.top.run()", globals(), locals(), RuntimeError,
            ": The following parameters collide with connected inputs: comp2.x in driver"
        )
コード例 #29
0
    def test_diverge_converge_nondiff_comp3_forward(self):
        
        self.top = set_as_top(Assembly())

        exp1 = ['y1 = 2.0*x1**2',
                'y2 = 3.0*x1']
        deriv1 = ['dy1_dx1 = 4.0*x1',
                  'dy2_dx1 = 3.0']

        exp2 = ['y1 = 0.5*x1']
        deriv2 = ['dy1_dx1 = 0.5']

        exp3 = ['y1 = 3.5*x1']
        deriv3 = ['dy1_dx1 = 3.5']

        exp4 = ['y1 = x1 + 2.0*x2',
                'y2 = 3.0*x1',
                'y3 = x1*x2']
        deriv4 = ['dy1_dx1 = 1.0',
                  'dy1_dx2 = 2.0',
                  'dy2_dx1 = 3.0',
                  'dy2_dx2 = 0.0',
                  'dy3_dx1 = x2',
                  'dy3_dx2 = x1']

        exp5 = ['y1 = x1 + 3.0*x2 + 2.0*x3']
        deriv5 = ['dy1_dx1 = 1.0',
                  'dy1_dx2 = 3.0',
                  'dy1_dx3 = 2.0']
        
        self.top.add('driver', SimpleDriver())
        self.top.add('comp1', ExecCompWithDerivatives(exp1, deriv1))
        self.top.add('comp2', ExecCompWithDerivatives(exp2, deriv2))
        self.top.add('comp3', ExecComp(exp3))
        self.top.add('comp4', ExecCompWithDerivatives(exp4, deriv4))
        self.top.add('comp5', ExecCompWithDerivatives(exp5, deriv5))

        self.top.driver.workflow.add(['comp1', 'comp2', 'comp3', 'comp4', 'comp5'])

        self.top.connect('comp1.y1', 'comp2.x1')
        self.top.connect('comp1.y2', 'comp3.x1')
        self.top.connect('comp2.y1', 'comp4.x1')
        self.top.connect('comp3.y1', 'comp4.x2')
        self.top.connect('comp4.y1', 'comp5.x1')
        self.top.connect('comp4.y2', 'comp5.x2')
        self.top.connect('comp4.y3', 'comp5.x3')
        
        self.top.driver.add_parameter('comp1.x1', low=-100, high=100)
        self.top.driver.add_objective('comp5.y1')
        
        self.top.comp1.x1 = 2.0        
        self.top.run()

        J = self.top.driver.calc_gradient(inputs=['comp1.x1'],
                                          outputs=['comp5.y1'],
                                          mode='forward',
                                          return_format='dict')
        
        collective_assert_rel_error(self, 
                                    J['comp5.y1']['comp1.x1'][0][0], 
                                    313.0, 0.0001)
コード例 #30
0
 def setUp(self):
     self.top = set_as_top(Assembly())
     self.top.add('driver', MyDriver())
     self.top.add('comp', ExecComp(exprs=['c=x+y', 'd=x-y']))
     self.top.driver.workflow.add('comp')