Skip to content

Commit

Permalink
AGani properties calculations and Joback has units now
Browse files Browse the repository at this point in the history
  • Loading branch information
SalvadorBrandolin committed Dec 20, 2024
1 parent d0e1915 commit 79351bc
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 109 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ dependencies = [
'rdkit >= 2023.9.5',
'pandas >= 2.0.3',
'pubchempy >= 1.0.4',
'pulp >= 2.9.0'
'pulp >= 2.9.0',
'pint >= 0.22'
]
138 changes: 110 additions & 28 deletions tests/miscs/test_joback_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,109 @@

import pytest

from pint import Quantity as q

from rdkit import Chem

from ugropy import joback
from ugropy import joback, ureg


@pytest.mark.joback
def test_p_dichlorobenzene():
mol = joback.get_groups("C1=CC(=CC=C1Cl)Cl", "smiles")

assert mol.subgroups == {"-Cl": 2, "ring=CH-": 4, "ring=C<": 2}
assert np.allclose(mol.normal_boiling_point, 443.4, atol=1e-2)
assert np.allclose(mol.fusion_temperature, 256, atol=1)
assert np.allclose(mol.critical_temperature, 675, atol=1)
assert np.allclose(mol.critical_pressure, 41.5, atol=1e-1)
assert np.allclose(mol.critical_volume, 362, atol=1)
assert np.allclose(mol.h_formation, 26.41, atol=1e-2)
assert np.allclose(mol.g_formation, 78.56, atol=1e-2)
assert np.allclose(mol.heat_capacity_ideal_gas(298), 112.3, atol=1)
assert np.allclose(mol.heat_capacity_ideal_gas(400), 139.2, atol=1)
assert np.allclose(mol.heat_capacity_ideal_gas(800), 206.8, atol=1)
assert np.allclose(mol.heat_capacity_ideal_gas(1000), 224.6, atol=1)
assert np.allclose(mol.h_vaporization, 40.66, atol=1e-2)
assert np.allclose(mol.h_fusion, 13.3, atol=1e-1)
assert np.allclose(mol.viscosity_liquid(333.8), 7.26e-4, atol=1e-6)
assert np.allclose(mol.viscosity_liquid(374.4), 4.92e-4, atol=1e-6)
assert np.allclose(mol.viscosity_liquid(403.1), 3.91e-4, atol=1e-6)
assert np.allclose(mol.viscosity_liquid(423.3), 3.40e-4, atol=1e-6)

assert np.allclose(mol.normal_boiling_point, q(443.4, "K"), atol=1e-2)
assert mol.normal_boiling_point.units == ureg.kelvin

assert np.allclose(mol.fusion_temperature, q(256, "K"), atol=1)
assert mol.fusion_temperature.units == ureg.kelvin

assert np.allclose(mol.critical_temperature, q(675, "K"), atol=1)
assert mol.critical_temperature.units == ureg.kelvin

assert np.allclose(mol.critical_pressure, q(41.5, "bar"), atol=1e-1)
assert mol.critical_pressure.units == ureg.bar

assert np.allclose(mol.critical_volume, q(362, "cm^3/mol"), atol=1)
assert mol.critical_volume.units == ureg.centimeter**3 / ureg.mole

assert np.allclose(
mol.ig_enthalpy_formation, q(26.41, "kJ/mol"), atol=1e-2
)
assert mol.ig_enthalpy_formation.units == ureg.kilojoule / ureg.mole

assert np.allclose(mol.ig_gibbs_formation, q(78.56, "kJ/mol"), atol=1e-2)
assert mol.ig_gibbs_formation.units == ureg.kilojoule / ureg.mole

assert np.allclose(
mol.heat_capacity_ideal_gas(298), q(112.3, "J/mol/K"), atol=1
)
assert (
mol.heat_capacity_ideal_gas(298).units
== ureg.joule / ureg.mole / ureg.kelvin
)

assert np.allclose(
mol.heat_capacity_ideal_gas(400), q(139.2, "J/mol/K"), atol=1
)
assert (
mol.heat_capacity_ideal_gas(400).units
== ureg.joule / ureg.mole / ureg.kelvin
)

assert np.allclose(
mol.heat_capacity_ideal_gas(800), q(206.8, "J/mol/K"), atol=1
)
assert (
mol.heat_capacity_ideal_gas(800).units
== ureg.joule / ureg.mole / ureg.kelvin
)

assert np.allclose(
mol.heat_capacity_ideal_gas(1000), q(224.6, "J/mol/K"), atol=1
)
assert (
mol.heat_capacity_ideal_gas(1000).units
== ureg.joule / ureg.mole / ureg.kelvin
)

assert np.allclose(
mol.vaporization_enthalpy, q(40.66, "kJ/mol"), atol=1e-2
)
assert mol.vaporization_enthalpy.units == ureg.kilojoule / ureg.mole

assert np.allclose(mol.fusion_enthalpy, q(13.3, "kJ/mol"), atol=1e-1)
assert mol.fusion_enthalpy.units == ureg.kilojoule / ureg.mole

assert np.allclose(
mol.viscosity_liquid(333.8), q(7.26e-4, "Pa s"), atol=1e-6
)
assert mol.viscosity_liquid(333.8).units == ureg.pascal * ureg.second

assert np.allclose(
mol.viscosity_liquid(374.4), q(4.92e-4, "Pa s"), atol=1e-6
)
assert mol.viscosity_liquid(374.4).units == ureg.pascal * ureg.second

