Coverage for backend/ahuora-compounds/ahuora_property_packages/utils/fix_state_vars.py: 4%

38 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-05-13 02:47 +0000

1from idaes.core.util.exceptions import ConfigurationError 

2 

3""" 

4This method is analogous to the fix_state_vars method in 

5idaes.core.util.initialization, but it allows us to handle 

6the case where we have constraints that define the state. 

7""" 

8 

9def fix_state_vars(blk, state_args=None): 

10 """ 

11 Method for fixing state variables within StateBlocks. Method takes an 

12 optional argument of values to use when fixing variables. 

13 

14 Args: 

15 blk : An IDAES StateBlock object in which to fix the state variables 

16 state_args : a dict containing values to use when fixing state 

17 variables. Keys must match with names used in the 

18 define_state_vars method, and indices of any variables must 

19 agree. 

20 

21 Returns: 

22 A dict keyed by block index, state variable name (as defined by 

23 define_state_variables) and variable index indicating the fixed status 

24 of each variable before the fix_state_vars method was applied. 

25 """ 

26 if state_args is None: 

27 state_args = {} 

28 

29 flags = {} 

30 for k, b in blk.items(): 

31 flow_exprs = { 

32 "flow_mol", 

33 "flow_mass", 

34 "flow_vol" 

35 } 

36 other_exprs = { 

37 "pressure", 

38 "enth_mol", 

39 "enth_mass", 

40 "entr_mol", 

41 "entr_mass", 

42 "temperature", 

43 "total_energy_flow", 

44 "custom_vapor_frac", 

45 "vapor_frac", 

46 } 

47 for n, v in b.define_state_vars().items(): 

48 fix_var = True 

49 if n in flow_exprs: 

50 for prop_name in flow_exprs: 

51 if hasattr(b, prop_name + "_var") and getattr(b, prop_name + "_var").is_fixed(): 

52 fix_var = False 

53 break 

54 elif n in other_exprs: 

55 if not v.is_fixed(): 

56 # check if any of the constraints exist 

57 for prop_name in other_exprs: 

58 if hasattr(b, prop_name + "_var") and getattr(b, prop_name + "_var").is_fixed(): 

59 # don't fix this variable - it is defined by a constraint 

60 other_exprs.remove(prop_name) 

61 fix_var = False 

62 break 

63 for i in v: 

64 flags[k, n, i] = v[i].is_fixed() 

65 if fix_var and not v[i].is_fixed(): 

66 # If not fixed, fix at either guess provided or current value 

67 if n in state_args: 

68 # Try to get initial guess from state_args 

69 try: 

70 if i is None: 

71 val = state_args[n] 

72 else: 

73 val = state_args[n][i] 

74 except KeyError: 

75 raise ConfigurationError( 

76 "Indexes in state_args did not agree with " 

77 "those of state variable {}. Please ensure " 

78 "that indexes for initial guesses are correct.".format( 

79 n 

80 ) 

81 ) 

82 v[i].fix(val) 

83 else: 

84 # No guess, try to use current value 

85 if v[i].value is not None: 

86 v[i].fix() 

87 else: 

88 # No initial value - raise Exception before this 

89 # gets to a solver. 

90 raise ConfigurationError( 

91 "State variable {} does not have a value " 

92 "assigned. This usually occurs when a Var " 

93 "is not assigned an initial value when it is " 

94 "created. Please ensure all variables have " 

95 "valid values before fixing them.".format(v.name) 

96 ) 

97 return flags