solutionControl.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 | Copyright (C) 2011-2016 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 "solutionControl.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32  defineTypeNameAndDebug(solutionControl, 0);
33 }
34 
35 
36 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
37 
38 void Foam::solutionControl::read(const bool absTolOnly)
39 {
40  const dictionary& solutionDict = this->dict();
41 
42  // Read solution controls
43  nNonOrthCorr_ =
44  solutionDict.lookupOrDefault<label>("nNonOrthogonalCorrectors", 0);
45  momentumPredictor_ =
46  solutionDict.lookupOrDefault("momentumPredictor", true);
47  transonic_ = solutionDict.lookupOrDefault("transonic", false);
48  consistent_ = solutionDict.lookupOrDefault("consistent", false);
49 
50  // Read residual information
51  const dictionary residualDict
52  (
53  solutionDict.subOrEmptyDict("residualControl")
54  );
55 
56  DynamicList<fieldData> data(residualControl_);
57 
58  forAllConstIter(dictionary, residualDict, iter)
59  {
60  const word& fName = iter().keyword();
61  const label fieldi = applyToField(fName, false);
62  if (fieldi == -1)
63  {
64  fieldData fd;
65  fd.name = fName.c_str();
66 
67  if (absTolOnly)
68  {
69  fd.absTol = readScalar(residualDict.lookup(fName));
70  fd.relTol = -1;
71  fd.initialResidual = -1;
72  }
73  else
74  {
75  if (iter().isDict())
76  {
77  const dictionary& fieldDict(iter().dict());
78  fd.absTol = readScalar(fieldDict.lookup("tolerance"));
79  fd.relTol = readScalar(fieldDict.lookup("relTol"));
80  fd.initialResidual = 0.0;
81  }
82  else
83  {
85  << "Residual data for " << iter().keyword()
86  << " must be specified as a dictionary"
87  << exit(FatalError);
88  }
89  }
90 
91  data.append(fd);
92  }
93  else
94  {
95  fieldData& fd = data[fieldi];
96  if (absTolOnly)
97  {
98  fd.absTol = readScalar(residualDict.lookup(fName));
99  }
100  else
101  {
102  if (iter().isDict())
103  {
104  const dictionary& fieldDict(iter().dict());
105  fd.absTol = readScalar(fieldDict.lookup("tolerance"));
106  fd.relTol = readScalar(fieldDict.lookup("relTol"));
107  }
108  else
109  {
111  << "Residual data for " << iter().keyword()
112  << " must be specified as a dictionary"
113  << exit(FatalError);
114  }
115  }
116  }
117  }
118 
119  residualControl_.transfer(data);
120 
121  if (debug)
122  {
123  forAll(residualControl_, i)
124  {
125  const fieldData& fd = residualControl_[i];
126  Info<< "residualControl[" << i << "]:" << nl
127  << " name : " << fd.name << nl
128  << " absTol : " << fd.absTol << nl
129  << " relTol : " << fd.relTol << nl
130  << " iniResid : " << fd.initialResidual << endl;
131  }
132  }
133 }
134 
135 
137 {
138  read(false);
139 }
140 
141 
143 (
144  const word& fieldName,
145  const bool useRegEx
146 ) const
147 {
148  forAll(residualControl_, i)
149  {
150  if (useRegEx && residualControl_[i].name.match(fieldName))
151  {
152  return i;
153  }
154  else if (residualControl_[i].name == fieldName)
155  {
156  return i;
157  }
158  }
159 
160  return -1;
161 }
162 
163 
165 {
166 // storePrevIter<label>();
167  storePrevIter<scalar>();
168  storePrevIter<vector>();
169  storePrevIter<sphericalTensor>();
170  storePrevIter<symmTensor>();
171  storePrevIter<tensor>();
172 }
173 
174 
175 template<class Type>
177 (
178  const word& fieldName,
179  ITstream& data,
180  scalar& firstRes,
181  scalar& lastRes
182 ) const
183 {
185 
186  if (mesh_.foundObject<fieldType>(fieldName))
187  {
188  const List<SolverPerformance<Type>> sp(data);
189  firstRes = cmptMax(sp.first().initialResidual());
190  lastRes = cmptMax(sp.last().initialResidual());
191  }
192 }
193 
194 
196 (
197  const word& fieldName,
198  ITstream& data,
199  scalar& lastRes
200 ) const
201 {
202  scalar firstRes = 0;
203 
204  maxTypeResidual<scalar>(fieldName, data, firstRes, lastRes);
205  maxTypeResidual<vector>(fieldName, data, firstRes, lastRes);
206  maxTypeResidual<sphericalTensor>(fieldName, data, firstRes, lastRes);
207  maxTypeResidual<symmTensor>(fieldName, data, firstRes, lastRes);
208  maxTypeResidual<tensor>(fieldName, data, firstRes, lastRes);
209 
210  return firstRes;
211 }
212 
213 
214 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
215 
216 Foam::solutionControl::solutionControl(fvMesh& mesh, const word& algorithmName)
217 :
218  IOobject
219  (
220  "solutionControl",
221  mesh.time().timeName(),
222  mesh
223  ),
224  mesh_(mesh),
225  residualControl_(),
226  algorithmName_(algorithmName),
227  nNonOrthCorr_(0),
228  momentumPredictor_(true),
229  transonic_(false),
230  consistent_(false),
231  corr_(0),
232  corrNonOrtho_(0)
233 {}
234 
235 
236 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
237 
239 {}
240 
241 
242 // ************************************************************************* //
dictionary dict
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
virtual label applyToField(const word &fieldName, const bool useRegEx=true) const
Return index of field in residualControl_ if present.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Generic GeometricField class.
T & first()
Return the first element of the list.
Definition: UListI.H:114
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:185
void maxTypeResidual(const word &fieldName, ITstream &data, scalar &firstRes, scalar &lastRes) const
bool read(const char *, int32_t &)
Definition: int32IO.C:85
dynamicFvMesh & mesh
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
A class for handling words, derived from string.
Definition: word.H:59
scalar maxResidual(const word &fieldName, ITstream &data, scalar &lastRes) const
word timeName
Definition: getTimeIndex.H:3
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
virtual void read()
Read controls from fvSolution dictionary.
static const char nl
Definition: Ostream.H:262
defineTypeNameAndDebug(combustionModel, 0)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void storePrevIterFields() const
Store previous iteration fields.
virtual ~solutionControl()
Destructor.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
messageStream Info
fvSolution solutionDict(runTime)
T & last()
Return the last element of the list.
Definition: UListI.H:128
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Input token stream.
Definition: ITstream.H:49
dictionary subOrEmptyDict(const word &, const bool mustRead=false) const
Find and return a sub-dictionary as a copy, or.
Definition: dictionary.C:727
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576