assert np.allclose(
mol.viscosity_liquid(403.1), q(3.91e-4, "Pa s"), atol=1e-6
)
assert mol.viscosity_liquid(403.1).units == ureg.pascal * ureg.second

assert np.allclose(
mol.viscosity_liquid(423.3), q(3.40e-4, "Pa s"), atol=1e-6
)
assert mol.viscosity_liquid(423.3).units == ureg.pascal * ureg.second


@pytest.mark.joback
def test_p_dichlorobenzene_real_nbt():
mol = joback.get_groups(
"C1=CC(=CC=C1Cl)Cl", "smiles", normal_boiling_point=447
)
assert np.allclose(mol.critical_temperature, 681, atol=1)
assert np.allclose(mol.critical_temperature.magnitude, 681, atol=1)


@pytest.mark.joback
Expand All @@ -46,23 +115,36 @@ def test_acentric_factor():
"smiles",
normal_boiling_point=329.8,
)
assert np.allclose(mol.acentric_factor, 0.525, atol=1e-2)
assert np.allclose(mol.acentric_factor.magnitude, 0.525, atol=1e-2)
assert mol.acentric_factor.units == ureg.dimensionless


@pytest.mark.joback
def test_vapor_pressure():
mol = joback.get_groups(
"CC(C)O", identifier_type="smiles", normal_boiling_point=355.4
)
assert np.allclose(mol.vapor_pressure(450), 15.09, atol=2)
assert np.allclose(mol.vapor_pressure(450).magnitude, 15.09, atol=2)
assert mol.vapor_pressure(450).units == ureg.bar


@pytest.mark.joback
def test_liquid_heat_capacity():
mol = joback.get_groups("CC(=O)C", identifier_type="smiles")
assert np.allclose(28.0, mol.heat_capacity_liquid(180) * 0.239, atol=4e-1)
assert np.allclose(28.2, mol.heat_capacity_liquid(209) * 0.239, atol=1)
assert np.allclose(29.8, mol.heat_capacity_liquid(297) * 0.239, atol=3)
assert np.allclose(
28.0, mol.heat_capacity_liquid(180).magnitude * 0.239, atol=4e-1
)
assert (
mol.heat_capacity_liquid(180).units
== ureg.joule / ureg.mole / ureg.kelvin
)

assert np.allclose(
28.2, mol.heat_capacity_liquid(209).magnitude * 0.239, atol=1
)
assert np.allclose(
29.8, mol.heat_capacity_liquid(297).magnitude * 0.239, atol=3
)


@pytest.mark.joback
Expand All @@ -88,14 +170,14 @@ def test_rdkit_mol():
assert mol1.critical_volume == mol2.critical_volume
assert mol1.normal_boiling_point == mol2.normal_boiling_point
assert mol1.fusion_temperature == mol2.fusion_temperature
assert mol1.h_formation == mol2.h_formation
assert mol1.g_formation == mol2.g_formation
assert mol1.ig_enthalpy_formation == mol2.ig_enthalpy_formation
assert mol1.ig_gibbs_formation == mol2.ig_gibbs_formation
assert np.allclose(
mol1.heat_capacity_ideal_gas_params,
mol2.heat_capacity_ideal_gas_params,
)
assert mol1.h_fusion == mol2.h_fusion
assert mol1.h_vaporization == mol2.h_vaporization
assert mol1.fusion_enthalpy == mol2.fusion_enthalpy
assert mol1.vaporization_enthalpy == mol2.vaporization_enthalpy
assert mol1.sum_na == mol2.sum_na
assert mol1.sum_nb == mol2.sum_nb
assert mol1.molecular_weight == mol2.molecular_weight
Expand Down
3 changes: 2 additions & 1 deletion ugropy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
implemented.
"""

from .constants import ureg
from .core import instantiate_mol_object
from .core.frag_classes.abdulelah_gani.abdulelah_gani_pst import (
AbdulelahGaniPSTModel,
Expand Down Expand Up @@ -41,7 +42,7 @@
# =========================================================================
# Misc
# -------------------------------------------------------------------------
"constants",
"ureg",
"writers",
"instantiate_mol_object",
"Groups",
Expand Down
6 changes: 6 additions & 0 deletions ugropy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

from pathlib import Path

from pint import UnitRegistry


# Unit registry
ureg = UnitRegistry()


# constants.py path
_here = Path(__file__).parent
Expand Down
9 changes: 8 additions & 1 deletion ugropy/core/frag_classes/abdulelah_gani/abdulelah_gani.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ def __init__(
abdulelah_gani_p: AbdulelahGaniPSTModel,
abdulelah_gani_s: AbdulelahGaniPSTModel,
abdulelah_gani_t: AbdulelahGaniPSTModel,
properties_contributions: pd.DataFrame,
properties_biases: pd.DataFrame,
) -> None:

self.primary_model = abdulelah_gani_p
self.secondary_model = abdulelah_gani_s
self.tertiary_model = abdulelah_gani_t
self.properties_contributions = properties_contributions
self.properties_biases = properties_biases

def get_groups(
self,
Expand All @@ -47,11 +51,14 @@ def get_groups(
tertiary_groups = self.tertiary_model.get_groups(
identifier, identifier_type, solver, search_multiple_solutions
)

result = AGaniFragmentationResult(
primary_groups.molecule,
primary_groups,
secondary_groups,
tertiary_groups,
self.properties_contributions,
self.properties_biases,
)

return result
Loading

0 comments on commit 79351bc

Please sign in to comment.