Skip to content

Commit 2caf7ec

Browse files
authored
Merge pull request #752 from NDM14/feature/614/raise-warning-capacity
Raise Warning if the Capacity Array is Not Set But mapc2p is
2 parents 62d693c + 2f526ee commit 2caf7ec

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/pyclaw/solution.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ def is_valid(self):
198198
- (bool) - True if valid, false otherwise
199199
"""
200200
return all([state.is_valid() for state in self.states])
201-
202-
203201
def __str__(self):
204202
output = "states:\n"
205203
# This is information about each of the states

src/pyclaw/state.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def is_valid(self):
178178
179179
"""
180180
import logging
181+
from .geometry import identity_map
181182
valid = True
182183
logger = logging.getLogger('pyclaw.solution')
183184
if not self.q.flags['F_CONTIGUOUS']:
@@ -187,6 +188,19 @@ def is_valid(self):
187188
if not self.aux.flags['F_CONTIGUOUS']:
188189
logger.debug('aux array is not Fortran contiguous.')
189190
valid = False
191+
if self.grid.mapc2p in list(identity_map.values()):
192+
pass # No mapping
193+
else:
194+
if self.aux is None:
195+
raise ValueError("Mapped grid requires a capacity function, stored in the aux array, " \
196+
"but no aux array is present. Please set state.num_aux to a positive value.")
197+
elif self.aux is not None:
198+
if self.index_capa == -1:
199+
raise ValueError("Capacity function index is not set. " \
200+
"Please set state.index_capa to the appropriate index in the aux array.")
201+
elif self.index_capa < 0 or self.index_capa > self.num_aux -1:
202+
raise ValueError("Capacity function index out of range. " \
203+
"Please set state.index_capa to the appropriate index in the aux array.")
190204
return valid
191205

192206
def set_cparam(self,fortran_module):

src/pyclaw/tests/test_state.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
from pyclaw import State
3+
import pyclaw
4+
from clawpack.pyclaw import Patch
5+
import numpy as np
6+
from examples.advection_2d_annulus import advection_annulus
7+
8+
class TestState():
9+
10+
@pytest.fixture
11+
def state(self) -> State:
12+
r_lower = 0.2
13+
r_upper = 1.0
14+
m_r = 40
15+
16+
theta_lower = 0.0
17+
theta_upper = np.pi*2.0
18+
m_theta = 120
19+
20+
r = pyclaw.Dimension(r_lower,r_upper,m_r,name='r')
21+
theta = pyclaw.Dimension(theta_lower,theta_upper,m_theta,name='theta')
22+
23+
patch = Patch([r, theta])
24+
patch.grid.mapc2p = advection_annulus.mapc2p_annulus
25+
patch.grid.num_ghost = 2
26+
num_eqn = 1
27+
state = State(patch,num_eqn)
28+
29+
advection_annulus.qinit(state)
30+
31+
dx, dy = state.grid.delta
32+
p_corners = state.grid.p_nodes
33+
state.aux = advection_annulus.edge_velocities_and_area(p_corners[0],p_corners[1],dx,dy)
34+
state.index_capa = 2
35+
return state
36+
37+
38+
@pytest.mark.parametrize(("raise_error", "aux_none", "invalid_index_capa"),
39+
[(False, False, -1), # Valid state
40+
(True, True, -1), # Invalid state: aux is None
41+
(True, False, -1), # Invalid state: index_capa is -1
42+
(True, False, -10)]) # Invalid state: index_capa is out of bounds
43+
def test_state_initialization(self, state: State,
44+
raise_error: bool,
45+
aux_none: bool,
46+
invalid_index_capa: int) -> None:
47+
if raise_error:
48+
if aux_none:
49+
state.aux = None
50+
elif state.aux is not None:
51+
state.index_capa = invalid_index_capa
52+
with pytest.raises(ValueError):
53+
assert not state.is_valid()
54+
55+
else:
56+
assert state.is_valid()

0 commit comments

Comments
 (0)