From d016590bbc204e1bf50e8bbd0fa3f5834c88d2b3 Mon Sep 17 00:00:00 2001 From: Andrew Heather Date: Fri, 18 Aug 2017 14:09:34 +0100 Subject: [PATCH] ENH: AMIInterpolation - updated to perform face triangulation once only; code clean-up --- .../preProcessing/mapFieldsPar/mapFieldsPar.C | 6 +- .../field/mapFields/mapFields.C | 6 +- .../AMIInterpolation/AMIInterpolation.C | 103 +++++-------- .../AMIInterpolation/AMIInterpolation.H | 36 ++--- .../AMIInterpolation/AMIInterpolationI.H | 8 +- .../AMIInterpolationParallelOps.C | 28 ++-- .../AMIMethod/AMIMethod/AMIMethod.C | 97 ++++++++---- .../AMIMethod/AMIMethod/AMIMethod.H | 37 +++-- .../AMIMethod/AMIMethod/AMIMethodI.H | 32 ++++ .../AMIMethod/AMIMethod/AMIMethodNew.C | 4 - .../AMIMethod/directAMI/directAMI.C | 17 +- .../AMIMethod/directAMI/directAMI.H | 2 - .../faceAreaWeightAMI/faceAreaWeightAMI.C | 123 ++++++++++++--- .../faceAreaWeightAMI/faceAreaWeightAMI.H | 16 +- .../AMIMethod/mapNearestAMI/mapNearestAMI.C | 19 +-- .../AMIMethod/mapNearestAMI/mapNearestAMI.H | 2 - .../partialFaceAreaWeightAMI.C | 4 - .../partialFaceAreaWeightAMI.H | 2 - .../faceAreaIntersect/faceAreaIntersect.C | 145 +++++++++++++----- .../faceAreaIntersect/faceAreaIntersect.H | 46 ++++-- .../faceAreaIntersect/faceAreaIntersectI.H | 26 +++- .../cyclicAMIPolyPatch/cyclicAMIPolyPatch.C | 18 +-- src/sampling/meshToMesh/meshToMesh.C | 12 +- 23 files changed, 509 insertions(+), 280 deletions(-) diff --git a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C index 49a1910688..1d021b5280 100644 --- a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C +++ b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C @@ -233,10 +233,10 @@ int main(int argc, char *argv[]) meshToMesh::interpolationMethod method = meshToMesh::interpolationMethodNames_[mapMethod]; - patchMapMethod = AMIPatchToPatchInterpolation::interpolationMethodToWord - ( + patchMapMethod = AMIPatchToPatchInterpolation::interpolationMethodNames_ + [ meshToMesh::interpolationMethodAMI(method) - ); + ]; } // Optionally override diff --git a/src/functionObjects/field/mapFields/mapFields.C b/src/functionObjects/field/mapFields/mapFields.C index 80e3f454f6..78f6bd13f0 100644 --- a/src/functionObjects/field/mapFields/mapFields.C +++ b/src/functionObjects/field/mapFields/mapFields.C @@ -89,10 +89,10 @@ void Foam::functionObjects::mapFields::createInterpolation // Lookup corresponding AMI method word patchMapMethodName = - AMIPatchToPatchInterpolation::interpolationMethodToWord - ( + AMIPatchToPatchInterpolation::interpolationMethodNames_ + [ meshToMesh::interpolationMethodAMI(mapMethod) - ); + ]; // Optionally override if (dict.readIfPresent("patchMapMethod", patchMapMethodName)) diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C index 0cac4dce5f..9aad249798 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,26 +46,8 @@ Foam::AMIInterpolation::interpolationMethodNames_ }; template -Foam::word -Foam::AMIInterpolation::interpolationMethodToWord -( - const interpolationMethod& im -) -{ - return interpolationMethodNames_[im]; -} - - -template -typename Foam::AMIInterpolation::interpolationMethod -Foam::AMIInterpolation::wordTointerpolationMethod -( - const word& im -) -{ - return interpolationMethodNames_[im]; -} - +bool Foam::AMIInterpolation::cacheIntersections_ = + false; template template @@ -156,7 +138,7 @@ void Foam::AMIInterpolation::projectPointsToSurface template void Foam::AMIInterpolation::normaliseWeights ( - const scalarField& patchAreas, + const scalarList& patchAreas, const word& patchName, const labelListList& addr, scalarListList& wght, @@ -237,14 +219,14 @@ template void Foam::AMIInterpolation::agglomerate ( const autoPtr& targetMapPtr, - const scalarField& fineSrcMagSf, + const scalarList& fineSrcMagSf, const labelListList& fineSrcAddress, const scalarListList& fineSrcWeights, const labelList& sourceRestrictAddressing, const labelList& targetRestrictAddressing, - scalarField& srcMagSf, + scalarList& srcMagSf, labelListList& srcAddress, scalarListList& srcWeights, scalarField& srcWeightsSum, @@ -339,9 +321,9 @@ void Foam::AMIInterpolation::agglomerate labelList oldToNew(targetCoarseSize, -1); label newi = 0; - forAll(elems, i) + for (const label elemi : elems) { - label fineElem = elemsMap[elems[i]]; + label fineElem = elemsMap[elemi]; label coarseElem = allRestrict[fineElem]; if (oldToNew[coarseElem] == -1) { @@ -375,9 +357,8 @@ void Foam::AMIInterpolation::agglomerate // reachable elements (using the local constructMap) const labelList& elemsMap = map.constructMap()[Pstream::myProcNo()]; - forAll(elemsMap, i) + for (const label fineElem : elemsMap) { - label fineElem = elemsMap[i]; label coarseElem = allRestrict[fineElem]; tgtCompactMap[fineElem] = coarseElem; } @@ -403,12 +384,12 @@ void Foam::AMIInterpolation::agglomerate // Get the maximum target coarse size for this set of // received data. label remoteTargetCoarseSize = labelMin; - forAll(elems, i) + for (const label elemi : elems) { remoteTargetCoarseSize = max ( remoteTargetCoarseSize, - allRestrict[elems[i]] + allRestrict[elemi] ); } remoteTargetCoarseSize += 1; @@ -417,9 +398,8 @@ void Foam::AMIInterpolation::agglomerate labelList oldToNew(remoteTargetCoarseSize, -1); label newi = 0; - forAll(elems, i) + for (const label fineElem : elems) { - label fineElem = elems[i]; // fineElem now points to section from proci label coarseElem = allRestrict[fineElem]; if (oldToNew[coarseElem] == -1) @@ -563,9 +543,9 @@ void Foam::AMIInterpolation::constructFromSurface if (debug) { OFstream os("amiSrcPoints.obj"); - forAll(srcPoints, i) + for (const point& pt : srcPoints) { - meshTools::writeOBJ(os, srcPoints[i]); + meshTools::writeOBJ(os, pt); } } @@ -584,9 +564,9 @@ void Foam::AMIInterpolation::constructFromSurface if (debug) { OFstream os("amiTgtPoints.obj"); - forAll(tgtPoints, i) + for (const point& pt : tgtPoints) { - meshTools::writeOBJ(os, tgtPoints[i]); + meshTools::writeOBJ(os, pt); } } @@ -620,7 +600,7 @@ Foam::AMIInterpolation::AMIInterpolation const bool reverseTarget ) : - methodName_(interpolationMethodToWord(method)), + methodName_(interpolationMethodNames_[method]), reverseTarget_(reverseTarget), requireMatch_(requireMatch), singlePatchProc_(-999), @@ -683,7 +663,7 @@ Foam::AMIInterpolation::AMIInterpolation const bool reverseTarget ) : - methodName_(interpolationMethodToWord(method)), + methodName_(interpolationMethodNames_[method]), reverseTarget_(reverseTarget), requireMatch_(requireMatch), singlePatchProc_(-999), @@ -870,10 +850,6 @@ void Foam::AMIInterpolation::update << tgtTotalSize << " target faces" << endl; - // Calculate face areas - srcMagSf_ = patchMagSf(srcPatch, triMode_); - tgtMagSf_ = patchMagSf(tgtPatch, triMode_); - // Calculate if patches present on multiple processors singlePatchProc_ = calcDistribution(srcPatch, tgtPatch); @@ -919,13 +895,6 @@ void Foam::AMIInterpolation::update newTgtPoints ); - scalarField newTgtMagSf(newTgtPatch.size()); - forAll(newTgtPatch, facei) - { - newTgtMagSf[facei] = newTgtPatch[facei].mag(newTgtPatch.points()); - } - - // Calculate AMI interpolation autoPtr> AMIPtr ( @@ -934,8 +903,6 @@ void Foam::AMIInterpolation::update methodName_, srcPatch, newTgtPatch, - srcMagSf_, - newTgtMagSf, triMode_, reverseTarget_, requireMatch_ && (lowWeightCorrection_ < 0) @@ -950,6 +917,12 @@ void Foam::AMIInterpolation::update tgtWeights_ ); + // Note: using patch face areas calculated by the AMI method + // - TODO: should move into the calculate method + srcMagSf_.transfer(AMIPtr->srcMagSf()); + tgtMagSf_.transfer(AMIPtr->tgtMagSf()); + map.reverseDistribute(tgtPatch.size(), tgtMagSf_); + // Now // ~~~ // srcAddress_ : per srcPatch face a list of the newTgtPatch (not @@ -967,21 +940,19 @@ void Foam::AMIInterpolation::update // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - forAll(srcAddress_, i) + for (labelList& addressing : srcAddress_) { - labelList& addressing = srcAddress_[i]; - forAll(addressing, addri) + for (label& addr : addressing) { - addressing[addri] = tgtFaceIDs[addressing[addri]]; + addr = tgtFaceIDs[addr]; } } - forAll(tgtAddress_, i) + for (labelList& addressing : tgtAddress_) { - labelList& addressing = tgtAddress_[i]; - forAll(addressing, addri) + for (label& addr : addressing) { - addressing[addri] = globalSrcFaces.toGlobal(addressing[addri]); + addr = globalSrcFaces.toGlobal(addr); } } @@ -1036,8 +1007,6 @@ void Foam::AMIInterpolation::update methodName_, srcPatch, tgtPatch, - srcMagSf_, - tgtMagSf_, triMode_, reverseTarget_, requireMatch_ && (lowWeightCorrection_ < 0) @@ -1052,6 +1021,9 @@ void Foam::AMIInterpolation::update tgtWeights_ ); + srcMagSf_.transfer(AMIPtr->srcMagSf()); + tgtMagSf_.transfer(AMIPtr->tgtMagSf()); + AMIPtr->normaliseWeights(true, *this); } @@ -1623,9 +1595,8 @@ const nearest.setDistance(GREAT); label nearestFacei = -1; - forAll(addr, i) + for (const label srcFacei : addr) { - const label srcFacei = addr[i]; const face& f = srcPatch[srcFacei]; pointHit ray = f.ray(tgtPoint, n, srcPoints); @@ -1672,9 +1643,8 @@ const // Target face addresses that intersect source face srcFacei const labelList& addr = srcAddress_[srcFacei]; - forAll(addr, i) + for (const label tgtFacei : addr) { - const label tgtFacei = addr[i]; const face& f = tgtPatch[tgtFacei]; pointHit ray = f.ray(srcPoint, n, tgtPoints); @@ -1719,9 +1689,8 @@ const const labelList& addr = srcAddress[i]; const point& srcPt = srcPatch.faceCentres()[i]; - forAll(addr, j) + for (const label tgtPti : addr) { - label tgtPti = addr[j]; const point& tgtPt = tgtPatch.faceCentres()[tgtPti]; meshTools::writeOBJ(os, srcPt); diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H index f455885421..7c904abfcb 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -95,17 +95,7 @@ public: static const Enum interpolationMethodNames_; - //- Convert interpolationMethod to word representation - static word interpolationMethodToWord - ( - const interpolationMethod& method - ); - - //- Convert word to interpolationMethod - static interpolationMethod wordTointerpolationMethod - ( - const word& method - ); + static bool cacheIntersections_; //- Calculate the patch face magnitudes for the given tri-mode template @@ -142,7 +132,7 @@ private: // Source patch //- Source face areas - scalarField srcMagSf_; + scalarList srcMagSf_; //- Addresses of target faces per source face labelListList srcAddress_; @@ -157,7 +147,7 @@ private: // Target patch //- Target face areas - scalarField tgtMagSf_; + scalarList tgtMagSf_; //- Addresses of source faces per target face labelListList tgtAddress_; @@ -250,7 +240,7 @@ private: // numerical error! static void normaliseWeights ( - const scalarField& patchAreas, + const scalarList& patchAreas, const word& patchName, const labelListList& addr, scalarListList& wght, @@ -266,14 +256,14 @@ private: static void agglomerate ( const autoPtr& targetMap, - const scalarField& fineSrcMagSf, + const scalarList& fineSrcMagSf, const labelListList& fineSrcAddress, const scalarListList& fineSrcWeights, const labelList& sourceRestrictAddressing, const labelList& targetRestrictAddressing, - scalarField& srcMagSf, + scalarList& srcMagSf, labelListList& srcAddress, scalarListList& srcWeights, scalarField& srcWeightsSum, @@ -311,7 +301,7 @@ public: const faceAreaIntersect::triangulationMode& triMode, const bool requireMatch = true, const word& methodName = - interpolationMethodToWord(imFaceAreaWeight), + interpolationMethodNames_[imFaceAreaWeight], const scalar lowWeightCorrection = -1, const bool reverseTarget = false ); @@ -338,7 +328,7 @@ public: const faceAreaIntersect::triangulationMode& triMode, const bool requireMatch = true, const word& methodName = - interpolationMethodToWord(imFaceAreaWeight), + interpolationMethodNames_[imFaceAreaWeight], const scalar lowWeightCorrection = -1, const bool reverseTarget = false ); @@ -381,10 +371,10 @@ public: // Source patch //- Return const access to source patch face areas - inline const scalarField& srcMagSf() const; + inline const List& srcMagSf() const; //- Return access to source patch face areas - inline scalarField& srcMagSf(); + inline List& srcMagSf(); //- Return const access to source patch addressing inline const labelListList& srcAddress() const; @@ -415,10 +405,10 @@ public: // Target patch //- Return const access to target patch face areas - inline const scalarField& tgtMagSf() const; + inline const List& tgtMagSf() const; //- Return access to target patch face areas - inline scalarField& tgtMagSf(); + inline List& tgtMagSf(); //- Return const access to target patch addressing inline const labelListList& tgtAddress() const; diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationI.H index 72556d3737..747fd1696e 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationI.H +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationI.H @@ -49,7 +49,7 @@ applyLowWeightCorrection() const template -inline const Foam::scalarField& +inline const Foam::List& Foam::AMIInterpolation::srcMagSf() const { return srcMagSf_; @@ -57,7 +57,7 @@ Foam::AMIInterpolation::srcMagSf() const template -inline Foam::scalarField& +inline Foam::List& Foam::AMIInterpolation::srcMagSf() { return srcMagSf_; @@ -121,7 +121,7 @@ Foam::AMIInterpolation::srcMap() const template -inline const Foam::scalarField& +inline const Foam::List& Foam::AMIInterpolation::tgtMagSf() const { return tgtMagSf_; @@ -129,7 +129,7 @@ Foam::AMIInterpolation::tgtMagSf() const template -inline Foam::scalarField& +inline Foam::List& Foam::AMIInterpolation::tgtMagSf() { return tgtMagSf_; diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationParallelOps.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationParallelOps.C index d8da237463..64f803c43f 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationParallelOps.C +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolationParallelOps.C @@ -100,9 +100,9 @@ Foam::AMIInterpolation::calcOverlappingProcs { const treeBoundBoxList& bbp = procBb[proci]; - forAll(bbp, bbi) + for (const treeBoundBox& tbb: bbp) { - if (bbp[bbi].overlaps(bb)) + if (tbb.overlaps(bb)) { overlaps[proci] = true; nOverlaps++; @@ -128,7 +128,7 @@ void Foam::AMIInterpolation::distributePatches { PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking); - for (label domain = 0; domain < Pstream::nProcs(); domain++) + for (label domain = 0; domain < Pstream::nProcs(); ++domain) { const labelList& sendElems = map.subMap()[domain]; @@ -195,7 +195,7 @@ void Foam::AMIInterpolation::distributePatches } // Consume - for (label domain = 0; domain < Pstream::nProcs(); domain++) + for (label domain = 0; domain < Pstream::nProcs(); ++domain) { const labelList& recvElems = map.constructMap()[domain]; @@ -251,9 +251,8 @@ distributeAndMergePatches SubList