Skip to content

Commit

Permalink
Improved error messages for bindings (#9)
Browse files Browse the repository at this point in the history
* Adding error messages to check length of values and vertex id arrays provided for write commands

* Adding provided and expected values in assertion statements

Co-authored-by: ishaandesai <[email protected]>
  • Loading branch information
BenjaminRodenberg and IshaanDesai authored Apr 6, 2021
1 parent 8c66cc3 commit 2c68630
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions cyprecice/cyprecice.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ cdef class Interface:

if len(position) > 0:
dimensions = len(position)
assert(dimensions == self.get_dimensions())
assert dimensions == self.get_dimensions(), "Dimensions of vertex coordinate in set_mesh_vertex does not match with dimensions in problem definition. Provided dimensions: {}, expected dimensions: {}".format(dimensions, self.get_dimensions())
elif len(position) == 0:
dimensions = self.get_dimensions()

Expand Down Expand Up @@ -530,7 +530,7 @@ cdef class Interface:

if len(positions) > 0:
size, dimensions = positions.shape
assert(dimensions == self.get_dimensions())
assert dimensions == self.get_dimensions(), "Dimensions of vertex coordinates in set_mesh_vertices does not match with dimensions in problem definition. Provided dimensions: {}, expected dimensions: {}".format(dimensions, self.get_dimensions())
elif len(positions) == 0:
size = positions.shape[0]
dimensions = self.get_dimensions()
Expand Down Expand Up @@ -640,7 +640,7 @@ cdef class Interface:

if len(positions) > 0:
size, dimensions = positions.shape
assert(dimensions == self.get_dimensions())
assert dimensions == self.get_dimensions(), "Dimensions of position coordinates in get_mesh_vertex_ids_from_positions does not match with dimensions in problem definition. Provided dimensions: {}, expected dimensions: {}".format(dimensions, self.get_dimensions())
elif len(positions) == 0:
size = positions.shape[0]
dimensions = self.get_dimensions()
Expand Down Expand Up @@ -892,15 +892,15 @@ cdef class Interface:

if len(values) > 0:
size, dimensions = values.shape
assert(dimensions == self.get_dimensions())
assert dimensions == self.get_dimensions(), "Dimensions of vector data in write_block_vector_data does not match with dimensions in problem definition. Provided dimensions: {}, expected dimensions: {}".format(dimensions, self.get_dimensions())
if len(values) == 0:
size = 0

cdef np.ndarray[int, ndim=1] _vertex_ids = np.ascontiguousarray(vertex_ids, dtype=np.int32)
cdef np.ndarray[double, ndim=1] _values = np.ascontiguousarray(values.flatten(), dtype=np.double)

assert(_values.size == size * self.get_dimensions())
assert(_vertex_ids.size == size)
assert _values.size == size * self.get_dimensions(), "Vector data is not provided for all vertices in write_block_vector_data. Check length of input data provided. Provided size: {}, expected size: {}".format(_values.size, size * self.get_dimensions())
assert _vertex_ids.size == size, "Vertex IDs are of incorrect length in write_block_vector_data. Check length of vertex ids input. Provided size: {}, expected size: {}".format(_vertex_ids.size, size)

self.thisptr.writeBlockVectorData (data_id, size, <const int*>_vertex_ids.data, <const double*>_values.data)

Expand Down Expand Up @@ -941,11 +941,14 @@ cdef class Interface:
>>> interface.write_vector_data(data_id, vertex_id, value)
"""
check_array_like(value, "value", "write_vector_data")
assert len(value) > 0, "Input vector data is empty in write_vector_data"

assert(len(value) > 0)
dimensions = len(value)
assert(dimensions == self.get_dimensions())

assert dimensions == self.get_dimensions(), "Dimensions of vector data in write_vector_data does not match with dimensions in problem definition. Provided dimensions: {}, expected dimensions: {}".format(dimensions, self.get_dimensions())

cdef np.ndarray[np.double_t, ndim=1] _value = np.ascontiguousarray(value, dtype=np.double)

self.thisptr.writeVectorData (data_id, vertex_id, <const double*>_value.data)

def write_block_scalar_data (self, data_id, vertex_ids, values):
Expand Down Expand Up @@ -985,13 +988,11 @@ cdef class Interface:
if len(values) == 0:
size = 0


cdef np.ndarray[int, ndim=1] _vertex_ids = np.ascontiguousarray(vertex_ids, dtype=np.int32)
cdef np.ndarray[double, ndim=1] _values = np.ascontiguousarray(values, dtype=np.double)

assert(_values.size == size)
assert(_vertex_ids.size == size)

assert _values.size == size, "Scalar data is not provided for all vertices in write_block_scalar_data. Check size of input data provided. Provided size: {}, expected size: {}".format(_values.size, size)
assert _vertex_ids.size == size, "Vertex IDs are of incorrect length in write_block_scalar_data. Check size of vertex ids input. Provided size: {}, expected size: {}".format(_vertex_ids.size, size)
self.thisptr.writeBlockScalarData (data_id, size, <const int*>_vertex_ids.data, <const double*>_values.data)

def write_scalar_data (self, data_id, vertex_id, double value):
Expand Down

0 comments on commit 2c68630

Please sign in to comment.