Debian Codegraph @

packages/muelu/research/max/AdditiveMG/Smooth_Prolongation.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "Smooth_Prolongation.hpp"
11
12 namespace MueLu {
13
14 AdditiveVariant::AdditiveVariant(RCP<crs_matrix_type> A, RCP<multivector_type> coords, DomainPartitioning domain) {
15 domain_ = domain;
16
17 TEUCHOS_TEST_FOR_EXCEPT(!(GlobalComm_.is_null()));
18 GlobalComm_ = A->getComm();
19 TEUCHOS_TEST_FOR_EXCEPT(GlobalComm_.is_null());
20
21 TEUCHOS_TEST_FOR_EXCEPT(!(coords_.is_null()));
22 coords_ = coords;
23 TEUCHOS_TEST_FOR_EXCEPT((coords_.is_null()));
24
25 TEUCHOS_TEST_FOR_EXCEPT(!(DomainMap_.is_null()));
26 DomainMap_ = A->getDomainMap();
27 TEUCHOS_TEST_FOR_EXCEPT(DomainMap_.is_null());
28
29 TEUCHOS_TEST_FOR_EXCEPT(!(RangeMap_.is_null()));
30 RangeMap_ = A->getRangeMap();
31 TEUCHOS_TEST_FOR_EXCEPT(RangeMap_.is_null());
32
33 TEUCHOS_TEST_FOR_EXCEPT(!(B_fine_.is_null()) || !(B_coarse_.is_null()));
34 AdditiveFineSmoother(A);
35 AdditiveCoarseSolver(A);
36 TEUCHOS_TEST_FOR_EXCEPT(B_fine_.is_null() || B_coarse_.is_null());
37
38 /*TEUCHOS_TEST_FOR_EXCEPT( !( B_DD_.is_null() ) || !( B_coarse_.is_null() ) );
39 AdditiveFineSmoother( A );
40 AdditiveCoarseSolver( A );
41 TEUCHOS_TEST_FOR_EXCEPT( B_DD_.is_null() || B_coarse_.is_null() );
42 TEUCHOS_TEST_FOR_EXCEPT( !B_DD_->isInitialized() || !B_DD_->isComputed() );*/
43 }
44
45 void AdditiveVariant::AdditiveFineSmoother(RCP<crs_matrix_type> A) {
46 // Creation of the MueLu list for the DD preconditioner
47 RCP<ParameterList> dd_list = rcp(new Teuchos::ParameterList());
48 dd_list->setName("MueLu");
49 dd_list->set("verbosity", "low");
50 dd_list->set("number of equations", 1);
51 dd_list->set("max levels", 1);
52 dd_list->set("coarse: type", "SCHWARZ"); // FOR A ONE LEVEL PRECONDITIONER THE COARSE LEVEL IS INTERPRETED AS SMOOTHING LEVEL
53
54 ParameterList& dd_smooth_sublist = dd_list->sublist("coarse: params");
55 dd_smooth_sublist.set("schwarz: overlap level", 0);
56 dd_smooth_sublist.set("schwarz: combine mode", "Zero");
57 dd_smooth_sublist.set("subdomain solver name", "RILUK");
58
59 ParameterList& coarse_subdomain_solver = dd_smooth_sublist.sublist("subdomain solver parameters");
60 coarse_subdomain_solver.set("fact: iluk level-of-fill", 3);
61 coarse_subdomain_solver.set("fact: absolute threshold", 0.);
62 coarse_subdomain_solver.set("fact: relative threshold", 1.);
63 coarse_subdomain_solver.set("fact: relax value", 0.);
64
65 B_fine_ = CreateTpetraPreconditioner((RCP<operator_type>)A, *dd_list);
66
67 /*B_DD_ = rcp( new Ifpack2::AdditiveSchwarz< row_matrix_type, precond_type >(A, 0) );
68 B_DD_->initialize();
69 B_DD_->compute();*/
70 }
71
72 void AdditiveVariant::AdditiveCoarseSolver(RCP<crs_matrix_type> A) {
73 // Creation of the MueLu list for the DD preconditioner
74 RCP<ParameterList> coarse_list = rcp(new Teuchos::ParameterList());
75 coarse_list->setName("MueLu");
76 coarse_list->set("verbosity", "low");
77 coarse_list->set("number of equations", 1);
78 coarse_list->set("max levels", 2);
79 coarse_list->set("multigrid algorithm", "unsmoothed");
80 coarse_list->set("aggregation: type", "brick");
81 coarse_list->set("aggregation: brick x size", domain_.bricksize_x);
82 coarse_list->set("aggregation: brick y size", domain_.bricksize_y);
83 coarse_list->set("aggregation: brick z size", domain_.bricksize_z);
84 coarse_list->set("aggregation: drop scheme", "classical");
85 coarse_list->set("smoother: pre or post", "none");
86 coarse_list->set("repartition: enable", true);
87 coarse_list->set("repartition: partitioner", "zoltan");
88 coarse_list->set("repartition: start level", 1);
89 coarse_list->set("repartition: min rows per proc", static_cast<int>(A->getGlobalNumRows()));
90 coarse_list->set("repartition: max imbalance", 1.2);
91 coarse_list->set("repartition: remap parts", false);
92 coarse_list->set("coarse: type", "SCHWARZ");
93
94 // Creation of Sublist for smoother
95 ParameterList& coarse_smooth_sublist = coarse_list->sublist("coarse: params");
96 coarse_smooth_sublist.set("schwarz: overlap level", 0);
97 coarse_smooth_sublist.set("schwarz: combine mode", "Zero");
98 coarse_smooth_sublist.set("subdomain solver name", "RILUK");
99
100 // Creation of the sublist for the subdomain solver
101 ParameterList& coarse_subdomain_solver = coarse_smooth_sublist.sublist("subdomain solver parameters");
102 coarse_subdomain_solver.set("fact: iluk level-of-fill", 3);
103 coarse_subdomain_solver.set("fact: absolute threshold", 0.);
104 coarse_subdomain_solver.set("fact: relative threshold", 1.);
105 coarse_subdomain_solver.set("fact: relax value", 0.);
106
107 /*ParameterList& coarse_file_sublist = coarse_list->sublist("export data");
108 coarse_file_sublist.set("A", "{0}");
109 coarse_file_sublist.set("P", "{0,1}");
110 coarse_file_sublist.set("R", "{1}");*/
111
112 // Manual set up of the prolongation and restriction
113 MueLu::ParameterListInterpreter<scalar_type> mueLuFactory(*coarse_list);
114 RCP<MueLu::Hierarchy<scalar_type>> H = mueLuFactory.CreateHierarchy();
115 H->setVerbLevel(Teuchos::VERB_HIGH);
116 RCP<xpetra_matrix> mueluA = MueLu::TpetraCrs_To_XpetraMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>(A);
117
118 RCP<Xpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>> coords = Xpetra::toXpetra(coords_);
119
120 H->GetLevel(0)->Set("A", mueluA);
121 H->GetLevel(0)->Set("Coordinates", coords);
122
123 // Multigrid setup phase
124 mueLuFactory.SetupHierarchy(*H);
125
126 RCP<Level> L = H->GetLevel(1);
127
128 RCP<Xpetra::Matrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>> prolong, restr;
129
130 if (L->IsAvailable("P"))
131 prolong = L->template Get<RCP<Xpetra::Matrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>>>("P");
132
133 if (L->IsAvailable("R"))
134 restr = L->template Get<RCP<Xpetra::Matrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>>>("R");
135
136 RCP<crs_matrix_type> tpetra_prolong = toTpetra(prolong);
137 RCP<crs_matrix_type> tpetra_restr = toTpetra(restr);
138
139 int mypid = GlobalComm_->getRank();
140 GlobalComm_->barrier();
141
142 // We have to transform P into a condensed multivector
143 RCP<multivector_type> identity_shrunk = rcp(new multivector_type(tpetra_prolong->getDomainMap(), 3));
144 Teuchos::ArrayView<const global_ordinal_type> myIdentityGlobalElements = tpetra_prolong->getDomainMap()->getLocalElementList();
145 typedef typename Teuchos::ArrayView<const global_ordinal_type>::const_iterator iter_type;
146
147 int my_color = (mypid - 1) % 3;
148 Teuchos::ArrayRCP<scalar_type> localMV = identity_shrunk->getDataNonConst(my_color);
149
150 for (iter_type it = myIdentityGlobalElements.begin(); it != myIdentityGlobalElements.end(); ++it) {
151 const local_ordinal_type i_local = *it;
152 const local_ordinal_type aux = identity_shrunk->getMap()->getLocalElement(i_local);
153 localMV[aux] = 1.0;
154 }
155
156 RCP<multivector_type> P_shrunk = rcp(new multivector_type(tpetra_prolong->getRangeMap(), 3));
157 tpetra_prolong->apply(*identity_shrunk, *P_shrunk);
158 RCP<multivector_type> AP_shrunk = rcp(new multivector_type(A->getRangeMap(), 3));
159 A->apply(*P_shrunk, *AP_shrunk);
160
161 TEUCHOS_TEST_FOR_EXCEPT(B_fine_.is_null());
162
163 //========================================================================================================
164
165 // CREATION OF BAP
166
167 RCP<multivector_type> BAP_multivector = rcp(new multivector_type(B_fine_->getRangeMap(), AP_shrunk->getNumVectors()));
168 RCP<multivector_type> BAP_shrunk = rcp(new multivector_type(B_fine_->getRangeMap(), AP_shrunk->getNumVectors()));
169 B_fine_->apply(*AP_shrunk, *BAP_shrunk, Teuchos::NO_TRANS, Teuchos::ScalarTraits<scalar_type>::one(), Teuchos::ScalarTraits<scalar_type>::zero());
170
171 // I just need this to generate the right colMap to populate BAP
172 RCP<crs_matrix_type> AP = rcp(new crs_matrix_type(tpetra_prolong->getRowMap(), tpetra_prolong->getColMap(), tpetra_prolong->getGlobalNumRows()));
173 Tpetra::MatrixMatrix::Multiply(*A, false, *tpetra_prolong, false, *AP, true);
174
175 GlobalComm_->barrier();
176
177 RCP<crs_matrix_type> BAP = rcp(new crs_matrix_type(tpetra_prolong->getRowMap(), AP->getColMap(), tpetra_prolong->getGlobalNumCols()));
178 Teuchos::ArrayView<const global_ordinal_type> myLocalElements = BAP->getRowMap()->getLocalElementList();
179
180 for (int color = 0; color < 3; ++color) {
181 Teuchos::ArrayRCP<const scalar_type> localBAP = BAP_shrunk->getData(color);
182
183 for (iter_type it = myLocalElements.begin(); it != myLocalElements.end(); ++it) {
184 const local_ordinal_type i_local = *it;
185 const local_ordinal_type aux = BAP->getRowMap()->getLocalElement(i_local);
186
187 std::vector<global_ordinal_type> BAP_inds;
188 std::vector<scalar_type> BAP_vals;
189
190 local_ordinal_type aux2;
191
192 if ((mypid - 1) % 3 == color && (mypid - 1) >= 0 && (mypid - 1) < tpetra_prolong->getGlobalNumCols())
193 aux2 = BAP->getColMap()->getLocalElement(mypid - 1);
194 else if ((mypid - 2) % 3 == color && (mypid - 2) >= 0 && (mypid - 2) < tpetra_prolong->getGlobalNumCols())
195 aux2 = BAP->getColMap()->getLocalElement(mypid - 2);
196 else if ((mypid) % 3 == color && (mypid) >= 0 && (mypid) < tpetra_prolong->getGlobalNumCols())
197 aux2 = BAP->getColMap()->getLocalElement(mypid);
198
199 if (aux2 >= 0) {
200 BAP_inds.emplace_back(aux2);
201 BAP_vals.emplace_back(localBAP[aux]);
202 BAP->insertLocalValues(aux, BAP_inds, BAP_vals);
203 }
204 }
205 }
206 BAP->fillComplete(tpetra_prolong->getDomainMap(), tpetra_prolong->getRangeMap());
207
208 //=============================================================================================================
209
210 RCP<crs_matrix_type> Pbar = Tpetra::MatrixMatrix::add(1.0, false, *tpetra_prolong, -1.0, false, *BAP);
211 RCP<xpetra_matrix> mueluPbar = MueLu::TpetraCrs_To_XpetraMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>(Pbar);
212
213 H->GetLevel(1)->Set("Pbar", mueluPbar);
214
215 H->IsPreconditioner(true);
216 B_coarse_ = rcp(new muelu_tpetra_operator_type(H));
217 }
218
219 void AdditiveVariant::apply(const multivector_type& r, multivector_type& Pr, Teuchos::ETransp mode = Teuchos::NO_TRANS, scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(), scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const {
220 Tpetra::Export<local_ordinal_type, global_ordinal_type, node_type> Export_fine1(r.getMap(), DomainMap_);
221 Tpetra::Export<local_ordinal_type, global_ordinal_type, node_type> Export_fine2(RangeMap_, r.getMap());
222
223 multivector_type r_fine(DomainMap_, 1);
224 r_fine.doImport(r, Export_fine1, Tpetra::INSERT);
225 multivector_type B_fine_Pr(RangeMap_, 1);
226 B_fine_->apply(r_fine, B_fine_Pr, mode, alpha, beta);
227 multivector_type B_Pr1(r.getMap(), 1);
228 B_Pr1.doImport(B_fine_Pr, Export_fine2, Tpetra::INSERT);
229
230 multivector_type r_coarse(DomainMap_, 1);
231 r_coarse.doImport(r, Export_fine1, Tpetra::INSERT);
232 multivector_type B_coarse_Pr(RangeMap_, 1);
233 B_coarse_->apply(r_coarse, B_coarse_Pr, mode, alpha, beta);
234 multivector_type B_Pr2(r.getMap(), 1);
235 B_Pr2.doImport(B_coarse_Pr, Export_fine2, Tpetra::INSERT);
236
237 multivector_type B_Pr_sum(r.getMap(), 1);
238 B_Pr_sum.update(1.0, B_Pr1, 1.0, B_Pr2, 0.0); // Careful to set the correct coefficients!!!
239
240 Pr = B_Pr_sum;
241 }
242
243 } // namespace MueLu