From 98aa92a07f4befa2e9abdffa2ee2aeb009102aa2 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 21 Jun 2021 16:27:32 +0200
Subject: [PATCH] BUG: noise did not set num of output fields (VTK legacy)
 [fixes #2133]

ENH: less cryptic error message in surfaceWriters::vtkWriter

- fall back to nFields=1 and report the problem.
---
 .../noiseModels/surfaceNoise/surfaceNoise.C      |  2 ++
 src/surfMesh/writers/vtk/vtkSurfaceWriter.C      | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C b/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
index 5ad342225d1..fa0f3bada3b 100644
--- a/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
+++ b/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
@@ -304,6 +304,7 @@ scalar surfaceNoise::writeSurfaceData
                     false  // serial - already merged
                 );
 
+                writerPtr_->nFields(1); // Legacy VTK
                 writerPtr_->write(title, allData);
 
                 writerPtr_->endTime();
@@ -341,6 +342,7 @@ scalar surfaceNoise::writeSurfaceData
                 false  // serial - already merged
             );
 
+            writerPtr_->nFields(1); // Legacy VTK
             writerPtr_->write(title, data);
 
             writerPtr_->endTime();
diff --git a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
index 65df3d04a5c..016351e6212 100644
--- a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
+++ b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C
@@ -289,6 +289,22 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::writeTemplate
 
     if (Pstream::master() || !parallel_)
     {
+        if (!nFields_ && writer_->legacy())
+        {
+            // Emit error message, but attempt to recover anyhow
+            nFields_ = 1;
+
+            FatalErrorInFunction
+                << "Using VTK legacy format, but did not define nFields!"
+                << nl
+                << "Assuming nFields=1 (may be incorrect) and continuing..."
+                << nl
+                << "    Field " << fieldName << " to " << outputFile << nl;
+
+            Info<< FatalError;
+            Info<< endl;
+        }
+
         if (this->isPointData())
         {
             writer_->beginPointData(nFields_);
-- 
GitLab