|
1 |
| -# dss_python |
2 |
| -Python bindings and misc tools for using OpenDSS (EPRI Distribution System Simulator). Based on CFFI and dss_capi, aiming for full COM compatibility on Windows and Linux. |
| 1 | +-------------------------------------------------- |
| 2 | +dss_python: Unofficial bindings for EPRI's OpenDSS |
| 3 | +-------------------------------------------------- |
| 4 | + |
| 5 | +Python bindings and misc tools for using OpenDSS (EPRI Distribution System Simulator). Based on CFFI and `dss_capi`, aiming for full COM compatibility on Windows and Linux. |
| 6 | + |
| 7 | +If you are looking for the C API library, see [`dss_capi`](http://github.com/PMeira/dss-capi/). |
| 8 | + |
| 9 | +Version 0.9.1, based on [OpenDSS revision 2123](https://sourceforge.net/p/electricdss/code/2123/tree/). |
| 10 | +This is a work-in-progress but it's deemed stable enough to be made public. |
| 11 | + |
| 12 | +This module mimics the COM structure (as exposed via `win32com` or `comtypes`), effectively enabling multi-platform compatibility at Python level. |
| 13 | +Most of the COM documentation can be used as-is, but instead of returning tuples or lists, this modules returns/accepts NumPy arrays for numeric data exchange. |
| 14 | + |
| 15 | +This module depends on CFFI, NumPy and, optionally, SciPy.Sparse for reading the sparse system admittance matrix. |
| 16 | + |
| 17 | + |
| 18 | +Missing features and limitations |
| 19 | +================================ |
| 20 | + |
| 21 | +Most limitations are inherited from `dss_capi`, i.e.: |
| 22 | + |
| 23 | +- Only the 64-bit version of OpenDSS is built. A 32-bit version should be possible with a few changes. |
| 24 | +- Currently not implemented: |
| 25 | + - `DSSEvents` from `DLL/ImplEvents.pas`: seems too dependent on COM. |
| 26 | + - `DSSProgress` from `DLL/ImplDSSProgress.pas`: would need a reimplementation depending on the target UI (GUI, text, headless, etc.) |
| 27 | + |
| 28 | +- Although tests were successful on openSuse 42.3 (both CPython 3.6 and PyPy3.5 v5.10.1), Linux binaries are not yet available. For the time being, you need to build them yourself. |
| 29 | + |
| 30 | +Extra features |
| 31 | +============== |
| 32 | +Besides most of the COM methods, some of the unique DDLL methods are also exposed in adapted forms, namely the methods from `DYMatrix.pas`, especially `GetCompressedYMatrix` (check the source files for more information). |
| 33 | + |
| 34 | +Since no GUI components are used in the FreePascal DLL, we are experimenting with different ways of handling OpenDSS errors. Currently, the `DSS.Text.Command` call checks for OpenDSS errors (through the `DSS.Error` interface) and converts those to Python exceptions. Ideally every error should be converted to Python exceptions, but that could negatively impact performance. You can manually trigger an error check by calling the function `CheckForError()` from the main module. |
| 35 | + |
| 36 | + |
| 37 | +Installing |
| 38 | +========== |
| 39 | + |
| 40 | +On Windows (64-bit Python 2.7 and 3.6), you can install directly from pip: |
| 41 | + |
| 42 | +``` |
| 43 | +pip install dss_python |
| 44 | +``` |
| 45 | + |
| 46 | +If successful, you can then import the `dss` module from your Python interpreter. |
| 47 | + |
| 48 | + |
| 49 | +Building |
| 50 | +======== |
| 51 | + |
| 52 | +Get this repository: |
| 53 | + |
| 54 | +``` |
| 55 | + git clone https://github.com/PMeira/dss_python.git |
| 56 | +``` |
| 57 | + |
| 58 | +Assuming you successfully built or downloaded the `dss_capi` (check [its repository](http://github.com/PMeira/dss-capi/) for instructions), keep the folder organization as follows: |
| 59 | + |
| 60 | +``` |
| 61 | +dss_capi/ |
| 62 | +dss_python/ |
| 63 | +electricdss/ |
| 64 | +``` |
| 65 | + |
| 66 | +Open a command prompt in the `dss_python` subfolder and run the build process: |
| 67 | + |
| 68 | +``` |
| 69 | +python setup.py build |
| 70 | +python setup.py install |
| 71 | +``` |
| 72 | + |
| 73 | +Example usage |
| 74 | +============= |
| 75 | + |
| 76 | +If you were using `win32com` in code like: |
| 77 | + |
| 78 | +``` |
| 79 | +import win32com.client |
| 80 | +dss_engine = win32com.client.Dispatch("OpenDSSEngine.DSS") |
| 81 | +``` |
| 82 | + |
| 83 | +or `comtypes`: |
| 84 | + |
| 85 | +``` |
| 86 | +import comtypes.client |
| 87 | +dss_engine = comtypes.client.CreateObject("OpenDSSEngine.DSS") |
| 88 | +``` |
| 89 | + |
| 90 | +you can replace that fragment with: |
| 91 | +``` |
| 92 | +import dss |
| 93 | +dss.use_com_compat() |
| 94 | +dss_engine = dss.DSS |
| 95 | +``` |
| 96 | + |
| 97 | +Assuming you have a DSS script named `master.dss`, you should be able to run it as shown below: |
| 98 | + |
| 99 | +``` |
| 100 | +import dss |
| 101 | +dss.use_com_compat() |
| 102 | +dss_engine = dss.DSS |
| 103 | +
|
| 104 | +dss_engine.Text.Command = "compile c:/dss_files/master.dss" |
| 105 | +dss_engine.ActiveCircuit.Solution.Solve() |
| 106 | +voltages = dss_engine.ActiveCircuit.AllBusVolts |
| 107 | +
|
| 108 | +for i in range(len(voltages) // 2): |
| 109 | + print('node %d: %f + j%f' % (i, voltages[2*i], voltages[2*i + 1])) |
| 110 | +``` |
| 111 | + |
| 112 | +If you do not need the mixed-cased handling, you can omit the call to `use_com_compat()` and use the casing used in this project. |
| 113 | + |
| 114 | + |
| 115 | +Testing |
| 116 | +======= |
| 117 | +Since the DLL is built using FreePascal, which is not officially supported by EPRI, the results are validated running sample networks provided in the official OpenDSS distribution. The only modifications are done directly by the script, removing interactive features and some minor other minor issues. |
| 118 | + |
| 119 | +The validation scripts is `tests/validation.py` and requires the same folder structure as the building process. You need `win32com` to run it. |
| 120 | + |
| 121 | +Currently, the following sample files from the official OpenDSS repository are used: |
| 122 | + |
| 123 | +``` |
| 124 | + Distrib/EPRITestCircuits/ckt5/Master_ckt5.dss |
| 125 | + Distrib/EPRITestCircuits/ckt7/Master_ckt7.dss |
| 126 | + Distrib/EPRITestCircuits/ckt24/Master_ckt24.dss |
| 127 | + Distrib/IEEETestCases/8500-Node/Master-unbal.dss |
| 128 | + Distrib/IEEETestCases/IEEE 30 Bus/Master.dss |
| 129 | + Distrib/IEEETestCases/NEVTestCase/NEVMASTER.DSS |
| 130 | + Distrib/IEEETestCases/37Bus/ieee37.dss |
| 131 | + Distrib/IEEETestCases/4Bus-DY-Bal/4Bus-DY-Bal.DSS |
| 132 | + Distrib/IEEETestCases/4Bus-GrdYD-Bal/4Bus-GrdYD-Bal.DSS |
| 133 | + Distrib/IEEETestCases/4Bus-OYOD-Bal/4Bus-OYOD-Bal.DSS |
| 134 | + Distrib/IEEETestCases/4Bus-OYOD-UnBal/4Bus-OYOD-UnBal.DSS |
| 135 | + Distrib/IEEETestCases/4Bus-YD-Bal/4Bus-YD-Bal.DSS |
| 136 | + Distrib/IEEETestCases/4Bus-YY-Bal/4Bus-YY-Bal.DSS |
| 137 | + Distrib/IEEETestCases/123Bus/IEEE123Master.dss |
| 138 | + Distrib/IEEETestCases/123Bus/SolarRamp.DSS |
| 139 | + Distrib/IEEETestCases/13Bus/IEEE13Nodeckt.dss |
| 140 | + Test/IEEE13_LineSpacing.dss |
| 141 | + Test/IEEE13_LineGeometry.dss |
| 142 | + Test/IEEE13_LineAndCableSpacing.dss |
| 143 | + Test/IEEE13_Assets.dss |
| 144 | + Test/CableParameters.dss |
| 145 | + Test/Cable_constants.DSS |
| 146 | + Test/BundleDemo.DSS |
| 147 | + Test/IEEE13_SpacingGeometry.dss |
| 148 | + Test/TextTsCable750MCM.dss |
| 149 | + Test/TestDDRegulator.dss |
| 150 | + Test/XYCurvetest.dss |
| 151 | + Test/PVSystemTestHarm.dss |
| 152 | + Test/TestAuto.dss |
| 153 | + Test/Stevenson.dss |
| 154 | + Test/YgD-Test.dss |
| 155 | + Test/Master_TestCapInterface.DSS |
| 156 | + Test/LoadTest.DSS |
| 157 | + Test/IEEELineGeometry.dss |
| 158 | + Test/ODRegTest.dss |
| 159 | + Test/MultiCircuitTest.DSS |
| 160 | + Test/TriplexLineCodeCalc.DSS |
| 161 | + Test/PVSystemTest-Duty.dss |
| 162 | + Test/PVSystemTest.dss |
| 163 | + Test/REACTORTest.DSS |
| 164 | +``` |
| 165 | + |
| 166 | +On Windows 10, remember to set the compatibility layer to Windows 7 (set the environment variable `__COMPAT_LAYER=WIN7RTM`), otherwise you may encounter issues with COM due to [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization) on Python 3.6. |
| 167 | + |
| 168 | +There is no validation on Linux yet, since we cannnot run the COM module there. The most likely solution will be to pickle the data on Windows and load them on Linux. |
| 169 | + |
| 170 | +Roadmap |
| 171 | +======= |
| 172 | +Besides bug fixes, the main funcionality of this library is mostly done. Notable desirable features that may be implemented are: |
| 173 | + |
| 174 | +- More and better documentation, including the integration of the help strings from the IDL/COM definition files. |
| 175 | +- Create wheels for Linux distributions, maybe using the Anaconda stack. |
| 176 | +- Create a more "Pythonic" API. This would break compatibility with COM, but may result in a more pleasant environment for using OpenDSS in Python. |
| 177 | + |
| 178 | + |
| 179 | +Questions? |
| 180 | +========== |
| 181 | +If you have any question, feel free to open a ticket on Github or contact me through [Twitter](https://twitter.com/PCMMeira). |
| 182 | +Please allow me a few days to respond. |
| 183 | + |
| 184 | + |
| 185 | +Credits / Acknowlegement |
| 186 | +======================== |
| 187 | +`dss_python` is based on EPRI's OpenDSS via the [`dss_capi`](http://github.com/PMeira/dss-capi/) project, check its licensing information. |
| 188 | + |
| 189 | +This project is licensed under the (new) BSD, available in the `LICENSE` file. It's the same license OpenDSS uses (`OPENDSS_LICENSE`). |
| 190 | + |
| 191 | +I thank my colleagues at the University of Campinas, Brazil, for providing feedback and helping me test this module. |
0 commit comments