# [Trilinos-Users] Solving a trivial nonlinear system with pytrilinos nox with an exact Jacobian.

Bill Spotz wfspotz at sandia.gov
Mon Jan 25 17:28:31 MST 2010

```Scott,

So, this illustrates an improvement I need to make to the
PyTrilinos.NOX documentation.

NOX.defaultSolver() calls NOX.defaultGroup(), whose documentation says:

defaultGroup(nonlinearParameters, initGuess, reqInterface,
jacInterface=None,
jacobian=None, precInterface=None,
preconditioner=None) -> Group

Return a NOX.Epetra.Group based upon the given input arguments:

nonlinearParameters - a dict with nonlinear parameters.  Can be
obtained
from defaultNonlinearParameters()
initGuess           - an initial guess Epetra.Vector.
reqInterface        - an Interface.Required object
jacInterface        - an Interface.Jacobian object
jacobian            - if jacInterface is given, this should be the
Epetra.Operator that is the Jacobian matrix
precInterface       - an Interface.Preconditioner object
preconditioner      - if precInterface is given, this should be the
Epetra.Operator that is the Preconditioner

So when you provide jacInterface, you should also provide jacobian (I
will try to improve the facet of the interface).

I have now gone through this use case and I determined that I have not
implemented everything you need.  The NOX C++ code is going to call
back to your python computeJacobian() method, sending Jac as an
Epetra.Operator, downcast from whatever type you provided to
defaultSolver() in the "jacobian" argument.  In C++, you would upcast
back to an Epetra_RowMatrix or Epetra_CrsMatrix.  In python, there is
no way to do this.  This operator later gets passed to an AztecOO
solver, and it has to be an Epetra_RowMatrix or something derived from
it.

What I ned to do is change the conversion logic so that the "Jac"
argument in computeJacobian() is sent back to you as the same type you
provided in "jacobian" when you call defaultSolver().  Until then, you
can use finite difference coloring, as shown in exNOX_1Dfem.

On Jan 25, 2010, at 3:42 PM, Askey, Scott A Capt USAF AETC AFIT/ENY
wrote:

> How do I pass in the formula for the analytical jacobian to NOX?
> Following the template for the 1DFEM and the doc strings are a bit
> vague on this point.
>
>
> V/R
>
> Scott
>
>
> The problem is taken from didasko/examples/nox/ex1.cpp
> F=[x1**2+x2**2-1, x2-x1**2]...find the roots.
>
>
>
> The error is:
> ValueError: invalid null reference in method
> 'new_LinearSystemAztecOO', argument 5 of type 'Teuchos::RCP<
> Epetra_Operator > const &'
>
> My 30 line attemp follows:
>
> from PyTrilinos import Epetra, NOX
>
> class MyProblem(NOX.Epetra.Interface.Required):
> 	def __init__(self):
> 			NOX.Epetra.Interface.Required.__init__(self)
> 			self.__comm              = Epetra.SerialComm()
> 	def computeF(self,x,F,flag):
> 		try:
> 			F[:]=[x[0]*x[0]+x[1]*x[1]-1,x[1]-x[0]*x[0]]
> 		except Exception, e:
> 			print "Expeption in computeF method "
> 			print e
> 			return False
> 		return True
>
> class MyJacobian(NOX.Epetra.Interface.Jacobian):
> 	def __init__(self):
> 			NOX.Epetra.Interface.Jacobian.__init__(self)
> 			self.__comm              = Epetra.SerialComm()
> 	def computeJacobian(self,x,Jac,flag):
> 		try:
> 			Jac=[[2*x[0],2*x[1]],[-2*x[0],1]]
> 		except Exception, e:
> 			print "Expeption in computeJacobian"
> 			print e
> 			return False
> 		return True
>
> def main():
> 	soln =Epetra.Vector([.5,.5])
> 	initGuess=Epetra.Vector([.5,.5])
>
> 	problem=MyProblem()
> 	myJac=MyJacobian()
>
> 	NOX.defaultSolver(initGuess,problem,jacInterface=myJac,jacobian=None\
> 		, precInterface=None, preconditioner=None, nlParams=None)
> 	finalGroup = solver.getSolutionGroup()
> 	finalSoln  = finalGroup.getX()
>
>
>
>
>
>
>
> _______________________________________________
> Trilinos-Users mailing list
> Trilinos-Users at software.sandia.gov
> http://software.sandia.gov/mailman/listinfo/trilinos-users

** Bill Spotz                                              **
** Sandia National Laboratories  Voice: (505)845-0170      **
** P.O. Box 5800                 Fax:   (505)284-0154      **
** Albuquerque, NM 87185-0370    Email: wfspotz at sandia.gov **

```