From 14b328d99002585d2d089b916e25f85c18172b24 Mon Sep 17 00:00:00 2001
From: Kutalmis Bercin <kutalmis.bercin@esi-group.com>
Date: Fri, 25 Jun 2021 18:49:00 +0100
Subject: [PATCH] ENH: turbulenceFields: enable custom prefix for output fields

---
 .../field/turbulenceFields/turbulenceFields.C     | 15 +++++++++------
 .../field/turbulenceFields/turbulenceFields.H     | 11 +++++++++--
 .../turbulenceFields/turbulenceFieldsTemplates.C  |  6 +++---
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFields.C b/src/functionObjects/field/turbulenceFields/turbulenceFields.C
index c445161ddbf..b8bf12f9822 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFields.C
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFields.C
@@ -94,12 +94,12 @@ void Foam::functionObjects::turbulenceFields::initialise()
 {
     for (const word& f : fieldSet_)
     {
-        const word scopedName(modelName_ + ':' + f);
+        const word localName(IOobject::scopedName(prefix_, f));
 
-        if (obr_.found(scopedName))
+        if (obr_.found(localName))
         {
             WarningInFunction
-                << "Cannot store turbulence field " << scopedName
+                << "Cannot store turbulence field " << localName
                 << " since an object with that name already exists"
                 << nl << endl;
 
@@ -141,6 +141,7 @@ Foam::functionObjects::turbulenceFields::turbulenceFields
 :
     fvMeshFunctionObject(name, runTime, dict),
     initialised_(false),
+    prefix_(dict.getOrDefault<word>("prefix", "turbulenceProperties")),
     fieldSet_()
 {
     read(dict);
@@ -153,6 +154,8 @@ bool Foam::functionObjects::turbulenceFields::read(const dictionary& dict)
 {
     if (fvMeshFunctionObject::read(dict))
     {
+        dict.readIfPresent("prefix", prefix_);
+
         if (dict.found("field"))
         {
             fieldSet_.insert(dict.get<word>("field"));
@@ -168,7 +171,7 @@ bool Foam::functionObjects::turbulenceFields::read(const dictionary& dict)
             Info<< "storing fields:" << nl;
             for (const word& f : fieldSet_)
             {
-                Info<< "    " << modelName_ << ':' << f << nl;
+                Info<< "    " << IOobject::scopedName(prefix_, f) << nl;
             }
             Info<< endl;
         }
@@ -348,9 +351,9 @@ bool Foam::functionObjects::turbulenceFields::write()
 {
     for (const word& f : fieldSet_)
     {
-        const word scopedName(modelName_ + ':' + f);
+        const word localName(IOobject::scopedName(prefix_, f));
 
-        writeObject(scopedName);
+        writeObject(localName);
     }
     Info<< endl;
 
diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFields.H b/src/functionObjects/field/turbulenceFields/turbulenceFields.H
index f796e1e753e..de483bfa3c4 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFields.H
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFields.H
@@ -35,8 +35,8 @@ Description
     output during calculations, and stores/writes them on the mesh database
     for further manipulation.
 
-    Fields are stored as copies of the original, with the prefix
-    "turbulenceModel:", e.g.:
+    Fields are stored as copies of the original with a user-defined prefix
+    e.g. a prefix of \c turbulenceModel yields the following for field \c R:
 
     \verbatim
       turbulenceModel:R
@@ -79,6 +79,9 @@ Usage
             // Option-2
             field       R;
 
+        // Optional entries (runtime modifiable)
+        prefix          <word>;
+
         // Inherited entries
         ...
     }
@@ -91,6 +94,7 @@ Usage
       libs         | Library name: fieldFunctionObjects    | word | yes  | -
       fields       | Names of fields to store (see below)  | wordList | yes | -
       field        | Name of a field to store (see below)  | word | yes  | -
+      prefix  | Name of output-field prefix | word | no | turbulenceProperties
     \endtable
 
     where \c fields can include:
@@ -213,6 +217,9 @@ protected:
         //- Flag to track initialisation
         bool initialised_;
 
+        //- Name of output-field prefix
+        word prefix_;
+
         //- Fields to load
         wordHashSet fieldSet_;
 
diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C b/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
index 45df782cd4b..6d4cc906586 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
@@ -39,9 +39,9 @@ void Foam::functionObjects::turbulenceFields::processField
 {
     typedef GeometricField<Type, fvPatchField, volMesh> FieldType;
 
-    const word scopedName(modelName_ + ':' + fieldName);
+    const word localName(IOobject::scopedName(prefix_, fieldName));
 
-    FieldType* fldPtr = obr_.getObjectPtr<FieldType>(scopedName);
+    FieldType* fldPtr = obr_.getObjectPtr<FieldType>(localName);
 
     if (fldPtr)
     {
@@ -55,7 +55,7 @@ void Foam::functionObjects::turbulenceFields::processField
             (
                 IOobject
                 (
-                    scopedName,
+                    localName,
                     obr_.time().timeName(),
                     obr_,
                     IOobject::READ_IF_PRESENT,
-- 
GitLab