Loading...
Searching...
No Matches
PETScVector.h
// Copyright (C) 2004-2016 Johan Hoffman, Johan Jansson, Anders Logg
// and Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN 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.
//
// DOLFIN 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 DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Garth N. Wells, 2005-2010.
// Modified by Kent-Andre Mardal, 2008.
// Modified by Ola Skavhaug, 2008.
// Modified by Martin Alnæs, 2008.
// Modified by Fredrik Valdmanis, 2011.
#ifndef __DOLFIN_PETSC_VECTOR_H
#define __DOLFIN_PETSC_VECTOR_H
#ifdef HAS_PETSC
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <petscsys.h>
#include <petscvec.h>
#include <dolfin/log/log.h>
#include <dolfin/common/types.h>
#include "GenericVector.h"
#include "PETScObject.h"
namespace dolfin
{
class SparsityPattern;
template<typename T> class Array;
class PETScVector : public GenericVector, public PETScObject
{
public:
PETScVector();
explicit PETScVector(MPI_Comm comm);
PETScVector(MPI_Comm comm, std::size_t N);
explicit PETScVector(const SparsityPattern& sparsity_pattern);
PETScVector(const PETScVector& x);
explicit PETScVector(Vec x);
virtual ~PETScVector();
//--- Implementation of the GenericTensor interface ---
virtual void zero();
virtual void apply(std::string mode);
virtual MPI_Comm mpi_comm() const;
virtual std::string str(bool verbose) const;
//--- Implementation of the GenericVector interface ---
virtual std::shared_ptr<GenericVector> copy() const;
virtual void init(std::size_t N);
virtual void init(std::pair<std::size_t, std::size_t> range);
virtual void init(std::pair<std::size_t, std::size_t> range,
const std::vector<std::size_t>& local_to_global_map,
const std::vector<la_index>& ghost_indices);
// Bring init function from GenericVector into scope
using GenericVector::init;
virtual bool empty() const;
virtual std::size_t size() const;
virtual std::size_t local_size() const;
virtual std::pair<std::int64_t, std::int64_t> local_range() const;
virtual bool owns_index(std::size_t i) const;
virtual void get(double* block, std::size_t m,
const dolfin::la_index* rows) const;
virtual void get_local(double* block, std::size_t m,
const dolfin::la_index* rows) const;
virtual void set(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void set_local(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void add(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void add_local(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void get_local(std::vector<double>& values) const;
virtual void set_local(const std::vector<double>& values);
virtual void add_local(const Array<double>& values);
virtual void gather(GenericVector& y,
const std::vector<dolfin::la_index>& indices) const;
virtual void gather(std::vector<double>& x,
const std::vector<dolfin::la_index>& indices) const;
virtual void gather_on_zero(std::vector<double>& x) const;
virtual void axpy(double a, const GenericVector& x);
virtual void abs();
virtual double inner(const GenericVector& v) const;
virtual double norm(std::string norm_type) const;
virtual double min() const;
virtual double max() const;
virtual double sum() const;
virtual double sum(const Array<std::size_t>& rows) const;
virtual const PETScVector& operator*= (double a);
virtual const PETScVector& operator*= (const GenericVector& x);
virtual const PETScVector& operator/= (double a);
virtual const PETScVector& operator+= (const GenericVector& x);
virtual const PETScVector& operator+= (double a);
virtual const PETScVector& operator-= (const GenericVector& x);
virtual const PETScVector& operator-= (double a);
virtual const GenericVector& operator= (const GenericVector& x);
virtual const PETScVector& operator= (double a);
virtual void update_ghost_values();
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const;
//--- Special PETSc functions ---
void set_options_prefix(std::string options_prefix);
std::string get_options_prefix() const;
void set_from_options();
Vec vec() const;
const PETScVector& operator= (const PETScVector& x);
void reset(Vec vec);
friend class PETScNestMatrix;
private:
// Initialise PETSc vector
void _init(std::pair<std::size_t, std::size_t> range,
const std::vector<std::size_t>& local_to_global_map,
const std::vector<la_index>& ghost_indices);
// PETSc Vec pointer
Vec _x;
// PETSc norm types
static const std::map<std::string, NormType> norm_types;
};
}
#endif
#endif
dolfin::Array
Definition Array.h:42
dolfin::GenericLinearAlgebraFactory
Base class for LinearAlgebra factories.
Definition GenericLinearAlgebraFactory.h:47
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::GenericVector::init
virtual void init(const TensorLayout &tensor_layout)
Definition GenericVector.h:58
dolfin::PETScObject::PETScObject
PETScObject()
Constructor. Ensures that PETSc has been initialised.
Definition PETScObject.h:38
dolfin::PETScVector::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows) const
Definition PETScVector.cpp:237
dolfin::PETScVector::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return ownership range of a vector.
Definition PETScVector.cpp:357
dolfin::PETScVector::~PETScVector
virtual ~PETScVector()
Destructor.
Definition PETScVector.cpp:95
dolfin::PETScVector::inner
virtual double inner(const GenericVector &v) const
Return inner product with given vector.
Definition PETScVector.cpp:521
dolfin::PETScVector::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition PETScVector.cpp:661
dolfin::PETScVector::update_ghost_values
virtual void update_ghost_values()
Update values shared from remote processes.
Definition PETScVector.cpp:425
dolfin::PETScVector::sum
virtual double sum() const
Return sum of values of vector.
Definition PETScVector.cpp:599
dolfin::PETScVector::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using global indices.
Definition PETScVector.cpp:248
dolfin::PETScVector::owns_index
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition PETScVector.cpp:368
dolfin::PETScVector::copy
virtual std::shared_ptr< GenericVector > copy() const
Return copy of vector.
Definition PETScVector.cpp:101
dolfin::PETScVector::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition PETScVector.cpp:815
dolfin::PETScVector::set_from_options
void set_from_options()
Definition PETScVector.cpp:849
dolfin::PETScVector::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition PETScVector.cpp:303
dolfin::PETScVector::init
virtual void init(std::size_t N)
Initialize vector to global size N.
Definition PETScVector.cpp:106
dolfin::PETScVector::get_options_prefix
std::string get_options_prefix() const
Definition PETScVector.cpp:834
dolfin::PETScVector::axpy
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation).
Definition PETScVector.cpp:532
dolfin::PETScVector::apply
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition PETScVector.cpp:280
dolfin::PETScVector::operator=
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition PETScVector.cpp:375
dolfin::PETScVector::set_options_prefix
void set_options_prefix(std::string options_prefix)
Definition PETScVector.cpp:820
dolfin::PETScVector::operator*=
virtual const PETScVector & operator*=(double a)
Multiply vector by given number.
Definition PETScVector.cpp:479
dolfin::PETScVector::set_local
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
dolfin::PETScVector::reset
void reset(Vec vec)
Definition PETScVector.cpp:867
dolfin::PETScVector::size
virtual std::size_t size() const
Return size of vector.
Definition PETScVector.cpp:317
dolfin::PETScVector::min
virtual double min() const
Return minimum value of vector.
Definition PETScVector.cpp:579
dolfin::PETScVector::PETScVector
PETScVector()
Create empty vector (on MPI_COMM_WORLD).
Definition PETScVector.cpp:48
dolfin::PETScVector::add_local
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
dolfin::PETScVector::norm
virtual double norm(std::string norm_type) const
Return norm of vector.
Definition PETScVector.cpp:562
dolfin::PETScVector::operator+=
virtual const PETScVector & operator+=(const GenericVector &x)
Add given vector.
Definition PETScVector.cpp:448
dolfin::PETScVector::add
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
dolfin::PETScVector::gather
virtual void gather(GenericVector &y, const std::vector< dolfin::la_index > &indices) const
Definition PETScVector.cpp:705
dolfin::PETScVector::empty
virtual bool empty() const
Return true if vector is empty.
Definition PETScVector.cpp:312
dolfin::PETScVector::get_local
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
dolfin::PETScVector::abs
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition PETScVector.cpp:552
dolfin::PETScVector::operator/=
virtual const PETScVector & operator/=(double a)
Divide vector by given number.
Definition PETScVector.cpp:512
dolfin::PETScVector::max
virtual double max() const
Return maximum value of vector.
Definition PETScVector.cpp:589
dolfin::PETScVector::operator-=
virtual const PETScVector & operator-=(const GenericVector &x)
Subtract given vector.
Definition PETScVector.cpp:466
dolfin::PETScVector::mpi_comm
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition PETScVector.cpp:294
dolfin::PETScVector::vec
Vec vec() const
Return pointer to PETSc Vec object.
Definition PETScVector.cpp:862
dolfin::PETScVector::gather_on_zero
virtual void gather_on_zero(std::vector< double > &x) const
Definition PETScVector.cpp:786
dolfin::PETScVector::local_size
virtual std::size_t local_size() const
Return local size of vector.
Definition PETScVector.cpp:338
dolfin::SparsityPattern
Definition SparsityPattern.h:46
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32