This file covers: 
- how to setup the environment in MagicDraw for the simulation extension
- how to model physical interaction/signal flow (i.e. use the extention)
- how to generate simulation files from the model (focus on Modelica)

Instructions

Prerequisites
The SysML models are created with the SysML plugin of MagicDraw 18 (MD 17.0.5 may work as well).
A Modelica tool is needed to simulate the file.

(MD 17 required lines to be added to sysml.alias to map internal SysML elements IDs to "official" IDs.)
(MD 17 required "-Dexport.references.to.stereotype.application\=true" added to JAVA_ARGS in magicdraw.properties.)

Install
The directory should have this file, the translator (jar file executed with Java 1.7), and the simulation profile (sysphs.zip).

MagicDraw configuration
The following lines must be added to the file [MagicDraw Install directory]/data/sysml.alias.properties:
href_to_id.regexp.http\://www\\.omg\\.org/spec/SysML/20120401/SysML\\.xmi#_SysML_Blocks_PackageableElement-Block_PackageableElement=_11_5EAPbeta_be00301_1147424179914_458922_958
href_to_id.regexp.http\://www\\.omg\\.org/spec/SysML/20120401/SysML\\.xmi#_SysML_Ports_u0026Flows_PackageableElement-FlowProperty_PackageableElement=_11_5EAPbeta_be00301_1147436061009_177713_1911

href_to_id.http\://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_DerivedUnit_PackageableElement=_16_5_1_12c903cb_1245417652468_149854_5164
href_to_id.http\://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_DerivedUnit_PackageableElement-factor_Property=_16_5_1_12c903cb_1245417703671_904951_5219
href_to_id.http\://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_Unit_PackageableElement-symbol_Property=_16_5_1_12c903cb_1245417296734_435636_5009

id_to_href._16_5_1_12c903cb_1245417652468_149854_5164=http://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_DerivedUnit_PackageableElement
id_to_href._16_5_1_12c903cb_1245417703671_904951_5219=http://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_DerivedUnit_PackageableElement-factor_Property
id_to_href._16_5_1_12c903cb_1245417296734_435636_5009=http://www.omg.org/spec/SysML/20120322/QUDV.xmi#_QUDV_Unit_PackageableElement-symbol_Property

These lines map the Block and FlowProperty stereotypes to their equivalent in MagicDraw

Profile import
To use the SysML extension, the profile need to be imported (using the "Use module" menu entry, select SysPhSProfile.xmi). Remember that all the profiles used in the model need to be explicitly applied to the root model/package.

Value types and units
Units are represented by instance specifications classified by SysML Unit, with a given symbol used by the simulation tools. Units are indicated in value types. For example, the Resistance value type specializes Real, and refers to the Ohm unit in QUDV.

Modeling physical and signal flows
We start modeling block interactions with a SysML block that contains a flow property. 
In the case of signal-flow, the type of that in or out flow property is a primitive datatype, and the flow property is also stereotyped by PhSVariable.
In the case of physical interaction, the type of that inout flow property is a specialization of ConservedSubstance, which typically has a pair of PhSVariables: one with isConserved=true corresponding to a flow rate, and one with isConserved=false with a potential.

Modeling simulation parameters
Simulation parameters are properties that can only be changed during the initialization phase of the simulation, and then stay constant during the integration phase. This is represented by the PhSConstant stereotype.

Initial assignments and equations
It is important to differentiate between assignments and equations when dealing with variables. Variables that are not PhSConstant need to appear in equations, or simulators will complain that the value of the variable over time is not defined. This is especially the case for variable for which an initial value has been given.

XMI export
The SysML model needs to be saved in XMI, with the ".xmi" file extension. MagicDraw can export models in XMI, but it gives then a ".xml" extension. The file can however be renamed with a ".xmi" extension, and used normally by MagicDraw afterwards. 

Generation of simulation file
The simulation model generator can be launched by opening the Jar file, and selecting the target XMI file. The file will be loaded, and the list of all the SysML blocks should appear. It is necessary to select which block needs to be translated.
The next step is to select the target (Modelica or Simulink) and the options (depending on the target). 

Modeling notes
- Element names should not contain any space, and they should not correspond to a Modelica/Simscape keyword

Step-by-step example

STEP 1: CREATE PROJECT
- create a new SysML project

