parFaFieldDistributorCache.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 \*---------------------------------------------------------------------------*/
27 
29 
30 #include "areaFields.H"
31 #include "edgeFields.H"
32 #include "fieldsDistributor.H"
33 #include "faMeshDistributor.H"
34 #include "faMeshSubset.H"
35 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 template<class GeoField>
39 void Foam::parFaFieldDistributorCache::redistributeAndWrite
40 (
41  const faMeshDistributor& distributor,
42  PtrList<GeoField>& fields,
43  const bool isWriteProc
44 )
45 {
46  for (GeoField& fld : fields)
47  {
48  tmp<GeoField> tfld = distributor.distributeField(fld);
49 
50  if (isWriteProc)
51  {
52  tfld().write();
53  }
54  }
55 }
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
61 (
62  const Time& baseRunTime,
63  const fileName& proc0CaseName,
64  const bool decompose, // i.e. read from undecomposed case
65 
66  const boolList& areaMeshOnProc,
67  refPtr<fileOperation>& readHandler,
68  const fileName& areaMeshInstance,
69  faMesh& mesh
70 )
71 {
72  Time& runTime = const_cast<Time&>(mesh.time());
73  const bool oldProcCase = runTime.processorCase();
74 
75  autoPtr<faMeshSubset> subsetterPtr;
76 
77  // Missing an area mesh somewhere?
78  if (areaMeshOnProc.found(false))
79  {
80  const bool oldParRun = UPstream::parRun(false);
81  const int oldCache = fileOperation::cacheLevel(0);
82 
83  // A zero-sized mesh with boundaries.
84  // This is used to create zero-sized fields.
85  subsetterPtr.reset(new faMeshSubset(mesh, zero{}));
86 
87  fileOperation::cacheLevel(oldCache);
88  UPstream::parRun(oldParRun); // Restore parallel state
89 
90  // Deregister from polyMesh ...
91  auto& obr = const_cast<objectRegistry&>
92  (
93  subsetterPtr->subMesh().thisDb()
94  );
95 
96  obr.checkOut(faMesh::typeName);
97  obr.checkOut("faBoundaryMesh");
98  obr.checkOut("faSchemes");
99  obr.checkOut("faSolution");
100  }
101 
102  // Get original objects (before incrementing time!)
103  if (UPstream::master() && decompose)
104  {
105  runTime.caseName() = baseRunTime.caseName();
106  runTime.processorCase(false);
107  }
108 
109  IOobjectList objects;
110 
111  if (readHandler)
112  {
113  auto oldHandler = fileOperation::fileHandler(readHandler);
114  const auto oldComm = UPstream::commWorld(fileHandler().comm());
115 
116  objects = IOobjectList(mesh.thisDb(), runTime.timeName());
117  readHandler = fileOperation::fileHandler(oldHandler);
118  UPstream::commWorld(oldComm);
119  }
120 
121 
122  if (UPstream::master() && decompose)
123  {
124  runTime.caseName() = proc0CaseName;
125  runTime.processorCase(oldProcCase);
126  }
127 
128  Info<< "From time " << runTime.timeName()
129  << " mesh:" << mesh.thisDb().objectRelPath()
130  << " have objects:" << objects.names() << endl;
131 
132  if (UPstream::master() && decompose)
133  {
134  runTime.caseName() = baseRunTime.caseName();
135  runTime.processorCase(false);
136  }
137 
138 
139  #undef doFieldReading
140  #define doFieldReading(Storage) \
141  fieldsDistributor::readFields \
142  ( \
143  areaMeshOnProc, readHandler, mesh, subsetterPtr, objects, \
144  Storage, \
145  true /* (deregister field) */ \
146  );
147 
148  // areaFields
149  doFieldReading(scalarAreaFields_);
150  doFieldReading(vectorAreaFields_);
151  doFieldReading(sphericalTensorAreaFields_);
152  doFieldReading(symmTensorAreaFields_);
153  doFieldReading(tensorAreaFields_);
154 
155  // edgeFields
156  doFieldReading(scalarEdgeFields_);
157  doFieldReading(vectorEdgeFields_);
158  doFieldReading(tensorEdgeFields_);
159  doFieldReading(sphericalTensorEdgeFields_);
160  doFieldReading(symmTensorEdgeFields_);
161  #undef doFieldReading
162 }
163 
164 
165 void Foam::parFaFieldDistributorCache::redistributeAndWrite
166 (
167  const faMeshDistributor& distributor,
168  const bool isWriteProc
169 )
170 {
171  redistributeAndWrite(distributor, scalarAreaFields_, isWriteProc);
172  redistributeAndWrite(distributor, vectorAreaFields_, isWriteProc);
173  redistributeAndWrite(distributor, sphericalTensorAreaFields_, isWriteProc);
174  redistributeAndWrite(distributor, symmTensorAreaFields_, isWriteProc);
175  redistributeAndWrite(distributor, tensorAreaFields_, isWriteProc);
176 
177  redistributeAndWrite(distributor, scalarEdgeFields_, isWriteProc);
178  redistributeAndWrite(distributor, vectorEdgeFields_, isWriteProc);
179  redistributeAndWrite(distributor, sphericalTensorEdgeFields_, isWriteProc);
180  redistributeAndWrite(distributor, symmTensorEdgeFields_, isWriteProc);
181  redistributeAndWrite(distributor, tensorEdgeFields_, isWriteProc);
182 }
183 
184 
185 // ************************************************************************* //
const fileName & caseName() const noexcept
Return case name.
Definition: TimePathsI.H:78
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
bool processorCase() const noexcept
True if this is a processor case.
Definition: TimePathsI.H:52
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
virtual const objectRegistry & thisDb() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:376
dynamicFvMesh & mesh
static int cacheLevel() noexcept
Return cache level.
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds)
Definition: UPstream.H:429
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
fileName objectRelPath() const
The object path relative to the root.
Definition: IOobject.C:524
messageStream Info
Information stream (stdout output on master, null elsewhere)
static const fileOperation & fileHandler()
Return the current file handler. Will create the default file handler if necessary.
void read(const Time &baseRunTime, const fileName &proc0CaseName, const bool decompose, const boolList &areaMeshOnProc, refPtr< fileOperation > &readHandler, const fileName &areaMeshInstance, faMesh &mesh)
Read distributed fields.
List< bool > boolList
A List of bools.
Definition: List.H:60