#1663 Octave interface to get NNC-pair information

This commit is contained in:
Bjørnar Grip Fjær 2017-06-29 09:07:21 +02:00
parent de414c4277
commit 9bea767cdf
5 changed files with 259 additions and 0 deletions

View File

@ -87,6 +87,7 @@ set( SOCKET_INTERFACE_FILES
SocketInterface/RiaProjectInfoCommands.cpp
SocketInterface/RiaCaseInfoCommands.cpp
SocketInterface/RiaGeometryCommands.cpp
SocketInterface/RiaNNCCommands.cpp
SocketInterface/RiaPropertyDataCommands.cpp
SocketInterface/RiaWellDataCommands.cpp
SocketInterface/RiaSocketTools.cpp

View File

@ -0,0 +1,97 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaSocketCommand.h"
#include "RiaSocketServer.h"
#include "RiaSocketTools.h"
#include "RiaApplication.h"
#include "RiaPreferences.h"
#include "RigActiveCellInfo.h"
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigMainGrid.h"
#include "Rim3dOverlayInfoConfig.h"
#include "RimCellEdgeColors.h"
#include "RimCellRangeFilterCollection.h"
#include "RimEclipseCase.h"
#include "RimEclipseCellColors.h"
#include "RimEclipsePropertyFilterCollection.h"
#include "RimEclipseView.h"
#include "RimEclipseWellCollection.h"
#include "RimReservoirCellResultsStorage.h"
#include <QTcpSocket>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiaGetNNCConnections: public RiaSocketCommand
{
public:
static QString commandName () { return QString("GetNNCConnections"); }
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
{
RimEclipseCase* rimCase = RiaSocketTools::findCaseFromArgs(server, args);
if (!rimCase) return true;
// Write data back to octave: columnCount, GridNr I J K GridNr I J K
if (!(rimCase && rimCase->eclipseCaseData() && rimCase->eclipseCaseData()->mainGrid()))
{
// No data available
socketStream << (quint64)0;
return true;
}
RigMainGrid* mainGrid = rimCase->eclipseCaseData()->mainGrid();
size_t connectionCount = mainGrid->nncData()->connections().size();
socketStream << (quint64)connectionCount;
for (const RigConnection& connection : mainGrid->nncData()->connections())
{
const RigCell& cell1 = mainGrid->globalCellArray()[connection.m_c1GlobIdx];
const RigCell& cell2 = mainGrid->globalCellArray()[connection.m_c2GlobIdx];
sendCellInfo(socketStream, cell1);
sendCellInfo(socketStream, cell2);
}
return true;
}
static void sendCellInfo(QDataStream& socketStream, const RigCell& cell)
{
RigGridBase* hostGrid = cell.hostGrid();
size_t gridLocalCellIndex = cell.gridLocalCellIndex();
size_t i, j, k;
hostGrid->ijkFromCellIndex(gridLocalCellIndex, &i, &j, &k);
socketStream << (qint32)hostGrid->gridIndex();
socketStream << (qint32)i << (qint32)j << (qint32)k;
}
};
static bool RiaGetNNCConnections_init = RiaSocketCommandFactory::instance()->registerCreator<RiaGetNNCConnections>(RiaGetNNCConnections::commandName());

View File

@ -8,6 +8,7 @@ set(CPP_SOURCES
riSetActiveCellProperty.cpp
riGetActiveCellInfo.cpp
riGetMainGridDimensions.cpp
riGetNNCConnections.cpp
riGetCurrentCase.cpp
riGetCaseGroups.cpp
riGetSelectedCases.cpp
@ -172,6 +173,7 @@ if (RESINSIGHT_OCTAVE_PLUGIN_QMAKE AND RESINSIGHT_OCTAVE_PLUGIN_MKOCTFILE)
"${CMAKE_CURRENT_BINARY_DIR}/riSetActiveCellProperty.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetActiveCellInfo.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetMainGridDimensions.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetNNCConnections.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCurrentCase.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCaseGroups.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetSelectedCases.oct"

View File

@ -0,0 +1,153 @@
#include <QtNetwork>
#include <octave/oct.h>
#include <octave/oct-map.h>
#include "riSettings.h"
#include "RiaSocketDataTransfer.cpp" // NB! Include cpp-file to avoid linking of additional file in oct-compile configuration
struct GridLocalCell
{
int gridIndex;
int i;
int j;
int k;
};
struct Connection
{
GridLocalCell fromCell;
GridLocalCell toCell;
};
void getNNCConnections(std::vector<Connection>& connections, 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;
}
QString command = QString("GetNNCConnections %1").arg(caseId);
QByteArray cmdBytes = command.toLatin1();
QDataStream socketStream(&socket);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
socketStream << (qint64)(cmdBytes.size());
socket.write(cmdBytes);
while (socket.bytesAvailable() < (int)sizeof(quint64))
{
if (!socket.waitForReadyRead(riOctavePlugin::longTimeOutMilliSecs))
{
error((("Waiting for header: ") + socket.errorString()).toLatin1().data());
return;
}
OCTAVE_QUIT;
}
quint64 connectionCount;
quint64 byteCount;
quint64 rowByteSize = sizeof(qint32) * 4 * 2;
socketStream >> connectionCount;
byteCount = connectionCount * rowByteSize;
while (socket.bytesAvailable() < (int)byteCount)
{
if (!socket.waitForReadyRead(riOctavePlugin::longTimeOutMilliSecs))
{
error((("Waiting for data: ") + socket.errorString()).toLatin1().data());
return;
}
OCTAVE_QUIT;
}
connections.resize(connectionCount);
for (size_t i = 0; i < connectionCount; ++i)
{
socketStream >> connections[i].fromCell.gridIndex;
socketStream >> connections[i].fromCell.i >> connections[i].fromCell.j >> connections[i].fromCell.k;
socketStream >> connections[i].toCell.gridIndex;
socketStream >> connections[i].toCell.i >> connections[i].toCell.j >> connections[i].toCell.k;
}
return;
}
DEFUN_DLD(riGetNNCConnections, args, nargout,
"Usage:\n"
"\n"
" riGetNNCConnections([CaseId])\n"
"\n"
"This function returns a two dimensional matrix containing grid and IJK information\n"
"for each NNC in the requested case."
)
{
int nargin = args.length();
if (nargin > 1)
{
error("riGetNNCConnections: Too many arguments, CaseId are optional input arguments.\n");
print_usage();
}
else if (nargout < 1)
{
error("riGetNNCConnections: Missing output argument.\n");
print_usage();
}
else
{
std::vector<Connection> connections;
qint64 caseId = -1;
if (nargin > 0)
{
if (args(0).is_numeric_type())
{
unsigned int argCaseId = args(0).uint_value();
caseId = argCaseId;
}
}
getNNCConnections(connections, "127.0.0.1", 40001, caseId);
Cell cellValuesGridIndex(connections.size(), 2);
Cell cellValuesI(connections.size(), 2);
Cell cellValuesJ(connections.size(), 2);
Cell cellValuesK(connections.size(), 2);
for (size_t i = 0; i < connections.size(); ++i)
{
cellValuesGridIndex(i, 0) = connections[i].fromCell.gridIndex;
cellValuesGridIndex(i, 1) = connections[i].toCell.gridIndex;
cellValuesI(i, 0) = connections[i].fromCell.i;
cellValuesI(i, 1) = connections[i].toCell.i;
cellValuesJ(i, 0) = connections[i].fromCell.j;
cellValuesJ(i, 1) = connections[i].toCell.j;
cellValuesK(i, 0) = connections[i].fromCell.k;
cellValuesK(i, 1) = connections[i].toCell.k;
}
octave_map m;
m.assign(riOctavePlugin::cellIndex_gridIndex, cellValuesGridIndex);
m.assign(riOctavePlugin::cellIndex_I, cellValuesI);
m.assign(riOctavePlugin::cellIndex_J, cellValuesJ);
m.assign(riOctavePlugin::cellIndex_K, cellValuesK);
return octave_value(m);
}
return octave_value();
}

View File

@ -60,5 +60,11 @@ namespace riOctavePlugin
char timeStepDate_Hour[] = "Hour";
char timeStepDate_Minute[] = "Minute";
char timeStepDate_Second[] = "Second";
// Octave data structure : CellIndex
char cellIndex_gridIndex[] = "GridIndex";
char cellIndex_I[] = "I";
char cellIndex_J[] = "J";
char cellIndex_K[] = "K";
}