class Solver(object): """ Base solver class. This class is subclassed by NonlinearSolver and LinearSolver, which are in turn subclassed by actual solver implementations. Attributes ---------- _system : <System> Pointer to the owning system. _depth : int How many subsolvers deep this solver is (0 means not a subsolver). _vec_names : [str, ...] List of right-hand-side (RHS) vector names. _mode : str 'fwd' or 'rev', applicable to linear solvers only. _iter_count : int Number of iterations for the current invocation of the solver. _rec_mgr : <RecordingManager> object that manages all recorders added to this solver cite : str Listing of relevant citations that should be referenced when publishing work that uses this class. options : <OptionsDictionary> Options dictionary. recording_options : <OptionsDictionary> Recording options dictionary. supports : <OptionsDictionary> Options dictionary describing what features are supported by this solver. _filtered_vars_to_record : Dict Dict of list of var names to record _norm0 : float Normalization factor _problem_meta : dict Problem level metadata. """ # Object to store some formatting for iprint that is shared across all solvers. SOLVER = 'base_solver' def __init__(self, **kwargs): """ Initialize all attributes. Parameters ---------- **kwargs : dict of keyword arguments Keyword arguments that will be mapped into the Solver options. """ self._system = None self._depth = 0 self._vec_names = None self._mode = 'fwd' self._iter_count = 0 self._problem_meta = None # Solver options self.options = OptionsDictionary(parent_name=self.msginfo) self.options.declare('maxiter', types=int, default=10, desc='maximum number of iterations') self.options.declare('atol', default=1e-10, desc='absolute error tolerance') self.options.declare('rtol', default=1e-10, desc='relative error tolerance') self.options.declare('iprint', types=int, default=1, desc='whether to print output') self.options.declare( 'err_on_non_converge', types=bool, default=False, desc="When True, AnalysisError will be raised if we don't converge." ) # Case recording options self.recording_options = OptionsDictionary(parent_name=self.msginfo) self.recording_options.declare( 'record_abs_error', types=bool, default=True, desc='Set to True to record absolute error at the \ solver level') self.recording_options.declare( 'record_rel_error', types=bool, default=True, desc='Set to True to record relative error at the \ solver level') self.recording_options.declare( 'record_inputs', types=bool, default=True, desc='Set to True to record inputs at the solver level') self.recording_options.declare( 'record_outputs', types=bool, default=True, desc='Set to True to record outputs at the solver level') self.recording_options.declare( 'record_solver_residuals', types=bool, default=False, desc='Set to True to record residuals at the solver level') self.recording_options.declare( 'record_metadata', types=bool, desc='Deprecated. Recording ' 'of metadata will always be done', deprecation="The recording option, record_metadata, on " "Solver is " "deprecated. Recording of metadata will always be done", default=True) self.recording_options.declare( 'includes', types=list, default=['*'], desc="Patterns for variables to include in recording. \ Paths are relative to solver's Group. \ Uses fnmatch wildcards") self.recording_options.declare( 'excludes', types=list, default=[], desc="Patterns for vars to exclude in recording. \ (processed post-includes) \ Paths are relative to solver's Group. \ Uses fnmatch wildcards") # Case recording related self._filtered_vars_to_record = {} self._norm0 = 0.0 # What the solver supports. self.supports = OptionsDictionary(parent_name=self.msginfo) self.supports.declare('gradients', types=bool, default=False) self.supports.declare('implicit_components', types=bool, default=False) self._declare_options() self.options.update(kwargs) self._rec_mgr = RecordingManager() self.cite = "" @property def msginfo(self): """ Return info to prepend to messages. Returns ------- str Info to prepend to messages. """ if self._system is None: return type(self).__name__ return '{} in {}'.format(type(self).__name__, self._system().msginfo) @property def _recording_iter(self): if self._problem_meta is None: raise RuntimeError( f"{self.msginfo}: Can't access recording_iter because " "_setup_solvers has not been called.") return self._problem_meta['recording_iter'] @property def _solver_info(self): if self._problem_meta is None: raise RuntimeError( f"{self.msginfo}: Can't access solver_info because _setup_solvers " "has not been called.") return self._problem_meta['solver_info'] def _assembled_jac_solver_iter(self): """ Return an empty generator of lin solvers using assembled jacs. """ for i in (): yield def add_recorder(self, recorder): """ Add a recorder to the solver's RecordingManager. Parameters ---------- recorder : <CaseRecorder> A recorder instance to be added to RecManager. """ if MPI: raise RuntimeError( "Recording of Solvers when running parallel code is not supported yet" ) self._rec_mgr.append(recorder) def _declare_options(self): """ Declare options before kwargs are processed in the init method. This is optionally implemented by subclasses of Solver. """ pass def _setup_solvers(self, system, depth): """ Assign system instance, set depth, and optionally perform setup. Parameters ---------- system : <System> pointer to the owning system. depth : int depth of the current system (already incremented). """ self._system = weakref.ref(system) self._depth = depth self._problem_meta = system._problem_meta if system.pathname: parent_name = self.msginfo self.options._parent_name = parent_name self.recording_options._parent_name = parent_name self.supports._parent_name = parent_name if isinstance(self, LinearSolver) and not system._use_derivatives: return self._rec_mgr.startup(self) myoutputs = myresiduals = myinputs = [] incl = self.recording_options['includes'] excl = self.recording_options['excludes'] # doesn't matter if we're a linear or nonlinear solver. The names for # inputs, outputs, and residuals are the same for both the 'linear' and 'nonlinear' # vectors. if system.pathname: incl = ['.'.join((system.pathname, i)) for i in incl] excl = ['.'.join((system.pathname, i)) for i in excl] if self.recording_options['record_solver_residuals']: myresiduals = [ n for n in system._residuals._abs_iter() if check_path(n, incl, excl) ] if self.recording_options['record_outputs']: myoutputs = [ n for n in system._outputs._abs_iter() if check_path(n, incl, excl) ] if self.recording_options['record_inputs']: myinputs = [ n for n in system._inputs._abs_iter() if check_path(n, incl, excl) ] self._filtered_vars_to_record = { 'input': myinputs, 'output': myoutputs, 'residual': myresiduals } def _set_solver_print(self, level=2, type_='all'): """ Control printing for solvers and subsolvers in the model. Parameters ---------- level : int iprint level. Set to 2 to print residuals each iteration; set to 1 to print just the iteration totals; set to 0 to disable all printing except for failures, and set to -1 to disable all printing including failures. type_ : str Type of solver to set: 'LN' for linear, 'NL' for nonlinear, or 'all' for all. """ self.options['iprint'] = level def _mpi_print(self, iteration, abs_res, rel_res): """ Print residuals from an iteration. Parameters ---------- iteration : int iteration counter, 0-based. abs_res : float current absolute residual norm. rel_res : float current relative residual norm. """ if (self.options['iprint'] == 2 and (self._system().comm.rank == 0 or os.environ.get('USE_PROC_FILES'))): prefix = self._solver_info.prefix solver_name = self.SOLVER if prefix.endswith('precon:'): solver_name = solver_name[3:] print_str = prefix + solver_name print_str += ' %d ; %.9g %.9g' % (iteration, abs_res, rel_res) print(print_str) def _mpi_print_header(self): """ Print header text before solving. """ if (self.options['iprint'] > 0 and (self._system().comm.rank == 0 or os.environ.get('USE_PROC_FILES'))): pathname = self._system().pathname if pathname: nchar = len(pathname) prefix = self._solver_info.prefix header = prefix + "\n" header += prefix + nchar * "=" + "\n" header += prefix + pathname + "\n" header += prefix + nchar * "=" print(header) def _iter_initialize(self): """ Perform any necessary pre-processing operations. Returns ------- float initial error. float error at the first iteration. """ pass def _run_apply(self): """ Run the appropriate apply method on the system. """ pass def _linearize(self): """ Perform any required linearization operations such as matrix factorization. """ pass def _linearize_children(self): """ Return a flag that is True when we need to call linearize on our subsystems' solvers. Returns ------- boolean Flag for indicating child linerization """ return True def __str__(self): """ Return a string representation of the solver. Returns ------- str String representation of the solver. """ return self.SOLVER def record_iteration(self, **kwargs): """ Record an iteration of the current Solver. Parameters ---------- **kwargs : dict Keyword arguments (used for abs and rel error). """ if not self._rec_mgr._recorders: return metadata = create_local_meta(self.SOLVER) # Get the data data = { 'abs': kwargs.get('abs') if self.recording_options['record_abs_error'] else None, 'rel': kwargs.get('rel') if self.recording_options['record_rel_error'] else None, 'input': {}, 'output': {}, 'residual': {} } system = self._system() vec_name = 'nonlinear' if isinstance(self, NonlinearSolver) else 'linear' filt = self._filtered_vars_to_record parallel = self._rec_mgr._check_parallel( ) if system.comm.size > 1 else False if self.recording_options['record_outputs']: data['output'] = system._retrieve_data_of_kind( filt, 'output', vec_name, parallel) if self.recording_options['record_inputs']: data['input'] = system._retrieve_data_of_kind( filt, 'input', vec_name, parallel) if self.recording_options['record_solver_residuals']: data['residual'] = system._retrieve_data_of_kind( filt, 'residual', vec_name, parallel) self._rec_mgr.record_iteration(self, data, metadata) def cleanup(self): """ Clean up resources prior to exit. """ # shut down all recorders self._rec_mgr.shutdown() def _set_complex_step_mode(self, active): """ Turn on or off complex stepping mode. Recurses to turn on or off complex stepping mode in all subsystems and their vectors. Parameters ---------- active : bool Complex mode flag; set to True prior to commencing complex step. """ pass def _disallow_distrib_solve(self): """ Raise an exception if our system or any subsystems are distributed or non-local. """ s = self._system() if s.comm.size == 1: return from openmdao.core.group import Group if s._has_distrib_vars or (isinstance(s, Group) and s._contains_parallel_group): msg = "{} linear solver in {} cannot be used in or above a ParallelGroup or a " + \ "distributed component." raise RuntimeError(msg.format(type(self).__name__, s.msginfo))
class Solver(object): """ Base solver class. This class is subclassed by NonlinearSolver and LinearSolver, which are in turn subclassed by actual solver implementations. Attributes ---------- _system : <System> Pointer to the owning system. _depth : int How many subsolvers deep this solver is (0 means not a subsolver). _vec_names : [str, ...] List of right-hand-side (RHS) vector names. _mode : str 'fwd' or 'rev', applicable to linear solvers only. _iter_count : int Number of iterations for the current invocation of the solver. _rec_mgr : <RecordingManager> object that manages all recorders added to this solver cite : str Listing of relevant citations that should be referenced when publishing work that uses this class. options : <OptionsDictionary> Options dictionary. recording_options : <OptionsDictionary> Recording options dictionary. supports : <OptionsDictionary> Options dictionary describing what features are supported by this solver. _filtered_vars_to_record: Dict Dict of list of var names to record _norm0: float Normalization factor _solver_info : SolverInfo A stack-like object shared by all Solvers in the model. """ # Object to store some formatting for iprint that is shared across all solvers. SOLVER = 'base_solver' def __init__(self, **kwargs): """ Initialize all attributes. Parameters ---------- **kwargs : dict of keyword arguments Keyword arguments that will be mapped into the Solver options. """ self._system = None self._depth = 0 self._vec_names = None self._mode = 'fwd' self._iter_count = 0 self._solver_info = None # Solver options self.options = OptionsDictionary(parent_name=self.msginfo) self.options.declare('maxiter', types=int, default=10, desc='maximum number of iterations') self.options.declare('atol', default=1e-10, desc='absolute error tolerance') self.options.declare('rtol', default=1e-10, desc='relative error tolerance') self.options.declare('iprint', types=int, default=1, desc='whether to print output') self.options.declare('err_on_maxiter', types=bool, default=None, allow_none=True, desc="Deprecated. Use 'err_on_non_converge'.") self.options.declare( 'err_on_non_converge', types=bool, default=False, desc="When True, AnalysisError will be raised if we don't converge." ) # Case recording options self.recording_options = OptionsDictionary(parent_name=self.msginfo) self.recording_options.declare( 'record_abs_error', types=bool, default=True, desc='Set to True to record absolute error at the \ solver level') self.recording_options.declare( 'record_rel_error', types=bool, default=True, desc='Set to True to record relative error at the \ solver level') self.recording_options.declare( 'record_inputs', types=bool, default=True, desc='Set to True to record inputs at the solver level') self.recording_options.declare( 'record_outputs', types=bool, default=True, desc='Set to True to record outputs at the solver level') self.recording_options.declare( 'record_solver_residuals', types=bool, default=False, desc='Set to True to record residuals at the solver level') self.recording_options.declare('record_metadata', types=bool, desc='Record metadata', default=True) self.recording_options.declare( 'includes', types=list, default=['*'], desc="Patterns for variables to include in recording. \ Paths are relative to solver's Group. \ Uses fnmatch wildcards") self.recording_options.declare( 'excludes', types=list, default=[], desc="Patterns for vars to exclude in recording. \ (processed post-includes) \ Paths are relative to solver's Group. \ Uses fnmatch wildcards") # Case recording related self._filtered_vars_to_record = {} self._norm0 = 0.0 # What the solver supports. self.supports = OptionsDictionary(parent_name=self.msginfo) self.supports.declare('gradients', types=bool, default=False) self.supports.declare('implicit_components', types=bool, default=False) self._declare_options() self.options.update(kwargs) self._rec_mgr = RecordingManager() self.cite = "" @property def msginfo(self): """ Return info to prepend to messages. Returns ------- str Info to prepend to messages. """ if self._system is None: return type(self).__name__ return '{} in {}'.format(type(self).__name__, self._system().msginfo) def _assembled_jac_solver_iter(self): """ Return an empty generator of lin solvers using assembled jacs. """ for i in (): yield def add_recorder(self, recorder): """ Add a recorder to the solver's RecordingManager. Parameters ---------- recorder : <CaseRecorder> A recorder instance to be added to RecManager. """ if MPI: raise RuntimeError( "Recording of Solvers when running parallel code is not supported yet" ) self._rec_mgr.append(recorder) def _declare_options(self): """ Declare options before kwargs are processed in the init method. This is optionally implemented by subclasses of Solver. """ pass def _setup_solvers(self, system, depth): """ Assign system instance, set depth, and optionally perform setup. Parameters ---------- system : <System> pointer to the owning system. depth : int depth of the current system (already incremented). """ self._system = weakref.ref(system) self._depth = depth self._solver_info = system._solver_info self._recording_iter = system._recording_iter if system.pathname: parent_name = self.msginfo self.options._parent_name = parent_name self.recording_options._parent_name = parent_name self.supports._parent_name = parent_name if isinstance(self, LinearSolver) and not system._use_derivatives: return self._rec_mgr.startup(self) self._rec_mgr.record_metadata(self) myoutputs = myresiduals = myinputs = [] incl = self.recording_options['includes'] excl = self.recording_options['excludes'] # doesn't matter if we're a linear or nonlinear solver. The names for # inputs, outputs, and residuals are the same for both the 'linear' and 'nonlinear' # vectors. if system.pathname: incl = ['.'.join((system.pathname, i)) for i in incl] excl = ['.'.join((system.pathname, i)) for i in excl] if self.recording_options['record_solver_residuals']: myresiduals = [ n for n in system._residuals._views if check_path(n, incl, excl) ] if self.recording_options['record_outputs']: myoutputs = [ n for n in system._outputs._views if check_path(n, incl, excl) ] if self.recording_options['record_inputs']: myinputs = [ n for n in system._inputs._views if check_path(n, incl, excl) ] self._filtered_vars_to_record = { 'input': myinputs, 'output': myoutputs, 'residual': myresiduals } # Raise a deprecation warning for changed option. if 'err_on_maxiter' in self.options and self.options[ 'err_on_maxiter'] is not None: self.options['err_on_non_converge'] = self.options[ 'err_on_maxiter'] warn_deprecation( "The 'err_on_maxiter' option provides backwards compatibility " "with earlier version of OpenMDAO; use options['err_on_non_converge'] " "instead.") def _set_solver_print(self, level=2, type_='all'): """ Control printing for solvers and subsolvers in the model. Parameters ---------- level : int iprint level. Set to 2 to print residuals each iteration; set to 1 to print just the iteration totals; set to 0 to disable all printing except for failures, and set to -1 to disable all printing including failures. type_ : str Type of solver to set: 'LN' for linear, 'NL' for nonlinear, or 'all' for all. """ self.options['iprint'] = level def _mpi_print(self, iteration, abs_res, rel_res): """ Print residuals from an iteration. Parameters ---------- iteration : int iteration counter, 0-based. abs_res : float current absolute residual norm. rel_res : float current relative residual norm. """ if (self.options['iprint'] == 2 and self._system().comm.rank == 0): prefix = self._solver_info.prefix solver_name = self.SOLVER if prefix.endswith('precon:'): solver_name = solver_name[3:] print_str = prefix + solver_name print_str += ' %d ; %.9g %.9g' % (iteration, abs_res, rel_res) print(print_str) def _mpi_print_header(self): """ Print header text before solving. """ pass def _solve(self): """ Run the iterative solver. """ maxiter = self.options['maxiter'] atol = self.options['atol'] rtol = self.options['rtol'] iprint = self.options['iprint'] self._mpi_print_header() self._iter_count = 0 norm0, norm = self._iter_initialize() self._norm0 = norm0 self._mpi_print(self._iter_count, norm, norm / norm0) while self._iter_count < maxiter and norm > atol and norm / norm0 > rtol: with Recording(type(self).__name__, self._iter_count, self) as rec: self._single_iteration() self._iter_count += 1 self._run_apply() norm = self._iter_get_norm() # With solvers, we want to record the norm AFTER the call, but the call needs to # be wrapped in the with for stack purposes, so we locally assign norm & norm0 # into the class. rec.abs = norm rec.rel = norm / norm0 if norm0 == 0: norm0 = 1 self._mpi_print(self._iter_count, norm, norm / norm0) system = self._system() if system.comm.rank == 0 or os.environ.get('USE_PROC_FILES'): prefix = self._solver_info.prefix + self.SOLVER # Solver terminated early because a Nan in the norm doesn't satisfy the while-loop # conditionals. if np.isinf(norm) or np.isnan(norm): msg = "Solver '{}' on system '{}': residuals contain 'inf' or 'NaN' after {} " + \ "iterations." if iprint > -1: print(prefix + msg.format(self.SOLVER, system.pathname, self._iter_count)) # Raise AnalysisError if requested. if self.options['err_on_non_converge']: raise AnalysisError( msg.format(self.SOLVER, system.pathname, self._iter_count)) # Solver hit maxiter without meeting desired tolerances. elif (norm > atol and norm / norm0 > rtol): msg = "Solver '{}' on system '{}' failed to converge in {} iterations." if iprint > -1: print(prefix + msg.format(self.SOLVER, system.pathname, self._iter_count)) # Raise AnalysisError if requested. if self.options['err_on_non_converge']: raise AnalysisError( msg.format(self.SOLVER, system.pathname, self._iter_count)) # Solver converged elif iprint == 1: print(prefix + ' Converged in {} iterations'.format(self._iter_count)) elif iprint == 2: print(prefix + ' Converged') def _iter_initialize(self): """ Perform any necessary pre-processing operations. Returns ------- float initial error. float error at the first iteration. """ pass def _run_apply(self): """ Run the appropriate apply method on the system. """ pass def _linearize(self): """ Perform any required linearization operations such as matrix factorization. """ pass def _linearize_children(self): """ Return a flag that is True when we need to call linearize on our subsystems' solvers. Returns ------- boolean Flag for indicating child linerization """ return True def __str__(self): """ Return a string representation of the solver. Returns ------- str String representation of the solver. """ return self.SOLVER def record_iteration(self, **kwargs): """ Record an iteration of the current Solver. Parameters ---------- **kwargs : dict Keyword arguments (used for abs and rel error). """ if not self._rec_mgr._recorders: return metadata = create_local_meta(self.SOLVER) # Get the data data = { 'abs': kwargs.get('abs') if self.recording_options['record_abs_error'] else None, 'rel': kwargs.get('rel') if self.recording_options['record_rel_error'] else None, 'input': {}, 'output': {}, 'residual': {} } system = self._system() vec_name = 'nonlinear' if isinstance(self, NonlinearSolver) else 'linear' filt = self._filtered_vars_to_record parallel = self._rec_mgr._check_parallel( ) if system.comm.size > 1 else False if self.recording_options['record_outputs']: data['output'] = system._retrieve_data_of_kind( filt, 'output', vec_name, parallel) if self.recording_options['record_inputs']: data['input'] = system._retrieve_data_of_kind( filt, 'input', vec_name, parallel) if self.recording_options['record_solver_residuals']: data['residual'] = system._retrieve_data_of_kind( filt, 'residual', vec_name, parallel) self._rec_mgr.record_iteration(self, data, metadata) def cleanup(self): """ Clean up resources prior to exit. """ # shut down all recorders self._rec_mgr.shutdown() def _set_complex_step_mode(self, active): """ Turn on or off complex stepping mode. Recurses to turn on or off complex stepping mode in all subsystems and their vectors. Parameters ---------- active : bool Complex mode flag; set to True prior to commencing complex step. """ pass def _disallow_distrib_solve(self): """ Raise an exception if our system or any subsystems are distributed or non-local. """ s = self._system() if s.comm.size == 1: return from openmdao.core.group import Group if (isinstance(s, Group) and s._has_distrib_vars) or (isinstance(s, Component) and s.options['distributed']): raise RuntimeError( "%s has a %s solver and contains a distributed system." % (s.msginfo, type(self).__name__)) if not (np.all(s._var_sizes['nonlinear']['output']) and np.all(s._var_sizes['nonlinear']['input'])): raise RuntimeError( "%s has a %s solver and contains remote variables." % (s.msginfo, type(self).__name__))