Question: Program in Python, can only use the modules math , copy , matplotlib.plot , numpy and scipy. They also provide the answer but with missing
Program in Python, can only use the modules math, copy, matplotlib.plot, numpy and scipy.

They also provide the answer but with missing parts of code
import numpy as np import scipy as sp import math from scipy.optimize import fsolve def GetNodeFlowRates(Q, seg_names): ''' There are 8 nodes in this network and mass conservation should be true at each node. That is, sum of all mass flow rates at a node should equal zero. :param Q: List of flow rates for the pipe segments. :param seg_names: Dictionary of pipe names and indices :return: the list of node equation results ''' # I can retrieve the flow in a pipe from Q by Q[seg_names['a-b']] for example # Be sure to remember a positive flow moves in direction from lowest letter to highest letter for a pipe segment Sum_a= #&MISSING CODE HERE #sum all flows at node a Sum_b = #&MISSING CODE HERE #sum all flows at node b Sum_c = #&MISSING CODE HERE #sum all flows at node c Sum_d = #&MISSING CODE HERE #sum all flows at node d Sum_e = #&MISSING CODE HERE #sum all flows at node e Sum_f = #&MISSING CODE HERE #sum all flows at node f Sum_g = #&MISSING CODE HERE #sum all flows at node g Sum_h = #&MISSING CODE HERE #sum all flows at node h return [Sum_a,Sum_b,Sum_c,Sum_d,Sum_e,Sum_f,Sum_g,Sum_h] def GetLoopHeadLoss(Q,*args): ''' This calculates the head loss in the three loops of the pipe network. :param Q: the list of flow rates :param args: an unspecified number of positional arguments. :return: the list of head losses for the loops. ''' #unpack *args seg_names, seg_lengths, seg_diams, viscosity, roughness = args Scalar=lambda q: 1.0 if q>0 else -1.0 #using this to determine which direction flow is going to determine pressure drop or rise through pipe. #head loss in loop A is net zero around the loop #note: the head loss calculate by the Darcy-Weisbach equation is always positive. Need keep trac of which way I am traversing #the loop (pipe segment) and if it counts as a pressure loss or increase. Hence, the Scalar calculation #&MISSING CODE HERE #multiple lines missing. need to sum the head losses around the loop A as HLA #head loss in loop B is net zero around the loop #&MISSING CODE HERE #multiple lines missing. need to sum the head losses around the loop B as HLB #head loss in loop C is net zero around the loop #&MISSING CODE HERE #multiple lines missing. need to sum the head losses around the loop C as HLC return [HLA, HLB, HLC] def Find_FlowRates(Q, *args): ''' Finds the flow rates that satisfy the constraints of the pipe network (i.e., node and loop equations) :param Q: numpy array with 11 elements for the flow rates needed. :param args: arguments used in calculation. note: *args means an unspecified number of positional arguments. It could be a tuple, list or simple numbers :return: ''' #unpack *args into recognizable names density = args[0] viscosity = args[1] roughness = args[2] seg_names = args[3] seg_lengths = args[4] seg_diams = args[5] #Assume incompressible fluids and apply continuity at nodes. #Also, the pressure drop around loops A, B, and C has to equal zero. #continuity equations (sum of volumetric flows at node = 0) NF=GetNodeFlowRates(Q,seg_names) #returned result is list of 8 node equation results, when correct set of flow rates guessed, the should all be zero #head loss around each loop should be zero LHL=GetLoopHeadLoss(Q,seg_names, seg_lengths,seg_diams, viscosity,roughness) #returned result is list of 3 loop equation results result=(NF+ LHL) return result def Reynolds(V, nu, dia): ''' Calculates the reynolds number :param V: velocity in m/s :param nu: kinematic viscosity in m^2/s :param dia: pipe diameter in m :return: ''' return V*diau def FrictionFactor(relrough, Re): ''' Use the Colebrook equation to find the friction factor. NOTE: math.log is natural log, math.log10 is base 10 log :param relrough: the relative roughness for the pipe :param Re: the reynolds number :return: the darcy friction factor ''' def ffc(ff): #implementing Colebrook equation for fsolve to find ff LHS= #&MISSING CODE HERE RHS= #&MISSING CODE HERE return LHS-RHS f=fsolve(ffc,0.008) #use fsolve to find friction factor return f[0] def HeadLoss(L, d, nu, rough, Q): ''' Use the Darcy-Weisbach equation to find the head loss through a section of pipe. :param L: pipe length in m :param d: pipe diameter in m :param nu: kinematic viscosity of water in m^2/s :param rough: pipe roughness in m :param Q: volumetric flow rate in L/s :return: ''' A = #&MISSING CODE HERE #calculate pipe cross sectional area in m^2 V = #&MISSING CODE HERE #calculate absolute value of average water velocity in pipe g=9.81 #gravity Re = Reynolds(V, nu, d) #calculate the reynolds number relrough=rough/d #calculate the relative roughness for the pipe ff = FrictionFactor(relrough, Re) #calculate the Darcy friction factor hl = #&MISSING CODE HERE #calculate the head loss in m return hl def main(): ''' This program analyzes flows in a given pipe network based on the following: 1. The pipe segments are named by their endpoint node names: e.g., a-b, b-e, etc. 2. Flow from the lower letter to the higher letter of a pipe is considered positive. 3. Pressure decreases in the direction of flow through a pipe. 4. At each node in the pipe network, mass is conserved. 5. For any loop in the pipe network, the pressure loss is zero Approach to analyzing the pipe network: Step 1: assign convenient names to the pipes using a dictionary for indices Step 2: populate tuples with pipe diameters and lengths to correspond to the named pipes from step 1. Step 3: calculate the flow rates in each pipe using fsolve Step 4: output results Step 5: check results against expected properties of zero head loss around a loop and mass conservation at nodes. :return: ''' # overall properties for the pipe fluid and pipe network density = 1000 # kg/m^3 mu = 0.00089 # N*s/m^2 dynamic viscosity nu = mu/density # m^2/s kinematic viscosity roughness = 0.00025 # in m #There are 10 segments of pipe, inconnveniently named. #step 1: create a dictionary to refer to pipes by name seg_names={'a-b':0,'a-c':1, #&MISSING CODE HERE } #step 2: create tuples to contain pipe lengths and diameters (both in units of m) seg_lengths=(250,100,#&MISSING CODE HERE) seg_diams=(0.3, 0.2, #&MISSING CODE HERE) #observation of the pipe network shows 8 node equations and 3 loop equations, so I need 11 variables #we could remove node b from the system since it is redundant (i.e., just an elbow rather than a tee or cross), but we leave it in for now. Q0=np.full(11,10) #passing arguments to fsolve as a tuple seg_flow_rates=fsolve(Find_FlowRates,Q0, (#&MISSING CODE HERE)) #density, viscosity, roughness, pipe names, pipe lengths, pipe diameters #output the flow rates for each pipe segment for Seg in seg_names: print('The flow is segment {:} is {:0.4f} L/s'.format(#&MISSING CODE HERE)) #verify no mass accumulation in nodes print(GetNodeFlowRates(seg_flow_rates,seg_names)); #verify no net head loss for loops print(GetLoopHeadLoss(seg_flow_rates,seg_names, seg_lengths,seg_diams, viscosity,roughness)) nI=0 #outputting head losses for the pipe segments for fun for seg in seg_names: HL=HeadLoss(seg_lengths[nI],seg_diams[nI], viscosity, roughness, seg_flow_rates[nI]) tmpStr = "{:}".format(seg)+"\t{:5.3f}".format(HL) print(tmpStr) nI+=1 main() You will be writing three different Python programs in three different files named hw5a.py, hw5b.py and hw5c.py. You will place your files in a folder named HW5 and then zip the folder and upload it to Canvas. In this assignment, you must use variables, loops, if statements, your own function definitions and function calls. For now, you may not use any of the powerful functions available in python modules, with a few exceptions: You may import functions from the math, copy, matplotlib.plot, numpy and scipy. a.) A pipe network problem: Write a program that uses FSOLVE () to find the volumetric flow rate (m/s) of water in each segment of the pipe flow network given below. Use the following properties: water density = 1000 kg/m3 water viscosity = 0.00089 Nis/m2 pipe roughness = 0.00025 m. Your program should print the flow in each segment of pipe, nicely formatted, similar to: The flow in segment a-b is -0.0052 m^3/s The flow in segment a-c is 0.0272 m^3/s The flow in segment d-g is -0.0142 m^3/s etc. Notes: 1. Pressure decreases in direction of flow (e.g., APA-b = 0 if flow ba ) 2. The head loss around a pipe loop is zero. (e.g.,APa-+ AP7-e - APA-e - AP-d - APA-e = 0) 3. Mass is conserved at each node. Lim, = 0 4. Pressure loss in a pipe is calculated with the Darcy-Weisbach equation: Ap 5. Darcy friction factor is calculated by the Colebrook equation: 2.51 -2.0log Revt Lpva = D 2 e/D 3.7 60 L/S 300 mm a b 200 mm 4 200 mm 100 m 200 mm 30 Ls + e 150 mm B 200 mm d 150 mm 150 mm 8 250 mm h - 125 m 100 m 250 mm 125 m 15 Ls 115 L/ You will be writing three different Python programs in three different files named hw5a.py, hw5b.py and hw5c.py. You will place your files in a folder named HW5 and then zip the folder and upload it to Canvas. In this assignment, you must use variables, loops, if statements, your own function definitions and function calls. For now, you may not use any of the powerful functions available in python modules, with a few exceptions: You may import functions from the math, copy, matplotlib.plot, numpy and scipy. a.) A pipe network problem: Write a program that uses FSOLVE () to find the volumetric flow rate (m/s) of water in each segment of the pipe flow network given below. Use the following properties: water density = 1000 kg/m3 water viscosity = 0.00089 Nis/m2 pipe roughness = 0.00025 m. Your program should print the flow in each segment of pipe, nicely formatted, similar to: The flow in segment a-b is -0.0052 m^3/s The flow in segment a-c is 0.0272 m^3/s The flow in segment d-g is -0.0142 m^3/s etc. Notes: 1. Pressure decreases in direction of flow (e.g., APA-b = 0 if flow ba ) 2. The head loss around a pipe loop is zero. (e.g.,APa-+ AP7-e - APA-e - AP-d - APA-e = 0) 3. Mass is conserved at each node. Lim, = 0 4. Pressure loss in a pipe is calculated with the Darcy-Weisbach equation: Ap 5. Darcy friction factor is calculated by the Colebrook equation: 2.51 -2.0log Revt Lpva = D 2 e/D 3.7 60 L/S 300 mm a b 200 mm 4 200 mm 100 m 200 mm 30 Ls + e 150 mm B 200 mm d 150 mm 150 mm 8 250 mm h - 125 m 100 m 250 mm 125 m 15 Ls 115 L/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
