From a16c4ae920c6b554963338e3d705a0303dd06aa7 Mon Sep 17 00:00:00 2001
From: Sergio Ferraris <s.ferraris@opencfd.co.uk>
Date: Mon, 14 Dec 2020 14:58:57 +0000
Subject: [PATCH] ENH: Exposing specieComposition from ReactingMixture

The FO BilgerMixtureFraction needs access to specieComposition which is
stored in ReactingMixture. A virtual mechanism was added to
basicSpecieMixture to access specieComposition form rho and psi
reationThermos.

ptr was changed to autoPtr to avoid memory leaks (Kutalmis Bercin)
---
 .../TDACChemistryModel/TDACChemistryModel.C       |  8 +++++---
 .../reactionThermo/Make/files                     |  1 +
 .../reactionThermo/Make/options                   |  1 +
 .../basicSpecieMixture/basicSpecieMixture.H       | 14 ++++++++++++++
 .../mixtures/reactingMixture/reactingMixture.H    |  9 +++++++--
 .../psiReactionThermo/psiReactionThermo.H         | 15 ++++++++++++++-
 .../rhoReactionThermo/rhoReactionThermo.H         | 15 ++++++++++++++-
 7 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/TDACChemistryModel.C b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/TDACChemistryModel.C
index 187e3009324..c4d502ebe07 100644
--- a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/TDACChemistryModel.C
+++ b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/TDACChemistryModel.C
@@ -75,13 +75,15 @@ Foam::TDACChemistryModel<ReactionThermo, ThermoType>::TDACChemistryModel
     // Store the species composition according to the species index
     speciesTable speciesTab = composition.species();
 
-    const HashTable<List<specieElement>>& specComp =
+    autoPtr<HashTable<List<specieElement>>> specCompPtr
+    (
         dynamicCast<const reactingMixture<ThermoType>&>(this->thermo())
-       .specieComposition();
+       .specieComposition()
+    );
 
     forAll(specieComp_, i)
     {
-        specieComp_[i] = specComp[this->Y()[i].member()];
+        specieComp_[i] = (specCompPtr.ref())[this->Y()[i].member()];
     }
 
     mechRed_ = chemistryReductionMethod<ReactionThermo, ThermoType>::New
diff --git a/src/thermophysicalModels/reactionThermo/Make/files b/src/thermophysicalModels/reactionThermo/Make/files
index 8870103dfe0..e31e2076ffe 100644
--- a/src/thermophysicalModels/reactionThermo/Make/files
+++ b/src/thermophysicalModels/reactionThermo/Make/files
@@ -21,4 +21,5 @@ derivedFvPatchFields/mixedUnburntEnthalpy/mixedUnburntEnthalpyFvPatchScalarField
 
 functionObjects/moleFractions/moleFractionsFunctionObjects.C
 
+
 LIB = $(FOAM_LIBBIN)/libreactionThermophysicalModels
diff --git a/src/thermophysicalModels/reactionThermo/Make/options b/src/thermophysicalModels/reactionThermo/Make/options
index 26ee649fb42..20754771fa2 100644
--- a/src/thermophysicalModels/reactionThermo/Make/options
+++ b/src/thermophysicalModels/reactionThermo/Make/options
@@ -4,6 +4,7 @@ EXE_INC = \
     -I$(LIB_SRC)/thermophysicalModels/basic/lnInclude \
     -I$(LIB_SRC)/thermophysicalModels/specie/lnInclude \
     -I$(LIB_SRC)/thermophysicalModels/solidSpecie/lnInclude \
+    -I$(LIB_SRC)/functionObjects/field/lnInclude
 
 LIB_LIBS = \
     -lfiniteVolume \
diff --git a/src/thermophysicalModels/reactionThermo/mixtures/basicSpecieMixture/basicSpecieMixture.H b/src/thermophysicalModels/reactionThermo/mixtures/basicSpecieMixture/basicSpecieMixture.H
index d7db29cc424..522c4be19e9 100644
--- a/src/thermophysicalModels/reactionThermo/mixtures/basicSpecieMixture/basicSpecieMixture.H
+++ b/src/thermophysicalModels/reactionThermo/mixtures/basicSpecieMixture/basicSpecieMixture.H
@@ -6,6 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2014-2017 OpenFOAM Foundation
+    Copyright (C) 2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -42,12 +43,15 @@ SourceFiles
 #define basicSpecieMixture_H
 
 #include "basicMultiComponentMixture.H"
