DOLFIN
DOLFIN C++ interface
Scalar.h
1 // Copyright (C) 2007-2014 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Garth N. Wells, 2007-2015.
19 // Modified by Ola Skavhaug, 2007.
20 // Modified by Martin Alnaes, 2014.
21 
22 #ifndef __SCALAR_H
23 #define __SCALAR_H
24 
25 #include <string>
26 #include <vector>
27 #include <dolfin/common/MPI.h>
28 #include <dolfin/common/SubSystemsManager.h>
29 #include <dolfin/common/types.h>
30 #include "DefaultFactory.h"
31 #include "GenericTensor.h"
32 #include "TensorLayout.h"
33 
34 namespace dolfin
35 {
36 
37  class TensorLayout;
38 
41 
42  class Scalar : public GenericTensor
43  {
44  public:
45 
47  Scalar() : Scalar(MPI_COMM_WORLD) {}
48 
50  Scalar(MPI_Comm comm) : GenericTensor(), _value(0.0), _local_increment(0.0),
51  _mpi_comm(comm) {}
52 
54  virtual ~Scalar() {}
55 
56  //--- Implementation of the GenericTensor interface ---
57 
59  virtual void init(const TensorLayout& tensor_layout)
60  {
61  _value = 0.0;
62  _local_increment = 0.0;
63  _mpi_comm.reset(tensor_layout.mpi_comm());
64  }
65 
67  virtual bool empty() const
68  { return false; }
69 
71  virtual std::size_t rank() const
72  { return 0; }
73 
75  virtual std::size_t size(std::size_t dim) const
76  {
77  // TODO: This is inconsistent in two ways:
78  // - tensor.size(i) is defined for i < tensor.rank(), so not at all for a Scalar.
79  // - the number of components of a tensor is the product of the sizes, returning 0 here makes no sense.
80  // Is this used for anything? If yes, consider fixing that code. If no, just make this an error for any dim.
81 
82  if (dim != 0)
83  {
84  dolfin_error("Scalar.h",
85  "get size of scalar",
86  "Dim must be equal to zero.");
87  }
88 
89  return 0;
90  }
91 
93  virtual std::pair<std::int64_t, std::int64_t>
94  local_range(std::size_t dim) const
95  {
96  dolfin_error("Scalar.h",
97  "get local range of scalar",
98  "The local_range() function is not available for scalars");
99  return {0, 0};
100  }
101 
103  virtual void get(double* block, const dolfin::la_index* num_rows,
104  const dolfin::la_index * const * rows) const
105  {
106  dolfin_error("Scalar.h",
107  "get global value of scalar",
108  "The get() function is not available for scalars");
109  }
110 
112  virtual void set(const double* block, const dolfin::la_index* num_rows,
113  const dolfin::la_index * const * rows)
114  {
115  dolfin_error("Scalar.h",
116  "set global value of scalar",
117  "The set() function is not available for scalars");
118  }
119 
121  virtual void set_local(const double* block, const dolfin::la_index* num_rows,
122  const dolfin::la_index * const * rows)
123  {
124  dolfin_error("Scalar.h",
125  "set local value of scalar",
126  "The set_local() function is not available for scalars");
127  }
128 
130  virtual void add(const double* block, const dolfin::la_index* num_rows,
131  const dolfin::la_index * const * rows)
132  {
133  dolfin_assert(block);
134  _local_increment += block[0];
135  }
136 
138  virtual void add_local(const double* block, const dolfin::la_index* num_rows,
139  const dolfin::la_index * const * rows)
140  {
141  dolfin_assert(block);
142  _local_increment += block[0];
143  }
144 
146  virtual void add(const double* block,
147  const std::vector<ArrayView<const dolfin::la_index>>& rows)
148  {
149  dolfin_assert(block);
150  _local_increment += block[0];
151  }
152 
154  virtual void add_local(const double* block,
155  const std::vector<ArrayView<const dolfin::la_index>>& rows)
156  {
157  dolfin_assert(block);
158  _local_increment += block[0];
159  }
160 
162  virtual void zero()
163  {
164  _value = 0.0;
165  _local_increment = 0.0;
166  }
167 
169  virtual void apply(std::string mode)
170  {
171  _value = _value + MPI::sum(_mpi_comm.comm(), _local_increment);
172  _local_increment = 0.0;
173  }
174 
176  virtual MPI_Comm mpi_comm() const
177  { return _mpi_comm.comm(); }
178 
180  virtual std::string str(bool verbose) const
181  {
182  std::stringstream s;
183  s << "<Scalar value " << _value << ">";
184  return s.str();
185  }
186 
187  //--- Scalar interface ---
188 
190  virtual std::shared_ptr<Scalar> copy() const
191  {
192  std::shared_ptr<Scalar> s(new Scalar);
193  s->_value = _value;
194  s->_local_increment = _local_increment;
195  s->_mpi_comm.reset(_mpi_comm.comm());
196  return s;
197  }
198 
199  //--- Special functions
200 
203  {
204  DefaultFactory f;
205  return f.factory();
206  }
207 
210  double get_scalar_value() const
211  { return _value; }
212 
215  void add_local_value(double value)
216  { _local_increment += value; }
217 
218  private:
219 
220  // Value of scalar
221  double _value;
222 
223  // Local intermediate value of scalar prior to apply call
224  double _local_increment;
225 
226  // MPI communicator
227  dolfin::MPI::Comm _mpi_comm;
228 
229  };
230 
231 }
232 
233 #endif
Scalar()
Create zero scalar.
Definition: Scalar.h:47
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition: Scalar.h:169
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition: Scalar.h:94
virtual void add_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using local indices.
Definition: Scalar.h:138
Definition: adapt.h:29
Definition: Scalar.h:42
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: Scalar.h:162
virtual void add(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using global indices.
Definition: Scalar.h:146
virtual std::shared_ptr< Scalar > copy() const
Return copy of scalar.
Definition: Scalar.h:190
virtual GenericLinearAlgebraFactory & factory() const
Return a factory for the default linear algebra backend.
Definition: Scalar.h:202
virtual void set_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using local indices.
Definition: Scalar.h:121
virtual void add(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using global indices.
Definition: Scalar.h:130
Definition: TensorLayout.h:41
MPI_Comm comm() const
Return the underlying MPI_Comm object.
Definition: MPI.cpp:117
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
static T sum(MPI_Comm comm, const T &value)
Sum values and return sum.
Definition: MPI.h:814
virtual std::size_t rank() const
Return tensor rank (number of dimensions)
Definition: Scalar.h:71
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: TensorLayout.h:92
A common interface for arbitrary rank tensors.
Definition: GenericTensor.h:48
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: Scalar.h:176
Definition: ArrayView.h:31
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition: Scalar.h:75
double get_scalar_value() const
Definition: Scalar.h:210
void add_local_value(double value)
Definition: Scalar.h:215
virtual void add_local(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using local indices.
Definition: Scalar.h:154
virtual bool empty() const
Return true if empty.
Definition: Scalar.h:67
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual ~Scalar()
Destructor.
Definition: Scalar.h:54
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
void reset(MPI_Comm comm)
Definition: MPI.cpp:91
static GenericLinearAlgebraFactory & factory()
Return instance of default backend.
Definition: DefaultFactory.cpp:85
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: Scalar.h:180
Definition: MPI.h:76
Scalar(MPI_Comm comm)
Create zero scalar.
Definition: Scalar.h:50
virtual void init(const TensorLayout &tensor_layout)
Initialize zero tensor using sparsity pattern.
Definition: Scalar.h:59
Default linear algebra factory based on global parameter "linear_algebra_backend".
Definition: DefaultFactory.h:35