mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Added riGetGridDimensions
p4#: 21664
This commit is contained in:
131
OctavePlugin/riGetGridDimensions.cpp
Normal file
131
OctavePlugin/riGetGridDimensions.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include <QtNetwork>
|
||||
#include <octave/oct.h>
|
||||
|
||||
#include "riSettings.h"
|
||||
|
||||
|
||||
void getGridDimensions(int32NDArray& gridDimensions, const QString &hostName, quint16 port, const qint64& caseId)
|
||||
{
|
||||
QString serverName = hostName;
|
||||
quint16 serverPort = port;
|
||||
|
||||
const int Timeout = 5 * 1000;
|
||||
|
||||
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("GetGridDimensions %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((("Waiting for header: ") + socket.errorString()).toLatin1().data());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
quint64 byteCount;
|
||||
socketStream >> byteCount;
|
||||
|
||||
quint64 gridCount = byteCount / (3 * sizeof(quint64));
|
||||
|
||||
dim_vector dv (1, 1);
|
||||
dv(0) = 3;
|
||||
dv(1) = gridCount;
|
||||
|
||||
gridDimensions.resize(dv);
|
||||
|
||||
for (size_t i = 0; i < gridCount; i++)
|
||||
{
|
||||
quint64 iCount;
|
||||
quint64 jCount;
|
||||
quint64 kCount;
|
||||
|
||||
socketStream >> iCount;
|
||||
socketStream >> jCount;
|
||||
socketStream >> kCount;
|
||||
|
||||
gridDimensions(0, i) = iCount;
|
||||
gridDimensions(1, i) = jCount;
|
||||
gridDimensions(2, i) = kCount;
|
||||
}
|
||||
|
||||
QString tmp = QString("riGetGridDimensions : Read grid dimensions");
|
||||
if (caseId < 0)
|
||||
{
|
||||
tmp += QString(" from current case.");
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp += QString(" from caseID: %1.").arg(caseId);
|
||||
}
|
||||
|
||||
octave_stdout << tmp.toStdString() << std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
DEFUN_DLD (riGetGridDimensions, args, nargout,
|
||||
"Usage:\n"
|
||||
"\n"
|
||||
" riGetGridDimensions([CaseId])\n"
|
||||
"\n"
|
||||
"This function returns a two dimensional matrix: One row for each grid, starting with the main grid.\n"
|
||||
"NOTE: This means that the <20>normal<61> GridIndices where 0 means Main Grid does not work directly with this matrix. You have to add 1.\n"
|
||||
"The columns contain the following information:\n"
|
||||
"[NI, NJ, NK]: I, J, K dimensions of the grid.\n"
|
||||
"If the CaseId is not defined, ResInsight<68>s Current Case is used.\n"
|
||||
)
|
||||
{
|
||||
int nargin = args.length ();
|
||||
if (nargin > 1)
|
||||
{
|
||||
error("riGetGridDimensions: Too many arguments. Only the name or index of the case is valid input.\n");
|
||||
print_usage();
|
||||
}
|
||||
else if (nargout < 1)
|
||||
{
|
||||
error("riGetGridDimensions: Missing 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;
|
||||
}
|
||||
}
|
||||
|
||||
int32NDArray gridDimensions;
|
||||
getGridDimensions(gridDimensions, "127.0.0.1", 40001, caseId);
|
||||
|
||||
return octave_value(gridDimensions);
|
||||
}
|
||||
|
||||
return octave_value_list ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user