Beruflich Dokumente
Kultur Dokumente
S-Functions, Revisited
A.W.M. (Jos) van Schijndel Eindhoven University of Technology, A.W.M.v.Schijndel@tue.nl;
Abstract: COMSOL Multiphysics® has time step the solution is exported. Furthermore,
standard facilities to export models to different boundary values can be applied at will.
SimuLink. Normally, the standard export A simple heating problem is used to show the
works well if the solvers, available in approach.
SimuLink, can handle the problem. However, Section 4 provides a former case study using
if a model in Comsol needs special solvers, Comsol, where the airflow temperature at the
for example airflow or other non-linear inlet of an office is controlled by an on/off
problems, the standard export to SimuLink controller in SimuLink.
is often not suitable, because the standard Section 5 presents the conclusions.
solvers of SimuLink cannot handle such a
problem efficiently. This can cause long 2. S-Functions in SimuLink®
simulation duration times and even leads to
no solution at all. The paper presents a possible The main idea is to use S-Functions
solution to this problem by implementing (Mathworks 1998). In order to have a good
Comsol code in the discrete section of a understanding of how S-Functions work, a short
SimuLink S-Function The advantage of this summary is provided here. An S-function
approach is that the special solvers of (system-function) is a computer language
Comsol can be used in the SimuLink description of a dynamic system available in
environment. This can lead to significant SimuLink. The form of an S-function is very
improvement of the simulation duration time. general and can accommodate continuous,
Keywords: S-Function, SimuLink, Comsol, non discrete and hybrid systems. As a matter of fact,
linear, implementation nearly all SimuLink models can be described as
S-functions. Each block within a SimuLink
1. Introduction model has the following general characteristics:
a vector of inputs, u, a vector of outputs, y, and a
Comsol has standard facilities to export vector of states, x, as shown by the illustration in
models to SimuLink. Normally, the standard Figure 1.
export works well if the solvers, available in
SimuLink, can handle the problem. However, if
a model in Comsol needs special solvers, for
example airflow or other non-linear problems,
the standard export to SimuLink is often not
suitable, because the standard solvers of
SimuLink cannot handle such a problem
efficiently. This can cause long simulation
duration times and even leads to no solution at
all.
Similar earlier work, based on FemLab
(former Comsol), was already published in 2005.
Due to the quite different MatLab interface in
Comsol compared with FemLab, it is worthwhile
to update this past work to the most recent
Figure 1. Illustration of the general characteristics of
Comsol version. an S-Function
Section 2 introduces the concept of S-
Functions in SimuLink. The state vector may consist of continuous
Section 3 presents the approach by states, discrete states, or a combination of both.
implementing Comsol code in the discrete The mathematical relationships between the
section of a SimuLink S-Function. The S- inputs, outputs, and the states are expressed by
Function solves each time step the non-linear the equations presented in the above figure.
problem using the Comsol solver. After each SimuLink partitions the state vector x into two
Cmodel.sol('sol1').runAll;
3.3.1 Input
The input parameter (u) is the heat source. This
parameter can be set manually by:
Figure 5. Comparison with a simulation of the whole
period at once (+) and manually stepping (o)
Cmodel.physics('ht').feature('hs1').
set('Q', 1, '500');
The latter shows how to split a complete
simulation into several time steps using MatLab
This changes the heat source from 1000 to 500
commands. Using the results of the previous
W/m3
Sections we are now able to combine all mfiles
for the final implementation.
3.3.2 Time range
The time range is set by
3.4 Final implementation
Cmodel.sol('sol1').feature('t1').set
('tlist', The final part is to copy and paste the mfiles of
'range(0,3600,2*24*3600)'); Appendix 2 & 3 into the S-Function of Appendix
1. The reader should notice that model of
Here the end time is changed to 48 hours. appendix 3 should be copied into the S-Function
of Appendix 4 (at the right place see comment)
3.3.3 Init values in order to get a proper S-Function. To verify the
The init values are set by: working of S-Function with the included Comsol
(i.e. Appendix 4), the simple heating model was
Cmodel.physics('ht').feature('init1' controlled with a Relay in SimuLink. Figure 6
).set('T', 1, '283.15[K]'); shows the SimuLink model. The Relay controls
the temperature between 288 K and 290 K by
The initial temperature is changed to 283.15 K providing heat 1000 (W/m3) or cool -1000
(W/m3) to the model. In Figure 7 the simulation
3.3.4 Output results are presented. The proper switching of the
controller and response of the model is observed.
The output (y) is the temperature at the centre of This is a verification of the implementation of
the square, evaluated for the last time step. This the work so far.
parameter can be set manually by:
y=mphinterp(Cmodel,'T','coord',[0.5;
0.5],'Solnum','end')
3.5 Discussion
Appendix 2
Cmodel.physics('ht').feature('init1').set('T', 1, '293.15[K]');
Tmid_all5(1)=293.15;
for i=1:24
Cmodel.sol('sol1').feature('t1').set('tlist', 'range(0,3600)');
Cmodel.sol('sol1').runAll;
u=mphinterp(Cmodel,'T','coord',[0.5;0.5],'Solnum','end')
Tmid_all5(i+1)= u;
ustr=[num2str(u) '[K]'];
Cmodel.physics('ht').feature('init1').set('T', 1, ustr);
end
sys=y;