Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformulate solubility_products constraints #1197

Merged
merged 23 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ def build_parameters(rblock, config):
mutable=True, initialize=1e-4, doc="Smoothing parameter for smooth maximum"
)

rblock.s_norm = Param(
mutable=True,
initialize=1e-4,
doc="Normalizing factor for solid precipitation term",
)
if hasattr(rblock, "k_eq_ref"):
rblock.s_norm.value = rblock.k_eq_ref.value
lxhowl marked this conversation as resolved.
Show resolved Hide resolved

rblock.s_scale = Param(
mutable=True,
initialize=1,
doc="Scaling factor for solid precipitation term w.r.t saturated status Q = Ksp - f(C)",
)

@staticmethod
def return_expression(b, rblock, r_idx, T):
e = None
Expand Down Expand Up @@ -181,6 +195,8 @@ def return_expression(b, rblock, r_idx, T):
if sunits is not None:
s = s / sunits

s = rblock.s_scale * s / (s + rblock.s_norm)

Q = b.k_eq[r_idx] - e

# Need to remove units again
Expand All @@ -189,7 +205,7 @@ def return_expression(b, rblock, r_idx, T):
if Qunits is not None:
Q = Q / Qunits

return s - smooth_max(0, s - Q, rblock.eps) == 0
return Q - smooth_max(0, Q - s, rblock.eps) == 0

@staticmethod
def calculate_scaling_factors(b, sf_keq):
Expand Down Expand Up @@ -234,6 +250,20 @@ def build_parameters(rblock, config):
mutable=True, initialize=1e-4, doc="Smoothing parameter for smooth maximum"
)

rblock.s_norm = Param(
mutable=True,
initialize=1e-4,
doc="Normalizing factor for solid precipitation term",
)
if hasattr(rblock, "k_eq_ref"):
rblock.s_norm.value = rblock.k_eq_ref.value
lxhowl marked this conversation as resolved.
Show resolved Hide resolved

rblock.s_scale = Param(
mutable=True,
initialize=10,
doc="Scaling factor for solid precipitation term w.r.t saturated status Q = ln(Ksp) - ln(f(C))",
)

@staticmethod
def return_expression(b, rblock, r_idx, T):
e = None
Expand Down Expand Up @@ -284,10 +314,12 @@ def return_expression(b, rblock, r_idx, T):
if sunits is not None:
s = s / sunits

s = rblock.s_scale * s / (s + rblock.s_norm)

Q = b.log_k_eq[r_idx] - e
# Q should be unitless due to log form

return s - smooth_max(0, s - Q, rblock.eps) == 0
return Q - smooth_max(0, Q - s, rblock.eps) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one or both Q and s need to be zero, could this just bee smooth min(Q, s) == 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current expression goes Q-0.5(Q-s+|Q-s|) == 0 while the smooth min gives 0.5(Q+s-|Q-s|)==0. The complexity remains the same. The current expression keeps Q as a leading term, which might be easy for users to scale the solubility product with other equilibrium reactions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eslickj I think this formulation is correct - it is a bit more complicated than just one of Q or s needing to be zero.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewlee94, I'm sure you are probably right. Can someone explain the difference to me? Why is this one preferred? I think both should work, but I may be missing something. To me, the the one in the code currently just seems convoluted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eslickj I cannot remember the details - I asked Larry Biegler to help with this is and this is the form he suggested.


@staticmethod
def calculate_scaling_factors(b, sf_keq):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,19 @@ def test_solubility_no_order():
m.thermo[1].flow_mol_phase_comp["sol", "c1"]
+ m.thermo[1].flow_mol_phase_comp["sol", "c2"]
) / (pyunits.mol / pyunits.s)
Q = m.rxn[1].k_eq["r1"] - (
m.thermo[1].mole_frac_phase_comp["p1", "c1"]
** m.rparams.reaction_r1.reaction_order["p1", "c1"]
* m.thermo[1].mole_frac_phase_comp["p1", "c2"]
** m.rparams.reaction_r1.reaction_order["p1", "c2"]
)
s = m.rparams.reaction_r1.s_scale * s / (s + m.rparams.reaction_r1.s_norm)

assert str(rform) == str(
s - smooth_max(0, s - Q / pyunits.dimensionless, m.rparams.reaction_r1.eps) == 0
)
Q = (
m.rxn[1].k_eq["r1"]
- (
m.thermo[1].mole_frac_phase_comp["p1", "c1"]
** m.rparams.reaction_r1.reaction_order["p1", "c1"]
* m.thermo[1].mole_frac_phase_comp["p1", "c2"]
** m.rparams.reaction_r1.reaction_order["p1", "c2"]
)
) / (pyunits.dimensionless)

assert str(rform) == str(Q - smooth_max(0, Q - s, m.rparams.reaction_r1.eps) == 0)


@pytest.mark.unit
Expand Down Expand Up @@ -523,24 +526,27 @@ def test_solubility_product_with_order():
m.thermo[1].flow_mol_phase_comp["sol", "c1"]
+ m.thermo[1].flow_mol_phase_comp["sol", "c2"]
) / (pyunits.mol / pyunits.s)
Q = m.rxn[1].k_eq["r1"] - (
m.thermo[1].mole_frac_phase_comp["p1", "c1"]
** m.rparams.reaction_r1.reaction_order["p1", "c1"]
* m.thermo[1].mole_frac_phase_comp["p1", "c2"]
** m.rparams.reaction_r1.reaction_order["p1", "c2"]
* m.thermo[1].mole_frac_phase_comp["p2", "c1"]
** m.rparams.reaction_r1.reaction_order["p2", "c1"]
* m.thermo[1].mole_frac_phase_comp["p2", "c2"]
** m.rparams.reaction_r1.reaction_order["p2", "c2"]
* m.thermo[1].mole_frac_phase_comp["sol", "c1"]
** m.rparams.reaction_r1.reaction_order["sol", "c1"]
* m.thermo[1].mole_frac_phase_comp["sol", "c2"]
** m.rparams.reaction_r1.reaction_order["sol", "c2"]
)
s = m.rparams.reaction_r1.s_scale * s / (s + m.rparams.reaction_r1.s_norm)

assert str(rform) == str(
s - smooth_max(0, s - Q / pyunits.dimensionless, m.rparams.reaction_r1.eps) == 0
)
Q = (
m.rxn[1].k_eq["r1"]
- (
m.thermo[1].mole_frac_phase_comp["p1", "c1"]
** m.rparams.reaction_r1.reaction_order["p1", "c1"]
* m.thermo[1].mole_frac_phase_comp["p1", "c2"]
** m.rparams.reaction_r1.reaction_order["p1", "c2"]
* m.thermo[1].mole_frac_phase_comp["p2", "c1"]
** m.rparams.reaction_r1.reaction_order["p2", "c1"]
* m.thermo[1].mole_frac_phase_comp["p2", "c2"]
** m.rparams.reaction_r1.reaction_order["p2", "c2"]
* m.thermo[1].mole_frac_phase_comp["sol", "c1"]
** m.rparams.reaction_r1.reaction_order["sol", "c1"]
* m.thermo[1].mole_frac_phase_comp["sol", "c2"]
** m.rparams.reaction_r1.reaction_order["sol", "c2"]
)
) / (pyunits.dimensionless)

assert str(rform) == str(Q - smooth_max(0, Q - s, m.rparams.reaction_r1.eps) == 0)


@pytest.mark.unit
Expand Down Expand Up @@ -667,14 +673,16 @@ def test_log_solubility_no_order():
m.thermo[1].flow_mol_phase_comp["sol", "c1"]
+ m.thermo[1].flow_mol_phase_comp["sol", "c2"]
) / (pyunits.mol / pyunits.s)
s = m.rparams.reaction_r1.s_scale * s / (s + m.rparams.reaction_r1.s_norm)

Q = m.rxn[1].log_k_eq["r1"] - (
m.thermo[1].log_mole_frac_phase_comp["p1", "c1"]
* m.rparams.reaction_r1.reaction_order["p1", "c1"]
+ m.thermo[1].log_mole_frac_phase_comp["p1", "c2"]
* m.rparams.reaction_r1.reaction_order["p1", "c2"]
)

assert str(rform) == str(s - smooth_max(0, s - Q, m.rparams.reaction_r1.eps) == 0)
assert str(rform) == str(Q - smooth_max(0, Q - s, m.rparams.reaction_r1.eps) == 0)


@pytest.mark.unit
Expand Down Expand Up @@ -757,6 +765,8 @@ def test_log_solubility_product_with_order():
m.thermo[1].flow_mol_phase_comp["sol", "c1"]
+ m.thermo[1].flow_mol_phase_comp["sol", "c2"]
) / (pyunits.mol / pyunits.s)
s = m.rparams.reaction_r1.s_scale * s / (s + m.rparams.reaction_r1.s_norm)

Q = m.rxn[1].log_k_eq["r1"] - (
m.thermo[1].log_mole_frac_phase_comp["p1", "c1"]
* m.rparams.reaction_r1.reaction_order["p1", "c1"]
Expand All @@ -772,7 +782,7 @@ def test_log_solubility_product_with_order():
* m.rparams.reaction_r1.reaction_order["sol", "c2"]
)

assert str(rform) == str(s - smooth_max(0, s - Q, m.rparams.reaction_r1.eps) == 0)
assert str(rform) == str(Q - smooth_max(0, Q - s, m.rparams.reaction_r1.eps) == 0)


@pytest.mark.unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@


solver = get_solver()
solver.options["tol"] = 1e-8
lxhowl marked this conversation as resolved.
Show resolved Hide resolved


def dummy_h(b, *args, **kwargs):
Expand Down Expand Up @@ -283,10 +284,10 @@ def test_saturated(self, model):
assert pytest.approx(0, abs=1.1e-3) == value(
model.fs.R101.outlet.flow_mol_phase_comp[0, "Sol", "NaCl"]
)
assert pytest.approx(6.158968, rel=1e-5) == value(
assert pytest.approx(6.159876, rel=1e-5) == value(
model.fs.R101.outlet.flow_mol_phase_comp[0, "Liq", "Na+"]
)
assert pytest.approx(6.158968, rel=1e-5) == value(
assert pytest.approx(6.159876, rel=1e-5) == value(
model.fs.R101.outlet.flow_mol_phase_comp[0, "Liq", "Cl-"]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def define_state(b):
)
b.mole_frac_comp = Var(
b.component_list,
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
initialize=1 / len(b.component_list),
doc="Mixture mole fractions",
units=pyunits.dimensionless,
Expand Down Expand Up @@ -134,7 +134,7 @@ def define_state(b):
b.mole_frac_phase_comp = Var(
b.phase_component_set,
initialize=1 / len(b.component_list),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
doc="Phase mole fractions",
units=pyunits.dimensionless,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def define_state(b):
)
b.mole_frac_comp = Var(
b.component_list,
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
initialize=1 / len(b.component_list),
doc="Mixture mole fractions",
units=pyunits.dimensionless,
Expand Down Expand Up @@ -140,7 +140,7 @@ def define_state(b):
b.mole_frac_phase_comp = Var(
b.phase_component_set,
initialize=1 / len(b.component_list),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
doc="Phase mole fractions",
units=pyunits.dimensionless,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def define_state(b):

b.mole_frac_comp = Var(
b.component_list,
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
initialize=1 / len(b.component_list),
doc="Mixture mole fractions",
units=pyunits.dimensionless,
Expand All @@ -144,7 +144,7 @@ def define_state(b):
b.mole_frac_phase_comp = Var(
b.phase_component_set,
initialize=1 / len(b.component_list),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
doc="Phase mole fractions",
units=pyunits.dimensionless,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def define_state(b):

b.mole_frac_comp = Var(
b.component_list,
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
initialize=1 / len(b.component_list),
doc="Mixture mole fractions",
units=pyunits.dimensionless,
Expand All @@ -132,7 +132,7 @@ def define_state(b):
b.mole_frac_phase_comp = Var(
b.phase_component_set,
initialize=1 / len(b.component_list),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
doc="Phase mole fractions",
units=pyunits.dimensionless,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def mole_frac_comp(b, j):

b.mole_frac_phase_comp = Var(
b.phase_component_set,
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
initialize=1 / len(b.component_list),
doc="Phase mole fractions",
units=pyunits.dimensionless,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _apparent_species_state(b):
b.mole_frac_phase_comp_true = Var(
b.params.true_phase_component_set,
initialize=1 / len(b.params.true_species_set),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
lxhowl marked this conversation as resolved.
Show resolved Hide resolved
doc="Phase-component molar fractions of true species",
units=pyunits.dimensionless,
)
Expand Down Expand Up @@ -192,7 +192,7 @@ def _true_species_state(b):
b.mole_frac_phase_comp_apparent = Var(
b.params.apparent_phase_component_set,
initialize=1 / len(b.params.apparent_species_set),
bounds=(1e-20, 1.001),
bounds=(0, 1.001),
doc="Phase-component molar fractions of apparent species",
units=pyunits.dimensionless,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def phe(self):
@pytest.mark.build
@pytest.mark.unit
def test_build(self, phe):

assert hasattr(phe.fs.unit, "hot_side_inlet")
assert len(phe.fs.unit.hot_side_inlet.vars) == 4
assert hasattr(phe.fs.unit.hot_side_inlet, "flow_mol")
Expand Down Expand Up @@ -163,7 +162,8 @@ def test_dof(self, phe):
@pytest.mark.component
def test_initialize(self, phe):
initialization_tester(
phe, duty=(245000, pyunits.W), optarg={"bound_push": 1e-8, "mu_init": 1e-8}
phe,
duty=(245000, pyunits.W), # , optarg={"bound_push": 1e-8, "mu_init": 1e-8}
lxhowl marked this conversation as resolved.
Show resolved Hide resolved
)

@pytest.mark.solver
Expand Down