# Using DiffEqBase.NLSolveTerminationCondition in NonlinearSolve.jl

I am trying to solve a system of three nonlinear equations and would like to use the termination conditions provided by DiffEqBase.NLSolveTerminationCondition (see the documentation of NonlinearSolve.jl.

The function definitions in my problem are of the form

``````f!(r,param,a) = eq_bin!(r, param, kappa, gamma, a; verbose = verbose::Bool)

J!(J,param,a) = eq_bin_jac!(J, param, kappa, gamma, a, v)

function Jv!(Jv,vec,param,a)
eq_bin_jac!(J, param, kappa, gamma, a, v)
mul!(Jv,J,vec)
return nothing
end

function Jvt!(Jv,vec,param,a)
eq_bin_jac!(J, param, kappa, gamma, a, v)
mul!(Jv,J',vec)
return nothing
end

NF = NonlinearFunction{true, specialize}(f!;
jac = J!,
jvp = Jv!,
vjp = Jvt!,
jac_prototype = jp
)
``````

which I want to solve using

``````probN = NonlinearProblem(NF, x_init, a)
sol = solve(probN, method, reltol = reltol, abstol = abstol, maxiters = maxiters)
``````

Now it sometimes happens with unfortunate starting values, that my system blows up, or that I do not get an improvement over a series of iterations of my system. Hence, I would like to use
`NLSolveTerminationCondition(NLSolveTerminationMode.AbsSafeBest)`.

However, I do not understand where I should put this termination condtion in my solver setup. solve does not have an obvious suitable kwarg and nor does NonlinearProblem from what I saw.

Any help as to how to use the termination condition funcitonality is greatly appreciated.

What method are you using?

`TrustRegion()` and `NewtonRaphson()` from `NonlinearSolve`. The fromer has a tendency to get stuck, while the latterr often blows up.

They aren’t using the new termination conditions quite yet. @Oscar_Smith probably a good time to finish add TerminationCondition support to NewtonRaphson by oscardssmith · Pull Request #162 · SciML/NonlinearSolve.jl · GitHub ?

That is unfortunate, so my best shot is to mimick the behaviour using callbacks I suppose?

I was just trying to use `isoutofdomain` to reject steps wehre the solution blows up or is negative using
`isoutofdomain = (u,p,t)->any(x -> (x < 0 || abs(x) > 1e4), u)` but I still get the same negative values and solutions that blow up as if I was using no `isoutofdomain`. Also tried the ` callback = PositiveDomain()` which did not do anything for me either. Solver was again `NewtonRaphson()`. Any ideas on how to make this work?