From 980a5fed40d4001b9d6f08bba1e28dc9d6aa2bcd Mon Sep 17 00:00:00 2001
From: Andrew Heather <>
Date: Wed, 26 Jun 2019 11:55:22 +0100
Subject: [PATCH] COMP: Added some volatile statements for clang TODO: need to
 revisit

---
 doc/BuildIssues.md                            | 19 +++++++++++--
 .../absoluteEnthalpy/absoluteEnthalpy.H       | 21 ++++++++++++++
 .../absoluteInternalEnergy.H                  | 28 +++++++++++++++++++
 .../sensibleEnthalpy/sensibleEnthalpy.H       | 25 +++++++++++++++--
 .../specie/thermo/thermo/EtoHthermo.H         | 18 ++++++++++++
 .../specie/thermo/thermo/HtoEthermo.H         | 18 ++++++++++++
 .../specie/thermo/thermo/thermoI.H            |  5 ++++
 7 files changed, 130 insertions(+), 4 deletions(-)

diff --git a/doc/BuildIssues.md b/doc/BuildIssues.md
index a3a4f5d3754..9b8e10cadd4 100644
--- a/doc/BuildIssues.md
+++ b/doc/BuildIssues.md
@@ -1,6 +1,21 @@
 ## Known Build Issues (OpenFOAM-v1906)
 
-### Intel MPI with Gcc/Clang)
+### Thermo problems with Clang
+
+Clang builds required updates to the thermophysical libraries to prevent
+optimised builds from generating sigFpe's.  The changes are wrapped in `#ifdef`
+`__clang__` statements to not affect other compilers.
+
+The following tutorials experience known failures:
+
+- combustion/XiFoam/RAS/moriyoshiHomogeneous
+- multiphase/reactingTwoPhaseEulerFoam/laminar/bubbleColumnEvaporatingDissolving
+
+
+This will be further investigated to identify the root cause.
+
+
+### Intel MPI with Gcc/Clang
 
 Either `I_MPI_ROOT` (preferred) or `MPI_ROOT` can be used to specify
 the Intel-MPI installation directory path.
@@ -86,7 +101,7 @@ packages:
 It appears that spack will otherwise ignore any `paraview+qt` version
 and attempt to install a `paraview~qt` version instead.
 
---
+
 <!-- Links -->
 
 [page ParaView]:  http://www.paraview.org/
diff --git a/src/thermophysicalModels/specie/thermo/absoluteEnthalpy/absoluteEnthalpy.H b/src/thermophysicalModels/specie/thermo/absoluteEnthalpy/absoluteEnthalpy.H
index 38f8a656d9d..6a1bb29c01c 100644
--- a/src/thermophysicalModels/specie/thermo/absoluteEnthalpy/absoluteEnthalpy.H
+++ b/src/thermophysicalModels/specie/thermo/absoluteEnthalpy/absoluteEnthalpy.H
@@ -82,7 +82,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar cp = thermo.Cp(p, T);
+                return cp;
+                #else
                 return thermo.Cp(p, T);
+                #endif
             }
 
             //- Cp/Cp []
@@ -104,7 +111,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar ha = thermo.Ha(p, T);
+                return ha;
+                #else
                 return thermo.Ha(p, T);
+                #endif
             }
 
             //- Temperature from absolute enthalpy
@@ -117,7 +131,14 @@ public:
                 const scalar T0
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar tha = thermo.THa(h, p, T0);
+                return tha;
+                #else
                 return thermo.THa(h, p, T0);
+                #endif
             }
 };
 
diff --git a/src/thermophysicalModels/specie/thermo/absoluteInternalEnergy/absoluteInternalEnergy.H b/src/thermophysicalModels/specie/thermo/absoluteInternalEnergy/absoluteInternalEnergy.H
index d71076a835b..b720159a558 100644
--- a/src/thermophysicalModels/specie/thermo/absoluteInternalEnergy/absoluteInternalEnergy.H
+++ b/src/thermophysicalModels/specie/thermo/absoluteInternalEnergy/absoluteInternalEnergy.H
@@ -83,7 +83,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar cv = thermo.Cv(p, T);
+                return cv;
+                #else
                 return thermo.Cv(p, T);
+                #endif
             }
 
             //- Cp/Cv []
@@ -94,7 +101,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar gamma = thermo.gamma(p, T);
+                return gamma;
+                #else
                 return thermo.gamma(p, T);
+                #endif
             }
 
             // Absolute internal energy [J/kg]
@@ -105,7 +119,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar ea = thermo.Ea(p, T);
+                return ea;
+                #else
                 return thermo.Ea(p, T);
+                #endif
             }
 
             //- Temperature from absolute internal energy
@@ -118,7 +139,14 @@ public:
                 const scalar T0
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar tea = thermo.TEa(e, p, T0);
+                return tea;
+                #else
                 return thermo.TEa(e, p, T0);
+                #endif
             }
 };
 
diff --git a/src/thermophysicalModels/specie/thermo/sensibleEnthalpy/sensibleEnthalpy.H b/src/thermophysicalModels/specie/thermo/sensibleEnthalpy/sensibleEnthalpy.H
index 41bc17f2ac9..65211b74e0d 100644
--- a/src/thermophysicalModels/specie/thermo/sensibleEnthalpy/sensibleEnthalpy.H
+++ b/src/thermophysicalModels/specie/thermo/sensibleEnthalpy/sensibleEnthalpy.H
@@ -74,7 +74,7 @@ public:
                 return "h";
             }
 
