diff --git a/src/field/AddToStorage.h b/src/field/AddToStorage.h index dbb0b947d21d02133a4125808c99141edc110151..add0eb16d82095f1134245b36b8750759d45683a 100644 --- a/src/field/AddToStorage.h +++ b/src/field/AddToStorage.h @@ -246,6 +246,36 @@ BlockDataID addCloneToStorage( const shared_ptr< BlockStorage_T > & blocks, +//********************************************************************************************************************** +/*! Adds a flattened shallow copy of an existing field to BlockStorage +* +* Template parameters: +* Field_T the type of the field that should be cloned and flattened +* BlockStorage_T the type of the BlockStorage ( will be deduced automatically ) +* +* Parameters: +* \param blocks BlockStorage where the original field is stored and the new one is created +* \param fieldToClone BlockDataID of the Field that is cloned +* \param identifier name for new the field ( displayed in GUI and debugging functions ) +*/ +//********************************************************************************************************************** +template< typename Field_T, typename BlockStorage_T > +BlockDataID addFlattenedShallowCopyToStorage( const shared_ptr< BlockStorage_T > & blocks, + ConstBlockDataID fieldToClone, + const std::string & identifier, + const Set<SUID> & requiredSelectors = Set<SUID>::emptySet(), + const Set<SUID> & incompatibleSelectors = Set<SUID>::emptySet() ) +{ + return blocks->addBlockData( make_shared< field::FlattenedShallowCopyBlockDataHandling< Field_T > >( fieldToClone ), + identifier, requiredSelectors, incompatibleSelectors ); +} + + + + + + + //********************************************************************************************************************** /*! BlockDataCreator for fields * diff --git a/src/field/blockforest/BlockDataHandling.h b/src/field/blockforest/BlockDataHandling.h index 49968c760379b1ac99ff0ccee3b194562b41b583..711259355328eef82854266eee12326e8da103b6 100644 --- a/src/field/blockforest/BlockDataHandling.h +++ b/src/field/blockforest/BlockDataHandling.h @@ -538,5 +538,31 @@ private: + + + +template< typename Field_T > +class FlattenedShallowCopyBlockDataHandling : public blockforest::AlwaysInitializeBlockDataHandling< typename Field_T::FlattenedGhostLayerField > +{ +public: + + FlattenedShallowCopyBlockDataHandling( const ConstBlockDataID & fieldToClone ) : + fieldToClone_( fieldToClone ) + {} + + typename Field_T::FlattenedGhostLayerField * initialize( IBlock * const block ) + { + const Field_T * toClone = block->template getData< Field_T >( fieldToClone_ ); + return toClone->flattenedShallowCopy(); + } + +private: + + ConstBlockDataID fieldToClone_; + +}; // class FlattenedShallowCopyBlockDataHandling + + + } // namespace field } // namespace walberla diff --git a/tests/field/AddToStorageTest.cpp b/tests/field/AddToStorageTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2117f7557c8720e94a1347aa6bea42843e12f6fc --- /dev/null +++ b/tests/field/AddToStorageTest.cpp @@ -0,0 +1,88 @@ +//====================================================================================================================== +// +// This file is part of waLBerla. waLBerla 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. +// +// waLBerla 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 waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. +// +//! \file CollectTest.h +//! \author Martin Bauer <martin.bauer@fau.de> +// +//====================================================================================================================== + + +#include "core/debug/TestSubsystem.h" +#include "core/Environment.h" + +#include "blockforest/Initialization.h" + +#include "field/AddToStorage.h" + + +namespace walberla { + + +int main( int argc, char ** argv ) +{ + debug::enterTestMode(); + walberla::Environment walberlaEnv( argc, argv ); + + auto blocks = blockforest::createUniformBlockGrid( 2, 2, 2, // blocks in x,y,z + 4, 4, 4, // nr of cells per block + 1.0, // dx + false + ); + typedef GhostLayerField<Vector3<uint_t>,1> VectorField; + typedef GhostLayerField<uint_t, 3> FlattenedField; + BlockDataID fieldID = field::addToStorage<VectorField>( blocks, "Field" ); + BlockDataID flattenedID = field::addFlattenedShallowCopyToStorage<VectorField>( blocks, fieldID, "flattened Field"); + + for( auto blockIt = blocks->begin(); blockIt != blocks->end(); ++blockIt ) + { + VectorField * field = blockIt->getData<VectorField>( fieldID ); + + for( auto cellIt = field->beginXYZ(); cellIt != field->end(); ++cellIt ) + { + for( uint_t f = 0; f < 3; ++f) + { + uint_t val = uint_t(&(*cellIt)[f]); + (*cellIt)[f] = val; + } + } + } + + BlockDataID copyID = field::addCloneToStorage<VectorField>( blocks, fieldID, "copied Field"); + + for( auto blockIt = blocks->begin(); blockIt != blocks->end(); ++blockIt ) + { + VectorField * field = blockIt->getData<VectorField>( fieldID ); + FlattenedField * flattened = blockIt->getData<FlattenedField>( flattenedID ); + VectorField * copy = blockIt->getData<VectorField>( copyID ); + + for( auto cellIt = field->beginXYZ(); cellIt != field->end(); ++cellIt ) + { + for( uint_t f = 0; f < 3; ++f) + { + WALBERLA_CHECK_EQUAL((*cellIt)[f], copy->get(cellIt.x(), cellIt.y(), cellIt.z())[f]); + WALBERLA_CHECK_UNEQUAL(&(*cellIt)[f], &(copy->get(cellIt.x(), cellIt.y(), cellIt.z())[f])); + WALBERLA_CHECK_EQUAL(&(*cellIt)[f], &(flattened->get(cellIt.x(), cellIt.y(), cellIt.z(), f))); + } + } + } + + return 0; +} +} + +int main( int argc, char ** argv ) +{ + return walberla::main(argc,argv); +} diff --git a/tests/field/CMakeLists.txt b/tests/field/CMakeLists.txt index 2fb6b91887db6aea9ef2c21d67d687a55f2b7e0f..6a6b8546d3e80d8202f5edee95933f242b7beb64 100644 --- a/tests/field/CMakeLists.txt +++ b/tests/field/CMakeLists.txt @@ -7,6 +7,9 @@ waLBerla_compile_test( FILES AccuracyEvaluationTest.cpp DEPENDS blockforest ) waLBerla_execute_test( NAME AccuracyEvaluationTest4 COMMAND $<TARGET_FILE:AccuracyEvaluationTest> PROCESSES 4 ) +waLBerla_compile_test( FILES AddToStorageTest.cpp DEPENDS blockforest ) +waLBerla_execute_test( NAME AddToStorageTest ) + waLBerla_compile_test( FILES communication/FieldPackInfoTest.cpp DEPENDS blockforest ) waLBerla_execute_test( NAME FieldPackInfoTest )