{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Export 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 pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import case and run simulation" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/Users/jinningwang/work/ams/ams/cases/5bus/pjm5bus_demo.xlsx\"...\n", "Input file parsed in 0.0761 seconds.\n", "Zero Line parameters detected, adjusted to default values: rate_b, rate_c.\n", "All bus type are PQ, adjusted given load and generator connection status.\n", "System set up in 0.0021 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": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Building system matrices\n", "Parsing OModel for \n", "Evaluating OModel for \n", "Finalizing OModel for \n", " initialized in 0.0100 seconds.\n", " solved as optimal in 0.0066 seconds, converged in 9 iterations with CLARABEL.\n", "Report saved to \"pjm5bus_demo_out.txt\" in 0.0010 seconds.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " RParam should have all non-negative values.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.run(solver='CLARABEL')" ] }, { "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": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Report saved to \"pjm5bus_demo_out.txt\" in 0.0015 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.report()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The report is like:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AMS 1.1.0.post75.dev0+gcc96e89c\n", "Copyright (C) 2023-2025 Jinning Wang\n", "\n", "AMS comes with ABSOLUTELY NO WARRANTY\n", "Case file: /Users/jinningwang/work/ams/ams/cases/5bus/pjm5bus_demo.xlsx\n", "Report time: 12/21/2025 12:00:06 AM\n", "\n", "\n", "========== System Statistics ==========\n", "Buses 5\n", "Generators 5\n", "Loads 3\n", "Shunts 0\n", "Lines 7\n", "Transformers 0\n", "Areas 3\n", "Zones 5\n", "\n", "============================== DCOPF ==============================\n", " Objective Function\n", "\n", "Value in $ 9.536\n", "\n", " P (p.u.)\n", "\n", "Generation 10\n", "Load 10\n", "\n", "Bus DATA:\n", " Name vBus (p.u.) aBus (rad) pi ($/p.u.)\n", "\n", "0 A 1 0.023989 0.77623\n", "1 B 1 0.034668 0.1\n", "2 C 1 0.013068 3\n", "3 D 1 -0 1.5705\n", "4 E 1 0.022896 0.91705\n", "\n", "Line DATA:\n", " Name plf (p.u.) mu1 ($/p.u.) mu2 ($/p.u.)\n", "\n", "Line_1 Line AB -0.38001 0 0\n", "Line_2 Line AD 0.78912 0 0\n", "Line_3 Line AE 0.17089 0 0\n", "Line_4 Line BC 2 0 3.4198\n", "Line_5 Line CD 0.43998 0 0\n", "Line_6 Line DE -0.77089 0 0\n", "Line_7 Line AB2 -0.38001 0 0\n", "\n", "StaticGen DATA:\n", " Name pg (p.u.) pmaxe (p.u.) pmine (p.u.)\n", "\n", "PV_1 Alta 0.2 2.1 0.2\n", "PV_3 Solitude 1.44 5.2 0.5\n", "PV_5 Brighton 0.6 6 0.6\n", "PV_2 PV 2 5.76 99 -99\n", "Slack_4 Sundance 2 2 0.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": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing OModel for \n", "Evaluating OModel for \n", "Finalizing OModel for \n", " initialized in 0.0133 seconds.\n", " solved as optimal in 0.0194 seconds, converged in 14 iterations with CLARABEL.\n", "Report saved to \"pjm5bus_demo_out.txt\" in 0.0102 seconds.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " RParam should have all non-negative values.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.ED.run(solver='CLARABEL')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'pjm5bus_demo_ED.csv'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_csv = sp.ED.export_csv()\n", "\n", "out_csv" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(out_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": 10, "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", " \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", " \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", " \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 PV_2pg Slack_4vBus 0vBus 1vBus 2vBus 3
0EDT10.20.5000000.64.8289361.8010641.01.01.01.0
1EDT20.20.5000000.64.8258381.4341621.01.01.01.0
2EDT30.20.5000000.64.8230741.1069261.01.01.01.0
3EDT40.20.5000000.64.8218180.9581821.01.01.01.0
4EDT50.20.5000000.64.8211480.8788521.01.01.01.0
5EDT60.20.5000000.64.8216510.9383491.01.01.01.0
6EDT70.20.5000000.64.8253351.3746651.01.01.01.0
7EDT80.20.5000000.64.8296901.8903101.01.01.01.0
8EDT90.20.5751150.64.9048852.0000001.01.01.01.0
9EDT100.20.6907660.65.0192342.0000001.01.01.01.0
10EDT110.20.8064170.65.1335832.0000001.01.01.01.0
11EDT120.20.9270960.65.2529042.0000001.01.01.01.0
12EDT130.21.0326910.65.3573092.0000001.01.01.01.0
13EDT140.21.1734830.65.4965172.0000001.01.01.01.0
14EDT150.21.2891350.65.6108652.0000001.01.01.01.0
15EDT160.21.3746160.65.6953842.0000001.01.01.01.0
16EDT170.21.4399840.65.7600162.0000001.01.01.01.0
17EDT180.21.4399840.65.7600162.0000001.01.01.01.0
18EDT190.21.3947290.65.7152712.0000001.01.01.01.0
19EDT200.21.2187380.65.5412622.0000001.01.01.01.0
20EDT210.21.0880020.65.4119982.0000001.01.01.01.0
21EDT220.20.9622940.65.2877062.0000001.01.01.01.0
22EDT230.20.6807090.65.0092912.0000001.01.01.01.0
23EDT240.20.5000000.64.8281831.7118171.01.01.01.0
\n", "
" ], "text/plain": [ " Time pg PV_1 pg PV_3 pg PV_5 pg PV_2 pg Slack_4 vBus 0 vBus 1 \\\n", "0 EDT1 0.2 0.500000 0.6 4.828936 1.801064 1.0 1.0 \n", "1 EDT2 0.2 0.500000 0.6 4.825838 1.434162 1.0 1.0 \n", "2 EDT3 0.2 0.500000 0.6 4.823074 1.106926 1.0 1.0 \n", "3 EDT4 0.2 0.500000 0.6 4.821818 0.958182 1.0 1.0 \n", "4 EDT5 0.2 0.500000 0.6 4.821148 0.878852 1.0 1.0 \n", "5 EDT6 0.2 0.500000 0.6 4.821651 0.938349 1.0 1.0 \n", "6 EDT7 0.2 0.500000 0.6 4.825335 1.374665 1.0 1.0 \n", "7 EDT8 0.2 0.500000 0.6 4.829690 1.890310 1.0 1.0 \n", "8 EDT9 0.2 0.575115 0.6 4.904885 2.000000 1.0 1.0 \n", "9 EDT10 0.2 0.690766 0.6 5.019234 2.000000 1.0 1.0 \n", "10 EDT11 0.2 0.806417 0.6 5.133583 2.000000 1.0 1.0 \n", "11 EDT12 0.2 0.927096 0.6 5.252904 2.000000 1.0 1.0 \n", "12 EDT13 0.2 1.032691 0.6 5.357309 2.000000 1.0 1.0 \n", "13 EDT14 0.2 1.173483 0.6 5.496517 2.000000 1.0 1.0 \n", "14 EDT15 0.2 1.289135 0.6 5.610865 2.000000 1.0 1.0 \n", "15 EDT16 0.2 1.374616 0.6 5.695384 2.000000 1.0 1.0 \n", "16 EDT17 0.2 1.439984 0.6 5.760016 2.000000 1.0 1.0 \n", "17 EDT18 0.2 1.439984 0.6 5.760016 2.000000 1.0 1.0 \n", "18 EDT19 0.2 1.394729 0.6 5.715271 2.000000 1.0 1.0 \n", "19 EDT20 0.2 1.218738 0.6 5.541262 2.000000 1.0 1.0 \n", "20 EDT21 0.2 1.088002 0.6 5.411998 2.000000 1.0 1.0 \n", "21 EDT22 0.2 0.962294 0.6 5.287706 2.000000 1.0 1.0 \n", "22 EDT23 0.2 0.680709 0.6 5.009291 2.000000 1.0 1.0 \n", "23 EDT24 0.2 0.500000 0.6 4.828183 1.711817 1.0 1.0 \n", "\n", " vBus 2 vBus 3 \n", "0 1.0 1.0 \n", "1 1.0 1.0 \n", "2 1.0 1.0 \n", "3 1.0 1.0 \n", "4 1.0 1.0 \n", "5 1.0 1.0 \n", "6 1.0 1.0 \n", "7 1.0 1.0 \n", "8 1.0 1.0 \n", "9 1.0 1.0 \n", "10 1.0 1.0 \n", "11 1.0 1.0 \n", "12 1.0 1.0 \n", "13 1.0 1.0 \n", "14 1.0 1.0 \n", "15 1.0 1.0 \n", "16 1.0 1.0 \n", "17 1.0 1.0 \n", "18 1.0 1.0 \n", "19 1.0 1.0 \n", "20 1.0 1.0 \n", "21 1.0 1.0 \n", "22 1.0 1.0 \n", "23 1.0 1.0 " ] }, "execution_count": 10, "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": 11, "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.12.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }