{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Profile in Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Profiling with Python CProfiler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before getting started, this example requires the config flag `PFlow.init_tds` to be `0`, which is the default value." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:01.143169Z", "start_time": "2021-03-18T00:45:00.625170Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:54.441579Z", "iopub.status.busy": "2023-10-07T18:12:54.441398Z", "iopub.status.idle": "2023-10-07T18:12:54.748318Z", "shell.execute_reply": "2023-10-07T18:12:54.747909Z", "shell.execute_reply.started": "2023-10-07T18:12:54.441560Z" } }, "outputs": [], "source": [ "import andes\n", "from andes.utils.paths import get_case\n", "\n", "case_path = get_case(\"kundur/kundur_full.xlsx\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Passing `profile=True, no_output = True` to `run` will enable the profiler and have the results printed." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:04.746020Z", "start_time": "2021-03-18T00:45:01.145480Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:54.748971Z", "iopub.status.busy": "2023-10-07T18:12:54.748833Z", "iopub.status.idle": "2023-10-07T18:12:56.475340Z", "shell.execute_reply": "2023-10-07T18:12:56.474803Z", "shell.execute_reply.started": "2023-10-07T18:12:54.748959Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Working directory: \"/home/hacui/repos/andes/examples\"\n", "> Loaded generated Python code in \"/home/hacui/.andes/pycode\".\n", "Parsing input file \"/home/hacui/repos/andes/andes/cases/kundur/kundur_full.xlsx\"...\n", "Input file parsed in 0.3536 seconds.\n", "System internal structure set up in 0.0412 seconds.\n", "-> System connectivity check results:\n", " No islanded bus detected.\n", " System is interconnected.\n", " Each island has a slack bus correctly defined and enabled.\n", "\n", "-> Power flow calculation\n", " Numba: Off\n", " Sparse solver: KLU\n", " Solution method: NR method\n", "Power flow initialized in 0.0051 seconds.\n", "0: |F(x)| = 14.9282832\n", "1: |F(x)| = 3.608627841\n", "2: |F(x)| = 0.1701107882\n", "3: |F(x)| = 0.002038626956\n", "4: |F(x)| = 3.745103977e-07\n", "Converged in 5 iterations in 0.0045 seconds.\n", "Initialization for dynamics completed in 0.0368 seconds.\n", "Initialization was successful.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "19305110348f4c4b999a2c527437abff", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00: Line.Line_8 status changed to 0 at t=2.0 sec.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Simulation to t=20.00 sec completed in 0.9577 seconds.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " 2126948 function calls (2100484 primitive calls) in 1.634 seconds\n", "\n", " Ordered by: cumulative time\n", " List reduced from 5556 to 40 due to restriction <40>\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 1 0.006 0.006 1.065 1.065 /home/hacui/repos/andes/andes/routines/tds.py:326(run)\n", " 603 0.001 0.000 0.891 0.001 /home/hacui/repos/andes/andes/routines/tds.py:521(itm_step)\n", " 603 0.051 0.000 0.890 0.001 /home/hacui/repos/andes/andes/routines/daeint.py:27(step)\n", " 2025 0.006 0.000 0.703 0.000 /home/hacui/repos/andes/andes/routines/tds.py:820(fg_update)\n", " 10176 0.060 0.000 0.646 0.000 /home/hacui/repos/andes/andes/system.py:1672(call_models)\n", " 1 0.000 0.000 0.558 0.558 /home/hacui/repos/andes/andes/main.py:275(load)\n", " 1040/207 0.003 0.000 0.423 0.002 :1022(_find_and_load)\n", " 960/129 0.002 0.000 0.420 0.003 :987(_find_and_load_unlocked)\n", " 847/34 0.001 0.000 0.419 0.012 :877(exec_module)\n", " 905/66 0.002 0.000 0.419 0.006 {built-in method builtins.exec}\n", " 1246/34 0.001 0.000 0.418 0.012 :233(_call_with_frames_removed)\n", " 925/130 0.002 0.000 0.415 0.003 :664(_load_unlocked)\n", " 2030 0.001 0.000 0.367 0.000 /home/hacui/repos/andes/andes/system.py:1047(g_update)\n", " 1 0.000 0.000 0.355 0.355 /home/hacui/repos/andes/andes/io/__init__.py:115(parse)\n", " 1 0.000 0.000 0.353 0.353 /home/hacui/repos/andes/andes/io/xlsx.py:88(read)\n", " 20279 0.082 0.000 0.349 0.000 /home/hacui/repos/andes/andes/core/model/model.py:963(g_update)\n", " 35 0.000 0.000 0.270 0.008 /home/hacui/repos/andes/andes/utils/lazyimport.py:61(__maybe_import__)\n", " 34 0.000 0.000 0.214 0.006 /home/hacui/repos/andes/andes/utils/lazyimport.py:73(__getattr__)\n", " 1 0.000 0.000 0.213 0.213 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/__init__.py:1()\n", " 1 0.000 0.000 0.169 0.169 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/core/api.py:1()\n", " 1 0.000 0.000 0.161 0.161 /home/hacui/repos/andes/andes/system.py:93(__init__)\n", " 109/107 0.000 0.000 0.138 0.001 /home/hacui/mambaforge/envs/a/lib/python3.10/importlib/__init__.py:108(import_module)\n", " 109/107 0.000 0.000 0.138 0.001 :1038(_gcd_import)\n", " 1 0.000 0.000 0.122 0.122 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/io/excel/_base.py:460(read_excel)\n", " 254/196 0.000 0.000 0.108 0.001 {built-in method builtins.__import__}\n", " 2030 0.001 0.000 0.101 0.000 /home/hacui/repos/andes/andes/system.py:1033(f_update)\n", " 1 0.000 0.000 0.100 0.100 /home/hacui/repos/andes/andes/system.py:1800(import_models)\n", " 79/52 0.000 0.000 0.097 0.002 :1182(exec_module)\n", " 79/52 0.082 0.001 0.097 0.002 {built-in method _imp.exec_dynamic}\n", "1636/1630 0.009 0.000 0.097 0.000 {built-in method builtins.__build_class__}\n", " 1 0.000 0.000 0.097 0.097 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/io/excel/_base.py:1520(__init__)\n", " 1 0.000 0.000 0.094 0.094 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/io/excel/_openpyxl.py:534(__init__)\n", " 20279 0.039 0.000 0.086 0.000 /home/hacui/repos/andes/andes/core/model/model.py:935(f_update)\n", " 2030 0.002 0.000 0.084 0.000 /home/hacui/repos/andes/andes/system.py:989(l_update_eq)\n", " 4 0.000 0.000 0.083 0.021 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/compat/_optional.py:81(import_optional_dependency)\n", " 1864/802 0.002 0.000 0.082 0.000 :1053(_handle_fromlist)\n", " 1 0.000 0.000 0.080 0.080 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/openpyxl/__init__.py:1()\n", " 2025 0.076 0.000 0.076 0.000 /home/hacui/.andes/pycode/GENROU.py:20(g_update)\n", " 1 0.000 0.000 0.069 0.069 /home/hacui/mambaforge/envs/a/lib/python3.10/site-packages/pandas/core/arrays/__init__.py:1()\n", " 20279 0.011 0.000 0.068 0.000 /home/hacui/repos/andes/andes/core/model/model.py:712(l_check_eq)\n", "\n", "\n", "-> Single process finished in 1.7222 seconds.\n" ] } ], "source": [ "ss = andes.run(\n", " case_path, profile=True, routine=\"tds\", no_output=True, default_config=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Profiling with `line_profiler`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`line_profiler` provides line-based profiling results for functions. \n", "\n", "Install with `pip install line_profiler` and restart the notebook." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:04.755264Z", "start_time": "2021-03-18T00:45:04.751789Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:56.476292Z", "iopub.status.busy": "2023-10-07T18:12:56.476173Z", "iopub.status.idle": "2023-10-07T18:12:56.478877Z", "shell.execute_reply": "2023-10-07T18:12:56.478545Z", "shell.execute_reply.started": "2023-10-07T18:12:56.476280Z" } }, "outputs": [], "source": [ "import andes\n", "from andes.utils.paths import get_case\n", "\n", "case_path = get_case(\"kundur/kundur_full.xlsx\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Profile power flow " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pass the function name to profile to the magic `%lprun`, followed by a call to the function itself or an upper-level function.\n", "\n", "Results will be shown in a popup window." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:05.289558Z", "start_time": "2021-03-18T00:45:04.756814Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:56.479955Z", "iopub.status.busy": "2023-10-07T18:12:56.479732Z", "iopub.status.idle": "2023-10-07T18:12:56.762658Z", "shell.execute_reply": "2023-10-07T18:12:56.762162Z", "shell.execute_reply.started": "2023-10-07T18:12:56.479943Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Working directory: \"/home/hacui/repos/andes/examples\"\n", "> Reloaded generated Python code of module \"pycode\".\n", "Parsing input file \"/home/hacui/repos/andes/andes/cases/kundur/kundur_full.xlsx\"...\n", "Input file parsed in 0.0498 seconds.\n", "System internal structure set up in 0.0402 seconds.\n", "-> System connectivity check results:\n", " No islanded bus detected.\n", " System is interconnected.\n", " Each island has a slack bus correctly defined and enabled.\n", "\n", "-> Power flow calculation\n", " Numba: Off\n", " Sparse solver: KLU\n", " Solution method: NR method\n", "Power flow initialized in 0.0050 seconds.\n", "0: |F(x)| = 14.9282832\n", "1: |F(x)| = 3.608627841\n", "2: |F(x)| = 0.1701107882\n", "3: |F(x)| = 0.002038626956\n", "4: |F(x)| = 3.745103977e-07\n", "Converged in 5 iterations in 0.0041 seconds.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "-> Single process finished in 0.2605 seconds.\n" ] }, { "data": { "text/plain": [ "Timer unit: 1e-09 s\n", "\n", "Total time: 0.0114298 s\n", "File: /home/hacui/repos/andes/andes/routines/pflow.py\n", "Function: run at line 211\n", "\n", "Line # Hits Time Per Hit % Time Line Contents\n", "==============================================================\n", " 211 def run(self, **kwargs):\n", " 212 \"\"\"\n", " 213 Solve the power flow using the selected method.\n", " 214 \n", " 215 Returns\n", " 216 -------\n", " 217 bool\n", " 218 convergence status\n", " 219 \"\"\"\n", " 220 \n", " 221 1 1363.0 1363.0 0.0 system = self.system\n", " 222 1 581.0 581.0 0.0 if self.config.check_conn == 1:\n", " 223 1 1378597.0 1e+06 12.1 self.system.connectivity()\n", " 224 \n", " 225 1 250450.0 250450.0 2.2 self.summary()\n", " 226 1 5393416.0 5e+06 47.2 self.init()\n", " 227 \n", " 228 1 401.0 401.0 0.0 if system.dae.m == 0:\n", " 229 logger.error(\"Loaded case contains no power flow element.\")\n", " 230 system.exit_code = 1\n", " 231 return False\n", " 232 \n", " 233 1 762.0 762.0 0.0 method = self.config.method.lower()\n", " 234 \n", " 235 1 4629.0 4629.0 0.0 t0, _ = elapsed()\n", " 236 \n", " 237 # ---------- Call solution methods ----------\n", " 238 1 251.0 251.0 0.0 if method == 'nr':\n", " 239 1 4163484.0 4e+06 36.4 self.nr_solve()\n", " 240 elif method == 'nk':\n", " 241 self.newton_krylov()\n", " 242 \n", " 243 1 5711.0 5711.0 0.0 t1, s1 = elapsed(t0)\n", " 244 1 311.0 311.0 0.0 self.exec_time = t1 - t0\n", " 245 \n", " 246 1 260.0 260.0 0.0 if not self.converged:\n", " 247 if abs(self.mis[-1] - self.mis[-2]) < self.config.tol:\n", " 248 max_idx = np.argmax(np.abs(system.dae.xy))\n", " 249 name = system.dae.xy_name[max_idx]\n", " 250 logger.error('Mismatch is not correctable possibly due to large load-generation imbalance.')\n", " 251 logger.error('Largest mismatch on equation associated with <%s>', name)\n", " 252 else:\n", " 253 logger.error('Power flow failed after %d iterations for \"%s\".', self.niter + 1, system.files.case)\n", " 254 \n", " 255 else:\n", " 256 1 222407.0 222407.0 1.9 logger.info('Converged in %d iterations in %s.', self.niter + 1, s1)\n", " 257 \n", " 258 # make a copy of power flow solutions\n", " 259 1 3146.0 3146.0 0.0 self.x_sol = system.dae.x.copy()\n", " 260 1 862.0 862.0 0.0 self.y_sol = system.dae.y.copy()\n", " 261 \n", " 262 1 441.0 441.0 0.0 if self.config.init_tds:\n", " 263 system.TDS.init()\n", " 264 1 330.0 330.0 0.0 if self.config.report:\n", " 265 1 1803.0 1803.0 0.0 system.PFlow.report()\n", " 266 \n", " 267 1 491.0 491.0 0.0 system.exit_code = 0 if self.converged else 1\n", " 268 1 110.0 110.0 0.0 return self.converged" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%load_ext line_profiler\n", "\n", "%lprun -f andes.routines.pflow.PFlow.run andes.run(case_path, no_output=True, default_config=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, do" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:05.550955Z", "start_time": "2021-03-18T00:45:05.292174Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:56.763291Z", "iopub.status.busy": "2023-10-07T18:12:56.763177Z", "iopub.status.idle": "2023-10-07T18:12:56.890400Z", "shell.execute_reply": "2023-10-07T18:12:56.889986Z", "shell.execute_reply.started": "2023-10-07T18:12:56.763280Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Working directory: \"/home/hacui/repos/andes/examples\"\n", "> Reloaded generated Python code of module \"pycode\".\n", "Parsing input file \"/home/hacui/repos/andes/andes/cases/kundur/kundur_full.xlsx\"...\n", "Input file parsed in 0.0293 seconds.\n", "System internal structure set up in 0.0231 seconds.\n", "-> System connectivity check results:\n", " No islanded bus detected.\n", " System is interconnected.\n", " Each island has a slack bus correctly defined and enabled.\n", "\n", "-> Power flow calculation\n", " Numba: Off\n", " Sparse solver: KLU\n", " Solution method: NR method\n", "Power flow initialized in 0.0043 seconds.\n", "0: |F(x)| = 14.9282832\n", "1: |F(x)| = 3.608627841\n", "2: |F(x)| = 0.1701107882\n", "3: |F(x)| = 0.002038626956\n", "4: |F(x)| = 3.745103977e-07\n", "Converged in 5 iterations in 0.0040 seconds.\n" ] }, { "data": { "text/plain": [ "Timer unit: 1e-09 s\n", "\n", "Total time: 0.0109119 s\n", "File: /home/hacui/repos/andes/andes/routines/pflow.py\n", "Function: run at line 211\n", "\n", "Line # Hits Time Per Hit % Time Line Contents\n", "==============================================================\n", " 211 def run(self, **kwargs):\n", " 212 \"\"\"\n", " 213 Solve the power flow using the selected method.\n", " 214 \n", " 215 Returns\n", " 216 -------\n", " 217 bool\n", " 218 convergence status\n", " 219 \"\"\"\n", " 220 \n", " 221 1 972.0 972.0 0.0 system = self.system\n", " 222 1 511.0 511.0 0.0 if self.config.check_conn == 1:\n", " 223 1 1557962.0 2e+06 14.3 self.system.connectivity()\n", " 224 \n", " 225 1 253977.0 253977.0 2.3 self.summary()\n", " 226 1 4784035.0 5e+06 43.8 self.init()\n", " 227 \n", " 228 1 451.0 451.0 0.0 if system.dae.m == 0:\n", " 229 logger.error(\"Loaded case contains no power flow element.\")\n", " 230 system.exit_code = 1\n", " 231 return False\n", " 232 \n", " 233 1 842.0 842.0 0.0 method = self.config.method.lower()\n", " 234 \n", " 235 1 4840.0 4840.0 0.0 t0, _ = elapsed()\n", " 236 \n", " 237 # ---------- Call solution methods ----------\n", " 238 1 211.0 211.0 0.0 if method == 'nr':\n", " 239 1 4053092.0 4e+06 37.1 self.nr_solve()\n", " 240 elif method == 'nk':\n", " 241 self.newton_krylov()\n", " 242 \n", " 243 1 5711.0 5711.0 0.1 t1, s1 = elapsed(t0)\n", " 244 1 200.0 200.0 0.0 self.exec_time = t1 - t0\n", " 245 \n", " 246 1 241.0 241.0 0.0 if not self.converged:\n", " 247 if abs(self.mis[-1] - self.mis[-2]) < self.config.tol:\n", " 248 max_idx = np.argmax(np.abs(system.dae.xy))\n", " 249 name = system.dae.xy_name[max_idx]\n", " 250 logger.error('Mismatch is not correctable possibly due to large load-generation imbalance.')\n", " 251 logger.error('Largest mismatch on equation associated with <%s>', name)\n", " 252 else:\n", " 253 logger.error('Power flow failed after %d iterations for \"%s\".', self.niter + 1, system.files.case)\n", " 254 \n", " 255 else:\n", " 256 1 241955.0 241955.0 2.2 logger.info('Converged in %d iterations in %s.', self.niter + 1, s1)\n", " 257 \n", " 258 # make a copy of power flow solutions\n", " 259 1 3116.0 3116.0 0.0 self.x_sol = system.dae.x.copy()\n", " 260 1 701.0 701.0 0.0 self.y_sol = system.dae.y.copy()\n", " 261 \n", " 262 1 562.0 562.0 0.0 if self.config.init_tds:\n", " 263 system.TDS.init()\n", " 264 1 241.0 241.0 0.0 if self.config.report:\n", " 265 1 1753.0 1753.0 0.0 system.PFlow.report()\n", " 266 \n", " 267 1 441.0 441.0 0.0 system.exit_code = 0 if self.converged else 1\n", " 268 1 110.0 110.0 0.0 return self.converged" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ss = andes.load(case_path, no_output=True, default_config=True)\n", "\n", "%lprun -f ss.PFlow.run ss.PFlow.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To dig into the Newton Raphson iteration steps, profile each step instead with:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:05.736950Z", "start_time": "2021-03-18T00:45:05.648221Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:56.891013Z", "iopub.status.busy": "2023-10-07T18:12:56.890902Z", "iopub.status.idle": "2023-10-07T18:12:57.101133Z", "shell.execute_reply": "2023-10-07T18:12:57.100689Z", "shell.execute_reply.started": "2023-10-07T18:12:56.891002Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Working directory: \"/home/hacui/repos/andes/examples\"\n", "> Reloaded generated Python code of module \"pycode\".\n", "Parsing input file \"/home/hacui/repos/andes/andes/cases/kundur/kundur_full.xlsx\"...\n", "Input file parsed in 0.0299 seconds.\n", "System internal structure set up in 0.0242 seconds.\n", "-> System connectivity check results:\n", " No islanded bus detected.\n", " System is interconnected.\n", " Each island has a slack bus correctly defined and enabled.\n", "\n", "-> Power flow calculation\n", " Numba: Off\n", " Sparse solver: KLU\n", " Solution method: NR method\n", "Power flow initialized in 0.0045 seconds.\n", "0: |F(x)| = 14.9282832\n", "1: |F(x)| = 3.608627841\n", "2: |F(x)| = 0.1701107882\n", "3: |F(x)| = 0.002038626956\n", "4: |F(x)| = 3.745103977e-07\n", "Converged in 5 iterations in 0.0040 seconds.\n" ] }, { "data": { "text/plain": [ "Timer unit: 1e-09 s\n", "\n", "Total time: 0.00289216 s\n", "File: /home/hacui/repos/andes/andes/routines/pflow.py\n", "Function: nr_step at line 104\n", "\n", "Line # Hits Time Per Hit % Time Line Contents\n", "==============================================================\n", " 104 def nr_step(self):\n", " 105 \"\"\"\n", " 106 Solve a single iteration step using the Newton-Raphson method.\n", " 107 \n", " 108 Returns\n", " 109 -------\n", " 110 float\n", " 111 maximum absolute mismatch\n", " 112 \"\"\"\n", " 113 \n", " 114 5 2064.0 412.8 0.1 system = self.system\n", " 115 \n", " 116 # ---------- Build numerical DAE----------\n", " 117 5 1288374.0 257674.8 44.5 self.fg_update()\n", " 118 \n", " 119 # ---------- update the Jacobian on conditions ----------\n", " 120 5 2365.0 473.0 0.1 if self.config.method != 'dishonest' or (self.niter < self.config.n_factorize):\n", " 121 5 1263947.0 252789.4 43.7 system.j_update(self.models)\n", " 122 5 4048.0 809.6 0.1 self.solver.worker.new_A = True\n", " 123 \n", " 124 # ---------- prepare and solve linear equations ----------\n", " 125 5 12294.0 2458.8 0.4 self.res[:system.dae.n] = -system.dae.f[:]\n", " 126 5 9229.0 1845.8 0.3 self.res[system.dae.n:] = -system.dae.g[:]\n", " 127 \n", " 128 10 15200.0 1520.0 0.5 self.A = sparse([[system.dae.fx, system.dae.gx],\n", " 129 5 1994.0 398.8 0.1 [system.dae.fy, system.dae.gy]])\n", " 130 \n", " 131 5 1422.0 284.4 0.0 if not self.config.linsolve:\n", " 132 5 95764.0 19152.8 3.3 self.inc = self.solver.solve(self.A, self.res)\n", " 133 else:\n", " 134 self.inc = self.solver.linsolve(self.A, self.res)\n", " 135 \n", " 136 5 15670.0 3134.0 0.5 system.dae.x += np.ravel(self.inc[:system.dae.n])\n", " 137 5 11501.0 2300.2 0.4 system.dae.y += np.ravel(self.inc[system.dae.n:])\n", " 138 \n", " 139 # find out variables associated with maximum mismatches\n", " 140 5 591.0 118.2 0.0 fmax = 0\n", " 141 5 1232.0 246.4 0.0 if system.dae.n > 0:\n", " 142 fmax_idx = np.argmax(np.abs(system.dae.f))\n", " 143 fmax = system.dae.f[fmax_idx]\n", " 144 logger.debug(\"Max. diff mismatch %.10g on %s\", fmax, system.dae.x_name[fmax_idx])\n", " 145 \n", " 146 5 28063.0 5612.6 1.0 gmax_idx = np.argmax(np.abs(system.dae.g))\n", " 147 5 2987.0 597.4 0.1 gmax = system.dae.g[gmax_idx]\n", " 148 5 87689.0 17537.8 3.0 logger.debug(\"Max. algeb mismatch %.10g on %s\", gmax, system.dae.y_name[gmax_idx])\n", " 149 \n", " 150 5 6012.0 1202.4 0.2 mis = max(abs(fmax), abs(gmax))\n", " 151 5 41141.0 8228.2 1.4 system.vars_to_models()\n", " 152 \n", " 153 5 570.0 114.0 0.0 return mis" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ss = andes.load(case_path, no_output=True, default_config=True)\n", "%lprun -f ss.PFlow.nr_step ss.PFlow.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Profile time-domain simulation" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-10-07T18:12:57.101809Z", "iopub.status.busy": "2023-10-07T18:12:57.101690Z", "iopub.status.idle": "2023-10-07T18:12:57.125489Z", "shell.execute_reply": "2023-10-07T18:12:57.125128Z", "shell.execute_reply.started": "2023-10-07T18:12:57.101796Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Initialization for dynamics completed in 0.0209 seconds.\n", "Initialization was successful.\n" ] } ], "source": [ "xy = ss.TDS.init()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:09.446492Z", "start_time": "2021-03-18T00:45:05.743150Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:57.126107Z", "iopub.status.busy": "2023-10-07T18:12:57.125991Z", "iopub.status.idle": "2023-10-07T18:12:58.079389Z", "shell.execute_reply": "2023-10-07T18:12:58.078996Z", "shell.execute_reply.started": "2023-10-07T18:12:57.126095Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "-> Time Domain Simulation Summary:\n", "Sparse Solver: KLU\n", "Simulation time: 0.0-20.0 s.\n", "Fixed step size: h=33.33 ms. Shrink if not converged.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "13d61b45623a422b8086d6a5df1e8920", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00: Line.Line_8 status changed to 0 at t=2.0 sec.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Simulation to t=20.00 sec completed in 0.9426 seconds.\n" ] }, { "data": { "text/plain": [ "Timer unit: 1e-09 s\n", "\n", "Total time: 0.882568 s\n", "File: /home/hacui/repos/andes/andes/routines/tds.py\n", "Function: itm_step at line 521\n", "\n", "Line # Hits Time Per Hit % Time Line Contents\n", "==============================================================\n", " 521 def itm_step(self):\n", " 522 \"\"\"\n", " 523 Integrate for the step size of ``self.h`` using implicit trapezoid method.\n", " 524 \n", " 525 Returns\n", " 526 -------\n", " 527 bool\n", " 528 Convergence status in ``self.converged``.\n", " 529 \n", " 530 \"\"\"\n", " 531 602 882567523.0 1e+06 100.0 return self.method.step(self)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%lprun -f ss.TDS.itm_step ss.TDS.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cleanup" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2021-03-18T00:45:10.807143Z", "start_time": "2021-03-18T00:45:09.448316Z" }, "execution": { "iopub.execute_input": "2023-10-07T18:12:58.080019Z", "iopub.status.busy": "2023-10-07T18:12:58.079899Z", "iopub.status.idle": "2023-10-07T18:12:58.583058Z", "shell.execute_reply": "2023-10-07T18:12:58.582178Z", "shell.execute_reply.started": "2023-10-07T18:12:58.080005Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No output file found in the working directory.\n" ] } ], "source": [ "!andes misc -C" ] } ], "metadata": { "interpreter": { "hash": "4c42303ec617988e96980582546035234a0dbb343f6614254a1d5bddbb9babb9" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }