ensightOutputTemplates.C 7.92 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-2020 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
-------------------------------------------------------------------------------
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 "ensightOutput.H"
#include "ensightPTraits.H"

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

template<template<typename> class FieldContainer, class Type>
34
void Foam::ensightOutput::Detail::copyComponent
35
(
36 37 38
    scalarField& res,
    const FieldContainer<Type>& input,
    const direction cmpt
39 40
)
{
41
    res.resize(input.size());
42

43
    auto iter = res.begin();
44

45
    for (const Type& val : input)
46
    {
47 48
        *iter = component(val, cmpt);
        ++iter;
49
    }
50 51
}

52

53 54 55 56 57 58 59 60 61 62 63 64
template<template<typename> class FieldContainer>
bool Foam::ensightOutput::Detail::writeCoordinates
(
    ensightGeoFile& os,
    const label partId,
    const word& partName,
    const label nPoints,
    const FieldContainer<Foam::point>& fld,
    bool parallel
)
{
    parallel = parallel && Pstream::parRun();
65

66
    const label nSlaves = (parallel ? Pstream::nProcs() : 0);
67

68 69 70 71
    // Using manual copyComponent(...) instead of fld.component() to support
    // indirect lists etc.

    scalarField send(fld.size());
72 73 74

    if (Pstream::master())
    {
75
        // Serial output, or parallel (master)
76

77 78
        os.beginPart(partId, partName);
        os.beginCoordinates(nPoints);
79

80
        for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
81
        {
82 83
            copyComponent(send, fld, cmpt);
            os.writeList(send);
84

85
            for (int slave=1; slave < nSlaves; ++slave)
86
            {
87 88 89
                IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                scalarField recv(fromSlave);
                os.writeList(recv);
90 91 92
            }
        }
    }
93
    else if (nSlaves)
94 95 96
    {
        // Parallel (slaves)

97
        for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
98
        {
99
            copyComponent(send, fld, cmpt);
100 101 102 103 104 105 106

            OPstream toMaster
            (
                Pstream::commsTypes::scheduled,
                Pstream::masterNo()
            );

107
            toMaster << send;
108 109 110 111 112 113 114
        }
    }

    return true;
}


115 116
template<template<typename> class FieldContainer, class Type>
bool Foam::ensightOutput::Detail::writeFieldComponents
117 118
(
    ensightFile& os,
119 120
    const char* key,
    const FieldContainer<Type>& fld,
121 122 123 124 125
    bool parallel
)
{
    parallel = parallel && Pstream::parRun();

126
    const label nSlaves = (parallel ? Pstream::nProcs() : 0);
127

128
    // Preliminary checks
129
    {
130 131 132 133 134 135 136 137 138
        bool hasField = !fld.empty();

        if (parallel)
        {
            reduce(hasField, orOp<bool>());
        }

        // No field
        if (!hasField) return false;
139 140 141
    }


142 143
    // Using manual copyComponent(...) instead of fld.component() to support
    // indirect lists etc.
144

145
    scalarField send(fld.size());
146 147 148

    if (Pstream::master())
    {
149
        // Serial output, or parallel (master)
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        os.writeKeyword(key);

        for (direction d=0; d < pTraits<Type>::nComponents; ++d)
        {
            const direction cmpt = ensightPTraits<Type>::componentOrder[d];

            copyComponent(send, fld, cmpt);
            os.writeList(send);

            for (label slave=1; slave < nSlaves; ++slave)
            {
                IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                scalarField recv(fromSlave);
                os.writeList(recv);
            }
        }
    }
    else if (nSlaves)
169
    {
170
        // Parallel (slaves)
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185
        for (direction d=0; d < pTraits<Type>::nComponents; ++d)
        {
            const direction cmpt = ensightPTraits<Type>::componentOrder[d];

            copyComponent(send, fld, cmpt);

            OPstream toMaster
            (
                Pstream::commsTypes::scheduled,
                Pstream::masterNo()
            );

            toMaster << send;
        }
186 187 188 189 190 191 192 193 194
    }

    return true;
}


