Skip to content

Commit

Permalink
Merge branch 'newest-bmi-support' of github.com:eWaterCycle/grpc4bmi …
Browse files Browse the repository at this point in the history
…into newest-bmi-support
  • Loading branch information
goord committed Jul 11, 2019
2 parents 2b8c426 + 98d9b86 commit c282c88
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 42 deletions.
6 changes: 3 additions & 3 deletions grpc4bmi/bmi_grpc_legacy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def setValueAtIndices(self, request, context):
index_array = numpy.array(request.indices)
if request.HasField("values_int"):
array = numpy.array(request.values_int.values, dtype=numpy.int32)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, src=array)
if request.HasField("values_float"):
array = numpy.array(request.values_int.values, dtype=numpy.float32)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, src=array)
if request.HasField("values_double"):
array = numpy.array(request.values_double.values, dtype=numpy.float64)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, src=array)
return bmi_pb2.Empty()

def getGridSize(self, request, context):
Expand Down
112 changes: 87 additions & 25 deletions grpc4bmi/bmi_grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,47 @@ def getVarUnits(self, request, context):
def getVarNBytes(self, request, context):
return bmi_pb2.GetVarNBytesResponse(nbytes=self.bmi_model_.get_var_nbytes(request.name))

def getVarLocation(self, request, context):
location = self.bmi_model_.get_var_location(request.name)
lookup = {
"node": bmi_pb2.GetVarLocationResponse.Location.NODE,
"edge": bmi_pb2.GetVarLocationResponse.Location.EDGE,
"face": bmi_pb2.GetVarLocationResponse.Location.FACE,
}
return bmi_pb2.GetVarLocationResponse(location=lookup[location])

def _reserve_values(self, name):
dtype = self.bmi_model_.get_var_type(name)
item_size = self.bmi_model_.get_var_itemsize(name)
total_size = self.bmi_model_.get_var_nbytes(name)
size = total_size // item_size
return numpy.empty(size, dtype=dtype)

def getValue(self, request, context):
vals = self.bmi_model_.get_value(request.name)
if vals.dtype == numpy.int32:
return bmi_pb2.GetValueResponse(values_int=bmi_pb2.IntArrayMessage(values=vals.flatten()))
if vals.dtype == numpy.float32:
return bmi_pb2.GetValueResponse(values_float=bmi_pb2.FloatArrayMessage(values=vals.flatten()))
if vals.dtype == numpy.float64:
return bmi_pb2.GetValueResponse(values_double=bmi_pb2.DoubleArrayMessage(values=vals.flatten()))
raise NotImplementedError("Arrays with type %s cannot be transmitted through this GRPC channel" % vals.dtype)
values = self._reserve_values(request.name)
values = self.bmi_model_.get_value(request.name, values)
if values.dtype == numpy.int32:
return bmi_pb2.GetValueResponse(values_int=bmi_pb2.IntArrayMessage(values=values.flatten()))
if values.dtype == numpy.float32:
return bmi_pb2.GetValueResponse(values_float=bmi_pb2.FloatArrayMessage(values=values.flatten()))
if values.dtype == numpy.float64:
return bmi_pb2.GetValueResponse(values_double=bmi_pb2.DoubleArrayMessage(values=values.flatten()))
raise NotImplementedError("Arrays with type %s cannot be transmitted through this GRPC channel" % values.dtype)

def getValuePtr(self, request, context):
raise NotImplementedError("Array references cannot be transmitted through this GRPC channel")

def getValueAtIndices(self, request, context):
indices = numpy.array(request.indices)
vals = self.bmi_model_.get_value_at_indices(request.name, indices)
if vals.dtype == numpy.int32:
return bmi_pb2.GetValueAtIndicesResponse(values_int=bmi_pb2.IntArrayMessage(values=vals.flatten()))
if vals.dtype == numpy.float32:
return bmi_pb2.GetValueAtIndicesResponse(values_float=bmi_pb2.FloatArrayMessage(values=vals.flatten()))
if vals.dtype == numpy.float64:
return bmi_pb2.GetValueAtIndicesResponse(values_double=bmi_pb2.DoubleArrayMessage(values=vals.flatten()))
raise NotImplementedError("Arrays with type %s cannot be transmitted through this GRPC channel" % vals.dtype)
values = numpy.empty(len(indices), dtype=self.bmi_model_.get_var_type(request.name))
values = self.bmi_model_.get_value_at_indices(request.name, values, indices)
if values.dtype == numpy.int32:
return bmi_pb2.GetValueAtIndicesResponse(values_int=bmi_pb2.IntArrayMessage(values=values.flatten()))
if values.dtype == numpy.float32:
return bmi_pb2.GetValueAtIndicesResponse(values_float=bmi_pb2.FloatArrayMessage(values=values.flatten()))
if values.dtype == numpy.float64:
return bmi_pb2.GetValueAtIndicesResponse(values_double=bmi_pb2.DoubleArrayMessage(values=values.flatten()))
raise NotImplementedError("Arrays with type %s cannot be transmitted through this GRPC channel" % values.dtype)

def setValue(self, request, context):
if request.HasField("values_int"):
Expand All @@ -114,13 +132,13 @@ def setValueAtIndices(self, request, context):
index_array = numpy.array(request.indices)
if request.HasField("values_int"):
array = numpy.array(request.values_int.values, dtype=numpy.int32)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, array)
if request.HasField("values_float"):
array = numpy.array(request.values_int.values, dtype=numpy.float32)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, array)
if request.HasField("values_double"):
array = numpy.array(request.values_double.values, dtype=numpy.float64)
self.bmi_model_.set_value_at_indices(request.name, indices=index_array, src=array)
self.bmi_model_.set_value_at_indices(request.name, index_array, array)
return bmi_pb2.Empty()

def getGridSize(self, request, context):
Expand All @@ -132,20 +150,64 @@ def getGridRank(self, request, context):
def getGridType(self, request, context):
return bmi_pb2.GetGridTypeResponse(type=self.bmi_model_.get_grid_type(request.grid_id))

def _reserve_grid_values(self, grid_id):
return numpy.empty(self.bmi_model_.get_grid_rank(grid_id), dtype=numpy.int32)

def getGridShape(self, request, context):
return bmi_pb2.GetGridShapeResponse(shape=self.bmi_model_.get_grid_shape(request.grid_id))
values = self._reserve_grid_values(request.grid_id)
return bmi_pb2.GetGridShapeResponse(shape=self.bmi_model_.get_grid_shape(request.grid_id, values))

def getGridSpacing(self, request, context):
return bmi_pb2.GetGridSpacingResponse(spacing=self.bmi_model_.get_grid_spacing(request.grid_id))
values = self._reserve_grid_values(request.grid_id)
return bmi_pb2.GetGridSpacingResponse(spacing=self.bmi_model_.get_grid_spacing(request.grid_id, values))

def getGridOrigin(self, request, context):
return bmi_pb2.GetGridOriginResponse(origin=self.bmi_model_.get_grid_origin(request.grid_id))
values = self._reserve_grid_values(request.grid_id)
return bmi_pb2.GetGridOriginResponse(origin=self.bmi_model_.get_grid_origin(request.grid_id, values))

def _reserve_shape(self, grid_id, dim_index):
shape = self.bmi_model_.get_grid_shape(grid_id, self._reserve_grid_values(grid_id))
return numpy.empty(shape[dim_index], dtype=numpy.float64)

def getGridX(self, request, context):
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_x(request.grid_id))
values = self._reserve_shape(request.grid_id, 0)
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_x(request.grid_id, values))

def getGridY(self, request, context):
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_y(request.grid_id))
values = self._reserve_shape(request.grid_id, 1)
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_y(request.grid_id, values))

def getGridZ(self, request, context):
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_z(request.grid_id))
values = self._reserve_shape(request.grid_id, 2)
return bmi_pb2.GetGridPointsResponse(coordinates=self.bmi_model_.get_grid_z(request.grid_id, values))

def getGridNodeCount(self, request, context):
return bmi_pb2.GetGridElementCountResponse(count=self.bmi_model_.get_grid_node_count(request.grid_id))

def getGridEdgeCount(self, request, context):
return bmi_pb2.GetGridElementCountResponse(count=self.bmi_model_.get_grid_edge_count(request.grid_id))

def getGridFaceCount(self, request, context):
return bmi_pb2.GetGridElementCountResponse(count=self.bmi_model_.get_grid_edge_count(request.grid_id))

def getGridEdgeNodes(self, request, context):
size = 2 * self.bmi_model_.get_grid_node_count(request.grid_id)
links = numpy.empty(size, dtype=numpy.int64)
links = self.bmi_model_.get_grid_edge_nodes(request.grid_id, links)
return bmi_pb2.GetGridEdgeNodesResponse(links=links)

def getGridFaceNodes(self, request, context):
nodes_per_face = self._get_grid_nodes_per_face(request.grid_id)
size = numpy.sum(nodes_per_face)
links = numpy.empty(size, dtype=numpy.int64)
links = self.bmi_model_.get_grid_face_nodes(request.grid_id, links)
return bmi_pb2.GetGridFaceNodesResponse(links=links)

def _get_grid_nodes_per_face(self, grid_id):
size = self.bmi_model_.get_grid_face_count(grid_id)
links = numpy.empty(size, dtype=numpy.int64)
return self.bmi_model_.get_grid_nodes_per_face(grid_id, links)

def getGridNodesPerFace(self, request, context):
links = self._get_grid_nodes_per_face(request.grid_id)
return bmi_pb2.GetGridNodesPerFaceResponse(links=links)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ docker
pytest
pytest-cov
futures
https://github.com/eWaterCycle/bmi-python/archive/master.zip#egg=bmi-heat
https://github.com/csdms/bmi-example-python/archive/master.zip#egg=bmi-heat
sphinx
sphinxcontrib-napoleon
sphinx-argparse
Expand Down
Loading

0 comments on commit c282c88

Please sign in to comment.