From 9bea767cdfd31c027fad59d07404190be02cd670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Grip=20Fj=C3=A6r?= Date: Thu, 29 Jun 2017 09:07:21 +0200 Subject: [PATCH] #1663 Octave interface to get NNC-pair information --- ApplicationCode/CMakeLists.txt | 1 + .../SocketInterface/RiaNNCCommands.cpp | 97 +++++++++++ OctavePlugin/CMakeLists.txt | 2 + OctavePlugin/riGetNNCConnections.cpp | 153 ++++++++++++++++++ OctavePlugin/riSettings.h | 6 + 5 files changed, 259 insertions(+) create mode 100644 ApplicationCode/SocketInterface/RiaNNCCommands.cpp create mode 100644 OctavePlugin/riGetNNCConnections.cpp diff --git a/ApplicationCode/CMakeLists.txt b/ApplicationCode/CMakeLists.txt index 836b7d38fd..7efd69c90a 100644 --- a/ApplicationCode/CMakeLists.txt +++ b/ApplicationCode/CMakeLists.txt @@ -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 diff --git a/ApplicationCode/SocketInterface/RiaNNCCommands.cpp b/ApplicationCode/SocketInterface/RiaNNCCommands.cpp new file mode 100644 index 0000000000..65d7094b0e --- /dev/null +++ b/ApplicationCode/SocketInterface/RiaNNCCommands.cpp @@ -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 +// 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 + + + + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +class RiaGetNNCConnections: public RiaSocketCommand +{ +public: + static QString commandName () { return QString("GetNNCConnections"); } + + virtual bool interpretCommand(RiaSocketServer* server, const QList& 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::commandName()); diff --git a/OctavePlugin/CMakeLists.txt b/OctavePlugin/CMakeLists.txt index 21ec239bad..954899eef8 100644 --- a/OctavePlugin/CMakeLists.txt +++ b/OctavePlugin/CMakeLists.txt @@ -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" diff --git a/OctavePlugin/riGetNNCConnections.cpp b/OctavePlugin/riGetNNCConnections.cpp new file mode 100644 index 0000000000..05d8a3f524 --- /dev/null +++ b/OctavePlugin/riGetNNCConnections.cpp @@ -0,0 +1,153 @@ +#include +#include +#include + +#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& 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 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(); +} \ No newline at end of file diff --git a/OctavePlugin/riSettings.h b/OctavePlugin/riSettings.h index 63ad778b37..3e80a781d9 100644 --- a/OctavePlugin/riSettings.h +++ b/OctavePlugin/riSettings.h @@ -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"; }