Test for ijk dimention match before reading data

Whitespace fixup
p4#: 21749
This commit is contained in:
Magne Sjaastad 2013-05-28 11:11:32 +02:00
parent 3b05c59b55
commit 408795c0fc

View File

@ -51,7 +51,6 @@ public:
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
RimCase* rimCase = RiaSocketTools::findCaseFromArgs(server, args);
QString propertyName = args[2];
@ -679,277 +678,296 @@ public:
m_currentTimeStepNumberToRead(0),
m_invalidDataDetected(false),
m_porosityModelEnum(RifReaderInterface::MATRIX_RESULTS)
{}
{}
static QString commandName () { return QString("SetGridProperty"); }
static QString commandName () { return QString("SetGridProperty"); }
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
int caseId = args[1].toInt();
RimCase* rimCase = server->findReservoir(caseId);
if (!rimCase)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the case with ID : \"%1\"").arg(caseId));
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
int caseId = args[1].toInt();
RimCase* rimCase = server->findReservoir(caseId);
if (!rimCase)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the case with ID : \"%1\"").arg(caseId));
return true;
}
return true;
}
m_currentGridIndex = args[2].toInt();
QString propertyName = args[3];
QString porosityModelName = args[4];
m_currentGridIndex = args[2].toInt();
QString propertyName = args[3];
QString porosityModelName = args[4];
if (porosityModelName == "Fracture")
{
m_porosityModelEnum = RifReaderInterface::FRACTURE_RESULTS;
}
if (porosityModelName == "Fracture")
{
m_porosityModelEnum = RifReaderInterface::FRACTURE_RESULTS;
}
// Find the requested data, Or create a set if we are setting data and it is not found
if (server->currentClient()->bytesAvailable() < (int)sizeof(quint64)*5) return true;
quint64 cellCountI = 0;
quint64 cellCountJ = 0;
quint64 cellCountK = 0;
socketStream >> cellCountI;
socketStream >> cellCountJ;
socketStream >> cellCountK;
socketStream >> m_timeStepCountToRead;
socketStream >> m_bytesPerTimeStepToRead;
RigGridBase* grid = rimCase->reservoirData()->grid(m_currentGridIndex);
if (!grid)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the grid index : %1").arg(m_currentGridIndex));
return true;
}
// Read header
if (server->currentClient()->bytesAvailable() < (int)sizeof(quint64)*5) return true;
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
std::vector< std::vector<double> >* scalarResultFrames = NULL;
quint64 cellCountI = 0;
quint64 cellCountJ = 0;
quint64 cellCountK = 0;
socketStream >> cellCountI;
socketStream >> cellCountJ;
socketStream >> cellCountK;
if (rimCase && rimCase->results(m_porosityModelEnum))
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->findOrLoadScalarResult(RimDefines::GENERATED, propertyName);
if (grid->cellCountI() != cellCountI ||
grid->cellCountJ() != cellCountJ ||
grid->cellCountK() != cellCountK)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Destination grid size do not match incoming grid size for grid index : %1").arg(m_currentGridIndex));
return true;
}
if (scalarResultIndex == cvf::UNDEFINED_SIZE_T)
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->cellResults()->addEmptyScalarResult(RimDefines::GENERATED, propertyName, true);
}
socketStream >> m_timeStepCountToRead;
socketStream >> m_bytesPerTimeStepToRead;
if (scalarResultIndex != cvf::UNDEFINED_SIZE_T)
{
scalarResultFrames = &(rimCase->results(m_porosityModelEnum)->cellResults()->cellScalarResults(scalarResultIndex));
m_currentScalarIndex = scalarResultIndex;
m_currentPropertyName = propertyName;
}
}
if (m_timeStepCountToRead == 0 || m_bytesPerTimeStepToRead == 0)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("Zero data to read for ") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n");
}
if (scalarResultFrames == NULL)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the %1 model property named: \"%2\"").arg(porosityModelName).arg(propertyName));
return true;
}
// Create a list of all the requested timesteps
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
std::vector< std::vector<double> >* scalarResultFrames = NULL;
m_requestedTimesteps.clear();
if (rimCase && rimCase->results(m_porosityModelEnum))
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->findOrLoadScalarResult(RimDefines::GENERATED, propertyName);
if (args.size() <= 5)
{
// Select all
for (size_t tsIdx = 0; tsIdx < m_timeStepCountToRead; ++tsIdx)
{
m_requestedTimesteps.push_back(tsIdx);
}
}
else
{
bool timeStepReadError = false;
for (int argIdx = 5; argIdx < args.size(); ++argIdx)
{
bool conversionOk = false;
int tsIdx = args[argIdx].toInt(&conversionOk);
if (scalarResultIndex == cvf::UNDEFINED_SIZE_T)
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->cellResults()->addEmptyScalarResult(RimDefines::GENERATED, propertyName, true);
}
if (conversionOk)
{
m_requestedTimesteps.push_back(tsIdx);
}
else
{
timeStepReadError = true;
}
}
if (scalarResultIndex != cvf::UNDEFINED_SIZE_T)
{
scalarResultFrames = &(rimCase->results(m_porosityModelEnum)->cellResults()->cellScalarResults(scalarResultIndex));
m_currentScalarIndex = scalarResultIndex;
m_currentPropertyName = propertyName;
}
}
if (timeStepReadError)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: riGetActiveCellProperty : \n") + RiaSocketServer::tr("An error occured while interpreting the requested timesteps."));
}
if (scalarResultFrames == NULL)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the %1 model property named: \"%2\"").arg(porosityModelName).arg(propertyName));
return true;
}
}
// Create a list of all the requested timesteps
if (! m_requestedTimesteps.size())
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("No time steps specified").arg(porosityModelName).arg(propertyName));
m_requestedTimesteps.clear();
return true;
}
if (args.size() <= 5)
{
// Select all
for (size_t tsIdx = 0; tsIdx < m_timeStepCountToRead; ++tsIdx)
{
m_requestedTimesteps.push_back(tsIdx);
}
}
else
{
bool timeStepReadError = false;
for (int argIdx = 5; argIdx < args.size(); ++argIdx)
{
bool conversionOk = false;
int tsIdx = args[argIdx].toInt(&conversionOk);
// Resize the result container to be able to receive time steps at the specified time step indices
if (conversionOk)
{
m_requestedTimesteps.push_back(tsIdx);
}
else
{
timeStepReadError = true;
}
}
std::vector<size_t>::iterator maxTimeStepIt = std::max_element(m_requestedTimesteps.begin(), m_requestedTimesteps.end());
CVF_ASSERT(maxTimeStepIt != m_requestedTimesteps.end());
if (timeStepReadError)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: riGetActiveCellProperty : \n") + RiaSocketServer::tr("An error occured while interpreting the requested timesteps."));
}
size_t maxTimeStepIdx = (*maxTimeStepIt);
if (scalarResultFrames->size() <= maxTimeStepIdx)
{
scalarResultFrames->resize(maxTimeStepIdx+1);
}
}
m_currentReservoir = rimCase;
m_scalarResultsToAdd = scalarResultFrames;
if (! m_requestedTimesteps.size())
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("No time steps specified").arg(porosityModelName).arg(propertyName));
if (server->currentClient()->bytesAvailable())
{
return this->interpretMore(server, server->currentClient());
}
return true;
}
return false;
}
// Resize the result container to be able to receive time steps at the specified time step indices
virtual bool interpretMore(RiaSocketServer* server, QTcpSocket* currentClient)
{
if (m_invalidDataDetected) return true;
std::vector<size_t>::iterator maxTimeStepIt = std::max_element(m_requestedTimesteps.begin(), m_requestedTimesteps.end());
CVF_ASSERT(maxTimeStepIt != m_requestedTimesteps.end());
if (!currentClient->bytesAvailable()) return false;
size_t maxTimeStepIdx = (*maxTimeStepIt);
if (scalarResultFrames->size() <= maxTimeStepIdx)
{
scalarResultFrames->resize(maxTimeStepIdx+1);
}
QDataStream socketStream(currentClient);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
m_currentReservoir = rimCase;
m_scalarResultsToAdd = scalarResultFrames;
RigGridBase* grid = m_currentReservoir->reservoirData()->grid(m_currentGridIndex);
if (!grid)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("No grid found") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n");
if (server->currentClient()->bytesAvailable())
{
return this->interpretMore(server, server->currentClient());
}
m_invalidDataDetected = true;
currentClient->abort();
return false;
}
return true;
}
virtual bool interpretMore(RiaSocketServer* server, QTcpSocket* currentClient)
{
if (m_invalidDataDetected) return true;
// If we have not read the header and there are data enough: Read it.
// Do nothing if we have not enough data
if (!currentClient->bytesAvailable()) return false;
if (m_timeStepCountToRead == 0 || m_bytesPerTimeStepToRead == 0)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("Zero data to read for ") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n");
QDataStream socketStream(currentClient);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
m_invalidDataDetected = true;
currentClient->abort();
RigGridBase* grid = m_currentReservoir->reservoirData()->grid(m_currentGridIndex);
if (!grid)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("No grid found") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n");
return true;
}
m_invalidDataDetected = true;
currentClient->abort();
if (m_timeStepCountToRead != m_requestedTimesteps.size())
{
CVF_ASSERT(false);
}
return true;
}
// If nothing should be read, or we already have read everything, do nothing
// Do nothing if we have not enough data
if (m_timeStepCountToRead == 0 || m_bytesPerTimeStepToRead == 0)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("Zero data to read for ") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n");
if ((m_timeStepCountToRead == 0) || (m_currentTimeStepNumberToRead >= m_timeStepCountToRead) ) return true;
m_invalidDataDetected = true;
currentClient->abort();
// Check if a complete timestep is available, return and whait for readyRead() if not
if (currentClient->bytesAvailable() < (int)m_bytesPerTimeStepToRead) return false;
return true;
}
size_t cellCountFromOctave = m_bytesPerTimeStepToRead / sizeof(double);
if (m_timeStepCountToRead != m_requestedTimesteps.size())
{
CVF_ASSERT(false);
}
RigActiveCellInfo* activeCellInfo = m_currentReservoir->reservoirData()->activeCellInfo(m_porosityModelEnum);
size_t globalCellResultCount = activeCellInfo->globalCellResultCount();
// If nothing should be read, or we already have read everything, do nothing
// Make sure the size of the retreiving container is correct.
// If it is, this is noops
if ((m_timeStepCountToRead == 0) || (m_currentTimeStepNumberToRead >= m_timeStepCountToRead) ) return true;
for (size_t tIdx = 0; tIdx < m_timeStepCountToRead; ++tIdx)
{
size_t tsId = m_requestedTimesteps[tIdx];
m_scalarResultsToAdd->at(tsId).resize(globalCellResultCount, HUGE_VAL);
}
// Check if a complete timestep is available, return and whait for readyRead() if not
if (currentClient->bytesAvailable() < (int)m_bytesPerTimeStepToRead) return false;
if ((currentClient->bytesAvailable() >= (int)m_bytesPerTimeStepToRead) && (m_currentTimeStepNumberToRead < m_timeStepCountToRead))
{
// Read a single time step with data
size_t cellCountFromOctave = m_bytesPerTimeStepToRead / sizeof(double);
std::vector<double> doubleValues(cellCountFromOctave);
RigActiveCellInfo* activeCellInfo = m_currentReservoir->reservoirData()->activeCellInfo(m_porosityModelEnum);
size_t globalCellResultCount = activeCellInfo->globalCellResultCount();
qint64 bytesRead = currentClient->read((char*)(doubleValues.data()), m_bytesPerTimeStepToRead);
size_t doubleValueIndex = 0;
// Make sure the size of the retreiving container is correct.
// If it is, this is noops
for (size_t k = 0; k < grid->cellCountK(); k++)
{
for (size_t j = 0; j < grid->cellCountJ(); j++)
{
for (size_t i = 0; i < grid->cellCountI(); i++)
{
size_t localCellIndex = grid->cellIndexFromIJK(i,j,k);
size_t gcIdx = grid->globalGridCellIndex(localCellIndex);
for (size_t tIdx = 0; tIdx < m_timeStepCountToRead; ++tIdx)
{
size_t tsId = m_requestedTimesteps[tIdx];
m_scalarResultsToAdd->at(tsId).resize(globalCellResultCount, HUGE_VAL);
}
size_t resultIdx = activeCellInfo->cellResultIndex(gcIdx);
if ((currentClient->bytesAvailable() >= (int)m_bytesPerTimeStepToRead) && (m_currentTimeStepNumberToRead < m_timeStepCountToRead))
{
// Read a single time step with data
if (resultIdx < m_scalarResultsToAdd->at(m_requestedTimesteps[m_currentTimeStepNumberToRead]).size())
{
m_scalarResultsToAdd->at(m_requestedTimesteps[m_currentTimeStepNumberToRead])[resultIdx] = doubleValues[doubleValueIndex];
}
std::vector<double> doubleValues(cellCountFromOctave);
doubleValueIndex++;
}
}
}
qint64 bytesRead = currentClient->read((char*)(doubleValues.data()), m_bytesPerTimeStepToRead);
size_t doubleValueIndex = 0;
++m_currentTimeStepNumberToRead;
}
for (size_t k = 0; k < grid->cellCountK(); k++)
{
for (size_t j = 0; j < grid->cellCountJ(); j++)
{
for (size_t i = 0; i < grid->cellCountI(); i++)
{
size_t localCellIndex = grid->cellIndexFromIJK(i,j,k);
size_t gcIdx = grid->globalGridCellIndex(localCellIndex);
// If we have read all the data, refresh the views
size_t resultIdx = activeCellInfo->cellResultIndex(gcIdx);
if (m_currentTimeStepNumberToRead == m_timeStepCountToRead)
{
if (m_currentReservoir != NULL)
{
// Create a new input property if we have an input reservoir
RimInputCase* inputRes = dynamic_cast<RimInputCase*>(m_currentReservoir);
if (inputRes)
{
RimInputProperty* inputProperty = NULL;
inputProperty = inputRes->m_inputPropertyCollection->findInputProperty(m_currentPropertyName);
if (!inputProperty)
{
inputProperty = new RimInputProperty;
inputProperty->resultName = m_currentPropertyName;
inputProperty->eclipseKeyword = "";
inputProperty->fileName = "";
inputRes->m_inputPropertyCollection->inputProperties.push_back(inputProperty);
RimUiTreeModelPdm* treeModel = RiuMainWindow::instance()->uiPdmModel();
treeModel->updateUiSubTree(inputRes->m_inputPropertyCollection());
}
inputProperty->resolvedState = RimInputProperty::RESOLVED_NOT_SAVED;
}
if (resultIdx < m_scalarResultsToAdd->at(m_requestedTimesteps[m_currentTimeStepNumberToRead]).size())
{
m_scalarResultsToAdd->at(m_requestedTimesteps[m_currentTimeStepNumberToRead])[resultIdx] = doubleValues[doubleValueIndex];
}
if( m_currentScalarIndex != cvf::UNDEFINED_SIZE_T &&
m_currentReservoir->reservoirData() &&
m_currentReservoir->reservoirData()->results(m_porosityModelEnum) )
{
m_currentReservoir->reservoirData()->results(m_porosityModelEnum)->recalculateMinMax(m_currentScalarIndex);
}
doubleValueIndex++;
}
}
}
for (size_t i = 0; i < m_currentReservoir->reservoirViews.size(); ++i)
{
if (m_currentReservoir->reservoirViews[i])
{
m_currentReservoir->reservoirViews[i]->updateCurrentTimeStepAndRedraw();
}
}
}
++m_currentTimeStepNumberToRead;
}
return true;
}
// If we have read all the data, refresh the views
return false;
}
if (m_currentTimeStepNumberToRead == m_timeStepCountToRead)
{
if (m_currentReservoir != NULL)
{
// Create a new input property if we have an input reservoir
RimInputCase* inputRes = dynamic_cast<RimInputCase*>(m_currentReservoir);
if (inputRes)
{
RimInputProperty* inputProperty = NULL;
inputProperty = inputRes->m_inputPropertyCollection->findInputProperty(m_currentPropertyName);
if (!inputProperty)
{
inputProperty = new RimInputProperty;
inputProperty->resultName = m_currentPropertyName;
inputProperty->eclipseKeyword = "";
inputProperty->fileName = "";
inputRes->m_inputPropertyCollection->inputProperties.push_back(inputProperty);
RimUiTreeModelPdm* treeModel = RiuMainWindow::instance()->uiPdmModel();
treeModel->updateUiSubTree(inputRes->m_inputPropertyCollection());
}
inputProperty->resolvedState = RimInputProperty::RESOLVED_NOT_SAVED;
}
if( m_currentScalarIndex != cvf::UNDEFINED_SIZE_T &&
m_currentReservoir->reservoirData() &&
m_currentReservoir->reservoirData()->results(m_porosityModelEnum) )
{
m_currentReservoir->reservoirData()->results(m_porosityModelEnum)->recalculateMinMax(m_currentScalarIndex);
}
for (size_t i = 0; i < m_currentReservoir->reservoirViews.size(); ++i)
{
if (m_currentReservoir->reservoirViews[i])
{
m_currentReservoir->reservoirViews[i]->updateCurrentTimeStepAndRedraw();
}
}
}
return true;
}
return false;
}
private:
RimCase* m_currentReservoir;