def _read_hstate(self): """ Parse the hydrogen energy level populations file """ try: self._hstate_data = np.loadtxt(self._hstate_filename) except OSError: log.debug(f'{self._hstate_filename} not found') self._hstate_data = None
def _read_ine(self): """ Parse non-equilibrium ionization population fraction files and set attributes for relevant quantities """ # TODO: clean this up somehow? I've purposefully included # a lot of comments because the format of this file makes # the parsing code quite opaque try: with open(self._ine_filename, 'r') as f: lines = f.readlines() except FileNotFoundError: log.debug(f'{self._ine_filename} not found') return # First parse all of the population fraction arrays n_s = self.coordinate.shape[0] # NOTE: Have to calculate the number of elements we have # computed population fractions for as we do not necessarily # know this ahead of time n_e = int(len(lines) / n_s - 1) # The file is arranged in n_s groups of n_e+1 lines each where the first # line is the coordinate and the subsequent lines are the population fraction # for each element, with each column corresponding to an ion of that element # First, separate by coordinate pop_lists = [ lines[i * (n_e + 1) + 1:(i + 1) * (n_e + 1)] for i in range(n_s) ] # Convert each row of each group into a floating point array pop_lists = [[np.array(l.split(), dtype=float) for l in p] for p in pop_lists] # NOTE: each row has Z+2 entries as the first entry is the atomic number Z # Get these from just the first group as the number of elements is the same # for each Z = np.array([p[0] for p in pop_lists[0]], dtype=int) pop_arrays = [np.zeros((n_s, z + 1)) for z in Z] for i, p in enumerate(pop_lists): for j, l in enumerate(p): pop_arrays[j][i, :] = l[ 1:] # Skip first entry, it is the atomic number # Then set attributes for each ion of each element for z, p in zip(Z, pop_arrays): name = plasmapy.particles.element_name(z) attr = f'_population_fraction_{name}' setattr(self, attr, p) for p in [(f'{attr[1:]}_{i+1}', attr, i, '') for i in range(z + 1)]: add_property(*p)
def get_master_time(hydrad_root, read_from_cfg=False): amr_files = glob.glob(os.path.join(hydrad_root, 'Results/profile*.amr')) if read_from_cfg: log.debug('Creating master time array from config files') with open(os.path.join(hydrad_root, 'HYDRAD/config/hydrad.cfg'), 'r') as f: lines = f.readlines() cadence = float(lines[3]) with open(amr_files[0]) as f: start_time = float(f.readline()) time = start_time + np.arange(len(amr_files)) * cadence else: log.debug('Reading master time array from all AMR files') time = np.zeros((len(amr_files), )) for i, af in enumerate(amr_files): with open(af, 'r') as f: time[i] = f.readline() return sorted(time) * u.s
def _read_trm(self): """ Parse the equation terms file """ try: with open(self._trm_filename, 'r') as f: lines = f.readlines() except FileNotFoundError: log.debug(f'{self._trm_filename} not found') return n_elements = int(len(lines) / 5) self._trm_data = np.zeros([n_elements, 3]) # The files come in sets of 5 rows # -- Loop coordinate, and at each one: # -- Terms of mass equation # -- Terms of momentum equation # -- Terms of electron energy equation # -- Terms of hydrogen energy equation # Right now, we only read 3 values from this: # the electron heating, hydrogen heating, # and bolometric radiative losses for i in range(len(lines)): j = int(i / 5) line = lines[i].strip().split() # Electron heating and radiative loss terms from the # electron energy equation if i % 5 == 3: self._trm_data[j, 0] = float(line[5]) self._trm_data[j, 1] = float(line[6]) # Hydrogen heating term from the hydrogen energy # equation if i % 5 == 4: self._trm_data[j, 2] = float(line[5]) properties = [ ('electron_heating_term', '_trm_data', 0, 'erg cm-3 s-1'), ('hydrogen_heating_term', '_trm_data', 2, 'erg cm-3 s-1'), ('radiative_loss_term', '_trm_data', 1, 'erg cm-3 s-1') ] for p in properties: add_property(*p)
def get_master_time(hydrad_root, read_from_cfg=False): """ Get array of times that correspond to each timestep for the entire simulation. Parameters ---------- hydrad_root : `str` or path-like read_from_cfg : `bool`, optional If True, create the time array from the cadence as specified in `HYDRAD/config/hydrad.cfg` and the start time as given in the first AMR file. Note that this is substantially faster than reading the time from every AMR file, but there may be small differences between these approximate time steps and the exact time steps listed in the AMR files. Returns ------- : `~astropy.units.Quantity` """ amr_files = sorted( glob.glob(os.path.join(hydrad_root, 'Results/profile*.amr'))) if read_from_cfg: log.debug('Creating master time array from config files') # NOTE: Sometimes this file is capitalized and some OSes are sensitive to this cfg_file = os.path.join(hydrad_root, 'HYDRAD/config/hydrad.cfg') if not os.path.isfile(cfg_file): log.debug('hydrad.cfg not found; trying HYDRAD.cfg') cfg_file = os.path.join(hydrad_root, 'HYDRAD/config/HYDRAD.cfg') with open(cfg_file, 'r') as f: lines = f.readlines() cadence = float(lines[3]) with open(amr_files[0]) as f: start_time = float(f.readline()) time = start_time + np.arange(len(amr_files)) * cadence else: log.debug('Reading master time array from all AMR files') time = np.zeros((len(amr_files), )) for i, af in enumerate(amr_files): with open(af, 'r') as f: time[i] = f.readline() return sorted(time) * u.s
def get_master_time(hydrad_root, read_from_cfg=False): amr_files = sorted(glob.glob(os.path.join(hydrad_root, 'Results/profile*.amr'))) if read_from_cfg: log.debug('Creating master time array from config files') # NOTE: Sometimes this file is capitalized and some OSes are sensitive to this cfg_file = os.path.join(hydrad_root, 'HYDRAD/config/hydrad.cfg') if not os.path.isfile(cfg_file): log.debug('hydrad.cfg not found; trying HYDRAD.cfg') cfg_file = os.path.join(hydrad_root, 'HYDRAD/config/HYDRAD.cfg') with open(cfg_file, 'r') as f: lines = f.readlines() cadence = float(lines[3]) with open(amr_files[0]) as f: start_time = float(f.readline()) time = start_time + np.arange(len(amr_files)) * cadence else: log.debug('Reading master time array from all AMR files') time = np.zeros((len(amr_files),)) for i, af in enumerate(amr_files): with open(af, 'r') as f: time[i] = f.readline() return sorted(time) * u.s