/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
-------------------------------------------------------------------------------
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
setFields
Group
grpPreProcessingUtilities
Description
Set values on a selected set of cells/patch-faces via a dictionary.
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "Time.H"
#include "fvMesh.H"
#include "topoSetSource.H"
#include "cellSet.H"
#include "faceSet.H"
#include "volFields.H"
using namespace Foam;
template
bool setCellFieldType
(
const word& fieldTypeDesc,
const fvMesh& mesh,
const labelList& selectedCells,
Istream& fieldValueStream
)
{
typedef GeometricField fieldType;
if (fieldTypeDesc != fieldType::typeName + "Value")
{
return false;
}
word fieldName(fieldValueStream);
// Check the current time directory
IOobject fieldHeader
(
fieldName,
mesh.time().timeName(),
mesh,
IOobject::MUST_READ
);
// Check the "constant" directory
if (!fieldHeader.typeHeaderOk(true))
{
fieldHeader = IOobject
(
fieldName,
mesh.time().constant(),
mesh,
IOobject::MUST_READ
);
}
// Check field exists
if (fieldHeader.typeHeaderOk(true))
{
Info<< " Setting internal values of "
<< fieldHeader.headerClassName()
<< " " << fieldName << endl;
fieldType field(fieldHeader, mesh, false);
const Type& value = pTraits(fieldValueStream);
if (selectedCells.size() == field.size())
{
field.primitiveFieldRef() = value;
}
else
{
forAll(selectedCells, celli)
{
field[selectedCells[celli]] = value;
}
}
typename GeometricField::
Boundary& fieldBf = field.boundaryFieldRef();
forAll(field.boundaryField(), patchi)
{
fieldBf[patchi] = fieldBf[patchi].patchInternalField();
}
if (!field.write())
{
FatalErrorInFunction
<< "Failed writing field " << fieldName << endl;
}
}
else
{
WarningInFunction
<< "Field " << fieldName << " not found" << endl;
// Consume value
(void)pTraits(fieldValueStream);
}
return true;
}
class setCellField
{
public:
setCellField()
{}
autoPtr clone() const
{
return autoPtr::New();
}
class iNew
{
const fvMesh& mesh_;
const labelList& selectedCells_;
public:
iNew(const fvMesh& mesh, const labelList& selectedCells)
:
mesh_(mesh),
selectedCells_(selectedCells)
{}
iNew(const fvMesh& mesh, labelList&& selectedCells)
:
mesh_(mesh),
selectedCells_(std::move(selectedCells))
{}
autoPtr operator()(Istream& fieldValues) const
{
word fieldType(fieldValues);
if
(
!(
setCellFieldType
(fieldType, mesh_, selectedCells_, fieldValues)
|| setCellFieldType
(fieldType, mesh_, selectedCells_, fieldValues)
|| setCellFieldType
(fieldType, mesh_, selectedCells_, fieldValues)
|| setCellFieldType
(fieldType, mesh_, selectedCells_, fieldValues)
|| setCellFieldType
(fieldType, mesh_, selectedCells_, fieldValues)
)
)
{
WarningInFunction
<< "field type " << fieldType << " not currently supported"
<< endl;
}
return autoPtr::New();
}
};
};
template
bool setFaceFieldType
(
const word& fieldTypeDesc,
const fvMesh& mesh,
const labelList& selectedFaces,
Istream& fieldValueStream
)
{
typedef GeometricField fieldType;
if (fieldTypeDesc != fieldType::typeName + "Value")
{
return false;
}
word fieldName(fieldValueStream);
// Check the current time directory
IOobject fieldHeader
(
fieldName,
mesh.time().timeName(),
mesh,
IOobject::MUST_READ
);
// Check the "constant" directory
if (!fieldHeader.typeHeaderOk(true))
{
fieldHeader = IOobject
(
fieldName,
mesh.time().constant(),
mesh,
IOobject::MUST_READ
);
}
// Check field exists
if (fieldHeader.typeHeaderOk(true))
{
Info<< " Setting patchField values of "
<< fieldHeader.headerClassName()
<< " " << fieldName << endl;
fieldType field(fieldHeader, mesh);
const Type& value = pTraits(fieldValueStream);
// Create flat list of selected faces and their value.
Field allBoundaryValues(mesh.nBoundaryFaces());
forAll(field.boundaryField(), patchi)
{
SubField
(
allBoundaryValues,
field.boundaryField()[patchi].size(),
field.boundaryField()[patchi].patch().start()
- mesh.nInternalFaces()
) = field.boundaryField()[patchi];
}
// Override
bool hasWarned = false;
labelList nChanged
(
returnReduce(field.boundaryField().size(), maxOp