Skip to content

Instantly share code, notes, and snippets.

@kdheepak
Created August 24, 2018 17:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kdheepak/7bb98f8f0a994cc5a2ee981a7980dae3 to your computer and use it in GitHub Desktop.
ComboFederate Pi Exchange example
import helics as h
fedinitstring = "--federates=1"
deltat = 0.01
helicsversion = h.helicsGetVersion()
print("PI RECEIVER: Helics version = {}".format(helicsversion))
# Create Federate Info object that describes the federate properties */
print("PI RECEIVER: Creating Federate Info")
fedinfo = h.helicsFederateInfoCreate()
# Set Federate name
print("PI RECEIVER: Setting Federate Info Name")
status = h.helicsFederateInfoSetFederateName(fedinfo, "TestB Federate")
# Set core type from string
print("PI RECEIVER: Setting Federate Info Core Type")
status = h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq")
# Federate init string
print("PI RECEIVER: Setting Federate Info Init String")
status = 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("PI RECEIVER: Setting Federate Info Time Delta")
status = h.helicsFederateInfoSetTimeDelta(fedinfo, deltat)
print("PI RECEIVER: Setting Federate Info Logging")
status = h.helicsFederateInfoSetLoggingLevel(fedinfo, 1)
# Create value federate
print("PI RECEIVER: Creating Value Federate")
fed = h.helicsCreateCombinationFederate(fedinfo)
print("PI RECEIVER: Value federate created")
# Subscribe to PI SENDER's publication
sub = h.helicsFederateRegisterSubscription(fed, "testA", "double", "")
print("PI RECEIVER: Subscription registered")
epid = h.helicsFederateRegisterGlobalEndpoint(fed, "endpoint1", "")
fid = h.helicsFederateRegisterSourceFilter(fed, h.helics_delay_filter, "endpoint2", "filter-name")
h.helicsFilterSet(fid, "delay", 2.0)
status = h.helicsFederateEnterExecutionMode(fed)
print("PI RECEIVER: Entering execution mode")
value = 0.0
prevtime = 0
currenttime = h.helicsFederateRequestTime(fed, 100)[-1]
print("PI RECEIVER: Current time is {} ".format(currenttime))
isupdated = h.helicsSubscriptionIsUpdated(sub)
if (isupdated == 1):
result, value = h.helicsSubscriptionGetDouble(sub)
print("PI RECEIVER: Received value = {} at time {} from PI SENDER".format(value, currenttime))
while (currenttime <= 100):
currenttime = h.helicsFederateRequestTime(fed, 100)[-1]
isupdated = h.helicsSubscriptionIsUpdated(sub)
print(currenttime)
if (isupdated == 1):
result, value = h.helicsSubscriptionGetDouble(sub)
print("PI RECEIVER: Received value = {} at time {} from PI SENDER".format(value, currenttime))
status = h.helicsFederateFinalize(fed)
h.helicsFederateFree(fed)
h.helicsCloseLibrary()
print("PI RECEIVER: Federate finalized")
import time
import helics as h
from math import pi
initstring = "2 --name=mainbroker"
fedinitstring = "--broker=mainbroker --federates=1"
deltat = 0.01
helicsversion = h.helicsGetVersion()
print("PI SENDER: Helics version = {}".format(helicsversion))
# Create broker #
print("Creating Broker")
broker = h.helicsCreateBroker("zmq", "", initstring)
print("Created Broker")
print("Checking if Broker is connected")
isconnected = h.helicsBrokerIsConnected(broker)
print("Checked if Broker is connected")
if isconnected == 1:
print("Broker created and connected")
# Create Federate Info object that describes the federate properties #
fedinfo = h.helicsFederateInfoCreate()
# Set Federate name #
status = h.helicsFederateInfoSetFederateName(fedinfo, "TestA Federate")
# Set core type from string #
status = h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq")
# Federate init string #
status = h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring)
# Set the message interval (timedelta) for federate. Note th#
# 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 #
status = h.helicsFederateInfoSetTimeDelta(fedinfo, deltat)
status = h.helicsFederateInfoSetLoggingLevel(fedinfo, 1)
# Create value federate #
fed = h.helicsCreateCombinationFederate(fedinfo)
print("PI SENDER: Value federate created")
# Register the publication #
pub = h.helicsFederateRegisterGlobalPublication(fed, "testA", "double", "")
print("PI SENDER: Publication registered")
epid = h.helicsFederateRegisterGlobalEndpoint(fed, "endpoint2", "")
# Enter execution mode #
status = h.helicsFederateEnterExecutionMode(fed)
print("PI SENDER: Entering execution mode")
# This federate will be publishing deltat*pi for numsteps steps #
this_time = 0.0
value = pi
for t in range(5, 10):
val = value
currenttime = h.helicsFederateRequestTime(fed, t)
status = h.helicsPublicationPublishDouble(pub, val)
print("PI SENDER: Sending value pi = {} at time {} to PI RECEIVER".format(val, currenttime[-1]))
status = h.helicsEndpointSendMessageRaw(epid, "endpoint1", str(val))
time.sleep(1)
status = h.helicsFederateFinalize(fed)
print("PI SENDER: Federate finalized")
while (h.helicsBrokerIsConnected(broker)):
time.sleep(1)
h.helicsFederateFree(fed)
h.helicsCloseLibrary()
print("PI SENDER: Broker disconnected")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment