PrecisionAdaptor.H 6.43 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
    Copyright (C) 2019 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Class
    Foam::PrecisionAdaptor

Description
    Conversion adaptor for Field that either wraps as a tmp reference
    or creates the necessary tmp and copies the values on construction
    and destruction.
    This provides automatic conversion between (scalar) types for use
    with linear solvers able to run mixed precision.

\*---------------------------------------------------------------------------*/

#ifndef PrecisionAdaptor_H
#define PrecisionAdaptor_H

41
#include "tmpNrc.H"
42 43 44 45 46 47 48 49
#include "Field.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

//- A const Field wrapper with possible data conversion
50
template<class Type, class InputType, template<class> class Container = Field>
51 52
class ConstPrecisionAdaptor
:
53
    public tmpNrc<Container<Type>>
54
{
55 56 57
    // Private Member Functions

        //- Copy in field
58
        void copyInput(const Container<InputType>& input)
59 60 61
        {
            this->clear();

62
            Container<Type>* p = new Container<Type>(input.size());
63 64 65 66 67
            this->reset(p);
            std::copy(input.cbegin(), input.cend(), p->begin());
        }

        //- Construct from tmp Field, copy/move as required
68
        void moveInput(tmp<Container<InputType>>& input)
69 70 71
        {
            if (std::is_same<Type, InputType>::value)
            {
72
                auto& tinput = reinterpret_cast<tmp<Container<Type>>&>(input);
73 74 75

                if (tinput.isTmp())
                {
76 77
                    // Reset to tmp
                    this->reset(tinput.ptr());
78 79 80 81 82 83 84 85 86 87 88 89 90 91
                }
                else
                {
                    this->cref(tinput.cref());
                }
            }
            else
            {
                this->copyInput(input());
            }
            input.clear();
        }


92 93
public:

94
    //- The adapted field type
95
    typedef Container<Type> FieldType;
96 97


98 99
    // Constructors

100 101
        //- Construct from Container<InputType>, copying on input as required
        ConstPrecisionAdaptor(const Container<InputType>& input)
102
        :
103
            tmpNrc<Container<Type>>()
104 105 106
        {
            if (std::is_same<Type, InputType>::value)
            {
107
                this->cref(reinterpret_cast<const FieldType&>(input));
108 109 110
            }
            else
            {
111
                this->copyInput(input);
112 113 114 115
            }
        }


116 117
        //- Construct from tmp Container, copy/move as required
        ConstPrecisionAdaptor(tmp<Container<InputType>>&& input)
118
        :
119
            tmpNrc<Container<Type>>()
120 121 122 123 124
        {
            this->moveInput(input);
        }


125 126
        //- Construct from tmp Container, copy/move as required
        ConstPrecisionAdaptor(const tmp<Container<InputType>>& input)
127
        :
128
            tmpNrc<Container<Type>>()
129
        {
130
            this->moveInput(const_cast<tmp<Container<InputType>>&>(input));
131 132 133
        }


134 135 136
    // Member Functions

        //- Return the field
137
        static const Container<Type>& get
138
        (
139 140
            const Container<InputType>& input,
            Container<Type>& dst
141 142 143 144
        )
        {
            if (std::is_same<Type, InputType>::value)
            {
145
                return reinterpret_cast<const FieldType&>(input);
146 147 148 149 150 151 152 153 154 155 156 157
            }
            else
            {
                dst.resize(input.size());
                std::copy(input.cbegin(), input.cend(), dst.begin());
                return dst;
            }
        }
};


//- A Field wrapper with possible data conversion
158
template<class Type, class InputType, template<class> class Container = Field>
159 160
class PrecisionAdaptor
:
161
    public tmpNrc<Container<Type>>
162 163 164 165
{
    // Private Data

        //- Reference to underlying field
166
        Container<InputType>& ref_;
167 168


169 170 171
    // Private Member Functions

        //- Copy in field
172
        void copyInput(const Container<InputType>& input, const bool copy)
173
        {
174
            Container<Type>* p = new Container<Type>(input.size());
175
            this->reset(p);
176 177 178 179
            if (copy)
            {
                std::copy(input.cbegin(), input.cend(), p->begin());
            }
180 181 182
        }


183 184
public:

185
    //- The adapted field type
186
    typedef Container<Type> FieldType;
187 188


189 190
    // Constructors

191 192
        //- Construct from Container<InputType>, copying on input if required
        PrecisionAdaptor(Container<InputType>& input, const bool copy = true)
193
        :
194
            tmpNrc<Container<Type>>(),
195 196 197 198
            ref_(input)
        {
            if (std::is_same<Type, InputType>::value)
            {
199
                this->cref(reinterpret_cast<const FieldType&>(input));
200 201 202
            }
            else
            {
203
                this->copyInput(input, copy);
204 205 206 207 208 209 210 211 212
            }
        }


    //- Destructor, copying on destroy
    ~PrecisionAdaptor()
    {
        if (this->isTmp())
        {
213
            const FieldType& store = this->cref();
214 215 216 217
            ref_.resize(store.size());
            std::copy(store.cbegin(), store.cend(), ref_.begin());
        }
    }
218 219 220 221 222 223 224 225 226


    // Member Functions

        //- Allow modification without const-ref check
        FieldType& ref()
        {
            return this->constCast();
        }
227 228 229 230 231 232 233 234 235 236 237 238 239
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //