Skip to content

exporter.opendss

This module consists of class and helper functions to export distribution model in opendss format.

ConstantPowerFactorLoadWriter

Bases: LoadWriter

Constant Power Factor load writer inherits from Load writer.

Refer to base class for base attributes.

Attributes:

Name Type Description
mapping_dict(dict)

Load name to bus name mapping

Source code in shift\exporter\opendss.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class ConstantPowerFactorLoadWriter(LoadWriter):
    """Constant Power Factor load writer inherits from Load writer.

    Refer to base class for base attributes.

    Attributes:
        mapping_dict(dict): Load name to bus name mapping
    """

    def __init__(
        self, loads: List[Load], mapping_dict: dict, file_name: str
    ) -> None:
        """Constructor for `ConstantPowerFactorLoadWriter` class.

        Refer to base class for base class arguments.

        Args:
            mapping_dict (dict): Load name to bus name mapping
        """
        super().__init__(loads, file_name)
        self.mapping_dict = mapping_dict

    def write(self, folder_location: str) -> None:
        """Refer to base class for more details."""
        super().write(folder_location)

        load_contents = []
        for load in self.loads:
            # pylint: disable-next=line-too-long
            bus1 = f"{remove_invalid_chars(self.mapping_dict[load.name])}.{load.phase.value}"
            load_contents.append(
                f"new load.{remove_invalid_chars(load.name)} "
                + f"phases={load.num_phase.value} bus1={bus1} "
                + f"kv={load.kv} kw={load.kw} pf={load.pf} "
                + f"conn={load.conn_type.value}\n\n"
            )
            self.coord_dict[bus1.split(".")[0]] = (
                load.longitude,
                load.latitude,
            )

        if load_contents:
            with open(
                os.path.join(folder_location, self.file_name),
                "w",
                encoding="utf-8",
            ) as fpointer:
                fpointer.writelines(load_contents)

            self.files.append(self.file_name)

__init__(loads, mapping_dict, file_name)

Constructor for ConstantPowerFactorLoadWriter class.

Refer to base class for base class arguments.

Parameters:

Name Type Description Default
mapping_dict dict

Load name to bus name mapping

required
Source code in shift\exporter\opendss.py
130
131
132
133
134
135
136
137
138
139
140
141
def __init__(
    self, loads: List[Load], mapping_dict: dict, file_name: str
) -> None:
    """Constructor for `ConstantPowerFactorLoadWriter` class.

    Refer to base class for base class arguments.

    Args:
        mapping_dict (dict): Load name to bus name mapping
    """
    super().__init__(loads, file_name)
    self.mapping_dict = mapping_dict

write(folder_location)

Refer to base class for more details.

Source code in shift\exporter\opendss.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def write(self, folder_location: str) -> None:
    """Refer to base class for more details."""
    super().write(folder_location)

    load_contents = []
    for load in self.loads:
        # pylint: disable-next=line-too-long
        bus1 = f"{remove_invalid_chars(self.mapping_dict[load.name])}.{load.phase.value}"
        load_contents.append(
            f"new load.{remove_invalid_chars(load.name)} "
            + f"phases={load.num_phase.value} bus1={bus1} "
            + f"kv={load.kv} kw={load.kw} pf={load.pf} "
            + f"conn={load.conn_type.value}\n\n"
        )
        self.coord_dict[bus1.split(".")[0]] = (
            load.longitude,
            load.latitude,
        )

    if load_contents:
        with open(
            os.path.join(folder_location, self.file_name),
            "w",
            encoding="utf-8",
        ) as fpointer:
            fpointer.writelines(load_contents)

        self.files.append(self.file_name)

DSSWriter

Bases: ABC

Base class for OpenDSS writer.

Attributes:

Name Type Description
files List[str]

List of opendss file names

coord_dict dict

Mapping between busname and coordinates

Source code in shift\exporter\opendss.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class DSSWriter(ABC):
    """Base class for OpenDSS writer.

    Attributes:
        files (List[str]): List of opendss file names
        coord_dict (dict): Mapping between busname and coordinates
    """

    def __init__(self) -> None:
        """Constructor for `DSSWriter` class."""

        self.files = []
        self.coord_dict = {}

    def get_filenames(self) -> List[str]:
        """Returns the dss files exported by the class
        assuming subclass will update this attribute.
        """

        return self.files

    def write(self, folder_location: str) -> None:
        """Write models in the specified folder.

        Args:
            folder_location (str): Valid folder path

        Raises:
            FolderNotFoundError: If folder path does not exist.
        """

        if not os.path.exists(folder_location):
            raise FolderNotFoundError(folder_location)

    def get_coords(self) -> dict:
        """Returns coordinate mapping for all buses."""
        return self.coord_dict

__init__()

Constructor for DSSWriter class.

