Debian Codegraph @

packages/muelu/research/q2q1/MueLu_Q2Q1uPFactory.hpp

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 #ifndef MUELU_Q2Q1UPFACTORY_DECL_HPP
11 #define MUELU_Q2Q1UPFACTORY_DECL_HPP
12
13 #include <Teuchos_RCP.hpp>
14
15 #include <Xpetra_MapFactory.hpp>
16 #include <Xpetra_MatrixFactory.hpp>
17 #include <Xpetra_MultiVectorFactory.hpp>
18 #include <Xpetra_IO.hpp>
19
20 #include "MueLu_ConfigDefs.hpp"
21
22 #include "MueLu_Level.hpp"
23 #include "MueLu_Monitor.hpp"
24 #include "MueLu_PerfUtils.hpp"
25 #include "MueLu_PFactory.hpp"
26 #include "MueLu_Utilities.hpp"
27
28 #include <algorithm>
29 #include <vector>
30 #include <sys/stat.h>
31
32 namespace MueLu {
33
34 template <class LocalOrdinal = int>
35 class MyCptList_ {
36 typedef LocalOrdinal LO;
37
38 public:
39 MyCptList_(int n, int nnzPerRow = 100) {
40 TEUCHOS_TEST_FOR_EXCEPTION(nnzPerRow <= 0, Exceptions::RuntimeError, "Why is nnzPerRow " << nnzPerRow << "?");
41
42 nnzPerRow_ = nnzPerRow;
43 storage_.resize(n * nnzPerRow);
44 numCpts_.resize(n, 0);
45
46 list_.resize(n, NULL);
47 for (int i = 0; i < n; i++)
48 list_[i] = &storage_[i * nnzPerRow];
49 }
50
51 size_t getLocalNumRows() const { return list_.size(); }
52 int getNnzPerRow() const { return nnzPerRow_; }
53 std::vector<short>& getNumCpts() { return numCpts_; }
54 Teuchos::Array<LO>& getCList() { return cptlist_; }
55 const Teuchos::Array<LO>& getCList() const { return cptlist_; }
56 const std::vector<short>& getNumCpts() const { return numCpts_; }
57 LO* operator()(int i) { return list_[i]; }
58 const LO* operator()(int i) const { return list_[i]; }
59
60 private:
61 std::vector<LO*> list_; // list[k] gives the CPOINTs that interpolate to the k-th fine point
62 // These CPOINTs are given as fine grid *local* indices
63 std::vector<short> numCpts_; // Number of CPOINTs for each point
64 Teuchos::Array<LO> cptlist_; // List of CPOINTs for each point
65 int nnzPerRow_; // Max number of CPOINTs per row in order for a row to use storage_
66 std::vector<LO> storage_; // Large data array used to store most CPOINT information
67 };
68
69 template <class Scalar = MueLu::DefaultScalar,
70 class LocalOrdinal = MueLu::DefaultLocalOrdinal,
71 class GlobalOrdinal = MueLu::DefaultGlobalOrdinal,
72 class Node = MueLu::DefaultNode>
73 class Q2Q1uPFactory : public PFactory {
74 #include "MueLu_UseShortNames.hpp"
75 typedef MyCptList_<LocalOrdinal> MyCptList;
76
77 private:
78 enum Status {
79 UNASSIGNED = '0',
80 CANDIDATE = '1',
81 FPOINT = '2',
82 TWOTIMER = '3',
83 CPOINT = '4',
84 CPOINT_U = '5'
85 };
86
87 std::string getStatusString(char status) const {
88 switch (status) {
89 case UNASSIGNED: return "UNASSIGNED";
90 case CANDIDATE: return "CANDIDATE";
91 case FPOINT: return "FPOINT";
92 case TWOTIMER: return "TWOTIMER";
93 case CPOINT: return "CPOINT";
94 case CPOINT_U: return "CPOINT_U";
95 default: return "UNKNOWN";
96 }
97 }
98
99 public:
100 //! @name Constructors/Destructors.
101 //@{
102
103 //! Constructor
104 Q2Q1uPFactory() {}
105
106 //! Destructor.
107 virtual ~Q2Q1uPFactory() {}
108 //@}
109
110 RCP<const ParameterList> GetValidParameterList() const;
111
112 //! Input
113 //@{
114
115 void DeclareInput(Level& fineLevel, Level& coarseLevel) const;
116
117 //@}
118
119 //! @name Build methods.
120 //@{
121
122 void Build(Level& fineLevel, Level& coarseLevel) const;
123 void BuildP(Level& fineLevel, Level& coarseLevel) const;
124
125 //@}
126
127 private:
128 void FindDist4Cpts(const Matrix& A, const MultiVector& coords, const Array<LO>& userCpts, std::vector<char>& status, MyCptList& myCpts, int levelID) const;
129 void PhaseTwoPattern(const Matrix& A, const MultiVector& coords, const std::vector<char>& status, MyCptList& myCpts) const;
130 void FindMidPoints(const Matrix& A, const MultiVector& coords, Array<LO>& Cptlist, const MyCptList& myCpts) const;
131 void CompDistances(const Matrix& A, LO start, int numDist, std::vector<LO>& dist1, std::vector<LO>& dist2,
132 std::vector<LO>& dist3, std::vector<LO>& dist4) const;
133 void CreateCrsPointers(const Matrix& A, ArrayRCP<const size_t>& ia, ArrayRCP<const LO>& ja) const;
134 void CptDepends2Pattern(const Matrix& A, const MyCptList& myCpts, RCP<Matrix>& P, LO offset) const;
135
136 void DumpStatus(const std::string& filename, const std::vector<char>& status, int NDim, bool isAmalgamated = true) const;
137 void DumpCoords(const std::string& filename, const MultiVector& coords) const;
138 };
139
140 // Sort a double array and move along list2 to match sorted array
141 template <class SC>
142 void Muelu_az_dsort2(std::vector<SC>& dlist, std::vector<int>& list2) {
143 int l, r, j, i, flag;
144 int RR2;
145 SC dRR, dK;
146
147 int N = dlist.size();
148 if (N <= 1) return;
149
150 l = N / 2 + 1;
151 r = N - 1;
152 l = l - 1;
153 dRR = dlist[l - 1];
154 dK = dlist[l - 1];
155
156 if (list2.size()) {
157 RR2 = list2[l - 1];
158 while (r != 0) {
159 j = l;
160 flag = 1;
161
162 while (flag == 1) {
163 i = j;
164 j = j + j;
165
166 if (j > r + 1)
167 flag = 0;
168 else {
169 if (j < r + 1)
170 if (dlist[j] > dlist[j - 1]) j = j + 1;
171
172 if (dlist[j - 1] > dK) {
173 dlist[i - 1] = dlist[j - 1];
174 list2[i - 1] = list2[j - 1];
175 } else {
176 flag = 0;
177 }
178 }
179 }
180 dlist[i - 1] = dRR;
181 list2[i - 1] = RR2;
182
183 if (l == 1) {
184 dRR = dlist[r];
185 RR2 = list2[r];
186 dK = dlist[r];
187 dlist[r] = dlist[0];
188 list2[r] = list2[0];
189 r = r - 1;
190 } else {
191 l = l - 1;
192 dRR = dlist[l - 1];
193 RR2 = list2[l - 1];
194 dK = dlist[l - 1];
195 }
196 }
197 dlist[0] = dRR;
198 list2[0] = RR2;
199 } else {
200 while (r != 0) {
201 j = l;
202 flag = 1;
203 while (flag == 1) {
204 i = j;
205 j = j + j;
206 if (j > r + 1)
207 flag = 0;
208 else {
209 if (j < r + 1)
210 if (dlist[j] > dlist[j - 1]) j = j + 1;
211 if (dlist[j - 1] > dK) {
212 dlist[i - 1] = dlist[j - 1];
213 } else {
214 flag = 0;
215 }
216 }
217 }
218 dlist[i - 1] = dRR;
219 if (l == 1) {
220 dRR = dlist[r];
221 dK = dlist[r];
222 dlist[r] = dlist[0];
223 r = r - 1;
224 } else {
225 l = l - 1;
226 dRR = dlist[l - 1];
227 dK = dlist[l - 1];
228 }
229 }
230 dlist[0] = dRR;
231 }
232 }
233
234 /* ******************************************************************* */
235 /* sort an array and move along list2 and/or list to match sorted array*/
236 /* ------------------------------------------------------------------- */
237 template <class SC>
238 void Muelu_az_sort(int list[], int N, int list2[], SC list3[]) {
239 int l, r, RR, K, j, i, flag;
240 int RR2;
241 SC RR3;
242
243 if (N <= 1) return;
244
245 l = N / 2 + 1;
246 r = N - 1;
247 l = l - 1;
248 RR = list[l - 1];
249 K = list[l - 1];
250
251 if ((list2 != NULL) && (list3 != NULL)) {
252 RR2 = list2[l - 1];
253 RR3 = list3[l - 1];
254 while (r != 0) {
255 j = l;
256 flag = 1;
257
258 while (flag == 1) {
259 i = j;
260 j = j + j;
261
262 if (j > r + 1)
263 flag = 0;
264 else {
265 if (j < r + 1)
266 if (list[j] > list[j - 1]) j = j + 1;
267
268 if (list[j - 1] > K) {
269 list[i - 1] = list[j - 1];
270 list2[i - 1] = list2[j - 1];
271 list3[i - 1] = list3[j - 1];
272 } else {
273 flag = 0;
274 }
275 }
276 }
277
278 list[i - 1] = RR;
279 list2[i - 1] = RR2;
280 list3[i - 1] = RR3;
281
282 if (l == 1) {
283 RR = list[r];
284 RR2 = list2[r];
285 RR3 = list3[r];
286
287 K = list[r];
288 list[r] = list[0];
289 list2[r] = list2[0];
290 list3[r] = list3[0];
291 r = r - 1;
292 } else {
293 l = l - 1;
294 RR = list[l - 1];
295 RR2 = list2[l - 1];
296 RR3 = list3[l - 1];
297 K = list[l - 1];
298 }
299 }
300
301 list[0] = RR;
302 list2[0] = RR2;
303 list3[0] = RR3;
304 } else if (list2 != NULL) {
305 RR2 = list2[l - 1];
306 while (r != 0) {
307 j = l;
308 flag = 1;
309
310 while (flag == 1) {
311 i = j;
312 j = j + j;
313
314 if (j > r + 1)
315 flag = 0;
316 else {
317 if (j < r + 1)
318 if (list[j] > list[j - 1]) j = j + 1;
319
320 if (list[j - 1] > K) {
321 list[i - 1] = list[j - 1];
322 list2[i - 1] = list2[j - 1];
323 } else {
324 flag = 0;
325 }
326 }
327 }
328
329 list[i - 1] = RR;
330 list2[i - 1] = RR2;
331
332 if (l == 1) {
333 RR = list[r];
334 RR2 = list2[r];
335
336 K = list[r];
337 list[r] = list[0];
338 list2[r] = list2[0];
339 r = r - 1;
340 } else {
341 l = l - 1;
342 RR = list[l - 1];
343 RR2 = list2[l - 1];
344 K = list[l - 1];
345 }
346 }
347
348 list[0] = RR;
349 list2[0] = RR2;
350 } else if (list3 != NULL) {
351 RR3 = list3[l - 1];
352 while (r != 0) {
353 j = l;
354 flag = 1;
355
356 while (flag == 1) {
357 i = j;
358 j = j + j;
359
360 if (j > r + 1)
361 flag = 0;
362 else {
363 if (j < r + 1)
364 if (list[j] > list[j - 1]) j = j + 1;
365
366 if (list[j - 1] > K) {
367 list[i - 1] = list[j - 1];
368 list3[i - 1] = list3[j - 1];
369 } else {
370 flag = 0;
371 }
372 }
373 }
374
375 list[i - 1] = RR;
376 list3[i - 1] = RR3;
377
378 if (l == 1) {
379 RR = list[r];
380 RR3 = list3[r];
381
382 K = list[r];
383 list[r] = list[0];
384 list3[r] = list3[0];
385 r = r - 1;
386 } else {
387 l = l - 1;
388 RR = list[l - 1];
389 RR3 = list3[l - 1];
390 K = list[l - 1];
391 }
392 }
393
394 list[0] = RR;
395 list3[0] = RR3;
396
397 } else {
398 while (r != 0) {
399 j = l;
400 flag = 1;
401
402 while (flag == 1) {
403 i = j;
404 j = j + j;
405
406 if (j > r + 1)
407 flag = 0;
408 else {
409 if (j < r + 1)
410 if (list[j] > list[j - 1]) j = j + 1;
411
412 if (list[j - 1] > K) {
413 list[i - 1] = list[j - 1];
414 } else {
415 flag = 0;
416 }
417 }
418 }
419
420 list[i - 1] = RR;
421
422 if (l == 1) {
423 RR = list[r];
424
425 K = list[r];
426 list[r] = list[0];
427 r = r - 1;
428 } else {
429 l = l - 1;
430 RR = list[l - 1];
431 K = list[l - 1];
432 }
433 }
434
435 list[0] = RR;
436 }
437 }
438
439 // Merge two already sorted lists into one combined sorted list.
440 // NOTE: lists are given as integer arrays. These integer arrays give
441 // locations in CoordDist[] defining the list values. That the ith value
442 // associated with the Candidates list is actually CoordDist[Candidates[i]].
443 template <class T>
444 void MergeSort(std::vector<int>& oldCandidates, size_t numOldCandidates, const std::vector<int>& newCandidates, const std::vector<T>& coordDist, ArrayRCP<const size_t> ia) {
445 size_t numNewCandidates = newCandidates.size();
446 size_t numCandidates = numOldCandidates + numNewCandidates;
447
448 oldCandidates.resize(numCandidates);
449
450 int i = numOldCandidates - 1;
451 int j = numNewCandidates - 1;
452 int k = numCandidates - 1;
453 while ((i >= 0) || (j >= 0)) {
454 if (i < 0)
455 oldCandidates[k--] = newCandidates[j--];
456 else if (j < 0)
457 oldCandidates[k--] = oldCandidates[i--];
458 else {
459 int ii = oldCandidates[i];
460 int jj = newCandidates[j];
461
462 // Must match code above. There is something arbitrary
463 // and crappy about the current weighting.
464
465 #ifdef optimal
466 if (-coordDist[ii] - .01 * (ia[ii + 1] - ia[ii]) + 1.e-10 * (ii + 1) <
467 -coordDist[jj] - .01 * (ia[jj + 1] - ia[jj]) + 1.e-10 * (jj + 1))
468 #else
469 if (coordDist[ii] - .0 * (ia[ii + 1] - ia[ii]) + 1.e-3 * (ii + 1) <
470 coordDist[jj] - .0 * (ia[jj + 1] - ia[jj]) + 1.e-3 * (jj + 1))
471 // if (ii < jj)
472 #endif
473 oldCandidates[k--] = newCandidates[j--];
474 else
475 oldCandidates[k--] = oldCandidates[i--];
476 }
477 }
478 }
479
480 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
481 RCP<const ParameterList> Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetValidParameterList() const {
482 RCP<ParameterList> validParamList = rcp(new ParameterList());
483
484 validParamList->set<RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
485
486 RCP<const FactoryBase> rcpThis = rcpFromRef(*this);
487 validParamList->set<RCP<const FactoryBase> >("CoordinatesVelocity", rcpThis, "Generating factory of the coordinates");
488 validParamList->set<RCP<const FactoryBase> >("AForPat", rcpThis, "Generating factory for Apattern");
489 validParamList->set<RCP<const FactoryBase> >("CoordinatesPressure", rcpThis, "Generating factory of the coordinates");
490 validParamList->set<RCP<const FactoryBase> >("p2vMap", rcpThis, "Mapping of pressure coords to u-velocity coords");
491
492 validParamList->set<std::string>("mode", "pressure", "Mode");
493 validParamList->set<bool>("phase2", false, "Use extra phase to improve pattern");
494 validParamList->set<bool>("dump status", false, "Output status");
495
496 validParamList->set<double>("tau_2", sqrt(0.0015), "tau_2 parameter from the paper (used for mid points)");
497
498 return validParamList;
499 }
500
501 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
502 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& fineLevel, Level& coarseLevel) const {
503 Input(fineLevel, "A");
504
505 const ParameterList& pL = GetParameterList();
506 bool pressureMode = (pL.get<std::string>("mode") == "pressure");
507
508 // NOTE: we cannot simply do Input(fineLevel, "CoordinatePressure", as in
509 // valid parameter list we specified *this as the generating factory
510 if (fineLevel.GetLevelID()) {
511 if (pressureMode) {
512 Input(fineLevel, "CoordinatesPressure");
513 } else {
514 Input(fineLevel, "CoordinatesVelocity");
515 Input(fineLevel, "AForPat");
516 Input(fineLevel, "p2vMap");
517 }
518 }
519 }
520
521 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
522 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(Level& fineLevel, Level& coarseLevel) const {
523 return BuildP(fineLevel, coarseLevel);
524 }
525
526 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
527 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildP(Level& fineLevel, Level& coarseLevel) const {
528 FactoryMonitor m(*this, "Build", coarseLevel);
529
530 typedef Teuchos::ScalarTraits<SC> STS;
531
532 const ParameterList& pL = GetParameterList();
533 bool pressureMode = (pL.get<std::string>("mode") == "pressure");
534 GetOStream(Runtime0) << (pressureMode ? "Pressure" : "Velocity") << " mode" << std::endl;
535
536 bool fineLevelID = fineLevel.GetLevelID();
537
538 RCP<Matrix> A = Get<RCP<Matrix> >(fineLevel, "A");
539 Xpetra::global_size_t N = A->getRowMap()->getGlobalNumElements();
540
541 RCP<MyCptList> myCpts = rcp(new MyCptList(N));
542 std::vector<char> status(N, UNASSIGNED);
543
544 RCP<MultiVector> coords;
545 RCP<Matrix> AForPat;
546 int NDim = -1;
547 if (pressureMode) {
548 if (fineLevelID == 0)
549 coords = fineLevel.Get<RCP<MultiVector> >("CoordinatesPressure", NoFactory::get());
550 else
551 coords = Get<RCP<MultiVector> >(fineLevel, "CoordinatesPressure");
552 NDim = coords->getNumVectors();
553
554 Array<LO> userCpts; // pressure does not reuse any CPOINTs
555 FindDist4Cpts(*A, *coords, userCpts, status, *myCpts, fineLevelID);
556
557 if (pL.get<bool>("phase2")) {
558 // Beef up any limited pattern
559 PhaseTwoPattern(*A, *coords, status, *myCpts);
560 }
561
562 } else {
563 // Do all the coarsening/pattern stuff on amalgamated velocities.
564 // We need to guarantee that all velocity dofs are treated identically
565 // This means that we must amalgmate AForPat and the velocity coordinates
566 if (fineLevelID == 0)
567 coords = fineLevel.Get<RCP<MultiVector> >("CoordinatesVelocity", NoFactory::get());
568 else
569 coords = Get<RCP<MultiVector> >(fineLevel, "CoordinatesVelocity");
570 if (fineLevelID == 0)
571 AForPat = fineLevel.Get<RCP<Matrix> >("AForPat", NoFactory::get());
572 else
573 AForPat = Get<RCP<Matrix> >(fineLevel, "AForPat");
574 NDim = coords->getNumVectors();
575
576 TEUCHOS_TEST_FOR_EXCEPTION(!coarseLevel.IsAvailable("PresCptsAndMids"), Exceptions::RuntimeError,
577 "Pressure points are not available");
578
579 Array<LO> userCpts = coarseLevel.Get<Array<LO> >("PresCptsAndMids");
580 GetOStream(Runtime1) << "Found stored pressure C-points: " << userCpts.size() << " " << userCpts << std::endl;
581
582 TEUCHOS_TEST_FOR_EXCEPTION(N % NDim, Exceptions::RuntimeError, "Number of velocity DOFs is odd");
583 Xpetra::global_size_t NN = N / NDim;
584
585 std::vector<GO> gNodeIds(NN);
586 for (size_t k = 0; k < NN; k++)
587 gNodeIds[k] = k;
588
589 RCP<Map> nodeMap = MapFactory::Build(AForPat->getRowMap()->lib(), NN, gNodeIds, 0, AForPat->getRowMap()->getComm());
590
591 // FIXME: remove magic number 30
592 RCP<Matrix> amalgA = MatrixFactory::Build(nodeMap, nodeMap, 30);
593 RCP<CrsMatrix> amalgA_crs = toCrsMatrix(amalgA);
594
595 // FIXME: this should be written similar to CoalesceDropFactory Merge
596 for (LO row = 0; row < as<LO>(AForPat->getRowMap()->getLocalNumElements()); row += NDim) {
597 GO grid = AForPat->getRowMap()->getGlobalElement(row);
598 GO currentId = grid / NDim;
599
600 Teuchos::ArrayView<const LO> inds;
601 Teuchos::ArrayView<const SC> vals;
602 AForPat->getLocalRowView(row, inds, vals);
603
604 size_t nnz = inds.size();
605
606 // Count the number of nonzero block columns in this row
607 // NOTE: this assumes that blocks are dense, i.e. that if one column is
608 // nonzero, then all columns in the same block are nonzeros
609 LO realnnz = 0;
610 for (LO col = 0; col < Teuchos::as<LO>(nnz); col++)
611 if (inds[col] % NDim == 0)
612 realnnz++;
613
614 if (realnnz == 0)
615 continue;
616
617 Teuchos::Array<GO> cnodeIds(realnnz, 0);
618 Teuchos::Array<SC> ones(realnnz, STS::one()); // Pattern has all 1's
619
620 realnnz = 0;
621 for (LO col = 0; col < Teuchos::as<LO>(nnz); col++) {
622 if (inds[col] % NDim == 0) {
623 GO gcid = AForPat->getColMap()->getGlobalElement(inds[col]);
624 cnodeIds[realnnz++] = gcid / NDim;
625 }
626 }
627 amalgA_crs->insertGlobalValues(currentId, cnodeIds, ones);
628 }
629 amalgA_crs->fillComplete(nodeMap, nodeMap);
630
631 // Amalgmate the velocity coordinates
632 // NOTE: This assumes that the original coords vector contains duplicated (x NDim) degrees of freedom
633 RCP<MultiVector> amalgCoords = Xpetra::MultiVectorFactory<SC, LO, GO, Node>::Build(nodeMap, NDim);
634
635 for (int j = 0; j < NDim; j++) {
636 ArrayRCP<SC> coordView = coords->getDataNonConst(j);
637 ArrayRCP<SC> amalgCoordView = amalgCoords->getDataNonConst(j);
638 for (size_t k = 0; k < NN; k++)
639 amalgCoordView[k] = coordView[k * NDim];
640 }
641
642 // On the finest level, we must map userCpts (which corresponds to
643 // pressure cpts and pressure mid-points) to the velocity variables
644 //
645 // NOTE: on coarser levels the lower numbered velocity dofs correspond
646 // to points that are co-located with pressures and the two numberings
647 // are identical so no translation is needed.
648 if (fineLevelID == 0) {
649 ArrayRCP<LO> p2vMap = fineLevel.Get<ArrayRCP<LO> >("p2vMap", NoFactory::get());
650
651 for (int k = 0; k < userCpts.size(); k++)
652 userCpts[k] = p2vMap[userCpts[k]] / NDim;
653 }
654
655 GetOStream(Runtime1) << "Amalgamated velocity C-points: " << userCpts.size() << " " << userCpts << std::endl;
656
657 // Now determine velocity CPOINTs for amalgamated system
658 RCP<MyCptList> amalgCpts = rcp(new MyCptList(NN));
659 std::vector<char> amalgStatus(NN, UNASSIGNED);
660
661 FindDist4Cpts(*amalgA, *amalgCoords, userCpts, amalgStatus, *amalgCpts, fineLevelID);
662
663 if (pL.get<bool>("phase2")) {
664 // Beef up any limited pattern
665 PhaseTwoPattern(*amalgA, *amalgCoords, amalgStatus, *amalgCpts);
666 }
667
668 // Unamalgamate data
669 Array<LO>& Cptlist = myCpts->getCList();
670 Array<LO>& amalgCptlist = amalgCpts->getCList();
671 std::vector<short>& numCpts = myCpts->getNumCpts();
672 std::vector<short>& amalgNumCpts = amalgCpts->getNumCpts();
673
674 int p = amalgCptlist.size();
675
676 Cptlist.resize(p * NDim);
677 for (int k = 0; k < p; k++) {
678 Cptlist[k * NDim] = amalgCptlist[k] * NDim;
679 for (int j = 1; j < NDim; j++)
680 Cptlist[k * NDim + j] = Cptlist[k * NDim] + j;
681 }
682
683 for (Xpetra::global_size_t i = 0; i < NN; i++) {
684 for (int j = 0; j < NDim; j++) {
685 status[N - 1 - (i * NDim + j)] = amalgStatus[NN - 1 - (i)];
686 numCpts[i * NDim + j] = amalgNumCpts[i];
687 for (int k = 0; k < amalgNumCpts[i]; k++)
688 (*myCpts)(i * NDim + j)[k] = (*amalgCpts)(i)[k] * NDim + j;
689 }
690 }
691 }
692
693 const bool doStatusOutput = pL.get<bool>("dump status");
694 if (doStatusOutput) {
695 const Array<LO>& Cptlist = myCpts->getCList();
696 std::vector<short>& numCpts = myCpts->getNumCpts();
697
698 std::string depPrefix = std::string("dep0-l") + toString(fineLevel.GetLevelID()) + (pressureMode ? "-p-" : "-v-");
699
700 std::vector<char> depStatus(N);
701 // Graph is unamalgamted, so we need to skip some CPOINTs as they are
702 // essentially duplicated for different velocities
703 for (int k = 0; k < Cptlist.size(); k += NDim) {
704 for (Xpetra::global_size_t i = 0; i < N; i++) {
705 bool isPresent = false;
706 for (int j = 0; j < numCpts[i]; j++)
707 if ((*myCpts)(i)[j] == Cptlist[k])
708 isPresent = true;
709 depStatus[i] = (isPresent ? FPOINT : UNASSIGNED);
710 }
711 depStatus[Cptlist[k]] = CPOINT;
712
713 DumpStatus(depPrefix + toString(k), depStatus, NDim, false);
714 }
715 }
716
717 RCP<Matrix> P;
718 // FIXME :hardwired hack, pressure gids must not overlap with velocity gids
719 if (pressureMode)
720 CptDepends2Pattern(*A, *myCpts, P, 999999);
721 else
722 CptDepends2Pattern(*AForPat, *myCpts, P, 0);
723
724 #if 0
725 if (pressureMode) {
726 IO::Write("Ap_l" + MueLu::toString(fineLevel.GetLevelID()) + ".mm", *A);
727 IO::Write("Pp_tent_l" + MueLu::toString(coarseLevel.GetLevelID()) + ".mm", *P);
728 } else {
729 IO::Write("Av_l" + MueLu::toString(fineLevel.GetLevelID()) + ".mm", *A);
730 IO::Write("Pv_tent_l" + MueLu::toString(coarseLevel.GetLevelID()) + ".mm", *P);
731 }
732 #endif
733
734 // Construct coarse map
735 RCP<const Map> coarseMap = P->getDomainMap();
736
737 // Construct coarse nullspace
738 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(coarseMap, 1);
739 coarseNullspace->putScalar(STS::one());
740
741 // Construct coarse coordinates
742 const Array<LO>& Cptlist = myCpts->getCList();
743 RCP<MultiVector> coarseCoords = MultiVectorFactory::Build(coarseMap, NDim);
744 for (int k = 0; k < NDim; k++) {
745 ArrayRCP<const SC> coords1D = coords->getData(k);
746 ArrayRCP<SC> coarseCoords1D = coarseCoords->getDataNonConst(k);
747
748 for (int i = 0; i < coarseCoords1D.size(); i++)
749 coarseCoords1D[i] = coords1D[Cptlist[i]];
750 }
751
752 // Level Set
753 Set(coarseLevel, "P", P);
754 Set(fineLevel, "CoarseMap", coarseMap);
755 if (pressureMode) {
756 Set(coarseLevel, "CoordinatesPressure", coarseCoords);
757
758 } else {
759 Set(coarseLevel, "CoordinatesVelocity", coarseCoords);
760 // FIXME: why does coarse pattern matrix look like?
761 RCP<Matrix> AP = Xpetra::MatrixMatrix<SC, LO, GO, NO>::Multiply(*AForPat, false, *P, false, GetOStream(Statistics2), true, true);
762 RCP<Matrix> RAP = Xpetra::MatrixMatrix<SC, LO, GO, NO>::Multiply(*P, true, *AP, false, GetOStream(Statistics2), true, true);
763 Set(coarseLevel, "AForPat", RAP);
764 }
765 Set(coarseLevel, "Nullspace", coarseNullspace);
766
767 // Compute data for velocity
768 if (pressureMode) {
769 Array<LO> velCptlist = Cptlist;
770 FindMidPoints(*A, *coords, velCptlist, *myCpts);
771 coarseLevel.Set<Array<LO> >("PresCptsAndMids", velCptlist, NoFactory::get());
772 }
773 }
774
775 template <class T>
776 void PrintVector(const std::vector<T>& v, const std::string& name, int n = -1) {
777 std::cout << "======================" << std::endl;
778 if (!name.empty())
779 std::cout << "=== " << name << " ===" << std::endl;
780 if (n == -1)
781 n = v.size();
782 for (int i = 0; i < n; i++)
783 std::cout << i << ": " << v[i] << std::endl;
784 std::cout << "======================" << std::endl;
785 }
786
787 // distance2 returns _squared_ distance
788 // i.e. the final sqrt calculation is not done
789 template <class SC>
790 SC distance2(const ArrayRCP<ArrayRCP<const SC> >& coords1D, int i, int j) {
791 const int NDim = coords1D.size();
792
793 SC d = Teuchos::ScalarTraits<SC>::zero();
794 for (int k = 0; k < NDim; k++) {
795 SC dtmp = coords1D[k][j] - coords1D[k][i];
796 d += dtmp * dtmp;
797 }
798
799 return d;
800 }
801
802 template <int N = 10000>
803 std::string i2s(int i) {
804 std::ostringstream os;
805 if (i >= N) {
806 os << i;
807 } else {
808 if (i < 10) os << "0";
809 if (i < 100) os << "0";
810 if (i < 1000) os << "0";
811 if (i < 10000) os << "0";
812 if (i < 100000) os << "0";
813 if (i < 1000000) os << "0";
814 if (i < 10000000) os << "0";
815 if (i < 100000000) os << "0";
816 if (i < 1000000000) os << "0";
817 }
818 os << i;
819 return os.str();
820 }
821
822 // Initial fill Cptlist with a set of distance 4 points (during phase one).
823 // Additional Cpts are then determined looking for large gaps between the
824 // phase one Cpts. Candidate additional Cpts corresponds to phase one FPOINTs
825 // that have only 1 or 2 Cpts within a graph distance of 3 and are generally
826 // far (via graph or coordinate distances) from existing Cpts. We also define
827 // a sparsity pattern. An initial pattern is computed which basically
828 // includes all FPOINTs within a distance 3 from a Cpt. Additional entries
829 // are added to the initial sparsity pattern via PhaseTwoPattern(). These
830 // points correspond to Fpoints that only interpolate from 2 or less Cpts,
831 // are also far from existing Cpoints, and where the orientation of the
832 // interpolation Cpts is skewed to one side of the Fpoint (see
833 // PhaseTwoPattern for more details).
834 //
835 // NOTE: inefficiencies
836 // The main inefficiency is the sorting of the CandidateList. This is
837 // expensive and it is not clear how important it really is. My guess is that
838 // we could actually leave the CandidateList unsorted and things would work
839 // almost as well. We could also do some kind of hybrid where we sort the
840 // first 100 candidates. Use this data to create something like 10 bins and
841 // for any further candidates just put them in the right bin. When we need to
842 // choose a new Cpt from the CandidateList, just pick any one from the lowest
843 // populated bin. There are also potential inefficiences with respect to
844 // malloc(). I doubt that these are a big problem, but one could allocate
845 // some workspaces ahead of time to avoid the constant malloc/free cycle in
846 // CompDistances().
847 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
848 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
849 FindDist4Cpts(const Matrix& A, const MultiVector& coords, const Array<LO>& userCpts, std::vector<char>& status, MyCptList& myCpts, int levelID) const {
850 int NDim = coords.getNumVectors();
851 size_t numRows = A.getLocalNumRows();
852
853 ArrayRCP<const size_t> ia;
854 ArrayRCP<const LO> ja;
855 CreateCrsPointers(A, ia, ja);
856
857 ArrayRCP<ArrayRCP<const SC> > coords1D(NDim);
858 for (int k = 0; k < NDim; k++)
859 coords1D[k] = coords.getData(k);
860
861 typedef Teuchos::ScalarTraits<SC> STS;
862 SC zero = STS::zero();
863
864 // Initialize coordDist to some large value.
865 // coordDist is an attempt to measure an average distance from a given
866 // point to all the CPOINTs that it depends on. The averages are harmonic, so
867 // basically the initial large coordDist will be averaged away with the 1st
868 // harmonic average. The initial big value computed here is
869 // (Max(x)-Min(x))^2 + (Max(y)-Min(y))^2 + (Max(z)-Min(z))^2
870 SC big = zero;
871 for (int i = 0; i < NDim; i++) {
872 SC dmin = *std::min_element(coords1D[i].begin(), coords1D[i].end());
873 SC dmax = *std::max_element(coords1D[i].begin(), coords1D[i].end());
874
875 big += ((dmax - dmin) * (dmax - dmin));
876 }
877 // MATCH_MATLAB
878 std::vector<SC> coordDist(numRows, 10000 * big);
879
880 const ParameterList& pL = GetParameterList();
881 const bool doStatusOutput = pL.get<bool>("dump status");
882 const bool pressureMode = (pL.get<std::string>("mode") == "pressure");
883
884 // Set all Dirichlet points as Fpoints FIXME: why FPOINTs?
885 // However, if a Dirichlet point is in userCpts, it will be added to the
886 // Cpt list later
887 for (size_t i = 0; i < numRows; i++)
888 if (ia[i + 1] - ia[i] == 1)
889 status[i] = FPOINT;
890
891 // userCpts have already been fixed to be CPOINTs so we want to first mark
892 // them appropriately, and put them first in the CPOINT list, but still go
893 // through loops below to update distances and FPOINTs. Initialization is
894 // done here so that these points do not end up with more than 1 nnz in the
895 // associated sparsity pattern row.
896 TEUCHOS_TEST_FOR_EXCEPTION(myCpts.getCList().size(), Exceptions::RuntimeError,
897 "myCpts in FindDist4Points must be uninitialized");
898 Array<LO>& Cptlist = myCpts.getCList();
899 for (int i = 0; i < userCpts.size(); i++) {
900 status[userCpts[i]] = CPOINT_U;
901 Cptlist.push_back(userCpts[i]);
902 }
903
904 std::string st = std::string("status-l") + toString(levelID) + (pressureMode ? "-p-" : "-v-");
905 int dumpCount = 0;
906 if (doStatusOutput) {
907 DumpCoords("coord-l" + toString(levelID) + (pressureMode ? "-p" : "-v"), coords);
908 DumpStatus(st + i2s(dumpCount++) + "-A", status, NDim);
909 }
910
911 std::vector<short>& numCpts = myCpts.getNumCpts();
912
913 // Determine CPOINTs
914 int userCcount = 0;
915 size_t numCandidates = 0;
916 std::vector<char> distIncrement(numRows, 0);
917 std::vector<double> cumGraphDist(numRows, 0.);
918 std::vector<LO> candidateList(numRows, 0);
919 size_t i = 0;
920 while (i < numRows) {
921 LO newCpt = -1;
922
923 // Check userCpts list
924 //
925 // These were essentially already determined to be CPOINTs by some other
926 // functions. We want to then add them one-by-one, updating distances,
927 // and look to see if further CPOINTs can be added once we have finished
928 // all of the userCpts
929 if (userCcount < userCpts.size())
930 newCpt = userCpts[userCcount++];
931
932 // Check for possible CPOINT on candidate list
933 // FIXME: Could CANDIDATE list contain non-CANDIDATE statuses?
934 while ((newCpt == -1) && (numCandidates > 0)) {
935 if (status[candidateList[numCandidates - 1]] <= CANDIDATE) {
936 newCpt = candidateList[numCandidates - 1];
937 status[newCpt] = CPOINT;
938
939 if (doStatusOutput)
940 DumpStatus(st + i2s(dumpCount++) + "-B", status, NDim);
941 }
942 numCandidates--;
943 // FIXME: Why is there no i++ here?
944 }
945
946 // If no new CPOINT identified in candidate list, check the unassigned list
947 while ((newCpt == -1) && (i < numRows)) {
948 if (status[i] == UNASSIGNED) {
949 newCpt = i;
950 status[newCpt] = CPOINT;
951
952 if (doStatusOutput)
953 DumpStatus(st + i2s(dumpCount++) + "-C", status, NDim);
954 }
955 i++;
956 }
957
958 // Update distances and the status of neighbors neighbors to reflect a
959 // newly found CPOINT
960 if (newCpt != -1) {
961 std::vector<LO> dist1, dist2, dist3, dist4;
962 // FIXME: Should CompDistances automatically exclude other CPOINTs?
963 CompDistances(A, newCpt, 4, dist1, dist2, dist3, dist4);
964
965 // Make sure that the only CPOINT in dist3 is newCpt. All others should be excluded.
966 int numDist3 = 0;
967 for (size_t k = 0; k < dist3.size(); k++) {
968 LO j = dist3[k];
969 if (status[j] < CPOINT)
970 dist3[numDist3++] = j;
971 }
972 dist3.resize(numDist3);
973 dist3.push_back(newCpt);
974
975 // UNASSIGNED or CANDIDATE distance 3 and closer neighbors are put into FPOINT list
976 // FIXME: why not put TWOTIMER there too?
977 bool dumpStatus = false;
978 for (size_t k = 0; k < dist3.size(); k++) {
979 LO j = dist3[k];
980 if (status[j] == UNASSIGNED || status[j] == CANDIDATE) {
981 status[j] = FPOINT;
982 dumpStatus = true;
983 }
984 }
985 if (dumpStatus && doStatusOutput)
986 DumpStatus(st + i2s(dumpCount++) + "-D", status, NDim);
987
988 // Update myCpts() to reflect dependence of neighbors on newCpt
989 for (size_t k = 0; k < dist3.size(); k++) {
990 LO j = dist3[k];
991
992 TEUCHOS_TEST_FOR_EXCEPTION(numCpts[j] >= myCpts.getNnzPerRow(), Exceptions::RuntimeError,
993 "Increase max number of C points per row");
994 myCpts(j)[numCpts[j]++] = newCpt;
995 }
996
997 // Update cumGraphDist
998 // NOTE: order matters as dist2 is contained within dist3, etc.
999 // FIXME: Do dist2 and dist1 contain CPOINTs?
1000 for (size_t k = 0; k < dist3.size(); k++) distIncrement[dist3[k]] = 3;
1001 for (size_t k = 0; k < dist2.size(); k++) distIncrement[dist2[k]] = 2;
1002 for (size_t k = 0; k < dist1.size(); k++) distIncrement[dist1[k]] = 1;
1003 distIncrement[newCpt] = 0;
1004
1005 for (size_t k = 0; k < dist3.size(); k++) {
1006 LO j = dist3[k];
1007 // MATCH_MATLAB: (numCpts[j]-1) is to match Matlab, where numCpts is updated after distance calculation
1008 cumGraphDist[j] = (cumGraphDist[j] * (numCpts[j] - 1) + distIncrement[j]) / numCpts[j];
1009 }
1010 cumGraphDist[newCpt] = 0;
1011
1012 // Compute coordinate distance to CPOINT
1013 //
1014 // Distance of CANDIDATEs to CPOINTs will be used to determine the next
1015 // chosen CPOINT from the candidate list. Distances are also used to
1016 // decide where new CPOINTs should be added.
1017 for (size_t k = 0; k < dist4.size(); k++) {
1018 LO j = dist4[k];
1019
1020 SC distance = distance2(coords1D, newCpt, j);
1021
1022 // Harmonic average new distance with old distance
1023 // There should really be a '2' in front of this expression. This
1024 // is actually a bug in the code. However, if I put a '2', I don't
1025 // get the perfect coarsening for a uniform mesh ... so I'm leaving
1026 // if for now without the 2.
1027 // MATCH_MATLAB
1028 coordDist[j] = 2.0 * (coordDist[j] * distance) / (coordDist[j] + distance);
1029 #if 0
1030 SC kkk = 10.;
1031 if (coordDist[j] > distance)
1032 coordDist[j] = (kkk*coordDist[j]*distance)/(coordDist[j]*(kkk-1)+ distance);
1033 coordDist[j] = (kkk*coordDist[j]*distance)/(coordDist[j] + distance*(kkk-1));
1034 #endif
1035 }
1036
1037 // Mark all unassigned dist4 points as CANDIDATE and compress
1038 // dist4 so that it only contains entries for the candidate list.
1039 size_t numNewCandidates = 0;
1040 dumpStatus = false;
1041 for (size_t k = 0; k < dist4.size(); k++) {
1042 LO j = dist4[k];
1043
1044 // NOTE: numNewCandidates is always <= k, so we don't overwrite the
1045 // dist4 before reading
1046 if (status[j] == CANDIDATE) {
1047 // Mark as already being assigned again to candidate list so that
1048 // entry in old 'sorted' candidate list can be removed and a new
1049 // 'unsorted' entry can be created. This new entry will later be
1050 // sorted reflecting the new coordinate distance.
1051 status[j] = TWOTIMER;
1052 dist4[numNewCandidates++] = j;
1053 dumpStatus = true;
1054
1055 } else if (status[j] == UNASSIGNED) {
1056 status[j] = CANDIDATE;
1057 dist4[numNewCandidates++] = j;
1058 dumpStatus = true;
1059 }
1060 }
1061 dist4.resize(numNewCandidates);
1062 if (dumpStatus && doStatusOutput)
1063 DumpStatus(st + i2s(dumpCount++) + "-E", status, pressureMode);
1064
1065 // Now remove all TWOTIMERs from the old candidate list
1066 size_t numOldCandidates = 0;
1067 dumpStatus = false;
1068 for (size_t k = 0; k < numCandidates; k++) {
1069 LO j = candidateList[k];
1070
1071 if (status[j] == CANDIDATE) {
1072 candidateList[numOldCandidates++] = j;
1073 }
1074 if (status[j] == TWOTIMER) {
1075 status[j] = CANDIDATE;
1076 dumpStatus = true;
1077 }
1078 }
1079 if (dumpStatus && doStatusOutput)
1080 DumpStatus(st + i2s(dumpCount++) + "-F", status, NDim);
1081
1082 // Sort the candidates based on distances (breaking ties via degrees,
1083 // encouraging points near boundary). First, we order new candidates
1084 // and then we merge together two sorted lists.
1085 //
1086 // NOTE: to match matlab (and break ties), I added the 1.e-10 term
1087 std::vector<double> ddtemp(numNewCandidates);
1088 for (size_t k = 0; k < numNewCandidates; k++) {
1089 LO j = dist4[k];
1090 // MATCH_MATLAB
1091 #ifdef optimal
1092 // This one is better, but we are trying to replicate Matlab now
1093 ddtemp[k] = -coordDist[j] - .01 * (ia[j + 1] - ia[j]) + 1e-10 * (j + 1);
1094 #else
1095 ddtemp[k] = +coordDist[j] - 0.0 * (ia[j + 1] - ia[j]) + 1e-3 * (j + 1);
1096 #endif
1097 }
1098 Muelu_az_dsort2(ddtemp, dist4);
1099
1100 MergeSort(candidateList, numOldCandidates, dist4, coordDist, ia);
1101 numCandidates = numOldCandidates + numNewCandidates;
1102 }
1103 }
1104
1105 // Add additional CPOINTs based on some score which includes the number of
1106 // CPOINTs that an FPOINT depends on as well as its distance (both graph
1107 // and coordinate) to nearby CPOINTs.
1108 const double graphWeight = 0.8;
1109 const double orientWeight = 0.5;
1110 for (int numCDepends = 1; numCDepends <= 2; numCDepends++) {
1111 numCandidates = 0;
1112
1113 std::vector<int> candidates;
1114 for (i = 0; i < numRows; i++)
1115 if (status[i] < CPOINT && numCpts[i] == numCDepends) {
1116 candidates.push_back(i);
1117 numCandidates++;
1118 }
1119
1120 if (numCandidates != 0) {
1121 // Sort FPOINTs based on distance to CPOINTs and orientation
1122 double maxGraphDist = -1e20;
1123 double maxCoordDist = -1e20;
1124 for (size_t p = 0; p < numCandidates; p++) {
1125 LO j = candidates[p];
1126
1127 maxGraphDist = std::max(maxGraphDist, cumGraphDist[j]);
1128 maxCoordDist = std::max(maxCoordDist, coordDist[j]);
1129 }
1130
1131 std::vector<double> score(numCandidates);
1132 std::vector<double> orientation(numCandidates);
1133 for (size_t p = 0; p < numCandidates; p++) {
1134 LO j = candidates[p];
1135
1136 double graphScore = cumGraphDist[j] / maxGraphDist;
1137 double coordScore = coordDist[j] / maxCoordDist;
1138 // MATCH_MATLAB
1139 score[p] = -(graphWeight * graphScore + (1 - graphWeight) * coordScore + 1e-6 * (j + 1));
1140
1141 if (numCDepends == 2) {
1142 // Orientation of -1 means that we have two CPOINTs on opposite
1143 // sides of the FPOINT. Orientation of 0 implies that things are
1144 // orthogonal. orientation of 1 implies that Cpoints are on the
1145 // same side
1146 SC norm = zero, vec1[3], vec2[3];
1147 for (int k = 0; k < NDim; k++) {
1148 vec1[k] = coords1D[k][j] - coords1D[k][myCpts(j)[0]];
1149 norm += vec1[k] * vec1[k];
1150 }
1151 norm = sqrt(norm);
1152 for (int k = 0; k < NDim; k++)
1153 vec1[k] /= norm;
1154
1155 norm = zero;
1156 for (int k = 0; k < NDim; k++) {
1157 vec2[k] = coords1D[k][j] - coords1D[k][myCpts(j)[1]];
1158 norm += vec2[k] * vec2[k];
1159 }
1160 norm = sqrt(norm);
1161 for (int k = 0; k < NDim; k++)
1162 vec2[k] /= norm;
1163
1164 orientation[p] = zero;
1165 for (int k = 0; k < NDim; k++)
1166 orientation[p] += vec1[k] * vec2[k];
1167
1168 score[p] = -(orientWeight * orientation[p] - (1 - orientWeight) * score[p]);
1169
1170 } else {
1171 orientation[p] = 1.0;
1172 }
1173 }
1174
1175 std::vector<LO> index(numCandidates);
1176 for (size_t p = 0; p < numCandidates; p++)
1177 index[p] = p;
1178 Muelu_az_dsort2(score, index);
1179
1180 for (size_t p = 0; p < numCandidates; p++) {
1181 int newCpt = candidates[index[p]];
1182
1183 if (numCpts[newCpt] == numCDepends &&
1184 cumGraphDist[newCpt] >= 2.6 &&
1185 orientation[index[p]] > -0.2) {
1186 status[newCpt] = CPOINT;
1187 numCpts[newCpt] = 1;
1188 myCpts(newCpt)[0] = newCpt;
1189
1190 if (doStatusOutput)
1191 DumpStatus(st + i2s(dumpCount++) + "-G", status, NDim);
1192
1193 std::vector<LO> dist1, dist2, dist3, dist4;
1194 CompDistances(A, newCpt, 3, dist1, dist2, dist3, dist4);
1195
1196 // Make sure that there are no CPOINTs in dist1, dist2, dist3.
1197 int numDist1 = 0;
1198 for (size_t k = 0; k < dist1.size(); k++) {
1199 LO j = dist1[k];
1200 if (status[j] < CPOINT)
1201 dist1[numDist1++] = j;
1202 }
1203 dist1.resize(numDist1);
1204 int numDist2 = 0;
1205 for (size_t k = 0; k < dist2.size(); k++) {
1206 LO j = dist2[k];
1207 if (status[j] < CPOINT)
1208 dist2[numDist2++] = j;
1209 }
1210 dist2.resize(numDist2);
1211 int numDist3 = 0;
1212 for (size_t k = 0; k < dist3.size(); k++) {
1213 LO j = dist3[k];
1214 if (status[j] < CPOINT)
1215 dist3[numDist3++] = j;
1216 }
1217 dist3.resize(numDist3);
1218
1219 // Update cumGraphDist
1220 // NOTE: order matters as dist2 is contained within dist3, etc.
1221 for (size_t k = 0; k < dist3.size(); k++) distIncrement[dist3[k]] = 3;
1222 for (size_t k = 0; k < dist2.size(); k++) distIncrement[dist2[k]] = 2;
1223 for (size_t k = 0; k < dist1.size(); k++) distIncrement[dist1[k]] = 1;
1224 distIncrement[newCpt] = 0;
1225
1226 // Update myCpts() to reflect dependence of neighbors on newCpt
1227 for (size_t k = 0; k < dist3.size(); k++) {
1228 LO j = dist3[k];
1229
1230 TEUCHOS_TEST_FOR_EXCEPTION(numCpts[j] >= myCpts.getNnzPerRow(), Exceptions::RuntimeError,
1231 "Increase max number of C points per row");
1232 myCpts(j)[numCpts[j]++] = newCpt;
1233 }
1234
1235 for (size_t k = 0; k < dist3.size(); k++) {
1236 LO j = dist3[k];
1237 // (numCpts[j]-1) is to match Matlab, where numCpts is updated after distance calculation
1238 cumGraphDist[j] = (cumGraphDist[j] * (numCpts[j] - 1) + distIncrement[j]) / numCpts[j];
1239 }
1240 cumGraphDist[newCpt] = 0;
1241 }
1242 }
1243 }
1244 }
1245
1246 // Build up the CPOINT list
1247 for (i = 0; i < numRows; i++)
1248 if (status[i] == CPOINT)
1249 Cptlist.push_back(i);
1250 else if (status[i] == CPOINT_U)
1251 status[i] = CPOINT;
1252 }
1253
1254 // Look at pattern rows which have only one or two nonzeros and consider
1255 // adding additional nonzero entries. New nonzero possibilities for row k are
1256 // obtained by looking at k's neighbors (as determined by the matrix) to see
1257 // what CPOINTs these neighbors interpolate from. The final determination is
1258 // based on a composite score that considers the distance between k and the
1259 // possible new CPOINT as well as the orientation of the new possible CPOINT
1260 // with respect to k's current CPOINTs. Generally, points which are on the
1261 // opposite side of k' current CPOINTs are favored.
1262 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1263 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1264 PhaseTwoPattern(const Matrix& A, const MultiVector& coords, const std::vector<char>& status, MyCptList& myCpts) const {
1265 GetOStream(Runtime0) << "Starting phase 2" << std::endl;
1266
1267 int NDim = coords.getNumVectors();
1268 size_t numRows = A.getLocalNumRows();
1269
1270 ArrayRCP<const size_t> ia;
1271 ArrayRCP<const LO> ja;
1272 CreateCrsPointers(A, ia, ja);
1273
1274 ArrayRCP<ArrayRCP<const SC> > coords1D(NDim);
1275 for (int k = 0; k < NDim; k++)
1276 coords1D[k] = coords.getData(k);
1277
1278 std::vector<short>& numCpts = myCpts.getNumCpts();
1279
1280 typedef Teuchos::ScalarTraits<SC> STS;
1281 SC zero = STS::zero();
1282
1283 size_t N = myCpts.getCList().size();
1284 std::vector<int> nearbyCs(N);
1285 std::vector<double> score(N);
1286 std::vector<double> dists(N);
1287
1288 std::vector<char> scratch(numRows, 'n');
1289 std::vector<LO> candidates(numRows);
1290
1291 for (int numCDepends = 1; numCDepends <= 2; numCDepends++) {
1292 int numCandidates = 0;
1293 for (size_t i = 0; i < numRows; i++)
1294 if (numCpts[i] == numCDepends && status[i] < CPOINT)
1295 candidates[numCandidates++] = i;
1296
1297 for (int p = 0; p < numCandidates; p++) {
1298 // Mark already existing CPOINT dependencies
1299 LO i = candidates[p];
1300 LO* cpts = myCpts(i);
1301 for (int k = 0; k < numCpts[i]; k++)
1302 scratch[cpts[k]] = 'y';
1303
1304 // Make a list of my neighbors' CPOINT dependencies, excluding all
1305 // already existing CPOINT dependencies for candidates[p]
1306 const LO* neighs = &ja[ia[i]];
1307 int numNeighbors = ia[i + 1] - ia[i];
1308 int numNearbyCs = 0;
1309 for (int k = 0; k < numNeighbors; k++) {
1310 LO curNeigh = neighs[k];
1311 const LO* neighCs = myCpts(curNeigh);
1312
1313 for (int j = 0; j < numCpts[curNeigh]; j++) {
1314 LO neighNeighC = neighCs[j];
1315
1316 if (scratch[neighNeighC] != 'y') {
1317 scratch[neighNeighC] = 'y';
1318 nearbyCs[numNearbyCs++] = neighNeighC;
1319 }
1320 }
1321 }
1322
1323 // Reset scratch
1324 for (int k = 0; k < numCpts[i]; k++)
1325 scratch[cpts[k]] = 'n';
1326 for (int k = 0; k < numNearbyCs; k++)
1327 scratch[nearbyCs[k]] = 'n';
1328
1329 // MATCH_MATLB
1330 std::sort(nearbyCs.begin(), nearbyCs.begin() + numNearbyCs);
1331
1332 if (numNearbyCs != 0) {
1333 SC norm = zero, vec1[3] = {0.0, 0.0, 0.0}, vec2[3] = {0.0, 0.0, 0.0};
1334 for (int k = 0; k < NDim; k++) {
1335 vec1[k] = coords1D[k][i] - coords1D[k][cpts[0]];
1336 norm += vec1[k] * vec1[k];
1337 }
1338 norm = sqrt(norm);
1339 for (int k = 0; k < NDim; k++)
1340 vec1[k] /= norm;
1341
1342 if (numCDepends == 2) {
1343 norm = zero;
1344 for (int k = 0; k < NDim; k++) {
1345 vec2[k] = coords1D[k][i] - coords1D[k][cpts[1]];
1346 norm += vec2[k] * vec2[k];
1347 }
1348 norm = sqrt(norm);
1349 for (int k = 0; k < NDim; k++)
1350 vec2[k] /= norm;
1351
1352 } else {
1353 for (int k = 0; k < NDim; k++)
1354 vec2[k] = vec1[k];
1355 }
1356
1357 for (int k = 0; k < numNearbyCs; k++) score[k] = 0;
1358 for (int k = 0; k < numNearbyCs; k++) dists[k] = 0;
1359
1360 for (int j = 0; j < numNearbyCs; j++) {
1361 SC newVec[3];
1362
1363 norm = 0;
1364 for (int k = 0; k < NDim; k++) {
1365 newVec[k] = coords1D[k][nearbyCs[j]] - coords1D[k][i];
1366 norm += newVec[k] * newVec[k];
1367 }
1368 norm = sqrt(norm);
1369 for (int k = 0; k < NDim; k++)
1370 newVec[k] /= norm;
1371
1372 score[j] = 0;
1373 for (int k = 0; k < NDim; k++)
1374 score[j] += newVec[k] * (vec1[k] + vec2[k]);
1375 // Why??
1376 score[j] /= 2;
1377
1378 dists[j] = norm;
1379 }
1380
1381 // Normalize distances
1382 double maxDist = 0.;
1383 for (int j = 0; j < numNearbyCs; j++)
1384 if (maxDist < dists[j])
1385 maxDist = dists[j];
1386
1387 for (int j = 0; j < numNearbyCs; j++)
1388 dists[j] /= maxDist;
1389
1390 const double distWeight = 0.3;
1391 double maxComposite = -10000;
1392 int maxIndex = -1;
1393 for (int j = 0; j < numNearbyCs; j++) {
1394 // The formula is
1395 // if (score[j] - distWeight*dists[j] > maxComposite)
1396 // MATCH_MATLAB
1397 double composite = score[j] - distWeight * dists[j] + 1.0e-7 * (nearbyCs[j] - 1);
1398 if (maxComposite < composite) {
1399 maxComposite = composite;
1400 maxIndex = j;
1401 }
1402 }
1403
1404 if (score[maxIndex] - 0.2 * numCDepends > -0.3) {
1405 TEUCHOS_TEST_FOR_EXCEPTION(numCpts[i] >= myCpts.getNnzPerRow(),
1406 Exceptions::RuntimeError, "Increase max number of C points per row");
1407 myCpts(i)[numCpts[i]++] = nearbyCs[maxIndex];
1408 }
1409 }
1410 }
1411 }
1412 }
1413
1414 // Compute mid-points associated with adjacent Cpts.
1415 //
1416 // Basically, the location of each Fpoint is compared with the average
1417 // location of each Cpoint in Pat(Fpoint,:). If the location of the Fpoint is
1418 // close to this average it is considered as a possible mid-point. In
1419 // addition, however, we look to see if a possible mid-point is "close" or
1420 // not to an already computed mid-point. If it is NOT too close, then this
1421 // possible mid-point is declared to be an actual mid-point.
1422 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1423 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1424 FindMidPoints(const Matrix& A, const MultiVector& coords, Array<LO>& Cptlist, const MyCptList& myCpts) const {
1425 int NDim = coords.getNumVectors();
1426 size_t numRows = A.getLocalNumRows();
1427
1428 const ParameterList& pL = GetParameterList();
1429 double tau_2 = pL.get<double>("tau_2");
1430 // In calculations, we use tau_2^2
1431 tau_2 = tau_2 * tau_2;
1432
1433 const std::vector<short>& numCpts = myCpts.getNumCpts();
1434
1435 ArrayRCP<const size_t> ia;
1436 ArrayRCP<const LO> ja;
1437 CreateCrsPointers(A, ia, ja);
1438
1439 ArrayRCP<ArrayRCP<const SC> > coords1D(NDim);
1440 for (int k = 0; k < NDim; k++)
1441 coords1D[k] = coords.getData(k);
1442
1443 typedef Teuchos::ScalarTraits<SC> STS;
1444 SC zero = STS::zero();
1445
1446 // Calculate number of nonzeros per row, make it negative, and then sort.
1447 // The idea is that when assigning midpoints, we want to start by looking
1448 // at points which have many coarse point dependencies
1449 std::vector<int> nnzPerRow(numRows);
1450 std::vector<int> index(numRows);
1451 for (size_t i = 0; i < numRows; i++) {
1452 nnzPerRow[i] = (numCpts[i] ? numCpts[i] : 1);
1453 nnzPerRow[i] = -100000 * numCpts[i] + i;
1454 index[i] = i;
1455 }
1456
1457 // Sort only for the purposes of filling 'index', which determines the
1458 // order that we search for possible midpoints
1459 Muelu_az_sort<SC>(&nnzPerRow[0], numRows, &index[0], NULL);
1460
1461 // Reset so that we have unsorted version of nnzPerRow and also mark points
1462 // which cannot be mid points
1463 std::vector<char> lookedAt(numRows, 'n');
1464 for (size_t i = 0; i < numRows; i++) {
1465 nnzPerRow[i] = (numCpts[i] ? numCpts[i] : 1);
1466 if (nnzPerRow[i] == 1)
1467 lookedAt[i] = 'y';
1468 }
1469 for (int i = 0; i < Cptlist.size(); i++)
1470 lookedAt[Cptlist[i]] = 'y';
1471
1472 // Compute some target midpoints based on taking averages associated with
1473 // the sparsity pattern and coarse grid point locations.
1474 ArrayRCP<ArrayRCP<SC> > targetMidCoords1D(NDim);
1475 for (int k = 0; k < NDim; k++) {
1476 ArrayRCP<SC>& target1D = targetMidCoords1D[k];
1477
1478 target1D.resize(numRows);
1479 for (size_t i = 0; i < numRows; i++) {
1480 target1D[i] = zero;
1481
1482 for (int j = 0; j < numCpts[i]; j++)
1483 target1D[i] += coords1D[k][myCpts(i)[j]];
1484
1485 target1D[i] /= nnzPerRow[i];
1486 }
1487 }
1488
1489 std::vector<char> isMidPoint(numRows, 'n');
1490 std::vector<char> inNearbyCs(numRows, 'n');
1491 std::vector<char> inNeighs(numRows, 'n');
1492 std::vector<int> neighs(numRows);
1493 std::vector<int> sameCGroup(50);
1494
1495 int numMidPoints = 0;
1496 for (size_t i = 0; i < numRows; i++) {
1497 int curF = index[i];
1498
1499 if (lookedAt[curF] == 'y')
1500 continue;
1501 lookedAt[curF] = 'y';
1502
1503 const LO* curFCs = myCpts(curF);
1504
1505 for (int j = 0; j < numCpts[curF]; j++)
1506 inNearbyCs[curFCs[j]] = 'y';
1507
1508 // Find all FPOINTs with the same curFCs (perhaps
1509 // containing additional curFCs) as curF and
1510 // put them in sameCGroup
1511 int numNeigh = 0;
1512 neighs[numNeigh++] = curF;
1513 inNeighs[curF] = 'y';
1514
1515 int nextLayerStart = 0;
1516 int nextLayerEnd = 0;
1517 int numSameGrp = 0;
1518
1519 int flag = 1;
1520 while (flag == 1) {
1521 flag = 0;
1522
1523 for (int k = nextLayerStart; k <= nextLayerEnd; k++) {
1524 LO curNeigh = neighs[k];
1525 const LO* neighCs = myCpts(curNeigh);
1526
1527 // Check if subset of this neighbor's CPOINT dependencies include all
1528 // the CPOINT dependencies of curF
1529 int sum = 0;
1530 for (int j = 0; j < numCpts[curNeigh]; j++)
1531 if (inNearbyCs[neighCs[j]] == 'y')
1532 sum++;
1533
1534 if (sum == nnzPerRow[curF]) {
1535 lookedAt[curNeigh] = 'y';
1536
1537 // Make sure we have enough space
1538 if (Teuchos::as<int>(sameCGroup.size()) <= numSameGrp)
1539 sameCGroup.resize(2 * numSameGrp);
1540
1541 sameCGroup[numSameGrp++] = curNeigh;
1542 flag = 1;
1543 }
1544
1545 // Add neighbors of curNeigh that haven't already been
1546 // add to the neighbor list while processing curF
1547 for (size_t j = ia[curNeigh]; j < ia[curNeigh + 1]; j++)
1548 if (inNeighs[ja[j]] == 'n') {
1549 neighs[numNeigh++] = ja[j];
1550 inNeighs[ja[j]] = 'y';
1551 }
1552 }
1553
1554 nextLayerStart = nextLayerEnd + 1;
1555 nextLayerEnd = numNeigh - 1;
1556 }
1557
1558 // Reset status arrays
1559 for (int j = 0; j < numNeigh; j++)
1560 inNeighs[neighs[j]] = 'n';
1561 for (int j = 0; j < numCpts[curF]; j++)
1562 inNearbyCs[curFCs[j]] = 'n';
1563
1564 // At this point we have now constructed a group of possible mid points
1565 // all with the same Cpt dependencies. Now, we need to find the one in
1566 // this group which is closest to the target midpoint coordinates.
1567 double smallest = 1e30;
1568 int smallestIndex = -1;
1569 for (int j = 0; j < numSameGrp; j++) {
1570 // MATCH_MATLAB
1571 double dist = 1e-8 * (sameCGroup[j] + 1);
1572
1573 for (int k = 0; k < NDim; k++) {
1574 double dtemp = coords1D[k][sameCGroup[j]] - targetMidCoords1D[k][curF];
1575 dist += dtemp * dtemp;
1576 }
1577 if (dist < smallest) {
1578 smallest = dist;
1579 smallestIndex = sameCGroup[j];
1580 }
1581 }
1582
1583 // So now smallestIndex is the best midpoint candidate within sameCGroup.
1584 // We now need to check if smallestIndex is really close to an already
1585 // existing mid-point. In fact, we could have multiple copies of
1586 // mid-points or some very close mid-points. To see this, consider
1587 //
1588 // P1 P2
1589 //
1590 //
1591 //
1592 // P3 P4
1593 // where P1/P4's midpoint is the same as P2/P3's midpoint. We get rid of
1594 // these by checking if a new potential midpoint is close to any previous
1595 // midpoints.
1596
1597 // Check if anybody in sameCGroup is already a midpoint. If so, check
1598 // each one of these midpoints to see if any of these is real close to
1599 // the curF.
1600 flag = 0;
1601 for (int j = 0; j < numSameGrp; j++)
1602 if (isMidPoint[sameCGroup[j]] == 'y')
1603 flag = 1;
1604
1605 if (flag == 1) {
1606 // Get an idea of the spacing between curFCs
1607 double delta = 0.0;
1608 for (int k = 0; k < NDim; k++) {
1609 double dmin = coords1D[k][curFCs[0]];
1610 double dmax = dmin;
1611
1612 for (int j = 1; j < numCpts[curF]; j++) {
1613 SC c = coords1D[k][curFCs[j]];
1614 if (c < dmin) dmin = c;
1615 if (c > dmax) dmax = c;
1616 }
1617 delta += ((dmax - dmin) * (dmax - dmin));
1618 }
1619
1620 // Now find the closest point among all sameCGroup midPoints to
1621 // smallestIndex. If this point is not too close, we make smallestIndex
1622 // a new mid-point.
1623 double close = 1000000.;
1624 for (int j = 0; j < numSameGrp; j++) {
1625 int t = sameCGroup[j];
1626 if (isMidPoint[t] == 'y') {
1627 double current = distance2(coords1D, smallestIndex, t);
1628 if (current < close)
1629 close = current;
1630 }
1631 }
1632
1633 if (close / delta > tau_2) {
1634 isMidPoint[smallestIndex] = 'y';
1635 numMidPoints++;
1636 }
1637
1638 } else {
1639 isMidPoint[smallestIndex] = 'y';
1640 numMidPoints++;
1641 }
1642 }
1643
1644 // This loop also sorts mid points
1645 int count = 0;
1646 for (size_t i = 0; i < numRows; i++)
1647 if (isMidPoint[i] == 'y') {
1648 Cptlist.push_back(i);
1649 count++;
1650 }
1651
1652 TEUCHOS_TEST_FOR_EXCEPTION(count != numMidPoints, Exceptions::RuntimeError,
1653 "Wrong with the number of mid points: " << count << " vs. " << numMidPoints);
1654 }
1655
1656 // Convert information in Cptlist, myCpts into a sparsity pattern matrix
1657 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1658 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1659 CptDepends2Pattern(const Matrix& A, const MyCptList& myCpts, RCP<Matrix>& P, LocalOrdinal offset) const {
1660 RCP<const Map> rowMap = A.getRowMap();
1661 size_t numRows = myCpts.getLocalNumRows();
1662
1663 // FIXME: how does offset play here?
1664 const Array<LO>& Cptlist = myCpts.getCList();
1665 RCP<const Map> coarseMap = MapFactory::Build(rowMap->lib(), Cptlist.size(), rowMap->getIndexBase() + offset, rowMap->getComm());
1666
1667 P = rcp(new CrsMatrixWrap(rowMap, coarseMap, 0));
1668 RCP<CrsMatrix> Pcrs = toCrsMatrix(P);
1669
1670 ArrayRCP<size_t> iaP;
1671 ArrayRCP<LO> jaP;
1672 ArrayRCP<SC> valP;
1673
1674 const std::vector<short>& numCpts = myCpts.getNumCpts();
1675 size_t nnzEstimate = std::accumulate(numCpts.begin(), numCpts.end(), 0);
1676
1677 Pcrs->allocateAllValues(nnzEstimate, iaP, jaP, valP);
1678
1679 ArrayView<size_t> ia = iaP();
1680 ArrayView<LO> ja = jaP();
1681 ArrayView<SC> val = valP();
1682
1683 std::vector<GO> coarseCmap(numRows, -1);
1684 for (int i = 0; i < Cptlist.size(); i++)
1685 coarseCmap[Cptlist[i]] = i;
1686
1687 SC one = Teuchos::ScalarTraits<SC>::one();
1688
1689 // Build up the prolongator sparsity pattern and the initial
1690 // guess used for Emin (which must have row sums equal to one)
1691
1692 ia[0] = 0;
1693 size_t nnzCount = 0;
1694 for (size_t i = 0; i < numRows; i++) {
1695 const LO* cpts = myCpts(i);
1696
1697 for (int j = 0; j < numCpts[i]; j++) {
1698 ja[nnzCount] = coarseCmap[cpts[j]];
1699 TEUCHOS_TEST_FOR_EXCEPTION(ja[nnzCount] == -1, Exceptions::RuntimeError,
1700 "Point " << cpts[j] << " is not in the global list of cpoints, but it is in the list for " << i);
1701 val[nnzCount] = one / ((SC)numCpts[i]);
1702 nnzCount++;
1703 }
1704 // NOTE: we could theoretically sort here
1705 // Do we need to, though?
1706 ia[i + 1] = nnzCount;
1707 }
1708
1709 if (rowMap->lib() == Xpetra::UseTpetra) {
1710 // - Cannot resize for Epetra, as it checks for same pointers
1711 // - Need to resize for Tpetra, as it check ().size() == ia[numRows]
1712 // NOTE: these invalidate ja and val views
1713 jaP.resize(nnzCount);
1714 valP.resize(nnzCount);
1715 }
1716
1717 Pcrs->setAllValues(iaP, jaP, valP);
1718 Pcrs->expertStaticFillComplete(coarseMap, A.getDomainMap());
1719 }
1720
1721 // Compute all points which are within a distance 1-4 from StartPt
1722 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1723 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1724 CompDistances(const Matrix& A, LO start, int numDist, std::vector<LO>& dist1, std::vector<LO>& dist2, std::vector<LO>& dist3, std::vector<LO>& dist4) const {
1725 TEUCHOS_TEST_FOR_EXCEPTION(numDist < 1 || numDist > 4, Exceptions::InvalidArgument, "CompDistances() cannot compute " << numDist << " distances");
1726
1727 size_t numRows = A.getGlobalNumRows();
1728
1729 ArrayRCP<const size_t> ia;
1730 ArrayRCP<const LO> ja;
1731 CreateCrsPointers(A, ia, ja);
1732
1733 std::vector<char> added(numRows, 'n');
1734 std::vector<LO> neighs;
1735 neighs.reserve(100);
1736
1737 neighs.push_back(start);
1738 added[start] = 'y';
1739
1740 for (int k = 1; k <= numDist; k++) {
1741 int numNeighs = neighs.size();
1742 for (int i = 0; i < numNeighs; i++)
1743 for (size_t j = ia[neighs[i]]; j < ia[neighs[i] + 1]; j++)
1744 if (added[ja[j]] == 'n') {
1745 added[ja[j]] = 'y';
1746 neighs.push_back(ja[j]);
1747 }
1748
1749 if (k == 1) dist1 = neighs;
1750 if (k == 2) dist2 = neighs;
1751 if (k == 3) dist3 = neighs;
1752 if (k == 4) dist4 = neighs;
1753 }
1754 }
1755
1756 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1757 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1758 CreateCrsPointers(const Matrix& A, ArrayRCP<const size_t>& ia, ArrayRCP<const LO>& ja) const {
1759 RCP<const CrsMatrixWrap> Awrap = rcp_dynamic_cast<const CrsMatrixWrap>(rcpFromRef(A));
1760 TEUCHOS_TEST_FOR_EXCEPTION(Awrap.is_null(), Exceptions::RuntimeError, "A is not of CrsMatrixWrap type");
1761
1762 ArrayRCP<const SC> val;
1763 Awrap->getCrsMatrix()->getAllValues(ia, ja, val);
1764 }
1765
1766 const std::string OUTPUT_DIR = "status/";
1767
1768 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1769 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1770 DumpStatus(const std::string& filename, const std::vector<char>& status, int NDim, bool isAmalgamated) const {
1771 const std::string dirName = OUTPUT_DIR;
1772
1773 struct stat sb;
1774 TEUCHOS_TEST_FOR_EXCEPTION(stat(dirName.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode), Exceptions::RuntimeError,
1775 "Please create a \"" << dirName << "\" directory");
1776
1777 std::ofstream ofs((dirName + filename).c_str());
1778 size_t step = (isAmalgamated ? 1 : NDim);
1779 for (size_t i = 0; i < status.size(); i += step)
1780 ofs << status[i] << std::endl;
1781 }
1782
1783 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1784 void Q2Q1uPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1785 DumpCoords(const std::string& filename, const MultiVector& coords) const {
1786 const std::string dirName = OUTPUT_DIR;
1787
1788 struct stat sb;
1789 if (stat(dirName.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode))
1790 GetOStream(Errors) << "Please create a \"" << dirName << "\" directory" << std::endl;
1791
1792 const int NDim = coords.getNumVectors();
1793 const int n = coords.getLocalLength();
1794
1795 ArrayRCP<ArrayRCP<const SC> > coords1D(NDim);
1796 for (int k = 0; k < NDim; k++)
1797 coords1D[k] = coords.getData(k);
1798
1799 std::ofstream ofs((dirName + filename).c_str());
1800 for (int i = 0; i < n; i++) {
1801 for (int k = 0; k < NDim; k++)
1802 ofs << " " << coords1D[k][i];
1803 ofs << std::endl;
1804 }
1805 }
1806
1807 } // namespace MueLu
1808
1809 #endif // MUELU_Q2Q1UPFACTORY_DECL_HPP