1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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"
29 #include "volFields.H"
33 #include "ensightWriteBinary.H"
37 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
39 void writeData(const scalarField& sf, OFstream& ensightFile)
43 if (mag( sf[i] ) >= scalar(floatScalarVSMALL))
45 ensightFile << setw(12) << sf[i] << nl;
49 ensightFile << setw(12) << scalar(0) << nl;
58 const Field<Type>& vf,
63 scalarField mf(map.size());
67 mf[i] = component(vf[map[i]], cmpt);
77 const Field<Type>& vf,
78 const labelList& map1,
79 const labelList& map2,
83 scalarField mf(map1.size() + map2.size());
87 mf[i] = component(vf[map1[i]], cmpt);
90 label offset = map1.size();
94 mf[i + offset] = component(vf[map2[i]], cmpt);
105 const Field<Type>& vf,
106 const labelList& prims,
108 OFstream& ensightFile
113 if (Pstream::master())
115 ensightFile << key << nl;
117 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
119 writeData(map(vf, prims, cmpt), ensightFile);
121 for (int slave=1; slave<Pstream::nProcs(); slave++)
123 IPstream fromSlave(Pstream::scheduled, slave);
124 scalarField data(fromSlave);
125 writeData(data, ensightFile);
131 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
133 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
134 toMaster<< map(vf, prims, cmpt);
142 void writeAllDataBinary
145 const Field<Type>& vf,
146 const labelList& prims,
148 std::ofstream& ensightFile
153 if (Pstream::master())
155 writeEnsDataBinary(key,ensightFile);
157 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
159 writeEnsDataBinary(map(vf, prims, cmpt), ensightFile);
161 for (int slave=1; slave<Pstream::nProcs(); slave++)
163 IPstream fromSlave(Pstream::scheduled, slave);
164 scalarField data(fromSlave);
165 writeEnsDataBinary(data, ensightFile);
171 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
173 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
174 toMaster<< map(vf, prims, cmpt);
182 void writeAllFaceData
185 const labelList& prims,
187 const Field<Type>& pf,
188 const labelList& patchProcessors,
189 OFstream& ensightFile
194 if (Pstream::master())
196 ensightFile << key << nl;
198 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
200 writeData(map(pf, prims, cmpt), ensightFile);
202 forAll (patchProcessors, i)
204 if (patchProcessors[i] != 0)
206 label slave = patchProcessors[i];
207 IPstream fromSlave(Pstream::scheduled, slave);
208 scalarField pf(fromSlave);
210 writeData(pf, ensightFile);
217 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
219 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
220 toMaster<< map(pf, prims, cmpt);
228 void writeAllFaceDataBinary
231 const labelList& prims,
233 const Field<Type>& pf,
234 const labelList& patchProcessors,
235 std::ofstream& ensightFile
240 if (Pstream::master())
242 writeEnsDataBinary(key,ensightFile);
244 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
246 writeEnsDataBinary(map(pf, prims, cmpt), ensightFile);
248 forAll (patchProcessors, i)
250 if (patchProcessors[i] != 0)
252 label slave = patchProcessors[i];
253 IPstream fromSlave(Pstream::scheduled, slave);
254 scalarField pf(fromSlave);
256 writeEnsDataBinary(pf, ensightFile);
263 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
265 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
266 toMaster<< map(pf, prims, cmpt);
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)
287 if (Pstream::master())
291 << setw(10) << ensightPatchI << nl;
297 boundaryFaceSet.tris,
307 boundaryFaceSet.quads,
317 boundaryFaceSet.polys,
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)
347 if (Pstream::master())
349 writeEnsDataBinary("part",ensightFile);
350 writeEnsDataBinary(ensightPatchI,ensightFile);
353 writeAllFaceDataBinary
356 boundaryFaceSet.tris,
363 writeAllFaceDataBinary
366 boundaryFaceSet.quads,
373 writeAllFaceDataBinary
376 boundaryFaceSet.polys,
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();
417 forAll(allPatchNames, i)
419 if (allPatchNames[i] == patchName)
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())
439 ensightCaseFile.setf(ios_base::left);
442 << pTraits<Type>::typeName
443 << " per element: 1 "
444 << setw(15) << pfName
445 << (' ' + prepend + "***." + pfName).c_str()
449 // set the filename of the ensight file
450 fileName ensightFileName(timeFile + "." + pfName);
451 ensightFilePtr = new OFstream
453 postProcPath/ensightFileName,
454 runTime.writeFormat(),
455 runTime.writeVersion(),
456 runTime.writeCompression()
460 OFstream& ensightFile = *ensightFilePtr;
462 if (Pstream::master())
464 ensightFile << pTraits<Type>::typeName << nl;
474 boundaryFaceSets[patchi],
475 nPatchPrims.find(patchName)(),
482 faceSets nullFaceSets;
490 nPatchPrims.find(patchName)(),
496 if (Pstream::master())
498 delete ensightFilePtr;
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())
539 // set the filename of the ensight file
540 fileName ensightFileName(timeFile + "." + fieldObject.name());
541 ensightFilePtr = new OFstream
543 postProcPath/ensightFileName,
544 runTime.writeFormat(),
545 runTime.writeVersion(),
546 runTime.writeCompression()
550 OFstream& ensightFile = *ensightFilePtr;
552 GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
554 if (patchNames.empty())
556 if (Pstream::master())
560 ensightCaseFile.setf(ios_base::left);
563 << pTraits<Type>::typeName
564 << " per element: 1 "
565 << setw(15) << vf.name()
566 << (' ' + prepend + "***." + vf.name()).c_str()
571 << pTraits<Type>::typeName << nl
573 << setw(10) << 1 << nl;
575 ensightFile.setf(ios_base::scientific, ios_base::floatfield);
576 ensightFile.precision(5);
579 if (meshCellSets.nHexesWedges)
581 if (Pstream::master())
583 ensightFile << "hexa8" << nl;
585 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
589 map(vf, hexes, wedges, cmpt),
593 for (int slave=1; slave<Pstream::nProcs(); slave++)
595 IPstream fromSlave(Pstream::scheduled, slave);
596 scalarField data(fromSlave);
597 writeData(data, ensightFile);
603 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
605 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
606 toMaster<< map(vf, hexes, wedges, cmpt);
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);
617 label ensightPatchI = eMesh.patchPartOffset();
619 forAll(allPatchNames, patchi)
621 const word& patchName = allPatchNames[patchi];
622 const labelList& patchProcessors = allPatchProcs[patchi];
624 if (patchNames.empty() || patchNames.found(patchName))
626 if (mesh.boundary()[patchi].size())
632 vf.boundaryField()[patchi],
635 boundaryFaceSets[patchi],
636 nPatchPrims.find(patchName)(),
646 else if (Pstream::master())
648 faceSets nullFaceSet;
658 nPatchPrims.find(patchName)(),
670 if (Pstream::master())
672 delete ensightFilePtr;
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())
713 // set the filename of the ensight file
714 fileName ensightFileName(timeFile + "." + fieldObject.name());
715 ensightFilePtr = new std::ofstream
717 (postProcPath/ensightFileName).c_str(),
718 ios_base::out | ios_base::binary | ios_base::trunc
720 // Check on file opened?
723 std::ofstream& ensightFile = *ensightFilePtr;
725 GeometricField<Type, fvPatchField, volMesh> vf(fieldObject, mesh);
727 if (patchNames.empty())
729 if (Pstream::master())
733 ensightCaseFile.setf(ios_base::left);
736 << pTraits<Type>::typeName
737 << " per element: 1 "
738 << setw(15) << vf.name()
739 << (' ' + prepend + "***." + vf.name()).c_str()
743 writeEnsDataBinary(pTraits<Type>::typeName,ensightFile);
744 writeEnsDataBinary("part",ensightFile);
745 writeEnsDataBinary(1,ensightFile);
748 if (meshCellSets.nHexesWedges)
750 if (Pstream::master())
752 writeEnsDataBinary("hexa8",ensightFile);
754 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
758 map(vf, hexes, wedges, cmpt),
762 for (int slave=1; slave<Pstream::nProcs(); slave++)
764 IPstream fromSlave(Pstream::scheduled, slave);
765 scalarField data(fromSlave);
766 writeEnsDataBinary(data, ensightFile);
772 for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
774 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
775 toMaster<< map(vf, hexes, wedges, cmpt);
785 meshCellSets.nPrisms,
817 label ensightPatchI = eMesh.patchPartOffset();
819 forAll(allPatchNames, patchi)
821 const word& patchName = allPatchNames[patchi];
822 const labelList& patchProcessors = allPatchProcs[patchi];
824 if (patchNames.empty() || patchNames.found(patchName))
826 if (mesh.boundary()[patchi].size())
830 writePatchFieldBinary
832 vf.boundaryField()[patchi],
835 boundaryFaceSets[patchi],
836 nPatchPrims.find(patchName)(),
846 else if (Pstream::master())
848 faceSets nullFaceSet;
852 writePatchFieldBinary
858 nPatchPrims.find(patchName)(),
870 if (Pstream::master())
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,
886 Foam::Ostream& ensightCaseFile
891 ensightFieldBinary<Type>
903 ensightFieldAscii<Type>
916 // ************************************************************************* //