From 4b7ff209f6821340d86dab1e794155b56c361499 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Wed, 22 May 2013 08:11:39 +0200 Subject: [PATCH] Added getCellCenters p4#: 21688 --- .../RigReservoirBuilderMock.cpp | 9 +- .../SocketInterface/RiaSocketCommands.cpp | 57 ++++++++ OctavePlugin/CMakeLists.txt | 2 + OctavePlugin/riGetCellCenters.cpp | 137 ++++++++++++++++++ 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 OctavePlugin/riGetCellCenters.cpp diff --git a/ApplicationCode/ReservoirDataModel/RigReservoirBuilderMock.cpp b/ApplicationCode/ReservoirDataModel/RigReservoirBuilderMock.cpp index a7cbb3fed4..aad790fb4b 100644 --- a/ApplicationCode/ReservoirDataModel/RigReservoirBuilderMock.cpp +++ b/ApplicationCode/ReservoirDataModel/RigReservoirBuilderMock.cpp @@ -256,8 +256,13 @@ void RigReservoirBuilderMock::populateReservoir(RigCaseData* eclipseCase) } // Add grid coarsening for main grid - eclipseCase->mainGrid()->addCoarseningBox(1, 2, 1, 3, 1, 4); - eclipseCase->mainGrid()->addCoarseningBox(3, 4, 4, 5, 5, 6); + if (cellDimension().x() > 4 && + cellDimension().y() > 5 && + cellDimension().z() > 6) + { + eclipseCase->mainGrid()->addCoarseningBox(1, 2, 1, 3, 1, 4); + eclipseCase->mainGrid()->addCoarseningBox(3, 4, 4, 5, 5, 6); + } } diff --git a/ApplicationCode/SocketInterface/RiaSocketCommands.cpp b/ApplicationCode/SocketInterface/RiaSocketCommands.cpp index fe8e79fdce..862041e453 100644 --- a/ApplicationCode/SocketInterface/RiaSocketCommands.cpp +++ b/ApplicationCode/SocketInterface/RiaSocketCommands.cpp @@ -32,6 +32,8 @@ #include "RigCaseData.h" #include "RigCaseCellResultsData.h" +#include + class RiaGetTimeStepDates : public RiaSocketCommand { @@ -318,3 +320,58 @@ public: }; static bool RiaGetCoarseningInfo_init = RiaSocketCommandFactory::instance()->registerCreator(RiaGetCoarseningInfo::commandName()); + + +class RiaGetCellCenters : public RiaSocketCommand +{ +public: + static QString commandName () { return QString("GetCellCenters"); } + + virtual bool interpretCommand(RiaSocketServer* server, const QList& args, QDataStream& socketStream) + { + int argCaseGroupId = -1; + size_t argGridIndex = 0; + + if (args.size() == 2) + { + argGridIndex = args[1].toInt(); + } + else if (args.size() == 3) + { + argCaseGroupId = args[1].toInt(); + argGridIndex = args[2].toUInt(); + } + + RimCase* rimCase = server->findReservoir(argCaseGroupId); + if (!rimCase || !rimCase->reservoirData() || (argGridIndex >= rimCase->reservoirData()->gridCount()) ) + { + // No data available + socketStream << (quint64)0 << (quint64)0 ; + return true; + } + + RigGridBase* rigGrid = rimCase->reservoirData()->grid(argGridIndex); + + quint64 cellCount = (quint64)rigGrid->cellCount(); + std::vector cellCenterValues(cellCount * 3); + for (size_t i = 0; i < cellCount; i++) + { + cvf::Vec3d center = rigGrid->cell(i).center(); + + cellCenterValues[i * 3 + 0] = center.x(); + cellCenterValues[i * 3 + 1] = center.y(); + cellCenterValues[i * 3 + 2] = center.z(); + } + + socketStream << cellCount; + quint64 byteCount = cellCount * 3 * sizeof(double); + socketStream << byteCount; + + server->currentClient()->write((const char *)cellCenterValues.data(), byteCount); + + return true; + } + +}; + +static bool RiaGetCellCenters_init = RiaSocketCommandFactory::instance()->registerCreator(RiaGetCellCenters::commandName()); diff --git a/OctavePlugin/CMakeLists.txt b/OctavePlugin/CMakeLists.txt index 947e9d9c3f..ce1b776ed3 100644 --- a/OctavePlugin/CMakeLists.txt +++ b/OctavePlugin/CMakeLists.txt @@ -13,6 +13,7 @@ set(CPP_SOURCES riGetTimeStepDays.cpp riGetGridDimensions.cpp riGetCoarseningInfo.cpp + riGetCellCenters.cpp ) if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") @@ -115,6 +116,7 @@ else() "${CMAKE_CURRENT_BINARY_DIR}/riGetTimeStepDays.oct" "${CMAKE_CURRENT_BINARY_DIR}/riGetGridDimensions.oct" "${CMAKE_CURRENT_BINARY_DIR}/riGetCoarseningInfo.oct" + "${CMAKE_CURRENT_BINARY_DIR}/riGetCellCenters.oct" SOURCES ${CPP_SOURCES} ) diff --git a/OctavePlugin/riGetCellCenters.cpp b/OctavePlugin/riGetCellCenters.cpp new file mode 100644 index 0000000000..01808c0273 --- /dev/null +++ b/OctavePlugin/riGetCellCenters.cpp @@ -0,0 +1,137 @@ +#include +#include + +#include "riSettings.h" + + +void getCellCenters(NDArray& cellCenterValues, const QString &hostName, quint16 port, const qint32& caseId, const quint32& gridIndex) +{ + QString serverName = hostName; + quint16 serverPort = port; + + const int timeout = riOctavePlugin::timeOutMilliSecs; + + QTcpSocket socket; + socket.connectToHost(serverName, serverPort); + + if (!socket.waitForConnected(timeout)) + { + error((("Connection: ") + socket.errorString()).toLatin1().data()); + return; + } + + // Create command and send it: + + QString command = QString("GetCellCenters %1 %2").arg(caseId).arg(gridIndex); + 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(timeout)) + { + error((("Wating for header: ") + socket.errorString()).toLatin1().data()); + return; + } + } + + // Read timestep count and blocksize + + quint64 cellCount; + quint64 byteCount; + + socketStream >> cellCount; + socketStream >> byteCount; + + dim_vector dv (1, 1); + dv(0) = 3; + dv(1) = cellCount; + + cellCenterValues.resize(dv); + + if (!(byteCount && cellCount)) + { + error ("Could not find the requested data in ResInsight"); + return; + } + + // Wait for available data for each column, then read data for each column + while (socket.bytesAvailable() < (qint64)(byteCount)) + { + if (!socket.waitForReadyRead(timeout)) + { + error((("Waiting for data: ") + socket.errorString()).toLatin1().data()); + return; + } + OCTAVE_QUIT; + } + + quint64 bytesRead = 0; + double* internalMatrixData = cellCenterValues.fortran_vec(); + bytesRead = socket.read((char*)(internalMatrixData), byteCount); + + if (byteCount != bytesRead) + { + error("Could not read binary double data properly from socket"); + octave_stdout << "Cell count: " << cellCount << std::endl; + } + + return; +} + + + +DEFUN_DLD (riGetCellCenters, args, nargout, + "Usage:\n" + "\n" + " riGetActiveCellInfo([CaseId], GridIndex )\n" + "\n" + "This function returns the UTM coordinates (X, Y, Z) of the center point of all the cells in the grid.\n" + "If the CaseId is not defined, ResInsight’s Current Case is used.\n" + ) +{ + int nargin = args.length (); + if (nargin > 2) + { + error("riGetCellCenters: Too many arguments. CaseId is optional input argument.\n"); + print_usage(); + } + else if (nargout < 1) + { + error("riGetCellCenters: Missing output argument.\n"); + print_usage(); + } + else + { + NDArray cellCenterValues; + + qint32 caseId = -1; + quint32 gridIndex = 0; + + if (nargin == 1) + { + gridIndex = args(0).uint_value(); + } + else if (nargin == 2) + { + unsigned int argCaseId = args(0).uint_value(); + caseId = argCaseId; + + gridIndex = args(1).uint_value(); + } + + getCellCenters(cellCenterValues, "127.0.0.1", 40001, caseId, gridIndex); + + return octave_value(cellCenterValues); + } + + return octave_value(); +} +