From c59b6db3c447d4509a8a9c2c07c597207d8398cf Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 3 Oct 2022 16:54:58 +0100
Subject: [PATCH] ENH: viewFactorsGen: re-enable 2D. See #2560

This reverts the v2206 behaviour so does not include
the edge-integration (2LI) optimisation - it uses
the exact same code as v2206.
---
 .../viewFactorsGen/viewFactorsGen.C           | 56 ++++++++++++++++++-
 .../singleCellFvMesh/singleCellFvMesh.C       | 31 ++++++++--
 .../singleCellFvMesh/singleCellFvMesh.H       | 15 +++--
 3 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/applications/utilities/preProcessing/viewFactorsGen/viewFactorsGen.C b/applications/utilities/preProcessing/viewFactorsGen/viewFactorsGen.C
index 748a73e711b..3f9d82d8080 100644
--- a/applications/utilities/preProcessing/viewFactorsGen/viewFactorsGen.C
+++ b/applications/utilities/preProcessing/viewFactorsGen/viewFactorsGen.C
@@ -1091,10 +1091,62 @@ int main(int argc, char *argv[])
             }
         }
     }
+    else if (mesh.nSolutionD() == 2)
+    {
+        const boundBox& box = mesh.bounds();
+        const Vector<label>& dirs = mesh.geometricD();
+        vector emptyDir = Zero;
+        forAll(dirs, i)
+        {
+            if (dirs[i] == -1)
+            {
+                emptyDir[i] = 1.0;
+            }
+        }
+
+        scalar wideBy2 = (box.span() & emptyDir)*2.0;
+
+        forAll(localCoarseSf, coarseFaceI)
+        {
+            const vector& Ai = localCoarseSf[coarseFaceI];
+            const vector& Ci = localCoarseCf[coarseFaceI];
+            vector Ain = Ai/mag(Ai);
+            vector R1i = Ci + (mag(Ai)/wideBy2)*(Ain ^ emptyDir);
+            vector R2i = Ci - (mag(Ai)/wideBy2)*(Ain ^ emptyDir) ;
+
+            const label fromPatchId = compactPatchId[coarseFaceI];
+            patchArea[fromPatchId] += mag(Ai);
+
+            const labelList& visCoarseFaces = visibleFaceFaces[coarseFaceI];
+            forAll(visCoarseFaces, visCoarseFaceI)
+            {
+                F2LI[coarseFaceI].setSize(visCoarseFaces.size());
+                label compactJ = visCoarseFaces[visCoarseFaceI];
+                const vector& Aj = compactCoarseSf[compactJ];
+                const vector& Cj = compactCoarseCf[compactJ];
+
+                const label toPatchId = compactPatchId[compactJ];
+
+                vector Ajn = Aj/mag(Aj);
+                vector R1j = Cj + (mag(Aj)/wideBy2)*(Ajn ^ emptyDir);
+                vector R2j = Cj - (mag(Aj)/wideBy2)*(Ajn ^ emptyDir);
+
+                scalar d1 = mag(R1i - R2j);
+                scalar d2 = mag(R2i - R1j);
+                scalar s1 = mag(R1i - R1j);
+                scalar s2 = mag(R2i - R2j);
+
+                scalar Fij = mag((d1 + d2) - (s1 + s2))/(4.0*mag(Ai)/wideBy2);
+
+                F2LI[coarseFaceI][visCoarseFaceI] = Fij;
+                sumViewFactorPatch[fromPatchId][toPatchId] += Fij*mag(Ai);
+            }
+        }
+    }
     else
     {
-         FatalErrorInFunction
-            << " View factors are not available in 2D "
+        FatalErrorInFunction
+            << " View factors are not available in 1D "
             << exit(FatalError);
     }
 
diff --git a/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.C b/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.C
index 05a74ef6bd8..66fe8239a99 100644
--- a/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.C
+++ b/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.C
@@ -53,6 +53,15 @@ void Foam::singleCellFvMesh::agglomerateMesh
         const polyPatch& pp = oldPatches[patchi];
         if (pp.size() > 0)
         {
+            if (agglom[patchi].size() != pp.size())
+            {
+                FatalErrorInFunction
+                    << "agglomeration on patch " << patchi
+                    << " (size " << pp.size()
+                    << ") is of size " << agglom[patchi].size()
+                    << exit(FatalError);
+            }
+
             nAgglom[patchi] = max(agglom[patchi])+1;
 
             forAll(pp, i)
@@ -229,6 +238,7 @@ void Foam::singleCellFvMesh::agglomerateMesh
         patchSizes[patchi] = coarseI-patchStarts[patchi];
     }
 
+    //patchFaces.setSize(coarseI);
     //Pout<< "patchStarts:" << patchStarts << endl;
     //Pout<< "patchSizes:" << patchSizes << endl;
 
@@ -398,7 +408,8 @@ void Foam::singleCellFvMesh::agglomerateMesh
 Foam::singleCellFvMesh::singleCellFvMesh
 (
     const IOobject& io,
-    const fvMesh& mesh
+    const fvMesh& mesh,
+    const bool doInit
 )
 :
     fvMesh(io, Zero, false),
@@ -478,6 +489,12 @@ Foam::singleCellFvMesh::singleCellFvMesh
     }
 
     agglomerateMesh(mesh, agglom);
+
+    // initialise all (lower levels and current)
+    if (doInit)
+    {
+        fvMesh::init(true); // initialise fvMesh and underlying levels
+    }
 }
 
 
@@ -485,7 +502,8 @@ Foam::singleCellFvMesh::singleCellFvMesh
 (
     const IOobject& io,
     const fvMesh& mesh,
-    const labelListList& patchFaceAgglomeration
+    const labelListList& patchFaceAgglomeration,
+    const bool doInit
 )
 :
     fvMesh(io, Zero, false),
@@ -556,12 +574,17 @@ Foam::singleCellFvMesh::singleCellFvMesh
     )
 {
     agglomerateMesh(mesh, patchFaceAgglomeration);
+    // initialise all (lower levels and current)
+    if (doInit)
+    {
+        fvMesh::init(true); // initialise fvMesh and underlying levels
+    }
 }
 
 
-Foam::singleCellFvMesh::singleCellFvMesh(const IOobject& io)
+Foam::singleCellFvMesh::singleCellFvMesh(const IOobject& io, const bool doInit)
 :
-    fvMesh(io),
+    fvMesh(io, doInit),
     patchFaceAgglomeration_
     (
         IOobject
diff --git a/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.H b/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.H
index 7ef256804dd..cdde0dc1870 100644
--- a/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.H
+++ b/src/finiteVolume/fvMesh/singleCellFvMesh/singleCellFvMesh.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2019 OpenCFD Ltd.
+    Copyright (C) 2019,2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -155,7 +155,12 @@ public:
     // Constructors
 
         //- Construct from fvMesh and no agglomeration
-        singleCellFvMesh(const IOobject& io, const fvMesh&);
+        singleCellFvMesh
+        (
+            const IOobject& io,
+            const fvMesh&,
+            const bool doInit=true
+        );
 
         //- Construct from fvMesh and agglomeration of boundary faces.
         //  Agglomeration is per patch, per patch face index the agglomeration
@@ -164,11 +169,13 @@ public:
         (
             const IOobject& io,
             const fvMesh&,
-            const labelListList& patchFaceAgglomeration
+            const labelListList& patchFaceAgglomeration,
+            const bool doInit=true
         );
 
         //- Read from IOobject
-        singleCellFvMesh(const IOobject& io);
+        singleCellFvMesh(const IOobject& io, const bool doInit=true);
+
 
     // Member Functions
 
-- 
GitLab