View Issue Details

IDProjectCategoryView StatusLast Update
0003370OpenFOAMPatchpublic2019-10-14 09:33
Reportertniemi Assigned Towill  
PrioritylowSeverityminorReproducibilitysometimes
Status resolvedResolutionfixed 
Fixed in Versiondev 
Summary0003370: fieldValues: guard against division by zero
DescriptionI have attached a patch which adds protection against division by zero to volFieldValue and surfaceFieldValue for weighted operations. Since the recent commit which added executeAtStart, it is quite easy to hit division by zero if weighting with eg. phi or alphaRhoPhi.
TagsNo tags attached.

Activities

tniemi

2019-10-13 19:45

reporter  

fieldValues.diff (2,660 bytes)   
diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
index 1300e6e8b..38ddd5ba9 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     | Website:  https://openfoam.org
-    \\  /    A nd           | Copyright (C) 2011-2018 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2019 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -197,7 +197,7 @@ processSameTypeValues
         {
             if (weightField.size())
             {
-                result = sum(weightField*values)/sum(weightField);
+                result = sum(weightField*values)/max(sum(weightField), vSmall);
             }
             else
             {
@@ -218,7 +218,9 @@ processSameTypeValues
 
             if (weightField.size())
             {
-                result = sum(weightField*magSf*values)/sum(magSf*weightField);
+                result =
+                    sum(weightField*magSf*values)
+                   /max(sum(magSf*weightField), vSmall);
             }
             else
             {
diff --git a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C
index 2bc002309..5e3658243 100644
--- a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C
+++ b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C
@@ -104,7 +104,7 @@ Type Foam::functionObjects::fieldValues::volFieldValue::processValues
         }
         case operationType::weightedAverage:
         {
-            result = gSum(weightField*values)/gSum(weightField);
+            result = gSum(weightField*values)/max(gSum(weightField), vSmall);
             break;
         }
         case operationType::volAverage:
@@ -114,7 +114,8 @@ Type Foam::functionObjects::fieldValues::volFieldValue::processValues
         }
         case operationType::weightedVolAverage:
         {
-            result = gSum(weightField*V*values)/gSum(weightField*V);
+            result =
+                gSum(weightField*V*values)/max(gSum(weightField*V), vSmall);
             break;
         }
         case operationType::volIntegrate:
fieldValues.diff (2,660 bytes)   

will

2019-10-14 09:33

manager   ~0010842

Thanks for the patch. Resolved by https://github.com/OpenFOAM/OpenFOAM-dev/commit/8ea55e8f06d6028afa9d3b38033132606c35c09d

Issue History

Date Modified Username Field Change
2019-10-13 19:45 tniemi New Issue
2019-10-13 19:45 tniemi File Added: fieldValues.diff
2019-10-14 09:33 will Assigned To => will
2019-10-14 09:33 will Status new => resolved
2019-10-14 09:33 will Resolution open => fixed
2019-10-14 09:33 will Fixed in Version => dev
2019-10-14 09:33 will Note Added: 0010842