From df57b2e1a38e8c531c2c3d97142277a0581c7c16 Mon Sep 17 00:00:00 2001 From: "James R. Maddison" Date: Wed, 17 Jul 2024 15:28:58 +0100 Subject: [PATCH] Add dtype and size checking to var_to_petsc and var_from_petsc --- tlm_adjoint/interface.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tlm_adjoint/interface.py b/tlm_adjoint/interface.py index 7fcd95f8..01f81dc1 100644 --- a/tlm_adjoint/interface.py +++ b/tlm_adjoint/interface.py @@ -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) @@ -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)