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-2019 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  {
101  << "block[" << blockNo << "] has "
102  << block->GetNumberOfBlocks()
103  << " datasets prior to adding set " << datasetNo
104  << " with name: " << datasetName << endl;
105  }
106 
107  block->SetBlock(datasetNo, dataset);
108 
109  // name the block when assigning dataset 0
110  if (datasetNo == 0)
111  {
112  output->GetMetaData(blockNo)->Set
113  (
114  vtkCompositeDataSet::NAME(),
115  range.name()
116  );
117  }
118 
119  if (datasetName.size())
120  {
121  block->GetMetaData(datasetNo)->Set
122  (
123  vtkCompositeDataSet::NAME(),
124  datasetName.c_str()
125  );
126  }
127 }
128 
129 
130 vtkDataSet* Foam::vtkPVblockMesh::GetDataSetFromBlock
131 (
132  vtkMultiBlockDataSet* output,
133  const arrayRange& range,
134  const label datasetNo
135 )
136 {
137  const int blockNo = range.block();
138 
139  vtkDataObject* blockDO = output->GetBlock(blockNo);
140  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
141 
142  if (block)
143  {
144  return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
145  }
146 
147  return 0;
148 }
149 
150 
151 Foam::label Foam::vtkPVblockMesh::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::vtkPVblockMesh::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::vtkPVblockMesh::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::vtkPVblockMesh::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();
233  << "available(";
234  for (int elemI = 0; elemI < nElem; ++elemI)
235  {
236  Info<< " \"" << select->GetArrayName(elemI) << "\"";
237  }
238  Info<< " )\nselected(";
239 
240  forAll(selections, elemI)
241  {
242  Info<< " " << selections[elemI];
243  }
244  Info<< " )\n";
245  }
246 
247  return selections;
248 }
249 
250 
251 Foam::stringList Foam::vtkPVblockMesh::getSelectedArrayEntries
252 (
253  vtkDataArraySelection* select,
254  const arrayRange& range
255 )
256 {
257  stringList selections(range.size());
258  label nElem = 0;
259 
260  for (int elemI = range.start(); elemI < range.end(); ++elemI)
261  {
262  if (select->GetArraySetting(elemI))
263  {
264  selections[nElem++] = select->GetArrayName(elemI);
265  }
266  }
267  selections.setSize(nElem);
268 
269 
270  if (debug)
271  {
273  << "available(";
274  for (int elemI = range.start(); elemI < range.end(); ++elemI)
275  {
276  Info<< " \"" << select->GetArrayName(elemI) << "\"";
277  }
278  Info<< " )\nselected(";
279 
280  forAll(selections, elemI)
281  {
282  Info<< " " << selections[elemI];
283  }
284  Info<< " )\n";
285  }
286 
287  return selections;
288 }
289 
290 
291 void Foam::vtkPVblockMesh::setSelectedArrayEntries
292 (
293  vtkDataArraySelection* select,
294  const stringList& selections
295 )
296 {
297  const int nElem = select->GetNumberOfArrays();
298  select->DisableAllArrays();
299 
300  // Loop through entries, setting values from selectedEntries
301  for (int elemI=0; elemI < nElem; ++elemI)
302  {
303  string arrayName(select->GetArrayName(elemI));
304 
305  forAll(selections, elemI)
306  {
307  if (selections[elemI] == arrayName)
308  {
309  select->EnableArray(arrayName.c_str());
310  break;
311  }
312  }
313  }
314 }
315 
316 
317 void Foam::vtkPVblockMesh::updateBoolListStatus
318 (
319  boolList& status,
320  vtkDataArraySelection* selection
321 )
322 {
323  if (debug)
324  {
325  InfoInFunction << endl;
326  }
327 
328  const label nElem = selection->GetNumberOfArrays();
329  if (status.size() != nElem)
330  {
331  status.setSize(nElem);
332  status = false;
333  }
334 
335  forAll(status, elemI)
336  {
337  const int setting = selection->GetArraySetting(elemI);
338 
339  status[elemI] = setting;
340 
341  if (debug)
342  {
343  Info<< " part[" << elemI << "] = "
344  << status[elemI]
345  << " : " << selection->GetArrayName(elemI) << endl;
346  }
347  }
348 }
349 
350 
351 // ************************************************************************* //
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:434
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
autoPtr< BasicCompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleMomentumTransportModel::transportModel &transport)
static const word null
An empty word.
Definition: word.H:77
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:208
List< string > stringList
A List of strings.
Definition: stringList.H:50
messageStream Info
label n
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.