vtkPVblockMeshUtils.C
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) 2011-2018 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 "vtkPVblockMesh.H"
30 #include "vtkPVblockMeshReader.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::vtkPVblockMesh::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  {
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::vtkPVblockMesh::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 Foam::label Foam::vtkPVblockMesh::GetNumberOfDataSets
151 (
152  vtkMultiBlockDataSet* output,
153  const arrayRange& range
154 )
155 {
156  const int blockNo = range.block();
157 
158  vtkDataObject* blockDO = output->GetBlock(blockNo);
159  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
160  if (block)
161  {
162  return block->GetNumberOfBlocks();
163  }
164 
165  return 0;
166 }
167 
168 
169 Foam::wordHashSet Foam::vtkPVblockMesh::getSelected
170 (
171  vtkDataArraySelection* select
172 )
173 {
174  int nElem = select->GetNumberOfArrays();
175  wordHashSet selections(2*nElem);
176 
177  for (int elemI=0; elemI < nElem; ++elemI)
178  {
179  if (select->GetArraySetting(elemI))
180  {
181  selections.insert(getFirstWord(select->GetArrayName(elemI)));
182  }
183  }
184 
185  return selections;
186 }
187 
188 
189 Foam::wordHashSet Foam::vtkPVblockMesh::getSelected
190 (
191  vtkDataArraySelection* select,
192  const arrayRange& range
193 )
194 {
195  int nElem = select->GetNumberOfArrays();
196  wordHashSet selections(2*nElem);
197 
198  for (int elemI = range.start(); elemI < range.end(); ++elemI)
199  {
200  if (select->GetArraySetting(elemI))
201  {
202  selections.insert(getFirstWord(select->GetArrayName(elemI)));
203  }
204  }
205 
206  return selections;
207 }
208 
209 
210 Foam::stringList Foam::vtkPVblockMesh::getSelectedArrayEntries
211 (
212  vtkDataArraySelection* select
213 )
214 {
215  stringList selections(select->GetNumberOfArrays());
216  label nElem = 0;
217 
218  forAll(selections, elemI)
219  {
220  if (select->GetArraySetting(elemI))
221  {
222  selections[nElem++] = select->GetArrayName(elemI);
223  }
224  }
225  selections.setSize(nElem);
226 
227 
228  if (debug)
229  {
230  label nElem = select->GetNumberOfArrays();
231  Info<< "available(";
232  for (int elemI = 0; elemI < nElem; ++elemI)
233  {
234  Info<< " \"" << select->GetArrayName(elemI) << "\"";
235  }
236  Info<< " )\nselected(";
237 
238  forAll(selections, elemI)
239  {
240  Info<< " " << selections[elemI];
241  }
242  Info<< " )\n";
243  }
244 
245  return selections;
246 }
247 
248 
249 Foam::stringList Foam::vtkPVblockMesh::getSelectedArrayEntries
250 (
251  vtkDataArraySelection* select,
252  const arrayRange& range
253 )
254 {
255  stringList selections(range.size());
256  label nElem = 0;
257 
258  for (int elemI = range.start(); elemI < range.end(); ++elemI)
259  {
260  if (select->GetArraySetting(elemI))
261  {
262  selections[nElem++] = select->GetArrayName(elemI);
263  }
264  }
265  selections.setSize(nElem);
266 
267 
268  if (debug)
269  {
270  Info<< "available(";
271  for (int elemI = range.start(); elemI < range.end(); ++elemI)
272  {
273  Info<< " \"" << select->GetArrayName(elemI) << "\"";
274  }
275  Info<< " )\nselected(";
276 
277  forAll(selections, elemI)
278  {
279  Info<< " " << selections[elemI];
280  }
281  Info<< " )\n";
282  }
283 
284  return selections;
285 }
286 
287 
288 void Foam::vtkPVblockMesh::setSelectedArrayEntries
289 (
290  vtkDataArraySelection* select,
291  const stringList& selections
292 )
293 {
294  const int nElem = select->GetNumberOfArrays();
295  select->DisableAllArrays();
296 
297  // Loop through entries, setting values from selectedEntries
298  for (int elemI=0; elemI < nElem; ++elemI)
299  {
300  string arrayName(select->GetArrayName(elemI));
301 
302  forAll(selections, elemI)
303  {
304  if (selections[elemI] == arrayName)
305  {
306  select->EnableArray(arrayName.c_str());
307  break;
308  }
309  }
310  }
311 }
312 
313 
314 void Foam::vtkPVblockMesh::updateBoolListStatus
315 (
316  boolList& status,
317  vtkDataArraySelection* selection
318 )
319 {
320  if (debug)
321  {
322  Info<< "<beg> Foam::vtkPVblockMesh::updateBoolListStatus" << endl;
323  }
324 
325  const label nElem = selection->GetNumberOfArrays();
326  if (status.size() != nElem)
327  {
328  status.setSize(nElem);
329  status = false;
330  }
331 
332  forAll(status, elemI)
333  {
334  const int setting = selection->GetArraySetting(elemI);
335 
336  status[elemI] = setting;
337 
338  if (debug)
339  {
340  Info<< " part[" << elemI << "] = "
341  << status[elemI]
342  << " : " << selection->GetArrayName(elemI) << endl;
343  }
344  }
345  if (debug)
346  {
347  Info<< "<end> Foam::vtkPVblockMesh::updateBoolListStatus" << endl;
348  }
349 }
350 
351 
352 
353 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
354 
355 
356 // ************************************************************************* //
static bool valid(char)
Is this character valid for a word.
Definition: wordI.H:115
A HashTable with keys but without contents.
Definition: HashSet.H:59
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
static const word null
An empty word.
Definition: word.H:77
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:207
List< string > stringList
A List of strings.
Definition: stringList.H:50
messageStream Info
label n
Namespace for OpenFOAM.