def calc_gradient(self, indep_list, unknown_list, mode='auto',
                      return_format='array', dv_scale=None, cn_scale=None,
                      sparsity=None):
        """ Returns the gradient for the system that is specified in
        self.root. This function is used by the optimizer but also can be
        used for testing derivatives on your model.

        Args
        ----
        indep_list : iter of strings
            Iterator of independent variable names that derivatives are to
            be calculated with respect to. All params must have a IndepVarComp.

        unknown_list : iter of strings
            Iterator of output or state names that derivatives are to
            be calculated for. All must be valid unknowns in OpenMDAO.

        mode : string, optional
            Deriviative direction, can be 'fwd', 'rev', 'fd', or 'auto'.
            Default is 'auto', which uses mode specified on the linear solver
            in root.

        return_format : string, optional
            Format for the derivatives, can be 'array' or 'dict'.

        dv_scale : dict, optional
            Dictionary of driver-defined scale factors on the design variables.

        cn_scale : dict, optional
            Dictionary of driver-defined scale factors on the constraints.

        sparsity : dict, optional
            Dictionary that gives the relevant design variables for each
            constraint. This option is only supported in the `dict` return
            format.

        Returns
        -------
        ndarray or dict
            Jacobian of unknowns with respect to params.
        """
        if mode not in ['auto', 'fwd', 'rev', 'fd']:
            msg = "mode must be 'auto', 'fwd', 'rev', or 'fd'"
            raise ValueError(msg)

        if return_format not in ['array', 'dict']:
            msg = "return_format must be 'array' or 'dict'"
            raise ValueError(msg)

        with self.root._dircontext:
            # Either analytic or finite difference
            if mode == 'fd' or self.root.fd_options['force_fd']:
                return self._calc_gradient_fd(indep_list, unknown_list,
                                              return_format, dv_scale=dv_scale,
                                              cn_scale=cn_scale, sparsity=sparsity)
            else:
                return self._calc_gradient_ln_solver(indep_list, unknown_list,
                                                     return_format, mode,
                                                     dv_scale=dv_scale,
                                                     cn_scale=cn_scale,
                                                     sparsity=sparsity)
dataset/ETHPy150Open OpenMDAO/OpenMDAO/openmdao/core/problem.py/Problem.calc_gradient/original