Commit f1ca8946 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: support DimensionedField construction with Xfer

- can be used to reduce copying
parent 32a6c1d9
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -45,6 +45,27 @@ if (&(df1).mesh() != &(df2).mesh()) \
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type, class GeoMesh>
void Foam::DimensionedField<Type, GeoMesh>::checkFieldSize() const
{
const label fieldSize = this->size();
if (fieldSize)
{
const label meshSize = GeoMesh::size(this->mesh_);
if (fieldSize != meshSize)
{
FatalErrorInFunction
<< "size of field = " << fieldSize
<< " is not the same as the size of mesh = "
<< meshSize
<< abort(FatalError);
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type, class GeoMesh>
......@@ -61,14 +82,43 @@ DimensionedField<Type, GeoMesh>::DimensionedField
mesh_(mesh),
dimensions_(dims)
{
if (field.size() && field.size() != GeoMesh::size(mesh))
{
FatalErrorInFunction
<< "size of field = " << field.size()
<< " is not the same as the size of mesh = "
<< GeoMesh::size(mesh)
<< abort(FatalError);
}
checkFieldSize();
}
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField
(
const IOobject& io,
const Mesh& mesh,
const dimensionSet& dims,
const Xfer<Field<Type>>& field
)
:
regIOobject(io),
Field<Type>(field),
mesh_(mesh),
dimensions_(dims)
{
checkFieldSize();
}
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField
(
const IOobject& io,
const Mesh& mesh,
const dimensionSet& dims,
const Xfer<List<Type>>& field
)
:
regIOobject(io),
Field<Type>(field),
mesh_(mesh),
dimensions_(dims)
{
checkFieldSize();
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -53,14 +53,14 @@ template<class Type, class GeoMesh> class DimensionedField;
template<class Type, class GeoMesh> Ostream& operator<<
(
Ostream&,
const DimensionedField<Type, GeoMesh>&
Ostream& os,
const DimensionedField<Type, GeoMesh>& df
);
template<class Type, class GeoMesh> Ostream& operator<<
(
Ostream&,
const tmp<DimensionedField<Type, GeoMesh>>&
Ostream& os,
const tmp<DimensionedField<Type, GeoMesh>>& tdf
);
......@@ -102,6 +102,9 @@ private:
// Private Member Functions
//- Assert that non-zero field size == mesh size
void checkFieldSize() const;
void readIfPresent(const word& fieldDictEntry = "value");
......@@ -122,87 +125,105 @@ public:
//- Construct from components
DimensionedField
(
const IOobject&,
const IOobject& io,
const Mesh& mesh,
const dimensionSet&,
const Field<Type>&
const dimensionSet& dims,
const Field<Type>& field
);
//- Construct from components, transferring the initial field content
DimensionedField
(
const IOobject& io,
const Mesh& mesh,
const dimensionSet& dims,
const Xfer<Field<Type>>& field
);
//- Construct from components, transferring the initial field content
DimensionedField
(
const IOobject& io,
const Mesh& mesh,
const dimensionSet& dims,
const Xfer<List<Type>>& field
);
//- Construct from components
// Used for temporary fields which are initialised after construction
DimensionedField
(
const IOobject&,
const IOobject& io,
const Mesh& mesh,
const dimensionSet&,
const dimensionSet& dims,
const bool checkIOFlags = true
);
//- Construct from components
DimensionedField
(
const IOobject&,
const IOobject& io,
const Mesh& mesh,
const dimensioned<Type>&,
const dimensioned<Type>& dt,
const bool checkIOFlags = true
);
//- Construct from Istream
DimensionedField
(
const IOobject&,
const IOobject& io,
const Mesh& mesh,
const word& fieldDictEntry="value"
const word& fieldDictEntry = "value"
);
//- Construct from dictionary
DimensionedField
(
const IOobject&,
const IOobject& io,
const Mesh& mesh,
const dictionary& fieldDict,
const word& fieldDictEntry="value"
const word& fieldDictEntry = "value"
);
//- Construct as copy
DimensionedField
(
const DimensionedField<Type, GeoMesh>&
const DimensionedField<Type, GeoMesh>& df
);
//- Construct as copy or re-use as specified.
DimensionedField
(
DimensionedField<Type, GeoMesh>&,
DimensionedField<Type, GeoMesh>& df,
bool reuse
);
//- Construct by transferring the DimensionedField
DimensionedField
(
const Xfer<DimensionedField<Type, GeoMesh>>&
const Xfer<DimensionedField<Type, GeoMesh>>& df
);
//- Construct as copy of tmp<DimensionedField> deleting argument
#ifndef NoConstructFromTmp
DimensionedField
(
const tmp<DimensionedField<Type, GeoMesh>>&
const tmp<DimensionedField<Type, GeoMesh>>& tdf
);
#endif
//- Construct as copy resetting IO parameters
DimensionedField
(
const IOobject&,
const DimensionedField<Type, GeoMesh>&
const IOobject& io,
const DimensionedField<Type, GeoMesh>& df
);
//- Construct as copy resetting IO parameters and re-use as specified.
DimensionedField
(
const IOobject&,
DimensionedField<Type, GeoMesh>&,
const IOobject& io,
DimensionedField<Type, GeoMesh>& df,
bool reuse
);
......@@ -210,14 +231,14 @@ public:
DimensionedField
(
const word& newName,
const DimensionedField<Type, GeoMesh>&
const DimensionedField<Type, GeoMesh>& df
);
//- Construct as copy resetting name and re-use as specified.
DimensionedField
(
const word& newName,
DimensionedField<Type, GeoMesh>&,
DimensionedField<Type, GeoMesh>& df,
bool reuse
);
......@@ -225,7 +246,7 @@ public:
DimensionedField
(
const word& newName,
const Xfer<DimensionedField<Type, GeoMesh>>&
const Xfer<DimensionedField<Type, GeoMesh>>& df
);
//- Construct as copy resetting name
......@@ -233,7 +254,7 @@ public:
DimensionedField
(
const word& newName,
const tmp<DimensionedField<Type, GeoMesh>>&
const tmp<DimensionedField<Type, GeoMesh>>& tdf
);
#endif
......@@ -262,28 +283,30 @@ public:
//- Return non-const access to dimensions
inline dimensionSet& dimensions();
//- Return field
inline const Field<Type>& field() const;
//- Return field
inline Field<Type>& field();
//- Return a component field of the field
tmp<DimensionedField<cmptType, GeoMesh>> component
(
const direction
const direction d
) const;
//- Replace a component field of the field
void replace
(
const direction,
const DimensionedField<cmptType, GeoMesh>&
const direction d,
const DimensionedField<cmptType, GeoMesh>& df
);
//- Replace a component field of the field
void replace
(
const direction,
const tmp<DimensionedField<cmptType, GeoMesh>>&
const direction d,
const tmp<DimensionedField<cmptType, GeoMesh>>& tdf
);
//- Return the field transpose (only defined for second rank tensors)
......@@ -295,60 +318,60 @@ public:
//- Calculate and return weighted average
dimensioned<Type> weightedAverage
(
const DimensionedField<scalar, GeoMesh>&
const DimensionedField<scalar, GeoMesh>& weightField
) const;
//- Calculate and return weighted average
dimensioned<Type> weightedAverage
(
const tmp<DimensionedField<scalar, GeoMesh>>&
const tmp<DimensionedField<scalar, GeoMesh>>& tweightField
) const;
// Write
bool writeData(Ostream&, const word& fieldDictEntry) const;
bool writeData(Ostream& os, const word& fieldDictEntry) const;
bool writeData(Ostream&) const;
bool writeData(Ostream& os) const;
// Member Operators
void operator=(const DimensionedField<Type, GeoMesh>&);
void operator=(const tmp<DimensionedField<Type, GeoMesh>>&);
void operator=(const dimensioned<Type>&);
void operator=(const DimensionedField<Type, GeoMesh>& df);
void operator=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
void operator=(const dimensioned<Type>& dt);
void operator+=(const DimensionedField<Type, GeoMesh>&);
void operator+=(const tmp<DimensionedField<Type, GeoMesh>>&);
void operator+=(const DimensionedField<Type, GeoMesh>& df);
void operator+=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
void operator-=(const DimensionedField<Type, GeoMesh>&);
void operator-=(const tmp<DimensionedField<Type, GeoMesh>>&);
void operator-=(const DimensionedField<Type, GeoMesh>& df);
void operator-=(const tmp<DimensionedField<Type, GeoMesh>>& tdf);
void operator*=(const DimensionedField<scalar, GeoMesh>&);
void operator*=(const tmp<DimensionedField<scalar, GeoMesh>>&);
void operator*=(const DimensionedField<scalar, GeoMesh>& df);
void operator*=(const tmp<DimensionedField<scalar, GeoMesh>>& tdf);
void operator/=(const DimensionedField<scalar, GeoMesh>&);
void operator/=(const tmp<DimensionedField<scalar, GeoMesh>>&);
void operator/=(const DimensionedField<scalar, GeoMesh>& df);
void operator/=(const tmp<DimensionedField<scalar, GeoMesh>>& tdf);
void operator+=(const dimensioned<Type>&);
void operator-=(const dimensioned<Type>&);
void operator+=(const dimensioned<Type>& dt);
void operator-=(const dimensioned<Type>& dt);
void operator*=(const dimensioned<scalar>&);
void operator/=(const dimensioned<scalar>&);
void operator*=(const dimensioned<scalar>& dt);
void operator/=(const dimensioned<scalar>& dt);
// Ostream Operators
friend Ostream& operator<< <Type, GeoMesh>
(
Ostream&,
const DimensionedField<Type, GeoMesh>&
Ostream& os,
const DimensionedField<Type, GeoMesh>& df
);
friend Ostream& operator<< <Type, GeoMesh>
(
Ostream&,
const tmp<DimensionedField<Type, GeoMesh>>&
Ostream& os,
const tmp<DimensionedField<Type, GeoMesh>>& tdf
);
};
......
......@@ -194,7 +194,7 @@ tmp<DimensionedField<ReturnType, GeoMesh>> operator Op \
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define BINARY_TYPE_OPERATOR_SF(ReturnType, Type1, Type2, Op, OpName, OpFunc) \
#define BINARY_TYPE_OPERATOR_SF(ReturnType, Type1, Type2, Op, OpName, OpFunc) \
\
TEMPLATE \
tmp<DimensionedField<ReturnType, GeoMesh>> operator Op \
......@@ -225,7 +225,7 @@ tmp<DimensionedField<ReturnType, GeoMesh>> operator Op \
);
#define BINARY_TYPE_OPERATOR_FS(ReturnType, Type1, Type2, Op, OpName, OpFunc) \
#define BINARY_TYPE_OPERATOR_FS(ReturnType, Type1, Type2, Op, OpName, OpFunc) \
\
TEMPLATE \
tmp<DimensionedField<ReturnType, GeoMesh>> operator Op \
......
......@@ -105,7 +105,7 @@ public:
//- Return a component field of the field
inline tmp<DimensionedField<cmptType, GeoMesh>> component
(
const direction
const direction d
) const;
//- Return the field transpose (only defined for second rank tensors)
......@@ -115,7 +115,7 @@ public:
// Member operators
//- Assignment
inline void operator=(const SubDimensionedField<Type, GeoMesh>&);
inline void operator=(const SubDimensionedField<Type, GeoMesh>& rhs);
//- Allow cast to a const DimensionedField<Type, GeoMesh>&
inline operator const DimensionedField<Type, GeoMesh>&() const;
......
......@@ -93,20 +93,23 @@ class Xfer
public:
typedef T Type;
// Constructors
//- Store object pointer and manage its deletion
// Can also be used later to transfer by assignment
inline explicit Xfer(T* = 0);
inline explicit Xfer(T* p = 0);
//- Construct by copying or by transferring the parameter contents
inline explicit Xfer(T&, bool allowTransfer=false);
inline explicit Xfer(T& t, bool allowTransfer=false);
//- Construct by copying the parameter contents
inline explicit Xfer(const T&);
inline explicit Xfer(const T& t);
//- Construct by transferring the contents
inline Xfer(const Xfer<T>&);
inline Xfer(const Xfer<T>& t);
//- Destructor
......@@ -122,10 +125,10 @@ public:
// Member Operators
//- Transfer the contents into the object
inline void operator=(T&);
inline void operator=(T& t);
//- Transfer the contents into the object
inline void operator=(const Xfer<T>&);
inline void operator=(const Xfer<T>& t);
//- Reference to the underlying datatype
inline T& operator()() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment