From 458cec60b180d3b52590d41054183f566dff914c Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 15 Jun 2020 15:33:39 +0200
Subject: [PATCH] ENH: support time-varying Tref for externalCoupledTemperature
 (#1729)

---
 ...oupledTemperatureMixedFvPatchScalarField.C | 45 ++++++++++---------
 ...oupledTemperatureMixedFvPatchScalarField.H | 10 +++--
 .../system/changeDictionaryDict               | 13 +++++-
 3 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.C b/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.C
index dda73211ce1..2db768229d1 100644
--- a/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.C
+++ b/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.C
@@ -89,23 +89,23 @@ externalCoupledTemperatureMixedFvPatchScalarField
     externalCoupledMixedFvPatchField<scalar>(p, iF),
     outTempType_(outputTemperatureType::WALL),
     refTempType_(refTemperatureType::CELL),
-    Tref_(Zero)
+    Tref_(nullptr)
 {}
 
 
 Foam::externalCoupledTemperatureMixedFvPatchScalarField::
 externalCoupledTemperatureMixedFvPatchScalarField
 (
-    const externalCoupledTemperatureMixedFvPatchScalarField& ptf,
+    const externalCoupledTemperatureMixedFvPatchScalarField& rhs,
     const fvPatch& p,
     const DimensionedField<scalar, volMesh>& iF,
     const fvPatchFieldMapper& mapper
 )
 :
-    externalCoupledMixedFvPatchField<scalar>(ptf, p, iF, mapper),
-    outTempType_(ptf.outTempType_),
-    refTempType_(ptf.refTempType_),
-    Tref_(ptf.Tref_)
+    externalCoupledMixedFvPatchField<scalar>(rhs, p, iF, mapper),
+    outTempType_(rhs.outTempType_),
+    refTempType_(rhs.refTempType_),
+    Tref_(rhs.Tref_.clone())
 {}
 
 
@@ -129,7 +129,7 @@ externalCoupledTemperatureMixedFvPatchScalarField
             refTemperatureType::CELL
         )
     ),
