vtkPV3blockMeshUtils.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) 2011 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 Description
25  Misc helper methods and utilities
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "vtkPV3blockMesh.H"
30 #include "vtkPV3blockMeshReader.h"
31 
32 // VTK includes
33 #include "vtkDataArraySelection.h"
34 #include "vtkDataSet.h"
35 #include "vtkMultiBlockDataSet.h"
36 #include "vtkInformation.h"
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
43  // Extract up to the first non-word characters
44  inline word getFirstWord(const char* str)
45  {
46  if (str)
47  {
48  label n = 0;
49  while (str[n] && word::valid(str[n]))
50  {
51  ++n;
52  }
53  return word(str, n, true);
54  }
55  else
56  {
57  return word::null;
58  }
59 
60  }
62 
63 } // End namespace Foam
64 
65 
66 
67 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
68 
69 void Foam::vtkPV3blockMesh::AddToBlock
70 (
71  vtkMultiBlockDataSet* output,
72  vtkDataSet* dataset,
73  const arrayRange& range,
74  const label datasetNo,
75  const std::string& datasetName
76 )
77 {
78  const int blockNo = range.block();
79 
80  vtkDataObject* blockDO = output->GetBlock(blockNo);
81  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
82 
83  if (!block)
84  {
85  if (blockDO)
86  {
87  FatalErrorIn("Foam::vtkPV3blockMesh::AddToBlock")
88  << "Block already has a vtkDataSet assigned to it"
89  << endl;
90  return;
91  }
92 
93  block = vtkMultiBlockDataSet::New();
94  output->SetBlock(blockNo, block);
95  block->Delete();
96  }
97 
98  if (debug)
99  {
100  Info<< "block[" << blockNo << "] has "
101  << block->GetNumberOfBlocks()
102  << " datasets prior to adding set " << datasetNo
103  << " with name: " << datasetName << endl;
104  }
105 
106  block->SetBlock(datasetNo, dataset);
107 
108  // name the block when assigning dataset 0
109  if (datasetNo == 0)
110  {
111  output->GetMetaData(blockNo)->Set
112  (
113  vtkCompositeDataSet::NAME(),
114  range.name()
115  );
116  }
117 
118  if (datasetName.size())
119  {
120  block->GetMetaData(datasetNo)->Set
121  (
122  vtkCompositeDataSet::NAME(),
123  datasetName.c_str()
124  );
125  }
126 }
127 
128 
129 vtkDataSet* Foam::vtkPV3blockMesh::GetDataSetFromBlock
130 (
131  vtkMultiBlockDataSet* output,
132  const arrayRange& range,
133  const label datasetNo
134 )
135 {
136  const int blockNo = range.block();
137 
138  vtkDataObject* blockDO = output->GetBlock(blockNo);
139  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
140 
141  if (block)
142  {
143  return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
144  }
145 
146  return 0;
147 }
148 
149 
150 // ununsed at the moment
151 Foam::label Foam::vtkPV3blockMesh::GetNumberOfDataSets
152 (
153  vtkMultiBlockDataSet* output,
154  const arrayRange& range
155 )
156 {
157  const int blockNo = range.block();
158 
159  vtkDataObject* blockDO = output->GetBlock(blockNo);
160  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
161  if (block)
162  {
163  return block->GetNumberOfBlocks();
164  }
165 
166  return 0;
167 }
168 
169 
170 Foam::wordHashSet Foam::vtkPV3blockMesh::getSelected
171 (
172  vtkDataArraySelection* select
173 )
174 {
175  int nElem = select->GetNumberOfArrays();
176  wordHashSet selections(2*nElem);
177 
178  for (int elemI=0; elemI < nElem; ++elemI)
179  {
180  if (select->GetArraySetting(elemI))
181  {
182  selections.insert(getFirstWord(select->GetArrayName(elemI)));
183  }
184  }
185 
186  return selections;
187 }
188 
189 
190 Foam::wordHashSet Foam::vtkPV3blockMesh::getSelected
191 (
192  vtkDataArraySelection* select,
193  const arrayRange& range
194 )
195 {
196  int nElem = select->GetNumberOfArrays();
197  wordHashSet selections(2*nElem);
198 
199  for (int elemI = range.start(); elemI < range.end(); ++elemI)
200  {
201  if (select->GetArraySetting(elemI))
202  {
203  selections.insert(getFirstWord(select->GetArrayName(elemI)));
204  }
205  }
206 
207  return selections;
208 }
209 
210 
211 Foam::stringList Foam::vtkPV3blockMesh::getSelectedArrayEntries
212 (
213  vtkDataArraySelection* select
214 )
215 {
216  stringList selections(select->GetNumberOfArrays());
217  label nElem = 0;
218 
219  forAll(selections, elemI)
220  {
221  if (select->GetArraySetting(elemI))
222  {
223  selections[nElem++] = select->GetArrayName(elemI);
224  }
225  }
226  selections.setSize(nElem);
227 
228 
229  if (debug)
230  {
231  label nElem = select->GetNumberOfArrays();
232  Info<< "available(";
233  for (int elemI = 0; elemI < nElem; ++elemI)
234  {
235  Info<< " \"" << select->GetArrayName(elemI) << "\"";
236  }
237  Info<< " )\nselected(";
238 
239  forAll(selections, elemI)
240  {
241  Info<< " " << selections[elemI];
242  }
243  Info<< " )\n";
244  }
245 
246  return selections;
247 }
248 
249 
250 Foam::stringList Foam::vtkPV3blockMesh::getSelectedArrayEntries
251 (
252  vtkDataArraySelection* select,
253  const arrayRange& range
254 )
255 {
256  stringList selections(range.size());
257  label nElem = 0;
258 
259  for (int elemI = range.start(); elemI < range.end(); ++elemI)
260  {
261  if (select->GetArraySetting(elemI))
262  {
263  selections[nElem++] = select->GetArrayName(elemI);
264  }
265  }
266  selections.setSize(nElem);
267 
268 
269  if (debug)
270  {
271  Info<< "available(";
272  for (int elemI = range.start(); elemI < range.end(); ++elemI)
273  {
274  Info<< " \"" << select->GetArrayName(elemI) << "\"";
275  }
276  Info<< " )\nselected(";
277 
278  forAll(selections, elemI)
279  {
280  Info<< " " << selections[elemI];
281  }
282  Info<< " )\n";
283  }
284 
285  return selections;
286 }
287 
288 
289 void Foam::vtkPV3blockMesh::setSelectedArrayEntries
290 (
291  vtkDataArraySelection* select,
292  const stringList& selections
293 )
294 {
295  const int nElem = select->GetNumberOfArrays();
296  select->DisableAllArrays();
297 
298  // Loop through entries, setting values from selectedEntries
299  for (int elemI=0; elemI < nElem; ++elemI)
300  {
301  string arrayName(select->GetArrayName(elemI));
302 
303  forAll(selections, elemI)
304  {
305  if (selections[elemI] == arrayName)
306  {
307  select->EnableArray(arrayName.c_str());
308  break;
309  }
310  }
311  }
312 }
313 
314 
315 void Foam::vtkPV3blockMesh::updateBoolListStatus
316 (
317  boolList& status,
318  vtkDataArraySelection* selection
319 )
320 {
321  if (debug)
322  {
323  Info<< "<beg> Foam::vtkPV3blockMesh::updateBoolListStatus" << endl;
324  }
325 
326  const label nElem = selection->GetNumberOfArrays();
327  if (status.size() != nElem)
328  {
329  status.setSize(nElem);
330  status = false;
331  }
332 
333  forAll(status, elemI)
334  {
335  const int setting = selection->GetArraySetting(elemI);
336 
337  status[elemI] = setting;
338 
339  if (debug)
340  {
341  Info<< " part[" << elemI << "] = "
342  << status[elemI]
343  << " : " << selection->GetArrayName(elemI) << endl;
344  }
345  }
346  if (debug)
347  {
348  Info<< "<end> Foam::vtkPV3blockMesh::updateBoolListStatus" << endl;
349  }
350 }
351 
352 
353 
354 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
355 
356 
357 // ************************************************************************* //
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:207
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
List< string > stringList
A List of strings.
Definition: stringList.H:50
messageStream Info
Namespace for OpenFOAM.
autoPtr< BasicCompressibleTurbulenceModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleTurbulenceModel::transportModel &transport, const word &propertiesName)
label n
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool valid(char)
Is this character valid for a word.
Definition: wordI.H:117
#define forAll(list, i)
Definition: UList.H:421
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A HashTable with keys but without contents.
Definition: HashSet.H:59
static const word null
An empty word.
Definition: word.H:77
List< bool > boolList
Bool container classes.
Definition: boolList.H:50