|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# MHKiT WEC-Sim Example\n", |
| 8 | + "\n", |
| 9 | + "This notebook demonstrates using the MHKiT wave module and [WEC-Sim](http://wec-sim.github.io/WEC-Sim/index.html) together to perform a resource characterization study in MHKiT, simulate representative cases with WEC-Sim, and visualize the results in MHKiT to estimate MAEP (Mean Annual Energy Production).\n", |
| 10 | + "\n", |
| 11 | + " 1. Characterize the available resource at a location\n", |
| 12 | + " - See the PacWave example notebook\n", |
| 13 | + " 2. Write a WEC-Sim batch file for the given clusters\n", |
| 14 | + " 3. Simulate the device *in WEC-Sim*.\n", |
| 15 | + " - Ensure that the spectra used in WEC-Sim is identical to the one used in MHKiT.\n", |
| 16 | + " 4. Load WEC-Sim batch results\n", |
| 17 | + " 5. Assess results and visualize quantities of interest\n", |
| 18 | + "\n", |
| 19 | + "This example uses WEC-Sim to simulate the [Oscillating Surge Wave Energy Converter (OSWEC)](https://wec-sim.github.io/WEC-Sim/main/user/tutorials.html#oscillating-surge-wec-oswec), a flap-type device.\n", |
| 20 | + "\n", |
| 21 | + "Start by importing MHKiT and the necessary python packages (e.g.`scipy.io`, `matplotlib.pyplot`, `pandas`, `numpy`)." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [ |
| 29 | + { |
| 30 | + "ename": "ModuleNotFoundError", |
| 31 | + "evalue": "No module named 'mhkit'", |
| 32 | + "output_type": "error", |
| 33 | + "traceback": [ |
| 34 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 35 | + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", |
| 36 | + "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmhkit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m wave\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mscipy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msio\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n", |
| 37 | + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'mhkit'" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
| 41 | + "source": [ |
| 42 | + "from mhkit import wave\n", |
| 43 | + "import scipy.io as sio\n", |
| 44 | + "import matplotlib.pyplot as plt\n", |
| 45 | + "import pandas as pd\n", |
| 46 | + "import numpy as np" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "## 1. Characterize the available resource at a location\n", |
| 54 | + "\n", |
| 55 | + "This example will use an abbreviated version of `PacWave_resource_characterization_example.ipynb`. \n", |
| 56 | + "For full details on downloading, calculating, and visualizing the k-means clusters representation of the site's wave resouce, see that example.\n", |
| 57 | + "\n", |
| 58 | + "We select the N=32 cluster as it's total energy flux is closet to the total energy flux of the site considering all wave conditions. We will load the PacWave example output, which can be easily saved after running the example with the command `results[32].to_csv(\"pacwave_cluster_32.csv\")`. We will start this example by reading in that csv output and formatting it for WEC-Sim." |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 2, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "ename": "NameError", |
| 68 | + "evalue": "name 'pd' is not defined", |
| 69 | + "output_type": "error", |
| 70 | + "traceback": [ |
| 71 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 72 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 73 | + "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241m.\u001b[39mread_csv(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata/wave/pacwave_cluster_32.csv\u001b[39m\u001b[38;5;124m\"\u001b[39m, index_col\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n\u001b[0;32m 2\u001b[0m results\n", |
| 74 | + "\u001b[1;31mNameError\u001b[0m: name 'pd' is not defined" |
| 75 | + ] |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "results = pd.read_csv(\"data/wave/pacwave_cluster_32.csv\", index_col=0)\n", |
| 80 | + "results" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "## 2. Write a WEC-Sim batch file for the given clusters\n", |
| 88 | + "\n", |
| 89 | + "[WEC-Sim MCR](https://wec-sim.github.io/WEC-Sim/main/user/advanced_features.html#multiple-condition-runs-mcr) (multiple condition run) files should contain a structure `mcr` that contains two variables: `header` and `cases`. Each column of `header` and `cases` denotes a variable and it's value respectively. Each row is another simulation. WEC-Sim defines waves using the significant wave height and peak period. We will isolate these values from the results of the cluster analysis and create a dictionary that is written to the `.mat` file." |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 3, |
| 95 | + "metadata": { |
| 96 | + "scrolled": true |
| 97 | + }, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "ename": "NameError", |
| 101 | + "evalue": "name 'results' is not defined", |
| 102 | + "output_type": "error", |
| 103 | + "traceback": [ |
| 104 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 105 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 106 | + "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m ws_mcr_cases \u001b[38;5;241m=\u001b[39m \u001b[43mresults\u001b[49m[[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHm0\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTp\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\n\u001b[0;32m 2\u001b[0m ws_mcr_header \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwaves.height\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwaves.period\u001b[39m\u001b[38;5;124m\"\u001b[39m], dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mobject\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 3\u001b[0m ws_mcr_out \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmcr\u001b[39m\u001b[38;5;124m'\u001b[39m: {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mheader\u001b[39m\u001b[38;5;124m'\u001b[39m: ws_mcr_header, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcases\u001b[39m\u001b[38;5;124m'\u001b[39m: ws_mcr_cases}}\n", |
| 107 | + "\u001b[1;31mNameError\u001b[0m: name 'results' is not defined" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "source": [ |
| 112 | + "ws_mcr_cases = results[[\"Hm0\",\"Tp\"]]\n", |
| 113 | + "ws_mcr_header = np.array([\"waves.height\",\"waves.period\"], dtype='object')\n", |
| 114 | + "ws_mcr_out = {'mcr': {'header': ws_mcr_header, 'cases': ws_mcr_cases}}\n", |
| 115 | + "sio.savemat('mcr_mhkit.mat', ws_mcr_out)" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "markdown", |
| 120 | + "metadata": {}, |
| 121 | + "source": [ |
| 122 | + "## 3. Simulate the device *in WEC-Sim*\n", |
| 123 | + "\n", |
| 124 | + "Now that the MCR file is created, we need to go simulate WEC performance in these wave conditions using WEC-Sim. To recreate the data used in the next step, use the created MCR file with WEC-Sim's [OSWEC example](https://github.com/WEC-Sim/WEC-Sim/tree/main/examples/OSWEC). For an accurate comparison to the power calculated in the resource characterization, we should ensure that the WEC-Sim cases use irregular JONSWAP wave spectra as in the PacWave example.\n", |
| 125 | + "\n", |
| 126 | + "For convenience in this demonstration, we enforce OSWEC model stability in the extreme wave conditions by arbitrarily applying a large PTO stiffness and damping:\n", |
| 127 | + "```\n", |
| 128 | + "pto(1).stiffness = 1e5;\n", |
| 129 | + "pto(1).damping = 5e7;\n", |
| 130 | + "```\n", |
| 131 | + "\n", |
| 132 | + "To reduce the amount of extranenous data saved for this example, we limit the WEC-Sim output to the PTO's power output in the `userDefinedFunctions.m` script:\n", |
| 133 | + "```\n", |
| 134 | + "if exist('imcr','var')\n", |
| 135 | + " if imcr == 1\n", |
| 136 | + " nmcr = size(mcr.cases,1);\n", |
| 137 | + " power = nan(1, nmcr);\n", |
| 138 | + " end\n", |
| 139 | + "\n", |
| 140 | + " iRampEnd = simu.rampTime./simu.dtOut + 1;\n", |
| 141 | + " power(imcr) = -mean(output.ptos(1).powerInternalMechanics(iRampEnd:end,5));\n", |
| 142 | + "\n", |
| 143 | + " if imcr == nmcr\n", |
| 144 | + " % Save output\n", |
| 145 | + " save('mcr_mhkit_power.mat', 'power');\n", |
| 146 | + " end\n", |
| 147 | + "end\n", |
| 148 | + "```" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "markdown", |
| 153 | + "metadata": {}, |
| 154 | + "source": [ |
| 155 | + "## 4. Load WEC-Sim batch results\n", |
| 156 | + "\n", |
| 157 | + "Note that in this example we do not save the entire WEC-Sim `output` structure for each case. See the `wecsim_example.ipynb` for information on loading that WEC-Sim data. Here the output is one array of average power output that we will load and compare to the resource characterization.\n", |
| 158 | + "\n", |
| 159 | + "Note that the power output \\[W\\] is significantly larger than the energy flux \\[W/m\\] due to the width of the OSWEC. " |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": 4, |
| 165 | + "metadata": { |
| 166 | + "scrolled": true |
| 167 | + }, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "ename": "NameError", |
| 171 | + "evalue": "name 'sio' is not defined", |
| 172 | + "output_type": "error", |
| 173 | + "traceback": [ |
| 174 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 175 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 176 | + "Cell \u001b[1;32mIn[4], line 5\u001b[0m\n\u001b[0;32m 2\u001b[0m filename \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m./data/wave/mcr_mhkit_power.mat\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Load data using the `wecsim.read_output` function which returns a dictionary of Datasets.\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m wecsim_data \u001b[38;5;241m=\u001b[39m \u001b[43msio\u001b[49m\u001b[38;5;241m.\u001b[39mloadmat(filename)\n\u001b[0;32m 6\u001b[0m results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mP\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m wecsim_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpower\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m 7\u001b[0m results\n", |
| 177 | + "\u001b[1;31mNameError\u001b[0m: name 'sio' is not defined" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
| 181 | + "source": [ |
| 182 | + "# Relative location and filename of simulated WEC-Sim data (run with mooring)\n", |
| 183 | + "filename = \"./data/wave/mcr_mhkit_power.mat\"\n", |
| 184 | + "\n", |
| 185 | + "# Load data using the `wecsim.read_output` function which returns a dictionary of Datasets.\n", |
| 186 | + "wecsim_data = sio.loadmat(filename)\n", |
| 187 | + "results['P'] = wecsim_data['power'][0]\n", |
| 188 | + "results" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "markdown", |
| 193 | + "metadata": {}, |
| 194 | + "source": [ |
| 195 | + "## 5. Assess results and visualize quantities of interest\n", |
| 196 | + "Now that we have loaded the OSWEC's modeled power, we can assess it's performance relative to the incoming wave and calculate the mean annual energy production (MAEP) using MHKiT." |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": 5, |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [ |
| 204 | + { |
| 205 | + "ename": "NameError", |
| 206 | + "evalue": "name 'wave' is not defined", |
| 207 | + "output_type": "error", |
| 208 | + "traceback": [ |
| 209 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 210 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 211 | + "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCW\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mwave\u001b[49m\u001b[38;5;241m.\u001b[39mperformance\u001b[38;5;241m.\u001b[39mcapture_width(results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mP\u001b[39m\u001b[38;5;124m'\u001b[39m], results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mJ\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m 2\u001b[0m oswec_width \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m18\u001b[39m\n\u001b[0;32m 3\u001b[0m results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCWR\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCW\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m/\u001b[39m oswec_width\n", |
| 212 | + "\u001b[1;31mNameError\u001b[0m: name 'wave' is not defined" |
| 213 | + ] |
| 214 | + } |
| 215 | + ], |
| 216 | + "source": [ |
| 217 | + "results['CW'] = wave.performance.capture_width(results['P'], results['J'])\n", |
| 218 | + "oswec_width = 18\n", |
| 219 | + "results['CWR'] = results['CW'] / oswec_width\n", |
| 220 | + "results" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 6, |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [ |
| 228 | + { |
| 229 | + "ename": "NameError", |
| 230 | + "evalue": "name 'wave' is not defined", |
| 231 | + "output_type": "error", |
| 232 | + "traceback": [ |
| 233 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 234 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 235 | + "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m MAEP \u001b[38;5;241m=\u001b[39m \u001b[43mwave\u001b[49m\u001b[38;5;241m.\u001b[39mperformance\u001b[38;5;241m.\u001b[39mmean_annual_energy_production_matrix(results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCW\u001b[39m\u001b[38;5;124m'\u001b[39m], results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mJ\u001b[39m\u001b[38;5;124m'\u001b[39m], results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mweights\u001b[39m\u001b[38;5;124m'\u001b[39m]) \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m1000\u001b[39m \u001b[38;5;66;03m# kWh\u001b[39;00m\n\u001b[0;32m 2\u001b[0m MAEP \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mround(MAEP, \u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m.\u001b[39mitem()\n\u001b[0;32m 3\u001b[0m MAEP\n", |
| 236 | + "\u001b[1;31mNameError\u001b[0m: name 'wave' is not defined" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "source": [ |
| 241 | + "MAEP = wave.performance.mean_annual_energy_production_matrix(results['CW'], results['J'], results['weights']) / 1000 # kWh\n", |
| 242 | + "MAEP = np.round(MAEP, 0).item()\n", |
| 243 | + "MAEP" |
| 244 | + ] |
| 245 | + } |
| 246 | + ], |
| 247 | + "metadata": { |
| 248 | + "kernelspec": { |
| 249 | + "display_name": "Python 3 (ipykernel)", |
| 250 | + "language": "python", |
| 251 | + "name": "python3" |
| 252 | + }, |
| 253 | + "language_info": { |
| 254 | + "codemirror_mode": { |
| 255 | + "name": "ipython", |
| 256 | + "version": 3 |
| 257 | + }, |
| 258 | + "file_extension": ".py", |
| 259 | + "mimetype": "text/x-python", |
| 260 | + "name": "python", |
| 261 | + "nbconvert_exporter": "python", |
| 262 | + "pygments_lexer": "ipython3", |
| 263 | + "version": "3.10.9" |
| 264 | + } |
| 265 | + }, |
| 266 | + "nbformat": 4, |
| 267 | + "nbformat_minor": 4 |
| 268 | +} |
0 commit comments