📜 ⬆️ ⬇️

Work with CST parameters from Matlab

Headline
At the beginning of our career path, small tasks are posed to us, the result of the solution of which we immediately run to show the leader. We get tips and comments, then we run to redo. At the end of the working day we turn off the computer and go home with peace of mind.

But as your experience and task list grows, there is a shortage of time. At some point it begins to seem that you can not leave the office. Then automation will help us become more productive. Under a cat there will be a small experience of the designer of antenna engineering.

Introduction


In the past and my first post on Habré, I told you, dear reader, how to manage CST MWS using the Matlab code. Let the comment in the comments this article has not received, but there are reviews of my colleagues and friends. And the most important question: “Will you show specific applications?” It’s impossible that the application options are limited only by your imagination and the functionality of the package did not work. According to this, at the request of the workers today we will look at the example of controlling the CST MWS from outside.

In this case, I wanted to force the machine to perform tasks while I was not there, or I was busy with something more important and useful at the current time. Therefore, it was not only decided to dig into your favorite electrodynamic modeling packages ( CST Microwave Studio and Ansys HFSS ) for control from outside. Ideally, using a tool that I know very well, which is Mathworks Matlab .
')
First of all, I would like to note that the article does not claim to be super uniqueness and in some parts will consist of a translation of the CST certificate, and the things highlighted below will seem too obvious to someone.

Work with parameters


I love the options. By inexperience, I used to develop non-parametric models, but now I parametrize almost everything. Why am I doing this? And I like it so much. This way I can rebuild any geometry in seconds without error if I made a good model. No problems with geometry = no problems with the calculated grid = no problems with optimization, so you can always quickly transfer the project to other frequencies, to other enclosures and much more.

Very often a situation arises when it is necessary to carry out a series of calculations with a change in the parameter and an analysis of the behavior of the structure as it changes. The built-in utility Parameter Sweep suggests that we change the parameters according to a linear or logarithmic law, choosing the number of points or the step between them. It is also possible to enter specific parameter values. And if the law of parameter change wants to see another?

Parameters in CST

Strategy


In the general case, our task is quite simple, it comes down to calculating the parameter values ​​and transferring the CST for calculation. Whatever was not so boring, fasten the output of any graphs. Below is a simple flowchart of what we will do.

Figure 1 - Block diagram of the script

The only thing I want to note is that I called the export of the result in text format, and put the stage of presentation of the simulation results in Matlab into the “Post-processing” block.

Let's start


For example, we will use my 5-minute project of a rectangular patch antenna.

Patch antenna

The figure above shows the model itself for the calculation and the parameter L, which we will change.

Let the parameter L be a quadratic law ranging from 10 to 100, the number of values ​​5:

x = sqrt(10):(10-sqrt(10))/5:10; y = x.^2; 

Just in case, I’ll give a graph, even though everyone understands how it should look.

Parabola

We launch CST and open our project, and also attach all the necessary CST objects to variables:

 cst = actxserver('CSTStudio.Application'); mws = cst.invoke('NewMWS'); mws.invoke('OpenFile','path\to\file.cst'); solv = mws.invoke('Solver'); export = mws.invoke('ASCIIExport'); plot1d = mws.invoke('Plot1D'); 

Briefly about what these 6 lines represent in the order of their writing:

  1. Binding the CST Studio application object to the cst variable
  2. Binding the CST MWS window to the mws variable
  3. Opening in the CST MWS file window
  4. Binding calculator object to solv variable
  5. Binding an Export Object in ASCII to an Export Variable
  6. Binding the object of one-dimensional graphs to the variable plot1d

Next, we introduce an intermediate variable, which is useful for generating the paths to the result files:

 f_cell = cell(length(y),1); 

