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

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

  • Upload
    voxuyen

  • View
    241

  • Download
    2

Embed Size (px)

Citation preview

1

Clément DAVID &Yann COLLETTE

The Scilab Consortium

XcosHybrid dynamic systems modeler and

simulator

16th june 2010

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

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

4

Xcos: Overview

5

Xcos: Overview

6

Xcos: Overview

7

Xcos: Overview

8

Xcos: Overview

9

Xcos: Overview

10

Xcos: Overview

11

Xcos: Overview

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

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

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

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

16

Demo time,

any questions ?

www.scilab.org

17

ScilabTec 2010

Optimization of a PID regulator

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);

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

20

Optimization of a PID regulator

Before optimization After optimization

21

Demo time,

any questions ?

www.scilab.org

22

ScilabTec 2010

Programming new blocks

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

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;

} }

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

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

27

The FFT scope

Some more custom blocks

Signal 1

Signal 2

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

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

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

30

Demo time,

any questions ?

www.scilab.org

31

Thanks for your attention,

any questions ?

www.scilab.org