Exemple #1
0
    def test_correct_vals_in_jac(self):

        #params_dict = OrderedDict()
        #params_dict['x'] = { 'val': np.ones((2)),
        #'pathname' : 'x',
        #'shape': 2, 'size' : 2 }

        #unknowns_dict = OrderedDict()
        #unknowns_dict['y'] = { 'val': np.zeros((2)),
        #'pathname' : 'y',
        #'shape': 2, 'size' : 2 }

        #resids_dict = OrderedDict()
        #resids_dict['y'] = { 'val': np.zeros((2)),
        #'pathname' : 'y',
        #'shape': 2, 'size' : 2 }

        #params = SrcVecWrapper()
        #params.setup(params_dict, store_byobjs=True)

        #unknowns = SrcVecWrapper()
        #unknowns.setup(unknowns_dict, store_byobjs=True)

        #resids = SrcVecWrapper()
        #resids.setup(resids_dict, store_byobjs=True)

        params = self.p.root.c1.params
        unknowns = self.p.root.c1.unknowns
        resids = self.p.root.c1.resids
        self.p.root.c1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.c1.fd_jacobian(params, unknowns, resids)

        expected_jac = {('y', 'x'): np.array([[2., 7.], [5., -3.]])}

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        #Got lucky that the way this comp was written, it would accept any square
        # matrix. But provided jacobian would be really wrong!

        params = self.p.root.junk.params
        unknowns = self.p.root.junk.unknowns
        resids = self.p.root.junk.resids

        params['x'] = np.ones((2, 2))
        unknowns['y'] = np.zeros((2, 2))
        resids['y'] = np.zeros((2, 2))

        self.p.root.c1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.c1.fd_jacobian(params, unknowns, resids)

        expected_jac = {
            ('y', 'x'):
            np.array([[2, 0, 7, 0], [0, 2, 0, 7], [5, 0, -3, 0], [0, 5, 0,
                                                                  -3]])
        }

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)
    def test_correct_vals_in_jac(self):

        #params_dict = OrderedDict()
        #params_dict['x'] = { 'val': np.ones((2)),
                             #'pathname' : 'x',
                             #'shape': 2, 'size' : 2 }

        #unknowns_dict = OrderedDict()
        #unknowns_dict['y'] = { 'val': np.zeros((2)),
                               #'pathname' : 'y',
                             #'shape': 2, 'size' : 2 }

        #resids_dict = OrderedDict()
        #resids_dict['y'] = { 'val': np.zeros((2)),
                             #'pathname' : 'y',
                             #'shape': 2, 'size' : 2 }

        #params = SrcVecWrapper()
        #params.setup(params_dict, store_byobjs=True)

        #unknowns = SrcVecWrapper()
        #unknowns.setup(unknowns_dict, store_byobjs=True)

        #resids = SrcVecWrapper()
        #resids.setup(resids_dict, store_byobjs=True)

        params = self.p.root.c1.params
        unknowns = self.p.root.c1.unknowns
        resids = self.p.root.c1.resids
        self.p.root.c1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.c1.fd_jacobian(params, unknowns, resids)

        expected_jac = {('y', 'x'): np.array([[ 2.,  7.],[ 5., -3.]])}

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        #Got lucky that the way this comp was written, it would accept any square
        # matrix. But provided jacobian would be really wrong!

        params = self.p.root.junk.params
        unknowns = self.p.root.junk.unknowns
        resids = self.p.root.junk.resids

        params['x'] = np.ones((2,2))
        unknowns['y'] = np.zeros((2,2))
        resids['y'] = np.zeros((2,2))

        self.p.root.c1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.c1.fd_jacobian(params, unknowns, resids)

        expected_jac = {('y', 'x'): np.array([[ 2,  0,  7,  0],
                                              [ 0,  2,  0,  7],
                                              [ 5,  0, -3,  0],
                                              [ 0,  5,  0, -3]
                                             ])}

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)
Exemple #3
0
    def test_correct_vals_in_jac_implicit(self):

        # Partials

        params = self.p.root.ci1.params
        unknowns = self.p.root.ci1.unknowns
        resids = self.p.root.ci1.resids

        params['x'] = np.array([0.5])
        unknowns['y'] = np.array([0.0])
        unknowns['z'] = np.array([0.0])
        resids['y'] = np.array([0.0])
        resids['z'] = np.array([0.0])

        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids)
        expected_jac = {}
        # Output equation
        expected_jac[('y', 'x')] = 1.
        expected_jac[('y', 'z')] = 2.0
        # State equation
        expected_jac[('z', 'z')] = params['x'] + 1
        expected_jac[('z', 'x')] = unknowns['z']

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        # Totals

        # Really tighten this up
        self.p.root.ci1.atol = 1e-14
        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params,
                                          unknowns,
                                          resids,
                                          total_derivs=True)
        expected_jac = {}
        expected_jac[('y', 'x')] = -2.5555555555555554
        expected_jac[('z', 'x')] = -1.7777777777777777

        assert_equal_jacobian(self, jac, expected_jac, 1e-5)
    def test_correct_vals_in_jac_implicit(self):

        # Partials

        params = self.p.root.ci1.params
        unknowns = self.p.root.ci1.unknowns
        resids = self.p.root.ci1.resids

        params['x'] = np.array([0.5])
        unknowns['y'] = np.array([0.0])
        unknowns['z'] = np.array([0.0])
        resids['y'] = np.array([0.0])
        resids['z'] = np.array([0.0])

        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids)
        expected_jac = {}
        # Output equation
        expected_jac[('y', 'x')] = 1.
        expected_jac[('y', 'z')] = 2.0
        # State equation
        expected_jac[('z', 'z')] = params['x'] + 1
        expected_jac[('z', 'x')] = unknowns['z']

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        # Totals

        # Really tighten this up
        self.p.root.ci1.atol = 1e-14
        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids, total_derivs=True)
        expected_jac = {}
        expected_jac[('y', 'x')] = -2.5555555555555554
        expected_jac[('z', 'x')] = -1.7777777777777777

        assert_equal_jacobian(self, jac, expected_jac, 1e-5)
    def test_correct_vals_in_jac_implicit(self):

        params_dict = OrderedDict()
        params_dict['x'] = {
            'val': np.array([0.5]),
            'pathname': 'x',
            'promoted_name': 'x',
            'shape': (1, ),
            'size': 1
        }

        unknowns_dict = OrderedDict()
        unknowns_dict['y'] = {
            'val': np.array([0.0]),
            'pathname': 'y',
            'promoted_name': 'y',
            'shape': (1, ),
            'size': 1
        }
        unknowns_dict['z'] = {
            'val': np.array([0.0]),
            'pathname': 'z',
            'promoted_name': 'z',
            'shape': (1, ),
            'size': 1
        }

        resids_dict = OrderedDict()
        resids_dict['y'] = {
            'val': np.array([0.0]),
            'pathname': 'y',
            'promoted_name': 'y',
            'shape': (1, ),
            'size': 1
        }
        resids_dict['z'] = {
            'val': np.array([0.0]),
            'pathname': 'z',
            'promoted_name': 'z',
            'shape': (1, ),
            'size': 1
        }

        params = SrcVecWrapper()
        params.setup(params_dict, store_byobjs=True)

        unknowns = SrcVecWrapper()
        unknowns.setup(unknowns_dict, store_byobjs=True)

        resids = SrcVecWrapper()
        resids.setup(resids_dict, store_byobjs=True)

        # Partials

        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids)
        expected_jac = {}
        # Output equation
        expected_jac[('y', 'x')] = 1.
        expected_jac[('y', 'z')] = 2.0
        # State equation
        expected_jac[('z', 'z')] = params['x'] + 1
        expected_jac[('z', 'x')] = unknowns['z']

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        # Totals

        # Really tighten this up
        self.p.root.ci1.atol = 1e-14
        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params,
                                          unknowns,
                                          resids,
                                          total_derivs=True)
        expected_jac = {}
        expected_jac[('y', 'x')] = -2.5555555555555554
        expected_jac[('z', 'x')] = -1.7777777777777777

        assert_equal_jacobian(self, jac, expected_jac, 1e-5)
    def test_correct_vals_in_jac_implicit(self):

        params_dict = OrderedDict()
        params_dict['x'] = { 'val': np.array([0.5]),
                             'pathname' : 'x',
                             'promoted_name' : 'x',
                             'shape': (1,), 'size' : 1 }

        unknowns_dict = OrderedDict()
        unknowns_dict['y'] = { 'val': np.array([0.0]),
                               'pathname' : 'y',
                               'promoted_name' : 'y',
                             'shape': (1,), 'size' : 1 }
        unknowns_dict['z'] = { 'val': np.array([0.0]),
                               'pathname' : 'z',
                               'promoted_name' : 'z',
                             'shape': (1,), 'size' : 1 }

        resids_dict = OrderedDict()
        resids_dict['y'] = { 'val': np.array([0.0]),
                             'pathname' : 'y',
                             'promoted_name' : 'y',
                             'shape': (1,), 'size' : 1 }
        resids_dict['z'] = { 'val': np.array([0.0]),
                             'pathname' : 'z',
                             'promoted_name' : 'z',
                             'shape': (1,), 'size' : 1 }

        params = SrcVecWrapper()
        params.setup(params_dict, store_byobjs=True)

        unknowns = SrcVecWrapper()
        unknowns.setup(unknowns_dict, store_byobjs=True)

        resids = SrcVecWrapper()
        resids.setup(resids_dict, store_byobjs=True)

        # Partials

        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids)
        expected_jac = {}
        # Output equation
        expected_jac[('y', 'x')] = 1.
        expected_jac[('y', 'z')] = 2.0
        # State equation
        expected_jac[('z', 'z')] = params['x'] + 1
        expected_jac[('z', 'x')] = unknowns['z']

        assert_equal_jacobian(self, jac, expected_jac, 1e-8)

        # Totals

        # Really tighten this up
        self.p.root.ci1.atol = 1e-14
        self.p.root.ci1.solve_nonlinear(params, unknowns, resids)

        jac = self.p.root.ci1.fd_jacobian(params, unknowns, resids, total_derivs=True)
        expected_jac = {}
        expected_jac[('y', 'x')] = -2.5555555555555554
        expected_jac[('z', 'x')] = -1.7777777777777777

        assert_equal_jacobian(self, jac, expected_jac, 1e-5)