What can we achieve from CST about the parameters used in the project?
  • We can delete the parameter with the command
     mws.invoke('DeleteParameter','ParameterName'); 
  • Find out the number of parameters
     mws.invoke('GetNumberOfParameters'); 
  • Find the name of the parameter by its number
     mws.invoke('GetParameterName','ParameterNumber'); 
  • Find out the value of the parameter by its number in double
     mws.invoke('GetParameterNValue','ParameterNumber'); 
  • Find out the value of the parameter by its number in string
     mws.invoke('GetParameterNValue','ParameterNumber'); 
  • Find out the value of the parameter by its name in string
     mws.invoke('RestoreParameter','ParameterName'); 
  • Find out the value of the parameter by its name in double
     mws.invoke('RestoreDoubleParameter','ParameterName'); 
  • Learn the expression with which the parameter is calculated by its name in string
     mws.invoke('RestoreParameterExpression','ParameterName'); 
  • Create a new parameter string (or replace the existing one) with its description
     mws.invoke('StoreParameterWithDescription','ParameterName', 'ParameterValue','ParameterDescription'); 
  • Create a new parameter string (or replace an existing one) without a description.
     mws.invoke('StoreParameter','ParameterName', 'ParameterValue'); 
  • Create a new double parameter (or replace an existing one) without a description.
     mws.invoke('StoreDoubleParameter','ParameterName', 'ParameterValue'); 
  • Make sure that the parameter name is not taken, if it is not necessary to create a new one with the specified values, otherwise do not touch the existing one.
     mws.invoke('MakeSureParameterExists','ParameterName', 'ParameterValue'); 
  • Add a description to an existing parameter by name.
     mws.invoke('SetParameterDescription','ParameterName'); 
  • Pull out the arrays by the number of the start of the calculation with the names of the parameters and their values
     mws.invoke('GetParameterCombination','ResultId','ParameterNames','ParametersValues'); 

I somehow could not fasten the last function in the list to the matlab. But not the essence, in this case, we do not need it.

Since I greatly simplified the task, only one function is interesting from the list presented in the spoiler:

 mws.invoke('StoreDoubleParameter','ParameterName', 'ParameterValue'); 

In the body of the loop, we will need to do only 4 operations: pass the parameter, update the model, run the calculation and export the results.

Open a cycle with a length from 1 to the number of values ​​of the parameter y

 for i=1: length(y) 

Pass the parameter:

 mws.invoke('StoreDoubleParameter','L', y(i)); 

We update the model:

 mws.invoke('Rebuild'); 

Run the calculation:

 solv.invoke('Start'); 

