1 #include "Teko_mlutils.hpp"
7 #include "Epetra_MpiComm.h"
9 #include "Epetra_SerialComm.h"
11 #include "Epetra_Vector.h"
13 #include "ml_epetra_utils.h"
14 #include "ml_op_utils.h"
16 #include "Thyra_EpetraLinearOp.hpp"
18 #include "EpetraExt_RowMatrixOut.h"
20 #include "Teuchos_XMLParameterListHelpers.hpp"
22 #include "Teko_InverseFactory.hpp"
23 #include "Teko_InverseLibrary.hpp"
25 #include "Teko_PreconditionerFactory.hpp"
26 #include "Teko_EpetraOperatorWrapper.hpp"
27 #include "Teko_SmootherPreconditionerFactory.hpp"
33 Teuchos::RCP<Epetra_Map> buildRowMap(ML_Operator *mlOp) {
34 ML_Comm *comm = mlOp->comm;
37 mpi_comm = comm->USR_comm;
38 Epetra_MpiComm eComm(mpi_comm);
40 Epetra_SerialComm eComm;
44 int rowCnt = mlOp->outvec_leng;
45 return Teuchos::rcp(
new Epetra_Map(-1, rowCnt, 0, eComm));
51 Teuchos::RCP<Epetra_CrsMatrix> convertToCrsMatrix(ML_Operator *mlOp,
52 const Teuchos::RCP<Epetra_Map> &rowMapArg) {
53 ML_Comm *comm = mlOp->comm;
56 mpi_comm = comm->USR_comm;
57 Epetra_MpiComm eComm(mpi_comm);
59 Epetra_SerialComm eComm;
63 Teuchos::RCP<Epetra_Map> rowMap = rowMapArg;
64 if (rowMapArg == Teuchos::null) rowMap = buildRowMap(mlOp);
67 Epetra_CrsMatrix *crsMatrixPtr = 0;
68 int maxNumNonzeros = 0;
71 ML_Operator2EpetraCrsMatrix(mlOp, crsMatrixPtr, maxNumNonzeros,
false, cpuTime, verbose);
73 return Teuchos::rcp(crsMatrixPtr);
76 Teko::LinearOp buildTekoBlockOp(ML_Operator *mlOp,
int level) {
77 Teko_DEBUG_SCOPE(
"Teko::mlutils::buildTekoBlockOp", 0);
81 int numRows = ML_Operator_BlkMatNumBlockRows(mlOp);
82 int numCols = ML_Operator_BlkMatNumBlockCols(mlOp);
84 Teko::BlockedLinearOp tekoOp = Teko::createBlockedOp();
85 Teko::beginBlockFill(tekoOp, numRows, numCols);
86 for (
int i = 0; i < numRows; i++) {
87 for (
int j = 0; j < numCols; j++) {
88 ML_Operator *subBlock = ML_Operator_BlkMatExtract(mlOp, i, j);
93 RCP<const Epetra_CrsMatrix> eCrsMat = convertToCrsMatrix(subBlock);
97 ss <<
"A(" << level <<
")_" << i <<
"_" << j <<
".mm";
98 EpetraExt::RowMatrixToMatrixMarketFile(ss.str().c_str(),*eCrsMat);
102 Teko::LinearOp tekoSubBlock = Thyra::epetraLinearOp(eCrsMat);
103 Teko::setBlock(i, j, tekoOp, tekoSubBlock);
107 Teko::endBlockFill(tekoOp);
109 return tekoOp.getConst();
112 int smoother(ML_Smoother *mydata,
int leng1,
double x[],
int leng2,
double rhs[]) {
113 Teko_DEBUG_SCOPE(
"Teko::mlutils::smoother", 10);
118 SmootherData *smootherData = (
struct SmootherData *)ML_Get_MySmootherData(mydata);
120 Epetra_Vector X(View, smootherData->Amat->OperatorDomainMap(), x);
121 Epetra_Vector Y(View, smootherData->Amat->OperatorRangeMap(), rhs);
123 smootherData->smootherOperator->Apply(Y, X);
128 Teuchos::RCP<Teko::InverseFactory> ML_Gen_Init_Teko_Prec(
const std::string smoothName,
129 const Teko::InverseLibrary &invLib) {
130 Teko_DEBUG_SCOPE(
"Teko::mlutils::ML_Gen_Init_Teko_Prec", 10);
137 Teuchos::RCP<Teko::PreconditionerFactory> precFact;
138 Teuchos::RCP<Teko::InverseFactory> invFact = invLib.getInverseFactory(smoothName);
143 extern "C" void ML_Destroy_Smoother_Teko(
void *data) {
148 extern "C" int ML_Gen_Smoother_Teko(ML *ml,
int level,
int pre_or_post,
int ntimes,
149 const Teuchos::RCP<const Teuchos::ParameterList> &tekoPL,
150 const Teuchos::RCP<const Teko::InverseLibrary> &invLib_in,
151 const std::string &inverse,
bool isBlocked) {
152 Teko_DEBUG_SCOPE(
"Teko::mlutils::ML_Gen_Smoother_Teko", 10);
153 ML_Operator *BlkMat = &(ml->Amat[level]);
155 Teuchos::RCP<const Teko::InverseLibrary> invLib;
156 if (invLib_in == Teuchos::null) {
158 Teuchos::RCP<Teko::InverseLibrary> ncInvLib =
159 Teko::InverseLibrary::buildFromParameterList(*tekoPL);
160 ncInvLib->setRequestHandler(rh);
162 invLib = ncInvLib.getConst();
168 Teko::LinearOp tekoAmat;
171 tekoAmat = Teko::mlutils::buildTekoBlockOp(BlkMat, level);
174 Teuchos::RCP<const Epetra_CrsMatrix> eCrsMat = convertToCrsMatrix(BlkMat);
175 tekoAmat = Thyra::epetraLinearOp(eCrsMat);
183 Teuchos::RCP<Teko::InverseFactory> invFact = ML_Gen_Init_Teko_Prec(inverse, *invLib);
184 Teuchos::RCP<Teko::RequestHandler> rh = invFact->getRequestHandler();
188 Teko::LinearOp smootherOp = Teko::buildSmootherLinearOp(tekoAmat, precOp, ntimes,
true);
194 ML_Set_Smoother(ml, level, pre_or_post, (
void *)data, Teko::mlutils::smoother, NULL);
195 ml->post_smoother[level].data_destroy = ML_Destroy_Smoother_Teko;
Implements the Epetra_Operator interface with a Thyra LinearOperator. This enables the use of absrtac...
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.