Adding an Export for Simulink Node with the COMSOL® API Syntax
To add an Export for Simulink® feature node to the COMSOL Multiphysics model using the COMSOL API, enter the command:
model.externalInterface.create(<simtag>, "SimulinkCosimulation")
where <simtag> is a string that set the tag of the feature node.
To set the cosimulation filename enter:
model.externalInterface(<simtag>).set("filename", <filename>)
where <filename> is a string that defines the name of the file.
To export the cosimulation file type:
model.externalInterface(<simtag>).run()
Setting Up the Inputs
For each input you want in the cosimulation file you need to specify a name and an initial value. Note that input can only be chosen from the global parameters list.
To create an input enter:
model.externalInterface(<simtag>).setIndex("inputname", <name>, idx)
where <name> is a string that defines the parameter name used as input, and idx an integer that defines the input number.
To set the initial value of an input enter:
model.externalInterface(<simtag>).setIndex("inputvalue", value, idx)
where value is the desired initial value of input for the cosimulation.
To remove an input enter:
model.externalInterface(<simtag>).remove("inputname", idx)
model.externalInterface(<simtag>).remove("inputunit", idx)
model.externalInterface(<simtag>).remove("inputvalue", idx)
where idx is the index of the input to remove.
The order of the input corresponds of the order of the entry port in the COMSOL Cosimulation block in Simulink. To change the order of the input enter:
model.externalInterface(<simtag>).move("inputname", new int [] {idx}, relativeChange)
model.externalInterface(<simtag>).move("inputunit", new int [] {idx}, relativeChange)
model.externalInterface(<simtag>).move("inputvalue", new int [] {idx}, relativeChange)
where idx is the index of the input to move and relativeChange an integer corresponding to the relative position change (negative for upward change, positive for downward change).
Setting Up the Block Parameters
For each block parameter you want in the cosimulation file you need to specify a name and an initial value. Note that block parameter can only be chosen from the global parameters list.
To create a block parameter enter:
model.externalInterface(<simtag>).setIndex("blockname", <name>, idx)
where <name> is a string that defines the parameter name used as block parameter, and idx an integer.
To set the value of a block parameter enter:
model.externalInterface(<simtag>).setIndex("blockvalue", value, idx)
To remove a block parameter enter:
model.externalInterface(<simtag>).remove("blockname", idx)
model.externalInterface(<simtag>).remove("blockunit", idx)
model.externalInterface(<simtag>).remove("blockvalue", idx)
where idx is the index of the block parameter to remove.
Setting Up the Outputs
To export a file for cosimulation, you need to specify at least one output. The output can be define with any expression with a global scope.
To create an output enter:
model.externalInterface(<simtag>).setIndex("outputexpr", <expr>, idx)
where <expr> is a string that defines the output expression, and idx the index position.
To set an output name enter:
model.externalInterface(<simtag>).setIndex("outputdescr", <descr>, idx)
where <descr> is a string that set the output name at the position idx.
To set an output unit enter:
model.externalInterface(<simtag>).setIndex("outputunit", <unit>, idx)
where <unit> is a string that set the output name at the position idx.
To remove an output enter:
model.externalInterface(<simtag>).remove("outputexpr", new int [] {idx})
model.externalInterface(<simtag>).remove("outputdescr", idx)
where idx is the index of the output to remove.
The order of the output corresponds of the order of the output port in the COMSOL Cosimulation block in Simulink. To change their order enter:
model.externalInterface(<simtag>).move("outputexpr", new int [] {idx}, relativeChange)
model.externalInterface(<simtag>).move("outputdescr", new int [] {idx}, relativeChange)
where idx is the index of the output to move and relativeChange an integer corresponding to the relative position change (negative for upward change, positive for downward change).
Setting Up the Study
To set the study to run in cosimulation enter:
model.externalInterface(<simtag>).set("study", <stdtag>)
where <stdtag> is a string that defines the tag of the study.
Setting Up the Dependencies
When the selected study is of type Time Dependent, you may want to specify the dependencies between outputs and inputs.
To set dependencies enter:
model.externalInterface(<simtag>).set("dependencies", idxIn, idxOut)
where idxIn is the input index and idxOut is the output index.
Setting Up the Block Image
For better visualization of the COMSOL Cosimulation block you can include an image.
To set an image enter:
model.externalInterface(<simtag>).set("image", <filename>)
where <filename> is a string.
To embed the image inside the COMSOL model, enter:
model.externalInterface(<simtag>).importImage()
To discard the image, enter:
model.externalInterface(<simtag>).discard()