Octave interface: Rebuilt the riSetActiveCellProperties command.

Accept the new arguments, refactored to new framework, improved error handling.
Still needs to support specified timesteps and Grid Coarsening
p4#: 21713
This commit is contained in:
Jacob Støren
2013-05-24 11:36:59 +02:00
parent 7ba9cef84b
commit 767c1d2409
5 changed files with 452 additions and 180 deletions

View File

@@ -54,6 +54,261 @@
#include "cafFactory.h"
#include "RigGridBase.h"
#include "RiaSocketTools.h"
class RiaSetActiveCellProperty: public RiaSocketCommand
{
public:
RiaSetActiveCellProperty() :
m_currentReservoir(NULL),
m_scalarResultsToAdd(NULL),
m_currentScalarIndex(cvf::UNDEFINED_SIZE_T),
m_timeStepCountToRead(0),
m_bytesPerTimeStepToRead(0),
m_currentTimeStepToRead(0),
m_invalidActiveCellCountDetected(false),
m_porosityModelEnum(RifReaderInterface::MATRIX_RESULTS)
{}
static QString commandName () { return QString("SetActiveCellProperty"); }
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
RimCase* rimCase = RiaSocketTools::findCaseFromArgs(server, args);
QString propertyName = args[2];
QString porosityModelName = args[3];
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
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
std::vector< std::vector<double> >* scalarResultFrames = NULL;
if (rimCase && rimCase->results(m_porosityModelEnum))
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->findOrLoadScalarResult(propertyName);
if (scalarResultIndex == cvf::UNDEFINED_SIZE_T)
{
scalarResultIndex = rimCase->results(m_porosityModelEnum)->cellResults()->addEmptyScalarResult(RimDefines::GENERATED, propertyName, true);
}
if (scalarResultIndex != cvf::UNDEFINED_SIZE_T)
{
scalarResultFrames = &(rimCase->results(m_porosityModelEnum)->cellResults()->cellScalarResults(scalarResultIndex));
m_currentScalarIndex = scalarResultIndex;
m_currentPropertyName = propertyName;
}
}
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
m_requestedTimesteps.clear();
if (args.size() <= 4)
{
// Select all
for (size_t tsIdx = 0; tsIdx < scalarResultFrames->size(); ++tsIdx)
{
m_requestedTimesteps.push_back(tsIdx);
}
}
else
{
bool timeStepReadError = false;
for (int argIdx = 4; argIdx < args.size(); ++argIdx)
{
bool conversionOk = false;
int tsIdx = args[argIdx].toInt(&conversionOk);
if (conversionOk)
{
m_requestedTimesteps.push_back(tsIdx);
}
else
{
timeStepReadError = true;
}
}
if (timeStepReadError)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: riGetActiveCellProperty : \n") + RiaSocketServer::tr("An error occured while interpreting the requested timesteps."));
}
}
m_currentReservoir = rimCase;
m_scalarResultsToAdd = scalarResultFrames;
if (server->currentClient()->bytesAvailable())
{
return this->interpretMore(server, server->currentClient());
}
return false;
}
virtual bool interpretMore(RiaSocketServer* server, QTcpSocket* currentClient)
{
if (m_invalidActiveCellCountDetected) return true;
if (!currentClient->bytesAvailable()) return false;
QDataStream socketStream(currentClient);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
// If we have not read the header and there are data enough: Read it.
// Do nothing if we have not enough data
if (m_timeStepCountToRead == 0 || m_bytesPerTimeStepToRead == 0)
{
if (currentClient->bytesAvailable() < (int)sizeof(quint64)*2) return false;
socketStream >> m_timeStepCountToRead;
socketStream >> m_bytesPerTimeStepToRead;
}
// If nothing should be read, or we already have read everything, do nothing
if ((m_timeStepCountToRead == 0) || (m_currentTimeStepToRead >= m_timeStepCountToRead) ) return true;
// Check if a complete timestep is available, return and whait for readyRead() if not
if (currentClient->bytesAvailable() < (int)m_bytesPerTimeStepToRead) return false;
size_t cellCountFromOctave = m_bytesPerTimeStepToRead / sizeof(double);
size_t gridActiveCellCount = m_currentReservoir->reservoirData()->activeCellInfo(m_porosityModelEnum)->globalActiveCellCount();
size_t gridTotalCellCount = m_currentReservoir->reservoirData()->mainGrid()->cellCount();
if (cellCountFromOctave != gridActiveCellCount && cellCountFromOctave != gridTotalCellCount)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("The number of cells in the data coming from octave does not match the case") + ":\"" + m_currentReservoir->caseUserDescription() + "\"\n"
" Octave: " + QString::number(cellCountFromOctave) + "\n"
" " + m_currentReservoir->caseUserDescription() + ": Active cell count: " + QString::number(gridActiveCellCount) + " Total cell count: " + QString::number(gridTotalCellCount)) ;
cellCountFromOctave = 0;
m_invalidActiveCellCountDetected = true;
currentClient->abort();
return true;
}
// Make sure the size of the retreiving container is correct.
// If it is, this is noops
m_scalarResultsToAdd->resize(m_timeStepCountToRead);
for (size_t tIdx = 0; tIdx < m_timeStepCountToRead; ++tIdx)
{
m_scalarResultsToAdd->at(tIdx).resize(cellCountFromOctave, HUGE_VAL);
}
// Read available complete timestepdata
while ((currentClient->bytesAvailable() >= (int)m_bytesPerTimeStepToRead) && (m_currentTimeStepToRead < m_timeStepCountToRead))
{
qint64 bytesRead = 0;
double * internalMatrixData = m_scalarResultsToAdd->at(m_currentTimeStepToRead).data();
#if 1 // Use raw data transfer. Faster.
bytesRead = currentClient->read((char*)(internalMatrixData), m_bytesPerTimeStepToRead);
#else
for (size_t cIdx = 0; cIdx < cellCountFromOctave; ++cIdx)
{
socketStream >> internalMatrixData[cIdx];
if (socketStream.status() == QDataStream::Ok) bytesRead += sizeof(double);
}
#endif
if ((int)m_bytesPerTimeStepToRead != bytesRead)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") +
RiaSocketServer::tr("Could not read binary double data properly from socket"));
}
++m_currentTimeStepToRead;
}
// If we have read all the data, refresh the views
if (m_currentTimeStepToRead == 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;
std::vector< std::vector<double> >* m_scalarResultsToAdd;
size_t m_currentScalarIndex;
QString m_currentPropertyName;
std::vector<size_t> m_requestedTimesteps;
RifReaderInterface::PorosityModelResultType m_porosityModelEnum;
quint64 m_timeStepCountToRead;
quint64 m_bytesPerTimeStepToRead;
size_t m_currentTimeStepToRead;
bool m_invalidActiveCellCountDetected;
};
static bool RiaSetActiveCellProperty_init = RiaSocketCommandFactory::instance()->registerCreator<RiaSetActiveCellProperty>(RiaSetActiveCellProperty::commandName());
@@ -65,12 +320,12 @@ RiaSocketServer::RiaSocketServer(QObject* parent)
m_tcpServer(NULL),
m_currentClient(NULL),
m_currentCommandSize(0),
m_scalarResultsToAdd(NULL),
m_currentTimeStepToRead(0),
m_currentReservoir(NULL),
m_currentScalarIndex(cvf::UNDEFINED_SIZE_T),
m_invalidActiveCellCountDetected(false),
m_readState(ReadingCommand),
// m_scalarResultsToAdd(NULL),
// m_currentTimeStepToRead(0),
// m_currentReservoir(NULL),
// m_currentScalarIndex(cvf::UNDEFINED_SIZE_T),
// m_invalidActiveCellCountDetected(false),
// m_readState(ReadingCommand),
m_currentCommand(NULL)
{
m_errorMessageDialog = new QErrorMessage(RiuMainWindow::instance());
@@ -127,9 +382,15 @@ void RiaSocketServer::slotNewClientConnection()
}
else
{
if (m_readState == ReadingPropertyData)
if (m_currentCommand)
{
readPropertyDataFromOctave();
if (m_currentCommand->interpretMore(this, m_currentClient))
{
delete m_currentCommand;
m_currentCommand = NULL;
}
CVF_ASSERT(m_currentCommand == NULL);
}
terminateCurrentConnection();
@@ -154,17 +415,17 @@ void RiaSocketServer::handleClientConnection(QTcpSocket* clientToHandle)
// Initialize state varianbles
m_currentCommandSize = 0;
m_scalarResultsToAdd = NULL;
// m_scalarResultsToAdd = NULL;
m_timeStepCountToRead = 0;
m_bytesPerTimeStepToRead = 0;
m_currentTimeStepToRead = 0;
m_currentReservoir = NULL;
m_currentScalarIndex = cvf::UNDEFINED_SIZE_T;
m_currentPropertyName = "";
// m_timeStepCountToRead = 0;
// m_bytesPerTimeStepToRead = 0;
// m_currentTimeStepToRead = 0;
// m_currentReservoir = NULL;
// m_currentScalarIndex = cvf::UNDEFINED_SIZE_T;
// m_currentPropertyName = "";
connect(m_currentClient, SIGNAL(disconnected()), this, SLOT(slotCurrentClientDisconnected()));
m_readState = ReadingCommand;
//m_readState = ReadingCommand;
m_currentCommand = NULL;
if (m_currentClient->bytesAvailable())
@@ -242,7 +503,10 @@ void RiaSocketServer::readCommandFromOctave()
CVF_ASSERT(args.size() > 0);
std::cout << args[0].data() << std::endl;
m_currentCommand = RiaSocketCommandFactory::instance()->create(args[0]);
if (m_currentCommand)
{
bool finished = m_currentCommand->interpretCommand(this, args, socketStream);
@@ -256,97 +520,12 @@ void RiaSocketServer::readCommandFromOctave()
{
// Todo: When all commands are into new shape, do the "unknown command" error output here.
bool isSetProperty = args[0] == "SetProperty"; // SetProperty [casename/index] PropertyName
if (!( isSetProperty ))
{
m_errorMessageDialog->showMessage(tr("ResInsight SocketServer: \n") + tr("Unknown command: %1").arg(args[0].data()));
terminateCurrentConnection();
return;
}
int caseId = -1;
QString propertyName;
RimCase* rimCase = NULL;
// Find the correct arguments
if (isSetProperty)
{
if (args.size() == 2)
{
propertyName = args[1];
}
else if (args.size() > 2)
{
caseId = args[1].toInt();
propertyName = args[2];
}
}
rimCase = this->findReservoir(caseId);
if (rimCase == NULL)
{
m_errorMessageDialog->showMessage(tr("ResInsight SocketServer: \n") + tr("Could not find the Case with CaseId : \"%1\"").arg(caseId));
return;
}
if (isSetProperty)
{
// Find the requested data, Or create a set if we are setting data and it is not found
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
std::vector< std::vector<double> >* scalarResultFrames = NULL;
if (rimCase && rimCase->results(RifReaderInterface::MATRIX_RESULTS))
{
scalarResultIndex = rimCase->results(RifReaderInterface::MATRIX_RESULTS)->findOrLoadScalarResult(propertyName);
if (scalarResultIndex == cvf::UNDEFINED_SIZE_T && isSetProperty)
{
scalarResultIndex = rimCase->results(RifReaderInterface::MATRIX_RESULTS)->cellResults()->addEmptyScalarResult(RimDefines::GENERATED, propertyName, true);
}
if (scalarResultIndex != cvf::UNDEFINED_SIZE_T)
{
scalarResultFrames = &(rimCase->results(RifReaderInterface::MATRIX_RESULTS)->cellResults()->cellScalarResults(scalarResultIndex));
m_currentScalarIndex = scalarResultIndex;
m_currentPropertyName = propertyName;
}
}
if (scalarResultFrames == NULL)
{
m_errorMessageDialog->showMessage(tr("ResInsight SocketServer: \n") + tr("Could not find the property named: \"%1\"").arg(propertyName));
}
// Set property
{
m_readState = ReadingPropertyData;
// Disconnect the socket from calling this slot again.
m_currentReservoir = rimCase;
if ( scalarResultFrames != NULL)
{
m_scalarResultsToAdd = scalarResultFrames;
if (m_currentClient->bytesAvailable())
{
this->readPropertyDataFromOctave();
}
}
}
}
}
}
#if 0
//--------------------------------------------------------------------------------------------------
/// This method reads data from octave and puts it into the resInsight Structures
//--------------------------------------------------------------------------------------------------
@@ -466,12 +645,14 @@ void RiaSocketServer::readPropertyDataFromOctave()
}
}
}
#endif
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaSocketServer::slotCurrentClientDisconnected()
{
#if 0
if (m_timeStepCountToRead > 0
&& m_currentTimeStepToRead < m_timeStepCountToRead
&& m_currentClient->bytesAvailable()
@@ -479,7 +660,21 @@ void RiaSocketServer::slotCurrentClientDisconnected()
{
this->readPropertyDataFromOctave();
}
#else
if (m_currentCommand)
{
if (m_currentCommand->interpretMore(this, m_currentClient))
{
delete m_currentCommand;
m_currentCommand = NULL;
}
/// What do we do here ?
CVF_ASSERT(m_currentCommand == NULL);
}
#endif
terminateCurrentConnection();
QTcpSocket *newClient = m_tcpServer->nextPendingConnection();
@@ -505,17 +700,23 @@ void RiaSocketServer::terminateCurrentConnection()
// Clean up more state:
m_currentCommandSize = 0;
m_timeStepCountToRead = 0;
m_bytesPerTimeStepToRead = 0;
m_currentTimeStepToRead = 0;
m_scalarResultsToAdd = NULL;
m_currentReservoir = NULL;
m_currentScalarIndex = cvf::UNDEFINED_SIZE_T;
m_currentPropertyName = "";
m_invalidActiveCellCountDetected = false;
if (m_currentCommand)
{
delete m_currentCommand;
m_currentCommand = NULL;
}
m_readState = ReadingCommand;
m_currentCommandSize = 0;
// m_timeStepCountToRead = 0;
// m_bytesPerTimeStepToRead = 0;
// m_currentTimeStepToRead = 0;
// m_scalarResultsToAdd = NULL;
// m_currentReservoir = NULL;
// m_currentScalarIndex = cvf::UNDEFINED_SIZE_T;
// m_currentPropertyName = "";
// m_invalidActiveCellCountDetected = false;
// m_readState = ReadingCommand;
}
@@ -524,6 +725,7 @@ void RiaSocketServer::terminateCurrentConnection()
//--------------------------------------------------------------------------------------------------
void RiaSocketServer::slotReadyRead()
{
#if 0
switch (m_readState)
{
case ReadingCommand :
@@ -542,5 +744,19 @@ void RiaSocketServer::slotReadyRead()
CVF_ASSERT(false);
break;
}
#else
if (m_currentCommand)
{
if (m_currentCommand->interpretMore(this, m_currentClient))
{
delete m_currentCommand;
m_currentCommand = NULL;
}
}
else
{
readCommandFromOctave();
}
#endif
}