Next, we prepare string variables for the paths to save files (with the first line we translate the current parameter values ​​into a string, the second one - create the string for the path to save the file, the third one - write the address of the current result file to the array of cells:

 str_y = num2str(y(i)); f_name = strcat('C:\Results_patch\patch_z_im_y=', str_y,'.txt'); f_cell(i,1) = {f_name}; 

As a result for export, I chose the graph of the imaginary part of the input resistance, so next is the selection of the corresponding branch of the result tree, the procedure for exporting to ASCII, described in the previous post , as well as the end of the cycle.

 mws.invoke('SelectTreeItem','1D Results\Z Matrix\Z1,1'); plot1d.invoke('PlotView','imaginary'); export.invoke('Reset'); export.invoke('FileName',f_name); export.invoke('Mode','FixedNumber'); export.invoke('Step','1001'); export.invoke('Execute') end 

After the done actions, we have a set of files with the result of calculating the imaginary part of the input resistance. But what else to do with frequency-dependent quantities? Display as a graph.

Since all the output results in ASCII from CST have a header, we need to get rid of it, in our case these are two lines at the beginning of each file:

  Frequency / MHz Z1,1/imaginary ---------------------------------------------------------------------- 

To do this, we use the unpretentious function given under the spoiler below, which I wrote quite a while ago. The input parameter for the function is the path to the file to remove the header.

The function reads the file line by line, then overwrites the data without a header into it, and on output gives you a variable with the data from the file.

Remove header
 function [f_cell] = h_remove(ishod) ffile = fopen(ishod,'rt'); f_cell=cell(1003,1); i=1; for i=1:length(f_cell) %while feof(Farfield) ==0 f_cell{i,1} = fgetl(ffile); %i = i+1; end f_cell = f_cell(3: end, 1); fclose(ffile); ffile = fopen(ishod, 'w+'); for i = 1: length(f_cell) fprintf(ffile, '%s\r\n', f_cell{i,1}); end fclose(ffile); f_dat = load(ishod); end 


With the help of the above function, we load into the variable results of the calculations, display the graph and lightly brush it.

 y1 = h_remove(f_cell{1,1}); y2 = h_remove(f_cell{2,1}); y3 = h_remove(f_cell{3,1}); y4 = h_remove(f_cell{4,1}); y5 = h_remove(f_cell{5,1}); y6 = h_remove(f_cell{6,1}); plot(y1(:,1),y1(:,2), '-r', y1(:,1),y2(:,2), '--r',y1(:,1),y3(:,2), '-.r',y4(:,1),y1(:,2), '-b',y1(:,1),y5(:,2), '--b',y1(:,1),y6(:,2), '-.b') grid on xlabel(', ') ylabel(', ') legend(num2str(y(1)),num2str(y(2)),num2str(y(3)),num2str(y(4)),num2str(y(5)),num2str(y(6))) 

Final schedule

findings


In just such an uncomplicated way, in just 39 lines, we forced CST to make a parametric calculation according to the law of parameter change that we set, derived the calculation results for further processing in ASCII, well, and screwed the output in Matlab.

You can say: Uh, “everything can be done without Matlab!” What are you thinking here is the built-in macro editor on VBA. ” And I will answer: “Yes, you are right. You can write everything in VBA if you like it that way. ”

But further processing of the results in Matlab may be useful. You can also use the Report Generator. Then it becomes possible to leave the car to carry out the calculation in your absence, and then it remains only to print and put the ready report on the manager’s table.

Links


Below I place the test script parsed here, and the h_remove functions for your experiments, as well as a link to the project file for those who did not notice the link in the text:

→ Antenna design

Script
 %%    x = sqrt(10):(10-sqrt(10))/5:10; y = x.^2; %%     CST %cst = actxserver('CSTStudio.Application'); %mws = cst.invoke('NewMWS'); %mws.invoke('OpenFile','C:\patch.cst'); solv = mws.invoke('Solver'); export = mws.invoke('ASCIIExport'); plot1d = mws.invoke('Plot1D'); f_cell = cell(length(y),1); %%    for i=1: length(y) mws.invoke('StoreDoubleParameter','L', y(i)); mws.invoke('Rebuild'); solv.invoke('Start'); str_y = num2str(y(i)); f_name = strcat('C:\Results_patch\patch_z_im_y=', str_y,'.txt'); f_cell(i,1) = {f_name}; mws.invoke('SelectTreeItem','1D Results\Z Matrix\Z1,1'); plot1d.invoke('PlotView','imaginary'); export.invoke('Reset'); export.invoke('FileName',f_name); export.invoke('Mode','FixedNumber'); export.invoke('Step','1001'); export.invoke('Execute') end %%  y1 = h_remove(f_cell{1,1}); y2 = h_remove(f_cell{2,1}); y3 = h_remove(f_cell{3,1}); y4 = h_remove(f_cell{4,1}); y5 = h_remove(f_cell{5,1}); y6 = h_remove(f_cell{6,1}); plot(y1(:,1),y1(:,2), '-r', y1(:,1),y2(:,2), '--r',y1(:,1),y3(:,2), '-.r',y4(:,1),y1(:,2), '-b',y1(:,1),y5(:,2), '--b',y1(:,1),y6(:,2), '-.b') grid on xlabel(', ') ylabel(', ') legend(num2str(y(1)),num2str(y(2)),num2str(y(3)),num2str(y(4)),num2str(y(5)),num2str(y(6))) 


h_remove
 function [f_dat] = h_remove(ishod) %     ffile = fopen(ishod,'rt'); %        f_cell=cell(1003,1); %          i=1; for i=1:length(f_cell) %while feof(Farfield) ==0 f_cell{i,1} = fgetl(ffile); %i = i+1; end f_cell = f_cell(3: end, 1); fclose(ffile); ffile = fopen(ishod, 'w+'); for i = 1: length(f_cell) fprintf(ffile, '%s\r\n', f_cell{i,1}); end fclose(ffile); f_dat = load(ishod); end 

Source: https://habr.com/ru/post/336396/


All Articles