ENH: limit vtk floatField range (fixes #1055)

- for space-savings the VTK fields are normally written as 'float'
  rather than double. When a double field contains very large values,
  these can result in a overflow when converted to float.

  Now trap these with the appropriate numeric limits.
  No warning when these values are clipped: it should be readily
  apparent from the output.

ENH: handle symmTensor component swapping directly on VTK output.

- use VTK output routines in vtkSurfaceWriter to benefit from the
  above changes
This commit is contained in:
Mark Olesen
2018-11-01 17:58:36 +00:00
parent 0ce7e364a4
commit 7cb51f5b98
8 changed files with 223 additions and 204 deletions

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -25,6 +25,7 @@ License
#include "foamVtkAppendRawFormatter.H"
#include "foamVtkOutputOptions.H"
#include <limits>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -105,8 +106,21 @@ void Foam::vtk::appendRawFormatter::write(const float val)
void Foam::vtk::appendRawFormatter::write(const double val)
{
// std::cerr<<"double as float=" << val << '\n';
float copy(val);
write(copy);
// Limit range of double to float conversion
if (val >= std::numeric_limits<float>::max())
{
write(std::numeric_limits<float>::max());
}
else if (val <= std::numeric_limits<float>::lowest())
{
write(std::numeric_limits<float>::lowest());
}
else
{
float copy(val);
write(copy);
}
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -25,6 +25,7 @@ License
#include "foamVtkAsciiFormatter.H"
#include "foamVtkOutputOptions.H"
#include <limits>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -139,8 +140,20 @@ void Foam::vtk::asciiFormatter::write(const float val)
void Foam::vtk::asciiFormatter::write(const double val)
{
next();
os()<< float(val);
// Limit range of double to float conversion
if (val >= std::numeric_limits<float>::max())
{
write(std::numeric_limits<float>::max());
}
else if (val <= std::numeric_limits<float>::lowest())
{
write(std::numeric_limits<float>::lowest());
}
else
{
float copy(val);
write(copy);
}
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "foamVtkBase64Layer.H"
#include <limits>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -96,8 +97,21 @@ void Foam::vtk::foamVtkBase64Layer::write(const float val)
void Foam::vtk::foamVtkBase64Layer::write(const double val)
{
// std::cerr<<"double as float=" << val << '\n';
float copy(val);
write(copy);
// Limit range of double to float conversion
if (val >= std::numeric_limits<float>::max())
{
write(std::numeric_limits<float>::max());
}
else if (val <= std::numeric_limits<float>::lowest())
{
write(std::numeric_limits<float>::lowest());
}
else
{
float copy(val);
write(copy);
}
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -26,6 +26,7 @@ License
#include "foamVtkLegacyRawFormatter.H"
#include "foamVtkOutputOptions.H"
#include "endian.H"
#include <limits>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -98,10 +99,7 @@ void Foam::vtk::legacyRawFormatter::write
}
void Foam::vtk::legacyRawFormatter::write
(
const label val
)
void Foam::vtk::legacyRawFormatter::write(const label val)
{
// std::cerr<<"label is:" << sizeof(val) << '\n';
@ -121,10 +119,7 @@ void Foam::vtk::legacyRawFormatter::write
}
void Foam::vtk::legacyRawFormatter::write
(
const float val
)
void Foam::vtk::legacyRawFormatter::write(const float val)
{
// std::cerr<<"float is:" << sizeof(val) << '\n';
@ -142,15 +137,25 @@ void Foam::vtk::legacyRawFormatter::write
}
void Foam::vtk::legacyRawFormatter::write
(
const double val
)
void Foam::vtk::legacyRawFormatter::write(const double val)
{
// Legacy cannot support Float64 anyhow.
// std::cerr<<"write double as float:" << val << '\n';
float copy(val);
write(copy);
// Limit range of double to float conversion
if (val >= std::numeric_limits<float>::max())
{
write(std::numeric_limits<float>::max());
}
else if (val <= std::numeric_limits<float>::lowest())
{
write(std::numeric_limits<float>::lowest());
}
else
{
float copy(val);
write(copy);
}
}

View File

@ -48,6 +48,7 @@ SourceFiles
#include "foamVtkCore.H"
#include "foamVtkFormatter.H"
#include "floatScalar.H"
#include "symmTensor.H"
#include "IOstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -231,6 +232,22 @@ namespace legacy
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Template specialization for symmTensor ordering
template<>
inline void write(vtk::formatter& fmt, const symmTensor& val)
{
// symmTensor ( XX, XY, XZ, YY, YZ, ZZ )
// VTK order ( XX, YY, ZZ, XY, YZ, XZ ) -> (0, 3, 5, 1, 4, 2)
fmt.write(component(val, 0)); // XX
fmt.write(component(val, 3)); // YY
fmt.write(component(val, 5)); // ZZ
fmt.write(component(val, 1)); // XY
fmt.write(component(val, 4)); // YZ
fmt.write(component(val, 2)); // XZ
}
} // End namespace vtk
} // End namespace Foam