#1675 Selected cell interface for Octave

This commit is contained in:
Bjørnar Grip Fjær 2017-08-03 14:35:46 +02:00
parent fc8eefb5fe
commit cf2df771d4
5 changed files with 596 additions and 0 deletions

View File

@ -29,6 +29,10 @@
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigMainGrid.h"
#include "RigGeoMechCaseData.h"
#include "RigFemPartCollection.h"
#include "RigFemPart.h"
#include "RigFemPartGrid.h"
#include "Rim3dOverlayInfoConfig.h"
#include "RimCellEdgeColors.h"
@ -39,6 +43,10 @@
#include "RimEclipseView.h"
#include "RimEclipseWellCollection.h"
#include "RimReservoirCellResultsStorage.h"
#include "RimGeoMechCase.h"
#include "RimGeoMechView.h"
#include "RiuSelectionManager.h"
#include <QTcpSocket>
@ -538,3 +546,95 @@ public:
static bool RiaGetTimeStepDays_init = RiaSocketCommandFactory::instance()->registerCreator<RiaGetTimeStepDays>(RiaGetTimeStepDays::commandName());
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiaGetSelectedCells: public RiaSocketCommand
{
public:
static QString commandName () { return QString("GetSelectedCells"); }
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
// findCaseFromArgs only returns RimEclipseCase, so geomech cases are not supported because of this.
// The rest of the function supports geomech cases, so using a findCaseFromArgs that supports geomech
// cases is the only change needed to add geomech support.
RimEclipseCase* rimCase = RiaSocketTools::findCaseFromArgs(server, args);
if (!rimCase) return true;
// Write data back to octave: column count, bytes per column, caseId, gridNumber, cellI, cellJ, cellK
std::array<std::vector<qint32>, 5> selectedCellInfo;
getSelectedCells(rimCase,
selectedCellInfo[0],
selectedCellInfo[1],
selectedCellInfo[2],
selectedCellInfo[3],
selectedCellInfo[4]);
// First write column count
quint64 columnCount = 5;
socketStream << columnCount;
// then the byte-size of the size of one column
quint64 columnByteCount = (quint64)(selectedCellInfo[0].size()*sizeof(qint32));
socketStream << columnByteCount;
// Write back table data
for (size_t tIdx = 0; tIdx < columnCount; ++tIdx)
{
RiaSocketTools::writeBlockData(server, server->currentClient(), (const char *)selectedCellInfo[tIdx].data(), columnByteCount);
}
return true;
}
static void getSelectedCells(const RimCase* reservoirCase,
std::vector<qint32>& caseNumber,
std::vector<qint32>& gridNumber,
std::vector<qint32>& cellI,
std::vector<qint32>& cellJ,
std::vector<qint32>& cellK)
{
std::vector<RiuSelectionItem*> items;
RiuSelectionManager::instance()->selectedItems(items);
for (const RiuSelectionItem* item : items)
{
size_t i, j, k;
size_t gridIndex;
size_t caseId;
if (item->type() == RiuSelectionItem::ECLIPSE_SELECTION_OBJECT)
{
const RiuEclipseSelectionItem* eclipseItem = static_cast<const RiuEclipseSelectionItem*>(item);
eclipseItem->m_view->eclipseCase()->eclipseCaseData()->grid(eclipseItem->m_gridIndex)->ijkFromCellIndex(eclipseItem->m_cellIndex, &i, &j, &k);
gridIndex = eclipseItem->m_gridIndex;
caseId = eclipseItem->m_view->eclipseCase()->caseId;
}
else if (item->type() == RiuSelectionItem::GEOMECH_SELECTION_OBJECT)
{
const RiuGeoMechSelectionItem* geomechItem = static_cast<const RiuGeoMechSelectionItem*>(item);
geomechItem->m_view->geoMechCase()->geoMechData()->femParts()->part(geomechItem->m_gridIndex)->structGrid()->ijkFromCellIndex(geomechItem->m_cellIndex, &i, &j, &k);
gridIndex = geomechItem->m_gridIndex;
caseId = geomechItem->m_view->geoMechCase()->caseId;
}
else
{
continue;
}
if (caseId == reservoirCase->caseId)
{
caseNumber.push_back(caseId);
gridNumber.push_back(gridIndex);
cellI.push_back(i + 1);
cellJ.push_back(j + 1);
cellK.push_back(k + 1);
}
}
}
};
static bool RiaGetSelectedCells_init = RiaSocketCommandFactory::instance()->registerCreator<RiaGetSelectedCells>(RiaGetSelectedCells::commandName());

