Commit 6d0f78f7 authored by Timo Koch's avatar Timo Koch
Browse files

Merge branch 'fix/vtkoutput-constructor' into 'master'

Fix/vtkoutput constructor

Closes #571

See merge request !1838
parents 1e6580fd be735b32
......@@ -10,6 +10,8 @@ Differences Between DuMuX 3.2 and DuMuX 3.1
- __Van Genuchten__: The VanGenuchten-Mualem material law now allows to set a parameter `l` (default to 0.5) which is sometimes fitted.
- __Runtime variable output precision e.g. Float64__: The VtkOutputModule has been adapted to allow easy changes of the vtk output precision. It is now possible to specify output precision in the input file using `Vtk.Precision` followed by either `Float32`, `Float64`, `UInt32`, `UInt8` or `Int32`. `Float32` stays the default. We especially advice the use of `Float64` when working with restart files.
An additional new option is `Vtk.CoordPrecision` which changes the precision of the coordinates only and uses the default of `Vtk.Precision`.
### Immediate interface changes not allowing/requiring a deprecation period
- Remove `Grid.HeapSize` as dune-ugrid removed the according feature as well.
......
......@@ -22,5 +22,6 @@ vtkfunction.hh
vtkmultiwriter.hh
vtknestedfunction.hh
vtkoutputmodule.hh
vtkprecision.hh
vtksequencewriter.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dumux/io)
......@@ -21,8 +21,8 @@
* \ingroup InputOutput
* \brief Dune style VTK functions
*/
#ifndef VTK_FUNCTION_HH
#define VTK_FUNCTION_HH
#ifndef DUMUX_IO_VTK_FUNCTION_HH
#define DUMUX_IO_VTK_FUNCTION_HH
#include <string>
#include <memory>
......@@ -31,10 +31,10 @@
#include <dune/grid/io/file/vtk/common.hh>
#include <dune/grid/io/file/vtk/function.hh>
#include <dumux/io/vtkprecision.hh>
#include <dumux/common/typetraits/typetraits.hh>
namespace Dumux {
namespace Vtk {
namespace Dumux::Vtk {
/*!
* \ingroup InputOutput
......@@ -63,9 +63,20 @@ public:
virtual double evaluate(int mycomp, const Element& e, const Dune::FieldVector<ctype, dim>&) const
{ return accessChooser_(mycomp, mapper_.index(e), IsIndexable<decltype(field_[0])>()); }
#if DUNE_VERSION_GTE(DUNE_GRID, 2, 7)
//! get output precision for the field
Dumux::Vtk::Precision precision() const override
{ return precision_; }
#endif
//! Constructor
VectorP0VTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper)
VectorP0VTKFunction(const GridView& gridView,
const Mapper& mapper,
const F& field,
const std::string& name,
int nComps,
Dumux::Vtk::Precision precision = Dumux::Vtk::Precision::float32)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper), precision_(precision)
{
if (field.size()!=(unsigned int)(mapper.size()))
DUNE_THROW(Dune::IOError, "VectorP0VTKFunction: size mismatch between field "
......@@ -91,6 +102,7 @@ private:
const std::string name_;
int nComps_;
const Mapper& mapper_;
Dumux::Vtk::Precision precision_;
};
/*!
......@@ -131,9 +143,21 @@ public:
return interpolation.global(xi);
}
#if DUNE_VERSION_GTE(DUNE_GRID, 2, 7)
//! get output precision for the field
Dumux::Vtk::Precision precision() const override
{ return precision_; }
#endif
//! Constructor
VectorP1VTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper)
VectorP1VTKFunction(const GridView& gridView,
const Mapper& mapper,
const F& field,
const std::string& name,
int nComps,
Dumux::Vtk::Precision precision = Dumux::Vtk::Precision::float32)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper), precision_(precision)
{
if (field.size()!=(unsigned int)( mapper.size() ))
DUNE_THROW(Dune::IOError, "VectorP1VTKFunction: size mismatch between field "
......@@ -158,6 +182,7 @@ private:
const std::string name_;
int nComps_;
const Mapper& mapper_;
Dumux::Vtk::Precision precision_;
};
/*!
......@@ -202,9 +227,20 @@ public:
return interpolation.global(xi);
}
#if DUNE_VERSION_GTE(DUNE_GRID, 2, 7)
//! get output precision for the field
Dumux::Vtk::Precision precision() const override
{ return precision_; }
#endif
//! Constructor
VectorP1NonConformingVTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper)
VectorP1NonConformingVTKFunction(const GridView& gridView,
const Mapper& mapper,
const F& field,
const std::string& name,
int nComps,
Dumux::Vtk::Precision precision = Dumux::Vtk::Precision::float32)
: field_(field), name_(name), nComps_(nComps), mapper_(mapper), precision_(precision)
{
if (field.size()!=(unsigned int)(mapper.size()))
DUNE_THROW(Dune::IOError, "VectorP1NonConformingVTKFunction: size mismatch between field "
......@@ -232,6 +268,8 @@ private:
const std::string name_;
int nComps_;
const Mapper& mapper_;
Dumux::Vtk::Precision precision_;
};
/*!
......@@ -252,18 +290,19 @@ public:
template <typename F, class Mapper>
Field(const GridView& gridView, const Mapper& mapper, F const& f,
const std::string& name, int numComp = 1, int codim = 0,
Dune::VTK::DataMode dm = Dune::VTK::conforming)
Dune::VTK::DataMode dm = Dune::VTK::conforming,
Dumux::Vtk::Precision precision = Dumux::Vtk::Precision::float32)
: codim_(codim)
{
if (codim == GridView::dimension)
{
if (dm == Dune::VTK::conforming)
field_ = std::make_shared< VectorP1VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
field_ = std::make_shared< VectorP1VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp, precision);
else
field_ = std::make_shared< VectorP1NonConformingVTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
field_ = std::make_shared< VectorP1NonConformingVTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp, precision);
}
else if (codim == 0)
field_ = std::make_shared< VectorP0VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
field_ = std::make_shared< VectorP0VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp, precision);
else
DUNE_THROW(Dune::NotImplemented, "Only element or vertex quantities allowed.");
}
......@@ -276,6 +315,16 @@ public:
//! return the number of components of this field
virtual int ncomps() const { return field_->ncomps(); }
//! return the precision of this field
virtual Dumux::Vtk::Precision precision() const
{
#if DUNE_VERSION_LT(DUNE_GRID, 2, 7)
return Dumux::Vtk::Precision::float32;
#else
return field_->precision();
#endif
}
//! codimension of the entities on which the field values live
int codim() const { return codim_; }
......@@ -295,8 +344,6 @@ private:
std::shared_ptr<Dune::VTKFunction<GridView>> field_;
};
} // end namespace Vtk
} // end namespace Dumux
} // end namespace Dumux::Vtk
#endif
This diff is collapsed.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program 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. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \ingroup InputOutput
* \brief Vtk output precision options available in Dumux
*/
#ifndef DUMUX_IO_VTK_PRECISION_HH
#define DUMUX_IO_VTK_PRECISION_HH
#include <string_view>
#include <dune/common/version.hh>
#include <dune/grid/io/file/vtk/common.hh>
namespace Dumux::Vtk {
#if DUNE_VERSION_LT(DUNE_GRID, 2, 7)
//! which precision to use when writing out data to vtk files
enum class Precision
{
int32,
uint8,
uint32,
float32,
float64
};
#else
using Dune::VTK::Precision;
#endif
/*!
* \ingroup InputOutput
* \brief Maps a string (e.g. from input) to a Dune precision type
*/
inline Precision stringToPrecision(std::string_view precisionName)
{
// this should really be constexpr but GCC <= 7.2 has a bug which
// doesn't allow string_view to be constexpr
static const std::array<std::pair<std::string_view, Precision>, 5> nameToPrecision
{{
{ "Float32", Precision::float32 },
{ "Float64", Precision::float64 },
{ "UInt32", Precision::uint32 },
{ "UInt8", Precision::uint8 },
{ "Int32", Precision::int32 },
}};
for (const auto& [name, precision] : nameToPrecision)
if (name == precisionName)
return precision;
DUNE_THROW(Dune::InvalidStateException, "Unknown precision type " << precisionName);
}
} // end namespace Dumux::Vtk
#endif
......@@ -66,3 +66,36 @@ dumux_add_test(NAME test_vtkreader_1d_polyline
dumux_add_test(NAME test_vtk_staggeredfreeflowpvnames
SOURCES test_vtk_staggeredfreeflowpvnames.cc
LABELS unit io)
# VTKOutputModule precision tests (before dune 2.7 everything was Float32)
if(${DUNE_GRID_VERSION} VERSION_LESS 2.7)
dumux_add_test(NAME test_vtkoutputmodule
SOURCES test_vtkoutputmodule.cc
LABELS unit io
COMMAND ${CMAKE_SOURCE_DIR}/bin/testing/runtest.py
CMD_ARGS --script fuzzy
--command "${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule"
--files ${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_allfloat-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_allfloat-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_allfloat-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_float-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_allfloat-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_double-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_allfloat-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_doublecoord-00000.vtu)
else()
dumux_add_test(NAME test_vtkoutputmodule
SOURCES test_vtkoutputmodule.cc
LABELS unit io
COMMAND ${CMAKE_SOURCE_DIR}/bin/testing/runtest.py
CMD_ARGS --script fuzzy
--command "${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule"
--files ${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_allfloat-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_allfloat-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_float-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_float-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_double-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_double-00000.vtu
${CMAKE_SOURCE_DIR}/test/references/test_vtkoutputmodule_doublecoord-reference.vtu
${CMAKE_CURRENT_BINARY_DIR}/test_vtkoutputmodule_doublecoord-00000.vtu)
endif()
#include <config.h>
#include <array>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/grid/yaspgrid.hh>
#include <dumux/common/parameters.hh>
#include <dumux/io/vtkoutputmodule.hh>
#include <dumux/discretization/cellcentered/tpfa/fvgridgeometry.hh>
int main(int argc, char** argv) try
{
using namespace Dumux;
Dune::MPIHelper::instance(argc, argv);
Parameters::init([](Dune::ParameterTree& params)
{
params["Double.Vtk.Precision"] = "Float64";
params["Single.Vtk.Precision"] = "Float32";
params["DoubleCoord.Vtk.CoordPrecision"] = "Float64";
});
using Grid = Dune::YaspGrid<2>;
Grid grid({1.0,1.0}, {2,2});
using GridGeometry = CCTpfaFVGridGeometry<typename Grid::LeafGridView>;
auto gridGeometry = std::make_shared<GridGeometry>(grid.leafGridView());
std::vector<int> integers({0, 1, 2, 3});
std::vector<float> floats({0.1, 1.2, 2.3, 3.4});
std::vector<double> doubles({0.123456789101112131415, 1.3, 2.5, 3.9});
// single precision
{
VtkOutputModuleBase<GridGeometry> vtkWriter(*gridGeometry, "test_vtkoutputmodule_float", "Single");
vtkWriter.addField(integers, "integer", Vtk::Precision::int32);
vtkWriter.addField(floats, "float", Vtk::Precision::float32);
vtkWriter.addField(doubles, "double", Vtk::Precision::float64);
vtkWriter.addField(doubles, "default");
vtkWriter.write(0.0);
}
// double precision
{
VtkOutputModuleBase<GridGeometry> vtkWriter(*gridGeometry, "test_vtkoutputmodule_double", "Double");
vtkWriter.addField(integers, "integer", Vtk::Precision::int32);
vtkWriter.addField(floats, "float", Vtk::Precision::float32);
vtkWriter.addField(doubles, "double", Vtk::Precision::float64);
vtkWriter.addField(doubles, "default");
vtkWriter.write(0.0);
}
// single precision, coordinates double
{
VtkOutputModuleBase<GridGeometry> vtkWriter(*gridGeometry, "test_vtkoutputmodule_doublecoord", "DoubleCoord");
vtkWriter.addField(integers, "integer", Vtk::Precision::int32);
vtkWriter.addField(floats, "float", Vtk::Precision::float32);
vtkWriter.addField(doubles, "double", Vtk::Precision::float64);
vtkWriter.addField(doubles, "default");
vtkWriter.write(0.0);
}
// single precision, all float32
{
VtkOutputModuleBase<GridGeometry> vtkWriter(*gridGeometry, "test_vtkoutputmodule_allfloat", "Single");
vtkWriter.addField(integers, "integer", Vtk::Precision::float32);
vtkWriter.addField(floats, "float", Vtk::Precision::float32);
vtkWriter.addField(doubles, "double", Vtk::Precision::float32);
vtkWriter.addField(doubles, "default");
vtkWriter.write(0.0);
}
Parameters::print();
return 0;
} // end main
catch (Dumux::ParameterException &e)
{
std::cerr << std::endl << e << " ---> Abort!" << std::endl;
return 1;
}
catch (Dune::Exception &e)
{
std::cerr << "Dune reported error: " << e << " ---> Abort!" << std::endl;
return 3;
}
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="4" NumberOfPoints="9">
<CellData Scalars="process rank">
<DataArray type="Float32" Name="process rank" NumberOfComponents="1" format="ascii">
0 0 0 0
</DataArray>
<DataArray type="Float32" Name="integer" NumberOfComponents="1" format="ascii">
0 1 2 3
</DataArray>
<DataArray type="Float32" Name="float" NumberOfComponents="1" format="ascii">
0.1 1.2 2.3 3.4
</DataArray>
<DataArray type="Float32" Name="double" NumberOfComponents="1" format="ascii">
0.123457 1.3 2.5 3.9
</DataArray>
<DataArray type="Float32" Name="default" NumberOfComponents="1" format="ascii">
0.123457 1.3 2.5 3.9
</DataArray>
</CellData>
<Points>
<DataArray type="Float32" Name="Coordinates" NumberOfComponents="3" format="ascii">
0 0 0 0.5 0 0 0 0.5 0 0.5 0.5 0
1 0 0 1 0.5 0 0 1 0 0.5 1 0
1 1 0
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="ascii">
0 1 3 2 1 4 5 3 2 3 7 6
3 5 8 7
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="ascii">
4 8 12 16
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="ascii">
9 9 9 9
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="4" NumberOfPoints="9">
<CellData Scalars="process rank">
<DataArray type="Float32" Name="process rank" NumberOfComponents="1" format="ascii">
0 0 0 0
</DataArray>
<DataArray type="Int32" Name="integer" NumberOfComponents="1" format="ascii">
0 1 2 3
</DataArray>
<DataArray type="Float32" Name="float" NumberOfComponents="1" format="ascii">
0.1 1.2 2.3 3.4
</DataArray>
<DataArray type="Float64" Name="double" NumberOfComponents="1" format="ascii">
0.123456789101112 1.3 2.5 3.9
</DataArray>
<DataArray type="Float64" Name="default" NumberOfComponents="1" format="ascii">
0.123456789101112 1.3 2.5 3.9
</DataArray>
</CellData>
<Points>
<DataArray type="Float64" Name="Coordinates" NumberOfComponents="3" format="ascii">
0 0 0 0.5 0 0 0 0.5 0 0.5 0.5 0
1 0 0 1 0.5 0 0 1 0 0.5 1 0
1 1 0
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="ascii">
0 1 3 2 1 4 5 3 2 3 7 6
3 5 8 7
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="ascii">
4 8 12 16
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="ascii">
9 9 9 9
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="4" NumberOfPoints="9">
<CellData Scalars="process rank">
<DataArray type="Float32" Name="process rank" NumberOfComponents="1" format="ascii">
0 0 0 0
</DataArray>
<DataArray type="Int32" Name="integer" NumberOfComponents="1" format="ascii">
0 1 2 3
</DataArray>
<DataArray type="Float32" Name="float" NumberOfComponents="1" format="ascii">
0.1 1.2 2.3 3.4
</DataArray>
<DataArray type="Float64" Name="double" NumberOfComponents="1" format="ascii">
0.123456789101112 1.3 2.5 3.9
</DataArray>
<DataArray type="Float32" Name="default" NumberOfComponents="1" format="ascii">
0.123457 1.3 2.5 3.9
</DataArray>
</CellData>
<Points>
<DataArray type="Float64" Name="Coordinates" NumberOfComponents="3" format="ascii">
0 0 0 0.5 0 0 0 0.5 0 0.5 0.5 0
1 0 0 1 0.5 0 0 1 0 0.5 1 0
1 1 0
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="ascii">
0 1 3 2 1 4 5 3 2 3 7 6
3 5 8 7
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="ascii">
4 8 12 16
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="ascii">
9 9 9 9
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="4" NumberOfPoints="9">
<CellData Scalars="process rank">
<DataArray type="Float32" Name="process rank" NumberOfComponents="1" format="ascii">
0 0 0 0
</DataArray>
<DataArray type="Int32" Name="integer" NumberOfComponents="1" format="ascii">
0 1 2 3
</DataArray>
<DataArray type="Float32" Name="float" NumberOfComponents="1" format="ascii">
0.1 1.2 2.3 3.4
</DataArray>
<DataArray type="Float64" Name="double" NumberOfComponents="1" format="ascii">
0.123456789101112 1.3 2.5 3.9
</DataArray>
<DataArray type="Float32" Name="default" NumberOfComponents="1" format="ascii">
0.123457 1.3 2.5 3.9
</DataArray>
</CellData>
<Points>
<DataArray type="Float32" Name="Coordinates" NumberOfComponents="3" format="ascii">
0 0 0 0.5 0 0 0 0.5 0 0.5 0.5 0
1 0 0 1 0.5 0 0 1 0 0.5 1 0
1 1 0
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="ascii">
0 1 3 2 1 4 5 3 2 3 7 6
3 5 8 7
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="ascii">
4 8 12 16
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="ascii">
9 9 9 9
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
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