gradScheme.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-2022 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 \*---------------------------------------------------------------------------*/
25 
26 #include "fv.H"
27 #include "objectRegistry.H"
28 #include "solution.H"
29 
30 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const fvMesh& mesh,
36  Istream& schemeData
37 )
38 {
39  if (fv::debug)
40  {
41  InfoInFunction << "Constructing gradScheme<Type>" << endl;
42  }
43 
44  if (schemeData.eof())
45  {
47  (
48  schemeData
49  ) << "Grad scheme not specified" << endl << endl
50  << "Valid grad schemes are :" << endl
51  << IstreamConstructorTablePtr_->sortedToc()
52  << exit(FatalIOError);
53  }
54 
55  const word schemeName(schemeData);
56 
57  typename IstreamConstructorTable::iterator cstrIter =
58  IstreamConstructorTablePtr_->find(schemeName);
59 
60  if (cstrIter == IstreamConstructorTablePtr_->end())
61  {
63  (
64  schemeData
65  ) << "Unknown grad scheme " << schemeName << nl << nl
66  << "Valid grad schemes are :" << endl
67  << IstreamConstructorTablePtr_->sortedToc()
68  << exit(FatalIOError);
69  }
70 
71  return cstrIter()(mesh, schemeData);
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
76 
77 template<class Type>
79 {}
80 
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
82 
83 template<class Type>
85 <
87 >
89 (
90  const VolField<Type>& vsf,
91  const word& name
92 ) const
93 {
94  typedef typename outerProduct<vector, Type>::type GradType;
95 
96  if (!this->mesh().changing() && this->mesh().solution().cache(name))
97  {
98  if
99  (
100  !mesh().objectRegistry::template
101  foundObject<VolField<GradType>>(name)
102  )
103  {
104  solution::cachePrintMessage("Calculating and caching", name, vsf);
105  tmp<VolField<GradType>> tgGrad = calcGrad(vsf, name);
106  regIOobject::store(tgGrad.ptr());
107  }
108 
109  solution::cachePrintMessage("Retrieving", name, vsf);
110  VolField<GradType>& gGrad =
111  mesh().objectRegistry::template lookupObjectRef<VolField<GradType>>
112  (
113  name
114  );
115 
116  if (gGrad.upToDate(vsf))
117  {
118  return gGrad;
119  }
120  else
121  {
122  solution::cachePrintMessage("Deleting", name, vsf);
123  gGrad.release();
124  delete &gGrad;
125 
126  solution::cachePrintMessage("Recalculating", name, vsf);
127  tmp<VolField<GradType>> tgGrad = calcGrad(vsf, name);
128 
129  solution::cachePrintMessage("Storing", name, vsf);
130  regIOobject::store(tgGrad.ptr());
131  VolField<GradType>& gGrad =
132  mesh().objectRegistry::template
133  lookupObjectRef<VolField<GradType>>
134  (
135  name
136  );
137 
138  return gGrad;
139  }
140  }
141  else
142  {
143  if
144  (
145  mesh().objectRegistry::template
146  foundObject<VolField<GradType>>(name)
147  )
148  {
149  VolField<GradType>& gGrad =
150  mesh().objectRegistry::template
151  lookupObjectRef<VolField<GradType>>
152  (
153  name
154  );
155 
156  if (gGrad.ownedByRegistry())
157  {
158  solution::cachePrintMessage("Deleting", name, vsf);
159  gGrad.release();
160  delete &gGrad;
161  }
162  }
163 
164  solution::cachePrintMessage("Calculating", name, vsf);
165  return calcGrad(vsf, name);
166  }
167 }
168 
169 
170 template<class Type>
171 Foam::tmp
172 <
174 >
176 (
177  const VolField<Type>& vsf
178 ) const
179 {
180  return grad(vsf, "grad(" + vsf.name() + ')');
181 }
182 
183 
184 template<class Type>
185 Foam::tmp
186 <
188 >
190 (
191  const tmp<VolField<Type>>& tvsf
192 ) const
193 {
194  typedef typename outerProduct<vector, Type>::type GradType;
195 
196  tmp<VolField<GradType>> tgrad = grad(tvsf());
197  tvsf.clear();
198  return tgrad;
199 }
200 
201 
202 // ************************************************************************* //
Generic GeometricField class.
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:99
virtual ~gradScheme()
Destructor.
Definition: gradScheme.C:78
tmp< VolField< typename outerProduct< vector, Type >::type > > grad(const VolField< Type > &, const word &name) const
Calculate and return the grad of the given field.
static tmp< gradScheme< Type > > New(const fvMesh &mesh, Istream &schemeData)
Return a pointer to a new gradScheme created on freestore.
Definition: gradScheme.C:34
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank) >::type type
Definition: products.H:90
void release()
Release ownership of this object from its registry.
Definition: regIOobjectI.H:83
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:329
Selector class for relaxation factors, solver type and solution.
Definition: solution.H:51
A class for managing temporary objects.
Definition: tmp.H:55
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:205
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define InfoInFunction
Report an information message using Foam::Info.
tmp< VolField< typename outerProduct< vector, Type >::type > > grad(const SurfaceField< Type > &ssf)
Definition: fvcGrad.C:46
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488