/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "ensightField.H" #include "fvMesh.H" #include "volFields.H" #include "OFstream.H" #include "IOmanip.H" #include "itoa.H" #include "volPointInterpolation.H" #include "ensightBinaryStream.H" #include "ensightAsciiStream.H" #include "globalIndex.H" #include "ensightPTraits.H" using namespace Foam; // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template tmp> volField ( const fvMeshSubset& meshSubsetter, const VolField& vf ) { if (meshSubsetter.hasSubMesh()) { tmp> tfld ( meshSubsetter.interpolate(vf) ); tfld.ref().checkOut(); tfld.ref().rename(vf.name()); return tfld; } else { return vf; } } template Field map ( const Field& vf, const labelList& map1, const labelList& map2 ) { Field mf(map1.size() + map2.size()); forAll(map1, i) { mf[i] = vf[map1[i]]; } label offset = map1.size(); forAll(map2, i) { mf[i + offset] = vf[map2[i]]; } return mf; } template void writeField ( const char* key, const Field& vf, ensightStream& ensightFile ) { if (returnReduce(vf.size(), sumOp()) > 0) { if (Pstream::master()) { ensightFile.write(key); for (direction cmpt=0; cmpt::nComponents; cmpt++) { ensightFile.write(vf.component(cmpt)); for (int slave=1; slave::nComponents; cmpt++) { OPstream toMaster ( Pstream::commsTypes::scheduled, Pstream::masterNo() ); toMaster<< vf.component(cmpt); } } } } template bool writePatchField ( const Field& pf, const label patchi, const label ensightPatchi, const faceSets& boundaryFaceSet, const ensightMesh::nFacePrimitives& nfp, ensightStream& ensightFile ) { if (nfp.nTris || nfp.nQuads || nfp.nPolys) { if (Pstream::master()) { ensightFile.writePartHeader(ensightPatchi); } writeField ( "tria3", Field(pf, boundaryFaceSet.tris), ensightFile ); writeField ( "quad4", Field(pf, boundaryFaceSet.quads), ensightFile ); writeField ( "nsided", Field(pf, boundaryFaceSet.polys), ensightFile ); return true; } else { return false; } } template void writePatchField ( const word& fieldName, const Field& pf, const word& patchName, const ensightMesh& eMesh, const fileName& postProcPath, const word& prepend, const label timeIndex, const bool binary, Ostream& ensightCaseFile ) { const Time& runTime = eMesh.mesh().time(); const List& boundaryFaceSets = eMesh.boundaryFaceSets(); const wordList& allPatchNames = eMesh.allPatchNames(); const HashTable& nPatchPrims = eMesh.nPatchPrims(); label ensightPatchi = eMesh.patchPartOffset(); label patchi = -1; forAll(allPatchNames, i) { if (allPatchNames[i] == patchName) { patchi = i; break; } ensightPatchi++; } word pfName = patchName + '.' + fieldName; word timeFile = prepend + itoa(timeIndex); ensightStream* ensightFilePtr = nullptr; if (Pstream::master()) { if (timeIndex == 0) { ensightCaseFile.setf(ios_base::left); ensightCaseFile << ensightPTraits::typeName << " per element: 1 " << setw(15) << pfName << (' ' + prepend + "****." + pfName).c_str() << nl; } // set the filename of the ensight file fileName ensightFileName(timeFile + "." + pfName); if (binary) { ensightFilePtr = new ensightBinaryStream ( postProcPath/ensightFileName, runTime ); } else { ensightFilePtr = new ensightAsciiStream ( postProcPath/ensightFileName, runTime ); } } ensightStream& ensightFile = *ensightFilePtr; if (Pstream::master()) { ensightFile.write(ensightPTraits::typeName); } if (patchi >= 0) { writePatchField ( pf, patchi, ensightPatchi, boundaryFaceSets[patchi], nPatchPrims.find(patchName)(), ensightFile ); } else { faceSets nullFaceSets; writePatchField ( Field(), -1, ensightPatchi, nullFaceSets, nPatchPrims.find(patchName)(), ensightFile ); } if (Pstream::master()) { delete ensightFilePtr; } } template void ensightField ( const VolField& vf, const ensightMesh& eMesh, const fileName& postProcPath, const word& prepend, const label timeIndex, const bool binary, Ostream& ensightCaseFile ) { Info<< "Converting field " << vf.name() << endl; word timeFile = prepend + itoa(timeIndex); const fvMesh& mesh = eMesh.mesh(); const Time& runTime = mesh.time(); const cellSets& meshCellSets = eMesh.meshCellSets(); const List& boundaryFaceSets = eMesh.boundaryFaceSets(); const wordList& allPatchNames = eMesh.allPatchNames(); const wordHashSet& patchNames = eMesh.patchNames(); const HashTable& nPatchPrims = eMesh.nPatchPrims(); const List& faceZoneFaceSets = eMesh.faceZoneFaceSets(); const wordHashSet& faceZoneNames = eMesh.faceZoneNames(); const HashTable& nFaceZonePrims = eMesh.nFaceZonePrims(); const labelList& tets = meshCellSets.tets; const labelList& pyrs = meshCellSets.pyrs; const labelList& prisms = meshCellSets.prisms; const labelList& wedges = meshCellSets.wedges; const labelList& hexes = meshCellSets.hexes; const labelList& polys = meshCellSets.polys; ensightStream* ensightFilePtr = nullptr; if (Pstream::master()) { // set the filename of the ensight file fileName ensightFileName(timeFile + "." + vf.name()); if (binary) { ensightFilePtr = new ensightBinaryStream ( postProcPath/ensightFileName, runTime ); } else { ensightFilePtr = new ensightAsciiStream ( postProcPath/ensightFileName, runTime ); } } ensightStream& ensightFile = *ensightFilePtr; if (Pstream::master()) { if (timeIndex == 0) { ensightCaseFile.setf(ios_base::left); ensightCaseFile << ensightPTraits::typeName << " per element: 1 " << setw(15) << vf.name() << (' ' + prepend + "****." + vf.name()).c_str() << nl; } ensightFile.write(ensightPTraits::typeName); } if (patchNames.empty()) { eMesh.barrier(); if (Pstream::master()) { ensightFile.writePartHeader(1); } writeField ( "hexa8", map(vf, hexes, wedges), ensightFile ); writeField ( "penta6", Field(vf, prisms), ensightFile ); writeField ( "pyramid5", Field(vf, pyrs), ensightFile ); writeField ( "tetra4", Field(vf, tets), ensightFile ); writeField ( "nfaced", Field(vf, polys), ensightFile ); } label ensightPatchi = eMesh.patchPartOffset(); forAll(allPatchNames, patchi) { const word& patchName = allPatchNames[patchi]; eMesh.barrier(); if (patchNames.empty() || patchNames.found(patchName)) { if ( writePatchField ( vf.boundaryField()[patchi], patchi, ensightPatchi, boundaryFaceSets[patchi], nPatchPrims.find(patchName)(), ensightFile ) ) { ensightPatchi++; } } } // write faceZones, if requested if (faceZoneNames.size()) { // Interpolates cell values to faces - needed only when exporting // faceZones... SurfaceField sf ( linearInterpolate(vf) ); forAllConstIter(wordHashSet, faceZoneNames, iter) { const word& faceZoneName = iter.key(); eMesh.barrier(); label zoneID = mesh.faceZones().findIndex(faceZoneName); const faceZone& fz = mesh.faceZones()[zoneID]; // Prepare data to write label nIncluded = 0; forAll(fz, i) { if (eMesh.faceToBeIncluded(fz[i])) { ++nIncluded; } } Field values(nIncluded); // Loop on the faceZone and store the needed field values label j = 0; forAll(fz, i) { label facei = fz[i]; if (mesh.isInternalFace(facei)) { values[j] = sf[facei]; ++j; } else { if (eMesh.faceToBeIncluded(facei)) { label patchi = mesh.poly().boundary().whichPatch(facei); const polyPatch& pp = mesh.poly().boundary()[patchi]; label patchFacei = pp.whichFace(facei); Type value = sf.boundaryField()[patchi][patchFacei]; values[j] = value; ++j; } } } if ( writePatchField ( values, zoneID, ensightPatchi, faceZoneFaceSets[zoneID], nFaceZonePrims.find(faceZoneName)(), ensightFile ) ) { ensightPatchi++; } } } if (Pstream::master()) { delete ensightFilePtr; } } template void ensightPointField ( const PointField& pf, const ensightMesh& eMesh, const fileName& postProcPath, const word& prepend, const label timeIndex, const bool binary, Ostream& ensightCaseFile ) { Info<< "Converting field " << pf.name() << endl; word timeFile = prepend + itoa(timeIndex); const fvMesh& mesh = eMesh.mesh(); const wordList& allPatchNames = eMesh.allPatchNames(); const wordHashSet& patchNames = eMesh.patchNames(); const wordHashSet& faceZoneNames = eMesh.faceZoneNames(); ensightStream* ensightFilePtr = nullptr; if (Pstream::master()) { // set the filename of the ensight file fileName ensightFileName(timeFile + "." + pf.name()); if (binary) { ensightFilePtr = new ensightBinaryStream ( postProcPath/ensightFileName, mesh.time() ); } else { ensightFilePtr = new ensightAsciiStream ( postProcPath/ensightFileName, mesh.time() ); } } ensightStream& ensightFile = *ensightFilePtr; if (Pstream::master()) { if (timeIndex == 0) { ensightCaseFile.setf(ios_base::left); ensightCaseFile << ensightPTraits::typeName << " per node: 1 " << setw(15) << pf.name() << (' ' + prepend + "****." + pf.name()).c_str() << nl; } ensightFile.write(ensightPTraits::typeName); } if (eMesh.patchNames().empty()) { eMesh.barrier(); if (Pstream::master()) { ensightFile.writePartHeader(1); } writeField ( "coordinates", Field(pf.primitiveField(), eMesh.uniquePointMap()), ensightFile ); } label ensightPatchi = eMesh.patchPartOffset(); forAll(allPatchNames, patchi) { const word& patchName = allPatchNames[patchi]; eMesh.barrier(); if (patchNames.empty() || patchNames.found(patchName)) { const fvPatch& p = mesh.boundary()[patchi]; if ( returnReduce(p.size(), sumOp()) > 0 ) { // Renumber the patch points/faces into unique points labelList pointToGlobal; labelList uniqueMeshPointLabels; autoPtr globalPointsPtr = mesh.globalData().mergePoints ( p.poly().meshPoints(), p.poly().meshPointMap(), pointToGlobal, uniqueMeshPointLabels ); if (Pstream::master()) { ensightFile.writePartHeader(ensightPatchi); } writeField ( "coordinates", Field(pf.primitiveField(), uniqueMeshPointLabels), ensightFile ); ensightPatchi++; } } } // write faceZones, if requested if (faceZoneNames.size()) { forAllConstIter(wordHashSet, faceZoneNames, iter) { const word& faceZoneName = iter.key(); eMesh.barrier(); label zoneID = mesh.faceZones().findIndex(faceZoneName); const faceZone& fz = mesh.faceZones()[zoneID]; if (returnReduce(fz.patch().nPoints(), sumOp()) > 0) { // Renumber the faceZone points/faces into unique points labelList pointToGlobal; labelList uniqueMeshPointLabels; autoPtr globalPointsPtr = mesh.globalData().mergePoints ( fz.patch().meshPoints(), fz.patch().meshPointMap(), pointToGlobal, uniqueMeshPointLabels ); if (Pstream::master()) { ensightFile.writePartHeader(ensightPatchi); } writeField ( "coordinates", Field ( pf.primitiveField(), uniqueMeshPointLabels ), ensightFile ); ensightPatchi++; } } } if (Pstream::master()) { delete ensightFilePtr; } } template void ensightField ( const VolField& vf, const ensightMesh& eMesh, const fileName& postProcPath, const word& prepend, const label timeIndex, const bool binary, const bool nodeValues, Ostream& ensightCaseFile ) { if (nodeValues) { tmp> pfld ( volPointInterpolation::New(vf.mesh()).interpolate(vf) ); pfld.ref().rename(vf.name()); ensightPointField ( pfld, eMesh, postProcPath, prepend, timeIndex, binary, ensightCaseFile ); } else { ensightField ( vf, eMesh, postProcPath, prepend, timeIndex, binary, ensightCaseFile ); } } // ************************************************************************* //