-
Notifications
You must be signed in to change notification settings - Fork 49
/
Connection.cpp
303 lines (247 loc) · 9.79 KB
/
Connection.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include "Connection.h"
#include "Logging.h"
#include "ssd/ConnectionGeometry.h"
#include "ssd/Tags.h"
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
oms::Connection::Connection(const oms::ComRef& conA, const oms::ComRef& conB, bool suppressUnitConversion, oms_connection_type_enu_t type)
{
std::string str;
this->type = type;
str = std::string(conA);
this->conA = new char[str.size()+1];
strcpy(this->conA, str.c_str());
str = std::string(conB);
this->conB = new char[str.size()+1];
strcpy(this->conB, str.c_str());
this->geometry = reinterpret_cast<ssd_connection_geometry_t*>(new oms::ssd::ConnectionGeometry());
tlmparameters = NULL;
this->suppressUnitConversion = suppressUnitConversion;
}
oms::Connection::~Connection()
{
if (this->conA) delete[] this->conA;
if (this->conB) delete[] this->conB;
if (this->tlmparameters) delete tlmparameters;
if (this->geometry) delete reinterpret_cast<oms::ssd::ConnectionGeometry*>(this->geometry);
}
oms::Connection::Connection(const oms::Connection& rhs)
{
this->type = rhs.type;
this->conA = new char[strlen(rhs.conA)+1];
strcpy(this->conA, rhs.conA);
this->conB = new char[strlen(rhs.conB)+1];
strcpy(this->conB, rhs.conB);
oms::ssd::ConnectionGeometry* geometry_ = new oms::ssd::ConnectionGeometry();
*geometry_ = *reinterpret_cast<oms::ssd::ConnectionGeometry*>(rhs.geometry);
this->geometry = reinterpret_cast<ssd_connection_geometry_t*>(geometry_);
tlmparameters = NULL;
this->suppressUnitConversion = rhs.suppressUnitConversion;
}
oms::Connection& oms::Connection::operator=(const oms::Connection& rhs)
{
// check for self-assignment
if(&rhs == this)
return *this;
if (this->type != rhs.type)
logWarning("[oms::Connection::operator=] changing type of connection");
this->type = rhs.type;
if (this->conA) delete[] this->conA;
this->conA = new char[strlen(rhs.conA)+1];
strcpy(this->conA, rhs.conA);
if (this->conB) delete[] this->conB;
this->conB = new char[strlen(rhs.conB)+1];
strcpy(this->conB, rhs.conB);
oms::ssd::ConnectionGeometry* geometry_ = new oms::ssd::ConnectionGeometry();
*geometry_ = *reinterpret_cast<oms::ssd::ConnectionGeometry*>(rhs.geometry);
this->geometry = reinterpret_cast<ssd_connection_geometry_t*>(geometry_);
setTLMParameters(rhs.tlmparameters);
this->suppressUnitConversion = rhs.suppressUnitConversion;
return *this;
}
oms_status_enu_t oms::Connection::exportToSSD(pugi::xml_node &root) const
{
pugi::xml_node node;
if(type == oms_connection_single)
node = root.append_child(oms::ssp::Draft20180219::ssd::connection);
else if(type == oms_connection_bus || type == oms_connection_tlm)
node = root.append_child(oms::ssp::Draft20180219::bus_connection);
ComRef startConnectorRef(conA);
ComRef startElementRef = startConnectorRef.pop_front();
ComRef endConnectorRef(conB);
ComRef endElementRef = endConnectorRef.pop_front();
node.append_attribute("startElement") = startConnectorRef.isEmpty() ? "" : startElementRef.c_str();
node.append_attribute("startConnector") = startConnectorRef.isEmpty() ? startElementRef.c_str() : startConnectorRef.c_str();
node.append_attribute("endElement") = endConnectorRef.isEmpty() ? "" : endElementRef.c_str();
node.append_attribute("endConnector") = endConnectorRef.isEmpty() ? endElementRef.c_str() : endConnectorRef.c_str();
if (suppressUnitConversion)
node.append_attribute("suppressUnitConversion") = suppressUnitConversion;
if(type == oms_connection_tlm)
{
node.append_attribute("delay") = std::to_string(tlmparameters->delay).c_str();
node.append_attribute("alpha") = std::to_string(tlmparameters->alpha).c_str();
node.append_attribute("linearimpedance") = std::to_string(tlmparameters->linearimpedance).c_str();
node.append_attribute("angularimpedance") = std::to_string(tlmparameters->angularimpedance).c_str();
}
getGeometry()->exportToSSD(node);
return oms_status_ok;
}
void oms::Connection::setGeometry(const oms::ssd::ConnectionGeometry* newGeometry, bool inverse)
{
oms::ssd::ConnectionGeometry* geometry_ = reinterpret_cast<oms::ssd::ConnectionGeometry*>(this->geometry);
if (geometry_)
delete geometry_;
geometry_ = new oms::ssd::ConnectionGeometry(*newGeometry, inverse);
this->geometry = reinterpret_cast<ssd_connection_geometry_t*>(geometry_);
}
void oms::Connection::setTLMParameters(const oms_tlm_connection_parameters_t* parameters)
{
if (tlmparameters)
{
delete tlmparameters;
tlmparameters = NULL;
}
if (parameters)
setTLMParameters(parameters->delay, parameters->alpha, parameters->linearimpedance, parameters->angularimpedance);
}
void oms::Connection::setTLMParameters(double delay, double alpha, double linearimpedance, double angualrimpedance)
{
if (!tlmparameters)
tlmparameters = new oms_tlm_connection_parameters_t;
tlmparameters->delay = delay;
tlmparameters->alpha = alpha;
tlmparameters->linearimpedance = linearimpedance;
tlmparameters->angularimpedance = angualrimpedance;
}
bool oms::Connection::isStrictEqual(const oms::ComRef& signalA, const oms::ComRef& signalB) const
{
return signalA == oms::ComRef(this->conA) && signalB == oms::ComRef(this->conB);
}
bool oms::Connection::isEqual(const oms::ComRef& signalA, const oms::ComRef& signalB) const
{
return (signalA == oms::ComRef(this->conA) && signalB == oms::ComRef(this->conB)) || (signalA == oms::ComRef(this->conB) && signalB == oms::ComRef(this->conA));
}
bool oms::Connection::isEqual(const oms::Connection& connection) const
{
const oms::ComRef& conA_ = connection.getSignalA();
const oms::ComRef& conB_ = connection.getSignalB();
return isEqual(conA_, conB_);
}
bool oms::Connection::containsSignal(const oms::ComRef& signal) const
{
return signal.isRootOf(oms::ComRef(this->conA)) || signal.isRootOf(oms::ComRef(this->conB));
}
bool oms::Connection::containsSignalB(const oms::ComRef& signal) const
{
return signal.isRootOf(oms::ComRef(this->conB));
}
bool oms::Connection::isValid(const ComRef& crefA, const ComRef& crefB, const Connector& conA, const Connector& conB)
{
bool connectorA, connectorB;
// Check connector A
if (crefA.isValidIdent()) // this is a system
{
// Connector A of a systems must be input or parameter
connectorA = conA.isInput() || conA.isParameter();
}
else // this is an element
{
// Connector A of an element must be output, calculated parameter, or inout
// TODO: check for inout, neither FMI-1.0 nor FMI-2.0 do support inout
connectorA = conA.isOutput() || conA.isCalculatedParameter();
}
// Check connector B
if (crefB.isValidIdent()) // this is a system
{
// Connector B of a systems must be output or calculated parameter
connectorB = conB.isOutput() || conB.isCalculatedParameter();
}
else // this is an element
{
// Connector A of an element must be input, parameter, or inout
// TODO: check for inout, neither FMI-1.0 nor FMI-2.0 do support inout
connectorB = conB.isParameter() || conB.isInput();
}
// both connectors must be valid in order to make the connection valid
return connectorA && connectorB;
}
bool oms::Connection::isValidUnits(const ComRef& crefA, const ComRef& crefB, const Connector& conA, const Connector& conB)
{
if (conA.connectorUnits.empty() || conB.connectorUnits.empty())
return true;
std::map<std::string, std::string> connectorAUnits, connectorBUnits;
getSIUnits(conA, connectorAUnits);
getSIUnits(conB, connectorBUnits);
if (connectorAUnits != connectorBUnits)
return false;
return true;
}
void oms::Connection::getSIUnits(const Connector& connector, std::map<std::string, std::string>& baseUnits)
{
std::vector<std::string> SIunits = {"kg", "m", "s", "A", "K", "mol", "cd", "rad"};
for (const auto & unit : SIunits)
{
for (const auto & it: connector.connectorUnits)
{
auto baseUnit = it.second.find(unit);
if (baseUnit != it.second.end())
baseUnits[baseUnit->first] = baseUnit->second;
else
baseUnits[unit] = "0";
}
}
}
oms_status_enu_t oms::Connection::rename(const ComRef& cref, const ComRef& newCref)
{
oms::ComRef tailA(getSignalA());
const oms::ComRef frontA = tailA.pop_front();
if (frontA == cref)
{
std::string str(newCref + tailA);
delete [] this->conA;
this->conA = new char[str.size()+1];
strcpy(this->conA, str.c_str());
}
oms::ComRef tailB(getSignalB());
const oms::ComRef frontB = tailB.pop_front();
if (frontB == cref)
{
std::string str(newCref + tailB);
delete [] this->conB;
this->conB = new char[str.size()+1];
strcpy(this->conB, str.c_str());
}
return oms_status_ok;
}