<s> """<STR_LIT>""" <EOL> from math import isnan <EOL> from openmdao . core . system import AnalysisError <EOL> from openmdao . solvers . backtracking import BackTracking <EOL> from openmdao . solvers . solver_base import NonLinearSolver <EOL> from openmdao . util . record_util import update_local_meta , create_local_meta <EOL> class Newton ( NonLinearSolver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( Newton , self ) . __init__ ( ) <EOL> self . supports [ '<STR_LIT>' ] = True <EOL> opt = self . options <EOL> opt . add_option ( '<STR_LIT>' , <NUM_LIT> , lower = <NUM_LIT:0.0> , <EOL> desc = '<STR_LIT>' ) <EOL> opt . add_option ( '<STR_LIT>' , <NUM_LIT> , lower = <NUM_LIT:0.0> , <EOL> desc = '<STR_LIT>' ) <EOL> opt . add_option ( '<STR_LIT>' , <NUM_LIT:20> , lower = <NUM_LIT:0> , <EOL> desc = '<STR_LIT>' ) <EOL> opt . add_option ( '<STR_LIT>' , <NUM_LIT:1.0> , <EOL> desc = '<STR_LIT>' ) <EOL> opt . add_option ( '<STR_LIT>' , True , <EOL> desc = '<STR_LIT>' ) <EOL> self . print_name = '<STR_LIT>' <EOL> self . line_search = BackTracking ( ) <EOL> self . ln_solver = None <EOL> def setup ( self , sub ) : <EOL> """<STR_LIT>""" <EOL> if self . line_search : <EOL> self . line_search . setup ( sub ) <EOL> if self . ln_solver : <EOL> self . ln_solver . setup ( sub ) <EOL> def solve ( self , params , unknowns , resids , system , metadata = None ) : <EOL> """<STR_LIT>""" <EOL> atol = self . options [ '<STR_LIT>' ] <EOL> rtol = self . options [ '<STR_LIT>' ] <EOL> maxiter = self . options [ '<STR_LIT>' ] <EOL> alpha = self . options [ '<STR_LIT>' ] <EOL> self . iter_count = <NUM_LIT:0> <EOL> local_meta = create_local_meta ( metadata , system . pathname ) <EOL> system . ln_solver . local_meta = local_meta <EOL> update_local_meta ( local_meta , ( self . iter_count , <NUM_LIT:0> ) ) <EOL> system . children_solve_nonlinear ( local_meta ) <EOL> system . apply_nonlinear ( params , unknowns , resids ) <EOL> f_norm = resids . norm ( ) <EOL> f_norm0 = f_norm <EOL> if self . options [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> self . print_norm ( self . print_name , system . pathname , <NUM_LIT:0> , f_norm , <EOL> f_norm0 ) <EOL> arg = system . drmat [ None ] <EOL> result = system . dumat [ None ] <EOL> while self . iter_count < maxiter and f_norm > atol and f_norm / f_norm0 > rtol : <EOL> system . _sys_linearize ( params , unknowns , resids , total_derivs = False ) <EOL> arg . vec [ : ] = - resids . vec <EOL> with system . _dircontext : <EOL> system . solve_linear ( system . dumat , system . drmat , <EOL> [ None ] , mode = '<STR_LIT>' , solver = self . ln_solver ) <EOL> self . iter_count += <NUM_LIT:1> <EOL> f_norm = self . line_search . solve ( params , unknowns , resids , system , <EOL> self , alpha , f_norm0 , metadata ) <EOL> if self . iter_count >= maxiter or isnan ( f_norm ) : <EOL> msg = '<STR_LIT>' % self . iter_count <EOL> fail = True <EOL> else : <EOL> fail = False <EOL> if self . options [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> if not fail : <EOL> msg = '<STR_LIT>' <EOL> self . print_norm ( self . print_name , system . pathname , self . iter_count , <EOL> f_norm , f_norm0 , msg = msg ) <EOL> if fail and self . options [ '<STR_LIT>' ] : <EOL> raise AnalysisError ( "<STR_LIT>" % ( system . pathname , <EOL> msg ) ) <EOL> def print_all_convergence ( self ) : <EOL> """<STR_LIT>""" <EOL> self . options [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . line_search . options [ '<STR_LIT>' ] = <NUM_LIT:1> </s>
