/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2019-2022 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 . Application setExprFields Group grpPreProcessingUtilities Description Set values on a selected set of cells/patch-faces via a dictionary. Note Based on funkySetFields from Bernhard Gschaider \*---------------------------------------------------------------------------*/ #include "argList.H" #include "Time.H" #include "fvMesh.H" #include "pointMesh.H" #include "volFields.H" #include "surfaceFields.H" #include "pointFields.H" #include "exprOps.H" #include "volumeExprDriver.H" #include "timeSelector.H" #include "readFields.H" using namespace Foam; using FieldAssociation = expressions::FieldAssociation; word fieldGeoType(const FieldAssociation geoType) { switch (geoType) { case FieldAssociation::POINT_DATA : return "points"; break; case FieldAssociation::FACE_DATA : return "faces"; break; case FieldAssociation::VOLUME_DATA : return "cells"; break; default: break; } return "unknown"; } //- Simple control structure to with collected switches to simplify passing struct setExprFieldsControl { bool dryRun; bool debugParsing; bool cacheVariables; bool hasDimensions; bool createNew; bool keepPatches; bool correctPatches; bool correctBCs; IOstreamOption streamOpt; }; template void doCorrectBoundaryConditions ( bool correctBCs, GeometricField& field ) { if (correctBCs) { Info<< "Correcting boundary conditions: " << field.name() << nl; field.correctBoundaryConditions(); } } template void doCorrectBoundaryConditions ( bool correctBCs, GeometricField& field ) { if (correctBCs) { Info<< "Correcting boundary conditions: " << field.name() << nl; field.correctBoundaryConditions(); } } template void doCorrectBoundaryConditions ( bool correctBCs, GeometricField& field ) {} template bool setField ( const word& fieldName, const GeoField& evaluated, const boolField& fieldMask, const dimensionSet& dims, const wordList& valuePatches, const setExprFieldsControl& ctrl ) { Info<< "setField(" << fieldName << "): " << pTraits::typeName << endl; const auto& mesh = evaluated.mesh(); tmp toutput; if (ctrl.createNew) { // Create with zero toutput = GeoField::New ( fieldName, mesh, Foam::zero{}, // value dims ); } else { // Read toutput = tmp::New ( IOobject ( fieldName, mesh.thisDb().time().timeName(), mesh.thisDb(), IOobject::MUST_READ, IOobject::NO_WRITE, IOobject::NO_REGISTER ), mesh ); } auto& output = toutput.ref(); label numValuesChanged = 0; // Internal field if (fieldMask.empty()) { // No field-mask - set entire internal field numValuesChanged = output.size(); output.primitiveFieldRef() = evaluated; } else { auto& internal = output.primitiveFieldRef(); forAll(internal, idx) { if (fieldMask[idx]) { internal[idx] = evaluated[idx]; ++numValuesChanged; } } } // Boundary fields forAll(evaluated.boundaryField(), patchi) { auto& pf = output.boundaryFieldRef()[patchi]; if (pf.patch().coupled()) { pf == evaluated.boundaryField()[patchi]; } } doCorrectBoundaryConditions(ctrl.correctBCs, output); const label numTotal = returnReduce(output.size(), sumOp