Homework 16

Problem 1

Write a function as for problem 6 of HW 14. That is, write a function for the Haaland evaluating $f(Re, \epsilon/D)$, where the Haaland equation is given by

$$\frac{1}{\sqrt{f}} = -1.8\log_10\left[ \left(\frac{\epsilon/D}{3.7}\right)^{1.11} + \frac{6.9}{Re}\right].$$

Create a matrix holding $f$ values with rows corresponding to $Re$ taking on 100 values uniformly spaced on a log scale between 1000 and $1\times 10^8$, and columns corresponding to $\epsilon/D$ values of 0.0, 0.0001, 0.001, and 0.01.

Part a

On a single plot, plot $f$ versus $Re$ for each value of $\epsilon/D$. Use a logscale for $Re$ rather than plotting $f$ versus $\log_{10}Re$. Use lines with different colors. Include axes labels and a legend.

Part b

Use np.savetxt to save the data to a file called haaland.dat with data separated only by spaces, and fomatted using scientific notation with five decimal places. The first column should be $Re$, and subsequent columns hold $f(Re, \epsilon/D)$ for the different values of $\epsilon/D$. Include a header that indicates the variables so that the first line of the file is something like # Re, f(Re,e/D=0), f(Re,e/D=0.0001), etc. Turn in the haaland.dat file as part of your solution.

You should verify your file visually in a text editor and make sure it looks good and has the correct number of lines.

Problem 2

A reaction among species $A$, $B$, and $C$ is written as

$$A \rightarrow B \rightarrow C.$$

The first reaction has rate constant $k_1=2$ and the second reaction has rate constant $k_2=1.$

The ODEs for the concentrations of the three species are \begin{align} \frac{dA}{dt} &= -k_1A, \\ \frac{dB}{dt} &= k_1A - k_2B, \\ \frac{dC}{dt} &= k_2B. \end{align}

The initial concentrations are $A_0=1$, $B_0=C_0=0$. Evaluate $A$, $B$, and $C$ at discrete times using the Explicit Euler method. Solve to a time of 5 seconds using $\Delta t=0.1$.

Part a

On a single plot, show the concentrations of the three species versus time. Be sure to include axis labels and a legend. Use lines with different linestyles for each curve. Don’t include a chart title.

Part b

Use np.savetxt to save a data file of the results called concentrations_b.dat and turn in the file. Make time the first column, followed by concentrations of $A$, $B$, and $C$. Include an appropriate header. Format the using five decimal places using either floating point or scientific notation.

Part c

Redo Part b, but use basic file I/O using a with statement, not np.savetxt. Here is a reference to using format strings that were briefly introduced in class. Save the file as concentrations_c.dat and turn in the file.

Part d

Use np.loadtxt to load concentrations_b.dat. Assume the time was saved in seconds. Replot the data with time changed to minutes, but only plot up to $t=2.5$ seconds.

Part e

Use basic file I/O using a with statement, not np.loadtxt to load concentrations_c.dat. Assume the time was saved in seconds. Replot the data with the time in hours; plot up to $t=4$ seconds. Use symbols, not lines for the data; use triangles, squares, and circles for $A$, $B$, and $C$, respectively.