averageCondition.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) 2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "averageCondition.H"
28 #include "Time.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(averageCondition, 0);
35  addToRunTimeSelectionTable(runTimeCondition, averageCondition, dictionary);
36 }
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42 (
43  const word& name,
44  const objectRegistry& obr,
45  const dictionary& dict,
46  functionObjectState& state
47 )
48 :
49  runTimeCondition(name, obr, dict, state),
50  functionObjectName_(dict.lookup("functionObjectName")),
51  fieldNames_(dict.lookup("fields")),
52  tolerance_(readScalar(dict.lookup("tolerance"))),
53  window_(dict.lookupOrDefault<scalar>("window", -1)),
54  totalTime_(fieldNames_.size(), obr_.time().deltaTValue()),
55  resetOnRestart_(false)
56 {
57  if (resetOnRestart_)
58  {
59  const dictionary& dict = conditionDict();
60 
61  forAll(fieldNames_, fieldI)
62  {
63  const word& fieldName = fieldNames_[fieldI];
64 
65  if (dict.found(fieldName))
66  {
67  const dictionary& valueDict = dict.subDict(fieldName);
68  totalTime_[fieldI] = readScalar(valueDict.lookup("totalTime"));
69  }
70  }
71  }
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
76 
78 {}
79 
80 
81 // * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
82 
84 {
85  bool satisfied = true;
86 
87  if (!active_)
88  {
89  return satisfied;
90  }
91 
92  scalar dt = obr_.time().deltaTValue();
93 
94  if (log_) Info<< " " << type() << ": " << name_ << " averages:" << nl;
95 
96  DynamicList<label> unprocessedFields(fieldNames_.size());
97 
98  forAll(fieldNames_, fieldI)
99  {
100  const word& fieldName(fieldNames_[fieldI]);
101 
102  scalar Dt = totalTime_[fieldI];
103  scalar alpha = (Dt - dt)/Dt;
104  scalar beta = dt/Dt;
105 
106  if (window_ > 0)
107  {
108  if (Dt - dt >= window_)
109  {
110  alpha = (window_ - dt)/window_;
111  beta = dt/window_;
112  }
113  else
114  {
115  // Ensure that averaging is performed over window time
116  // before condition can be satisfied
117  satisfied = false;
118  }
119  }
120 
121  bool processed = false;
122  calc<scalar>(fieldName, alpha, beta, satisfied, processed);
123  calc<vector>(fieldName, alpha, beta, satisfied, processed);
124  calc<sphericalTensor>(fieldName, alpha, beta, satisfied, processed);
125  calc<symmTensor>(fieldName, alpha, beta, satisfied, processed);
126  calc<tensor>(fieldName, alpha, beta, satisfied, processed);
127 
128  if (!processed)
129  {
130  unprocessedFields.append(fieldI);
131  }
132 
133  totalTime_[fieldI] += dt;
134  }
135 
136  if (unprocessedFields.size())
137  {
139  << "From function object: " << functionObjectName_ << nl
140  << "Unprocessed fields:" << nl;
141 
142  forAll(unprocessedFields, i)
143  {
144  label fieldI = unprocessedFields[i];
145  Info<< " " << fieldNames_[fieldI] << nl;
146  }
147  }
148 
149  if (log_) Info<< endl;
150 
151  return satisfied;
152 }
153 
154 
156 {
157  dictionary& conditionDict = this->conditionDict();
158 
159  forAll(fieldNames_, fieldI)
160  {
161  const word& fieldName = fieldNames_[fieldI];
162 
163  // value dictionary should be present - mean values are written there
164  if (conditionDict.found(fieldName))
165  {
166  dictionary& valueDict = conditionDict.subDict(fieldName);
167  valueDict.add("totalTime", totalTime_[fieldI], true);
168  }
169  else
170  {
171  dictionary valueDict;
172  valueDict.add("totalTime", totalTime_[fieldI], true);
173  conditionDict.add(fieldName, valueDict);
174  }
175  }
176 }
177 
178 
179 // ************************************************************************* //
Foam::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:54
beta
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
Foam::averageCondition::apply
virtual bool apply()
Apply the condition.
Definition: averageCondition.C:83
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::functionObjectState
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: functionObjectState.H:54
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DynamicList< label >
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:216
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::averageCondition::averageCondition
averageCondition(const word &name, const objectRegistry &obr, const dictionary &dict, functionObjectState &state)
Constructor.
Definition: averageCondition.C:42
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::averageCondition::write
virtual void write()
Write.
Definition: averageCondition.C:155
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::label
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
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
averageCondition.H
Foam::averageCondition::~averageCondition
virtual ~averageCondition()
Destructor.
Definition: averageCondition.C:77
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
readScalar
#define readScalar
Definition: doubleScalar.C:38
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729