#include "Didasko_ConfigDefs.h"
#if defined(HAVE_DIDASKO_EPETRA) && defined(HAVE_DIDASKO_NOX) && defined(HAVE_NOX_EPETRA)
#include "Epetra_ConfigDefs.h"
#ifdef HAVE_MPI
#include "mpi.h"
#include "Epetra_MpiComm.h"
#else
#include "Epetra_SerialComm.h"
#endif
#include "Epetra_Map.h"
#include "Epetra_Vector.h"
#include "Epetra_RowMatrix.h"
#include "Epetra_CrsMatrix.h"
#include "NOX.H"
#include "NOX_Epetra_Interface_Required.H"
#include "NOX_Epetra_Interface_Jacobian.H"
#include "NOX_Epetra_LinearSystem_AztecOO.H"
#include "NOX_Epetra_Group.H"
static void get_neighbours( const int i, const int nx, const int ny,
int & left, int & right,
int & lower, int & upper)
{
int ix, iy;
ix = i%nx;
iy = (i - ix)/nx;
if( ix == 0 )
left = -1;
else
left = i-1;
if( ix == nx-1 )
right = -1;
else
right = i+1;
if( iy == 0 )
lower = -1;
else
lower = i-nx;
if( iy == ny-1 )
upper = -1;
else
upper = i+nx;
return;
}
Epetra_CrsMatrix * CreateLaplacian( const int nx, const int ny,
const Epetra_Comm * Comm)
{
int NumGlobalElements = nx * ny;
Epetra_Map * Map = new Epetra_Map(NumGlobalElements,0,*Comm);
int NumMyElements = Map->NumMyElements();
int * MyGlobalElements = Map->MyGlobalElements();
double hx = 1.0/(nx-1);
double hy = 1.0/(ny-1);
double off_left = -1.0/(hx*hx);
double off_right = -1.0/(hx*hx);
double off_lower = -1.0/(hy*hy);
double off_upper = -1.0/(hy*hy);
double diag = 2.0/(hx*hx) + 2.0/(hy*hy);
int left, right, lower, upper;
Epetra_CrsMatrix * A = new Epetra_CrsMatrix(Copy,*Map,5);
double *Values = new double[4];
int *Indices = new int[4];
for( int i=0 ; i<NumMyElements; ++i ) {
int NumEntries=0;
get_neighbours( MyGlobalElements[i], nx, ny,
left, right, lower, upper);
if( left != -1 ) {
Indices[NumEntries] = left;
Values[NumEntries] = off_left;
++NumEntries;
}
if( right != -1 ) {
Indices[NumEntries] = right;
Values[NumEntries] = off_right;
++NumEntries;
}
if( lower != -1 ) {
Indices[NumEntries] = lower;
Values[NumEntries] = off_lower;
++NumEntries;
}
if( upper != -1 ) {
Indices[NumEntries] = upper;
Values[NumEntries] = off_upper;
++NumEntries;
}
A->InsertGlobalValues(MyGlobalElements[i], NumEntries, Values, Indices);
A->InsertGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i);
}
A->FillComplete();
delete [] Indices;
delete [] Values;
delete Map;
return A;
}
class PDEProblem {
public:
PDEProblem(const int nx, const int ny, const double lambda,
const Epetra_Comm * Comm) :
nx_(nx), ny_(ny), lambda_(lambda)
{
hx_ = 1.0/(nx_-1);
hy_ = 1.0/(ny_-1);
Matrix_ = CreateLaplacian(nx_,ny_,Comm);
}
~PDEProblem()
{
delete Matrix_;
}
void ComputeF(const Epetra_Vector & x, Epetra_Vector & f)
{
double diag = 2.0/(hx_*hx_) + 2.0/(hy_*hy_);
int NumMyElements = Matrix_->Map().NumMyElements();
int * MyGlobalElements = Matrix_->Map().MyGlobalElements( );
for( int i=0 ; i<NumMyElements; ++i ) {
Matrix_->ReplaceGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i);
}
Matrix_->Multiply(false,x,f);
for( int i=0 ; i<NumMyElements; ++i ) {
f[i] += lambda_*exp(x[i]);
}
}
void UpdateJacobian(const Epetra_Vector & x)
{
double diag = 2.0/(hx_*hx_) + 2.0/(hy_*hy_);
int NumMyElements = Matrix_->Map().NumMyElements();
int * MyGlobalElements = Matrix_->Map().MyGlobalElements( );
for( int i=0 ; i<NumMyElements; ++i ) {
double newdiag = diag + lambda_*exp(x[i]);
Matrix_->ReplaceGlobalValues(MyGlobalElements[i], 1,
&newdiag, MyGlobalElements+i);
}
}
Epetra_CrsMatrix * GetMatrix()
{
return Matrix_;
}
private:
int nx_, ny_;
double hx_, hy_;
Epetra_CrsMatrix * Matrix_;
double lambda_;
};
class SimpleProblemInterface : public NOX::Epetra::Interface::Required,
public NOX::Epetra::Interface::Jacobian
{
public:
SimpleProblemInterface( PDEProblem * Problem ) :
Problem_(Problem) {};
~SimpleProblemInterface()
{
};
bool computeF(const Epetra_Vector & x, Epetra_Vector & f,
NOX::Epetra::Interface::Required::FillType F )
{
Problem_->ComputeF(x,f);
return true;
};
bool computeJacobian(const Epetra_Vector & x, Epetra_Operator & Jac)
{
Problem_->UpdateJacobian(x);
return true;
}
bool computePrecMatrix(const Epetra_Vector & x, Epetra_RowMatrix & M)
{
cout << "*ERR* SimpleProblem::preconditionVector()\n";
cout << "*ERR* don't use explicit preconditioning" << endl;
throw 1;
}
bool computePreconditioner(const Epetra_Vector & x, Epetra_Operator & O)
{
cout << "*ERR* SimpleProblem::preconditionVector()\n";
cout << "*ERR* don't use explicit preconditioning" << endl;
throw 1;
}
private:
PDEProblem * Problem_;
};
int main( int argc, char **argv )
{
#ifdef HAVE_MPI
MPI_Init(&argc, &argv);
Epetra_MpiComm Comm(MPI_COMM_WORLD);
#else
Epetra_SerialComm Comm;
#endif
int nx = 5;
int ny = 6;
double lambda = 1.0;
PDEProblem Problem(nx,ny,lambda,&Comm);
Epetra_Vector InitialGuess(Problem.GetMatrix()->Map());
InitialGuess.PutScalar(0.0);
Teuchos::RCP<SimpleProblemInterface> interface =
Teuchos::rcp(new SimpleProblemInterface(&Problem) );
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
Teuchos::rcp(new Teuchos::ParameterList);
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
nlParams.set("Nonlinear Solver", "Line Search Based");
Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
printParams.set("MyPID", Comm.MyPID());
printParams.set("Output Precision", 3);
printParams.set("Output Processor", 0);
printParams.set("Output Information",
NOX::Utils::OuterIteration +
NOX::Utils::OuterIterationStatusTest +
NOX::Utils::InnerIteration +
NOX::Utils::Parameters +
NOX::Utils::Details +
NOX::Utils::Warning);
Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
searchParams.set("Method","More'-Thuente");
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
dirParams.set("Method", "Newton");
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
newtonParams.set("Forcing Term Method", "Constant");
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
lsParams.set("Aztec Solver", "GMRES");
lsParams.set("Max Iterations", 800);
lsParams.set("Tolerance", 1e-4);
lsParams.set("Output Frequency", 50);
lsParams.set("Aztec Preconditioner", "ilu");
Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( Problem.GetMatrix(), false );
Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
iReq,
iJac, A,
InitialGuess));
NOX::Epetra::Vector noxInitGuess(InitialGuess, NOX::DeepCopy);
Teuchos::RCP<NOX::Epetra::Group> grpPtr =
Teuchos::rcp(new NOX::Epetra::Group(printParams,
iReq,
noxInitGuess,
linSys));
Teuchos::RCP<NOX::StatusTest::NormF> testNormF =
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-4));
Teuchos::RCP<NOX::StatusTest::MaxIters> testMaxIters =
Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
Teuchos::RCP<NOX::StatusTest::Combo> combo =
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR,
testNormF, testMaxIters));
Teuchos::RCP<NOX::Solver::Generic> solver =
NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
NOX::StatusTest::StatusType status = solver->solve();
if( NOX::StatusTest::Converged != status )
cout << "\n" << "-- NOX solver converged --" << "\n";
else
cout << "\n" << "-- NOX solver did not converge --" << "\n";
cout << "\n" << "-- Parameter List From Solver --" << "\n";
solver->getList().print(cout);
const NOX::Epetra::Group & finalGroup =
dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
const Epetra_Vector & finalSolution =
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
if( Comm.MyPID() == 0 ) cout << "Computed solution : " << endl;
cout << finalSolution;
#ifdef HAVE_MPI
MPI_Finalize();
#endif
return(EXIT_SUCCESS);
}
#else
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_MPI
#include "mpi.h"
#endif
int main(int argc, char *argv[])
{
#ifdef HAVE_MPI
MPI_Init(&argc,&argv);
#endif
puts("Please configure Didasko with:");
puts("--enable-epetra");
puts("--enable-ifpack");
puts("--enable-aztecoo");
puts("--enable-nox-epetra");
puts("--enable-nox\n");
#ifdef HAVE_MPI
MPI_Finalize();
#endif
return(EXIT_SUCCESS);
}
#endif