Skip to content

Commit

Permalink
updating the basic examples with octave compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MuMonish committed Aug 21, 2023
1 parent 875b2f2 commit 1debc81
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
24 changes: 17 additions & 7 deletions examples/pireceiver.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@
% if the condition is true and if so continues. If false, execution stops.

%% Initialize HELICS library in MATLAB
helicsStartup()
%helicsStartup()
%HELICS_PATH = 'C:\Users\mukh915\matHELICS\helics';
HELICS_PATH = '/home/helics-user/Softwares_user/matHELICS/';
addpath(HELICS_PATH)
% Checking if Octave or Matlab
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
if isOctave
% Using custom Import as the import function is not yet implemented in Octave
addpath(fullfile(HELICS_PATH, '+helics'))
else
import helics.*
end

import helics.*;
%% Configuration
deltat = 0.01; %Base time interval (seconds)
sim_stop_time = 20;

% HELICS options
helics_core_type = 'zmq';
helics_core_type = 'zmq';
fedinitstring = '--federates=1'; % required with current C interface when using separate processes for each federate

%% Provide summary information
Expand All @@ -39,16 +49,16 @@
helics.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring);


%% Set the message interval (timedelta) for federate.
%% Set the message interval (timedelta) for federate.
% Note:
% 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
% setTimedelta routine is a multiplier for the default timedelta
% (default unit = seconds).

% Set one message interval
helicsFederateInfoSetTimeProperty(fedinfo,helics_property_time_delta,deltat);
helicsFederateInfoSetIntegerProperty(fedinfo,helics_property_int_log_level,helics_log_level_warning);
helicsFederateInfoSetTimeProperty(fedinfo, HelicsProperties.HELICS_PROPERTY_TIME_DELTA, deltat);
helicsFederateInfoSetIntegerProperty(fedinfo, HelicsProperties.HELICS_PROPERTY_INT_LOG_LEVEL, HelicsLogLevels.HELICS_LOG_LEVEL_WARNING);



Expand Down
34 changes: 22 additions & 12 deletions examples/pisender.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
% PISENDER script demonstrating MATLAB-HELICS interface
%
% Usage:
% 1. Start two separate MATLAB terminals
% 2. In the first:
% 1. Start two separate MATLAB terminals
% 2. In the first:
% >> pisender
% 3. In the second:
% >> pireciever


%% Initialize HELICS library in MATLAB
helicsStartup()
%helicsStartup()
%HELICS_PATH = 'C:\Users\mukh915\matHELICS\helics'; % matHELICS path
HELICS_PATH = '/home/helics-user/Softwares_user/matHELICS/'; % matHELICS path
addpath(HELICS_PATH)
% Checking if Octave or Matlab
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
if isOctave
% Using custom Import as the import function is not yet implemented in Octave
addpath(fullfile(HELICS_PATH, '+helics'))
else
import helics.*
end

import helics.*;
%% Configuration
deltat = 1; %Base time interval (seconds)
numsteps = 20;
Expand All @@ -20,7 +30,7 @@
% Note: these configure this matlab process to host the main broker and 1
% federate
pisend_start_broker = true;
helics_core_type = 'zmq';
helics_core_type = 'zmq';
broker_initstring = '--federates 2 --name=mainbroker';
fed_initstring = '--broker=mainbroker --federates=1';

Expand Down Expand Up @@ -59,19 +69,19 @@
% Note:
% 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
% setTimedelta routine is a multiplier for the default timedelta
% (default unit = seconds).

% Set one message interval
helicsFederateInfoSetTimeProperty(fedinfo,helics_property_time_delta,deltat);
helicsFederateInfoSetIntegerProperty(fedinfo,helics_property_int_log_level,helics_log_level_warning);
helicsFederateInfoSetTimeProperty(fedinfo, HelicsProperties.HELICS_PROPERTY_TIME_DELTA, deltat);
helicsFederateInfoSetIntegerProperty(fedinfo, HelicsProperties.HELICS_PROPERTY_INT_LOG_LEVEL, HelicsLogLevels.HELICS_LOG_LEVEL_WARNING);

%% Actually create value federate
vfed = helicsCreateValueFederate('MATLAB Pi SENDER Federate',fedinfo);
disp('PI SENDER: Value federate created');

%% Register our value to publish
pub = helicsFederateRegisterGlobalPublication(vfed, 'testA', helics_data_type_double, '');
pub = helicsFederateRegisterGlobalPublication(vfed, 'testA', HelicsDataTypes.HELICS_DATA_TYPE_DOUBLE, '');
disp('PI SENDER: Publication registered (testA)');

%% Start execution
Expand Down Expand Up @@ -111,11 +121,11 @@
helicsBrokerWaitForDisconnect(broker,-1);
disp('PI SENDER: Broker disconnected');

helics.helicsFederateFree(vfed);
helics.helicsCloseLibrary();
helicsFederateFree(vfed);
helicsCloseLibrary();
else
%But if we just setup the federate, we can simply call endFederate
helicsFederateDestroy(vfed); %#ok<UNRCH>
helicsFederateDestroy(vfed); %#ok<UNRCH>
disp('PI SENDER: Federate finalized');
end

Expand Down
19 changes: 15 additions & 4 deletions examples/simpleFederate.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
%% a single federate example for HELICS in matlab

helicsStartup
import helics.*
%helicsStartup
%HELICS_PATH = 'C:\Users\mukh915\matHELICS\helics'; % matHELICS path
HELICS_PATH = '/home/helics-user/Softwares_user/matHELICS/'; % matHELICS path
addpath(HELICS_PATH)
% Checking if Octave or Matlab
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
if isOctave
% Using custom Import as the import function is not yet implemented in Octave
addpath(fullfile(HELICS_PATH, '+helics'))
else
import helics.*
end

% create a broker so we don't have to do this separately
brk=helicsCreateBroker('','B1','-f1');
% create a combination federate so we add both endpoint and
% publications/inputs
fi=helicsCreateFederateInfo();
fed1=helicsCreateCombinationFederate('fed1',fi);
helicsFederateSetFlagOption(fed1,HelicsFederateFlags.HELICS_FLAG_UNINTERRUPTIBLE,HELICS_TRUE)
helicsFederateSetFlagOption(fed1, HelicsFederateFlags.HELICS_FLAG_UNINTERRUPTIBLE, HELICS_TRUE)
% register Endpoint1/2
e1=helicsFederateRegisterGlobalEndpoint(fed1,'Endpoint1','');
e2=helicsFederateRegisterGlobalEndpoint(fed1,'Endpoint2','');
Expand Down Expand Up @@ -58,5 +69,5 @@
%now the broker should terminate the cosimulation
helicsBrokerWaitForDisconnect(brk,0);
%close up the library and clean up any memory use that hasn't been cleaned
%already.
%already.
helicsCloseLibrary();

0 comments on commit 1debc81

Please sign in to comment.