- The MAGIC repository contains 2 packages:
omfandloadshape_upsampling- The
omfpackage is a nearly identical copy of the officialnreca-bts/omfrepository. It contains the new version of thecyberInverters.pymodel that usesPyMAGICas the back-end instead ofPyCIGAR. Various scenarios that use the newcyberInverters.pymodel are committed toomf/omf/data/Model/admin/. These scenarios are described in more detail below - The
loadshape_upsamplingpackage is designed to upsample lower-resolution load shapes (e.g. 15-minute resolution) into higher-resolution load shapes (e.g. 1-second resolution)- This package allows us to transform 15-minute load profiles from the SMART-DS dataset into 1-second load profiles so that our use cases and input configurations can be more detailed
- The
- A third repository,
PyMAGIC, must be copied into the base of theMAGICrepository. It is required to run the newcyberInverters.pymodel- This is a private repository and requires that the user has permissions to download the repository from its source
- E.g. Run
git clone git@github.com:lbnl-cybersecurity/PyMAGIC.gitin the root of the MAGIC repository - The latest PyMAGIC commit that is confirmed to work with this repository is a904a6bb29d8c8fd9d1d2fef1e911ce716e3d050, dated 2026-08-11
- E.g. Run
git clone git@github.com:lbnl-cybersecurity/PyMAGIC.git && git -C PyMAGIC checkout a904a6bb29d8c8fd9d1d2fef1e911ce716e3d050in the root of the MAGIC repository to get a verified-working copy of PyMAGIC
- E.g. Run
- E.g. Run
- This is a private repository and requires that the user has permissions to download the repository from its source
- The installation script is designed to run on Ubuntu Linux. It was validated on 20.04 LTS and 26.04 LTS
- Run the following command to install the prerequisites that are required by the installation script
sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y python3 git openssh-client - The
MAGICrepository can be installed by running thelinux_install.pyinstallation scriptpython3 linux_install.py - The installation script performs the following actions:
- It checks that the user has already git cloned the
PyMAGICrepository into the root of theMAGICrepository. The script will error if the repository hasn't been downloaded- This can be done by running
git clone git@github.com:lbnl-cybersecurity/PyMAGIC.gitin the root of theMAGICrepository. The user must have the proper ssh configuration to perform this action. Alternatively, rungit clone https://github.com/lbnl-cybersecurity/PyMAGIC.gitto clone without ssh keys
- This can be done by running
- It checks out the latest commit in
PyMAGICthat is confirmed to work with thecyberInverters.pymodel- The commit is
a904a6bb29d8c8fd9d1d2fef1e911ce716e3d050, dated 2026-08-11
- The commit is
- It runs
utils/pymagic/patch.pyto patch the necessary files inPyMAGICso that the omf can callPyMAGIC. This includes actions like copyingutils/pymagic/src/omf_observer_federate.pyintoPyMAGIC/src/pymagic/federates/omf_observer_federate.py - It attempts to download the Rainforest Automation Energy (RAE) dataset that is required by the
loadshape_upsamplingpackage- This download usually doesn't work because Harvard Dataverse blocks scripted downloads. The user will probably need to go to
https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/ZJW4LCand download the ZIP file. Extract the ZIP file todata/rae
- This download usually doesn't work because Harvard Dataverse blocks scripted downloads. The user will probably need to go to
- It attempts to download the SMART-DS dataset that is required by the
loadshape_upsamplingpackage- This download usually works and uses the aws CLI. If the download fails, the user should go to
https://data.openei.org/s3_viewer?bucket=oedi-data-lake&prefix=SMART-DS%2Fv1.0%2F&limit=50and download the entire2018directory. That2018directory should be placed atdata/smartds/2018
- This download usually works and uses the aws CLI. If the download fails, the user should go to
- It creates three python virtual environments, one for
omf, one forPyMAGIC, and one forloadshape_upsampling- Each virtual environment is located within its respective package
- E.g.
omf/.venv,PyMAGIC/.venv, andloadshape_upsampling/.venvare created. Each virtual environment must be activated before running code in the respective package
- E.g.
- Each virtual environment is located within its respective package
- It checks that the user has already git cloned the
- The
omfpackage can be run by activating the virtual environment and starting the Flask web server- E.g.
. omf/.venv/bin/activate && cd omf/omf && python web.py
- E.g.
- The web server is bound to port 5001, so go to
http://localhost:5001to access the web server
- The
loadshape_upsamplingpackage can be run by activating the virtual environment and runningtrain.pyfollowed byinterpolate.py- E.g.
. loadshape_upsampling/.venv/bin/activate && python loadshape_upsampling/src/train.py- This script creates the library of shapelets that are used to upsample lower-resolution load shapes
- E.g.
. loadshape_upsampling/.venv/bin/activate && python loadshape_upsampling/src/interpolate.py- This script performs the actual upsample process on the designated load shape/feeder
- Warning: this can crash your computer if you run out of disk space because this command can generate 100+ GB of files when upsampling entire feeders. The script aborts if it detects the user does not have enough filespace
- E.g.
- The upsampling process can also be quantitatively verified by running
. loadshape_upsampling/.venv/bin/activate && python loadshape_upsampling/src/interpolate.py --validate - The default configuration for
interpolate.pyupsamples feederrhs2_1247--rdt1262in the SMART-DS dataset- The output is written to
loadshape_upsampling/outputs - The configuration can be adjusted by editing
loadshape_upsampling/config/config.yaml- The most common yaml key that the user will probably want to change is
smartds_circuit. This can be set torhs2_1247--rdt1262orrhs2_1247--rdt1264
- The most common yaml key that the user will probably want to change is
- The output is written to
- It is also possible to interpolate individual loads or per-unit load profiles in the SMART-DS dataset
- E.g.
interpolate.py --listwill show which loads will be interpolated if the script is run as-is - E.g.
interpolate.py --load load_p1rlv6095will interpolate just that physical load - E.g.
interpolate.py --per-unit --load com_kw_14989_puwill interpolate just that per-unit load shape
- E.g.
- Compressed copies of the full-feeder upsampled CSVs and their accompanying .html visualization files are committed to the outputs directory
- The
PyMAGICrepository is only intended to be used as the back-end forcyberInverters.py. Please read the documentation inside of thePyMAGICrepository for instructions on how to use it directly
- The
omfpackage is a copy of thenreca-bts/omfrepository that applies the following changes:cyberInverters.pyandcyberInverters.htmlhave been rewritten to use thePyMAGICbackend instead of thePyCIGARbackendomf/omf/web.pyis modified to accept large form submissions via the Flask web server with these lines:app.config['MAX_FORM_MEMORY_SIZE'] = None # was 500KB default in Werkzeug 3.1+ app.config['MAX_CONTENT_LENGTH'] = None # optional: also uncap total body app.config['MAX_FORM_PARTS'] = None # optional: uncap field count- Werkzeug used to accept forms of unlimited size, but newer versions of Werkzeug cap the limit at 500 KB. If the user has installed a version of
Werkzeug > 3.1 in their environment, these lines are necessary to prevent 413 errors when
cyberInverters.htmlis submitted to the web server
- Werkzeug used to accept forms of unlimited size, but newer versions of Werkzeug cap the limit at 500 KB. If the user has installed a version of
Werkzeug > 3.1 in their environment, these lines are necessary to prevent 413 errors when
omf/pyproject.tomlis modified to include thePyYAMLpackage because user input fromcyberInverters.htmlis used to update thePyMAGICinput files for a given model run
omf/omf/static/testFiles/pyMAGICcontains 4 input configurations for the newcyberInverters.pymodel- ieee37 no attack
- ieee37 staggered voltage imbalance attack
- ieee123 no attack
- ieee123 staggered voltage imbalance attack
- These input configurations are described in more detail below
- The desired scenario for
cyberInverters.pycan be chosen by editing lines 40 - 43 incyberInverters.pydef new(model_dir): '''Create a new instance of this model. Returns true on success, false on failure.''' topology = 'ieee37' #topology = 'ieee37_attack' #topology = 'ieee123' #topology = 'ieee123_attack' - Set the
topologyvariable to point to the desired input configuration inside ofMAGIC/omf/omf/static/testFiles/pyMAGIC

