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-2018 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  argList::addBoolOption
206  (
207  "noLagrangian",
208  "Skip mapping lagrangian positions and fields"
209  );
210 
211  argList args(argc, argv);
212  #include "foamDlOpenLibs.H"
213 
214  fileName rootDirTarget(args.rootPath());
215  fileName caseDirTarget(args.globalCaseName());
216 
217  const fileName casePath = args[1];
218  const fileName rootDirSource = casePath.path();
219  const fileName caseDirSource = casePath.name();
220 
221  Info<< "Source: " << rootDirSource << " " << caseDirSource << endl;
222  word sourceRegion = fvMesh::defaultRegion;
223  if (args.readIfPresent("sourceRegion", sourceRegion))
224  {
225  Info<< "Source region: " << sourceRegion << endl;
226  }
227 
228  Info<< "Target: " << rootDirTarget << " " << caseDirTarget << endl;
229  word targetRegion = fvMesh::defaultRegion;
230  if (args.readIfPresent("targetRegion", targetRegion))
231  {
232  Info<< "Target region: " << targetRegion << endl;
233  }
234 
235  const bool consistent = args.found("consistent");
236 
237 
238  word mapMethod = meshToMesh::interpolationMethodNames_
239  [
240  meshToMesh::interpolationMethod::imCellVolumeWeight
241  ];
242 
243  if (args.readIfPresent("mapMethod", mapMethod))
244  {
245  Info<< "Mapping method: " << mapMethod << endl;
246  }
247 
248  word patchMapMethod;
249  if (meshToMesh::interpolationMethodNames_.found(mapMethod))
250  {
251  // Lookup corresponding AMI method
252  meshToMesh::interpolationMethod method =
253  meshToMesh::interpolationMethodNames_[mapMethod];
254 
255  patchMapMethod = meshToMesh::interpolationMethodAMI(method);
256  }
257 
258  word procMapMethod =
259  meshToMesh::procMapMethodNames_
260  [
261  meshToMesh::procMapMethod::pmAABB
262  ];
263 
264  if (args.readIfPresent("procMapMethod", procMapMethod))
265  {
266  Info<< "Processor map method: " << procMapMethod << endl;
267  }
268 
269 
270  // Optionally override
271  if (args.readIfPresent("patchMapMethod", patchMapMethod))
272  {
273  Info<< "Patch mapping method: " << patchMapMethod << endl;
274  }
275 
276 
277  if (patchMapMethod.empty())
278  {
280  << "No valid patchMapMethod for method " << mapMethod
281  << ". Please supply one through the 'patchMapMethod' option"
282  << exit(FatalError);
283  }
284 
285  const bool subtract = args.found("subtract");
286  if (subtract)
287  {
288  Info<< "Subtracting mapped source field from target" << endl;
289  }
290 
291  // Non-mandatory
292  const wordRes selectedFields(args.getList<wordRe>("fields", false));
293 
294  const bool noLagrangian = args.found("noLagrangian");
295 
296  #include "createTimes.H"
297 
298  HashTable<word> patchMap;
299  wordList cuttingPatches;
300 
301  if (!consistent)
302  {
303  IOdictionary mapFieldsDict
304  (
305  IOobject
306  (
307  "mapFieldsDict",
308  runTimeTarget.system(),
310  IOobject::MUST_READ_IF_MODIFIED,
311  IOobject::NO_WRITE,
312  false
313  )
314  );
315 
316  mapFieldsDict.readEntry("patchMap", patchMap);
317  mapFieldsDict.readEntry("cuttingPatches", cuttingPatches);
318  }
319 
320  #include "setTimeIndex.H"
321 
322  Info<< "\nCreate meshes\n" << endl;
323 
324  fvMesh meshSource
325  (
326  IOobject
327  (
328  sourceRegion,
329  runTimeSource.timeName(),
331  )
332  );
333 
334  fvMesh meshTarget
335  (
336  IOobject
337  (
338  targetRegion,
339  runTimeTarget.timeName(),
341  )
342  );
343 
344  Info<< "Source mesh size: " << meshSource.globalData().nTotalCells() << tab
345  << "Target mesh size: " << meshTarget.globalData().nTotalCells()
346  << nl << endl;
347 
348  if (consistent)
349  {
350  mapConsistentMesh
351  (
352  meshSource,
353  meshTarget,
354  mapMethod,
355  patchMapMethod,
356  procMapMethod,
357  subtract,
358  selectedFields,
359  noLagrangian
360  );
361  }
362  else
363  {
364  mapSubMesh
365  (
366  meshSource,
367  meshTarget,
368  patchMap,
369  cuttingPatches,
370  mapMethod,
371  patchMapMethod,
372  procMapMethod,
373  subtract,
374  selectedFields,
375  noLagrangian
376  );
377  }
378 
379  runTimeSource.printExecutionTime(Info);
380 
381  Info<< "\nEnd\n" << endl;
382 
383  return 0;
384 }
385 
386 
387 // ************************************************************************* //
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
Foam::fileName::name
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:209
meshToMesh.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:302
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:63
foamDlOpenLibs.H
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:59
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::argList::path
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
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:372
Foam::tab
constexpr char tab
Definition: Ostream.H:384
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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::globalCaseName
const fileName & globalCaseName() const
Return global case name.
Definition: argListI.H:75
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157