Tutorial Scilab Xcos Modelica Part3 0
Tutorial Scilab Xcos Modelica Part3 0
Tutorial Scilab Xcos Modelica Part3 0
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
www.openeering.com
Xcos Programming
Xcos + Modelica
www.openeering.com
page 2/19
The initiation function contains a spontaneous initiation and a memory effect modeled with a negative exponential as a function of the memory of year of drug abuse relative to the number of current light users.
The LHY initial conditions are The problem is completed with the specification of the initial conditions at the time .
www.openeering.com
page 3/19
Model data
(Model data) : the annual rate at which light users quit : the annual rate at which light users escalate to heavy use : the annual rate at which heavy users quit : the forgetting rate
(Initiation function) : the number of innovators per year : the annual rate at which light users attract non-users : the constant which measures the deterrent effect of heavy use : the maximum rate of generation for initiation
Initiation function
(Initial conditions) : the initial simulation time; : Light users at the initial time; : Heavy users at the initial time; : Decaying heavy users at the initial time.
Initial conditions
www.openeering.com
page 4/19
Modelica language is useful for exchanging mathematical models since it provides a way to describe model components and their structure in libraries. The description of the these models is done using a declarative language based on equations, in contrast to usual programming that is based on the use of assignment statements. This permits a great flexibility and enhance reading scheme since equations have no pre-defined causality, equations do not have a pre-defined input/output relation, models are acausal models.
Modelica blocks are identical to standard Xcos blocks except for the fact that are of implicit type. Blocks with implicit dynamic ports mean that the connections between two or more ports do not impose any transfer of information in a known direction. These blocks are denoted by a black square and the user can connect implicit blocks of the same domain.
Modelica blocks in Xcos require a C compiler since Modelica translates the system directly into a C file that is then linked to the Xcos environment. To check if a compiler is available in your Scilab environment use command haveacompiler. For information about supported and compatible compilers see Scilab documentation.
In the figure we show the available blocks for the Electrical palette.
www.openeering.com
page 5/19
Through: A variable whose value is determined by the measure with an instrument in series. (e.g. current for the electrical domain)
Typically, the product of an across variable with a through variable is the power exchanged by the element.
Step 7: Roadmap
In this tutorial we describe how to construct the LHY model and how to simulate it in Xcos + Modelica. We implement the system in the following way: First we provide a comparative example (RLC circuit) between Xcos and Modelica; A general description of the Modelica Blocks available in Xcos is given; Then we implement the LHY model in Xcos + Modelica; In the end, we test the program and visualize the results. Descriptions RLC example Xcos MBLOCK LHY example Test and visualize Steps 8-11 12-16 17-20 21
www.openeering.com
page 6/19
The adopted scheme, reported on the right, is composed of the following components: Voltage source (Amplitude A = 2.0 [V], Frequency f = 1 [Hz]); with equation: ;
Resistor (Resistance ;
0.2
where we are interested in measuring the voltage across the capacitor and the current through the voltage source.
www.openeering.com
page 7/19
Analytical solution:
www.openeering.com
page 8/19
The next step is to add sum blocks that repres ent the local conversion of Kirchhoffs laws:
as follows:
www.openeering.com
page 9/19
First of all, it is necessary to note that at each node there are the local conservation of the through (or flux) variable current and of the across variable potential.
The following elements are used: Ground that fixes the reference potential; AC voltage source that is the active element of the network; Resistor, Inductor and Capacitor that are the passive elements of the networks; Voltage and Current sensors that are used as interface elements (data exchange) from Modelica to Xcos.
www.openeering.com
page 10/19
Input variables types: In this field, the type of input ports is specified. Two options are available: E: for explicit ports; I: for implicit ports. Output variables: Similar to input variables settings; Output variables types: Similar to input variables types;
www.openeering.com
page 11/19
www.openeering.com
page 12/19
The idea is to re-write the model Resistor such that it provides a further output that contains its instantaneous power. In this case we use the following external model file (PowerResistor.mo):
model PowerResistor // Define input/output model port // Pin p, n; extends TwoPin; // Model paramter parameter Real R = 1 "Resistance"; // Output power port Real PVal; equation PVal = v*i; R*i = v; end PowerResistor;
www.openeering.com
page 13/19
Input variables: "p" Input variables types: "I" Output variables: ["n";"PVal"] Output variables types: ["I";"E"] Parameters in Modelica: ["R"] Parameters properties: [0] Function name: PowerResistor.mo
www.openeering.com
page 14/19
www.openeering.com
page 15/19
class lhymodel ////automatically generated //// //parameters parameter Real tau = 5.000000e+004; parameter Real s = 6.100000e-001; parameter Real smax = 1.000000e-001; parameter Real q = 3.443000e+000; parameter Real a = 1.630000e-001; parameter Real b = 2.400000e-002; parameter Real g = 6.200000e-002; parameter Real delta = 2.910000e-001; Real L (start=1.400000e+006); Real H (start=1.300000e+005); Real Y (start=1.100000e+005); //output variables Real Ival,Lval,Hval,Yval; ////do not modify above this line //// equation // Output Lval = L; Hval = H; Yval = Y; // Initiation equation Ival = tau + max(smax,s*exp(-q*Y/L))*L; // LHY model der(L) = Ival - (a+b)*L; der(H) = b*L - g*H; der(Y) = H - delta*Y; end lhymodel;
www.openeering.com
page 16/19
www.openeering.com
page 17/19
www.openeering.com
page 18/19
www.openeering.com
page 19/19