From 2bf10a78378d1268978b506e744ad52fd5a99255 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Thu, 13 Feb 2020 21:09:23 +0100 Subject: [PATCH] #4549 Add python method to get cell centers on a grid Equivalent to GetCellCenters in Octave. --- .../GrpcInterface/GrpcProtos/Grid.proto | 3 +- .../GrpcInterface/Python/rips/grid.py | 29 ++++- .../Python/rips/tests/test_grids.py | 3 + .../GrpcInterface/RiaGrpcGridService.cpp | 102 +++++++++++++++++- .../GrpcInterface/RiaGrpcGridService.h | 38 +++++++ 5 files changed, 168 insertions(+), 7 deletions(-) diff --git a/ApplicationCode/GrpcInterface/GrpcProtos/Grid.proto b/ApplicationCode/GrpcInterface/GrpcProtos/Grid.proto index aaea7f0b53..07ebe697db 100644 --- a/ApplicationCode/GrpcInterface/GrpcProtos/Grid.proto +++ b/ApplicationCode/GrpcInterface/GrpcProtos/Grid.proto @@ -7,7 +7,7 @@ import "Case.proto"; service Grid { - rpc GetCellCenters(GridRequest) returns(CellCenters) {} + rpc GetCellCenters(GridRequest) returns(stream CellCenters) {} rpc GetDimensions(GridRequest) returns (GridDimensions) {} } @@ -33,3 +33,4 @@ message GridDimensions { Vec3i dimensions = 1; } + diff --git a/ApplicationCode/GrpcInterface/Python/rips/grid.py b/ApplicationCode/GrpcInterface/Python/rips/grid.py index ba306d932e..56858de3a2 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/grid.py +++ b/ApplicationCode/GrpcInterface/Python/rips/grid.py @@ -13,7 +13,7 @@ import rips.generated.Grid_pb2_grpc as Grid_pb2_grpc class Grid: """Grid Information. Not meant to be constructed separately - Create Grid objects using mathods on Case: Grid() and Grids() + Create Grid objects using methods on Case: Grid() and Grids() """ def __init__(self, index, case, channel): self.__channel = channel @@ -32,3 +32,30 @@ class Grid: return self.__stub.GetDimensions( Grid_pb2.GridRequest(case_request=case_request, grid_index=self.index)).dimensions + + + def cell_centers_async(self): + """The cells center for all cells in given grid async. + + Returns: + Iterator to a list of Vec3d: class with double attributes x, y, x giving cell centers + """ + case_request = Case_pb2.CaseRequest(id=self.case.case_id) + chunks = self.__stub.GetCellCenters( + Grid_pb2.GridRequest(case_request=case_request, + grid_index=self.index)) + for chunk in chunks: + yield chunk + + def cell_centers(self): + """The cell center for all cells in given grid + + Returns: + List of Vec3d: class with double attributes x, y, x giving cell centers + """ + centers = [] + chunks = self.cell_centers_async() + for chunk in chunks: + for center in chunk.centers: + centers.append(center) + return centers diff --git a/ApplicationCode/GrpcInterface/Python/rips/tests/test_grids.py b/ApplicationCode/GrpcInterface/Python/rips/tests/test_grids.py index 80ad582d16..0df9cc3e0c 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/tests/test_grids.py +++ b/ApplicationCode/GrpcInterface/Python/rips/tests/test_grids.py @@ -15,3 +15,6 @@ def test_10k(rips_instance, initialize_test): assert(dimensions.i == 90) assert(dimensions.j == 96) assert(dimensions.k == 36) + + cell_centers = grid.cell_centers() + assert(len(cell_centers) == (dimensions.i * dimensions.j * dimensions.k)) diff --git a/ApplicationCode/GrpcInterface/RiaGrpcGridService.cpp b/ApplicationCode/GrpcInterface/RiaGrpcGridService.cpp index a0772d67ec..ce1bb0a729 100644 --- a/ApplicationCode/GrpcInterface/RiaGrpcGridService.cpp +++ b/ApplicationCode/GrpcInterface/RiaGrpcGridService.cpp @@ -19,6 +19,7 @@ #include "RiaGrpcCallbacks.h" +#include "RigCell.h" #include "RigEclipseCaseData.h" #include "RigMainGrid.h" @@ -26,11 +27,86 @@ using namespace rips; +//-------------------------------------------------------------------------------------------------- +/// Convert internal ResInsight representation of cells with negative depth to positive depth. +//-------------------------------------------------------------------------------------------------- +static inline void convertVec3dToPositiveDepth( cvf::Vec3d* vec ) +{ + double& z = vec->z(); + z *= -1; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -grpc::Status - RiaGrpcGridService::GetDimensions( grpc::ServerContext* context, const GridRequest* request, GridDimensions* reply ) +RiaCellCenterStateHandler::RiaCellCenterStateHandler() + : m_request( nullptr ) + , m_eclipseCase( nullptr ) + , m_grid( nullptr ) + , m_currentCellIdx( 0u ) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +grpc::Status RiaCellCenterStateHandler::init( const rips::GridRequest* request ) +{ + CAF_ASSERT( request ); + m_request = request; + + RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->case_request().id() ); + m_eclipseCase = dynamic_cast( rimCase ); + + if ( !m_eclipseCase ) + { + return grpc::Status( grpc::NOT_FOUND, "Eclipse Case not found" ); + } + + size_t gridIndex = (size_t)request->grid_index(); + if ( gridIndex >= m_eclipseCase->mainGrid()->gridCount() ) + { + return grpc::Status( grpc::NOT_FOUND, "Grid not found" ); + } + + m_grid = m_eclipseCase->mainGrid()->gridByIndex( gridIndex ); + + return grpc::Status::OK; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +grpc::Status RiaCellCenterStateHandler::assignReply( rips::CellCenters* reply ) +{ + const size_t packageSize = RiaGrpcServiceInterface::numberOfMessagesForByteCount( sizeof( rips::CellCenters ) ); + size_t packageIndex = 0u; + reply->mutable_centers()->Reserve( (int)packageSize ); + for ( ; packageIndex < packageSize && m_currentCellIdx < m_grid->cellCount(); ++packageIndex ) + { + cvf::Vec3d center = m_grid->cell( m_currentCellIdx ).center(); + + convertVec3dToPositiveDepth( ¢er ); + + Vec3d* cellCenter = reply->add_centers(); + cellCenter->set_x( center.x() ); + cellCenter->set_y( center.y() ); + cellCenter->set_z( center.z() ); + m_currentCellIdx++; + } + if ( packageIndex > 0u ) + { + return Status::OK; + } + return Status( grpc::OUT_OF_RANGE, "We've reached the end. This is not an error but means transmission is finished" ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +grpc::Status RiaGrpcGridService::GetDimensions( grpc::ServerContext* context, + const rips::GridRequest* request, + rips::GridDimensions* reply ) { RimCase* rimCase = findCase( request->case_request().id() ); @@ -55,6 +131,17 @@ grpc::Status return grpc::Status( grpc::NOT_FOUND, "Eclipse Case not found" ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +grpc::Status RiaGrpcGridService::GetCellCenters( grpc::ServerContext* context, + const rips::GridRequest* request, + rips::CellCenters* reply, + RiaCellCenterStateHandler* stateHandler ) +{ + return stateHandler->assignReply( reply ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -62,9 +149,14 @@ std::vector RiaGrpcGridService::createCallbacks() { typedef RiaGrpcGridService Self; - return {new RiaGrpcUnaryCallback( this, - &Self::GetDimensions, - &Self::RequestGetDimensions )}; + return { + + new RiaGrpcServerToClientStreamCallback( this, + &Self::GetCellCenters, + &Self::RequestGetCellCenters, + new RiaCellCenterStateHandler ), + + new RiaGrpcUnaryCallback( this, &Self::GetDimensions, &Self::RequestGetDimensions )}; } static bool RiaGrpcGridService_init = diff --git a/ApplicationCode/GrpcInterface/RiaGrpcGridService.h b/ApplicationCode/GrpcInterface/RiaGrpcGridService.h index 6f73c5dad4..97db44ff20 100644 --- a/ApplicationCode/GrpcInterface/RiaGrpcGridService.h +++ b/ApplicationCode/GrpcInterface/RiaGrpcGridService.h @@ -21,9 +21,47 @@ #include "RiaGrpcServiceInterface.h" +#include + +class RigGridBase; +class RimEclipseCase; + +namespace rips +{ +class GridRequest; +class CellCenters; +class GridDimensions; +}; // namespace rips + +//================================================================================================== +// +// State handler for streaming of active cell info +// +//================================================================================================== +class RiaCellCenterStateHandler +{ + typedef grpc::Status Status; + +public: + RiaCellCenterStateHandler(); + grpc::Status init( const rips::GridRequest* request ); + grpc::Status assignReply( rips::CellCenters* reply ); + +protected: + const rips::GridRequest* m_request; + RimEclipseCase* m_eclipseCase; + size_t m_currentCellIdx; + const RigGridBase* m_grid; +}; + class RiaGrpcGridService final : public rips::Grid::AsyncService, public RiaGrpcServiceInterface { public: + grpc::Status GetCellCenters( grpc::ServerContext* context, + const rips::GridRequest* request, + rips::CellCenters* reply, + RiaCellCenterStateHandler* stateHandler ); + grpc::Status GetDimensions( grpc::ServerContext* context, const rips::GridRequest* request, rips::GridDimensions* reply ) override;