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.
explicit_builder_and_solver.hpp
Go to the documentation of this file.
1 //
2 // Project Name: KratosSolidMechanicsApplication $
3 // Created by: $Author: JMCarbonell $
4 // Last modified by: $Co-Author: $
5 // Date: $Date: July 2013 $
6 // Revision: $Revision: March 2018 $
7 //
8 //
9 
10 #if !defined(KRATOS_EXPLICIT_BUILDER_AND_SOLVER_H_INCLUDED)
11 #define KRATOS_EXPLICIT_BUILDER_AND_SOLVER_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 namespace Kratos
21 {
22 
25 
29 
33 
37 
41 
45 template<class TSparseSpace,
46  class TDenseSpace, //= DenseSpace<double>,
47  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
48  >
49 class ExplicitBuilderAndSolver : public SolutionBuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver >
50 {
51  public:
52 
53 
56 
59 
61 
64 
71 
75 
77 
81 
84  : BaseType()
85  {
86  }
87 
90  {
91  }
95 
99 
101  ModelPart& rModelPart,
102  SystemMatrixType& rA) override
103  {
104  KRATOS_TRY
105 
106  //Set Nodal Mass to zero
107  NodesContainerType& pNodes = rModelPart.Nodes();
108  ElementsContainerType& pElements = rModelPart.Elements();
109  ProcessInfo& rCurrentProcessInfo = rModelPart.GetProcessInfo();
110 
111 #ifdef _OPENMP
112  int number_of_threads = omp_get_max_threads();
113 #else
114  int number_of_threads = 1;
115 #endif
116 
117  vector<unsigned int> node_partition;
118  OpenMPUtils::CreatePartition(number_of_threads, pNodes.size(), node_partition);
119 
120  vector<unsigned int> element_partition;
121  OpenMPUtils::CreatePartition(number_of_threads, pElements.size(), element_partition);
122 
123 #pragma omp parallel
124  {
125 #pragma omp for
126 
127  for(int k=0; k<number_of_threads; k++)
128  {
129  typename NodesContainerType::iterator i_begin=pNodes.ptr_begin()+node_partition[k];
130  typename NodesContainerType::iterator i_end=pNodes.ptr_begin()+node_partition[k+1];
131 
132  for(ModelPart::NodeIterator i=i_begin; i!= i_end; ++i)
133  {
134  double& nodal_mass = i->FastGetSolutionStepValue(NODAL_MASS);
135  nodal_mass = 0.0;
136  }
137  }
138 
139  }
140 
141  //Calculate and assemble Mass Matrix on nodes
142 
143  bool CalculateLumpedMassMatrix = false;
144  if( rCurrentProcessInfo.Has(COMPUTE_LUMPED_MASS_MATRIX) ){
145  CalculateLumpedMassMatrix = rCurrentProcessInfo[COMPUTE_LUMPED_MASS_MATRIX];
146  }
147 
148 #pragma omp parallel
149  {
150  int k = OpenMPUtils::ThisThread();
151  typename ElementsContainerType::iterator ElemBegin = pElements.begin() + element_partition[k];
152  typename ElementsContainerType::iterator ElemEnd = pElements.begin() + element_partition[k + 1];
153 
154  for (typename ElementsContainerType::iterator itElem = ElemBegin; itElem != ElemEnd; ++itElem) //MSI: To be parallelized
155  {
156  Matrix MassMatrix;
157 
158  Element::GeometryType& geometry = itElem->GetGeometry();
159 
160  (itElem)->CalculateMassMatrix(MassMatrix, rCurrentProcessInfo);
161 
162  const unsigned int dimension = geometry.WorkingSpaceDimension();
163 
164  unsigned int index = 0;
165  for (unsigned int i = 0; i <geometry.size(); i++)
166  {
167  index = i*dimension;
168 
169  double& mass = geometry(i)->FastGetSolutionStepValue(NODAL_MASS);
170 
171  geometry(i)->SetLock();
172 
173  if(!CalculateLumpedMassMatrix){
174  for (unsigned int j = 0; j <MassMatrix.size2(); j++)
175  {
176  mass += MassMatrix(index,j);
177  }
178  }
179  else{
180  mass += MassMatrix(index,index);
181  }
182 
183  geometry(i)->UnSetLock();
184  }
185  }
186  }
187 
188  rCurrentProcessInfo[COMPUTE_LUMPED_MASS_MATRIX] = CalculateLumpedMassMatrix;
189 
190  KRATOS_CATCH( "" )
191 
192  }
193 
194  //**************************************************************************
195  //**************************************************************************
196 
198  ModelPart& rModelPart,
199  SystemVectorType& rb) override
200  {
201  KRATOS_TRY
202 
203  // Compute condition contributions to RHS.
204  CalculateAndAddConditionsRHS(pScheme, rModelPart);
205 
206  // Compute element contributions to RHS.
207  CalculateAndAddElementsRHS(pScheme, rModelPart);
208 
209 
210  KRATOS_CATCH( "" )
211 
212  }
213 
214 
216  ModelPart& rModelPart )
217  {
218 
219  KRATOS_TRY
220 
221  ProcessInfo& rCurrentProcessInfo = rModelPart.GetProcessInfo();
222  ConditionsContainerType& rConditions = rModelPart.Conditions();
223 
224 #ifdef _OPENMP
225  int number_of_threads = omp_get_max_threads();
226 #else
227  int number_of_threads = 1;
228 #endif
229 
230  vector<unsigned int> condition_partition;
231  OpenMPUtils::CreatePartition(number_of_threads, rConditions.size(), condition_partition);
232 
233 
234 #pragma omp parallel for
235  for(int k=0; k<number_of_threads; k++)
236  {
237  typename ConditionsContainerType::ptr_iterator it_begin=rConditions.ptr_begin()+condition_partition[k];
238  typename ConditionsContainerType::ptr_iterator it_end=rConditions.ptr_begin()+condition_partition[k+1];
239 
240  for (typename ConditionsContainerType::ptr_iterator it= it_begin; it!=it_end; ++it)
241  {
242 
243  LocalSystemVectorType RHS_Condition_Contribution = LocalSystemVectorType(0);
244 
245  Element::EquationIdVectorType EquationId; //Dummy
246 
247  pScheme->Condition_Calculate_RHS_Contribution(*it, RHS_Condition_Contribution, EquationId, rCurrentProcessInfo);
248 
249 
250  }
251  }
252 
253  KRATOS_CATCH("")
254  }
255 
257  ModelPart& rModelPart)
258  {
259 
260  KRATOS_TRY
261 
262  ProcessInfo& rCurrentProcessInfo = rModelPart.GetProcessInfo();
263  ElementsContainerType& pElements = rModelPart.Elements();
264 
265 #ifdef _OPENMP
266  int number_of_threads = omp_get_max_threads();
267 #else
268  int number_of_threads = 1;
269 #endif
270 
271  vector<unsigned int> element_partition;
272  OpenMPUtils::CreatePartition(number_of_threads, pElements.size(), element_partition);
273 
274 #pragma omp parallel for
275  for(int k=0; k<number_of_threads; k++)
276  {
277  typename ElementsContainerType::ptr_iterator it_begin=pElements.ptr_begin()+element_partition[k];
278  typename ElementsContainerType::ptr_iterator it_end=pElements.ptr_begin()+element_partition[k+1];
279  for (typename ElementsContainerType::ptr_iterator it= it_begin; it!=it_end; ++it)
280  {
281 
282  LocalSystemVectorType RHS_Contribution = LocalSystemVectorType(0);
283  Element::EquationIdVectorType EquationId; //Dummy
284 
285  pScheme->Calculate_RHS_Contribution(*it, RHS_Contribution, EquationId, rCurrentProcessInfo);
286 
287  }
288  }
289 
290  KRATOS_CATCH("")
291  }
292 
293 
297  void Clear() override
298  {
299  BaseType::Clear();
300  }
301 
309  int Check(ModelPart& rModelPart) override
310  {
311  KRATOS_TRY
312 
313  return 0;
314 
315  KRATOS_CATCH( "" )
316  }
317 
321 
325 
329 
331 
332 protected:
353 
354 private:
355 
378 
379 };
380 
382 
385 
389 
391 
393 
394 } // namespace Kratos.
395 
396 #endif // KRATOS_EXPLICIT_BUILDER_AND_SOLVER_H_INCLUDED defined
397 
398 
bool Has(const Variable< TDataType > &rThisVariable) const
Checks if the data container has a value associated with a given variable.
Definition: data_value_container.h:382
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
Explicit Solution Buider and Solver base class.
Definition: explicit_builder_and_solver.hpp:50
ModelPart::ElementsContainerType ElementsContainerType
Definition: explicit_builder_and_solver.hpp:73
BaseType::SchemePointerType SchemePointerType
Definition: explicit_builder_and_solver.hpp:76
ExplicitBuilderAndSolver()
Default Constructor.
Definition: explicit_builder_and_solver.hpp:83
void CalculateAndAddElementsRHS(SchemePointerType pScheme, ModelPart &rModelPart)
Definition: explicit_builder_and_solver.hpp:256
void BuildLHS(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA) override
Function to perform the building of the LHS,.
Definition: explicit_builder_and_solver.hpp:100
BaseType::SystemVectorType SystemVectorType
Definition: explicit_builder_and_solver.hpp:66
BaseType::SystemVectorPointerType SystemVectorPointerType
Definition: explicit_builder_and_solver.hpp:68
void BuildRHS(SchemePointerType pScheme, ModelPart &rModelPart, SystemVectorType &rb) override
Function to perform the build of the RHS.
Definition: explicit_builder_and_solver.hpp:197
SolutionBuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: explicit_builder_and_solver.hpp:60
ModelPart::NodesContainerType NodesContainerType
Definition: explicit_builder_and_solver.hpp:72
void Clear() override
This function is intended to be called at the end of the solution step to clean up memory storage not...
Definition: explicit_builder_and_solver.hpp:297
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: explicit_builder_and_solver.hpp:69
BaseType::SystemMatrixPointerType SystemMatrixPointerType
Definition: explicit_builder_and_solver.hpp:67
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: explicit_builder_and_solver.hpp:70
ModelPart::ConditionsContainerType ConditionsContainerType
Definition: explicit_builder_and_solver.hpp:74
int Check(ModelPart &rModelPart) override
Definition: explicit_builder_and_solver.hpp:309
KRATOS_CLASS_POINTER_DEFINITION(ExplicitBuilderAndSolver)
Pointer definition of ExplicitBuilderAndSolver.
BaseType::LocalFlagType LocalFlagType
Definition: explicit_builder_and_solver.hpp:62
BaseType::SystemMatrixType SystemMatrixType
Definition: explicit_builder_and_solver.hpp:65
~ExplicitBuilderAndSolver() override
Destructor.
Definition: explicit_builder_and_solver.hpp:89
BaseType::DofsArrayType DofsArrayType
Definition: explicit_builder_and_solver.hpp:63
void CalculateAndAddConditionsRHS(SchemePointerType pScheme, ModelPart &rModelPart)
Definition: explicit_builder_and_solver.hpp:215
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
SizeType WorkingSpaceDimension() const
Definition: geometry.h:1287
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
MeshType::NodeIterator NodeIterator
Definition: model_part.h:134
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
Solution Buider and Solver base class.
Definition: solution_builder_and_solver.hpp:63
virtual void Clear()
This function is intended to be called at the end of the solution step to clean up memory storage not...
Definition: solution_builder_and_solver.hpp:305
TSparseSpace::MatrixPointerType SystemMatrixPointerType
Definition: solution_builder_and_solver.hpp:78
TSparseSpace::VectorType SystemVectorType
Definition: solution_builder_and_solver.hpp:76
TDenseSpace::VectorType LocalSystemVectorType
Definition: solution_builder_and_solver.hpp:82
TSparseSpace::MatrixType SystemMatrixType
Definition: solution_builder_and_solver.hpp:75
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: solution_builder_and_solver.hpp:81
TSparseSpace::VectorPointerType SystemVectorPointerType
Definition: solution_builder_and_solver.hpp:79
SchemeType::Pointer SchemePointerType
Definition: solution_builder_and_solver.hpp:85
Solver local flags class definition.
Definition: solution_local_flags.hpp:48
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
int dimension
Definition: isotropic_damage_automatic_differentiation.py:123
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
integer i
Definition: TensorModule.f:17