Microflow 3D  v1.0
Thread.h
Go to the documentation of this file.
1 // ==============================================================================================
2 // Microflow 3D, http://www.microflow.pwr.edu.pl/
3 // Created by Roman Szafran on 18.05.19.
4 // Modified by Roman Szafran on 07.09.19.
5 // Copyright (c) 2019 Wroclaw University of Science and Technology.
6 // Distributed under the Apache License, Version 2.0. You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0 or see accompanying file license.txt.
8 // Redistributions of source code must retain the above copyright and license notice.
9 // ==============================================================================================
10 # pragma once
11 
12 #include <cstdint>
13 #include <memory>
14 #include <vector>
15 #include <omp.h>
16 #include <iostream>
17 #include <algorithm>
18 
19 #include "MFDatabase/Node.h"
21 #include "MFDatabase/Vec3.h"
22 #include "MFDatabase/Vec4.h"
23 #include "GeometryBuilder/NodeID.h"
25 
26 namespace MF {
27  namespace Database {
28 
37  class Thread {
38  public:
39  Thread (uint64_t NodeID, uint32_t NrOfNodes){
40  MF::GB::NodeID nodeTMP;
41  nodeTMP.node_id.NodeID = NodeID;
42  m_NodeArray_Ptr = std::make_shared<std::vector<MF::Database::Node>>(NrOfNodes);
43  m_PropagationTable_Ptr = std::make_shared<NodePropArray>(NrOfNodes);
44  m_SecondHalfPropagationTable_Ptr = std::make_shared<NodePropArray>(NrOfNodes);
45  m_Coordinate_Ptr = std::make_shared<std::vector<MF::Database::Vec3<int32_t>>>(NrOfNodes);
46  m_NodeType = nodeTMP.node_id.Node.nodeType;
47  m_uidThreadNr = nodeTMP.node_id.Node.uidThreadNr;
48  m_ThreadCount = nodeTMP.node_id.Node.ThreadCount;
49  m_ComponentNr = nodeTMP.node_id.Node.ComponentNr;
50  m_PhaseNr = nodeTMP.node_id.Node.PhaseNr; };
51 
52  ~Thread() = default;
53 
58  static std::shared_ptr<MF::Database::Thread> New(const std::shared_ptr<MF::GU::LatticeParametersD3Q19>& LatticeParameters_Ptr, uint64_t NodeID, uint32_t NrOfNodes) {
59  auto Thread_Ptr = std::make_shared<MF::Database::Thread>(NodeID, NrOfNodes);
60  Thread_Ptr->m_Thread_Ptr = Thread_Ptr;
61  MF::Database::Thread::m_LatticeParameters_Ptr = LatticeParameters_Ptr;
62  return Thread_Ptr;
63  }
64 
65  inline static std::shared_ptr<MF::GU::LatticeParametersD3Q19> m_LatticeParameters_Ptr;
66  std::shared_ptr<MF::Database::Thread> m_Thread_Ptr {nullptr};
67  std::string m_ThreadName {""};
68  uint16_t m_NodeType {0};
69  uint16_t m_uidThreadNr {0};
70  uint16_t m_ThreadCount {0};
71  uint8_t m_ComponentNr {0};
72  uint8_t m_PhaseNr {0};
73  bool m_DoPreCollision {false};
74  bool m_DoCollision {false};
75  bool m_DoPropagation {true};
76  bool m_IsInletBalanceSurface {false};
77  bool m_IsOutletBalanceSurface {false};
78 
80  double m_BoundaryValue_RhoLB {1.0};
82 
84  MF::Database::Vec4<double> (*m_pVRLBFunction)(const MF::Database::Node*) {nullptr};
85  void (*m_pColideFunc)(MF::Database::Node*) {nullptr};
86 
87  std::shared_ptr<std::vector<MF::Database::Node>> m_NodeArray_Ptr {nullptr};
88  std::shared_ptr<NodePropArray> m_PropagationTable_Ptr {nullptr};
89  std::shared_ptr<NodePropArray> m_SecondHalfPropagationTable_Ptr {nullptr};
90  std::shared_ptr<std::vector<MF::Database::Vec3<int32_t>>> m_Coordinate_Ptr = {nullptr};
91 
92  // Accessors ----------------------------------------------------------------------------------------------------------------------------------------------------------
93  inline MF::Database::Node* getPropTableNodePointer(const std::size_t &nodeNr, const uint8_t &LinkNr) {return (*m_PropagationTable_Ptr)[nodeNr][LinkNr];};
94  inline MF::Database::Node* getSecPropTableNodePointer(const std::size_t &nodeNr, const uint8_t &LinkNr) {return (*m_SecondHalfPropagationTable_Ptr)[nodeNr][LinkNr];};
95  inline MF::Database::Node* getPropTableNodePointer(const MF::Database::Node* nodePointer, const uint8_t &LinkNr) {std::size_t nodeNr = nodePointer - m_NodeArray_Ptr->data(); return (*m_PropagationTable_Ptr)[nodeNr][LinkNr];};
96  inline std::size_t getNodeNr(const MF::Database::Node* nodePointer) {return nodePointer - m_NodeArray_Ptr->data();};
97  inline MF::Database::Node& getPropTableNodeRef(const std::size_t &nodeNr, const uint8_t &LinkNr) {return *(*m_PropagationTable_Ptr)[nodeNr][LinkNr];};
98  inline MF::Database::Node& getSecondPropTableNodeRef(const std::size_t &nodeNr, const uint8_t &LinkNr) {return *(*m_PropagationTable_Ptr)[nodeNr][LinkNr];};
99  inline void setNodePtrInPropTable(const std::size_t &nodeNr, const uint8_t &LinkNr, MF::Database::Node* nodePtr) { (*m_PropagationTable_Ptr)[nodeNr][LinkNr] = nodePtr;};
100  inline void setNodePtrInSecondPropTable(const std::size_t &nodeNr, const uint8_t &LinkNr, MF::Database::Node* nodePtr) { (*m_SecondHalfPropagationTable_Ptr)[nodeNr][LinkNr] = nodePtr;};
101  inline MF::Database::Node& getPropTableNodeRef(const MF::Database::Node* nodePointer, const uint8_t &LinkNr) {std::size_t nodeNr = nodePointer - m_NodeArray_Ptr->data(); return *(*m_PropagationTable_Ptr)[nodeNr][LinkNr];};
102  inline MF::Database::Node& getNodeRef(const std::size_t &nodeNr) {return (*m_NodeArray_Ptr)[nodeNr];};
103  inline MF::Database::Node* getNodePointer(const std::size_t &nodeNr) {return &(*m_NodeArray_Ptr)[nodeNr];};
104  inline int32_t& getNodeCoordX(const std::size_t &nodeNr){return (*m_Coordinate_Ptr)[nodeNr].x;};
105  inline int32_t& getNodeCoordY(const std::size_t &nodeNr){return (*m_Coordinate_Ptr)[nodeNr].y;};
106  inline int32_t& getNodeCoordZ(const std::size_t &nodeNr){return (*m_Coordinate_Ptr)[nodeNr].z;};
107  inline void setNodeCoordX(const std::size_t &nodeNr, const int32_t& X){(*m_Coordinate_Ptr)[nodeNr].x = X;};
108  inline void setNodeCoordY(const std::size_t &nodeNr, const int32_t& Y){(*m_Coordinate_Ptr)[nodeNr].y = Y;};
109  inline void setNodeCoordZ(const std::size_t &nodeNr, const int32_t& Z){(*m_Coordinate_Ptr)[nodeNr].z = Z;};
110  inline uint32_t getNumberOfNodes () {return m_NodeArray_Ptr->size();};
111 
116  inline MF::Database::Node* getLinkedNodePointer(const std::size_t &nodeNr, const uint8_t f){
117  for (auto k = 0; k < MFQ19_H; k++){
118  auto direction1 = MF::GU::LatticeParametersD3Q19::SwapDirections[k][0];
119  auto direction2 = MF::GU::LatticeParametersD3Q19::SwapDirections[k][1];
120  if (f == direction1)
121  return getPropTableNodePointer(nodeNr, k);
122  else if (f == direction2)
123  return getSecPropTableNodePointer(nodeNr, k);
124  else if (f == 0)
125  return getNodePointer(nodeNr);
126  }
127  return nullptr;
128  };
129 
134  void inline DoCollisionOnly(const int& CPU_ThreadsNr) {
135  std::size_t node;
136  omp_set_num_threads(CPU_ThreadsNr);
137  #pragma omp parallel for default(none) schedule(guided) //OpenMP compilation directive
138  for (node = 0; node < m_NodeArray_Ptr->size(); node++) {
139  (*m_pColideFunc)(&(*m_NodeArray_Ptr)[node]);
140  }
141  };
142 
147  void inline DoPreCollisionOnly(const int& CPU_ThreadsNr) {
148  std::size_t node;
149  omp_set_num_threads(CPU_ThreadsNr); // Number of CPU threads to use
150  #pragma omp parallel for default(none) schedule(guided) //OpenMP compilation directive
151  for (node = 0; node < m_NodeArray_Ptr->size(); node++) {
152  (*m_pBoundaryFunction)(&(*m_NodeArray_Ptr)[node]);
153  }
154  };
155 
160  void inline DoPreANDCollision(const int& CPU_ThreadsNr) {
161  std::size_t node;
162  omp_set_num_threads(CPU_ThreadsNr); // Number of CPU threads to use
163  #pragma omp parallel for default(none) schedule(guided) //OpenMP compilation directive
164  for (node = 0; node < m_NodeArray_Ptr->size(); node++) {
165  (*m_pBoundaryFunction)(&(*m_NodeArray_Ptr)[node]);
166  (*m_pColideFunc)(&(*m_NodeArray_Ptr)[node]);
167  }
168  };
169 
174  void inline DoPropagation(const int& CPU_ThreadsNr) {
175  std::size_t node;
176  uint64_t LinkNr;
177  omp_set_num_threads(CPU_ThreadsNr); // Number of CPU threads to use
178  #pragma omp parallel for schedule(static) default(none) private(LinkNr)//OpenMP compilation directive
179  for (node = 0; node < m_NodeArray_Ptr->size(); node++) {
180  for (LinkNr = 0; LinkNr < MFQ19_H; LinkNr++) {
181  if (getPropTableNodePointer(node, LinkNr) != nullptr) {
182  const auto link1 = m_LatticeParameters_Ptr->SwapDirections[LinkNr][0];
183  const auto link2 = m_LatticeParameters_Ptr->SwapDirections[LinkNr][1];
184  std::swap(getPropTableNodeRef(node, LinkNr).FQ19[link1],(*m_NodeArray_Ptr)[node].FQ19[link2]);
185  }
186  }
187  }
188  };
189  };
190 
191  } /* namespace Database */
192 } /* namespace MF */
bool m_IsInletBalanceSurface
Flag used during post-processing stage. If true, nodes lie on the inlet surface. Value used for manua...
Definition: Thread.h:76
struct MF::GB::NodeID::@0::@1 Node
uint16_t m_uidThreadNr
MFThread number +1 from thread config file. Automatically created threads have uidThreadNr = 0...
Definition: Thread.h:69
int32_t & getNodeCoordZ(const std::size_t &nodeNr)
Returns Y coordinate of node nodeNr.
Definition: Thread.h:106
void DoPreCollisionOnly(const int &CPU_ThreadsNr)
Invokes ONLY the pre-collision function for all nodes.
Definition: Thread.h:147
void DoCollisionOnly(const int &CPU_ThreadsNr)
Invokes the collision function for all nodes.
Definition: Thread.h:134
MFThread is an object that collects some number of Nodes of the same type which shares the same addit...
Definition: Thread.h:37
std::shared_ptr< NodePropArray > m_SecondHalfPropagationTable_Ptr
Propagation table of pointers to nodes. Second half.
Definition: Thread.h:89
MF::Database::Node & getPropTableNodeRef(const MF::Database::Node *nodePointer, const uint8_t &LinkNr)
Sets a pointer to a linked node in a second propagation table.
Definition: Thread.h:101
MF::Database::Node * getSecPropTableNodePointer(const std::size_t &nodeNr, const uint8_t &LinkNr)
Returns a pointer to a linked node from a propagation table.
Definition: Thread.h:94
A class that allows to determine the proper NodeID from combined nodeType, uidThreadNr, ThreadCount, ComponentNr and PhaseNr.
Definition: NodeID.h:18
std::shared_ptr< std::vector< MF::Database::Vec3< int32_t > > > m_Coordinate_Ptr
Original nodes coordinates in a sparse geometry.
Definition: Thread.h:90
MF::Database::Vec3< double > m_BoundaryValue_ULB
Initial or boundary value of LB velocity (x,y,z).
Definition: Thread.h:79
std::shared_ptr< MF::Database::Thread > m_Thread_Ptr
Shared Ptr to MFThread.
Definition: Thread.h:66
Basic data structure for storing f(i) data for each computational grid node.
Definition: Node.h:26
MF::Database::Node & getNodeRef(const std::size_t &nodeNr)
Returns a reference to a linked node from a propagation table.
Definition: Thread.h:102
MF::Database::Node & getSecondPropTableNodeRef(const std::size_t &nodeNr, const uint8_t &LinkNr)
Returns a reference to a linked node from a propagation table.
Definition: Thread.h:98
void setNodeCoordX(const std::size_t &nodeNr, const int32_t &X)
Returns Z coordinate of node nodeNr.
Definition: Thread.h:107
int32_t & getNodeCoordY(const std::size_t &nodeNr)
Returns X coordinate of node nodeNr.
Definition: Thread.h:105
std::string m_ThreadName
Name of MFThread.
Definition: Thread.h:67
MF::Database::Node * getPropTableNodePointer(const std::size_t &nodeNr, const uint8_t &LinkNr)
Definition: Thread.h:93
MF::Database::Node & getPropTableNodeRef(const std::size_t &nodeNr, const uint8_t &LinkNr)
Converts a node pointer to a node number.
Definition: Thread.h:97
bool m_IsOutletBalanceSurface
Flag used during post-processing stage. If true, nodes lie on the outlet surface. Value used for manu...
Definition: Thread.h:77
std::size_t getNodeNr(const MF::Database::Node *nodePointer)
Returns a pointer to a linked node from a propagation table.
Definition: Thread.h:96
static std::shared_ptr< MF::GU::LatticeParametersD3Q19 > m_LatticeParameters_Ptr
Lattice parameters shared pointer.
Definition: Thread.h:65
uint16_t m_NodeType
Node type number.
Definition: Thread.h:68
void(* m_pColideFunc)(MF::Database::Node *)
Velocity and rho function pointer.
Definition: Thread.h:85
uint8_t m_ComponentNr
Component number for multicomponent systems. Max. 255.
Definition: Thread.h:71
void setNodeCoordY(const std::size_t &nodeNr, const int32_t &Y)
Sets X coordinate of node nodeNr.
Definition: Thread.h:108
void setNodeCoordZ(const std::size_t &nodeNr, const int32_t &Z)
Sets Y coordinate of node nodeNr.
Definition: Thread.h:109
void setNodePtrInSecondPropTable(const std::size_t &nodeNr, const uint8_t &LinkNr, MF::Database::Node *nodePtr)
Sets a pointer to a linked node in a propagation table.
Definition: Thread.h:100
MF::Database::Vec3< double > m_XYZForce_LB
Components X, Y, Z of the volumetric force, e.g. gravitational force [N/m3].
Definition: Thread.h:81
Thread(uint64_t NodeID, uint32_t NrOfNodes)
Definition: Thread.h:39
#define f(x)
double m_BoundaryValue_RhoLB
Initial or boundary value of Rho LB.
Definition: Thread.h:80
uint64_t NodeID
Definition: NodeID.h:29
union MF::GB::NodeID::@0 node_id
std::shared_ptr< NodePropArray > m_PropagationTable_Ptr
Propagation table of pointers to nodes. First half.
Definition: Thread.h:88
static std::shared_ptr< MF::Database::Thread > New(const std::shared_ptr< MF::GU::LatticeParametersD3Q19 > &LatticeParameters_Ptr, uint64_t NodeID, uint32_t NrOfNodes)
MFThread allocator.
Definition: Thread.h:58
bool m_DoPreCollision
Flag for pre-collision stage. If true, thread is processed during pre-collision stage.
Definition: Thread.h:73
void DoPreANDCollision(const int &CPU_ThreadsNr)
Invokes the pre-collision and collision functions for all nodes.
Definition: Thread.h:160
void setNodePtrInPropTable(const std::size_t &nodeNr, const uint8_t &LinkNr, MF::Database::Node *nodePtr)
Returns a reference to a linked node from a second propagation table.
Definition: Thread.h:99
#define MFQ19_H
Number of elements (linked directions) in propagation table for one node.
int32_t & getNodeCoordX(const std::size_t &nodeNr)
Returns a pointer to node of nodeNr.
Definition: Thread.h:104
uint32_t getNumberOfNodes()
Sets Z coordinate of node nodeNr.
Definition: Thread.h:110
bool m_DoPropagation
Flag for propagation stage. If true, thread is processed during propagation stage.
Definition: Thread.h:75
MF::Database::Node * getNodePointer(const std::size_t &nodeNr)
Returns a reference to node of nodeNr.
Definition: Thread.h:103
std::shared_ptr< std::vector< MF::Database::Node > > m_NodeArray_Ptr
< Collision function pointer.
Definition: Thread.h:87
void DoPropagation(const int &CPU_ThreadsNr)
Do the propagation for all nodes.
Definition: Thread.h:174
MF::Database::Node * getPropTableNodePointer(const MF::Database::Node *nodePointer, const uint8_t &LinkNr)
Returns a pointer to a linked node from a second propagation table.
Definition: Thread.h:95
uint16_t m_ThreadCount
For partitioned MFThreads, it is a part number. For non partitioned MFThreads = 0;.
Definition: Thread.h:70
bool m_DoCollision
Flag for collision stage. If true, thread is processed during collision stage.
Definition: Thread.h:74
uint8_t m_PhaseNr
Phase number for multiphase flows. Max. 255.
Definition: Thread.h:72
MF::Database::Node * getLinkedNodePointer(const std::size_t &nodeNr, const uint8_t f)
Returns the number of nodes in the thread.
Definition: Thread.h:116
static constexpr uint8_t SwapDirections[MFQ19_H][2]
Indexes of swap directions of f(i) in D3Q19.
void(* m_pBoundaryFunction)(MF::Database::Node *)
Definition: Thread.h:83