From a2744b7b0161b68fba43c7bb02eaaebefae70e5e Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Jun 2025 18:05:59 +0200 Subject: [PATCH] get_species_references function to fluid_system added to show data references --- src/gaspype/__init__.py | 20 ++++++++++++++------ tests/test_fluid_references.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 tests/test_fluid_references.py diff --git a/src/gaspype/__init__.py b/src/gaspype/__init__.py index e5ff204..2cc0eff 100644 --- a/src/gaspype/__init__.py +++ b/src/gaspype/__init__.py @@ -14,9 +14,9 @@ FloatArray = NDArray[NDFloat] _T = TypeVar('_T', 'fluid', 'elements') -data = pkgutil.get_data(__name__, 'data/therm_data.bin') -assert data is not None, 'Could not load thermodynamic data' -species_db = db_reader(data) +_data = pkgutil.get_data(__name__, 'data/therm_data.bin') +assert _data is not None, 'Could not load thermodynamic data' +_species_db = db_reader(_data) kB = 1.380649e-23 # J/K NA = 6.02214076e23 # 1/mol @@ -86,10 +86,10 @@ def species(pattern: str = '*', element_names: str | list[str] = [], use_regex: pattern = '^' + pattern + '(,.*)?$' if element_names == []: - return [sn for sn in species_db.names if re.fullmatch(pattern, sn)] + return [sn for sn in _species_db.names if re.fullmatch(pattern, sn)] else: return [ - s.name for s in species_db + s.name for s in _species_db if re.fullmatch(pattern, s.name) and (len(elements) == 0 or set(s.composition.keys()).issubset(elements))] @@ -160,7 +160,7 @@ class fluid_system: element_compositions: list[dict[str, int]] = list() for i, s in enumerate(species): - species_data = species_db.read(s) + species_data = _species_db.read(s) if not species_data: raise Exception(f'Species {s} not found') element_compositions.append(species_data.composition) @@ -240,6 +240,14 @@ class fluid_system: Array of gibbs free energy divided by RT (dimensionless) """ return lookup(self._g_rt_array, t, self._t_offset) + + def get_species_references(self) -> str: + """Get a string with the references for all fluids of the fluid system + + Returns: + String with the references + """ + return '\n'.join([f'{s:<12}: {_species_db[s].ref_string}' for s in self.species]) def __add__(self, other: 'fluid_system') -> 'fluid_system': assert isinstance(other, self.__class__) diff --git a/tests/test_fluid_references.py b/tests/test_fluid_references.py new file mode 100644 index 0000000..c2f9ead --- /dev/null +++ b/tests/test_fluid_references.py @@ -0,0 +1,12 @@ +import gaspype as gp + +def test_fluid_references(): + fs = gp.fluid_system('Cl, CH4, H2O, C2H6, C3H8') + + reference_string = """Cl : Hf:Cox,1989. Moore,1971. Moore,1970a. Gordon,1999. [g 7/97] +CH4 : Gurvich,1991 pt1 p44 pt2 p36. [g 8/99] +H2O : Hf:Cox,1989. Woolley,1987. TRC(10/88) tuv25. [g 8/89] +C2H6 : Ethane. Pamidimukkala,1982. [g 7/00] +C3H8 : Hf:TRC(10/85) w1350. Chao,1973. [g 2/00]""" + + assert reference_string == fs.get_species_references(), 'fs.get_species_references() == ' + fs.get_species_references() \ No newline at end of file