View File

@ -38,9 +38,12 @@
#include "RimEclipseInputPropertyCollection.h"
#include "RimEclipseView.h"
#include "RimReservoirCellResultsStorage.h"
#include "RimGeoMechView.h"
#include "RimGeoMechCase.h"
#include "RiuMainWindow.h"
#include "RiuProcessMonitor.h"
#include "RiuSelectionManager.h"
#include <QErrorMessage>
@ -1152,3 +1155,163 @@ public:
};
static bool RiaGetPropertyNames_init = RiaSocketCommandFactory::instance()->registerCreator<RiaGetPropertyNames>(RiaGetPropertyNames::commandName());
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiaGetGridPropertyForSelectedCells: public RiaSocketCommand
{
public:
static QString commandName() { return QString("GetGridPropertyForSelectedCells"); }
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
RimEclipseCase* rimCase = RiaSocketTools::findCaseFromArgs(server, args);
if (!rimCase) return true;
QString propertyName = args[2];
RifReaderInterface::PorosityModelResultType porosityModel = RifReaderInterface::MATRIX_RESULTS;
if (args.size() > 1)
{
QString prorosityModelString = args[3];
if (prorosityModelString.toUpper() == "FRACTURE")
{
porosityModel = RifReaderInterface::FRACTURE_RESULTS;
}
}
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
if (rimCase && rimCase->results(porosityModel))
{
scalarResultIndex = rimCase->results(porosityModel)->findOrLoadScalarResult(propertyName);
}
std::vector<size_t> requestedTimesteps;
if (args.size() < 5)
{
// Select all
for (size_t tsIdx = 0; tsIdx < rimCase->results(porosityModel)->cellResults()->timeStepCount(scalarResultIndex); ++tsIdx)
{
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)
{
requestedTimesteps.push_back(tsIdx);
}
else
{
timeStepReadError = true;
}
}
if (timeStepReadError)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: riGetGridProperty : \n")
+ RiaSocketServer::tr("An error occurred while interpreting the requested time steps."));
}
}
if (!(rimCase && rimCase->eclipseCaseData() && rimCase->eclipseCaseData()->mainGrid()) )
{
// No data available
socketStream << (quint64)0 << (quint64)0 ;
return true;
}
std::vector< std::pair<size_t, size_t> > selectedCells = getSelectedCellsForCase(rimCase);
// First write column count
quint64 timestepCount = (quint64)requestedTimesteps.size();
socketStream << timestepCount;
// then the byte-size of the size of one column
quint64 timestepByteCount = (quint64)(selectedCells.size()*sizeof(double));
socketStream << timestepByteCount;
size_t valueCount = RiaSocketDataTransfer::maximumValueCountInBlock();
std::vector<double> values(valueCount);
size_t valueIndex = 0;
for (size_t timeStep : requestedTimesteps)
{
const std::vector<double>& scalarResults = rimCase->results(porosityModel)->cellResults()->cellScalarResults(scalarResultIndex, timeStep);
for (const std::pair<size_t, size_t> selectedCell : selectedCells)
{
cvf::ref<RigResultAccessor> resultAccessor = RigResultAccessorFactory::createFromUiResultName(rimCase->eclipseCaseData(), selectedCell.first, porosityModel, timeStep, propertyName);
if (resultAccessor.isNull())
{
return false;
}
values[valueIndex] = resultAccessor->cellScalar(selectedCell.second);
valueIndex++;
if (valueIndex >= valueCount)
{
if (!RiaSocketTools::writeBlockData(server, server->currentClient(), (const char *)values.data(), valueIndex * sizeof(double)))
{
return false;
}
valueIndex = 0;
}
}
}
// Write remaining data
if (!RiaSocketTools::writeBlockData(server, server->currentClient(), (const char *)values.data(), valueIndex * sizeof(double)))
{
return false;
}
return true;
}
static std::vector< std::pair<size_t, size_t> > getSelectedCellsForCase(const RimCase* reservoirCase)
{
std::vector<RiuSelectionItem*> items;
RiuSelectionManager::instance()->selectedItems(items);
std::vector< std::pair<size_t, size_t> > selectedCells;
for (const RiuSelectionItem* item : items)
{
if (item->type() == RiuSelectionItem::ECLIPSE_SELECTION_OBJECT)
{
const RiuEclipseSelectionItem* eclipseItem = static_cast<const RiuEclipseSelectionItem*>(item);
if (eclipseItem->m_view->eclipseCase()->caseId == reservoirCase->caseId)
{
selectedCells.push_back(std::make_pair(eclipseItem->m_gridIndex, eclipseItem->m_cellIndex));
}
}
else if (item->type() == RiuSelectionItem::GEOMECH_SELECTION_OBJECT)
{
const RiuGeoMechSelectionItem* geomechItem = static_cast<const RiuGeoMechSelectionItem*>(item);
if (geomechItem->m_view->geoMechCase()->caseId == reservoirCase->caseId)
{
selectedCells.push_back(std::make_pair(geomechItem->m_gridIndex, geomechItem->m_cellIndex));
}
}
}
return selectedCells;
}
};
static bool RiaGetGridPropertyForSelectedCells_init = RiaSocketCommandFactory::instance()->registerCreator<RiaGetGridPropertyForSelectedCells>(RiaGetGridPropertyForSelectedCells::commandName());