Source code in shift\exporter\opendss.py
69
70
71
72
73
def __init__(self) -> None:
    """Constructor for `DSSWriter` class."""

    self.files = []
    self.coord_dict = {}

get_coords()

Returns coordinate mapping for all buses.

Source code in shift\exporter\opendss.py
95
96
97
def get_coords(self) -> dict:
    """Returns coordinate mapping for all buses."""
    return self.coord_dict

get_filenames()

Returns the dss files exported by the class assuming subclass will update this attribute.

Source code in shift\exporter\opendss.py
75
76
77
78
79
80
def get_filenames(self) -> List[str]:
    """Returns the dss files exported by the class
    assuming subclass will update this attribute.
    """

    return self.files

write(folder_location)

Write models in the specified folder.

Parameters:

Name Type Description Default
folder_location str

Valid folder path

required

Raises:

Type Description
FolderNotFoundError

If folder path does not exist.

Source code in shift\exporter\opendss.py
82
83
84
85
86
87
88
89
90
91
92
93
def write(self, folder_location: str) -> None:
    """Write models in the specified folder.

    Args:
        folder_location (str): Valid folder path

    Raises:
        FolderNotFoundError: If folder path does not exist.
    """

    if not os.path.exists(folder_location):
        raise FolderNotFoundError(folder_location)

GeometryBasedLineWriter

Bases: LineWriter

Writer for geometry based line segments.

Refer to base class for base class attributes.

Attributes:

Name Type Description
geometry_file_name str

OpenDSS file name for writing line geometries

wire_file_name str

OpenDSS file name for writing wires

cable_file_name str

OpenDSS file name for writing cables

Source code in shift\exporter\opendss.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
class GeometryBasedLineWriter(LineWriter):
    """Writer for geometry based line segments.

    Refer to base class for base class attributes.

    Attributes:
        geometry_file_name (str): OpenDSS file name for writing line geometries
        wire_file_name (str): OpenDSS file name for writing wires
        cable_file_name (str): OpenDSS file name for writing cables
    """

    def __init__(
        self,
        lines: List[Line],
        line_file_name: str,
        geometry_file_name: str,
        wire_file_name: str,
        cable_file_name: str,
    ) -> None:
        """Constructor for `GeometryBasedLineWriter` class.

        Refer to base class for base class arguments.

        Args:
            geometry_file_name (str): OpenDSS file name
                for writing line geometries
            wire_file_name (str): OpenDSS file name for writing wires
            cable_file_name (str): OpenDSS file name for writing cables
        """

        super().__init__(lines, line_file_name)
        self.geometry_file_name = geometry_file_name
        self.wire_file_name = wire_file_name
        self.cable_file_name = cable_file_name

    def write(self, folder_location: str) -> None:
        """Refer to base class for more details.

        Raises:
            NotImplementedError: If conductor type passed is tap shielded cable.
        """
        super().write(folder_location)

        # To keep the contents for parts of line segments
        line_contents, geometry_contents, wire_contents, cable_contents = (
            [],
            [],
            [],
            [],
        )

        # To keep track of geometry objects
        geom_objects_dict = {}

        for line in self.lines:

            # Check if the geom already exists in the object list
            geom = line.geometry
            gk = geom.__class__

            if gk not in geom_objects_dict:
                geom_objects_dict[gk] = []

            if geom not in geom_objects_dict[gk]:
                geom_objects_dict[gk].append(geom)
            else:
                geom = geom_objects_dict[gk][geom_objects_dict[gk].index(geom)]

            # pylint: disable-next=line-too-long
            bus1 = f"{remove_invalid_chars(line.fromnode)}.{line.fromnode_phase.value}"
            bus2 = (
                f"{remove_invalid_chars(line.tonode)}.{line.tonode_phase.value}"
            )
            line_contents.append(
                f"new line.{remove_invalid_chars(line.name)} "
                + f"bus1={bus1} "
                + f"bus2={bus2} "
                + f"length={line.length if line.length !=0 else 0.0001}"
                + f" geometry={geom.name} units={line.length_unit}\n\n"
            )

            self.coord_dict[bus1.split(".")[0]] = (
                line.fromnode.split("_")[0],
                line.fromnode.split("_")[1],
            )
            self.coord_dict[bus2.split(".")[0]] = (
                line.tonode.split("_")[0],
                line.tonode.split("_")[1],
            )

        geom_object_list = [
            obj for _, obj_arr in geom_objects_dict.items() for obj in obj_arr
        ]

        # Loop over all the geometries

        # To keep track of wire objects
        wire_object_list, cable_object_list = [], []

        for geom in geom_object_list:

            # Check if the geom already exists in the object list

            if hasattr(geom, "phase_wire"):
                wire_attr = "wire"
                if geom.phase_wire in wire_object_list:
                    phase_cond = wire_object_list[
                        wire_object_list.index(geom.phase_wire)
                    ]
                else:
                    phase_cond = geom.phase_wire
                    wire_object_list.append(phase_cond)

                if hasattr(geom, "neutral_wire"):
                    if geom.neutral_wire in wire_object_list:
                        neutral_wire = wire_object_list[
                            wire_object_list.index(geom.neutral_wire)
                        ]
                    else:
                        neutral_wire = geom.neutral_wire
                        wire_object_list.append(neutral_wire)
            else:
                wire_attr = "cncable"
                if geom.phase_cable in cable_object_list:
                    phase_cond = cable_object_list[
                        cable_object_list.index(geom.phase_cable)
                    ]
                else:
                    phase_cond = geom.phase_cable
                    cable_object_list.append(phase_cond)

            geom_x_array = geom.configuration.get_x_array()
            geom_h_array = geom.configuration.get_h_array()

            geom_content = (
                f"new linegeometry.{geom.name} "
                + f"nconds={geom.num_conds} nphases={geom.num_phase.value} "
                + "reduce=no\n"
            )

            for id, items in enumerate(zip(geom_x_array, geom_h_array)):
                if id == len(geom_x_array) - 1 and hasattr(
                    geom, "neutral_wire"
                ):
                    geom_content += (
                        f"~ cond={id+1} {wire_attr}={neutral_wire.name} "
                        # pylint: disable-next=line-too-long
                        + f"x={items[0]} h={items[1]} units={geom.configuration.unit}\n"
                    )
                else:
                    geom_content += (
                        f"~ cond={id+1} {wire_attr}={phase_cond.name} "
                        # pylint: disable-next=line-too-long
                        + f"x={items[0]} h={items[1]} units={geom.configuration.unit}\n"
                    )
            geom_content += "\n"
            geometry_contents.append(geom_content)

        # Let's create wire and cables
        for wire in wire_object_list:

            wire_contents.append(
                f"new wiredata.{remove_invalid_chars(wire.name)} "
                + f"diam={wire.diam} gmrac={wire.gmrac} "
                + f"gmrunits={wire.gmrunits} normamps={wire.normamps} "
                + f"rac={wire.rac} runits={wire.runits} "
                + f"radunits={wire.radunits}\n\n"
            )

        for wire in cable_object_list:

            # Define concentric cable
            if not hasattr(wire, "taplayer"):
                cable_contents.append(
                    f"new CNData.{remove_invalid_chars(wire.name)}\n"
                    # pylint: disable-next=line-too-long
                    f"~ runits={wire.runits} radunits={wire.radunits} gmrunits={wire.gmrunits}\n"
                    + f"~ inslayer={wire.inslayer} diains={wire.diains}"
                    + f" diacable={wire.diacable} epsr=2.3\n"
                    + f"~ rac={wire.rac} gmrac={wire.gmrac} diam={wire.diam}\n"
                    + f"~ rstrand={wire.rstrand} gmrstrand={wire.gmrstrand}"
                    + f" diastrand={wire.diastrand} k={wire.k}"
                    + f" normamps={wire.normamps}\n\n"
                )

            else:
                raise NotImplementedError(
                    f"Writer for this type {wire} is not developed yet!"
                )

        for file_, contents in zip(
            [
                self.wire_file_name,
                self.cable_file_name,
                self.geometry_file_name,
                self.file_name,
            ],
            [wire_contents, cable_contents, geometry_contents, line_contents],
        ):
            if contents:
                with open(
                    os.path.join(folder_location, file_), "w", encoding="utf-8"
                ) as fpointer:
                    fpointer.writelines(contents)
                self.files.append(file_)

__init__(lines, line_file_name, geometry_file_name, wire_file_name, cable_file_name)

Constructor for GeometryBasedLineWriter class.

Refer to base class for base class arguments.

Parameters:

Name Type Description Default
geometry_file_name str

OpenDSS file name for writing line geometries

required
wire_file_name str

OpenDSS file name for writing wires

required
cable_file_name str

OpenDSS file name for writing cables

required
Source code in shift\exporter\opendss.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def __init__(
    self,
    lines: List[Line],
    line_file_name: str,
    geometry_file_name: str,
    wire_file_name: str,
    cable_file_name: str,
) -> None:
    """Constructor for `GeometryBasedLineWriter` class.

    Refer to base class for base class arguments.

    Args:
        geometry_file_name (str): OpenDSS file name
            for writing line geometries
        wire_file_name (str): OpenDSS file name for writing wires
        cable_file_name (str): OpenDSS file name for writing cables
    """

    super().__init__(lines, line_file_name)
    self.geometry_file_name = geometry_file_name
    self.wire_file_name = wire_file_name
    self.cable_file_name = cable_file_name

write(folder_location)

Refer to base class for more details.

Raises:

Type Description
NotImplementedError

If conductor type passed is tap shielded cable.

Source code in shift\exporter\opendss.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
def write(self, folder_location: str) -> None:
    """Refer to base class for more details.

    Raises:
        NotImplementedError: If conductor type passed is tap shielded cable.
    """
    super().write(folder_location)

    # To keep the contents for parts of line segments
    line_contents, geometry_contents, wire_contents, cable_contents = (
        [],
        [],
        [],
        [],
    )

    # To keep track of geometry objects
    geom_objects_dict = {}

    for line in self.lines:

        # Check if the geom already exists in the object list
        geom = line.geometry
        gk = geom.__class__

        if gk not in geom_objects_dict:
            geom_objects_dict[gk] = []

        if geom not in geom_objects_dict[gk]:
            geom_objects_dict[gk].append(geom)
        else:
            geom = geom_objects_dict[gk][geom_objects_dict[gk].index(geom)]

        # pylint: disable-next=line-too-long
        bus1 = f"{remove_invalid_chars(line.fromnode)}.{line.fromnode_phase.value}"
        bus2 = (
            f"{remove_invalid_chars(line.tonode)}.{line.tonode_phase.value}"
        )
        line_contents.append(
            f"new line.{remove_invalid_chars(line.name)} "
            + f"bus1={bus1} "
            + f"bus2={bus2} "
            + f"length={line.length if line.length !=0 else 0.0001}"
            + f" geometry={geom.name} units={line.length_unit}\n\n"
        )

        self.coord_dict[bus1.split(".")[0]] = (
            line.fromnode.split("_")[0],
            line.fromnode.split("_")[1],
        )
        self.coord_dict[bus2.split(".")[0]] = (
            line.tonode.split("_")[0],
            line.tonode.split("_")[1],
        )

    geom_object_list = [
        obj for _, obj_arr in geom_objects_dict.items() for obj in obj_arr
    ]

    # Loop over all the geometries

    # To keep track of wire objects
    wire_object_list, cable_object_list = [], []

    for geom in geom_object_list:

        # Check if the geom already exists in the object list

        if hasattr(geom, "phase_wire"):
            wire_attr = "wire"
            if geom.phase_wire in wire_object_list:
                phase_cond = wire_object_list[
                    wire_object_list.index(geom.phase_wire)
                ]
            else:
                phase_cond = geom.phase_wire
                wire_object_list.append(phase_cond)

            if hasattr(geom, "neutral_wire"):
                if geom.neutral_wire in wire_object_list:
                    neutral_wire = wire_object_list[
                        wire_object_list.index(geom.neutral_wire)
                    ]
                else:
                    neutral_wire = geom.neutral_wire
                    wire_object_list.append(neutral_wire)
        else:
            wire_attr = "cncable"
            if geom.phase_cable in cable_object_list:
                phase_cond = cable_object_list[
                    cable_object_list.index(geom.phase_cable)
                ]
            else:
                phase_cond = geom.phase_cable
                cable_object_list.append(phase_cond)

        geom_x_array = geom.configuration.get_x_array()
        geom_h_array = geom.configuration.get_h_array()

        geom_content = (
            f"new linegeometry.{geom.name} "
            + f"nconds={geom.num_conds} nphases={geom.num_phase.value} "
            + "reduce=no\n"
        )

        for id, items in enumerate(zip(geom_x_array, geom_h_array)):
            if id == len(geom_x_array) - 1 and hasattr(
                geom, "neutral_wire"
            ):
                geom_content += (
                    f"~ cond={id+1} {wire_attr}={neutral_wire.name} "
                    # pylint: disable-next=line-too-long
                    + f"x={items[0]} h={items[1]} units={geom.configuration.unit}\n"
                )
            else:
                geom_content += (
                    f"~ cond={id+1} {wire_attr}={phase_cond.name} "
                    # pylint: disable-next=line-too-long
                    + f"x={items[0]} h={items[1]} units={geom.configuration.unit}\n"
                )
        geom_content += "\n"
        geometry_contents.append(geom_content)

    # Let's create wire and cables
    for wire in wire_object_list:

        wire_contents.append(
            f"new wiredata.{remove_invalid_chars(wire.name)} "
            + f"diam={wire.diam} gmrac={wire.gmrac} "
            + f"gmrunits={wire.gmrunits} normamps={wire.normamps} "
            + f"rac={wire.rac} runits={wire.runits} "
            + f"radunits={wire.radunits}\n\n"
        )

    for wire in cable_object_list:

        # Define concentric cable
        if not hasattr(wire, "taplayer"):
            cable_contents.append(
                f"new CNData.{remove_invalid_chars(wire.name)}\n"
                # pylint: disable-next=line-too-long
                f"~ runits={wire.runits} radunits={wire.radunits} gmrunits={wire.gmrunits}\n"
                + f"~ inslayer={wire.inslayer} diains={wire.diains}"
                + f" diacable={wire.diacable} epsr=2.3\n"
                + f"~ rac={wire.rac} gmrac={wire.gmrac} diam={wire.diam}\n"
                + f"~ rstrand={wire.rstrand} gmrstrand={wire.gmrstrand}"
                + f" diastrand={wire.diastrand} k={wire.k}"
                + f" normamps={wire.normamps}\n\n"
            )

        else:
            raise NotImplementedError(
                f"Writer for this type {wire} is not developed yet!"
            )

    for file_, contents in zip(
        [
            self.wire_file_name,
            self.cable_file_name,
            self.geometry_file_name,
            self.file_name,
        ],
        [wire_contents, cable_contents, geometry_contents, line_contents],
    ):
        if contents:
            with open(
                os.path.join(folder_location, file_), "w", encoding="utf-8"
            ) as fpointer:
                fpointer.writelines(contents)
            self.files.append(file_)

LineWriter

Bases: DSSWriter

Base line writer inherits from DSS writer.

Attributes:

Name Type Description
lines List[Line]

List of Line objects.

file_name str

OpenDSS filename for writing line segments

Source code in shift\exporter\opendss.py
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class LineWriter(DSSWriter):
    """Base line writer inherits from DSS writer.

    Attributes:
        lines (List[Line]): List of `Line` objects.
        file_name (str): OpenDSS filename for writing line segments
    """

    def __init__(self, lines: List[Line], file_name: str) -> None:
        """Constructor for `LineWriter` class.

        Args:
            lines (List[Line]): List of `Line` objects.
            file_name (str): OpenDSS filename for writing line segments
        """

        super().__init__()
        self.lines = lines
        self.file_name = file_name

__init__(lines, file_name)

Constructor for LineWriter class.

Parameters:

Name Type Description Default
lines List[Line]

List of Line objects.

required
file_name str

OpenDSS filename for writing line segments

required
Source code in shift\exporter\opendss.py
247
248
249
250
251
252
253
254
255
256
257
def __init__(self, lines: List[Line], file_name: str) -> None:
    """Constructor for `LineWriter` class.

    Args:
        lines (List[Line]): List of `Line` objects.
        file_name (str): OpenDSS filename for writing line segments
    """

    super().__init__()
    self.lines = lines
    self.file_name = file_name

LoadWriter

Bases: DSSWriter

Base load writer inherits from DSS writer.

Attributes:

Name Type Description
loads List[Load]

List of Load objects

file_name str

OpenDSS file name to write all loads.

Source code in shift\exporter\opendss.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class LoadWriter(DSSWriter):
    """Base load writer inherits from DSS writer.

    Attributes:
        loads (List[Load]): List of `Load` objects
        file_name (str): OpenDSS file name to write all loads.
    """

    def __init__(self, loads: List[Load], file_name: str) -> None:
        """Constructor for `LoadWriter` class.

        Args:
            loads (List[Load]): List of `Load` objects
            file_name (str): OpenDSS file name to write all loads.
        """

        super().__init__()
        self.loads = loads
        self.file_name = file_name

__init__(loads, file_name)

Constructor for LoadWriter class.

Parameters:

Name Type Description Default
loads List[Load]

List of Load objects

required
file_name str

OpenDSS file name to write all loads.

required
Source code in shift\exporter\opendss.py
108
109
110
111
112
113
114
115
116
117
118
def __init__(self, loads: List[Load], file_name: str) -> None:
    """Constructor for `LoadWriter` class.

    Args:
        loads (List[Load]): List of `Load` objects
        file_name (str): OpenDSS file name to write all loads.
    """

    super().__init__()
    self.loads = loads
    self.file_name = file_name

OpenDSSExporter

Bases: BaseExporter

OpenDSS Exporter Class.

Attributes:

Name Type Description
writers List[DSSWriter]

List of DSSWriter Instances

folder_location str

Path to a folder for writing OpenDSS files

master_file_name str

OpenDSS file name for master file

circuit_name str

OpenDSS circuit name

circuit_kv float

OpenDSS circuit voltage in kV

circuit_freq freq

OpenDSS circuit base frequency in Hz

kv_arrays List[float]

List of base voltage levels

circuit_phase Phase

Phase instance for OpenDSS circuit

circuit_bus str

Bus name for OpenDSS circuit

circuit_num_phase NumPhase

NumPhase instance for OpenDSS circuit

circuit_z1 List[float]

List of positive sequence impedance values

circuit_z0 List[float]

List of zero sequence impedance values

Source code in shift\exporter\opendss.py
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
class OpenDSSExporter(BaseExporter):
    """OpenDSS Exporter Class.

    Attributes:
        writers (List[DSSWriter]): List of `DSSWriter` Instances
        folder_location (str): Path to a folder for writing OpenDSS files
        master_file_name (str): OpenDSS file name for master file
        circuit_name (str): OpenDSS circuit name
        circuit_kv (float): OpenDSS circuit voltage in kV
        circuit_freq (freq): OpenDSS circuit base frequency in Hz
        kv_arrays (List[float]): List of base voltage levels
        circuit_phase (Phase): Phase instance for OpenDSS circuit
        circuit_bus (str): Bus name for OpenDSS circuit
        circuit_num_phase (NumPhase): NumPhase instance for OpenDSS circuit
        circuit_z1 (List[float]): List of positive sequence impedance values
        circuit_z0 (List[float]): List of zero sequence impedance values
    """

    def __init__(
        self,
        writers: List[DSSWriter],
        folder_location: str,
        master_file_name: str,
        circuit_name: str,
        circuit_kv: float,
        circuit_freq: float,
        circuit_phase: Phase,
        circuit_num_phase: NumPhase,
        circuit_bus: str,
        circuit_z1: List[float],
        circuit_z0: List[float],
        kv_arrays: List[float],
    ) -> None:
        """Constructor for `OpenDSSExporter` class.

        Args:
            writers (List[DSSWriter]): List of `DSSWriter` Instances
            folder_location (str): Path to a folder for writing OpenDSS files
            master_file_name (str): OpenDSS file name for master file
            circuit_name (str): OpenDSS circuit name
            circuit_kv (float): OpenDSS circuit voltage in kV
            circuit_freq (freq): OpenDSS circuit base frequency in Hz
            circuit_phase (Phase): Phase instance for OpenDSS circuit
            circuit_num_phase (NumPhase): NumPhase instance for OpenDSS circuit
            circuit_bus (str): Bus name for OpenDSS circuit
            circuit_z1 (List[float]): List of positive sequence impedance values
            circuit_z0 (List[float]): List of zero sequence impedance values
            kv_arrays (List[float]): List of base voltage levels

        Raises:
            UnsupportedFrequencyError: If invalid frequency is passed.
            FolderNotFoundError: If folder path is not found.

        """

        if circuit_freq not in VALID_FREQUENCIES:
            raise UnsupportedFrequencyError(circuit_freq)
        self.writers = writers
        self.folder_location = folder_location
        self.master_file_name = master_file_name
        self.circuit_name = circuit_name
        self.circuit_kv = circuit_kv
        self.circuit_freq = circuit_freq
        self.kv_arrays = kv_arrays
        self.circuit_phase = circuit_phase
        self.circuit_bus = circuit_bus
        self.circuit_num_phase = circuit_num_phase
        self.circuit_z1 = circuit_z1
        self.circuit_z0 = circuit_z0

        if not os.path.exists(self.folder_location):
            raise FolderNotFoundError(folder_location)

    def export(self):
        """Refer to base class for more details."""

        files = []
        coord_dict = {}
        for writer in self.writers:
            writer.write(self.folder_location)
            files += writer.get_filenames()
            coord_dict.update(writer.get_coords())

        coord_content = ""
        for key, vals in coord_dict.items():
            coord_content += f"{key}, {vals[0]}, {vals[1]}\n"

        with open(
            os.path.join(self.folder_location, "buscoords.dss"),
            "w",
            encoding="utf-8",
        ) as fpointer:
            fpointer.writelines(coord_content)

        master_file_content = (
            "clear\n\n"
            + f"new circuit.{self.circuit_name} basekv={self.circuit_kv} "
            # pylint: disable-next=line-too-long
            + f"basefreq={self.circuit_freq} pu=1.0 phases={self.circuit_num_phase.value} "
            + f"Z1={self.circuit_z1} Z0={self.circuit_z0} "
            # pylint: disable-next=line-too-long
            + f"bus1={remove_invalid_chars(self.circuit_bus)}.{self.circuit_phase.value} \n\n"
        )

        for file in files:
            master_file_content += f"redirect {file}\n\n"

        master_file_content += (
            f"set voltagebases={self.kv_arrays}\n\nCalcvoltagebases\n\n"
        )
        master_file_content += "Buscoords buscoords.dss\n\nsolve"

        with open(
            os.path.join(self.folder_location, self.master_file_name),
            "w",
            encoding="utf-8",
        ) as fpointer:
            fpointer.writelines(master_file_content)

__init__(writers, folder_location, master_file_name, circuit_name, circuit_kv, circuit_freq, circuit_phase, circuit_num_phase, circuit_bus, circuit_z1, circuit_z0, kv_arrays)

Constructor for OpenDSSExporter class.

Parameters:

Name Type Description Default
writers List[DSSWriter]

List of DSSWriter Instances

required
folder_location str

Path to a folder for writing OpenDSS files

required
master_file_name str

OpenDSS file name for master file

required
circuit_name str

OpenDSS circuit name

required
circuit_kv float

OpenDSS circuit voltage in kV

required
circuit_freq freq

OpenDSS circuit base frequency in Hz

