{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 8. Exercise solution\n",
"\n",
"The full script is provided below."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"~~~python \n",
"import pandas as pd\n",
"import numpy as np\n",
"from scipy.interpolate import griddata\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"\n",
"\n",
"# Set name of Excel file to read containing known points\n",
"file_known = 'known_points.xlsx'\n",
"\n",
"# Set name of sheet to read from Excel file\n",
"sheet_known = 'Sheet1'\n",
"\n",
"# Read data from Excel sheet into a dataframe\n",
"df = pd.read_excel(file_known, sheet_name=sheet_known, skiprows=7)\n",
"\n",
"# Extract column names starting with 'Y' into new dataframe of known Y-coords\n",
"df_y = df[df.columns[df.columns.str.startswith('Y')]]\n",
"\n",
"# Extract column names starting with 'Z' into new dataframe of known Z-coords\n",
"df_z_known = df[df.columns[df.columns.str.startswith('Z')]]\n",
"\n",
"# Flatten dataframe values into 1D array (matri format -> vector format)\n",
"y_known = df_y.values.flatten()\n",
"z_known = df_z_known.values.flatten()\n",
"\n",
"# Extract known x-values\n",
"x_known = df['X']\n",
"\n",
"# Create X-array by repeating itself as many times as there are Y-columns\n",
"# This will create matching(x, y)-points between arrays x and y\n",
"x_known = np.repeat(x_known, len(df_y.columns))\n",
"\n",
"# Mirror known y-values and add corresponding x- and y-values\n",
"x_known = np.append(x_known, x_known)\n",
"y_known = np.append(y_known, -y_known)\n",
"z_known = np.append(z_known, z_known)\n",
"\n",
"# Arrange known (x, y) points to fit input for interpolation\n",
"xy_known = np.array(list(zip(x_known, y_known)))\n",
"\n",
"# Set names and read Excel file with nodes to be interpolated\n",
"file_nodes = 'points_to_be_interpolated.xlsx'\n",
"sheet_nodes = 'XLSX-Export'\n",
"df_nodes = pd.read_excel(file_nodes, sheet_name=sheet_nodes)\n",
"\n",
"# Extract x- and y-coordinates of nodes to be interpolated\n",
"x_nodes = df_nodes['X [m]']\n",
"y_nodes = df_nodes['Y [m]']\n",
"\n",
"# Extract node numbers for points to be interpolated\n",
"node_no = df_nodes['NR']\n",
"\n",
"# Perform interpolation calculation\n",
"z_interpolated = griddata(xy_known, z_known, (x_nodes, y_nodes), method='cubic')\n",
"\n",
"\n",
"####################\n",
"### Exercise 1.2 ###\n",
"####################\n",
"# Create figure object\n",
"fig = plt.figure()\n",
"\n",
"# Create axis object for 3D plot\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"\n",
"# Plot known points as 3D scatter plot (ax.scatter(...))\n",
"ax.scatter(x_known, y_known, z_known, '-.', color='limegreen')\n",
"\n",
"# Plot interpolated points as 3D scatter plot\n",
"ax.scatter(x_nodes, y_nodes, z_interpolated,\n",
" '.', color='cornflowerblue', s=0.1)\n",
"\n",
"# Show figure\n",
"plt.show()\n",
"\n",
"\n",
"####################\n",
"### Exercise 1.3 ###\n",
"####################\n",
"# Write Sofistik input code to .dat-file for applying the interpolated z-values as\n",
"# imposed displacement load (settlement) in all points (x, y)\n",
"with open(f'generated_file.dat', 'w') as file:\n",
"\n",
" # Write the 'static' text to file\n",
" file.write('''+PROG SOFILOAD \n",
"\n",
"LC 25 type 'P' fact 1.0 facd 0.0 titl 'LT settlement all nodes' \\n''')\n",
"\n",
" # Write the 'variable' text to file with node number/settlement pairs\n",
" for node, settlement in zip(node_no, z_interpolated):\n",
" file.write(f' POIN NODE {node} WIDE 0 TYPE WZZ {settlement} \\n')\n",
"\n",
" # Write 'static' END statement to file\n",
" file.write('END')\n",
"\n",
"~~~"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# End of exercises\n",
"\n",
"*The cell below is for setting the style of this document. It's not part of the exercises.*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"HTML(''.format(open('../css/cowi.css').read()))"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Table of Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "165px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}