{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Output Simulation Results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In AMS, the results can be output in different formats.\n", "\n", "One is the plain-text format, where it lists all solved dispatch requests.\n", "Another is the CSV format, where the dispatch results are exported to a CSV file." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import ams\n", "\n", "import datetime\n", "\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last run time: 2024-02-28 12:15:03\n", "ams:0.9.0\n" ] } ], "source": [ "print(\"Last run time:\", datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", "\n", "print(f'ams:{ams.__version__}')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import case and run simulation" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/home/jwang175/miniconda3/envs/amsre/lib/python3.9/site-packages/ams/cases/5bus/pjm5bus_demo.xlsx\"...\n", "Input file parsed in 0.2256 seconds.\n", "Zero line rates detacted in rate_a, rate_b, rate_c, adjusted to 999.\n", "If expect a line outage, please set 'u' to 0.\n", "System set up in 0.0144 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('5bus/pjm5bus_demo.xlsx'),\n", " setup=True,\n", " no_output=False,)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Routine initialized in 0.0266 seconds.\n", "DCOPF solved as optimal in 0.0259 seconds, converged after 9 iterations using solver ECOS.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.run(solver='ECOS')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Report to plain text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, the system method ``report()`` can generated a plain-text report of the simulation results.\n", "\n", "If multiple simulation runs are performed, the report will contain all of them." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Report saved to \"pjm5bus_demo_out.txt\" in 0.0022 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.report()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The report is like:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AMS 0.9.0\n", "Copyright (C) 2023-2024 Jinning Wang\n", "\n", "AMS comes with ABSOLUTELY NO WARRANTY\n", "Case file: /home/jwang175/miniconda3/envs/amsre/lib/python3.9/site-packages/ams/cases/5bus/pjm5bus_demo.xlsx\n", "Report time: 02/28/2024 12:15:03 PM\n", "\n", "\n", "========== System Statistics ==========\n", "Buses 5\n", "Generators 4\n", "Loads 3\n", "Shunts 0\n", "Lines 7\n", "Transformers 0\n", "Areas 3\n", "Regions 2\n", "\n", "============================== DCOPF ==============================\n", " P (p.u.)\n", "\n", "Generation 10\n", "Load 10\n", "\n", "Bus DATA:\n", " Name aBus (rad)\n", "\n", "Bus_1 A 0.006759\n", "Bus_2 B -0.013078\n", "Bus_3 C 0.004073\n", "Bus_4 D -0.014101\n", "Bus_5 E 0.006747\n", "\n", "Line DATA:\n", " Name plf (p.u.)\n", "\n", "Line_0 Line AB 0.70595\n", "Line_1 Line AD 0.68617\n", "Line_2 Line AE 0.001925\n", "Line_3 Line BC -1.5881\n", "Line_4 Line CD 0.61191\n", "Line_5 Line DE -0.70193\n", "Line_6 Line AB2 0.70595\n", "\n", "StaticGen DATA:\n", " Name pg (p.u.)\n", "\n", "PV_1 Alta 2.1\n", "PV_3 Solitude 5.2\n", "PV_5 Brighton 0.7\n", "Slack_4 Sundance 2\n", "\n", "\n" ] } ], "source": [ "report_file = \"pjm5bus_demo_out.txt\"\n", "\n", "with open(report_file, 'r') as file:\n", " report_content = file.read()\n", "\n", "print(report_content)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export to CSV" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dispatch simulation can also be exported to a CSV file." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Routine initialized in 0.0541 seconds.\n", "ED solved as optimal in 0.0516 seconds, converged after 9 iterations using solver ECOS.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.ED.run(solver='ECOS')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'pjm5bus_demo_ED.csv'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.ED.export_csv()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('pjm5bus_demo_ED.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the exported CSV file, each row represents a timeslot, and each column represents a variable." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Timepg PV_1pg PV_3pg PV_5pg Slack_4aBus Bus_1aBus Bus_2aBus Bus_3aBus Bus_4aBus Bus_5
0EDT12.13.230.62.00.008403-0.014115-0.005731-0.0079500.008663
1EDT22.12.860.62.00.008755-0.014395-0.007696-0.0068560.009146
2EDT32.12.530.62.00.009069-0.014645-0.009448-0.0058790.009578
3EDT42.12.380.62.00.009213-0.014758-0.010244-0.0054350.009775
4EDT52.12.300.62.00.009290-0.014817-0.010667-0.0051970.009881
5EDT62.12.360.62.00.009233-0.014772-0.010349-0.0053750.009802
\n", "
" ], "text/plain": [ " Time pg PV_1 pg PV_3 pg PV_5 pg Slack_4 aBus Bus_1 aBus Bus_2 \\\n", "0 EDT1 2.1 3.23 0.6 2.0 0.008403 -0.014115 \n", "1 EDT2 2.1 2.86 0.6 2.0 0.008755 -0.014395 \n", "2 EDT3 2.1 2.53 0.6 2.0 0.009069 -0.014645 \n", "3 EDT4 2.1 2.38 0.6 2.0 0.009213 -0.014758 \n", "4 EDT5 2.1 2.30 0.6 2.0 0.009290 -0.014817 \n", "5 EDT6 2.1 2.36 0.6 2.0 0.009233 -0.014772 \n", "\n", " aBus Bus_3 aBus Bus_4 aBus Bus_5 \n", "0 -0.005731 -0.007950 0.008663 \n", "1 -0.007696 -0.006856 0.009146 \n", "2 -0.009448 -0.005879 0.009578 \n", "3 -0.010244 -0.005435 0.009775 \n", "4 -0.010667 -0.005197 0.009881 \n", "5 -0.010349 -0.005375 0.009802 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[:, :10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cleanup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remove the output files." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "os.remove('pjm5bus_demo_out.txt')\n", "os.remove('pjm5bus_demo_ED.csv')" ] } ], "metadata": { "kernelspec": { "display_name": "ams", "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.9.18" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d2b3bf80176349caa68dc4a3c77bd06eaade8abc678330f7d1c813c53380e5d2" } } }, "nbformat": 4, "nbformat_minor": 2 }