Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

HW09: Additional Equation Solving Practice

Problem 1

This problem reviews the use of non-linear solvers. You can use any numerical non-linear solver. Below is an image of a portion of a horizonal pipe network with pipes label P1, P2, and P3. Fluid enters the left and then exits at the right through both P2 and P3. Each of those pipes have different properties (lengths, diameters, and roughness). Your task is to solve for the flow in P2 and P3 given a flow in P1.

Action 1: Create a class called fluid using param.Parameterized that has the following properties:

  • density (default of 1000 kg/m3)

  • mu (default of 1E-3 Pa-sec for viscosity)

Action 2: Create a class called pipeClass using param.Parameterized that has the following properties:

  • length (m)

  • diameter (m)

  • roughness (m)

  • flow (m3/s)

  • fluidinside (a fluid object with the default being the fluid default): specify with fluidinside = param.Parameter(fluid()). You can refer to the fluidinside density or mu with self.fluidinside.density or self.fluidinside.mu, respectively.

  • a function called velocity that returns the velocity of the fluid given self.flow and self.diameter: i.e. velocity = self.flow/(np.pi/4*self.diameter**2)

  • a function called reynoldsNumber that returns the Reynolds number for the pipe flow: i.e. Re = self.velocity()*self.diameter*self.fluidinside.density/self.fluidinside.mu

    • for the Reynolds number, assume a density of 1000 kg/m3 and a viscosity of 1E-3 Pa*sec (the default fluid() parameters)

  • a function called frictionFactor that returns the friction factor for the pipe using the the Haaland Equation

  • a function to determine the pressuredrop in the horizontal pipe = ρfLv2/(2D)\rho fLv^2/(2D). ρ\rho is density, f the friction factor, L the length, v the velocity, D the diameter.

All of the functions in the class need only be functions of self

Action 3: Now create three pipe objects with the following properties:

PipeLength (m)Diameter (m)Roughness (m)
P11000.50.0001
P22000.250.00001
P3500.750.0002

Other System Properties:

  • The total flow rate through the system is 0.1 m^3/s

  • The fluid is water with the properties above referenced.

  • The fluid is turbulent (Re > 4000)

Action 4: Setup a function to calculate the pressure drop through P1 and a second pipe object with a given flow rate in the second pipe object using the above objects and their class functions, e.g. def routepressuredrop(objectj,flowj):. You can do this step with three lines of object based code.

Action 5: Then setup a function to solve for the flow in each pipe. The flow in each pipe will be different such that the total pressure drop through P1 and P3 is equal to P1 and P2, e.g. routepressuredrop(P2,flow2) = routepressuredrop(P3,flow3)

Action 6: Report the flow rate in each pipe and verify that the flow is indeed turbulent(Re > 4000). Also report the total pressure drop through the system (P1 + P3) and verify that it is equal to the pressure drop through P1 and P2.

Hopefully, this example shows you how using objects helps you keep track of the variables in your code. And that it helps reduce the errors made.

Problem 2

Program the same problem above without using objects. I started to do this for the key and I thought it would be rude of me to ask you to actually do this so this problem is a free-bee. I’m giving you the result for free below. It doesn’t look as nice and organized as the code is above (especially if I don’t name my variables nicely).

0.0024372371378035057

By my count 32 lines of code without objects. 28 lines of code with objects. Which way do you like better? Why?

Your response here

Problem 3

In class we used lambda for an inline function: f = lambda x: x**2 -5. I can use a lambda function to define the arguments to use in a multivariable function as follows:

Example: Solve this equation for y given: x2+y2+c=56.3x^2+y^2+c =56.3 where x=3x = 3 and c=−4.2c = -4.2

Now solve this equation with x = 3 and c = -4.2 with a guess of 3. Remembering that Call like this: fsolve(function,guess).

7.176350047203662

Notice that I used the inline function definition with lambda to reduce my variable count to 1. Another way is with arguments:

7.176350047203662

Action Solve the below set of equations given b = 3 and c = 5. You must keep b and c as parameters in the function call.

2⋅x2+y2=25x+y+z=cy+4.3⋅z=b2\cdot x^2 + y^2 = 25 \\ x + y + z = c \\ y + 4.3\cdot z = b