LinearEpetraOperator.hpp 9.47 KB
Newer Older
Tiziano Passerini's avatar
Tiziano Passerini committed
1
//@HEADER
uvilla's avatar
uvilla committed
2
/*
Tiziano Passerini's avatar
Tiziano Passerini committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
*******************************************************************************

    Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
    Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University

    This file is part of LifeV.

    LifeV is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    LifeV 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with LifeV.  If not, see <http://www.gnu.org/licenses/>.

*******************************************************************************
*/
//@HEADER

/*!
    @file
    @brief Some linear operators which inherit from Epetra_Operator

    @author Umberto Villa <uvilla.@emory.edu>

    @date 03-09-2010

    In this file we define a Lifev::Operators:LinearOperator which inherit from Epetra_Operator.
    LinearOperator adds to the interface of Epetra_Operator a method "Apply" and "ApplyInverse"
37
    which work with LifeV::VectorEpetra instead of Epetra_MultiVector.
Tiziano Passerini's avatar
Tiziano Passerini committed
38
39
    LinearOperator is an abstract class with virtual public interface.
    IdentityOperator and NullOperator are defined in this file and both inherit from LinearOperator.
uvilla's avatar
uvilla committed
40
41
 */

42
43
#ifndef LINEAREPETRAOPERATOR_H
#define LINEAREPETRAOPERATOR_H 1
uvilla's avatar
uvilla committed
44
45


Simone Deparis's avatar
Simone Deparis committed
46
#include <life/lifecore/LifeV.hpp>
uvilla's avatar
uvilla committed
47

uvilla's avatar
uvilla committed
48
49
50
51
52
// Tell the compiler to ignore specific kind of warnings:
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wextra"

uvilla's avatar
uvilla committed
53
54
55
56
57
#include <Epetra_Comm.h>
#include <Epetra_Map.h>
#include <Epetra_Operator.h>
#include <Epetra_MultiVector.h>

uvilla's avatar
uvilla committed
58
59
60
61
62
// Tell the compiler to ignore specific kind of warnings:
#pragma GCC diagnostic warning "-Wunused-variable"
#pragma GCC diagnostic warning "-Wunused-parameter"
#pragma GCC diagnostic warning "-Wextra"

63
#include <life/lifearray/VectorEpetra.hpp>
uvilla's avatar
uvilla committed
64
65
66
67
68
69
70
71
72
73
74
75

namespace LifeV
{
namespace Operators
{
//! @class LinearOperator
/*! @brief Abstract class which defines the interface of a Linear Operator.
 *
 * LinearOperator is an abstract which inherits from Epetra_Operator.
 * LinearOperator should be the base class for all the LifeV class which implements a linear operator.
 *
 * LinearOperator ensures perfect compatibility with all the Trilinos solvers,
76
 * plus it supports directly the LifeV::VectorEpetra data.
uvilla's avatar
uvilla committed
77
78
 *
 * Two concrete methods are implemented in LinearOperator
79
80
 * int apply(const VectorEpetra &X, VectorEpetra & Y) const ;
 * int applyInverse(const VectorEpetra &X, VectorEpetra &Y) const.
uvilla's avatar
uvilla committed
81
82
 *
 *
83
 * Such methods extract a raw Epetra_MultiVector from the VectorEpetra and then call the virtual methods
uvilla's avatar
uvilla committed
84
85
86
87
88
89
90
91
92
93
94
 * Int Apply(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const
 * or
 * Int ApplyInverse(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const
 * respectively.
 *
 *
 */
class LinearOperator : public Epetra_Operator
{
public:

Tiziano Passerini's avatar
Tiziano Passerini committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    //! @name Destructor
    //@{
    //! Destructor
    virtual ~LinearOperator() {};
    //@}

    //! @name Attribute set methods
    //@{

    //! If set true, transpose of this operator will be applied.
    /*! This flag allows the transpose of the given operator to be used implicitly.  Setting this flag
        affects only the Apply() and ApplyInverse() methods.  If the implementation of this interface
    does not support transpose use, this method should return a value of -1.

    \param In
       UseTranspose -If true, multiply by the transpose of operator, otherwise just use operator.

    \return Integer error code, set to 0 if successful.  Set to -1 if this implementation does not support transpose.
    */
    virtual int SetUseTranspose(bool UseTranspose) = 0;
    //@}

    //! @name Mathematical functions
    //@{

    //! Returns the result of a Epetra_Operator applied to a Epetra_MultiVector X in Y.
    /*!
    \param In
       X - A Epetra_MultiVector of dimension NumVectors to multiply with matrix.
    \param Out
       Y -A Epetra_MultiVector of dimension NumVectors containing result.

    \return Integer error code, set to 0 if successful.
    */
    virtual int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const = 0;

131
    //! Returns the result of a LinearOperator applied to a VectorEpetra X in Y.
Tiziano Passerini's avatar
Tiziano Passerini committed
132
133
    /*!
    \param In
134
       X - A VectorEpetra to multiply with matrix.
Tiziano Passerini's avatar
Tiziano Passerini committed
135
    \param Out
136
       Y -A VectorEpetra containing result.
Tiziano Passerini's avatar
Tiziano Passerini committed
137
138
139

    \return Integer error code, set to 0 if successful.
    */
140
    inline int apply(const VectorEpetra & X, VectorEpetra & Y) const
Tiziano Passerini's avatar
Tiziano Passerini committed
141
    {
142
        return Apply(X.epetraVector(), Y.epetraVector());
Tiziano Passerini's avatar
Tiziano Passerini committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    }

    //! Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y.
    /*!
    \param In
       X - A Epetra_MultiVector of dimension NumVectors to solve for.
    \param Out
       Y -A Epetra_MultiVector of dimension NumVectors containing result.

    \return Integer error code, set to 0 if successful.

    \warning In order to work with AztecOO, any implementation of this method must
              support the case where X and Y are the same object.
    */
    virtual int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const = 0;

159
    //! Returns the result of a LinearOperator inverse applied to an VectorEpetra X in Y.
Tiziano Passerini's avatar
Tiziano Passerini committed
160
161
    /*!
    \param In
162
       X - A VectorEpetra to solve for.
Tiziano Passerini's avatar
Tiziano Passerini committed
163
    \param Out
164
       Y -A VectorEpetra containing result.
Tiziano Passerini's avatar
Tiziano Passerini committed
165
166
167
168
169
170
171

    \return Integer error code, set to 0 if successful.

    \warning In order to work with AztecOO, any implementation of this method must
              support the case where X and Y are the same object.
    */

172
    inline int applyInverse(const VectorEpetra & X, VectorEpetra Y)
Tiziano Passerini's avatar
Tiziano Passerini committed
173
    {
174
        return ApplyInverse(X.epetraVector(), Y.epetraVector());
Tiziano Passerini's avatar
Tiziano Passerini committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    }

    //! Returns the infinity norm of the global matrix.
    /* Returns the quantity \f$ \| A \|_\infty\f$ such that
       \f[\| A \|_\infty = \max_{1\lei\lem} \sum_{j=1}^n |a_{ij}| \f].

       \warning This method must not be called unless HasNormInf() returns true.
    */
    virtual double NormInf() const = 0;
    //@}

    //! @name Attribute access functions
    //@{

    //! Returns a character string describing the operator
    virtual const char * Label() const = 0;

    //! Returns the current UseTranspose setting.
    virtual bool UseTranspose() const = 0;

    //! Returns true if the \e this object can provide an approximate Inf-norm, false otherwise.
    virtual bool HasNormInf() const = 0;

    //! Returns a pointer to the Epetra_Comm communicator associated with this operator.
    virtual const Epetra_Comm & Comm() const = 0;

