Skip to content
Snippets Groups Projects
Commit 31bfba7d authored by Federico Felici's avatar Federico Felici
Browse files

Added .slx running to tests

parent 201b26fd
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ classdef SCDsignal
if numel(varargin)>=2 && ~isempty(varargin{2}), S.QualityTag = varargin{2}; end
if numel(varargin)>=3 && ~isempty(varargin{3}), S.ProductionState = varargin{3}; end
siz = S.size;
siz = S.datasize;
assert(~(siz(1)==1 && siz(2)>1),'SCDsignal:RowVector',...
'Row vectors are not supported due to matlab bug. Size=[%d %d]',siz(1),siz(2));
% to reproduce the bug:
......@@ -34,13 +34,13 @@ classdef SCDsignal
function dimension = dimension(obj)
% dimension as required by buses etc.
siz = size(obj);
siz = obj.datasize;
if siz(2) == 1, dimension = siz(1);
else, dimension = siz;
end
end
function siz = size(obj,dim)
function siz = datasize(obj,dim)
% size of data
if nargin==1, siz = size(obj.Value);
else, siz = size(obj.Value,dim);
......@@ -54,18 +54,26 @@ classdef SCDsignal
valid = isRunning && isGood;
end
function [myBusObj] = createBus(obj)
% creates a bus object corresponding to this signal
tmp = Simulink.Bus.createObject(struct(obj));
myBusObj = evalin('base',tmp.busName);
evalin('base',sprintf('clear %s',tmp.busName));
myBusObj.Description = obj.BusName;
% 2D to 1D where possible
for iEl = 1:numel(myBusObj.Elements)
dims = myBusObj.Elements(iEl).Dimensions;
if all(dims == 1)
myBusObj.Elements(iEl).Dimensions = dims(1);
function [BusesObj] = createBus(obj)
BusesObj = repmat(Simulink.Bus,size(obj));
if numel(obj)>1 % for arrays of SCDsignal objects
for ii = 1:numel(obj)
BusesObj(ii) = createBus(obj(ii)); % recursive call
end
else
% creates a bus object corresponding to this signal class
tmp = Simulink.Bus.createObject(struct(obj));
myBusObj = evalin('base',tmp.busName);
evalin('base',sprintf('clear %s',tmp.busName));
myBusObj.Description = obj.BusName;
% 2D to 1D where possible
for iEl = 1:numel(myBusObj.Elements)
dims = myBusObj.Elements(iEl).Dimensions;
if dims(2) == 1
myBusObj.Elements(iEl).Dimensions = dims(1);
end
end
BusesObj = myBusObj;
end
end
......
classdef SCDsignal_test < matlab.unittest.TestCase
properties (MethodSetupParameter)
type = {'single','double','logical'}
m = {1,2,100};
n = {1,2};
type = {'single','double','logical','double'};
m = {1, 100, 3, 3 };
n = {1, 1, 3, 10 };
end
properties
sig; % signal object to be tested
bus; % associated bus
modelname = 'SCDsignal_tester'; % slx model name for signal testing
end
methods (TestMethodSetup)
function check_size(testCase,m,n,type)
methods (TestClassSetup)
function load_slx(testCase)
% load SLX used for testing
load_system(testCase.modelname);
end
end
methods (TestMethodSetup,ParameterCombination = 'sequential')
function build_sig_and_bus(testCase,m,n,type)
% build signal and associated bus used for testing
testCase.assumeFalse(n>1 && m==1,'skip row vector cases due to matlab bug');
data = cast(rand(m,n),type); % example single time slice data for this signal
......@@ -30,8 +39,16 @@ classdef SCDsignal_test < matlab.unittest.TestCase
end
end
methods (TestClassTeardown)
function close_simulink(testCase)
% close open simulink model
close_system(testCase.modelname,0);
end
end
methods(Test)
function test_creation(testCase)
function test_bus_size(testCase)
% test size of bus
S = testCase.sig;
sigBus = testCase.bus;
......@@ -43,28 +60,88 @@ classdef SCDsignal_test < matlab.unittest.TestCase
end
function test_slx(testCase)
% test using bus and signals in an SLX simulation
S = testCase.sig;
% dummy simulation data
value = timeseries(cast(rand(S.size),S.datatype),0);
quality = timeseries(QualityTag(0));
state = timeseries(ProductionState(0));
%% Prepare dummy simulation data
time = 0:1:10; % time vector
dataslice = cast(rand(S.datasize),S.datatype);
if S.datasize(2)==1 % scalar or vector
data = repmat(dataslice',numel(time),1);
else % matrix
data = repmat(dataslice,1,1,numel(time));
end
% timeseries
tsvalue = timeseries(data,time);
tsquality = timeseries(QualityTag(0),time);
tsstate = timeseries(ProductionState(0),time);
% use struct
datastruct.Value = value;
datastruct.Tag = quality;
datastruct.State = state;
% build structure of timeseries: use struct
datastruct.Value = tsvalue;
datastruct.Tag = tsquality;
datastruct.State = tsstate;
assignin('base','sigBus',testCase.bus); % bus must be in base workspace for method below to work
inputSignal1 = Simulink.SimulationData.createStructOfTimeseries('sigBus',datastruct);
% use cells
datacell = {value,quality,state};
% build structure of timeseries: use cells
datacell = {tsvalue,tsquality,tsstate};
inputSignal2 = Simulink.SimulationData.createStructOfTimeseries('sigBus',datacell);
% check that both methods give the same result
testCase.verifyEqual(inputSignal1,inputSignal2);
%% Send signal to test slx and test throughput
busname = 'SCDbusToBeTested';
assignin('base',busname,testCase.bus)
% SimIn object to configure Simulation
SimIn = Simulink.SimulationInput(testCase.modelname);
% Create dataset
Dataset = createInputDataset(testCase.modelname);
% assign input data signal to Simulink.Signal object
DataIn = Simulink.SimulationData.Signal;
DataIn.Values = inputSignal1;
% assign as first element in input data set
Dataset = Dataset.setElement(1,DataIn);
% assign this as external input for simulation
SimIn.ExternalInput = Dataset;
% Custom parameters for this run to save outport data
SimIn = SimIn.setModelParameter('SaveOutput','on'); % set to save outport signals
SimIn = SimIn.setModelParameter('OutputSaveName','SimOut');
SimIn = SimIn.setModelParameter('SaveFormat','Dataset');
SimIn = SimIn.setModelParameter('StartTime',num2str(time(1)));
SimIn = SimIn.setModelParameter('StopTime',num2str(time(end)));
SimIn = SimIn.setModelParameter('FixedStep',num2str(mean(diff(time))));
% simulate - simulate only single types to save time
result = sim(SimIn);
% check that output data came through unchanged
out = result.SimOut{1}.Values;
in = inputSignal1;
testCase.verifyEqual(in.Value.data,out.Value.data,...
'input and output values not equal');
testCase.verifyEqual(in.ProductionState.Data,out.ProductionState.Data,...
'input and output ProductionStates not equal');
testCase.verifyEqual(in.QualityTag.Data,out.QualityTag.Data,...
'input and output QualityTag not equal');
end
function test_bus_of_signals(testCase)
% test for a bus consisting of several SCDsignals
S1 = testCase.sig;
data2 = ones(2*S1.datasize(1),S1.datasize(2),'int32');
S2 = SCDsignal(data2);
% Bus of buses
S = struct('S1',{S1},'S2',{S2});
end
end
end
\ No newline at end of file
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment