surfMeshSamplers.C 11.6 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2016-2019 OpenCFD Ltd.
6 7 8 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 41 42 43 44 45 46
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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 "surfMeshSamplers.H"
#include "volFields.H"
#include "dictionary.H"
#include "Time.H"
#include "IOmanip.H"
#include "volPointInterpolation.H"
#include "PatchTools.H"
#include "mapPolyMesh.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineTypeNameAndDebug(surfMeshSamplers, 0);
    addToRunTimeSelectionTable
    (
        functionObject,
        surfMeshSamplers,
        dictionary
    );
47
} // End namespace Foam
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79


bool Foam::surfMeshSamplers::verbose_ = false;


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

// //- Temporary object registry for passing around values
// const objectRegistry& tmpRegistry() const;
//
// //- Temporary object registry for passing around values
// const objectRegistry& tmpRegistry(const word& subName) const;

// const Foam::objectRegistry&
// Foam::surfMeshSamplers::tmpRegistry() const
// {
//     // Sub-registry for sampling, choose name for fewer collisions
//     return mesh_.thisDb().subRegistry
//     (
//         "$tmp$" + type() + "$" + name(),
//         true,
//         false
//     );
// }
//
//
// const Foam::objectRegistry&
// Foam::surfMeshSamplers::tmpRegistry(const word& subName) const
// {
//     return tmpRegistry().subRegistry(subName, true, false);
// }

80 81 82 83 84 85 86 87 88 89 90 91 92 93
bool Foam::surfMeshSamplers::add_rhoU(const word& derivedName)
{
    const objectRegistry& db = mesh_.thisDb();

    const bool existed = db.foundObject<volVectorField>(derivedName);

    if (existed)
    {
        return false; // Volume field already existed - not added.
    }


    // rhoU = rho * U

94
    const auto* rhoPtr = mesh_.findObject<volScalarField>("rho");
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    const volVectorField& U = mesh_.lookupObject<volVectorField>("U");

    tmp<volVectorField> tresult;

    if (rhoPtr)
    {
        const auto& rho = *rhoPtr;

        tresult = tmp<volVectorField>::New
        (
            derivedName, (rho * U)
        );
    }
    else
    {
        const dimensionedScalar rho("rho", dimDensity, rhoRef_);

        tresult = tmp<volVectorField>::New
        (
            derivedName, (rho * U)
        );
    }

    db.store(tresult.ptr());

    return !existed;
}


bool Foam::surfMeshSamplers::add_pTotal(const word& derivedName)
{
    const objectRegistry& db = mesh_.thisDb();

    const bool existed = db.foundObject<volVectorField>(derivedName);

    if (existed)
    {
        return false; // Volume field already existed - not added.
    }

    // pTotal = p + rho * U^2 / 2

137
    const auto* rhoPtr = mesh_.findObject<volScalarField>("rho");
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    const volScalarField& p = mesh_.lookupObject<volScalarField>("p");
    const volVectorField& U = mesh_.lookupObject<volVectorField>("U");

    tmp<volScalarField> tresult;

    if (rhoPtr)
    {
        const auto& rho = *rhoPtr;

        tresult = tmp<volScalarField>::New
        (
            derivedName, (p + 0.5 * rho * magSqr(U))
        );
    }
    else
    {
        const dimensionedScalar rho("rho", dimDensity, rhoRef_);

        tresult = tmp<volScalarField>::New
        (
            derivedName, (rho * (p + 0.5 * magSqr(U)))
        );
    }

    db.store(tresult.ptr());

    return !existed;
}

167 168 169 170 171 172 173 174 175 176 177

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::surfMeshSamplers::surfMeshSamplers
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
    functionObjects::regionFunctionObject(name, runTime, dict),
178
    PtrList<surfMeshSample>(),
179
    mesh_(refCast<const fvMesh>(obr_)),
180 181
    fieldSelection_(),
    derivedNames_(),
182
    sampleFaceScheme_(),
183
    rhoRef_(1.0)
