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-2019 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  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  gf_(gf),
74  gf0_(gf.oldTime()),
75  gf_0_(gf0_.name() + "_", gf0_)
76  {}
77 
78 
79  //- Destructor
81  {
82  // Reset the old-time field
83  gf0_ = gf_0_;
84 
85  // Correct the time index of the field to correspond to the global time
86  gf_.timeIndex() = time().timeIndex();
87  gf0_.timeIndex() = time().timeIndex();
88  }
89 
90  //- Access to time
91  const Time& time() const
92  {
93  return gf_.time();
94  }
95 
96  //- Correct the time index of the field to correspond to
97  // the sub-cycling time.
98  //
99  // The time index is incremented to protect the old-time value from
100  // being updated at the beginning of the time-loop in the case of
101  // outer iteration
102  void updateTimeIndex()
103  {
104  gf_.timeIndex() = time().timeIndex() + 1;
105  gf0_.timeIndex() = time().timeIndex() + 1;
106  }
107 };
108 
109 
110 /*---------------------------------------------------------------------------*\
111  Class subCycleFields Declaration
112 \*---------------------------------------------------------------------------*/
113 
114 template<class GeometricField>
115 class subCycleFields
116 {
117  // Private Data
118 
119  //- List of pointers to the fields being sub-cycled
120  List<GeometricField*> gfPtrs_;
121 
122  //- List of pointers to the fields old-time field being sub-cycled
123  // Needed to avoid calls to oldTime() which may cause
124  // unexpected updates of the old-time field
125  List<GeometricField*> gf0Ptrs_;
126 
127  //- Copy of the "real" old-time value of the fields
128  PtrList<GeometricField> gf_0Ptrs_;
129 
130 
131 public:
132 
133  //- Type of the list of fields
135 
136  // Constructors
137 
138  //- Construct from field list and number of sub-cycles
140  :
141  gfPtrs_(gfPtrs),
142  gf0Ptrs_(gfPtrs.size()),
143  gf_0Ptrs_(gfPtrs.size())
144  {
145  forAll(gfPtrs_, i)
146  {
147  gf0Ptrs_[i] = &gfPtrs_[i]->oldTime();
148 
149  gf_0Ptrs_.set
150  (
151  i,
152  new volScalarField
153  (
154  gf0Ptrs_[i]->name() + "_",
155  *gf0Ptrs_[i]
156  )
157  );
158  }
159  }
160 
161 
162  //- Destructor
163  ~subCycleFields()
164  {
165  forAll(gfPtrs_, i)
166  {
167  // Reset the old-time fields
168  *gf0Ptrs_[i] = gf_0Ptrs_[i];
169 
170  // Correct the time index of the fields
171  // to correspond to the global time
172  gfPtrs_[i]->timeIndex() = time().timeIndex();
173  gf0Ptrs_[i]->timeIndex() = time().timeIndex();
174  }
175  }
176 
177  //- Access to time
178  const Time& time() const
179  {
180  return gfPtrs_[0]->time();
181  }
182 
183  //- Correct the time index of the fields to correspond to
184  // the sub-cycling time.
185  //
186  // The time index is incremented to protect the old-time value from
187  // being updated at the beginning of the time-loop in the case of
188  // outer iteration
189  void updateTimeIndex()
190  {
191  forAll(gfPtrs_, i)
192  {
193  gfPtrs_[i]->timeIndex() = time().timeIndex() + 1;
194  gf0Ptrs_[i]->timeIndex() = time().timeIndex() + 1;
195  }
196  }
197 };
198 
199 
200 /*---------------------------------------------------------------------------*\
201  Class subCycle Declaration
202 \*---------------------------------------------------------------------------*/
203 
204 template
205 <
206  class GeometricField,
207  template<class> class SubCycleField = subCycleField
208 >
209 class subCycle
210 :
211  public SubCycleField<GeometricField>,
212  public subCycleTime
213 {
214 
215 public:
216 
217  // Constructors
218 
219  //- Construct field and number of sub-cycles
221  (
222  typename SubCycleField<GeometricField>::FieldsType& gf,
223  const label nSubCycles
224  )
225  :
226  SubCycleField<GeometricField>(gf),
227  subCycleTime(const_cast<Time&>(this->time()), nSubCycles)
228  {
229  // Update the field time index to correspond to the sub-cycle time
230  this->updateTimeIndex();
231  }
232 
233  //- Disallow default bitwise copy construction
234  subCycle(const subCycle<GeometricField>&) = delete;
235 
236 
237  //- Destructor
238  // End the subCycleTime, which restores the time state
239  ~subCycle()
240  {
241  endSubCycle();
242  }
243 
244 
245  // Member Operators
246 
247  //- Disallow default bitwise assignment
248  void operator=(const subCycle<GeometricField>&) = delete;
249 };
250 
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 } // End namespace Foam
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
258 #endif
259 
260 // ************************************************************************* //
GeometricField FieldsType
Type of the field.
Definition: subCycle.H:65
rho oldTime()
#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
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
~subCycleField()
Destructor.
Definition: subCycle.H:79
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:90
Perform a subCycleTime on a field or list of fields.
Definition: subCycle.H:208
subCycleField(GeometricField &gf)
Construct from field and number of sub-cycles.
Definition: subCycle.H:70
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.
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:101
const Time & time() const
Return time.
Definition: IOobject.C:360
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 sub-cycling times.
Definition: subCycleTime.H:48
Namespace for OpenFOAM.