66import numpy as np
77import pytest
88
9- from pydrad .parse import Strand
9+ from pydrad .parse import Profile , Strand
1010
1111VAR_NAMES = [
1212 'coordinate' ,
6363def strand (hydrad ):
6464 return Strand (hydrad )
6565
66+ @pytest .fixture
67+ def strand_only_amr_time_cfg (hydrad ):
68+ return Strand (hydrad ,
69+ read_from_cfg = True ,
70+ read_phy = False ,
71+ read_ine = False ,
72+ read_trm = False ,
73+ read_hstate = False ,
74+ read_scl = False )
75+
6676@pytest .fixture
6777def strand_only_amr (hydrad ):
6878 return Strand (hydrad ,
6979 read_phy = False ,
7080 read_ine = False ,
7181 read_trm = False ,
7282 read_hstate = False ,
73- read_scl = False ,
74- )
83+ read_scl = False )
7584
7685
7786def test_parse_initial_conditions (strand ):
@@ -111,6 +120,15 @@ def test_time_arrays_same(hydrad, strand):
111120 assert u .allclose (strand .time , strand2 .time , rtol = 0.0 , atol = 1e-2 * u .s )
112121
113122
123+ def test_strand_indexing (strand_only_amr ):
124+ # Make sure indexing is tracked correctly across repeated slicing
125+ # of strand
126+ assert strand_only_amr [2 ]._index == 2
127+ strand_slice = strand_only_amr [1 :4 ]
128+ assert strand_slice [1 ]._index == 2
129+ assert strand_slice [1 ]._amr_filename == strand_only_amr [2 ]._amr_filename
130+
131+
114132def test_to_hdf5 (strand , tmp_path ):
115133 filename = tmp_path / 'hydrad_results.h5'
116134 strand .to_hdf5 (filename , * VAR_NAMES )
@@ -130,6 +148,7 @@ def test_emission_measure(strand):
130148 assert isinstance (bins , u .Quantity )
131149 assert len (bins ) == len (em ) + 1
132150
151+
133152def test_term_file_output (strand ):
134153 for p in strand :
135154 # The electron energy equation's numerical viscosity term is always 0:
@@ -145,26 +164,45 @@ def test_term_file_output(strand):
145164 rtol = 0.0 ,
146165 )
147166
167+
148168def test_term_file_units (strand ):
149169 assert strand [0 ].mass_advection .unit == u .Unit ('g s-1 cm-3' )
150170 assert strand [0 ].momentum_gravity .unit == u .Unit ('dyne s-1 cm-3' )
151171 assert strand [0 ].electron_viscous_stress .unit == u .Unit ('erg s-1 cm-3' )
152172 assert strand [0 ].hydrogen_collisions .unit == u .Unit ('erg s-1 cm-3' )
153173
174+
154175def test_scale_file_output (strand ):
155176 for p in strand :
156177 # all time-scales should be strictly greater than 0
157178 assert all (t > (0.0 * u .s ) for t in p .radiative_timescale )
158179 assert all (t > (0.0 * u .s ) for t in p .collisional_timescale )
159180 assert all (t > (0.0 * u .s ) for t in p .ion_conductive_timescale )
160181
182+
161183def test_scale_file_units (strand ):
162184 assert strand [0 ].advective_timescale .unit == u .Unit ('s' )
163185 assert strand [0 ].electron_conductive_timescale .unit == u .Unit ('s' )
164186 assert strand [0 ].collisional_timescale .unit == u .Unit ('s' )
165187
188+
166189def test_amr_file_units (strand , strand_only_amr ):
167190 assert strand [0 ].mass_density .unit == u .Unit ('g cm-3' )
168191 assert strand_only_amr [0 ].mass_density .unit == u .Unit ('g cm-3' )
169192 assert strand [0 ].electron_mass_density .unit == u .Unit ('g cm-3' )
170193 assert strand_only_amr [0 ].electron_mass_density .unit == u .Unit ('g cm-3' )
194+
195+
196+ def test_profile_instantiation (strand_only_amr , strand_only_amr_time_cfg ):
197+ # Test various ways to instantiate a Profile
198+ # No index, no master time
199+ p = Profile (strand_only_amr .hydrad_root , strand_only_amr .time [1 ])
200+ assert p ._index == 1
201+ # No index, no master time, read from cfg
202+ # NOTE: This uses a different strand object as the original time array must also be read from the cfg
203+ # file rather than the array file. Otherwise, the values will be slightly different.
204+ p = Profile (strand_only_amr_time_cfg .hydrad_root , strand_only_amr_time_cfg .time [1 ], read_from_cfg = True )
205+ assert p ._index == 1
206+ # No index, master time
207+ p = Profile (strand_only_amr .hydrad_root , strand_only_amr .time [1 ], master_time = strand_only_amr ._master_time )
208+ assert p ._index == 1
0 commit comments