Loading...
Searching...
No Matches
DimensionedField.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2015-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
30#include "dimensionedType.H"
31
32// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33
34// Check that both fields use the same mesh
35#undef checkField
36#define checkField(fld1, fld2, op) \
37if (&(fld1).mesh() != &(fld2).mesh()) \
38{ \
39 FatalErrorInFunction \
40 << "Different mesh for fields " \
41 << (fld1).name() << " and " << (fld2).name() \
42 << " during operation " << op \
43 << abort(FatalError); \
44}
45
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49template<class Type, class GeoMesh>
50void Foam::DimensionedField<Type, GeoMesh>::checkFieldSize() const
51{
52 const label fieldSize = this->size();
53 if (fieldSize)
54 {
55 const label meshSize = GeoMesh::size(this->mesh_);
56 if (fieldSize != meshSize)
57 {
59 << "size of field = " << fieldSize
60 << " is not the same as the size of mesh = "
61 << meshSize
62 << abort(FatalError);
63 }
64 }
65}
66
67
68// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
69
70template<class Type, class GeoMesh>
72(
73 const IOobject& io,
74 const Mesh& mesh,
75 const dimensionSet& dims,
76 const Field<Type>& field
77)
78:
79 regIOobject(io),
80 DynamicField<Type>(field),
81 mesh_(mesh),
82 dimensions_(dims)
83{
84 checkFieldSize();
85}
86
87
88template<class Type, class GeoMesh>
90(
91 const IOobject& io,
92 const Mesh& mesh,
93 const dimensionSet& dims,
95)
96:
98 DynamicField<Type>(std::move(field)),
99 mesh_(mesh),
100 dimensions_(dims)
101{
102 checkFieldSize();
103}
104
105
106template<class Type, class GeoMesh>
108(
109 const IOobject& io,
110 const Mesh& mesh,
111 const dimensionSet& dims,
113)
114:
116 DynamicField<Type>(std::move(field)),
117 mesh_(mesh),
118 dimensions_(dims)
119{
120 checkFieldSize();
121}
122
123
124template<class Type, class GeoMesh>
126(
127 const IOobject& io,
128 const Mesh& mesh,
129 const dimensionSet& dims,
130 const tmp<Field<Type>>& tfield
131)
132:
134 DynamicField<Type>(tfield.constCast(), tfield.movable()),
135 mesh_(mesh),
136 dimensions_(dims)
138 tfield.clear();
139 checkFieldSize();
140}
141
142
143template<class Type, class GeoMesh>
145(
146 const IOobject& io,
147 const Mesh& mesh,
148 const dimensionSet& dims,
149 const bool checkIOFlags,
150 const bool extraCapacity
151)
152:
154 DynamicField<Type>
155 (
156 // (size,capacity)
157 std::pair<label,label>
158 (
159 GeoMesh::size(mesh),
160 GeoMesh::size(mesh)
161 + (extraCapacity ? GeoMesh::boundary_size(mesh) : label(0))
162 )
163 ),
164 mesh_(mesh),
165 dimensions_(dims)
166{
167 if (checkIOFlags)
169 readIfPresent();
170 }
171}
172
173
174template<class Type, class GeoMesh>
176(
177 const IOobject& io,
178 const Mesh& mesh,
179 const Type& value,
180 const dimensionSet& dims,
181 const bool checkIOFlags,
182 const bool extraCapacity
184:
186 DynamicField<Type>
187 (
188 // (size,capacity)
189 std::pair<label,label>
190 (
191 GeoMesh::size(mesh),
193 + (extraCapacity ? GeoMesh::boundary_size(mesh) : label(0))
194 )
195 ),
196 mesh_(mesh),
197 dimensions_(dims)
198{
199 if (!checkIOFlags || !readIfPresent())
200 {
201 // Set default value (if not read)
202 this->field() = value;
203 }
204}
205
206
207template<class Type, class GeoMesh>
209(
210 const IOobject& io,
211 const Mesh& mesh,
212 const dimensioned<Type>& dt,
213 const bool checkIOFlags,
214 const bool extraCapacity
215)
216:
217 DimensionedField<Type, GeoMesh>
218 (
219 io,
220 mesh,
221 dt.value(),
222 dt.dimensions(),
223 checkIOFlags,
224 extraCapacity
225 )
226{}
227
228
229template<class Type, class GeoMesh>
231(
233)
234:
235 regIOobject(df),
236 DynamicField<Type>(df),
237 mesh_(df.mesh_),
238 dimensions_(df.dimensions_),
239 oriented_(df.oriented_)
240{}
241
242
243template<class Type, class GeoMesh>
245(
248:
249 DimensionedField<Type, GeoMesh>(df, true)
250{}
251
252
253template<class Type, class GeoMesh>
255(
257 bool reuse
258)
259:
260 regIOobject(df, reuse),
261 DynamicField<Type>(df, reuse),
262 mesh_(df.mesh_),
263 dimensions_(df.dimensions_),
264 oriented_(df.oriented_)
265{}
266
267
268template<class Type, class GeoMesh>
270(
272)
273:
274 DimensionedField<Type, GeoMesh>(tdf.constCast(), tdf.movable())
275{
276 tdf.clear();
277}
278
279
280template<class Type, class GeoMesh>
282(
283 const IOobject& io,
285)
286:
288 DynamicField<Type>(df),
289 mesh_(df.mesh_),
290 dimensions_(df.dimensions_),
291 oriented_(df.oriented_)
292{}
293
294
295template<class Type, class GeoMesh>
297(
298 const IOobject& io,
301:
302 DimensionedField<Type, GeoMesh>(io, df, true)
303{}
304
305
306template<class Type, class GeoMesh>
308(
309 const IOobject& io,
311 bool reuse
312)
313:
314 regIOobject(io, df),
315 DynamicField<Type>(df, reuse),
316 mesh_(df.mesh_),
317 dimensions_(df.dimensions_),
318 oriented_(df.oriented_)
319{}
320
321
322template<class Type, class GeoMesh>
324(
325 const IOobject& io,
327)
328:
329 DimensionedField<Type, GeoMesh>(io, tdf.constCast(), tdf.movable())
330{
331 tdf.clear();
332}
333
334
335template<class Type, class GeoMesh>
337(
338 const word& newName,
340)
341:
342 regIOobject(newName, df, newName != df.name()),
343 DynamicField<Type>(df),
344 mesh_(df.mesh_),
345 dimensions_(df.dimensions_),
346 oriented_(df.oriented_)
347{}
348
349
350template<class Type, class GeoMesh>
352(
353 const word& newName,
356:
357 DimensionedField<Type, GeoMesh>(newName, df, true)
358{}
359
360
361template<class Type, class GeoMesh>
363(
364 const word& newName,
366 bool reuse
367)
368:
369 regIOobject(newName, df, true),
370 DynamicField<Type>(df, reuse),
371 mesh_(df.mesh_),
372 dimensions_(df.dimensions_),
373 oriented_(df.oriented_)
374{}
375
376
377template<class Type, class GeoMesh>
379(
380 const word& newName,
382)
383:
384 DimensionedField<Type, GeoMesh>(newName, tdf.constCast(), tdf.movable())
386 tdf.clear();
387}
388
389
390template<class Type, class GeoMesh>
393{
395}
396
397
398// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //
399
400template<class Type, class GeoMesh>
402{
403 // FUTURE: register cache field info
404 // // this->db().cacheTemporaryObject(*this);
405}
406
407
408// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
409
410template<class Type, class GeoMesh>
412<
414 <
416 >
417>
419(
420 const direction d
421) const
422{
424 (
425 name() + ".component(" + ::Foam::name(d) + ')',
426 mesh_,
427 dimensions_
428 );
429
430 Foam::component(tresult.ref(), *this, d);
431
432 return tresult;
433}
434
435
436template<class Type, class GeoMesh>
438(
439 const direction d,
440 const DimensionedField
441 <
443 >& df
444)
445{
447}
448
449
450template<class Type, class GeoMesh>
452(
453 const direction d,
454 const tmp
455 <
456 DimensionedField
457 <
459 >
460 >& tdf
461)
462{
463 replace(d, tdf());
464 tdf.clear();
465}
466
467
468template<class Type, class GeoMesh>
471{
473 (
474 name() + ".T()",
475 mesh_,
476 dimensions_
477 );
478
479 Foam::T(tresult.ref(), *this);
480
481 return tresult;
482}
483
484
485template<class Type, class GeoMesh>
487(
488 const label comm
489) const
490{
491 return
493 (
494 this->name() + ".average()",
495 this->dimensions(),
496 gAverage(this->field(), comm)
497 );
498}
499
500
501template<class Type, class GeoMesh>
503(
505 const label comm
506) const
507{
508 return
510 (
511 this->name() + ".weightedAverage(weights)",
512 this->dimensions(),
513 gWeightedAverage(weights.field(), this->field(), comm)
514 );
515}
516
517
518template<class Type, class GeoMesh>
520(
521 const tmp<DimensionedField<scalar, GeoMesh>>& tweights,
522 const label comm
523) const
524{
525 dimensioned<Type> result = this->weightedAverage(tweights(), comm);
526 tweights.clear();
527 return result;
528}
529
530
531// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
532
533template<class Type, class GeoMesh>
535(
537)
538{
539 if (this == &df)
540 {
541 return; // Self-assignment is a no-op
542 }
543
544 checkField(*this, df, "=");
545
546 dimensions_ = df.dimensions();
547 oriented_ = df.oriented();
549}
550
551
552template<class Type, class GeoMesh>
554(
556)
557{
558 auto& df = tdf.constCast();
559
560 if (this == &df)
561 {
562 return; // Self-assignment is a no-op
563 }
564
565 checkField(*this, df, "=");
566
567 dimensions_ = df.dimensions();
568 oriented_ = df.oriented();
569 this->transfer(df);
570 tdf.clear();
571}
572
573
574template<class Type, class GeoMesh>
576(
577 const dimensioned<Type>& dt
578)
580 dimensions_ = dt.dimensions();
582}
583
584
585template<class Type, class GeoMesh>
587{
589}
590
591
592#define COMPUTED_ASSIGNMENT(TYPE, op) \
593 \
594template<class Type, class GeoMesh> \
595void Foam::DimensionedField<Type, GeoMesh>::operator op \
596( \
597 const DimensionedField<TYPE, GeoMesh>& df \
598) \
599{ \
600 checkField(*this, df, #op); \
601 \
602 dimensions_ op df.dimensions(); \
603 oriented_ op df.oriented(); \
604 DynamicField<Type>::operator op(df); \
605} \
606 \
607template<class Type, class GeoMesh> \
608void Foam::DimensionedField<Type, GeoMesh>::operator op \
609( \
610 const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
611) \
612{ \
613 operator op(tdf()); \
614 tdf.clear(); \
615} \
616 \
617template<class Type, class GeoMesh> \
618void Foam::DimensionedField<Type, GeoMesh>::operator op \
619( \
620 const dimensioned<TYPE>& dt \
621) \
622{ \
623 dimensions_ op dt.dimensions(); \
624 DynamicField<Type>::operator op(dt.value()); \
625}
626
627COMPUTED_ASSIGNMENT(Type, +=)
628COMPUTED_ASSIGNMENT(Type, -=)
629COMPUTED_ASSIGNMENT(scalar, *=)
630COMPUTED_ASSIGNMENT(scalar, /=)
631
632#undef COMPUTED_ASSIGNMENT
633
634// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
635
636#undef checkField
637
638// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
639
640#include "DimensionedFieldIO.C"
641#include "DimensionedFieldNew.C"
643
644// ************************************************************************* //
#define checkField(fld1, fld2, op)
#define COMPUTED_ASSIGNMENT(TYPE, op)
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors).
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
GeoMesh::Mesh Mesh
The mesh type for the DimensionedField.
this_type & constCast() const noexcept
Return non-const reference to this field.
const DynamicField< Type > & field() const noexcept
Return const-reference to the primitive field values.
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction d) const
Return a component field of the field.
const Mesh & mesh() const noexcept
Return const reference to mesh.
const dimensionSet & dimensions() const noexcept
Return dimensions.
void operator=(const DimensionedField< Type, GeoMesh > &df)
void replace(const direction d, const DimensionedField< cmptType, GeoMesh > &df)
Replace a component field of the field.
Field< Type >::cmptType cmptType
Component type of the field elements.
dimensioned< Type > average(const label comm=UPstream::worldComm) const
Return the global arithmetic average.
friend Ostream & operator(Ostream &os, const DimensionedField< Type, GeoMesh > &df)
Calls DimensionedField::writeData().
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &weights, const label comm=UPstream::worldComm) const
Return the global weighted average.
static tmp< DimensionedField< Type, GeoMesh > > New(const word &name, IOobjectOption::registerOption regOpt, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &iField)
Return tmp field (NO_READ, NO_WRITE) from name, mesh, dimensions, copy of internal field....
virtual ~DimensionedField()
Destructor.
Dynamically sized Field. Similar to DynamicList, but inheriting from a Field instead of a List.
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
void transfer(List< Type > &list)
void operator=(const T &val)
Assign addressed entries to the given value.
constexpr DynamicField() noexcept
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
void replace(const direction, const UList< cmptType > &)
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition GeoMesh.H:46
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void size(const label n)
Definition UList.H:118
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Generic dimensioned Type class.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
rDeltaTY field()
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
const auto & io
auto & name
Type gAverage(const FieldField< Field, Type > &f, const label comm)
The global arithmetic average of a FieldField.
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
Type gWeightedAverage(const UList< scalar > &weights, const UList< Type > &fld, const label comm)
The global weighted average of a field, using the mag() of the weights.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Type weightedAverage(const UList< scalar > &weights, const UList< Type > &fld)
The local weighted average of a field, using the mag() of the weights.
uint8_t direction
Definition direction.H:49
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)