#!/usr/bin/env python # coding: utf-8 # # Simulate # This example gives a "hello world" example to use AMS. # ## Import and Setting the Verbosity Level # We first import the `ams` library. # In[1]: import ams # We can configure the verbosity level for logging (output messages) by passing a verbosity level (10-DEBUG, 20-INFO, 30-WARNING, 40-ERROR, 50-CRITICAL) to the `stream_level` argument of `ams.main.config_logger()`. Verbose level 10 is useful for getting debug output. # # The logging level can be altered by calling ``config_logger`` again with new ``stream_level`` and ``file_level``. # In[2]: ams.config_logger(stream_level=20) # Note that the above ``ams.config_logger()`` is a shorthand to ``ams.main.config_logger()``. # # If this step is omitted, the default `INFO` level (`stream_level=20`) will be used. # ## Run Simulations # ### Load Case # AMS supports multiple input file formats, including AMS ``.xlsx`` file, MATPOWER ``.m`` file, PYPOWER ``.py`` file, and PSS/E ``.raw`` file. # # Here we use the AMS ``.xlsx`` file as an example. The source file locates at ``$HOME/ams/ams/cases/ieee39/ieee39_uced.xlsx``. # In[3]: sp = ams.load(ams.get_case('5bus/pjm5bus_uced.xlsx'), setup=True, no_output=True,) # ### Inspect Models and Routines # In AMS, ``model`` refers to the device-level models, and they are registered to an OrderedDict ``models``. # In[4]: sp.models # We can inspect the detailed model data in the form of DataFrame. # In[5]: sp.PQ.as_df() # In AMS, all supported routines are registered to an OrderedDict ``routines``. # In[6]: sp.routines # ### Solve a Routine # Before solving a routine, we need to initialize it first. # Here Real-time Economic Dispatch (RTED) is used as an example. # In[7]: sp.RTED.init() # Then, one can solve it by calling ``run()``. # Here, argument `solver` can be passed to specify the solver to use, such as `solver='ECOS'`. # # Installed solvers can be listed by ``ams.shared.installed_solvers``, # and more detailes of solver can be found at [CVXPY-Choosing a solver](https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver). # In[8]: ams.shared.installed_solvers # In[9]: sp.RTED.run(solver='CLARABEL') # The solved results are stored in each variable itself. # For example, the solved power generation of ten generators # are stored in ``pg.v``. # In[10]: sp.RTED.pg.v # Here, ``get_all_idxes()`` can be used to get the index of a variable. # In[11]: sp.RTED.pg.get_all_idxes() # Part of the solved results can be accessed with given indices. # In[12]: sp.RTED.get(src='pg', attr='v', idx=['PV_1', 'PV_3']) # All Vars are listed in an OrderedDict ``vars``. # In[13]: sp.RTED.vars # The Objective value can be accessed with ``obj.v``. # In[14]: sp.RTED.obj.v # Similarly, all Constrs are listed in an OrderedDict ``constrs``, # and the expression values can also be accessed. # In[15]: sp.RTED.constrs # We can also inspect the `Constraint` values. # In[16]: sp.RTED.rgu.v