STEP 2: CREATE INFRASTRUCTURE
- import SysPhSProfile.xmi using the "File/Use Module" or "File/Use Project" menu item
- apply profile by right clicking on the root model and selecting the SysPhS profile. You may need to unselect "apply filter"
- import SysPhSLibrary.xmi using the "File/Use Module" or "File/Use Project" menu item

STEP 3: CREATE DATATYPES
- create a value type "Voltage", specialization of "Real", with a unit "volt"
- create a value type "Current", specialization of "Real", with a unit "ampere"
- create a value type "Time", specialization of "Real", with a unit "second"
- create a value  "Resistance", specialization of "Real", with a unit "ohm"

STEP 4: CREATE STRUCTURAL MODEL
- create a block definition diagram "CircuitComponents"
  - create a block "ElectricityFlow" specialization of ConservedSubstance from the SysPhS library.
    - create a conserved phs variable "i" typed by "Current"
    - create a non-conserved phs variable "v" typed by "Voltage"
  - create a block "Pin"
    - create a flow property "electricity", typed by "ElectricityFlow"
  - create an abstract block "TwoPinElectricalComponent"  
    - create a port "p" typed by Pin
    - create a port "n"  typed by Pin
    - create a property "i" typed by "Current", stereotyped by PhSVariable
    - create a property "v" typed by "Voltage", stereotyped by PhSVariable
  - create a block "Source" specialization of "TwoPinElectricalComponent"
    - create a property "amp" typed by Voltage, stereotyped by PhSConstant
    - create a property "t" typed by Time, stereotyped by PhSConstant
  - create a block "Resistor" specialization of "TwoPinElectricalComponent"
    - create a property "resistance" typed by Resistance, stereotyped by PhSConstant
  - create a block "Ground"
    - create a port "p" typed by Pin
    - create a property "i" typed by "Current"
    - create a property "v" typed by "Voltage"

STEP 5: CREATE CONSTRAINTS
- create a block definition diagram "CircuitConstraints"
  - create a constraint block "TwoPinElectricalConstraint"
    - create a parameter "i" typed by "Real"
    - create a parameter "iPos" typed by "Real"
    - create a parameter "iNeg" typed by "Real"
    - create a parameter "v" typed by "Real"
    - create a parameter "vPos" typed by "Real"
    - create a parameter "vNeg" typed by "Real"
    - create a constraint "i=iPos"
    - create a constraint "iPos+iNeg=0"
    - create a constraint "v=vPos-vNeg"
  - create a constraint block "SourceConstraint" specialization of "TwoPinElectricalConstraint"
    - create a parameter "t" typed by Real
    - create a parameter "amp" typed by Real
    - create a constraint "v=amp*sin(2*3.14*t)"
    - create a constraint "t=time"
  - create a constraint block "ResistorConstraint" specialization of "TwoPinElectricalConstraint"
    - create a parameter "r" typed by Real
    - create a constraint "v=r*i"
  - create a constraint block "GroundConstraint"
    - create a parameter "i" typed by "Real"
    - create a parameter "iPos" typed by "Real"
    - create a parameter "v" typed by "Real"
    - create a parameter "vPos" typed by "Real"
    - create a constraint "i=iPos"
    - create a constraint "v=vPos"
    - create a constraint "vPos=0"

STEP 6: BIND CONSTRAINTS TO STRUCTURE
- add a property "sourceConstraint" to "Source", typed by "SourceConstraint"
- create a parametric diagram for "Source" and bind the variables
- add a property "groundConstraint" to "Ground", typed by "GroundConstraint"
- create a parametric diagram for "Ground" and bind the variables
- add a property "resistorConstraint" to "Resistor", typed by "resistorConstraint"
- create a parametric diagram for "Resistor" and bind the variables

STEP 7: CREATE PARAMETRICS MODEL
- create a block Circuit with an IBD
  - drop a source, a ground, and a resistor
  - connect the components

STEP 8: CREATE AN INSTANCE
- create an instance specification typed by "Resistor", with a resistance of 20
- add this instance specification as default value of the circuit resistor
- create an instance specification typed by "Source", with an amp of 220
- add this instance specification as default value of the circuit source

STEP 9: SOLVE INSTANCE/SIMULATION
- save project
- export project to "UML 2.5 XMI File" 
- rename the file from "*.xml" to "*.xmi"
- run the translator by opening the JAR file
- select the XMI file
- select the Circuit block
- select Modelica target
- generate the file
- open generated file with Modelica tool
- simulate for 10 seconds
