vtkPVFoamUtils.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 "vtkPVFoam.H"
30 #include "vtkPVFoamReader.h"
31 
32 // OpenFOAM includes
33 #include "fvMesh.H"
34 #include "Time.H"
35 #include "IFstream.H"
36 #include "memInfo.H"
37 
38 // VTK includes
39 #include "vtkDataArraySelection.h"
40 #include "vtkDataSet.h"
41 #include "vtkMultiBlockDataSet.h"
42 #include "vtkInformation.h"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
49  // Extract up to the first non-word characters
50  inline word getFirstWord(const char* str)
51  {
52  if (str)
53  {
54  label n = 0;
55  while (str[n] && word::valid(str[n]))
56  {
57  ++n;
58  }
59  return word(str, n, true);
60  }
61  else
62  {
63  return word::null;
64  }
65 
66  }
68 
69 } // End namespace Foam
70 
71 
72 
73 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
74 
75 void Foam::vtkPVFoam::AddToBlock
76 (
77  vtkMultiBlockDataSet* output,
78  vtkDataSet* dataset,
79  const arrayRange& range,
80  const label datasetNo,
81  const std::string& datasetName
82 )
83 {
84  const int blockNo = range.block();
85 
86  vtkDataObject* blockDO = output->GetBlock(blockNo);
87  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
88 
89  if (!block)
90  {
91  if (blockDO)
92  {
94  << "Block already has a vtkDataSet assigned to it"
95  << endl;
96  return;
97  }
98 
99  block = vtkMultiBlockDataSet::New();
100  output->SetBlock(blockNo, block);
101  block->Delete();
102  }
103 
104  if (debug)
105  {
106  Info<< "block[" << blockNo << "] has "
107  << block->GetNumberOfBlocks()
108  << " datasets prior to adding set " << datasetNo
109  << " with name: " << datasetName << endl;
110  }
111 
112  block->SetBlock(datasetNo, dataset);
113 
114  // name the block when assigning dataset 0
115  if (datasetNo == 0)
116  {
117  output->GetMetaData(blockNo)->Set
118  (
119  vtkCompositeDataSet::NAME(),
120  range.name()
121  );
122  }
123 
124  if (datasetName.size())
125  {
126  block->GetMetaData(datasetNo)->Set
127  (
128  vtkCompositeDataSet::NAME(),
129  datasetName.c_str()
130  );
131  }
132 }
133 
134 
135 vtkDataSet* Foam::vtkPVFoam::GetDataSetFromBlock
136 (
137  vtkMultiBlockDataSet* output,
138  const arrayRange& range,
139  const label datasetNo
140 )
141 {
142  const int blockNo = range.block();
143 
144  vtkDataObject* blockDO = output->GetBlock(blockNo);
145  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
146 
147  if (block)
148  {
149  return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
150  }
151 
152  return 0;
153 }
154 
155 
156 Foam::label Foam::vtkPVFoam::GetNumberOfDataSets
157 (
158  vtkMultiBlockDataSet* output,
159  const arrayRange& range
160 )
161 {
162  const int blockNo = range.block();
163 
164  vtkDataObject* blockDO = output->GetBlock(blockNo);
165  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
166  if (block)
167  {
168  return block->GetNumberOfBlocks();
169  }
170 
171  return 0;
172 }
173 
174 
175 Foam::word Foam::vtkPVFoam::getPartName(const int partId)
176 {
177  return getFirstWord(reader_->GetPartArrayName(partId));
178 }
179 
180 
181 Foam::wordHashSet Foam::vtkPVFoam::getSelected
182 (
183  vtkDataArraySelection* select
184 )
185 {
186  int nElem = select->GetNumberOfArrays();
187  wordHashSet selections(2*nElem);
188 
189  for (int elemI=0; elemI < nElem; ++elemI)
190  {
191  if (select->GetArraySetting(elemI))
192  {
193  selections.insert(getFirstWord(select->GetArrayName(elemI)));
194  }
195  }
196 
197  return selections;
198 }
199 
200 
201 Foam::wordHashSet Foam::vtkPVFoam::getSelected
202 (
203  vtkDataArraySelection* select,
204  const arrayRange& range
205 )
206 {
207  int nElem = select->GetNumberOfArrays();
208  wordHashSet selections(2*nElem);
209 
210  for (int elemI = range.start(); elemI < range.end(); ++elemI)
211  {
212  if (select->GetArraySetting(elemI))
213  {
214  selections.insert(getFirstWord(select->GetArrayName(elemI)));
215  }
216  }
217 
218  return selections;
219 }
220 
221 
222 Foam::stringList Foam::vtkPVFoam::getSelectedArrayEntries
223 (
224  vtkDataArraySelection* select
225 )
226 {
227  stringList selections(select->GetNumberOfArrays());
228  label nElem = 0;
229 
230  forAll(selections, elemI)
231  {
232  if (select->GetArraySetting(elemI))
233  {
234  selections[nElem++] = select->GetArrayName(elemI);
235  }
236  }
237  selections.setSize(nElem);
238 
239 
240  if (debug)
241  {
242  label nElem = select->GetNumberOfArrays();
243  Info<< "available(";
244  for (int elemI = 0; elemI < nElem; ++elemI)
245  {
246  Info<< " \"" << select->GetArrayName(elemI) << "\"";
247  }
248  Info<< " )\nselected(";
249 
250  forAll(selections, elemI)
251  {
252  Info<< " " << selections[elemI];
253  }
254  Info<< " )\n";
255  }
256 
257  return selections;
258 }
259 
260 
261 Foam::stringList Foam::vtkPVFoam::getSelectedArrayEntries
262 (
263  vtkDataArraySelection* select,
264  const arrayRange& range
265 )
266 {
267  stringList selections(range.size());
268  label nElem = 0;
269 
270  for (int elemI = range.start(); elemI < range.end(); ++elemI)
271  {
272  if (select->GetArraySetting(elemI))
273  {
274  selections[nElem++] = select->GetArrayName(elemI);
275  }
276  }
277  selections.setSize(nElem);
278 
279 
280  if (debug)
281  {
282  Info<< "available(";
283  for (int elemI = range.start(); elemI < range.end(); ++elemI)
284  {
285  Info<< " \"" << select->GetArrayName(elemI) << "\"";
286  }
287  Info<< " )\nselected(";
288 
289  forAll(selections, elemI)
290  {
291  Info<< " " << selections[elemI];
292  }
293  Info<< " )\n";
294  }
295 
296  return selections;
297 }
298 
299 
300 void Foam::vtkPVFoam::setSelectedArrayEntries
301 (
302  vtkDataArraySelection* select,
303  const stringList& selections
304 )
305 {
306  const int nElem = select->GetNumberOfArrays();
307  select->DisableAllArrays();
308 
309  // Loop through entries, setting values from selectedEntries
310  for (int elemI=0; elemI < nElem; ++elemI)
311  {
312  string arrayName(select->GetArrayName(elemI));
313 
314  forAll(selections, elemI)
315  {
316  if (selections[elemI] == arrayName)
317  {
318  select->EnableArray(arrayName.c_str());
319  break;
320  }
321  }
322  }
323 }
324 
325 
326 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
327 
329 {
330  memInfo mem;
331 
332  if (mem.valid())
333  {
334  Info<< "mem peak/size/rss: " << mem << "\n";
335  }
336 }
337 
338 
339 // ************************************************************************* //
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
static void printMemory()
Simple memory used debugging information.
#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)
A class for handling words, derived from string.
Definition: word.H:59
static const word null
An empty word.
Definition: word.H:77
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:207
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:82
Memory usage information for the process running this object.
Definition: memInfo.H:61
List< string > stringList
A List of strings.
Definition: stringList.H:50
messageStream Info
label n
Namespace for OpenFOAM.