catalystFunctionObject.C 9.41 KB
Newer Older
1 2 3 4 5 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
     \\/     M anipulation  | Copyright (C) 2018 CINECA
-------------------------------------------------------------------------------
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"
31
#include "sigFpe.H"
32 33 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
#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;
        }
    }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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;
    }

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 137 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 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
    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);

    const dictionary* inputDictPtr = dict.subDictPtr("inputs");

    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_);
    }

195
    dict.read("scripts", scripts_);     // Python scripts
196 197 198 199 200
    expand(scripts_, dict);             // Expand and check availability


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

    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;
        }

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

228 229 230 231
        // 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);
232 233 234 235 236 237 238 239 240 241 242 243 244 245
        ++nInputs;
    }

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

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

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

        Info<< ")" << nl;
    }

    return true;
}


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

272 273 274 275 276 277
    // 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

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    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)
    {
294 295 296 297 298 299
        if (catalyst::coprocess::debug > 1)
        {
            Pout<< type() << ": query catalyst for " << nChannels
                << " channels" << nl;
        }

300 301 302
        nChannels = adaptor_().query(dataq);
    }

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

    if (!nChannels)
    {
        return true;
    }

    catalyst::outputChannels outputs;

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

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

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

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

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

352

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


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

363 364
    sigFpeHandling.restore();

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    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();
384 385
    inputs_.clear();

386 387 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
    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);
    }
}


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