vtkPV3FoamUtils.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-2015 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 "vtkPV3Foam.H"
30 #include "vtkPV3FoamReader.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::vtkPV3Foam::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::vtkPV3Foam::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 // ununsed at the moment
157 Foam::label Foam::vtkPV3Foam::GetNumberOfDataSets
158 (
159  vtkMultiBlockDataSet* output,
160  const arrayRange& range
161 )
162 {
163  const int blockNo = range.block();
164 
165  vtkDataObject* blockDO = output->GetBlock(blockNo);
166  vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
167  if (block)
168  {
169  return block->GetNumberOfBlocks();
170  }
171 
172  return 0;
173 }
174 
175 
176 Foam::word Foam::vtkPV3Foam::getPartName(const int partId)
177 {
178  return getFirstWord(reader_->GetPartArrayName(partId));
179 }
180 
181 
182 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
183 (
184  vtkDataArraySelection* select
185 )
186 {
187  int nElem = select->GetNumberOfArrays();
188  wordHashSet selections(2*nElem);
189 
190  for (int elemI=0; elemI < nElem; ++elemI)
191  {
192  if (select->GetArraySetting(elemI))
193  {
194  selections.insert(getFirstWord(select->GetArrayName(elemI)));
195  }
196  }
197 
198  return selections;
199 }
200 
201 
202 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
203 (
204  vtkDataArraySelection* select,
205  const arrayRange& range
206 )
207 {
208  int nElem = select->GetNumberOfArrays();
209  wordHashSet selections(2*nElem);
210 
211  for (int elemI = range.start(); elemI < range.end(); ++elemI)
212  {
213  if (select->GetArraySetting(elemI))
214  {
215  selections.insert(getFirstWord(select->GetArrayName(elemI)));
216  }
217  }
218 
219  return selections;
220 }
221 
222 
223 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
224 (
225  vtkDataArraySelection* select
226 )
227 {
228  stringList selections(select->GetNumberOfArrays());
229  label nElem = 0;
230 
231  forAll(selections, elemI)
232  {
233  if (select->GetArraySetting(elemI))
234  {
235  selections[nElem++] = select->GetArrayName(elemI);
236  }
237  }
238  selections.setSize(nElem);
239 
240 
241  if (debug)
242  {
243  label nElem = select->GetNumberOfArrays();
244  Info<< "available(";
245  for (int elemI = 0; elemI < nElem; ++elemI)
246  {
247  Info<< " \"" << select->GetArrayName(elemI) << "\"";
248  }
249  Info<< " )\nselected(";
250 
251  forAll(selections, elemI)
252  {
253  Info<< " " << selections[elemI];
254  }
255  Info<< " )\n";
256  }
257 
258  return selections;
259 }
260 
261 
262 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
263 (
264  vtkDataArraySelection* select,
265  const arrayRange& range
266 )
267 {
268  stringList selections(range.size());
269  label nElem = 0;
270 
271  for (int elemI = range.start(); elemI < range.end(); ++elemI)
272  {
273  if (select->GetArraySetting(elemI))
274  {
275  selections[nElem++] = select->GetArrayName(elemI);
276  }
277  }
278  selections.setSize(nElem);
279 
280 
281  if (debug)
282  {
283  Info<< "available(";
284  for (int elemI = range.start(); elemI < range.end(); ++elemI)
285  {
286  Info<< " \"" << select->GetArrayName(elemI) << "\"";
287  }
288  Info<< " )\nselected(";
289 
290  forAll(selections, elemI)
291  {
292  Info<< " " << selections[elemI];
293  }
294  Info<< " )\n";
295  }
296 
297  return selections;
298 }
299 
300 
301 void Foam::vtkPV3Foam::setSelectedArrayEntries
302 (
303  vtkDataArraySelection* select,
304  const stringList& selections
305 )
306 {
307  const int nElem = select->GetNumberOfArrays();
308  select->DisableAllArrays();
309 
310  // Loop through entries, setting values from selectedEntries
311  for (int elemI=0; elemI < nElem; ++elemI)
312  {
313  string arrayName(select->GetArrayName(elemI));
314 
315  forAll(selections, elemI)
316  {
317  if (selections[elemI] == arrayName)
318  {
319  select->EnableArray(arrayName.c_str());
320  break;
321  }
322  }
323  }
324 }
325 
326 
327 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
328 
330 {
331  memInfo mem;
332 
333  if (mem.valid())
334  {
335  Info<< "mem peak/size/rss: " << mem << "\n";
336  }
337 }
338 
339 
340 // ************************************************************************* //
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:82
static bool valid(char)
Is this character valid for a word.
Definition: wordI.H:117
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void printMemory()
Simple memory used debugging information.
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
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.