Debian Codegraph @

packages/muelu/research/max/XpetraSplitting/Xpetra_MatrixSplitting.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 /*
11 * Xpetra_MatrixSplitting.hpp
12 *
13 * Created on: August 17, 2017
14 * Author: Massimiliano Lupo Pasini ([email protected])
15 */
16 #ifndef XPETRA_MATRIXSPLITTING_HPP
17 #define XPETRA_MATRIXSPLITTING_HPP
18
19 // Xpetra
20 #include "Xpetra_Map.hpp"
21 #include "Xpetra_MapFactory.hpp"
22 #include "Xpetra_Matrix.hpp"
23 #include <Xpetra_CrsMatrixWrap_fwd.hpp>
24 #include "Xpetra_IO.hpp"
25 #include "Xpetra_RegionHandler_def.hpp"
26
27 // Ifpack2
28 #include "Ifpack2_OverlappingRowMatrix_def.hpp"
29 #include "Xpetra_TpetraMultiVector_decl.hpp"
30
31 // MueLu
32 #include <MueLu_Utilities.hpp>
33
34 /** \file Xpetra_MatrixSplitting.hpp
35
36 Declarations for the class Xpetra::MatrixSplitting.
37 */
38 namespace Xpetra {
39
40 // Definition of the predicate for the regionToAll structure.
41 // Given a tuple made of node index and a specific region it belongs to,
42 // this predicate returns true if the node has composite index which coincides with the index specified in input.
43 template <class GlobalOrdinal>
44 class checkerRegionToAll {
45 public:
46 // Constructor
47 checkerRegionToAll(GlobalOrdinal node_index) { node_index_ = node_index; };
48
49 // Unary Operator
50 bool operator()(const std::tuple<GlobalOrdinal, GlobalOrdinal> &node) { return (std::get<0>(node) == node_index_); }
51
52 private:
53 GlobalOrdinal node_index_;
54 };
55
56 // Definition of the predicate for the node_ structure.
57 // Given a tuple made of node index and a specific region it belongs to,
58 // this predicate returns true if the node has composite index which coincides with the index specified in input.
59 // It does the same thing as checkerRegionToAll but it works on a different data structure
60 template <class GlobalOrdinal>
61 class checkerAllToRegion {
62 public:
63 // Constructor
64 checkerAllToRegion(GlobalOrdinal node_index) { node_index_ = node_index; };
65
66 // Unary Operator
67 bool operator()(const std::tuple<GlobalOrdinal, GlobalOrdinal> &node) { return (std::get<1>(node) == node_index_); }
68
69 private:
70 GlobalOrdinal node_index_;
71 };
72
73 // Definition of the predicate for the node_ structure.
74 // Given a tuple made of node index and a specific region it belongs to,
75 // this predicate returns true if the node has composite index which coincides with the index specified in input.
76 // This checker is specifically used only for nodes lying on the interface
77 template <class GlobalOrdinal>
78 class checkerInterfaceNodes {
79 public:
80 // Constructor
81 checkerInterfaceNodes(GlobalOrdinal node_index) { node_index_ = node_index; };
82
83 // Unary Operator
84 bool operator()(const std::tuple<int, Array<GlobalOrdinal> > &node) { return (std::get<0>(node) == node_index_); }
85
86 private:
87 GlobalOrdinal node_index_;
88 };
89
90 /*!
91 @class Xpetra::MatrixSplitting class.
92 @brief Xpetra-specific matrix class.
93
94 This class is specific to Xpetra and has no analogue in Epetra or Tpetra. The main motivation for this class is to be able to access matrix data in a manner different than how it is stored.
95 For example, it might be more convenient to treat ("view") a matrix stored in compressed row storage as if it were a block matrix. The Xpetra::MatrixSplitting class is intended to manage these "views".
96
97 <B>How to create a Matrix from an existing CrsMatrix</B>
98
99 */
100
101 typedef std::string viewLabel_t;
102
103 template <class Scalar = Operator<>::scalar_type,
104 class LocalOrdinal = Operator<>::local_ordinal_type,
105 class GlobalOrdinal = typename Operator<LocalOrdinal>::global_ordinal_type,
106 class Node = typename Operator<LocalOrdinal, GlobalOrdinal>::node_type,
107 UnderlyingLib lib = Xpetra::UseEpetra,
108 bool collapse = false>
109 class MatrixSplitting : public Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
110 typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map;
111 typedef Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> Matrix;
112 typedef Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node> CrsGraph;
113 typedef Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrix;
114 typedef Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixWrap;
115 typedef Xpetra::CrsMatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixFactory;
116 typedef Xpetra::MatrixView<Scalar, LocalOrdinal, GlobalOrdinal, Node> MatrixView;
117
118 // Xpetra structures must be converted into Tpetra specialized ones to construct an Ifpack2::OverlappingRowMatrix object
119 // Once the Ifpack2::OverlappingRowMatrix class is transferred into the Xpetra directory, the following 6 lines can be changed/removed
120 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> tpetra_crs_matrix;
121 typedef Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> tpetra_row_matrix;
122
123 public:
124 //! @name Constructor/Destructor Methods
125 //@{
126
127 //! Constructor specifying fixed number of entries for each row.
128 MatrixSplitting(RCP<Matrix> matrix, RCP<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > nodes) {
129 std::cout << "This version of MatrixSplitting constructor is NOT currently supported \n";
130 }
131 //
132 //
133 MatrixSplitting(const char *matrix_file_name,
134 Teuchos::RCP<Xpetra::RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionHandler,
135 RCP<const Teuchos::Comm<int> > comm) {
136 comm_ = comm;
137 regionHandler_ = regionHandler;
138 Array<GlobalOrdinal> elementlist = regionHandler_->GetGlobalRowMap();
139 num_total_elements_ = regionHandler_->GetNumGlobalElements();
140 num_total_regions_ = regionHandler_->GetNumTotalRegions();
141
142 if (comm_->getRank() == 0)
143 std::cout << "MatrixSplitting constructor initialized" << std::endl;
144 region_matrix_initialized_.clear();
145 for (int i = 0; i < num_total_regions_; ++i)
146 region_matrix_initialized_.push_back(false);
147
148 // Create Xpetra map for composite stiffness matrix
149 if (comm_->getRank() == 0)
150 std::cout << "Starting construction of Composite Map" << std::endl;
151 RCP<const Xpetra::Map<int, GlobalOrdinal, Node> > xpetraMap;
152 xpetraMap = Xpetra::MapFactory<int, GlobalOrdinal, Node>::Build(lib, num_total_elements_, elementlist, 0, comm);
153 if (comm_->getRank() == 0)
154 std::cout << "Finished construction of Composite Map" << std::endl;
155
156 if (comm_->getRank() == 0)
157 std::cout << "Started reading composite matrix" << std::endl;
158 // Import matrix from an .mm file into an Xpetra wrapper for an Epetra matrix
159 compositeMatrixData_ = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Read(matrix_file_name, xpetraMap);
160 if (comm_->getRank() == 0)
161 std::cout << "Finished reading composite matrix" << std::endl;
162
163 CreateRegionMatrices(regionHandler_->GetRegionRowMaps());
164 }
165
166 //! Destructor
167 virtual ~MatrixSplitting() {}
168 //@}
169
170 //! @name Insertion/Removal Methods
171 //@{
172
173 //! Insert matrix entries, using global IDs.
174 /** All index values must be in the global space.
175 \pre \c globalRow exists as an ID in the global row map
176 \pre <tt>isLocallyIndexed() == false</tt>
177 \pre <tt>isStorageOptimized() == false</tt>
178
179 \post <tt>isGloballyIndexed() == true</tt>
180
181 \note If \c globalRow does not belong to the matrix on this node, then it will be communicated to the appropriate node when globalAssemble() is called (which will, at the latest, occur during the next call to fillComplete().) Otherwise, the entries will be inserted in the local matrix.
182 \note If the matrix row already contains values at the indices corresponding to values in \c cols, then the new values will be summed with the old values; this may happen at insertion or during the next call to fillComplete().
183 \note If <tt>hasColMap() == true</tt>, only (cols[i],vals[i]) where cols[i] belongs to the column map on this node will be inserted into the matrix.
184 */
185 void insertGlobalValues(GlobalOrdinal globalRow, const ArrayView<const GlobalOrdinal> &cols, const ArrayView<const Scalar> &vals) {
186 compositeMatrixData_->insertGlobalValues(globalRow, cols, vals);
187 }
188
189 //! Insert matrix entries, using local IDs.
190 /** All index values must be in the local space.
191 \pre \c localRow exists as an ID in the globalrow map
192 \pre <tt>isGloballyIndexed() == false</tt>
193 \pre <tt>isStorageOptimized() == false</tt>
194
195 \post <tt>isLocallyIndexed() == true</tt>
196 */
197 void insertLocalValues(LocalOrdinal localRow, const ArrayView<const LocalOrdinal> &cols, const ArrayView<const Scalar> &vals) {
198 compositeMatrixData_->insertLocalValues(localRow, cols, vals);
199 }
200
201 //! \brief Replace matrix entries, using global IDs.
202 /** All index values must be in the global space.
203
204 \pre \c globalRow is a global row belonging to the matrix on this node.
205
206 \note If (globalRow,cols[i]) corresponds to an entry that is duplicated in this matrix row (likely because it was inserted more than once and fillComplete() has not been called in the interim), the behavior of this function is not defined. */
207 void replaceGlobalValues(GlobalOrdinal globalRow,
208 const ArrayView<const GlobalOrdinal> &cols,
209 const ArrayView<const Scalar> &vals) { compositeMatrixData_->replaceGlobalValues(globalRow, cols, vals); }
210
211 //! Replace matrix entries, using local IDs.
212 /** All index values must be in the local space.
213 Note that if a value is not already present for the specified location in the matrix, the input value will be ignored silently.
214 */
215 void replaceLocalValues(LocalOrdinal localRow,
216 const ArrayView<const LocalOrdinal> &cols,
217 const ArrayView<const Scalar> &vals) { compositeMatrixData_->replaceLocalValues(localRow, cols, vals); }
218
219 //! Set all matrix entries equal to scalar
220 virtual void setAllToScalar(const Scalar &alpha) { compositeMatrixData_->setAllToScalar(alpha); }
221
222 //! Scale the current values of a matrix, this = alpha*this.
223 void scale(const Scalar &alpha) {
224 compositeMatrixData_->scale(alpha);
225 }
226
227 //@}
228
229 //! @name Transformational Methods
230 //@{
231
232 /*! Resume fill operations.
233 After calling fillComplete(), resumeFill() must be called before initiating any changes to the matrix.
234
235 resumeFill() may be called repeatedly.
236
237 \post <tt>isFillActive() == true<tt>
238 \post <tt>isFillComplete() == false<tt>
239 */
240 void resumeFill(const RCP<ParameterList> &params = null) {
241 compositeMatrixData_->resumeFill(params);
242 }
243
244 /*! \brief Signal that data entry is complete, specifying domain and range maps.
245
246 Off-node indices are distributed (via globalAssemble()), indices are sorted, redundant indices are eliminated, and global indices are transformed to local indices.
247
248 \pre <tt>isFillActive() == true<tt>
249 \pre <tt>isFillComplete()() == false<tt>
250
251 \post <tt>isFillActive() == false<tt>
252 \post <tt>isFillComplete() == true<tt>
253 \post if <tt>os == DoOptimizeStorage<tt>, then <tt>isStorageOptimized() == true</tt>
254 */
255 void fillComplete(const RCP<const Map> &domainMap, const RCP<const Map> &rangeMap, const RCP<Teuchos::ParameterList> &params = null) {
256 compositeMatrixData_->fillComplete(domainMap, rangeMap, params);
257
258 // Update default view with the colMap because colMap can be <tt>null</tt> until fillComplete() is called.
259 updateDefaultView();
260 }
261
262 /*! \brief Signal that data entry is complete.
263
264 Off-node entries are distributed (via globalAssemble()), repeated entries are summed, and global indices are transformed to local indices.
265
266 \note This method calls fillComplete( getRowMap(), getRowMap(), os ).
267
268 \pre <tt>isFillActive() == true<tt>
269 \pre <tt>isFillComplete()() == false<tt>
270
271 \post <tt>isFillActive() == false<tt>
272 \post <tt>isFillComplete() == true<tt>
273 \post if <tt>os == DoOptimizeStorage<tt>, then <tt>isStorageOptimized() == true</tt>
274 */
275 // TODO : Get ride of "Tpetra"::OptimizeOption
276 void fillComplete(const RCP<ParameterList> &params = null) {
277 compositeMatrixData_->fillComplete(params);
278
279 // Update default view with the colMap because colMap can be <tt>null</tt> until fillComplete() is called.
280 updateDefaultView();
281 }
282
283 //@}
284
285 //! Returns the number of global rows in this matrix.
286 /** Undefined if isFillActive().
287 */
288 global_size_t getGlobalNumRows() const {
289 return compositeMatrixData_->getGlobalNumRows();
290 }
291
292 //! \brief Returns the number of global columns in the matrix.
293 /** Undefined if isFillActive().
294 */
295 global_size_t getGlobalNumCols() const {
296 return compositeMatrixData_->getGlobalNumCols();
297 }
298
299 //! Returns the number of matrix rows owned on the calling node.
300 size_t getLocalNumRows() const {
301 return compositeMatrixData_->getLocalNumRows();
302 }
303
304 //! Returns the global number of entries in this matrix.
305 global_size_t getGlobalNumEntries() const {
306 return compositeMatrixData_->getGlobalNumEntries();
307 }
308
309 //! Returns the local number of entries in this matrix.
310 size_t getLocalNumEntries() const {
311 return compositeMatrixData_->getLocalNumEntries();
312 }
313
314 //! Returns the current number of entries on this node in the specified local row.
315 /*! Returns OrdinalTraits<size_t>::invalid() if the specified local row is not valid for this matrix. */
316 size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const {
317 return compositeMatrixData_->getNumEntriesInLocalRow(localRow);
318 }
319
320 //! \brief Returns the maximum number of entries across all rows/columns on all nodes.
321 /** Undefined if isFillActive().
322 */
323 size_t getGlobalMaxNumRowEntries() const {
324 return compositeMatrixData_->getGlobalMaxNumRowEntries();
325 }
326
327 //! \brief Returns the maximum number of entries across all rows/columns on this node.
328 /** Undefined if isFillActive().
329 */
330 size_t getLocalMaxNumRowEntries() const {
331 return compositeMatrixData_->getLocalMaxNumRowEntries();
332 }
333
334 //! \brief Returns the number of regions in the composite domain.
335 global_size_t getNumRegions() const {
336 TEUCHOS_TEST_FOR_EXCEPTION(region_matrix_initialized_.size() == 0, Exceptions::RuntimeError, "Regions have not been initialized yet \n");
337 for (int i = 0; i < region_matrix_initialized_.size(); ++i)
338 TEUCHOS_TEST_FOR_EXCEPTION(!region_matrix_initialized_[i], Exceptions::RuntimeError, "Region matrix for region " << i + 1 << "has not been initialized yet \n");
339
340 return num_total_regions_;
341 }
342
343 //! \brief If matrix indices are in the local range, this function returns true. Otherwise, this function returns false. */
344 bool isLocallyIndexed() const {
345 return compositeMatrixData_->isLocallyIndexed();
346 }
347
348 //! \brief If matrix indices are in the global range, this function returns true. Otherwise, this function returns false. */
349 bool isGloballyIndexed() const {
350 return compositeMatrixData_->isGloballyIndexed();
351 }
352
353 //! Returns \c true if fillComplete() has been called and the matrix is in compute mode.
354 bool isFillComplete() const {
355 return compositeMatrixData_->isFillComplete();
356 }
357
358 //! Extract a list of entries in a specified local row of the matrix. Put into storage allocated by calling routine.
359 /*!
360 \param LocalRow - (In) Local row number for which indices are desired.
361 \param Indices - (Out) Local column indices corresponding to values.
362 \param Values - (Out) Matrix values.
363 \param NumIndices - (Out) Number of indices.
364
365 Note: A std::runtime_error exception is thrown if either \c Indices or \c Values is not large enough to hold the data associated
366 with row \c LocalRow. If \c LocalRow is not valid for this node, then \c Indices and \c Values are unchanged and \c NumIndices is
367 returned as OrdinalTraits<size_t>::invalid().
368
369 \pre <tt>isLocallyIndexed()==true</tt> or <tt>hasColMap() == true</tt>
370 */
371 void getLocalRowCopy(LocalOrdinal LocalRow,
372 const ArrayView<LocalOrdinal> &Indices,
373 const ArrayView<Scalar> &Values,
374 size_t &NumEntries) const {
375 compositeMatrixData_->getLocalRowCopy(LocalRow, Indices, Values, NumEntries);
376 }
377
378 //! Extract a const, non-persisting view of global indices in a specified row of the matrix.
379 /*!
380 \param GlobalRow - (In) Global row number for which indices are desired.
381 \param Indices - (Out) Global column indices corresponding to values.
382 \param Values - (Out) Row values
383 \pre <tt>isLocallyIndexed() == false</tt>
384 \post <tt>indices.size() == getNumEntriesInGlobalRow(GlobalRow)</tt>
385
386 Note: If \c GlobalRow does not belong to this node, then \c indices is set to null.
387 */
388 void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView<const GlobalOrdinal> &indices, ArrayView<const Scalar> &values) const {
389 compositeMatrixData_->getGlobalRowView(GlobalRow, indices, values);
390 }
391
392 //! Extract a const, non-persisting view of local indices in a specified row of the matrix.
393 /*!
394 \param LocalRow - (In) Local row number for which indices are desired.
395 \param Indices - (Out) Global column indices corresponding to values.
396 \param Values - (Out) Row values
397 \pre <tt>isGloballyIndexed() == false</tt>
398 \post <tt>indices.size() == getNumEntriesInLocalRow(LocalRow)</tt>
399
400 Note: If \c LocalRow does not belong to this node, then \c indices is set to null.
401 */
402 void getLocalRowView(LocalOrdinal LocalRow, ArrayView<const LocalOrdinal> &indices, ArrayView<const Scalar> &values) const {
403 compositeMatrixData_->getLocalRowView(LocalRow, indices, values);
404 }
405
406 //! \brief Get a copy of the diagonal entries owned by this node, with local row idices.
407 /*! Returns a distributed Vector object partitioned according to this matrix's row map, containing the
408 the zero and non-zero diagonals owned by this node. */
409 void getLocalDiagCopy(Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &diag) const {
410 compositeMatrixData_->getLocalDiagCopy(diag);
411 }
412
413 //! Get offsets of the diagonal entries in the matrix.
414 void getLocalDiagOffsets(ArrayRCP<size_t> &offsets) const {
415 compositeMatrixData_->getLocalDiagOffsets(offsets);
416 }
417
418 //! Get a copy of the diagonal entries owned by this node, with local row indices, using row offsets.
419 void getLocalDiagCopy(Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &diag, const ArrayView<const size_t> &offsets) const {
420 compositeMatrixData_->getLocalDiagCopy(diag, offsets);
421 }
422
423 //! Get Frobenius norm of the matrix
424 typename ScalarTraits<Scalar>::magnitudeType getFrobeniusNorm() const {
425 return compositeMatrixData_->getFrobeniusNorm();
426 }
427
428 //! Left scale matrix using the given vector entries
429 void leftScale(const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &x) {
430 compositeMatrixData_->leftScale(x);
431 }
432
433 //! Neighbor2 scale matrix using the given vector entries
434 void rightScale(const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &x) {
435 compositeMatrixData_->rightScale(x);
436 }
437
438 //! Returns true if compositeConstants have been computed; false otherwise
439 bool haveGlobalConstants() const {
440 return compositeMatrixData_->haveGlobalConstants();
441 }
442
443 //@}
444
445 //! @name Methods implementing Matrix
446 //@{
447
448 //! \brief Computes the sparse matrix-multivector multiplication.
449 /*! Performs \f$Y = \alpha A^{\textrm{mode}} X + \beta Y\f$, with one special exceptions:
450 - if <tt>beta == 0</tt>, apply() overwrites \c Y, so that any values in \c Y (including NaNs) are ignored.
451 */
452 virtual void apply(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &X,
453 Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &Y,
454 Teuchos::ETransp mode = Teuchos::NO_TRANS,
455 Scalar alpha = ScalarTraits<Scalar>::one(),
456 Scalar beta = ScalarTraits<Scalar>::zero()) const {
457 compositeMatrixData_->apply(X, Y, mode, alpha, beta);
458 }
459
460 //! \brief Returns the Map associated with the domain of this operator.
461 //! This will be <tt>null</tt> until fillComplete() is called.
462 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
463 return compositeMatrixData_->getDomainMap();
464 }
465
466 //! Returns the Map associated with the domain of this operator.
467 //! This will be <tt>null</tt> until fillComplete() is called.
468 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
469 return compositeMatrixData_->getRangeMap();
470 }
471
472 //! \brief Returns the Map that describes the column distribution in this matrix.
473 //! This might be <tt>null</tt> until fillComplete() is called.
474 const RCP<const Map> &getColMap() const { return getColMap(Matrix::GetCurrentViewLabel()); }
475
476 //! \brief Returns the Map that describes the column distribution in this matrix.
477 const RCP<const Map> &getColMap(viewLabel_t viewLabel) const {
478 TEUCHOS_TEST_FOR_EXCEPTION(Matrix::operatorViewTable_.containsKey(viewLabel) == false, Xpetra::Exceptions::RuntimeError, "Xpetra::Matrix.GetColMap(): view '" + viewLabel + "' does not exist.");
479 updateDefaultView(); // If CrsMatrix::fillComplete() have been used instead of MatrixSplitting::fillComplete(), the default view is updated.
480 return Matrix::operatorViewTable_.get(viewLabel)->GetColMap();
481 }
482
483 void removeEmptyProcessesInPlace(const RCP<const Map> &newMap) {
484 compositeMatrixData_->removeEmptyProcessesInPlace(newMap);
485 this->operatorViewTable_.get(this->GetCurrentViewLabel())->SetRowMap(compositeMatrixData_->getRowMap());
486 this->operatorViewTable_.get(this->GetCurrentViewLabel())->SetColMap(compositeMatrixData_->getColMap());
487 }
488
489 //@}
490
491 //! Implements DistObject interface
492 //{@
493
494 //! Access function for the Tpetra::Map this DistObject was constructed with.
495 const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getMap() const {
496 return compositeMatrixData_->getMap();
497 }
498
499 //! Import.
500 void doImport(const Matrix &source,
501 const Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> &importer, CombineMode CM) {
502 std::cout << "Import not implemented" << std::endl;
503 // const MatrixSplitting & sourceWrp = dynamic_cast<const MatrixSplitting &>(source);
504 // compositeMatrixData_->doImport(*sourceWrp.getCrsMatrix(), importer, CM);
505 }
506
507 //! Export.
508 void doExport(const Matrix &dest,
509 const Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> &importer, CombineMode CM) {
510 std::cout << "Export not implemented" << std::endl;
511 // const MatrixSplitting & destWrp = dynamic_cast<const MatrixSplitting &>(dest);
512 // compositeMatrixData_->doExport(*destWrp.getCrsMatrix(), importer, CM);
513 }
514
515 //! Import (using an Exporter).
516 void doImport(const Matrix &source,
517 const Xpetra::Export<LocalOrdinal, GlobalOrdinal, Node> &exporter, CombineMode CM) {
518 std::cout << "Import not implemented" << std::endl;
519 // const MatrixSplitting & sourceWrp = dynamic_cast<const MatrixSplitting &>(source);
520 // compositeMatrixData_->doImport(*sourceWrp.getCrsMatrix(), exporter, CM);
521 }
522
523 //! Export (using an Importer).
524 void doExport(const Matrix &dest,
525 const Xpetra::Export<LocalOrdinal, GlobalOrdinal, Node> &exporter, CombineMode CM) {
526 std::cout << "Export not implemented" << std::endl;
527 // const MatrixSplitting & destWrp = dynamic_cast<const MatrixSplitting &>(dest);
528 // compositeMatrixData_->doExport(*destWrp.getCrsMatrix(), exporter, CM);
529 }
530
531 // @}
532
533 //! @name Overridden from Teuchos::Describable
534 //@{
535
536 /** \brief Return a simple one-line description of this object. */
537 std::string description() const {
538 return "Xpetra::MatrixSplitting";
539 }
540
541 /** \brief Print the object with some verbosity level to an FancyOStream object. */
542 void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const {
543 compositeMatrixData_->describe(out, verbLevel);
544 }
545 //@}
546
547 //! Get methods
548 //@{
549
550 //! Returns the CrsGraph associated with this matrix.
551 RCP<const CrsGraph> getCrsGraph() const { return compositeMatrixData_->getCrsGraph(); }
552
553 //! Returns an Xpetra::CrsMatrix pointer to the the composite matrix
554 RCP<CrsMatrix> getCrsMatrix() const { return compositeMatrixData_; }
555
556 //! Returns an Xpetra::Matrix pointer to the composite matrix
557 RCP<Matrix> getMatrix() const { return compositeMatrixData_; }
558
559 //! Returns an Xpetra::Matrix pointer to the region matrix associated with a specific region index
560 RCP<Matrix> getRegionMatrix(GlobalOrdinal region_idx) const {
561 // The region index is assumed to start from 0
562 TEUCHOS_TEST_FOR_EXCEPTION(num_total_regions_ <= 0, Exceptions::RuntimeError, "Regions not initialized yet ( total number of regions is <=0 ) \n");
563 TEUCHOS_TEST_FOR_EXCEPTION(region_idx >= num_total_regions_, Exceptions::RuntimeError, "Region index not valid \n");
564
565 return regionMatrixData_[region_idx];
566 }
567
568 //! Return a ppointer to the underlying regionHandler object used for the matrix splitting
569 RCP<RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node> > getRegionHandler() const {
570 return regionHandler_;
571 }
572 //@}
573
574 //! Write methods
575 //{@
576 void writeGlobalMatrix() {
577 std::string file_name;
578 file_name += "./output/A_composite.mm";
579 Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write(file_name, *compositeMatrixData_);
580 }
581
582 void writeRegionMatrices() {
583 for (int i = 0; i < num_total_regions_; ++i) {
584 std::string region_str = std::to_string(i + 1);
585 std::string file_name;
586 file_name += "./output/A_region_";
587 file_name += region_str;
588 file_name += ".mm";
589 Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write(file_name.c_str(), *regionMatrixData_[i]);
590 }
591 }
592 // @}
593
594 private:
595 // Default view is created after fillComplete()
596 // Because ColMap might not be available before fillComplete().
597 void CreateDefaultView() {
598 // Create default view
599 this->defaultViewLabel_ = "point";
600 this->CreateView(this->GetDefaultViewLabel(), compositeMatrixData_->getRowMap(), compositeMatrixData_->getColMap());
601
602 // Set current view
603 this->currentViewLabel_ = this->GetDefaultViewLabel();
604 }
605
606 // The colMap can be <tt>null</tt> until fillComplete() is called. The default view of the Matrix have to be updated when fillComplete() is called.
607 // If CrsMatrix::fillComplete() have been used instead of MatrixSplitting::fillComplete(), the default view is updated when getColMap() is called.
608 void updateDefaultView() const {
609 if ((finalDefaultView_ == false) && compositeMatrixData_->isFillComplete()) {
610 // Update default view with the colMap
611 Matrix::operatorViewTable_.get(Matrix::GetDefaultViewLabel())->SetColMap(compositeMatrixData_->getColMap());
612 finalDefaultView_ = true;
613 }
614 }
615
616 //! Private Methods
617 //{@
618
619 //! Creation of region matrices
620 //@{
621 void RegionMatrix(GlobalOrdinal region_idx) {
622 TEUCHOS_TEST_FOR_EXCEPTION(num_total_regions_ != regionMatrixData_.size(), Exceptions::RuntimeError, "Number of regions does not match with the size of regionMatrixData_ structure \n");
623 RCP<Matrix> region_matrix = regionMatrixData_[region_idx];
624
625 RCP<tpetra_crs_matrix> tpetraGlobalMatrix = toTpetra(compositeMatrixData_);
626 Ifpack2::OverlappingRowMatrix<tpetra_row_matrix> enlargedMatrix(tpetraGlobalMatrix, 2);
627
628 region_matrix->resumeFill();
629
630 // Region matrices are initially built to be a chopped version of the composite matrix
631 InitializeRegionMatrices(region_idx, region_matrix, enlargedMatrix);
632
633 // If the template paramater is set to collapse by the user, then interface entries of the region matrix are modified to collapse
634 // information coming from adjacent regions. If the collapsing is not done, then the splitting is calculated
635 if (collapse)
636 RegionCollapse(region_idx, region_matrix, enlargedMatrix);
637 else
638 RegionSplitting(region_idx, region_matrix, enlargedMatrix);
639
640 region_matrix->fillComplete();
641 };
642 // @}
643
644 //! @name Initialization of Region matrices
645 //@{
646 void InitializeRegionMatrices(GlobalOrdinal region_idx, RCP<Matrix> &region_matrix, Ifpack2::OverlappingRowMatrix<tpetra_row_matrix> &enlargedMatrix) {
647 // Region matrices are initially built to be a chopped version of the composite matrix
648 TEUCHOS_TEST_FOR_EXCEPTION(region_matrix_initialized_[region_idx], Exceptions::RuntimeError, "Surrogate region stiffness matrices are already initialized by chopping the composite stiffness matrix \n");
649
650 Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > regionToAll = regionHandler_->GetRegionToAll(region_idx);
651
652 // THIS IS THE CORE OF THE PROBLEM WHERE ONE NEEDS TO POPULATE THE REGIONAL MATRICES BY ACCESSING ENTRIES OF THE GLOBAL MATRIX
653 //
654 ArrayView<const GlobalOrdinal> MyRegionElements = region_matrix->getRowMap()->getLocalElementList();
655 for (typename ArrayView<const GlobalOrdinal>::iterator iter = MyRegionElements.begin(); iter != MyRegionElements.end(); ++iter) {
656 // Nodes are saved in data structures with 1 as base index
657 checkerRegionToAll<GlobalOrdinal> unaryPredicate(*iter + 1);
658 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator;
659 composite_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicate);
660 TEUCHOS_TEST_FOR_EXCEPTION(composite_iterator == regionToAll.end(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
661 << " node with region index: " << *iter + 1 << " is not in regionToAll[" << region_idx << "]"
662 << "\n");
663 GlobalOrdinal node_idx = std::get<1>(*composite_iterator);
664 LocalOrdinal node_local_idx = enlargedMatrix.getRowMap()->getLocalElement(node_idx - 1);
665
666 ArrayView<const GlobalOrdinal> inds;
667 ArrayView<const Scalar> vals;
668 enlargedMatrix.getLocalRowView(node_local_idx, inds, vals);
669
670 std::vector<GlobalOrdinal> region_inds_vector(0);
671 std::vector<Scalar> region_vals_vector(0);
672
673 for (LocalOrdinal i = 0; i < inds.size(); ++i) {
674 // Nodes are saved in data structures with 1 as base index
675 GlobalOrdinal composite_col_ind = enlargedMatrix.getColMap()->getGlobalElement(inds[i]) + 1;
676 checkerAllToRegion<GlobalOrdinal> unaryPredicate2(composite_col_ind);
677 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator region_iterator;
678 region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerAllToRegion<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicate2);
679 if (region_iterator != regionToAll.end()) {
680 region_inds_vector.push_back(std::get<0>(*region_iterator) - 1);
681 region_vals_vector.push_back(vals[i]);
682 }
683 }
684 ArrayView<GlobalOrdinal> region_inds(region_inds_vector);
685 ArrayView<Scalar> region_vals(region_vals_vector);
686 region_matrix->insertGlobalValues(*iter, region_inds, region_vals);
687 }
688 region_matrix_initialized_[region_idx] = true;
689 }
690 //@}
691
692 //! @name Collapse of External neighbouring nodes information on Region matrices
693 //@{
694 void RegionCollapse(GlobalOrdinal region_idx, RCP<Matrix> &region_matrix, Ifpack2::OverlappingRowMatrix<tpetra_row_matrix> &enlargedMatrix) {
695 TEUCHOS_TEST_FOR_EXCEPTION(!region_matrix_initialized_[region_idx], Exceptions::RuntimeError, "The composite stiffness matrix must be chopped into surrogate region matrices before collapsing \n");
696 TEUCHOS_TEST_FOR_EXCEPTION(regionHandler_->GetNumRegionNodes(region_idx) != regionMatrixData_[region_idx]->getGlobalNumRows(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Number of region nodes in region " << region_idx + 1 << " does not coincide with the value returned by regionMatrixData_[" << region_idx + 1 << "]->getGlobalNumRows() \n");
697
698 Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > regionToAll = regionHandler_->GetRegionToAll(region_idx);
699
700 // This portion of the code assumes that the number of region nodes is the same on each direction of the domain
701 // Foir a 2D problem we have then nx = ny = sqrt( num_region_ndoes_ )
702 GlobalOrdinal n;
703 GlobalOrdinal nx;
704 GlobalOrdinal ny;
705
706 n = regionHandler_->GetNumRegionNodes(region_idx);
707 nx = std::sqrt(n);
708 ny = nx;
709 TEUCHOS_TEST_FOR_EXCEPTION(static_cast<double>(nx - std::floor(static_cast<double>(std::sqrt(static_cast<double>(n))))) != 0.0, Exceptions::RuntimeError, "The code assumes that the regions are 2D and that the number of region nodes is the same on each direction of the domain \n");
710
711 // interfaceNodes contains nodes on an interface between any regions
712 Array<std::tuple<int, Array<GlobalOrdinal> > > interfaceNodes = regionHandler_->GetInterfaceNodes();
713
714 ArrayView<const GlobalOrdinal> MyRegionElements = region_matrix->getRowMap()->getLocalElementList();
715 for (typename ArrayView<const GlobalOrdinal>::iterator iter = MyRegionElements.begin(); iter != MyRegionElements.end(); ++iter) {
716 // Nodes are saved in data structures with 1 as base index
717 GlobalOrdinal region_node_idx = *iter + 1;
718 checkerRegionToAll<GlobalOrdinal> unaryPredicate(region_node_idx);
719 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator;
720 composite_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicate);
721 TEUCHOS_TEST_FOR_EXCEPTION(composite_iterator == regionToAll.end(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
722 << " node with region index: " << region_node_idx << " is not in regionToAll[" << region_idx << "]"
723 << "\n");
724
725 GlobalOrdinal composite_node_idx = std::get<1>(*composite_iterator);
726 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2(composite_node_idx);
727 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator;
728 interface_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2);
729
730 // Here we assuming that a specific labeling choice is adopted region wise and we use it to distinguish coarse node from fine nodes
731 bool coarse_point = false;
732 if (region_node_idx % 3 == 1)
733 coarse_point = true;
734
735 GlobalOrdinal region_node_idx_neighbor1 = 0;
736 GlobalOrdinal region_node_idx_neighbor2 = 0;
737
738 // Horizontal-Vertical Collapse
739 if (interface_iterator != interfaceNodes.end() && region_node_idx > ny && region_node_idx <= (nx - 1) * ny && !coarse_point) {
740 region_node_idx_neighbor1 = region_node_idx - ny;
741 region_node_idx_neighbor2 = region_node_idx + ny;
742 } else if (interface_iterator != interfaceNodes.end() && region_node_idx % ny > 1 && !coarse_point) {
743 region_node_idx_neighbor1 = region_node_idx - 1;
744 region_node_idx_neighbor2 = region_node_idx + 1;
745 }
746
747 if (region_node_idx_neighbor1 != 0 && region_node_idx_neighbor2 != 0) {
748 // Computation of composite index for neighbor1 node
749 checkerRegionToAll<GlobalOrdinal> unaryPredicateLeft(region_node_idx_neighbor1);
750 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor1;
751 composite_iterator_neighbor1 = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateLeft);
752
753 // Computation of composite index for neighbor2 node
754 checkerRegionToAll<GlobalOrdinal> unaryPredicateRight(region_node_idx_neighbor2);
755 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor2;
756 composite_iterator_neighbor2 = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateRight);
757
758 TEUCHOS_TEST_FOR_EXCEPTION(composite_iterator_neighbor1 == regionToAll.end() || composite_iterator_neighbor2 == regionToAll.end(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
759 << " node with region index: " << region_node_idx << " lies on the interface between regions: " << std::get<1>(*interface_iterator) << " BUT has compositely mislabeled neighbouring nodes missing from regionToAll \n");
760
761 // Check to see if neighbor1 node lies on a coarse line
762 GlobalOrdinal composite_node_idx_neighbor1 = std::get<1>(*composite_iterator_neighbor1);
763 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighbor1(composite_node_idx_neighbor1);
764 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor1;
765 interface_iterator_neighbor1 = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighbor1);
766
767 // Check to see if neighbor2 node lies on a coarse line
768 GlobalOrdinal composite_node_idx_neighbor2 = std::get<1>(*composite_iterator_neighbor2);
769 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighbor2(composite_node_idx_neighbor2);
770 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor2;
771 interface_iterator_neighbor2 = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighbor2);
772
773 // I apply the collapse only if the current node is a fine node which lies on a coarse line
774 // This means that the neighbor1 node and neighbor2 node must both lie on the coarse line as well
775 if (interface_iterator_neighbor1 != interfaceNodes.end() && interface_iterator_neighbor2 != interfaceNodes.end()) {
776 // For each fine node on a horixontal coarse line on the interface, I extract the rows from the composite matrix
777 LocalOrdinal node_idx = enlargedMatrix.getRowMap()->getLocalElement(composite_node_idx - 1);
778 LocalOrdinal node_idx_neighbor1 = enlargedMatrix.getRowMap()->getLocalElement(composite_node_idx_neighbor1 - 1);
779 LocalOrdinal node_idx_neighbor2 = enlargedMatrix.getRowMap()->getLocalElement(composite_node_idx_neighbor2 - 1);
780 ArrayView<const LocalOrdinal> inds;
781 ArrayView<const Scalar> vals;
782 enlargedMatrix.getLocalRowView(node_idx, inds, vals);
783 ArrayView<const LocalOrdinal> inds_neighbor1;
784 ArrayView<const Scalar> vals_neighbor1;
785 enlargedMatrix.getLocalRowView(node_idx_neighbor1, inds_neighbor1, vals_neighbor1);
786 ArrayView<const LocalOrdinal> inds_neighbor2;
787 ArrayView<const Scalar> vals_neighbor2;
788 enlargedMatrix.getLocalRowView(node_idx_neighbor2, inds_neighbor2, vals_neighbor2);
789
790 std::vector<LocalOrdinal> inds_vector = createVector(inds);
791 std::vector<LocalOrdinal> inds_neighbor1_vector = createVector(inds_neighbor1);
792 std::vector<LocalOrdinal> inds_neighbor2_vector = createVector(inds_neighbor2);
793
794 std::vector<GlobalOrdinal> composite_inds_vector(0);
795 std::vector<GlobalOrdinal> composite_inds_neighbor1_vector(0);
796 std::vector<GlobalOrdinal> composite_inds_neighbor2_vector(0);
797
798 for (typename std::vector<LocalOrdinal>::iterator iter_node = inds_vector.begin(); iter_node != inds_vector.end(); ++iter_node)
799 composite_inds_vector.push_back(enlargedMatrix.getRowMap()->getGlobalElement(*iter_node));
800 std::sort(composite_inds_vector.begin(), composite_inds_vector.end());
801
802 for (typename std::vector<LocalOrdinal>::iterator iter_node = inds_neighbor1_vector.begin(); iter_node != inds_neighbor1_vector.end(); ++iter_node)
803 composite_inds_neighbor1_vector.push_back(enlargedMatrix.getRowMap()->getGlobalElement(*iter_node));
804
805 std::sort(composite_inds_neighbor1_vector.begin(), composite_inds_neighbor1_vector.end());
806
807 for (typename std::vector<LocalOrdinal>::iterator iter_node = inds_neighbor2_vector.begin(); iter_node != inds_neighbor2_vector.end(); ++iter_node)
808 composite_inds_neighbor2_vector.push_back(enlargedMatrix.getRowMap()->getGlobalElement(*iter_node));
809
810 std::sort(composite_inds_neighbor2_vector.begin(), composite_inds_neighbor2_vector.end());
811
812 // IDENTIFICATION OF EXTERNAL NODES THROUGH COMPOSITE INDICES STARTS HERE
813 std::vector<GlobalOrdinal> composite_node_idx_neighbor1_extra;
814 std::vector<GlobalOrdinal> composite_node_idx_neighbor2_extra;
815 std::vector<GlobalOrdinal> composite_node_idx_extra(composite_inds_vector);
816
817 // The follolwing triple of vector is expected to EVENTUALLY contain only one entry:
818 // the label of the external node with information to collapse close to neighbor1, neighbor2 and central node
819 std::vector<GlobalOrdinal> diff_neighbor1;
820 std::vector<GlobalOrdinal> diff_neighbor2;
821 std::vector<GlobalOrdinal> diff_center;
822
823 // Identification of external node from the side of neighbor1
824 {
825 // Compute the intersection between neoghbourhood of neighbor1 node and neighbourhood of central node
826 std::set_intersection(composite_inds_vector.begin(), composite_inds_vector.end(), composite_inds_neighbor1_vector.begin(), composite_inds_neighbor1_vector.end(), std::back_inserter(composite_node_idx_neighbor1_extra));
827 for (typename std::vector<GlobalOrdinal>::iterator iter_node = composite_node_idx_neighbor1_extra.begin(); iter_node != composite_node_idx_neighbor1_extra.end(); ++iter_node) {
828 checkerAllToRegion<GlobalOrdinal> unaryPredicateExtra(*iter_node + 1);
829 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator region_iterator_extra;
830 region_iterator_extra = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerAllToRegion<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateExtra);
831
832 // Invalidation of node indices for nodes belonging to the current region
833 if (region_iterator_extra != regionToAll.end())
834 *iter_node = -1;
835 }
836
837 // Removal of invalidated indices associated with nodes belonging to current region: (external nodes do not belong to this region)
838 composite_node_idx_neighbor1_extra.erase(std::remove(composite_node_idx_neighbor1_extra.begin(), composite_node_idx_neighbor1_extra.end(), -1), composite_node_idx_neighbor1_extra.end());
839
840 // External node from neighbor1 side does not belong to the neighborhood of neighbor2
841 std::set_difference(composite_node_idx_neighbor1_extra.begin(), composite_node_idx_neighbor1_extra.end(), composite_inds_neighbor2_vector.begin(), composite_inds_neighbor2_vector.end(), std::inserter(diff_neighbor1, diff_neighbor1.begin()));
842 TEUCHOS_TEST_FOR_EXCEPTION(diff_neighbor1.size() != 1, Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
843 << "Mislabeling of nodes obstructed the identification of the extra node: region node " << region_node_idx << " leads to diff_neighbor1.size()= " << diff_neighbor1.size() << " \n");
844 }
845
846 // Identification of external node from the side of neighbor2
847 {
848 // Compute the intersection between neighbourhood of neighbor2 node and neighbourhood of central node
849 std::set_intersection(composite_inds_vector.begin(), composite_inds_vector.end(), composite_inds_neighbor2_vector.begin(), composite_inds_neighbor2_vector.end(), std::back_inserter(composite_node_idx_neighbor2_extra));
850
851 for (typename std::vector<GlobalOrdinal>::iterator iter_node = composite_node_idx_neighbor2_extra.begin(); iter_node != composite_node_idx_neighbor2_extra.end(); ++iter_node) {
852 checkerAllToRegion<GlobalOrdinal> unaryPredicateExtra(*iter_node + 1);
853 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator region_iterator_extra;
854 region_iterator_extra = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerAllToRegion<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateExtra);
855
856 // Invalidation of node indices for nodes belonging to the current region
857 if (region_iterator_extra != regionToAll.end())
858 *iter_node = -1;
859 }
860
861 // Removal of invalidated indices associated with nodes belonging to current region: (external nodes do not belong to this region)
862 composite_node_idx_neighbor2_extra.erase(std::remove(composite_node_idx_neighbor2_extra.begin(), composite_node_idx_neighbor2_extra.end(), -1), composite_node_idx_neighbor2_extra.end());
863
864 // External node from neighbor2 side does not belong to the neighborhood of neighbor1
865 std::set_difference(composite_node_idx_neighbor2_extra.begin(), composite_node_idx_neighbor2_extra.end(), composite_inds_neighbor1_vector.begin(), composite_inds_neighbor1_vector.end(), std::inserter(diff_neighbor2, diff_neighbor2.begin()));
866 TEUCHOS_TEST_FOR_EXCEPTION(diff_neighbor2.size() != 1, Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
867 << "Mislabeling of nodes obstructed the identification of the extra node: region node " << region_node_idx << " leads to diff_neighbor2.size()= " << diff_neighbor2.size() << " \n");
868 }
869
870 // Identification of external node from the side of central node
871 {
872 for (typename std::vector<GlobalOrdinal>::iterator iter_node = composite_node_idx_extra.begin(); iter_node != composite_node_idx_extra.end(); ++iter_node) {
873 checkerAllToRegion<GlobalOrdinal> unaryPredicateExtra(*iter_node + 1);
874 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator region_iterator_extra;
875 region_iterator_extra = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerAllToRegion<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateExtra);
876
877 // Invalidation of node indices for nodes belonging to the current region
878 if (region_iterator_extra != regionToAll.end())
879 *iter_node = -1;
880 }
881
882 // Removal of invalidated indices associated with nodes belonging to current region: (external nodes do not belong to this region)
883 composite_node_idx_extra.erase(std::remove(composite_node_idx_extra.begin(), composite_node_idx_extra.end(), -1), composite_node_idx_extra.end());
884 std::vector<GlobalOrdinal> diff_center_temp;
885
886 // At thie point composite_node_idx_extra contains indices of all the three external nodes: two of these must be removed since they are already tracked
887 // External nodes from neighbors1's and neighbor2's side must be removed
888 std::set_difference(composite_node_idx_extra.begin(), composite_node_idx_extra.end(), diff_neighbor1.begin(), diff_neighbor1.end(), std::inserter(diff_center_temp, diff_center_temp.begin()));
889 std::set_difference(diff_center_temp.begin(), diff_center_temp.end(), diff_neighbor2.begin(), diff_neighbor2.end(), std::inserter(diff_center, diff_center.begin()));
890 TEUCHOS_TEST_FOR_EXCEPTION(diff_center.size() != 1, Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
891 << "Mislabeling of nodes obstructed the identification of the extra node: region node " << region_node_idx << " leads to diff_center.size()= " << diff_center.size() << " \n");
892 }
893
894 // Computation of local indices for central node and its neighbors
895 LocalOrdinal local_region_node_idx = region_matrix->getRowMap()->getLocalElement(region_node_idx);
896 LocalOrdinal local_region_node_idx_neighbor1 = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor1);
897 LocalOrdinal local_region_node_idx_neighbor2 = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor2);
898
899 // Computation of local indices for external nodes
900 LocalOrdinal local_extra_central = enlargedMatrix.getRowMap()->getLocalElement(diff_center[0]);
901 LocalOrdinal local_extra_neighbor1 = enlargedMatrix.getRowMap()->getLocalElement(diff_neighbor1[0]);
902 LocalOrdinal local_extra_neighbor2 = enlargedMatrix.getRowMap()->getLocalElement(diff_neighbor2[0]);
903
904 ArrayView<const GlobalOrdinal> region_row;
905 ArrayView<const GlobalOrdinal> region_col;
906 ArrayView<const Scalar> region_val;
907
908 // Extract Row view of the region matrix before collapsing
909 if (region_matrix->isLocallyIndexed())
910 region_matrix->getLocalRowView(local_region_node_idx, region_col, region_val);
911 else
912 region_matrix->getGlobalRowView(region_node_idx, region_col, region_val);
913
914 // Extract Row of overlapped composite matrix to detect node with information to collapse
915 ArrayView<const GlobalOrdinal> external_row;
916 ArrayView<const GlobalOrdinal> external_col;
917 ArrayView<const Scalar> external_val;
918 enlargedMatrix.getLocalRowView(node_idx, external_col, external_val);
919
920 // neighbor1 collapse
921 {
922 Scalar initial_value = 0;
923 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = region_col.begin(); iter_view != region_col.end(); ++iter_view) {
924 if (region_matrix->isLocallyIndexed())
925 if (*iter_view == local_region_node_idx_neighbor1)
926 initial_value = region_val[iter_view - region_col.begin()];
927 if (region_matrix->isGloballyIndexed())
928 if (*iter_view == region_node_idx_neighbor1)
929 initial_value = region_val[iter_view - region_col.begin()];
930
931 if (initial_value != 0)
932 break;
933 }
934
935 Scalar external_value = 0;
936 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = external_col.begin(); iter_view != external_col.end(); ++iter_view) {
937 if (*iter_view == local_extra_neighbor1)
938 external_value = external_val[iter_view - external_col.begin()];
939
940 if (external_value != 0)
941 break;
942 }
943
944 Scalar new_value = external_value; // new matrix entry generated with the collapsing
945 std::vector<GlobalOrdinal> new_entry_ind;
946 std::vector<Scalar> new_entry_val;
947 new_entry_ind.push_back(region_node_idx_neighbor1 - 1);
948 new_entry_val.push_back(new_value);
949
950 // If a nonzero value is already stored in the specified position, the new values is SUMMED to the already existing one
951 // See description of insertGlobalValues(...)
952 region_matrix->insertGlobalValues(region_node_idx - 1, new_entry_ind, new_entry_val);
953 }
954 // neighbor2 collapse
955 {
956 Scalar initial_value = 0;
957 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = region_col.begin(); iter_view != region_col.end(); ++iter_view) {
958 if (region_matrix->isLocallyIndexed())
959 if (*iter_view == local_region_node_idx_neighbor2)
960 initial_value = region_val[iter_view - region_col.begin()];
961 if (region_matrix->isGloballyIndexed())
962 if (*iter_view == region_node_idx_neighbor2)
963 initial_value = region_val[iter_view - region_col.begin()];
964
965 if (initial_value != 0)
966 break;
967 }
968
969 Scalar external_value = 0;
970 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = external_col.begin(); iter_view != external_col.end(); ++iter_view) {
971 if (*iter_view == local_extra_neighbor2)
972 external_value = external_val[iter_view - external_col.begin()];
973
974 if (external_value != 0)
975 break;
976 }
977
978 Scalar new_value = external_value; // new matrix entry generated with the collapsing
979 std::vector<GlobalOrdinal> new_entry_ind;
980 std::vector<Scalar> new_entry_val;
981 new_entry_ind.push_back(region_node_idx_neighbor2 - 1);
982 new_entry_val.push_back(new_value);
983
984 // If a nonzero value is already stored in the specified position, the new values is SUMMED to the already existing one
985 // See description of insertGlobalValues(...)
986 region_matrix->insertGlobalValues(region_node_idx - 1, new_entry_ind, new_entry_val);
987 }
988 // central node collapse
989 {
990 Scalar initial_value = 0;
991 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = region_col.begin(); iter_view != region_col.end(); ++iter_view) {
992 if (region_matrix->isLocallyIndexed())
993 if (*iter_view == local_region_node_idx)
994 initial_value = region_val[iter_view - region_col.begin()];
995 if (region_matrix->isGloballyIndexed())
996 if (*iter_view == region_node_idx)
997 initial_value = region_val[iter_view - region_col.begin()];
998
999 if (initial_value != 0)
1000 break;
1001 }
1002
1003 Scalar external_value = 0;
1004 for (typename ArrayView<const GlobalOrdinal>::iterator iter_view = external_col.begin(); iter_view != external_col.end(); ++iter_view) {
1005 if (*iter_view == local_extra_central)
1006 external_value = external_val[iter_view - external_col.begin()];
1007
1008 if (external_value != 0)
1009 break;
1010 }
1011
1012 Scalar new_value = external_value; // new matrix entry generated with the collapsing
1013 std::vector<GlobalOrdinal> new_entry_ind;
1014 std::vector<Scalar> new_entry_val;
1015 new_entry_ind.push_back(region_node_idx - 1);
1016 new_entry_val.push_back(new_value);
1017
1018 // If a nonzero value is already stored in the specified position, the new values is SUMMED to the already existing one
1019 // See description of insertGlobalValues(...)
1020 region_matrix->insertGlobalValues(region_node_idx - 1, new_entry_ind, new_entry_val);
1021 }
1022 }
1023 }
1024 }
1025 }
1026 //@}
1027
1028 //! @name Creation of Region Splitting
1029 //@{
1030 void RegionSplitting(GlobalOrdinal region_idx, RCP<Matrix> &region_matrix, Ifpack2::OverlappingRowMatrix<tpetra_row_matrix> &enlargedMatrix) {
1031 TEUCHOS_TEST_FOR_EXCEPTION(!region_matrix_initialized_[region_idx], Exceptions::RuntimeError, "The composite stiffness matrix must be chopped into surrogate region matrices before collapsing \n");
1032 TEUCHOS_TEST_FOR_EXCEPTION(regionHandler_->GetNumRegionNodes(region_idx) != regionMatrixData_[region_idx]->getGlobalNumRows(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Number of region nodes in region " << region_idx + 1 << " does not coincide with the value returned by regionMatrixData_[" << region_idx + 1 << "]->getGlobalNumRows() \n");
1033
1034 Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > regionToAll = regionHandler_->GetRegionToAll(region_idx);
1035
1036 // This portion of the code assumes that the number of region nodes is the same on each direction of the domain
1037 // Foir a 2D problem we have then nx = ny = sqrt( num_region_ndoes_ )
1038 GlobalOrdinal n;
1039 GlobalOrdinal nx;
1040 GlobalOrdinal ny;
1041
1042 n = regionHandler_->GetNumRegionNodes(region_idx);
1043 nx = std::sqrt(n);
1044 ny = nx;
1045 TEUCHOS_TEST_FOR_EXCEPTION(static_cast<double>(nx - std::floor(static_cast<double>(std::sqrt(static_cast<double>(n))))) != 0.0, Exceptions::RuntimeError, "The code assumes that the regions are 2D and that the number of region nodes is the same on each direction of the domain \n");
1046
1047 // interfaceNodes contains nodes on an interface between any regions
1048 Array<std::tuple<int, Array<GlobalOrdinal> > > interfaceNodes = regionHandler_->GetInterfaceNodes();
1049
1050 ArrayView<const GlobalOrdinal> MyRegionElements = region_matrix->getRowMap()->getLocalElementList();
1051 for (typename ArrayView<const GlobalOrdinal>::iterator iter = MyRegionElements.begin(); iter != MyRegionElements.end(); ++iter) {
1052 // Nodes are saved in data structures with 1 as base index
1053 GlobalOrdinal region_node_idx = *iter + 1;
1054 checkerRegionToAll<GlobalOrdinal> unaryPredicate(region_node_idx);
1055 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator;
1056 composite_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicate);
1057 TEUCHOS_TEST_FOR_EXCEPTION(composite_iterator == regionToAll.end(), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
1058 << " node with region index: " << region_node_idx << " is not in regionToAll[" << region_idx << "]"
1059 << "\n");
1060
1061 GlobalOrdinal composite_node_idx = std::get<1>(*composite_iterator);
1062 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2(composite_node_idx);
1063 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator;
1064 interface_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2);
1065
1066 GlobalOrdinal region_node_idx_neighbor_w = 0;
1067 GlobalOrdinal region_node_idx_neighbor_e = 0;
1068 GlobalOrdinal region_node_idx_neighbor_n = 0;
1069 GlobalOrdinal region_node_idx_neighbor_s = 0;
1070
1071 int count_neighbours = 0;
1072
1073 if (interface_iterator != interfaceNodes.end() && region_node_idx > ny) {
1074 region_node_idx_neighbor_w = region_node_idx - ny;
1075 count_neighbours++;
1076 }
1077
1078 if (interface_iterator != interfaceNodes.end() && region_node_idx <= (nx - 1) * ny) {
1079 region_node_idx_neighbor_e = region_node_idx + ny;
1080 count_neighbours++;
1081 }
1082
1083 if (interface_iterator != interfaceNodes.end() && region_node_idx % ny != 1) {
1084 region_node_idx_neighbor_s = region_node_idx - 1;
1085 count_neighbours++;
1086 }
1087
1088 if (interface_iterator != interfaceNodes.end() && region_node_idx % ny != 0) {
1089 region_node_idx_neighbor_n = region_node_idx + 1;
1090 count_neighbours++;
1091 }
1092
1093 bool interface_line = false;
1094 bool interface_corner = false;
1095
1096 if (3 == count_neighbours)
1097 interface_line = true;
1098 else if (2 == count_neighbours)
1099 interface_corner = true;
1100
1101 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor_e;
1102 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor_e;
1103 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor_w;
1104 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor_w;
1105 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor_s;
1106 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor_s;
1107 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator composite_iterator_neighbor_n;
1108 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator interface_iterator_neighbor_n;
1109
1110 if (interface_line || interface_corner) {
1111 // Computation of composite index for East node
1112 if (region_node_idx_neighbor_e != 0) {
1113 checkerRegionToAll<GlobalOrdinal> unaryPredicateEast(region_node_idx_neighbor_e);
1114 composite_iterator_neighbor_e = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateEast);
1115
1116 // Check to see if neighbor_e node lies on a coarse line
1117 GlobalOrdinal composite_node_idx_neighbor_e = std::get<1>(*composite_iterator_neighbor_e);
1118 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighborEast(composite_node_idx_neighbor_e);
1119 interface_iterator_neighbor_e = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighborEast);
1120 } else
1121 interface_iterator_neighbor_e = interfaceNodes.end();
1122
1123 // Computation of composite index for West node
1124 if (region_node_idx_neighbor_w != 0) {
1125 checkerRegionToAll<GlobalOrdinal> unaryPredicateWest(region_node_idx_neighbor_w);
1126 composite_iterator_neighbor_w = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateWest);
1127
1128 // Check to see if neighbor_w node lies on a coarse line
1129 GlobalOrdinal composite_node_idx_neighbor_w = std::get<1>(*composite_iterator_neighbor_w);
1130 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighborWest(composite_node_idx_neighbor_w);
1131 interface_iterator_neighbor_w = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighborWest);
1132 } else
1133 interface_iterator_neighbor_w = interfaceNodes.end();
1134
1135 // Computation of composite index for South node
1136 if (region_node_idx_neighbor_s != 0) {
1137 checkerRegionToAll<GlobalOrdinal> unaryPredicateSouth(region_node_idx_neighbor_s);
1138 composite_iterator_neighbor_s = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateSouth);
1139
1140 // Check to see if neighbor_s node lies on a coarse line
1141 GlobalOrdinal composite_node_idx_neighbor_s = std::get<1>(*composite_iterator_neighbor_s);
1142 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighborSouth(composite_node_idx_neighbor_s);
1143 interface_iterator_neighbor_s = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighborSouth);
1144 } else
1145 interface_iterator_neighbor_s = interfaceNodes.end();
1146
1147 // Computation of composite index for North node
1148 if (region_node_idx_neighbor_n != 0) {
1149 checkerRegionToAll<GlobalOrdinal> unaryPredicateNorth(region_node_idx_neighbor_n);
1150 composite_iterator_neighbor_n = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicateNorth);
1151
1152 // Check to see if neighbor_n node lies on a coarse line
1153 GlobalOrdinal composite_node_idx_neighbor_n = std::get<1>(*composite_iterator_neighbor_n);
1154 checkerInterfaceNodes<GlobalOrdinal> unaryPredicate2neighborNorth(composite_node_idx_neighbor_n);
1155 interface_iterator_neighbor_n = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerInterfaceNodes<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicate2neighborNorth);
1156 } else
1157 interface_iterator_neighbor_n = interfaceNodes.end();
1158
1159 int count_neighbours_interface = 0;
1160 if (interface_iterator_neighbor_e != interfaceNodes.end())
1161 count_neighbours_interface++;
1162 if (interface_iterator_neighbor_w != interfaceNodes.end())
1163 count_neighbours_interface++;
1164 if (interface_iterator_neighbor_s != interfaceNodes.end())
1165 count_neighbours_interface++;
1166 if (interface_iterator_neighbor_n != interfaceNodes.end())
1167 count_neighbours_interface++;
1168
1169 TEUCHOS_TEST_FOR_EXCEPTION(count_neighbours_interface > count_neighbours, Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
1170 << " node with region index: " << region_node_idx << " has inconsistent information on the number of neighbours: count_neighbours = " << count_neighbours << "but count_neighbours_interface =" << count_neighbours_interface << " \n");
1171
1172 // First the splitting is applied on extradiagonal entries
1173
1174 // Computation of local indices for central node and its neighbors
1175 // Node index base start from 1 in the structures used, but Trilinos maps start from 0, so
1176 // indices must be shifted by 1
1177 LocalOrdinal local_region_node_idx = region_matrix->getRowMap()->getLocalElement(region_node_idx - 1);
1178 LocalOrdinal local_region_node_idx_neighbor_e = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor_e - 1);
1179 LocalOrdinal local_region_node_idx_neighbor_w = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor_w - 1);
1180 LocalOrdinal local_region_node_idx_neighbor_s = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor_s - 1);
1181 LocalOrdinal local_region_node_idx_neighbor_n = region_matrix->getRowMap()->getLocalElement(region_node_idx_neighbor_n - 1);
1182
1183 ArrayView<const GlobalOrdinal> region_col;
1184 ArrayView<const Scalar> region_val;
1185
1186 // Extract Row view of the region matrix
1187 if (region_matrix->isLocallyIndexed())
1188 region_matrix->getLocalRowView(local_region_node_idx, region_col, region_val);
1189 else
1190 region_matrix->getGlobalRowView(*iter, region_col, region_val);
1191
1192 std::vector<LocalOrdinal> region_col_vector = createVector(region_col);
1193 std::vector<LocalOrdinal> ind_vector(0);
1194 std::vector<Scalar> val_vector(0);
1195
1196 // Extraction of the info about East neighbour to halve the associated entry in the matrix
1197 if (interface_iterator_neighbor_e != interfaceNodes.end()) {
1198 typename std::vector<GlobalOrdinal>::iterator iter_east_vector;
1199 GlobalOrdinal east_ind;
1200 if (region_matrix->isLocallyIndexed()) {
1201 iter_east_vector = std::find(region_col_vector.begin(), region_col_vector.end(), local_region_node_idx_neighbor_e);
1202 east_ind = region_matrix->getRowMap()->getGlobalElement(*iter_east_vector);
1203 } else {
1204 iter_east_vector = std::find(region_col_vector.begin(), region_col_vector.end(), region_node_idx_neighbor_e - 1);
1205 east_ind = *iter_east_vector;
1206 }
1207 Scalar east_val = -0.5 * region_val[iter_east_vector - region_col_vector.begin()];
1208 ind_vector.push_back(east_ind);
1209 val_vector.push_back(east_val);
1210 }
1211
1212 // Extraction of the info about West neighbour to halve the associated entry in the matrix
1213 if (interface_iterator_neighbor_w != interfaceNodes.end()) {
1214 typename std::vector<GlobalOrdinal>::iterator iter_west_vector;
1215 GlobalOrdinal west_ind;
1216 if (region_matrix->isLocallyIndexed()) {
1217 iter_west_vector = std::find(region_col_vector.begin(), region_col_vector.end(), local_region_node_idx_neighbor_w);
1218 west_ind = region_matrix->getRowMap()->getGlobalElement(*iter_west_vector);
1219 } else {
1220 iter_west_vector = std::find(region_col_vector.begin(), region_col_vector.end(), region_node_idx_neighbor_w - 1);
1221 west_ind = *iter_west_vector;
1222 }
1223 Scalar west_val = -0.5 * region_val[iter_west_vector - region_col_vector.begin()];
1224 ind_vector.push_back(west_ind);
1225 val_vector.push_back(west_val);
1226 }
1227
1228 // Extraction of the info about South neighbour to halve the associated entry in the matrix
1229 if (interface_iterator_neighbor_s != interfaceNodes.end()) {
1230 typename std::vector<GlobalOrdinal>::iterator iter_south_vector;
1231 GlobalOrdinal south_ind;
1232 if (region_matrix->isLocallyIndexed()) {
1233 iter_south_vector = std::find(region_col_vector.begin(), region_col_vector.end(), local_region_node_idx_neighbor_s);
1234 south_ind = region_matrix->getRowMap()->getGlobalElement(*iter_south_vector);
1235 } else {
1236 iter_south_vector = std::find(region_col_vector.begin(), region_col_vector.end(), region_node_idx_neighbor_s - 1);
1237 south_ind = *iter_south_vector;
1238 }
1239 Scalar south_val = -0.5 * region_val[iter_south_vector - region_col_vector.begin()];
1240 ind_vector.push_back(south_ind);
1241 val_vector.push_back(south_val);
1242 }
1243
1244 // Extraction of the info about North neighbour to halve the associated entry in the matrix
1245 if (interface_iterator_neighbor_n != interfaceNodes.end()) {
1246 typename std::vector<GlobalOrdinal>::iterator iter_north_vector;
1247 GlobalOrdinal north_ind;
1248 if (region_matrix->isLocallyIndexed()) {
1249 iter_north_vector = std::find(region_col_vector.begin(), region_col_vector.end(), local_region_node_idx_neighbor_n);
1250 north_ind = region_matrix->getRowMap()->getGlobalElement(*iter_north_vector);
1251 } else {
1252 iter_north_vector = std::find(region_col_vector.begin(), region_col_vector.end(), region_node_idx_neighbor_n - 1);
1253 north_ind = *iter_north_vector;
1254 }
1255 Scalar north_val = -0.5 * region_val[iter_north_vector - region_col_vector.begin()];
1256 ind_vector.push_back(north_ind);
1257 val_vector.push_back(north_val);
1258 }
1259
1260 // Extraction of the info about my Node ID to split the associated entry in the matrix
1261 // The ratio used for the splitting depends on the num ber of regions this current node
1262 // belongs to
1263 typename std::vector<LocalOrdinal>::iterator iter_center_vector;
1264 GlobalOrdinal center_ind;
1265 if (region_matrix->isLocallyIndexed()) {
1266 iter_center_vector = std::find(region_col_vector.begin(), region_col_vector.end(), local_region_node_idx);
1267 center_ind = region_matrix->getRowMap()->getGlobalElement(*iter_center_vector);
1268 } else {
1269 iter_center_vector = std::find(region_col_vector.begin(), region_col_vector.end(), region_node_idx - 1);
1270 center_ind = *iter_center_vector;
1271 }
1272
1273 // Count of the nubmer of regions the current node belogns to
1274 GlobalOrdinal region_belonging = std::get<1>(*interface_iterator).size();
1275 TEUCHOS_TEST_FOR_EXCEPTION(region_belonging < 2, Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Region: " << region_idx << " - "
1276 << " node with composite index: " << std::get<0>(*interface_iterator) << " should lie on an interface between regions but the nubmer of regions it belongs to is only " << region_belonging << "\n");
1277
1278 Scalar center_val;
1279 // If a node is on a corner between four itnerfaces, then each the entry A(node_idx,node_idx) must be split in four parts
1280 // otherwise the entry must be divided by two, similarly to what done for the neighbours
1281
1282 center_val = -(1 - static_cast<Scalar>(1 / static_cast<Scalar>(region_belonging))) * region_val[iter_center_vector - region_col_vector.begin()];
1283 ind_vector.push_back(center_ind);
1284 val_vector.push_back(center_val);
1285
1286 region_matrix->insertGlobalValues(region_node_idx - 1, ind_vector, val_vector);
1287 }
1288 }
1289 }
1290 //@}
1291
1292 //! @name Creation of Region matrices
1293 //@{
1294 void CreateRegionMatrices(Array<Array<GlobalOrdinal> > region_maps) {
1295 TEUCHOS_TEST_FOR_EXCEPTION(num_total_regions_ != region_maps.size(), Exceptions::RuntimeError, "Number of regions does not match with the size of region_maps structure \n");
1296
1297 regionMatrixData_.clear();
1298
1299 for (int i = 0; i < num_total_regions_; ++i) {
1300 // Create Xpetra map for region stiffness matrix
1301 RCP<const Xpetra::Map<int, GlobalOrdinal, Node> > xpetraMap;
1302 xpetraMap = Xpetra::MapFactory<int, GlobalOrdinal, Node>::Build(lib, regionHandler_->GetNumRegionNodes(i), region_maps[i], 0, comm_);
1303 int num_elements = xpetraMap->getGlobalNumElements();
1304
1305 RCP<CrsMatrix> crs_matrix;
1306 if (Xpetra::UseEpetra == lib)
1307 crs_matrix = rcp(new EpetraCrsMatrix(xpetraMap, num_elements));
1308 else if (Xpetra::UseTpetra == lib)
1309 crs_matrix = rcp(new TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xpetraMap, num_elements));
1310 else
1311 std::cerr << " The library to build matrices must be either Epetra or Tpetra \n";
1312
1313 RCP<Matrix> matrixPointer = rcp(new CrsMatrixWrap(crs_matrix));
1314 regionMatrixData_.push_back(matrixPointer);
1315 }
1316
1317 for (GlobalOrdinal i = 0; i < num_total_regions_; ++i)
1318 RegionMatrix(i);
1319 };
1320 //@}
1321
1322 //@}
1323
1324 //! Private variables
1325 //@{
1326
1327 // The boolean finalDefaultView_ keep track of the status of the default view (= already updated or not)
1328 // See also MatrixSplitting::updateDefaultView()
1329 mutable bool finalDefaultView_;
1330
1331 Array<bool> region_matrix_initialized_;
1332
1333 RCP<const Teuchos::Comm<int> > comm_;
1334
1335 //! Handling node-to-region mappings etc.
1336 RCP<RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionHandler_;
1337
1338 //! The original (non-splitted) matrix
1339 RCP<Matrix> compositeMatrixData_;
1340
1341 //! The matrix after splitting according to region layout
1342 Array<RCP<Matrix> > regionMatrixData_;
1343
1344 GlobalOrdinal num_total_elements_ = 0;
1345 GlobalOrdinal num_total_regions_ = 0;
1346
1347 //@}
1348
1349 }; // class MatrixSplitting
1350
1351 } // namespace Xpetra
1352
1353 #endif // XPETRA_MATRIXSPLITTING_HPP