Commit 4d499d3c authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: basic i-j-k Field container (#1216)

- this is a simple container for fields with i-j-k addressing.

  It does not support field operations directly, but is primarily
  intended to be used when assembling field information with i-j-k
  logic. After assembly, the field can be transferred to a regular
  field for normal operations.  Eg,

      IjkField<scalar> assemble({15, 16, 200});

      // .. fill in i-j-k fields

      Field<scalar> final(std::move(assemble));
      assemble.clear();   // be pedantic
      ...
parent e9323ecb
Test-IjkField.C
EXE = $(FOAM_USER_APPBIN)/Test-IjkField
EXE_INC = -DFULLDEBUG
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Functionality of IjkField
\*---------------------------------------------------------------------------*/
#include "point.H"
#include "IjkField.H"
#include "IOstreams.H"
using namespace Foam;
template<class T>
Ostream& print(const IjkField<T>& fld)
{
Info<< static_cast<const Field<T>&>(fld).size()
<< " " << fld.sizes() << ' ' << flatOutput(fld);
return Info;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
// Create with inconsistent sizes
IjkField<label> field0({1, 2, 3}, identity(10));
IjkField<scalar> field1({3, 4, 5});
IjkField<scalar> field2({2, 3, 3});
forAll(field1, i)
{
field1[i] = -i;
}
forAll(field2, i)
{
field2[i] = i;
}
Info<< "ijk field "; print(field1) << nl;
Info<< "ijk field "; print(field2) << nl;
field1.resize(field2.sizes());
Info<< "resized "; print(field1) << nl;
field1 *= 2;
Info<< "Multiply: "; print(field1) << nl;
field1.resize({1, 2, 3});
Info<< "Resize - shrink: "; print(field1) << nl;
field1.resize({2, 3, 2});
Info<< "Resize - grow: "; print(field1) << nl;
field1.resize({3, 2, 2});
Info<< "Resize - repartition: "; print(field1) << nl;
field1 = field2;
Info<< "Copied: "; print(field1) << nl;
field1 = 3.14159;
Info<< "Assigned: "; print(field1) << nl;
field1 += 3.14159;
Info<< "+= operator: "; print(field1) << nl;
field1 /= 1.2;
Info<< "/= operator: "; print(field1) << nl;
// Field operations are still limited, but we can bypass things too
{
Field<scalar>& tmpField = field1;
tmpField = sqr(tmpField);
Info<< "squared (workaround): "; print(field1) << nl;
}
Info<< nl
<< "Before transfer: addr:" << long(field1.data())
<< " size:" << field1.size() << nl;
Field<scalar> sfield1(std::move(field1));
field1.clear();
Info<< "After transfer to regular field" << nl
<< " source:" << long(field1.data()) << nl
<< " target:" << long(sfield1.data()) << nl
<< "Values"
<< " source:";
print(field1) << nl;
Info<< " target:" << flatOutput(sfield1) << nl;
Info << "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::IjkField<Type>::resize
(
const labelVector& newSizes,
const Type& val
)
{
labelVector& ourSizes = sizes();
if (ijk_.empty() || !cmptProduct(newSizes))
{
// Either/both are empty, can redimension directly
ourSizes = newSizes;
Field<Type>::resize(ijk_.size(), val);
return;
}
const unsigned diffs
(
(ourSizes.x() != newSizes.x() ? 0x100 : 0)
| (ourSizes.y() != newSizes.y() ? 0x010 : 0)
| (ourSizes.z() != newSizes.z() ? 0x001 : 0)
);
switch (diffs)
{
case 0x000:
// No change
return;
break;
case 0x001:
// Change in k only, can redimension directly
ourSizes = newSizes;
Field<Type>::resize(ijk_.size(), val);
return;
break;
case 0x010:
// 2D change in j only, can redimension directly
if (ourSizes.z() == 1)
{
ourSizes = newSizes;
Field<Type>::resize(ijk_.size(), val);
return;
}
break;
}
if ((ourSizes.x()*ourSizes.y()) == newSizes.x()*newSizes.y())
{
// Re-partition i,j with the same size
ourSizes = newSizes;
Field<Type>::resize(ijk_.size(), val);
return;
}
IjkField<Type>& ourContent = *this;
IjkField<Type> newContent(newSizes, val);
// Need new addressing space
const label ni = min(ourSizes.x(), newSizes.x());
const label nj = min(ourSizes.y(), newSizes.y());
const label nk = min(ourSizes.z(), newSizes.z());
for (label k=0; k<nk; ++k)
{
for (label j=0; j<nj; ++j)
{
for (label i=0; i<ni; ++i)
{
newContent(i,j,k) = ourContent(i,j,k);
}
}
}
ourSizes = newSizes;
Field<Type>::transfer(newContent);
}
template<class Type>
void Foam::IjkField<Type>::resize(const labelVector& newSizes)
{
resize(newSizes, Zero);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
void Foam::IjkField<Type>::operator=(const IjkField<Type>& rhs)
{
if (this != &rhs)
{
sizes() = rhs.sizes();
Field<Type>::operator=(rhs);
}
}
template<class Type>
void Foam::IjkField<Type>::operator=(const tmp<IjkField<Type>>& rhs)
{
if (this != &(rhs()))
{
sizes() = rhs().sizes();
Field<Type>::operator=(rhs());
}
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::IjkField
Description
Generic templated field type.
SourceFiles
IjkFieldFunctions.H
IjkFieldFunctionsM.H
IjkFieldMapper.H
IjkFieldI.H
IjkFieldM.H
IjkField.C
IjkFieldFunctions.C
IjkFieldFunctionsM.C
\*---------------------------------------------------------------------------*/
#ifndef IjkField_H
#define IjkField_H
#include "ijkAddressing.H"
#include "Field.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class IjkField Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class IjkField
:
public Field<Type>
{
// Private Data
//- The i-j-k addressing
ijkAddressing ijk_;
//- Copy construct
inline IjkField(const IjkField<Type>& fld);
public:
// Constructors
//- Construct zero-sized
inline IjkField();
//- Construct with sizing information, leaving values uninitialized
inline explicit IjkField(const labelVector& ijk);
//- Construct with sizing information and initial value
inline IjkField(const labelVector& ijk, const Type& val);
//- Construct with sizing information and initial values of zero
inline IjkField(const labelVector& ijk, const zero);
//- Copy construct from components
inline IjkField(const labelVector& ijk, const UList<Type>& list);
//- Move construct from components
inline IjkField(const labelVector& ijk, Field<Type>&& field);
// Member Functions
// Access
//- Return i,j,k addressing
inline const ijkAddressing& ijk() const;
//- Return i,j,k addressing for modification
inline ijkAddressing& ijk();
//- Return i,j,k addressing sizes
inline const labelVector& sizes() const;
//- Return i,j,k addressing sizes for modification
inline labelVector& sizes();
// Edit
//- Clear dimensions and field
inline void clear();
//- Change dimensions. Fill new values with Zero
void resize(const labelVector& newSizes);
//- Change dimensions
void resize(const labelVector& newSizes, const Type& val);
// Access Operators
//- Field access at given i-j-k position
inline const Type& operator()
(
const label i,
const label j,
const label k
) const;
//- Field access at given i-j-k position
inline Type& operator()
(
const label i,
const label j,
const label k
);
//- Field access at given i-j-k position
inline const Type& operator()(const labelVector& ijk) const;
//- Field access at given i-j-k position
inline Type& operator()(const labelVector& ijk);
// Member Operators
//- Copy assignment
void operator=(const IjkField<Type>& rhs);
void operator=(const tmp<IjkField<Type>>& rhs);
//- Move assignment
inline void operator=(IjkField<Type>&& rhs);
//- Value assignment
inline void operator=(const Type& val);
inline void operator=(const zero);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "IjkFieldI.H"
#ifdef NoRepository
#include "IjkField.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::IjkField<Type>::IjkField()
:
Field<Type>(),
ijk_()
{}
template<class Type>
inline Foam::IjkField<Type>::IjkField(const labelVector& ijk)
:
Field<Type>(cmptProduct(ijk)),
ijk_(ijk)
{}
template<class Type>
inline Foam::IjkField<Type>::IjkField
(
const labelVector& ijk,
const Type& val
)
:
Field<Type>(cmptProduct(ijk), val),
ijk_(ijk)
{}
template<class Type>
inline Foam::IjkField<Type>::IjkField
(
const labelVector& ijk,
const zero
)
:
Field<Type>(cmptProduct(ijk), Zero),
ijk_(ijk)
{}
template<class Type>
inline Foam::IjkField<Type>::IjkField
(
const labelVector& ijk,
const UList<Type>& list
)
:
Field<Type>(list),
ijk_(ijk)
{
if (ijk_.size() != Field<Type>::size())
{
#ifdef FULLDEBUG
WarningInFunction
<< "Resizing field to match i-j-k sizing " << sizes()
<< nl << nl;
#endif
Field<Type>::resize(ijk_.size(), Zero);
}
}
template<class Type>
inline Foam::IjkField<Type>::IjkField
(
const labelVector& ijk,
Field<Type>&& field
)
:
Field<Type>(std::move(field)),
ijk_(ijk)
{
if (ijk_.size() != Field<Type>::size())
{
#ifdef FULLDEBUG
WarningInFunction
<< "Resizing field to match i-j-k sizing " << sizes()
<< nl << nl;
#endif
Field<Type>::resize(ijk_.size(), Zero);
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
inline const Foam::ijkAddressing& Foam::IjkField<Type>::ijk() const
{
return ijk_;
}
template<class Type>
inline Foam::ijkAddressing& Foam::IjkField<Type>::ijk()
{
return ijk_;
}
template<class Type>
inline const Foam::labelVector& Foam::IjkField<Type>::sizes() const
{
return ijk_.sizes();
}
template<class Type>
inline Foam::labelVector& Foam::IjkField<Type>::sizes()
{
return ijk_.sizes();
}
template<class Type>
inline void Foam::IjkField<Type>::clear()
{
ijk_.clear();