Field.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) 2011-2015 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 
26 #include "FieldMapper.H"
27 #include "FieldM.H"
28 #include "dictionary.H"
29 #include "contiguous.H"
30 
31 // * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
32 
33 template<class Type>
34 const char* const Foam::Field<Type>::typeName("Field");
35 
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
39 template<class Type>
41 :
42  List<Type>()
43 {}
44 
45 
46 template<class Type>
48 :
49  List<Type>(size)
50 {}
51 
52 
53 template<class Type>
54 Foam::Field<Type>::Field(const label size, const Type& t)
55 :
56  List<Type>(size, t)
57 {}
58 
59 
60 template<class Type>
62 (
63  const UList<Type>& mapF,
64  const labelUList& mapAddressing
65 )
66 :
67  List<Type>(mapAddressing.size())
68 {
69  map(mapF, mapAddressing);
70 }
71 
72 
73 template<class Type>
75 (
76  const tmp<Field<Type> >& tmapF,
77  const labelUList& mapAddressing
78 )
79 :
80  List<Type>(mapAddressing.size())
81 {
82  map(tmapF, mapAddressing);
83 }
84 
85 
86 template<class Type>
88 (
89  const UList<Type>& mapF,
90  const labelListList& mapAddressing,
91  const scalarListList& mapWeights
92 )
93 :
94  List<Type>(mapAddressing.size())
95 {
96  map(mapF, mapAddressing, mapWeights);
97 }
98 
99 
100 template<class Type>
102 (
103  const tmp<Field<Type> >& tmapF,
104  const labelListList& mapAddressing,
105  const scalarListList& mapWeights
106 )
107 :
108  List<Type>(mapAddressing.size())
109 {
110  map(tmapF, mapAddressing, mapWeights);
111 }
112 
113 
114 template<class Type>
116 (
117  const UList<Type>& mapF,
118  const FieldMapper& mapper
119 )
120 :
121  List<Type>(mapper.size())
122 {
123  map(mapF, mapper);
124 }
125 
126 
127 template<class Type>
129 (
130  const UList<Type>& mapF,
131  const FieldMapper& mapper,
132  const Type& defaultValue
133 )
134 :
135  List<Type>(mapper.size(), defaultValue)
136 {
137  map(mapF, mapper);
138 }
139 
140 
141 template<class Type>
143 (
144  const UList<Type>& mapF,
145  const FieldMapper& mapper,
146  const UList<Type>& defaultValues
147 )
148 :
149  List<Type>(defaultValues)
150 {
151  map(mapF, mapper);
152 }
153 
154 
155 template<class Type>
157 (
158  const tmp<Field<Type> >& tmapF,
159  const FieldMapper& mapper
160 )
161 :
162  List<Type>(mapper.size())
163 {
164  map(tmapF, mapper);
165 }
166 
167 
168 template<class Type>
170 (
171  const tmp<Field<Type> >& tmapF,
172  const FieldMapper& mapper,
173  const Type& defaultValue
174 )
175 :
176  List<Type>(mapper.size(), defaultValue)
177 {
178  map(tmapF, mapper);
179 }
180 
181 
182 template<class Type>
184 (
185  const tmp<Field<Type> >& tmapF,
186  const FieldMapper& mapper,
187  const UList<Type>& defaultValues
188 )
189 :
190  List<Type>(defaultValues)
191 {
192  map(tmapF, mapper);
193 }
194 
195 
196 template<class Type>
198 :
199  refCount(),
200  List<Type>(f)
201 {}
202 
203 
204 template<class Type>
206 :
207  List<Type>(f, reUse)
208 {}
209 
210 
211 template<class Type>
213 :
214  List<Type>(f)
215 {}
216 
217 
218 template<class Type>
220 :
221  List<Type>(f)
222 {}
223 
224 
225 template<class Type>
227 :
228  List<Type>(list)
229 {}
230 
231 
232 // Construct as copy of tmp<Field>
233 #ifndef NoConstructFromTmp
234 template<class Type>
236 :
237  List<Type>(const_cast<Field<Type>&>(tf()), tf.isTmp())
238 {
239  const_cast<Field<Type>&>(tf()).resetRefCount();
240 }
241 #endif
242 
243 
244 template<class Type>
246 :
247  List<Type>(is)
248 {}
249 
250 
251 template<class Type>
253 (
254  const word& keyword,
255  const dictionary& dict,
256  const label s
257 )
258 {
259  if (s)
260  {
261  ITstream& is = dict.lookup(keyword);
262 
263  // Read first token
264  token firstToken(is);
265 
266  if (firstToken.isWord())
267  {
268  if (firstToken.wordToken() == "uniform")
269  {
270  this->setSize(s);
272  }
273  else if (firstToken.wordToken() == "nonuniform")
274  {
275  is >> static_cast<List<Type>&>(*this);
276  if (this->size() != s)
277  {
279  (
280  "Field<Type>::Field"
281  "(const word& keyword, const dictionary&, const label)",
282  dict
283  ) << "size " << this->size()
284  << " is not equal to the given value of " << s
285  << exit(FatalIOError);
286  }
287  }
288  else
289  {
291  (
292  "Field<Type>::Field"
293  "(const word& keyword, const dictionary&, const label)",
294  dict
295  ) << "expected keyword 'uniform' or 'nonuniform', found "
296  << firstToken.wordToken()
297  << exit(FatalIOError);
298  }
299  }
300  else
301  {
302  if (is.version() == 2.0)
303  {
305  (
306  "Field<Type>::Field"
307  "(const word& keyword, const dictionary&, const label)",
308  dict
309  ) << "expected keyword 'uniform' or 'nonuniform', "
310  "assuming deprecated Field format from "
311  "Foam version 2.0." << endl;
312 
313  this->setSize(s);
314 
315  is.putBack(firstToken);
317  }
318  else
319  {
321  (
322  "Field<Type>::Field"
323  "(const word& keyword, const dictionary&, const label)",
324  dict
325  ) << "expected keyword 'uniform' or 'nonuniform', found "
326  << firstToken.info()
327  << exit(FatalIOError);
328  }
329  }
330  }
331 }
332 
333 
334 template<class Type>
336 {
337  return tmp<Field<Type> >(new Field<Type>(*this));
338 }
339 
340 
341 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
342 
343 template<class Type>
345 (
346  const UList<Type>& mapF,
347  const labelUList& mapAddressing
348 )
349 {
350  Field<Type>& f = *this;
351 
352  if (f.size() != mapAddressing.size())
353  {
354  f.setSize(mapAddressing.size());
355  }
356 
357  if (mapF.size() > 0)
358  {
359  forAll(f, i)
360  {
361  label mapI = mapAddressing[i];
362 
363  if (mapI >= 0)
364  {
365  f[i] = mapF[mapI];
366  }
367  }
368  }
369 }
370 
371 
372 template<class Type>
374 (
375  const tmp<Field<Type> >& tmapF,
376  const labelUList& mapAddressing
377 )
378 {
379  map(tmapF(), mapAddressing);
380  tmapF.clear();
381 }
382 
383 
384 template<class Type>
386 (
387  const UList<Type>& mapF,
388  const labelListList& mapAddressing,
389  const scalarListList& mapWeights
390 )
391 {
392  Field<Type>& f = *this;
393 
394  if (f.size() != mapAddressing.size())
395  {
396  f.setSize(mapAddressing.size());
397  }
398 
399  if (mapWeights.size() != mapAddressing.size())
400  {
402  (
403  "void Field<Type>::map\n"
404  "(\n"
405  " const UList<Type>& mapF,\n"
406  " const labelListList& mapAddressing,\n"
407  " const scalarListList& mapWeights\n"
408  ")"
409  ) << "Weights and addressing map have different sizes. Weights size: "
410  << mapWeights.size() << " map size: " << mapAddressing.size()
411  << abort(FatalError);
412  }
413 
414  forAll(f, i)
415  {
416  const labelList& localAddrs = mapAddressing[i];
417  const scalarList& localWeights = mapWeights[i];
418 
419  f[i] = pTraits<Type>::zero;
420 
421  forAll(localAddrs, j)
422  {
423  f[i] += localWeights[j]*mapF[localAddrs[j]];
424  }
425  }
426 }
427 
428 
429 template<class Type>
431 (
432  const tmp<Field<Type> >& tmapF,
433  const labelListList& mapAddressing,
434  const scalarListList& mapWeights
435 )
436 {
437  map(tmapF(), mapAddressing, mapWeights);
438  tmapF.clear();
439 }
440 
441 
442 template<class Type>
444 (
445  const UList<Type>& mapF,
446  const FieldMapper& mapper
447 )
448 {
449  if
450  (
451  mapper.direct()
452  && notNull(mapper.directAddressing())
453  && mapper.directAddressing().size()
454  )
455  {
456  map(mapF, mapper.directAddressing());
457  }
458  else if (!mapper.direct() && mapper.addressing().size())
459  {
460  map(mapF, mapper.addressing(), mapper.weights());
461  }
462 }
463 
464 
465 template<class Type>
467 (
468  const tmp<Field<Type> >& tmapF,
469  const FieldMapper& mapper
470 )
471 {
472  map(tmapF(), mapper);
473  tmapF.clear();
474 }
475 
476 
477 template<class Type>
479 (
480  const FieldMapper& mapper
481 )
482 {
483  if
484  (
485  (
486  mapper.direct()
487  && notNull(mapper.directAddressing())
488  && mapper.directAddressing().size()
489  )
490  || (!mapper.direct() && mapper.addressing().size())
491  )
492  {
493  Field<Type> fCpy(*this);
494  map(fCpy, mapper);
495  }
496  else
497  {
498  this->setSize(mapper.size());
499  }
500 }
501 
502 
503 template<class Type>
505 (
506  const UList<Type>& mapF,
507  const labelUList& mapAddressing
508 )
509 {
510  Field<Type>& f = *this;
511 
512  forAll(mapF, i)
513  {
514  label mapI = mapAddressing[i];
515 
516  if (mapI >= 0)
517  {
518  f[mapI] = mapF[i];
519  }
520  }
521 }
522 
523 
524 template<class Type>
526 (
527  const tmp<Field<Type> >& tmapF,
528  const labelUList& mapAddressing
529 )
530 {
531  rmap(tmapF(), mapAddressing);
532  tmapF.clear();
533 }
534 
535 
536 template<class Type>
538 (
539  const UList<Type>& mapF,
540  const labelUList& mapAddressing,
541  const UList<scalar>& mapWeights
542 )
543 {
544  Field<Type>& f = *this;
545 
547 
548  forAll(mapF, i)
549  {
550  f[mapAddressing[i]] += mapF[i]*mapWeights[i];
551  }
552 }
553 
554 
555 template<class Type>
557 (
558  const tmp<Field<Type> >& tmapF,
559  const labelUList& mapAddressing,
560  const UList<scalar>& mapWeights
561 )
562 {
563  rmap(tmapF(), mapAddressing, mapWeights);
564  tmapF.clear();
565 }
566 
567 
568 template<class Type>
570 {
571  TFOR_ALL_F_OP_OP_F(Type, *this, =, -, Type, *this)
572 }
573 
574 
575 template<class Type>
578 (
579  const direction d
580 ) const
581 {
582  tmp<Field<cmptType> > Component(new Field<cmptType>(this->size()));
583  ::Foam::component(Component(), *this, d);
584  return Component;
585 }
586 
587 
588 template<class Type>
590 (
591  const direction d,
592  const UList<cmptType>& sf
593 )
594 {
595  TFOR_ALL_F_OP_FUNC_S_F(Type, *this, ., replace, const direction, d,
596  cmptType, sf)
597 }
598 
599 
600 template<class Type>
602 (
603  const direction d,
604  const tmp<Field<cmptType> >& tsf
605 )
606 {
607  replace(d, tsf());
608  tsf.clear();
609 }
610 
611 
612 template<class Type>
614 (
615  const direction d,
616  const cmptType& c
617 )
618 {
619  TFOR_ALL_F_OP_FUNC_S_S(Type, *this, ., replace, const direction, d,
620  cmptType, c)
621 }
622 
623 
624 template<class Type>
626 {
627  tmp<Field<Type> > transpose(new Field<Type>(this->size()));
628  ::Foam::T(transpose(), *this);
629  return transpose;
630 }
631 
632 
633 template<class Type>
634 void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const
635 {
636  os.writeKeyword(keyword);
637 
638  bool uniform = false;
639 
640  if (this->size() && contiguous<Type>())
641  {
642  uniform = true;
643 
644  forAll(*this, i)
645  {
646  if (this->operator[](i) != this->operator[](0))
647  {
648  uniform = false;
649  break;
650  }
651  }
652  }
653 
654  if (uniform)
655  {
656  os << "uniform " << this->operator[](0) << token::END_STATEMENT;
657  }
658  else
659  {
660  os << "nonuniform ";
662  os << token::END_STATEMENT;
663  }
664 
665  os << endl;
666 }
667 
668 
669 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
670 
671 template<class Type>
673 {
674  if (this == &rhs)
675  {
676  FatalErrorIn("Field<Type>::operator=(const Field<Type>&)")
677  << "attempted assignment to self"
678  << abort(FatalError);
679  }
680 
682 }
683 
684 
685 template<class Type>
687 {
689 }
690 
691 
692 template<class Type>
694 {
696 }
697 
698 
699 template<class Type>
701 {
702  if (this == &(rhs()))
703  {
704  FatalErrorIn("Field<Type>::operator=(const tmp<Field>&)")
705  << "attempted assignment to self"
706  << abort(FatalError);
707  }
708 
709  // This is dodgy stuff, don't try it at home.
710  Field* fieldPtr = rhs.ptr();
711  List<Type>::transfer(*fieldPtr);
712  delete fieldPtr;
713 }
714 
715 
716 template<class Type>
717 void Foam::Field<Type>::operator=(const Type& t)
718 {
720 }
721 
722 
723 template<class Type>
724 template<class Form, class Cmpt, int nCmpt>
726 {
727  TFOR_ALL_F_OP_S(Type, *this, =, VSType, vs)
728 }
729 
730 
731 #define COMPUTED_ASSIGNMENT(TYPE, op) \
732  \
733 template<class Type> \
734 void Foam::Field<Type>::operator op(const UList<TYPE>& f) \
735 { \
736  TFOR_ALL_F_OP_F(Type, *this, op, TYPE, f) \
737 } \
738  \
739 template<class Type> \
740 void Foam::Field<Type>::operator op(const tmp<Field<TYPE> >& tf) \
741 { \
742  operator op(tf()); \
743  tf.clear(); \
744 } \
745  \
746 template<class Type> \
747 void Foam::Field<Type>::operator op(const TYPE& t) \
748 { \
749  TFOR_ALL_F_OP_S(Type, *this, op, TYPE, t) \
750 }
751 
756 
757 #undef COMPUTED_ASSIGNMENT
758 
759 
760 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
761 
762 template<class Type>
763 Foam::Ostream& Foam::operator<<(Ostream& os, const Field<Type>& f)
764 {
765  os << static_cast<const List<Type>&>(f);
766  return os;
767 }
768 
769 
770 template<class Type>
771 Foam::Ostream& Foam::operator<<(Ostream& os, const tmp<Field<Type> >& tf)
772 {
773  os << tf();
774  tf.clear();
775  return os;
776 }
777 
778 
779 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
780 
781 #include "FieldFunctions.C"
782 
783 // ************************************************************************* //
#define TFOR_ALL_F_OP_FUNC_S_S(typeF1, f1, OP, FUNC, typeS1, s1, typeS2, s2)
Definition: FieldM.H:236
unsigned char direction
Definition: direction.H:43
Pre-declare related SubField type.
Definition: Field.H:61
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:148
#define TFOR_ALL_F_OP_OP_F(typeF1, f1, OP1, OP2, typeF2, f2)
Definition: FieldM.H:338
static const char *const typeName
Definition: Field.H:94
bool isWord() const
Definition: tokenI.H:221
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 ))
#define TFOR_ALL_F_OP_S(typeF, f, OP, typeS, s)
Definition: FieldM.H:356
void writeEntry(Ostream &) const
Write the UList as a dictionary entry.
Definition: UListIO.C:35
Input token stream.
Definition: ITstream.H:49
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
labelList f(nPoints)
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:578
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:43
virtual const scalarListList & weights() const
Definition: FieldMapper.H:88
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
tmp< Field< Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:625
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
A class for handling words, derived from string.
Definition: word.H:59
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
tmp< Field< Type > > clone() const
Clone.
Definition: Field.C:335
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:46
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const word & wordToken() const
Definition: tokenI.H:226
const volScalarField & T
Definition: createFields.H:25
#define IOWarningIn(functionName, ios)
Report an IO warning using Foam::Warning.
void resetRefCount()
Reset the reference count to zero.
Definition: refCount.H:88
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
Type & operator[](const label)
Return element of UList.
void setSize(const label)
Reset size of List.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
#define TFOR_ALL_F_OP_FUNC_S_F(typeF1, f1, OP, FUNC, typeS, s, typeF2, f2)
Definition: FieldM.H:219
void autoMap(const FieldMapper &map)
Map from self.
Definition: Field.C:479
void operator=(const Field< Type > &)
Definition: Field.C:672
void putBack(const token &)
Put back token.
Definition: Istream.C:30
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
pTraits< Type >::cmptType cmptType
Component type.
Definition: Field.H:86
#define forAll(list, i)
Definition: UList.H:421
void writeEntry(const word &keyword, Ostream &os) const
Write the field as a dictionary entry.
Definition: Field.C:634
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
A token holds items read from Istream.
Definition: token.H:67
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:345
#define COMPUTED_ASSIGNMENT(TYPE, op)
Definition: Field.C:731
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Reference counter for various OpenFOAM components.
Definition: refCount.H:45
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
virtual label size() const =0
const tensorField & tf
Template function to specify if the data of a type are contiguous.
virtual const labelUList & directAddressing() const
Definition: FieldMapper.H:70
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
void rmap(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 reverse-map from the given field
Definition: Field.C:505
void negate()
Negate this field.
Definition: Field.C:569
High performance macro functions for Field<Type> algebra. These expand using either array element acc...
Traits class for primitives.
Definition: pTraits.H:50
Field()
Construct null.
Definition: Field.C:40
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
Templated vector space.
Definition: VectorSpace.H:52
label size() const
Return the number of elements in the UList.
virtual const labelListList & addressing() const
Definition: FieldMapper.H:79
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual bool direct() const =0
Uniform/equally-weighted distribution model.
Definition: uniform.H:47
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:590
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
A class for managing temporary objects.
Definition: PtrList.H:118
void operator=(const UList< T > &)
Assignment from UList operator. Takes linear time.
Definition: List.C:429