BUGFIX: Uninitialised member variables
[foam-extend-3.2.git] / applications / utilities / postProcessing / graphics / PV3FoamReader / vtkPV3Foam / vtkPV3FoamUtilities.C
blob44c4746a454096b2d31a5c8eb3a4454c45473c70
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Misc helper methods and utilities
28 \*---------------------------------------------------------------------------*/
30 #include "vtkPV3Foam.H"
31 #include "vtkPV3FoamReader.h"
33 // Foam includes
34 #include "fvMesh.H"
35 #include "Time.H"
36 #include "IFstream.H"
38 // VTK includes
39 #include "vtkDataArraySelection.h"
40 #include "vtkDataSet.h"
41 #include "vtkMultiBlockDataSet.h"
42 #include "vtkInformation.h"
44 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
46 void Foam::vtkPV3Foam::AddToBlock
48     vtkMultiBlockDataSet* output,
49     vtkDataSet* dataset,
50     const partInfo& selector,
51     const label datasetNo,
52     const string& datasetName
55     const int blockNo = selector.block();
57     vtkDataObject* blockDO = output->GetBlock(blockNo);
58     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
60     if (!block)
61     {
62         if (blockDO)
63         {
64             FatalErrorIn("Foam::vtkPV3Foam::AddToBlock")
65                 << "Block already has a vtkDataSet assigned to it"
66                 << endl;
67             return;
68         }
70         block = vtkMultiBlockDataSet::New();
71         output->SetBlock(blockNo, block);
72         block->Delete();
73     }
75     if (debug)
76     {
77         Info<< "block[" << blockNo << "] has "
78             << block->GetNumberOfBlocks()
79             <<  " datasets prior to adding set " << datasetNo
80             <<  " with name: " << datasetName << endl;
81     }
83     block->SetBlock(datasetNo, dataset);
85     // name the block when assigning dataset 0
86     if (datasetNo == 0)
87     {
88         output->GetMetaData(blockNo)->Set
89         (
90             vtkCompositeDataSet::NAME(),
91             selector.name()
92         );
93     }
95     if (datasetName.size())
96     {
97         block->GetMetaData(datasetNo)->Set
98         (
99             vtkCompositeDataSet::NAME(),
100             datasetName.c_str()
101         );
102     }
106 vtkDataSet* Foam::vtkPV3Foam::GetDataSetFromBlock
108     vtkMultiBlockDataSet* output,
109     const partInfo& selector,
110     const label datasetNo
113     const int blockNo = selector.block();
115     vtkDataObject* blockDO = output->GetBlock(blockNo);
116     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
118     if (block)
119     {
120         return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
121     }
123     return 0;
127 // ununsed at the moment
128 Foam::label Foam::vtkPV3Foam::GetNumberOfDataSets
130     vtkMultiBlockDataSet* output,
131     const partInfo& selector
134     const int blockNo = selector.block();
136     vtkDataObject* blockDO = output->GetBlock(blockNo);
137     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
138     if (block)
139     {
140         return block->GetNumberOfBlocks();
141     }
143     return 0;
147 Foam::word Foam::vtkPV3Foam::getPartName(int partId)
149     return getFirstWord(reader_->GetPartArrayName(partId));
153 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
155     vtkDataArraySelection* select
158     int nElem = select->GetNumberOfArrays();
159     wordHashSet selections(2*nElem);
161     for (int elemI=0; elemI < nElem; ++elemI)
162     {
163         if (select->GetArraySetting(elemI))
164         {
165             selections.insert(getFirstWord(select->GetArrayName(elemI)));
166         }
167     }
169     return selections;
173 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
175     vtkDataArraySelection* select,
176     const partInfo& selector
179     int nElem = select->GetNumberOfArrays();
180     wordHashSet selections(2*nElem);
182     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
183     {
184         if (select->GetArraySetting(elemI))
185         {
186             selections.insert(getFirstWord(select->GetArrayName(elemI)));
187         }
188     }
190     return selections;
194 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
196     vtkDataArraySelection* select
199     stringList selections(select->GetNumberOfArrays());
200     label nElem = 0;
202     forAll(selections, elemI)
203     {
204         if (select->GetArraySetting(elemI))
205         {
206             selections[nElem++] = select->GetArrayName(elemI);
207         }
208     }
209     selections.setSize(nElem);
212     if (debug)
213     {
214         label nElem = select->GetNumberOfArrays();
215         Info<< "available(";
216         for (int elemI = 0; elemI < nElem; ++elemI)
217         {
218             Info<< " \"" << select->GetArrayName(elemI) << "\"";
219         }
220         Info<< " )\nselected(";
222         forAll(selections, elemI)
223         {
224             Info<< " " << selections[elemI];
225         }
226         Info<< " )\n";
227     }
229     return selections;
233 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
235     vtkDataArraySelection* select,
236     const partInfo& selector
239     stringList selections(selector.size());
240     label nElem = 0;
242     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
243     {
244         if (select->GetArraySetting(elemI))
245         {
246             selections[nElem++] = select->GetArrayName(elemI);
247         }
248     }
249     selections.setSize(nElem);
252     if (debug)
253     {
254         Info<< "available(";
255         for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
256         {
257             Info<< " \"" << select->GetArrayName(elemI) << "\"";
258         }
259         Info<< " )\nselected(";
261         forAll(selections, elemI)
262         {
263             Info<< " " << selections[elemI];
264         }
265         Info<< " )\n";
266     }
268     return selections;
272 void Foam::vtkPV3Foam::setSelectedArrayEntries
274     vtkDataArraySelection* select,
275     const stringList& selections
278     const int nElem = select->GetNumberOfArrays();
279     select->DisableAllArrays();
281     // Loop through entries, setting values from selectedEntries
282     for (int elemI=0; elemI < nElem; ++elemI)
283     {
284         string arrayName(select->GetArrayName(elemI));
286         forAll(selections, elemI)
287         {
288             if (selections[elemI] == arrayName)
289             {
290                 select->EnableArray(arrayName.c_str());
291                 break;
292             }
293         }
294     }
298 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
300 // parse these bits of info from /proc/meminfo (Linux)
302 // MemTotal:      2062660 kB
303 // MemFree:       1124400 kB
305 // used = MemTotal - MemFree is what the free(1) uses.
307 void Foam::vtkPV3Foam::printMemory()
309     const char* meminfo = "/proc/meminfo";
311     if (exists(meminfo))
312     {
313         IFstream is(meminfo);
314         label memTotal = 0;
315         label memFree = 0;
317         string line;
319         while (is.getLine(line).good())
320         {
321             char tag[32];
322             int value;
324             if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2)
325             {
326                 if (!strcmp(tag, "MemTotal:"))
327                 {
328                     memTotal = value;
329                 }
330                 else if (!strcmp(tag, "MemFree:"))
331                 {
332                     memFree = value;
333                 }
334             }
335         }
337         Info << "memUsed: " << (memTotal - memFree) << " kB\n";
338     }
342 // ************************************************************************* //