Beispiel #1
0
    def test_simple_array_comp2D(self):

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

        root.add('p1', IndepVarComp('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_desvar('x', lower=-50.0, upper=50.0)

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

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

        obj = prob['o']
        assert_rel_error(self, obj, 20.0, 1e-6)
    def test_scaler_adder_array_inf(self):

        # make sure inf doesn't bomb out

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

        root.add('p1', IndepVarComp('x', val=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])
        root.add('comp', ArrayComp2D(), promotes=['*'])
        root.add('constraint', ExecComp('con = x + y',
                                        x=np.array([[1.0, 1.0], [1.0, 1.0]]),
                                        y=np.array([[1.0, 1.0], [1.0, 1.0]]),
                                        con=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])

        driver.add_desvar('x', lower=np.array([[-1e5, -1e5], [-np.inf, -1e5]]),
                          upper=np.array([1e25, 1e25, np.inf, 1e25]),
                         adder=np.array([[10.0, 100.0], [1000.0, 10000.0]]),
                         scaler=np.array([[1.0e-2, 2.0], [3.0, 4.0e12]]))
        driver.add_objective('y', adder=np.array([[10.0, 100.0], [1000.0, 10000.0]]),
                         scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))
        driver.add_constraint('con', upper=np.zeros((2, 2)), adder=np.array([[10.0, 100.0], [1000.0, 10000.0]]),
                              scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))

        prob.setup(check=False)
        prob.run()
