Skip to content

Commit 3b37b05

Browse files
committed
Formatting
1 parent 596667e commit 3b37b05

File tree

4 files changed

+88
-67
lines changed

4 files changed

+88
-67
lines changed

CADETPythonSimulator/unit_operation.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def initialize(self) -> NoReturn:
9494

9595
@property
9696
def states(self) -> dict[str, State]:
97-
"""dict: State array blocks of the unit operation, indexed by name."""
97+
"""dict: State array block of the unit operation, indexed by name."""
9898
if self._states is None:
9999
raise NotInitializedError("Unit operation state is not yet initialized.")
100100

@@ -115,7 +115,7 @@ def y(self, y: np.ndarray) -> NoReturn:
115115

116116
@property
117117
def state_derivatives(self) -> dict[str, State]:
118-
"""dict: State derivative array blocks of the unit operation, indexed by name."""
118+
"""dict: State derivative array block of the unit operation, indexed by name."""
119119
if self._state_derivatives is None:
120120
raise NotInitializedError("Unit operation state is not yet initialized.")
121121

@@ -327,7 +327,7 @@ def get_outlet_state(
327327
def get_outlet_state_flat(
328328
self,
329329
unit_port_index: int
330-
) -> NoReturn:
330+
) -> dict[str, np.ndarray]:
331331
"""
332332
Get the state of the unit operation outlet for a given port.
333333
@@ -573,14 +573,16 @@ def compute_residual(
573573
Q_in = self.Q_in[0]
574574
Q_out = self.Q_out[0]
575575

576-
# for i in range(self.n_comp):
577-
# self.residuals['bulk']['c'][i] = c_dot[i] * V + V_dot * c[i] - Q_in * c_in[i] + Q_out * c[i]
578-
# Alternative: Can we vectorize this?
579-
self.residuals['bulk']['c'] = calculate_residual_concentration_cstr(c, c_dot, V, V_dot, Q_in, Q_out, c_in)
580576

581-
self.residuals['bulk']['Volume'] = calculate_residual_volume_cstr(V, V_dot, Q_in, Q_out)
577+
self.residuals['bulk']['c'] = calculate_residual_concentration_cstr(
578+
c, c_dot, V, V_dot, Q_in, Q_out, c_in
579+
)
580+
581+
self.residuals['bulk']['Volume'] = calculate_residual_volume_cstr(
582+
V, V_dot, Q_in, Q_out
583+
)
582584

583-
self.residuals['inlet']['viscosity'] = calculate_residuals_visc_cstr()
585+
self.residuals['inlet']['viscosity'] = calculate_residual_visc_cstr()
584586

585587
class DeadEndFiltration(UnitOperationBase):
586588
"""

CADETPythonSimulator/viscosity.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class ViscosityBase(Structure):
99
"""Base class for mixed viscosity calculations."""
1010

1111
@abstractmethod
12-
def get_mixture_viscosity(self, viscosities: np.ndarray, fractions: np.ndarray) -> float:
12+
def get_mixture_viscosity(
13+
self, viscosities: np.ndarray, fractions: np.ndarray
14+
) -> float:
1315
"""Calculate mixed viscosity with given viscosities and volume fractions.
1416
1517
Parameters
@@ -44,7 +46,9 @@ def _validate_viscosities_input(
4446
class AverageViscosity(ViscosityBase):
4547
"""Calculate mixed viscosity using the average mean."""
4648

47-
def get_mixture_viscosity(self, viscosities: np.ndarray, fractions: np.ndarray) -> float:
49+
def get_mixture_viscosity(
50+
self, viscosities: np.ndarray, fractions: np.ndarray
51+
) -> float:
4852
"""Calculate mixed viscosity using the arithmetic mean.
4953
5054
Parameters
@@ -68,7 +72,9 @@ def get_mixture_viscosity(self, viscosities: np.ndarray, fractions: np.ndarray)
6872
class LogarithmicMixingViscosity(ViscosityBase):
6973
"""Calculate mixed viscosity using the logarithmic mixing rule."""
7074

