Octave: Updated GetActiveCellProperty to the new specification.

Still missing handling of coarse grids
p4#: 21677
This commit is contained in:
Jacob Støren 2013-05-21 11:18:28 +02:00
parent eff924c630
commit 2009838b04
3 changed files with 157 additions and 45 deletions

View File

@ -140,4 +140,4 @@ QList<QVariant> Json::decodeInnerToList(QScriptValue arrayValue)
list.append(QVariant(decodeInner(it.value())));
}
return list;
}
}

View File

@ -282,7 +282,7 @@ public:
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
quint64 argCaseGroupId = -1;
int argCaseGroupId = -1;
if (args.size() == 2)
{
@ -567,29 +567,34 @@ public:
RimCase* rimCase = findCaseFromArgs(server, args);
if (!rimCase) return true;
QString propertyName = args[2];
QString porosityModelName = args[3];
// Find the requested data, Or create a set if we are setting data and it is not found
RifReaderInterface::PorosityModelResultType porosityModelEnum = RifReaderInterface::MATRIX_RESULTS;
if (porosityModelName == "Fracture")
{
porosityModelEnum = RifReaderInterface::FRACTURE_RESULTS;
}
// Find the requested data
size_t scalarResultIndex = cvf::UNDEFINED_SIZE_T;
std::vector< std::vector<double> >* scalarResultFrames = NULL;
if (rimCase && rimCase->results(RifReaderInterface::MATRIX_RESULTS))
if (rimCase && rimCase->results(porosityModelEnum))
{
scalarResultIndex = rimCase->results(RifReaderInterface::MATRIX_RESULTS)->findOrLoadScalarResult(propertyName);
scalarResultIndex = rimCase->results(porosityModelEnum)->findOrLoadScalarResult(propertyName);
if (scalarResultIndex != cvf::UNDEFINED_SIZE_T)
{
scalarResultFrames = &(rimCase->results(RifReaderInterface::MATRIX_RESULTS)->cellResults()->cellScalarResults(scalarResultIndex));
scalarResultFrames = &(rimCase->results(porosityModelEnum)->cellResults()->cellScalarResults(scalarResultIndex));
}
}
if (scalarResultFrames == NULL)
{
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the property named: \"%1\"").arg(propertyName));
server->errorMessageDialog()->showMessage(RiaSocketServer::tr("ResInsight SocketServer: \n") + RiaSocketServer::tr("Could not find the %1 model property named: \"%2\"").arg(porosityModelName).arg(propertyName));
}
// Write data back : timeStepCount, bytesPrTimestep, dataForTimestep0 ... dataForTimestepN
@ -601,23 +606,61 @@ public:
}
else
{
// Create a list of all the requested timesteps
std::vector<size_t> requestedTimesteps;
if (args.size() <= 4)
{
// Select all
for (size_t tsIdx = 0; tsIdx < scalarResultFrames->size(); ++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: riGetActiveCellProperty : \n") + RiaSocketServer::tr("An error occured while interpreting the requested timesteps."));
}
}
// First write timestep count
quint64 timestepCount = (quint64)scalarResultFrames->size();
quint64 timestepCount = (quint64)requestedTimesteps.size();
socketStream << timestepCount;
// then the byte-size of the result values in one timestep
// TODO: Rewrite to handle coarsening
size_t timestepResultCount = scalarResultFrames->front().size();
quint64 timestepByteCount = (quint64)(timestepResultCount*sizeof(double));
socketStream << timestepByteCount ;
// Then write the data.
for (size_t tIdx = 0; tIdx < scalarResultFrames->size(); ++tIdx)
for (size_t tIdx = 0; tIdx < requestedTimesteps.size(); ++tIdx)
{
#if 1 // Write data as raw bytes, fast but does not handle byteswapping
server->currentClient()->write((const char *)scalarResultFrames->at(tIdx).data(), timestepByteCount); // Raw print of data. Fast but no platform conversion
server->currentClient()->write((const char *)scalarResultFrames->at(requestedTimesteps[tIdx]).data(), timestepByteCount); // Raw print of data. Fast but no platform conversion
#else // Write data using QDataStream, does byteswapping for us. Must use QDataStream on client as well
for (size_t cIdx = 0; cIdx < scalarResultFrames->at(tIdx).size(); ++cIdx)
for (size_t cIdx = 0; cIdx < scalarResultFrames->at(requestedTimesteps[tIdx]).size(); ++cIdx)
{
socketStream << scalarResultFrames->at(tIdx)[cIdx];
}

View File

@ -1,13 +1,11 @@
#include <QtNetwork>
#include <octave/oct.h>
#include "riSettings.h"
void getEclipseProperty(Matrix& propertyFrames, const QString &hostName, quint16 port, QString caseName, QString propertyName)
void getEclipseProperty(Matrix& propertyFrames, const QString &serverName, quint16 serverPort,
const qint64& caseId, QString propertyName, const int32NDArray& requestedTimeSteps, QString porosityModel)
{
QString serverName = hostName;
quint16 serverPort = port;
const int Timeout = 5 * 1000;
const int Timeout = riOctavePlugin::timeOutMilliSecs;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
@ -18,14 +16,22 @@ void getEclipseProperty(Matrix& propertyFrames, const QString &hostName, quint16
return;
}
// Create command and send it:
QString command("GetProperty ");
command += caseName + " " + propertyName;
QByteArray cmdBytes = command.toLatin1();
QDataStream socketStream(&socket);
socketStream.setVersion(QDataStream::Qt_4_0);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
// Create command as a string with arguments , and send it:
QString command;
command += "GetProperty " + 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)));
if (i != requestedTimeSteps.length() -1) command += " ";
}
QByteArray cmdBytes = command.toLatin1();
socketStream << (qint64)(cmdBytes.size());
socket.write(cmdBytes);
@ -60,6 +66,7 @@ void getEclipseProperty(Matrix& propertyFrames, const QString &hostName, quint16
}
// Wait for available data for each timestep, then read data for each timestep
for (size_t tIdx = 0; tIdx < timestepCount; ++tIdx)
{
while (socket.bytesAvailable() < (int)byteCount)
@ -98,13 +105,13 @@ void getEclipseProperty(Matrix& propertyFrames, const QString &hostName, quint16
QString tmp = QString("riGetActiveCellProperty : Read %1").arg(propertyName);
if (caseName == "-1")
if (caseId < 0)
{
tmp += QString(" from active case.");
tmp += QString(" from current case.");
}
else
{
tmp += QString(" from %1.").arg(caseName);
tmp += QString(" from case with Id: %1.").arg(caseId);
}
octave_stdout << tmp.toStdString() << " Active cells : " << activeCellCount << ", Timesteps : " << timestepCount << std::endl;
@ -116,35 +123,97 @@ void getEclipseProperty(Matrix& propertyFrames, const QString &hostName, quint16
DEFUN_DLD (riGetActiveCellProperty, args, nargout,
"Usage:\n"
"\n"
" riGetActiveCellProperty( [CaseName/CaseIndex], PropertyName )\n"
"Matrix[numActiveCells][numTimestepsRequested] riGetActiveCellProperty([CaseId], PropertyName, [RequestedTimeSteps], [PorosityModel = \"Matrix\"|\"Fracture\"] )"
"\n"
"Returns a two dimentional matrix: [ActiveCells][Timesteps]\n"
"Containing the requested property data from the Eclipse Case defined.\n"
"If the Eclipse Case is not defined, the active View in ResInsight is used."
"This function returns a two dimensional matrix: [ActiveCells][Num TimestepsRequested] containing the requested property data from the case with CaseId."
"If the case contains coarse-cells, the results are expanded onto the active cells."
"If the CaseId is not defined, ResInsights Current Case is used."
"The RequestedTimeSteps must contain a list of indices to the requested timesteps. If not defined, all the timesteps are returned."
)
{
if (nargout < 1)
{
error("riGetActiveCellProperty: Missing output argument.\n");
print_usage();
return octave_value_list ();
}
int nargin = args.length ();
if (nargin < 1)
{
error("riGetActiveCellProperty: Too few arguments. The name of the property requested is neccesary.\n");
print_usage();
return octave_value_list ();
}
else if (nargout < 1)
if (nargin > 4)
{
error("riGetActiveCellProperty: Missing output argument.\n");
error("riGetActiveCellProperty: Too many arguments.\n");
print_usage();
return octave_value_list ();
}
else
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())
{
Matrix propertyFrames;
if (nargin > 1)
getEclipseProperty(propertyFrames, "127.0.0.1", 40001, args(0).char_matrix_value().row_as_string(0).c_str(), args(1).char_matrix_value().row_as_string(0).c_str());
else
getEclipseProperty(propertyFrames, "127.0.0.1", 40001, "-1", args(0).char_matrix_value().row_as_string(0).c_str());
return octave_value(propertyFrames);
argIndices[0] = -1;
for (size_t aIdx = 1; aIdx < argIndices.size(); ++aIdx)
--argIndices[aIdx];
}
return octave_value_list ();
// Check if we have a Requested TimeSteps
if (!(nargin > argIndices[2] && args(argIndices[2]).is_matrix_type()))
{
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("riGetActiveCellProperty: Unexpected argument after the PorosityModel.\n");
print_usage();
return octave_value_list ();
}
// The actual values are
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("riGetActiveCellProperty: The value for \"PorosityModel\" is unknown. Please use either \"Matrix\" or \"Fracture\"\n");
print_usage();
return octave_value_list ();
}
getEclipseProperty(propertyFrames, "127.0.0.1", 40001, caseId, propertyName.c_str(), requestedTimeSteps, porosityModel.c_str());
return octave_value(propertyFrames);
}