Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / applications / utilities / postProcessing / graphics / PV3FoamReader / vtkPV3Foam / vtkPV3FoamUtilities.C
blob076e9928aef1be57b8a642d99c0f9ba839ce04a8
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Misc helper methods and utilities
27 \*---------------------------------------------------------------------------*/
29 #include "vtkPV3Foam.H"
30 #include "vtkPV3FoamReader.h"
32 // Foam includes
33 #include "fvMesh.H"
34 #include "foamTime.H"
35 #include "IFstream.H"
37 // VTK includes
38 #include "vtkDataArraySelection.h"
39 #include "vtkDataSet.h"
40 #include "vtkMultiBlockDataSet.h"
41 #include "vtkInformation.h"
43 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
45 void Foam::vtkPV3Foam::AddToBlock
47     vtkMultiBlockDataSet* output,
48     vtkDataSet* dataset,
49     const partInfo& selector,
50     const label datasetNo,
51     const string& datasetName
54     const int blockNo = selector.block();
56     vtkDataObject* blockDO = output->GetBlock(blockNo);
57     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
59     if (!block)
60     {
61         if (blockDO)
62         {
63             FatalErrorIn("Foam::vtkPV3Foam::AddToBlock")
64                 << "Block already has a vtkDataSet assigned to it"
65                 << endl;
66             return;
67         }
69         block = vtkMultiBlockDataSet::New();
70         output->SetBlock(blockNo, block);
71         block->Delete();
72     }
74     if (debug)
75     {
76         Info<< "block[" << blockNo << "] has "
77             << block->GetNumberOfBlocks()
78             <<  " datasets prior to adding set " << datasetNo
79             <<  " with name: " << datasetName << endl;
80     }
82     block->SetBlock(datasetNo, dataset);
84     // name the block when assigning dataset 0
85     if (datasetNo == 0)
86     {
87         output->GetMetaData(blockNo)->Set
88         (
89             vtkCompositeDataSet::NAME(),
90             selector.name()
91         );
92     }
94     if (datasetName.size())
95     {
96         block->GetMetaData(datasetNo)->Set
97         (
98             vtkCompositeDataSet::NAME(),
99             datasetName.c_str()
100         );
101     }
105 vtkDataSet* Foam::vtkPV3Foam::GetDataSetFromBlock
107     vtkMultiBlockDataSet* output,
108     const partInfo& selector,
109     const label datasetNo
112     const int blockNo = selector.block();
114     vtkDataObject* blockDO = output->GetBlock(blockNo);
115     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
117     if (block)
118     {
119         return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
120     }
122     return 0;
126 // ununsed at the moment
127 Foam::label Foam::vtkPV3Foam::GetNumberOfDataSets
129     vtkMultiBlockDataSet* output,
130     const partInfo& selector
133     const int blockNo = selector.block();
135     vtkDataObject* blockDO = output->GetBlock(blockNo);
136     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
137     if (block)
138     {
139         return block->GetNumberOfBlocks();
140     }
142     return 0;
146 Foam::word Foam::vtkPV3Foam::getPartName(int partId)
148     return getFirstWord(reader_->GetPartArrayName(partId));
152 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
154     vtkDataArraySelection* select
157     int nElem = select->GetNumberOfArrays();
158     wordHashSet selections(2*nElem);
160     for (int elemI=0; elemI < nElem; ++elemI)
161     {
162         if (select->GetArraySetting(elemI))
163         {
164             selections.insert(getFirstWord(select->GetArrayName(elemI)));
165         }
166     }
168     return selections;
172 Foam::wordHashSet Foam::vtkPV3Foam::getSelected
174     vtkDataArraySelection* select,
175     const partInfo& selector
178     int nElem = select->GetNumberOfArrays();
179     wordHashSet selections(2*nElem);
181     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
182     {
183         if (select->GetArraySetting(elemI))
184         {
185             selections.insert(getFirstWord(select->GetArrayName(elemI)));
186         }
187     }
189     return selections;
193 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
195     vtkDataArraySelection* select
198     stringList selections(select->GetNumberOfArrays());
199     label nElem = 0;
201     forAll(selections, elemI)
202     {
203         if (select->GetArraySetting(elemI))
204         {
205             selections[nElem++] = select->GetArrayName(elemI);
206         }
207     }
208     selections.setSize(nElem);
211     if (debug)
212     {
213         label nElem = select->GetNumberOfArrays();
214         Info<< "available(";
215         for (int elemI = 0; elemI < nElem; ++elemI)
216         {
217             Info<< " \"" << select->GetArrayName(elemI) << "\"";
218         }
219         Info<< " )\nselected(";
221         forAll(selections, elemI)
222         {
223             Info<< " " << selections[elemI];
224         }
225         Info<< " )\n";
226     }
228     return selections;
232 Foam::stringList Foam::vtkPV3Foam::getSelectedArrayEntries
234     vtkDataArraySelection* select,
235     const partInfo& selector
238     stringList selections(selector.size());
239     label nElem = 0;
241     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
242     {
243         if (select->GetArraySetting(elemI))
244         {
245             selections[nElem++] = select->GetArrayName(elemI);
246         }
247     }
248     selections.setSize(nElem);
251     if (debug)
252     {
253         Info<< "available(";
254         for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
255         {
256             Info<< " \"" << select->GetArrayName(elemI) << "\"";
257         }
258         Info<< " )\nselected(";
260         forAll(selections, elemI)
261         {
262             Info<< " " << selections[elemI];
263         }
264         Info<< " )\n";
265     }
267     return selections;
271 void Foam::vtkPV3Foam::setSelectedArrayEntries
273     vtkDataArraySelection* select,
274     const stringList& selections
277     const int nElem = select->GetNumberOfArrays();
278     select->DisableAllArrays();
280     // Loop through entries, setting values from selectedEntries
281     for (int elemI=0; elemI < nElem; ++elemI)
282     {
283         string arrayName(select->GetArrayName(elemI));
285         forAll(selections, elemI)
286         {
287             if (selections[elemI] == arrayName)
288             {
289                 select->EnableArray(arrayName.c_str());
290                 break;
291             }
292         }
293     }
297 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
299 // parse these bits of info from /proc/meminfo (Linux)
301 // MemTotal:      2062660 kB
302 // MemFree:       1124400 kB
304 // used = MemTotal - MemFree is what the free(1) uses.
306 void Foam::vtkPV3Foam::printMemory()
308     const char* meminfo = "/proc/meminfo";
310     if (exists(meminfo))
311     {
312         IFstream is(meminfo);
313         label memTotal = 0;
314         label memFree = 0;
316         string line;
318         while (is.getLine(line).good())
319         {
320             char tag[32];
321             int value;
323             if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2)
324             {
325                 if (!strcmp(tag, "MemTotal:"))
326                 {
327                     memTotal = value;
328                 }
329                 else if (!strcmp(tag, "MemFree:"))
330                 {
331                     memFree = value;
332                 }
333             }
334         }
336         Info << "memUsed: " << (memTotal - memFree) << " kB\n";
337     }
341 // ************************************************************************* //