diff --git a/src/pyclaw/solution.py b/src/pyclaw/solution.py index 6a6c8cdc..08d209bd 100755 --- a/src/pyclaw/solution.py +++ b/src/pyclaw/solution.py @@ -198,8 +198,6 @@ def is_valid(self): - (bool) - True if valid, false otherwise """ return all([state.is_valid() for state in self.states]) - - def __str__(self): output = "states:\n" # This is information about each of the states diff --git a/src/pyclaw/state.py b/src/pyclaw/state.py index eff412f7..ff8b6f56 100755 --- a/src/pyclaw/state.py +++ b/src/pyclaw/state.py @@ -187,6 +187,16 @@ def is_valid(self): if not self.aux.flags['F_CONTIGUOUS']: logger.debug('aux array is not Fortran contiguous.') valid = False + if self.grid.mapc2p is not None and self.aux is None: + raise ValueError("Mapped grid requires a capacity function, stored in the aux array, " \ + "but no aux array is present. Please set state.num_aux to a positive value.") + elif self.grid.mapc2p is not None and self.aux is not None: + if self.index_capa == -1: + raise ValueError("Capacity function index is not set. " \ + "Please set state.index_capa to the appropriate index in the aux array.") + elif self.index_capa < 0 or self.index_capa > self.num_aux -1: + raise ValueError("Capacity function index out of range. " \ + "Please set state.index_capa to the appropriate index in the aux array.") return valid def set_cparam(self,fortran_module): diff --git a/src/pyclaw/tests/unittests/test_state.py b/src/pyclaw/tests/unittests/test_state.py new file mode 100644 index 00000000..cb71693c --- /dev/null +++ b/src/pyclaw/tests/unittests/test_state.py @@ -0,0 +1,56 @@ +import pytest +from pyclaw import State +import pyclaw +from clawpack.pyclaw import Patch +import numpy as np +from examples.advection_2d_annulus import advection_annulus + +class TestState(): + + @pytest.fixture + def state(self) -> State: + r_lower = 0.2 + r_upper = 1.0 + m_r = 40 + + theta_lower = 0.0 + theta_upper = np.pi*2.0 + m_theta = 120 + + r = pyclaw.Dimension(r_lower,r_upper,m_r,name='r') + theta = pyclaw.Dimension(theta_lower,theta_upper,m_theta,name='theta') + + patch = Patch([r, theta]) + patch.grid.mapc2p = advection_annulus.mapc2p_annulus + patch.grid.num_ghost = 2 + num_eqn = 1 + state = State(patch,num_eqn) + + advection_annulus.qinit(state) + + dx, dy = state.grid.delta + p_corners = state.grid.p_nodes + state.aux = advection_annulus.edge_velocities_and_area(p_corners[0],p_corners[1],dx,dy) + state.index_capa = 2 + return state + + + @pytest.mark.parametrize(("raise_error", "aux_none", "invalid_index_capa"), + [(False, False, -1), # Valid state + (True, True, -1), # Invalid state: aux is None + (True, False, -1), # Invalid state: index_capa is -1 + (True, False, -10)]) # Invalid state: index_capa is out of bounds + def test_state_initialization(self, state: State, + raise_error: bool, + aux_none: bool, + invalid_index_capa: int) -> None: + if raise_error: + if aux_none: + state.aux = None + elif state.aux is not None: + state.index_capa = invalid_index_capa + with pytest.raises(ValueError): + assert not state.is_valid() + + else: + assert state.is_valid() \ No newline at end of file