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

Apply and enforce Black formatting #134

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# PR #134 (Apply and enforce Black formatting)
6561c4fc1968ef38bdd6221f985f421fe024c21f
19 changes: 18 additions & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ defaults:
shell: bash -l {0}

jobs:

code-formatting:
name: Check code formatting (Black)
# OS and/or Python version don't make a difference, so we choose ubuntu and 3.10 for performance
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.DEFAULT_PYTHON_VERSION }}
- name: Install Black
run: |
pip install --progress-bar off black[jupyter]==24.3.0
- name: Run Black to verify that the committed code is formatted
run: |
black --check .

spell-check:
name: Check Spelling
runs-on: ubuntu-latest
Expand All @@ -57,7 +74,7 @@ jobs:
# description: Run pytest with dev dependencies
name: pytest (py${{ matrix.python-version }}/${{ matrix.os }})
runs-on: ${{ matrix.runner-image }}
needs: [spell-check]
needs: [code-formatting, spell-check]
strategy:
fail-fast: false
matrix:
Expand Down
188 changes: 107 additions & 81 deletions idaes_examples/archive/data_reconciliation/boiler_data_rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@
from idaes.core.util.model_statistics import degrees_of_freedom
import idaes.core.util.scaling as iscale


def deactivate_performance_constraints(m):
for blk in m.fs.component_objects(pyo.Block, descend_into=False):
try:
Uval = value(blk.overall_heat_transfer_coefficient[0])
Dps = value(blk.deltaP_shell[0])
Dpt = value(blk.deltaP_tube[0])
blk.overall_heat_transfer_coefficient[0].setlb(Uval*0.25)
blk.overall_heat_transfer_coefficient[0].setub(Uval*2.5)
blk.deltaP_shell.setlb(Dps*10)
blk.overall_heat_transfer_coefficient[0].setlb(Uval * 0.25)
blk.overall_heat_transfer_coefficient[0].setub(Uval * 2.5)
blk.deltaP_shell.setlb(Dps * 10)
blk.deltaP_shell.setub(0)
blk.deltaP_tube.setlb(Dpt*10)
blk.deltaP_tube.setlb(Dpt * 10)
blk.deltaP_tube.setub(0)

blk.overall_heat_transfer_coefficient_eqn.deactivate()
Expand Down Expand Up @@ -98,15 +99,15 @@ def deactivate_performance_constraints(m):
blk.gas_emissivity_eqn.deactivate()
except AttributeError:
pass
print('degrees of freedom = ' + str(degrees_of_freedom(m)))
print("degrees of freedom = " + str(degrees_of_freedom(m)))


def boiler_flowsheet():
"""
Make the flowsheet object, fix some variables, and solve the problem
"""
import idaes.models_extra.power_generation.flowsheets.\
supercritical_power_plant.boiler_subflowsheet_build as blr
import idaes.models_extra.power_generation.flowsheets.supercritical_power_plant.boiler_subflowsheet_build as blr

# First, we import the boler model from boiler_subflowsheet_build
m, solver = blr.main()
# initialize boiler flowsheet
Expand All @@ -123,92 +124,121 @@ def boiler_flowsheet():
# Therefore, in the new model, these variables will change with plant load,
# More details regarding this methodology can be found in Zamarripa et al.,
# FOCAPD 2 page paper included in this directory as FOCAPD_paper.pdf
m.fs.SR = pyo.Var(initialize=1.15, doc='stoichiometric ratio')
m.fs.SR = pyo.Var(initialize=1.15, doc="stoichiometric ratio")
m.fs.SR.setlb(1.0)
m.fs.SR.setub(1.25)
m.fs.SR.fix(1.15)
m.fs.coal_flow = pyo.Var(initialize=54.01, doc='coal flowrate kg.s')
m.fs.FGET = pyo.Var(initialize=1300.00, doc='flue gas exit temperature K')
m.fs.FG_flow = pyo.Var(m.fs.prop_fluegas.component_list,
initialize=2000,
domain=pyo.Reals, doc='component molar flow rate')
init_fg = {"H2O": (8.69/100), "CO2": (14.49/100),
"N2": (0.6999), "O2": (2.47/100), "NO": 0.0006, "SO2": (0.002)}
m.fs.fg_frac = pyo.Var(m.fs.prop_fluegas.component_list,
initialize=init_fg,
doc='flue gas molar fraction')
m.fs.coal_flow = pyo.Var(initialize=54.01, doc="coal flowrate kg.s")
m.fs.FGET = pyo.Var(initialize=1300.00, doc="flue gas exit temperature K")
m.fs.FG_flow = pyo.Var(
m.fs.prop_fluegas.component_list,
initialize=2000,
domain=pyo.Reals,
doc="component molar flow rate",
)
init_fg = {
"H2O": (8.69 / 100),
"CO2": (14.49 / 100),
"N2": (0.6999),
"O2": (2.47 / 100),
"NO": 0.0006,
"SO2": (0.002),
}
m.fs.fg_frac = pyo.Var(
m.fs.prop_fluegas.component_list,
initialize=init_fg,
doc="flue gas molar fraction",
)

