Loading...
Searching...
No Matches
mapFields.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) 2016-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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\*---------------------------------------------------------------------------*/
27
28#include "mapFields.H"
29#include "meshToMesh.H"
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36namespace functionObjects
37{
40}
41}
42
43
44// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
45
46void Foam::functionObjects::mapFields::createInterpolation
47(
48 const dictionary& dict,
49 const fvMesh& meshTarget,
50 const fvMesh& mapRegion
51)
52{
53 const word mapMethodName(dict.get<word>("mapMethod"));
55 {
57 << type() << " " << name() << ": unknown map method "
58 << mapMethodName << nl
59 << "Available methods include: "
61 << exit(FatalError);
62 }
63
65 (
67 );
68
69 // Lookup corresponding AMI method
70 word patchMapMethodName = meshToMesh::interpolationMethodAMI(mapMethod);
71
72 // Optionally override
73 if (dict.readIfPresent("patchMapMethod", patchMapMethodName))
74 {
75 Info<< indent
76 << " Patch mapping method: " << patchMapMethodName << endl;
77 }
78
79 Info<< indent
80 << " Creating mesh to mesh interpolation" << endl;
81
82 if (dict.get<bool>("consistent"))
83 {
84 interpPtr_.reset
85 (
86 new meshToMesh
87 (
88 mapRegion,
89 meshTarget,
90 mapMethodName,
91 patchMapMethodName
92 )
93 );
94 }
95 else
96 {
97 HashTable<word> patchMap;
98
99 bool createPatchMap = dict.getOrDefault<bool>("createPatchMap", false);
100
101 const entry* ePtr = dict.findEntry("patchMap");
102
103 if (createPatchMap && ePtr)
104 {
106 << "Requested 'createPatchMap' but 'patchMap' entry provided. "
107 << "Please remove one of these entries"
108 << exit(FatalIOError);
109 }
110
111 if (!createPatchMap && !ePtr)
112 {
114 << "Either the 'createPatchMap' or 'patchMap' entry must be "
115 << "provided when not using the 'consistent' mapping option"
116 << exit(FatalIOError);
117 }
118
119 const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
120 const polyBoundaryMesh& pbmT = mapRegion.boundaryMesh();
121 DynamicList<label> cuttingIndices;
122
123 if (createPatchMap)
124 {
125 Info<< " Creating patchMap and cuttingPatches" << endl;
126
127 if (dict.found("cuttingPatches"))
128 {
130 << "Ignoring user supplied cuttingPatches when "
131 << "createPatchMap option is active"
132 << endl;
133 }
134
135 forAll(pbmT, patchiT)
136 {
137 const polyPatch& ppT = pbmT[patchiT];
138
139 if (!polyPatch::constraintType(ppT.type()))
140 {
141 const word& patchNameT = ppT.name();
142 const label patchi = pbm.findPatchID(patchNameT);
143
144 if (patchi == -1)
145 {
146 Info<< " Adding to cuttingPatches: "
147 << ppT.name() << endl;
148
149 cuttingIndices.push_back(ppT.index());
150 }
151 else
152 {
153 if (returnReduce(ppT.size(), sumOp<label>()) > 0)
154 {
155 Info<< " Adding to patchMap: " << ppT.name()
156 << endl;
157
158 patchMap.set(patchNameT, patchNameT);
159 }
160 }
161 }
162 }
163 }
164 else
165 {
166 // Read patch map
167 patchMap = HashTable<word>(ePtr->stream());
168
169 // Read cutting patches using wordRe's
170 const wordRes cuttingPatchRes = dict.get<wordRes>("cuttingPatches");
171 cuttingIndices.push_back(pbmT.indices(cuttingPatchRes));
172 }
173
174 const wordList cuttingPatches(pbmT.names(), cuttingIndices);
175
176 // Checks
177 {
178 // Find patch names that exist on target mesh that are not included
179 // in the patchMap
180 wordHashSet unknownPatchNames;
181 for (const auto& ppT : pbmT)
182 {
183 if
184 (
185 !polyPatch::constraintType(ppT.type())
186 && !patchMap.found(ppT.name())
187 && returnReduce(ppT.size(), sumOp<label>()) > 0
188 )
189 {
190 unknownPatchNames.insert(ppT.name());
191 }
192 }
193
194 for (const label patchiT : cuttingIndices)
195 {
196 const word& patchName = pbmT[patchiT].name();
197
198 unknownPatchNames.erase(patchName);
199
200 if (patchMap.found(patchName))
201 {
202 Info<< " Removing cutting patch from patchMap: "
203 << patchName << endl;
204
205 patchMap.erase(patchName);
206 }
207 }
208
209 if (unknownPatchNames.size())
210 {
212 << "Patches not present in source mesh. "
213 << "Add to cuttingPatches? Patches: "
214 << unknownPatchNames
215 << exit(FatalError);
216 }
217 }
218
219 interpPtr_.reset
220 (
221 new meshToMesh
222 (
223 mapRegion,
224 meshTarget,
225 mapMethodName,
226 patchMapMethodName,
227 patchMap,
228 cuttingPatches
229 )
230 );
231 }
232}
233
234
235const Foam::fvMesh& Foam::functionObjects::mapFields::lookupMapRegion() const
236{
237 // Load & register mesh
238 const word mapRegionName(dict_.get<word>("mapRegion"));
239 const auto* mapRegionPtr = mesh_.time().findObject<fvMesh>(mapRegionName);
240 if (mapRegionPtr)
241 {
242 return *mapRegionPtr;
243 }
244 else
245 {
246 Info<< indent
247 << " Reading mesh " << mapRegionName << endl;
248
249 fvMesh* ptr = new fvMesh
250 (
251 IOobject
252 (
253 mapRegionName,
254 mesh_.time().constant(),
255 mesh_.time(), // objectRegistry
257 )
258 );
259
260 // Store new mesh on its object registry
261 ptr->objectRegistry::store();
262 return *ptr;
263 }
264}
265
266
267// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
268
270(
271 const word& name,
272 const Time& runTime,
273 const dictionary& dict
274)
275:
276 fvMeshFunctionObject(name, runTime, dict),
277 interpPtr_(),
278 fieldNames_()
280 read(dict);
281}
282
283
284// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
285
287{
289 {
290 dict_ = dict.optionalSubDict(typeName + "Coeffs");
291 dict_.readEntry("fields", fieldNames_);
292
293 createInterpolation(dict_, mesh_, lookupMapRegion());
294
295 return true;
296 }
297
298 return false;
299}
300
301
303{
304 Log << type() << " " << name() << " execute:" << nl;
305
306 bool ok = false;
307
308 const word mapRegionName(dict_.get<word>("mapRegion"));
309 const fvMesh& mapRegion = lookupMapRegion();
310
311 if (mesh_.changing() || mapRegion.changing())
312 {
313 createInterpolation(dict_, mesh_, mapRegion);
314 }
315
316 ok = mapFieldType<scalar>() || ok;
317 ok = mapFieldType<vector>() || ok;
318 ok = mapFieldType<sphericalTensor>() || ok;
319 ok = mapFieldType<symmTensor>() || ok;
320 ok = mapFieldType<tensor>() || ok;
321
322 if (log)
323 {
324 if (!ok)
325 {
326 Info<< " none" << nl;
327 }
329 Info<< endl;
330 }
331 return true;
332}
333
334
336{
337 Log << type() << " " << name() << " write:" << nl;
338
339 bool ok = false;
340
341 ok = writeFieldType<scalar>() || ok;
342 ok = writeFieldType<vector>() || ok;
343 ok = writeFieldType<sphericalTensor>() || ok;
344 ok = writeFieldType<symmTensor>() || ok;
345 ok = writeFieldType<tensor>() || ok;
346
347 if (log)
348 {
349 if (!ok)
350 {
351 Info<< " none" << nl;
352 }
353
354 Info<< endl;
355 }
356
357 return true;
358}
359
360
361// ************************************************************************* //
#define Log
Definition PDRblock.C:28
bool found
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
const polyBoundaryMesh & pbm
@ MUST_READ
Reading required.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
virtual const word & type() const =0
Runtime type information.
const fvMesh & mesh_
Reference to the fvMesh.
fvMeshFunctionObject(const fvMeshFunctionObject &)=delete
No copy construct.
Computes the natural logarithm of an input volScalarField.
Definition log.H:212
Maps input fields from local mesh to secondary mesh at runtime.
Definition mapFields.H:226
mapFields(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Definition mapFields.C:263
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
Definition mapFields.C:279
virtual bool execute()
Execute the function-object operations.
Definition mapFields.C:295
virtual bool write()
Write the function-object results.
Definition mapFields.C:328
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
interpolationMethod
Enumeration specifying interpolation method.
Definition meshToMesh.H:70
static word interpolationMethodAMI(const interpolationMethod method)
Conversion between mesh and patch interpolation methods.
Definition meshToMesh.C:639
static const Enum< interpolationMethod > interpolationMethodNames_
Definition meshToMesh.H:77
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundary mesh.
Definition polyMesh.H:609
bool changing() const noexcept
Is mesh changing (topology changing and/or moving).
Definition polyMesh.H:768
static bool constraintType(const word &patchType)
Return true if the given type is a constraint type.
Definition polyPatch.C:255
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
messageStream Info
Information stream (stdout output on master, null elsewhere).
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
dimensionedScalar log(const dimensionedScalar &ds)
T returnReduce(const T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299