- A regular power flow simulation is performed on the ieee37 delta feeder. There are PV inverters but no batteries. This scenario establishes the normal voltage behavior

- The ieee37 feeder is subject to a voltage imbalance attack. 60% of each PV inverter's VVVW breakpoints are shifted +0.05 p.u. upward to raise voltage,
staggered by phase pair
- ab over t=1000-1133 s
- bc over 1134-1267 s
- ac over 1268-1400 s
- Only one phase pair rises at a time and the feeder becomes unbalanced. There is no adaptive controller defense or algorithmic defense

- A regular power flow simulation is performed on the ieee123 wye feeder. There are PV inverters and batteries. This scenario establishes the normal voltage behavior

- The ieee123 feeder is subject to a voltage imbalance attack. 60% of each PV inverter's VVVW breakpoints are shifted +0.05 p.u. upward to raise voltage,
staggered by phase
- a over t=1000-1133 s
- b over 1134-1267 s
- c over 1268-1400 s
- Only one phase rises at a time and the feeder becomes unbalanced. There is no adaptive controller defense or algorithmic defense

- The
PyMAGICrepository is required to run the newcyberInverters.pymodel - Once the PyMAGIC repository has been downloaded and placed in the root of the
MAGIC repository, the omf integration must be installed into itomf_observer_federate.pyis copied into place, and thepatch_*.pyscripts inutils/pymagic/srcapply the integration's small edits toPyMAGIC's own files in place.PyMAGICis private, so this repository stores only the edits, never modified copies ofPyMAGICfiles
- The script
utils/pymagic/patch.pyperforms the copy and runs every patch script. The patch scripts exit with an error ifPyMAGIChas drifted from the pinned commit such that an edit no longer applies cleanly- The
linux_install.pyscript will run this script at install time. If the install script runs before thePyMAGICrepository has been downloaded, the script will exit with an error and tell the user to download thePyMAGICrepository
- The


