initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / dataConversion / foamToEnsight / ensightField.C
blob90a597e57888a3e2155b7deba0efb09e90f04a12
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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             runTime.writeFormat(),
455             runTime.writeVersion(),
456             runTime.writeCompression()
457         );
458     }
460     OFstream& ensightFile = *ensightFilePtr;
462     if (Pstream::master())
463     {
464         ensightFile << pTraits<Type>::typeName << nl;
465     }
467     if (patchi >= 0)
468     {
469         writePatchField
470         (
471             pf,
472             patchi,
473             ensightPatchI,
474             boundaryFaceSets[patchi],
475             nPatchPrims.find(patchName)(),
476             patchProcessors,
477             ensightFile
478         );
479     }
480     else
481     {
482         faceSets nullFaceSets;
484         writePatchField
485         (
486             Field<Type>(),
487             -1,
488             ensightPatchI,
489             nullFaceSets,
490             nPatchPrims.find(patchName)(),
491             patchProcessors,
492             ensightFile
493         );
494     }
496     if (Pstream::master())
497     {
498         delete ensightFilePtr;
499     }
503 template<class Type>
504 void ensightFieldAscii
506     const Foam::IOobject& fieldObject,
507     const Foam::ensightMesh& eMesh,
508     const Foam::fileName& postProcPath,
509     const Foam::word& prepend,
510     const Foam::label timeIndex,
511     Foam::Ostream& ensightCaseFile
514     Info<< "Converting field " << fieldObject.name() << endl;
516     word timeFile = prepend + itoa(timeIndex);
518     const fvMesh& mesh = eMesh.mesh();
519     const Time& runTime = mesh.time();
521     const cellSets& meshCellSets = eMesh.meshCellSets();
522     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
523     const wordList& allPatchNames = eMesh.allPatchNames();
524     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
525     const wordHashSet& patchNames = eMesh.patchNames();
526     const HashTable<ensightMesh::nFacePrimitives>&
527         nPatchPrims = eMesh.nPatchPrims();
529     const labelList& tets = meshCellSets.tets;
530     const labelList& pyrs = meshCellSets.pyrs;
531     const labelList& prisms = meshCellSets.prisms;
532     const labelList& wedges = meshCellSets.wedges;
533     const labelList& hexes = meshCellSets.hexes;
534     const labelList& polys = meshCellSets.polys;
536     OFstream *ensightFilePtr = NULL;
537     if (Pstream::master())
538     {
539         // set the filename of the ensight file
540         fileName ensightFileName(timeFile + "." + fieldObject.name());
541         ensightFilePtr = new OFstream
542         (
543             postProcPath/ensightFileName,
544             runTime.writeFormat(),
545             runTime.writeVersion(),
546             runTime.writeCompression()
547         );
548     }
550     OFstream& ensightFile = *ensightFilePtr;
552     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
554     if (patchNames.empty())
555     {
556         if (Pstream::master())
557         {
558             if (timeIndex == 0)
559             {
560                 ensightCaseFile.setf(ios_base::left);
562                 ensightCaseFile
563                     << pTraits<Type>::typeName
564                     << " per element:            1       "
565                     << setw(15) << vf.name()
566                     << (' ' + prepend + "***." + vf.name()).c_str()
567                     << nl;
568             }
570             ensightFile
571                 << pTraits<Type>::typeName << nl
572                 << "part" << nl
573                 << setw(10) << 1 << nl;
575             ensightFile.setf(ios_base::scientific, ios_base::floatfield);
576             ensightFile.precision(5);
577         }
579         if (meshCellSets.nHexesWedges)
580         {
581             if (Pstream::master())
582             {
583                 ensightFile << "hexa8" << nl;
585                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
586                 {
587                     writeData
588                     (
589                         map(vf, hexes, wedges, cmpt),
590                         ensightFile
591                     );
593                     for (int slave=1; slave<Pstream::nProcs(); slave++)
594                     {
595                         IPstream fromSlave(Pstream::scheduled, slave);
596                         scalarField data(fromSlave);
597                         writeData(data, ensightFile);
598                     }
599                 }
600             }
601             else
602             {
603                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
604                 {
605                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
606                     toMaster<< map(vf, hexes, wedges, cmpt);
607                 }
608             }
609         }
611         writeAllData("penta6", vf, prisms, meshCellSets.nPrisms, ensightFile);
612         writeAllData("pyramid5", vf, pyrs, meshCellSets.nPyrs, ensightFile);
613         writeAllData("tetra4", vf, tets, meshCellSets.nTets, ensightFile);
614         writeAllData("nfaced", vf, polys, meshCellSets.nPolys, ensightFile);
615     }
617     label ensightPatchI = eMesh.patchPartOffset();
619     forAll(allPatchNames, patchi)
620     {
621         const word& patchName = allPatchNames[patchi];
622         const labelList& patchProcessors = allPatchProcs[patchi];
624         if (patchNames.empty() || patchNames.found(patchName))
625         {
626             if (mesh.boundary()[patchi].size())
627             {
628                 if
629                 (
630                     writePatchField
631                     (
632                         vf.boundaryField()[patchi],
633                         patchi,
634                         ensightPatchI,
635                         boundaryFaceSets[patchi],
636                         nPatchPrims.find(patchName)(),
637                         patchProcessors,
638                         ensightFile
639                     )
640                 )
641                 {
642                     ensightPatchI++;
643                 }
645             }
646             else if (Pstream::master())
647             {
648                 faceSets nullFaceSet;
650                 if
651                 (
652                     writePatchField
653                     (
654                         Field<Type>(),
655                         -1,
656                         ensightPatchI,
657                         nullFaceSet,
658                         nPatchPrims.find(patchName)(),
659                         patchProcessors,
660                         ensightFile
661                     )
662                 )
663                 {
664                     ensightPatchI++;
665                 }
666             }
667         }
668     }
670     if (Pstream::master())
671     {
672         delete ensightFilePtr;
673     }
677 template<class Type>
678 void ensightFieldBinary
680     const Foam::IOobject& fieldObject,
681     const Foam::ensightMesh& eMesh,
682     const Foam::fileName& postProcPath,
683     const Foam::word& prepend,
684     const Foam::label timeIndex,
685     Foam::Ostream& ensightCaseFile
688     Info<< "Converting field (binary) " << fieldObject.name() << endl;
690     word timeFile = prepend + itoa(timeIndex);
692     const fvMesh& mesh = eMesh.mesh();
693     //const Time& runTime = mesh.time();
695     const cellSets& meshCellSets = eMesh.meshCellSets();
696     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
697     const wordList& allPatchNames = eMesh.allPatchNames();
698     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
699     const wordHashSet& patchNames = eMesh.patchNames();
700     const HashTable<ensightMesh::nFacePrimitives>&
701         nPatchPrims = eMesh.nPatchPrims();
703     const labelList& tets = meshCellSets.tets;
704     const labelList& pyrs = meshCellSets.pyrs;
705     const labelList& prisms = meshCellSets.prisms;
706     const labelList& wedges = meshCellSets.wedges;
707     const labelList& hexes = meshCellSets.hexes;
708     const labelList& polys = meshCellSets.polys;
710     std::ofstream *ensightFilePtr = NULL;
711     if (Pstream::master())
712     {
713         // set the filename of the ensight file
714         fileName ensightFileName(timeFile + "." + fieldObject.name());
715         ensightFilePtr = new std::ofstream
716         (
717             (postProcPath/ensightFileName).c_str(),
718             ios_base::out | ios_base::binary | ios_base::trunc
719         );
720         // Check on file opened?
721     }
723     std::ofstream& ensightFile = *ensightFilePtr;
725     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
727     if (patchNames.empty())
728     {
729         if (Pstream::master())
730         {
731             if (timeIndex == 0)
732             {
733                 ensightCaseFile.setf(ios_base::left);
735                 ensightCaseFile
736                     << pTraits<Type>::typeName
737                     << " per element:            1       "
738                     << setw(15) << vf.name()
739                     << (' ' + prepend + "***." + vf.name()).c_str()
740                     << nl;
741             }
743             writeEnsDataBinary(pTraits<Type>::typeName,ensightFile);
744             writeEnsDataBinary("part",ensightFile);
745             writeEnsDataBinary(1,ensightFile);
746         }
748         if (meshCellSets.nHexesWedges)
749         {
750             if (Pstream::master())
751             {
752                 writeEnsDataBinary("hexa8",ensightFile);
754                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
755                 {
756                     writeEnsDataBinary
757                     (
758                         map(vf, hexes, wedges, cmpt),
759                         ensightFile
760                     );
762                     for (int slave=1; slave<Pstream::nProcs(); slave++)
763                     {
764                         IPstream fromSlave(Pstream::scheduled, slave);
765                         scalarField data(fromSlave);
766                         writeEnsDataBinary(data, ensightFile);
767                     }
768                 }
769             }
770             else
771             {
772                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
773                 {
774                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
775                     toMaster<< map(vf, hexes, wedges, cmpt);
776                 }
777             }
778         }
780         writeAllDataBinary
781         (
782             "penta6",
783             vf,
784             prisms,
785             meshCellSets.nPrisms,
786             ensightFile
787         );
789         writeAllDataBinary
790         (
791             "pyramid5",
792             vf,
793             pyrs,
794             meshCellSets.nPyrs,
795             ensightFile
796         );
798         writeAllDataBinary
799         (
800             "tetra4",
801             vf,
802             tets,
803             meshCellSets.nTets,
804             ensightFile
805         );
807         writeAllDataBinary
808         (
809             "nfaced",
810             vf,
811             polys,
812             meshCellSets.nPolys,
813             ensightFile
814         );
815     }
817     label ensightPatchI = eMesh.patchPartOffset();
819     forAll(allPatchNames, patchi)
820     {
821         const word& patchName = allPatchNames[patchi];
822         const labelList& patchProcessors = allPatchProcs[patchi];
824         if (patchNames.empty() || patchNames.found(patchName))
825         {
826             if (mesh.boundary()[patchi].size())
827             {
828                 if
829                 (
830                     writePatchFieldBinary
831                     (
832                         vf.boundaryField()[patchi],
833                         patchi,
834                         ensightPatchI,
835                         boundaryFaceSets[patchi],
836                         nPatchPrims.find(patchName)(),
837                         patchProcessors,
838                         ensightFile
839                     )
840                 )
841                 {
842                     ensightPatchI++;
843                 }
845             }
846             else if (Pstream::master())
847             {
848                 faceSets nullFaceSet;
850                 if
851                 (
852                     writePatchFieldBinary
853                     (
854                         Field<Type>(),
855                         -1,
856                         ensightPatchI,
857                         nullFaceSet,
858                         nPatchPrims.find(patchName)(),
859                         patchProcessors,
860                         ensightFile
861                     )
862                 )
863                 {
864                     ensightPatchI++;
865                 }
866             }
867         }
868     }
870     if (Pstream::master())
871     {
872         ensightFile.close();
873     }
877 template<class Type>
878 void ensightField
880     const Foam::IOobject& fieldObject,
881     const Foam::ensightMesh& eMesh,
882     const Foam::fileName& postProcPath,
883     const Foam::word& prepend,
884     const Foam::label timeIndex,
885     const bool binary,
886     Foam::Ostream& ensightCaseFile
889     if (binary)
890     {
891         ensightFieldBinary<Type>
892         (
893             fieldObject,
894             eMesh,
895             postProcPath,
896             prepend,
897             timeIndex,
898             ensightCaseFile
899         );
900     }
901     else
902     {
903         ensightFieldAscii<Type>
904         (
905             fieldObject,
906             eMesh,
907             postProcPath,
908             prepend,
909             timeIndex,
910             ensightCaseFile
911         );
912     }
916 // ************************************************************************* //