Commit 2cc472aa authored by Joseph Weston's avatar Joseph Weston

check for missing system parameters in 'hamiltonian_submatrix'

parent cc717c32
......@@ -242,6 +242,17 @@ def make_dense_full(ham, args, params, CGraph gr, diag,
return h_sub
def _check_parameters_match(expected_parameters, params):
if params is None:
params = {}
missing = set(expected_parameters) - set(params)
if missing:
msg = ('System is missing required parameters: ',
', '.join(map('"{}"'.format, missing)))
raise TypeError(''.join(msg))
@cython.embedsignature(True)
def hamiltonian_submatrix(self, args=(), to_sites=None, from_sites=None,
sparse=False, return_norb=False, *, params=None):
......@@ -287,6 +298,9 @@ def hamiltonian_submatrix(self, args=(), to_sites=None, from_sites=None,
n = self.graph.num_nodes
matrix = ta.matrix
if not args: # Then perhaps parameters
_check_parameters_match(self.parameters, params)
if from_sites is None:
diag = n * [None]
from_norb = np.empty(n, gint_dtype)
......
......@@ -1225,9 +1225,11 @@ def test_argument_passing():
with raises(TypeError):
inf_syst.hamiltonian(0, 0, *(2, 1), params=dict(p1=2, p2=1))
# test that missing any parameters raises TypeError
# Missing parameters raises TypeError
with raises(TypeError):
syst.hamiltonian(0, 0, params=dict(fake=10))
syst.hamiltonian(0, 0, params=dict(p1=2))
with raises(TypeError):
syst.hamiltonian_submatrix(params=dict(p1=2))
# test that passing parameters without default values works, and that
# passing parameters with default values fails
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment