Model description
In-memory model of modelDescription.xml, based on ElementTree.
It replaces the hand-written expat/print rewriting of modelDescription.xml. It loads the descriptor into an ElementTree, gives access to its variables and model structure, and writes it back. Operations modify the tree in place, so everything they do not touch (annotations, aliases, comments, namespaces, type definitions, ...) is preserved, and escaping is done by the serializer.
Conformity with FMI-2.0.5 and FMI-3.0.2 (fmi-standard.org):
- The file is written in UTF-8 and starts with the XML declaration, as required by both standards (FMI-2 §2.2, FMI-3 §2.4: "The first line of an XML file [...] must contain the encoding scheme [...] always UTF-8").
- Element order is preserved: it is significant for both standards (e.g. the order of
<ScalarVariable>defines the FMI-2index, the order of<ContinuousStateDerivative>defines the FMI-3 state vector). - FMI-2 type definitions (
<TypeDefinitions><SimpleType><Real/>) share their tag names with variable types; only children of<ModelVariables>are considered as variables.
This module has no dependency on the rest of the package.
ModelDescription ¶
modelDescription.xml loaded as an ElementTree.
Attributes:
| Name | Type | Description |
|---|---|---|
root | Element | The |
fmi_version | int | Major FMI version, |
namespaces | list[tuple[str, str]] |
|
prolog | list[Element] | Comments and processing instructions before the root element. |
epilog | list[Element] | Comments and processing instructions after the root element. |
Source code in fmu_manipulation_toolbox/model_description.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
interfaces property ¶
Interface type elements present in the document, e.g. {"CoSimulation": <element>}.
attributes(element) ¶
Attributes of element, namespaced ones named prefix:name as in the file.
iter_ports() ¶
load(source) classmethod ¶
Parse a descriptor from a file path or from its raw bytes.
Raises:
| Type | Description |
|---|---|
ModelDescriptionError | If the document is not well-formed or is not an FMI-2/FMI-3 model description. |
Source code in fmu_manipulation_toolbox/model_description.py
model_exchange_sizes(label='') ¶
Number of continuous states (nx) and of event indicators (nz).
- FMI-2:
nxis the number of<Unknown>entries of<Derivatives>(FMI-2 variables are scalar),nzthenumberOfEventIndicatorsattribute of<fmiModelDescription>. - FMI-3:
nxandnzcount the elements of the variables listed by<ContinuousStateDerivative>and<EventIndicator>: an array counts for its number of elements, its dimensions being given by<Dimension start>or by the start value of a structural parameter.
An FMI-3 entry whose size cannot be resolved counts for one element, with a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label | str | Name of the FMU, used in the warnings. | '' |
Returns:
| Type | Description |
|---|---|
tuple[int, int] | tuple[int, int]: |
Source code in fmu_manipulation_toolbox/model_description.py
model_structure_entries() ¶
Entries of <ModelStructure> as (section, element), in document order.
FMI-2: section is the enclosing list ("Outputs", "Derivatives", "InitialUnknowns") and element an <Unknown> (1-based index). FMI-3: section is the tag of the entry itself ("Output", "ContinuousStateDerivative", ...) which refers to a valueReference. This is the convention of OperationAbstract.model_structure_attrs.
Source code in fmu_manipulation_toolbox/model_description.py
parent_of(element) ¶
Parent of element, or None for the root. Raises KeyError if not in the document.
Source code in fmu_manipulation_toolbox/model_description.py
save(destination) ¶
Write the descriptor in UTF-8, with the XML declaration on the first line.
Source code in fmu_manipulation_toolbox/model_description.py
variables() ¶
Variable elements, in document order (comments excluded).
For FMI-2, the position in this list plus one is the index used by <ModelStructure> and by the derivative attribute.
Source code in fmu_manipulation_toolbox/model_description.py
ModelDescriptionError ¶
Bases: Exception
Raised when a modelDescription.xml cannot be loaded or is not an FMI-2/FMI-3 descriptor.
ModelVariable ¶
View on one variable of <ModelVariables>.
This is the object given to OperationAbstract.port_attrs for each variable. It gives dict-like access to the attributes of every level ([], get(), in): for FMI-2, the <ScalarVariable> attributes and those of its type element (e.g. <Real>); for FMI-3, the attributes of the variable element and the values of its <Start> elements (String and Binary variables), under the key start. It reads and writes the tree directly: attrs_list holds the attrib dictionaries of the underlying elements, so a change made through this view is a change of the document.
Attributes:
| Name | Type | Description |
|---|---|---|
element | Element |
|
fmi_version | int |
|
Source code in fmu_manipulation_toolbox/model_description.py
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 304 305 306 307 308 309 310 311 | |
attrs_list property ¶
Attribute dictionaries, outermost element first.
FMI-2: [<ScalarVariable> attributes, <Real>/<Integer>/... attributes]. FMI-3: [<Float64>/... attributes], followed by one {"start": value} mapping per <Start> element (String and Binary variables).
causality property ¶
Causality, local when not declared (FMI-2 §2.2.7, FMI-3 §2.4.7.4).
dimensions property ¶
FMI-3 <Dimension> elements as ("start", size) or ("valueReference", vr).
A single dimension of size 1 is reported as a scalar ([]).
typed_element property ¶
Element carrying the type: the child <Real>, <Integer>... (FMI-2) or the variable itself (FMI-3).
variability property ¶
Variability, with the default of the standard when not declared (FMI-2 §2.2.7, FMI-3 §2.4.7.4).
FMI-2: continuous. FMI-3: fixed for parameters, otherwise continuous for Float32/Float64 and discrete for the other types.
__setitem__(key, value) ¶
Change an existing attribute. Raises KeyError if no level holds key: attributes are not created.
Source code in fmu_manipulation_toolbox/model_description.py
has_type(names) ¶
Whether the type of the variable is one of names (see matches_type).
PrefixedAttributes ¶
Bases: MutableMapping
Attributes of an element, with namespaced names written prefix:name.
ElementTree names a namespaced attribute {uri}name. Callbacks written for the former expat implementation expect the name found in the file, e.g. xsi:noNamespaceSchemaLocation; this view translates names both ways and reads from / writes to the element's attrib.
Source code in fmu_manipulation_toolbox/model_description.py
default_variability(fmi_version, causality, fmi_type) ¶
Variability of a variable that does not declare one (FMI-2 §2.2.7, FMI-3 §2.4.7.4).
FMI-2: continuous. FMI-3: fixed for parameters, otherwise continuous for Float32/Float64 and discrete for the other types.
Source code in fmu_manipulation_toolbox/model_description.py
matches_type(fmi_version, fmi_type, names) ¶
Whether fmi_type is one of names.
A name is the exact type (Real, Float64, Int32...) or, for an FMI-3 variable, an FMI-2 type standing for its family (see ModelDescription.FMI2_TYPE_FAMILIES): Real selects Float32 and Float64, Integer every integer type.