required
circuit_phase Phase

Phase instance for OpenDSS circuit

required
circuit_num_phase NumPhase

NumPhase instance for OpenDSS circuit

required
circuit_bus str

Bus name for OpenDSS circuit

required
circuit_z1 List[float]

List of positive sequence impedance values

required
circuit_z0 List[float]

List of zero sequence impedance values

required
kv_arrays List[float]

List of base voltage levels

required

Raises:

Type Description
UnsupportedFrequencyError

If invalid frequency is passed.

FolderNotFoundError

If folder path is not found.

Source code in shift\exporter\opendss.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
def __init__(
    self,
    writers: List[DSSWriter],
    folder_location: str,
    master_file_name: str,
    circuit_name: str,
    circuit_kv: float,
    circuit_freq: float,
    circuit_phase: Phase,
    circuit_num_phase: NumPhase,
    circuit_bus: str,
    circuit_z1: List[float],
    circuit_z0: List[float],
    kv_arrays: List[float],
) -> None:
    """Constructor for `OpenDSSExporter` class.

    Args:
        writers (List[DSSWriter]): List of `DSSWriter` Instances
        folder_location (str): Path to a folder for writing OpenDSS files
        master_file_name (str): OpenDSS file name for master file
        circuit_name (str): OpenDSS circuit name
        circuit_kv (float): OpenDSS circuit voltage in kV
        circuit_freq (freq): OpenDSS circuit base frequency in Hz
        circuit_phase (Phase): Phase instance for OpenDSS circuit
        circuit_num_phase (NumPhase): NumPhase instance for OpenDSS circuit
        circuit_bus (str): Bus name for OpenDSS circuit
        circuit_z1 (List[float]): List of positive sequence impedance values
        circuit_z0 (List[float]): List of zero sequence impedance values
        kv_arrays (List[float]): List of base voltage levels

    Raises:
        UnsupportedFrequencyError: If invalid frequency is passed.
        FolderNotFoundError: If folder path is not found.

    """

    if circuit_freq not in VALID_FREQUENCIES:
        raise UnsupportedFrequencyError(circuit_freq)
    self.writers = writers
    self.folder_location = folder_location
    self.master_file_name = master_file_name
    self.circuit_name = circuit_name
    self.circuit_kv = circuit_kv
    self.circuit_freq = circuit_freq
    self.kv_arrays = kv_arrays
    self.circuit_phase = circuit_phase
    self.circuit_bus = circuit_bus
    self.circuit_num_phase = circuit_num_phase
    self.circuit_z1 = circuit_z1
    self.circuit_z0 = circuit_z0

    if not os.path.exists(self.folder_location):
        raise FolderNotFoundError(folder_location)

export()

Refer to base class for more details.

Source code in shift\exporter\opendss.py
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def export(self):
    """Refer to base class for more details."""

    files = []
    coord_dict = {}
    for writer in self.writers:
        writer.write(self.folder_location)
        files += writer.get_filenames()
        coord_dict.update(writer.get_coords())

    coord_content = ""
    for key, vals in coord_dict.items():
        coord_content += f"{key}, {vals[0]}, {vals[1]}\n"

    with open(
        os.path.join(self.folder_location, "buscoords.dss"),
        "w",
        encoding="utf-8",
    ) as fpointer:
        fpointer.writelines(coord_content)

    master_file_content = (
        "clear\n\n"
        + f"new circuit.{self.circuit_name} basekv={self.circuit_kv} "
        # pylint: disable-next=line-too-long
        + f"basefreq={self.circuit_freq} pu=1.0 phases={self.circuit_num_phase.value} "
        + f"Z1={self.circuit_z1} Z0={self.circuit_z0} "
        # pylint: disable-next=line-too-long
        + f"bus1={remove_invalid_chars(self.circuit_bus)}.{self.circuit_phase.value} \n\n"
    )

    for file in files:
        master_file_content += f"redirect {file}\n\n"

    master_file_content += (
        f"set voltagebases={self.kv_arrays}\n\nCalcvoltagebases\n\n"
    )
    master_file_content += "Buscoords buscoords.dss\n\nsolve"

    with open(
        os.path.join(self.folder_location, self.master_file_name),
        "w",
        encoding="utf-8",
    ) as fpointer:
        fpointer.writelines(master_file_content)

TransformerWriter

Bases: DSSWriter

Transformer writer inherits from DSS writer.

Attributes:

Name Type Description
transformers List[Transformer]

List of Transformer objects

file_name str

OpenDSS filename to store the transformers

Source code in shift\exporter\opendss.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class TransformerWriter(DSSWriter):
    """Transformer writer inherits from DSS writer.

    Attributes:
        transformers (List[Transformer]): List of `Transformer` objects
        file_name (str): OpenDSS filename to store the transformers
    """

    def __init__(self, transformers: List[Transformer], file_name: str) -> None:
        """Constructor for `TransformerWriter` class.

        Args:
            transformers (List[Transformer]): List of `Transformer` objects
            file_name (str): OpenDSS filename to store the transformers
        """

        super().__init__()
        self.transformers = transformers
        self.file_name = file_name

