\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Oct 24, 2024 | 665 words | 7 min read

6.2.3. Task 3#

Learning Objectives#

Investigate the advantages of user-defined functions in Python; Create and implement user-defined functions in Python; recognize the similarities and differences between a main function and a called function.

Introduction#

Python is a powerful programming language for performing computations. It contains several built-in functions commonly used for computations and mathematical operations. In addition, it offers the possibility for the user to define their own functions. This makes it easy for the programmer to generalize processes, so they can be reused. In addition, it helps to modularize code, making it easier to read and troubleshoot.

When writing many functions, it is often useful to write them in a separate file (called a module) and import them into another file to reduce the amount of code in that file and to organize like functions together. In this task, you will be writing all your functions in separate files and importing them into your main script using the import statement.

You are developing a wind turbine for a prominent energy company, and your team has been tasked to write a Python program to help model the performance of the turbine. The wind power generated can be calculated using (6.3) and (6.4), where \(P\) is the power in Watts (\(\watt\)), \(\rho\) is the air density in \(\kilo\gram\per\meter\cubed\), \(A\) is the area of the blade sweep in \(\meter\squared\), \(l\) is the length of a blade in \(\meter\), \(v\) is the wind velocity in \(\meter\per\second\), and \(C_p\) is the unitless power coefficient.

(6.3)#\[P = \frac{1}{2} \rho A v^3 C_p\]
(6.4)#\[A = \pi l^2\]

It is assumed that the wind turbine is located offshore and is situated at sea-level, so the air density is \(\qty{1.23}{\kilo\gram\per\meter\cubed}\). The \(C_p\) value is unique to every turbine and is a function of wind speed that the turbine is operating in; we will assume it is a constant of \(0.4\) in our model.

The energy company wishes to use the energy generated by the wind turbine to power a heater for a warehouse. The equation for energy \(E\) in Joules (\(\J\)), delivered by a Power \(P\) in Watts, for a time \(t\) in seconds is given by (6.5).

(6.5)#\[E = P t\]

Task Instructions#

Start by creating a flowchart for each function described below in your previously created PDF file. Then implement them in Python.

Write a Python function named turbine_power in a separate file named py2_team_3_turbine_power_teamnumber.py that calculates and returns the power generated by the wind turbine. This function should take values for air density, blade_length, velocity of the wind, and the power coefficient as its arguments in this order. Since wind turbines have a maximum wind speed they can operate at, which for this turbine is \(\qty{20}{\meter\per\second}\), add some logic to display an error message when the wind speed is above this value. Also, set the power output to zero as the turbine is not operating. Similarly, turbines have a minimum required wind speed, in this case \(\qty{4}{\meter\per\second}\), that they need to start to operate. If the wind speed is below this value, also add an error message and set the power to zero.

Next, write a function named heater in a separate file named py2_team_3_heater_teamnumber.py that calculates and returns the amount of energy delivered by the heater in a given span of time. This function should take the power output from the turbine in Joules and an amount of time in seconds as arguments.

Finally, starting from the Python template, write a program named py2_team_3_main_teamnumber.py that imports your turbine_power and heater functions. Your program should assign the following values to variables at the beginning of your main function.

  • \(l = \qty{50}{\meter}\)

  • \(\rho = \qty{1.23}{\kilo\gram\per\meter\cubed}\)

  • \(C_p = 0.4\)

The values for wind velocity in \(\meter\per\second\) and the time in \(\hour\) should be collected from the user. Once all the values are defined, use them to to determine the power generated and the amount of heat output by calling your imported functions with the appropriate arguments. Print the results on the screen.

Sample Output#

All measurements are in degrees. Use the values in Table 6.5 below to test your program.

Table 6.5 Test Cases#

Case

Wind Velocity (\(\meter\per\second\))

Time (\(\hour\))

1

6

2

2

3.0

3.5

3

23

0.5

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 py2_team_3_main_teamnumber.py Enter the velocity in m/s: 6 Enter the time in hours: 2 The power generated by the turbine is 417329.2 W The amount of heat output by the heater is 3004770010.3 J

Case 2 Sample Output

$ python3 py2_team_3_main_teamnumber.py Enter the velocity in m/s: 3.0 Enter the time in hours: 3.5 Error, velocity is too low, no power generated The power generated by the turbine is 0.0 W The amount of heat output by the heater is 0.0 J

Case 3 Sample Output

$ python3 py2_team_3_main_teamnumber.py Enter the velocity in m/s: 23 Enter the time in hours: 0.5 Error, velocity is too high, no power generated The power generated by the turbine is 0.0 W The amount of heat output by the heater is 0.0 J