BioSTEAMDevelopmentGroup / BioSTEAMDevelopmentGroup/Bioindustrial-Park
Consultation about loop convergence
- Dominant language
- Jupyter Notebook
- Stars
- 53
- Forks
- 27
- PR merge metrics
- No merged PRs in 30d
Description
Hello, when I run the following code, I have a problem with the loop not converging. I tried some ways, but it still hasn’t been solved. Could I ask you how to solve it? Thanks for your help.
The code is as follows:
```
import biosteam as bst
bst.nbtutorial()
from biosteam import settings
from biosteam import units
import thermosteam as tmo
Riboflavin = bst.Chemical('Riboflavin', Hf=-55700, Tc=650+273.15, Tb=240+273.15, Hvap=70000)
Riboflavin.mu.l.add_method(f=0.0003259750718128194)
Ethanol = bst.Chemical('Ethanol')
Water = bst.Chemical('Water')
bst.settings.set_thermo([Riboflavin, Ethanol, Water])
riboflavin = bst.Stream(
ID='riboflavin',
price=0.0916,
total_flow=1000,
units='kg/hr',
Riboflavin=300,
Ethanol=350,
T=100+273.15,
P=101325,
)
Water = bst.Stream(
ID='Water',
price=0.0916,
total_flow=350,
Water=1,
units='kg/hr',
T=100+273.15,
P=101325,
)
recycle=bst.Stream('recycle')
M101=units.Mixer('M101', (riboflavin, Water))
P101=units.Pump('P101', M101-0, P=101325*1.1)
M102=units.Mixer('M102', (P101-0, recycle))
P102=units.Pump('P102', M102-0, P=101325*2)
d=M102-0
@P102.add_specification(run=True)
def adjust_pump_pressure_P102():
P102.P = d.P + 10132.5
H101 = units.HXutility('H101', P102-0, T=370, rigorous=True)
D101 = units.BinaryDistillation('D101', H101-0, P=10132.5, Lr=0.9, Hr=0.9, k=1.2, Rmin=0.001, LHK=('Ethanol', 'Water'))
H102 = units.HXutility('H102', D101-0, T=300, rigorous=True)
F102 = units.Flash('F102', H102-0, outs=('F102_vapor', 'F102_liquid'), P=10132.5, Q=0)
P103=units.Pump('P103', F102-1, recycle, P=101325)
@P103.add_specification(run=True, impacted_units=[M101])
def adjust_ethanol_flow():
F_mass_Ethanol = recycle.imass['Ethanol']
F_mass_Water = recycle.imass['Water']
riboflavin.imass['Ethanol'] = max(350 - F_mass_Ethanol, 0)
Water.imass['Water'] = 350 - F_mass_Water
@P103.add_specification(run=True)
def adjust_pump_pressure_P103():
a = H102-0
P103.P = a.P + 10132.5
Riboflavin_sys = bst.main_flowsheet.create_system('Riboflavin_sys')
Riboflavin_sys.simulate()
Riboflavin_sys.diagram('thorough', format='png')
riboflavin.imass
```
The error is as follows:
```
RuntimeError Traceback (most recent call last)
Cell In[1], line 71
68 P103.P = a.P + 10132.5
70 Riboflavin_sys = bst.main_flowsheet.create_system('Riboflavin_sys')
---> 71 Riboflavin_sys.simulate()
72 Riboflavin_sys.diagram('thorough', format='png')
73 riboflavin.imass
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2601, in System.simulate(self, update_configuration, units, design_and_cost, **kwargs)
2581 def simulate(self, update_configuration: Optional[bool]=None, units=None,
2582 design_and_cost=None, **kwargs):
2583 """
2584 If system is dynamic, run the system dynamically. Otherwise, converge
2585 the path of unit operations to steady state. After running/converging
(...)
2599
2600 """
-> 2601 with self.flowsheet.temporary():
2602 specifications = self._specifications
2603 if specifications and not self._running_specifications:
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_flowsheet.py:38, in TemporaryFlowsheet.__exit__(self, type, exception, traceback)
36 def __exit__(self, type, exception, traceback):
37 main_flowsheet.set_flowsheet(self.original)
---> 38 if exception: raise exception
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2657, in System.simulate(self, update_configuration, units, design_and_cost, **kwargs)
2651 outputs = self.simulate(
2652 update_configuration=True,
2653 design_and_cost=design_and_cost,
2654 **kwargs
2655 )
2656 else:
-> 2657 raise error
2658 else:
2659 if (not update_configuration # Avoid infinite loop
2660 and self._connections != [i.get_connection() for i in self.streams]):
2661 # Connections has been updated within simulation.
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2644, in System.simulate(self, update_configuration, units, design_and_cost, **kwargs)
2642 else:
2643 try:
-> 2644 outputs = self.converge(**kwargs)
2645 if design_and_cost: self._summary()
2646 except Exception as error:
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2344, in System.converge(self, recycle_data, update_recycle_data)
2342 for i in range(self._N_runs): method()
2343 else:
-> 2344 method()
2345 if update_recycle_data:
2346 try: recycle_data.update()
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2297, in System._solve(self)
2295 data = self._get_recycle_data()
2296 f = self._iter_run_conditional if conditional else self._iter_run
-> 2297 try: solver(f, data, **kwargs)
2298 except (IndexError, ValueError) as error:
2299 data = self._get_recycle_data()
File D:\anaconda\envs\zddd\lib\site-packages\flexsolve\iterative_solvers.py:48, in conditional_fixed_point(f, x)
46 condition = True
47 while condition:
---> 48 x1, condition = f(x0)
49 x0 = x1
50 return x1
File D:\anaconda\envs\zddd\lib\site-packages\biosteam\_system.py:2092, in System._iter_run_conditional(self, data)
2084 not_converged = not (
2085 (mol_error < self.molar_tolerance
2086 or rmol_error < self.relative_molar_tolerance)
(...)
2089 or rT_error < self.relative_temperature_tolerance)
2090 )
2091 if not_converged and self._iter >= self.maxiter:
-> 2092 if self.strict_convergence: raise RuntimeError(f'{repr(self)} could not converge' + self._error_info())
2093 else: not_converged = False
2094 return self._get_recycle_data(), not_converged
RuntimeError: could not converge
Highest convergence error among components in recycle
stream M101-0 after 200 loops:
- flow rate 5.74e+01 kmol/hr (1e+02%)
- temperature 0.00e+00 K (0%)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at biosteam/_system.py, following System.simulate(), converge(), and _solve() into flexsolve/iterative_solvers.py. Reproduce the supplied notebook code and inspect the recycle stream M101-0 and its convergence errors; done would mean identifying and resolving the reported failure to converge.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100