-    Tref_(Zero)
+    Tref_(nullptr)
 {
     if (dict.found("outputTemperature"))
     {
@@ -146,7 +146,7 @@ externalCoupledTemperatureMixedFvPatchScalarField
 
     if (refTempType_ == refTemperatureType::USER)
     {
-        Tref_ = dict.get<scalar>("Tref");
+        Tref_ = Function1<scalar>::New("Tref", dict);
     }
 
     if (dict.found("refValue"))
@@ -185,27 +185,27 @@ externalCoupledTemperatureMixedFvPatchScalarField
 Foam::externalCoupledTemperatureMixedFvPatchScalarField::
 externalCoupledTemperatureMixedFvPatchScalarField
 (
-    const externalCoupledTemperatureMixedFvPatchScalarField& ecmpf
+    const externalCoupledTemperatureMixedFvPatchScalarField& rhs
 )
 :
-    externalCoupledMixedFvPatchField<scalar>(ecmpf),
-    outTempType_(ecmpf.outTempType_),
-    refTempType_(ecmpf.refTempType_),
-    Tref_(ecmpf.Tref_)
+    externalCoupledMixedFvPatchField<scalar>(rhs),
+    outTempType_(rhs.outTempType_),
+    refTempType_(rhs.refTempType_),
+    Tref_(rhs.Tref_.clone())
 {}
 
 
 Foam::externalCoupledTemperatureMixedFvPatchScalarField::
 externalCoupledTemperatureMixedFvPatchScalarField
 (
-    const externalCoupledTemperatureMixedFvPatchScalarField& ecmpf,
+    const externalCoupledTemperatureMixedFvPatchScalarField& rhs,
     const DimensionedField<scalar, volMesh>& iF
 )
 :
-    externalCoupledMixedFvPatchField<scalar>(ecmpf, iF),
-    outTempType_(ecmpf.outTempType_),
-    refTempType_(ecmpf.refTempType_),
-    Tref_(ecmpf.Tref_)
+    externalCoupledMixedFvPatchField<scalar>(rhs, iF),
+    outTempType_(rhs.outTempType_),
+    refTempType_(rhs.refTempType_),
+    Tref_(rhs.Tref_.clone())
 {}
 
 
@@ -270,7 +270,10 @@ void Foam::externalCoupledTemperatureMixedFvPatchScalarField::writeData
     if (refTempType_ == refTemperatureType::USER)
     {
         // User-specified reference temperature
-        tfluid = tmp<scalarField>::New(size(), Tref_);
+        const scalar currTref =
+            Tref_->value(this->db().time().timeOutputValue());
+
+        tfluid = tmp<scalarField>::New(size(), currTref);
     }
     else
     {
@@ -344,9 +347,9 @@ void Foam::externalCoupledTemperatureMixedFvPatchScalarField::write
         refTemperatureNames[refTempType_]
     );
 
-    if (refTempType_ == refTemperatureType::USER)
+    if (Tref_)
     {
-        os.writeEntry("Tref", Tref_);
+        Tref_->writeData(os);
     }
 }
 
diff --git a/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.H b/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.H
index 1d0728a1ffc..c82d23b4dbb 100644
--- a/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.H
+++ b/src/functionObjects/field/externalCoupled/externalCoupledTemperatureMixed/externalCoupledTemperatureMixedFvPatchScalarField.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2013-2016 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -98,6 +98,9 @@ Usage
         Tref     | Reference temperature [K] for htc        | conditional |
     \endtable
 
+    The user-specified reference temperature Tref is specified as
+    a Foam::Function1 of time but spatially uniform.
+
 SeeAlso
     externalCoupledFunctionObject
     mixedFvPatchField
@@ -112,6 +115,7 @@ SourceFiles
 #define externalCoupledTemperatureMixedFvPatchScalarField_H
 
 #include "externalCoupledMixedFvPatchFields.H"
+#include "Function1.H"
 #include "Enum.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@@ -159,7 +163,7 @@ class externalCoupledTemperatureMixedFvPatchScalarField
         enum refTemperatureType refTempType_;
 
         //- User-specified reference temperature for HTC calculation
-        scalar Tref_;
+        autoPtr<Function1<scalar>> Tref_;
 
 
 public:
@@ -238,7 +242,7 @@ public:
     virtual ~externalCoupledTemperatureMixedFvPatchScalarField() = default;
 
 
-    // Member functions
+    // Member Functions
 
         //- Write header
         virtual void writeHeader(Ostream& os) const;
diff --git a/tutorials/compressible/rhoPimpleFoam/RAS/externalCoupledSquareBendLiq/system/changeDictionaryDict b/tutorials/compressible/rhoPimpleFoam/RAS/externalCoupledSquareBendLiq/system/changeDictionaryDict
index 34d7db5d7de..c657768d857 100644
--- a/tutorials/compressible/rhoPimpleFoam/RAS/externalCoupledSquareBendLiq/system/changeDictionaryDict
+++ b/tutorials/compressible/rhoPimpleFoam/RAS/externalCoupledSquareBendLiq/system/changeDictionaryDict
@@ -1,7 +1,7 @@
 /*--------------------------------*- C++ -*----------------------------------*\
 | =========                 |                                                 |
 | \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
-|  \\    /   O peration     | Version:  v1912                                 |
+|  \\    /   O peration     | Version:  v2006                                 |
 |   \\  /    A nd           | Website:  www.openfoam.com                      |
 |    \\/     M anipulation  |                                                 |
 \*---------------------------------------------------------------------------*/
@@ -29,7 +29,16 @@ T
         inletWalls
         {
             type        externalCoupledTemperature;
-            // outputTemperature   fluid; // fluid|wall
+            outputTemperature   fluid;      // (fluid|wall)
+            htcRefTemperature   user;       // (cell|user)
+
+            Tref
+            {
+                type        expression;
+
+                // arg() means time here
+                expression  #{ arg() <= 0.1 ? 300 : 320 #};
+            }
         }
     }
 }
-- 
GitLab