+#include "specieElement.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
 {
 
+typedef HashTable<List<specieElement>> speciesCompositionTable;
+
 /*---------------------------------------------------------------------------*\
                  Class basicSpecieMixture Declaration
 \*---------------------------------------------------------------------------*/
@@ -202,6 +206,16 @@ public:
                 const scalar p,
                 const scalar T
             ) const = 0;
+
+            //- Species composition
+            virtual autoPtr<speciesCompositionTable> specieComposition() const
+            {
+                return
+                    autoPtr<speciesCompositionTable>
+                    (
+                        new speciesCompositionTable()
+                    );
+            }
 };
 
 
diff --git a/src/thermophysicalModels/reactionThermo/mixtures/reactingMixture/reactingMixture.H b/src/thermophysicalModels/reactionThermo/mixtures/reactingMixture/reactingMixture.H
index 08763c8a2a5..bb33b6cfbf4 100644
--- a/src/thermophysicalModels/reactionThermo/mixtures/reactingMixture/reactingMixture.H
+++ b/src/thermophysicalModels/reactionThermo/mixtures/reactingMixture/reactingMixture.H
@@ -6,6 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
+    Copyright (C) 2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -115,9 +116,13 @@ public:
         using PtrList<Reaction<ThermoType>>::operator[];
 
         //- Table of species composition
-        const speciesCompositionTable& specieComposition() const
+        virtual autoPtr<speciesCompositionTable> specieComposition() const
         {
-            return speciesComposition_;
+            return
+                autoPtr<speciesCompositionTable>
+                (
+                    new speciesCompositionTable(speciesComposition_)
+                );
         }
 };
 
diff --git a/src/thermophysicalModels/reactionThermo/psiReactionThermo/psiReactionThermo.H b/src/thermophysicalModels/reactionThermo/psiReactionThermo/psiReactionThermo.H
index fe1035f45a9..080e26315e1 100644
--- a/src/thermophysicalModels/reactionThermo/psiReactionThermo/psiReactionThermo.H
+++ b/src/thermophysicalModels/reactionThermo/psiReactionThermo/psiReactionThermo.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2015 OpenFOAM Foundation
-    Copyright (C) 2017 OpenCFD Ltd.
+    Copyright (C) 2017-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -45,12 +45,15 @@ SourceFiles
 #include "basicSpecieMixture.H"
 #include "autoPtr.H"
 #include "runTimeSelectionTables.H"
+#include "specieElement.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
 {
 
+typedef HashTable<List<specieElement>> speciesCompositionTable;
+
 /*---------------------------------------------------------------------------*\
                      Class psiReactionThermo Declaration
 \*---------------------------------------------------------------------------*/
@@ -137,6 +140,16 @@ public:
 
         //- Return the composition of the multi-component mixture
         virtual const basicSpecieMixture& composition() const = 0;
+
+        //- Table of species composition
+        autoPtr<speciesCompositionTable> specieComposition() const
+        {
+            return
+                autoPtr<speciesCompositionTable>
+                (
+                    composition().specieComposition()
+                );
+        }
 };
 
 
diff --git a/src/thermophysicalModels/reactionThermo/rhoReactionThermo/rhoReactionThermo.H b/src/thermophysicalModels/reactionThermo/rhoReactionThermo/rhoReactionThermo.H
index e28274331a8..72ce0177b6a 100644
--- a/src/thermophysicalModels/reactionThermo/rhoReactionThermo/rhoReactionThermo.H
+++ b/src/thermophysicalModels/reactionThermo/rhoReactionThermo/rhoReactionThermo.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2015 OpenFOAM Foundation
-    Copyright (C) 2017 OpenCFD Ltd.
+    Copyright (C) 2017-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -45,12 +45,15 @@ SourceFiles
 #include "basicSpecieMixture.H"
 #include "autoPtr.H"
 #include "runTimeSelectionTables.H"
+#include "specieElement.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
 {
 
+typedef HashTable<List<specieElement>> speciesCompositionTable;
+
 /*---------------------------------------------------------------------------*\
                      Class rhoReactionThermo Declaration
 \*---------------------------------------------------------------------------*/
@@ -137,6 +140,16 @@ public:
 
         //- Return the composition of the multi-component mixture
         virtual const basicSpecieMixture& composition() const = 0;
+
+        //- Table of species composition
+        autoPtr<speciesCompositionTable> specieComposition() const
+        {
+            return
+                autoPtr<speciesCompositionTable>
+                (
+                    composition().specieComposition()
+                );
+        }
 };
 
 
-- 
GitLab