timeVaryingMappedFixedValuePointPatchField.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) 2012-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 
27 #include "Time.H"
28 #include "AverageIOField.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
33 Foam::
36 (
37  const pointPatch& p,
39 )
40 :
42  fieldTableName_(iF.name()),
43  setAverage_(false),
44  perturb_(0),
45  mapperPtr_(NULL),
46  sampleTimes_(0),
47  startSampleTime_(-1),
48  startSampledValues_(0),
49  startAverage_(Zero),
50  endSampleTime_(-1),
51  endSampledValues_(0),
52  endAverage_(Zero),
53  offset_()
54 {}
55 
56 
57 template<class Type>
58 Foam::
61 (
63  const pointPatch& p,
65  const pointPatchFieldMapper& mapper
66 )
67 :
68  fixedValuePointPatchField<Type>(ptf, p, iF, mapper),
69  fieldTableName_(ptf.fieldTableName_),
70  setAverage_(ptf.setAverage_),
71  perturb_(ptf.perturb_),
72  mapMethod_(ptf.mapMethod_),
73  mapperPtr_(NULL),
74  sampleTimes_(0),
75  startSampleTime_(-1),
76  startSampledValues_(0),
77  startAverage_(Zero),
78  endSampleTime_(-1),
79  endSampledValues_(0),
80  endAverage_(Zero),
81  offset_
82  (
83  ptf.offset_.valid()
84  ? ptf.offset_().clone().ptr()
85  : NULL
86  )
87 {}
88 
89 
90 template<class Type>
91 Foam::
94 (
95  const pointPatch& p,
97  const dictionary& dict
98 )
99 :
101  fieldTableName_(iF.name()),
102  setAverage_(readBool(dict.lookup("setAverage"))),
103  perturb_(dict.lookupOrDefault("perturb", 1e-5)),
104  mapMethod_
105  (
106  dict.lookupOrDefault<word>
107  (
108  "mapMethod",
109  "planarInterpolation"
110  )
111  ),
112  mapperPtr_(NULL),
113  sampleTimes_(0),
114  startSampleTime_(-1),
115  startSampledValues_(0),
116  startAverage_(Zero),
117  endSampleTime_(-1),
118  endSampledValues_(0),
119  endAverage_(Zero),
120  offset_()
121 {
122  if (dict.found("offset"))
123  {
124  offset_ = Function1<Type>::New("offset", dict);
125  }
126 
127  dict.readIfPresent("fieldTableName", fieldTableName_);
128 
129  if (dict.found("value"))
130  {
132  (
133  Field<Type>("value", dict, p.size())
134  );
135  }
136  else
137  {
138  // Note: use evaluate to do updateCoeffs followed by a reset
139  // of the pointPatchField::updated_ flag. This is
140  // so if first use is in the next time step it retriggers
141  // a new update.
142  pointPatchField<Type>::evaluate(Pstream::blocking);
143  }
144 }
145 
146 
147 template<class Type>
148 Foam::
151 (
153 )
154 :
156  fieldTableName_(ptf.fieldTableName_),
157  setAverage_(ptf.setAverage_),
158  perturb_(ptf.perturb_),
159  mapMethod_(ptf.mapMethod_),
160  mapperPtr_(ptf.mapperPtr_),
161  sampleTimes_(ptf.sampleTimes_),
162  startSampleTime_(ptf.startSampleTime_),
163  startSampledValues_(ptf.startSampledValues_),
164  startAverage_(ptf.startAverage_),
165  endSampleTime_(ptf.endSampleTime_),
166  endSampledValues_(ptf.endSampledValues_),
167  endAverage_(ptf.endAverage_),
168  offset_
169  (
170  ptf.offset_.valid()
171  ? ptf.offset_().clone().ptr()
172  : NULL
173  )
174 {}
175 
176 
177 template<class Type>
178 Foam::
181 (
184 )
185 :
187  fieldTableName_(ptf.fieldTableName_),
188  setAverage_(ptf.setAverage_),
189  perturb_(ptf.perturb_),
190  mapMethod_(ptf.mapMethod_),
191  mapperPtr_(ptf.mapperPtr_),
192  sampleTimes_(ptf.sampleTimes_),
193  startSampleTime_(ptf.startSampleTime_),
194  startSampledValues_(ptf.startSampledValues_),
195  startAverage_(ptf.startAverage_),
196  endSampleTime_(ptf.endSampleTime_),
197  endSampledValues_(ptf.endSampledValues_),
198  endAverage_(ptf.endAverage_),
199  offset_
200  (
201  ptf.offset_.valid()
202  ? ptf.offset_().clone().ptr()
203  : NULL
204  )
205 {}
206 
207 
208 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
209 
210 template<class Type>
212 (
213  const pointPatchFieldMapper& m
214 )
215 {
217  if (startSampledValues_.size())
218  {
219  startSampledValues_.autoMap(m);
220  endSampledValues_.autoMap(m);
221  }
222  // Clear interpolator
223  mapperPtr_.clear();
224  startSampleTime_ = -1;
225  endSampleTime_ = -1;
226 }
227 
228 
229 template<class Type>
231 (
232  const pointPatchField<Type>& ptf,
233  const labelList& addr
234 )
235 {
237 
239  refCast<const timeVaryingMappedFixedValuePointPatchField<Type>>(ptf);
240 
241  startSampledValues_.rmap(tiptf.startSampledValues_, addr);
242  endSampledValues_.rmap(tiptf.endSampledValues_, addr);
243 
244  // Clear interpolator
245  mapperPtr_.clear();
246  startSampleTime_ = -1;
247  endSampleTime_ = -1;
248 }
249 
250 
251 template<class Type>
253 {
254  // Initialise
255  if (startSampleTime_ == -1 && endSampleTime_ == -1)
256  {
257  const polyMesh& pMesh = this->patch().boundaryMesh().mesh()();
258 
259  // Read the initial point position
260  pointField meshPts;
261 
262  if (pMesh.pointsInstance() == pMesh.facesInstance())
263  {
264  meshPts = pointField(pMesh.points(), this->patch().meshPoints());
265  }
266  else
267  {
268  // Load points from facesInstance
269  if (debug)
270  {
271  Info<< "Reloading points0 from " << pMesh.facesInstance()
272  << endl;
273  }
274 
275  pointIOField points0
276  (
277  IOobject
278  (
279  "points",
280  pMesh.facesInstance(),
281  polyMesh::meshSubDir,
282  pMesh,
283  IOobject::MUST_READ,
284  IOobject::NO_WRITE,
285  false
286  )
287  );
288  meshPts = pointField(points0, this->patch().meshPoints());
289  }
290 
291  pointIOField samplePoints
292  (
293  IOobject
294  (
295  "points",
296  this->db().time().constant(),
297  "boundaryData"/this->patch().name(),
298  this->db(),
299  IOobject::MUST_READ,
300  IOobject::AUTO_WRITE,
301  false
302  )
303  );
304 
305  // tbd: run-time selection
306  bool nearestOnly =
307  (
308  !mapMethod_.empty()
309  && mapMethod_ != "planarInterpolation"
310  );
311 
312  // Allocate the interpolator
313  mapperPtr_.reset
314  (
316  (
317  samplePoints,
318  meshPts,
319  perturb_,
320  nearestOnly
321  )
322  );
323 
324  // Read the times for which data is available
325 
326  const fileName samplePointsFile = samplePoints.filePath();
327  const fileName samplePointsDir = samplePointsFile.path();
328  sampleTimes_ = Time::findTimes(samplePointsDir);
329 
330  if (debug)
331  {
332  Info<< "timeVaryingMappedFixedValuePointPatchField : In directory "
333  << samplePointsDir << " found times "
334  << pointToPointPlanarInterpolation::timeNames(sampleTimes_)
335  << endl;
336  }
337  }
338 
339  // Find current time in sampleTimes
340  label lo = -1;
341  label hi = -1;
342 
343  bool foundTime = mapperPtr_().findTime
344  (
345  sampleTimes_,
346  startSampleTime_,
347  this->db().time().value(),
348  lo,
349  hi
350  );
351 
352  if (!foundTime)
353  {
355  << "Cannot find starting sampling values for current time "
356  << this->db().time().value() << nl
357  << "Have sampling values for times "
358  << pointToPointPlanarInterpolation::timeNames(sampleTimes_) << nl
359  << "In directory "
360  << this->db().time().constant()/"boundaryData"/this->patch().name()
361  << "\n on patch " << this->patch().name()
362  << " of field " << fieldTableName_
363  << exit(FatalError);
364  }
365 
366 
367  // Update sampled data fields.
368 
369  if (lo != startSampleTime_)
370  {
371  startSampleTime_ = lo;
372 
373  if (startSampleTime_ == endSampleTime_)
374  {
375  // No need to reread since are end values
376  if (debug)
377  {
378  Pout<< "checkTable : Setting startValues to (already read) "
379  << "boundaryData"
380  /this->patch().name()
381  /sampleTimes_[startSampleTime_].name()
382  << endl;
383  }
384  startSampledValues_ = endSampledValues_;
385  startAverage_ = endAverage_;
386  }
387  else
388  {
389  if (debug)
390  {
391  Pout<< "checkTable : Reading startValues from "
392  << "boundaryData"
393  /this->patch().name()
394  /sampleTimes_[lo].name()
395  << endl;
396  }
397 
398  // Reread values and interpolate
400  (
401  IOobject
402  (
403  fieldTableName_,
404  this->db().time().constant(),
405  "boundaryData"
406  /this->patch().name()
407  /sampleTimes_[startSampleTime_].name(),
408  this->db(),
409  IOobject::MUST_READ,
410  IOobject::AUTO_WRITE,
411  false
412  )
413  );
414 
415  if (vals.size() != mapperPtr_().sourceSize())
416  {
418  << "Number of values (" << vals.size()
419  << ") differs from the number of points ("
420  << mapperPtr_().sourceSize()
421  << ") in file " << vals.objectPath() << exit(FatalError);
422  }
423 
424  startAverage_ = vals.average();
425  startSampledValues_ = mapperPtr_().interpolate(vals);
426  }
427  }
428 
429  if (hi != endSampleTime_)
430  {
431  endSampleTime_ = hi;
432 
433  if (endSampleTime_ == -1)
434  {
435  // endTime no longer valid. Might as well clear endValues.
436  if (debug)
437  {
438  Pout<< "checkTable : Clearing endValues" << endl;
439  }
440  endSampledValues_.clear();
441  }
442  else
443  {
444  if (debug)
445  {
446  Pout<< "checkTable : Reading endValues from "
447  << "boundaryData"
448  /this->patch().name()
449  /sampleTimes_[endSampleTime_].name()
450  << endl;
451  }
452  // Reread values and interpolate
454  (
455  IOobject
456  (
457  fieldTableName_,
458  this->db().time().constant(),
459  "boundaryData"
460  /this->patch().name()
461  /sampleTimes_[endSampleTime_].name(),
462  this->db(),
463  IOobject::MUST_READ,
464  IOobject::AUTO_WRITE,
465  false
466  )
467  );
468 
469  if (vals.size() != mapperPtr_().sourceSize())
470  {
472  << "Number of values (" << vals.size()
473  << ") differs from the number of points ("
474  << mapperPtr_().sourceSize()
475  << ") in file " << vals.objectPath() << exit(FatalError);
476  }
477 
478  endAverage_ = vals.average();
479  endSampledValues_ = mapperPtr_().interpolate(vals);
480  }
481  }
482 }
483 
484 
485 template<class Type>
487 {
488  if (this->updated())
489  {
490  return;
491  }
492 
493  checkTable();
494 
495  // Interpolate between the sampled data
496 
497  Type wantedAverage;
498 
499  if (endSampleTime_ == -1)
500  {
501  // only start value
502  if (debug)
503  {
504  Pout<< "updateCoeffs : Sampled, non-interpolated values"
505  << " from start time:"
506  << sampleTimes_[startSampleTime_].name() << nl;
507  }
508 
509  this->operator==(startSampledValues_);
510  wantedAverage = startAverage_;
511  }
512  else
513  {
514  scalar start = sampleTimes_[startSampleTime_].value();
515  scalar end = sampleTimes_[endSampleTime_].value();
516 
517  scalar s = (this->db().time().value()-start)/(end-start);
518 
519  if (debug)
520  {
521  Pout<< "updateCoeffs : Sampled, interpolated values"
522  << " between start time:"
523  << sampleTimes_[startSampleTime_].name()
524  << " and end time:" << sampleTimes_[endSampleTime_].name()
525  << " with weight:" << s << endl;
526  }
527 
528  this->operator==((1-s)*startSampledValues_ + s*endSampledValues_);
529  wantedAverage = (1-s)*startAverage_ + s*endAverage_;
530  }
531 
532  // Enforce average. Either by scaling (if scaling factor > 0.5) or by
533  // offsetting.
534  if (setAverage_)
535  {
536  const Field<Type>& fld = *this;
537 
538  Type averagePsi = gAverage(fld);
539 
540  if (debug)
541  {
542  Pout<< "updateCoeffs :"
543  << " actual average:" << averagePsi
544  << " wanted average:" << wantedAverage
545  << endl;
546  }
547 
548  if (mag(averagePsi) < VSMALL)
549  {
550  // Field too small to scale. Offset instead.
551  const Type offset = wantedAverage - averagePsi;
552  if (debug)
553  {
554  Pout<< "updateCoeffs :"
555  << " offsetting with:" << offset << endl;
556  }
557  this->operator==(fld+offset);
558  }
559  else
560  {
561  const scalar scale = mag(wantedAverage)/mag(averagePsi);
562 
563  if (debug)
564  {
565  Pout<< "updateCoeffs :"
566  << " scaling with:" << scale << endl;
567  }
568  this->operator==(scale*fld);
569  }
570  }
571 
572  // apply offset to mapped values
573  if (offset_.valid())
574  {
575  const scalar t = this->db().time().timeOutputValue();
576  this->operator==(*this + offset_->value(t));
577  }
578 
579  if (debug)
580  {
581  Pout<< "updateCoeffs : set fixedValue to min:" << gMin(*this)
582  << " max:" << gMax(*this)
583  << " avg:" << gAverage(*this) << endl;
584  }
585 
587 }
588 
589 
590 template<class Type>
592 (
593  Ostream& os
594 ) const
595 {
597  os.writeKeyword("setAverage") << setAverage_ << token::END_STATEMENT << nl;
598  if (perturb_ != 1e-5)
599  {
600  os.writeKeyword("perturb") << perturb_ << token::END_STATEMENT << nl;
601  }
602 
603  if (fieldTableName_ != this->internalField().name())
604  {
605  os.writeKeyword("fieldTableName") << fieldTableName_
606  << token::END_STATEMENT << nl;
607  }
608 
609  if
610  (
611  (
612  !mapMethod_.empty()
613  && mapMethod_ != "planarInterpolation"
614  )
615  )
616  {
617  os.writeKeyword("mapMethod") << mapMethod_
618  << token::END_STATEMENT << nl;
619  }
620 
621  if (offset_.valid())
622  {
623  offset_->writeData(os);
624  }
625 }
626 
627 
628 // ************************************************************************* //
dictionary dict
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
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
virtual void rmap(const pointPatchField< Type > &, const labelList &)
Reverse map the given PointPatchField onto.
A class for handling file names.
Definition: fileName.H:69
void checkTable()
Find boundary data inbetween current time and interpolate.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A time-varying form of a mapped fixed value boundary condition.
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:778
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Type gMin(const FieldField< Field, Type > &f)
A FixedValue boundary condition for pointField.
void size(const label)
Override size to be inconsistent with allocated storage.
A primitive field + average with IO.
Foam::pointPatchFieldMapper.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
bool readBool(Istream &)
Definition: boolIO.C:60
const Type & average() const
timeVaryingMappedFixedValuePointPatchField(const pointPatch &, const DimensionedField< Type, pointMesh > &)
Construct from patch and internal field.
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Abstract base class for point-mesh patch fields.
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
const dimensionedScalar e
Elementary charge.
Definition: doubleFloat.H:78
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< ' ';}gmvFile<< nl;forAll(lagrangianScalarNames, i){const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:299
Pre-declare SubField and related Field type.
Definition: Field.H:57
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
A class for handling words, derived from string.
Definition: word.H:59
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:88
virtual autoPtr< pointPatchField< Type > > clone() const
Construct and return a clone.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
static const zero Zero
Definition: zero.H:91
const polyMesh & mesh() const
Return the mesh reference.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const char nl
Definition: Ostream.H:262
Type gMax(const FieldField< Field, Type > &f)
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Constant dispersed-phase particle diameter model.
Basic pointPatch represents a set of points from the mesh.
Definition: pointPatch.H:56
Type gAverage(const FieldField< Field, Type > &f)
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
volScalarField & p
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
runTime write()
const word & name() const
Return name.
Definition: IOobject.H:260
virtual void autoMap(const pointPatchFieldMapper &)
Map (and resize as needed) from self given a mapping object.
Interpolates between two sets of unstructured points using 2D Delaunay triangulation. Used in e.g. timeVaryingMapped bcs.
label size() const
Return size.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451