/
Modification.onb
151 lines (151 loc) · 15.2 KB
/
Modification.onb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE OMNotebook>
<Notebook>
<GroupCell closed="false" >
<GroupCell closed="false" >
<TextCell style="Title" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Arial; font-size:38pt; font-weight:600; color:#000000;">Inheritance through Modification</p></body></html></Text>
</TextCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">General Description</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">The class concept with inheritance is the key to reuse of modeling knowledge in Modelica. However, in contrast to many other object-oriented languages Modelica also provides inheritance through modification via so-called <span style=" font-style:italic;">modifiers</span>. This makes reuse even easier, and can be expressed very concisely. In fact, the three operations: inheritance, specialization through modifications, and object instantiation can be expressed within a single Modelica declaration.<br /><br />A modifier modifies one or more declaration(s) from an inherited class by changing some aspect(s) of the inherited declarations. The most common kind of modifiers just changes the default value (start value) in a declaration equation.<br /></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class Rocket
Real altitude(start = 59404);
end Rocket;
</Input>
<Output></Output>
</InputCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Modifiers in Variable Declarations</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Modifiers change the type of a declared variable by modifying specified member declarations of the unmodified type. For example, the <span style=" font-family:Courier New;">Real</span> type of the variable A2 has its default declaration equations for its members (<span style=" font-family:Courier New;">unit</span> and <span style=" font-family:Courier New;">fixed</span>) replaced by those given by the modifiers <span style=" font-family:Courier New;">unit = "Voltage"</span> and<span style=" font-family:Courier New;"> fixed = true</span>.<br /></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class Mod
Real A1; // Scalar variable
Real A2(unit = "Voltage", fixed = true); // Scalar with modifiers
end Mod;
</Input>
<Output></Output>
</InputCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Modifiers for Array Variables</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Regarding modifiers for array variables, it is important to remember that each attribute has the same dimensionality as the array itself. Single elements of an array, e.g. the start array for <span style=" font-family:Courier New;">A6</span> can be modified by indexing the attribute in the modification equation, e.g. <span style=" font-family:Courier New;">start[2,1]</span>.<br /></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class Arr
Real A3[2,2]; // Array variable
Real A4[2,2](start = {{1, 0}, {0, 1}}); // Modifies the start value for the whole array
Real A5[2,2](unit = {{"Voltage", "Voltage"},
{"Voltage", "Voltage"}});
Real A6[2,2](start[2, 1] = 2.3); // Array with single element modification
end Arr;
</Input>
<Output></Output>
</InputCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Modification of Protected Elements</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Protected elements may not be modified or redeclared in class modifications, despite the fact the protected elements are inherited. </p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class D
parameter Real a; // a is a public element
protected
parameter Real b; // b is a protected element
end D; </Input>
<Output></Output>
</InputCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;"></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class D1 = D(a = 10); // OK, since a is public
</Input>
<Output></Output>
</InputCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;"></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class D2 = D(a = 10, b = 20); // Illegal, since b is protected!
</Input>
<Output></Output>
</InputCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Simulation of D1 and D2</p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>simulate( D1 );</Input>
<Output></Output>
</InputCell>
<TextCell style="Output" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Courier New; font-size:11pt; color:#000000;"></p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">When simulating D2 <span style=" font-weight:600;">*Borde väl ge fel i loggen??*</span></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>simulate( D2 );</Input>
<Output></Output>
</InputCell>
<TextCell style="Output" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Courier New; font-size:11pt; color:#000000;"></p></body></html></Text>
</TextCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Single Modification Rule</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Two arguments of a modification are not allowed to denote the same attribute of a declared element, quite self-evident, but easy to forget in the context of array fields. </p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class C1
parameter Real x[3];
end C1;</Input>
<Output></Output>
</InputCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;"></p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>class C2 = C1(x = {1, 1, 1}, x[2] = 2); // Error: x[2] designated twice
</Input>
<Output></Output>
</InputCell>
</GroupCell>
<GroupCell closed="false" >
<TextCell style="Section" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">Simulation of C2</p></body></html></Text>
</TextCell>
<TextCell style="Text" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">We see in the log for the simulation on <span style=" font-family:Courier New;">C2</span> that it is illegal to modify <span style=" font-family:Courier New;">x[2]</span> twice.</p></body></html></Text>
</TextCell>
<InputCell style="Input" closed="true" >
<Input>simulate( C2 );</Input>
<Output></Output>
</InputCell>
<TextCell style="Output" >
<Text><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Courier New; font-size:11pt; color:#000000;"></p></body></html></Text>
</TextCell>
</GroupCell>
</GroupCell>
</GroupCell>
</Notebook>