LinearEpetraOperator.hpp 9.04 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
37
38
39
*******************************************************************************

    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"
    which work with LifeV::EpetraVector instead of Epetra_MultiVector.
    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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


#include <life/lifecore/life.hpp>

#include <Epetra_Comm.h>
#include <Epetra_Map.h>
#include <Epetra_Operator.h>
#include <Epetra_MultiVector.h>

#include <life/lifearray/EpetraVector.hpp>

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,
 * plus it supports directly the LifeV::EpetraVector data.
 *
 * Two concrete methods are implemented in LinearOperator
 * int apply(const EpetraVector &X, EpetraVector & Y) const ;
 * int applyInverse(const EpetraVector &X, EpetraVector &Y) const.
 *
 *
 * Such methods extract a raw Epetra_MultiVector from the EpetraVector and then call the virtual methods
 * 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
85
86
87
88
89
90
91
92
93
94
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
131
    //! @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;

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

    \return Integer error code, set to 0 if successful.
    */
    inline int apply(const EpetraVector & X, EpetraVector & Y) const
    {
132
        return Apply(X.epetraVector(), Y.epetraVector());
Tiziano Passerini's avatar
Tiziano Passerini committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    }

    //! 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;

    //! Returns the result of a LinearOperator inverse applied to an EpetraVector X in Y.
    /*!
    \param In
       X - A EpetraVector to solve for.
    \param Out
       Y -A EpetraVector 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.
    */

    inline int applyInverse(const EpetraVector & X, EpetraVector Y)
    {
164
        return ApplyInverse(X.epetraVector(), Y.epetraVector());
Tiziano Passerini's avatar
Tiziano Passerini committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    }

    //! 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
197

Tiziano Passerini's avatar
Tiziano Passerini committed
198
};
uvilla's avatar
uvilla committed
199
200
201
202
203
204

//! @class IdentityOperator
/*! @brief Identity operator x = I*x. */
class IdentityOperator : public LinearOperator
{
public:
Tiziano Passerini's avatar
Tiziano Passerini committed
205
206
207
208
209
210
211
212
213
214
215
216
    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
217
private:
Tiziano Passerini's avatar
Tiziano Passerini committed
218
219
220
    std::string M_name;
    boost::shared_ptr<Epetra_Map> M_map;
    bool M_useTranspose;
uvilla's avatar
uvilla committed
221
222
223
224
225
226
227
};

//! @class NullOperator
/*! @brief Null operator 0 = Z*x. */
class NullOperator : public LinearOperator
{
public:
Tiziano Passerini's avatar
Tiziano Passerini committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    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;}
    int ApplyInverse(const Epetra_MultiVector & /*X*/, Epetra_MultiVector & Y) const {Y.PutScalar(1.0/0.0); return -1;}
    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
245
private:
Tiziano Passerini's avatar
Tiziano Passerini committed
246
247
248
249
    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
250
251
252
253
};

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