From 3657b84eb4a90e5de3aa3627aa7b19c3c54b0342 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 23 Oct 2017 10:15:50 +0200
Subject: [PATCH] ENH: use FixedList for dimensionSet storage

- makes it easier to transmit or stream as a list of scalars
---
 src/OpenFOAM/dimensionSet/dimensionSet.C   | 60 +++++++++---------
 src/OpenFOAM/dimensionSet/dimensionSet.H   | 71 +++++++++++-----------
 src/OpenFOAM/dimensionSet/dimensionSetIO.C | 12 ++--
 3 files changed, 69 insertions(+), 74 deletions(-)

diff --git a/src/OpenFOAM/dimensionSet/dimensionSet.C b/src/OpenFOAM/dimensionSet/dimensionSet.C
index 269d25282f..b9c9be7b39 100644
--- a/src/OpenFOAM/dimensionSet/dimensionSet.C
+++ b/src/OpenFOAM/dimensionSet/dimensionSet.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -59,23 +59,10 @@ Foam::dimensionSet::dimensionSet
 }
 
 
-Foam::dimensionSet::dimensionSet
-(
-    const scalar mass,
-    const scalar length,
-    const scalar time,
-    const scalar temperature,
-    const scalar moles
-)
-{
-    exponents_[MASS] = mass;
-    exponents_[LENGTH] = length;
-    exponents_[TIME] = time;
-    exponents_[TEMPERATURE] = temperature;
-    exponents_[MOLES] = moles;
-    exponents_[CURRENT] = 0;
-    exponents_[LUMINOUS_INTENSITY] = 0;
-}
+Foam::dimensionSet::dimensionSet(const FixedList<scalar,7>& dims)
+:
+    exponents_(dims)
+{}
 
 
 Foam::dimensionSet::dimensionSet(const dimensionSet& ds)
@@ -88,13 +75,13 @@ Foam::dimensionSet::dimensionSet(const dimensionSet& ds)
 
 bool Foam::dimensionSet::dimensionless() const
 {
-    for (int Dimension=0; Dimension<nDimensions; ++Dimension)
+    for (int d=0; d<nDimensions; ++d)
     {
-        // ie, mag(exponents_[Dimension]) > smallExponent
+        // ie, mag(exponents_[d]) > smallExponent
         if
         (
-            exponents_[Dimension] > smallExponent
-         || exponents_[Dimension] < -smallExponent
+            exponents_[d] > smallExponent
+         || exponents_[d] < -smallExponent
         )
         {
             return false;
@@ -105,12 +92,21 @@ bool Foam::dimensionSet::dimensionless() const
 }
 
 
+const Foam::FixedList<Foam::scalar,7>& Foam::dimensionSet::values() const
+{
+    return exponents_;
+}
+
+
+Foam::FixedList<Foam::scalar,7>& Foam::dimensionSet::values()
+{
+    return exponents_;
+}
+
+
 void Foam::dimensionSet::reset(const dimensionSet& ds)
 {
-    for (int Dimension=0; Dimension<nDimensions; ++Dimension)
-    {
-        exponents_[Dimension] = ds.exponents_[Dimension];
-    }
+    exponents_ = ds.exponents_;
 }
 
 
@@ -142,11 +138,11 @@ Foam::scalar& Foam::dimensionSet::operator[](const label type)
 
 bool Foam::dimensionSet::operator==(const dimensionSet& ds) const
 {
-    for (int Dimension=0; Dimension < nDimensions; ++Dimension)
+    for (int d=0; d<nDimensions; ++d)
     {
         if
         (
-            mag(exponents_[Dimension] - ds.exponents_[Dimension])
+            mag(exponents_[d] - ds.exponents_[d])
           > smallExponent
         )
         {
@@ -536,9 +532,9 @@ Foam::dimensionSet Foam::operator*
 {
     dimensionSet dimProduct(ds1);
 
-    for (int Dimension=0; Dimension<dimensionSet::nDimensions; Dimension++)
+    for (int d=0; d<dimensionSet::nDimensions; ++d)
     {
-        dimProduct.exponents_[Dimension] += ds2.exponents_[Dimension];
+        dimProduct.exponents_[d] += ds2.exponents_[d];
     }
 
     return dimProduct;
@@ -553,9 +549,9 @@ Foam::dimensionSet Foam::operator/
 {
     dimensionSet dimQuotient(ds1);
 
-    for (int Dimension=0; Dimension<dimensionSet::nDimensions; Dimension++)
+    for (int d=0; d<dimensionSet::nDimensions; ++d)
     {
-        dimQuotient.exponents_[Dimension] -= ds2.exponents_[Dimension];
+        dimQuotient.exponents_[d] -= ds2.exponents_[d];
     }
 
     return dimQuotient;
diff --git a/src/OpenFOAM/dimensionSet/dimensionSet.H b/src/OpenFOAM/dimensionSet/dimensionSet.H
index 94a1608c9a..ca18178fee 100644
--- a/src/OpenFOAM/dimensionSet/dimensionSet.H
+++ b/src/OpenFOAM/dimensionSet/dimensionSet.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -120,26 +120,29 @@ Ostream& operator<<(Ostream&, const dimensionSet&);
 
 class dimensionSet
 {
-
 public:
 
+    //- The array of dimension exponents
+    typedef FixedList<scalar,7> list_type;
+
+
     // Member constants
 
         enum
         {
-            nDimensions = 7    // Number of dimensions in SI is 7
+            nDimensions = 7     //!< 7 base dimensions
         };
 
         //- Define an enumeration for the names of the dimension exponents
         enum dimensionType
         {
-            MASS,               // kilogram   kg
-            LENGTH,             // metre      m
-            TIME,               // second     s
-            TEMPERATURE,        // Kelvin     K
-            MOLES,              // mole       mol
-            CURRENT,            // Ampere     A
-            LUMINOUS_INTENSITY  // Candela    Cd
+            MASS,               //!< kilogram   kg
+            LENGTH,             //!< metre      m
+            TIME,               //!< second     s
+            TEMPERATURE,        //!< Kelvin     K
+            MOLES,              //!< mole       mol
+            CURRENT,            //!< Ampere     A
+            LUMINOUS_INTENSITY  //!< Candela    Cd
         };
 
 
@@ -150,6 +153,12 @@ public:
 
 private:
 
+    // Private data
+
+        //- The array of dimension exponents
+        list_type exponents_;
+
+
     // Private classes
 
         class tokeniser
@@ -167,17 +176,17 @@ private:
 
             // Private Member Functions
 
-                void push(const token&);
+                void push(const token& t);
 
                 token pop();
 
-                void unpop(const token&);
+                void unpop(const token& t);
 
         public:
 
             // Constructors
 
-                 tokeniser(Istream&);
+                 tokeniser(Istream& is);
 
 
             // Member Functions
@@ -212,13 +221,6 @@ private:
             const HashTable<dimensionedScalar>&
         ) const;
 
-
-    // private data
-
-        // dimensionSet stored as an array of dimension exponents
-        scalar exponents_[nDimensions];
-
-
 public:
 
     // Declare name of the class and its debug switch
@@ -227,8 +229,7 @@ public:
 
     // Constructors
 
-        //- Construct given individual dimension exponents for all
-        //  seven dimensions
+        //- Construct from exponents for the first five or all seven dimensions
         dimensionSet
         (
             const scalar mass,
@@ -236,20 +237,12 @@ public:
             const scalar time,
             const scalar temperature,
             const scalar moles,
-            const scalar current,
-            const scalar luminousIntensity
+            const scalar current = 0,
+            const scalar luminousIntensity = 0
         );
 
-        //- Construct given individual dimension exponents for first
-        //  five dimensions
-        dimensionSet
-        (
-            const scalar mass,
-            const scalar length,
-            const scalar time,
-            const scalar temperature,
-            const scalar moles
-        );
+        //- Construct from exponents for all seven dimensions
+        dimensionSet(const FixedList<scalar,7>& dimensions);
 
         //- Copy constructor
         dimensionSet(const dimensionSet& ds);
@@ -261,7 +254,7 @@ public:
         }
 
         //- Construct from Istream
-        dimensionSet(Istream&);
+        dimensionSet(Istream& is);
 
 
     // Member functions
@@ -269,7 +262,13 @@ public:
         //- Return true if it is dimensionless
         bool dimensionless() const;
 
-        void reset(const dimensionSet&);
+        //- Return const access to the exponents as a list
+        const FixedList<scalar,7>& values() const;
+
+        //- Return non-const access to the exponents as a list
+        FixedList<scalar,7>& values();
+
+        void reset(const dimensionSet& ds);
 
 
     // I/O
diff --git a/src/OpenFOAM/dimensionSet/dimensionSetIO.C b/src/OpenFOAM/dimensionSet/dimensionSetIO.C
index 9fcfd9ee20..076c710410 100644
--- a/src/OpenFOAM/dimensionSet/dimensionSetIO.C
+++ b/src/OpenFOAM/dimensionSet/dimensionSetIO.C
@@ -49,7 +49,7 @@ Foam::dimensionSet::tokeniser::tokeniser(Istream& is)
 
 void Foam::dimensionSet::tokeniser::push(const token& t)
 {
-    label end = (start_+size_)%tokens_.size();
+    const label end = (start_+size_)%tokens_.size();
     tokens_[end] = t;
     if (size_ == tokens_.size())
     {
@@ -444,9 +444,9 @@ Foam::Istream& Foam::dimensionSet::read
     {
         // Read first five dimensions
         exponents_[dimensionSet::MASS] = nextToken.number();
-        for (int Dimension=1; Dimension<dimensionSet::CURRENT; Dimension++)
+        for (int d=1; d<dimensionSet::CURRENT; ++d)
         {
-            is >> exponents_[Dimension];
+            is >> exponents_[d];
         }
 
         // Read next token
@@ -681,11 +681,11 @@ Foam::Ostream& Foam::dimensionSet::write
     }
     else
     {
-        for (int d=0; d<dimensionSet::nDimensions-1; d++)
+        for (int d=0; d<dimensionSet::nDimensions; ++d)
         {
-            os << exponents_[d] << token::SPACE;
+            if (d) os << token::SPACE;
+            os << exponents_[d];
         }
-        os << exponents_[dimensionSet::nDimensions-1];
     }
 
     os  << token::END_SQR;
-- 
GitLab