    //! Returns the Epetra_Map object associated with the domain of this operator.
    virtual const Epetra_Map & OperatorDomainMap() const = 0;

    //! Returns the Epetra_Map object associated with the range of this operator.
    virtual const Epetra_Map & OperatorRangeMap() const = 0;
    //@}
uvilla's avatar
uvilla committed
207

Tiziano Passerini's avatar
Tiziano Passerini committed
208
};
uvilla's avatar
uvilla committed
209
210
211
212
213
214

//! @class IdentityOperator
/*! @brief Identity operator x = I*x. */
class IdentityOperator : public LinearOperator
{
public:
Tiziano Passerini's avatar
Tiziano Passerini committed
215
216
217
218
219
220
221
222
223
224
225
226
    IdentityOperator():M_name("identity"), M_useTranspose(false) {};
    void setUp(const boost::shared_ptr<Epetra_Map> & map) {M_map = map;}
    int SetUseTranspose(bool useTranspose) {M_useTranspose = useTranspose; return 0; }
    int Apply(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const {Y = X; return 0;}
    int ApplyInverse(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const {Y=X; return 0;}
    double NormInf() const {return 1.0;}
    const char * Label() const {return M_name.c_str();}
    bool UseTranspose() const {return M_useTranspose;}
    bool HasNormInf() const {return true;}
    const Epetra_Comm & Comm() const {return M_map->Comm();}
    const Epetra_Map & OperatorDomainMap() const {return *M_map;}
    const Epetra_Map & OperatorRangeMap()  const {return *M_map;}
uvilla's avatar
uvilla committed
227
private:
Tiziano Passerini's avatar
Tiziano Passerini committed
228
229
230
    std::string M_name;
    boost::shared_ptr<Epetra_Map> M_map;
    bool M_useTranspose;
uvilla's avatar
uvilla committed
231
232
233
234
235
236
237
};

//! @class NullOperator
/*! @brief Null operator 0 = Z*x. */
class NullOperator : public LinearOperator
{
public:
Tiziano Passerini's avatar
Tiziano Passerini committed
238
239
240
241
242
243
244
245
246
    NullOperator():M_name("Null Operator"), M_useTranspose(false) {};
    void setUp(const boost::shared_ptr<Epetra_Map> & domainMap,
               const boost::shared_ptr<Epetra_Map> &  rangeMap)
    {
        M_domainMap = domainMap;
        M_rangeMap = rangeMap;
    }
    int SetUseTranspose(bool useTranspose) {M_useTranspose = useTranspose; return 0; }
    int Apply(const Epetra_MultiVector & /*X*/, Epetra_MultiVector & Y) const {Y.PutScalar(0.0); return 0;}
247
248
    int ApplyInverse(const Epetra_MultiVector & /*X*/, Epetra_MultiVector & Y) const
    			{Y.PutScalar(std::numeric_limits<Real>::quiet_NaN( )); return -1;}
Tiziano Passerini's avatar
Tiziano Passerini committed
249
250
251
252
253
254
255
    double NormInf() const {return 0.0;}
    const char * Label() const {return M_name.c_str();}
    bool UseTranspose() const {return M_useTranspose;}
    bool HasNormInf() const {return true;}
    const Epetra_Comm & Comm() const {return M_rangeMap->Comm();}
    const Epetra_Map & OperatorDomainMap() const {return *M_domainMap;}
    const Epetra_Map & OperatorRangeMap()  const {return *M_rangeMap;}
uvilla's avatar
uvilla committed
256
private:
Tiziano Passerini's avatar
Tiziano Passerini committed
257
258
259
260
    std::string M_name;
    boost::shared_ptr<Epetra_Map> M_domainMap;
    boost::shared_ptr<Epetra_Map> M_rangeMap;
    bool M_useTranspose;
uvilla's avatar
uvilla committed
261
262
263
264
};

} /*end namespace Operators*/
} /*end namespace */
265
#endif /* LINEAREPETRAOPERATOR_H */