Skip to content
Snippets Groups Projects
Commit 8ed41533 authored by Jenni Rinker's avatar Jenni Rinker
Browse files

remove transience at beginning

parent 0f85e97e
No related branches found
No related tags found
No related merge requests found
Pipeline #29026 passed
%% Cell type:markdown id:fa60cf2d tags:
# Loading and plotting
One of the most common exercises in engineering is to load saved data and visualize it in some way. In this demo, we will be loading examining a time-marching response to turbulent wind.
%% Cell type:raw id:f46c763b tags:
To do the exercises in this notebook, please download the results file and save it as ``res_12_ms_TI_0.1.txt``: :download:`download file <res_12_ms_TI_0.1.txt>`
%% Cell type:markdown id:18f29da9 tags:
### Exercises for the reader
1. Open the results file in a text editor of your choice. How many columns are there? What does each column represent?
2. Scroll through the results file and find out what the wind speed is at $t=60$ s. What are the responses at $t = 345$ s?
3. Make a function that loads the results file and returns vectors t, u, x1 and x2.
3. Make a function that (1) loads the results file, (2) removes 60 seconds at the beggining to eliminate transience and, and (3) returns vectors t, u, x1 and x2.
4. Write a script that finds and prints the wind speed and responses at a selected time. Verify the output of your script with your answer to Exercise 2.
5. Make a function that plots Turbie results versus time. The figure should have two subplots: the top showing the wind speed and the bottom showing the blade and tower deflections. Label the figure as you see fit. **Bonus points!** Allow the user to specify the time span (i.e., x-limits) of the plot, so they can zoom into a specific time if they like.
6. Use your function to plot the given results file versus time. If you did the bonus exercise in Exercise 5, zoom in from 60 to 660. Your plot should look something like this:
<img src="figures/2-plot-results.png" alt="Example results" style="width: 600px;"/>
%% Cell type:markdown id:4d721a00 tags:
### Answers to the exercises
#### Exercise 1
There are four columns, and they represent time, wind speed, blade deflection and tower deflection.
#### Exercise 2
The wind speed at 60 s is 11.665 m/s. The blade deflection at 345 s is 0.854 m, whereas the tower deflection is 0.552 m.
#### Exercise 3
%% Cell type:code id:aa95c965 tags:
``` python
import numpy as np
def load_results(path):
def load_results(path, t_start=60):
"""Load turbie results from text file.
Returns 4 [nt] arrays: t, u, x1 and x2.
"""
res_arr = np.loadtxt(path, skiprows=1)
t, u, x1, x2 = res_arr.T
return t, u, x1, x2
t, u, x1, x2 = np.loadtxt(path, skiprows=1, unpack=True)
mask = t >= t_start
return t[mask], u[mask], x1[mask], x2[mask]
```
%% Cell type:markdown id:a2c04fc5 tags:
#### Exercise 4
%% Cell type:code id:eccfa876 tags:
``` python
path = 'res_12_ms_TI_0.1.txt' # path to file
t, u, x1, x2 = load_results(path) # load (nt x 4) array
def find_response(t_p):
"""Find response closest to given time point"""
t_idx = np.argmin(np.abs(t - t_p)) # find index closest to time point
return u[t_idx], x1[t_idx], x2[t_idx]
t_p = 60 # time point of interest
u_pt = find_response(t_p)[0] # wind speed is first element returned
print(f'The wind speed at t = {t_p} s is {u_pt} m/s.')
t_p = 345 # time point of interest
x1_pt, x2_pt = find_response(t_p)[1:] # second and third elements
print(f'The blade deflection at t = {t_p} s is {x1_pt} m and the tower deflection is {x2_pt}.')
```
%% Output
The wind speed at t = 60 s is 11.665 m/s.
The blade deflection at t = 345 s is 0.854 m and the tower deflection is 0.552.
%% Cell type:markdown id:0a1ed1e7 tags:
#### Exercise 5
%% Cell type:code id:7f0379d7 tags:
``` python
import matplotlib.pyplot as plt
def plot_results(t, u, x1, x2, tspan=None):
"""Make a plot of the Turbie results array.
Returns the figure and axes handles.
"""
if tspan is None: # if tspan not given, take whole range
tspan = [t[0], t[-1]]
# mask the data so it falls within tspan
mask = (t >= tspan[0]) & (t <= tspan[1])
t = t[mask]
u = u[mask]
x1 = x1[mask]
x2 = x2[mask]
fig, axs = plt.subplots(2, 1, clear=True, figsize=(10, 4))
ax = axs[0] # subplot 1: wind speed
ax.plot(t, u)
ax.set_ylabel('Wind speed [m/s]')
ax.set_xlim(tspan)
ax = axs[1] # subplot 2: blade and tower deflection
ax.plot(t, x1, label='Blades')
ax.plot(t, x2, label='Tower')
ax.set_ylabel('Deflection [m]')
ax.set_xlabel('Time [s]')
ax.set_xlim(tspan)
ax.legend()
plt.tight_layout() # make sure the axes fit the figure
return fig, axs
```
%% Cell type:markdown id:b7cf2e34 tags:
#### Exercise 6
%% Cell type:code id:a264dd5b tags:
``` python
fig, axs = plot_results(t, u, x1, x2, tspan=[60, 660])
fig.suptitle('Results for file ' + path) # add title
fig.tight_layout() # rescale axes to fit nicely
```
%% Output
%% Cell type:markdown id:b9bf2cfc tags:
......
......@@ -48,13 +48,14 @@ def get_turbie_system_matrices(turbie_path=None):
return M, C, K
def load_results(path):
"""Load turbie results from text file.
def load_results(path, t_start=0):
"""Load turbie results from text file.
Remove t_start from beginning.
Returns 4 [nt] arrays: t, u, x1 and x2.
"""
res_arr = np.loadtxt(path, skiprows=1)
t, u, x1, x2 = res_arr.T
return t, u, x1, x2
t, u, x1, x2 = np.loadtxt(path, skiprows=1, unpack=True)
mask = t >= t_start
return t[mask], u[mask], x1[mask], x2[mask]
def load_wind(path):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment