Skip to content
Snippets Groups Projects
rawSurfaceWriterImpl.C 6.13 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*---------------------------------------------------------------------------*\
      =========                 |
      \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
       \\    /   O peration     |
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        \\  /    A nd           | www.openfoam.com
    
         \\/     M anipulation  |
    -------------------------------------------------------------------------------
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        Copyright (C) 2011-2014 OpenFOAM Foundation
    
        Copyright (C) 2015-2021 OpenCFD Ltd.
    
    -------------------------------------------------------------------------------
    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/>.
    
    \*---------------------------------------------------------------------------*/
    
    #include "OFstream.H"
    #include "OSspecific.H"
    #include "IOmanip.H"
    
    // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
    
    namespace Foam
    {
    
        // Emit x,y,z
        static inline void writePoint(Ostream& os, const point& p)
        {
            os << p.x() << ' ' << p.y() << ' ' << p.z();
        }
    
    
        // Emit each component
        template<class Type>
        static inline void writeData(Ostream& os, const Type& val)
        {
            for (direction i=0; i < pTraits<Type>::nComponents; ++i)
            {
                os  << ' ' << component(val, i);
            }
        }
    
    
        // Write x/y/z header. Must be called first
        static inline void writeHeaderXYZ(Ostream& os)
        {
            os  << "# x y z";
        }
    
        // Write area header
        static inline void writeHeaderArea(Ostream& os)
    
            os  << "  area_x area_y area_z";
    
        template<class Type>
        static inline void writeHeader(Ostream& os, const word& fieldName)
    
            os  << "  " << fieldName;
        }
    
        template<class Type>
        void writeHeaderComponents(Ostream& os, const word& fieldName)
        {
            os  << ' ';
            for (direction i=0; i < pTraits<Type>::nComponents; ++i)
            {
                os  << ' ' << fieldName << '_' << Type::componentNames[i];
            }
    
        }
    
        template<>
        void writeHeader<vector>(Ostream& os, const word& fieldName)
        {
    
            writeHeaderComponents<vector>(os, fieldName);
    
        }
    
        template<>
        void writeHeader<sphericalTensor>(Ostream& os, const word& fieldName)
        {
    
            writeHeaderComponents<sphericalTensor>(os, fieldName);
    
        }
    
        template<>
        void writeHeader<symmTensor>(Ostream& os, const word& fieldName)
        {
    
            writeHeaderComponents<symmTensor>(os, fieldName);
    
        }
    
        template<>
        void writeHeader<tensor>(Ostream& os, const word& fieldName)
        {
    
            writeHeaderComponents<tensor>(os, fieldName);
    
        }
    
    } // End namespace Foam
    
    
    // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
    
    template<class Type>
    
    Foam::fileName Foam::surfaceWriters::rawWriter::writeTemplate
    
        const Field<Type>& localValues
    )
    
        // Field:  rootdir/<TIME>/<field>_surfaceName.raw
    
        fileName outputFile = outputPath_.path();
        if (useTimeDir() && !timeName().empty())
    
            // Splice in time-directory
            outputFile /= timeName();
    
        // Append <field>_surfaceName.raw
        outputFile /= fieldName + '_' + outputPath_.name();
        outputFile.ext("raw");
    
    
        // Output scaling for the variable, but not for integer types.
        // could also solve with clever templating
    
        const scalar varScale =
        (
            std::is_integral<Type>::value
          ? scalar(1)
          : fieldScale_.getOrDefault<scalar>(fieldName, 1)
        );
    
    
            Info<< "Writing field " << fieldName;
            if (!equal(varScale, 1))
            {
                Info<< " (scaling " << varScale << ')';
            }
            Info<< " to " << outputFile << endl;
    
        // Implicit geometry merge()
        tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
    
    
        const meshedSurf& surf = surface();
    
        if (Pstream::master() || !parallel_)
    
            const auto& values = tfield();
            const pointField& points = surf.points();
            const faceList& faces = surf.faces();
    
            const bool withFaceNormal = (writeNormal_ && !this->isPointData());
    
    
            if (!isDir(outputFile.path()))
    
                mkDir(outputFile.path());
    
            OFstream os(outputFile, streamOpt_);
    
    
            // Header
            {
                os  << "# " << fieldName;
                if (this->isPointData())
                {
                    os  << "  POINT_DATA ";
                }
                else
                {
                    os << "  FACE_DATA ";
                }
                os << values.size() << nl;
    
    
                writeHeader<Type>(os, fieldName);
    
                if (withFaceNormal)
                {
                    writeHeaderArea(os);
                }
                os << nl;
    
            }
    
    
            if (this->isPointData())
            {
                // Node values
                forAll(values, elemi)
                {
    
                    writePoint(os, points[elemi]*geometryScale_);
    
                    writeData(os, values[elemi]);
    
                // Face values
                forAll(values, elemi)
                {
    
                    const face& f = faces[elemi];
    
                    writePoint(os, f.centre(points)*geometryScale_);
    
                    writeData(os,  values[elemi]);
    
                    if (withFaceNormal)
                    {
                        os << ' ';
                        writePoint(os, f.areaNormal(points)*geometryScale_);
                    }
                    os << nl;
    
    }
    
    
    // ************************************************************************* //