binaryTree.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2016-2017 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::binaryTree
26 
27 Description
28 
29  Data storage of the chemistryOnLineLibrary according to a binary
30  tree structure.
31 
32  0 (root node)
33  / \
34  0 0
35  / \ / \
36  L R L 0
37  / \
38  L R
39 
40  L: leafLeft_
41  R: leafRight_
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef binaryTree_H
46 #define binaryTree_H
47 
48 #include "binaryNode.H"
49 #include "chemPointISAT.H"
50 
51 namespace Foam
52 {
53 
54 template<class CompType, class ThermoType>
55 class TDACChemistryModel;
56 
57 template<class CompType, class ThermoType>
58 class binaryTree
59 {
60 
61 public:
64 
65 private:
66 
67  //- Reference to the chemistryModel
69 
70  //- Root node of the binary tree
71  bn *root_;
72 
73  //- Maximum number of elements in the binary tree
74  label maxNLeafs_;
75 
76  //- Size of the BST (= number of chemPoint stored)
77  label size_;
78 
79  //- Secondary retrieve search variables
80  label n2ndSearch_;
81  label max2ndSearch_;
82 
83  //- Insert new node at the position of phi0
84  // phi0 should be already attached to another node or the pointer to it
85  // will be lost
86  void insertNode
87  (
88  chP*& phi0,
89  bn*& newNode
90  );
91 
92  //- Perform a search in the subtree starting from the subtree node y
93  // This search continue to use the hyperplan to walk the tree
94  // If covering EOA is found return true and x points to the chemPoint
95  bool inSubTree
96  (
97  const scalarField& phiq,
98  bn* y,
99  chP* x
100  );
101 
102  void deleteSubTree(binaryNode<CompType, ThermoType>* subTreeRoot);
103 
104  inline void deleteSubTree()
105  {
106  deleteSubTree(root_);
107  }
108 
109  //- Replace the binaryNode u with v
110  void transplant(bn* u, bn* v);
111 
112  chP* chemPSibling(bn* y);
113  chP* chemPSibling(chP* x);
114 
115  bn* nodeSibling(bn* y);
116  bn* nodeSibling(chP* x);
117 
118  void deleteAllNode(bn* subTreeRoot);
119 
120  dictionary coeffsDict_;
121 
122 public:
123  //- Constructors
124 
125  //- Construct from dictionary and chemistryOnLineLibrary
126  binaryTree
127  (
129  dictionary coeffsDict
130  );
131 
132  //- Member functions
133  inline label size()
134  {
135  return size_;
136  }
137 
138  //- Computes iteratively the depth of the subTree
139  label depth(bn* subTreeRoot);
141  inline label depth()
142  {
143  return depth(root_);
144  }
146  inline bn* root()
147  {
148  return root_;
149  }
151  inline label maxNLeafs()
152  {
153  return maxNLeafs_;
154  }
155 
156  // Insert a new leaf starting from the parent node of phi0
157  // Parameters: phi0 the leaf to replace by a node
158  // phiq the new composition to store
159  // Rphiq the mapping of the new composition point
160  // A the mapping gradient matrix
161  // B the matrix used to initialize the EOA
162  // nCols the size of the matrix
163  // Returns: void
164  // Description :
165  //1) Create a new leaf with the data to initialize the EOA and to
166  // retrieve the mapping by linear interpolation (the EOA is
167  // initialize in the chemPoint constructor)
168  //2) Get the parent node of phi0 and connect a new node in place of the
169  // leaf of phi0. This new node is constructed with phi0 on the left
170  // and phiq on the right (the hyperplane is computed inside the
171  // binaryNode constructor)
172  void insertNewLeaf
173  (
174  const scalarField& phiq,
175  const scalarField& Rphiq,
176  const scalarSquareMatrix& A,
177  const scalarField& scaleFactor,
178  const scalar& epsTol,
179  const label nCols,
180  chP*& phi0
181  );
182 
183 
184 
185  // Search the binaryTree until the nearest leaf of a specified
186  // leaf is found.
187  void binaryTreeSearch
188  (
189  const scalarField& phiq,
190  bn* node,
191  chP*& nearest
192  );
193 
194  // Perform a secondary binary tree search starting from a failed
195  // chemPoint x, with a depth-first search algorithm
196  // If another candidate is found return true and x points to the chemP
197  bool secondaryBTSearch(const scalarField& phiq, chP*& x);
198 
199  //- Delete a leaf from the binary tree and reshape the binary tree for
200  // the following binary tree search
201  // Return the index in the nodeList of the removed node
202  // (-1 when no node)
203  void deleteLeaf(chP*& phi0);
204 
205  //- Cheap balance function
206  // This function just roughly separate the space in two parts
207  // with a hyperplane which separate the two extreme chemPoint in the
208  // direction of the maximum the variance
209  // Then, it repopulate the tree with this hyperplane stored at the root
210  // and by inserting the chemPoint in increasing order of value in that
211  // direction
212  void balance();
214  inline void deleteAllNode()
215  {
216  deleteAllNode(root_);
217  }
218 
219  chP* treeMin(bn* subTreeRoot);
221  inline chP* treeMin()
222  {
223  return treeMin(root_);
224  }
225 
226  chP* treeSuccessor(chP* x);
227 
228  //- Removes every entries of the tree and delete the associated objects
229  void clear();
230 
231  //- ListFull
232  bool isFull();
233 
234  void resetNumRetrieve();
235 };
236 
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 } // End namespace Foam
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
244 #ifdef NoRepository
245  #include "binaryTree.C"
246 #endif
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #endif
251 
252 // ************************************************************************* //
Extends chemistryModel by adding the TDAC method.
void resetNumRetrieve()
Definition: binaryTree.C:824
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
chP * treeSuccessor(chP *x)
Definition: binaryTree.C:751
void balance()
Cheap balance function.
Definition: binaryTree.C:641
void insertNewLeaf(const scalarField &phiq, const scalarField &Rphiq, const scalarSquareMatrix &A, const scalarField &scaleFactor, const scalar &epsTol, const label nCols, chP *&phi0)
Definition: binaryTree.C:384
Leaf of the binary tree. The chemPoint stores the composition &#39;phi&#39;, the mapping of this composition ...
const dimensionedScalar phi0
Magnetic flux quantum: default SI units: [Wb].
chemPointISAT< CompType, ThermoType > chP
Definition: binaryTree.H:62
bool secondaryBTSearch(const scalarField &phiq, chP *&x)
Definition: binaryTree.C:521
void binaryTreeSearch(const scalarField &phiq, bn *node, chP *&nearest)
Definition: binaryTree.C:467
scalar y
binaryNode< CompType, ThermoType > bn
Definition: binaryTree.H:61
Data storage of the chemistryOnLineLibrary according to a binary tree structure.
Definition: binaryTree.H:57
void clear()
Removes every entries of the tree and delete the associated objects.
Definition: binaryTree.C:803
Node of the binary tree.
Definition: binaryNode.H:49
binaryTree(TDACChemistryModel< CompType, ThermoType > &chemistry, dictionary coeffsDict)
Constructors.
Definition: binaryTree.C:345
label size()
Member functions.
Definition: binaryTree.H:132
label maxNLeafs()
Definition: binaryTree.H:150
psiChemistryModel & chemistry
Definition: createFields.H:29
void deleteLeaf(chP *&phi0)
Delete a leaf from the binary tree and reshape the binary tree for.
Definition: binaryTree.C:578
bool isFull()
ListFull.
Definition: binaryTree.C:817
void deleteAllNode()
Definition: binaryTree.H:213
Namespace for OpenFOAM.