View File

@ -14,6 +14,7 @@ set(CPP_SOURCES
riGetDynamicNNCValues.cpp
riGetStaticNNCValues.cpp
riGetSelectedCases.cpp
riGetSelectedCells.cpp
riGetCases.cpp
riGetTimeStepDates.cpp
riGetTimeStepDays.cpp
@ -25,6 +26,7 @@ set(CPP_SOURCES
riGetActiveCellCorners.cpp
riGetGridProperty.cpp
riSetGridProperty.cpp
riGetGridPropertyForSelectedCells.cpp
riGetPropertyNames.cpp
riGetWellNames.cpp
riGetWellStatus.cpp
@ -181,6 +183,7 @@ if (RESINSIGHT_OCTAVE_PLUGIN_QMAKE AND RESINSIGHT_OCTAVE_PLUGIN_MKOCTFILE)
"${CMAKE_CURRENT_BINARY_DIR}/riGetCurrentCase.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCaseGroups.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetSelectedCases.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetSelectedCells.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCases.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetTimeStepDates.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetTimeStepDays.oct"
@ -192,6 +195,7 @@ if (RESINSIGHT_OCTAVE_PLUGIN_QMAKE AND RESINSIGHT_OCTAVE_PLUGIN_MKOCTFILE)
"${CMAKE_CURRENT_BINARY_DIR}/riGetActiveCellCorners.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetGridProperty.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riSetGridProperty.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetGridPropertyForSelectedCells.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetPropertyNames.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetWellNames.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetWellStatus.oct"

View File

@ -0,0 +1,199 @@
#include <QtNetwork>
#include <QStringList>
#include <octave/oct.h>
#include "riSettings.h"
#include "RiaSocketDataTransfer.cpp" // NB! Include cpp-file to avoid linking of additional file in oct-compile configuration
void getGridPropertyForSelectedCells(Matrix& propertyFrames, const QString &serverName, quint16 serverPort,
const qint64& caseId, QString propertyName, const int32NDArray& requestedTimeSteps, QString porosityModel)
{
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
if (!socket.waitForConnected(riOctavePlugin::connectTimeOutMilliSecs))
{
error((("Connection: ") + socket.errorString()).toLatin1().data());
return;
}
QDataStream socketStream(&socket);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
// Create command as a string with arguments , and send it:
QString command;
command += "GetGridPropertyForSelectedCells " + QString::number(caseId) + " " + propertyName + " " + porosityModel;
for (int i = 0; i < requestedTimeSteps.length(); ++i)
{
if (i == 0) command += " ";
command += QString::number(static_cast<int>(requestedTimeSteps.elem(i)) - 1); // To make the index 0-based
if (i != requestedTimeSteps.length() - 1) command += " ";
}
QByteArray cmdBytes = command.toLatin1();
socketStream << (qint64)(cmdBytes.size());
socket.write(cmdBytes);
// Get response. First wait for the header
while (socket.bytesAvailable() < (int)(2*sizeof(quint64)))
{
if (!socket.waitForReadyRead(riOctavePlugin::longTimeOutMilliSecs))
{
error((("Waiting for header: ") + socket.errorString()).toLatin1().data());
return;
}
}
// Read timestep count and blocksize
quint64 timestepCount;
quint64 byteCount;
size_t selectedCellCount;
socketStream >> timestepCount;
socketStream >> byteCount;
selectedCellCount = byteCount / sizeof(double);
propertyFrames.resize(selectedCellCount, timestepCount);
if (!(byteCount && timestepCount))
{
error ("Could not find the requested data in ResInsight");
return;
}
quint64 totalByteCount = byteCount * timestepCount;
double* internalMatrixData = propertyFrames.fortran_vec();
QStringList errorMessages;
if (!RiaSocketDataTransfer::readBlockDataFromSocket(&socket, (char*)(internalMatrixData), totalByteCount, errorMessages))
{
for (int i = 0; i < errorMessages.size(); i++)
{
error(errorMessages[i].toLatin1().data());
}
return;
}
QString tmp = QString("riGetGridPropertyForSelectedCells : Read %1").arg(propertyName);
if (caseId < 0)
{
tmp += QString(" from current case.");
}
else
{
tmp += QString(" from case with Id: %1.").arg(caseId);
}
octave_stdout << tmp.toStdString() << " Selected cells cells : " << selectedCellCount << ", Time steps : " << timestepCount << std::endl;
return;
}
DEFUN_DLD (riGetGridPropertyForSelectedCells, args, nargout,
"Usage:\n"
"\n"
"Matrix[numSelectedCells][numTimestepsRequested]\n"
" riGetGridPropertyForSelectedCells([CaseId], PropertyName, [RequestedTimeSteps], [PorosityModel = \"Matrix\"|\"Fracture\"] )\n"
"\n"
"This function returns a two dimensional matrix: [numSelectedCells][numTimestepsRequested] containing the requested property data from the case with CaseId.\n"
"If the CaseId is not defined, ResInsight's Current Case is used.\n"
"The RequestedTimeSteps must contain a list of 1-based indices to the requested time steps. If not defined, all the time steps are returned.\n"
)
{
if (nargout < 1)
{
error("riGetGridPropertyForSelectedCells: Missing output argument.\n");
print_usage();
return octave_value_list ();
}
int nargin = args.length ();
if (nargin < 1)
{
error("riGetGridPropertyForSelectedCells: Too few arguments. The name of the property requested is necessary.\n");
print_usage();
return octave_value_list ();
}
if (nargin > 4)
{
error("riGetGridPropertyForSelectedCells: Too many arguments.\n");
print_usage();
return octave_value_list ();
}
std::vector<int> argIndices;
argIndices.push_back(0);
argIndices.push_back(1);
argIndices.push_back(2);
argIndices.push_back(3);
// Check if we have a CaseId:
if (!args(argIndices[0]).is_numeric_type())
{
argIndices[0] = -1;
for (size_t aIdx = 1; aIdx < argIndices.size(); ++aIdx)
--argIndices[aIdx];
}
// Check if we have a Requested TimeSteps
if (!(nargin > argIndices[2] && (args(argIndices[2]).is_matrix_type() || args(argIndices[2]).is_numeric_type()) && !args(argIndices[2]).is_string()))
{
argIndices[2] = -1;
for (size_t aIdx = 3; aIdx < argIndices.size(); ++aIdx)
--argIndices[aIdx];
}
// Check if we have a PorosityModel
int lastArgumentIndex = argIndices[3] ;
if (!(nargin > argIndices[3] && args(argIndices[3]).is_string()))
{
argIndices[3] = -1;
for (size_t aIdx = 4; aIdx < argIndices.size(); ++aIdx)
--argIndices[aIdx];
}
// Check if we have more arguments than we should
if (nargin > lastArgumentIndex + 1)
{
error("riGetGridPropertyForSelectedCells: Unexpected argument after the PorosityModel.\n");
print_usage();
return octave_value_list ();
}
// Setup the argument list
Matrix propertyFrames;
int caseId = -1;
std::string propertyName = "UNDEFINED";
int32NDArray requestedTimeSteps;
std::string porosityModel = "Matrix";
if (argIndices[0] >= 0) caseId = args(argIndices[0]).int_value();
if (argIndices[1] >= 0) propertyName = args(argIndices[1]).char_matrix_value().row_as_string(0);
if (argIndices[2] >= 0) requestedTimeSteps = args(argIndices[2]).int32_array_value();
if (argIndices[3] >= 0) porosityModel = args(argIndices[3]).string_value();
if (porosityModel != "Matrix" && porosityModel != "Fracture")
{
error("riGetGridPropertyForSelectedCells: The value for \"PorosityModel\" is unknown. Please use either \"Matrix\" or \"Fracture\"\n");
print_usage();
return octave_value_list ();
}
getGridPropertyForSelectedCells(propertyFrames, "127.0.0.1", 40001, caseId, propertyName.c_str(), requestedTimeSteps, porosityModel.c_str());
return octave_value(propertyFrames);
}

View File

@ -0,0 +1,130 @@
#include <QtNetwork>
#include <QStringList>
#include <octave/oct.h>
#include "riSettings.h"
#include "RiaSocketDataTransfer.cpp" // NB! Include cpp-file to avoid linking of additional file in oct-compile configuration
void getSelectedCells(int32NDArray& selectedCellInfo, const QString &hostName, quint16 port, const qint64& caseId)
{
QString serverName = hostName;
quint16 serverPort = port;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
if (!socket.waitForConnected(riOctavePlugin::connectTimeOutMilliSecs))
{
error((("Connection: ") + socket.errorString()).toLatin1().data());
return;
}
// Create command and send it:
QString command = QString("GetSelectedCells %1").arg(caseId);
QByteArray cmdBytes = command.toLatin1();
QDataStream socketStream(&socket);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
socketStream << (qint64)(cmdBytes.size());
socket.write(cmdBytes);
// Get response. First wait for the header
while (socket.bytesAvailable() < (int)(2*sizeof(quint64)))
{
if (!socket.waitForReadyRead(riOctavePlugin::longTimeOutMilliSecs))
{
error((("Waiting for header: ") + socket.errorString()).toLatin1().data());
return;
}
}
// Read timestep count and blocksize
quint64 columnCount;
quint64 byteCountForOneTimestep;
size_t selectedCellCount;
socketStream >> columnCount;
socketStream >> byteCountForOneTimestep;
selectedCellCount = byteCountForOneTimestep / sizeof(qint32);
dim_vector dv (2, 1);
dv(0) = selectedCellCount;
dv(1) = columnCount;
selectedCellInfo.resize(dv);
if (!(byteCountForOneTimestep && columnCount))
{
error ("No selected cells found in ResInsight");
return;
}
qint32* internalMatrixData = (qint32*)selectedCellInfo.fortran_vec()->mex_get_data();
QStringList errorMessages;
if (!RiaSocketDataTransfer::readBlockDataFromSocket(&socket, (char*)(internalMatrixData), columnCount * byteCountForOneTimestep, errorMessages))
{
for (int i = 0; i < errorMessages.size(); i++)
{
error(errorMessages[i].toLatin1().data());
}
OCTAVE_QUIT;
}
QString tmp = QString("riGetSelectedCells : Read selected cell info");
octave_stdout << tmp.toStdString() << " Selected cells: " << selectedCellCount << ", Columns: " << columnCount << std::endl;
return;
}
DEFUN_DLD (riGetSelectedCells, args, nargout,
"Usage:\n"
"\n"
" Matrix[numSelectedCells][5] riGetSelectedCells()\n"
"\n"
"This function returns a two dimensional matrix containing cell info for each selected cell.\n"
"The columns contain the following information:\n"
"[CaseId, GridIdx, I, J, K]\n"
" CaseId : The ID of the case the cell resides in.\n"
" GridIdx : The index of the grid the cell resides in. (Main grid has index 0)\n"
" I, J, K : 1-based index address of the cell in the grid.\n"
)
{
int nargin = args.length ();
if (nargin > 1)
{
error("riGetSelectedCells: Too many arguments.\n");
print_usage();
}
else if (nargout < 1)
{
error("riGetSelectedCells: Missing output argument.\n");
print_usage();
}
else
{
qint64 caseId = -1;
if (nargin > 0)
{
unsigned int argCaseId = args(0).uint_value();
caseId = argCaseId;
}
int32NDArray propertyFrames;
getSelectedCells(propertyFrames, "127.0.0.1", 40001, caseId);
return octave_value(propertyFrames);
}
return octave_value();
}