184 185 186 187 188 189 190 191 192 193 194 195 196
{
    read(dict);
}


Foam::surfMeshSamplers::surfMeshSamplers
(
    const word& name,
    const objectRegistry& obr,
    const dictionary& dict
)
:
    functionObjects::regionFunctionObject(name, obr, dict),
197
    PtrList<surfMeshSample>(),
198 199
    mesh_(refCast<const fvMesh>(obr)),
    fieldSelection_(),
200
    derivedNames_(),
201
    sampleFaceScheme_(),
202
    rhoRef_(1.0)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
{
    read(dict);
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

void Foam::surfMeshSamplers::verbose(const bool verbosity)
{
    verbose_ = verbosity;
}


bool Foam::surfMeshSamplers::execute()
{
218
    if (empty())
219
    {
220 221
        return true;
    }
222

223
    const objectRegistry& db = mesh_.thisDb();
224

225 226 227 228 229 230
    // Manage derived names
    DynamicList<word> added(derivedNames_.size());
    DynamicList<word> cleanup(derivedNames_.size());

    for (const word& derivedName : derivedNames_)
    {
231 232
        // This is a fairly ugly dispatch mechanism

233
        if (derivedName == "rhoU")
234
        {
235
            if (add_rhoU(derivedName))
236 237
            {
                cleanup.append(derivedName);
238
            }
239
            added.append(derivedName);
240 241 242
        }
        else if (derivedName == "pTotal")
        {
243
            if (add_pTotal(derivedName))
244 245
            {
                cleanup.append(derivedName);
246
            }
247
            added.append(derivedName);
248
        }
249 250 251 252 253 254 255 256 257
        else
        {
            WarningInFunction
                << "unknown derived name: " << derivedName << nl
                << "Use one of 'rhoU', 'pTotal'" << nl
                << endl;
        }
    }

258

259 260
    // The acceptable fields
    wordHashSet acceptable(added);
261 262 263 264 265
    acceptable.insert(acceptType<scalar>());
    acceptable.insert(acceptType<vector>());
    acceptable.insert(acceptType<sphericalTensor>());
    acceptable.insert(acceptType<symmTensor>());
    acceptable.insert(acceptType<tensor>());
266

267 268 269 270
    const wordList fields = acceptable.sortedToc();
    if (!fields.empty())
    {
        for (surfMeshSample& s : surfaces())
271
        {
272 273
            // Potentially monitor the update for writing geometry?
            if (s.needsUpdate())
274
            {
275
                s.update();
276 277
            }

278
            s.sample(fields, sampleFaceScheme_);
279
        }
280 281
    }

282 283 284 285 286
    // Cleanup any locally introduced names
    for (const word& fieldName : cleanup)
    {
        db.checkOut(fieldName);
    }
287

288 289 290 291 292 293 294 295 296 297 298 299
    return true;
}


bool Foam::surfMeshSamplers::write()
{
    // Write sampled fields (on surface)
    //
    // Doesn't bother checking which fields have been generated here
    // or elsewhere

    // This could be more efficient
300
    wordRes select(fieldSelection_.size() + derivedNames_.size());
301 302

    label nElem = 0;
303
    for (const wordRe& item : fieldSelection_)
304
    {
305
        select[nElem++] = item;
306
    }
307
    for (const auto& derivedName : derivedNames_)
308
    {
309
        select[nElem++] = derivedName;
310 311
    }

312 313
    // Avoid duplicate entries
    select.uniq();
314

315
    for (const surfMeshSample& s : surfaces())
316 317 318 319 320 321 322 323 324 325
    {
        s.write(select);
    }

    return true;
}


bool Foam::surfMeshSamplers::read(const dictionary& dict)
{
326
    PtrList<surfMeshSample>::clear();
327
    fieldSelection_.clear();
328 329
    derivedNames_.clear();

330 331
    rhoRef_ = dict.lookupOrDefault<scalar>("rhoRef", 1);

332
    const bool createOnRead = dict.lookupOrDefault("createOnRead", false);
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    sampleFaceScheme_ =
        dict.lookupOrDefault<word>("sampleScheme", "cell");

    const entry* eptr = dict.findEntry("surfaces");

    if (eptr && eptr->isDict())
    {
        PtrList<surfMeshSample> surfs(eptr->dict().size());

        label surfi = 0;

        for (const entry& dEntry : eptr->dict())
        {
            if (dEntry.isDict())
            {
                autoPtr<surfMeshSample> surf =
                    surfMeshSample::New
                    (
                        dEntry.keyword(),
                        mesh_,
                        dEntry.dict()
                    );

                if (surf.valid() && surf->enabled())
                {
                    surfs.set(surfi, surf);
                    ++surfi;
                }
            }
        }

        surfs.resize(surfi);
        surfaces().transfer(surfs);
    }
    else if (eptr)
369
    {
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
        PtrList<surfMeshSample> surfs
        (
            eptr->stream(),
            surfMeshSample::iNew(mesh_)
        );

        forAll(surfs, surfi)
        {
            if (!surfs[surfi].enabled())
            {
                surfs.set(surfi, nullptr);
            }
        }

        surfs.resize(surfs.squeezeNull());
385

386 387 388 389 390 391
        surfaces().transfer(surfs);
    }

    auto& surfs = surfaces();
    if (surfs.size())
    {
392
        dict.readEntry("fields", fieldSelection_);
393 394
        fieldSelection_.uniq();

395
        Info<< type() << " fields:  " << flatOutput(fieldSelection_) << nl;
396 397 398

        if (dict.readIfPresent("derived", derivedNames_))
        {
399
            Info<< type() << " derived: " << flatOutput(derivedNames_) << nl;
400
        }
401
        Info<< nl;
402 403 404 405 406 407 408

        // Ensure all surfaces and merge information are expired
        expire();

        // Need to initialize corresponding surfMesh for others in the chain.
        // This can simply be a zero-sized placeholder, or the real surface with
        // faces.
409 410 411

        label surfi = 0;
        for (surfMeshSample& s : surfs)
412
        {
413
            if (!surfi)
414
            {
415 416 417 418 419 420 421
                Info<< "Sampled surface:" << nl;
            }
            Info<< "    " << s.name() << nl;

            if (createOnRead)
            {
                s.update();
422
            }
423 424 425 426 427 428
            else
            {
                s.create();
            }

            ++surfi;
429
        }
430 431

        Info<< nl;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    }

    return true;
}


void Foam::surfMeshSamplers::updateMesh(const mapPolyMesh& mpm)
{
    if (&mpm.mesh() == &mesh_)
    {
        expire();
    }

    // pointMesh and interpolation will have been reset in mesh.update
}


void Foam::surfMeshSamplers::movePoints(const polyMesh& m)
{
    if (&m == &mesh_)
    {
        expire();
    }
}


void Foam::surfMeshSamplers::readUpdate(const polyMesh::readUpdateState state)
{
    if (state != polyMesh::UNCHANGED)
    {
        expire();
    }
}


bool Foam::surfMeshSamplers::needsUpdate() const
{
469
    for (const surfMeshSample& s : surfaces())
470
    {
471
        if (s.needsUpdate())
472 473 474 475 476 477 478 479 480 481 482 483 484
        {
            return true;
        }
    }

    return false;
}


bool Foam::surfMeshSamplers::expire()
{
    bool justExpired = false;

485
    for (surfMeshSample& s : surfaces())
486
    {
487
        if (s.expire())
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        {
            justExpired = true;
        }
    }

    // True if any surfaces just expired
    return justExpired;
}


bool Foam::surfMeshSamplers::update()
{
    if (!needsUpdate())
    {
        return false;
    }

    bool updated = false;
506
    for (surfMeshSample& s : surfaces())
507
    {
508
        if (s.update())
509 510 511 512 513 514 515 516 517 518
        {
            updated = true;
        }
    }

    return updated;
}


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