Top Banner
1 Clément DAVID & Yann COLLETTE The Scilab Consortium Xcos Hybrid dynamic systems modeler and simulator 16 th june 2010
31

Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

Sep 01, 2018

Download

Documents

voxuyen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

1

Clément DAVID &Yann COLLETTE

The Scilab Consortium

XcosHybrid dynamic systems modeler and

simulator

16th june 2010

Page 2: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

2

● Internal Scilab module

Freely available with Scilab (free and open platform for numerical computation)

● Purposes

Hybrid dynamic systems modeler and simulator

Using functional black-boxes and data/event links

Xcos: Introduction

Page 3: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

3

Xcos: Features

● Graphically model, compile, and simulate dynamical systems

Combine continuous and discrete-time behaviors in the same model

Select model elements from Palettes of standard blocks

Program new blocks in C, Fortran, or Scilab Language

● Use standards

HDF5 standard which has been chosen to guarantee data exchanges between Scilab and Xcos Editor

A free Modelica compiler which enables the simulation of implicit diagrams

A brand new graphical user interface based on JGraphX

Page 4: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

4

Xcos: Overview

Page 5: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

5

Xcos: Overview

Page 6: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

6

Xcos: Overview

Page 7: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

7

Xcos: Overview

Page 8: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

8

Xcos: Overview

Page 9: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

9

Xcos: Overview

Page 10: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

10

Xcos: Overview

Page 11: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

11

Xcos: Overview

Page 12: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

12

Xcos: A simple block (1)

● Data links and data ports

Data communication is handled by data ports and data links (in black)

● Event links and event ports (discrete time)

Activation and control is performed through event ports and links (in red).

Xcos blockInputs Outputs

Controls

Commands

Page 13: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

13

Xcos: A simple block (2)

● An interface function (Scilab macro)

Used to configure the block when editing the diagram

● A functional implementation

Used to simulate the block in the simulation engine

Inputs Outputs

Controls

Commands

parametersmanagement

BLOCK.sci

runtime evaluationfunction

block.c

Page 14: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

14

Xcos editor

Scicos compiler

ScilabScicos

simulatorCode

generation

● Java-based diagram editor● Ergonomic● Run in parallel with Scilab interpreter● Backward compatibility

Xcos: the new editor

Page 15: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

15

Xcos: Reusing Scilab quality process

● Available in nightly builds

● Integrated in Scilab roadmap

● Use Scilab development flow● Git revision system● Code review● Automatic nightly validation

Page 16: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

16

Demo time,

any questions ?

www.scilab.org

Page 17: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

17

ScilabTec 2010

Optimization of a PID regulator

Page 18: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

18

Optimization of a PID regulator

The content of the xcos context:

if ~exists('w0') then w0 = 2*%pi*100; endif ~exists('K0') then K0 = 0.01; endif ~exists('m') then m = 0.5; endif ~exists('P') then P = 1; endif ~exists('I') then I = 1; endblock_output('values') = zeros(2000,2);block_output('times') = zeros(2000,1);

Page 19: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

19

Optimization of a PID regulator

importXcosDiagram('automatic_test.xcos');

function y = f_pid(x)context.w0 = w0;context.m = m;context.K0 = K0;context.P = x(1)*Pfact;context.I = x(2)*Ifact;Info = scicos_simulate(scs_m,list(),context,flag='nw');y_error = mean(abs((block_output('values')(:,1) - block_output('values')(:,2))));y_diff = mean(abs(diff(block_output('values')(:,2))));y = 0.5*y_error + 0.5*1*y_diff; .

endfunction

Page 20: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

20

Optimization of a PID regulator

Before optimization After optimization

Page 21: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

21

Demo time,

any questions ?

www.scilab.org

Page 22: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

22

ScilabTec 2010

Programming new blocks

Page 23: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

23

Definition of a new C blockGoal:Store N samples of the input in a bufferOuput the buffer as a vector output of size N

Parameters:Padding value (real parameter - rpar)Buffer size (int parameter - ipar)

We will first interface our block through a GENERIC block (In the user defined palette).

00,51

0,50

-0,5-1

-0,50

0,51

Buffer size

Page 24: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

24

Definition of a new C blockThe simulation function side

#include "scicos_block4.h"

