temporalInterpolate.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-2026 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 Description
25  Interpolate fields between time-steps e.g. for animation.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "argList.H"
30 #include "timeSelector.H"
31 
32 #include "fvMesh.H"
33 #include "Time.H"
34 #include "volFields.H"
35 #include "surfaceFields.H"
36 #include "pointFields.H"
37 #include "ReadFields.H"
38 #include "interpolationWeights.H"
39 #include "uniformInterpolate.H"
40 
41 using namespace Foam;
42 
43 class fieldInterpolator
44 {
45  Time& runTime_;
46  const fvMesh& mesh_;
47  const IOobjectList& objects_;
48  const HashSet<word>& selectedFields_;
49  instant ti_;
50  instant ti1_;
51  const interpolationWeights& interpolator_;
52  const wordList& timeNames_;
53  int divisions_;
54 
55 public:
56 
57  fieldInterpolator
58  (
59  Time& runTime,
60  const fvMesh& mesh,
61  const IOobjectList& objects,
62  const HashSet<word>& selectedFields,
63  const instant& ti,
64  const instant& ti1,
65  const interpolationWeights& interpolator,
66  const wordList& timeNames,
67  int divisions
68  )
69  :
70  runTime_(runTime),
71  mesh_(mesh),
72  objects_(objects),
73  selectedFields_(selectedFields),
74  ti_(ti),
75  ti1_(ti1),
76  interpolator_(interpolator),
77  timeNames_(timeNames),
78  divisions_(divisions)
79  {}
80 
81  template<class GeoFieldType>
82  void interpolate();
83 };
84 
85 
86 template<class GeoFieldType>
88 {
89  const word& fieldClassName = GeoFieldType::typeName;
90 
91  IOobjectList fields = objects_.lookupClass(fieldClassName);
92 
93  if (fields.size())
94  {
95  Info<< " " << fieldClassName << "s:";
96 
98  {
99  if
100  (
101  selectedFields_.empty()
102  || selectedFields_.found(fieldIter()->name())
103  )
104  {
105  Info<< " " << fieldIter()->name() << '(';
106 
107  scalar deltaT = (ti1_.value() - ti_.value())/(divisions_ + 1);
108 
109  for (int j=0; j<divisions_; j++)
110  {
111  instant timej = instant(ti_.value() + (j + 1)*deltaT);
112 
113  runTime_.setTime(instant(timej.name()), 0);
114 
115  Info<< timej.name();
116 
117  if (j < divisions_-1)
118  {
119  Info<< " ";
120  }
121 
122  // Calculate times to read and weights
123  labelList indices;
124  scalarField weights;
125  interpolator_.valueWeights
126  (
127  runTime_.value(),
128  indices,
129  weights
130  );
131 
132  const wordList selectedTimeNames
133  (
134  UIndirectList<word>(timeNames_, indices)()
135  );
136 
137  // Read on the objectRegistry all the required fields
138  ReadFields<GeoFieldType>
139  (
140  fieldIter()->name(),
141  mesh_,
142  selectedTimeNames
143  );
144 
145  GeoFieldType fieldj
146  (
147  uniformInterpolate<GeoFieldType>
148  (
149  IOobject
150  (
151  fieldIter()->name(),
152  runTime_.name(),
153  fieldIter()->db(),
156  false
157  ),
158  fieldIter()->name(),
159  selectedTimeNames,
160  weights
161  )
162  );
163 
164  fieldj.write();
165  }
166 
167  Info<< ')';
168  }
169  }
170 
171  Info<< endl;
172  }
173 }
174 
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 int main(int argc, char *argv[])
179 {
181  #include "addRegionOption.H"
183  (
184  "fields",
185  "list",
186  "specify a list of fields to be interpolated. Eg, '(U T p)' - "
187  "regular expressions not currently supported"
188  );
190  (
191  "divisions",
192  "integer",
193  "specify number of temporal sub-divisions to create (default = 1)."
194  );
196  (
197  "interpolationType",
198  "word",
199  "specify type of interpolation (linear or spline)"
200  );
201 
204 
205  HashSet<word> selectedFields;
206  if (args.optionFound("fields"))
207  {
208  args.optionLookup("fields")() >> selectedFields;
209  }
210  if (selectedFields.size())
211  {
212  Info<< "Interpolating fields " << selectedFields << nl << endl;
213  }
214  else
215  {
216  Info<< "Interpolating all fields" << nl << endl;
217  }
218 
219 
220  int divisions = 1;
221  if (args.optionFound("divisions"))
222  {
223  args.optionLookup("divisions")() >> divisions;
224  }
225  Info<< "Using " << divisions << " per time interval" << nl << endl;
226 
227 
228  const word interpolationType = args.optionLookupOrDefault<word>
229  (
230  "interpolationType",
231  "linear"
232  );
233  Info<< "Using interpolation " << interpolationType << nl << endl;
234 
235 
237 
238  scalarField timeVals(timeDirs.size());
239  wordList timeNames(timeDirs.size());
240  forAll(timeDirs, i)
241  {
242  timeVals[i] = timeDirs[i].value();
243  timeNames[i] = timeDirs[i].name();
244  }
245  autoPtr<interpolationWeights> interpolatorPtr
246  (
248  (
249  interpolationType,
250  timeVals
251  )
252  );
253 
254 
256 
257  Info<< "Interpolating fields for times:" << endl;
258 
259  for (label timei = 0; timei < timeDirs.size() - 1; timei++)
260  {
261  runTime.setTime(timeDirs[timei], timei);
262 
263  // Read objects in time directory
264  IOobjectList objects(mesh, runTime.name());
265 
266  fieldInterpolator interpolator
267  (
268  runTime,
269  mesh,
270  objects,
271  selectedFields,
272  timeDirs[timei],
273  timeDirs[timei+1],
274  interpolatorPtr(),
275  timeNames,
276  divisions
277  );
278 
279  // Interpolate vol fields
280  interpolator.interpolate<volScalarField>();
281  interpolator.interpolate<volVectorField>();
282  interpolator.interpolate<volSphericalTensorField>();
283  interpolator.interpolate<volSymmTensorField>();
284  interpolator.interpolate<volTensorField>();
285 
286  // Interpolate surface fields
287  interpolator.interpolate<surfaceScalarField>();
288  interpolator.interpolate<surfaceVectorField>();
289  interpolator.interpolate<surfaceSphericalTensorField>();
290  interpolator.interpolate<surfaceSymmTensorField>();
291  interpolator.interpolate<surfaceTensorField>();
292  }
293 
294  Info<< "End\n" << endl;
295 
296  return 0;
297 }
298 
299 
300 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Generic GeometricField class.
A HashTable with keys but without contents.
Definition: HashSet.H:62
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A List with indirect addressing.
Definition: UIndirectList.H:61
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:121
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:294
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
const word & name() const
Return reference to name.
Definition: fvMesh.H:447
An instant of time. Contains the time value and name.
Definition: instant.H:67
const word & name() const
Name (const access)
Definition: instant.H:126
Abstract base class for interpolating in 1D.
static autoPtr< interpolationWeights > New(const word &type, const scalarField &samples)
Return a reference to the selected interpolationWeights.
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static instantList timeDirs
Definition: globalFoam.H:44
Info<< "Calculating turbulent flame speed field St\n"<< endl;volScalarField St(IOobject("St", runTime.name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), flameWrinkling->Xi() *Su);multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:234
static tmp< SurfaceField< Type > > interpolate(const VolField< Type > &tvf, const surfaceScalarField &faceFlux, Istream &schemeData)
Interpolate field onto faces using scheme given by Istream.
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
messageStream Info
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:297
objects
Foam::argList args(argc, argv)
Foam::surfaceFields.