EpetraVector.cpp 26 KB
Newer Older
simone's avatar
simone committed
1
2
/* -*- mode: c++ -*-

malossi's avatar
malossi committed
3
 This file is part of the LifeV Applications.
simone's avatar
simone committed
4

malossi's avatar
malossi committed
5
6
7
 Author(s): Gilles Fourestey <gilles.fourestey@epfl.ch>
 Simone Deparis <simone.deparis@epfl.ch>
 Date: 2006-10-04
simone's avatar
simone committed
8

malossi's avatar
malossi committed
9
 Copyright (C) 2009 EPFL
simone's avatar
simone committed
10

malossi's avatar
malossi committed
11
12
13
14
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
simone's avatar
simone committed
15

malossi's avatar
malossi committed
16
17
18
19
 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.
simone's avatar
simone committed
20

malossi's avatar
malossi committed
21
22
23
24
25
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */
simone's avatar
simone committed
26
/**
malossi's avatar
malossi committed
27
28
29
30
 \file EpetraVector.cpp
 \author Gilles Fourestey <gilles.fourestey@epfl.ch>
         Simone Deparis <simone.deparis@epfl.ch>
 \date 2006-10-04
simone's avatar
simone committed
31
32
33
 */

#include <life/lifearray/EpetraVector.hpp>
grandper's avatar
grandper committed
34
#include <EpetraExt_MultiVectorOut.h>
simone's avatar
simone committed
35

malossi's avatar
malossi committed
36
namespace LifeV {
simone's avatar
simone committed
37

malossi's avatar
malossi committed
38
39
40
// ===================================================
//! Constructors
// ===================================================
simone's avatar
simone committed
41
42
EpetraVector::EpetraVector( const EpetraMap& _map, EpetraMapType maptype ):
    M_epetraMap   (new EpetraMap(_map)),
simone's avatar
   
simone committed
43
    M_maptype     (maptype),
44
45
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
simone's avatar
simone committed
46
47
48
{
}

49
50
EpetraVector::EpetraVector( const boost::shared_ptr<EpetraMap>& _map, EpetraMapType maptype ):
    M_epetraMap   (_map),
simone's avatar
   
simone committed
51
    M_maptype     (maptype),
52
53
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
54
55
56
{
}

malossi's avatar
malossi committed
57
58
EpetraVector::EpetraVector( const EpetraVector& vector):
    M_epetraMap   (vector.M_epetraMap),
simone's avatar
   
simone committed
59
    M_maptype     (vector.M_maptype),
60
61
    M_epetraVector(vector.M_epetraVector),
    M_combineMode (vector.M_combineMode)
62
63
64
{
}

malossi's avatar
malossi committed
65
66
EpetraVector::EpetraVector( const EpetraVector& vector, EpetraMapType maptype):
    M_epetraMap   (vector.M_epetraMap),
simone's avatar
   
simone committed
67
    M_maptype     (maptype),
68
69
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
simone's avatar
simone committed
70
{
malossi's avatar
malossi committed
71
    operator = (vector);
simone's avatar
simone committed
72
73
}

malossi's avatar
malossi committed
74
EpetraVector::EpetraVector( const EpetraVector& vector, EpetraMapType maptype,
simone's avatar
simone committed
75
                            Epetra_CombineMode combineMode):
malossi's avatar
malossi committed
76
    M_epetraMap   (vector.M_epetraMap),
simone's avatar
   
simone committed
77
    M_maptype     (maptype),
78
79
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
simone's avatar
simone committed
80
81
{

malossi's avatar
malossi committed
82
    if (maptype == vector.M_maptype)
simone's avatar
simone committed
83
    {
malossi's avatar
malossi committed
84
        M_epetraVector = vector.getEpetraVector();
simone's avatar
simone committed
85
86
87
88
89
90
91
        return;
    }

    *this *= 0.; // because of a buggy behaviour in case of multidefined indeces.

    switch (M_maptype) {
    case Unique:
malossi's avatar
malossi committed
92
        M_epetraVector.Export(vector.M_epetraVector, M_epetraMap->getImporter(), combineMode);
simone's avatar
simone committed
93
94
        return ;
    case Repeated:
malossi's avatar
malossi committed
95
        M_epetraVector.Import(vector.M_epetraVector, M_epetraMap->getExporter(), combineMode);
simone's avatar
simone committed
96
97
98
99
        return ;
    }
}

malossi's avatar
malossi committed
100
101
// Copies vector to FEvector that comes as Multivector
EpetraVector::EpetraVector( const Epetra_MultiVector&    vector,
simone's avatar
simone committed
102
                            boost::shared_ptr<EpetraMap> _map,
malossi's avatar
malossi committed
103
                            EpetraMapType                maptype ):
simone's avatar
simone committed
104
    M_epetraMap   ( _map),
simone's avatar
   
simone committed
105
    M_maptype     (maptype),
106
107
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
simone's avatar
simone committed
108
109
{

malossi's avatar
malossi committed
110
    assert(this->BlockMap().SameAs(vector.Map()) );
simone's avatar
simone committed
111

malossi's avatar
malossi committed
112
    M_epetraVector.Update(1., vector, 0.);
simone's avatar
simone committed
113
114
115

}

malossi's avatar
malossi committed
116
117
// Copies vector to a vector which resides only on the processor "reduceToProc"
EpetraVector::EpetraVector( const EpetraVector& vector, const int reduceToProc):
simone's avatar
   
simone committed
118
119
    M_epetraMap   (vector.M_epetraMap->createRootMap(reduceToProc)),
    M_maptype     (Unique),
120
121
    M_epetraVector(*M_epetraMap->getMap(M_maptype)),
    M_combineMode (Add)
simone's avatar
simone committed
122
{
malossi's avatar
malossi committed
123
    operator = (vector);
simone's avatar
simone committed
124
125
126
127
}



malossi's avatar
malossi committed
128
129
130
// ===================================================
//! Methods
// ===================================================
simone's avatar
simone committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! if row is mine returns the LID
//! if row is not mine and if the numCpus > 1, returns -1
//! if row is not mine and if the numCpus == 1, asserts
int EpetraVector::checkLID(const UInt row) const
{
    int lrow = BlockMap().LID(row); // BASEINDEX + 1, row + 1

    if (lrow < 0 && BlockMap().Comm().NumProc() == 1)
    {
        std::cout << M_epetraVector.Comm().MyPID() << " " << row << " " << lrow << std::endl;
        ERROR_MSG( "EpetraVector::checkLID ERROR : !! lrow < 0\n" );
    }

    return lrow;

}

//! if row is mine sets this[row] = value and return true
//! if row is not mine and if the numCpus > 1, returns false
//! if row is not mine and if the numCpus == 1, asserts
151
bool EpetraVector::checkAndSet(const UInt row, const data_type& value, UInt offset)
simone's avatar
simone committed
152
{
153
    int lrow = checkLID(row + offset);
simone's avatar
simone committed
154
155
156
157
158
159
160
161
162
    if (lrow < 0)
        return false;

    M_epetraVector[0][lrow] = value;
    return true;
}

//! Set the row row of the vector to value. If it isn't on this processor,
//! store it and send it and send it at next GlobalAssemble
simone's avatar
simone committed
163
int EpetraVector::replaceGlobalValues(std::vector<int>& rVec, std::vector<double>& datumVec)
simone's avatar
simone committed
164
165
{
    ASSERT( rVec.size() == datumVec.size(), "Error: rVec and datumVec should have the same size" );
simone's avatar
simone committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    ASSERT( M_maptype == Unique, "Error: Vector must have a unique map" );

    // Coding this part by hands, in fact I do not trust the following line (Simone, June 2008)
    // return M_epetraVector.ReplaceGlobalValues(rVec.size(), &rVec.front(), &datumVec.front());

    const Epetra_Comm&  Comm(M_epetraVector.Comm());
    int numProcs(Comm.NumProc());
    int MyPID   (Comm.MyPID()   );
    int i;

    // Note: Epetra_Comm::broadcast does not support passing of uint, hence
    //       I define an int pointer to make the broadcast but then come back to an
    //       UInt pointer to insert the data
    int*       r;
    data_type* datum;


    // loop on all proc
    for ( int p(0); p < numProcs; p++)
        {
malossi's avatar
malossi committed
186
187
            int sizeVec( static_cast<int>( rVec.size() ) );
            if ( sizeVec != static_cast<int>( datumVec.size() ) )
simone's avatar
simone committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                { //! vectors must be of the same size
                    ERROR_MSG( "diagonalize: vectors must be of the same size\n" );
                }

            Comm.Broadcast(&sizeVec, 1, p);

            if ( p == MyPID )
                {
                    r    =  &rVec    .front();
                    datum = &datumVec.front();
                }
            else
                {
                    r    = new int     [sizeVec];
                    datum = new data_type[sizeVec];
                }

            Comm.Broadcast(r,    sizeVec, p);
            Comm.Broadcast(datum,sizeVec, p);

            // row: if r is mine, Assign values
            for (i=0; i < sizeVec; ++i)
                checkAndSet(r[i], datum[i]);

            if ( p != MyPID )
                {
                    delete[] r;
                    delete[] datum;
                }

        }

    return 0;

simone's avatar
simone committed
222
223
224
225
226
227
228
229
230
}

int
EpetraVector::sumIntoGlobalValues (const int GID, const double value)
{
    return M_epetraVector.SumIntoGlobalValues(1, &GID, &value);
}


malossi's avatar
malossi committed
231
232
233
EpetraVector& EpetraVector::
add(const EpetraVector& vector,
    const int           offset )
simone's avatar
simone committed
234
{
malossi's avatar
malossi committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    if ( offset == 0 )
        return operator+= (vector);

    int numMyEntries = vector.M_epetraVector.MyLength ();
    const int*    gids       = vector.BlockMap().MyGlobalElements();

    // eg: (u,p) += p or (u,p) += u
    for (int i = 0; i < numMyEntries; ++i)
        {
            //        std::cout << gids[i] + offset << " " << gids[i] << std::endl;
            (*this)[gids[i]+offset] += vector(gids[i]);
        }

    return *this;
}

malossi's avatar
malossi committed
251
252
253
EpetraVector&
EpetraVector::subset(const EpetraVector& vector,
                     const UInt          offset )
malossi's avatar
malossi committed
254
{
malossi's avatar
malossi committed
255
    return this->subset( vector, getMap(), offset, static_cast<UInt> (0) );
malossi's avatar
malossi committed
256
257
258
259
260
}

EpetraVector& EpetraVector::
subset(const EpetraVector& vector,
       const EpetraMap&    map,
261
262
263
       const UInt          offset1,
       const UInt          offset2)
{
264
265
266
267
    if (M_maptype==Repeated && vector.getMaptype()==Unique )
    {
        return subset(EpetraVector(vector, Repeated), map, offset1, offset2);
    }
268
269
270
271
272
273
274
275
276
    return subset(vector.M_epetraVector, map, offset1, offset2);
}

EpetraVector& EpetraVector::
subset(const Epetra_MultiVector& vector,
       const EpetraMap&    map,
       const UInt          offset1,
       const UInt          offset2,
       const UInt          column)
malossi's avatar
malossi committed
277
{
278
279
280
281
282
    const int*    gids        = map.getMap(M_maptype)->MyGlobalElements();
    const UInt    numMyEntries = map.getMap(M_maptype)->NumMyElements();

    int lid1 ;
    int lid2 ;
malossi's avatar
malossi committed
283
284
285
286

    // eg:  p = (u,p) or u = (u,p)
    for (UInt i = 0; i < numMyEntries; ++i)
        {
287
288
289
            lid1 = vector.Map().LID(gids[i]+offset1);
            lid2 = BlockMap().LID(gids[i]+offset2);
            ASSERT( lid2 >= 0 & lid1 >= 0, "EpetraVector::subset ERROR : !! lid < 0\n" );
malossi's avatar
malossi committed
290
            //        std::cout << gids[i] + offset << " " << gids[i] << std::endl;
291
            M_epetraVector[0][lid2] = vector[column][lid1];
malossi's avatar
malossi committed
292
293
294
        }

    return *this;
simone's avatar
simone committed
295
296
}

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
void
EpetraVector::MeanValue(double* res) const
{
    M_epetraVector.MeanValue(res);
}

double
EpetraVector::Norm1() const
{
    double res;
    M_epetraVector.Norm1(&res);
    return res;
}

void
EpetraVector::Norm1(double* res) const
{
    M_epetraVector.Norm1(res);
}

malossi's avatar
malossi committed
317
318
319
320
321
322
void
EpetraVector::Norm1(double& res) const
{
    M_epetraVector.Norm1(&res);
}

simone's avatar
simone committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
double
EpetraVector::Norm2() const
{
    double res;
    M_epetraVector.Norm2(&res);
    return res;
}

void
EpetraVector::Norm2(double* res) const
{
    M_epetraVector.Norm2(res);
}

malossi's avatar
malossi committed
337
338
339
340
341
342
void
EpetraVector::Norm2( double& res ) const
{
    M_epetraVector.Norm2( &res );
}

simone's avatar
simone committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
double
EpetraVector::NormInf() const
{
    double res;
    M_epetraVector.NormInf(&res);
    return res;
}

void
EpetraVector::NormInf(double* res) const
{
    M_epetraVector.NormInf(res);
}

malossi's avatar
malossi committed
357
358
359
360
361
362
void
EpetraVector::NormInf(double& res) const
{
    M_epetraVector.NormInf(&res);
}

quinodoz's avatar
   
quinodoz committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
double
EpetraVector::MinValue() const
{
  double res;
  M_epetraVector.MinValue(&res);
  return res;
}

double
EpetraVector::MaxValue() const
{
  double res;
  M_epetraVector.MaxValue(&res);
  return res;
}

void
EpetraVector::MinValue(double* res) const
{
  M_epetraVector.MinValue(res);
}

void
EpetraVector::MaxValue(double* res) const
{
  M_epetraVector.MaxValue(res);
}

malossi's avatar
malossi committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
void
EpetraVector::MinValue(double& res) const
{
  M_epetraVector.MinValue(&res);
}

void
EpetraVector::MaxValue(double& res) const
{
  M_epetraVector.MaxValue(&res);
}

void
EpetraVector::Abs( void )
{
    M_epetraVector.Abs( M_epetraVector );
}

void
EpetraVector::Abs( EpetraVector& vector )
{
    vector.M_epetraVector.Abs( M_epetraVector );
}

// Scalar Products
EpetraVector::data_type
EpetraVector::Dot( const EpetraVector& vector ) const
{
    data_type scalarProduct;
    M_epetraVector.Dot( vector.M_epetraVector, &scalarProduct );

    return scalarProduct;
}

// Scalar Products
void
EpetraVector::Dot( const EpetraVector& vector, data_type& scalarProduct )
{
    M_epetraVector.Dot( vector.M_epetraVector, &scalarProduct );
}
simone's avatar
simone committed
431
432
433
434

void EpetraVector::spy( std::string const &filename ) const
{
    // Purpose: Matlab dumping and spy
grandper's avatar
grandper committed
435
    std::string nome = filename, uti = " , ";
simone's avatar
simone committed
436

437
438
439
440
441
442
443
444
    int  me    = M_epetraVector.Comm().MyPID();

    if (M_maptype == Repeated)
    {
        EpetraVector unique(*this, Unique, Zero);
        unique.spy(filename);
        return;
    }
simone's avatar
simone committed
445
446
447
448
449
450
451

    //
    // check on the file name
    //

    std::ostringstream myStream;
    myStream << me;
grandper's avatar
grandper committed
452
    nome = filename + ".m";
simone's avatar
simone committed
453

grandper's avatar
grandper committed
454
    EpetraExt::MultiVectorToMatlabFile(nome.c_str(), M_epetraVector);
simone's avatar
simone committed
455
456
}

malossi's avatar
malossi committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
void EpetraVector::ShowMe( std::ostream& output ) const
{
    EpetraVector redVec( *this, 0 ); // reduced vector (all at proc 0)

    if ( redVec.M_epetraVector.Comm().MyPID() )
        return; // do not need other CPUs now

    const double* Values = redVec.M_epetraVector[0];
    for ( int i = 0; i < redVec.M_epetraVector.GlobalLength () ; ++i )
        output << Values[i] << std::endl;
}



// ===================================================
//! Operators
// ===================================================
EpetraVector::data_type&
EpetraVector::operator[]( const UInt row )
{
    int lrow = BlockMap().LID(row); // BASEINDEX + 1, row + 1

    // hint: with gdb: break LifeV::EpetraVector<double>::operator[](unsigned int)
    if (lrow < 0 )
    {
        std::cout << M_epetraVector.Comm().MyPID() << " " << row << " " << lrow << std::endl;
        ERROR_MSG( "EpetraVector::operator [] ERROR : !! lrow < 0\n" );
    }

    return (M_epetraVector[0][lrow]);
}

const EpetraVector::data_type&
EpetraVector::operator[]( const UInt row ) const
{
    int lrow = BlockMap().LID(row); // BASEINDEX + 1 row+1
simone's avatar
simone committed
493

malossi's avatar
malossi committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
    if (lrow < 0 )
    {
        std::cout << M_epetraVector.Comm().MyPID() << " " << row << " " << lrow << std::endl;
        ERROR_MSG( "EpetraVector::operator () ERROR : !! lrow < 0\n" );

    }

    return (M_epetraVector[0][lrow]);
}

EpetraVector::data_type&
EpetraVector::operator()( const UInt row )
{
    return operator[](row);
}


const EpetraVector::data_type&
EpetraVector::operator()( const UInt row ) const
{
    return operator[](row);
}
simone's avatar
simone committed
516
517
518

// copies the value of a vector u. If the map is not the same,
// try to import the values.
malossi's avatar
malossi committed
519
520
EpetraVector&
EpetraVector::operator=( const EpetraVector& vector )
simone's avatar
simone committed
521
{
malossi's avatar
malossi committed
522
    if (&vector.getEpetraVector() == &this->getEpetraVector())
simone's avatar
simone committed
523
524
        return *this;

malossi's avatar
malossi committed
525
    if (BlockMap().SameAs(vector.BlockMap()) )
simone's avatar
simone committed
526
    {
malossi's avatar
malossi committed
527
        M_epetraVector = vector.getEpetraVector();
simone's avatar
simone committed
528
529
530
531
532
533
        return *this;
    }

    *this *= 0.; // because of a buggy behaviour in case of multidefined indeces.

    // vector have the same underlying EpetraMap, we then use the existing importer/exporter
malossi's avatar
malossi committed
534
535
    if ( M_epetraMap.get()  && vector.M_epetraMap.get() &&
         M_epetraMap->MapsAreSimilar( *vector.M_epetraMap ) )
simone's avatar
simone committed
536
537
    {
        // we shouldn't get here if we have the same maptype!
malossi's avatar
malossi committed
538
        assert(M_maptype != vector.M_maptype);
simone's avatar
simone committed
539
540
541

        switch (M_maptype) {
        case Unique:
542
            M_epetraVector.Export(vector.M_epetraVector, M_epetraMap->getImporter(), M_combineMode);
simone's avatar
simone committed
543
544
            return *this;
        case Repeated:
545
             M_epetraVector.Import(vector.M_epetraVector, M_epetraMap->getExporter(), M_combineMode);
simone's avatar
simone committed
546
547
548
            return *this;
        }
    }
malossi's avatar
malossi committed
549
    switch (vector.M_maptype) {
simone's avatar
simone committed
550
    case Repeated:
551
552
        //
        if (M_maptype != Repeated)
553
            return Export(vector.M_epetraVector, M_combineMode);
554
    case Unique:
555
            return Import(vector.M_epetraVector, M_combineMode);
simone's avatar
simone committed
556
557
558
559
560
561
562
563
564
    }

    // if we get here, it means that we have two different repeated maps.
    // To hande this case, we have to create a unique copy first:

    std::cout << "Tentative of export import from two repeated vectors based on different maps."
              << std::endl;

    EpetraVector vectorUnique(*M_epetraMap, Unique);
565
566
    vectorUnique.Export(vector.M_epetraVector, M_combineMode);
    M_epetraVector.Import(vectorUnique.M_epetraVector, M_epetraMap->getExporter(), M_combineMode);
simone's avatar
simone committed
567
568
569
570
571
572

    return *this;
}

//! copies the value of a Epetra_MultiVector u (assumed of width 1). If the map is not the same,
//! try to import the values. Calls Import with Add.
malossi's avatar
malossi committed
573
574
EpetraVector&
EpetraVector::operator=( const Epetra_MultiVector& vector )
simone's avatar
simone committed
575
{
malossi's avatar
malossi committed
576
    Epetra_FEVector const* feVec (dynamic_cast<Epetra_FEVector const*>(&vector));
simone's avatar
simone committed
577
578
579
580
581
    assert( feVec );

    // We hope we are guessing right
    switch (M_maptype) {
    case Unique:
582
        return Export(*feVec, M_combineMode);
simone's avatar
simone committed
583
    case Repeated:
584
        return Import(*feVec, M_combineMode);
simone's avatar
simone committed
585
586
587
588
589
    }

    return *this;
}

malossi's avatar
malossi committed
590
591
EpetraVector&
EpetraVector::operator=( data_type t )
simone's avatar
simone committed
592
{
malossi's avatar
malossi committed
593
    M_epetraVector.PutScalar(t);
simone's avatar
simone committed
594

malossi's avatar
malossi committed
595
596
597
598
599
600
601
602
603
604
    return *this;
}

// if the map is not the same, try to import values
EpetraVector&
EpetraVector::operator+=( const EpetraVector& vector )
{
    if ( this->BlockMap().SameAs( vector.BlockMap() ) )
        M_epetraVector.Update( 1., vector.M_epetraVector, 1. );
    else
simone's avatar
simone committed
605
    {
malossi's avatar
malossi committed
606
607
        EpetraVector vCopy( vector, M_maptype );
        M_epetraVector.Update( 1., vCopy.M_epetraVector, 1. );
simone's avatar
simone committed
608
609
    }

malossi's avatar
malossi committed
610
611
    return *this;
}
simone's avatar
simone committed
612

malossi's avatar
malossi committed
613
614
615
616
617
618
619
620
621
622
EpetraVector&
EpetraVector::operator-=( const EpetraVector& vector )
{
    if ( this->BlockMap().SameAs( vector.BlockMap() ) )
        M_epetraVector.Update( -1., vector.M_epetraVector, 1. );
    else
    {
        EpetraVector vCopy(vector, M_maptype);
        M_epetraVector.Update( -1., vCopy.M_epetraVector, 1. );
    }
simone's avatar
simone committed
623
624
625
626

    return *this;
}

malossi's avatar
malossi committed
627
628
629
// Element by element multiplication
EpetraVector&
EpetraVector::operator*=( const EpetraVector& vector )
simone's avatar
simone committed
630
{
malossi's avatar
malossi committed
631
632
633
634
635
636
637
638
639
640
641
642
    if ( this->BlockMap().SameAs( vector.BlockMap() ) )
        M_epetraVector.Multiply( 1.0, vector.M_epetraVector, M_epetraVector, 0.0 );
    else
    {
        EpetraVector vectorCopy( vector, M_maptype );
        M_epetraVector.Multiply( 1.0, vectorCopy.M_epetraVector, M_epetraVector, 0.0 );
    }

    return *this;
    /*
  int numMyEntries;
  const int*  gids;
simone's avatar
simone committed
643

malossi's avatar
malossi committed
644
  if (M_maptype == Unique)
simone's avatar
simone committed
645
    {
malossi's avatar
malossi committed
646
647
648
649
650
651
652
653
654
      numMyEntries = this->M_epetraVector.MyLength();
      gids  = this->BlockMap().MyGlobalElements();

      if (!this->BlockMap().SameAs(vector.BlockMap()))
    {

      EpetraVector vCopy(vector, M_maptype, Insert);
      vector = vCopy;
    }
simone's avatar
simone committed
655
656
    }

malossi's avatar
malossi committed
657
658
659
660
  if (M_maptype == Repeated)
    {
      numMyEntries = vector.M_epetraVector.MyLength();
      gids   = vector.BlockMap().MyGlobalElements();
simone's avatar
simone committed
661

malossi's avatar
malossi committed
662
663
664
665
666
667
      if (!this->BlockMap().SameAs(vector.BlockMap()))
    {
      EpetraVector vCopy(vector, M_maptype, Zero);
      vector = vCopy;
    }
    }
simone's avatar
simone committed
668

malossi's avatar
malossi committed
669
670
671
672
673
674
675
676
677

    for (int i = 0; i < numMyEntries; ++i)
    {
      (*this)[gids[i]] *= vector(gids[i]);
    }


  return *this;
  */
simone's avatar
simone committed
678
679
}

malossi's avatar
malossi committed
680
681
682
// Element by element division
EpetraVector&
EpetraVector::operator/=( const EpetraVector& vector )
simone's avatar
simone committed
683
{
malossi's avatar
malossi committed
684
685
686
687
688
689
690
    if ( this->BlockMap().SameAs( vector.BlockMap() ) )
        M_epetraVector.ReciprocalMultiply( 1.0, vector.M_epetraVector, M_epetraVector, 0.0 );
    else
    {
        EpetraVector vectorCopy( vector, M_maptype );
        M_epetraVector.ReciprocalMultiply( 1.0, vectorCopy.M_epetraVector, M_epetraVector, 0.0 );
    }
simone's avatar
simone committed
691

malossi's avatar
malossi committed
692
693
    return *this;
}
simone's avatar
simone committed
694

malossi's avatar
malossi committed
695
696
697
698
699
// Element by element sum
const EpetraVector
EpetraVector::operator+( const EpetraVector& vector ) const
{
    EpetraVector MyVectorCopy( *this );
simone's avatar
simone committed
700

malossi's avatar
malossi committed
701
702
703
    MyVectorCopy += vector;

    return MyVectorCopy;
simone's avatar
simone committed
704
705
}

malossi's avatar
malossi committed
706
707
708
// Element by element minus
const EpetraVector
EpetraVector::operator-( const EpetraVector& vector ) const
simone's avatar
simone committed
709
{
malossi's avatar
malossi committed
710
    EpetraVector MyVectorCopy( *this );
simone's avatar
simone committed
711

malossi's avatar
malossi committed
712
713
714
    MyVectorCopy -= vector;

    return MyVectorCopy;
715
716
}

malossi's avatar
malossi committed
717
718
719
// Element by element multiplication
const EpetraVector
EpetraVector::operator*( const EpetraVector& vector ) const
720
{
malossi's avatar
malossi committed
721
    EpetraVector MyVectorCopy( *this );
simone's avatar
simone committed
722

malossi's avatar
malossi committed
723
    MyVectorCopy *= vector;
simone's avatar
simone committed
724

malossi's avatar
malossi committed
725
    return MyVectorCopy;
simone's avatar
simone committed
726
727
}

malossi's avatar
malossi committed
728
729
730
731
732
// Element by element division
const EpetraVector
EpetraVector::operator/( const EpetraVector& vector ) const
{
    EpetraVector MyVectorCopy( *this );
simone's avatar
simone committed
733

malossi's avatar
malossi committed
734
735
736
737
738
739
740
741
    MyVectorCopy /= vector;

    return MyVectorCopy;
}

// Add a scalar quantity
EpetraVector&
EpetraVector::operator+=( const data_type& scalar )
simone's avatar
simone committed
742
{
malossi's avatar
malossi committed
743
744
745
746
    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            M_epetraVector[i][j] += scalar;
simone's avatar
simone committed
747
748
749
750

    return *this;
}

malossi's avatar
malossi committed
751
752
753
// Remove a scalar quantity
EpetraVector&
EpetraVector::operator-=( const data_type& scalar )
simone's avatar
simone committed
754
{
malossi's avatar
malossi committed
755
    this->operator+=( -scalar );
simone's avatar
simone committed
756
757
758
759

    return *this;
}

malossi's avatar
malossi committed
760
761
762
// Multiply by a scalar quantity
EpetraVector&
EpetraVector::operator*=( const data_type& scalar )
simone's avatar
simone committed
763
{
malossi's avatar
malossi committed
764
765
    M_epetraVector.Scale( scalar );

simone's avatar
simone committed
766
767
768
    return *this;
}

malossi's avatar
malossi committed
769
770
771
// Divide by a scalar quantity
EpetraVector&
EpetraVector::operator/=( const data_type& scalar )
772
{
malossi's avatar
malossi committed
773
    this->operator*=( 1. / scalar );
774

malossi's avatar
malossi committed
775
776
    return *this;
}
777

malossi's avatar
malossi committed
778
779
780
781
// Add a scalar quantity
const EpetraVector
EpetraVector::operator+( const data_type& scalar ) const
{
782
    EpetraVector MyVectorCopy( *this );
783

784
    MyVectorCopy += scalar;
785

786
    return MyVectorCopy;
787
788
}

malossi's avatar
malossi committed
789
790
791
// Remove a scalar quantity
const EpetraVector
EpetraVector::operator-( const data_type& scalar ) const
simone's avatar
simone committed
792
{
793
    EpetraVector MyVectorCopy( *this );
malossi's avatar
malossi committed
794

795
    MyVectorCopy -= scalar;
malossi's avatar
malossi committed
796

797
    return MyVectorCopy;
simone's avatar
simone committed
798
799
}

malossi's avatar
malossi committed
800
801
802
// Multiply by a scalar quantity
const EpetraVector
EpetraVector::operator*( const data_type& scalar ) const
simone's avatar
simone committed
803
{
804
    EpetraVector MyVectorCopy( *this );
malossi's avatar
malossi committed
805

806
    MyVectorCopy *= scalar;
malossi's avatar
malossi committed
807

808
    return MyVectorCopy;
simone's avatar
simone committed
809
810
}

malossi's avatar
malossi committed
811
812
813
814
// Divide by a scalar quantity
const EpetraVector
EpetraVector::operator/( const data_type& scalar ) const
{
815
    EpetraVector MyVectorCopy( *this );
malossi's avatar
malossi committed
816

817
    MyVectorCopy /= scalar;
malossi's avatar
malossi committed
818

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
    return MyVectorCopy;
}

//! - vector.
EpetraVector
operator-( const EpetraVector& vector )
{
    EpetraVector VectorCopy( vector );

    return VectorCopy *= static_cast<EpetraVector::data_type> ( -1.0 );
}

//! scalar + vector.
EpetraVector
operator+( const EpetraVector::data_type& scalar, const EpetraVector& vector )
{
    EpetraVector VectorCopy( vector );

    return VectorCopy += scalar;
}

//! scalar - vector.
EpetraVector
operator-( const EpetraVector::data_type& scalar, const EpetraVector& vector )
{
    EpetraVector VectorCopy( -vector );

    return VectorCopy += scalar;
malossi's avatar
malossi committed
847
}
simone's avatar
simone committed
848

849
//! scalar * vector.
simone's avatar
simone committed
850
EpetraVector
851
operator*( const EpetraVector::data_type& scalar, const EpetraVector& vector )
simone's avatar
simone committed
852
{
853
    EpetraVector VectorCopy( vector );
malossi's avatar
malossi committed
854

855
    return VectorCopy *= scalar;
simone's avatar
simone committed
856
857
}

malossi's avatar
malossi committed
858
859
860
861
862
863
864
865
866
867
// Comparison with a scalar value
EpetraVector
EpetraVector::operator==( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] == scalar ? true : false;
simone's avatar
simone committed
868

malossi's avatar
malossi committed
869
870
    return comparisonVector;
}
simone's avatar
simone committed
871

