LifeV
VectorEpetra.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief VectorEpetra
30 
31  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
32  @author Simone Deparis <simone.deparis@epfl.ch>
33  @author Cristiano Malossi <cristiano.malossi@epfl.ch>
34  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
35  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
36 
37  @date 04-10-2006
38  */
39 
40 
41 #include <EpetraExt_MultiVectorOut.h>
42 
43 
44 #include <lifev/core/LifeV.hpp>
45 #include <lifev/core/array/VectorEpetra.hpp>
46 
47 namespace LifeV
48 {
49 
50 // ===================================================
51 // Constructors & Destructor
52 // ===================================================
53 VectorEpetra::VectorEpetra ( const MapEpetraType& mapType, const combineMode_Type combineMode ) :
54  M_epetraMap (),
55  M_mapType ( mapType ),
57  M_combineMode ( combineMode )
58 {
59 }
60 
62  const MapEpetraType& mapType,
63  const combineMode_Type combineMode ) :
64  M_epetraMap ( new MapEpetra ( map ) ),
65  M_mapType ( mapType ),
66  M_combineMode ( combineMode )
67 {
68  ASSERT (M_epetraMap->map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
69 
70  M_epetraVector.reset( new vector_type ( *M_epetraMap->map (M_mapType) ) );
71 }
72 
73 VectorEpetra::VectorEpetra ( const std::shared_ptr<MapEpetra>& map,
74  const MapEpetraType& mapType,
75  const combineMode_Type combineMode ) :
76  M_epetraMap ( map ),
77  M_mapType ( mapType ),
78  M_combineMode ( combineMode )
79 {
80  ASSERT (M_epetraMap->map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
81 
82  M_epetraVector.reset( new vector_type ( *M_epetraMap->map (M_mapType) ) );
83 }
84 
87  M_mapType ( vector.M_mapType ),
89 {
90  if (vector.epetraVectorPtr().get()!=0)
91  M_epetraVector.reset( new vector_type ( vector.epetraVector() ) ); //This make a true copy!
92 }
93 
94 VectorEpetra::VectorEpetra ( const VectorEpetra& vector, const MapEpetraType& mapType) :
96  M_mapType ( mapType ),
98 {
99  ASSERT (M_epetraMap->map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
100 
101  M_epetraVector.reset( new vector_type ( *M_epetraMap->map ( M_mapType ) ) );
102 
103  operator = (vector);
104 }
105 
106 VectorEpetra::VectorEpetra ( const VectorEpetra& vector, const MapEpetraType& mapType,
107  const combineMode_Type& combineMode ) :
109  M_mapType ( mapType ),
110  M_combineMode ( vector.M_combineMode )
111 {
112  ASSERT (M_epetraMap->map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
113 
114  M_epetraVector.reset( new vector_type ( *M_epetraMap->map ( M_mapType ) ) );
115 
116  if (mapType == vector.M_mapType)
117  {
118  *M_epetraVector = vector.epetraVector();
119  return;
120  }
121 
122  *this = 0.; // because of a buggy behaviour in case of multidefined indeces.
123 
124  switch (M_mapType)
125  {
126  case Unique:
127  M_epetraVector->Export ( vector.epetraVector(), M_epetraMap->importer(), combineMode );
128  return ;
129  case Repeated:
130  M_epetraVector->Import ( vector.epetraVector(), M_epetraMap->exporter(), combineMode );
131  return ;
132  }
133 }
134 
135 VectorEpetra::VectorEpetra ( const Epetra_MultiVector& vector,
136  const std::shared_ptr<MapEpetra> map,
137  const MapEpetraType& mapType,
138  const combineMode_Type combineMode ) :
139  M_epetraMap ( map ),
140  M_mapType ( mapType ),
141  M_combineMode ( combineMode )
142 {
143  ASSERT (M_epetraMap->map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
144 
145  assert ( M_epetraMap->map ( mapType )->SameAs (vector.Map() ) );
146 
147  M_epetraVector.reset( new vector_type ( *M_epetraMap->map ( mapType ) ) );
148  M_epetraVector->Update (1., vector, 0.);
149 }
150 
151 VectorEpetra::VectorEpetra ( const VectorEpetra& vector, const Int& reduceToProc) :
152  M_mapType ( Unique ),
153  M_combineMode ( vector.M_combineMode )
154 {
155  ASSERT (vector.map().map(M_mapType).get()!=0, "Error! The stored MapEpetra does not have valid map pointer.\n");
156 
157  M_epetraMap = vector.M_epetraMap->createRootMap ( reduceToProc );
158 
159  M_epetraVector.reset( new vector_type ( *M_epetraMap->map (M_mapType) ) );
160 
161  operator = ( vector );
162 }
163 
164 // ===================================================
165 // Operators
166 // ===================================================
168 VectorEpetra::operator[] ( const UInt row )
169 {
170  Int lrow = blockMap().LID (static_cast<EpetraInt_Type> (row));
171 
172 #ifdef HAVE_LIFEV_DEBUG
173  if ( lrow < 0 )
174  {
175  std::cout << M_epetraVector->Comm().MyPID() << " " << row << " " << lrow << std::endl;
176  ERROR_MSG ( "VectorEpetra::operator [] ERROR : !! lrow < 0\n" );
177  }
178 #endif
179 
180  return (*M_epetraVector) [0][lrow];
181 }
182 
183 const VectorEpetra::data_type&
184 VectorEpetra::operator[] ( const UInt row ) const
185 {
186  Int lrow = blockMap().LID (static_cast<EpetraInt_Type> (row));
187 
188 #ifdef HAVE_LIFEV_DEBUG
189  if ( lrow < 0 )
190  {
191  std::cout << M_epetraVector->Comm().MyPID() << " " << row << " " << lrow << std::endl;
192  ERROR_MSG ( "VectorEpetra::operator () ERROR : !! lrow < 0\n" );
193 
194  }
195 #endif
196 
197  return ( (*M_epetraVector) [0][lrow]);
198 }
199 
201 VectorEpetra::operator() ( const UInt row )
202 {
203  return operator[] (static_cast<EpetraInt_Type> (row));
204 }
205 
206 
207 const VectorEpetra::data_type&
208 VectorEpetra::operator() ( const UInt row ) const
209 {
210  return operator[] (static_cast<EpetraInt_Type> (row));
211 }
212 
213 // copies the value of a vector u. If the map is not the same,
214 // try to import the values.
216 VectorEpetra::operator= ( const VectorEpetra& vector )
217 {
218  if ( &vector.epetraVector() == &this->epetraVector() )
219  {
220  return *this;
221  }
222 
223  if ( blockMap().SameAs ( vector.blockMap() ) )
224  {
225  *M_epetraVector = vector.epetraVector();
226  return *this;
227  }
228 
229  *this *= 0.; // because of a buggy behaviour in case of multidefined indeces.
230 
231  // vector have the same underlying MapEpetra, we then use the existing importer/exporter
232  if ( M_epetraMap.get() && vector.M_epetraMap.get() &&
233  M_epetraMap->mapsAreSimilar ( *vector.M_epetraMap ) )
234  {
235  // we shouldn't get here if we have the same maptype!
236  assert ( M_mapType != vector.M_mapType );
237 
238  switch ( M_mapType )
239  {
240  case Unique:
241  M_epetraVector->Export (vector.epetraVector(), M_epetraMap->importer(), M_combineMode);
242  return *this;
243  case Repeated:
244  M_epetraVector->Import (vector.epetraVector(), M_epetraMap->exporter(), M_combineMode);
245  return *this;
246  }
247  }
248  switch ( vector.M_mapType )
249  {
250  case Repeated:
251  if ( M_mapType != Repeated )
252  {
253  return Export (vector.epetraVector(), M_combineMode);
254  }
255  case Unique:
256  return Import (vector.epetraVector(), M_combineMode);
257  }
258 
259  // if we get here, it means that we have two different repeated maps.
260  // To handle this case, we have to create a unique copy first:
261 
262  std::cout << "Tentative of export import from two repeated vectors based on different maps."
263  << std::endl;
264 
265  VectorEpetra vectorUnique ( *M_epetraMap, Unique );
266  vectorUnique.Export ( vector.epetraVector(), M_combineMode );
267  M_epetraVector->Import ( vectorUnique.epetraVector(), M_epetraMap->exporter(), M_combineMode );
268 
269  return *this;
270 }
271 
272 // copies the value of a Epetra_MultiVector u (assumed of width 1). If the map is not the same,
273 // try to import the values. Calls Import with Add.
275 VectorEpetra::operator= ( const Epetra_MultiVector& vector )
276 {
277  Epetra_FEVector const* feVec ( dynamic_cast<Epetra_FEVector const*> (&vector) );
278  assert ( feVec );
279 
280  // We hope we are guessing right
281  switch ( M_mapType )
282  {
283  case Unique:
284  return Export ( *feVec, M_combineMode );
285  case Repeated:
286  return Import ( *feVec, M_combineMode );
287  }
288 
289  return *this;
290 }
291 
294 {
295  M_epetraVector->PutScalar (scalar);
296 
297  return *this;
298 }
299 
301 VectorEpetra::operator+= ( const VectorEpetra& vector )
302 {
303  if ( this->blockMap().SameAs ( vector.blockMap() ) )
304  {
305  M_epetraVector->Update ( 1., vector.epetraVector(), 1. );
306  }
307  else
308  {
310  M_epetraVector->Update ( 1., vCopy.epetraVector(), 1. );
311  }
312 
313  return *this;
314 }
315 
317 VectorEpetra::operator-= ( const VectorEpetra& vector )
318 {
319  if ( this->blockMap().SameAs ( vector.blockMap() ) )
320  {
321  M_epetraVector->Update ( -1., vector.epetraVector(), 1. );
322  }
323  else
324  {
325  VectorEpetra vCopy (vector, M_mapType);
326  M_epetraVector->Update ( -1., vCopy.epetraVector(), 1. );
327  }
328 
329  return *this;
330 }
331 
333 VectorEpetra::operator*= ( const VectorEpetra& vector )
334 {
335  if ( this->blockMap().SameAs ( vector.blockMap() ) )
336  {
337  M_epetraVector->Multiply ( 1.0, vector.epetraVector(), *M_epetraVector, 0.0 );
338  }
339  else
340  {
341  VectorEpetra vectorCopy ( vector, M_mapType );
342  M_epetraVector->Multiply ( 1.0, vectorCopy.epetraVector(), *M_epetraVector, 0.0 );
343  }
344 
345  return *this;
346 }
347 
349 VectorEpetra::operator/= ( const VectorEpetra& vector )
350 {
351  if ( this->blockMap().SameAs ( vector.blockMap() ) )
352  {
353  M_epetraVector->ReciprocalMultiply ( 1.0, vector.epetraVector(), *M_epetraVector, 0.0 );
354  }
355  else
356  {
357  VectorEpetra vectorCopy ( vector, M_mapType );
358  M_epetraVector->ReciprocalMultiply ( 1.0, vectorCopy.epetraVector(), *M_epetraVector, 0.0 );
359  }
360 
361  return *this;
362 }
363 
364 const VectorEpetra
365 VectorEpetra::operator+ ( const VectorEpetra& vector ) const
366 {
367  VectorEpetra MyVectorCopy ( *this );
368 
369  MyVectorCopy += vector;
370 
371  return MyVectorCopy;
372 }
373 
374 const VectorEpetra
375 VectorEpetra::operator- ( const VectorEpetra& vector ) const
376 {
377  VectorEpetra MyVectorCopy ( *this );
378 
379  MyVectorCopy -= vector;
380 
381  return MyVectorCopy;
382 }
383 
384 const VectorEpetra
385 VectorEpetra::operator* ( const VectorEpetra& vector ) const
386 {
387  VectorEpetra MyVectorCopy ( *this );
388 
389  MyVectorCopy *= vector;
390 
391  return MyVectorCopy;
392 }
393 
394 const VectorEpetra
395 VectorEpetra::operator/ ( const VectorEpetra& vector ) const
396 {
397  VectorEpetra MyVectorCopy ( *this );
398 
399  MyVectorCopy /= vector;
400 
401  return MyVectorCopy;
402 }
403 
405 VectorEpetra::operator+= ( const data_type& scalar )
406 {
407  Int i, j;
408  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
409  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
410  {
411  (*M_epetraVector) [i][j] += scalar;
412  }
413 
414  return *this;
415 }
416 
418 VectorEpetra::operator-= ( const data_type& scalar )
419 {
420  this->operator+= ( -scalar );
421 
422  return *this;
423 }
424 
426 VectorEpetra::operator*= ( const data_type& scalar )
427 {
428  M_epetraVector->Scale ( scalar );
429 
430  return *this;
431 }
432 
434 VectorEpetra::operator/= ( const data_type& scalar )
435 {
436  this->operator*= ( 1. / scalar );
437 
438  return *this;
439 }
440 
441 const VectorEpetra
442 VectorEpetra::operator+ ( const data_type& scalar ) const
443 {
444  VectorEpetra MyVectorCopy ( *this );
445 
446  MyVectorCopy += scalar;
447 
448  return MyVectorCopy;
449 }
450 
451 const VectorEpetra
452 VectorEpetra::operator- ( const data_type& scalar ) const
453 {
454  VectorEpetra MyVectorCopy ( *this );
455 
456  MyVectorCopy -= scalar;
457 
458  return MyVectorCopy;
459 }
460 
461 const VectorEpetra
462 VectorEpetra::operator* ( const data_type& scalar ) const
463 {
464  VectorEpetra MyVectorCopy ( *this );
465 
466  MyVectorCopy *= scalar;
467 
468  return MyVectorCopy;
469 }
470 
471 const VectorEpetra
472 VectorEpetra::operator/ ( const data_type& scalar ) const
473 {
474  VectorEpetra MyVectorCopy ( *this );
475 
476  MyVectorCopy /= scalar;
477 
478  return MyVectorCopy;
479 }
480 
482 VectorEpetra::operator== ( const Real& scalar ) const
483 {
484  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
485 
486  Int i, j;
487  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
488  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
489  {
490  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] == scalar ? true : false;
491  }
492 
493  return comparisonVector;
494 }
495 
497 VectorEpetra::operator!= ( const Real& scalar ) const
498 {
499  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
500 
501  Int i, j;
502  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
503  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
504  {
505  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] != scalar ? true : false;
506  }
507 
508  return comparisonVector;
509 }
510 
512 VectorEpetra::operator< ( const Real& scalar ) const
513 {
514  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
515 
516  Int i, j;
517  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
518  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
519  {
520  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] < scalar ? true : false;
521  }
522 
523  return comparisonVector;
524 }
525 
527 VectorEpetra::operator> ( const Real& scalar ) const
528 {
529  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
530 
531  Int i, j;
532  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
533  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
534  {
535  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] > scalar ? true : false;
536  }
537 
538  return comparisonVector;
539 }
540 
542 VectorEpetra::operator<= ( const Real& scalar ) const
543 {
544  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
545 
546  Int i, j;
547  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
548  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
549  {
550  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] <= scalar ? true : false;
551  }
552 
553  return comparisonVector;
554 }
555 
557 VectorEpetra::operator>= ( const Real& scalar ) const
558 {
559  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
560 
561  Int i, j;
562  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
563  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
564  {
565  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] >= scalar ? true : false;
566  }
567 
568  return comparisonVector;
569 }
570 
572 VectorEpetra::operator&& ( const VectorEpetra& vector ) const
573 {
574  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
575 
576  Int i, j;
577  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
578  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
579  {
580  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] && vector.epetraVector() [i][j];
581  }
582 
583  return comparisonVector;
584 }
585 
587 VectorEpetra::operator|| ( const VectorEpetra& vector ) const
588 {
589  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
590 
591  Int i, j;
592  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
593  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
594  {
595  comparisonVector.epetraVector() [i][j] = (*M_epetraVector) [i][j] || vector.epetraVector() [i][j];
596  }
597 
598  return comparisonVector;
599 }
600 
602 VectorEpetra::operator! ( void ) const
603 {
604  VectorEpetra comparisonVector ( *M_epetraMap, M_mapType );
605 
606  Int i, j;
607  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
608  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
609  {
610  comparisonVector.epetraVector() [i][j] = ! (*M_epetraVector) [i][j];
611  }
612 
613  return comparisonVector;
614 }
615 
616 
617 // ===================================================
618 // Methods
619 // ===================================================
620 bool VectorEpetra::isGlobalIDPresent (const UInt row) const
621 {
622  return blockMap().LID ( static_cast<EpetraInt_Type> (row) ) >= 0;
623 }
624 
626 {
627  Int lrow = blockMap().LID ( static_cast<EpetraInt_Type> (row) );
628 
629  if ( lrow < 0 && blockMap().Comm().NumProc() == 1 )
630  {
631  std::cout << M_epetraVector->Comm().MyPID() << " " << row << " " << lrow << std::endl;
632  ERROR_MSG ( "VectorEpetra::globalToLocalRowId ERROR : !! lrow < 0\n" );
633  }
634 
635  return lrow;
636 }
637 
638 bool VectorEpetra::setCoefficient ( const UInt row, const data_type& value, UInt offset )
639 {
640  Int lrow = globalToLocalRowId (row + offset);
641  if ( lrow < 0 )
642  {
643  return false;
644  }
645 
646  (*M_epetraVector) [0][lrow] = value;
647  return true;
648 }
649 
650 Int VectorEpetra::setCoefficients ( std::vector<Int>& rowsVector, std::vector<Real>& valuesVector )
651 {
652  ASSERT ( rowsVector.size() == valuesVector.size(), "Error: rowsVector and valuesVector should have the same size" );
653  ASSERT ( M_mapType == Unique, "Error: Vector must have a unique map" );
654 
655  // Coding this part by hands, in fact I do not trust the following line (Simone, June 2008)
656  // return M_epetraVector->ReplaceGlobalValues(rowsVector.size(), &rowsVector.front(), &valuesVector.front());
657 
658  const Epetra_Comm& Comm ( M_epetraVector->Comm() );
659  Int numProcs ( Comm.NumProc() );
660  Int MyPID ( Comm.MyPID() );
661  Int i;
662 
663  // Note: Epetra_Comm::broadcast does not support passing of uint, hence
664  // I define an int pointer to make the broadcast but then come back to an
665  // UInt pointer to insert the data
666  Int* r;
667  data_type* datum;
668 
669  // loop on all proc
670  for ( Int p (0); p < numProcs; p++ )
671  {
672  Int sizeVec ( static_cast<Int> ( rowsVector.size() ) );
673  if ( sizeVec != static_cast<Int> ( valuesVector.size() ) )
674  {
675  //! vectors must be of the same size
676  ERROR_MSG ( "diagonalize: vectors must be of the same size\n" );
677  }
678 
679  Comm.Broadcast (&sizeVec, 1, p);
680 
681  if ( p == MyPID )
682  {
683  r = &rowsVector.front();
684  datum = &valuesVector.front();
685  }
686  else
687  {
688  r = new Int [sizeVec];
689  datum = new data_type[sizeVec];
690  }
691 
692  Comm.Broadcast (r, sizeVec, p);
693  Comm.Broadcast (datum, sizeVec, p);
694 
695  // row: if r is mine, Assign values
696  for (i = 0; i < sizeVec; ++i)
697  {
698  setCoefficient (r[i], datum[i]);
699  }
700 
701  if ( p != MyPID )
702  {
703  delete[] r;
704  delete[] datum;
705  }
706  }
707 
708  return 0;
709 
710 }
711 
712 Int
713 VectorEpetra::sumIntoGlobalValues ( const Int GID, const Real value )
714 {
715  return M_epetraVector->SumIntoGlobalValues ( 1, &GID, &value );
716 }
717 
719 VectorEpetra::add ( const VectorEpetra& vector, const Int offset )
720 {
721 
722  Int numMyEntries = vector.M_epetraVector->MyLength ();
723  const Int* gids = vector.blockMap().MyGlobalElements();
724 
725  // eg: (u,p) += p or (u,p) += u
726  for ( Int i = 0; i < numMyEntries; ++i )
727  {
728  (*this) [gids[i] + offset] += vector (gids[i]);
729  }
730 
731  return *this;
732 }
733 
735 VectorEpetra::replace ( const VectorEpetra& vector, const Int& offset )
736 {
737  // Definitions
738  Int numMyEntries = vector.M_epetraVector->MyLength ();
739  const Int* globalIDs = vector.blockMap().MyGlobalElements();
740 
741  // Replace part of the vector
742  for ( Int i (0); i < numMyEntries; ++i )
743  {
744  ( *this ) [globalIDs[i] + offset] = vector ( globalIDs[i] );
745  }
746 
747  return *this;
748 }
749 
752  const UInt offset )
753 {
754  return this->subset ( vector, map(), offset, static_cast<UInt> (0) );
755 }
756 
759  const MapEpetra& map,
760  const UInt offset1,
761  const UInt offset2 )
762 {
763  if ( M_mapType == Repeated && vector.mapType() == Unique )
764  {
765  return subset (VectorEpetra ( vector, Repeated), map, offset1, offset2 );
766  }
767  return subset ( vector.epetraVector(), map, offset1, offset2 );
768 }
769 
771 VectorEpetra::subset ( const Epetra_MultiVector& vector,
772  const MapEpetra& map,
773  const UInt offset1,
774  const UInt offset2,
775  const UInt column )
776 {
777  const Int* gids = map.map (M_mapType)->MyGlobalElements();
778  const UInt numMyEntries = map.map (M_mapType)->NumMyElements();
779 
780  Int lid1 ;
781  Int lid2 ;
782 
783  // eg: p = (u,p) or u = (u,p)
784  for ( UInt i = 0; i < numMyEntries; ++i )
785  {
786  lid1 = vector.Map().LID ( static_cast<EpetraInt_Type> (gids[i] + offset1) );
787  lid2 = blockMap().LID ( static_cast<EpetraInt_Type> (gids[i] + offset2) );
788  ASSERT ( ( lid2 >= 0 ) && ( lid1 >= 0 ), "VectorEpetra::subset ERROR : !! lid < 0\n" );
789  (*M_epetraVector) [0][lid2] = vector[column][lid1];
790  }
791 
792  return *this;
793 }
794 
795 void
796 VectorEpetra::meanValue ( Real* result ) const
797 {
798  M_epetraVector->MeanValue (result);
799 }
800 
801 Real
803 {
804  Real result;
805  M_epetraVector->Norm1 (&result);
806  return result;
807 }
808 
809 void
810 VectorEpetra::norm1 ( Real* result ) const
811 {
812  this->norm1 (*result);
813 }
814 
815 void
816 VectorEpetra::norm1 ( Real& result ) const
817 {
818 
819  if (this->mapType() == Repeated)
820  {
821  VectorEpetra vUnique (*this, Unique, M_combineMode);
822  vUnique.norm1 ( &result );
823  return;
824  }
825  M_epetraVector->Norm1 (&result);
826 
827 }
828 
829 Real
831 {
832  Real result;
833  M_epetraVector->Norm2 (&result);
834  return result;
835 }
836 
837 void
838 VectorEpetra::norm2 ( Real* result ) const
839 {
840  this->norm2 (*result);
841 }
842 
843 void
844 VectorEpetra::norm2 ( Real& result ) const
845 {
846  if (this->mapType() == Repeated)
847  {
848  VectorEpetra vUnique (*this, Unique, M_combineMode);
849  vUnique.norm2 ( &result );
850  return;
851  }
852 
853  M_epetraVector->Norm2 ( &result );
854 }
855 
856 Real
858 {
859  Real result;
860  M_epetraVector->NormInf (&result);
861  return result;
862 }
863 
864 void
865 VectorEpetra::normInf ( Real* result ) const
866 {
867  M_epetraVector->NormInf (result);
868 }
869 
870 void
871 VectorEpetra::normInf ( Real& result ) const
872 {
873  M_epetraVector->NormInf (&result);
874 }
875 
876 Real
878 {
879  Real result;
880  M_epetraVector->MinValue (&result);
881  return result;
882 }
883 
884 Real
886 {
887  Real result;
888  M_epetraVector->MaxValue (&result);
889  return result;
890 }
891 
892 void
893 VectorEpetra::minValue ( Real* result ) const
894 {
895  M_epetraVector->MinValue (result);
896 }
897 
898 void
899 VectorEpetra::maxValue ( Real* result ) const
900 {
901  M_epetraVector->MaxValue (result);
902 }
903 
904 void
905 VectorEpetra::minValue ( Real& result ) const
906 {
907  M_epetraVector->MinValue (&result);
908 }
909 
910 void
911 VectorEpetra::maxValue ( Real& result ) const
912 {
913  M_epetraVector->MaxValue (&result);
914 }
915 
916 void
917 VectorEpetra::abs ( void )
918 {
919  M_epetraVector->Abs ( *M_epetraVector );
920 }
921 
922 void
924 {
925  vector.M_epetraVector->Abs ( *M_epetraVector );
926 }
927 
928 void
930 {
931  for ( Int i (0); i < M_epetraVector->NumVectors(); ++i )
932  {
933  for ( Int j (0); j < M_epetraVector->MyLength(); ++j )
934  {
935  (*M_epetraVector) [i][j] = std::sqrt ( (*M_epetraVector) [i][j] );
936  }
937  }
938 }
939 
940 // Scalar Products
942 VectorEpetra::dot ( const VectorEpetra& vector ) const
943 {
944  data_type scalarProduct;
945  M_epetraVector->Dot ( vector.epetraVector(), &scalarProduct );
946 
947  return scalarProduct;
948 }
949 
950 void
951 VectorEpetra::dot ( const VectorEpetra& vector, data_type& scalarProduct )
952 {
953  M_epetraVector->Dot ( vector.epetraVector(), &scalarProduct );
954 }
955 
956 void VectorEpetra::matrixMarket ( std::string const& fileName, const bool headers ) const
957 {
958  // Purpose: Matlab dumping and spy
959  std::string name = fileName;
960  std::string desc = "Created by LifeV";
961 
962  if (M_mapType == Repeated)
963  {
964  VectorEpetra unique (*this, Unique, Zero);
965  unique.spy (fileName);
966  return;
967  }
968 
969  name = fileName + ".mtx";
970 
971  EpetraExt::MultiVectorToMatrixMarketFile ( name.c_str(),
972  *M_epetraVector,
973  name.c_str(),
974  desc.c_str(),
975  headers );
976 }
977 
978 void VectorEpetra::spy ( std::string const& fileName ) const
979 {
980  // Purpose: Matlab dumping and spy
981  std::string name = fileName;
982 
983  Int me = M_epetraVector->Comm().MyPID();
984 
985  if (M_mapType == Repeated)
986  {
987  VectorEpetra unique ( *this, Unique, Zero );
988  unique.spy ( fileName );
989  return;
990  }
991 
992  // check on the file name
993  std::ostringstream myStream;
994  myStream << me;
995  name = fileName + ".m";
996 
997  EpetraExt::MultiVectorToMatlabFile ( name.c_str(), *M_epetraVector );
998 }
999 
1000 
1001 void VectorEpetra::showMe ( std::ostream& output ) const
1002 {
1003  VectorEpetra redVec ( *this, 0 ); // reduced vector (all at proc 0)
1004 
1005  if ( redVec.M_epetraVector->Comm().MyPID() )
1006  {
1007  return; // do not need other CPUs now
1008  }
1009 
1010  const Real* Values = redVec.epetraVector() [0];
1011  for ( Int i = 0; i < redVec.M_epetraVector->GlobalLength () ; ++i )
1012  {
1013  output << Values[i] << std::endl;
1014  }
1015 }
1016 
1017 void VectorEpetra::apply (const std::function<Real (Real)>& f)
1018 {
1019  Int i, j;
1020  for ( i = 0; i < M_epetraVector->NumVectors(); ++i )
1021  for ( j = 0; j < M_epetraVector->MyLength(); ++j )
1022  {
1023  (*M_epetraVector) [i][j] = f ( (*M_epetraVector) [i][j]);
1024  }
1025 }
1026 
1027 
1028 // ===================================================
1029 // Set Methods
1030 // ===================================================
1031 void
1033 {
1034  M_combineMode = combineMode;
1035 }
1036 
1037 void
1039 {
1040  setCombineMode (Add);
1041 }
1042 
1043 void
1045 {
1046  M_epetraMap.reset ( new MapEpetra ( map ) );
1047  M_epetraVector.reset ( new vector_type ( *M_epetraMap->map (M_mapType) ) );
1048 }
1049 
1050 // ===================================================
1051 // Get Methods
1052 // ===================================================
1053 Int
1055 {
1056  if ( M_epetraVector.get() )
1057  {
1058  return M_epetraVector->GlobalLength();
1059  }
1060  return 0;
1061 }
1062 
1063 // ===================================================
1064 // Private Methods
1065 // ===================================================
1066 VectorEpetra&
1067 VectorEpetra::Import (const Epetra_FEVector& vector, combineMode_Type combineMode )
1068 {
1069  if ( &vector == &this->epetraVector() )
1070  {
1071  return *this;
1072  }
1073 
1074  if ( blockMap().SameAs (vector.Map() ) )
1075  {
1076  *M_epetraVector = vector;
1077  return *this;
1078  }
1079 
1080  *this *= 0.; // because of a buggy behaviour in case of multidefined indeces.
1081 
1082  Epetra_Export reducedExport ( blockMap(), vector.Map() );
1083  M_epetraVector->Import ( vector, reducedExport, combineMode );
1084 
1085  return *this;
1086 }
1087 
1088 VectorEpetra&
1089 VectorEpetra::Export ( const Epetra_FEVector& vector, combineMode_Type combineMode )
1090 {
1091  if ( &vector == &this->epetraVector() )
1092  {
1093  return *this;
1094  }
1095 
1096  if ( blockMap().SameAs (vector.Map() ) )
1097  {
1098  *M_epetraVector = vector;
1099  return *this;
1100  }
1101 
1102  *this *= 0.; // because of a buggy behaviour in case of multidefined indeces.
1103 
1104  Epetra_Import reducedImport ( vector.Map(), blockMap() );
1105  M_epetraVector->Export (vector, reducedImport, combineMode);
1106 
1107  return *this;
1108 }
1109 
1111 operator- ( const VectorEpetra& vector )
1112 {
1113  VectorEpetra VectorCopy ( vector );
1114 
1115  return VectorCopy *= static_cast<VectorEpetra::data_type> ( -1.0 );
1116 }
1117 
1119 operator+ ( const VectorEpetra::data_type& scalar, const VectorEpetra& vector )
1120 {
1121  VectorEpetra VectorCopy ( vector );
1122 
1123  return VectorCopy += scalar;
1124 }
1125 
1127 operator- ( const VectorEpetra::data_type& scalar, const VectorEpetra& vector )
1128 {
1129  VectorEpetra VectorCopy ( -vector );
1130 
1131  return VectorCopy += scalar;
1132 }
1133 
1135 operator* ( const VectorEpetra::data_type& scalar, const VectorEpetra& vector )
1136 {
1137  VectorEpetra VectorCopy ( vector );
1138 
1139  return VectorCopy *= scalar;
1140 }
1141 
1142 } // end namespace LifeV
VectorEpetra & operator-=(const data_type &scalar)
Subtraction operator.
bool isGlobalIDPresent(const UInt row) const
Access operators.
VectorEpetra - The Epetra Vector format Wrapper.
VectorEpetra operator*(const VectorEpetra::data_type &scalar, const VectorEpetra &vector)
void normInf(Real *result) const
Compute and store the norm inf in the given pointed variable.
void minValue(Real *result) const
Compute and store the minimum value of the vector in the given pointed variable.
Epetra_CombineMode combineMode_Type
VectorEpetra operator-(const VectorEpetra::data_type &scalar, const VectorEpetra &vector)
const VectorEpetra operator*(const data_type &scalar) const
Multiplication operator.
VectorEpetra & operator+=(const data_type &scalar)
Addition operator.
VectorEpetra operator>=(const Real &scalar) const
Greater than or equal to operator.
const VectorEpetra operator-(const VectorEpetra &vector) const
Subtraction operator.
VectorEpetra(const VectorEpetra &vector, const MapEpetraType &mapType)
Copy constructor.
VectorEpetra & subset(const Epetra_MultiVector &vector, const MapEpetra &map, const UInt offset1, const UInt offset2, const UInt column=0)
Set the current vector to a subset of vector with an offset.
VectorEpetra & subset(const VectorEpetra &vector, const UInt offset=0)
Set the current vector to a subset of the given vector with an offset.
data_type & operator()(const UInt row)
Access operators.
const VectorEpetra operator+(const data_type &scalar) const
Operations with scalar values (do not modify the vector of the class)
VectorEpetra & add(const VectorEpetra &vector, const Int offset=0)
Add a vector to the current vector with an offset.
data_type dot(const VectorEpetra &vector) const
Compute the scalar product of two vectors.
vectorPtr_Type M_epetraVector
void abs(void)
Replace the vector with his absolute value.
VectorEpetra & Export(const Epetra_FEVector &vector, combineMode_Type combineMode)
Export the value of a vector.
bool setCoefficient(const UInt row, const data_type &value, UInt offset=0)
Look for the given global row and set its value.
void abs(VectorEpetra &vector)
Compute the absolute value of a vector and store it in an other vector.
VectorEpetra & Import(const Epetra_FEVector &vector, combineMode_Type combineMode)
Import the value of a vector.
Int globalToLocalRowId(const UInt row) const
Return the local Id of a global row.
Real norm1() const
Compute and return the norm 1.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void dot(const VectorEpetra &vector, data_type &scalarProduct)
Compute the scalar product of two vectors and store the result in a given variable.
void maxValue(Real &result) const
Compute and store the maximum value of the vector in the given variable.
MapEpetraType mapType() const
Return the map type of the vector (Unique or Repeated)
const data_type & operator[](const UInt row) const
Access operators.
VectorEpetra(const MapEpetra &map, const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Constructor - Using Maps.
void updateInverseJacobian(const UInt &iQuadPt)
VectorEpetra & operator/=(const data_type &scalar)
Division operator.
const VectorEpetra operator-(const data_type &scalar) const
Subtraction operator.
VectorEpetra operator!(void) const
Logical NOT operator.
VectorEpetra operator-(const VectorEpetra &vector)
void apply(const std::function< Real(Real)> &f)
VectorEpetra & operator*=(const VectorEpetra &vector)
Multiplication operator.
void setCombineMode(combineMode_Type combineMode)
Sets the combine mode for the import/export operations.
VectorEpetra operator &&(const VectorEpetra &vector) const
Logical AND operator.
#define ERROR_MSG(A)
Definition: LifeAssert.hpp:69
VectorEpetra operator||(const VectorEpetra &vector) const
Logical OR operator.
VectorEpetra & operator+=(const VectorEpetra &vector)
Addition operator.
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
Real normInf() const
Compute and return the norm inf.
VectorEpetra(const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Empty Constructor.
void norm2(Real *result) const
Compute and store the norm 2 in the given pointed variable.
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
VectorEpetra operator>(const Real &scalar) const
Greater than operator.
VectorEpetra & replace(const VectorEpetra &vector, const Int &offset)
Replace part of the vector with a given vector.
const VectorEpetra operator+(const VectorEpetra &vector) const
Addition operator.
const MapEpetra & map() const
Return the MapEpetra of the vector.
void sqrt()
Apply the square root to of each element in the vector.
Int setCoefficients(std::vector< Int > &rowsVector, std::vector< Real > &valuesVector)
Set the row of the vector to the given value.
VectorEpetra operator+(const VectorEpetra::data_type &scalar, const VectorEpetra &vector)
void norm1(Real *result) const
Compute and store the norm 1 in the given pointed variable.
VectorEpetra & operator-=(const VectorEpetra &vector)
Subtraction operator.
VectorEpetra & operator=(data_type scalar)
Affectation operator.
vector_type & epetraVector()
Return the VectorEpetra in the wrapper.
Real minValue() const
Compute and return the minimum value in the vector.
Real maxValue() const
Compute and return the maximum value in the vector.
Int size() const
Return the size of the vector.
double Real
Generic real data.
Definition: LifeV.hpp:175
const VectorEpetra operator/(const data_type &scalar) const
Division operator.
VectorEpetra(const VectorEpetra &vector, const Int &reduceToProc)
Copy constructor.
VectorEpetra(const std::shared_ptr< MapEpetra > &mapPtr, const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Constructor - Using Maps.
void setDefaultCombineMode()
Sets the combine mode for the import/export operations to default.
VectorEpetra & operator=(const Epetra_MultiVector &vector)
Affectation operator.
void norm2(Real &result) const
Compute and store the norm 2 in the given variable.
const VectorEpetra operator/(const VectorEpetra &vector) const
Division operator.
void maxValue(Real *result) const
Compute and store the maximum value of the vector in the given pointed variable.
VectorEpetra & operator*=(const data_type &scalar)
Multiplication operator.
const vector_type & epetraVector() const
Return the VectorEpetra in the wrapper.
void meanValue(Real *result) const
Compute the mean value of the vector components and store it in the given variable.
VectorEpetra operator==(const Real &scalar) const
Equality operator.
void norm1(Real &result) const
Compute and store the norm 1 in the given variable.
VectorEpetra(const VectorEpetra &vector)
Copy constructor.
VectorEpetra(const Epetra_MultiVector &vector, const std::shared_ptr< MapEpetra > map, const MapEpetraType &mapType, const combineMode_Type combineMode=Add)
Copy constructor.
VectorEpetra & operator=(const VectorEpetra &vector)
Affectation operator.
Int sumIntoGlobalValues(const Int GID, const Real value)
insert a global value
void setMap(const MapEpetra &map)
Sets the map to use for the epetra vector.
void normInf(Real &result) const
Compute and store the norm inf in the given variable.
combineMode_Type M_combineMode
VectorEpetra operator!=(const Real &scalar) const
Inequality operator.
VectorEpetra & subset(const VectorEpetra &vector, const MapEpetra &map, const UInt offset1, const UInt offset2)
Set the current vector to a subset of vector with an offset.
void showMe(std::ostream &output=std::cout) const
Print the contents of the vector.
const VectorEpetra operator*(const VectorEpetra &vector) const
Multiplication operator.
VectorEpetra(const VectorEpetra &vector, const MapEpetraType &mapType, const combineMode_Type &combineMode)
Copy constructor.
int EpetraInt_Type
Epetra int type (can be int or long long, accordingly to release notes)
Definition: LifeV.hpp:200
const data_type & operator()(const UInt row) const
Access operators.
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
void minValue(Real &result) const
Compute and store the minimum value of the vector in the given variable.
data_type & operator[](const UInt row)
Access operators.
VectorEpetra & operator/=(const VectorEpetra &vector)
Division operator.
MapEpetraType M_mapType
Real norm2() const
Compute and return the norm 2.