def test_value_federate_subscription_and_publication_registration(vFed): pubid3 = h.helicsFederateRegisterTypePublication(vFed, "pub3", "double", "V") subid1 = h.helicsFederateRegisterSubscription(vFed, "sub1", "") subid2 = h.helicsFederateRegisterSubscription(vFed, "sub2", "") subid3 = h.helicsFederateRegisterSubscription(vFed, "sub3", "V") h.helicsFederateEnterExecutingMode(vFed) publication_type = h.helicsPublicationGetType(pubid3) assert publication_type == 'double' sub_key = h.helicsSubscriptionGetKey(subid1) assert sub_key == 'sub1' sub_type = h.helicsInputGetType(subid1) assert sub_type == '' sub_key = h.helicsSubscriptionGetKey(subid2) assert sub_key == 'sub2' sub_key = h.helicsSubscriptionGetKey(subid3) assert sub_key == 'sub3' sub_type = h.helicsInputGetType(subid3) assert sub_type == '' sub_units = h.helicsInputGetUnits(subid3) assert sub_units == 'V' sub_type = h.helicsInputGetType(subid2) assert sub_type == ''
def test_valuefederate_subscription_and_publication_registration(): broker = createBroker() vFed, fedinfo = createValueFederate(1, "fed0") pubid = h.helicsFederateRegisterPublication(vFed, "pub1", h.HELICS_DATA_TYPE_STRING, "") pubid2 = h.helicsFederateRegisterGlobalPublication(vFed, "pub2", h.HELICS_DATA_TYPE_INT, "volts") pubid3 = h.helicsFederateRegisterTypePublication(vFed, "pub3", "double", "V") subid1 = h.helicsFederateRegisterSubscription(vFed, "sub1", "") subid2 = h.helicsFederateRegisterSubscription(vFed, "sub2", "") subid3 = h.helicsFederateRegisterSubscription(vFed, "sub3", "V") h.helicsFederateEnterExecutingMode(vFed) publication_type = h.helicsPublicationGetType(pubid3) assert publication_type == "double" sub_key = h.helicsSubscriptionGetKey(subid1) assert sub_key == "sub1" sub_type = h.helicsInputGetType(subid1) assert sub_type == "" sub_key = h.helicsSubscriptionGetKey(subid2) assert sub_key == "sub2" sub_key = h.helicsSubscriptionGetKey(subid3) assert sub_key == "sub3" sub_type = h.helicsInputGetType(subid3) assert sub_type == "" sub_units = h.helicsInputGetUnits(subid3) assert sub_units == "V" sub_type = h.helicsInputGetType(subid2) assert sub_type == "" subid_b = h.helicsFederateGetSubscription(vFed, "sub1") tmp = h.helicsSubscriptionGetKey(subid_b) assert tmp == "sub1" # check the getSubscriptionByIndex function subid_c = h.helicsFederateGetInputByIndex(vFed, 2) tmp = h.helicsInputGetUnits(subid_c) assert tmp == "V" # check publications sv = h.helicsPublicationGetKey(pubid) sv2 = h.helicsPublicationGetKey(pubid2) assert sv == "Testfed0/pub1" assert sv2 == "pub2" pub3name = h.helicsPublicationGetKey(pubid3) assert pub3name == "Testfed0/pub3" type = h.helicsPublicationGetType(pubid3) assert type == "double" units = h.helicsPublicationGetUnits(pubid3) assert units == "V" # check the getSubscription function pubid_b = h.helicsFederateGetPublication(vFed, "Testfed0/pub1") tmp = h.helicsPublicationGetType(pubid_b) assert tmp == "string" # check the getSubscriptionByIndex function pubid_c = h.helicsFederateGetPublicationByIndex(vFed, 1) tmp = h.helicsPublicationGetUnits(pubid_c) assert tmp == "volts" # this one should be invalid # @test_throws h.HELICSErrorInvalidArgument pubid_d = h.helicsFederateGetPublicationByIndex(vFed, 5) h.helicsFederateFinalize(vFed) state = h.helicsFederateGetState(vFed) assert state == h.HELICS_STATE_FINALIZE destroyFederate(vFed, fedinfo) destroyBroker(broker)
def test_valuefederate_default_value_tests(): broker = createBroker() vFed1, fedinfo = createValueFederate(1, "fed0") inp_raw1 = h.helicsFederateRegisterInput(vFed1, "key1", h.HELICS_DATA_TYPE_RAW, "raw") inp_raw2 = h.helicsFederateRegisterInput(vFed1, "key2", h.HELICS_DATA_TYPE_RAW, "raw") inp_bool = h.helicsFederateRegisterInput(vFed1, "key3", h.HELICS_DATA_TYPE_BOOLEAN, "") inp_time = h.helicsFederateRegisterInput(vFed1, "key4", h.HELICS_DATA_TYPE_TIME, "") inp_char = h.helicsFederateRegisterInput(vFed1, "key5", h.HELICS_DATA_TYPE_STRING, "") inp_vect = h.helicsFederateRegisterInput(vFed1, "key6", h.HELICS_DATA_TYPE_VECTOR, "V") inp_double = h.helicsFederateRegisterInput(vFed1, "key7", h.HELICS_DATA_TYPE_DOUBLE, "kW") inp_double2 = h.helicsFederateRegisterInput(vFed1, "key8", h.HELICS_DATA_TYPE_DOUBLE, "") inp_np = h.helicsFederateRegisterInput(vFed1, "key9", h.HELICS_DATA_TYPE_NAMED_POINT, "") h.helicsInputSetMinimumChange(inp_double, 1100.0) h.helicsInputSetDefaultDouble(inp_double, 10000.0) h.helicsInputSetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True) pub = h.helicsFederateRegisterPublication(vFed1, "", h.HELICS_DATA_TYPE_INT, "MW") h.helicsPublicationSetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True) h.helicsPublicationAddTarget(pub, "Testfed0/key7") h.helicsPublicationAddTarget(pub, "Testfed0/key8") h.helicsInputSetDefaultRaw(inp_raw1, "") data = "this is a string" h.helicsInputSetDefaultRaw(inp_raw2, data) h.helicsInputSetDefaultBoolean(inp_bool, True) h.helicsInputSetDefaultTime(inp_time, 12.3) h.helicsInputSetDefaultChar(inp_char, "q") h.helicsInputSetDefaultVector(inp_vect, []) h.helicsInputSetDefaultNamedPoint(inp_np, data, 15.7) h.helicsFederateEnterExecutingMode(vFed1) assert h.helicsInputGetInjectionUnits(inp_double) == "MW" assert h.helicsInputGetInjectionUnits(inp_double2) == "MW" assert h.helicsInputGetType(inp_double) == "double" assert h.helicsInputGetPublicationType(inp_double) == "int64" c2 = h.helicsInputGetChar(inp_char) assert c2 == "q" h.helicsInputGetVector(inp_vect) optset = h.helicsInputGetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED) assert optset == 1 optset = h.helicsPublicationGetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED) assert optset == 1 h.helicsPublicationPublishInteger(pub, 12) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputGetDouble(inp_double) == 12000.0 assert h.helicsInputGetDouble(inp_double2) == 12.0 h.helicsPublicationPublishInteger(pub, 13) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputIsUpdated(inp_double) is False assert h.helicsInputIsUpdated(inp_double2) is True assert h.helicsInputGetDouble(inp_double) == 12000.0 assert h.helicsInputGetDouble(inp_double2) == 13.0 h.helicsPublicationPublishInteger(pub, 15) h.helicsFederateRequestNextStep(vFed1) assert h.helicsInputIsUpdated(inp_double) is True assert h.helicsInputIsUpdated(inp_double2) is True h.helicsInputClearUpdate(inp_double) h.helicsInputClearUpdate(inp_double2) assert h.helicsInputIsUpdated(inp_double) is False assert h.helicsInputIsUpdated(inp_double2) is False _, rval = h.helicsInputGetNamedPoint(inp_np) assert rval == 15.7 out, rval = h.helicsInputGetNamedPoint(inp_np) assert out == "this is a string" assert rval == 15.7 h.helicsFederateFinalize(vFed1) destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def register_inputs(self, config=None, callback=None, **kwargs): """ Register configuration parameters with HELICS. The config parameters may include but not limited to: 1. Name of the federate 2. simulation length 2. Type of core to use (zmq/tcp/udp etc) 3. list (and type) of subscriptions 4. list (and type) of publications 5. broker address (if not default) :param config: config parameters :param callback: Register agent callback method :return: """ self._work_callback = callback # Build HELICS config from agent config helics_config = deepcopy(config) properties = helics_config.pop('properties', {}) if not properties: raise RuntimeError( "Invalid configuration. Missing properties dictionary") self._simulation_delta = properties.pop('timeDelta', 1.0) # seconds self._simulation_length = properties.pop('simulation_length', 3600) # seconds for key, value in properties.items(): helics_config[key] = value subscriptions = helics_config.pop('outputs', []) for sub in subscriptions: volttron_topic = sub.pop('volttron_topic', None) if volttron_topic is not None: self.helics_to_volttron_publish[sub.get( 'key')] = volttron_topic sub['key'] = sub.pop('sim_topic') # Replace 'outputs' key with 'subscriptions' key if subscriptions: helics_config['subscriptions'] = subscriptions publications = helics_config.pop('inputs', []) for pub in publications: volttron_topic = pub.pop('volttron_topic', None) pub['key'] = pub.pop('sim_topic') # Replace 'inputs' key with 'publications' key if publications: helics_config['publications'] = publications _log.debug("new config: {}".format(helics_config)) # Create a temporary json file tmp_file = os.path.join(os.getcwd(), 'fed_cfg.json') _log.debug("tmp file: {}".format(tmp_file)) with open(tmp_file, 'w') as fout: fout.write(jsonapi.dumps(helics_config)) _log.debug("Create Combination Federate") # Create federate from provided config parameters try: self.fed = h.helicsCreateCombinationFederateFromConfig(tmp_file) except h._helics.HelicsException as e: _log.exception("Error parsing HELICS config {}".format(e)) # Check if HELICS broker correctly registered inputs federate_name = h.helicsFederateGetName(self.fed) _log.debug("Federate name: {}".format(federate_name)) endpoint_count = h.helicsFederateGetEndpointCount(self.fed) _log.debug("Endpoint count: {}".format(endpoint_count)) subkeys_count = h.helicsFederateGetInputCount(self.fed) _log.debug("Subscription key count: {}".format(subkeys_count)) pubkeys_count = h.helicsFederateGetPublicationCount(self.fed) _log.debug("Publication key count: {}".format(endpoint_count)) for i in range(0, endpoint_count): try: endpt_idx = h.helicsFederateGetEndpointByIndex(self.fed, i) endpt_name = h.helicsEndpointGetName(endpt_idx) self.endpoints[endpt_name] = endpt_idx except h._helics.HelicsException as e: _log.exception( "Error getting helics endpoint index: {}".format(e)) for i in range(0, subkeys_count): inputs = dict() try: idx = h.helicsFederateGetInputByIndex(self.fed, i) inputs['sub_id'] = idx inputs['type'] = h.helicsInputGetType(idx) inputs['key'] = h.helicsSubscriptionGetKey(idx) self.inputs.append(inputs) data = dict(type=inputs['type'], value=None) except h._helics.HelicsException as e: _log.exception( "Error getting helics input index: {}".format(e)) for i in range(0, pubkeys_count): outputs = dict() try: idx = h.helicsFederateGetPublicationByIndex(self.fed, i) outputs['pub_id'] = idx outputs['type'] = h.helicsPublicationGetType(idx) pub_key = h.helicsPublicationGetKey(idx) _log.debug("Publication: {}".format(pub_key)) self.outputs[pub_key] = outputs data = dict(type=outputs['type'], value=None) except h._helics.HelicsException as e: _log.exception( "Error getting helics publication index: {}".format(e))