Skip to content

Commit

Permalink
Add dtype and size checking to var_to_petsc and var_from_petsc
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmaddison committed Jul 17, 2024
1 parent ed420a5 commit df57b2e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tlm_adjoint/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,12 @@ def var_to_petsc(x, vec):
The output :class:`petsc4py.PETSc.Vec`. The ghost is not updated.
"""

if not np.can_cast(var_dtype(x), PETSc.ScalarType):
raise ValueError("Invalid dtype")
if var_local_size(x) != vec.getLocalSize():
raise ValueError("Invalid size")
if var_global_size(x) != vec.getSize():
raise ValueError("Invalid size")
x._tlm_adjoint__var_interface_to_petsc(vec)


Expand All @@ -1369,6 +1375,12 @@ def var_from_petsc(x, vec):
The input :class:`petsc4py.PETSc.Vec`.
"""

if not np.can_cast(var_dtype(x), PETSc.ScalarType):
raise ValueError("Invalid dtype")
if var_local_size(x) != vec.getLocalSize():
raise ValueError("Invalid size")
if var_global_size(x) != vec.getSize():
raise ValueError("Invalid size")
x._tlm_adjoint__var_interface_from_petsc(vec)


Expand Down

0 comments on commit df57b2e

Please sign in to comment.