From db18a855dbf7f1ccacee24b37d113c04c01f1e81 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 16 May 2013 14:44:34 +0200 Subject: [PATCH] Added GetTimeStepDays p4#: 21662 --- .../SocketInterface/RiaSocketCommands.cpp | 77 ++++++++++- OctavePlugin/CMakeLists.txt | 2 + OctavePlugin/riGetTimeStepDays.cpp | 130 ++++++++++++++++++ 3 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 OctavePlugin/riGetTimeStepDays.cpp diff --git a/ApplicationCode/SocketInterface/RiaSocketCommands.cpp b/ApplicationCode/SocketInterface/RiaSocketCommands.cpp index d58fe1532b..4e59b94db2 100644 --- a/ApplicationCode/SocketInterface/RiaSocketCommands.cpp +++ b/ApplicationCode/SocketInterface/RiaSocketCommands.cpp @@ -80,7 +80,7 @@ public: std::vector timeStepDates = rimCase->reservoirData()->results(RifReaderInterface::MATRIX_RESULTS)->timeStepDates(scalarIndexWithMaxTimeStepCount); quint64 timeStepCount = timeStepDates.size(); - quint64 byteCount = sizeof(quint64) + timeStepCount * sizeof(qint32); + quint64 byteCount = sizeof(quint64) + 6 * timeStepCount * sizeof(qint32); socketStream << byteCount; socketStream << timeStepCount; @@ -114,3 +114,78 @@ public: }; static bool RiaGetTimeStepDates_init = RiaSocketCommandFactory::instance()->registerCreator(RiaGetTimeStepDates::commandName()); + + + +class RiaGetTimeStepDays : public RiaSocketCommand +{ +public: + static QString commandName () { return QString("GetTimeStepDays"); } + virtual bool interpretCommand(RiaSocketServer* server, const QList& args, QDataStream& socketStream) + { + int argCaseGroupId = -1; + + if (args.size() == 2) + { + argCaseGroupId = args[1].toInt(); + } + + RimCase* rimCase = server->findReservoir(argCaseGroupId); + + bool canFetchData = true; + + if (!rimCase || !rimCase->reservoirData()) + { + canFetchData = false; + } + + size_t scalarIndexWithMaxTimeStepCount = cvf::UNDEFINED_SIZE_T; + if (rimCase && rimCase->reservoirData()) + { + rimCase->reservoirData()->results(RifReaderInterface::MATRIX_RESULTS)->maxTimeStepCount(&scalarIndexWithMaxTimeStepCount); + if (scalarIndexWithMaxTimeStepCount == cvf::UNDEFINED_SIZE_T) + { + canFetchData = false; + } + } + + // Did not find any result to fetch data from, return zero data found + if (!canFetchData) + { + quint64 timeStepCount = 0; + quint64 byteCount = sizeof(quint64); + + socketStream << byteCount; + socketStream << timeStepCount; + + return true; + } + + std::vector timeStepDates = rimCase->reservoirData()->results(RifReaderInterface::MATRIX_RESULTS)->timeStepDates(scalarIndexWithMaxTimeStepCount); + + quint64 timeStepCount = timeStepDates.size(); + quint64 byteCount = sizeof(quint64) + timeStepCount * sizeof(qint32); + + socketStream << byteCount; + socketStream << timeStepCount; + + if (timeStepCount > 0) + { + double secondsInADay = 24 * 60 * 60; + + for (size_t i = 0; i < timeStepCount; i++) + { + double secondsDiff = timeStepDates[0].secsTo(timeStepDates[i]); + + double decimalDaysDiff = secondsDiff / secondsInADay; + + socketStream << decimalDaysDiff; + } + } + + return true; + } + +}; + +static bool RiaGetTimeStepDays_init = RiaSocketCommandFactory::instance()->registerCreator(RiaGetTimeStepDays::commandName()); diff --git a/OctavePlugin/CMakeLists.txt b/OctavePlugin/CMakeLists.txt index 31770722db..184dfb2d01 100644 --- a/OctavePlugin/CMakeLists.txt +++ b/OctavePlugin/CMakeLists.txt @@ -10,6 +10,7 @@ set(CPP_SOURCES riGetSelectedCases.cpp riGetCases.cpp riGetTimeStepDates.cpp + riGetTimeStepDays.cpp ) if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") @@ -109,6 +110,7 @@ else() "${CMAKE_CURRENT_BINARY_DIR}/riGetSelectedCases.oct" "${CMAKE_CURRENT_BINARY_DIR}/riGetCases.oct" "${CMAKE_CURRENT_BINARY_DIR}/riGetTimeStepDates.oct" + "${CMAKE_CURRENT_BINARY_DIR}/riGetTimeStepDays.oct" SOURCES ${CPP_SOURCES} ) diff --git a/OctavePlugin/riGetTimeStepDays.cpp b/OctavePlugin/riGetTimeStepDays.cpp new file mode 100644 index 0000000000..cc7cfe1a42 --- /dev/null +++ b/OctavePlugin/riGetTimeStepDays.cpp @@ -0,0 +1,130 @@ +#include +#include + +#include "riSettings.h" + +void getTimeStepDates( std::vector& decimalDays, + const qint64& caseId, + const QString& hostName, + quint16 port) +{ + 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("GetTimeStepDays %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)(sizeof(quint64))) + { + if (!socket.waitForReadyRead(timeout)) + { + error((("Wating for header: ") + socket.errorString()).toLatin1().data()); + return; + } + } + + quint64 byteCount; + socketStream >> byteCount; + + while (socket.bytesAvailable() < (int)(byteCount)) + { + if (!socket.waitForReadyRead(timeout)) + { + error((("Waiting for data: ") + socket.errorString()).toLatin1().data()); + return; + } + OCTAVE_QUIT; + } + + quint64 timeStepCount; + socketStream >> timeStepCount; + + octave_stdout << "byte count: " << byteCount << ", Timesteps: " << timeStepCount << std::endl; + + for (size_t i = 0; i < timeStepCount; i++) + { + double doubleValue; + + socketStream >> doubleValue; + decimalDays.push_back(doubleValue); + } + + return; +} + + + +DEFUN_DLD (riGetTimeStepDays, args, nargout, + "Usage:\n" + "\n" + " riGetTimeStepDays()\n" + "\n" + "This function returns the time from the simulation start\n" + "as decimal days for all the time steps as a Vector of doubles.\n" + "If the CaseId is not defined, ResInsight’s Current Case is used.\n" + ) +{ + int nargin = args.length (); + if (nargin > 1) + { + error("riGetTimeStepDays: Too many arguments, this function takes one optional argument.\n"); + print_usage(); + } + else if (nargout != 1) + { + error("riGetTimeStepDays: Wrong number of output arguments, this function requires one output argument.\n"); + print_usage(); + } + else + { + qint64 caseId = -1; + if (nargin > 0) + { + if (args(0).is_numeric_type()) + { + unsigned int argCaseId = args(0).uint_value(); + caseId = argCaseId; + } + } + + std::vector decimalDays; + + getTimeStepDates(decimalDays, caseId, "127.0.0.1", 40001); + + dim_vector dv(2); + dv(0) = decimalDays.size(); + dv(1) = 1; + NDArray oct_decimalDays(dv); + + for (size_t i = 0; i < decimalDays.size(); i++) + { + oct_decimalDays(i) = decimalDays[i]; + } + + return octave_value(oct_decimalDays); + } + + return octave_value(); +} +