KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
system_vector.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 #if !defined(KRATOS_SYSTEM_VECTOR_H_INCLUDED )
13 #define KRATOS_SYSTEM_VECTOR_H_INCLUDED
14 
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 
21 // External includes
22 
23 
24 // Project includes
25 #include "includes/define.h"
32 
33 
34 namespace Kratos
35 {
38 
41 
45 
49 
53 
57 
59 template<class TDataType=double, class TIndexType=std::size_t>
60 class SystemVector final
61 {
62 public:
65  typedef TIndexType IndexType;
66 
69 
73 
75  mpComm = rGraph.pGetComm();
76  mData.resize(rGraph.Size(),false);
77  }
78 
80  mpComm = rGraph.pGetComm();
81  mData.resize(rGraph.Size(),false);
82  }
83 
85  if(rComm.IsDistributed())
86  KRATOS_ERROR << "Attempting to construct a serial system_vector with a distributed communicator" << std::endl;
87  mpComm = &rComm;
88  mData.resize(size,false);
89  }
90 
92  const Vector& data,
94  if(rComm.IsDistributed())
95  KRATOS_ERROR << "Attempting to construct a serial system_vector with a distributed communicator" << std::endl;
96  mpComm = &rComm;
97  mData.resize(data.size(),false);
98  noalias(mData) = data;
99  }
101  explicit SystemVector(const SystemVector<TDataType,TIndexType>& rOtherVector){
102  mpComm = rOtherVector.mpComm;
103  mData.resize(rOtherVector.size(),false);
104 
106  (*this)[i] = rOtherVector[i];
107  });
108  }
109 
112 
113  const DataCommunicator& GetComm() const
114  {
115  return *mpComm;
116  }
117 
118  const DataCommunicator* pGetComm() const
119  {
120  return mpComm;
121  }
122 
126  void Clear()
127  {
128  mData.clear();
129  }
130 
131  void SetValue(const TDataType value)
132  {
133  IndexPartition<IndexType>(mData.size()).for_each([&](IndexType i){
134  mData[i] = value;
135  });
136  }
137 
138  IndexType size() const
139  {
140  return mData.size();
141  }
142 
143  TDataType& operator()(IndexType I){
144  return mData[I];
145  }
146 
147  const TDataType& operator()(IndexType I) const{
148  return mData[I];
149  }
150 
151  TDataType& operator[](IndexType I){
152  return mData[I];
153  }
154 
155  const TDataType& operator[](IndexType I) const{
156  return mData[I];
157  }
158 
161  {
162  return mData;
163  }
164 
167  {
168  return mData;
169  }
170 
171  void Add(const TDataType factor,
172  const SystemVector& rOtherVector
173  )
174  {
176  (*this)[i] += factor*rOtherVector[i];
177  });
178  }
179 
181  SystemVector& operator=(SystemVector const& rOtherVector){
183  (*this)[i] = rOtherVector[i];
184  });
185  return *this;
186  }
187 
188 
189  SystemVector& operator+=(const SystemVector& rOtherVector)
190  {
192  (*this)[i] += rOtherVector[i];
193  });
194  return *this;
195  }
196 
197  SystemVector& operator-=(const SystemVector& rOtherVector)
198  {
200  (*this)[i] -= rOtherVector[i];
201  });
202  return *this;
203  }
204 
205  SystemVector& operator*=(const TDataType multiplier_factor)
206  {
208  (*this)[i] *= multiplier_factor;
209  });
210  return *this;
211  }
212 
213  SystemVector& operator/=(const TDataType divide_factor)
214  {
216  (*this)[i] /= divide_factor;
217  });
218  return *this;
219  }
220 
221  TDataType Dot(const SystemVector& rOtherVector, IndexType gather_on_rank=0)
222  {
223  KRATOS_WARNING_IF("SystemVector", gather_on_rank != 0) << "the parameter gather_on_rank essentially does nothing for a non-distribued vector. It is added to have the same interface as for the distributed_system_vector" << std::endl;
224 
225  auto partition = IndexPartition<IndexType>(size());
226  TDataType dot_value = partition.template for_each< SumReduction<TDataType> >([&](IndexType i){
227  return (*this)[i]*rOtherVector[i];
228  });
229 
230  return dot_value; // note that the value to be reduced should be returned
231  }
232 
233 
237  void BeginAssemble(){} //the SMP version does nothing. This function is there to be implemented in the MPI case
238 
239  void FinalizeAssemble(){} //the SMP version does nothing. This function is there to be implemented in the MPI case
240 
241  template<class TVectorType, class TIndexVectorType >
242  void Assemble(
243  const TVectorType& rVectorInput,
244  const TIndexVectorType& EquationId
245  )
246  {
247  KRATOS_DEBUG_ERROR_IF(rVectorInput.size() != EquationId.size());
248 
249  for(unsigned int i=0; i<EquationId.size(); ++i){
250  IndexType global_i = EquationId[i];
251  KRATOS_DEBUG_ERROR_IF(global_i > mData.size());
252  AtomicAdd(mData[global_i] , rVectorInput[i]);
253  }
254  }
255 
256 
260 
261 
265 
266 
270 
272  std::string Info() const
273  {
274  std::stringstream buffer;
275  buffer << "SystemVector" ;
276  return buffer.str();
277  }
278 
280  void PrintInfo(std::ostream& rOStream) const {
281  rOStream << "SystemVector" << std::endl;
282  PrintData(rOStream);
283  }
284 
286  void PrintData(std::ostream& rOStream) const {
287  std::cout << mData << std::endl;
288  }
289 
293 
294 
296 
297 protected:
300 
301 
305 
306 
310 
311 
315 
316 
320 
321 
325 
326 
330 
331 
333 
334 private:
337 
338 
342  const DataCommunicator* mpComm;
344 
348 
349 
353 
354 
358 
359 
363 
364 
368 
370 
371 }; // Class SystemVector
372 
374 
377 
378 
382 
383 
385 template<class TDataType, class TIndexType>
386 inline std::istream& operator >> (std::istream& rIStream,
388  {
389  return rIStream;
390  }
391 
393 template<class TDataType, class TIndexType>
394 inline std::ostream& operator << (std::ostream& rOStream,
396 {
397  rThis.PrintInfo(rOStream);
398  rOStream << std::endl;
399  rThis.PrintData(rOStream);
400 
401  return rOStream;
402 }
404 
406 
407 } // namespace Kratos.
408 
409 #endif // KRATOS_SYSTEM_VECTOR_H_INCLUDED defined
410 
411 
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
static DataCommunicator & GetDataCommunicator(const std::string &rName)
Retrieve a registered DataCommunicator instance.
Definition: parallel_environment.cpp:26
const DataCommunicator * pGetComm() const
Definition: sparse_contiguous_row_graph.h:127
IndexType Size() const
Definition: sparse_contiguous_row_graph.h:141
Short class definition.
Definition: sparse_graph.h:66
IndexType Size() const
Definition: sparse_graph.h:129
const DataCommunicator * pGetComm() const
Definition: sparse_graph.h:124
Provides a SystemVector which implements FEM assemble capabilities, as well as some vector operations...
Definition: system_vector.h:61
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: system_vector.h:286
void Clear()
Definition: system_vector.h:126
const TDataType & operator()(IndexType I) const
Definition: system_vector.h:147
SystemVector(IndexType size, DataCommunicator &rComm=ParallelEnvironment::GetDataCommunicator("Serial"))
Definition: system_vector.h:84
KRATOS_CLASS_POINTER_DEFINITION(SystemVector)
Pointer definition of SystemVector.
SystemVector & operator-=(const SystemVector &rOtherVector)
Definition: system_vector.h:197
TDataType & operator[](IndexType I)
Definition: system_vector.h:151
const DenseVector< TDataType > & data() const
provides low level access to internal data
Definition: system_vector.h:166
void Add(const TDataType factor, const SystemVector &rOtherVector)
Definition: system_vector.h:171
SystemVector(const SparseContiguousRowGraph< IndexType > &rGraph)
Definition: system_vector.h:79
TDataType & operator()(IndexType I)
Definition: system_vector.h:143
SystemVector & operator*=(const TDataType multiplier_factor)
Definition: system_vector.h:205
SystemVector(const Vector &data, DataCommunicator &rComm=ParallelEnvironment::GetDataCommunicator("Serial"))
Definition: system_vector.h:91
void Assemble(const TVectorType &rVectorInput, const TIndexVectorType &EquationId)
Definition: system_vector.h:242
SystemVector & operator/=(const TDataType divide_factor)
Definition: system_vector.h:213
IndexType size() const
Definition: system_vector.h:138
void SetValue(const TDataType value)
Definition: system_vector.h:131
void BeginAssemble()
Definition: system_vector.h:237
const DataCommunicator * pGetComm() const
Definition: system_vector.h:118
const TDataType & operator[](IndexType I) const
Definition: system_vector.h:155
TIndexType IndexType
Definition: system_vector.h:65
TDataType Dot(const SystemVector &rOtherVector, IndexType gather_on_rank=0)
Definition: system_vector.h:221
~SystemVector()
Destructor.
Definition: system_vector.h:111
void FinalizeAssemble()
Definition: system_vector.h:239
std::string Info() const
Turn back information as a string.
Definition: system_vector.h:272
SystemVector & operator+=(const SystemVector &rOtherVector)
Definition: system_vector.h:189
SystemVector & operator=(SystemVector const &rOtherVector)
Assignment operator.
Definition: system_vector.h:181
const DataCommunicator & GetComm() const
Definition: system_vector.h:113
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: system_vector.h:280
SystemVector(const SystemVector< TDataType, TIndexType > &rOtherVector)
Copy constructor.
Definition: system_vector.h:101
SystemVector(const SparseGraph< IndexType > &rGraph)
Definition: system_vector.h:74
DenseVector< TDataType > & data()
provides low level access to internal data
Definition: system_vector.h:160
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
integer i
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254