public class FMUImport extends TypedAtomicActor implements Advanceable, ContinuousStepSizeController, ContinuousStatefulComponent
Read in a .fmu
file named by the
fmuFile parameter. The .fmu
file is a zipped
file that contains a file named modelDescription.xml
that describes the ports and parameters that are created.
At run time, method calls are made to C functions that are
included in shared libraries included in the .fmu
file.
To use this actor from within Vergil, use File -> Import -> Import FMU, which will prompt for a .fmu file. This actor is not available from the actor pane via drag and drop. The problem is that dragging and dropping this actor ends up trying to read fmuImport.fmu, which does not exist. If we added such a file, then dragging and dropping the actor would create an arbitrary actor with arbitrary ports.
FMI documentation may be found at http://www.modelisar.com/fmi.html.
Under the Continuous director, this actor invokes fmiDoStep() at the
beginning of the fire() method whenever time has advanced since the
last invocation of the fire() method. If at this point the FMU
discards the step, then the fire method will nonetheless produce
outputs, but they may not be valid for the current time.
This should not matter because the Continuous director will call
isStepSizeAccurate()
, which will return false, and it will
revert to the last committed and proceed with a smaller step size.
Under the Continuous director, when time is advanced, this actor
will be fired several times. The first firing will be at the
beginning of the time interval, which matches the last commit time of
the last postfire() invocation. The remaining firings will be at
times greater than the last commit time. These firings are all
speculative, in that any actor may reject the step size when they
occur. In the event that a step is rejected, the Continuous director
will call rollBackToCommittedState()
. If the FMU supports
it, then this method will use fmiSetFMUstate() to restore the state
to the state of the FMU at the time of the last postfire() (or
initialize(), if postfire() has not yet been invoked).
If the FMU does not support rolling back (indicated by the canGetAndSetFMUstate element in the XML file), then this actor assumes that the FMU is stateless and hence can be rolled back without any particular action. This may not be a good assumption, so it issues a warning.
Many tools that export FMUs fail to correctly declare when outputs do not depend on inputs. For this reason, this actor provides each output port with a "dependencies" parameter. If this parameter is left blank, then the dependencies are determined by the FMU's modelDescription.xml file. Otherwise, if this parameter contains a space-separated list of names of input ports, then the output port depends directly on those named input ports. If this parameter contains the string "none", then the output port depends directly on none of the input ports. If contains the string "all", then this output port depends on all input ports. Although the FMI standard is ambiguous, we infer dependency to mean that the value of an output at time t depends on the input at time t. It is irrelevant whether it depends on the input at earlier times.
Note that if the display name of a port is set, display name is used in as the name of the FMU scalar variable instead of the port name. This is useful in case FMU scalar variable names contain a period, because periods are not allowed in port names.
Note that if you use an instance of this class with the ContinuousDirector, you should in general use model exchange, not cosimulation. This is because the ContinuousDirector may advance time by calling fmiDoStep, and then later reject a step size. The only exception is that if the FMU implements rollback (via fmiGetFMUState and fmiSetFMUState), then it can be used with the ContinuousDirector.
Red (cxh) |
Red (cxh) |
Entity.ContainedObjectsIterator
Modifier and Type | Field and Description |
---|---|
protected com.sun.jna.Pointer |
_fmiComponent
The FMI component created by the
modelIdentifier_fmiInstantiateSlave() method.
|
protected FMIModelDescription |
_fmiModelDescription
A representation of the fmiModelDescription element of a
Functional Mock-up Unit (FMU) file.
|
protected double |
_fmiVersion
The version of FMI that the FMU declares it is compatible with,
converted to a double for easy comparison.
|
protected java.nio.DoubleBuffer |
_states
For model exchange, the FMU state variables.
|
StringParameter |
fmiVersion
The FMI version of the FMU.
|
FileParameter |
fmuFile
The Functional Mock-up Unit (FMU) file.
|
Parameter |
modelExchange
If true, then this FMU is for model exchange rather than co-simulation.
|
Parameter |
persistentInputs
If true, then previously received input values will be re-used on subsequent firings where
inputs are absent.
|
Parameter |
suppressWarnings
If true, suppress warnings about the FMU not being able to roll
back.
|
Parameter |
visible
If true, indicate to the FMU (if it supports it) that it is
allowed to create displays and otherwise interact with the user.
|
_typesValid
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
COMPLETED, NOT_READY, STOP_ITERATING
Constructor and Description |
---|
FMUImport(CompositeEntity container,
java.lang.String name)
Construct an actor with the given container and name.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
_checkEventIndicators()
Return true if we are not in the first firing and the
sign of some event indicator has changed.
|
protected void |
_debugToStdOut(java.lang.String message)
Print the debug message to stdout and flush stdout.
|
protected void |
_enterContinuousTimeMode()
Invoke the fmi2EnterContinuousTimeMode() function.
|
protected void |
_enterEventMode()
Invoke the fmi2EnterEventMode() function.
|
protected boolean |
_fmiCompletedIntegratorStep(boolean eventOccuredOrNoSetFMUStatePriorToCurrentPoint)
For model exchange, complete the integrator step.
|
protected double |
_fmiDoStep(Time newTime,
int newMicrostep)
Advance from the last firing time or last commit time to the specified time and
microstep by calling fmiDoStep(), if necessary.
|
protected void |
_fmiFreeInstance()
Free the instance of the FMU.
|
protected double[] |
_fmiGetDerivatives()
Return the derivatives of the continuous states provided by the FMU.
|
protected void |
_fmiInitialize()
Invoke _fmiInitialize() (for model exchange) or
_fmiInitializeSlave() (for co-simulation) on the FMU.
|
protected void |
_fmiSetContinuousStates(double[] values)
For model exchange, set the continuous states of the FMU to the specified array.
|
protected void |
_fmiSetTime(Time time)
Set the time of the FMU to the specified time.
|
protected static java.lang.String |
_fmiStatusDescription(int fmiStatus)
Return a string describing the specified fmiStatus.
|
protected void |
_fmiTerminate()
Terminate the FMU.
|
protected void |
_freeFMUState()
Free the memory recording FMU state, if the canGetAndSetFMUstate capability flag
for the FMU is true.
|
protected double |
_getStepSize()
Return the current step size.
|
protected void |
_recordFMUState()
Record the current FMU state.
|
protected void |
_requestRefiringIfNecessary()
If the FMU can provide a maximum step size, query for that maximum
step size and call fireAt() to ensure that the FMU is invoked
at the specified time.
|
protected void |
_restoreFMUState()
Restore the current FMU state to match that most recently
recorded, if the canGetAndSetFMUstate capability flag
for the FMU is true.
|
protected void |
_setFMUScalarVariable(FMIScalarVariable scalar,
Token token)
Set a scalar variable of the FMU to the value of a Ptolemy token.
|
protected void |
_setParameter(Parameter parameter,
FMIScalarVariable scalar)
Set a Ptolemy II Parameter to the value of a FMI
ScalarVariable.
|
protected boolean |
_skipIfKnown()
Return true if outputs are skipped if known.
|
boolean |
advance(Time time,
int microstep)
Advance to the specified time.
|
void |
attributeChanged(Attribute attribute)
If the specified attribute is fmuFile, then unzip
the file and load in the .xml file, creating and deleting parameters
as necessary.
|
void |
declareDelayDependency()
Set the dependency between all output ports and all input
ports of this actor.
|
void |
fire()
Invoke fmiDoStep() of the slave FMU, if necessary to catch
up to current time, and then set the (known) inputs of the
FMU and retrieve and send out any outputs for which all
inputs on which the output depends are known.
|
static void |
importFMU(java.lang.Object originator,
FileParameter fmuFileParameter,
NamedObj context,
double x,
double y,
boolean modelExchange)
Import a FMUFile.
|
void |
initialize()
Initialize this FMU wrapper.
|
boolean |
isStepSizeAccurate()
Return whether the most recent call to fmiDoStep()
succeeded.
|
boolean |
isStrict()
Return false if any output has been found that not depend
directly on an input.
|
boolean |
postfire()
Override the base class to record the current time as the last
commit time.
|
void |
preinitialize()
Instantiate the slave FMU component.
|
double |
refinedStepSize()
Return suggested refined step size, if the FMU has provided one.
|
void |
rollBackToCommittedState()
Roll back to committed state, if the FMU has asserted
canGetAndSetFMUstate in the XML file and has provided the methods
to set and restore state.
|
double |
suggestedStepSize()
Return the suggested next step size.
|
void |
wrapup()
Terminate and free the slave fmu.
|
_containedTypeConstraints, _customTypeConstraints, _defaultTypeConstraints, _fireAt, _fireAt, attributeTypeChanged, clone, clone, isBackwardTypeInferenceEnabled, newPort, typeConstraintList, typeConstraints
_actorFiring, _actorFiring, _declareDelayDependency, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, iterate, newReceiver, outputPortList, prefire, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, terminate
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName
_addPort, _description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getFullName, getModelErrorHandler, getName, getName, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, validateSettables, workspace
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
isFireFunctional, iterate, prefire, stop, stopFire, terminate
addInitializable, removeInitializable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
getDerivedLevel, getDerivedList, propagateValue
public StringParameter fmiVersion
public FileParameter fmuFile
public Parameter modelExchange
public Parameter persistentInputs
public Parameter suppressWarnings
public Parameter visible
protected com.sun.jna.Pointer _fmiComponent
protected FMIModelDescription _fmiModelDescription
protected double _fmiVersion
protected java.nio.DoubleBuffer _states
public FMUImport(CompositeEntity container, java.lang.String name) throws NameDuplicationException, IllegalActionException
container
- The container.name
- The name of this actor.IllegalActionException
- If the actor cannot be contained
by the proposed container.NameDuplicationException
- If the container already has an
actor with this name.public boolean advance(Time time, int microstep) throws IllegalActionException
advance
in interface Advanceable
time
- The time to advance.microstep
- The microstep to advance.IllegalActionException
- If an error occurs advancing time.public void attributeChanged(Attribute attribute) throws IllegalActionException
attributeChanged
in class NamedObj
attribute
- The attribute that has changed.IllegalActionException
- If the specified attribute
is fmuFile and the file cannot be opened or there
is a problem creating or destroying the parameters
listed in the file.public void declareDelayDependency() throws IllegalActionException
declareDelayDependency
in class AtomicActor<TypedIOPort>
IllegalActionException
- Not thrown in this base
class, derived classes should throw this exception if the
delay dependency cannot be computed.AtomicActor.getCausalityInterface()
,
AtomicActor._declareDelayDependency(IOPort, IOPort, double)
public void fire() throws IllegalActionException
fire
in interface Executable
fire
in class AtomicActor<TypedIOPort>
IllegalActionException
- If FMU indicates a failure.public void initialize() throws IllegalActionException
initialize
in interface Initializable
initialize
in class AtomicActor<TypedIOPort>
IllegalActionException
- If the slave FMU cannot be
initialized.public static void importFMU(java.lang.Object originator, FileParameter fmuFileParameter, NamedObj context, double x, double y, boolean modelExchange) throws IllegalActionException, java.io.IOException
originator
- The originator of the change request.fmuFileParameter
- The .fmuFilecontext
- The context in which the FMU actor is created.x
- The x-axis value of the actor to be created.y
- The y-axis value of the actor to be created.modelExchange
- If true, import for model exchange (vs. co-simulation).IllegalActionException
- If there is a problem instantiating the actor.java.io.IOException
- If there is a problem parsing the fmu file.public boolean isStepSizeAccurate()
isStepSizeAccurate
in interface ContinuousStepSizeController
public boolean isStrict()
isStrict
in interface Executable
isStrict
in class AtomicActor<TypedIOPort>
public boolean postfire() throws IllegalActionException
postfire
in interface Executable
postfire
in class AtomicActor<TypedIOPort>
IllegalActionException
- Not thrown in this base class.public void preinitialize() throws IllegalActionException
preinitialize
in interface Initializable
preinitialize
in class AtomicActor<TypedIOPort>
IllegalActionException
- if it cannot be instantiated.public double refinedStepSize() throws IllegalActionException
refinedStepSize
in interface ContinuousStepSizeController
IllegalActionException
- If the step size cannot be further refined.public void rollBackToCommittedState() throws IllegalActionException
rollBackToCommittedState
in interface ContinuousStatefulComponent
IllegalActionException
- If the rollback attempts to go
back further than the last committed time.public double suggestedStepSize() throws IllegalActionException
suggestedStepSize
in interface ContinuousStepSizeController
IllegalActionException
- If an actor suggests an illegal step size.public void wrapup() throws IllegalActionException
wrapup
in interface Initializable
wrapup
in class AtomicActor<TypedIOPort>
IllegalActionException
- If the slave fmu cannot be
terminated or freed.protected boolean _checkEventIndicators() throws IllegalActionException
IllegalActionException
- If the fmiGetEventIndicators
function is missing, or if calling it does not return fmiOK.protected void _enterContinuousTimeMode() throws IllegalActionException
IllegalActionException
- If there is a problem
invoking the fmi2ContinuousTimeMode() function in the fmi.protected void _enterEventMode() throws IllegalActionException
IllegalActionException
- If there is a problem
invoking the fmi2EnterEventMode() function in the fmi.protected void _debugToStdOut(java.lang.String message)
message
- The message to be displayed.protected boolean _fmiCompletedIntegratorStep(boolean eventOccuredOrNoSetFMUStatePriorToCurrentPoint) throws IllegalActionException
Under FMI previous to 2.0, if the FMU indicates that fmiEventUpdate() should be called, call it.
Note that in FMI-2.0, the ModelExchange attribute as an element
FMI20ModelExchangeCapabilities.completedIntegratorStepNotNeeded
that in the default is false, which means that fmiCompletedIntegratorStep()
should be called. However, if completedIntegratorStepNotNeeded is true,
then fmiCompletedIntegratorStep is not called.
Under FMI-2.0, if the fmi2CompletedIntegrationStep() method sets the value of the terminateSimulation parameter to true, then {&link ptolemy.actor.Director#finish()} is invoked.
eventOccuredOrNoSetFMUStatePriorToCurrentPoint
- For FMI
< 2.0, true if event update should be called. for FMI ≥ 2.0,
True if fmi2SetFMUState() will not be called for times before
the current time in this simulation.IllegalActionException
- If the FMU does not return fmiOK.protected double _fmiDoStep(Time newTime, int newMicrostep) throws IllegalActionException
newTime
- The time to advance to.newMicrostep
- The microstep to advance to.IllegalActionException
- If fmiDoStep() returns anything other than
fmiDiscard or fmiOK.protected void _fmiFreeInstance() throws IllegalActionException
IllegalActionException
- If the FMU does not return fmiOK.protected double[] _fmiGetDerivatives() throws IllegalActionException
IllegalActionException
- If the FMU does not return fmiOK.protected void _fmiInitialize() throws IllegalActionException
IllegalActionException
- If the FMU does not return fmiOK.protected void _fmiSetContinuousStates(double[] values) throws IllegalActionException
values
- The values to assign to the states.IllegalActionException
- If the length of the array does not match
the number of continuous states, or if the FMU does not return fmiOK.protected void _fmiSetTime(Time time) throws IllegalActionException
time
- The time.IllegalActionException
- If the FMU does not return fmiOK.protected static java.lang.String _fmiStatusDescription(int fmiStatus)
fmiStatus
- The status returned by an FMI procedure.protected void _fmiTerminate() throws IllegalActionException
IllegalActionException
- If the FMU does not return fmiOK.protected void _freeFMUState() throws IllegalActionException
IllegalActionException
- If freeing the memory the state fails.protected double _getStepSize() throws IllegalActionException
IllegalActionException
- If there is a problem getting
the currentStepSize.protected void _recordFMUState() throws IllegalActionException
IllegalActionException
- If recording the state fails.protected void _requestRefiringIfNecessary() throws IllegalActionException
IllegalActionException
- If the call to fireAt() throws it.protected void _restoreFMUState() throws IllegalActionException
IllegalActionException
- If there is no recorded state,
or if restoring the state fails.protected void _setParameter(Parameter parameter, FMIScalarVariable scalar) throws IllegalActionException
parameter
- The Ptolemy parameter to be set.scalar
- The FMI scalar variable that contains the value
to be setIllegalActionException
- If the scalar is of a type
that is not handled.protected void _setFMUScalarVariable(FMIScalarVariable scalar, Token token) throws IllegalActionException
scalar
- the FMI scalar to be set.token
- the Ptolemy token that contains the value to be set.IllegalActionException
- If the scalar is of a type
that is not handled or if the type of the token does not match
the type of the scalar.protected boolean _skipIfKnown() throws IllegalActionException
IllegalActionException
- If there is a problem getting
the currentStepSize.