71-
def get_mixture_viscosity(self, viscosities: np.ndarray, fractions: np.ndarray) -> float:
75+
def get_mixture_viscosity(
76+
self, viscosities: np.ndarray, fractions: np.ndarray
77+
) -> float:
7278
"""Calculate mixed viscosity using the logarithmic mixing rule.
7379
7480
Parameters

tests/test_residual.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from CADETPythonSimulator.exception import CADETPythonSimError
1111

1212

13-
# random number test
14-
TestCaseConc_level1 = {
13+
# Arbitrary parameter values
14+
TestCaseCSTRConc_level1 = {
1515
"values": {
1616
"c": np.array([1, 2, 3]),
1717
"c_dot": np.array([4, 5, 6]),
@@ -24,8 +24,8 @@
2424
"expected": np.array([-11, -7, -3])
2525
}
2626

27-
# flow in and out are equal, concentrations to
28-
TestCaseConc_equal = {
27+
# Flow in and out are equal, concentrations are equal
28+
TestCaseCSTRConc_equal = {
2929
"values": {
3030
"c": np.array([0.1,]),
3131
"c_dot": np.array([0,]),
@@ -38,8 +38,8 @@
3838
"expected": np.array([0,])
3939
}
4040

41-
# flow in and out are equal, but concentrations going into the unit are not
42-
TestCaseConc_diffcin = {
41+
# Flow in and out are equal, concentrations differ
42+
TestCaseCSTRConc_diffcin = {
4343
"values": {
4444
"c": np.array([0.1,]),
4545
"c_dot": np.array([0,]),
@@ -52,8 +52,8 @@
5252
"expected": np.array([-0.1,])
5353
}
5454

55-
# flow in and out are not equal, concentrantions going in are
56-
TestCaseConc_diffvol = {
55+
# Flow in and out differ, concentrations are equal
56+
TestCaseCSTRConc_diffvol = {
5757
"values": {
5858
"c": np.array([0.1,]),
5959
"c_dot": np.array([0,]),
@@ -66,8 +66,8 @@
6666
"expected": np.array([0,])
6767
}
6868

69-
# flow in and out are not, equal, concentrations aren't equal too
70-
TestCaseConc_diffvolc = {
69+
# Flow in and out differ, concentrations differ
70+
TestCaseCSTRConc_diffvolc = {
7171
"values": {
7272
"c": np.array([0.1,]),
7373
"c_dot": np.array([0.2,]),
@@ -84,24 +84,25 @@
8484
@pytest.mark.parametrize(
8585
"parameters",
8686
[
87-
TestCaseConc_level1,
88-
TestCaseConc_equal,
89-
TestCaseConc_diffcin,
90-
TestCaseConc_diffvol,
91-
TestCaseConc_diffvolc
87+
TestCaseCSTRConc_level1,
88+
TestCaseCSTRConc_equal,
89+
TestCaseCSTRConc_diffcin,
90+
TestCaseCSTRConc_diffvol,
91+
TestCaseCSTRConc_diffvolc
9292
]
9393
)
9494
class TestResidualConcCSTR():
9595
def test_calculation_concentration_cstr(self, parameters):
9696

9797
param_vec_conc = parameters["values"].values()
9898

99-
residual = calculate_residual_concentration_cstr(*param_vec_conc)
100-
101-
np.testing.assert_array_almost_equal(residual, parameters["expected"])
99+
np.testing.assert_array_almost_equal(
100+
calculate_residual_concentration_cstr(*param_vec_conc),
101+
parameters["expected"]
102+
)
102103

103104

104-
# random number test
105+
# Arbitrary parameter values
105106
TestCaseVol = {
106107
"values": {
107108
"V": 1,
@@ -123,7 +124,7 @@ def test_calculation_concentration_cstr(self, parameters):
123124
"expected": 0
124125
}
125126

126-
# Flow in is larger than out
127+
# Flow in is larger than flow out
127128
TestCaseVol_inge = {
128129
"values": {
129130
"V": 1,
@@ -134,7 +135,7 @@ def test_calculation_concentration_cstr(self, parameters):
134135
"expected": 0
135136
}
136137

137-
# Flow in is lesser than out
138+
# Flow in is sameller than flow out
138139
TestCaseVol_inle = {
139140
"values": {
140141
"V": 1,
@@ -145,11 +146,10 @@ def test_calculation_concentration_cstr(self, parameters):
145146
"expected": 0
146147
}
147148

148-
# Residual does not depend on Volumne
149-
149+
# Residual does not depend on volume
150150
TestCaseVol_vol = {
151151
"values": {
152-
"V": 1e10,
152+
"V": 1,
153153
"V_dot": 0,
154154
"Q_in": 0,
155155
"Q_out": 0,
@@ -170,10 +170,10 @@ def test_calculation_concentration_cstr(self, parameters):
170170
)
171171
class TestResidualVolCSTR():
172172
def test_calculation_cstr(self, parameters):
173-
174173
param_vec_volume = parameters["values"].values()
175-
176174
residual = calculate_residual_volume_cstr(*param_vec_volume)
175+
np.testing.assert_equal(residual, parameters["expected"])
176+
177177

178178
# Testcase 1: Membrane rejects all
179179
TestCaseDEFCake_rejects_all = {

tests/test_unit_operation.py

+42-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from CADETPythonSimulator.rejection import StepCutOff
1717

18+
1819
# %% Unit Operation Fixtures
1920
class TwoComponentFixture(CPSComponentSystem):
2021
def __init__(self, *args, **kwargs):
@@ -71,7 +72,6 @@ def __init__(self, component_system=None, name='cstr', *args, **kwargs):
7172
super().__init__(component_system, name, *args, **kwargs)
7273

7374

74-
7575
class DeadEndFiltrationFixture(UnitOperationFixture, DeadEndFiltration):
7676
def __init__(self,
7777
component_system=None,
@@ -116,7 +116,6 @@ def __init__(self,
116116
super().__init__(component_system, name, *args, **kwargs)
117117

118118

119-
120119
# %% Unit Operation State Structure
121120

122121
@pytest.mark.parametrize(
@@ -360,41 +359,51 @@ def test_initialize(self, unit_operation: UnitOperationBase, expected: dict):
360359
(
361360
CstrFixture(),
362361
{
363-
'states' : {
364-
'inlet' : {
365-
'c' : np.array([7, 8]),
366-
'viscosity' : [3]
362+
'states': {
363+
'inlet': {
364+
'c': np.array([7, 8]),
365+
'viscosity': [3]
367366
},
368-
'bulk' : {
369-
'c' : np.array([1, 2]),
370-
'Volume' : 1
367+
'bulk': {
368+
'c': np.array([1, 2]),
369+
'Volume': 1
371370
}
372371
},
373-
'state_derivatives' : {
374-
'inlet' : {
375-
'c' : [6, 7]
372+
'state_derivatives': {
373+
'inlet': {
374+
'c': [6, 7]
376375
},
377-
'bulk' : {
378-
'c' : np.array([4, 5]),
379-
'Volume' : 2
376+
'bulk': {
377+
'c': np.array([4, 5]),
378+
'Volume': 2
380379
}
381380
},
382-
'Q_in' : [3],
383-
'Q_out' : [4]
381+
'Q_in': [3],
382+
'Q_out': [4]
384383
},
385384
[
386-
("calculate_residual_concentration_cstr", lambda c, c_dot, V, V_dot, Q_in, Q_out, c_in: c_dot * V + V_dot * c - Q_in * c_in + Q_out * c),
387-
("calculate_residuals_visc_cstr", lambda *args : 0),
388-
("calculate_residual_volume_cstr", lambda V, V_dot, Q_in, Q_out: V_dot - Q_in + Q_out)
385+
(
386+
"calculate_residual_concentration_cstr",
387+
lambda c, c_dot, V, V_dot, Q_in, Q_out, c_in:
388+
c_dot * V + V_dot * c - Q_in * c_in + Q_out * c
389+
),
390+
(
391+
"calculate_residual_visc_cstr",
392+
lambda *args: 0
393+
),
394+
(
395+
"calculate_residual_volume_cstr",
396+
lambda V, V_dot, Q_in, Q_out: V_dot - Q_in + Q_out
397+
)
389398
],
390399
{
391-
'inlet' : {
392-
'c' : np.array([7, 8]),
393-
'viscosity' : 0
400+
'inlet': {
401+
'c': np.array([7, 8]),
402+
'viscosity': 0
394403
},
395-
'bulk' : {
396-
'c' : np.array([-11,-7]),
397-
'Volume' : 3
404+
'bulk': {
405+
'c': np.array([-11, -7]),
406+
'Volume': 3
398407
}
399408
}
400409
),
@@ -480,7 +489,7 @@ def test_unit_residual(
480489
"""Test the residual of unit operations."""
481490

482491
for funcname, func in residualfunc:
483-
monkeypatch.setattr('CADETPythonSimulator.unit_operation.'+funcname, func )
492+
monkeypatch.setattr('CADETPythonSimulator.unit_operation.'+funcname, func)
484493

485494
for key, value in case['states'].items():
486495
unit_operation.states[key] = value
@@ -495,9 +504,13 @@ def test_unit_residual(
495504

496505
for unit_module, module_dict in expected.items():
497506
for property, value in module_dict.items():
498-
np.testing.assert_equal(value, unit_operation.residuals[unit_module][property])
507+
np.testing.assert_equal(
508+
value,
509+
unit_operation.residuals[unit_module][property]
510+
)
511+
499512

500513
# %% Run tests
501514

502515
if __name__ == "__main__":
503-
pytest.main(["test_unit_operation.py"])
516+
pytest.main(["test_unit_operation.py"])

0 commit comments

Comments
 (0)