<jupyter_start><jupyter_text>OpenDSS<jupyter_code>import opendssdirect as odd
odd.__version__
odd.run_command("redirect ../../data/IEEE13/IEEE13Nodeckt.dss")
odd.run_command("Solve")
odd.Circuit.SetActiveClass("Vsource")

odd.Circuit.SetActiveElement("source")
odd.CktElement.Name()
BASEKV = odd.Vsources.BasekV()
odd.Vsources.BasekV(110)
odd.Vsources.BasekV()
odd.Vsources.AngleDeg()
odd.Vsources.AngleDeg(25)
odd.Vsources.AngleDeg()
# -*- coding: utf-8 -*-
import helics as h

fedinitstring = "--federates=1"
deltat = 0.01

helicsversion = h.helicsGetVersion()<jupyter_output><empty_output><jupyter_text>Create the federate Info object<jupyter_code># Create Federate Info object that describes the federate properties */
print("Creating Federate Info")
fedinfo = h.helicsCreateFederateInfo()

# Set Federate name
print("Setting Federate Info Name")
h.helicsFederateInfoSetCoreName(fedinfo, "OpenDSSFederate")

# Set core type from string
print("Setting Federate Info Core Type")
h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq")

# Federate init string
print("Setting Federate Info Init String")
h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring)

# Set the message interval (timedelta) for federate. Note that
# HELICS minimum message time interval is 1 ns and by default
# it uses a time delta of 1 second. What is provided to the
# setTimedelta routine is a multiplier for the default timedelta.

# Set one second message interval
print("Setting Federate Info Time Delta")
h.helicsFederateInfoSetTimeProperty(fedinfo, h.helics_property_time_delta, deltat)<jupyter_output><empty_output><jupyter_text>Create the value federate<jupyter_code>vfed = h.helicsCreateValueFederate("OpenDSSFederate", fedinfo)
sub = h.helicsFederateRegisterSubscription(vfed, "voltage", "")
pub = h.helicsFederateRegisterGlobalTypePublication(vfed, "load", "complex", "")<jupyter_output><empty_output><jupyter_text>Enter execution<jupyter_code>h.helicsFederateEnterExecutingMode(vfed)<jupyter_output><empty_output><jupyter_text>Start simulation<jupyter_code>import numpy as np

def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)
value = 0.0
prevtime = 0

granted_time = -1

for request_time in range(1, 300):

    while granted_time < request_time:
        granted_time = h.helicsFederateRequestTime(vfed, request_time)

    voltage, angle = h.helicsInputGetComplex(sub)

    odd.Circuit.SetActiveClass("Vsource")
    odd.Circuit.SetActiveElement("source")
    odd.Vsources.AngleDeg(angle)
    odd.Vsources.BasekV(voltage * BASEKV)
    print("Source voltage mag at time {}: {} ".format(request_time, voltage))
    print("Source voltage ang at time {}: {} ".format(request_time, angle))
    odd.run_command("Solve")
    
    odd.Circuit.SetActiveClass("Line")
    odd.Circuit.SetActiveElement("650632")
    S = sum([complex(x,y) for x, y in zip(odd.CktElement.Powers()[0:6], odd.CktElement.Powers()[1:6])])
    Pg = S.real
    Qg = S.imag
    print("Active power at time {}: {} ".format(request_time, Pg))
    print("Reactive power at time {}: {} ".format(request_time, Qg))
    
    h.helicsPublicationPublishComplex(pub, Pg / 1000, Qg / 1000)
    
    print()


h.helicsFederateFinalize(vfed)

h.helicsFederateFree(vfed)
h.helicsCloseLibrary()
print("Federate finalized")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12037037037037036, 'fraction_numerical': 0.013739545997610514, 'mean_word_length': 5.752016129032258, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '222549', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1165, 'n_words': 240, 'path': 'notebooks/OpenDSSDirect_PyPower_CoSim/opendssdirect.ipynb', 'repo_name': 'GMLC-TDC/pesgm-2019-helics-tutorial', 'size': '3348'}