| 1 |
"""Solvent database for media concentration corrections. |
| 2 |
|
| 3 |
Solvent molecular weights [g/mol] and densities [g/mL] at 20 °C, loaded from |
| 4 |
solvents.json alongside this module. Each solvent entry defines a canonical name |
| 5 |
and one or more lookup aliases (abbreviations, full names). |
| 6 |
|
| 7 |
Public API: |
| 8 |
solvents -- dict mapping alias (lowercase str) -> (mw, density) tuple |
| 9 |
""" |
| 10 |
|
| 11 |
import json |
| 12 |
import os |
| 13 |
|
| 14 |
|
| 15 |
def _load_solvents(): |
| 16 |
""" |
| 17 |
Load the bundled solvents.json and build a flat mapping from each lowercase alias to its (molecular weight, density) tuple. |
| 18 |
|
| 19 |
The JSON file is read from the same directory as this module. Each solvent entry's `aliases` list is expanded and normalized to lowercase. |
| 20 |
|
| 21 |
Returns: |
| 22 |
dict: Mapping where keys are lowercase alias strings and values are `(mw, density)` tuples — `mw` in g/mol and `density` in g/mL. |
| 23 |
""" |
| 24 |
json_path = os.path.join(os.path.dirname(__file__), 'solvents.json') |
| 25 |
with open(json_path, 'r') as f: |
| 26 |
data = json.load(f) |
| 27 |
|
| 28 |
result = {} |
| 29 |
for entry in data['solvents']: |
| 30 |
value = (entry['mw'], entry['density']) |
| 31 |
for alias in entry['aliases']: |
| 32 |
result[alias.lower()] = value |
| 33 |
return result |
| 34 |
|
| 35 |
|
| 36 |
solvents = _load_solvents() |
| 37 |
|
| 38 |
|
| 39 |
def lookup_solvent(name): |
| 40 |
"""Look up a solvent by alias and return its (mw, density). |
| 41 |
|
| 42 |
Raises ValueError with a "did you mean ..." hint and a list of common |
| 43 |
aliases if `name` is unknown. Use this to validate user input (e.g. |
| 44 |
--media / --freespace) up-front; `compute_media_conc` and other call |
| 45 |
sites can then assume the alias is valid. |
| 46 |
""" |
| 47 |
key = name.lower() |
| 48 |
if key not in solvents: |
| 49 |
import difflib |
| 50 |
suggestions = difflib.get_close_matches(key, solvents.keys(), n=5, cutoff=0.5) |
| 51 |
common = ['water', 'methanol', 'ethanol', 'acetone', 'dmso', 'dmf', |
| 52 |
'thf', 'benzene', 'toluene', 'chloroform', 'dcm', 'acetonitrile'] |
| 53 |
msg = f"Unknown solvent {name!r}." |
| 54 |
if suggestions: |
| 55 |
msg += f" Did you mean: {', '.join(suggestions)}?" |
| 56 |
msg += (f" Common aliases: {', '.join(common)} " |
| 57 |
f"({len(solvents)} total in goodvibes/solvents.json).") |
| 58 |
raise ValueError(msg) |
| 59 |
return solvents[key] |
| 60 |
|
| 61 |
|
| 62 |
def compute_media_conc(media, file): |
| 63 |
""" |
| 64 |
Compute the neat-solvent molar concentration when the output file corresponds to the specified solvent. |
| 65 |
|
| 66 |
Parameters: |
| 67 |
media (str): Solvent name as provided (e.g., from --media). |
| 68 |
file (str): Path to the output file used to infer the solvent name. |
| 69 |
|
| 70 |
Returns: |
| 71 |
float or None: Neat-solvent concentration in mol/L if the file's solvent matches `media`, `None` otherwise. |
| 72 |
""" |
| 73 |
from .utils import display_name |
| 74 |
key = media.lower() |
| 75 |
if key in solvents and key == display_name(file).lower(): |
| 76 |
mweight, density = solvents[key] |
| 77 |
return (density * 1000) / mweight |
| 78 |
return None |