-            // Heat capacity at constant pressure [J/(kg K)]
+            //- Heat capacity at constant pressure [J/(kg K)]
             scalar Cpv
             (
                 const Thermo& thermo,
@@ -82,7 +82,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar cp = thermo.Cp(p, T);
+                return cp;
+                #else
                 return thermo.Cp(p, T);
+                #endif
             }
 
             //- Cp/Cp []
@@ -96,7 +103,7 @@ public:
                 return 1;
             }
 
-            // Sensible enthalpy [J/kg]
+            //- Sensible enthalpy [J/kg]
             scalar HE
             (
                 const Thermo& thermo,
@@ -104,7 +111,14 @@ public:
                 const scalar T
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar hs = thermo.Hs(p, T);
+                return hs;
+                #else
                 return thermo.Hs(p, T);
+                #endif
             }
 
             //- Temperature from sensible enthalpy
@@ -117,7 +131,14 @@ public:
                 const scalar T0
             ) const
             {
+                #ifdef __clang__
+                // Using volatile to prevent compiler optimisations leading to
+                // a sigfpe
+                volatile const scalar ths = thermo.THs(h, p, T0);
+                return ths;
+                #else
                 return thermo.THs(h, p, T0);
+                #endif
             }
 };
 
diff --git a/src/thermophysicalModels/specie/thermo/thermo/EtoHthermo.H b/src/thermophysicalModels/specie/thermo/thermo/EtoHthermo.H
index 89d0a6d7d99..218041e0a61 100644
--- a/src/thermophysicalModels/specie/thermo/thermo/EtoHthermo.H
+++ b/src/thermophysicalModels/specie/thermo/thermo/EtoHthermo.H
@@ -4,7 +4,13 @@ inline scalar Cp
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar cv = Cv(p, T);
+    volatile const scalar cpmcv = EquationOfState::CpMCv(p, T);
+    return cv + cpmcv;
+    #else
     return Cv(p, T) + EquationOfState::CpMCv(p, T);
+    #endif
 }
 
 inline scalar Hs
@@ -13,7 +19,13 @@ inline scalar Hs
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar es = Es(p, T);
+    volatile const scalar rho = EquationOfState::rho(p, T);
+    return es + p/rho;
+    #else
     return Es(p, T) + p/EquationOfState::rho(p, T);
+    #endif
 }
 
 inline scalar Ha
@@ -22,5 +34,11 @@ inline scalar Ha
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar ea = Ea(p, T);
+    volatile const scalar rho = EquationOfState::rho(p, T);
+    return ea + p/rho;
+    #else
     return Ea(p, T) + p/EquationOfState::rho(p, T);
+    #endif
 }
diff --git a/src/thermophysicalModels/specie/thermo/thermo/HtoEthermo.H b/src/thermophysicalModels/specie/thermo/thermo/HtoEthermo.H
index 67561713fe8..7b8c34c8a97 100644
--- a/src/thermophysicalModels/specie/thermo/thermo/HtoEthermo.H
+++ b/src/thermophysicalModels/specie/thermo/thermo/HtoEthermo.H
@@ -4,7 +4,13 @@ inline scalar Cv
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar cp = Cp(p, T);
+    volatile const scalar cpmcv = EquationOfState::CpMCv(p, T);
+    return cp - cpmcv;
+    #else
     return Cp(p, T) - EquationOfState::CpMCv(p, T);
+    #endif
 }
 
 inline scalar Es
@@ -13,7 +19,13 @@ inline scalar Es
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar hs = Hs(p, T);
+    volatile const scalar rho = EquationOfState::rho(p, T);
+    return hs - p/rho;
+    #else
     return Hs(p, T) - p/EquationOfState::rho(p, T);
+    #endif
 }
 
 inline scalar Ea
@@ -22,5 +34,11 @@ inline scalar Ea
     const scalar T
 ) const
 {
+    #ifdef __clang__
+    volatile const scalar ha = Ha(p, T);
+    volatile const scalar rho = EquationOfState::rho(p, T);
+    return ha - p/rho;
+    #else
     return Ha(p, T) - p/EquationOfState::rho(p, T);
+    #endif
 }
diff --git a/src/thermophysicalModels/specie/thermo/thermo/thermoI.H b/src/thermophysicalModels/specie/thermo/thermo/thermoI.H
index f03d8b5b11a..7263432b979 100644
--- a/src/thermophysicalModels/specie/thermo/thermo/thermoI.H
+++ b/src/thermophysicalModels/specie/thermo/thermo/thermoI.H
@@ -118,7 +118,12 @@ template<class Thermo, template<class> class Type>
 inline Foam::scalar
 Foam::species::thermo<Thermo, Type>::gamma(const scalar p, const scalar T) const
 {
+    #ifdef __clang__
+    volatile const scalar Cp = this->Cp(p, T);
+    #else
     const scalar Cp = this->Cp(p, T);
+    #endif
+
     return Cp/(Cp - this->CpMCv(p, T));
 }
 
-- 
GitLab