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:
{
}
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)
{
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);
if (this->ByteOrder == vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN)
{
......@@ -112,9 +117,9 @@ public:
postskip = nPtsInPlane * (wextent[5] - extent[5]);
}
int ReadVector(FILE* fp,
int extent[6], int wextent[6],
int numDims, DataType* vector)
vtkIdType ReadVector(FILE* fp,
int extent[6], int wextent[6],
int numDims, DataType* vector)
{
vtkIdType n = vtkStructuredData::GetNumberOfPoints(extent);
......@@ -124,7 +129,7 @@ public:
// in the file)
memset(vector, 0, n*3*sizeof(DataType));
int retVal = 0;
vtkIdType retVal = 0;
DataType* buffer = new DataType[n];
for (int component = 0; component < numDims; component++)
{
......@@ -408,7 +413,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
{
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)
{
vtkByteSwap::Swap4LERange(block, n);
......@@ -417,11 +422,11 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
{
vtkByteSwap::Swap4BERange(block, n);
}
return retVal;
return retVal == n;
}
else
{
int count = 0;
vtkIdType count = 0;
for(int i=0; i<n; i++)
{
int num = fscanf(fp, "%d", &(block[i]));
......@@ -434,7 +439,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntBlock(FILE* fp, int n, int* block)
return 0;
}
}
return count;
return count == n;
}
}
......@@ -450,7 +455,7 @@ vtkDataArray* vtkMultiBlockPLOT3DReader::NewFloatArray()
}
}
int vtkMultiBlockPLOT3DReader::ReadValues(
vtkIdType vtkMultiBlockPLOT3DReader::ReadValues(
FILE* fp, int n, vtkDataArray* scalar)
{
if (this->Internal->Settings.BinaryFile)
......@@ -535,7 +540,7 @@ int vtkMultiBlockPLOT3DReader::ReadIntScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkIntArray* intArray = static_cast<vtkIntArray*>(scalar);
return arrayReader.ReadScalar(
fp, preskip, n, postskip, intArray->GetPointer(0));
fp, preskip, n, postskip, intArray->GetPointer(0)) == n;
}
else
{
......@@ -567,7 +572,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkFloatArray* floatArray = static_cast<vtkFloatArray*>(scalar);
return arrayReader.ReadScalar(
fp, preskip, n, postskip, floatArray->GetPointer(0));
fp, preskip, n, postskip, floatArray->GetPointer(0)) == n;
}
else
{
......@@ -577,7 +582,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
arrayReader.CalculateSkips(extent, wextent, preskip, postskip);
vtkDoubleArray* doubleArray = static_cast<vtkDoubleArray*>(scalar);
return arrayReader.ReadScalar(
fp, preskip, n, postskip, doubleArray->GetPointer(0));
fp, preskip, n, postskip, doubleArray->GetPointer(0)) == n;
}
}
else
......@@ -600,7 +605,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
return 0;
}
}
return count;
return count == n;
}
else
{
......@@ -620,7 +625,7 @@ int vtkMultiBlockPLOT3DReader::ReadScalar(
return 0;
}
}
return count;
return count == n;
}
}
}
......@@ -629,6 +634,9 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
void* vfp, int extent[6], int wextent[6],
int numDims, vtkDataArray* vector, vtkTypeUInt64 offset)
{
vtkIdType n = vtkStructuredData::GetNumberOfPoints(extent);
vtkIdType nValues = n*numDims;
FILE* fp = reinterpret_cast<FILE*>(vfp);
if (this->Internal->Settings.BinaryFile)
......@@ -642,25 +650,26 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
vtkPLOT3DArrayReader<float> arrayReader;
arrayReader.ByteOrder = this->Internal->Settings.ByteOrder;
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
{
vtkPLOT3DArrayReader<double> arrayReader;
arrayReader.ByteOrder = this->Internal->Settings.ByteOrder;
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
{
vtkIdType n = vtkStructuredData::GetNumberOfPoints(wextent);
// Initialize the 3rd component to 0 in case the input file is
// 2D
vector->FillComponent(2, 0);
int count = 0;
vtkIdType count = 0;
if (this->Internal->Settings.Precision == 4)
{
......@@ -695,7 +704,7 @@ int vtkMultiBlockPLOT3DReader::ReadVector(
tmpArray->Delete();
}
return count;
return count == nValues;
}
}
......@@ -1070,7 +1079,7 @@ int vtkMultiBlockPLOT3DReader::RequestInformation(
properties->SetNumberOfTuples(4);
// 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 "
"the q file (or the file is corrupt).");
......@@ -1130,6 +1139,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
vtkMultiBlockDataSet::SafeDownCast(doOutput);
if (!mb)
{
this->ClearGeometryCache();
return 0;
}
......@@ -1185,6 +1195,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
{
this->Controller->Broadcast(&numBlocks, 1, 0);
mb->SetNumberOfBlocks(numBlocks);
this->ClearGeometryCache();
return 1;
}
else
......@@ -1209,32 +1220,49 @@ int vtkMultiBlockPLOT3DReader::RequestData(
vtkTypeUInt64 offset = 0;
int error = 0;
// Only the first rank does meta-data checking
// using POSIX IO.
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 )
{
vtkErrorMacro("Error reading geometry file.");
fclose(xyzFp);
return 0;
}
if ( this->ReadGeometryHeader(xyzFp) != VTK_OK )
{
vtkErrorMacro("Error reading geometry file.");
fclose(xyzFp);
throw Plot3DException();
}
// Update from the value in the file.
numBlocks = static_cast<int>(this->Internal->Dimensions.size());
// Update from the value in the file.
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);
fclose(xyzFp);
error = 1;
}
}
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
// All meta-data needs to be broadcasted.
mp->Broadcast(&numBlocks, 1, 0);
if (rank > 0)
......@@ -1311,6 +1339,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
"the geometry file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
this->CloseFile(xyzFp2);
this->ClearGeometryCache();
return 0;
}
// Increment the offset for next read. This points to the
......@@ -1329,6 +1358,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
"the xyz file (or the file is corrupt).");
this->SetErrorCode(vtkErrorCode::PrematureEndOfFileError);
this->CloseFile(xyzFp2);
this->ClearGeometryCache();
return 0;
}
......@@ -1399,19 +1429,37 @@ int vtkMultiBlockPLOT3DReader::RequestData(
{
FILE* qFp = 0;
int nq=0, nqc=0, isOverflow=0;
int error = 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);
return 0;
error = 1;
}
}
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
int vals[3] = {nq, nqc, isOverflow};
mp->Broadcast(vals, 3, 0);
nq = vals[0];
......@@ -1458,56 +1506,73 @@ int vtkMultiBlockPLOT3DReader::RequestData(
mp->Broadcast(&numProperties, 1, 0);
properties->SetNumberOfTuples(numProperties);
error = 0;
if (rank == 0)
{
// Read fsmach, alpha, re, time;
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)
try
{
// We create a dummy array to use with ReadValues
vtkDataArray* dummyArray = properties->NewInstance();
dummyArray->SetVoidArray(properties->GetVoidPointer(4), 3, 1);
// Read GAMINF, BETA, TINF
if ( this->ReadValues(qFp, 3, dummyArray) == 0)
// Read fsmach, alpha, re, time;
if ( this->ReadValues(qFp, 4, properties) != 4)
{
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;
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) == 0)
if (isOverflow)
{
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;
// We create a dummy array to use with ReadValues
vtkDataArray* dummyArray = properties->NewInstance();
dummyArray->SetVoidArray(properties->GetVoidPointer(4), 3, 1);
// Read GAMINF, BETA, TINF
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);
nthOutput->GetFieldData()->AddArray(properties);
......@@ -1548,6 +1613,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
density->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(density);
......@@ -1573,6 +1639,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
momentum->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(momentum);
......@@ -1594,6 +1661,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
se->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(se);
......@@ -1618,6 +1686,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
gamma->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(gamma);
......@@ -1645,6 +1714,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
temp->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(temp);
......@@ -1691,6 +1761,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
}
this->CloseFile(qFp2);
temp->Delete();
this->ClearGeometryCache();
return 0;
}
nthOutput->GetPointData()->AddArray(temp);
......@@ -1737,21 +1808,37 @@ int vtkMultiBlockPLOT3DReader::RequestData(
FILE* fFp = 0;
std::vector<int> nFunctions(numBlocks);
int error = 0;
if (rank == 0)
{
if ( this->CheckFunctionFile(fFp) != VTK_OK)
try
{
return 0;
}
if ( this->CheckFunctionFile(fFp) != VTK_OK)
{
throw Plot3DException();
}
if ( this->ReadFunctionHeader(fFp, &nFunctions[0]) != VTK_OK )
{
if ( this->ReadFunctionHeader(fFp, &nFunctions[0]) != VTK_OK )
{
fclose(fFp);
throw Plot3DException();
}
offset = vtk_ftell(fFp);
fclose(fFp);
return 0;
}
offset = vtk_ftell(fFp);
fclose(fFp);
catch (Plot3DException&)
{
error = 1;
}
}
mp->Broadcast(&error, 1, 0);
if (error)
{
vtkErrorMacro("Error reading file " << this->XYZFileName);
this->ClearGeometryCache();
return 0;
}
mp->Broadcast(&nFunctions[0], numBlocks, 0);
mp->Broadcast(&offset, 1, 0);
......@@ -1796,6 +1883,7 @@ int vtkMultiBlockPLOT3DReader::RequestData(
"the function file (or the file is corrupt).");
this->CloseFile(fFp2);
functionArray->Delete();
this->ClearGeometryCache();
return 0;
}
offset += nTotalPts*this->Internal->Settings.Precision;
......
......@@ -262,7 +262,7 @@ protected:
int SkipByteCount (FILE* fp);
int ReadIntBlock (FILE* fp, int n, int* block);
int ReadValues(
vtkIdType ReadValues(
FILE* fp,
int n,
vtkDataArray* scalar);
......
Markdown is supported
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