def load_thermo_lib_by_path(path: str, thermo_db: ThermoDatabase): """ Load thermo library given its path. This is really helpful when the library is not located in the RMG-database. Args: path (str): Path to thermo library file thermo_database (ThermoDatabase): RMG thermo database object """ if not os.path.exists(path): raise ValueError(f'The library file {path} does not exist.') if path not in thermo_db.library_order: lib = ThermoLibrary() try: lib.load(path, ThermoDatabase().local_context, ThermoDatabase().global_context) except: raise ValueError(f'The library file {path} is not vaild.') return else: lib.label = path thermo_db.libraries[lib.label] = lib thermo_db.library_order.append(lib.label) print(f'Loading thermo library {os.path.split(path)[1]} ' f'from {os.path.split(path)[0]} ...') else: print(f'The library {path} has already been loaded')
def import_thermo(thermo_path, kinetic_model, models): local_context = { "ThermoData": ThermoData, "Wilhoit": Wilhoit, "NASAPolynomial": NASAPolynomial, "NASA": NASA, } library = ThermoLibrary(label=kinetic_model.model_name) # NOTE: In order for this feature to run we have to be on "rmg-py/importer" branch, may require reinstall # noqa: E501 library.SKIP_DUPLICATES = True library.load(thermo_path, local_context=local_context) for species_name, entry in library.entries.items(): logger.info(f"Importing Thermo entry for {species_name}") try: species = get_or_create_species(kinetic_model, species_name, [entry.item], models) thermo_data = entry.data poly1, poly2 = thermo_data.polynomials thermo, _ = models.Thermo.objects.get_or_create( species=species, coeffs_poly1=poly1.coeffs.tolist(), coeffs_poly2=poly2.coeffs.tolist(), temp_min_1=poly1.Tmin.value_si, temp_max_1=poly1.Tmax.value_si, temp_min_2=poly2.Tmin.value_si, temp_max_2=poly2.Tmax.value_si, ) thermo_comment = models.ThermoComment.objects.create( kinetic_model=kinetic_model, thermo=thermo) thermo_comment.comment = entry.long_desc or entry.short_desc or thermo_comment.comment thermo.save() thermo_comment.save() except Exception: logger.exception("Failed to import entry")
def read_thermo_lib_by_path(lib_path, thermo_db): """ Read thermo library given its library path Args: lib_path (str): Path to thermo library file thermo_database (ThermoDatabase): RMG thermo database object """ if not os.path.exists(lib_path): logging.error('The library file %s does not exist.' %(lib_path)) return if lib_path not in thermo_db.library_order: lib = ThermoLibrary() try: lib.load(lib_path, ThermoDatabase().local_context, ThermoDatabase().global_context) except: logging.error('The library file %s is not vaild.' % (lib_path)) return else: lib.label = lib_path thermo_db.libraries[lib.label] = lib thermo_db.library_order.append(lib.label) logging.info('Loading thermodynamics library {1} from {0} ...'.format( os.path.split(lib_path)[0], os.path.split(lib_path)[1]),) else: logging.warning('The library %s has already been loaded' %(lib_path))
def load_thermo_lib_by_path(path: str, thermo_db: ThermoDatabase, reload: bool = False): """ Load thermo library given its library path and store it into the given RMG ThermoDatabase instance Args: path (str): Path to thermo library file thermo_database (ThermoDatabase): RMG thermo database object reload (bool): Whether to reload the library if this library is in the ThermoDatabase """ lib = ThermoLibrary() try: lib.load(path, ThermoDatabase().local_context, ThermoDatabase().global_context) except FileNotFoundError: print(f'The library file {path} does not exist.') except (SyntaxError, ImportError): print(f'The library file {path} is not valid.') else: if path in thermo_db.library_order and not reload: print(f'The library {path} has already been loaded.') return elif path not in thermo_db.library_order: thermo_db.library_order.append(path) lib.label = path lib.name = path thermo_db.libraries[lib.label] = lib print(f'The thermodynamics library {path} is loaded.')
def find_thermo_libs(path): """ This function search for the thermo library based on /thermo/*.py or /libraries/*.py Args: path (str): The path to project directories Returns: thermo_lib_list (list): Entries of the path to thermo libraries """ # Initiate the thermo lib list thermo_lib_list = list() # Walk through the dirs under path for root_p, dirs, _ in os.walk(path): if not dirs: continue # Use ARC folder organization to check thermo library chk_path = os.path.join(root_p, 'thermo') if os.path.isdir(chk_path): # Find the corresponding thermo lib file thermo_lib = glob.glob(os.path.join(chk_path, '*.py'))[0] # Check the lib is valid by loading it if thermo_lib: try: lib = ThermoLibrary() lib.load(thermo_lib, ThermoDatabase().local_context, ThermoDatabase().global_context) except: continue else: thermo_lib_list.append(thermo_lib) logging.info("Find thermo library at {0}".format(thermo_lib)) return thermo_lib_list