mapFieldsPar.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  mapFieldsPar
26 
27 Description
28  Maps volume fields from one mesh to another, reading and
29  interpolating all fields present in the time directory of both cases.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #include "fvCFD.H"
34 #include "meshToMesh.H"
35 #include "processorPolyPatch.H"
36 #include "MapMeshes.H"
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 void mapConsistentMesh
41 (
42  const fvMesh& meshSource,
43  const fvMesh& meshTarget,
44  const meshToMesh::interpolationMethod& mapMethod,
45  const bool subtract,
46  const HashSet<word>& selectedFields,
47  const bool noLagrangian
48 )
49 {
50  Info<< nl << "Consistently creating and mapping fields for time "
51  << meshSource.time().timeName() << nl << endl;
52 
53  meshToMesh interp(meshSource, meshTarget, mapMethod);
54 
55  if (subtract)
56  {
57  MapMesh<minusEqOp>
58  (
59  interp,
60  selectedFields,
61  noLagrangian
62  );
63  }
64  else
65  {
66  MapMesh<plusEqOp>
67  (
68  interp,
69  selectedFields,
70  noLagrangian
71  );
72  }
73 }
74 
75 
76 void mapSubMesh
77 (
78  const fvMesh& meshSource,
79  const fvMesh& meshTarget,
80  const HashTable<word>& patchMap,
81  const wordList& cuttingPatches,
82  const meshToMesh::interpolationMethod& mapMethod,
83  const bool subtract,
84  const HashSet<word>& selectedFields,
85  const bool noLagrangian
86 )
87 {
88  Info<< nl << "Creating and mapping fields for time "
89  << meshSource.time().timeName() << nl << endl;
90 
91  meshToMesh interp
92  (
93  meshSource,
94  meshTarget,
95  mapMethod,
96  patchMap,
97  cuttingPatches
98  );
99 
100  if (subtract)
101  {
102  MapMesh<minusEqOp>
103  (
104  interp,
105  selectedFields,
106  noLagrangian
107  );
108  }
109  else
110  {
111  MapMesh<plusEqOp>
112  (
113  interp,
114  selectedFields,
115  noLagrangian
116  );
117  }
118 }
119 
120 
121 wordList addProcessorPatches
122 (
123  const fvMesh& meshTarget,
124  const wordList& cuttingPatches
125 )
126 {
127  // Add the processor patches to the cutting list
128  HashSet<word> cuttingPatchTable;
129  forAll(cuttingPatches, i)
130  {
131  cuttingPatchTable.insert(cuttingPatches[i]);
132  }
133 
134  const polyBoundaryMesh& pbm = meshTarget.boundaryMesh();
135 
136  forAll(pbm, patchi)
137  {
138  if (isA<processorPolyPatch>(pbm[patchi]))
139  {
140  const word& patchName = pbm[patchi].name();
141  cuttingPatchTable.insert(patchName);
142  }
143  }
144 
145  return cuttingPatchTable.toc();
146 }
147 
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 int main(int argc, char *argv[])
152 {
153  argList::addNote
154  (
155  "map volume fields from one mesh to another"
156  );
157 
158  argList::validArgs.append("sourceCase");
159 
160  argList::addOption
161  (
162  "sourceTime",
163  "scalar|'latestTime'",
164  "specify the source time"
165  );
166  argList::addOption
167  (
168  "sourceRegion",
169  "word",
170  "specify the source region"
171  );
172  argList::addOption
173  (
174  "targetRegion",
175  "word",
176  "specify the target region"
177  );
178  argList::addBoolOption
179  (
180  "consistent",
181  "source and target geometry and boundary conditions identical"
182  );
183  argList::addOption
184  (
185  "mapMethod",
186  "word",
187  "specify the mapping method"
188  );
189  argList::addBoolOption
190  (
191  "subtract",
192  "subtract mapped source from target"
193  );
194  argList::addOption
195  (
196  "fields",
197  "list",
198  "specify a list of fields to be mapped. Eg, '(U T p)' - "
199  "regular expressions not currently supported"
200  );
201  argList::addBoolOption
202  (
203  "noLagrangian",
204  "skip mapping lagrangian positions and fields"
205  );
206 
207  argList args(argc, argv);
208 
209  fileName rootDirTarget(args.rootPath());
210  fileName caseDirTarget(args.globalCaseName());
211 
212  const fileName casePath = args[1];
213  const fileName rootDirSource = casePath.path();
214  const fileName caseDirSource = casePath.name();
215 
216  Info<< "Source: " << rootDirSource << " " << caseDirSource << endl;
217  word sourceRegion = fvMesh::defaultRegion;
218  if (args.optionFound("sourceRegion"))
219  {
220  sourceRegion = args["sourceRegion"];
221  Info<< "Source region: " << sourceRegion << endl;
222  }
223 
224  Info<< "Target: " << rootDirTarget << " " << caseDirTarget << endl;
225  word targetRegion = fvMesh::defaultRegion;
226  if (args.optionFound("targetRegion"))
227  {
228  targetRegion = args["targetRegion"];
229  Info<< "Target region: " << targetRegion << endl;
230  }
231 
232  const bool consistent = args.optionFound("consistent");
233 
234  meshToMesh::interpolationMethod mapMethod =
235  meshToMesh::imCellVolumeWeight;
236 
237  if (args.optionFound("mapMethod"))
238  {
239  mapMethod = meshToMesh::interpolationMethodNames_[args["mapMethod"]];
240 
241  Info<< "Mapping method: "
242  << meshToMesh::interpolationMethodNames_[mapMethod] << endl;
243  }
244 
245  const bool subtract = args.optionFound("subtract");
246  if (subtract)
247  {
248  Info<< "Subtracting mapped source field from target" << endl;
249  }
250 
251  HashSet<word> selectedFields;
252  if (args.optionFound("fields"))
253  {
254  args.optionLookup("fields")() >> selectedFields;
255  }
256 
257  const bool noLagrangian = args.optionFound("noLagrangian");
258 
259  #include "createTimes.H"
260 
261  HashTable<word> patchMap;
262  wordList cuttingPatches;
263 
264  if (!consistent)
265  {
266  IOdictionary mapFieldsDict
267  (
268  IOobject
269  (
270  "mapFieldsDict",
271  runTimeTarget.system(),
273  IOobject::MUST_READ_IF_MODIFIED,
274  IOobject::NO_WRITE,
275  false
276  )
277  );
278 
279  mapFieldsDict.lookup("patchMap") >> patchMap;
280  mapFieldsDict.lookup("cuttingPatches") >> cuttingPatches;
281  }
282 
283  #include "setTimeIndex.H"
284 
285  Info<< "\nCreate meshes\n" << endl;
286 
287  fvMesh meshSource
288  (
289  IOobject
290  (
291  sourceRegion,
292  runTimeSource.timeName(),
294  )
295  );
296 
297  fvMesh meshTarget
298  (
299  IOobject
300  (
301  targetRegion,
302  runTimeTarget.timeName(),
304  )
305  );
306 
307  Info<< "Source mesh size: " << meshSource.nCells() << tab
308  << "Target mesh size: " << meshTarget.nCells() << nl << endl;
309 
310  if (consistent)
311  {
312  mapConsistentMesh
313  (
314  meshSource,
315  meshTarget,
316  mapMethod,
317  subtract,
318  selectedFields,
319  noLagrangian
320  );
321  }
322  else
323  {
324  mapSubMesh
325  (
326  meshSource,
327  meshTarget,
328  patchMap,
329  cuttingPatches,
330  mapMethod,
331  subtract,
332  selectedFields,
333  noLagrangian
334  );
335  }
336 
337  Info<< "\nEnd\n" << endl;
338 
339  return 0;
340 }
341 
342 
343 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const fileName & globalCaseName() const
Return case name.
Definition: argListI.H:54
static const char tab
Definition: Ostream.H:259
const fileName & rootPath() const
Return root path.
Definition: argListI.H:42
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
Time runTimeTarget(Time::controlDictName, args)
Time runTimeSource(Time::controlDictName, argsSrc)
word name() const
Return file name (part beyond last /)
Definition: fileName.C:183
static const char nl
Definition: Ostream.H:260
fileName path() const
Return the path to the caseName.
Definition: argListI.H:66
List< word > wordList
A List of words.
Definition: fileName.H:54
label patchi
messageStream Info
Foam::argList args(argc, argv)
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120