FreeFOAM The Cross-Platform CFD Toolkit
searchableSurfaces.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 "searchableSurfaces.H"
28 #include <OpenFOAM/ListOps.H>
29 #include <OpenFOAM/Time.H>
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 defineTypeNameAndDebug(searchableSurfaces, 0);
37 
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
43 // Construct with length.
44 Foam::searchableSurfaces::searchableSurfaces(const label size)
45 :
47  regionNames_(size),
48  allSurfaces_(identity(size))
49 {}
50 
51 
52 //Foam::searchableSurfaces::searchableSurfaces
53 //(
54 // const IOobject& io,
55 // const PtrList<dictionary>& dicts
56 //)
57 //:
58 // PtrList<searchableSurface>(dicts.size()),
59 // regionNames_(dicts.size()),
60 // allSurfaces_(identity(dicts.size()))
61 //{
62 // forAll(dicts, surfI)
63 // {
64 // const dictionary& dict = dicts[surfI];
65 //
66 // // Make IOobject with correct name
67 // autoPtr<IOobject> namedIO(io.clone());
68 // namedIO().rename(dict.lookup("name"));
69 //
70 // // Create and hook surface
71 // set
72 // (
73 // surfI,
74 // searchableSurface::New
75 // (
76 // dict.lookup("type"),
77 // namedIO(),
78 // dict
79 // )
80 // );
81 // const searchableSurface& s = operator[](surfI);
82 //
83 // // Construct default region names by prepending surface name
84 // // to region name.
85 // const wordList& localNames = s.regions();
86 //
87 // wordList globalNames(localNames.size());
88 // forAll(localNames, regionI)
89 // {
90 // globalNames[regionI] = s.name() + '_' + localNames[regionI];
91 // }
92 //
93 // // See if dictionary provides any global region names.
94 // if (dict.found("regions"))
95 // {
96 // const dictionary& regionsDict = dict.subDict("regions");
97 //
98 // forAllConstIter(dictionary, regionsDict, iter)
99 // {
100 // const word& key = iter().keyword();
101 //
102 // if (regionsDict.isDict(key))
103 // {
104 // // Get the dictionary for region iter.key()
105 // const dictionary& regionDict = regionsDict.subDict(key);
106 //
107 // label index = findIndex(localNames, key);
108 //
109 // if (index == -1)
110 // {
111 // FatalErrorIn
112 // (
113 // "searchableSurfaces::searchableSurfaces"
114 // "( const IOobject&, const dictionary&)"
115 // ) << "Unknown region name " << key
116 // << " for surface " << s.name() << endl
117 // << "Valid region names are " << localNames
118 // << exit(FatalError);
119 // }
120 //
121 // globalNames[index] = word(regionDict.lookup("name"));
122 // }
123 // }
124 // }
125 //
126 // // Now globalNames contains the names of the regions.
127 // Info<< "Surface:" << s.name() << " has regions:"
128 // << endl;
129 // forAll(globalNames, regionI)
130 // {
131 // Info<< " " << globalNames[regionI] << endl;
132 // }
133 //
134 // // Create reverse lookup
135 // forAll(globalNames, regionI)
136 // {
137 // regionNames_.insert
138 // (
139 // globalNames[regionI],
140 // labelPair(surfI, regionI)
141 // );
142 // }
143 // }
144 //}
145 
146 
147 Foam::searchableSurfaces::searchableSurfaces
148 (
149  const IOobject& io,
150  const dictionary& topDict
151 )
152 :
153  PtrList<searchableSurface>(topDict.size()),
154  names_(topDict.size()),
155  regionNames_(topDict.size()),
156  allSurfaces_(identity(topDict.size()))
157 {
158  label surfI = 0;
159  forAllConstIter(dictionary, topDict, iter)
160  {
161  const word& key = iter().keyword();
162 
163  if (!topDict.isDict(key))
164  {
166  (
167  "searchableSurfaces::searchableSurfaces"
168  "( const IOobject&, const dictionary&)"
169  ) << "Found non-dictionary entry " << iter()
170  << " in top-level dictionary " << topDict
171  << exit(FatalError);
172  }
173 
174  const dictionary& dict = topDict.subDict(key);
175 
176  names_[surfI] = key;
177  dict.readIfPresent("name", names_[surfI]);
178 
179  // Make IOobject with correct name
180  autoPtr<IOobject> namedIO(io.clone());
181  // Note: we would like to e.g. register triSurface 'sphere.stl' as
182  // 'sphere'. Unfortunately
183  // no support for having object read from different location than
184  // their object name. Maybe have stlTriSurfaceMesh which appends .stl
185  // when reading/writing?
186  namedIO().rename(key); // names_[surfI]
187 
188  // Create and hook surface
189  set
190  (
191  surfI,
193  (
194  dict.lookup("type"),
195  namedIO(),
196  dict
197  )
198  );
199  const searchableSurface& s = operator[](surfI);
200 
201  // Construct default region names by prepending surface name
202  // to region name.
203  const wordList& localNames = s.regions();
204 
205  wordList& rNames = regionNames_[surfI];
206  rNames.setSize(localNames.size());
207 
208  forAll(localNames, regionI)
209  {
210  rNames[regionI] = names_[surfI] + '_' + localNames[regionI];
211  }
212 
213  // See if dictionary provides any global region names.
214  if (dict.found("regions"))
215  {
216  const dictionary& regionsDict = dict.subDict("regions");
217 
218  forAllConstIter(dictionary, regionsDict, iter)
219  {
220  const word& key = iter().keyword();
221 
222  if (regionsDict.isDict(key))
223  {
224  // Get the dictionary for region iter.keyword()
225  const dictionary& regionDict = regionsDict.subDict(key);
226 
227  label index = findIndex(localNames, key);
228 
229  if (index == -1)
230  {
232  (
233  "searchableSurfaces::searchableSurfaces"
234  "( const IOobject&, const dictionary&)"
235  ) << "Unknown region name " << key
236  << " for surface " << s.name() << endl
237  << "Valid region names are " << localNames
238  << exit(FatalError);
239  }
240 
241  rNames[index] = word(regionDict.lookup("name"));
242  }
243  }
244  }
245 
246  surfI++;
247  }
248 
249  // Trim (not really necessary since we don't allow non-dictionary entries)
251  names_.setSize(surfI);
252  regionNames_.setSize(surfI);
253  allSurfaces_.setSize(surfI);
254 }
255 
256 
257 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
258 
259 Foam::label Foam::searchableSurfaces::findSurfaceID(const word& wantedName)
260  const
261 {
262  return findIndex(names_, wantedName);
263 }
264 
265 
266 // Find any intersection
268 (
269  const pointField& start,
270  const pointField& end,
271  labelList& hitSurfaces,
272  List<pointIndexHit>& hitInfo
273 ) const
274 {
276  (
277  *this,
278  allSurfaces_,
279  start,
280  end,
281  hitSurfaces,
282  hitInfo
283  );
284 }
285 
286 
287 // Find intersections of edge nearest to both endpoints.
289 (
290  const pointField& start,
291  const pointField& end,
292  labelListList& hitSurfaces,
293  List<List<pointIndexHit> >& hitInfo
294 ) const
295 {
297  (
298  *this,
299  allSurfaces_,
300  start,
301  end,
302  hitSurfaces,
303  hitInfo
304  );
305 }
306 
307 
308 // Find nearest. Return -1 or nearest point
310 (
311  const pointField& samples,
312  const scalarField& nearestDistSqr,
313  labelList& nearestSurfaces,
314  List<pointIndexHit>& nearestInfo
315 ) const
316 {
318  (
319  *this,
320  allSurfaces_,
321  samples,
322  nearestDistSqr,
323  nearestSurfaces,
324  nearestInfo
325  );
326 }
327 
328 
329 //- Calculate point which is on a set of surfaces.
331 (
332  const scalar initDistSqr,
333  const scalar convergenceDistSqr,
334  const point& start
335 ) const
336 {
338  (
339  *this,
340  allSurfaces_,
341  initDistSqr,
342  convergenceDistSqr,
343  start
344  );
345 }
346 
347 
348 // ************************ vim: set sw=4 sts=4 et: ************************ //