Skip to content

Commit

Permalink
Add density to ComponentSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
schmoelder committed Dec 6, 2024
1 parent e97d5b6 commit a7ec502
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
52 changes: 47 additions & 5 deletions CADETProcess/processModel/componentSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class Species(Structure):
The charge of the species. Default is 0.
molecular_weight : float
The molecular weight of the species.
density : float
Density of the species.
"""
name = String()
charge = Integer(default=0)
molecular_weight = UnsignedFloat()
density = UnsignedFloat()


class Component(Structure):
Expand All @@ -50,6 +53,8 @@ class Component(Structure):
Charge of component (including species).
molecular_weight : list
Molecular weight of component (including species).
density : list
Density of component (including species).
See Also
--------
Expand All @@ -60,7 +65,13 @@ class Component(Structure):
name = String()

def __init__(
self, name=None, species=None, charge=None, molecular_weight=None):
self,
name=None,
species=None,
charge=None,
molecular_weight=None,
density=None
):
"""
Parameters
----------
Expand All @@ -72,21 +83,25 @@ def __init__(
Charges of the subspecies.
molecular_weight : float or list of float or None, optional
Molecular weights of the subspecies.
density : list
Density of component (including species).
"""
self.name = name
self._species = []

if species is None:
self.add_species(name, charge, molecular_weight)
self.add_species(name, charge, molecular_weight, density)
elif isinstance(species, str):
self.add_species(species, charge, molecular_weight)
self.add_species(species, charge, molecular_weight, density)
elif isinstance(species, list):
if charge is None:
charge = len(species) * [None]
if molecular_weight is None:
molecular_weight = len(species) * [None]
if density is None:
density = len(species) * [None]
for i, spec in enumerate(species):
self.add_species(spec, charge[i], molecular_weight[i])
self.add_species(spec, charge[i], molecular_weight[i], density[i])
else:
raise CADETProcessError("Could not determine number of species")

Expand Down Expand Up @@ -136,6 +151,11 @@ def molecular_weight(self):
"""list of float or None: The molecular weights of the subspecies."""
return [spec.molecular_weight for spec in self.species]

@property
def density(self):
"""list of float or None: The density of the subspecies."""
return [spec.density for spec in self.species]

def __str__(self):
"""String representation of the component."""
return self.name
Expand Down Expand Up @@ -172,6 +192,8 @@ class ComponentSystem(Structure):
Charges of all components species.
molecular_weights : list
Molecular weights of all component species.
densities : list
Densities of all component species.
See Also
--------
Expand All @@ -182,7 +204,13 @@ class ComponentSystem(Structure):
name = String()

def __init__(
self, components=None, name=None, charges=None, molecular_weights=None):
self,
components=None,
name=None,
charges=None,
molecular_weights=None,
densities=None
):
"""Initialize the ComponentSystem object.
Parameters
Expand All @@ -196,6 +224,8 @@ def __init__(
The charges of each component.
molecular_weights : list, None
The molecular weights of each component.
densities : list, None
The densities of each component.
Raises
------
Expand All @@ -222,12 +252,15 @@ def __init__(
charges = n_comp * [None]
if molecular_weights is None:
molecular_weights = n_comp * [None]
if densities is None:
densities = n_comp * [None]

for i, comp in enumerate(components):
self.add_component(
comp,
charge=charges[i],
molecular_weight=molecular_weights[i],
density=densities[i]
)

@property
Expand Down Expand Up @@ -379,6 +412,15 @@ def molecular_weights(self):

return molecular_weights

@property
def densities(self):
"""list: List of species densities."""
densities = []
for comp in self.components:
densities += comp.density

return densities

def __repr__(self):
return f'{self.__class__.__name__}({self.names})'

Expand Down
11 changes: 11 additions & 0 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def setUp(self):
molecular_weight=[1, 0]
)

self.component_system_6 = ComponentSystem()
self.component_system_6.add_component(
'A',
species=['A+', 'A-'],
density=[1, 0]
)

def test_names(self):
names_expected = ['0', '1']
names = self.component_system_0.names
Expand Down Expand Up @@ -126,6 +133,10 @@ def test_molecular_weights(self):
molecular_weights = self.component_system_5.molecular_weights
np.testing.assert_equal(molecular_weights_expected, molecular_weights)

def test_densities(self):
densities_expected = [1, 0]
densities = self.component_system_6.densities
np.testing.assert_equal(densities_expected, densities)

if __name__ == '__main__':
unittest.main()

0 comments on commit a7ec502

Please sign in to comment.