<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
#######################################
#           Exercise 2-8
#######################################
"""
import sympy as sp

# Assignment
# U2.8 The raw material for the production of vegetable fat contains 28 % fat and 10 % water by weight.
# By pressing, oil containing 80 % fat and 20 % water by weight is obtained. The rest after pressing,
# which still contains 10% (by weight) fat, is extracted with hexane. The extract is separated from hexane
# by steaming and the remaining pure fat is added to the other product. The rest after extraction is then
# dried in a dryer so that it finally contains 5 % (by weight) moisture and 0.2 % (by weight) fat. What is
# the composition of the product? How much % of the total fat production is obtained by extraction?
# Result: The product contains 84.4 % (by weight) fat and 15.6 % (by weight) water. 26.2% of the total fat
# production is obtained by extraction.

# note: mass balance, substance balance is not possible due to "the rest after 
# pressing still contains 10% by weight of fat"

# Components: A fat, B water, C inert
# I node Machine press
#   input: 1 raw material                              
# outputs: 2 oil  3 rest after pressing                

wA1, wB1, wC1 = 0.28, 0.10, 1-0.28-0.10 # kg(A,B,C)/kgTOT raw material
wA2, wB2      = 0.80, 0.20              # kg(A,B)/kgTOT oil
wA3 = 0.10                              # kgA/kgTOT rest after pressing
m1 = 100 # kg chosen basis of calculation

# We need to compute
# m2, m3, wi3 %
# unknowns: masses m2, m3, composition wi3 %

m2, m3, wB3, wC3  = sp.symbols('m2 m3 wB3 wC3')
r1  = sp.Eq(m1,     m2+m3) 
r1A = sp.Eq(wA1*m1, wA2*m2+wA3*m3)
r1B = sp.Eq(wB1*m1, wB2*m2+wB3*m3)

# additional equation
dv11 = sp.Eq(wA3+wB3+wC3, 1)  # sum of mass fractions = 1

# 4 equations, 3(m)+2(wi3) = 4 unknowns can be solved

# II node extraction device + rectification device + dryer
#  inputs: 3 rest after pressing (from I)  4 hexane (= m6, hexane is not needed to be solved) 
#          5 steam (= m8, enters during rectification and exits from it, not needed to be solved)
# outputs: 6 hexane (= m4, not needed to be solved) 7 fat (from rectification) 
#          8 water (= m5, from rectification, not needed to be solved) 
#          9 rest after drying 10 steam (water that was in the rest after pressing, then after extraction 
#          and finally left the dryer as steam)

wA7 = 1                                    # kgA/kgTOT fat from rectification
wA9, wB9, wC9 = 0.002, 0.05, 1-0.002-0.05  # kg(A,B,C)/kgTOT rest after drying
wB10 = 1                                   # kgB/kgTOT steam
# We need compute
# unknowns: masses m7, m9 a m10
m7, m9, m10  = sp.symbols('m7 m9 m10')

r2  = sp.Eq(m3,     m7+m9+m10) 
r2A = sp.Eq(wA3*m3, wA7*m7+wA9*m9 + 0)
r2B = sp.Eq(wB3*m3, 0 + wB9*m9+wB10*m10)

# 3 equations + dv11, 4(m) + 2(wi3) = 6 unknowns 4/6 cannot be solved

# III node mixer
# inputs: 2 oil (from I)  7 fat (from II)          Inputs
# output: 11 product                               Output

# We need to compute
# unknowns: mass m11, composition wi11
m11, wA11, wB11  = sp.symbols('m11 wA11 wB11')

r3  = sp.Eq(m2 + m7, m11) 
r3A = sp.Eq(wA2 * m2 + wA7 * m7, wA11 * m11)

# additional equation
dv31 = sp.Eq(wA11 + wB11, 1)   # sum of mass fractions = 1

# 3 equations, 3(m) + 2(wi11) = 5 unknowns 3/5 cannot be solved

# calculation target: wi11, yield

# solve the system of equations
res = sp.solve([r1, r1A, r1B, dv11, r2, r2A, r2B, r3, r3A, dv31])

print("\nU2.8 solution:") #\n odřákuje   \n is New Line
print(res)
if type(res) == list:
    res = res[0]
m2 = res[m2]
m3 = res[m3]
m7 = res[m7]
m11 = res[m11]
wA11 = res[wA11]
wB11 = res[wB11]


# post-processing 
yield_percentage = m7 * wA7 / (m11 * wA11) * 100   # % yield

print(f"Solution: The product contains {round(wA11 * 100, 1)} wt. % fat, "
      f"{round(wB11 * 100, 1)} wt. % water.")
print(f"Extraction yields {round(yield_percentage, 1)}% of fat from the total production.")</pre></body></html>