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