废话不说直接进正题。
Maxwell软件中有采用VBScript进行自动化仿真的功能。
因此,也具有录制VB脚本的功能。在Tools->Record Script to File
点击后会让你选择将录制的VBS放在那里。我们用MATLAB控制Maxwell就需要这个功能。
这里,作为例子,我们建立一个test.vbs的文件。
操作时点击Tools->Record Script to File,选择好位置,并输入文件名。
这些确定之后,Maxwell就已经开启了脚本录制功能,我们对软件的每一个操作均会被记录在test.vbs。
比如,我们建立一个2D的电学仿真例子,并绘制一个矩形。
操作完毕后,我们点击Tools->Stop Script Recording。打开test.vbs,可以看到:
' ----------------------------------------------
' Script Recorded by Ansoft Maxwell Version 16.0.0
' 3:16:03 下午 十月 27, 2019
' ----------------------------------------------
Dim oAnsoftApp
Dim oDesktop
Dim oProject
Dim oDesign
Dim oEditor
Dim oModule
Set oAnsoftApp = CreateObject("AnsoftMaxwell.MaxwellScriptInterface")
Set oDesktop = oAnsoftApp.GetAppDesktop()
oDesktop.RestoreWindow
Set oProject = oDesktop.SetActiveProject("Project1")
oProject.InsertDesign "Maxwell 2D", "Maxwell2DDesign1", "Magnetostatic", ""
Set oDesign = oProject.SetActiveDesign("Maxwell2DDesign1")
oDesign.SetSolutionType "Electrostatic", "XY"
Set oEditor = oDesign.SetActiveEditor("3D Modeler")
oEditor.CreateRectangle Array("NAME:RectangleParameters", "IsCovered:=", true, "XStart:=", _
"0.3mm", "YStart:=", "-0.5mm", "ZStart:=", "0mm", "Width:=", "-1.5mm", "Height:=", _
"0.9mm", "WhichAxis:=", "Z"), Array("NAME:Attributes", "Name:=", "Rectangle1", "Flags:=", _
"", "Color:=", "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=", _
"Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) & "vacuum" & Chr(34) & "", "SolveInside:=", _
true)
在这里,前面配置信息改成MATLAB代码的方法,请参看下面给出的例子。这里不做过多解释
如果在运行代码时,前面的配置信息报错了,请重新安装Maxwell。
这里我主要说一下如何把VB脚本变成MATLAB代码。
上例中绘制矩形的VB脚本是
oEditor.CreateRectangle Array("NAME:RectangleParameters", "IsCovered:=", true, "XStart:=", _
"0.3mm", "YStart:=", "-0.5mm", "ZStart:=", "0mm", "Width:=", "-1.5mm", "Height:=", _
"0.9mm", "WhichAxis:=", "Z"), Array("NAME:Attributes", "Name:=", "Rectangle1", "Flags:=", _
"", "Color:=", "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=", _
"Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) & "vacuum" & Chr(34) & "", "SolveInside:=", _
true)
相应MATLAB代码应为
invoke(Editor,'CreateRectangle',...
{'NAME:RectangleParameters', 'CoordinateSystemID:=',true,...
'XStart:=','0.3mm','YStart:=','-0.5mm','ZStart:=','0mm',...
'Width:=','-1.5mm', 'Height:=','0.9mm','WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=',RegularName,'Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','vacuum','SolveInside:=',true});
对比之后会发现,vbs变成MATLAB代码需要把双引号变成单引号。
Array()变成{},当然代码开始部分也需要改变下,这部分的改变对于所有函数均是相同。
其余指令的变换那自己录制的VBS与本文下面的代码对照。
最后,还是建议大家仔细阅读下Maxwell的说明书。
里面有详细VBS函数的讲解,以及某些通过脚本录制得不到的内置函数。
Maxwell可以用VBS进行自动化仿真
MATLAB可以运行VBS对Maxwell进行控制
代码如下:
%计算每个极板之间的电容值
%有两个圆的流型
clc;
clear;
%%%%%%%%%%%%%
Num_ele=8; %电极数量
ratio=9/10; %电极长度与空隙长度的比值
PipelineRadius=50; %管道内表面半径长度,管壁厚度默认为1mm.
%%%%%%%%%%%%%%
iMaxwell = actxserver('AnsoftMaxwell.MaxwellScriptInterface');
Desktop = iMaxwell.GetAppDesktop();
Desktop.RestoreWindow;
Project = Desktop.NewProject;
invoke(Project,'InsertDesign','Maxwell 2D','Design2','','');
Design = Project.SetActiveDesign('Design2');
invoke(Design,'SetSolutionType','Electrostatic', 'XY');
Editor = Design.SetActiveEditor('3D Modeler');
%%%%绘制管壁
Radius=[num2str(PipelineRadius+1) 'mm'];
invoke(Editor,'CreateCircle',...
{'NAME:CircleParameters','CoordinateSystemID:=',-1,...
'XCenter:=','0mm','YCenter:=','0mm','ZCenter:=','0mm',...
'Radius:=',Radius,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=','pipe','Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','glass','SolveInside:=',true});
Radius=[num2str(PipelineRadius) 'mm'];
invoke(Editor,'CreateCircle',...
{'NAME:CircleParameters','CoordinateSystemID:=',-1,...
'XCenter:=','0mm','YCenter:=','0mm','ZCenter:=','0mm',...
'Radius:=',Radius,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=','Circle1','Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','glass','SolveInside:=',true});
invoke(Editor,'Subtract' ,...
{'NAME:Selections', 'Blank Parts:=', 'pipe', 'Tool Parts:=', 'Circle1'}, ...
{'NAME:SubtractParameters', 'CoordinateSystemID:=', -1, 'KeepOriginals:=', false});
%极板绘制 基板默认1mm厚度
%基准坐标
x=[num2str(PipelineRadius*cos(ratio*pi/Num_ele)) 'mm'];
y=[num2str(-PipelineRadius*sin(ratio*pi/Num_ele)) 'mm'];
for i=1:Num_ele
PlantName=['Plant' num2str(i)];
CircleName=['CirclePlant' num2str(i)];
RegularName=['Regular' num2str(i)];
IntersectName=['Plant' num2str(i) ',Regular' num2str(i) ];
Radius=[num2str(PipelineRadius+2) 'mm'];
invoke(Editor,'CreateCircle',...
{'NAME:CircleParameters','CoordinateSystemID:=',-1,...
'XCenter:=','0mm','YCenter:=','0mm','ZCenter:=','0mm',...
'Radius:=',Radius,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=',PlantName,'Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','copper','SolveInside:=',true});
Radius=[num2str(PipelineRadius+1) 'mm'];
invoke(Editor,'CreateCircle',...
{'NAME:CircleParameters','CoordinateSystemID:=',-1,...
'XCenter:=','0mm','YCenter:=','0mm','ZCenter:=','0mm',...
'Radius:=',Radius,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=',CircleName,'Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','copper','SolveInside:=',true});
invoke(Editor,'Subtract' ,...
{'NAME:Selections', 'Blank Parts:=', PlantName, 'Tool Parts:=',CircleName}, ...
{'NAME:SubtractParameters', 'CoordinateSystemID:=', -1, 'KeepOriginals:=', false});
%矩形
%旋转角度
alpha=2*pi*(i-1)/Num_ele; %sin(360/Num_ele*ratio*0.5*pi/180)
Width=[num2str(2*(PipelineRadius+1)*sin(pi*ratio/Num_ele)) 'mm'];
invoke(Editor,'CreateRectangle',...
{'NAME:RectangleParameters', 'CoordinateSystemID:=',-1,...
'XStart:=',x,'YStart:=',y,'ZStart:=','0mm',...
'Width:=',Width,'Height:=',Width,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=',RegularName,'Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','copper','SolveInside:=',true});
%旋转,旋转角度是弧度制
invoke(Editor,'Rotate' ,...
{'NAME:Selections', 'Selections:=',RegularName},...
{'NAME:RotateParameters', 'RotateAxis:=', 'Z', 'RotateAngle:=',alpha});
%相交
invoke(Editor,'Intersect' ,...
{'NAME:Selections','Selections:=',IntersectName}, ...
{'NAME:IntersectParameters', 'KeepOriginals:=',false});
end
Module = Design.GetModule('BoundarySetup');
VoltageValue='0V';
for i=1:Num_ele%创建N个电源,并接到极板上,
NAMEVoltage=['NAME:Voltage' num2str(i)];
NAMEObjects=['Plant' num2str(i)];
invoke(Module,'AssignVoltage', ...
{NAMEVoltage, 'Objects:=', {NAMEObjects}, 'Value:=', ...
VoltageValue, 'IsPositive:=', true});
end
err=MaxwellParameterSetup(Design,Num_ele);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%添加内容物%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Editor = Design.SetActiveEditor('3D Modeler');
Radius=[num2str(PipelineRadius) 'mm'];
invoke(Editor,'CreateCircle',...
{'NAME:CircleParameters','CoordinateSystemID:=',-1,...
'XCenter:=','0mm','YCenter:=','0mm','ZCenter:=','0mm',...
'Radius:=',Radius,'WhichAxis:=','Z'},...
{'NAME:Attributes','Name:=','inner','Flags:=','','Color:=','(132 132 193)', ...
'Transparency:=',0,'PartCoordinateSystem:=','Global','MaterialName:=','air','SolveInside:=',true});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%配置网格
Module = Design.GetModule('MeshSetup');
invoke(Module,'AssignLengthOp',{'NAME:Length1', 'RefineInside:=', true, 'Enabled:=',true,...
'Objects:=',{'inner'},'RestrictElem:=', false, 'NumMaxElem:=','1000', ...
'RestrictLength:=', true, 'MaxLength:=', '0.5mm'});
Module = Design.GetModule('AnalysisSetup');
invoke(Module,'InsertSetup', ...
'Electrostatic', {'NAME:Setup1','Enabled:=', true, 'MaximumPasses:=', 10, ...
'MinimumPasses:=', 2, 'MinimumConvergedPasses:=', 1,'PercentRefinement:=', 30,...
'SolveFieldOnly:=', false, 'PercentError:=', 1, 'SolveMatrixAtLast:=', true, ...
'PercentError:=', 1});
for i=1:Num_ele
%把第i个极板的电压设为20V,其他的设成0V,
Module = Design.GetModule('BoundarySetup');
CurrentVoltage=['Voltage' num2str(i)];
NameCurrentVoltage=['NAME:Voltage' num2str(i)];
if i==1
invoke(Module,'EditVoltage',CurrentVoltage,{NameCurrentVoltage,'Value:=','20V'});
else
PriorVoltage=['Voltage' num2str(i-1)];
NamePriorVoltage=['NAME:Voltage' num2str(i-1)];
invoke(Module,'EditVoltage',PriorVoltage,{NamePriorVoltage,'Value:=','0V'});
invoke(Module,'EditVoltage',CurrentVoltage,{NameCurrentVoltage,'Value:=','20V'});
end
invoke(Project,'AnalyzeAll');
Module = Design.GetModule('FieldsReporter');
if i==1%只写入一次即可
invoke(Module,'CopyNamedExprToStack', 'E_Vector');
end
%导出电压矢量
SaveAddress=['E:\Plant' num2str(i) '.csv'];
max=[num2str(PipelineRadius) 'mm'];
min=[num2str(-PipelineRadius) 'mm'];
space=[num2str(2*PipelineRadius/63) 'mm'];
invoke(Module,'ExportOnGrid',SaveAddress,...
{'-50mm','-50mm','0mm'},{'50mm','50mm','0mm'},{'3.2258064516129032258064516129032mm','3.2258064516129032258064516129032mm','0mm'},...
'Setup1 : LastAdaptive',{ },true,'Cartesian',{'0mm','0mm','0mm'});
end
delete(iMaxwell);
function err=MaxwellParameterSetup(Design,Num_ele)
if rem(Num_ele,4)~=0
error('所给极板数量不能被4整除');
end
Module = Design.GetModule('MaxwellParameterSetup');
%%%%%%%%%%%%%%%%%% 8电极
if Num_ele==8
invoke(Module,'AssignMatrix', ...
{'NAME:Matrix1', {'NAME:MatrixEntry',...
{'NAME:MatrixEntry','Source:=', 'Voltage1', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage2', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage3', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage4', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage5', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage6', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage7', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage8', 'NumberOfTurns:=', '1'},...
}, ...
{'NAME:MatrixGroup'},'GroundSources:=',''});
%%%%%%%%%%%%%%%%%% 12电极
elseif Num_ele==12
invoke(Module,'AssignMatrix', ...
{'NAME:Matrix1', {'NAME:MatrixEntry',...
{'NAME:MatrixEntry','Source:=', 'Voltage1', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage2', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage3', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage4', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage5', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage6', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage7', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage8', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage9', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage10', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage11', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage12', 'NumberOfTurns:=', '1'},...
}, ...
{'NAME:MatrixGroup'},'GroundSources:=',''});
%%%%%%%%%%%%%%%%%% 16电极
elseif Num_ele==16
invoke(Module,'AssignMatrix', ...
{'NAME:Matrix1', {'NAME:MatrixEntry',...
{'NAME:MatrixEntry','Source:=', 'Voltage1', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage2', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage3', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage4', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage5', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage6', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage7', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage8', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage9', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage10', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage11', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage12', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage13', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage14', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage15', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage16', 'NumberOfTurns:=', '1'},...
}, ...
{'NAME:MatrixGroup'},'GroundSources:=',''});
%%%%%%%%%%%%%%%%%% 20电极
elseif Num_ele==20
invoke(Module,'AssignMatrix', ...
{'NAME:Matrix1', {'NAME:MatrixEntry',...
{'NAME:MatrixEntry','Source:=', 'Voltage1', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage2', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage3', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage4', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage5', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage6', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage7', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage8', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage9', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage10', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage11', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage12', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage13', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage14', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage15', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage16', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage17', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage18', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage19', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage20', 'NumberOfTurns:=', '1'},...
}, ...
{'NAME:MatrixGroup'},'GroundSources:=',''});
%%%%%%%%%%%%%%%%%% 24电极
elseif Num_ele==24
invoke(Module,'AssignMatrix', ...
{'NAME:Matrix1', {'NAME:MatrixEntry',...
{'NAME:MatrixEntry','Source:=', 'Voltage1', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage2', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage3', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage4', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage5', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage6', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage7', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage8', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage9', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage10', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage11', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage12', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage13', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage14', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage15', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage16', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage17', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage18', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage19', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage20', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage21', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage22', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage23', 'NumberOfTurns:=', '1'},...
{'NAME:MatrixEntry','Source:=', 'Voltage24', 'NumberOfTurns:=', '1'},...
}, ...
{'NAME:MatrixGroup'},'GroundSources:=',''});
else
error('程序中只写到24个电极!');
end
err=0;
end