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.
morton_divide_input_to_partitions_process.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: Pooyan Dadvand
11 // Jordi Cotela
12 // Carlos Roig
13 //
14 
15 #pragma once
16 
17 // System includes
18 
19 // Project includes
22 
23 namespace Kratos
24 {
25 
28 
32 
33 
37 
41 
45 
47 
51  : public Process
52 {
53 public:
56 
59 
60  typedef std::size_t SizeType;
61  typedef std::size_t IndexType;
63 
67 
69  MortonDivideInputToPartitionsProcess(IO& rIO, SizeType NumberOfPartitions, int Dimension = 3)
70  : mrIO(rIO), mNumberOfPartitions(NumberOfPartitions), mDimension(Dimension)
71  {
72  }
73 
77  {
78  }
79 
82  {
83  }
84 
85 
89 
90  void operator()()
91  {
92  Execute();
93  }
94 
95 
99 
100  void Execute() override
101  {
102  KRATOS_TRY;
103 
104  if(mNumberOfPartitions < 2) // There is no need to partition it and just reading the input
105  {
106  return;
107  }
108 
109  // Reading connectivities
110  IO::ConnectivitiesContainerType elements_connectivities;
111  IO::ConnectivitiesContainerType conditions_connectivities;
112  IO::NodesContainerType nodes_container;
113 
114  int number_of_elements = mrIO.ReadElementsConnectivities(elements_connectivities);
115 
116  mrIO.ReadConditionsConnectivities(conditions_connectivities);
117  mrIO.ReadNodes(nodes_container);
118 
122 
123  MortonPartitioningProcess morton_partitioning_process(elements_connectivities, nodes_container, nodes_partitions, elements_partitions, mNumberOfPartitions, mDimension);
124  morton_partitioning_process.Execute();
125 
126 // GraphType domains_graph = zero_matrix<int>(mNumberOfPartitions, mNumberOfPartitions);
129 
130  GraphType domains_colored_graph;
131 
132  int colors_number;
133 
134  CalculateDomainsGraph(domains_graph, number_of_elements, elements_connectivities, nodes_partitions, elements_partitions);
135 
136  GraphColoringProcess(mNumberOfPartitions, domains_graph, domains_colored_graph, colors_number).Execute();
137 
138  //KRATOS_WATCH(domains_graph);
139 
140 // std::vector<DomainEntitiesIdContainer> domains_nodes;
141  IO::PartitionIndicesContainerType nodes_all_partitions;
142  IO::PartitionIndicesContainerType elements_all_partitions;
143  IO::PartitionIndicesContainerType conditions_all_partitions;
144 
145  ConditionsPartitioning(conditions_connectivities, nodes_partitions, conditions_partitions);
146  KRATOS_WATCH("ConditionsPartitioning finished")
147  // Dividing nodes
148  DividingNodes(nodes_all_partitions, elements_connectivities, conditions_connectivities, nodes_partitions, elements_partitions, conditions_partitions);
149  KRATOS_WATCH("DividingNodes finished")
150  // Dividing elements
151  DividingElements(elements_all_partitions, elements_partitions);
152  KRATOS_WATCH("DividingElements finished")
153  // Dividing conditions
154  DividingConditions(conditions_all_partitions, conditions_partitions);
155  KRATOS_WATCH("DividingConditions finished")
156 
157  IO::PartitionIndicesType io_nodes_partitions(nodes_partitions.begin(), nodes_partitions.end());
158  IO::PartitionIndicesType io_elements_partitions(elements_partitions.begin(), elements_partitions.end());
159  IO::PartitionIndicesType io_conditions_partitions(conditions_partitions.begin(), conditions_partitions.end());
160 
161  // Now dividing the input file
162  mrIO.DivideInputToPartitions(mNumberOfPartitions, domains_colored_graph,
163  io_nodes_partitions, io_elements_partitions, io_conditions_partitions,
164  nodes_all_partitions, elements_all_partitions, conditions_all_partitions);
165  KRATOS_WATCH("DivideInputToPartitions finished")
166  return;
167 
168  KRATOS_CATCH("")
169  }
170 
172  {
173  for(SizeType i_element = 0 ; i_element < NumberOfElements ; i_element++)
174  {
175  for(std::vector<std::size_t>::iterator i_node = ElementsConnectivities[i_element].begin() ;
176  i_node != ElementsConnectivities[i_element].end() ; i_node++)
177  {
178  SizeType node_rank = NPart[*i_node-1];
179  SizeType element_rank = EPart[i_element];
180  if(node_rank != element_rank)
181  {
182  //matrix!
183  rDomainsGraph(node_rank, element_rank) = 1;
184  rDomainsGraph(element_rank, node_rank) = 1;
185  }
186  }
187  }
188  }
189 
190 
194 
195 
199 
200 
204 
206  std::string Info() const override
207  {
208  return "MortonDivideInputToPartitionsProcess";
209  }
210 
212  void PrintInfo(std::ostream& rOStream) const override
213  {
214  rOStream << "MortonDivideInputToPartitionsProcess";
215  }
216 
218  void PrintData(std::ostream& rOStream) const override
219  {
220  }
221 
222 
226 
227 
229 
230 protected:
231 
233  {
234  public:
235  DomainEntitiesIdContainer(std::size_t NumberOfNeighbours)
236  {
237  mLocalIds.resize(NumberOfNeighbours);
238  mGhostsIds.resize(NumberOfNeighbours);
239  mInterfacesIds.resize(NumberOfNeighbours);
240  }
241 
242  std::vector<std::size_t>& AllIds()
243  {
244  return mAllIds;
245  }
246 
247  std::vector<std::vector<std::size_t> >& LocalIds()
248  {
249  return mLocalIds;
250  }
251 
252  std::vector<std::vector<std::size_t> >& GhostsIds()
253  {
254  return mGhostsIds;
255  }
256 
257  std::vector<std::vector<std::size_t> >& InterfacesIds()
258  {
259  return mInterfacesIds;
260  }
261  private:
262  std::vector<std::size_t> mAllIds;
263  std::vector<std::vector<std::size_t> > mLocalIds;
264  std::vector<std::vector<std::size_t> > mGhostsIds;
265  std::vector<std::vector<std::size_t> > mInterfacesIds;
266 
267  };
268 
271 
272 
276 
277 // ModelPart& mrModelPart;
278 
280 
282 
284 
285 
289 
290 
294 
295 
299  {
300  SizeType number_of_conditions = ConditionsConnectivities.size();
301 
302  ConditionsPartitions.resize(number_of_conditions);
303 
304  // getting the average of the partion indices of the condition nodes and take the nearest partition indices of the nodes to the average.
305  for(SizeType i_condition = 0 ; i_condition < number_of_conditions ; i_condition++)
306  {
307  if(ConditionsConnectivities[i_condition].size() > 0)
308  {
309  double average_index = 0.00;
310  for(IO::ConnectivitiesContainerType::value_type::iterator i_node = ConditionsConnectivities[i_condition].begin() ;
311  i_node != ConditionsConnectivities[i_condition].end() ; i_node++)
312  {
313  //get global id. We assume that node ids are began with one
314  const int my_gid = *i_node-1;
315 
316  average_index += NodesPartitions[my_gid];
317 
318  }
319  average_index /= ConditionsConnectivities[i_condition].size();
320 
321  double difference = mNumberOfPartitions + 10; // Just to be sure! ;-)
322 
323  for(IO::ConnectivitiesContainerType::value_type::iterator i_node = ConditionsConnectivities[i_condition].begin() ;
324  i_node != ConditionsConnectivities[i_condition].end() ; i_node++)
325  {
326  //get global id. We assume that node ids are began with one
327  const int my_gid = *i_node-1;
328 
329  //get the partition index for the node i am interested in
330  const int node_partition = NodesPartitions[my_gid];
331 
332  if(difference > fabs(average_index - node_partition))
333  {
334  difference = fabs(average_index - node_partition);
335  ConditionsPartitions[i_condition] = node_partition;
336  }
337  }
338 
339  }
340  }
341  }
342 
344  IO::ConnectivitiesContainerType& ElementsConnectivities,
345  IO::ConnectivitiesContainerType& ConditionsConnectivities,
347  MortonPartitioningProcess::PartitionIndicesType const& ElementsPartitions,
348  MortonPartitioningProcess::PartitionIndicesType const& ConditionsPartitions)
349  {
350  SizeType number_of_nodes = NodesPartitions.size();
351  SizeType number_of_elements = ElementsPartitions.size();
352  SizeType number_of_conditions = ConditionsPartitions.size();
353 
354  rNodesAllPartitions.resize(number_of_nodes);
355 
356  for(SizeType i_element = 0 ; i_element < number_of_elements ; i_element++)
357  {
358  const int element_partition = ElementsPartitions[i_element];
359 
360  //for each element in the model loop over its connectivities
361  for(IO::ConnectivitiesContainerType::value_type::iterator i_node = ElementsConnectivities[i_element].begin() ;
362  i_node != ElementsConnectivities[i_element].end() ; i_node++)
363  {
364  //get global id. We assume that node ids are began with one
365  const int my_gid = *i_node-1;
366 
367  //get the partition index for the node i am interested in
368  const int node_partition = NodesPartitions[my_gid];
369 
370  // adding the partition of the element to its nodes
371  if(element_partition != node_partition) // we will add the node_partition once afterward
372  {
373  std::cout << "Partiton element differnet form partition node" << std::endl;
374  rNodesAllPartitions[my_gid].push_back(element_partition);
375  }
376  }
377  }
378 
379  for(SizeType i_condition = 0 ; i_condition < number_of_conditions ; i_condition++)
380  {
381  const int condition_partition = ConditionsPartitions[i_condition];
382 
383  //for each element in the model loop over its connectivities
384  for(IO::ConnectivitiesContainerType::value_type::iterator i_node = ConditionsConnectivities[i_condition].begin() ;
385  i_node != ConditionsConnectivities[i_condition].end() ; i_node++)
386  {
387  //get global id. We assume that node ids are began with one
388  const int my_gid = *i_node-1;
389 
390  //get the partition index for the node i am interested in
391  const int node_partition = NodesPartitions[my_gid];
392 
393  // adding the partition of the element to its nodes
394  if(condition_partition != node_partition) // we will add the node_partition once afterward
395  rNodesAllPartitions[my_gid].push_back(condition_partition);
396  }
397  }
398 
399  // adding the nodes partition to their array of partitions and clear the repeated ones
400  for(SizeType i_node = 0 ; i_node < number_of_nodes ; i_node++)
401  {
402  IO::PartitionIndicesContainerType::value_type& node_partitions = rNodesAllPartitions[i_node];
403  node_partitions.push_back(NodesPartitions[i_node]);
404 
405  std::sort(node_partitions.begin(), node_partitions.end());
406  IO::PartitionIndicesContainerType::value_type::iterator new_end=std::unique(node_partitions.begin(), node_partitions.end());
407  node_partitions.resize(new_end - node_partitions.begin());
408  }
409  }
410 
412  {
413  SizeType number_of_elements = ElementsPartitions.size();
414 
415  rElementsAllPartitions.resize(number_of_elements);
416 
417  // adding the elements partition to their array of partitions
418  for(SizeType i_element = 0 ; i_element < number_of_elements ; i_element++)
419  {
420  rElementsAllPartitions[i_element].push_back(ElementsPartitions[i_element]);
421  }
422  }
423 
425  {
426  SizeType number_of_conditions = ConditionsPartitions.size();
427 
428  rConditionsAllPartitions.resize(number_of_conditions);
429 
430  // adding the condition partition to their array of partitions
431  for(SizeType i_condition = 0 ; i_condition < number_of_conditions ; i_condition++)
432  {
433  rConditionsAllPartitions[i_condition].push_back(ConditionsPartitions[i_condition]);
434  }
435  }
436 
437 
438 
442 
443 
447 
448 
452 
453 
455 
456 private:
459 
460 
464 
468 
472 
473 
477 
478 
482 
483 
487 
490 
492  //MortonDivideInputToPartitionsProcess(MortonDivideInputToPartitionsProcess const& rOther);
493 
494 
496 
497 }; // Class MortonDivideInputToPartitionsProcess
498 
500 
503 
504 
508 
509 
511 inline std::istream& operator >> (std::istream& rIStream,
513 
515 inline std::ostream& operator << (std::ostream& rOStream,
517 {
518  rThis.PrintInfo(rOStream);
519  rOStream << std::endl;
520  rThis.PrintData(rOStream);
521 
522  return rOStream;
523 }
525 
526 } // namespace Kratos.
Getting a graph of connectivities (of domains) and performs a coloring procedure.
Definition: graph_coloring_process.h:43
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: graph_coloring_process.h:99
IO provides different implementation of input output procedures which can be used to read and write w...
Definition: io.h:58
virtual void DivideInputToPartitions(SizeType NumberOfPartitions, const PartitioningInfo &rPartitioningInfo)
This method divides a model part into partitions.
Definition: io.h:490
virtual std::size_t ReadConditionsConnectivities(ConnectivitiesContainerType &rConditionsConnectivities)
This method reads the conditions connectivities.
Definition: io.h:338
virtual bool ReadNodes(NodesContainerType &rThisNodes)
This method reads the nodes from an array of nodes.
Definition: io.h:150
std::vector< std::vector< std::size_t > > ConnectivitiesContainerType
Definition: io.h:91
std::vector< std::size_t > PartitionIndicesType
Definition: io.h:95
std::vector< std::vector< std::size_t > > PartitionIndicesContainerType
Definition: io.h:93
virtual std::size_t ReadElementsConnectivities(ConnectivitiesContainerType &rElementsConnectivities)
This method reads the elements connectivities.
Definition: io.h:289
Definition: amatrix_interface.h:41
Definition: morton_divide_input_to_partitions_process.h:233
std::vector< std::vector< std::size_t > > & LocalIds()
Definition: morton_divide_input_to_partitions_process.h:247
std::vector< std::vector< std::size_t > > & InterfacesIds()
Definition: morton_divide_input_to_partitions_process.h:257
std::vector< std::size_t > & AllIds()
Definition: morton_divide_input_to_partitions_process.h:242
std::vector< std::vector< std::size_t > > & GhostsIds()
Definition: morton_divide_input_to_partitions_process.h:252
DomainEntitiesIdContainer(std::size_t NumberOfNeighbours)
Definition: morton_divide_input_to_partitions_process.h:235
Short class definition.
Definition: morton_divide_input_to_partitions_process.h:52
std::size_t IndexType
Definition: morton_divide_input_to_partitions_process.h:61
std::string Info() const override
Turn back information as a string.
Definition: morton_divide_input_to_partitions_process.h:206
KRATOS_CLASS_POINTER_DEFINITION(MortonDivideInputToPartitionsProcess)
Pointer definition of MortonDivideInputToPartitionsProcess.
MortonDivideInputToPartitionsProcess(MortonDivideInputToPartitionsProcess const &rOther)
Copy constructor.
Definition: morton_divide_input_to_partitions_process.h:75
void ConditionsPartitioning(IO::ConnectivitiesContainerType &ConditionsConnectivities, MortonPartitioningProcess::PartitionIndicesType const &NodesPartitions, MortonPartitioningProcess::PartitionIndicesType &ConditionsPartitions)
Definition: morton_divide_input_to_partitions_process.h:296
void DividingNodes(IO::PartitionIndicesContainerType &rNodesAllPartitions, IO::ConnectivitiesContainerType &ElementsConnectivities, IO::ConnectivitiesContainerType &ConditionsConnectivities, MortonPartitioningProcess::PartitionIndicesType const &NodesPartitions, MortonPartitioningProcess::PartitionIndicesType const &ElementsPartitions, MortonPartitioningProcess::PartitionIndicesType const &ConditionsPartitions)
Definition: morton_divide_input_to_partitions_process.h:343
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: morton_divide_input_to_partitions_process.h:100
void CalculateDomainsGraph(GraphType &rDomainsGraph, SizeType NumberOfElements, IO::ConnectivitiesContainerType &ElementsConnectivities, MortonPartitioningProcess::PartitionIndicesType const &NPart, MortonPartitioningProcess::PartitionIndicesType const &EPart)
Definition: morton_divide_input_to_partitions_process.h:171
void operator()()
Definition: morton_divide_input_to_partitions_process.h:90
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: morton_divide_input_to_partitions_process.h:212
void DividingElements(IO::PartitionIndicesContainerType &rElementsAllPartitions, MortonPartitioningProcess::PartitionIndicesType const &ElementsPartitions)
Definition: morton_divide_input_to_partitions_process.h:411
virtual ~MortonDivideInputToPartitionsProcess()
Destructor.
Definition: morton_divide_input_to_partitions_process.h:81
std::size_t SizeType
Definition: morton_divide_input_to_partitions_process.h:60
IO & mrIO
Definition: morton_divide_input_to_partitions_process.h:279
void DividingConditions(IO::PartitionIndicesContainerType &rConditionsAllPartitions, MortonPartitioningProcess::PartitionIndicesType const &ConditionsPartitions)
Definition: morton_divide_input_to_partitions_process.h:424
MortonDivideInputToPartitionsProcess(IO &rIO, SizeType NumberOfPartitions, int Dimension=3)
Default constructor.
Definition: morton_divide_input_to_partitions_process.h:69
GraphColoringProcess::GraphType GraphType
Definition: morton_divide_input_to_partitions_process.h:62
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: morton_divide_input_to_partitions_process.h:218
SizeType mNumberOfPartitions
Definition: morton_divide_input_to_partitions_process.h:281
SizeType mDimension
Definition: morton_divide_input_to_partitions_process.h:283
Short class definition.
Definition: morton_partitioning_process.h:88
std::vector< idxtype > PartitionIndicesType
Definition: morton_partitioning_process.h:100
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: morton_partitioning_process.h:152
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
The base class for all processes in Kratos.
Definition: process.h:49
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_WATCH(variable)
Definition: define.h:806
#define KRATOS_TRY
Definition: define.h:109
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
scalar_matrix< double > ScalarMatrix
Definition: amatrix_interface.h:728
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432