FreeFOAM The Cross-Platform CFD Toolkit
normalToFace.C
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) 1991-2010 OpenCFD Ltd.
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 \*---------------------------------------------------------------------------*/
25 
26 #include "normalToFace.H"
27 #include <OpenFOAM/polyMesh.H>
28 #include <meshTools/faceSet.H>
29 
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 defineTypeNameAndDebug(normalToFace, 0);
38 
39 addToRunTimeSelectionTable(topoSetSource, normalToFace, word);
40 
41 addToRunTimeSelectionTable(topoSetSource, normalToFace, istream);
42 
43 }
44 
45 
46 Foam::topoSetSource::addToUsageTable Foam::normalToFace::usage_
47 (
48  normalToFace::typeName,
49  "\n Usage: normalToFace (nx ny nz) <tol>\n\n"
50  " Select faces with normal aligned to unit vector (nx ny nz)\n"
51  " to within tol\n"
52 );
53 
54 
55 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
56 
57 void Foam::normalToFace::setNormal()
58 {
59  normal_ /= mag(normal_) + VSMALL;
60 
61  Info<< " normalToFace : Normalized vector to " << normal_ << endl;
62 
63  if (tol_ < -1 || tol_ > 1)
64  {
66  (
67  "normalToFace::normalToFace(const polyMesh&, const vector&"
68  ", const scalar)"
69  ) << "tolerance not within range -1..1 : " << tol_
70  << exit(FatalError);
71  }
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
76 
77 // Construct from components
79 (
80  const polyMesh& mesh,
81  const vector& normal,
82  const scalar tol
83 )
84 :
85  topoSetSource(mesh),
86  normal_(normal),
87  tol_(tol)
88 {
89  setNormal();
90 }
91 
92 
93 // Construct from dictionary
95 :
96  topoSetSource(mesh),
97  normal_(dict.lookup("normal")),
98  tol_(readScalar(dict.lookup("cos")))
99 {
100  setNormal();
101 }
102 
103 
104 // Construct from Istream
106 :
107  topoSetSource(mesh),
108  normal_(checkIs(is)),
109  tol_(readScalar(checkIs(is)))
110 {
111  setNormal();
112 }
113 
114 
115 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
116 
118 {}
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 (
125  const topoSetSource::setAction action,
126  topoSet& set
127 ) const
128 {
129  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
130  {
131  Info<< " Adding faces according to normal being aligned with "
132  << normal_ << " (to within " << tol_ << ") ..." << endl;
133 
134  forAll(mesh_.faceAreas(), faceI)
135  {
136  vector n = mesh_.faceAreas()[faceI];
137  n /= mag(n) + VSMALL;
138 
139  if (mag(1 - (n & normal_)) < tol_)
140  {
141  set.insert(faceI);
142  }
143  }
144  }
145  else if (action == topoSetSource::DELETE)
146  {
147  Info<< " Removing faces according to normal being aligned with "
148  << normal_ << " (to within " << tol_ << ") ..." << endl;
149 
150 
151  DynamicList<label> toBeRemoved(set.size()/10);
152 
153  forAllIter(topoSet, set, iter)
154  {
155  label faceI = iter.key();
156 
157  vector n = mesh_.faceAreas()[faceI];
158  n /= mag(n) + VSMALL;
159 
160  if (mag(1 - (n & normal_)) < tol_)
161  {
162  toBeRemoved.append(faceI);
163  }
164  }
165 
166  forAll(toBeRemoved, i)
167  {
168  set.erase(toBeRemoved[i]);
169  }
170  }
171 }
172 
173 
174 // ************************ vim: set sw=4 sts=4 et: ************************ //