0% found this document useful (0 votes)
35 views

Homework 14: Problem 1

This document discusses solving various problems related to chemical reactions and fluid flow using numerical methods in Python. Problem 1 asks to find roots of a polynomial using fsolve. Problem 2 asks to find solutions to two equations using fsolve and an initial guess. Problem 3 gives code to calculate the adiabatic flame temperature of a methane-air reaction using a thermo class and fsolve to solve for the product temperature. It provides equations and steps to implement in the incomplete code. Problem 4 asks to solve a system of 6 equations in 6 unknowns to model flow rates in a parallel pipe network.

Uploaded by

Miguel Montero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Homework 14: Problem 1

This document discusses solving various problems related to chemical reactions and fluid flow using numerical methods in Python. Problem 1 asks to find roots of a polynomial using fsolve. Problem 2 asks to find solutions to two equations using fsolve and an initial guess. Problem 3 gives code to calculate the adiabatic flame temperature of a methane-air reaction using a thermo class and fsolve to solve for the product temperature. It provides equations and steps to implement in the incomplete code. Problem 4 asks to solve a system of 6 equations in 6 unknowns to model flow rates in a parallel pipe network.

Uploaded by

Miguel Montero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?

download=false

Homework 14

Problem 1
Use fsolve to find the roots of the polynomial .

In [1]: import numpy as np


from scipy.optimize import fsolve

In [ ]:

Problem 2
Use fsolve to find the solution of the following two equations:

Use an initial guess of , = 1.

In [ ]:

1 of 6 3/7/2017 7:38 AM
HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?download=false

Problem 3
Compute the adiabatic flame temperature for a stoichiometric methane-air flame. The code is given below. There is a thermo
class that is modified from your last homework. Also, you'll need thermoData.yaml again. Then there is a function to define.
Fill in the blanks as indicated. You should also read all of the code given below and make sure you understand it.

Equation Summary:

Your function (started for you below) is: f_flame(Ta) = 0.


That is, .
is the unknown.

.
.
.
.
and are given.
.
Do these separately for reactants and products That is: for the
reactants. (Also is the same for products since mass is conserved.)
is computed using the thermo class. So, if t_CO2 is my thermo class object for , then
h_CO2=t_CO2.h_mass(T).

Description:

We have a chemical reaction:


+ 7.52 .
You can think of the burning as potential energy stored in the reactant bonds being released as kinetic energy in the
products so the product temperature is higher.
Adiabatic means there is no enthalpy loss. You can think of enthalpy as energy. This means the products have the
same enthalpy as the reactants. And this is just a statement that energy is conserved, like mass is.
The idea is to take a known reactant temperature, find the reactant enthalpy (which is an easy explicit equation you
can calculate directly), then set the product enthalpy equal to the reactant enthalpy and find the corresponding
product temperature (which is a harder nonlinear solve).
.
The reactants start at room temperature, , so we can compute their enthalpy.
We know the moles of reactants: , , .
So, we can compute the corresponding masses using the molecular weights.
Then we sum the masses of each species to get the total mass, and compute the mass fractions.
Then we can compute the enthalpy as . That is, the total enthalpy is the sum of the enthalpy
per unit mass of each species times the mass fraction of each species.
For reactants we have , where are evaluated using the
class function h_mass(T), and T=300 for reactants.
Now, . For products, we have , where we evaluate the class
function h_mass(Tp), where Tp is the product temperature we are trying to compute.
Solving for amounts to solving , where

2 of 6 3/7/2017 7:38 AM
HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?download=false

In [2]: import numpy as np


from scipy.optimize import fsolve
import yaml

class thermo:
def __init__(self, species, MW) :
"""
species: input string name of species in thermoData.yaml
M: input (species molecular weight, kg/kmol)
"""
self.Rgas = 8314.46 # J/kmol*K
self.M = MW
with open("thermoData.yaml") as yfile :
yfile = yaml.load(yfile)
self.a_lo = yfile[species]["a_lo"]
self.a_hi = yfile[species]["a_hi"]
self.T_lo = 300.
self.T_mid = 1000.
self.T_hi = 3000.

#--------------------------------------------------------
def h_mole(self,T) :
"""
return enthalpy in units of J/kmol
T: input (K)
"""
if T<=self.T_mid and T>=self.T_lo :
a = self.a_lo
elif T>self.T_mid and T<=self.T_hi :
a = self.a_hi
else :
print ("ERROR: temperature is out of range")
hrt = a[0] + a[1]/2.0*T + a[2]/3.0*T*T + a[3]/4.0*T**3.0 + a[4]/5.0*T**4.0
+ a[5]/T
return hrt * self.Rgas * T

