Object Builders¶
cimbuilder.object_builder has one builder class per CIM class: ACLineSegmentBuilder makes an
ACLineSegment, BreakerBuilder a Breaker, EnergyConsumerBuilder an EnergyConsumer, and so on. A
builder writes into a CIMantic Graphs model, one piece of the object at a time, and checks every attribute
and association name against the CIM profile as it goes. This page shows the pattern every builder
follows. A feeder from scratch then puts it to work, and the later pages
cover each kind of equipment.
Set up¶
The CIM profile and unit handling are process-wide environment variables, so set them before importing anything from CIMantic Graphs.
import os
os.environ["CIMG_CIM_PROFILE"] = "cim18v17"
os.environ["CIMG_USE_UNITS"] = "TRUE"
os.environ["CIMG_SYSTEM_MVA_BASE"] = "100"
from cimtables.units import Qty
from cimbuilder.network import new_feeder_model
from cimbuilder.object_builder import BaseVoltageBuilder, EnergyConsumerBuilder
Start with a network¶
Every builder writes into a network, a CIMantic Graphs model, and a container, the EquipmentContainer
that new equipment goes in by default. new_feeder_model makes an empty FeederModel around a new
Feeder and adds a 60 Hz BaseFrequency. authority and model seed the UUID of every object, so the
same build gives the same UUIDs every time it runs.
network = new_feeder_model(
authority="https://example.com/models",
model="object_builders",
Feeder="fdr_demo",
name="demo",
)
container = network.container
print(type(network).__name__, "around", type(container).__name__, container.mRID)
print(f"base frequency: {network.find_by_mRID('base_freq').frequency.to('Hz'):g} Hz")
FeederModel around Feeder fdr_demo base frequency: 60 Hz
To add equipment to an existing model instead, load it with CIMantic Graphs (for example a FeederModel
with an XMLFile connection, as the substation builder
pages do) and pass that model and its container to the builders.
Create, then add one part at a time¶
A builder is constructed with the network and the container. create() makes the object and adds it to
the graph. It takes only the identity of the object: mRID, name and description.
BaseVoltageBuilder(network, container).create(
mRID="bv_12_47kV", name="12.47 kV", nominalVoltage=Qty(12.47, "kV")
)
builder = EnergyConsumerBuilder(network, container)
load = builder.create(mRID="ld_671", name="671")
print(type(load).__name__, load.mRID, load.name)
EnergyConsumer ld_671 671
Each add_* method then fills in one CIM profile part, the slice of the class that one kind of model
exchange carries:
add_connectivity, the connectivity part: Terminals and their nodes, theEquipmentContainer, theBaseVoltageand the per-phase children.add_electrical, the electrical part: ratings and steady-state impedances.add_short_circuit, the short-circuit part: zero- and negative-sequence data.add_ssh, the steady-state hypothesis: the operating point, such aspandq, a switch'sopen, a tap changer'sstep, andinService.add_asset_info, the asset info part: catalog data on anAssetInfo, such as a wire or inverter datasheet.
Some builders have more, such as add_regulating_control or add_wire_configuration. Call only the ones
the model needs. Each method takes the object plus keyword-only arguments, and returns the object plus
whatever it created:
load, terminal, phases = builder.add_connectivity(
load, node="671", phaseConnection="Y", phases="ABC", BaseVoltage="bv_12_47kV"
)
print("terminal: ", terminal.mRID, "on node", terminal.ConnectivityNode.mRID)
print("phases: ", [phase.mRID for phase in phases])
print("container:", load.EquipmentContainer.mRID)
print("voltage: ", load.BaseVoltage.name)
terminal: ld_671_T1 on node 671 phases: ['ld_671_A', 'ld_671_B', 'ld_671_C'] container: fdr_demo voltage: 12.47 kV
The Terminal and the per-phase EnergyConsumerPhase objects take their mRIDs from the load:
<mrid>_T<sequence number=""> and <mrid>_<phase>.
Refer to other objects by mRID¶
Associations are mRID strings, such as BaseVoltage="bv_12_47kV". The target has to exist already, so
build catalogs, base voltages and containers before the equipment that refers to them. A missing mRID
raises a ValueError rather than leaving the association empty:
other = builder.create(mRID="ld_692")
try:
builder.add_electrical(other, LoadResponse="zip_missing")
except ValueError as error:
print(error)
'ld_692': LoadResponse 'zip_missing' not found in graph (build it before the object that references it)
Nodes are the exception. A node, node1 or node2 that does not exist yet becomes a new
ConnectivityNode in the container, as 671 did above. Build the node first with
ConnectivityNodeBuilder to give it a name or a BaseVoltage (see
Containers and nodes).
Values carry their units¶
Pass a Qty in any unit. It is converted and stored in CIM's SI unit, here W and VAr. Read a value back in
the unit you want with .to(). A plain number is taken to be in the SI unit already.
builder.add_ssh(load, p=Qty(1155, "kW"), q=Qty(660, "kVAr"))
print(load.p, "/", load.q)
print(f"{load.p.to('kW'):g} kW / {load.q.to('MVAr'):g} MVAr")
1155000.0 watt / 660000.0 VAr 1155 kW / 0.66 MVAr
Enumerations are strings¶
CIM enumerations, such as phaseConnection or a switch's phase code, are passed as their string value and
stored as the CIM enumeration member. An unknown string raises.
print(repr(load.phaseConnection), repr(terminal.phases))
<PhaseShuntConnectionKind.Y: 'Y'> <PhaseCode.ABC: 'ABC'>
Save the model¶
cimgraph.utils.write_xml(network, "model.xml") writes the whole graph as CIM RDF/XML, and
write_json_ld as JSON-LD. A feeder from scratch writes a model and reads
it back.
The builders¶
| Page | Builders |
|---|---|
| A feeder from scratch | a source, transformer, breaker, line, load, capacitor and PV system in one model |
| Containers and nodes | BaseVoltage, BasePower, Feeder, Substation, VoltageLevel, Bay, ConnectivityNode, BusbarSection, Junction, TopologicalNode |
| Lines | ACLineSegment, PerLengthSequenceImpedance, PerLengthPhaseImpedance, wire and spacing catalogs, SeriesCompensator |
| Switches | Breaker, Recloser, Fuse, Disconnector and the other switch classes, SwitchPhase, SwitchInfo |
| Transformers and regulators | PowerTransformer, RatioTapChanger, TapChangerControl, VoltageRegulator |
| Transformer tanks | TransformerTankConfiguration, transformer banks built from tanks |
| Loads and shunts | EnergyConsumer, LoadResponseCharacteristic, EnergyConnectionSchedule, LinearShuntCompensator, RegulatingControl |
| Sources and generation | EnergySource, SynchronousMachine, AsynchronousMachine, PowerElectronicsConnection with PV, battery and electric vehicle units |
| Measurements and limits | Analog, Discrete, OperationalLimitSet |
<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>