def ImpEulerStep(sys_func: Callable[[Vec_t, Vec_t, float], Vec_t],
                 x_n: Vec_t,
                 t_n: float,
                 h: float,
                 atol: float = 1.e-8,
                 rtol: float = 1.0e-5,
                 maxit: int = 20,
                 use_scipy: Optional[Union[bool, str]] = None,
                 use_cycADa: Optional[bool] = False,
                 *args,
                 **kwargs):
    dim_x: int = len(x_n)
    t_n1 = t_n + h

    def inner_step_func(x_n1):
        return sys_func((x_n1 - x_n) / h, x_n1, t_n1)

    if use_cycADa:
        _inner_step_func, _inner_step_Dfunc = cycADa_wrapper(
            inner_step_func, dim_x)
    else:
        _inner_step_func, _inner_step_Dfunc = inner_step_func, None
    ''' solve nonlinear root problem '''
    if (isinstance(use_scipy, bool) and
        (not use_scipy)) or (use_scipy is None):
        options_sn_inst = sparse_newton_options(atol_range=atol,
                                                atol_dom=atol,
                                                rtol_range=rtol,
                                                rtol_dom=rtol,
                                                atol_root=atol,
                                                max_it=maxit)
        report = sparse_nl_solve(_inner_step_func,
                                 jac=_inner_step_Dfunc,
                                 x0=x_n,
                                 options=options_sn_inst,
                                 linsolver=spsolve_simple_wrapper())  #,
        # callback = callback_matrix_spy()) #,
        # callback = callback_print_progress())
    elif (isinstance(use_scipy, bool)
          and use_scipy) or (isinstance(use_scipy, str) and
                             (use_scipy == 'least_squares')):
        report = least_squares(inner_step_func,
                               x0=x_n,
                               ftol=1.0e-12,
                               xtol=1.0e-12,
                               gtol=1.0e-12)
    elif isinstance(use_scipy, str) and (use_scipy == 'root'):
        report = root(inner_step_func, x0=x_n)
    else:
        raise Exception('?')

    success = report.success
    x_n1 = report.x

    return t_n1, x_n1, success, inner_step_func
Example #2
0
nopts = sparse_newton_options()
nopts.max_times_J_n_constant = 10
nopts.max_it = 10000
nopts.no_convergence_is_Error = False
''' try out all possible callbacks available so far '''
callback_print = callback_print_progress()  # print out progress to console
callback_log = callback_print_progress(
    file_path_and_name='ex1__Newton_fullstep.log')  # print out progress to log
callback_iter = callback_store_all_iterates(
)  # not only get the root but any iterate before
callback_spy = callback_matrix_spy(
    stop_at_each_fig=False)  # get a plot of the matrix sprsity pattern
callback = callback_of_callbacks(callback_print, callback_log, callback_iter,
                                 callback_spy)  # call all other callbacks

report = sparse_nl_solve(fun=fun,
                         x0=x0,
                         jac=jac,
                         linsolver=spilu_simple_wrapper(),
                         callback=callback,
                         options=nopts)

print('')
print(report)
print('')

print(callback_iter.Xs)
callback_spy.conclude()

print('script finished!')
x0 = 5.0 * np.ones(shape=(2, ))

option = sparse_newton_options()
option.atol_dom = 1.0e-12
option.atol_range = None
option.rtol_dom = None
option.rtol_range = None
option.apply_row_scaling = False  # <- better turn this off! This changes the 'weights' of the row equations and therefore alters the position of the optimum
# option.max_times_J_n_constant = 0

cb2 = callback_store_all_iterates()
callback = callback_of_callbacks(callback_print_progress(),
                                 cb2)  #, callback_matrix_spy())

report = sparse_nl_solve(fun=fun,
                         x0=x0,
                         linsolver=lsqr_simple_wrapper(),
                         callback=callback,
                         options=option)

print(report)
print('')
print(cb2.Xs)

print('\n')

print(least_squares(fun, report.x))

print('script finished!')
from paso.systems_of_euqations.nlin.generic_nlin_nr1.generic_nlin_nr1 import f_and_jac_f, f_df as fun, x0

from paso.solvers.nlin.sparse_nl_solver.newton.core import sparse_nl_solve

report = sparse_nl_solve(fun=fun, x0=x0)

print(report)

print('script finished!')
def nlinsolver(*args, **kwargs): return sparse_nl_solve(*args, options = options, **kwargs)

''' will fail, due to no root! '''
 def nlinsolver_instance(*args, **kwargs):
     return sparse_nl_solve(*args, options=options_newton, **kwargs)