def load_fields_from_yt_config(cls) -> List[str]: if cls.config_field and ytcfg.has_section(cls.config_field): cfg = ytcfg.get(cls.config_field, "fields") fields = [_.strip() for _ in cfg if _.strip() != ""] return fields return []
def __init__(self, ds, domain_id): ''' Initalize an instance of the class. This automatically sets the full path to the file. This is not intended to be overriden in most cases. If you need more flexibility, rewrite this function to your need in the inherited class. ''' self.ds = ds self.domain_id = domain_id basename = os.path.abspath(os.path.dirname(ds.parameter_filename)) iout = int( os.path.basename( ds.parameter_filename).split(".")[0].split("_")[1]) icpu = domain_id self.fname = os.path.join(basename, self.fname.format(iout=iout, icpu=icpu)) if self.file_descriptor is not None: self.file_descriptor = os.path.join(basename, self.file_descriptor) # Attempt to read the list of fields from the config file if self.config_field and ytcfg.has_section(self.config_field): cfg = ytcfg.get(self.config_field, 'fields') known_fields = [] for c in (_.strip() for _ in cfg.split('\n') if _.strip() != ''): field, field_type = (_.strip() for _ in c.split(',')) known_fields.append((field, field_type)) self.known_fields = known_fields
def __init__(self, domain): self.setup_handler(domain) # Attempt to read the list of fields from the config file if self.config_field and ytcfg.has_section(self.config_field): cfg = ytcfg.get(self.config_field, "fields") known_fields = [] for c in (_.strip() for _ in cfg.split("\n") if _.strip() != ""): field, field_type = (_.strip() for _ in c.split(",")) known_fields.append((field, field_type)) self.known_fields = known_fields
def __init__(self, ds, domain): """ Initalize an instance of the class. This automatically sets the full path to the file. This is not intended to be overriden in most cases. If you need more flexibility, rewrite this function to your need in the inherited class. """ self.ds = ds self.domain = domain self.domain_id = domain.domain_id basename = os.path.abspath(ds.root_folder) iout = int(os.path.basename(ds.parameter_filename).split(".")[0].split("_")[1]) if ds.num_groups > 0: igroup = ((domain.domain_id - 1) // ds.group_size) + 1 full_path = os.path.join( basename, "group_{:05d}".format(igroup), self.fname.format(iout=iout, icpu=domain.domain_id), ) else: full_path = os.path.join( basename, self.fname.format(iout=iout, icpu=domain.domain_id) ) if os.path.exists(full_path): self.fname = full_path else: raise FileNotFoundError( "Could not find particle file (type: %s). Tried %s" % (self.ptype, full_path) ) if self.file_descriptor is not None: if ds.num_groups > 0: # The particle file descriptor is *only* in the first group self.file_descriptor = os.path.join( basename, "group_00001", self.file_descriptor ) else: self.file_descriptor = os.path.join(basename, self.file_descriptor) # Attempt to read the list of fields from the config file if self.config_field and ytcfg.has_section(self.config_field): cfg = ytcfg.get(self.config_field, "fields") known_fields = [] for c in (_.strip() for _ in cfg.split("\n") if _.strip() != ""): field, field_type = (_.strip() for _ in c.split(",")) known_fields.append((field, field_type)) self.known_fields = known_fields
"Pressure", "Metallicity", ), } ## Regular expressions used to parse file descriptors VERSION_RE = re.compile(r"# version: *(\d+)") # This will match comma-separated strings, discarding whitespaces # on the left hand side VAR_DESC_RE = re.compile(r"\s*([^\s]+),\s*([^\s]+),\s*([^\s]+)") ## Configure family mapping particle_families = { "DM": 1, "star": 2, "cloud": 3, "dust": 4, "star_tracer": -2, "cloud_tracer": -3, "dust_tracer": -4, "gas_tracer": 0, } if ytcfg.has_section("ramses-families"): for key in particle_families.keys(): val = ytcfg.getint("ramses-families", key, fallback=None) if val is not None: mylog.info("Changing family %s from %s to %s", key, particle_families[key], val) particle_families[key] = val
def detect_fields(cls, ds): # Try to get the detected fields detected_fields = cls.get_detected_fields(ds) if detected_fields: return detected_fields num = os.path.basename( ds.parameter_filename).split(".")[0].split("_")[1] testdomain = 1 # Just pick the first domain file to read basepath = os.path.abspath(os.path.dirname(ds.parameter_filename)) basename = "%s/%%s_%s.out%05i" % (basepath, num, testdomain) fname = basename % 'hydro' fname_desc = os.path.join(basepath, cls.file_descriptor) attrs = cls.attrs with FortranFile(fname) as fd: hvals = fd.read_attrs(attrs) cls.parameters = hvals # Store some metadata ds.gamma = hvals['gamma'] nvar = hvals['nvar'] ok = False # Either the fields are given by dataset if ds._fields_in_file is not None: fields = list(ds._fields_in_file) ok = True elif os.path.exists(fname_desc): # Or there is an hydro file descriptor mylog.debug('Reading hydro file descriptor.') # For now, we can only read double precision fields fields = [e[0] for e in _read_fluid_file_descriptor(fname_desc)] # We get no fields for old-style hydro file descriptor ok = len(fields) > 0 elif cls.config_field and ytcfg.has_section(cls.config_field): # Or this is given by the config cfg = ytcfg.get(cls.config_field, 'fields') known_fields = [] for field in (_.strip() for _ in cfg.split('\n') if _.strip() != ''): known_fields.append(field.strip()) fields = known_fields ok = True # Else, attempt autodetection if not ok: foldername = os.path.abspath(os.path.dirname( ds.parameter_filename)) rt_flag = any(glob.glob(os.sep.join([foldername, 'info_rt_*.txt']))) if rt_flag: # rt run if nvar < 10: mylog.info('Detected RAMSES-RT file WITHOUT IR trapping.') fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "Pressure", "Metallicity", "HII", "HeII", "HeIII" ] else: mylog.info('Detected RAMSES-RT file WITH IR trapping.') fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "Pres_IR", "Pressure", "Metallicity", "HII", "HeII", "HeIII" ] else: if nvar < 5: mylog.debug( "nvar=%s is too small! YT doesn't currently support 1D/2D runs in RAMSES %s" ) raise ValueError # Basic hydro runs if nvar == 5: fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "Pressure" ] if nvar > 5 and nvar < 11: fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "Pressure", "Metallicity" ] # MHD runs - NOTE: THE MHD MODULE WILL SILENTLY ADD 3 TO THE NVAR IN THE MAKEFILE if nvar == 11: fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "x-Bfield-left", "y-Bfield-left", "z-Bfield-left", "x-Bfield-right", "y-Bfield-right", "z-Bfield-right", "Pressure" ] if nvar > 11: fields = [ "Density", "x-velocity", "y-velocity", "z-velocity", "x-Bfield-left", "y-Bfield-left", "z-Bfield-left", "x-Bfield-right", "y-Bfield-right", "z-Bfield-right", "Pressure", "Metallicity" ] mylog.debug( "No fields specified by user; automatically setting fields array to %s" % str(fields)) # Allow some wiggle room for users to add too many variables count_extra = 0 while len(fields) < nvar: fields.append("var" + str(len(fields))) count_extra += 1 if count_extra > 0: mylog.debug('Detected %s extra fluid fields.' % count_extra) cls.field_list = [(cls.ftype, e) for e in fields] cls.set_detected_fields(ds, fields) return fields
'd'), ('nstep', 2, 'i'), ('stat', 3, 'd'), ('cosm', 7, 'd'), ('timing', 5, 'd'), ('mass_sph', 1, 'd')) yield next_set field_aliases = { 'standard_five': ('Density', 'x-velocity', 'y-velocity', 'z-velocity', 'Pressure'), 'standard_six': ('Density', 'x-velocity', 'y-velocity', 'z-velocity', 'Pressure', 'Metallicity'), } particle_families = { 'DM': 1, 'star': 2, 'cloud': 3, 'dust': 4, 'star_tracer': -2, 'cloud_tracer': -3, 'dust_tracer': -4, 'gas_tracer': 0 } if ytcfg.has_section('ramses-families'): for key in particle_families.keys(): val = ytcfg.getint('ramses-families', key, fallback=None) if val is not None: mylog.info('Changing family %s from %s to %s' % (key, particle_families[key], val)) particle_families[key] = val