template<class Type>
bool Foam::ensightOutput::Detail::writeFaceSubField
(
195
    ensightFile& os,
196 197 198 199 200 201 202
    const Field<Type>& fld,
    const ensightFaces& part,
    bool parallel
)
{
    parallel = parallel && Pstream::parRun();

203
    // Preliminary checks: total() contains pre-reduced information
204
    {
205 206
        // No geometry
        if (parallel ? !part.total() : !part.size()) return false;
207

208
        bool hasField = !fld.empty();
209

210 211 212 213 214 215 216 217
        if (parallel)
        {
            reduce(hasField, orOp<bool>());
        }

        // No field
        if (!hasField) return false;
    }
218 219 220 221 222 223 224


    if (Pstream::master())
    {
        os.beginPart(part.index());
    }

225
    labelList localAddr;
226 227 228

    for (int typei=0; typei < ensightFaces::nTypes; ++typei)
    {
229
        const auto etype = ensightFaces::elemType(typei);
230

231
        ensightOutput::Detail::writeFieldComponents
232 233
        (
            os,
234 235
            ensightFaces::key(etype),
            SubField<Type>(fld, part.range(etype)),
236 237 238 239 240 241 242 243 244
            parallel
        );
    }

    return true;
}


template<class Type>
245
bool Foam::ensightOutput::writeField
246
(
247
    ensightFile& os,
248
    const Field<Type>& fld,
249 250
    const ensightCells& part,
    bool parallel
251 252
)
{
253
    parallel = parallel && Pstream::parRun();
254

255
    // Preliminary checks: total() contains pre-reduced information
256
    {
257 258
        // No geometry
        if (parallel ? !part.total() : !part.size()) return false;
259

260
        bool hasField = !fld.empty();
261

262 263 264 265
        if (parallel)
        {
            reduce(hasField, orOp<bool>());
        }
266

267 268 269
        // No field
        if (!hasField) return false;
    }
270 271 272 273 274 275

    if (Pstream::master())
    {
        os.beginPart(part.index());
    }

276 277 278 279 280 281 282 283 284 285 286 287
    for (int typei=0; typei < ensightCells::nTypes; ++typei)
    {
        const auto etype = ensightCells::elemType(typei);

        ensightOutput::Detail::writeFieldComponents
        (
            os,
            ensightCells::key(etype),
            UIndirectList<Type>(fld, part.cellIds(etype)),
            parallel
        );
    }
288 289 290 291 292 293

    return true;
}


template<class Type>
294
bool Foam::ensightOutput::writeField
295 296
(
    ensightFile& os,
297 298
    const Field<Type>& fld,
    const ensightFaces& part,
299 300 301 302 303
    bool parallel
)
{
    parallel = parallel && Pstream::parRun();

304
    // Preliminary checks: total() contains pre-reduced information
305
    {
306 307
        // No geometry
        if (parallel ? !part.total() : !part.size()) return false;
308

309
        bool hasField = !fld.empty();
310

311 312 313 314 315 316 317 318
        if (parallel)
        {
            reduce(hasField, orOp<bool>());
        }

        // No field
        if (!hasField) return false;
    }
319 320 321 322 323 324 325


    if (Pstream::master())
    {
        os.beginPart(part.index());
    }

326
    for (int typei=0; typei < ensightFaces::nTypes; ++typei)
327
    {
328
        const auto etype = ensightFaces::elemType(typei);
329

330
        ensightOutput::Detail::writeFieldComponents
331 332
        (
            os,
333 334
            ensightFaces::key(etype),
            UIndirectList<Type>(fld, part.faceIds(etype)),
335 336 337 338 339 340 341 342 343
            parallel
        );
    }

    return true;
}


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