Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_MLPreconditionerFactory.cpp
1 #include "Teko_MLPreconditionerFactory.hpp"
2 
3 #include "Teko_MLLinearOp.hpp"
4 
5 #include "ml_include.h"
6 #include "ml_MultiLevelPreconditioner.h"
7 #include "ml_epetra_utils.h"
8 #include "ml_RowMatrix.h"
9 
10 #include "Thyra_get_Epetra_Operator.hpp"
11 
12 namespace Teko {
13 
15 // MLPreconditionerState
17 
18 MLPreconditionerState::MLPreconditionerState() : isFilled_(false) {}
19 
20 void MLPreconditionerState::setMLComm(ML_Comm *comm) {
21  mlComm_ = Teuchos::rcpWithDealloc(comm, Teuchos::deallocFunctorDelete<ML_Comm>(cleanup_ML_Comm));
22 }
23 
24 void MLPreconditionerState::setMLOperator(ML_Operator *op) {
25  mlOp_ =
26  Teuchos::rcpWithDealloc(op, Teuchos::deallocFunctorDelete<ML_Operator>(cleanup_ML_Operator));
27 }
28 
29 void MLPreconditionerState::setIsFilled(bool value) { isFilled_ = value; }
30 
31 bool MLPreconditionerState::isFilled() const { return isFilled_; }
32 
33 void MLPreconditionerState::cleanup_ML_Comm(ML_Comm *mlComm) { ML_Comm_Destroy(&mlComm); }
34 
35 void MLPreconditionerState::cleanup_ML_Operator(ML_Operator *mlOp) { ML_Operator_Destroy(&mlOp); }
36 
37 void MLPreconditionerState::setAggregationMatrices(const std::vector<Epetra_RowMatrix *> &diags) {
38  diagonalOps_ = diags;
39 }
40 
41 Teuchos::RCP<ML_Epetra::MultiLevelPreconditioner> MLPreconditionerState::constructMLPreconditioner(
42  const Teuchos::ParameterList &mainList,
43  const std::vector<Teuchos::RCP<const Teuchos::ParameterList> > &coarseningParams) {
44  TEUCHOS_ASSERT(isFilled());
45  std::vector<Teuchos::ParameterList> cpls(coarseningParams.size());
46  for (std::size_t i = 0; i < coarseningParams.size(); i++) cpls[i] = *coarseningParams[i];
47 
48  mlPreconditioner_ = rcp(new ML_Epetra::MultiLevelPreconditioner(
49  &*mlOp_, mainList, &diagonalOps_[0], &cpls[0], diagonalOps_.size()));
50 
51  return mlPreconditioner_;
52 }
53 
55 // MLPreconditionerFactory
57 
58 MLPreconditionerFactory::MLPreconditionerFactory() {}
59 
61  BlockedLinearOp &blo, BlockPreconditionerState &state) const {
62  MLPreconditionerState &mlState = Teuchos::dyn_cast<MLPreconditionerState>(state);
63 
64  if (not mlState.isFilled()) fillMLPreconditionerState(blo, mlState);
65  TEUCHOS_ASSERT(mlState.isFilled());
66 
67  Teuchos::RCP<ML_Epetra::MultiLevelPreconditioner> mlPrecOp =
68  mlState.constructMLPreconditioner(mainParams_, coarseningParams_);
69 
70  // return Thyra::epetraLinearOp(mlPrecOp);
71  return Teuchos::rcp(new MLLinearOp(mlPrecOp));
72 }
73 
74 Teuchos::RCP<PreconditionerState> MLPreconditionerFactory::buildPreconditionerState() const {
75  return Teuchos::rcp(new MLPreconditionerState());
76 }
77 
79  MLPreconditionerState &mlState) const {
80  TEUCHOS_ASSERT(not mlState.isFilled());
81 
82  EpetraExt::CrsMatrix_SolverMap RowMatrixColMapTrans;
83 
84  int rowCnt = blockRowCount(blo);
85  int colCnt = blockRowCount(blo);
86 
87  // construct comm object...add to state
88  ML_Comm *mlComm;
89  ML_Comm_Create(&mlComm);
90  mlState.setMLComm(mlComm);
91 
92  ML_Operator *mlBlkMat = ML_Operator_Create(mlComm);
93  ML_Operator_BlkMatInit(mlBlkMat, mlComm, rowCnt, colCnt, ML_DESTROY_EVERYTHING);
94 
95  std::vector<Epetra_RowMatrix *> aggMats;
96  for (int r = 0; r < rowCnt; r++) {
97  for (int c = 0; c < colCnt; c++) {
98  ML_Operator *tmp = 0;
99  Epetra_RowMatrix *EMat = 0;
100  std::stringstream ss;
101 
102  ss << "fine_" << r << "_" << c;
103 
104  // extract crs matrix
105  Teuchos::RCP<Epetra_CrsMatrix> crsMat = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(
106  Thyra::get_Epetra_Operator(*blo->getNonconstBlock(r, c)));
107  EMat = ML_Epetra::ModifyEpetraMatrixColMap(*crsMat, RowMatrixColMapTrans, ss.str().c_str());
108  if (r == c) // setup diagonal scaling matrices
109  aggMats.push_back(EMat);
110 
111  // extract ml sub operator
112  tmp = ML_Operator_Create(mlComm);
113  ML_Operator_WrapEpetraMatrix(EMat, tmp);
114 
115  // add it to the block
116  ML_Operator_BlkMatInsert(mlBlkMat, tmp, r, c);
117  }
118  }
119  ML_Operator_BlkMatFinalize(mlBlkMat);
120 
121  // finish setting up state object
122  mlState.setMLOperator(mlBlkMat);
123 
124  // now set aggregation matrices
125  mlState.setAggregationMatrices(aggMats);
126 
127  mlState.setIsFilled(true); // register state object as filled
128 }
129 
133 void MLPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList &settings) {
134  using Teuchos::RCP;
135  using Teuchos::rcp;
136 
137  Teko_DEBUG_SCOPE("MLPreconditionerFactory::initializeFromParameterList", 10);
138 
139  // clear initial state
140  coarseningParams_.clear();
141  blockRowCount_ = 0;
142 
143  blockRowCount_ = settings.get<int>("Block Row Count");
144 
145  // read in main parameter list: with smoothing information
147  mainParams_ = settings.sublist("Smoothing Parameters");
148  mainParams_.set<Teuchos::RCP<const Teko::InverseLibrary> >("smoother: teko inverse library",
150 
151  // read in aggregation sub lists
153  const Teuchos::ParameterList &aggSublist = settings.sublist("Block Aggregation");
154 
155  for (int block = 0; block < blockRowCount_; block++) {
156  // write out sub list name: "Block #"
157  std::stringstream ss;
158  ss << "Block " << block;
159  std::string sublistName = ss.str();
160 
161  // grab sublist
162  RCP<Teuchos::ParameterList> userSpec =
163  rcp(new Teuchos::ParameterList(aggSublist.sublist(sublistName)));
164  coarseningParams_.push_back(userSpec);
165  }
166 }
167 
168 } // end namespace Teko
void setIsFilled(bool value)
Set if ML internals been constructed yet.
void fillMLPreconditionerState(const BlockedLinearOp &blo, MLPreconditionerState &mlState) const
Fills an ML preconditioner state object.
void initializeFromParameterList(const Teuchos::ParameterList &settings)
This function builds the internals of the preconditioner factory from a parameter list...
void setAggregationMatrices(const std::vector< Epetra_RowMatrix * > &diags)
Set matrices to build multigrid hierarcy from.
Contains operator internals need for ML.
An implementation of a state object for block preconditioners.
Teuchos::RCP< ML_Epetra::MultiLevelPreconditioner > constructMLPreconditioner(const Teuchos::ParameterList &mainList, const std::vector< Teuchos::RCP< const Teuchos::ParameterList > > &coarseningParams)
Build an ML preconditioner object using the set of coarsening parameters.
virtual LinearOp buildPreconditionerOperator(BlockedLinearOp &blo, BlockPreconditionerState &state) const
Function that is called to build the preconditioner for the linear operator that is passed in...
bool isFilled() const
Has this object been filled yet.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
void setMLComm(ML_Comm *comm)
set ML Comm pointer...it will be automatically cleaned up
void setMLOperator(ML_Operator *op)
set ML Operator pointer...it will be automatically cleaned up
virtual Teuchos::RCP< PreconditionerState > buildPreconditionerState() const
Function that permits the construction of an arbitrary PreconditionerState object.