10#include "Teko_mlutils.hpp"
16#include "Epetra_MpiComm.h"
18#include "Epetra_SerialComm.h"
20#include "Epetra_Vector.h"
22#include "ml_epetra_utils.h"
23#include "ml_op_utils.h"
25#include "Thyra_EpetraLinearOp.hpp"
27#include "EpetraExt_RowMatrixOut.h"
29#include "Teuchos_XMLParameterListHelpers.hpp"
31#include "Teko_InverseFactory.hpp"
32#include "Teko_InverseLibrary.hpp"
34#include "Teko_PreconditionerFactory.hpp"
35#include "Teko_EpetraOperatorWrapper.hpp"
36#include "Teko_SmootherPreconditionerFactory.hpp"
42Teuchos::RCP<Epetra_Map> buildRowMap(ML_Operator *mlOp) {
43 ML_Comm *comm = mlOp->comm;
46 mpi_comm = comm->USR_comm;
47 Epetra_MpiComm eComm(mpi_comm);
49 Epetra_SerialComm eComm;
53 int rowCnt = mlOp->outvec_leng;
54 return Teuchos::rcp(
new Epetra_Map(-1, rowCnt, 0, eComm));
60Teuchos::RCP<Epetra_CrsMatrix> convertToCrsMatrix(ML_Operator *mlOp,
61 const Teuchos::RCP<Epetra_Map> &rowMapArg) {
62 ML_Comm *comm = mlOp->comm;
65 mpi_comm = comm->USR_comm;
66 Epetra_MpiComm eComm(mpi_comm);
68 Epetra_SerialComm eComm;
72 Teuchos::RCP<Epetra_Map> rowMap = rowMapArg;
73 if (rowMapArg == Teuchos::null) rowMap = buildRowMap(mlOp);
76 Epetra_CrsMatrix *crsMatrixPtr = 0;
77 int maxNumNonzeros = 0;
80 ML_Operator2EpetraCrsMatrix(mlOp, crsMatrixPtr, maxNumNonzeros,
false, cpuTime, verbose);
82 return Teuchos::rcp(crsMatrixPtr);
85Teko::LinearOp buildTekoBlockOp(ML_Operator *mlOp,
int level) {
86 Teko_DEBUG_SCOPE(
"Teko::mlutils::buildTekoBlockOp", 0);
90 int numRows = ML_Operator_BlkMatNumBlockRows(mlOp);
91 int numCols = ML_Operator_BlkMatNumBlockCols(mlOp);
93 Teko::BlockedLinearOp tekoOp = Teko::createBlockedOp();
94 Teko::beginBlockFill(tekoOp, numRows, numCols);
95 for (
int i = 0; i < numRows; i++) {
96 for (
int j = 0; j < numCols; j++) {
97 ML_Operator *subBlock = ML_Operator_BlkMatExtract(mlOp, i, j);
102 RCP<const Epetra_CrsMatrix> eCrsMat = convertToCrsMatrix(subBlock);
105 std::stringstream ss;
106 ss <<
"A(" << level <<
")_" << i <<
"_" << j <<
".mm";
107 EpetraExt::RowMatrixToMatrixMarketFile(ss.str().c_str(),*eCrsMat);
111 Teko::LinearOp tekoSubBlock = Thyra::epetraLinearOp(eCrsMat);
112 Teko::setBlock(i, j, tekoOp, tekoSubBlock);
116 Teko::endBlockFill(tekoOp);
118 return tekoOp.getConst();
121int smoother(ML_Smoother *mydata,
int leng1,
double x[],
int leng2,
double rhs[]) {
122 Teko_DEBUG_SCOPE(
"Teko::mlutils::smoother", 10);
129 Epetra_Vector X(View, smootherData->Amat->OperatorDomainMap(), x);
130 Epetra_Vector Y(View, smootherData->Amat->OperatorRangeMap(), rhs);
132 smootherData->smootherOperator->Apply(Y, X);
137Teuchos::RCP<Teko::InverseFactory> ML_Gen_Init_Teko_Prec(
const std::string smoothName,
138 const Teko::InverseLibrary &invLib) {
139 Teko_DEBUG_SCOPE(
"Teko::mlutils::ML_Gen_Init_Teko_Prec", 10);
146 Teuchos::RCP<Teko::PreconditionerFactory> precFact;
147 Teuchos::RCP<Teko::InverseFactory> invFact = invLib.getInverseFactory(smoothName);
152extern "C" void ML_Destroy_Smoother_Teko(
void *data) {
153 Teko::mlutils::SmootherData *sData = (Teko::mlutils::SmootherData *)data;
157extern "C" int ML_Gen_Smoother_Teko(ML *ml,
int level,
int pre_or_post,
int ntimes,
158 const Teuchos::RCP<const Teuchos::ParameterList> &tekoPL,
159 const Teuchos::RCP<const Teko::InverseLibrary> &invLib_in,
160 const std::string &inverse,
bool isBlocked) {
161 Teko_DEBUG_SCOPE(
"Teko::mlutils::ML_Gen_Smoother_Teko", 10);
162 ML_Operator *BlkMat = &(ml->Amat[level]);
164 Teuchos::RCP<const Teko::InverseLibrary> invLib;
165 if (invLib_in == Teuchos::null) {
166 Teuchos::RCP<Teko::RequestHandler> rh = Teuchos::rcp(
new Teko::RequestHandler());
167 Teuchos::RCP<Teko::InverseLibrary> ncInvLib =
168 Teko::InverseLibrary::buildFromParameterList(*tekoPL);
169 ncInvLib->setRequestHandler(rh);
171 invLib = ncInvLib.getConst();
177 Teko::LinearOp tekoAmat;
180 tekoAmat = Teko::mlutils::buildTekoBlockOp(BlkMat, level);
183 Teuchos::RCP<const Epetra_CrsMatrix> eCrsMat = convertToCrsMatrix(BlkMat);
184 tekoAmat = Thyra::epetraLinearOp(eCrsMat);
188 Teko::mlutils::SmootherData *data =
new Teko::mlutils::SmootherData;
189 data->Amat = Teuchos::rcp(
new Teko::Epetra::EpetraOperatorWrapper(tekoAmat));
192 Teuchos::RCP<Teko::InverseFactory> invFact = ML_Gen_Init_Teko_Prec(inverse, *invLib);
193 Teuchos::RCP<Teko::RequestHandler> rh = invFact->getRequestHandler();
196 Teko::LinearOp precOp = Teko::buildInverse(*invFact, tekoAmat);
197 Teko::LinearOp smootherOp = Teko::buildSmootherLinearOp(tekoAmat, precOp, ntimes,
true);
200 data->smootherOperator = Teuchos::rcp(
new Teko::Epetra::EpetraOperatorWrapper(smootherOp));
203 ML_Set_Smoother(ml, level, pre_or_post, (
void *)data, Teko::mlutils::smoother, NULL);
204 ml->post_smoother[level].data_destroy = ML_Destroy_Smoother_Teko;