@@ -157,10 +157,11 @@ def __init_subclass__(cls,
157157 if default_phenomena is None :
158158 methods = (
159159 '_update_nonlinearities' ,
160- '_get_energy_departure_coefficient' ,
160+ '_update_energy_coefficient' ,
161+ '_update_variable' ,
161162 '_create_material_balance_equations' ,
162- '_create_energy_departure_equations ' ,
163- '_update_energy_variable ' ,
163+ '_create_energy_balance_equations ' ,
164+ '_create_bulk_balance_equations ' ,
164165 '_energy_variable' ,
165166 )
166167 if all ([getattr (cls , i ) is getattr (Unit , i ) for i in methods ]):
@@ -377,11 +378,11 @@ def _update_nonlinearities(self):
377378 self ._duty = self .Hnet
378379 Ts_new = [i .T for i in outs ]
379380 if all ([i == j for i , j in zip (Ts_new , Ts )]): # T constant
380- self ._energy_variable = None
381+ self .specified_variable = 'T'
381382 elif self .Hnet == Q : # Q constant
382- self ._energy_variable = 'T '
383+ self .specified_variable = 'Q '
383384 else :
384- self ._energy_variable = None
385+ self .specified_variable = 'B'
385386 for i , j in zip (outs , data ): i .set_data (j )
386387
387388 def _simulation_error (self ):
@@ -401,20 +402,35 @@ def _simulation_error(self):
401402 self ._outs = outs
402403 return np .abs (new_flows - flows ).sum (), np .abs (Ts_new - Ts ).sum ()
403404
404- def _get_energy_departure_coefficient (self , stream ):
405+ def _update_energy_coefficient (self , stream , coefficients ):
405406 """
406- tuple[object, float] Return energy departure coefficient of a stream
407- for phenomena-based simulation.
407+ Update coefficient of a stream
408+ for phenomena-based simulation and
409+ return the reference value.
408410 """
409- if self ._energy_variable == 'T' : return (self , stream .C )
411+ if self .specified_variable == 'Q' :
412+ C = stream .C
413+ coefficients [self , 'T' ] = - C
414+ return - C * stream .T
415+ return 0
416+
417+ def _update_variable (self , variable , value ):
418+ """
419+ Update variable being solved in equations for
420+ phenomena-based simulation.
421+ """
422+ if variable == 'T' :
423+ for i in self .outs : i .T = value
424+ else :
425+ raise ValueError ('invalid variable' )
410426
411427 def _create_material_balance_equations (self , composition_sensitive ):
412428 """
413429 list[tuple[dict, array]] Create material balance equations for
414430 phenomena-based simulation.
415431 """
416432 if self ._link_streams : return []
417- fresh_inlets , process_inlets , equations = self ._begin_equations (composition_sensitive )
433+ fresh_inlets , process_inlets , equations = self ._begin_material_equations (composition_sensitive )
418434 outs = self .flat_outs
419435 N = self .chemicals .size
420436 ones = np .ones (N )
@@ -425,7 +441,7 @@ def _create_material_balance_equations(self, composition_sensitive):
425441 except :
426442 rhs = predetermined_flow
427443 mol_total = sum ([i .mol for i in outs ])
428- for i , s in enumerate ( outs ) :
444+ for s in outs :
429445 split = s .mol / mol_total
430446 minus_split = - split
431447 eq_outs = {}
@@ -436,31 +452,58 @@ def _create_material_balance_equations(self, composition_sensitive):
436452 )
437453 return equations
438454
439- def _create_energy_departure_equations (self ):
455+ def _create_energy_balance_equations (self ):
440456 """
441457 list[tuple[dict, float]] Create energy departure equations for
442458 phenomena-based simulation.
443459 """
460+ fresh_inlets , process_inlets , equations = self ._begin_energy_equations ()
444461 if self ._energy_variable == 'T' :
445- coeff = {self : sum ([i .C for i in self .outs ])}
446- for i in self .ins : i ._update_energy_departure_coefficient (coeff )
447- if self ._dmol .any ():
448- dH = self ._duty - self .Hnet
462+ dmol = self ._dmol
463+ if dmol .any ():
464+ dH = self ._duty + sum ([i .Hf for i in self .outs ]) - sum ([i .Hf for i in self .ins ])
449465 else :
450- dH = self ._duty + self .H_in - self .H_out
451- return [(coeff , dH )]
466+ dH = self ._duty
467+ coeff = {(self , 'T' ): sum ([i .C for i in self .outs ])}
468+ for i , s in enumerate (self .outs ): coeff [self , i ] = s .h
469+ for i in process_inlets :
470+ dH += i ._update_energy_coefficient (coeff )
471+ for i in fresh_inlets :
472+ dH += i .H
473+ equations .append (
474+ (coeff , dH )
475+ )
476+ return equations
452477 else :
453- return []
478+ return equations
454479
455- def _update_energy_variable (self , departure ):
480+ def _create_bulk_balance_equations (self ):
456481 """
457- Update energy variable being solved in energy departure equations for
482+ list[tuple[dict, array]] Create bulk balance equations for
458483 phenomena-based simulation.
459484 """
460- if self ._energy_variable == 'T' :
461- for i in self .outs : i .T += departure
485+ if self ._link_streams : return []
486+ fresh_inlets , process_inlets , equations = self ._begin_bulk_equations ()
487+ outs = self .flat_outs
488+ predetermined_flow = [i .F_mol for i in fresh_inlets ]
489+ try :
490+ dF_mol = self ._dmol .sum ()
491+ rhs = predetermined_flow + dF_mol
492+ except :
493+ rhs = predetermined_flow
494+ F_mol_total = sum ([i .F_mol for i in outs ])
495+ for i , s in enumerate (outs ):
496+ split = s .F_mol / F_mol_total
497+ minus_split = - split
498+ eq_outs = {}
499+ for i in process_inlets : eq_outs [i , 'F_mol' ] = minus_split
500+ eq_outs [s , 'F_mol' ] = 1
501+ equations .append (
502+ (eq_outs , split * rhs )
503+ )
504+ return equations
462505
463- def _begin_equations (self , composition_sensitive ):
506+ def _begin_material_equations (self , composition_sensitive ):
464507 inlets = self .ins
465508 fresh_inlets = []
466509 process_inlets = []
@@ -519,6 +562,56 @@ def _begin_equations(self, composition_sensitive):
519562 equations = material_equations
520563 return fresh_inlets , process_inlets , equations
521564
565+ def _begin_bulk_equations (self ):
566+ inlets = self .ins
567+ fresh_inlets = []
568+ process_inlets = []
569+ equations = [f () for i in inlets for f in i .bulk_equations ]
570+ isfeed = lambda s : s .isfeed () or s .source ._recycle_system is not self ._recycle_system
571+ if equations :
572+ for i in inlets :
573+ if (isfeed (i ) and
574+ not i .bulk_equations ):
575+ fresh_inlets .append (i )
576+ elif len (i ) > 1 :
577+ process_inlets .extend (i )
578+ else :
579+ process_inlets .append (i )
580+ else :
581+ for i in inlets :
582+ if isfeed (i ):
583+ fresh_inlets .append (i )
584+ elif len (i ) > 1 :
585+ process_inlets .extend (i )
586+ else :
587+ process_inlets .append (i )
588+ return fresh_inlets , process_inlets , equations
589+
590+ def _begin_energy_equations (self ):
591+ inlets = self .ins
592+ fresh_inlets = []
593+ process_inlets = []
594+ equations = [f () for i in inlets for f in i .energy_equations ]
595+ isfeed = lambda s : s .isfeed () or s .source ._recycle_system is not self ._recycle_system
596+ if equations :
597+ for i in inlets :
598+ if (isfeed (i ) and
599+ not i .energy_equations ):
600+ fresh_inlets .append (i )
601+ elif len (i ) > 1 :
602+ process_inlets .extend (i )
603+ else :
604+ process_inlets .append (i )
605+ else :
606+ for i in inlets :
607+ if isfeed (i ):
608+ fresh_inlets .append (i )
609+ elif len (i ) > 1 :
610+ process_inlets .extend (i )
611+ else :
612+ process_inlets .append (i )
613+ return fresh_inlets , process_inlets , equations
614+
522615 Inlets = piping .Inlets
523616 Outlets = piping .Outlets
524617
0 commit comments