From b073c0a10485c0ba2596c6ddbcbf73388a176c1a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Thu, 15 Dec 2016 09:46:00 +0100
Subject: [PATCH] ENH: postOperation for surfaceFieldValue

- currently only 'none' or 'sqrt', which can be useful in combination
  with integrate or averaging functions.
---
 .../surfaceFieldValue/surfaceFieldValue.C     | 29 +++++++++++++++++++
 .../surfaceFieldValue/surfaceFieldValue.H     | 19 ++++++++++--
 .../surfaceFieldValueTemplates.C              | 20 +++++++++++--
 3 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
index baa86b3157b..5603e42f374 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
@@ -85,6 +85,18 @@ const char* Foam::NamedEnum
     "areaNormalIntegrate"
 };
 
+template<>
+const char* Foam::NamedEnum
+<
+    Foam::functionObjects::fieldValues::surfaceFieldValue::postOperationType,
+    2
+>::names[] =
+{
+    "none",
+    "sqrt"
+};
+
+
 const Foam::NamedEnum
 <
     Foam::functionObjects::fieldValues::surfaceFieldValue::regionTypes,
@@ -97,6 +109,13 @@ const Foam::NamedEnum
     16
 > Foam::functionObjects::fieldValues::surfaceFieldValue::operationTypeNames_;
 
+const Foam::NamedEnum
+<
+    Foam::functionObjects::fieldValues::surfaceFieldValue::postOperationType,
+    2
+>
+Foam::functionObjects::fieldValues::surfaceFieldValue::postOperationTypeNames_;
+
 
 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
 
@@ -649,6 +668,11 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
     surfaceWriterPtr_(nullptr),
     regionType_(regionTypeNames_.read(dict.lookup("regionType"))),
     operation_(operationTypeNames_.read(dict.lookup("operation"))),
+    postOperation_
+    (
+        postOperationTypeNames_
+        [dict.lookupOrDefault<word>("postOperation", "none")]
+    ),
     weightFieldName_("none"),
     orientWeightField_(false),
     orientedFieldsStart_(labelMax),
@@ -674,6 +698,11 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
     surfaceWriterPtr_(nullptr),
     regionType_(regionTypeNames_.read(dict.lookup("regionType"))),
     operation_(operationTypeNames_.read(dict.lookup("operation"))),
+    postOperation_
+    (
+        postOperationTypeNames_
+        [dict.lookupOrDefault<word>("postOperation", "none")]
+    ),
     weightFieldName_("none"),
     orientWeightField_(false),
     orientedFieldsStart_(labelMax),
diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.H b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.H
index 8f39f204c4a..55e23d419fe 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.H
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.H
@@ -86,6 +86,7 @@ Usage
         regionType   | face regionType: see below  | yes         |
         name         | name of face regionType if required  | no |
         operation    | operation to perform    | yes         |
+        postOperation | post-operation to perform    | no | none |
         weightField  | name of field to apply weighting | no |
         orientedWeightField  | name of oriented field to apply weighting | no |
         scaleFactor  | scale factor            | no          | 1
@@ -223,6 +224,17 @@ public:
         static const NamedEnum<operationType, 16> operationTypeNames_;
 
 
+        //- Post-operation type enumeration
+        enum postOperationType
+        {
+            postOpNone,
+            postOpSqrt
+        };
+
+        //- Operation type names
+        static const NamedEnum<postOperationType, 2> postOperationTypeNames_;
+
+
 private:
 
     // Private Member Functions
@@ -267,6 +279,9 @@ protected:
         //- Operation to apply to values
         operationType operation_;
 
+        //- Optional post-evaluation operation
+        postOperationType postOperation_;
+
         //- Weight field name - optional
         word weightFieldName_;
 
@@ -394,11 +409,11 @@ public:
         inline fileName outputDir() const;
 
         //- Templated helper function to output field values
-        template<class Type>
+        template<class Type, class WeightType>
         bool writeValues
         (
             const word& fieldName,
-            const scalarField& weightField,
+            const Field<WeightType>& weightField,
             const bool orient
         );
 
diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
index 5c76bc055bd..387a976da06 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValueTemplates.C
@@ -286,11 +286,11 @@ Type Foam::functionObjects::fieldValues::surfaceFieldValue::processValues
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-template<class Type>
+template<class Type, class WeightType>
 bool Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
 (
     const word& fieldName,
-    const scalarField& weightField,
+    const Field<WeightType>& weightField,
     const bool orient
 )
 {
@@ -355,6 +355,22 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
 
             Type result = processValues(values, Sf, weightField);
 
+            switch (postOperation_)
+            {
+                case postOpNone:
+                    break;
+                case postOpSqrt:
+                    {
+                        // sqrt: component-wise - doesn't change the type
+                        for (direction d=0; d < pTraits<Type>::nComponents; ++d)
+                        {
+                            setComponent(result,  d)
+                                = sqrt(mag(component(result,  d)));
+                        }
+                    }
+                    break;
+            }
+
             file()<< tab << result;
 
             Log << "    " << operationTypeNames_[operation_]
-- 
GitLab