def test_correct_vals_in_jac(self):

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

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

        resids_dict = OrderedDict()
        resids_dict['y'] = { 'val': np.zeros((2)),
                             'pathname' : 'y',
                             'promoted_name' : '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)

        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_dict = OrderedDict()
        params_dict['x'] = { 'val': np.ones((2, 2)),
                             'pathname' : 'x',
                             'promoted_name' : 'x',
                             'shape': (2,2), 'size' : 4 }

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

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

        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)

        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_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)