#--------------------------------------------------------
def h_mass(self,T) :
"""
return enthalpy in units of J/kg
T: input (K)
"""
return self.h_mole(T)/self.M

3 of 6 3/7/2017 7:38 AM
HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?download=false

In [3]: def f_flame(Ta) :


"""
We are solving for hp = sum_i y_i*h_i. In f=0 form this is f = hp - sum_i y_i*h
_i
We know the reactant temperature, so we can compute enthalpy (h). Then we know
hp = hr (adiabatic).
Vary T until sum_i y_i*h_i = hp.
Steps:
1. Given moles --> mass --> mass fractions.
2. Make thermo classes for each species.
3. Compute hr = sum_i y_i*h_i.
... Do this for the reactants, then products.
"""
no2 = 2. # kmol
nch4 = 1.
nn2 = 7.52
nco2 = 1.
nh2o = 2.
Mo2 = 32. # kg/kmol
Mch4 = 16.
Mn2 = 28.
Mco2 = 44.
Mh2o = 18.
mo2 = no2*Mo2 # mass
mch4 = nch4*Mch4 # mass
mn2 = nn2*Mn2 # mass
mh2o = nh2o*Mh2o
mco2 = nco2*Mco2
t_o2 = thermo("O2",Mo2) # thermo object; use as: t_o2.h_mass(T) to get
h_O2, etc.
t_ch4 = thermo("CH4",Mch4)
t_n2 = thermo("N2",Mn2)
t_co2 = thermo("CO2",Mco2)
t_h2o = thermo("H2O",Mh2o)

#-------- Reactants
# TO DO: compute total mass, then mass fractions
# TO DO: Set reactant temperature, then compute reactant enthalpy

#---------- Products
# TO DO: Set the product enthalpy = reactant enthalpy
# TO DO: Set the product mass fractions
# TO DO: Compute the enthalpy of the products corresponding to the current Tp
# Then return the function: f(Tp) = hp - hp_based_on_current_Tp

In [4]: # TO DO: Set a guess temperature, then solve for the product temperature

Problem 4

4 of 6 3/7/2017 7:38 AM
HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?download=false

Example: Solve a system of 6 equations in 6 unknowns

This is solving a parallel pipe network where we have three pipes that are connected at the beginning and the end. The pipes
can be of different lengths and diameter and pipe roughness. Given the total flow rate, and the pipe properties, find the flow
rate through each of three parallel pipes.

Unknowns: three flow rates: , , .


We need three equations.
We'll label the pipes 1, 2, and 3.
Eq. 1: .
That is, the total flow rate is just the sum through each pipe.
Because the pipes are connected, the pressure drop across each pipe is the same:
Eq. 2:
Eq. 3:
Now we need to relate the pressure drop equations to the unknowns. The pressure is related to the flow rate by:
, and we use , where is volumetric flow rate. Then,
substitute for v to get:

Here, is the friction factor in the pipe. We treat it as an unknown so we have three more unknowns: , , .
The Colbrook equation relates to for given pipe properties. So, we have three more equations.
Here are the six equations in terms of the six unknowns: , , , , , .

1. .

2.

3.

4. Colbrook equation relating to :

5. Colbrook equation relating to .


6. Colbrook equation relating to .

All units are SI.

5 of 6 3/7/2017 7:38 AM
HW14 http://localhost:8888/nbconvert/html/Desktop/HW14.ipynb?download=false

In [5]: def F_pipes(x) :


Q1 = x[0] # rename the vars so we can read our equations below.
Q2 = x[1]
Q3 = x[2]
f1 = x[3]
f2 = x[4]
f3 = x[5]

Qt = 0.01333 # Given total volumetric flow rate


e1 = 0.00024 # pipe roughness (m) (epsilon in the equation)
e2 = 0.00012
e3 = 0.0002
L1 = 100 # pipe length (m)
L2 = 150
L3 = 80
D1 = 0.05 # pipe diameter (m)
D2 = 0.045
D3 = 0.04
mu = 1.002E-3 # viscosity (kg/m*s)
rho = 998. # density (kg/m3)

F = np.zeros(6) # initialize the function array

# TO DO: Define the functions here

return F

#--------------------------------------
# TO DO: make a guess array for the unknowns: Q1, Q2, Q3, f1, f2, f3
# (use Q3 = Qtot-Q1-Q2 in your guess, for consistency)
# TO DO: Solve the problem and print the results.

In [ ]:

6 of 6 3/7/2017 7:38 AM

You might also like