Implemented getGridCellResults()

This commit is contained in:
Gaute Lindkvist 2019-05-21 16:33:41 +02:00
parent 86e202aed0
commit d1a0799861
5 changed files with 173 additions and 74 deletions

View File

@ -17,78 +17,150 @@
//////////////////////////////////////////////////////////////////////////////////
#include "RiaGrpcPropertiesService.h"
#include "RiaGrpcGridInfoService.h"
#include "RiaGrpcCallbacks.h"
#include "RiaGrpcGridInfoService.h"
#include "RigActiveCellInfo.h"
#include "RigActiveCellsResultAccessor.h"
#include "RigAllGridCellsResultAccessor.h"
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigEclipseResultAddress.h"
#include "RigMainGrid.h"
#include "RigResultAccessor.h"
#include "RigResultAccessorFactory.h"
#include "RimEclipseCase.h"
using namespace rips;
//--------------------------------------------------------------------------------------------------
/// Abstract handler base class for streaming cell results to client
///
//--------------------------------------------------------------------------------------------------
RiaActiveCellResultsStateHandler::RiaActiveCellResultsStateHandler()
: m_request(nullptr)
, m_resultValues(nullptr)
, m_currentCellIdx(0u)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaActiveCellResultsStateHandler::init(const ResultRequest* request)
class RiaCellResultsStateHandler
{
int caseId = request->request_case().id();
RimEclipseCase* eclipseCase = dynamic_cast<RimEclipseCase*>(RiaGrpcServiceInterface::findCase(caseId));
if (eclipseCase)
typedef grpc::Status Status;
public:
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaCellResultsStateHandler()
: m_request(nullptr)
, m_currentCellIdx(0u)
{
auto porosityModel = static_cast<RiaDefines::PorosityModelType>(request->porosity_model());
auto resultData = eclipseCase->eclipseCaseData()->results(porosityModel);
auto resultType = static_cast<RiaDefines::ResultCatType>(request->property_type());
size_t timeStep = static_cast<size_t>(request->time_step());
RigEclipseResultAddress resAddr(resultType, QString::fromStdString(request->property_name()));
if (resultData->hasResultEntry(resAddr))
{
if (timeStep < (int)resultData->timeStepCount(resAddr))
{
m_resultValues = &resultData->cellScalarResults(resAddr, timeStep);
return grpc::Status::OK;
}
return grpc::Status(grpc::NOT_FOUND, "No such time step");
}
return grpc::Status(grpc::NOT_FOUND, "No such result");
}
return grpc::Status(grpc::NOT_FOUND, "Couldn't find an Eclipse case matching the case Id");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaActiveCellResultsStateHandler::assignReply(ResultReplyArray* reply)
{
if (m_resultValues)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Status init(const ResultRequest* request)
{
const size_t packageSize = RiaGrpcServiceInterface::numberOfMessagesForByteCount(sizeof(rips::ResultReplyArray));
int caseId = request->request_case().id();
RimEclipseCase* eclipseCase = dynamic_cast<RimEclipseCase*>(RiaGrpcServiceInterface::findCase(caseId));
if (eclipseCase)
{
auto porosityModel = static_cast<RiaDefines::PorosityModelType>(request->porosity_model());
auto caseData = eclipseCase->eclipseCaseData();
auto resultData = caseData->results(porosityModel);
auto resultType = static_cast<RiaDefines::ResultCatType>(request->property_type());
size_t timeStep = static_cast<size_t>(request->time_step());
RigEclipseResultAddress resAddr(resultType, QString::fromStdString(request->property_name()));
if (resultData->ensureKnownResultLoaded(resAddr))
{
if (timeStep < (int)resultData->timeStepCount(resAddr))
{
initResultAccess(caseData, request->grid_index(), porosityModel, timeStep, resAddr);
return grpc::Status::OK;
}
return grpc::Status(grpc::NOT_FOUND, "No such time step");
}
return grpc::Status(grpc::NOT_FOUND, "No such result");
}
return grpc::Status(grpc::NOT_FOUND, "Couldn't find an Eclipse case matching the case Id");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Status assignReply(ResultReplyArray* reply)
{
const size_t packageSize = RiaGrpcServiceInterface::numberOfMessagesForByteCount(sizeof(rips::ResultReplyArray));
size_t packageIndex = 0u;
reply->mutable_values()->Reserve((int)packageSize);
for (; packageIndex < packageSize && m_currentCellIdx < m_resultValues->size(); ++packageIndex, ++m_currentCellIdx)
for (; packageIndex < packageSize && m_currentCellIdx < m_cellCount; ++packageIndex, ++m_currentCellIdx)
{
reply->add_values(m_resultValues->at(m_currentCellIdx));
reply->add_values(cellResult(m_currentCellIdx));
}
if (packageIndex > 0u)
{
return grpc::Status::OK;
}
return grpc::Status(grpc::OUT_OF_RANGE, "We've reached the end. This is not an error but means transmission is finished");
return grpc::Status(grpc::OUT_OF_RANGE,
"We've reached the end. This is not an error but means transmission is finished");
}
return grpc::Status(grpc::NOT_FOUND, "No result values found");
}
protected:
virtual void initResultAccess(RigEclipseCaseData* caseData,
size_t gridIndex,
RiaDefines::PorosityModelType porosityModel,
size_t timeStepIndex,
RigEclipseResultAddress resVarAddr) = 0;
virtual double cellResult(size_t currentCellIndex) const = 0;
protected:
const rips::ResultRequest* m_request;
size_t m_currentCellIdx;
size_t m_cellCount;
};
class RiaActiveCellResultsStateHandler : public RiaCellResultsStateHandler
{
protected:
void initResultAccess(RigEclipseCaseData* caseData,
size_t gridIndex,
RiaDefines::PorosityModelType porosityModel,
size_t timeStepIndex,
RigEclipseResultAddress resVarAddr) override
{
auto activeCellInfo = caseData->activeCellInfo(porosityModel);
m_resultValues = &(caseData->results(porosityModel)->cellScalarResults(resVarAddr, timeStepIndex));
m_cellCount = activeCellInfo->reservoirActiveCellCount();
}
double cellResult(size_t currentCellIndex) const override
{
return (*m_resultValues)[currentCellIndex];
}
private:
const std::vector<double>* m_resultValues;
};
class RiaGridCellResultsStateHandler : public RiaCellResultsStateHandler
{
protected:
void initResultAccess(RigEclipseCaseData* caseData,
size_t gridIndex,
RiaDefines::PorosityModelType porosityModel,
size_t timeStepIndex,
RigEclipseResultAddress resVarAddr) override
{
m_resultAccessor = RigResultAccessorFactory::createFromResultAddress(caseData, gridIndex, porosityModel, timeStepIndex, resVarAddr);
m_cellCount = caseData->grid(gridIndex)->cellCount();
}
double cellResult(size_t currentCellIndex) const override
{
return m_resultAccessor->cellScalar(currentCellIndex);
}
private:
cvf::ref<RigResultAccessor> m_resultAccessor;
};
//--------------------------------------------------------------------------------------------------
///
@ -121,10 +193,22 @@ grpc::Status RiaGrpcPropertiesService::GetAvailableProperties(grpc::ServerContex
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaGrpcPropertiesService::GetActiveCellResults(grpc::ServerContext* context,
const ResultRequest* request,
ResultReplyArray* reply,
RiaActiveCellResultsStateHandler* stateHandler)
grpc::Status RiaGrpcPropertiesService::GetActiveCellResults(grpc::ServerContext* context,
const ResultRequest* request,
ResultReplyArray* reply,
RiaActiveCellResultsStateHandler* stateHandler)
{
return stateHandler->assignReply(reply);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaGrpcPropertiesService::GetGridResults(grpc::ServerContext* context,
const rips::ResultRequest* request,
rips::ResultReplyArray* reply,
RiaGridCellResultsStateHandler* stateHandler)
{
return stateHandler->assignReply(reply);
}
@ -136,10 +220,14 @@ std::vector<RiaAbstractGrpcCallback*> RiaGrpcPropertiesService::createCallbacks(
{
typedef RiaGrpcPropertiesService Self;
return { new RiaGrpcCallback<Self, PropertiesRequest, AvailableProperties>(this, &Self::GetAvailableProperties, &Self::RequestGetAvailableProperties),
new RiaGrpcStreamCallback<Self, ResultRequest, ResultReplyArray, RiaActiveCellResultsStateHandler>(
this, &Self::GetActiveCellResults, &Self::RequestGetActiveCellResults, new RiaActiveCellResultsStateHandler)
return {new RiaGrpcCallback<Self, PropertiesRequest, AvailableProperties>(
this, &Self::GetAvailableProperties, &Self::RequestGetAvailableProperties),
new RiaGrpcStreamCallback<Self, ResultRequest, ResultReplyArray, RiaActiveCellResultsStateHandler>(
this, &Self::GetActiveCellResults, &Self::RequestGetActiveCellResults, new RiaActiveCellResultsStateHandler),
new RiaGrpcStreamCallback<Self, ResultRequest, ResultReplyArray, RiaGridCellResultsStateHandler>(
this, &Self::GetGridResults, &Self::RequestGetGridResults, new RiaGridCellResultsStateHandler)
};
}
static bool RiaGrpcPropertiesService_init = RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcPropertiesService>(typeid(RiaGrpcPropertiesService).hash_code());
static bool RiaGrpcPropertiesService_init =
RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcPropertiesService>(typeid(RiaGrpcPropertiesService).hash_code());

View File

@ -23,20 +23,8 @@
#include <grpcpp/grpcpp.h>
#include <vector>
class RiaActiveCellResultsStateHandler
{
typedef grpc::Status Status;
public:
RiaActiveCellResultsStateHandler();
Status init(const rips::ResultRequest* request);
Status assignReply(rips::ResultReplyArray* reply);
private:
const rips::ResultRequest* m_request;
const std::vector<double>* m_resultValues;
size_t m_currentCellIdx;
};
class RiaActiveCellResultsStateHandler;
class RiaGridCellResultsStateHandler;
//==================================================================================================
//
@ -53,6 +41,10 @@ public:
const rips::ResultRequest* request,
rips::ResultReplyArray* reply,
RiaActiveCellResultsStateHandler* stateHandler);
grpc::Status GetGridResults(grpc::ServerContext* context,
const rips::ResultRequest* request,
rips::ResultReplyArray* reply,
RiaGridCellResultsStateHandler* stateHandler);
std::vector<RiaAbstractGrpcCallback*> createCallbacks() override;
};

View File

@ -2,17 +2,17 @@
//
// Copyright (C) Statoil ASA
// Copyright (C) Ceetron Solutions AS
//
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

View File

@ -112,6 +112,16 @@ class Properties:
time_step = timeStep,
porosity_model = porosityModelEnum)
return self.properties.GetActiveCellResults(request)
def gridCellResults(self, caseId, propertyType, propertyName, timeStep, gridIndex = 0, porosityModel = 'MATRIX_MODEL'):
propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType)
porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel)
request = Properties_pb2.ResultRequest(request_case = CaseInfo_pb2.Case(id=caseId),
property_type = propertyTypeEnum,
property_name = propertyName,
time_step = timeStep,
grid_index = gridIndex,
porosity_model = porosityModelEnum)
return self.properties.GetGridResults(request)
class Instance:
@staticmethod

View File

@ -13,7 +13,16 @@ results = []
for resultChunk in resultChunks:
for value in resultChunk.values:
results.append(value)
print("Number of active cells: " + str(len(results)))
print("15th active cell: ")
for result in results:
print(result)
print("Transferred " + str(len(results)) + " cell results")
print("30th active cell: ")
print(results[29])
resultChunks = resInsight.properties.gridCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', 2)
results = []
for resultChunk in resultChunks:
for value in resultChunk.values:
results.append(value)
print("Transferred " + str(len(results)) + " cell results")
print("124498th cell: ")
print(results[124498])