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