Skip to content
2 changes: 0 additions & 2 deletions src/pyclaw/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/pyclaw/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def is_valid(self):

"""
import logging
from .geometry import identity_map
valid = True
logger = logging.getLogger('pyclaw.solution')
if not self.q.flags['F_CONTIGUOUS']:
Expand All @@ -187,6 +188,19 @@ 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 in list(identity_map.values()):
pass # No mapping
else:
if 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.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):
Expand Down
56 changes: 56 additions & 0 deletions src/pyclaw/tests/test_state.py
Original file line number Diff line number Diff line change
@@ -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()