# surrogate models valid for coal flowrate (30 to 70 kg/s) and SR (1-2.5)
def fg_mol_frac_rule(b, i):
if i == 'CO2':
return m.fs.fg_frac[i] == 0.23235491508307534735955\
* m.fs.SR**-0.5
elif i == 'H2O':
return m.fs.fg_frac[i] == 0.45009703293861530459807E-001 \
* m.fs.SR
elif i == 'N2':
return m.fs.fg_frac[i] == 1 - (m.fs.fg_frac['CO2']
+ m.fs.fg_frac['H2O']
+ m.fs.fg_frac['NO']
+ m.fs.fg_frac['O2']
+ m.fs.fg_frac['SO2'])
elif i == 'NO':
return m.fs.fg_frac[i] == 0.38148020722713599076938E-005 \
* m.fs.coal_flow
elif i == 'O2':
return m.fs.fg_frac[i] == 0.60149266916011525155317E-003 \
* m.fs.coal_flow
elif i == 'SO2':
return m.fs.fg_frac[i] == 0.21991717807021016347323E-004 \
* m.fs.coal_flow
m.fs.fg_mol_cn = pyo.Constraint(m.fs.prop_fluegas.component_list,
rule=fg_mol_frac_rule)
if i == "CO2":
return m.fs.fg_frac[i] == 0.23235491508307534735955 * m.fs.SR**-0.5
elif i == "H2O":
return m.fs.fg_frac[i] == 0.45009703293861530459807e-001 * m.fs.SR
elif i == "N2":
return m.fs.fg_frac[i] == 1 - (
m.fs.fg_frac["CO2"]
+ m.fs.fg_frac["H2O"]
+ m.fs.fg_frac["NO"]
+ m.fs.fg_frac["O2"]
+ m.fs.fg_frac["SO2"]
)
elif i == "NO":
return m.fs.fg_frac[i] == 0.38148020722713599076938e-005 * m.fs.coal_flow
elif i == "O2":
return m.fs.fg_frac[i] == 0.60149266916011525155317e-003 * m.fs.coal_flow
elif i == "SO2":
return m.fs.fg_frac[i] == 0.21991717807021016347323e-004 * m.fs.coal_flow

m.fs.fg_mol_cn = pyo.Constraint(
m.fs.prop_fluegas.component_list, rule=fg_mol_frac_rule
)

def fg_flow_rule(b, i):
mass_flow = 0.87757407893140026988732 * m.fs.coal_flow \
- 0.68240933480416252066014E-001 * m.fs.SR + 8.6386912890637752582\
* m.fs.coal_flow*m.fs.SR - 0.11737247790640543564002E-003 \
* (m.fs.coal_flow/m.fs.SR)**2 # ~ 28.3876e3 kg/s
mass_flow = (
0.87757407893140026988732 * m.fs.coal_flow
- 0.68240933480416252066014e-001 * m.fs.SR
+ 8.6386912890637752582 * m.fs.coal_flow * m.fs.SR
- 0.11737247790640543564002e-003 * (m.fs.coal_flow / m.fs.SR) ** 2
) # ~ 28.3876e3 kg/s

# flow mol component in mol/s = kg/s/kg/mol
return m.fs.FSH.hot_side.properties_in[0].flow_mol_comp[i] == \
(mass_flow / sum(m.fs.fg_frac[c]*m.fs.prop_fluegas.mw_comp[c]
for c in m.fs.prop_fluegas.component_list))\
return (
m.fs.FSH.hot_side.properties_in[0].flow_mol_comp[i]
== (
mass_flow
/ sum(
m.fs.fg_frac[c] * m.fs.prop_fluegas.mw_comp[c]
for c in m.fs.prop_fluegas.component_list
)
)
* m.fs.fg_frac[i]
m.fs.fg_flow_cn = pyo.Constraint(m.fs.prop_fluegas.component_list,
rule=fg_flow_rule)
)

m.fs.fg_flow_cn = pyo.Constraint(
m.fs.prop_fluegas.component_list, rule=fg_flow_rule
)

