Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToEnsight / ensightField.C
blob633ee33eb657a9ed2229390b44ded99ca9ccbf39
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 \*---------------------------------------------------------------------------*/
26 #include "ensightField.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
29 #include "OFstream.H"
30 #include "IOmanip.H"
31 #include "itoa.H"
32 #include "ensightWriteBinary.H"
34 using namespace Foam;
36 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
38 void writeData(const scalarField& sf, OFstream& ensightFile)
40     forAll(sf, i)
41     {
42         if (mag( sf[i] ) >= scalar(floatScalarVSMALL))
43         {
44             ensightFile << setw(12) << sf[i] << nl;
45         }
46         else
47         {
48             ensightFile << setw(12) << scalar(0) << nl;
49         }
50     }
54 template<class Type>
55 scalarField ensMap
57     const Field<Type>& vf,
58     const labelList& map,
59     const label cmpt
62     scalarField mf(map.size());
64     forAll(map, i)
65     {
66         mf[i] = component(vf[map[i]], cmpt);
67     }
69     return mf;
73 template<class Type>
74 scalarField ensMap
76     const Field<Type>& vf,
77     const labelList& map1,
78     const labelList& map2,
79     const label cmpt
82     scalarField mf(map1.size() + map2.size());
84     forAll(map1, i)
85     {
86         mf[i] = component(vf[map1[i]], cmpt);
87     }
89     label offset = map1.size();
91     forAll(map2, i)
92     {
93         mf[i + offset] = component(vf[map2[i]], cmpt);
94     }
96     return mf;
100 template<class Type>
101 void writeAllData
103     const char* key,
104     const Field<Type>& vf,
105     const labelList& prims,
106     const label nPrims,
107     OFstream& ensightFile
110     if (nPrims)
111     {
112         if (Pstream::master())
113         {
114             ensightFile << key << nl;
116             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
117             {
118                 writeData(ensMap(vf, prims, cmpt), ensightFile);
120                 for (int slave=1; slave<Pstream::nProcs(); slave++)
121                 {
122                     IPstream fromSlave(Pstream::scheduled, slave);
123                     scalarField data(fromSlave);
124                     writeData(data, ensightFile);
125                 }
126             }
127         }
128         else
129         {
130             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
131             {
132                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
133                 toMaster<< ensMap(vf, prims, cmpt);
134             }
135         }
136     }
140 template<class Type>
141 void writeAllDataBinary
143     const char* key,
144     const Field<Type>& vf,
145     const labelList& prims,
146     const label nPrims,
147     std::ofstream& ensightFile
150     if (nPrims)
151     {
152         if (Pstream::master())
153         {
154             writeEnsDataBinary(key,ensightFile);
156             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
157             {
158                 writeEnsDataBinary(ensMap(vf, prims, cmpt), ensightFile);
160                 for (int slave=1; slave<Pstream::nProcs(); slave++)
161                 {
162                     IPstream fromSlave(Pstream::scheduled, slave);
163                     scalarField data(fromSlave);
164                     writeEnsDataBinary(data, ensightFile);
165                 }
166             }
167         }
168         else
169         {
170             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
171             {
172                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
173                 toMaster<< ensMap(vf, prims, cmpt);
174             }
175         }
176     }
180 template<class Type>
181 void writeAllFaceData
183     const char* key,
184     const labelList& prims,
185     const label nPrims,
186     const Field<Type>& pf,
187     const labelList& patchProcessors,
188     OFstream& ensightFile
191     if (nPrims)
192     {
193         if (Pstream::master())
194         {
195             ensightFile << key << nl;
197             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
198             {
199                 writeData(ensMap(pf, prims, cmpt), ensightFile);
201                 forAll (patchProcessors, i)
202                 {
203                     if (patchProcessors[i] != 0)
204                     {
205                         label slave = patchProcessors[i];
206                         IPstream fromSlave(Pstream::scheduled, slave);
207                         scalarField pf(fromSlave);
209                         writeData(pf, ensightFile);
210                     }
211                 }
212             }
213         }
214         else
215         {
216             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
217             {
218                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
219                 toMaster<< ensMap(pf, prims, cmpt);
220             }
221         }
222     }
226 template<class Type>
227 void writeAllFaceDataBinary
229     const char* key,
230     const labelList& prims,
231     const label nPrims,
232     const Field<Type>& pf,
233     const labelList& patchProcessors,
234     std::ofstream& ensightFile
237     if (nPrims)
238     {
239         if (Pstream::master())
240         {
241             writeEnsDataBinary(key,ensightFile);
243             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
244             {
245                 writeEnsDataBinary(ensMap(pf, prims, cmpt), ensightFile);
247                 forAll (patchProcessors, i)
248                 {
249                     if (patchProcessors[i] != 0)
250                     {
251                         label slave = patchProcessors[i];
252                         IPstream fromSlave(Pstream::scheduled, slave);
253                         scalarField pf(fromSlave);
255                         writeEnsDataBinary(pf, ensightFile);
256                     }
257                 }
258             }
259         }
260         else
261         {
262             for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
263             {
264                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
265                 toMaster<< ensMap(pf, prims, cmpt);
266             }
267         }
268     }
272 template<class Type>
273 bool writePatchField
275     const Foam::Field<Type>& pf,
276     const Foam::label patchi,
277     const Foam::label ensightPatchI,
278     const Foam::faceSets& boundaryFaceSet,
279     const Foam::ensightMesh::nFacePrimitives& nfp,
280     const Foam::labelList& patchProcessors,
281     Foam::OFstream& ensightFile
284     if (nfp.nTris || nfp.nQuads || nfp.nPolys)
285     {
286         if (Pstream::master())
287         {
288             ensightFile
289                 << "part" << nl
290                 << setw(10) << ensightPatchI << nl;
291         }
293         writeAllFaceData
294         (
295             "tria3",
296             boundaryFaceSet.tris,
297             nfp.nTris,
298             pf,
299             patchProcessors,
300             ensightFile
301         );
303         writeAllFaceData
304         (
305             "quad4",
306             boundaryFaceSet.quads,
307             nfp.nQuads,
308             pf,
309             patchProcessors,
310             ensightFile
311         );
313         writeAllFaceData
314         (
315             "nsided",
316             boundaryFaceSet.polys,
317             nfp.nPolys,
318             pf,
319             patchProcessors,
320             ensightFile
321         );
323         return true;
324     }
325     else
326     {
327         return false;
328     }
332 template<class Type>
333 bool writePatchFieldBinary
335     const Foam::Field<Type>& pf,
336     const Foam::label patchi,
337     const Foam::label ensightPatchI,
338     const Foam::faceSets& boundaryFaceSet,
339     const Foam::ensightMesh::nFacePrimitives& nfp,
340     const Foam::labelList& patchProcessors,
341     std::ofstream& ensightFile
344     if (nfp.nTris || nfp.nQuads || nfp.nPolys)
345     {
346         if (Pstream::master())
347         {
348             writeEnsDataBinary("part",ensightFile);
349             writeEnsDataBinary(ensightPatchI,ensightFile);
350         }
352         writeAllFaceDataBinary
353         (
354             "tria3",
355             boundaryFaceSet.tris,
356             nfp.nTris,
357             pf,
358             patchProcessors,
359             ensightFile
360         );
362         writeAllFaceDataBinary
363         (
364             "quad4",
365             boundaryFaceSet.quads,
366             nfp.nQuads,
367             pf,
368             patchProcessors,
369             ensightFile
370         );
372         writeAllFaceDataBinary
373         (
374             "nsided",
375             boundaryFaceSet.polys,
376             nfp.nPolys,
377             pf,
378             patchProcessors,
379             ensightFile
380         );
382         return true;
383     }
384     else
385     {
386         return false;
387     }
391 template<class Type>
392 void writePatchField
394     const Foam::word& fieldName,
395     const Foam::Field<Type>& pf,
396     const Foam::word& patchName,
397     const Foam::ensightMesh& eMesh,
398     const Foam::fileName& postProcPath,
399     const Foam::word& prepend,
400     const Foam::label timeIndex,
401     Foam::Ostream& ensightCaseFile
404     const Time& runTime = eMesh.mesh().time();
406     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
407     const wordList& allPatchNames = eMesh.allPatchNames();
408     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
409     const HashTable<ensightMesh::nFacePrimitives>&
410         nPatchPrims = eMesh.nPatchPrims();
412     label ensightPatchI = eMesh.patchPartOffset();
414     label patchi = -1;
416     forAll(allPatchNames, i)
417     {
418         if (allPatchNames[i] == patchName)
419         {
420             patchi = i;
421             break;
422         }
423         ensightPatchI++;
424     }
427     const labelList& patchProcessors = allPatchProcs[patchi];
429     word pfName = patchName + '.' + fieldName;
431     word timeFile = prepend + itoa(timeIndex);
433     OFstream *ensightFilePtr = NULL;
434     if (Pstream::master())
435     {
436         if (timeIndex == 0)
437         {
438             ensightCaseFile.setf(ios_base::left);
440             ensightCaseFile
441                 << pTraits<Type>::typeName
442                 << " per element:            1       "
443                 << setw(15) << pfName
444                 << (' ' + prepend + "***." + pfName).c_str()
445                 << nl;
446         }
448         // set the filename of the ensight file
449         fileName ensightFileName(timeFile + "." + pfName);
450         ensightFilePtr = new OFstream
451         (
452             postProcPath/ensightFileName,
453             ios_base::out|ios_base::trunc,
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             ios_base::out|ios_base::trunc,
545             runTime.writeFormat(),
546             runTime.writeVersion(),
547             runTime.writeCompression()
548         );
549     }
551     OFstream& ensightFile = *ensightFilePtr;
553     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
555     if (patchNames.empty())
556     {
557         if (Pstream::master())
558         {
559             if (timeIndex == 0)
560             {
561                 ensightCaseFile.setf(ios_base::left);
563                 ensightCaseFile
564                     << pTraits<Type>::typeName
565                     << " per element:            1       "
566                     << setw(15) << vf.name()
567                     << (' ' + prepend + "***." + vf.name()).c_str()
568                     << nl;
569             }
571             ensightFile
572                 << pTraits<Type>::typeName << nl
573                 << "part" << nl
574                 << setw(10) << 1 << nl;
576             ensightFile.setf(ios_base::scientific, ios_base::floatfield);
577             ensightFile.precision(5);
578         }
580         if (meshCellSets.nHexesWedges)
581         {
582             if (Pstream::master())
583             {
584                 ensightFile << "hexa8" << nl;
586                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
587                 {
588                     writeData
589                     (
590                         ensMap(vf, hexes, wedges, cmpt),
591                         ensightFile
592                     );
594                     for (int slave=1; slave<Pstream::nProcs(); slave++)
595                     {
596                         IPstream fromSlave(Pstream::scheduled, slave);
597                         scalarField data(fromSlave);
598                         writeData(data, ensightFile);
599                     }
600                 }
601             }
602             else
603             {
604                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
605                 {
606                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
607                     toMaster<< ensMap(vf, hexes, wedges, cmpt);
608                 }
609             }
610         }
612         writeAllData("penta6", vf, prisms, meshCellSets.nPrisms, ensightFile);
613         writeAllData("pyramid5", vf, pyrs, meshCellSets.nPyrs, ensightFile);
614         writeAllData("tetra4", vf, tets, meshCellSets.nTets, ensightFile);
615         writeAllData("nfaced", vf, polys, meshCellSets.nPolys, ensightFile);
616     }
618     label ensightPatchI = eMesh.patchPartOffset();
620     forAll(allPatchNames, patchi)
621     {
622         const word& patchName = allPatchNames[patchi];
623         const labelList& patchProcessors = allPatchProcs[patchi];
625         if (patchNames.empty() || patchNames.found(patchName))
626         {
627             if (mesh.boundary()[patchi].size())
628             {
629                 if
630                 (
631                     writePatchField
632                     (
633                         vf.boundaryField()[patchi],
634                         patchi,
635                         ensightPatchI,
636                         boundaryFaceSets[patchi],
637                         nPatchPrims.find(patchName)(),
638                         patchProcessors,
639                         ensightFile
640                     )
641                 )
642                 {
643                     ensightPatchI++;
644                 }
646             }
647             else if (Pstream::master())
648             {
649                 faceSets nullFaceSet;
651                 if
652                 (
653                     writePatchField
654                     (
655                         Field<Type>(),
656                         -1,
657                         ensightPatchI,
658                         nullFaceSet,
659                         nPatchPrims.find(patchName)(),
660                         patchProcessors,
661                         ensightFile
662                     )
663                 )
664                 {
665                     ensightPatchI++;
666                 }
667             }
668         }
669     }
671     if (Pstream::master())
672     {
673         delete ensightFilePtr;
674     }
678 template<class Type>
679 void ensightFieldBinary
681     const Foam::IOobject& fieldObject,
682     const Foam::ensightMesh& eMesh,
683     const Foam::fileName& postProcPath,
684     const Foam::word& prepend,
685     const Foam::label timeIndex,
686     Foam::Ostream& ensightCaseFile
689     Info<< "Converting field (binary) " << fieldObject.name() << endl;
691     word timeFile = prepend + itoa(timeIndex);
693     const fvMesh& mesh = eMesh.mesh();
694     //const Time& runTime = mesh.time();
696     const cellSets& meshCellSets = eMesh.meshCellSets();
697     const List<faceSets>& boundaryFaceSets = eMesh.boundaryFaceSets();
698     const wordList& allPatchNames = eMesh.allPatchNames();
699     const List<labelList>& allPatchProcs = eMesh.allPatchProcs();
700     const wordHashSet& patchNames = eMesh.patchNames();
701     const HashTable<ensightMesh::nFacePrimitives>&
702         nPatchPrims = eMesh.nPatchPrims();
704     const labelList& tets = meshCellSets.tets;
705     const labelList& pyrs = meshCellSets.pyrs;
706     const labelList& prisms = meshCellSets.prisms;
707     const labelList& wedges = meshCellSets.wedges;
708     const labelList& hexes = meshCellSets.hexes;
709     const labelList& polys = meshCellSets.polys;
711     std::ofstream *ensightFilePtr = NULL;
712     if (Pstream::master())
713     {
714         // set the filename of the ensight file
715         fileName ensightFileName(timeFile + "." + fieldObject.name());
716         ensightFilePtr = new std::ofstream
717         (
718             (postProcPath/ensightFileName).c_str(),
719             ios_base::out | ios_base::binary | ios_base::trunc
720         );
721         // Check on file opened?
722     }
724     std::ofstream& ensightFile = *ensightFilePtr;
726     GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
728     if (patchNames.empty())
729     {
730         if (Pstream::master())
731         {
732             if (timeIndex == 0)
733             {
734                 ensightCaseFile.setf(ios_base::left);
736                 ensightCaseFile
737                     << pTraits<Type>::typeName
738                     << " per element:            1       "
739                     << setw(15) << vf.name()
740                     << (' ' + prepend + "***." + vf.name()).c_str()
741                     << nl;
742             }
744             writeEnsDataBinary(pTraits<Type>::typeName,ensightFile);
745             writeEnsDataBinary("part",ensightFile);
746             writeEnsDataBinary(1,ensightFile);
747         }
749         if (meshCellSets.nHexesWedges)
750         {
751             if (Pstream::master())
752             {
753                 writeEnsDataBinary("hexa8",ensightFile);
755                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
756                 {
757                     writeEnsDataBinary
758                     (
759                         ensMap(vf, hexes, wedges, cmpt),
760                         ensightFile
761                     );
763                     for (int slave=1; slave<Pstream::nProcs(); slave++)
764                     {
765                         IPstream fromSlave(Pstream::scheduled, slave);
766                         scalarField data(fromSlave);
767                         writeEnsDataBinary(data, ensightFile);
768                     }
769                 }
770             }
771             else
772             {
773                 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
774                 {
775                     OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
776                     toMaster<< ensMap(vf, hexes, wedges, cmpt);
777                 }
778             }
779         }
781         writeAllDataBinary
782         (
783             "penta6",
784             vf,
785             prisms,
786             meshCellSets.nPrisms,
787             ensightFile
788         );
790         writeAllDataBinary
791         (
792             "pyramid5",
793             vf,
794             pyrs,
795             meshCellSets.nPyrs,
796             ensightFile
797         );
799         writeAllDataBinary
800         (
801             "tetra4",
802             vf,
803             tets,
804             meshCellSets.nTets,
805             ensightFile
806         );
808         writeAllDataBinary
809         (
810             "nfaced",
811             vf,
812             polys,
813             meshCellSets.nPolys,
814             ensightFile
815         );
816     }
818     label ensightPatchI = eMesh.patchPartOffset();
820     forAll(allPatchNames, patchi)
821     {
822         const word& patchName = allPatchNames[patchi];
823         const labelList& patchProcessors = allPatchProcs[patchi];
825         if (patchNames.empty() || patchNames.found(patchName))
826         {
827             if (mesh.boundary()[patchi].size())
828             {
829                 if
830                 (
831                     writePatchFieldBinary
832                     (
833                         vf.boundaryField()[patchi],
834                         patchi,
835                         ensightPatchI,
836                         boundaryFaceSets[patchi],
837                         nPatchPrims.find(patchName)(),
838                         patchProcessors,
839                         ensightFile
840                     )
841                 )
842                 {
843                     ensightPatchI++;
844                 }
846             }
847             else if (Pstream::master())
848             {
849                 faceSets nullFaceSet;
851                 if
852                 (
853                     writePatchFieldBinary
854                     (
855                         Field<Type>(),
856                         -1,
857                         ensightPatchI,
858                         nullFaceSet,
859                         nPatchPrims.find(patchName)(),
860                         patchProcessors,
861                         ensightFile
862                     )
863                 )
864                 {
865                     ensightPatchI++;
866                 }
867             }
868         }
869     }
871     if (Pstream::master())
872     {
873         ensightFile.close();
874     }
878 template<class Type>
879 void ensightField
881     const Foam::IOobject& fieldObject,
882     const Foam::ensightMesh& eMesh,
883     const Foam::fileName& postProcPath,
884     const Foam::word& prepend,
885     const Foam::label timeIndex,
886     const bool binary,
887     Foam::Ostream& ensightCaseFile
890     if (binary)
891     {
892         ensightFieldBinary<Type>
893         (
894             fieldObject,
895             eMesh,
896             postProcPath,
897             prepend,
898             timeIndex,
899             ensightCaseFile
900         );
901     }
902     else
903     {
904         ensightFieldAscii<Type>
905         (
906             fieldObject,
907             eMesh,
908             postProcPath,
909             prepend,
910             timeIndex,
911             ensightCaseFile
912         );
913     }
917 // ************************************************************************* //