__init__(transformers, file_name)

Constructor for TransformerWriter class.

Parameters:

Name Type Description Default
transformers List[Transformer]

List of Transformer objects

required
file_name str

OpenDSS filename to store the transformers

required
Source code in shift\exporter\opendss.py
181
182
183
184
185
186
187
188
189
190
191
def __init__(self, transformers: List[Transformer], file_name: str) -> None:
    """Constructor for `TransformerWriter` class.

    Args:
        transformers (List[Transformer]): List of `Transformer` objects
        file_name (str): OpenDSS filename to store the transformers
    """

    super().__init__()
    self.transformers = transformers
    self.file_name = file_name

TwoWindingSimpleTransformerWriter

Bases: TransformerWriter

Writer for two winding transformer.

Source code in shift\exporter\opendss.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class TwoWindingSimpleTransformerWriter(TransformerWriter):
    """Writer for two winding transformer."""

    def write(self, folder_location: str) -> None:
        """Refer to base class for more details."""
        super().write(folder_location)

        trans_contents = []
        for trans in self.transformers:
            bus = (
                f"{remove_invalid_chars(trans.longitude)}_"
                + f"{remove_invalid_chars(trans.latitude)}"
            )
            bus1 = f"{bus}_htnode.{trans.primary_phase.value}"
            bus2 = f"{bus}_ltnode.{trans.secondary_phase.value}"
            trans_contents.append(
                f"new transformer.{remove_invalid_chars(trans.name)} "
                + f"phases={trans.num_phase.value} buses=[{bus1}, {bus2}] "
                # pylint: disable-next=line-too-long
                + f"conns=[{trans.primary_con.value}, {trans.secondary_con.value}] "
                + f"kvs=[{trans.primary_kv}, {trans.secondary_kv}] "
                + f"kvas=[{trans.kva}, {trans.kva}] xhl={trans.xhl} "
                # pylint: disable-next=line-too-long
                + f"%noloadloss={trans.pct_noloadloss} %r={trans.pct_r} leadlag=lead\n\n"
            )

            self.coord_dict[bus1.split(".")[0]] = (
                trans.longitude,
                trans.latitude,
            )
            self.coord_dict[bus2.split(".")[0]] = (
                trans.longitude,
                trans.latitude,
            )

        if trans_contents:
            with open(
                os.path.join(folder_location, self.file_name),
                "w",
                encoding="utf-8",
            ) as fpointer:
                fpointer.writelines(trans_contents)
            self.files.append(self.file_name)

write(folder_location)

Refer to base class for more details.

Source code in shift\exporter\opendss.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
def write(self, folder_location: str) -> None:
    """Refer to base class for more details."""
    super().write(folder_location)

    trans_contents = []
    for trans in self.transformers:
        bus = (
            f"{remove_invalid_chars(trans.longitude)}_"
            + f"{remove_invalid_chars(trans.latitude)}"
        )
        bus1 = f"{bus}_htnode.{trans.primary_phase.value}"
        bus2 = f"{bus}_ltnode.{trans.secondary_phase.value}"
        trans_contents.append(
            f"new transformer.{remove_invalid_chars(trans.name)} "
            + f"phases={trans.num_phase.value} buses=[{bus1}, {bus2}] "
            # pylint: disable-next=line-too-long
            + f"conns=[{trans.primary_con.value}, {trans.secondary_con.value}] "
            + f"kvs=[{trans.primary_kv}, {trans.secondary_kv}] "
            + f"kvas=[{trans.kva}, {trans.kva}] xhl={trans.xhl} "
            # pylint: disable-next=line-too-long
            + f"%noloadloss={trans.pct_noloadloss} %r={trans.pct_r} leadlag=lead\n\n"
        )

        self.coord_dict[bus1.split(".")[0]] = (
            trans.longitude,
            trans.latitude,
        )
        self.coord_dict[bus2.split(".")[0]] = (
            trans.longitude,
            trans.latitude,
        )

    if trans_contents:
        with open(
            os.path.join(folder_location, self.file_name),
            "w",
            encoding="utf-8",
        ) as fpointer:
            fpointer.writelines(trans_contents)
        self.files.append(self.file_name)

remove_invalid_chars(name)

Removes invalid OpenDSS charaters from a given string.

Source code in shift\exporter\opendss.py
52
53
54
55
56
57
58
def remove_invalid_chars(name: str) -> str:
    """Removes invalid OpenDSS charaters from a given string."""

    name = str(name)
    for char in [".", " ", "!"]:
        name = name.replace(char, "_")
    return name