Beispiel #3
0
    def test_array2D_index_connection(self):
        group = Group()
        group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        sub = group.add('sub', Group(), promotes=['*'])
        sub.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])
        group.add('obj', ExecComp('b = a'))
        group.connect('y', 'obj.a',  src_indices=[3])

        prob = Problem()
        prob.root = group
        prob.root.ln_solver = LinearGaussSeidel()
        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['obj.b'], mode='fwd', return_format='dict')
        Jbase = prob.root.sub.mycomp._jacobian_cache
        assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8)

        J = prob.calc_gradient(['x'], ['obj.b'], mode='rev', return_format='dict')
        Jbase = prob.root.sub.mycomp._jacobian_cache
        assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8)
    def test_simple_array_comp2D_array_lo_hi(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 = pyOptSparseDriver()
        prob.driver.add_param('x',
                              low=-50.0 * np.ones((2, 2)),
                              high=50.0 * np.ones((2, 2)))

        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 test_scaler_adder_array_int(self):

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

        root.add('p1', IndepVarComp('x', val=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])
        root.add('comp', ArrayComp2D(), promotes=['*'])
        root.add('constraint', ExecComp('con = x + y',
                                        x=np.array([[1.0, 1.0], [1.0, 1.0]]),
                                        y=np.array([[1.0, 1.0], [1.0, 1.0]]),
                                        con=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])

        driver.add_desvar('x', lower=np.array([[-1e5, -1e5], [-1e5, -1e5]]),
                          adder=np.array([[10, 100], [1000, 10000]]),
                          scaler=np.array([[1, 2], [3, 4]]))
        driver.add_objective('y', adder=np.array([[10, 100], [1000, 10000]]),
                             scaler=np.array([[1, 2], [3, 4]]))
        driver.add_constraint('con', upper=np.zeros((2, 2)), adder=np.array([[10, 100], [1000, 10000]]),
                              scaler=np.array([[1, 2], [3, 4]]))

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

        self.assertEqual(driver.param[0], 11.0)
        self.assertEqual(driver.param[1], 202.0)
        self.assertEqual(driver.param[2], 3003.0)
        self.assertEqual(driver.param[3], 40004.0)
        self.assertEqual(prob['x'][0, 0], 12.0)
        self.assertEqual(prob['x'][0, 1], 102.0)
        self.assertEqual(prob['x'][1, 0], 2003.0)
        self.assertEqual(prob['x'][1, 1], 20250.0)
        self.assertEqual(driver.obj_scaled[0], (prob['y'][0, 0] + 10.0)*1.0)
        self.assertEqual(driver.obj_scaled[1], (prob['y'][0, 1] + 100.0)*2.0)
        self.assertEqual(driver.obj_scaled[2], (prob['y'][1, 0] + 1000.0)*3.0)
        self.assertEqual(driver.obj_scaled[3], (prob['y'][1, 1] + 10000.0)*4.0)
        self.assertEqual(driver.param_low[0], (-1e5 + 10.0)*1.0)
        self.assertEqual(driver.param_low[1], (-1e5 + 100.0)*2.0)
        self.assertEqual(driver.param_low[2], (-1e5 + 1000.0)*3.0)
        self.assertEqual(driver.param_low[3], (-1e5 + 10000.0)*4.0)
        conval = prob['x'] + prob['y']
        self.assertEqual(driver.con_scaled[0], (conval[0, 0] + 10.0)*1.0)
        self.assertEqual(driver.con_scaled[1], (conval[0, 1] + 100.0)*2.0)
        self.assertEqual(driver.con_scaled[2], (conval[1, 0] + 1000.0)*3.0)
        self.assertEqual(driver.con_scaled[3], (conval[1, 1] + 10000.0)*4.0)

        J = driver.calc_gradient(['x'], ['y', 'con'])
        Jbase = np.array([[  2.,   1.,   3.,   7.],
                          [  4.,   2.,   6.,   5.],
                          [  3.,   6.,   9.,   8.],
                          [  1.,   3.,   2.,   4.],
                          [  3.,   1.,   3.,   7.],
                          [  4.,   3.,   6.,   5.],
                          [  3.,   6.,  10.,   8.],
                          [  1.,   3.,   2.,   5.]])
        assert_rel_error(self, J, Jbase, 1e-6)
    def test_scaler_adder_array(self):

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

        root.add('p1',
                 IndepVarComp('x', val=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])
        root.add('comp', ArrayComp2D(), promotes=['*'])
        root.add('constraint',
                 ExecComp('con = x + y',
                          x=np.array([[1.0, 1.0], [1.0, 1.0]]),
                          y=np.array([[1.0, 1.0], [1.0, 1.0]]),
                          con=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])

        driver.add_desvar('x',
                          lower=np.array([[-1e5, -1e5], [-1e5, -1e5]]),
                          upper=np.array([1e25, 1e25, 1e25, 1e25]),
                          adder=np.array([[10.0, 100.0], [1000.0, 10000.0]]),
                          scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))
        driver.add_objective('y',
                             adder=np.array([[10.0, 100.0], [1000.0,
                                                             10000.0]]),
                             scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))
        driver.add_constraint('con',
                              upper=np.zeros((2, 2)),
                              adder=np.array([[10.0, 100.0], [1000.0,
                                                              10000.0]]),
                              scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))

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

        self.assertEqual(driver.param[0], 11.0)
        self.assertEqual(driver.param[1], 202.0)
        self.assertEqual(driver.param[2], 3003.0)
        self.assertEqual(driver.param[3], 40004.0)
        self.assertEqual(prob['x'][0, 0], 12.0)
        self.assertEqual(prob['x'][0, 1], 102.0)
        self.assertEqual(prob['x'][1, 0], 2003.0)
        self.assertEqual(prob['x'][1, 1], 20250.0)
        self.assertEqual(driver.obj_scaled[0], (prob['y'][0, 0] + 10.0) * 1.0)
        self.assertEqual(driver.obj_scaled[1], (prob['y'][0, 1] + 100.0) * 2.0)
        self.assertEqual(driver.obj_scaled[2],
                         (prob['y'][1, 0] + 1000.0) * 3.0)
        self.assertEqual(driver.obj_scaled[3],
                         (prob['y'][1, 1] + 10000.0) * 4.0)
        self.assertEqual(driver.param_low[0], (-1e5 + 10.0) * 1.0)
        self.assertEqual(driver.param_low[1], (-1e5 + 100.0) * 2.0)
        self.assertEqual(driver.param_low[2], (-1e5 + 1000.0) * 3.0)
        self.assertEqual(driver.param_low[3], (-1e5 + 10000.0) * 4.0)
        conval = prob['x'] + prob['y']
        self.assertEqual(driver.con_scaled[0], (conval[0, 0] + 10.0) * 1.0)
        self.assertEqual(driver.con_scaled[1], (conval[0, 1] + 100.0) * 2.0)
        self.assertEqual(driver.con_scaled[2], (conval[1, 0] + 1000.0) * 3.0)
        self.assertEqual(driver.con_scaled[3], (conval[1, 1] + 10000.0) * 4.0)
Beispiel #7
0
    def test_unknown_vec(self):

        top = Problem()
        top.root = Group()
        top.root.add('comp', ArrayComp2D(), promotes=['x', 'y'])
        top.root.add('p1',
                     IndepVarComp('x', np.array([[1.0, 2.0], [3.0, 4.0]])),
                     promotes=['x'])
        top.root.add('comp2', SimpleComp())
        top.root.add('p2', IndepVarComp('x', 3.0))
        top.root.connect('p2.x', 'comp2.x')

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

        unknowns = ComplexStepSrcVecWrapper(top.root.comp.unknowns)

        # Unknowns are always complex
        y = unknowns['y']
        self.assertTrue(y.dtype == np.complex)
        self.assertEquals(y[0, 1], 46.0 + 0j)

        # Set an unknown
        unknowns['y'][0, 1] = 13.0 + 17.0j
        self.assertEquals(unknowns['y'][0, 1], 13.0 + 17.0j)

        # Extract flat var
        cval = unknowns.flat('y')
        self.assertEquals(cval[1], 13.0 + 17.0j)
        self.assertEquals(cval.shape[0], 4)

        unknowns = ComplexStepSrcVecWrapper(top.root.comp2.unknowns)

        # Unknowns are always complex
        y = unknowns['y']
        self.assertTrue(y.dtype == np.complex)
        self.assertEquals(y, 6.0 + 0j)

        # Set an unknown
        unknowns['y'] = 13.0 + 17.0j
        self.assertEquals(unknowns['y'], 13.0 + 17.0j)

        # Extract flat var
        cval = unknowns.flat('y')
        self.assertEquals(cval, 13.0 + 17.0j)
        self.assertEquals(cval.shape[0], 1)

        # Make sure all other functions work for coverage
        self.assertEquals(len(unknowns), 1)
        self.assertTrue('y' in unknowns)
        plist = [z for z in unknowns]
        self.assertEquals(plist, ['y'])
        self.assertEquals(unknowns.keys(), top.root.comp2.unknowns.keys())
        self.assertEquals(unknowns.metadata('y'),
                          top.root.comp2.unknowns.metadata('y'))
        plist1 = [z for z in unknowns.iterkeys()]
        plist2 = [z for z in top.root.comp2.unknowns.iterkeys()]
    def test_array2D(self):
        group = Group()
        group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        group.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])

        prob = Problem()
        prob.root = group
        prob.root.ln_solver = ScipyGMRES()
        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict')
        Jbase = prob.root.mycomp._jacobian_cache
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict')
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)
Beispiel #9
0
    def test_array2D(self):

        prob = Problem()
        prob.root = root = Group()
        root.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        root.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])

        root.mycomp.deriv_options['step_size'] = 1.0e-1
        root.mycomp.deriv_options['type'] = 'cs'

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

        J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict')
        Jbase = prob.root.mycomp._jacobian_cache
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict')
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)
Beispiel #10
0
    def test_array2D_no_decompose(self):
        group = Group()
        group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        group.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])

        prob = Problem()
        prob.root = group
        prob.root.ln_solver = DirectSolver()
        prob.root.ln_solver.options['jacobian_method'] = 'assemble'
        prob.root.ln_solver.options['save_LU_decomposition'] = False
        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict')
        Jbase = prob.root.mycomp._jacobian_cache
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict')
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)
Beispiel #11
0
    def test_param_vec(self):

        top = Problem()
        top.root = Group()
        top.root.add('comp', ArrayComp2D(), promotes=['x', 'y'])
        top.root.add('p1',
                     IndepVarComp('x', np.array([[1.0, 2.0], [3.0, 4.0]])),
                     promotes=['x'])
        top.root.add('comp2', SimpleComp())
        top.root.add('p2', IndepVarComp('x', 3.0))
        top.root.connect('p2.x', 'comp2.x')

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

        params = ComplexStepTgtVecWrapper(top.root.comp.params)

        # Get a param that isn't complex-stepped
        x = params['x']
        self.assertTrue(x.dtype == np.float)
        self.assertEquals(x[0, 1], 2.0)

        # Get a param that is now complex
        params.set_complex_var('x')
        x = params['x']
        self.assertTrue(x.dtype == np.complex)
        self.assertEquals(x[0, 1], 2.0 + 0j)

        # Apply complex step and get param
        params.step_complex(1, 4.0)
        x = params['x']
        self.assertEquals(x[0, 1], 2.0 + 4j)

        # Unset complex
        params.set_complex_var(None)
        x = params['x']
        self.assertEquals(x[0, 1], 2.0)

        params = ComplexStepTgtVecWrapper(top.root.comp2.params)

        # Get a param that isn't complex-stepped
        x = params['x']
        self.assertTrue(x.dtype == np.float)
        self.assertEquals(x, 3.0)

        # Get a param that is now complex
        params.set_complex_var('x')
        x = params['x']
        self.assertTrue(x.dtype == np.complex)
        self.assertEquals(x, 3.0 + 0j)

        # Apply complex step and get param
        params.step_complex(0, 4.0)
        self.assertEquals(x, 3.0 + 4j)

        # Make sure all other functions work for coverage
        self.assertEquals(len(params), 1)
        self.assertTrue('x' in params)
        plist = [z for z in params]
        self.assertEquals(plist, ['x'])
        self.assertEquals(params.keys(), top.root.comp2.params.keys())
        self.assertEquals(params.metadata('x'),
                          top.root.comp2.params.metadata('x'))
        plist1 = [z for z in params.iterkeys()]
        plist2 = [z for z in top.root.comp2.params.iterkeys()]
    def test_scaler_adder_array(self):
        class ScaleAddDriver(Driver):
            def run(self, problem):
                """ Save away scaled info."""

                params = self.get_desvars()
                param_meta = self.get_desvar_metadata()

                self.set_desvar('x', np.array([22.0, 404.0, 9009.0, 121000.0]))
                problem.root.solve_nonlinear()

                objective = self.get_objectives()
                constraint = self.get_constraints()

                # Stuff we saved should be in the scaled coordinates.
                self.param = params['x']
                self.obj_scaled = objective['y']
                self.con_scaled = constraint['con']
                self.param_low = param_meta['x']['lower']

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

        root.add('p1',
                 IndepVarComp('x', val=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])
        root.add('comp', ArrayComp2D(), promotes=['*'])
        root.add('constraint',
                 ExecComp('con = x + y',
                          x=np.array([[1.0, 1.0], [1.0, 1.0]]),
                          y=np.array([[1.0, 1.0], [1.0, 1.0]]),
                          con=np.array([[1.0, 1.0], [1.0, 1.0]])),
                 promotes=['*'])

        driver.add_desvar('x',
                          lower=np.array([[-1e5, -1e5], [-1e5, -1e5]]),
                          adder=np.array([[10.0, 100.0], [1000.0, 10000.0]]),
                          scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))
        driver.add_objective('y',
                             adder=np.array([[10.0, 100.0], [1000.0,
                                                             10000.0]]),
                             scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))
        driver.add_constraint('con',
                              upper=np.zeros((2, 2)),
                              adder=np.array([[10.0, 100.0], [1000.0,
                                                              10000.0]]),
                              scaler=np.array([[1.0, 2.0], [3.0, 4.0]]))

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

        self.assertEqual(driver.param[0], 11.0)
        self.assertEqual(driver.param[1], 202.0)
        self.assertEqual(driver.param[2], 3003.0)
        self.assertEqual(driver.param[3], 40004.0)
        self.assertEqual(prob['x'][0, 0], 12.0)
        self.assertEqual(prob['x'][0, 1], 102.0)
        self.assertEqual(prob['x'][1, 0], 2003.0)
        self.assertEqual(prob['x'][1, 1], 20250.0)
        self.assertEqual(driver.obj_scaled[0], (prob['y'][0, 0] + 10.0) * 1.0)
        self.assertEqual(driver.obj_scaled[1], (prob['y'][0, 1] + 100.0) * 2.0)
        self.assertEqual(driver.obj_scaled[2],
                         (prob['y'][1, 0] + 1000.0) * 3.0)
        self.assertEqual(driver.obj_scaled[3],
                         (prob['y'][1, 1] + 10000.0) * 4.0)
        self.assertEqual(driver.param_low[0], (-1e5 + 10.0) * 1.0)
        self.assertEqual(driver.param_low[1], (-1e5 + 100.0) * 2.0)
        self.assertEqual(driver.param_low[2], (-1e5 + 1000.0) * 3.0)
        self.assertEqual(driver.param_low[3], (-1e5 + 10000.0) * 4.0)
        conval = prob['x'] + prob['y']
        self.assertEqual(driver.con_scaled[0], (conval[0, 0] + 10.0) * 1.0)
        self.assertEqual(driver.con_scaled[1], (conval[0, 1] + 100.0) * 2.0)
        self.assertEqual(driver.con_scaled[2], (conval[1, 0] + 1000.0) * 3.0)
        self.assertEqual(driver.con_scaled[3], (conval[1, 1] + 10000.0) * 4.0)