Files
ResInsight/OctavePlugin/riGetActiveCellCenters.cpp
T

151 lines
4.0 KiB
C++
Raw Normal View History

2013-05-22 09:54:39 +02:00
#include <QtNetwork>
#include <QStringList>
2013-05-22 09:54:39 +02:00
#include <octave/oct.h>
#include "riSettings.h"
#include "RiaSocketDataTransfer.cpp" // NB! Include cpp-file to avoid linking of additional file in oct-compile configuration
2013-05-22 09:54:39 +02:00
void getActiveCellCenters(NDArray& cellCenterValues, const QString &hostName, quint16 port, const qint32& caseId, const QString& porosityModel)
2013-05-22 09:54:39 +02:00
{
QString serverName = hostName;
quint16 serverPort = port;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
if (!socket.waitForConnected(riOctavePlugin::connectTimeOutMilliSecs))
2013-05-22 09:54:39 +02:00
{
error("Connection: %s",socket.errorString().toLatin1().data());
2013-05-22 09:54:39 +02:00
return;
}
// Create command and send it:
QString command = QString("GetActiveCellCenters %1 %2").arg(caseId).arg(porosityModel);
2013-05-22 09:54:39 +02:00
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(riOctavePlugin::longTimeOutMilliSecs))
2013-05-22 09:54:39 +02:00
{
error("Waiting for header: %s",socket.errorString().toLatin1().data());
2013-05-22 09:54:39 +02:00
return;
}
}
// Read timestep count and blocksize
quint64 activeCellCount;
quint64 byteCount;
socketStream >> activeCellCount;
socketStream >> byteCount;
if (!(byteCount && activeCellCount))
{
error ("Could not find the requested data in ResInsight");
return;
}
dim_vector dv;
dv.resize(2);
dv(0) = activeCellCount;
dv(1) = 3;
cellCenterValues.resize(dv);
double* internalMatrixData = cellCenterValues.fortran_vec();
QStringList errorMessages;
if (!RiaSocketDataTransfer::readBlockDataFromSocket(&socket, (char*)(internalMatrixData), byteCount, errorMessages))
2013-05-22 09:54:39 +02:00
{
for (int i = 0; i < errorMessages.size(); i++)
2013-05-22 09:54:39 +02:00
{
error("%s",errorMessages[i].toLatin1().data());
2013-05-22 09:54:39 +02:00
}
OCTAVE_QUIT;
2013-05-22 09:54:39 +02:00
}
return;
}
DEFUN_DLD (riGetActiveCellCenters, args, nargout,
"Usage:\n"
"\n"
" riGetActiveCellCenters([CaseId], [PorosityModel = “Matrix”|”Fracture”] )\n"
2013-05-22 09:54:39 +02:00
"\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, ResInsights Current Case is used.\n"
)
{
if (nargout < 1)
{
error("riGetActiveCellCenters: Missing output argument.\n");
print_usage();
return octave_value_list ();
}
int nargin = args.length ();
if (nargin > 2)
2013-05-22 09:54:39 +02:00
{
error("riGetActiveCellCenters: Too many arguments.\n");
print_usage();
return octave_value_list ();
}
qint32 caseId = -1;
std::string porosityModel = "Matrix";
if (nargin == 1)
{
if (riOctavePlugin::isOctaveValueNumeric(args(0)))
2013-05-22 09:54:39 +02:00
{
caseId = args(0).uint_value();
}
else
{
porosityModel = args(0).string_value();
2013-05-22 09:54:39 +02:00
}
}
else if (nargin == 2)
2013-05-22 09:54:39 +02:00
{
if (riOctavePlugin::isOctaveValueNumeric(args(0)))
{
caseId = args(0).uint_value();
porosityModel = args(1).string_value();
}
else
{
caseId = args(1).uint_value();
porosityModel = args(0).string_value();
}
2013-05-22 09:54:39 +02:00
}
if (porosityModel != "Matrix" && porosityModel != "Fracture")
{
error("riGetActiveCellProperty: The value for \"PorosityModel\" is unknown. Please use either \"Matrix\" or \"Fracture\"\n");
print_usage();
return octave_value_list ();
}
NDArray cellCenterValues;
getActiveCellCenters(cellCenterValues, "127.0.0.1", 40001, caseId, porosityModel.c_str());
2013-05-22 09:54:39 +02:00
return octave_value(cellCenterValues);
}