def fg_temp_rule(b):
return m.fs.FSH.hot_side.properties_in[0].temperature == \
59836.381548557488713413 * m.fs.coal_flow**-0.5 \
+ 791.74814907302368283126 * m.fs.coal_flow**0.5 \
+ 0.60200443235342349090899 * m.fs.coal_flow**1.5 \
+ 285.74858049626226375040 * m.fs.SR**3 - 29865.27413896147845662\
* (m.fs.coal_flow*m.fs.SR)**-.5 - 518.58090213915738786454 \
* (m.fs.coal_flow*m.fs.SR)**0.5 - 0.86351166781748790735040E-002 \
* (m.fs.coal_flow*m.fs.SR)**2 - 30141.308801694875000976 \
* (m.fs.SR/m.fs.coal_flow)**0.5 - 18.109911868067683826666 \
* m.fs.coal_flow/m.fs.SR + 1017.0807559525446777116 \
* m.fs.SR/m.fs.coal_flow
return (
m.fs.FSH.hot_side.properties_in[0].temperature
== 59836.381548557488713413 * m.fs.coal_flow**-0.5
+ 791.74814907302368283126 * m.fs.coal_flow**0.5
+ 0.60200443235342349090899 * m.fs.coal_flow**1.5
+ 285.74858049626226375040 * m.fs.SR**3
- 29865.27413896147845662 * (m.fs.coal_flow * m.fs.SR) ** -0.5
- 518.58090213915738786454 * (m.fs.coal_flow * m.fs.SR) ** 0.5
- 0.86351166781748790735040e-002 * (m.fs.coal_flow * m.fs.SR) ** 2
- 30141.308801694875000976 * (m.fs.SR / m.fs.coal_flow) ** 0.5
- 18.109911868067683826666 * m.fs.coal_flow / m.fs.SR
+ 1017.0807559525446777116 * m.fs.SR / m.fs.coal_flow
)

m.fs.fg_temp_cn = pyo.Constraint(rule=fg_temp_rule)

def heat_2_PLSH_rule(b):
return m.fs.PlSH.heat_duty[0] == - 42149808.046329699456692 \
* pyo.log(m.fs.coal_flow) + 13125913.817196270450950 \
* pyo.exp(m.fs.SR) - 82168941.403612509369850 \
* m.fs.coal_flow**-.5 - 20751.165176131220505340 \
* (m.fs.coal_flow*m.fs.SR)**1.5 + 35303843.583323523402214 \
* (m.fs.coal_flow/m.fs.SR)**0.5
return (
m.fs.PlSH.heat_duty[0]
== -42149808.046329699456692 * pyo.log(m.fs.coal_flow)
+ 13125913.817196270450950 * pyo.exp(m.fs.SR)
- 82168941.403612509369850 * m.fs.coal_flow**-0.5
- 20751.165176131220505340 * (m.fs.coal_flow * m.fs.SR) ** 1.5
+ 35303843.583323523402214 * (m.fs.coal_flow / m.fs.SR) ** 0.5
)

m.fs.heat_2_PLSH_cn = pyo.Constraint(rule=heat_2_PLSH_rule)

def heat_2_ww_rule(b):
heat_total = 15383517.068522246554494 * m.fs.coal_flow \
- 145195958.70188459753990 * m.fs.SR**0.5 + 91548063.4268338829278\
* (m.fs.coal_flow*m.fs.SR)**0.5 - 11732787.822234204038978 \
* m.fs.coal_flow*m.fs.SR - 19639.552666366322227987 \
* (m.fs.coal_flow/m.fs.SR)**2
return m.fs.Water_wall.heat_duty[0] == heat_total \
- m.fs.PlSH.heat_duty[0]
heat_total = (
15383517.068522246554494 * m.fs.coal_flow
- 145195958.70188459753990 * m.fs.SR**0.5
+ 91548063.4268338829278 * (m.fs.coal_flow * m.fs.SR) ** 0.5
- 11732787.822234204038978 * m.fs.coal_flow * m.fs.SR
- 19639.552666366322227987 * (m.fs.coal_flow / m.fs.SR) ** 2
)
return m.fs.Water_wall.heat_duty[0] == heat_total - m.fs.PlSH.heat_duty[0]

m.fs.heat_2_ww_cn = pyo.Constraint(rule=heat_2_ww_rule)

# unfix variables to build flowsheet connections
Expand All @@ -217,14 +247,10 @@ def heat_2_ww_rule(b):
m.fs.FSH.hot_side.properties_in[:].flow_mol_comp.unfix()
m.fs.FSH.hot_side.properties_in[:].temperature.unfix()
m.fs.coal_flow.fix(50.15)
print('degrees of freedom = ' + str(degrees_of_freedom(m)))
print("degrees of freedom = " + str(degrees_of_freedom(m)))
# initialize surrogate models (better initial point before solve)
calculate_variable_from_constraint(
m.fs.PlSH.heat_duty[0],
m.fs.heat_2_PLSH_cn)
calculate_variable_from_constraint(
m.fs.Water_wall.heat_duty[0],
m.fs.heat_2_ww_cn)
calculate_variable_from_constraint(m.fs.PlSH.heat_duty[0], m.fs.heat_2_PLSH_cn)
calculate_variable_from_constraint(m.fs.Water_wall.heat_duty[0], m.fs.heat_2_ww_cn)
# set scaling parameters
iscale.calculate_scaling_factors(m)
# final solve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@
" \"FG_2_AIRPH_P\",\n",
"}\n",
"\n",
"\n",
"# Return expressions for the objective\n",
"def sse(model, data):\n",
" return sum((model.err[i]) ** 2 for i in objective_tags)"
Expand Down
Loading
Loading