smoothSurfaceData.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Application
27  smoothSurfaceData
28 
29 Description
30  Pre-processing, filtering of surface field data.
31  Currently this is just used to test filter settings
32  (for MappedFile) and only generates vtk output, which can be
33  easily loaded in ParaView.
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #include "argList.H"
38 #include "Time.H"
39 #include "clockTime.H"
40 #include "primitiveFields.H"
41 #include "surfaceReader.H"
42 #include "surfaceWriter.H"
43 #include "foamVtkSurfaceWriter.H"
44 #include "MappedFileFilterField.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 using namespace Foam;
49 
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 int main(int argc, char *argv[])
54 {
56  (
57  "Testing, pre-processing, filtering of surface field data"
58  );
59 
61  argList::addVerboseOption("Additional verbosity");
62 
63  argList::addArgument("input", "The input surface file");
64 
65  argList::addOption("radius", "m", "Specify filter radius [metres]");
66  argList::addOption("sweeps", "N", "Number of median filter stages");
68  (
69  "field",
70  "name",
71  "Field <scalar> to process (default: T)"
72  );
73 
75  (
76  "read-format",
77  "type",
78  "Input format (default: ensight)"
79  );
80 
81  argList args(argc, argv);
82  // #include "setRootCase.H"
83 
84  const int optVerbose = args.verbose();
85 
86  const word readFileType
87  (
88  args.getOrDefault<word>("read-format", "ensight")
89  );
90 
91  // Constant radius searching
92  label filterSweeps_(1);
93  scalar filterRadius_(0);
94  args.readIfPresent("sweeps", filterSweeps_);
95  args.readIfPresent("radius", filterRadius_);
96 
97  Info<< nl
98  << "Filter: radius=" << filterRadius_
99  << " sweeps=" << filterSweeps_ << endl;
100 
101  // Simple sanity check
102  if ((filterSweeps_ < 1) || (filterRadius_ <= VSMALL))
103  {
104  Info<< nl << "Nothing to do. Exiting..." << nl << endl;
105  return 0;
106  }
107 
108  word fieldName("T");
109  args.readIfPresent("field", fieldName);
110 
111 
112  const fileName importName = args.get<fileName>(1);
113 
114  auto readerPtr_ = surfaceReader::New(readFileType, importName);
115 
116  auto& reader = readerPtr_();
117 
118  const label fieldIndex = reader.fieldNames(0).find(fieldName);
119  if (fieldIndex == -1)
120  {
122  << "Unable to find field name: " << fieldName
123  << " in list of available fields: " << reader.fieldNames(0)
124  << exit(FatalError);
125  }
126 
127  clockTime timing;
128 
129  const meshedSurface& geom = reader.geometry(0);
130 
131  Info<< nl << "Read " << geom.nFaces() << " faces and "
132  << geom.nPoints() << " points in "
133  << timing.timeIncrement() << "s" << endl;
134 
136 
138 
139  fieldFilter.reset(geom, filterRadius_);
140 
141  Info<< nl << "Built weights/addressing "
142  << timing.timeIncrement() << "s" << endl;
143 
144  Info<< nl << "Processing " << reader.times().size() << " times" << nl;
145 
146  const instantList times(reader.times());
147 
148  forAll(times, timeIndex)
149  {
150  tmp<scalarField> tfield
151  (
152  reader.field(timeIndex, fieldIndex, pTraits<scalar>::zero)
153  );
154 
155  tfield = fieldFilter.evaluate(tfield, filterSweeps_);
156 
158  (
159  geom.points(),
160  geom,
161  word::printf("filtered_%06d", timeIndex)
162  );
163 
164  writer.piece(geom.points(), geom);
165 
168  writer.writeCellData(fieldName, tfield());
169  }
170 
171  Info<< nl << "Smoothing/writing "
172  << timing.timeIncrement() << "s" << endl;
173 
174  Info<< nl << "End\n" << endl;
175 
176  return 0;
177 }
178 
179 
180 // ************************************************************************* //
label nPoints() const
Number of points supporting patch faces.
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:453
A class for handling file names.
Definition: fileName.H:71
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Write faces/points (optionally with fields) as a vtp file or a legacy vtk file.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
A traits class, which is primarily used for primitives.
Definition: pTraits.H:50
virtual bool beginCellData(label nFields=0)
Begin CellData output section for specified number of fields.
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:413
The FilterField helper class provides a multi-sweep median filter for a Field of data associated with...
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static void addVerboseOption(const string &usage, bool advanced=false)
Enable a &#39;verbose&#39; bool option, with usage information.
Definition: argList.C:505
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:376
const Field< point_type > & points() const noexcept
Return reference to global points.
static void noCheckProcessorDirectories()
Remove checking of processor directories.
Definition: argList.C:562
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
void reset()
Reset to unweighted (pass-through)
int debug
Static debugging option.
vtk::internalMeshWriter writer(topoMesh, topoCells, vtk::formatType::INLINE_ASCII, runTime.path()/"blockTopology")
tmp< Field< Type > > evaluate(const tmp< Field< Type >> &tinput, const label nSweeps) const
Return the median smoothed field.
Specialisations of Field<T> for scalar, vector and tensor.
static autoPtr< surfaceReader > New(const word &readType, const fileName &fName, const dictionary &options=dictionary())
Return a reference to the selected surfaceReader.
Definition: surfaceReader.C:71
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:342
double timeIncrement() const
The time [seconds] since the last call to timeIncrement()
Definition: clockTimeI.H:53
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
void writeCellData(const word &fieldName, const UList< Type > &field)
Write primitive field of CellData.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
label nFaces() const noexcept
Number of faces in the patch.
virtual bool writeGeometry()
Write mesh topology.
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
Namespace for OpenFOAM.
label timeIndex
Definition: getTimeIndex.H:24
Starts timing and returns elapsed time from start. Uses std::chrono::high_resolution_clock for better...
Definition: clockTime.H:58