binaryTree.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2024 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 /*---------------------------------------------------------------------------*\
55  Class binaryTree Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 class binaryTree
59 {
60  // Private Data
61 
62  //- Reference to the ISAT table
64 
65  //- Root node of the binary tree
66  binaryNode *root_;
67 
68  //- Maximum number of elements in the binary tree
69  label maxNLeafs_;
70 
71  //- Size of the BST (= number of chemPoint stored)
72  label size_;
73 
74  //- Secondary retrieve search variables
75  label n2ndSearch_;
76 
77  //- Secondary retrieve search variables
78  label max2ndSearch_;
79 
80  label maxNumNewDim_;
81 
82  Switch printProportion_;
83 
84 
85  // Private Member Functions
86 
87  //- Insert new node at the position of phi0. phi0 should be already
88  // attached to another node or the pointer to it will be lost.
89  inline void insertNode(chemPointISAT*& phi0, binaryNode*& newNode);
90 
91  //- Perform a search in the subtree starting from the subtree node y.
92  // This search continues to use the hyperplane to walk the tree.
93  // If covering EOA is found return true and x points to the chemPoint.
94  bool inSubTree
95  (
96  const scalarField& phiq,
97  binaryNode* y,
99  );
100 
101  inline void deleteSubTree(binaryNode* subTreeRoot);
102 
103  inline void deleteSubTree();
104 
105  //- Replace the binaryNode u with v
106  inline void transplant(binaryNode* u, binaryNode* v);
107 
108  inline chemPointISAT* chemPSibling(binaryNode* y);
109 
110  inline chemPointISAT* chemPSibling(chemPointISAT* x);
111 
112  inline binaryNode* nodeSibling(binaryNode* y);
113 
114  inline binaryNode* nodeSibling(chemPointISAT* x);
115 
116  inline void deleteAllNode(binaryNode* subTreeRoot);
117 
118 
119 public:
120 
121  //- Constructors
122 
123  //- Construct from dictionary and chemistryOnLineLibrary
124  binaryTree
125  (
127  const dictionary& coeffDict
128  );
129 
130 
131  //- Member functions
132 
133  inline label size() const;
134 
135  //- Computes iteratively the depth of the subTree
136  inline label depth(binaryNode* subTreeRoot) const;
137 
138  inline label depth() const;
139 
140  inline binaryNode* root();
141 
142  inline label maxNLeafs() const;
143 
144  // Insert a new leaf starting from the parent node of phi0
145  // Parameters: phi0 the leaf to replace by a node
146  // phiq the new composition to store
147  // Rphiq the mapping of the new composition point
148  // A the mapping gradient matrix
149  // B the matrix used to initialise the EOA
150  // nCols the size of the matrix
151  // Returns: void
152  // Description :
153  //1) Create a new leaf with the data to initialise the EOA and to
154  // retrieve the mapping by linear interpolation (the EOA is
155  // initialise in the chemPoint constructor)
156  //2) Get the parent node of phi0 and connect a new node in place of the
157  // leaf of phi0. This new node is constructed with phi0 on the left
158  // and phiq on the right (the hyperplane is computed inside the
159  // binaryNode constructor)
160  void insertNewLeaf
161  (
162  const scalarField& phiq,
163  const scalarField& Rphiq,
164  const scalarSquareMatrix& A,
165  const scalarField& scaleFactor,
166  const scalar& epsTol,
167  const label nCols,
168  const label nActive,
170  );
171 
172  // Search the binaryTree until the nearest leaf of a specified
173  // leaf is found.
174  void binaryTreeSearch
175  (
176  const scalarField& phiq,
177  binaryNode* node,
178  chemPointISAT*& nearest
179  );
180 
181  // Perform a secondary binary tree search starting from a failed
182  // chemPoint x, with a depth-first search algorithm
183  // If another candidate is found return true and x points to the chemP
184  bool secondaryBTSearch(const scalarField& phiq, chemPointISAT*& x);
185 
186  //- Delete a leaf from the binary tree and reshape the binary tree for
187  // the following binary tree search
188  // Return the index in the nodeList of the removed node
189  // (-1 when no node)
190  void deleteLeaf(chemPointISAT*& phi0);
191 
192  //- Cheap balance function
193  // This function just roughly separate the space in two parts
194  // with a hyperplane which separate the two extreme chemPoint in the
195  // direction of the maximum the variance
196  // Then, it repopulate the tree with this hyperplane stored at the root
197  // and by inserting the chemPoint in increasing order of value in that
198  // direction
199  void balance();
200 
201  inline void deleteAllNode()
202  {
203  deleteAllNode(root_);
204  }
205 
206  inline chemPointISAT* treeMin(binaryNode* subTreeRoot);
207 
208  inline chemPointISAT* treeMin()
209  {
210  return treeMin(root_);
211  }
212 
214 
215  //- Removes every entries of the tree and delete the associated objects
216  inline void clear();
217 
218  //- ListFull
219  inline bool isFull();
220 
221  inline void resetNumRetrieve();
222 };
223 
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 } // End namespace Foam
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
231 #include "binaryTreeI.H"
232 
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 
235 #endif
236 
237 // ************************************************************************* //
scalar y
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
Node of the binary tree.
Definition: binaryNode.H:49
Data storage of the chemistryOnLineLibrary according to a binary tree structure.
Definition: binaryTree.H:58
void resetNumRetrieve()
Definition: binaryTreeI.H:365
void deleteLeaf(chemPointISAT *&phi0)
Delete a leaf from the binary tree and reshape the binary tree for.
Definition: binaryTree.C:299
binaryNode * root()
Definition: binaryTreeI.H:264
chemPointISAT * treeMin()
Definition: binaryTree.H:207
bool isFull()
ListFull.
Definition: binaryTreeI.H:359
label size() const
Member functions.
Definition: binaryTreeI.H:232
void binaryTreeSearch(const scalarField &phiq, binaryNode *node, chemPointISAT *&nearest)
Definition: binaryTreeI.H:277
bool secondaryBTSearch(const scalarField &phiq, chemPointISAT *&x)
Definition: binaryTree.C:243
chemPointISAT * treeSuccessor(chemPointISAT *x)
Definition: binaryTree.C:459
void insertNewLeaf(const scalarField &phiq, const scalarField &Rphiq, const scalarSquareMatrix &A, const scalarField &scaleFactor, const scalar &epsTol, const label nCols, const label nActive, chemPointISAT *&phi0)
Definition: binaryTree.C:156
void deleteAllNode()
Definition: binaryTree.H:200
label maxNLeafs() const
Definition: binaryTreeI.H:270
void clear()
Removes every entries of the tree and delete the associated objects.
Definition: binaryTreeI.H:346
label depth() const
Definition: binaryTreeI.H:258
void balance()
Cheap balance function.
Definition: binaryTree.C:361
binaryTree(chemistryTabulationMethods::ISAT &table, const dictionary &coeffDict)
Constructors.
Definition: binaryTree.C:138
Leaf of the binary tree. The chemPoint stores the composition 'phi', the mapping of this composition ...
Implementation of the ISAT (In-situ adaptive tabulation), for chemistry calculation.
Definition: ISAT.H:63
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
const dimensionedScalar phi0
Magnetic flux quantum: default SI units: [Wb].
static const coefficient A("A", dimPressure, 611.21)
Namespace for OpenFOAM.
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