mapFieldsPar.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  mapFieldsPar
29 
30 Group
31  grpPreProcessingUtilities
32 
33 Description
34  Maps volume fields from one mesh to another, reading and
35  interpolating all fields present in the time directory of both cases.
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #include "fvCFD.H"
40 #include "meshToMesh.H"
41 #include "processorPolyPatch.H"
42 #include "MapMeshes.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 void mapConsistentMesh
47 (
48  const fvMesh& meshSource,
49  const fvMesh& meshTarget,
50  const word& mapMethod,
51  const word& AMIMapMethod,
52  const word& procMapMethod,
53  const bool subtract,
54  const wordRes& selectedFields,
55  const bool noLagrangian
56 )
57 {
58  Info<< nl << "Consistently creating and mapping fields for time "
59  << meshSource.time().timeName() << nl << endl;
60 
61  meshToMesh interp
62  (
63  meshSource,
64  meshTarget,
65  mapMethod,
66  AMIMapMethod,
67  meshToMesh::procMapMethodNames_[procMapMethod]
68  );
69 
70  if (subtract)
71  {
72  MapMesh<minusEqOp>
73  (
74  interp,
75  selectedFields,
76  noLagrangian
77  );
78  }
79  else
80  {
81  MapMesh<plusEqOp>
82  (
83  interp,
84  selectedFields,
85  noLagrangian
86  );
87  }
88 }
89 
90 
91 void mapSubMesh
92 (
93  const fvMesh& meshSource,
94  const fvMesh& meshTarget,
95  const HashTable<word>& patchMap,
96  const wordList& cuttingPatches,
97  const word& mapMethod,
98  const word& AMIMapMethod,
99  const word& procMapMethod,
100  const bool subtract,
101  const wordRes& selectedFields,
102  const bool noLagrangian
103 )
104 {
105  Info<< nl << "Creating and mapping fields for time "
106  << meshSource.time().timeName() << nl << endl;
107 
108  meshToMesh interp
109  (
110  meshSource,
111  meshTarget,
112  mapMethod,
113  AMIMapMethod,
114  patchMap,
115  cuttingPatches,
116  meshToMesh::procMapMethodNames_[procMapMethod]
117  );
118 
119  if (subtract)
120  {
121  MapMesh<minusEqOp>
122  (
123  interp,
124  selectedFields,
125  noLagrangian
126  );
127  }
128  else
129  {
130  MapMesh<plusEqOp>
131  (
132  interp,
133  selectedFields,
134  noLagrangian
135  );
136  }
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 int main(int argc, char *argv[])
143 {
144  argList::addNote
145  (
146  "Map volume fields from one mesh to another"
147  );
148 
149  argList::addArgument("sourceCase");
150 
151  argList::addOption
152  (
153  "sourceTime",
154  "scalar|'latestTime'",
155  "Specify the source time"
156  );
157  argList::addOption
158  (
159  "sourceRegion",
160  "word",
161  "Specify the source region"
162  );
163  argList::addOption
164  (
165  "targetRegion",
166  "word",
167  "Specify the target region"
168  );
169  argList::addBoolOption
170  (
171  "consistent",
172  "Source and target geometry and boundary conditions identical"
173  );
174  argList::addOption
175  (
176  "mapMethod",
177  "word",
178  "Specify the mapping method "
179  "(direct|mapNearest|cellVolumeWeight|correctedCellVolumeWeight)"
180  );
181  argList::addOption
182  (
183  "patchMapMethod",
184  "word",
185  "Specify the patch mapping method (direct|mapNearest|faceAreaWeight)"
186  );
187  argList::addOption
188  (
189  "procMapMethod",
190  "word",
191  "Specify the processor distribution map method (AABB|LOD)"
192  );
193  argList::addBoolOption
194  (
195  "subtract",
196  "Subtract mapped source from target"
197  );
198  argList::addOption
199  (
200  "fields",
201  "wordRes",
202  "Specify single or multiple fields to reconstruct (all by default)."
203  " Eg, 'T' or '(p T U \"alpha.*\")'"
204  );
205 
206  argList::addBoolOption
207  (
208  "no-lagrangian", // noLagrangian
209  "Skip mapping lagrangian positions and fields"
210  );
211  argList::addOptionCompat("no-lagrangian", {"noLagrangian", 2106});
212 
213  argList args(argc, argv);
214  #include "foamDlOpenLibs.H"
215 
216  fileName rootDirTarget(args.rootPath());
217  fileName caseDirTarget(args.globalCaseName());
218 
219  const auto casePath = args.get<fileName>(1);
220  const fileName rootDirSource = casePath.path();
221  const fileName caseDirSource = casePath.name();
222 
223  Info<< "Source: " << rootDirSource << " " << caseDirSource << endl;
224  word sourceRegion = polyMesh::defaultRegion;
225  if (args.readIfPresent("sourceRegion", sourceRegion))
226  {
227  Info<< "Source region: " << sourceRegion << endl;
228  }
229 
230  Info<< "Target: " << rootDirTarget << " " << caseDirTarget << endl;
231  word targetRegion = polyMesh::defaultRegion;
232  if (args.readIfPresent("targetRegion", targetRegion))
233  {
234  Info<< "Target region: " << targetRegion << endl;
235  }
236 
237  const bool consistent = args.found("consistent");
238 
239 
240  word mapMethod = meshToMesh::interpolationMethodNames_
241  [
242  meshToMesh::interpolationMethod::imCellVolumeWeight
243  ];
244 
245  if (args.readIfPresent("mapMethod", mapMethod))
246  {
247  Info<< "Mapping method: " << mapMethod << endl;
248  }
249 
250  word patchMapMethod;
251  if (meshToMesh::interpolationMethodNames_.found(mapMethod))
252  {
253  // Lookup corresponding AMI method
254  meshToMesh::interpolationMethod method =
255  meshToMesh::interpolationMethodNames_[mapMethod];
256 
257  patchMapMethod = meshToMesh::interpolationMethodAMI(method);
258  }
259 
260  word procMapMethod =
261  meshToMesh::procMapMethodNames_
262  [
263  meshToMesh::procMapMethod::pmAABB
264  ];
265 
266  if (args.readIfPresent("procMapMethod", procMapMethod))
267  {
268  Info<< "Processor map method: " << procMapMethod << endl;
269  }
270 
271 
272  // Optionally override
273  if (args.readIfPresent("patchMapMethod", patchMapMethod))
274  {
275  Info<< "Patch mapping method: " << patchMapMethod << endl;
276  }
277 
278 
279  if (patchMapMethod.empty())
280  {
282  << "No valid patchMapMethod for method " << mapMethod
283  << ". Please supply one through the 'patchMapMethod' option"
284  << exit(FatalError);
285  }
286 
287  const bool subtract = args.found("subtract");
288  if (subtract)
289  {
290  Info<< "Subtracting mapped source field from target" << endl;
291  }
292 
293  // Non-mandatory
294  const wordRes selectedFields(args.getList<wordRe>("fields", false));
295 
296  const bool noLagrangian = args.found("no-lagrangian");
297 
298  #include "createTimes.H"
299 
300  HashTable<word> patchMap;
301  wordList cuttingPatches;
302 
303  if (!consistent)
304  {
305  IOdictionary mapFieldsDict
306  (
307  IOobject
308  (
309  "mapFieldsDict",
310  runTimeTarget.system(),
312  IOobject::MUST_READ_IF_MODIFIED,
313  IOobject::NO_WRITE,
314  false
315  )
316  );
317 
318  mapFieldsDict.readEntry("patchMap", patchMap);
319  mapFieldsDict.readEntry("cuttingPatches", cuttingPatches);
320  }
321 
322  #include "setTimeIndex.H"
323 
324  Info<< "\nCreate meshes\n" << endl;
325 
326  fvMesh meshSource
327  (
328  IOobject
329  (
330  sourceRegion,
331  runTimeSource.timeName(),
333  )
334  );
335 
336  fvMesh meshTarget
337  (
338  IOobject
339  (
340  targetRegion,
341  runTimeTarget.timeName(),
343  )
344  );
345 
346  Info<< "Source mesh size: " << meshSource.globalData().nTotalCells() << tab
347  << "Target mesh size: " << meshTarget.globalData().nTotalCells()
348  << nl << endl;
349 
350  if (consistent)
351  {
352  mapConsistentMesh
353  (
354  meshSource,
355  meshTarget,
356  mapMethod,
357  patchMapMethod,
358  procMapMethod,
359  subtract,
360  selectedFields,
361  noLagrangian
362  );
363  }
364  else
365  {
366  mapSubMesh
367  (
368  meshSource,
369  meshTarget,
370  patchMap,
371  cuttingPatches,
372  mapMethod,
373  patchMapMethod,
374  procMapMethod,
375  subtract,
376  selectedFields,
377  noLagrangian
378  );
379  }
380 
381  runTimeSource.printExecutionTime(Info);
382 
383  Info<< "\nEnd\n" << endl;
384 
385  return 0;
386 }
387 
388 
389 // ************************************************************************* //
Foam::subtract
void subtract(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:940
meshToMesh.H
Foam::argList::globalCaseName
const fileName & globalCaseName() const noexcept
Return global case name.
Definition: argListI.H:75
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::argList::get
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
foamDlOpenLibs.H
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::FatalError
error FatalError
processorPolyPatch.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
found
bool found
Definition: TABSMDCalcMethod2.H:32
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::nl
constexpr char nl
Definition: Ostream.H:404
runTimeSource
Time runTimeSource(Time::controlDictName, argsSrc)
Foam::argList::getList
List< T > getList(const label index) const
Get a List of values from the argument at index.
fvCFD.H
args
Foam::argList args(argc, argv)
runTimeTarget
Time runTimeTarget(Time::controlDictName, args)
Foam::argList::rootPath
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178