subCycle.H
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-2020 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 Class
25  Foam::subCycle
26 
27 Description
28  Perform a subCycleTime on a field or list of fields.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #ifndef subCycle_H
33 #define subCycle_H
34 
35 #include "subCycleTime.H"
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 
42 /*---------------------------------------------------------------------------*\
43  Class subCycleField Declaration
44 \*---------------------------------------------------------------------------*/
45 
46 template<class GeometricField>
47 class subCycleField
48 {
49  // Private Data
50 
51  //- Reference to the field being sub-cycled
52  GeometricField& gf_;
53 
54  //- Reference to the field old-time field being sub-cycled
55  // Needed to avoid calls to oldTime() which may cause
56  // unexpected updates of the old-time field
57  GeometricField& gf0_;
58 
59  //- Copy of the "real" old-time value of the field
60  tmp<GeometricField> gf_0_;
61 
62 
63 public:
64 
65  //- Type of the field
66  typedef GeometricField FieldsType;
67 
68  // Constructors
69 
70  //- Construct from field and number of sub-cycles
72  (
73  GeometricField& gf,
74  const label nSubCycles
75  )
76  :
77  gf_(gf),
78  gf0_(gf.oldTime())
79  {
80  if (nSubCycles > 1)
81  {
82  gf_0_ = GeometricField::New(gf0_.name() + "_", gf0_);
83  }
84  }
85 
86 
87  //- Destructor
89  {
90  if (gf_0_.valid())
91  {
92  // Reset the old-time field
93  gf0_ = gf_0_;
94 
95  // Correct the time index of the field
96  // to correspond to the global time
97  gf_.timeIndex() = time().timeIndex();
98  gf0_.timeIndex() = time().timeIndex();
99  }
100  }
101 
102  //- Access to time
103  const Time& time() const
104  {
105  return gf_.time();
106  }
107 
108  //- Correct the time index of the field to correspond to
109  // the sub-cycling time.
110  //
111  // The time index is incremented to protect the old-time value from
112  // being updated at the beginning of the time-loop in the case of
113  // outer iteration
114  void updateTimeIndex()
115  {
116  gf_.timeIndex() = time().timeIndex() + 1;
117  gf0_.timeIndex() = time().timeIndex() + 1;
118  }
119 };
120 
121 
122 /*---------------------------------------------------------------------------*\
123  Class subCycleFields Declaration
124 \*---------------------------------------------------------------------------*/
125 
126 template<class GeometricField>
127 class subCycleFields
128 {
129  // Private Data
130 
131  //- List of pointers to the fields being sub-cycled
132  List<GeometricField*> gfPtrs_;
133 
134  //- List of pointers to the fields old-time field being sub-cycled
135  // Needed to avoid calls to oldTime() which may cause
136  // unexpected updates of the old-time field
137  List<GeometricField*> gf0Ptrs_;
138 
139  //- Copy of the "real" old-time value of the fields
140  PtrList<GeometricField> gf_0Ptrs_;
141 
142 
143 public:
144 
145  //- Type of the list of fields
147 
148  // Constructors
149 
150  //- Construct from field list and number of sub-cycles
152  (
153  List<GeometricField*>& gfPtrs,
154  const label nSubCycles
155  )
156  :
157  gfPtrs_(gfPtrs),
158  gf0Ptrs_(gfPtrs.size())
159  {
160  if (nSubCycles > 1)
161  {
162  gf_0Ptrs_.setSize(gfPtrs.size());
163 
164  forAll(gfPtrs_, i)
165  {
166  gf0Ptrs_[i] = &gfPtrs_[i]->oldTime();
167 
168  gf_0Ptrs_.set
169  (
170  i,
171  new volScalarField
172  (
173  gf0Ptrs_[i]->name() + "_",
174  *gf0Ptrs_[i]
175  )
176  );
177  }
178  }
179  }
180 
181 
182  //- Destructor
183  ~subCycleFields()
184  {
185  if (gf_0Ptrs_.size())
186  {
187  forAll(gfPtrs_, i)
188  {
189  // Reset the old-time fields
190  *gf0Ptrs_[i] = gf_0Ptrs_[i];
191 
192  // Correct the time index of the fields
193  // to correspond to the global time
194  gfPtrs_[i]->timeIndex() = time().timeIndex();
195  gf0Ptrs_[i]->timeIndex() = time().timeIndex();
196  }
197  }
198  }
199 
200  //- Access to time
201  const Time& time() const
202  {
203  return gfPtrs_[0]->time();
204  }
205 
206  //- Correct the time index of the fields to correspond to
207  // the sub-cycling time.
208  //
209  // The time index is incremented to protect the old-time value from
210  // being updated at the beginning of the time-loop in the case of
211  // outer iteration
212  void updateTimeIndex()
213  {
214  forAll(gfPtrs_, i)
215  {
216  gfPtrs_[i]->timeIndex() = time().timeIndex() + 1;
217  gf0Ptrs_[i]->timeIndex() = time().timeIndex() + 1;
218  }
219  }
220 };
221 
222 
223 /*---------------------------------------------------------------------------*\
224  Class subCycle Declaration
225 \*---------------------------------------------------------------------------*/
226 
227 template
228 <
229  class GeometricField,
230  template<class> class SubCycleField = subCycleField
231 >
232 class subCycle
233 :
234  public SubCycleField<GeometricField>,
235  public subCycleTime
236 {
237 
238 public:
239 
240  // Constructors
241 
242  //- Construct field and number of sub-cycles
244  (
245  typename SubCycleField<GeometricField>::FieldsType& gf,
246  const label nSubCycles
247  )
248  :
249  SubCycleField<GeometricField>(gf, nSubCycles),
250  subCycleTime(const_cast<Time&>(this->time()), nSubCycles)
251  {
252  if (nSubCycles > 1)
253  {
254  // Update the field time index to correspond
255  // to the sub-cycle time
256  this->updateTimeIndex();
257  }
258  }
259 
260  //- Disallow default bitwise copy construction
261  subCycle(const subCycle<GeometricField>&) = delete;
262 
263 
264  //- Destructor
265  // End the subCycleTime, which restores the time state
266  ~subCycle()
267  {
268  endSubCycle();
269  }
270 
271 
272  // Member Operators
273 
274  //- Disallow default bitwise assignment
275  void operator=(const subCycle<GeometricField>&) = delete;
276 };
277 
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 } // End namespace Foam
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #endif
286 
287 // ************************************************************************* //
GeometricField FieldsType
Type of the field.
Definition: subCycle.H:65
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
const word & name() const
Return name.
Definition: IOobject.H:303
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
~subCycleField()
Destructor.
Definition: subCycle.H:87
static tmp< GeometricField< Type, PatchField, GeoMesh > > New(const word &name, const Internal &, const PtrList< PatchField< Type >> &)
Return a temporary field constructed from name,.
Generic GeometricField class.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
const Time & time() const
Access to time.
Definition: subCycle.H:102
subCycleField(GeometricField &gf, const label nSubCycles)
Construct from field and number of sub-cycles.
Definition: subCycle.H:71
bool valid() const
Is this temporary object valid,.
Definition: tmpI.H:167
Perform a subCycleTime on a field or list of fields.
Definition: subCycle.H:231
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
label timeIndex() const
Return the time index of the field.
rho oldTime()
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:35
void updateTimeIndex()
Correct the time index of the field to correspond to.
Definition: subCycle.H:113
const Time & time() const
Return time.
Definition: IOobject.C:318
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
A class for managing temporary objects.
Definition: PtrList.H:53
A class for managing sub-cycling times.
Definition: subCycleTime.H:48
Namespace for OpenFOAM.