- The
loadshape_upsamplingpackage is designed to upsample lower-resolution (e.g. 15-minute) load shapes into higher resolution (e.g. 1-second) load shapes while preserving the mean power consumption of the original load shape - The package is valuable because it allows the user to upsample SMART-DS load profiles into 1-second resolution in order to create more detailed scenarios
- The upsampling process works as follows:
- The user passes in a lower-resolution (e.g. 15-minute) load shape
- The lower-resolution load shape is split into intervals of a given size according to the user's configuration
- The default interval length is 15 minutes
- The mean power consumption of each interval is calculated
- Appliance power consumption patterns are drawn from a library of 1-second shapelets sampled from the Rainforest Automation Energy (RAE) dataset and are placed randomly across the intervals
- Once enough events have been placed such that the sum of the power of the events is within a threshold of the mean power of a given interval, that interval is considered complete and no more events are placed within that interval
- The remaining power in the interval is filled in with a smooth base layer that makes the upsampled interval match the mean of the original interval exactly
- The upsampling process is finished when the mean of every composed interval equals its original interval's mean
- The higher-resolution load shape has higher volatility and granularity than the input load shape while still preserving the power consumption of the original lower-resolution load shape
- As of 2026-08-17, next steps for the project include selecting a use case (e.g. peak shaving) that will demonstrate the value of cyberInverters.py and PyMAGIC to real-world users, such as coops
- Once a use case has been selected, an input configuration (i.e. a set of files in
testFiles/pyMAGIC) must be defined to represent that use case- PyMAGIC must support the use case (e.g. PyMAGIC doesn't currently have centralized battery charging and discharging so the peak shaving use case cannot be performed at this time)
- Once the base input configuration has been created, a cyberattack should be designed on top of that use case
- This is fairly simple and is done with sim.yaml for a given input configuration
- The cyberattack should be combined with a mitigation defense agent from LBNL. We need to describe our use case to LBNL and provide them with the base input configuration so that they can run it, stage the attack, and properly train a defense agent for it to provide attack mitigation
- Once complete, the results of running the use case, the cyberattack, and the defense mitigation should be documented
- https://data.openei.org/submissions/2981
- https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/ZJW4LC
- https://github.com/nreca-bts/omf
- https://github.com/lbnl-cybersecurity/PyMAGIC
Copyright (c) 2025, NRECA Research
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.