From 5889f66ebcfa8c3847d7e50d49ff0d9e260b2d97 Mon Sep 17 00:00:00 2001
From: graham <g.macpherson@opencfd.co.uk>
Date: Fri, 3 Jun 2011 16:04:37 +0100
Subject: [PATCH] ENH: Prevent overflow in weight field.

---
 .../decompose/scotchDecomp/scotchDecomp.C     | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/parallel/decompose/scotchDecomp/scotchDecomp.C b/src/parallel/decompose/scotchDecomp/scotchDecomp.C
index 17ff685d49e..0851431b965 100644
--- a/src/parallel/decompose/scotchDecomp/scotchDecomp.C
+++ b/src/parallel/decompose/scotchDecomp/scotchDecomp.C
@@ -409,11 +409,32 @@ Foam::label Foam::scotchDecomp::decomposeOneProc
                 << exit(FatalError);
         }
 
+        scalar velotabSum = sum(cWeights)/minWeights;
+
+        scalar rangeScale(1.0);
+
+        if (velotabSum > scalar(INT_MAX - 1))
+        {
+            // 0.9 factor of safety to avoid floating point round-off in
+            // rangeScale tipping the subsequent sum over the integer limit.
+            rangeScale = 0.9*scalar(INT_MAX - 1)/velotabSum;
+
+            WarningIn
+            (
+                "scotchDecomp::decompose"
+                "(const pointField&, const scalarField&)"
+            )   << "Sum of weights has overflowed integer: " << velotabSum
+                << ", compressing weight scale by a factor of " << rangeScale
+                << endl;
+        }
+
         // Convert to integers.
         velotab.setSize(cWeights.size());
+
         forAll(velotab, i)
         {
-            velotab[i] = int(cWeights[i]/minWeights);
+            velotab[i] =
+                int((cWeights[i]/minWeights - 1)*rangeScale) + 1;
         }
     }
 
-- 
GitLab