malossi's avatar
malossi committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
// Comparison with a scalar value
EpetraVector
EpetraVector::operator!=( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] != scalar ? true : false;

    return comparisonVector;
}

// Comparison with a scalar value
EpetraVector
EpetraVector::operator<( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] < scalar ? true : false;

    return comparisonVector;
}

// Comparison with a scalar value
EpetraVector
EpetraVector::operator>( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] > scalar ? true : false;

    return comparisonVector;
}

// Comparison with a scalar value
EpetraVector
EpetraVector::operator<=( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] <= scalar ? true : false;

    return comparisonVector;
}

// Comparison with a scalar value
EpetraVector
EpetraVector::operator>=( const Real& scalar )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] >= scalar ? true : false;

    return comparisonVector;
}

// Logical comparison
EpetraVector
EpetraVector::operator&&( const EpetraVector& vector )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );
simone's avatar
simone committed
947

malossi's avatar
malossi committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] && vector.M_epetraVector[i][j];

    return comparisonVector;
}

// Logical comparison
EpetraVector
EpetraVector::operator||( const EpetraVector& vector )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );

    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = M_epetraVector[i][j] || vector.M_epetraVector[i][j];

    return comparisonVector;
}

// Logical comparison
EpetraVector
EpetraVector::operator!( void )
{
    EpetraVector comparisonVector( *M_epetraMap, M_maptype );
simone's avatar
simone committed
975

malossi's avatar
malossi committed
976
977
978
979
980
981
982
983
984
    int i, j;
    for ( i=0; i < M_epetraVector.NumVectors(); ++i )
        for ( j=0; j < M_epetraVector.MyLength(); ++j )
            comparisonVector.M_epetraVector[i][j] = !M_epetraVector[i][j];

    return comparisonVector;
}


985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ===================================================
//! Set Methods
// ===================================================
void
EpetraVector::setCombineMode( Epetra_CombineMode combineMode )
{
    M_combineMode = combineMode;
}

void
EpetraVector::setDefaultCombineMode( )
{
    setCombineMode(Add);
}


For faster browsing, not all history is shown. View entire blame