2012-05-18 02:45:23 -05:00
# include <QtNetwork>
2014-04-15 06:40:53 -05:00
# include <QStringList>
2012-05-18 02:45:23 -05:00
# include <octave/oct.h>
2014-04-15 06:40:53 -05:00
2013-05-21 04:18:28 -05:00
# include "riSettings.h"
2012-05-18 02:45:23 -05:00
2014-04-15 06:40:53 -05:00
# include "RiaSocketDataTransfer.cpp" // NB! Include cpp-file to avoid linking of additional file in oct-compile configuration
2013-05-22 02:26:12 -05:00
void getActiveCellProperty ( Matrix & propertyFrames , const QString & serverName , quint16 serverPort ,
2013-05-21 04:18:28 -05:00
const qint64 & caseId , QString propertyName , const int32NDArray & requestedTimeSteps , QString porosityModel )
2012-05-18 02:45:23 -05:00
{
QTcpSocket socket ;
socket . connectToHost ( serverName , serverPort ) ;
2013-08-12 04:38:09 -05:00
if ( ! socket . waitForConnected ( riOctavePlugin : : connectTimeOutMilliSecs ) )
2012-05-18 02:45:23 -05:00
{
2020-11-03 05:25:15 -06:00
error ( " Connection: %s " , socket . errorString ( ) . toLatin1 ( ) . data ( ) ) ;
2012-05-18 02:45:23 -05:00
return ;
}
2013-05-21 04:18:28 -05:00
QDataStream socketStream ( & socket ) ;
socketStream . setVersion ( riOctavePlugin : : qtDataStreamVersion ) ;
2012-05-18 02:45:23 -05:00
2013-05-21 04:18:28 -05:00
// Create command as a string with arguments , and send it:
2012-05-18 02:45:23 -05:00
2013-05-21 04:18:28 -05:00
QString command ;
2013-05-22 02:26:12 -05:00
command + = " GetActiveCellProperty " + QString : : number ( caseId ) + " " + propertyName + " " + porosityModel ;
2013-05-21 04:18:28 -05:00
2019-01-07 05:46:42 -06:00
for ( int i = 0 ; i < requestedTimeSteps . numel ( ) ; + + i )
2013-05-21 04:18:28 -05:00
{
if ( i = = 0 ) command + = " " ;
2013-05-22 02:26:12 -05:00
command + = QString : : number ( static_cast < int > ( requestedTimeSteps . elem ( i ) ) - 1 ) ; // To make the index 0-based
2019-01-07 05:46:42 -06:00
if ( i ! = requestedTimeSteps . numel ( ) - 1 ) command + = " " ;
2013-05-21 04:18:28 -05:00
}
QByteArray cmdBytes = command . toLatin1 ( ) ;
2012-05-18 02:45:23 -05:00
socketStream < < ( qint64 ) ( cmdBytes . size ( ) ) ;
socket . write ( cmdBytes ) ;
// Get response. First wait for the header
while ( socket . bytesAvailable ( ) < ( int ) ( 2 * sizeof ( quint64 ) ) )
{
2013-09-30 13:54:31 -05:00
if ( ! socket . waitForReadyRead ( riOctavePlugin : : longTimeOutMilliSecs ) )
2012-05-18 02:45:23 -05:00
{
2020-11-03 05:25:15 -06:00
error ( " Waiting for header: %s " , socket . errorString ( ) . toLatin1 ( ) . data ( ) ) ;
2012-05-18 02:45:23 -05:00
return ;
}
}
// Read timestep count and blocksize
quint64 timestepCount ;
quint64 byteCount ;
size_t activeCellCount ;
socketStream > > timestepCount ;
socketStream > > byteCount ;
activeCellCount = byteCount / sizeof ( double ) ;
propertyFrames . resize ( activeCellCount , timestepCount ) ;
if ( ! ( byteCount & & timestepCount ) )
{
error ( " Could not find the requested data in ResInsight " ) ;
return ;
}
2014-04-15 06:40:53 -05:00
quint64 totalByteCount = byteCount * timestepCount ;
2013-05-21 04:18:28 -05:00
2014-04-15 06:40:53 -05:00
double * internalMatrixData = propertyFrames . fortran_vec ( ) ;
QStringList errorMessages ;
if ( ! RiaSocketDataTransfer : : readBlockDataFromSocket ( & socket , ( char * ) ( internalMatrixData ) , totalByteCount , errorMessages ) )
2012-05-18 02:45:23 -05:00
{
2014-04-15 06:40:53 -05:00
for ( int i = 0 ; i < errorMessages . size ( ) ; i + + )
2012-05-18 02:45:23 -05:00
{
2020-11-03 05:25:15 -06:00
error ( " %s " , errorMessages [ i ] . toLatin1 ( ) . data ( ) ) ;
2012-05-18 02:45:23 -05:00
}
2014-04-15 06:40:53 -05:00
return ;
2012-05-18 02:45:23 -05:00
}
QString tmp = QString ( " riGetActiveCellProperty : Read %1 " ) . arg ( propertyName ) ;
2013-05-21 04:18:28 -05:00
if ( caseId < 0 )
2012-05-18 02:45:23 -05:00
{
2013-05-21 04:18:28 -05:00
tmp + = QString ( " from current case. " ) ;
2012-05-18 02:45:23 -05:00
}
else
{
2013-05-21 04:18:28 -05:00
tmp + = QString ( " from case with Id: %1. " ) . arg ( caseId ) ;
2012-05-18 02:45:23 -05:00
}
octave_stdout < < tmp . toStdString ( ) < < " Active cells : " < < activeCellCount < < " , Timesteps : " < < timestepCount < < std : : endl ;
return ;
}
DEFUN_DLD ( riGetActiveCellProperty , args , nargout ,
" Usage: \n "
" \n "
2013-05-21 04:18:28 -05:00
" Matrix[numActiveCells][numTimestepsRequested] riGetActiveCellProperty([CaseId], PropertyName, [RequestedTimeSteps], [PorosityModel = \" Matrix \" | \" Fracture \" ] ) "
2012-05-18 02:45:23 -05:00
" \n "
2013-05-21 04:18:28 -05:00
" This function returns a two dimensional matrix: [ActiveCells][Num TimestepsRequested] containing the requested property data from the case with CaseId. "
" If the case contains coarse-cells, the results are expanded onto the active cells. "
" If the CaseId is not defined, ResInsight’ s Current Case is used. "
2013-05-22 02:26:12 -05:00
" The RequestedTimeSteps must contain a list of 1-based indices to the requested timesteps. If not defined, all the timesteps are returned. "
2012-05-18 02:45:23 -05:00
)
{
2013-05-21 04:18:28 -05:00
if ( nargout < 1 )
{
error ( " riGetActiveCellProperty: Missing output argument. \n " ) ;
print_usage ( ) ;
return octave_value_list ( ) ;
}
2012-05-18 02:45:23 -05:00
int nargin = args . length ( ) ;
if ( nargin < 1 )
{
error ( " riGetActiveCellProperty: Too few arguments. The name of the property requested is neccesary. \n " ) ;
print_usage ( ) ;
2013-05-21 04:18:28 -05:00
return octave_value_list ( ) ;
2012-05-18 02:45:23 -05:00
}
2013-05-21 04:18:28 -05:00
if ( nargin > 4 )
2012-05-18 02:45:23 -05:00
{
2013-05-21 04:18:28 -05:00
error ( " riGetActiveCellProperty: Too many arguments. \n " ) ;
2012-05-18 02:45:23 -05:00
print_usage ( ) ;
2013-05-21 04:18:28 -05:00
return octave_value_list ( ) ;
2012-05-18 02:45:23 -05:00
}
2013-05-21 04:18:28 -05:00
std : : vector < int > argIndices ;
argIndices . push_back ( 0 ) ;
argIndices . push_back ( 1 ) ;
argIndices . push_back ( 2 ) ;
argIndices . push_back ( 3 ) ;
// Check if we have a CaseId:
2019-01-15 00:28:50 -06:00
if ( ! riOctavePlugin : : isOctaveValueNumeric ( args ( argIndices [ 0 ] ) ) )
2013-05-21 04:18:28 -05:00
{
argIndices [ 0 ] = - 1 ;
for ( size_t aIdx = 1 ; aIdx < argIndices . size ( ) ; + + aIdx )
- - argIndices [ aIdx ] ;
}
// Check if we have a Requested TimeSteps
2013-10-06 01:45:40 -05:00
2019-01-15 00:28:50 -06:00
if ( ! ( nargin > argIndices [ 2 ] & & ( args ( argIndices [ 2 ] ) . is_matrix_type ( ) | | riOctavePlugin : : isOctaveValueNumeric ( args ( argIndices [ 2 ] ) ) ) & & ! args ( argIndices [ 2 ] ) . is_string ( ) ) )
2012-05-18 02:45:23 -05:00
{
2013-05-21 04:18:28 -05:00
argIndices [ 2 ] = - 1 ;
for ( size_t aIdx = 3 ; aIdx < argIndices . size ( ) ; + + aIdx )
- - argIndices [ aIdx ] ;
}
2012-05-18 02:45:23 -05:00
2013-05-21 04:18:28 -05:00
// Check if we have a PorosityModel
2012-05-18 02:45:23 -05:00
2013-05-21 04:18:28 -05:00
int lastArgumentIndex = argIndices [ 3 ] ;
if ( ! ( nargin > argIndices [ 3 ] & & args ( argIndices [ 3 ] ) . is_string ( ) ) )
{
argIndices [ 3 ] = - 1 ;
for ( size_t aIdx = 4 ; aIdx < argIndices . size ( ) ; + + aIdx )
- - argIndices [ aIdx ] ;
2012-05-18 02:45:23 -05:00
}
2013-05-21 04:18:28 -05:00
// Check if we have more arguments than we should
if ( nargin > lastArgumentIndex + 1 )
{
error ( " riGetActiveCellProperty: Unexpected argument after the PorosityModel. \n " ) ;
print_usage ( ) ;
return octave_value_list ( ) ;
}
2013-05-24 04:36:59 -05:00
// Setup the argument list
2013-05-21 04:18:28 -05:00
Matrix propertyFrames ;
int caseId = - 1 ;
std : : string propertyName = " UNDEFINED " ;
int32NDArray requestedTimeSteps ;
std : : string porosityModel = " Matrix " ;
if ( argIndices [ 0 ] > = 0 ) caseId = args ( argIndices [ 0 ] ) . int_value ( ) ;
if ( argIndices [ 1 ] > = 0 ) propertyName = args ( argIndices [ 1 ] ) . char_matrix_value ( ) . row_as_string ( 0 ) ;
if ( argIndices [ 2 ] > = 0 ) requestedTimeSteps = args ( argIndices [ 2 ] ) . int32_array_value ( ) ;
if ( argIndices [ 3 ] > = 0 ) porosityModel = args ( argIndices [ 3 ] ) . string_value ( ) ;
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 ( ) ;
}
2013-05-22 02:26:12 -05:00
getActiveCellProperty ( propertyFrames , " 127.0.0.1 " , 40001 , caseId , propertyName . c_str ( ) , requestedTimeSteps , porosityModel . c_str ( ) ) ;
2013-05-21 04:18:28 -05:00
return octave_value ( propertyFrames ) ;
2012-05-18 02:45:23 -05:00
}