void buffer_vect_xcos(scicos_block *block, int flag){ int nsamples = GetIparPtrs(block)[0]; double padding_value = GetRparPtrs(block)[0];

switch(flag) {

case Initialization: GetWorkPtrs(block) = (double *)MALLOC(sizeof(double)*nsamples); for(int i=0;i<nsamples;i++) ((double *)GetWorkPtrs(block))[i] = padding_value; break;

1

2

case StateUpdate: for(int i=1;i<nsamples;i++) ((double *)GetWorkPtrs(block))[i-1] = ((double *)GetWorkPtrs(block))[i]; ((double *)GetWorkPtrs(block))[nsamples-1] = ((double *)GetInPortRows(block,1))[0]; break;

3

4

1 – Initialization2 – Simulation finalization3 – Update block internal state4 – Put the value on the output port

case Ending: FREE(GetWorkPtrs(block)); break;

case OutputUpdate: for(int i=0;i<nsamples;i++) ((double *)GetOutPortPtrs(block,1))[i] = ((double *)GetWorkPtrs(block))[i]; break;

} }

Page 25: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

25

Definition of a new C blockThe interface function side (1/2)

function [x,y,typ]=BUFFERVECT_c(job,arg1,arg2)...select jobcase 'plot' then standard_draw(arg1)case 'getinputs' then [x,y,typ] = standard_inputs(arg1)case 'getoutputs' then [x,y,typ] = standard_outputs(arg1)case 'getorigin' then [x,y] = standard_origin(arg1)case 'set' then

case 'define' then

endendfunction

A

B

Page 26: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

26

Definition of a new C blockThe interface function side (2/2)

case 'define' then buf_size = 1; pad_val = 0.0;

model = scicos_model(); model.sim = list('buffer_vect',4); model.in = 1; model.in2 = 1; model.intyp = 1; model.outtyp = 1; model.out = buf_size; model.out2 = 1; model.evtin = 1; model.ipar = [buf_size]; model.rpar = [pad_val]; model.blocktype = 'd'; model.dep_ut = [%t %f];

label = string([buf_size; pad_val]); ...end

case 'set' then … while %t do

[ok,buf_size,pad_val,exprs] = scicos_getvalue('Set parameters Block',..

['Buffer size';'Padding value'], list('vec',1,'vec',1), label);

if ~ok then break, end if (buf_size<1) message ...

if ok then In = [model.in model.in2]; [model,graphics,ok] = set_io(model, Graphics, list(in,1), list(in,1),1,[]); if ok then graphics.exprs = label; model.ipar = [buf_size]; model.rpar = [pad_val]; … break; end end

end

Page 27: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

27

The FFT scope

Some more custom blocks

Signal 1

Signal 2

This block is not yet available under Scilab.(Scilab 5.3)

Page 28: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

28

External module hierarchyXcosMod

builder.sceEtc

XcosMod.startXcosMod.quit

macroshelp

en_USfr_FR

sci_gatewayCCppFfortran

srcCCppFortran

testsunit_testsnonreg_tests

Building the blocks and build the palette

Loading macros, libs, gateways and adding the palette to xcos

The interfacing functions of the blocks + others functions

The simulation functions of the blocks + other functions

Page 29: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

29

Palette loading process

// Build xcos palette// =========================================================

xpal = xcosPal("Buffer");xpal = xcosPalAddBlock(xpal, 'FFT_SCOPE', module_dir + '/macros/img/fft_scope.jpg');xpal = xcosPalAddBlock(xpal, 'PS_SCOPE', module_dir + '/macros/img/ps_scope.jpg');xpal = xcosPalAddBlock(xpal, 'VECTOR_SCOPE', module_dir + '/macros/img/vector_scope.jpg');xpal = xcosPalAddBlock(xpal, 'BUFFER_VECT', module_dir + '/macros/img/buffer_vect.jpg');xpal = xcosPalAddBlock(xpal, 'REAL_FFT', module_dir + '/macros/img/real_fft.jpg');xpal = xcosPalAddBlock(xpal, 'INVERSE_FFT', module_dir + '/macros/img/inverse_fft.jpg');xpal = xcosPalAddBlock(xpal, 'WINDOW_FUNC', module_dir + '/macros/img/window_func.jpg');

xcosPalExport(xpal, module_dir + '/Buffer.xpal');

// load palette// ======================================

xcosPalAdd(module_dir + '/Buffer.xpal');

A part of XcosMod/builder.sce

A part of XcosMod/etc/XcosMod.start

Page 30: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

30

Demo time,

any questions ?

www.scilab.org

Page 31: Xcos Hybrid dynamic systems modeler and simulator · Xcos Hybrid dynamic systems modeler and ... Hybrid dynamic systems modeler and simulator ... The simulation function side

31

Thanks for your attention,

any questions ?

www.scilab.org