catalystFunctionObject.C 9.52 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
6 7 8
     \\/     M anipulation  |
-------------------------------------------------------------------------------
                            | Copyright (C) 2018 CINECA
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
-------------------------------------------------------------------------------
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 "catalystFunctionObject.H"
#include "catalystCoprocess.H"
#include "stringOps.H"
#include "OSHA1stream.H"
#include "OSspecific.H"
33
#include "sigFpe.H"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 80 81 82 83 84
#include "addToRunTimeSelectionTable.H"

#include <vtkCPDataDescription.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkInformation.h>
#include <vtkSmartPointer.h>

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

namespace Foam
{
namespace functionObjects
{
    defineTypeNameAndDebug(catalystFunctionObject, 0);
    addToRunTimeSelectionTable
    (
        functionObject,
        catalystFunctionObject,
        dictionary
    );
} // End namespace functionObjects
} // End namespace Foam


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

Foam::label Foam::functionObjects::catalystFunctionObject::expand
(
    List<string>& scripts,
    const dictionary& dict
)
{
    label nscript = 0;

    forAll(scripts, scripti)
    {
        string& s = scripts[scripti];

        stringOps::inplaceExpand(s, dict, true, true);
        fileName::clean(s);  // Remove trailing, repeated slashes etc.

        if (isFile(s))
        {
            if (nscript != scripti)
            {
                scripts[nscript] = std::move(s);
            }
            ++nscript;
        }
    }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    const label nbad = (scripts.size() - nscript);

    if (nbad)
    {
        Warning
            << nl
            << "Could not resolve " << nbad << " of " << scripts.size()
            << " catalyst scripts - check your input" << nl << nl;
    }

    if (!nscript)
    {
        Warning
            << nl
            << "No catalyst scripts resolved - check your input" << nl << nl;
    }

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    scripts.resize(nscript);

    return nscript;
}


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

Foam::functionObjects::catalystFunctionObject::catalystFunctionObject
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
    functionObject(name),
    time_(runTime),
    outputDir_("<case>/insitu"),
    scripts_(),
    adaptor_(),
    inputs_()
{
    if (postProcess)
    {
        // Disable for post-process mode.
        // Emit as FatalError for the try/catch in the caller.
        FatalError
            << type() << " disabled in post-process mode"
            << exit(FatalError);
    }

    if (!read(dict))
    {
        FatalError
            << type() << " with errors"
            << exit(FatalError);
    }
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::functionObjects::catalystFunctionObject::~catalystFunctionObject()
{}


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

bool Foam::functionObjects::catalystFunctionObject::read(const dictionary& dict)
{
    functionObject::read(dict);

Mark Olesen's avatar
Mark Olesen committed
154
    const dictionary* inputDictPtr = dict.findDict("inputs");
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    if (!inputDictPtr || inputDictPtr->empty())
    {
        inputs_.clear();

        WarningInFunction
            << type() << " missing inputs" << endl;

        return false;
    }

    int debugLevel = 0;
    if (dict.readIfPresent("debug", debugLevel))
    {
        catalyst::coprocess::debug = debugLevel;
    }

    if (Pstream::master())
    {
        fileName dir;
        if (dict.readIfPresent("mkdir", dir))
        {
            dir.expand();
            dir.clean();
            Foam::mkDir(dir);
        }
    }


    // Track changes in outputDir and/or scripts
    OSHA1stream osha1;
    osha1 << outputDir_ << scripts_;
    const SHA1Digest oldDigest(osha1.digest());

    dict.readIfPresent("outputDir", outputDir_);
    outputDir_.expand();
    outputDir_.clean();
    if (Pstream::master())
    {
        Foam::mkDir(outputDir_);
    }

197 198
    dict.readEntry("scripts", scripts_);    // Python scripts
    expand(scripts_, dict);                 // Expand and check availability
199 200 201 202


    // Any changes detected
    osha1.reset();
Mark Olesen's avatar
Mark Olesen committed
203
    osha1 << outputDir_ << scripts_;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    if (adaptor_.valid() && oldDigest != osha1.digest())
    {
        adaptor_().reset(outputDir_, scripts_);
    }


    PtrList<catalyst::catalystInput> newList(inputDictPtr->size());

    label nInputs = 0;
    forAllConstIters(*inputDictPtr, iter)
    {
        const dictionary* subDictPtr = iter().dictPtr();
        if (iter().keyword().isPattern() || !subDictPtr)
        {
            continue;
        }

222
        auto input =
223 224 225 226 227
            catalyst::catalystInput::New
            (
                word(iter().keyword()),
                time_,
                *subDictPtr
228
            );
229

230 231 232 233
        // We may wish to perform additional validity or sanity checks on
        // the input before deciding to add it to the list.

        newList.set(nInputs, input);
234 235 236 237 238 239 240 241 242 243 244 245 246 247
        ++nInputs;
    }

    newList.resize(nInputs);
    inputs_.transfer(newList);

    if (inputs_.empty())
    {
        WarningInFunction
            << type() << " missing inputs" << endl;
    }
    else
    {
        Info<< type() << " " << name() << ":" << nl
248 249
            << "    output: " << outputDir_ << nl
            << "    scripts: " << scripts_ << nl
250 251 252 253 254 255
            << "    inputs:" << nl
            << "(" << nl;

        nInputs = 0;
        for (const auto& inp : inputs_)
        {
256
            if (nInputs++) Info<< nl;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            inp.print(Info);
        }

        Info<< ")" << nl;
    }

    return true;
}


bool Foam::functionObjects::catalystFunctionObject::execute()
{
    if (inputs_.empty())
    {
        return false;
    }

274 275 276 277 278 279
    // Disable any floating point trapping so that errors in
    // Catalyst pipelines do not kill the simulation
    // TODO: report that errors occurred?

    sigFpe::ignore sigFpeHandling; //<- disable in local scope

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    if (!adaptor_.valid())
    {
        adaptor_.reset(new catalyst::coprocess());
        adaptor_().reset(outputDir_, scripts_);
    }

    catalyst::dataQuery dataq(time_);

    label nChannels = 0;
    for (auto& inp : inputs_)
    {
        nChannels += inp.addChannels(dataq);
    }

    if (nChannels)
    {
296 297 298 299 300 301
        if (catalyst::coprocess::debug > 1)
        {
            Pout<< type() << ": query catalyst for " << nChannels
                << " channels" << nl;
        }

302 303 304
        nChannels = adaptor_().query(dataq);
    }

305 306 307 308 309 310
    if (catalyst::coprocess::debug > 1)
    {
        Pout<< type() << ": query catalyst for " << nChannels
            << " channels" << nl;
    }
    else if (catalyst::coprocess::debug)
311
    {
312 313
        Info<< type() << ": expecting data for " << nChannels
            << " channels" << nl;
314 315 316 317 318 319 320 321 322
    }

    if (!nChannels)
    {
        return true;
    }

    catalyst::outputChannels outputs;

323 324 325 326 327
    if (catalyst::coprocess::debug > 1)
    {
        Pout<< type() << ": converting input" << nl;
    }

328 329 330 331 332
    for (auto& inp : inputs_)
    {
        inp.convert(dataq, outputs);
    }

333 334
    if (catalyst::coprocess::debug > 1)
    {
335
        Pout<< type() << ": sending data for " << outputs.size()
336 337 338
            << " outputs" << nl;
    }

339 340
    if (outputs.size())
    {
341 342 343 344 345 346 347 348 349
        if (log)
        {
            Info<< type() << ": send data (";
            for (const word& channelName : outputs.sortedToc())
            {
                Info<< ' ' << channelName;
            }
            Info<< " )" << nl;
        }
350 351 352 353

        adaptor_().process(dataq, outputs);
    }

354

355 356 357 358 359 360
    if (catalyst::coprocess::debug > 1)
    {
        Pout<< type() << ": done step" << nl;
    }


361 362 363 364
    // Instead of relying on the destructor, manually restore the previous
    // SIGFPE state.
    // This is only to avoid compiler complaints about unused variables.

365 366
    sigFpeHandling.restore();

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    return true;
}


bool Foam::functionObjects::catalystFunctionObject::write()
{
    return true;
}


bool Foam::functionObjects::catalystFunctionObject::end()
{
    // Only here for extra feedback
    if (log && adaptor_.valid())
    {
        Info<< type() << ": Disconnecting ParaView Catalyst..." << nl;
    }

    adaptor_.clear();
386 387
    inputs_.clear();

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    return true;
}


void Foam::functionObjects::catalystFunctionObject::updateMesh
(
    const mapPolyMesh&
)
{
    for (auto& inp : inputs_)
    {
        inp.update(polyMesh::TOPO_CHANGE);
    }
}


void Foam::functionObjects::catalystFunctionObject::movePoints
(
    const polyMesh&
)
{
    for (auto& inp : inputs_)
    {
        inp.update(polyMesh::POINTS_MOVED);
    }
}


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