BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToEnsight / ensightField.C
blobe2dc741fc2b14eb2291fb4ae151ba48b2422019a
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 \*---------------------------------------------------------------------------*/
27 #include "ensightField.H"
28 #include "fvMesh.H"
29 #include "volFields.H"
30 #include "OFstream.H"
31 #include "IOmanip.H"
32 #include "itoa.H"
33 #include "ensightWriteBinary.H"
35 using namespace Foam;
37 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
39 void writeData(const scalarField& sf, OFstream& ensightFile)
41     forAll(sf, i)
42     {
43         if (mag( sf[i] ) >= scalar(floatScalarVSMALL))
44         {
45             ensightFile << setw(12) << sf[i] << nl;
46         }
47         else
48         {
49             ensightFile << setw(12) << scalar(0) << nl;
50         }
51     }
55 template<class Type>
56 scalarField map
58     const Field<Type>& vf,
59     const labelList& map,
60     const label cmpt
63     scalarField mf(map.size());
65     forAll(map, i)
66     {
67         mf[i] = component(vf[map[i]], cmpt);
68     }
70     return mf;
74 template<class Type>
75 scalarField map
77     const Field<Type>& vf,
78     const labelList& map1,
79     const labelList& map2,
80     const label cmpt
83     scalarField mf(map1.size() + map2.size());
85     forAll(map1, i)
86     {
87         mf[i] = component(vf[map1[i]], cmpt);
88     }
90     label offset = map1.size();
92     forAll(map2, i)
93     {
94         mf[i + offset] = component(vf[map2[i]], cmpt);
95     }
97     return mf;
101 template<class Type>
102 void writeAllData
104     const char* key,
105     const Field<Type>& vf,
106     const labelList& prims,
107     const label nPrims,
108     OFstream& ensightFile
111     if (nPrims)
112     {
113         if (Pstream::master())
114         {
115             ensightFile << key << nl;
117             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
118             {
119                 writeData(map(vf, prims, cmpt), ensightFile);
121                 for (int slave=1; slave<Pstream::nProcs(); slave++)
122                 {
123                     IPstream fromSlave(Pstream::scheduled, slave);
124                     scalarField data(fromSlave);
125                     writeData(data, ensightFile);
126                 }
127             }
128         }
129         else
130         {
131             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
132             {
133                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
134                 toMaster<< map(vf, prims, cmpt);
135             }
136         }
137     }
141 template<class Type>
142 void writeAllDataBinary
144     const char* key,
145     const Field<Type>& vf,
146     const labelList& prims,
147     const label nPrims,
148     std::ofstream& ensightFile
151     if (nPrims)
152     {
153         if (Pstream::master())
154         {
155             writeEnsDataBinary(key,ensightFile);
157             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
158             {
159                 writeEnsDataBinary(map(vf, prims, cmpt), ensightFile);
161                 for (int slave=1; slave<Pstream::nProcs(); slave++)
162                 {
163                     IPstream fromSlave(Pstream::scheduled, slave);
164                     scalarField data(fromSlave);
165                     writeEnsDataBinary(data, ensightFile);
166                 }
167             }
168         }
169         else
170         {
171             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
172             {
173                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
174                 toMaster<< map(vf, prims, cmpt);
175             }
176         }
177     }
181 template<class Type>
182 void writeAllFaceData
184     const char* key,
185     const labelList& prims,
186     const label nPrims,
187     const Field<Type>& pf,
188     const labelList& patchProcessors,
189     OFstream& ensightFile
192     if (nPrims)
193     {
194         if (Pstream::master())
195         {
196             ensightFile << key << nl;
198             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
199             {
200                 writeData(map(pf, prims, cmpt), ensightFile);
202                 forAll (patchProcessors, i)
203                 {
204                     if (patchProcessors[i] != 0)
205                     {
206                         label slave = patchProcessors[i];
207                         IPstream fromSlave(Pstream::scheduled, slave);
208                         scalarField pf(fromSlave);
210                         writeData(pf, ensightFile);
211                     }
212                 }
213             }
214         }
215         else
216         {
217             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
218             {
219                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
220                 toMaster<< map(pf, prims, cmpt);
221             }
222         }
223     }
227 template<class Type>
228 void writeAllFaceDataBinary
230     const char* key,
231     const labelList& prims,
232     const label nPrims,
233     const Field<Type>& pf,
234     const labelList& patchProcessors,
235     std::ofstream& ensightFile
238     if (nPrims)
239     {
240         if (Pstream::master())
241         {
242             writeEnsDataBinary(key,ensightFile);
244             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
245             {
246                 writeEnsDataBinary(map(pf, prims, cmpt), ensightFile);
248                 forAll (patchProcessors, i)
249                 {
250                     if (patchProcessors[i] != 0)
251                     {
252                         label slave = patchProcessors[i];
253                         IPstream fromSlave(Pstream::scheduled, slave);
254                         scalarField pf(fromSlave);
256                         writeEnsDataBinary(pf, ensightFile);
257                     }
258                 }
259             }
260         }
261         else
262         {
263             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
264             {
265                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
266                 toMaster<< map(pf, prims, cmpt);
267             }
268         }
269     }
273 template<class Type>
274 bool writePatchField
276     const Foam::Field<Type>& pf,
277     const Foam::label patchi,
278     const Foam::label ensightPatchI,
279     const Foam::faceSets& boundaryFaceSet,
280     const Foam::ensightMesh::nFacePrimitives& nfp,
281     const Foam::labelList& patchProcessors,
282     Foam::OFstream& ensightFile
285     if (nfp.nTris || nfp.nQuads || nfp.nPolys)
286     {
287         if (Pstream::master())
288         {
289             ensightFile
290                 << "part" << nl
291                 << setw(10) << ensightPatchI << nl;
292         }
294         writeAllFaceData
295         (
296             "tria3",
297             boundaryFaceSet.tris,
298             nfp.nTris,
299             pf,
300             patchProcessors,
301             ensightFile
302         );
304         writeAllFaceData
305         (
306             "quad4",
307             boundaryFaceSet.quads,
308             nfp.nQuads,
309             pf,
310             patchProcessors,
311             ensightFile
312         );
314         writeAllFaceData
315         (
316             "nsided",
317             boundaryFaceSet.polys,
318             nfp.nPolys,
319             pf,
320             patchProcessors,
321             ensightFile
322         );
324         return true;
325     }
326     else
327     {
328         return false;
329     }
333 template<class Type>
334 bool writePatchFieldBinary
336     const Foam::Field<Type>& pf,
337     const Foam::label patchi,
338     const Foam::label ensightPatchI,
339     const Foam::faceSets& boundaryFaceSet,
340     const Foam::ensightMesh::nFacePrimitives& nfp,
341     const Foam::labelList& patchProcessors,
342     std::ofstream& ensightFile
345     if (nfp.nTris || nfp.nQuads || nfp.nPolys)
346     {
347         if (Pstream::master())
348         {
349             writeEnsDataBinary("part",ensightFile);
350             writeEnsDataBinary(ensightPatchI,ensightFile);
351         }
353         writeAllFaceDataBinary
354         (
355             "tria3",
356             boundaryFaceSet.tris,
357             nfp.nTris,
358             pf,
359             patchProcessors,
360             ensightFile
361         );
363         writeAllFaceDataBinary
364         (
365             "quad4",
366             boundaryFaceSet.quads,
367             nfp.nQuads,
368             pf,
369             patchProcessors,
370             ensightFile
371         );
373         writeAllFaceDataBinary
374         (
375             "nsided",
376             boundaryFaceSet.polys,
377             nfp.nPolys,
378             pf,
379             patchProcessors,
380             ensightFile
381         );
383         return true;
384     }
385     else
386     {
387         return false;
388     }
392 template<class Type>
393 void writePatchField
395     const Foam::word& fieldName,
396     const Foam::Field<Type>& pf,
397     const Foam::word& patchName,
398     const Foam::ensightMesh& eMesh,
399     const Foam::fileName& postProcPath,
400     const Foam::word& prepend,
401     const Foam::label timeIndex,
402     Foam::Ostream& ensightCaseFile
405     const Time& runTime = eMesh.mesh().time();
407     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
408     const wordList& allPatchNames = eMesh.allPatchNames();
409     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
410     const HashTable<ensightMesh::nFacePrimitives>&
411         nPatchPrims = eMesh.nPatchPrims();
413     label ensightPatchI = eMesh.patchPartOffset();
415     label patchi = -1;
417     forAll(allPatchNames, i)
418     {
419         if (allPatchNames[i] == patchName)
420         {
421             patchi = i;
422             break;
423         }
424         ensightPatchI++;
425     }
428     const labelList& patchProcessors = allPatchProcs[patchi];
430     word pfName = patchName + '.' + fieldName;
432     word timeFile = prepend + itoa(timeIndex);
434     OFstream *ensightFilePtr = NULL;
435     if (Pstream::master())
436     {
437         if (timeIndex == 0)
438         {
439             ensightCaseFile.setf(ios_base::left);
441             ensightCaseFile
442                 << pTraits<Type>::typeName
443                 << " per element:            1       "
444                 << setw(15) << pfName
445                 << (' ' + prepend + "***." + pfName).c_str()
446                 << nl;
447         }
449         // set the filename of the ensight file
450         fileName ensightFileName(timeFile + "." + pfName);
451         ensightFilePtr = new OFstream
452         (
453             postProcPath/ensightFileName,
454             ios_base::out|ios_base::trunc,
455             runTime.writeFormat(),
456             runTime.writeVersion(),
457             runTime.writeCompression()
458         );
459     }
461     OFstream& ensightFile = *ensightFilePtr;
463     if (Pstream::master())
464     {
465         ensightFile << pTraits<Type>::typeName << nl;
466     }
468     if (patchi >= 0)
469     {
470         writePatchField
471         (
472             pf,
473             patchi,
474             ensightPatchI,
475             boundaryFaceSets[patchi],
476             nPatchPrims.find(patchName)(),
477             patchProcessors,
478             ensightFile
479         );
480     }
481     else
482     {
483         faceSets nullFaceSets;
485         writePatchField
486         (
487             Field<Type>(),
488             -1,
489             ensightPatchI,
490             nullFaceSets,
491             nPatchPrims.find(patchName)(),
492             patchProcessors,
493             ensightFile
494         );
495     }
497     if (Pstream::master())
498     {
499         delete ensightFilePtr;
500     }
504 template<class Type>
505 void ensightFieldAscii
507     const Foam::IOobject& fieldObject,
508     const Foam::ensightMesh& eMesh,
509     const Foam::fileName& postProcPath,
510     const Foam::word& prepend,
511     const Foam::label timeIndex,
512     Foam::Ostream& ensightCaseFile
515     Info<< "Converting field " << fieldObject.name() << endl;
517     word timeFile = prepend + itoa(timeIndex);
519     const fvMesh& mesh = eMesh.mesh();
520     const Time& runTime = mesh.time();
522     const cellSets& meshCellSets = eMesh.meshCellSets();
523     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
524     const wordList& allPatchNames = eMesh.allPatchNames();
525     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
526     const wordHashSet& patchNames = eMesh.patchNames();
527     const HashTable<ensightMesh::nFacePrimitives>&
528         nPatchPrims = eMesh.nPatchPrims();
530     const labelList& tets = meshCellSets.tets;
531     const labelList& pyrs = meshCellSets.pyrs;
532     const labelList& prisms = meshCellSets.prisms;
533     const labelList& wedges = meshCellSets.wedges;
534     const labelList& hexes = meshCellSets.hexes;
535     const labelList& polys = meshCellSets.polys;
537     OFstream *ensightFilePtr = NULL;
538     if (Pstream::master())
539     {
540         // set the filename of the ensight file
541         fileName ensightFileName(timeFile + "." + fieldObject.name());
542         ensightFilePtr = new OFstream
543         (
544             postProcPath/ensightFileName,
545             ios_base::out|ios_base::trunc,
546             runTime.writeFormat(),
547             runTime.writeVersion(),
548             runTime.writeCompression()
549         );
550     }
552     OFstream& ensightFile = *ensightFilePtr;
554     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
556     if (patchNames.empty())
557     {
558         if (Pstream::master())
559         {
560             if (timeIndex == 0)
561             {
562                 ensightCaseFile.setf(ios_base::left);
564                 ensightCaseFile
565                     << pTraits<Type>::typeName
566                     << " per element:            1       "
567                     << setw(15) << vf.name()
568                     << (' ' + prepend + "***." + vf.name()).c_str()
569                     << nl;
570             }
572             ensightFile
573                 << pTraits<Type>::typeName << nl
574                 << "part" << nl
575                 << setw(10) << 1 << nl;
577             ensightFile.setf(ios_base::scientific, ios_base::floatfield);
578             ensightFile.precision(5);
579         }
581         if (meshCellSets.nHexesWedges)
582         {
583             if (Pstream::master())
584             {
585                 ensightFile << "hexa8" << nl;
587                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
588                 {
589                     writeData
590                     (
591                         map(vf, hexes, wedges, cmpt),
592                         ensightFile
593                     );
595                     for (int slave=1; slave<Pstream::nProcs(); slave++)
596                     {
597                         IPstream fromSlave(Pstream::scheduled, slave);
598                         scalarField data(fromSlave);
599                         writeData(data, ensightFile);
600                     }
601                 }
602             }
603             else
604             {
605                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
606                 {
607                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
608                     toMaster<< map(vf, hexes, wedges, cmpt);
609                 }
610             }
611         }
613         writeAllData("penta6", vf, prisms, meshCellSets.nPrisms, ensightFile);
614         writeAllData("pyramid5", vf, pyrs, meshCellSets.nPyrs, ensightFile);
615         writeAllData("tetra4", vf, tets, meshCellSets.nTets, ensightFile);
616         writeAllData("nfaced", vf, polys, meshCellSets.nPolys, ensightFile);
617     }
619     label ensightPatchI = eMesh.patchPartOffset();
621     forAll(allPatchNames, patchi)
622     {
623         const word& patchName = allPatchNames[patchi];
624         const labelList& patchProcessors = allPatchProcs[patchi];
626         if (patchNames.empty() || patchNames.found(patchName))
627         {
628             if (mesh.boundary()[patchi].size())
629             {
630                 if
631                 (
632                     writePatchField
633                     (
634                         vf.boundaryField()[patchi],
635                         patchi,
636                         ensightPatchI,
637                         boundaryFaceSets[patchi],
638                         nPatchPrims.find(patchName)(),
639                         patchProcessors,
640                         ensightFile
641                     )
642                 )
643                 {
644                     ensightPatchI++;
645                 }
647             }
648             else if (Pstream::master())
649             {
650                 faceSets nullFaceSet;
652                 if
653                 (
654                     writePatchField
655                     (
656                         Field<Type>(),
657                         -1,
658                         ensightPatchI,
659                         nullFaceSet,
660                         nPatchPrims.find(patchName)(),
661                         patchProcessors,
662                         ensightFile
663                     )
664                 )
665                 {
666                     ensightPatchI++;
667                 }
668             }
669         }
670     }
672     if (Pstream::master())
673     {
674         delete ensightFilePtr;
675     }
679 template<class Type>
680 void ensightFieldBinary
682     const Foam::IOobject& fieldObject,
683     const Foam::ensightMesh& eMesh,
684     const Foam::fileName& postProcPath,
685     const Foam::word& prepend,
686     const Foam::label timeIndex,
687     Foam::Ostream& ensightCaseFile
690     Info<< "Converting field (binary) " << fieldObject.name() << endl;
692     word timeFile = prepend + itoa(timeIndex);
694     const fvMesh& mesh = eMesh.mesh();
695     //const Time& runTime = mesh.time();
697     const cellSets& meshCellSets = eMesh.meshCellSets();
698     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
699     const wordList& allPatchNames = eMesh.allPatchNames();
700     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
701     const wordHashSet& patchNames = eMesh.patchNames();
702     const HashTable<ensightMesh::nFacePrimitives>&
703         nPatchPrims = eMesh.nPatchPrims();
705     const labelList& tets = meshCellSets.tets;
706     const labelList& pyrs = meshCellSets.pyrs;
707     const labelList& prisms = meshCellSets.prisms;
708     const labelList& wedges = meshCellSets.wedges;
709     const labelList& hexes = meshCellSets.hexes;
710     const labelList& polys = meshCellSets.polys;
712     std::ofstream *ensightFilePtr = NULL;
713     if (Pstream::master())
714     {
715         // set the filename of the ensight file
716         fileName ensightFileName(timeFile + "." + fieldObject.name());
717         ensightFilePtr = new std::ofstream
718         (
719             (postProcPath/ensightFileName).c_str(),
720             ios_base::out | ios_base::binary | ios_base::trunc
721         );
722         // Check on file opened?
723     }
725     std::ofstream& ensightFile = *ensightFilePtr;
727     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
729     if (patchNames.empty())
730     {
731         if (Pstream::master())
732         {
733             if (timeIndex == 0)
734             {
735                 ensightCaseFile.setf(ios_base::left);
737                 ensightCaseFile
738                     << pTraits<Type>::typeName
739                     << " per element:            1       "
740                     << setw(15) << vf.name()
741                     << (' ' + prepend + "***." + vf.name()).c_str()
742                     << nl;
743             }
745             writeEnsDataBinary(pTraits<Type>::typeName,ensightFile);
746             writeEnsDataBinary("part",ensightFile);
747             writeEnsDataBinary(1,ensightFile);
748         }
750         if (meshCellSets.nHexesWedges)
751         {
752             if (Pstream::master())
753             {
754                 writeEnsDataBinary("hexa8",ensightFile);
756                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
757                 {
758                     writeEnsDataBinary
759                     (
760                         map(vf, hexes, wedges, cmpt),
761                         ensightFile
762                     );
764                     for (int slave=1; slave<Pstream::nProcs(); slave++)
765                     {
766                         IPstream fromSlave(Pstream::scheduled, slave);
767                         scalarField data(fromSlave);
768                         writeEnsDataBinary(data, ensightFile);
769                     }
770                 }
771             }
772             else
773             {
774                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
775                 {
776                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
777                     toMaster<< map(vf, hexes, wedges, cmpt);
778                 }
779             }
780         }
782         writeAllDataBinary
783         (
784             "penta6",
785             vf,
786             prisms,
787             meshCellSets.nPrisms,
788             ensightFile
789         );
791         writeAllDataBinary
792         (
793             "pyramid5",
794             vf,
795             pyrs,
796             meshCellSets.nPyrs,
797             ensightFile
798         );
800         writeAllDataBinary
801         (
802             "tetra4",
803             vf,
804             tets,
805             meshCellSets.nTets,
806             ensightFile
807         );
809         writeAllDataBinary
810         (
811             "nfaced",
812             vf,
813             polys,
814             meshCellSets.nPolys,
815             ensightFile
816         );
817     }
819     label ensightPatchI = eMesh.patchPartOffset();
821     forAll(allPatchNames, patchi)
822     {
823         const word& patchName = allPatchNames[patchi];
824         const labelList& patchProcessors = allPatchProcs[patchi];
826         if (patchNames.empty() || patchNames.found(patchName))
827         {
828             if (mesh.boundary()[patchi].size())
829             {
830                 if
831                 (
832                     writePatchFieldBinary
833                     (
834                         vf.boundaryField()[patchi],
835                         patchi,
836                         ensightPatchI,
837                         boundaryFaceSets[patchi],
838                         nPatchPrims.find(patchName)(),
839                         patchProcessors,
840                         ensightFile
841                     )
842                 )
843                 {
844                     ensightPatchI++;
845                 }
847             }
848             else if (Pstream::master())
849             {
850                 faceSets nullFaceSet;
852                 if
853                 (
854                     writePatchFieldBinary
855                     (
856                         Field<Type>(),
857                         -1,
858                         ensightPatchI,
859                         nullFaceSet,
860                         nPatchPrims.find(patchName)(),
861                         patchProcessors,
862                         ensightFile
863                     )
864                 )
865                 {
866                     ensightPatchI++;
867                 }
868             }
869         }
870     }
872     if (Pstream::master())
873     {
874         ensightFile.close();
875     }
879 template<class Type>
880 void ensightField
882     const Foam::IOobject& fieldObject,
883     const Foam::ensightMesh& eMesh,
884     const Foam::fileName& postProcPath,
885     const Foam::word& prepend,
886     const Foam::label timeIndex,
887     const bool binary,
888     Foam::Ostream& ensightCaseFile
891     if (binary)
892     {
893         ensightFieldBinary<Type>
894         (
895             fieldObject,
896             eMesh,
897             postProcPath,
898             prepend,
899             timeIndex,
900             ensightCaseFile
901         );
902     }
903     else
904     {
905         ensightFieldAscii<Type>
906         (
907             fieldObject,
908             eMesh,
909             postProcPath,
910             prepend,
911             timeIndex,
912             ensightCaseFile
913         );
914     }
918 // ************************************************************************* //