From b2b77c9c13e9438232fe2a938bb4707880d2c0ff Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 12 Jun 2018 23:22:23 +0200 Subject: [PATCH] ENH: add 'cellCentre' sampledSet (issue #869) - samples on cell centres, can optionally limit based on a bounding box. Can be used, for example, to extract volume fields into CSV format. --- src/sampling/Make/files | 1 + .../sampledSet/cellCentre/cellCentreSet.C | 132 ++++++++++++++++++ .../sampledSet/cellCentre/cellCentreSet.H | 115 +++++++++++++++ .../squareBend/system/controlDict | 1 + .../squareBend/system/sampleCellCentres | 34 +++++ 5 files changed, 283 insertions(+) create mode 100644 src/sampling/sampledSet/cellCentre/cellCentreSet.C create mode 100644 src/sampling/sampledSet/cellCentre/cellCentreSet.H create mode 100644 tutorials/compressible/rhoSimpleFoam/squareBend/system/sampleCellCentres diff --git a/src/sampling/Make/files b/src/sampling/Make/files index 438f67c235..a97d335f17 100644 --- a/src/sampling/Make/files +++ b/src/sampling/Make/files @@ -6,6 +6,7 @@ sampledSet/circle/circleSet.C sampledSet/cloud/cloudSet.C sampledSet/patchCloud/patchCloudSet.C sampledSet/polyLine/polyLineSet.C +sampledSet/cellCentre/cellCentreSet.C sampledSet/face/faceOnlySet.C sampledSet/midPoint/midPointSet.C sampledSet/midPointAndFace/midPointAndFaceSet.C diff --git a/src/sampling/sampledSet/cellCentre/cellCentreSet.C b/src/sampling/sampledSet/cellCentre/cellCentreSet.C new file mode 100644 index 0000000000..2631595ff1 --- /dev/null +++ b/src/sampling/sampledSet/cellCentre/cellCentreSet.C @@ -0,0 +1,132 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ 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 . + +\*---------------------------------------------------------------------------*/ + +#include "cellCentreSet.H" +#include "meshSearch.H" +#include "polyMesh.H" +#include "volFields.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(cellCentreSet, 0); + addToRunTimeSelectionTable(sampledSet, cellCentreSet, word); +} + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +void Foam::cellCentreSet::genSamples() +{ + const label len = mesh().nCells(); + + const auto& cellCentres = + refCast(mesh()).C().primitiveField(); + + labelList selectedCells = identity(len); + List selectedPoints; + + if (bounds_.empty()) + { + selectedPoints = cellCentres; + } + else + { + label count = 0; + for (label celli=0; celli < len; ++celli) + { + if (bounds_.contains(cellCentres[celli])) + { + selectedCells[count++] = celli; + } + } + + selectedCells.resize(count); + selectedPoints = UIndirectList(cellCentres, selectedCells); + } + + labelList samplingFaces(selectedCells.size(), -1); + labelList samplingSegments(selectedCells.size(), -1); + scalarList samplingCurveDist(selectedCells.size(), 0.0); + + // Move into *this + setSamples + ( + std::move(selectedPoints), + std::move(selectedCells), + std::move(samplingFaces), + std::move(samplingSegments), + std::move(samplingCurveDist) + ); + + if (debug) + { + write(Info); + } +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::cellCentreSet::cellCentreSet +( + const word& name, + const polyMesh& mesh, + const meshSearch& searchEngine, + const word& axis, + const boundBox& bbox +) +: + sampledSet(name, mesh, searchEngine, axis), + bounds_(bbox) +{ + genSamples(); +} + + +Foam::cellCentreSet::cellCentreSet +( + const word& name, + const polyMesh& mesh, + const meshSearch& searchEngine, + const dictionary& dict +) +: + sampledSet + ( + name, + mesh, + searchEngine, + dict.lookupOrDefault("axis", "xyz") + ), + bounds_(dict.lookupOrDefault("bounds", boundBox::invertedBox)) +{ + genSamples(); +} + + +// ************************************************************************* // diff --git a/src/sampling/sampledSet/cellCentre/cellCentreSet.H b/src/sampling/sampledSet/cellCentre/cellCentreSet.H new file mode 100644 index 0000000000..508e82ee51 --- /dev/null +++ b/src/sampling/sampledSet/cellCentre/cellCentreSet.H @@ -0,0 +1,115 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ 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 . + +Class + Foam::cellCentreSet + +Description + A sampleSet based on cell centres + + For a dictionary specification: + \table + Property | Description | Required | Default + type | cellCentre | yes | + axis | x, y, z, xyz, distance | no | xyz + bounds | limit with bounding box | no | + \endtable + +SourceFiles + cellCentreSet.C + +\*---------------------------------------------------------------------------*/ + +#ifndef cellCentreSet_H +#define cellCentreSet_H + +#include "sampledSet.H" +#include "boundBox.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class cellCentreSet Declaration +\*---------------------------------------------------------------------------*/ + +class cellCentreSet +: + public sampledSet +{ + // Private data + + //- Optional bounding box to restrict the geometry + const boundBox bounds_; + + + // Private Member Functions + + //- Generate samples + void genSamples(); + + +public: + + //- Runtime type information + TypeName("cellCentre"); + + + // Constructors + + //- Construct from components + cellCentreSet + ( + const word& name, + const polyMesh& mesh, + const meshSearch& searchEngine, + const word& axis, + const boundBox& bbox = boundBox::invertedBox + ); + + //- Construct from dictionary + cellCentreSet + ( + const word& name, + const polyMesh& mesh, + const meshSearch& searchEngine, + const dictionary& dict + ); + + + //- Destructor + virtual ~cellCentreSet() = default; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/tutorials/compressible/rhoSimpleFoam/squareBend/system/controlDict b/tutorials/compressible/rhoSimpleFoam/squareBend/system/controlDict index 74ccb50c5a..7e1979925b 100644 --- a/tutorials/compressible/rhoSimpleFoam/squareBend/system/controlDict +++ b/tutorials/compressible/rhoSimpleFoam/squareBend/system/controlDict @@ -53,6 +53,7 @@ functions { #include "sampling" // #include "samplingDebug" + // #include "sampleCellCentres" } diff --git a/tutorials/compressible/rhoSimpleFoam/squareBend/system/sampleCellCentres b/tutorials/compressible/rhoSimpleFoam/squareBend/system/sampleCellCentres new file mode 100644 index 0000000000..a11a449d6c --- /dev/null +++ b/tutorials/compressible/rhoSimpleFoam/squareBend/system/sampleCellCentres @@ -0,0 +1,34 @@ +// -*- C++ -*- + +sampleSets +{ + type sets; + libs ("libsampling.so"); + log on; + enabled true; + + writeControl timeStep; + writeInterval 10; + + setFormat vtk; + setFormat csv; + interpolationScheme cell; + + // fields ( U p ); + + fields ( U ); + + sets + ( + centres + { + type cellCentre; + // axis xyz; // default: xyz + + // bounds (-0.2 -1 -1) (-0.195 0 1); + bounds (-0.025 -1 -1) (-0.0225 0 1); // single cell layer + } + ); +} + +// ************************************************************************* //