/*---------------------------------------------------------------------------*\ ========= | \\ / 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 "fvMeshDistribute.H" #include "fvMeshAdder.H" #include "processorCyclicFvPatchField.H" #include "processorCyclicFvsPatchField.H" #include "nonConformalProcessorCyclicFvPatchField.H" #include "nonConformalProcessorCyclicFvsPatchField.H" #include "polyTopoChange.H" #include "removeCells.H" #include "polyDistributionMap.H" #include "syncTools.H" #include "CompactListList.H" #include "fvMeshTools.H" #include "globalIndex.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { defineTypeNameAndDebug(fvMeshDistribute, 0); //- Less function class that can be used for sorting processor patches class lessProcPatches { const labelList& nbrProc_; const labelList& referPatchIndex_; const labelList& referNbrPatchIndex_; public: lessProcPatches ( const labelList& nbrProc, const labelList& referPatchID, const labelList& referNbrPatchID ) : nbrProc_(nbrProc), referPatchIndex_(referPatchID), referNbrPatchIndex_(referNbrPatchID) {} bool operator()(const label a, const label b) { // Lower processor ID-s go first if (nbrProc_[a] < nbrProc_[b]) { return true; } else if (nbrProc_[a] > nbrProc_[b]) { return false; } // Non-cyclics go next else if (referPatchIndex_[a] == -1) { return true; } else if (referPatchIndex_[b] == -1) { return false; } // Cyclics should be ordered by refer patch ID if this is the owner // (lower processor ID), and by the neighbour refer patch ID if this is // the neighbour else if (Pstream::myProcNo() < nbrProc_[a]) { return referPatchIndex_[a] < referPatchIndex_[b]; } else { return referNbrPatchIndex_[a] < referNbrPatchIndex_[b]; } } }; } // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // void Foam::fvMeshDistribute::inplaceRenumberWithFlip ( const labelUList& oldToNew, const bool oldToNewHasFlip, const bool lstHasFlip, labelUList& lst ) { if (!lstHasFlip && !oldToNewHasFlip) { Foam::inplaceRenumber(oldToNew, lst); } else { // Either input data or map encodes sign so result encodes sign forAll(lst, elemI) { // Extract old value and sign label val = lst[elemI]; label sign = 1; if (lstHasFlip) { if (val > 0) { val = val-1; } else if (val < 0) { val = -val-1; sign = -1; } else { FatalErrorInFunction << "Problem : zero value " << val << " at index " << elemI << " out of " << lst.size() << " list with flip bit" << exit(FatalError); } } // Lookup new value and possibly change sign label newVal = oldToNew[val]; if (oldToNewHasFlip) { if (newVal > 0) { newVal = newVal-1; } else if (newVal < 0) { newVal = -newVal-1; sign = -sign; } else { FatalErrorInFunction << "Problem : zero value " << newVal << " at index " << elemI << " out of " << oldToNew.size() << " list with flip bit" << exit(FatalError); } } // Encode new value and sign lst[elemI] = sign*(newVal+1); } } } Foam::labelList Foam::fvMeshDistribute::select ( const bool selectEqual, const labelList& values, const label value ) { label n = 0; forAll(values, i) { if (selectEqual == (values[i] == value)) { n++; } } labelList indices(n); n = 0; forAll(values, i) { if (selectEqual == (values[i] == value)) { indices[n++] = i; } } return indices; } Foam::wordList Foam::fvMeshDistribute::fieldNames ( const word& typeName, label& nFields ) const { wordList fieldNames(mesh_.toc(typeName)); if (fieldNames.size()) { HashSet fieldSet(fieldNames); fieldSet -= fvMesh::curGeometryFields; fieldNames = fieldSet.toc(); nFields += checkEqualWordList(typeName, fieldNames); } return fieldNames; } Foam::label Foam::fvMeshDistribute::checkEqualWordList ( const word& typeName, const wordList& lst ) { List allNames(Pstream::nProcs()); allNames[Pstream::myProcNo()] = lst; Pstream::gatherList(allNames); Pstream::scatterList(allNames); for (label proci = 1; proci < Pstream::nProcs(); proci++) { if (allNames[proci] != allNames[0]) { FatalErrorInFunction << "When checking for equal numbers of " << typeName << " :" << nl << "processor0 has:" << allNames[0] << nl << "processor" << proci << " has:" << allNames[proci] << nl << typeName << " need to be synchronised on all processors." << exit(FatalError); } } return lst.size(); } Foam::wordList Foam::fvMeshDistribute::mergeWordList(const wordList& procNames) { List allNames(Pstream::nProcs()); allNames[Pstream::myProcNo()] = procNames; Pstream::gatherList(allNames); Pstream::scatterList(allNames); HashSet mergedNames; forAll(allNames, proci) { forAll(allNames[proci], i) { mergedNames.insert(allNames[proci][i]); } } return mergedNames.toc(); } void Foam::fvMeshDistribute::printMeshInfo(const fvMesh& mesh) { Pout<< "Primitives:" << nl << " points :" << mesh.nPoints() << nl << " bb :" << boundBox(mesh.points(), false) << nl << " internalFaces:" << mesh.nInternalFaces() << nl << " faces :" << mesh.nFaces() << nl << " cells :" << mesh.nCells() << nl; const fvBoundaryMesh& patches = mesh.boundary(); Pout<< "Patches:" << endl; forAll(patches, patchi) { const polyPatch& pp = patches[patchi].poly(); Pout<< " " << patchi << " name:" << pp.name() << " size:" << pp.size() << " start:" << pp.start() << " type:" << pp.type() << endl; } if (mesh.pointZones().size()) { Pout<< "PointZones:" << endl; forAll(mesh.pointZones(), zoneI) { const pointZone& pz = mesh.pointZones()[zoneI]; Pout<< " " << zoneI << " name:" << pz.name() << " size:" << pz.size() << endl; } } if (mesh.faceZones().size()) { Pout<< "FaceZones:" << endl; forAll(mesh.faceZones(), zoneI) { const faceZone& fz = mesh.faceZones()[zoneI]; Pout<< " " << zoneI << " name:" << fz.name() << " size:" << fz.size() << endl; } } if (mesh.cellZones().size()) { Pout<< "CellZones:" << endl; forAll(mesh.cellZones(), zoneI) { const cellZone& cz = mesh.cellZones()[zoneI]; Pout<< " " << zoneI << " name:" << cz.name() << " size:" << cz.size() << endl; } } } void Foam::fvMeshDistribute::printCoupleInfo ( const primitiveMesh& mesh, const labelList& sourceFace, const labelList& sourceProc, const labelList& sourcePatch, const labelList& sourceNewNbrProc ) { Pout<< nl << "Current coupling info:" << endl; forAll(sourceFace, bFacei) { label meshFacei = mesh.nInternalFaces() + bFacei; Pout<< " meshFace:" << meshFacei << " fc:" << mesh.faceCentres()[meshFacei] << " connects to proc:" << sourceProc[bFacei] << "/face:" << sourceFace[bFacei] << " which will move to proc:" << sourceNewNbrProc[bFacei] << endl; } } Foam::label Foam::fvMeshDistribute::findInternalPatch() const { const polyBoundaryMesh& patches = mesh_.poly().boundary(); label internalPatchi = -1; forAll(patches, patchi) { const polyPatch& pp = patches[patchi]; if (isA(pp)) { internalPatchi = patchi; break; } } if (internalPatchi == -1) { FatalErrorInFunction << "Cannot find a internal patch in " << patches.names() << nl << " of types " << patches.types() << nl << " An internal patch must be provided for the exposed " "internal faces." << exit(FatalError); } if (debug) { Pout<< "findInternalPatch : using patch " << internalPatchi << " name:" << patches[internalPatchi].name() << " type:" << patches[internalPatchi].type() << " for the exposed internal faces." << endl; } // Do additional test for processor patches intermingled with non-proc // patches. label procPatchi = -1; forAll(patches, patchi) { if (isA(patches[patchi])) { procPatchi = patchi; } else if (procPatchi != -1) { FatalErrorInFunction << "Processor patches should be at end of patch list." << endl << "Have processor patch " << procPatchi << " followed by non-processor patch " << patchi << " in patches " << patches.names() << abort(FatalError); } } return internalPatchi; } Foam::label Foam::fvMeshDistribute::findNonEmptyPatch() const { const polyBoundaryMesh& patches = mesh_.poly().boundary(); label nonEmptyPatchi = -1; forAllReverse(patches, patchi) { const polyPatch& pp = patches[patchi]; if (!isA(pp) && !pp.coupled()) { nonEmptyPatchi = patchi; break; } } if (nonEmptyPatchi == -1) { FatalErrorInFunction << "Cannot find a non-empty patch in " << patches.names() << nl << " of types " << patches.types() << nl << " An non-empty patch must be provided for the exposed " "internal faces." << exit(FatalError); } if (debug) { Pout<< "findNonEmptyPatch : using patch " << nonEmptyPatchi << " name:" << patches[nonEmptyPatchi].name() << " type:" << patches[nonEmptyPatchi].type() << " for the exposed non-empty faces." << endl; } return nonEmptyPatchi; } Foam::autoPtr Foam::fvMeshDistribute::deleteProcPatches ( const label destinationPatch ) { // New patchID per boundary faces to be repatched. Is -1 (no change) // or new patchID labelList newPatchID(mesh_.nFaces() - mesh_.nInternalFaces(), -1); forAll(mesh_.poly().boundary(), patchi) { const polyPatch& pp = mesh_.poly().boundary()[patchi]; if (isA(pp)) { if (debug) { Pout<< "Moving all faces of patch " << pp.name() << " into patch " << destinationPatch << endl; } label offset = pp.start() - mesh_.nInternalFaces(); forAll(pp, i) { newPatchID[offset+i] = destinationPatch; } } } // Note: order of boundary faces been kept the same since the // destinationPatch is at the end and we have visited the patches in // incremental order. labelListList dummyFaceMaps; autoPtr map = repatch(newPatchID, dummyFaceMaps); // Delete (now empty) processor patches. { labelList oldToNew(identityMap(mesh_.poly().boundary().size())); label newI = 0; // Non processor patches first forAll(mesh_.poly().boundary(), patchi) { if (!isA(mesh_.poly().boundary()[patchi])) { oldToNew[patchi] = newI++; } } label nNonProcPatches = newI; // Processor patches as last forAll(mesh_.poly().boundary(), patchi) { if (isA(mesh_.poly().boundary()[patchi])) { oldToNew[patchi] = newI++; } } fvMeshTools::reorderPatches(mesh_, oldToNew, nNonProcPatches, false); } return map; } Foam::autoPtr Foam::fvMeshDistribute::repatch ( const labelList& newPatchID, // per boundary face -1 or new patchID labelListList& constructFaceMap ) { polyTopoChange meshMod(mesh_); forAll(newPatchID, bFacei) { if (newPatchID[bFacei] != -1) { const label facei = mesh_.nInternalFaces() + bFacei; meshMod.modifyFace ( mesh_.faces()[facei], // modified face facei, // label of face mesh_.faceOwner()[facei], // owner -1, // neighbour false, // face flip newPatchID[bFacei] // patch for face ); } } // Do mapping of fields from one patchField to the other ourselves since // is currently not supported by topoChange. // Store boundary fields PtrList> vsf; saveBoundaryFields>(vsf); PtrList> vvf; saveBoundaryFields>(vvf); PtrList> vsptf; saveBoundaryFields>(vsptf); PtrList> vsytf; saveBoundaryFields>(vsytf); PtrList> vtf; saveBoundaryFields>(vtf); PtrList> ssf; saveBoundaryFields>(ssf); PtrList> svf; saveBoundaryFields>(svf); PtrList> ssptf; saveBoundaryFields>(ssptf); PtrList> ssytf; saveBoundaryFields>(ssytf); PtrList> stf; saveBoundaryFields>(stf); // Change the mesh (without keeping old points). // Note: parallel comms allowed. // // NOTE: there is one very particular problem with this ordering. // We first create the processor patches and use these to merge out // shared points (see mergeSharedPoints below). So temporarily points // and edges do not match! autoPtr map = meshMod.changeMesh(mesh_, true); // Update zones mesh_.topoChangeZones(map); // Update fields mesh_.mapFields(map); // Map patch fields using stored boundary fields. Note: assumes order // of fields has not changed in object registry! mapBoundaryFields>(map, vsf); mapBoundaryFields>(map, vvf); mapBoundaryFields>(map, vsptf); mapBoundaryFields>(map, vsytf); mapBoundaryFields>(map, vtf); mapBoundaryFields>(map, ssf); mapBoundaryFields>(map, svf); mapBoundaryFields>(map, ssptf); mapBoundaryFields>(map, ssytf); mapBoundaryFields>(map, stf); // Adapt constructMaps. if (debug) { label index = findIndex(map().reverseFaceMap(), -1); if (index != -1) { FatalErrorInFunction << "reverseFaceMap contains -1 at index:" << index << endl << "This means that the repatch operation was not just" << " a shuffle?" << abort(FatalError); } } forAll(constructFaceMap, proci) { inplaceRenumberWithFlip ( map().reverseFaceMap(), false, true, constructFaceMap[proci] ); } return map; } Foam::autoPtr Foam::fvMeshDistribute::mergeSharedPoints ( const labelList& pointToGlobalMaster, labelListList& constructPointMap ) { // Find out which sets of points get merged and create a map from // mesh point to unique point. label nShared = 0; forAll(pointToGlobalMaster, pointi) { if (pointToGlobalMaster[pointi] != -1) { nShared++; } } Map