Ansys Learning Forum Forums Discuss Simulation Process Integration and Design Optimization Maxwell & Matlab integration with Optislang in electric generator optimization Reply To: Maxwell & Matlab integration with Optislang in electric generator optimization

Veit Bayer
Ansys Employee

 

Dear Wenqin Song,

when you setup the MATLAB node in optiSLang, it first reads and tries to parse the script. But it cannot resolve dependencies, e.g.

a=1
b=a
results in b as STRING “a” because a is unknown.

Under the script there is a pulldown “Detect assignments”. Change this to "Test run". The MATLAB script is executed once, then in the list below the text you’ll find all variables with correct type and initial value.

Important hint on initial values: you want your script to run outside optiSLang, too, so you assign values, like A=1. But if A is a parameter to be varied by optiSLang, any parameter variation will be overwritten by this assignment. To prevent this, we typically use the following code to “register” parameters:

 

% Check if MATLAB is called outside optiSLang
if ~exist('OSL_REGULAR_EXECUTION','var')
  clear all;
  close all;
  OSL_REGULAR_EXECUTION = false;
end
% Inputs (define reference variables)
if OSL_REGULAR_EXECUTION == false
  X1 = 1.0;
  X2 = 1.0;
  X3 = 1.0; 

  X4 = 1.0;
  X5 = 1.0;
end

 

Please have close look at this tutorial and the corresponding script: Sensitivity Analysis of a Coupled Function in MATLAB (ansys.com)

Regards,
Veit