From c66bd4cd5e718894ce4ecdd5b204404a75d5c712 Mon Sep 17 00:00:00 2001 From: Simon Praetorius <simon.praetorius@tu-dresden.de> Date: Mon, 6 Jul 2020 08:34:07 +0200 Subject: [PATCH] rename read method due to missunderstanding --- dune/vtk/filereader.hh | 28 +++++++++++--------- dune/vtk/gridcreatorinterface.hh | 8 +++++- dune/vtk/gridcreators/lagrangegridcreator.hh | 19 +++++++++++-- dune/vtk/gridcreators/parallelgridcreator.hh | 2 +- dune/vtk/gridcreators/serialgridcreator.hh | 4 +-- dune/vtk/vtkreader.hh | 22 +++++++++------ dune/vtk/vtkreader.impl.hh | 20 +++++++------- src/lagrangereader.cc | 6 ++--- src/test/mixed_element_test.cc | 2 +- src/test/parallel_reader_writer_test.cc | 6 ++--- src/test/reader_writer_test.cc | 4 +-- src/vtkreader.cc | 12 ++++----- 12 files changed, 82 insertions(+), 51 deletions(-) diff --git a/dune/vtk/filereader.hh b/dune/vtk/filereader.hh index 77c40b9..08fb986 100644 --- a/dune/vtk/filereader.hh +++ b/dune/vtk/filereader.hh @@ -21,15 +21,15 @@ namespace Dune struct Accessor : public Implementation { template <class... Args> - static std::unique_ptr<Grid> readImp (Args&&... args) + static std::unique_ptr<Grid> createGridFromFileImpl (Args&&... args) { - return Implementation::readImpl(std::forward<Args>(args)...); + return Implementation::createGridFromFileImpl(std::forward<Args>(args)...); } template <class... Args> - static void readFactoryImpl (Args&&... args) + static void fillFactoryImpl (Args&&... args) { - return Implementation::readFactoryImpl(std::forward<Args>(args)...); + return Implementation::fillFactoryImpl(std::forward<Args>(args)...); } }; @@ -37,34 +37,38 @@ namespace Dune /// Reads the grid from a file with filename and returns a unique_ptr to the created grid. /// Redirects to concrete implementation of derivated class. template <class... Args> - static std::unique_ptr<Grid> read (const std::string &filename, Args&&... args) + static std::unique_ptr<Grid> createGridFromFile (const std::string &filename, Args&&... args) { - return Accessor::readImpl(filename, std::forward<Args>(args)...); + return Accessor::createGridFromFileImpl(filename, std::forward<Args>(args)...); } /// Reads the grid from a file with filename into a grid-factory. /// Redirects to concrete implementation of derivated class. template <class... Args> - static void read (GridFactory<Grid> &factory, const std::string &filename, Args&&... args) + static void fillFactory (GridFactory<Grid> &factory, + const std::string &filename, + Args&&... args) { - Accessor::readFactoryImpl(factory, filename, std::forward<Args>(args)...); + Accessor::fillFactoryImpl(factory, filename, std::forward<Args>(args)...); } protected: // default implementations - // Default implementation, redirects to factory read implementation. + // Default implementation, redirects to fillFactory implementation. template <class... Args> - static std::unique_ptr<Grid> readImpl (const std::string &filename, Args&&... args) + static std::unique_ptr<Grid> createGridFromFileImpl (const std::string &filename, + Args&&... args) { GridFactory<Grid> factory; - read(factory, filename, std::forward<Args>(args)...); + fillFactory(factory, filename, std::forward<Args>(args)...); return std::unique_ptr<Grid>{ factory.createGrid() }; } // Default implementation for reading into grid-factory: produces a runtime-error. template <class... Args> - static void readFactoryImpl (GridFactory<Grid> &/*factory*/, const std::string &/*filename*/, + static void fillFactoryImpl (GridFactory<Grid> &/*factory*/, + const std::string &/*filename*/, Args&&... /*args*/) { DUNE_THROW(NotImplemented, diff --git a/dune/vtk/gridcreatorinterface.hh b/dune/vtk/gridcreatorinterface.hh index 02def76..4a6b941 100644 --- a/dune/vtk/gridcreatorinterface.hh +++ b/dune/vtk/gridcreatorinterface.hh @@ -18,7 +18,7 @@ namespace Dune * Construct a grid from data read from VTK files. * * \tparam GridType Model of Dune::Grid - * \tparam GlobalCoordType Type of the global coordinates. + * \tparam GlobalCoordType Type of the global coordinates. * \tparam DerivedType Implementation of a concrete GridCreator. **/ template <class GridType, class DerivedType> @@ -56,6 +56,12 @@ namespace Dune asDerived().insertPiecesImpl(pieces); } + /// Construct the actual grid using the GridFactory + std::unique_ptr<Grid> createGrid () const + { + return factory_->createGrid(); + } + /// Return the associated GridFactory GridFactory<Grid>& factory () { diff --git a/dune/vtk/gridcreators/lagrangegridcreator.hh b/dune/vtk/gridcreators/lagrangegridcreator.hh index 8901e89..8b6b39a 100644 --- a/dune/vtk/gridcreators/lagrangegridcreator.hh +++ b/dune/vtk/gridcreators/lagrangegridcreator.hh @@ -131,7 +131,8 @@ namespace Dune // try to create element with parametrization if constexpr (Std::is_detected_v<HasParametrizedElements, GridFactory<GridType>>) { try { - factory().insertElement(type, element, localParametrization(parametrization_.size()-1)); + factory().insertElement(type, element, + localParametrization(parametrization_.size()-1)); } catch (Dune::GridError const& /* notImplemented */) { factory().insertElement(type, element); } @@ -232,9 +233,21 @@ namespace Dune return LocalFunction{gridCreator}; } + friend LocalFunction localFunction (LagrangeGridCreator const& gridCreator) + { + return LocalFunction{gridCreator}; + } + + friend LocalFunction localFunction (LagrangeGridCreator&& gridCreator) + { + DUNE_THROW(Dune::Exception, "Cannot pass temporary LagrangeGridCreator to localFunction(). Pass an lvalue-reference instead."); + return LocalFunction{gridCreator}; + } + struct EntitySet { using Grid = GridType; + using GlobalCoordinate = typename Self::GlobalCoordinate; }; /// Dummy function returning a placeholder entityset @@ -334,10 +347,12 @@ namespace Dune using LocalParametrization = typename LagrangeGridCreator::LocalParametrization; public: - explicit LocalFunction (LagrangeGridCreator& gridCreator) + explicit LocalFunction (LagrangeGridCreator const& gridCreator) : gridCreator_(&gridCreator) {} + explicit LocalFunction (LagrangeGridCreator&& gridCreator) = delete; + /// Collect a local parametrization on the element void bind (LocalContext const& element) { diff --git a/dune/vtk/gridcreators/parallelgridcreator.hh b/dune/vtk/gridcreators/parallelgridcreator.hh index 73cb460..9f4632c 100644 --- a/dune/vtk/gridcreators/parallelgridcreator.hh +++ b/dune/vtk/gridcreators/parallelgridcreator.hh @@ -42,7 +42,7 @@ namespace Dune { if (int(pieces.size()) == this->comm().size()) { VtkReader<Grid, Self> pieceReader(this->factory()); - pieceReader.readFromFile(pieces[this->comm().rank()], true); + pieceReader.read(pieces[this->comm().rank()], true); } } }; diff --git a/dune/vtk/gridcreators/serialgridcreator.hh b/dune/vtk/gridcreators/serialgridcreator.hh index d729149..2d99777 100644 --- a/dune/vtk/gridcreators/serialgridcreator.hh +++ b/dune/vtk/gridcreators/serialgridcreator.hh @@ -53,8 +53,8 @@ namespace Dune if (this->comm().rank() == 0) { VtkReader<Grid, Self> pieceReader(*this); for (std::string const& piece : pieces) { - pieceReader.readFromFile(piece, false); - pieceReader.createGrid(false); + pieceReader.read(piece, false); + pieceReader.fillGridCreator(false); } DiscontinuousGridCreator<Grid> creator(this->factory()); diff --git a/dune/vtk/vtkreader.hh b/dune/vtk/vtkreader.hh index e3b9c74..ccb06e5 100644 --- a/dune/vtk/vtkreader.hh +++ b/dune/vtk/vtkreader.hh @@ -61,18 +61,18 @@ namespace Dune : creator_(std::move(creator)) {} - /// Read the grid from file with `filename` into the GridFactory \ref factory_ + /// Read the grid from file with `filename` into the GridCreator /** * \param filename The name of the input file - * \param create If `false`, only fill internal data structures, if `true`, also create the grid. [true] + * \param fillCreator If `false`, only fill internal data structures, if `true`, pass the internal data to the GridCreator. [true] **/ - void readFromFile (std::string const& filename, bool create = true); + void read (std::string const& filename, bool fillCreator = true); /// Implementation of \ref FileReader interface - static void readFactoryImpl (GridFactory<Grid>& factory, std::string const& filename) + static void fillFactoryImpl (GridFactory<Grid>& factory, std::string const& filename) { VtkReader reader{factory}; - reader.readFromFile(filename); + reader.read(filename); } /// Obtains the creator of the grid @@ -81,6 +81,12 @@ namespace Dune return *creator_; } + /// Construct the actual grid using the GridCreator + std::unique_ptr<Grid> createGrid () const + { + return creator_->createGrid(); + } + /// Advanced read methods /// @{ @@ -98,9 +104,9 @@ namespace Dune **/ void readParallelFileFromStream (std::ifstream& input, int rank, int size, bool create = true); - /// Construct a grid using the GridCreator - // NOTE: requires the internal data structures to be filled by an aforgoing call to readFromFile - void createGrid (bool insertPieces = true); + /// Insert all internal data to the GridCreator + // NOTE: requires the internal data structures to be filled by an aforgoing call to read() + void fillGridCreator (bool insertPieces = true); /// @} diff --git a/dune/vtk/vtkreader.impl.hh b/dune/vtk/vtkreader.impl.hh index 604dd0c..dff794a 100644 --- a/dune/vtk/vtkreader.impl.hh +++ b/dune/vtk/vtkreader.impl.hh @@ -16,7 +16,7 @@ namespace Dune { template <class Grid, class Creator> -void VtkReader<Grid,Creator>::readFromFile (std::string const& filename, bool create) +void VtkReader<Grid,Creator>::read (std::string const& filename, bool fillCreator) { // check whether file exists! if (!filesystem::exists(filename)) @@ -27,10 +27,10 @@ void VtkReader<Grid,Creator>::readFromFile (std::string const& filename, bool cr std::string ext = filesystem::path(filename).extension().string(); if (ext == ".vtu") { - readSerialFileFromStream(input, create); + readSerialFileFromStream(input, fillCreator); pieces_.push_back(filename); } else if (ext == ".pvtu") { - readParallelFileFromStream(input, comm().rank(), comm().size(), create); + readParallelFileFromStream(input, comm().rank(), comm().size(), fillCreator); } else { DUNE_THROW(IOError, "File has unknown file-extension '" << ext << "'. Allowed are only '.vtu' and '.pvtu'."); } @@ -38,7 +38,7 @@ void VtkReader<Grid,Creator>::readFromFile (std::string const& filename, bool cr template <class Grid, class Creator> -void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bool create) +void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bool fillCreator) { clear(); std::string compressor = ""; @@ -225,13 +225,13 @@ void VtkReader<Grid,Creator>::readSerialFileFromStream (std::ifstream& input, bo if (section != NO_SECTION) DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!"); - if (create) - createGrid(); + if (fillCreator) + fillGridCreator(); } template <class Grid, class Creator> -void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, int commRank, int commSize, bool create) +void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, int commRank, int commSize, bool fillCreator) { clear(); @@ -276,8 +276,8 @@ void VtkReader<Grid,Creator>::readParallelFileFromStream (std::ifstream& input, if (section != NO_SECTION) DUNE_THROW(IOError, "VTK-File is incomplete. It must end with </VTKFile>!"); - if (create) - createGrid(); + if (fillCreator) + fillGridCreator(); } @@ -543,7 +543,7 @@ void VtkReader<Grid,Creator>::readAppended (std::ifstream& input, std::vector<T> template <class Grid, class Creator> -void VtkReader<Grid,Creator>::createGrid (bool insertPieces) +void VtkReader<Grid,Creator>::fillGridCreator (bool insertPieces) { assert(vec_points.size() == numberOfPoints_); assert(vec_types.size() == numberOfCells_); diff --git a/src/lagrangereader.cc b/src/lagrangereader.cc index 8a3543c..8743c57 100644 --- a/src/lagrangereader.cc +++ b/src/lagrangereader.cc @@ -51,7 +51,7 @@ int main(int argc, char** argv) { // Test using the (static) file-reader interface std::cout << "Test 1..." << std::endl; - auto gridPtr = VtkReader<GridType,GridCreator>::read(GRID_PATH "/" + filename + ".vtu"); + auto gridPtr = VtkReader<GridType,GridCreator>::createGridFromFile(GRID_PATH "/" + filename + ".vtu"); auto& grid = *gridPtr; grid.globalRefine(2); @@ -63,7 +63,7 @@ int main(int argc, char** argv) std::cout << "Test 2..." << std::endl; GridFactory<GridType> factory; VtkReader<GridType,GridCreator> reader(factory); - reader.readFromFile(GRID_PATH "/" + filename + ".vtu"); + reader.read(GRID_PATH "/" + filename + ".vtu"); auto gridPtr = factory.createGrid(); auto& grid = *gridPtr; @@ -80,7 +80,7 @@ int main(int argc, char** argv) GridFactory<GridType> factory; GridCreator creator(factory); VtkReader<GridType,GridCreator> reader(creator); - reader.readFromFile(GRID_PATH "/" + filename + ".vtu"); + reader.read(GRID_PATH "/" + filename + ".vtu"); auto gridPtr = factory.createGrid(); auto& grid = *gridPtr; diff --git a/src/test/mixed_element_test.cc b/src/test/mixed_element_test.cc index 0a5b446..6dbd9e1 100644 --- a/src/test/mixed_element_test.cc +++ b/src/test/mixed_element_test.cc @@ -82,7 +82,7 @@ template <class Grid, class Test> void reader_test (Test& test) { for (auto const& test_case : test_cases) { - auto grid = VtkReader<Grid>::read("mixed_element_test_" + std::get<0>(test_case) + ".vtu"); + auto grid = VtkReader<Grid>::createGridFromFile("mixed_element_test_" + std::get<0>(test_case) + ".vtu"); VtkUnstructuredGridWriter<typename Grid::LeafGridView> vtkWriter(grid->leafGridView(), std::get<1>(test_case), std::get<2>(test_case)); vtkWriter.write("mixed_element_test_" + std::get<0>(test_case) + "_2.vtu"); diff --git a/src/test/parallel_reader_writer_test.cc b/src/test/parallel_reader_writer_test.cc index 72dd52b..5a8b400 100644 --- a/src/test/parallel_reader_writer_test.cc +++ b/src/test/parallel_reader_writer_test.cc @@ -119,7 +119,7 @@ void reader_writer_test(MPIHelper& mpi, TestSuite& test, std::string const& test // Step 2: read the grid from file1 and write it back to file2 { GridFactory<Grid> factory; VtkReader<Grid, Creator> reader{factory}; - reader.readFromFile(base_name + ext); + reader.read(base_name + ext); std::unique_ptr<Grid> grid{ Hybrid::ifElse(HasParallelGridFactory<Grid>{}, [&](auto id) { return id(factory).createGrid(std::true_type{}); }, @@ -135,7 +135,7 @@ void reader_writer_test(MPIHelper& mpi, TestSuite& test, std::string const& test // Step 3: read the (parallel) file1 to get the piece filenames { GridFactory<Grid> factory; VtkReader<Grid, Creator> reader{factory}; - reader.readFromFile(base_name + "_1" + ext); + reader.read(base_name + "_1" + ext); std::unique_ptr<Grid> grid{ Hybrid::ifElse(HasParallelGridFactory<Grid>{}, [&](auto id) { return id(factory).createGrid(std::true_type{}); }, @@ -152,7 +152,7 @@ void reader_writer_test(MPIHelper& mpi, TestSuite& test, std::string const& test // Step 4: read the (parallel) file2 to get the piece filenames { GridFactory<Grid> factory; VtkReader<Grid, Creator> reader{factory}; - reader.readFromFile(base_name + "_2" + ext, false); + reader.read(base_name + "_2" + ext, false); pieces2 = reader.pieces(); } diff --git a/src/test/reader_writer_test.cc b/src/test/reader_writer_test.cc index c7edefb..0043bfc 100644 --- a/src/test/reader_writer_test.cc +++ b/src/test/reader_writer_test.cc @@ -105,7 +105,7 @@ void reader_test (MPIHelper& mpi, Test& test) { GridFactory<Grid> factory; VtkReader<Grid> reader{factory}; - reader.readFromFile("reader_writer_test_" + std::get<0>(test_case) + ext); + reader.read("reader_writer_test_" + std::get<0>(test_case) + ext); std::unique_ptr<Grid> grid{factory.createGrid()}; pieces1 = reader.pieces(); @@ -118,7 +118,7 @@ void reader_test (MPIHelper& mpi, Test& test) { GridFactory<Grid> factory2; VtkReader<Grid> reader2{factory2}; - reader2.readFromFile("reader_writer_test_" + std::get<0>(test_case) + "_2" + ext, false); + reader2.read("reader_writer_test_" + std::get<0>(test_case) + "_2" + ext, false); pieces2 = reader2.pieces(); } diff --git a/src/vtkreader.cc b/src/vtkreader.cc index 5ac2fd7..13be510 100644 --- a/src/vtkreader.cc +++ b/src/vtkreader.cc @@ -51,7 +51,7 @@ int main(int argc, char** argv) { std::cout << "read 'test_ascii_float32.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType>::read("test_ascii_float32.vtu"); + auto gridPtr = VtkReader<GridType>::createGridFromFile("test_ascii_float32.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView> vtkWriter(grid.leafGridView(), Vtk::ASCII); @@ -60,7 +60,7 @@ int main(int argc, char** argv) { std::cout << "read 'test_binary_float32.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType>::read("test_binary_float32.vtu"); + auto gridPtr = VtkReader<GridType>::createGridFromFile("test_binary_float32.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView> vtkWriter(grid.leafGridView(), Vtk::ASCII); @@ -69,7 +69,7 @@ int main(int argc, char** argv) { std::cout << "read 'test_compressed_float64.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType>::read("test_compressed_float64.vtu"); + auto gridPtr = VtkReader<GridType>::createGridFromFile("test_compressed_float64.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView> vtkWriter(grid.leafGridView(), Vtk::ASCII, Vtk::FLOAT64); @@ -78,7 +78,7 @@ int main(int argc, char** argv) { std::cout << "read 'test_ascii_float32.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType,DiscontinuousGridCreator<GridType>>::read("test_ascii_float32.vtu"); + auto gridPtr = VtkReader<GridType,DiscontinuousGridCreator<GridType>>::createGridFromFile("test_ascii_float32.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView> vtkWriter(grid.leafGridView(), Vtk::ASCII); @@ -90,7 +90,7 @@ int main(int argc, char** argv) using GridView3d = typename GridType3d::LeafGridView; std::cout << "read 'paraview_3d.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType3d>::read("paraview_3d.vtu"); + auto gridPtr = VtkReader<GridType3d>::createGridFromFile("paraview_3d.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView3d> vtkWriter(grid.leafGridView(), Vtk::ASCII, Vtk::FLOAT64); @@ -102,7 +102,7 @@ int main(int argc, char** argv) using GridType2d = UGGrid<2>; using GridView2d = typename GridType2d::LeafGridView; std::cout << "read 'paraview_2d.vtu'..." << std::endl; - auto gridPtr = VtkReader<GridType2d>::read("paraview_2d.vtu"); + auto gridPtr = VtkReader<GridType2d>::createGridFromFile("paraview_2d.vtu"); auto& grid = *gridPtr; VtkUnstructuredGridWriter<GridView2d> vtkWriter(grid.leafGridView(), Vtk::ASCII, Vtk::FLOAT64); -- GitLab