Quick start¶
This notebook converts the IEEE 13-node feeder from OpenDSS to CIM and back. It runs from docs/guide/ in the
repository, which holds the deck at ../../tests/decks/IEEE/IEEE13_CDPSM/IEEE13_CDPSM.dss. Install the package first
(Installation).
| Step | Call | Touches files |
|---|---|---|
| Read | DssReader(path).read_all() returns a DssModel |
reads the deck and its Redirects |
| Import | dss_to_cim(model, name=...) returns a cim18v17 network |
writes .cimtbl artifacts to a temporary directory unless you pass artifact_dir= |
| Export | DssExporter().to_model(network) returns a DssModel |
none |
| Write | DssWriter().write(model, path) |
writes the deck |
Only the reader and the writer touch the filesystem.
Read the deck¶
DssReader follows Redirects and ~ continuation lines, evaluates RPN values, and applies Edits and Open/Close
as the engine would. read_all returns a DssModel: records keyed by their DSS class.
from pathlib import Path
from cimhub_opendss import DssReader
deck = Path("../../tests/decks/IEEE/IEEE13_CDPSM/IEEE13_CDPSM.dss")
model = DssReader(deck).read_all()
for dss_class, records in model.graph.items():
print(f"{dss_class.__name__:10} {len(records)}")
Vsource 1 Transformer 6 Regcontrol 3 Linecode 7 Load 16 Capacitor 2 Line 16 Relay 1 Fuse 1 Recloser 1 Pvsystem 2 Storage 3 Xfmrcode 1 Options 1 Buscoords 22
Each record is a dataclass from the LinkML schema. Slots keep the units the deck used: this load's kW is in kW and its
kV in kV.
from cimhub_opendss.schema import opendss_schema as schema
print(model.list_by_class(schema.Load)[0])
Load({
'name': '611',
'bus1': '611.3',
'phases': 1,
'kV': 2.4,
'kW': 170.0,
'model': LoadModel(text='ConstantI', description='5: Constant Current Magnitude', meaning='https://gridappsd.github.io/dss-graph/LoadModel.5'),
'conn': ConnectionType(text='wye', description='Wye (star) connection', meaning='http://iec.ch/TC57/CIM100#WindingConnection.Y'),
'kvar': 80.0
})
Import to CIM¶
dss_to_cim maps the records to .cimtbl text and has cimtables build the CIM network from it. name is the master
document's Model and file stem. artifact_dir keeps the text; here it goes to a temporary directory.
import tempfile
from cimhub_opendss import dss_to_cim
work = Path(tempfile.mkdtemp())
network = dss_to_cim(model, name="IEEE13_CDPSM", artifact_dir=work / "cimtbl")
The network is a cim-graph model. network.graph holds its objects by CIM class. The deck's 16 Lines are 11
ACLineSegments and five switches, and its six Transformers are three PowerTransformers and three
VoltageRegulators (the regulated, equal-voltage ones).
import cimprofiles.cim18.cim18v17 as cim
for cim_class in (cim.ConnectivityNode, cim.ACLineSegment, cim.LoadBreakSwitch, cim.Fuse, cim.Recloser,
cim.PowerTransformer, cim.VoltageRegulator, cim.EnergyConsumer, cim.LinearShuntCompensator,
cim.PowerElectronicsConnection):
print(f"{cim_class.__name__:28} {len(network.graph.get(cim_class, {}))}")
ConnectivityNode 22 ACLineSegment 11 LoadBreakSwitch 3 Fuse 1 Recloser 1 PowerTransformer 3 VoltageRegulator 3 EnergyConsumer 16 LinearShuntCompensator 2 PowerElectronicsConnection 5
To read a value, use a CIMHub core getter. It converts CIM's SI value to the unit you name. Load 611 is a 170 kW, 80 kVAr load on phase C.
from cimhub_core.getters.load import get_phase_loads
load = next(obj for obj in network.graph[cim.EnergyConsumer].values() if obj.name == "611")
loads = get_phase_loads(load, "kW", "kVAr")
print(loads.total)
print([phase.name for phase in loads.phases])
PhasePower(p=170.0, q=80.0) ['C']
The .cimtbl artifacts¶
artifact_dir holds one file per DSS table plus the master, which Imports the others in dependency order (catalogs
before the equipment that uses them).
artifacts = work / "cimtbl"
print(sorted(path.name for path in artifacts.iterdir()))
print()
imports = [line for line in (artifacts / "IEEE13_CDPSM.cimtbl").read_text().splitlines() if line.startswith("Import")]
print("\n".join(imports))
['IEEE13_CDPSM.cimtbl', 'bus.cimtbl', 'buscoords.cimtbl', 'capacitor.cimtbl', 'line.cimtbl', 'linecode.cimtbl', 'linegeometry.cimtbl', 'load.cimtbl', 'pvsystem.cimtbl', 'regcontrol.cimtbl', 'storage.cimtbl', 'transformer.cimtbl', 'vsource.cimtbl', 'xfmrcode.cimtbl', 'zip_fractions.cimtbl'] Import bus.cimtbl Import zip_fractions.cimtbl Import linegeometry.cimtbl Import linecode.cimtbl Import xfmrcode.cimtbl Import vsource.cimtbl Import line.cimtbl Import transformer.cimtbl Import regcontrol.cimtbl Import load.cimtbl Import capacitor.cimtbl Import pvsystem.cimtbl Import storage.cimtbl Import buscoords.cimtbl
Values are copied as the deck gave them, and the unit is in the column header (p (kW)). A comment says what the
importer defaulted.
print("\n".join((artifacts / "load.cimtbl").read_text().splitlines()[6:14]))
Profile = cim18v17
Table EnergyConsumer: mRID, name, node, phases, phaseConnection, grounded, p (kW), q (kVAr), customerCount, LoadResponse, normallyInService, BaseVoltage, EquipmentContainer=fdr_ieee13nodeckt
# defaulted: NumCust=1, enabled=true
ld_611, 611, 611, C, Y, true, 170.0, 80.0, 1, zip_constant_i, true, bv_4p16kv
# defaulted: NumCust=1, enabled=true
ld_634a, 634a, 634, A, Y, true, 160.0, 110.0, 1, zip_constant_pq, true, bv_480v
# defaulted: NumCust=1, enabled=true
The master's comment lists every DSS class in the deck that no mapper reads. IEEE 13 has one Relay, so it is counted
and not imported.
print("\n".join((artifacts / "IEEE13_CDPSM.cimtbl").read_text().splitlines()[:3]))
# OpenDSS deck IEEE13_CDPSM # Mapping: cimhub_opendss/docs/CIMTBL_MAPPING.md#master # not mapped: Relay 1
Export to a deck¶
DssExporter().to_model reads the CIM network through the core getters and returns a DssModel, the same shape the reader
returns. name_source="name" writes each DSS name from the CIM name; the default is the mRID.
from cimhub_opendss import DssExporter
exported = DssExporter().to_model(network, name_source="name")
for dss_class, records in exported.graph.items():
print(f"{dss_class.__name__:10} {len(records)}")
Vsource 1 Linecode 7 Line 16 Fuse 1 Recloser 1 Xfmrcode 1 Transformer 6 Regcontrol 3 Load 16 Capacitor 2 Pvsystem 2 Storage 3 Buscoords 22 Options 1
DssWriter().write creates the deck and its folders. A deck with bus coordinates also gets a CSV beside it.
from cimhub_opendss import DssWriter
written = work / "out" / "IEEE13_CDPSM.dss"
DssWriter().write(exported, written)
print(sorted(path.name for path in written.parent.iterdir()))
for line in written.read_text().splitlines()[:3]:
print(line[:110])
print([line[:100] for line in written.read_text().splitlines() if line.startswith("new Load.611")])
['IEEE13_CDPSM.dss', 'IEEE13_CDPSM_buscoords.csv'] Clear Set defaultbasefreq=60 New Circuit.IEEE13Nodeckt phases=3 bus1=sourcebus.1.2.3 basekv=115 pu=1 angle=30 R1=0.160376682055275 X1=0.641 ['new Load.611 phases=1 bus1=611.3 kV=2.40177711982884 kW=170 model=5 conn=wye kvar=80 NumCust=1 enabl']
The written deck reads back through DssReader, with the same counts:
again = DssReader(written).read_all()
print(again.count(schema.Line), again.count(schema.Load), again.count(schema.Transformer))
16 16 6
cim_to_dss(network, path) does the export and the write in one call, and dss_to_cim does the read-side
mapping of an already-read model. Chained:
from cimhub_opendss import DssReader, cim_to_dss, dss_to_cim
network = dss_to_cim(DssReader(deck).read_all(), name="IEEE13_CDPSM")
cim_to_dss(network, "out/IEEE13_CDPSM.dss")
Next¶
- Import: options, the artifact files and what raises.
- Export: names, the circuit source, switch state and what raises.
- Round trip: checking a converted deck against the original in the engine.
- What converts: the DSS classes that import and export.
<style>
['pre { line-height: 125%; }\ntd.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\nspan.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\ntd.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\nspan.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\n.highlight-ipynb .hll { background-color: var(--jp-cell-editor-active-background) }\n.highlight-ipynb { background: var(--jp-cell-editor-background); color: var(--jp-mirror-editor-variable-color) }\n.highlight-ipynb .c { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment */\n.highlight-ipynb .err { color: var(--jp-mirror-editor-error-color) } /* Error */\n.highlight-ipynb .k { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword */\n.highlight-ipynb .o { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator */\n.highlight-ipynb .p { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation */\n.highlight-ipynb .ch { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Hashbang */\n.highlight-ipynb .cm { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Multiline */\n.highlight-ipynb .cp { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Preproc */\n.highlight-ipynb .cpf { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.PreprocFile */\n.highlight-ipynb .c1 { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Single */\n.highlight-ipynb .cs { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Special */\n.highlight-ipynb .kc { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Constant */\n.highlight-ipynb .kd { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Declaration */\n.highlight-ipynb .kn { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Namespace */\n.highlight-ipynb .kp { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Pseudo */\n.highlight-ipynb .kr { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Reserved */\n.highlight-ipynb .kt { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Type */\n.highlight-ipynb .m { color: var(--jp-mirror-editor-number-color) } /* Literal.Number */\n.highlight-ipynb .s { color: var(--jp-mirror-editor-string-color) } /* Literal.String */\n.highlight-ipynb .ow { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator.Word */\n.highlight-ipynb .pm { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation.Marker */\n.highlight-ipynb .w { color: var(--jp-mirror-editor-variable-color) } /* Text.Whitespace */\n.highlight-ipynb .mb { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Bin */\n.highlight-ipynb .mf { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Float */\n.highlight-ipynb .mh { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Hex */\n.highlight-ipynb .mi { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer */\n.highlight-ipynb .mo { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Oct */\n.highlight-ipynb .sa { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Affix */\n.highlight-ipynb .sb { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Backtick */\n.highlight-ipynb .sc { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Char */\n.highlight-ipynb .dl { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Delimiter */\n.highlight-ipynb .sd { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Doc */\n.highlight-ipynb .s2 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Double */\n.highlight-ipynb .se { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Escape */\n.highlight-ipynb .sh { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Heredoc */\n.highlight-ipynb .si { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Interpol */\n.highlight-ipynb .sx { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Other */\n.highlight-ipynb .sr { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Regex */\n.highlight-ipynb .s1 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Single */\n.highlight-ipynb .ss { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Symbol */\n.highlight-ipynb .il { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer.Long */']
</style>