{ "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 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/miniconda3/envs/amsre/lib/python3.12/site-packages/ams/cases/5bus/pjm5bus_demo.xlsx\"...\n", "Input file parsed in 0.0605 seconds.\n", "Zero line rates detacted in rate_b, rate_c, adjusted to 999.\n", "System set up in 0.0016 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.0081 seconds.\n", " solved as optimal in 0.0071 seconds, converged in 8 iterations with CLARABEL.\n", "Report saved to \"pjm5bus_demo_out.txt\" in 0.0008 seconds.\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.0013 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.0.5\n", "Copyright (C) 2023-2024 Jinning Wang\n", "\n", "AMS comes with ABSOLUTELY NO WARRANTY\n", "Case file: /Users/jinningwang/work/miniconda3/envs/amsre/lib/python3.12/site-packages/ams/cases/5bus/pjm5bus_demo.xlsx\n", "Report time: 04/10/2025 09:30:37 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 2\n", "\n", "============================== DCOPF ==============================\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 0 0.023989 0.077623\n", "1 B 0 0.034668 0.01\n", "2 C 0 0.013068 0.3\n", "3 D 0 -0 0.15705\n", "4 E 0 0.022896 0.091705\n", "\n", "Line DATA:\n", " Name plf (p.u.)\n", "\n", "Line_1 Line AB -0.38001\n", "Line_2 Line AD 0.78912\n", "Line_3 Line AE 0.17089\n", "Line_4 Line BC 2\n", "Line_5 Line CD 0.43998\n", "Line_6 Line DE -0.77089\n", "Line_7 Line AB2 -0.38001\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.0129 seconds.\n", " solved as optimal in 0.0145 seconds, converged in 11 iterations with CLARABEL.\n", "Report saved to \"pjm5bus_demo_out.txt\" in 0.0024 seconds.\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": [ "sp.ED.export_csv()" ] }, { "cell_type": "code", "execution_count": 9, "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": 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", "
Timepg PV_1pg PV_3pg PV_5pg PV_2pg Slack_4vBus 0vBus 1vBus 2vBus 3
0EDT10.20.5000000.64.8295231.8704770.00.00.00.0
1EDT20.20.9371530.65.2628472.0000000.00.00.00.0
2EDT30.21.4399840.65.7600162.0000000.00.00.00.0
3EDT40.20.9371530.65.2628472.0000000.00.00.00.0
4EDT50.20.5000000.64.8295231.8704770.00.00.00.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.829523 1.870477 0.0 0.0 \n", "1 EDT2 0.2 0.937153 0.6 5.262847 2.000000 0.0 0.0 \n", "2 EDT3 0.2 1.439984 0.6 5.760016 2.000000 0.0 0.0 \n", "3 EDT4 0.2 0.937153 0.6 5.262847 2.000000 0.0 0.0 \n", "4 EDT5 0.2 0.500000 0.6 4.829523 1.870477 0.0 0.0 \n", "\n", " vBus 2 vBus 3 \n", "0 0.0 0.0 \n", "1 0.0 0.0 \n", "2 0.0 0.0 \n", "3 0.0 0.0 \n", "4 0.0 0.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": "amsre", "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 }