1
+ % MATLAB-HFSS API
2
+ % Author: Giacomo Giannetti, University of Florence
3
+
4
+ %% Preamble
5
+
6
+ clear
7
+ clc
8
+ PAF = PathAndFile ;
9
+
10
+ filename1 = " VariablesProject.txt" ; % Project variables from HFSS
11
+ VariablesProject = importfile(filename1 );
12
+ filename2 = " VariablesDesign.txt" ; % Design variables from HFSS
13
+ VariablesDesign = importfile(filename2 );
14
+ Variables = [VariablesProject(: ,1 : 3 ); VariablesDesign(: ,1 : 3 )];
15
+ Var2Sub = SignpostSubstitution(PAF .filenameINstart_filename ,PAF .filenameIN_filename ,Variables );
16
+ PAF.SignpostList = Var2Sub{: ,4 };
17
+
18
+ x = Var2Sub{: ,2 };
19
+
20
+ output = SimHFSS_Matlab(x ,PAF );
21
+
22
+ figure
23
+ hold on
24
+ plot(output{1 }{: ,1 },output{1 }{: ,2 })
25
+ hold off
26
+
27
+ figure
28
+ hold on
29
+ plot(output{2 }{: ,1 },output{2 }{: ,2 })
30
+ hold off
31
+
32
+ %% Functions signposts
33
+ function Var2Sub = SignpostSubstitution(filenameIN ,filenameOUT ,Variables )
34
+
35
+ Var2Sub = Variables(~isnan(Variables{: ,2 }),: );
36
+
37
+ [Nvar , ~ ] = size(Var2Sub );
38
+ SignpostList = generateSignposts(Nvar );
39
+ Var2Sub = [Var2Sub , SignpostList ];
40
+
41
+ fid = fopen(filenameIN ); % Open the file to modify
42
+ C = textscan(fid ,' %s ' ,' delimiter' ,' \n ' );
43
+ fclose(fid );
44
+
45
+ % Update the variables
46
+ for k= 1 : 500 % numel(C{1,1})
47
+ % Find the values to substitute
48
+ if contains(C{1 ,1 }(k )," VariableProp" )
49
+ for idx= 1 : Nvar
50
+ ActualVar = extractBetween(C{1 ,1 }(k )," VariableProp('" ," ', '" );
51
+ if strcmp(ActualVar , Var2Sub{idx ,1 })
52
+ % C{1,1}(k) % Before substitution
53
+ if contains(C{1 ,1 }(k )," oa(" )
54
+ help = extractBetween(C{1 ,1 }(k )," ', '" ," ', oa(" );
55
+ else
56
+ help = extractBetween(C{1 ,1 }(k )," ', '" ," ')" );
57
+ end
58
+ ValueUnit = reverse(extractBefore(reverse(help )," '" )); % Numeric value with unit
59
+ Value = erase(ValueUnit ,char(Var2Sub{idx ,3 }));
60
+ C{1 ,1 }(k ) = regexprep(C{1 ,1 }(k ), Value , SignpostList{idx });
61
+ % C{1,1}(k) % After substitution
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ % Print new file
68
+ fid = fopen(filenameOUT ,' w' );
69
+ for k= 1 : numel(C{1 ,1 })
70
+ fprintf(fid ,' %s\r\n ' ,C{1 ,1 }{k ,1 });
71
+ end
72
+ fclose(fid );
73
+
74
+ end
75
+
76
+ function Signposts = generateSignposts(Nvar )
77
+
78
+ Numbers = [" 0" ; " 1" ; " 2" ; " 3" ; " 4" ; " 5" ; " 6" ; " 7" ; " 8" ; " 9" ];
79
+ Words = [" A" ; " B" ; " C" ; " D" ; " E" ; " F" ; " G" ; " H" ; " L" ; " M" ];
80
+ NWords = length(Words );
81
+
82
+ Signposts = cell(NWords ,1 );
83
+
84
+ for idx = 1 : Nvar
85
+ idxNumbers = floor(idx ./ NWords )+1 ;
86
+ idxWords = mod(idx , NWords );
87
+ if idxWords == 0
88
+ idxWords = NWords ;
89
+ idxNumbers = idxNumbers - 1 ;
90
+ end
91
+
92
+ Signposts{idx } = strcat(Numbers(idxNumbers ), Words(idxWords ), Numbers(idxNumbers ), Words(idxWords ));
93
+ end
94
+
95
+ end
96
+
97
+ function Variables = importfile(filename , dataLines )
98
+ % IMPORTFILE Import data from a text file
99
+ % VARIABLES = IMPORTFILE(FILENAME) reads data from text file FILENAME
100
+ % for the default selection. Returns the data as a table.
101
+ %
102
+ % VARIABLES = IMPORTFILE(FILE, DATALINES) reads data for the specified
103
+ % row interval(s) of text file FILENAME. Specify DATALINES as a
104
+ % positive scalar integer or a N-by-2 array of positive scalar integers
105
+ % for dis-contiguous row intervals.
106
+
107
+ %% Input handling
108
+
109
+ % If dataLines is not specified, define defaults
110
+ if nargin < 2
111
+ dataLines = [2 , Inf ];
112
+ end
113
+
114
+ %% Set up the Import Options and import the data
115
+ opts = delimitedTextImportOptions(" NumVariables" , 8 );
116
+
117
+ % Specify range and delimiter
118
+ opts.DataLines = dataLines ;
119
+ opts.Delimiter = " \t" ;
120
+
121
+ % Specify column names and types
122
+ opts.VariableNames = [" Name" , " Value" , " Unit" , " EvaluatedValue" , " Description" , " Readonly" , " Hidden" , " Sweep" ];
123
+ opts.VariableTypes = [" char" , " double" , " categorical" , " double" , " char" , " categorical" , " categorical" , " categorical" ];
124
+
125
+ % Specify file level properties
126
+ opts.ExtraColumnsRule = " ignore" ;
127
+ opts.EmptyLineRule = " read" ;
128
+
129
+ % Specify variable properties
130
+ opts = setvaropts(opts , [" Name" , " Description" ], " WhitespaceRule" , " preserve" );
131
+ opts = setvaropts(opts , [" Name" , " Unit" , " Description" , " Readonly" , " Hidden" , " Sweep" ], " EmptyFieldRule" , " auto" );
132
+ opts = setvaropts(opts , " EvaluatedValue" , " TrimNonNumeric" , true );
133
+ opts = setvaropts(opts , " EvaluatedValue" , " ThousandsSeparator" , " ," );
134
+
135
+ % Import the data
136
+ Variables = readtable(filename , opts );
137
+
138
+ end
139
+
140
+ %% Functions HFSS-MATLAB API
141
+
142
+ function PAF = PathAndFile
143
+ % Paths to change
144
+ PAF.mainPath = " C:\Users\giannetti\Documents\HFSS-MATLAB_interface\Dev6\" ;
145
+ PAF.HFSSpath = " C:\"" Program Files\AnsysEM\AnsysEM21.2\Win64\ansysedt.exe"" " ;
146
+ PAF.HFSSfile_filename = " Modified.aedt" ;
147
+ PAF.model_name = " Design" ;
148
+ PAF.HFSSscript_filename = " ExportToFile_Sparam.py" ;
149
+ PAF.HFSSoutput_filename = [" S11mag" , " S11pha" ];
150
+ PAF.filenameINstart_filename = " BaseNoSignposts.txt" ;
151
+ PAF.filenameIN_filename = " Base.txt" ;
152
+ PAF.filenameOUT_filename = PAF .HFSSfile_filename ;
153
+
154
+ % To not change
155
+ PAF.strBatchSolve = " /Ng /BatchSolve " ;
156
+ PAF.strBatchExtract = " /Ng /BatchExtract " ;
157
+
158
+ PAF.HFSSfile = strcat(PAF .mainPath , PAF .HFSSfile_filename );
159
+ PAF.HFSSscript = strcat(PAF .mainPath ,PAF .HFSSscript_filename ," " );
160
+ PAF.HFSSoutput = strcat(PAF .mainPath ,PAF .HFSSoutput_filename );
161
+ PAF.filenameIN = strcat(PAF .mainPath ,PAF .filenameIN_filename );
162
+ PAF.filenameOUT = strcat(PAF .mainPath ,PAF .filenameOUT_filename );
163
+ PAF.filenameLOG = strcat(PAF .mainPath , PAF .HFSSfile_filename , " .batchinfo\" , erase(PAF .HFSSfile_filename , " aedt" ), " log" );
164
+ PAF.cmdHFSSsim = convertStringsToChars(strcat(PAF .HFSSpath , PAF .strBatchSolve , PAF .HFSSfile ));
165
+ PAF.cmdHFSSres = convertStringsToChars(strcat(PAF .HFSSpath , PAF .strBatchExtract , PAF .HFSSscript , PAF .HFSSfile ));
166
+
167
+ WritingExportFile(PAF )
168
+ end
169
+
170
+ function ParameterUpdate(sx ,filenameIN ,filenameOUT ,PAF )
171
+ fid = fopen(filenameIN ); % Open the file to modify
172
+ C = textscan(fid ,' %s ' ,' delimiter' ,' \n ' );
173
+ fclose(fid );
174
+
175
+ formatSpec = " %.14f" ;
176
+ SignpostList = PAF .SignpostList ;
177
+
178
+ % Update the variables
179
+ for k= 1 : 500 % numel(C{1,1})
180
+ % Find the values to substitute
181
+ for idx= 1 : length(sx )
182
+ C{1 ,1 }(k ) = regexprep(C{1 ,1 }(k ),convertStringsToChars(SignpostList(idx )), num2str(sx(idx ), formatSpec ));
183
+ end
184
+ end
185
+
186
+ % Print new file
187
+ fid = fopen(filenameOUT ,' w' );
188
+ for k= 1 : numel(C{1 ,1 })
189
+ fprintf(fid ,' %s\r\n ' ,C{1 ,1 }{k ,1 });
190
+ end
191
+ fclose(fid );
192
+ end
193
+
194
+ function output = SimHFSS_Matlab(x ,PAF )
195
+
196
+ HFSSfile = PAF .HFSSfile ;
197
+ HFSSoutput = PAF .HFSSoutput ;
198
+ filenameLOG = PAF .filenameLOG ;
199
+ filenameIN = PAF .filenameIN ;
200
+ filenameOUT = HFSSfile ;
201
+ cmdHFSSsim = PAF .cmdHFSSsim ;
202
+ cmdHFSSres = PAF .cmdHFSSres ;
203
+
204
+ ParameterUpdate(x ,filenameIN ,filenameOUT ,PAF )
205
+ system(cmdHFSSsim );
206
+
207
+ % Check if the simulation was successful
208
+ fid = fopen(filenameLOG );
209
+ C = textscan(fid ,' %s ' ,' delimiter' ,' \n ' );
210
+ fclose(fid );
211
+
212
+ idx = 1 ;
213
+ while convertCharsToStrings(C{1 ,1 }{end }) == " [Exiting application]"
214
+ % Notify that HFSS stopped
215
+ t = now ; TimeNow = datetime(t ,' ConvertFrom' ,' datenum' );
216
+ DateString = datestr(TimeNow );
217
+ warning(strcat(" HFSS is not working properly and stopped at " , DateString ))
218
+
219
+ % The execution pauses if 10 trials to launch the simulation failed
220
+ idx = idx + 1 ;
221
+ if idx > 10
222
+ pause % Something does not work properly (e.g. the internet connection is missing)
223
+ end
224
+
225
+ % Launch again the simulation
226
+ pause(10 )
227
+ delete(strcat(HFSSfile , " .lock" ))
228
+ pause(10 )
229
+ system(cmdHFSSsim );
230
+
231
+ fid = fopen(filenameLOG );
232
+ C = textscan(fid ,' %s ' ,' delimiter' ,' \n ' );
233
+ fclose(fid );
234
+ end
235
+
236
+ % Result extraction
237
+ system(cmdHFSSres );
238
+ output = Results(HFSSoutput );
239
+ end
240
+
241
+ function output = Results(HFSSoutput )
242
+ Nout = length(HFSSoutput );
243
+ T = cell(1 ,Nout );
244
+
245
+ for idx= 1 : Nout
246
+ T{idx } = readtable(strcat(HFSSoutput(idx ), " .csv" ), ' VariableNamingRule' , ' preserve' );
247
+ end
248
+
249
+ output = T ;
250
+ end
251
+
252
+ function WritingExportFile(PAF )
253
+ N = length(PAF .HFSSoutput_filename );
254
+
255
+ fid = fopen(strcat(PAF .mainPath ,PAF .HFSSscript_filename ),' w' );
256
+
257
+ fprintf(fid ,' oDesktop.RestoreWindow()\r\n ' );
258
+ fprintf(fid ,' oProject = oDesktop.SetActiveProject("%s ")\r\n ' ,erase(PAF .HFSSfile_filename ," .aedt" ));
259
+ fprintf(fid ,' oDesign = oProject.SetActiveDesign("%s ")\r\n ' ,PAF .model_name );
260
+ fprintf(fid ,' oModule = oDesign.GetModule("ReportSetup")\r\n ' );
261
+
262
+ for idx = 1 : N
263
+ fprintf(fid ,' oModule.UpdateReports(["%s "])\r\n ' ,PAF .HFSSoutput_filename(idx ));
264
+ fprintf(fid ,' oModule.ExportToFile("%s ", "%s%s .csv")\r\n ' ,PAF .HFSSoutput_filename(idx ),PAF .mainPath ,PAF .HFSSoutput_filename(idx ));
265
+ end
266
+
267
+ fclose(fid );
268
+ end
0 commit comments