Commit 0e589dad authored by Berk Geveci's avatar Berk Geveci Committed by Kitware Robot
Browse files

Merge topic 'plot3d-error-handling-fix'

d91ecf98 BUG: Was using the wrong extent in computation.
005e70d3 Fixed warnings.
5f15c59d

 Cleaned up error handling in plot3d reader.
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Reviewed-by: Utkarsh Ayachit's avatarUtkarsh Ayachit <utkarsh.ayachit@kitware.com>
Merge-request: !433
parents bfd1cedd d91ecf98
...@@ -71,13 +71,18 @@ public: ...@@ -71,13 +71,18 @@ public:
{ {
} }
int ReadScalar(FILE* fp, vtkIdType preskip, vtkIdType n, vtkIdType postskip, DataType* scalar) vtkIdType ReadScalar(FILE* fp,
vtkIdType preskip,
vtkIdType n,
vtkIdType postskip,
DataType* scalar)
{ {
if (preskip > 0) if (preskip > 0)
{ {
vtk_fseek(fp, preskip*sizeof(DataType), SEEK_CUR); vtk_fseek(fp, preskip*sizeof(DataType), SEEK_CUR);
} }
int retVal = static_cast<int>(fread(scalar, sizeof(DataType), n, fp)); vtkIdType retVal = static_cast<vtkIdType>(
fread(scalar, sizeof(DataType), n, fp));
vtk_fseek(fp, postskip*sizeof(DataType), SEEK_CUR); vtk_fseek(fp, postskip*sizeof(DataType), SEEK_CUR);
if (this->ByteOrder == vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN) if (this->ByteOrder == vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN)
{ {
...@@ -112,9 +117,9 @@ public: ...@@ -112,9 +117,9 @@ public:
postskip = nPtsInPlane * (wextent[5] - extent[5]); postskip = nPtsInPlane * (wextent[5] - extent[5]);
} }
int ReadVector(FILE* fp, vtkIdType ReadVector(FILE* fp,
int extent[6], int wextent[6], int extent[6], int wextent[6],
int numDims, DataType* vector) int numDims, DataType* vector)
{ {
vtkIdType n = vtkStructuredData::GetNumberOfPoints(extent); vtkIdType n = vtkStructuredData::GetNumberOfPoints(extent);
...@@ -124,7 +129,7 @@ public: ...@@ -124,7 +129,7 @@ public:
// in the file) // in the file)
memset(vector, 0, n*3*sizeof(DataType)); memset(vector, 0, n*3*sizeof(DataType));
int retVal = 0; vtkIdType retVal = 0;
DataType* buffer = new DataType[n]; DataType* buffer = new DataType[n];
for (int component = 0; component < numDims; component++) for (int component = 0; component < numDims; component++)
{ {
...@@ -408,7 +413,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block) ...@@ -408,7 +413,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
{ {
if (this->Internal->Settings.BinaryFile) if (this->Internal->Settings.BinaryFile)
{ {
int retVal=static_cast<int>(fread(block, sizeof(int), n, fp)); vtkIdType retVal=static_cast<vtkIdType>(fread(block, sizeof(int), n, fp));
if (this->Internal->Settings.ByteOrder == FILE_LITTLE_ENDIAN) if (this->Internal->Settings.ByteOrder == FILE_LITTLE_ENDIAN)
{ {
vtkByteSwap::Swap4LERange(block, n); vtkByteSwap::Swap4LERange(block, n);
...@@ -417,11 +422,11 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block) ...@@ -417,11 +422,11 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
{ {
vtkByteSwap::Swap4BERange(block, n); vtkByteSwap::Swap4BERange(block, n);
} }
return retVal; return retVal == n;
} }
else else
{ {
int count = 0; vtkIdType count = 0;
for(int i=0; i<n; i++) for(int i=0; i<n; i++)
{ {
int num = fscanf(fp, "%d", &(block[i])); int num = fscanf(fp, "%d", &(block[i]));
...@@ -434,7 +439,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block) ...@@ -434,7 +439,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
return 0; return 0;
} }
} }
return count; return count == n;
} }
} }
...@@ -450,7 +455,7 @@ vtkDataArray* vtkMultiBlockPLOT3DReader::NewFloatArray() ...@@ -450,7 +455,7 @@ vtkDataArray* vtkMultiBlockPLOT3DReader::NewFloatArray()
} }
} }
int vtkMultiBlockPLOT3DReader::ReadValues( vtkIdType vtkMultiBlockPLOT3DReader::ReadValues(
FILE* fp, int n, vtkDataArray* scalar) FILE* fp, int n, vtkDataArray* scalar)
{ {
if (this->Internal->Settings.BinaryFile) if (this->Internal->Settings.BinaryFile)
...@@ -535,7 +540,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntScalar( ...@@ -535,7 +540,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip); arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkIntArray* intArray = static_cast<vtkIntArray*>(scalar); vtkIntArray* intArray = static_cast<vtkIntArray*>(scalar);
return arrayReader.ReadScalar( return arrayReader.ReadScalar(
fp, preskip, n, postskip, intArray->GetPointer(0)); fp, preskip, n, postskip, intArray->GetPointer(0)) == n;
} }
else else
{ {
...@@ -567,7 +572,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar( ...@@ -567,7 +572,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip); arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkFloatArray* floatArray = static_cast<vtkFloatArray*>(scalar); vtkFloatArray* floatArray = static_cast<vtkFloatArray*>(scalar);
return arrayReader.ReadScalar( return arrayReader.ReadScalar(
fp, preskip, n, postskip, floatArray->GetPointer(0)); fp, preskip, n, postskip, floatArray->GetPointer(0)) == n;
} }
else else
{ {
...@@ -577,7 +582,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar( ...@@ -577,7 +582,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip); arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkDoubleArray* doubleArray = static_cast<vtkDoubleArray*>(scalar); vtkDoubleArray* doubleArray = static_cast<vtkDoubleArray*>(scalar);
return arrayReader.ReadScalar( return arrayReader.ReadScalar(
fp, preskip, n, postskip, doubleArray->GetPointer(0)); fp, preskip, n, postskip, doubleArray->GetPointer(0)) == n;
} }
} }
else else
...@@ -600,7 +605,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar( ...@@ -600,7 +605,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
return 0; return 0;
} }
} }
return count; return count == n;
} }
else else
{ {
...@@ -620,7 +625,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar( ...@@ -620,7 +625,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
return 0; return 0;
} }
} }
return count; return count == n;
} }
} }
} }
...@@ -629,6 +634,9 @@ int vtkMultiBlockPLOT3DReader::ReadVector( ...@@ -629,6 +634,9 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
void* vfp, int extent[6], int wextent[6], void* vfp, int extent[6], int wextent[6],
int numDims, vtkDataArray* vector, vtkTypeUInt64 offset) int numDims, vtkDataArray* vector, vtkTypeUInt64 offset)
{ {
vtkIdType n = vtkStructuredData::GetNumberOfPoints(extent);
vtkIdType nValues = n*numDims;
FILE* fp = reinterpret_cast<FILE*>(vfp); FILE* fp = reinterpret_cast<FILE*>(vfp);
if (this->Internal->Settings.BinaryFile) if (this->Internal->Settings.BinaryFile)
...@@ -642,25 +650,26 @@ int vtkMultiBlockPLOT3DReader::ReadVector( ...@@ -642,25 +650,26 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
vtkPLOT3DArrayReader<float> arrayReader; vtkPLOT3DArrayReader<float> arrayReader;
arrayReader.ByteOrder = this->Internal->Settings.ByteOrder; arrayReader.ByteOrder = this->Internal->Settings.ByteOrder;
vtkFloatArray* floatArray = static_cast<vtkFloatArray*>(vector); vtkFloatArray* floatArray = static_cast<vtkFloatArray*>(vector);
return arrayReader.ReadVector(fp, extent, wextent, numDims, floatArray->GetPointer(0)); return arrayReader.ReadVector(
fp, extent, wextent, numDims, floatArray->GetPointer(0)) == nValues;
} }
else else
{ {
vtkPLOT3DArrayReader<double> arrayReader; vtkPLOT3DArrayReader<double> arrayReader;
arrayReader.ByteOrder = this->Internal->Settings.ByteOrder; arrayReader.ByteOrder = this->Internal->Settings.ByteOrder;
vtkDoubleArray* doubleArray = static_cast<vtkDoubleArray*>(vector); vtkDoubleArray* doubleArray = static_cast<vtkDoubleArray*>(vector);
return arrayReader.ReadVector(fp, extent, wextent, numDims, doubleArray->GetPointer(0)); return arrayReader.ReadVector(
fp, extent, wextent, numDims, doubleArray->GetPointer(0)) == nValues;
} }
} }
else else
{ {
vtkIdType n = vtkStructuredData::GetNumberOfPoints(wextent);
// Initialize the 3rd component to 0 in case the input file is // Initialize the 3rd component to 0 in case the input file is
// 2D // 2D
vector->FillComponent(2, 0); vector->FillComponent(2, 0);
int count = 0; vtkIdType count = 0;
if (this->Internal->Settings.Precision == 4) if (this->Internal->Settings.Precision == 4)
{ {
...@@ -695,7 +704,7 @@ int vtkMultiBlockPLOT3DReader::ReadVector( ...@@ -695,7 +704,7 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
tmpArray->Delete(); tmpArray->Delete();
} }
return count; return count == nValues;
} }
} }
...@@ -1070,7 +1079,7 @@ int vtkMultiBlockPLOT3DReader::RequestInformation( ...@@ -1070,7 +1079,7 @@ int vtkMultiBlockPLOT3DReader::RequestInformation(
properties->SetNumberOfTuples(4); properties->SetNumberOfTuples(4);
// Read fsmach, alpha, re, time; // Read fsmach, alpha, re, time;
if (this->ReadValues(qFp, 4, properties) == 0) if (this->ReadValues(qFp, 4, properties) != 4)
{ {
vtkErrorMacro("Encountered premature end-of-file while reading " vtkErrorMacro("Encountered premature end-of-file while reading "
"the q file (or the file is corrupt)."); "the q file (or the file is corrupt).");
...@@ -1130,6 +1139,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1130,6 +1139,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
vtkMultiBlockDataSet::SafeDownCast(doOutput); vtkMultiBlockDataSet::SafeDownCast(doOutput);
if (!mb) if (!mb)
{ {
this->ClearGeometryCache();
return 0; return 0;
} }
...@@ -1185,6 +1195,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1185,6 +1195,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
{ {
this->Controller->Broadcast(&numBlocks, 1, 0); this->Controller->Broadcast(&numBlocks, 1, 0);
mb->SetNumberOfBlocks(numBlocks); mb->SetNumberOfBlocks(numBlocks);
this->ClearGeometryCache();
return 1; return 1;
} }
else else
...@@ -1209,32 +1220,49 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1209,32 +1220,49 @@ int vtkMultiBlockPLOT3DReader::RequestData(
vtkTypeUInt64 offset = 0; vtkTypeUInt64 offset = 0;
int error = 0;
// Only the first rank does meta-data checking // Only the first rank does meta-data checking
// using POSIX IO. // using POSIX IO.
if (rank == 0) if (rank == 0)
{ {
if ( this->CheckGeometryFile(xyzFp) != VTK_OK) try
{ {
return 0; if ( this->CheckGeometryFile(xyzFp) != VTK_OK)
} {
throw Plot3DException();
}
if ( this->ReadGeometryHeader(xyzFp) != VTK_OK ) if ( this->ReadGeometryHeader(xyzFp) != VTK_OK )
{ {
vtkErrorMacro("Error reading geometry file."); vtkErrorMacro("Error reading geometry file.");
fclose(xyzFp); fclose(xyzFp);
return 0; throw Plot3DException();
} }
// Update from the value in the file. // Update from the value in the file.
numBlocks = static_cast<int>(this->Internal->Dimensions.size()); numBlocks = static_cast<int>(this->Internal->Dimensions.size());
if (this->Internal->Settings.BinaryFile) if (this->Internal->Settings.BinaryFile)
{
offset = vtk_ftell(xyzFp);
fclose(xyzFp);
}
}
catch (Plot3DException&)
{ {
offset = vtk_ftell(xyzFp); error = 1;
fclose(xyzFp);
} }
} }
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
// All meta-data needs to be broadcasted. // All meta-data needs to be broadcasted.
mp->Broadcast(&numBlocks, 1, 0); mp->Broadcast(&numBlocks, 1, 0);
if (rank > 0) if (rank > 0)
...@@ -1311,6 +1339,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1311,6 +1339,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
"the geometry file (or the file is corrupt)."); "the geometry file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError); this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
this->CloseFile(xyzFp2); this->CloseFile(xyzFp2);
this->ClearGeometryCache();
return 0; return 0;
} }
// Increment the offset for next read. This points to the // Increment the offset for next read. This points to the
...@@ -1329,6 +1358,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1329,6 +1358,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
"the xyz file (or the file is corrupt)."); "the xyz file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError); this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
this->CloseFile(xyzFp2); this->CloseFile(xyzFp2);
this->ClearGeometryCache();
return 0; return 0;
} }
...@@ -1399,19 +1429,37 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1399,19 +1429,37 @@ int vtkMultiBlockPLOT3DReader::RequestData(
{ {
FILE* qFp = 0; FILE* qFp = 0;
int nq=0, nqc=0, isOverflow=0; int nq=0, nqc=0, isOverflow=0;
int error = 0;
if (rank == 0) if (rank == 0)
{ {
if ( this->CheckSolutionFile(qFp) != VTK_OK) try
{ {
return 0; if ( this->CheckSolutionFile(qFp) != VTK_OK)
} {
throw Plot3DException();
}
if ( this->ReadQHeader(qFp, true, nq, nqc, isOverflow) != VTK_OK ) if ( this->ReadQHeader(qFp, true, nq, nqc, isOverflow) != VTK_OK )
{
fclose(qFp);
throw Plot3DException();
}
}
catch (Plot3DException&)
{ {
fclose(qFp); error = 1;
return 0;
} }
} }
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
int vals[3] = {nq, nqc, isOverflow}; int vals[3] = {nq, nqc, isOverflow};
mp->Broadcast(vals, 3, 0); mp->Broadcast(vals, 3, 0);
nq = vals[0]; nq = vals[0];
...@@ -1458,56 +1506,73 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1458,56 +1506,73 @@ int vtkMultiBlockPLOT3DReader::RequestData(
mp->Broadcast(&numProperties, 1, 0); mp->Broadcast(&numProperties, 1, 0);
properties->SetNumberOfTuples(numProperties); properties->SetNumberOfTuples(numProperties);
error = 0;
if (rank == 0) if (rank == 0)
{ {
// Read fsmach, alpha, re, time; try
if ( this->ReadValues(qFp, 4, properties) == 0)
{
vtkErrorMacro("Encountered premature end-of-file while reading "
"the q file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
fclose(qFp);
properties->Delete();
return 0;
}
if (isOverflow)
{ {
// We create a dummy array to use with ReadValues // Read fsmach, alpha, re, time;
vtkDataArray* dummyArray = properties->NewInstance(); if ( this->ReadValues(qFp, 4, properties) != 4)
dummyArray->SetVoidArray(properties->GetVoidPointer(4), 3, 1);
// Read GAMINF, BETA, TINF
if ( this->ReadValues(qFp, 3, dummyArray) == 0)
{ {
vtkErrorMacro("Encountered premature end-of-file while reading " vtkErrorMacro("Encountered premature end-of-file while reading "
"the q file (or the file is corrupt)."); "the q file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError); this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
fclose(qFp); fclose(qFp);
properties->Delete(); properties->Delete();
return 0; throw Plot3DException();
} }
// igam is an int if (isOverflow)
int igam;
this->ReadIntBlock(qFp, 1, &igam);
properties->SetTuple1(7, igam);
dummyArray->SetVoidArray(properties->GetVoidPointer(8), 3, 1);
// Read the rest of properties
if ( this->ReadValues(qFp, numProperties - 8, dummyArray) == 0)
{ {
vtkErrorMacro("Encountered premature end-of-file while reading " // We create a dummy array to use with ReadValues
"the q file (or the file is corrupt)."); vtkDataArray* dummyArray = properties->NewInstance();
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError); dummyArray->SetVoidArray(properties->GetVoidPointer(4), 3, 1);
fclose(qFp);
properties->Delete(); // Read GAMINF, BETA, TINF
return 0; if ( this->ReadValues(qFp, 3, dummyArray) != 3)
{
vtkErrorMacro("Encountered premature end-of-file while reading "
"the q file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
fclose(qFp);
properties->Delete();
throw Plot3DException();
}
// igam is an int
int igam;
this->ReadIntBlock(qFp, 1, &igam);
properties->SetTuple1(7, igam);
dummyArray->SetVoidArray(properties->GetVoidPointer(8), 3, 1);
// Read the rest of properties
if ( this->ReadValues(qFp, numProperties - 8, dummyArray) !=
numProperties - 8)
{
vtkErrorMacro("Encountered premature end-of-file while reading "
"the q file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
fclose(qFp);
properties->Delete();
throw Plot3DException();
}
dummyArray->Delete();
} }
dummyArray->Delete(); this->SkipByteCount(qFp);
}
catch (Plot3DException&)
{
error = 1;
} }
this->SkipByteCount(qFp);
} }
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
mp->Broadcast(properties, 0); mp->Broadcast(properties, 0);
nthOutput->GetFieldData()->AddArray(properties); nthOutput->GetFieldData()->AddArray(properties);
...@@ -1548,6 +1613,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1548,6 +1613,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
} }
this->CloseFile(qFp2); this->CloseFile(qFp2);
density->Delete(); density->Delete();
this->ClearGeometryCache();
return 0; return 0;
} }
nthOutput->GetPointData()->AddArray(density); nthOutput->GetPointData()->AddArray(density);
...@@ -1573,6 +1639,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1573,6 +1639,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
} }
this->CloseFile(qFp2); this->CloseFile(qFp2);
momentum->Delete(); momentum->Delete();
this->ClearGeometryCache();
return 0; return 0;
} }
nthOutput->GetPointData()->AddArray(momentum); nthOutput->GetPointData()->AddArray(momentum);
...@@ -1594,6 +1661,7 @@ int vtkMultiBlockPLOT3DReader::RequestData( ...@@ -1594,6 +1661,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
} }
this->CloseFile(qFp2);