DOLFIN
DOLFIN C++ interface
PETScVector.h
1 // Copyright (C) 2004-2016 Johan Hoffman, Johan Jansson, Anders Logg
2 // and Garth N. Wells
3 //
4 // This file is part of DOLFIN.
5 //
6 // DOLFIN is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // DOLFIN is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // Modified by Garth N. Wells, 2005-2010.
20 // Modified by Kent-Andre Mardal, 2008.
21 // Modified by Ola Skavhaug, 2008.
22 // Modified by Martin Alnæs, 2008.
23 // Modified by Fredrik Valdmanis, 2011.
24 
25 #ifndef __DOLFIN_PETSC_VECTOR_H
26 #define __DOLFIN_PETSC_VECTOR_H
27 
28 #ifdef HAS_PETSC
29 
30 #include <cstdint>
31 #include <map>
32 #include <memory>
33 #include <string>
34 #include <unordered_map>
35 #include <utility>
36 
37 #include <petscsys.h>
38 #include <petscvec.h>
39 
40 #include <dolfin/log/log.h>
41 #include <dolfin/common/types.h>
42 #include "GenericVector.h"
43 #include "PETScObject.h"
44 
45 namespace dolfin
46 {
47 
48  class SparsityPattern;
49  template<typename T> class Array;
50 
59 
60  class PETScVector : public GenericVector, public PETScObject
61  {
62  public:
63 
65  PETScVector();
66 
68  explicit PETScVector(MPI_Comm comm);
69 
71  PETScVector(MPI_Comm comm, std::size_t N);
72 
74  explicit PETScVector(const SparsityPattern& sparsity_pattern);
75 
77  PETScVector(const PETScVector& x);
78 
82  explicit PETScVector(Vec x);
83 
85  virtual ~PETScVector();
86 
87  //--- Implementation of the GenericTensor interface ---
88 
90  virtual void zero();
91 
93  virtual void apply(std::string mode);
94 
96  virtual MPI_Comm mpi_comm() const;
97 
99  virtual std::string str(bool verbose) const;
100 
101  //--- Implementation of the GenericVector interface ---
102 
104  virtual std::shared_ptr<GenericVector> copy() const;
105 
107  virtual void init(std::size_t N);
108 
110  virtual void init(std::pair<std::size_t, std::size_t> range);
111 
114  virtual void init(std::pair<std::size_t, std::size_t> range,
115  const std::vector<std::size_t>& local_to_global_map,
116  const std::vector<la_index>& ghost_indices);
117 
118  // Bring init function from GenericVector into scope
119  using GenericVector::init;
120 
122  virtual bool empty() const;
123 
125  virtual std::size_t size() const;
126 
128  virtual std::size_t local_size() const;
129 
131  virtual std::pair<std::int64_t, std::int64_t> local_range() const;
132 
134  virtual bool owns_index(std::size_t i) const;
135 
138  virtual void get(double* block, std::size_t m,
139  const dolfin::la_index* rows) const;
140 
142  virtual void get_local(double* block, std::size_t m,
143  const dolfin::la_index* rows) const;
144 
146  virtual void set(const double* block, std::size_t m,
147  const dolfin::la_index* rows);
148 
150  virtual void set_local(const double* block, std::size_t m,
151  const dolfin::la_index* rows);
152 
154  virtual void add(const double* block, std::size_t m,
155  const dolfin::la_index* rows);
156 
158  virtual void add_local(const double* block, std::size_t m,
159  const dolfin::la_index* rows);
160 
162  virtual void get_local(std::vector<double>& values) const;
163 
165  virtual void set_local(const std::vector<double>& values);
166 
168  virtual void add_local(const Array<double>& values);
169 
174  virtual void gather(GenericVector& y,
175  const std::vector<dolfin::la_index>& indices) const;
176 
179  virtual void gather(std::vector<double>& x,
180  const std::vector<dolfin::la_index>& indices) const;
181 
184  virtual void gather_on_zero(std::vector<double>& x) const;
185 
187  virtual void axpy(double a, const GenericVector& x);
188 
190  virtual void abs();
191 
193  virtual double inner(const GenericVector& v) const;
194 
196  virtual double norm(std::string norm_type) const;
197 
199  virtual double min() const;
200 
202  virtual double max() const;
203 
205  virtual double sum() const;
206 
208  virtual double sum(const Array<std::size_t>& rows) const;
209 
211  virtual const PETScVector& operator*= (double a);
212 
214  virtual const PETScVector& operator*= (const GenericVector& x);
215 
217  virtual const PETScVector& operator/= (double a);
218 
220  virtual const PETScVector& operator+= (const GenericVector& x);
221 
223  virtual const PETScVector& operator+= (double a);
224 
226  virtual const PETScVector& operator-= (const GenericVector& x);
227 
229  virtual const PETScVector& operator-= (double a);
230 
232  virtual const GenericVector& operator= (const GenericVector& x);
233 
235  virtual const PETScVector& operator= (double a);
236 
238  virtual void update_ghost_values();
239 
240  //--- Special functions ---
241 
243  virtual GenericLinearAlgebraFactory& factory() const;
244 
245  //--- Special PETSc functions ---
246 
249  void set_options_prefix(std::string options_prefix);
250 
253  std::string get_options_prefix() const;
254 
257  void set_from_options();
258 
260  Vec vec() const;
261 
263  const PETScVector& operator= (const PETScVector& x);
264 
267  void reset(Vec vec);
268 
269  private:
270 
271  // Initialise PETSc vector
272  void _init(std::pair<std::size_t, std::size_t> range,
273  const std::vector<std::size_t>& local_to_global_map,
274  const std::vector<la_index>& ghost_indices);
275 
276  // PETSc Vec pointer
277  Vec _x;
278 
279  // PETSc norm types
280  static const std::map<std::string, NormType> norm_types;
281 
282  };
283 
284 }
285 
286 #endif
287 
288 #endif
Definition: PETScVector.h:60
virtual void init(const TensorLayout &tensor_layout)
Definition: GenericVector.h:58
virtual void gather(GenericVector &y, const std::vector< dolfin::la_index > &indices) const
Definition: PETScVector.cpp:699
virtual std::size_t size() const
Return size of vector.
Definition: PETScVector.cpp:317
void set_from_options()
Definition: PETScVector.cpp:847
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation)
Definition: PETScVector.cpp:532
virtual double sum() const
Return sum of values of vector.
Definition: PETScVector.cpp:599
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: PETScVector.cpp:294
Definition: SparsityPattern.h:45
Definition: adapt.h:29
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition: PETScVector.cpp:552
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition: PETScVector.cpp:375
Definition: Array.h:41
PETScVector()
Create empty vector (on MPI_COMM_WORLD)
Definition: PETScVector.cpp:48
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: PETScVector.cpp:813
virtual double min() const
Return minimum value of vector.
Definition: PETScVector.cpp:579
virtual double inner(const GenericVector &v) const
Return inner product with given vector.
Definition: PETScVector.cpp:521
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: PETScVector.cpp:303
virtual const PETScVector & operator*=(double a)
Multiply vector by given number.
Definition: PETScVector.cpp:479
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using global indices.
Definition: PETScVector.cpp:264
virtual const PETScVector & operator+=(const GenericVector &x)
Add given vector.
Definition: PETScVector.cpp:448
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using local indices.
Definition: PETScVector.cpp:272
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return ownership range of a vector.
Definition: PETScVector.cpp:357
void reset(Vec vec)
Definition: PETScVector.cpp:865
void set_options_prefix(std::string options_prefix)
Definition: PETScVector.cpp:818
virtual std::shared_ptr< GenericVector > copy() const
Return copy of vector.
Definition: PETScVector.cpp:101
virtual const PETScVector & operator-=(const GenericVector &x)
Subtract given vector.
Definition: PETScVector.cpp:466
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const
Get block of values using local indices.
Definition: PETScVector.cpp:196
Vec vec() const
Return pointer to PETSc Vec object.
Definition: PETScVector.cpp:860
virtual std::size_t local_size() const
Return local size of vector.
Definition: PETScVector.cpp:338
virtual ~PETScVector()
Destructor.
Definition: PETScVector.cpp:95
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual double max() const
Return maximum value of vector.
Definition: PETScVector.cpp:589
Definition: PETScObject.h:33
virtual void update_ghost_values()
Update values shared from remote processes.
Definition: PETScVector.cpp:425
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: PETScVector.cpp:661
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using local indices.
Definition: PETScVector.cpp:256
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition: PETScVector.cpp:280
virtual void gather_on_zero(std::vector< double > &x) const
Definition: PETScVector.cpp:784
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition: PETScVector.cpp:368
virtual bool empty() const
Return true if vector is empty.
Definition: PETScVector.cpp:312
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual double norm(std::string norm_type) const
Return norm of vector.
Definition: PETScVector.cpp:562
virtual void init(std::size_t N)
Initialize vector to global size N.
Definition: PETScVector.cpp:106
std::string get_options_prefix() const
Definition: PETScVector.cpp:832
virtual const PETScVector & operator/=(double a)
Divide vector by given number.
Definition: PETScVector.cpp:512