LUscalarMatrixTemplates.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-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2025 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 \*---------------------------------------------------------------------------*/
28 
29 #include "LUscalarMatrix.H"
30 #include "SubList.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class Type>
36 (
37  List<Type>& x,
38  const UList<Type>& source
39 ) const
40 {
41  // If x and source are different initialize x = source
42  if (&x != &source)
43  {
44  x = source;
45  }
46 
47  const auto tag = UPstream::msgType();
48 
49  if (UPstream::parRun())
50  {
51  List<Type> allx; // scratch space (on master)
52 
53  const label startOfRequests = UPstream::nRequests();
54 
55  // Like globalIndex::gather()
56  if (UPstream::master(comm_))
57  {
58  allx.resize(m());
59 
60  SubList<Type>(allx, x.size()) = x;
61 
62  for (const int proci : UPstream::subProcs(comm_))
63  {
64  SubList<Type> procSlot
65  (
66  allx,
67  procOffsets_[proci+1]-procOffsets_[proci],
68  procOffsets_[proci]
69  );
70 
71  if (procSlot.empty())
72  {
73  // Nothing to do
74  }
75  else if constexpr (is_contiguous_v<Type>)
76  {
78  (
80  proci,
81  procSlot,
82  tag,
83  comm_
84  );
85  }
86  else
87  {
88  IPstream::recv(procSlot, proci, tag, comm_);
89  }
90  }
91  }
92  else
93  {
94  if (x.empty())
95  {
96  // Nothing to do
97  }
98  else if constexpr (is_contiguous_v<Type>)
99  {
101  (
104  x,
105  tag,
106  comm_
107  );
108  }
109  else
110  {
111  OPstream::send(x, UPstream::masterNo(), tag, comm_);
112  }
113  }
114 
115  UPstream::waitRequests(startOfRequests);
116 
117  // LUBacksubstitute and then like globalIndex::scatter()
118  if (UPstream::master(comm_))
119  {
120  LUBacksubstitute(*this, pivotIndices_, allx);
121 
122  x = SubList<Type>(allx, x.size());
123 
124  for (const int proci : UPstream::subProcs(comm_))
125  {
126  SubList<Type> procSlot
127  (
128  allx,
129  procOffsets_[proci+1]-procOffsets_[proci],
130  procOffsets_[proci]
131  );
132 
133  if (procSlot.empty())
134  {
135  // Nothing to do
136  }
137  else if constexpr (is_contiguous_v<Type>)
138  {
140  (
142  proci,
143  procSlot,
144  tag,
145  comm_
146  );
147  }
148  else
149  {
150  OPstream::send(procSlot, proci, tag, comm_);
151  }
152  }
153  }
154  else
155  {
156  if (x.empty())
157  {
158  // Nothing to do
159  }
160  else if constexpr (is_contiguous_v<Type>)
161  {
163  (
166  x,
167  tag,
168  comm_
169  );
170  }
171  else
172  {
173  IPstream::recv(x, UPstream::masterNo(), tag, comm_);
174  }
175  }
176 
177  UPstream::waitRequests(startOfRequests);
178  }
179  else
180  {
181  LUBacksubstitute(*this, pivotIndices_, x);
182  }
183 }
184 
185 
186 template<class Type>
188 (
189  const UList<Type>& source
190 ) const
191 {
192  auto tx = tmp<Field<Type>>::New(m());
193 
194  solve(tx.ref(), source);
195 
196  return tx;
197 }
198 
199 
200 // ************************************************************************* //
void solve(List< Type > &x, const UList< Type > &source) const
Solve the linear system with the given source.
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:150
static label nRequests() noexcept
Number of outstanding requests (on the internal list of requests)
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:697
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1586
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1787
static std::streamsize read(const UPstream::commsTypes commsType, const int fromProcNo, Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr)
Receive buffer contents (contiguous types) from given processor.
static void waitRequests()
Wait for all requests to finish.
Definition: UPstream.H:2189
static void recv(Type &value, const int fromProcNo, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Receive and deserialize a value. Uses operator>> for de-serialization.
Definition: IPstream.H:80
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition: UPstream.H:1596
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Write buffer contents (contiguous types only) to given processor.
SolverPerformance< Type > solve(faMatrix< Type > &, const dictionary &solverControls)
Solve returning the solution statistics given convergence tolerance.
label m() const noexcept
The number of rows.
Definition: Matrix.H:261
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1619
"nonBlocking" (immediate) : (MPI_Isend, MPI_Irecv)
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:1727
A class for managing temporary objects.
Definition: HashPtrTable.H:50
bool send()
Send buffer contents now and not in destructor [advanced usage]. Returns true on success.
Definition: OPstreams.C:84
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution in the source.