Matlab-Ansys Maxwell interface for optimization error: Error using Interface.​4824CFB6_4​885_4ECF_B​6C8_314CC7​C0C148/Ope​nProject - Error: The remote procedure call failed.

Hello,
I design an electric motor with Ansys Maxwell and in order to optimize it I have the optimization code writen in Matlab which I have coupled with Ansys Maxwell.
In every iteration of the optimization Matlab puts different values to the variables (according to the optimization method) I use for the design and sends them to Maxwell. Then Maxwell computes the FEA analysis and sends back the results to Matlab.
For my optimization I want to have 30 particles (code PSO) and 40 iterations, which means that there will be a bit more of 1200 FEA from Maxwell to be computed. Every time Matlab has the new variables ready it calls Maxwell to open the project I want to run the FEA on and after the computation is completed the results are sent to back Matlab and the Maxwell project cloeses. The project is called to open again for the next FEA and the same procedure continues until the end of the optimization.
The problem which comes up is this. After a number of iterations are completed, suddenly Matlab cannot call the Maxwell project to open and I get the fallowing error:
Error using Interface.4824CFB6_4885_4ECF_B6C8_314CC7C0C148/OpenProject
Error: The remote procedure call failed.
Error in ObjectiveFunction (line 13)
oProject = invoke(oDesktop,'OpenProject',[project_path project_name '.aedt']);
Error in PSO_myTry_withParameters (line 100)
Run_objF = fobj(currentX);
Error in main (line 27)
[ GBEST , cgcurve , objective_total ] = PSO_myTry_withParameters( noP , maxIter, problem , visFlag) ;
This depending on the optimization code used each time happens at a different iteration. For example, when I use PSO (30 particles - 40 iterations) happens during the 20th iteration, although it has started computing FEA normally in that iteration. Also, in 3 different computers it occured in the same iteration, during the 20th. In that iteration about 570 FEA have been computed by Maxwell and 570 times the project has been opened and closed.
Acoording to the above error it is Matlab that cannot continue working properly. At first I thought that I may keep a lot of information in arrays and probably after a number of info stored Matlab crashes, so I put the code to keep as less info as possible but still the same problem occured.
I also changed the path of the Maxwell problem and put it in the same folder with the Matlab scripts but again the problem occured.
Has anyone come up with a similar problem? Can anyone help me with this inconvenience?
Thank you beforehand.

15 comentarios

Could you confirm that you are using activexserver calls? And not for example, a .NET assembly?
I confirm, I use activexserver with the command:
iMaxwell = actxserver('Ansoft.ElectronicsDesktop');
Thank you beforehand Walter.
I find evidence that ANSYS uses .NET internally, so it is plausible that it might be possible to invoke it through an assembly. Or perhaps you could use System.Diagnostics.Process and send commands standard input?
Do you need to "close" the geometry or project?
Could you open it just once and leave it open until the end?
It is possible to keep the project opened and only change the variables every time needed, but I was told that in that way more RAM memory is consumed and a collegue in the lab had problems when he was leaving the project opened. Although, with the method I use, RAM cosnumes until 90% and more of its total space as iterations get more and more.
The way I call ANSYS is (I also attach the whole following code, with which I call Ansys Maxwell.):
project_name = 'srm_optim_1'; %για L=350mm
design_name = 'Maxwell2DDesign1';
project_path = 'D:\path ....\ ... \ ....\';
iMaxwell = actxserver('Ansoft.ElectronicsDesktop');
oDesktop = invoke(iMaxwell,'GetAppDesktop');
oDesktop.RestoreWindow;
oProject = invoke(oDesktop,'OpenProject',[project_path project_name '.aedt']);
oProject = oDesktop.SetActiveProject(project_name);
try
Do = x(1);
Do_str = num2str(Do);
Do_mat = [Do_str, 'mm'];
invoke(oProject,'SetVariableValue','$Do_new', Do_mat);
... add the other variables as well with the same command as the above
oDesign = invoke(oProject, 'SetActiveDesign', design_name);
invoke(oDesign,'Analyze', 'Setup1');
oModule = oDesign.GetModule('ReportSetup');
%Average torque calculation
Torque = oModule.GetPropertyValue('Data Filter','Torque Table 1:avg(Moving1.Torque):Curve1','Max');
Torque = str2double(Torque);
... obtain all the rest needed results with the same way as in the two above commands
F_tryCatch = [Obj_f , Torque, T_ripple, Pc, i_phA, Ar_coil0, J];
catch
F_tryCatch = [inf, inf, inf, inf, inf, inf, inf];
end
F_obj = F_tryCatch;
%% Finalize AnsysMaxwell App
oDesktop.CloseProject(project_name);
delete(iMaxwell);
clear iMaxwell oDesign oDesktop oModule oProject ;
As an experiment, try creating the ActiveX at the beginning of your program and delete after, leaving it open every time, but still opening and closing the project each cycle.
You might want to onCleanup to be sure of killing the ActiveX session on error.
Thank you again for your help.
About the ActiveX, you mean I should create the ActiveX where I put in the following code with bold letters and then in the last raw not to include it in the "clear" command ?
About the onCleanup, could you give me a hint about where to put it in the code because I have not used it before? It should be written like that: oC = onCleanup(@ObjectiveFunction); ?
function [ F_obj ] = ObjectiveFunction (x)
iMaxwell = actxserver('Ansoft.ElectronicsDesktop');
project_name = 'srm_optim_1'; %για L=350mm
design_name = 'Maxwell2DDesign1';
project_path = 'D:\path ....\ ... \ ....\';
oDesktop = invoke(iMaxwell,'GetAppDesktop');
oDesktop.RestoreWindow;
oProject = invoke(oDesktop,'OpenProject',[project_path project_name '.aedt']);
oProject = oDesktop.SetActiveProject(project_name);
try
.....
%% Finalize AnsysMaxwell App
oDesktop.CloseProject(project_name);
delete(iMaxwell);
clear oDesign oDesktop oModule oProject ; %with iMaxwell missing from this command
end
in main assign to iMaxwell and right after you do so
cleanme = onCleanup(@()delete(iMaxwell));
Then use https://www.mathworks.com/help/matlab/math/parameterizing-functions.html to define an anonymous function @(x) ObjectiveFunction(x,iMaxwell) and arrange to have that invoked by your pso instead of ObjectiveFunction directly. Now add iMaxwell as a second parameter to ObjectiveFunction
inside ObjectiveFunction you are now getting iMaxwell passed in, so remove the activex call there. Just go ahead and do the invoke() calls.
At the end of ObjectiveFunction do not delete or clear iMaxwell
You do not need to explicitly delete iMaxwell in main() when you are done with it, as long as main() is a function rather than a script. When main terminates due to normal return or due to error somewhere, the local variable cleanme will get automatically deleted. But the onCleanup action in it will get triggered along the way, and that anonymous function will delete(iMaxwell)
It is slightly convoluted but really not bad if you are familiar with paramaterizing functions which is a significant technique in MATLAB that you should get accustomed to.
Hello again,
I put the code with the changes you proposed to run but an error occured again. It run for 2 days and then after the 18th iteration was completed and during the 19th, the error of the photo occured.
I also, attach my scripts in case you want to check them out.
The error Matlab shown was:
Error using Interface.4824CFB6_4885_4ECF_B6C8_314CC7C0C148/CloseProject
Error: The RPC server is unavailable.
Error in ObjectiveFunction (line 118)
oDesktop.CloseProject(project_name);
Error in main>@(x)ObjectiveFunction(x,iMaxwell) (line 21)
problem.fobj = @(x) ObjectiveFunction(x,iMaxwell); % παίρνει την τιμή που έχει η Objective function
Error in PSO_myTry_withParameters (line 103)
Run_objF = fobj(currentX);
Error in main (line 32)
[ GBEST , cgcurve , objective_total ] = PSO_myTry_withParameters( noP , maxIter, problem, iMaxwell ) ;
I did say,
"You do not need to explicitly delete iMaxwell in main() when you are done with it, as long as main() is a function rather than a script."
but you used a script insted of a function. You really should put that inside a function for automatic cleanup to work.
This does not explain why the RPC server is failing, but does affect whether the iMaxwell connection is automatically cleaned up when a problem occurs.
I do not have ANSYS.. I also do not use Windows much (except for the occasional game)
Hello Walter,
I solved the problem in a way, but I am looking for an even better one.
To sum up the problem was that RAM gets overloaded and as a result the hole procedure stops.
So I think that it would be ideal if there was a command in matlab that can clean up my RAM. I mean that after every iteration of the for loop I use, my RAM goes up a little. Probably for a reason the computer for every iteration goes in a new memory space and writes, although thespace used before is not needed any more. I believe, that if there is a command that matlab calls Windows to clear the memory, things might be better.
Do you know if there is such a command? I have done a research online but I haven't found anything yet.
Thank you beforehand.
Hello sir,
I need some help regarding PSO optimization in ANSYS Maxwell. Can you help me in coding how to add PSO in optimization process through ansys maxwell? Can you please share your basic step of this coding?
@Georgios Krassakopoulos, Hello sir, Can you explain what is "Data Filter" in objective function matlab coding?
hi, i also used the PSO to optimize by using maxwell and matlab. The reason why the RAM become large every time iteration increasing, is that there is a ansys lincense problem. you can just restart the maxwell. For me, i used below command
Desktop.QuitApplication;
clear iMaxwell
clear Desktop
here, you must clear the iMaxwell and Desktop, otherwise, you cannot use command to open maxwell again.

Iniciar sesión para comentar.

Respuestas (1)

I taken error as follow:
Error using Interface.2F9C9B95_5169_4291_A70B_5F095D3FEFA6/invoke
Check for incorrect argument data type or missing argument in call to function 'SetVariableValue'.
How can I define the variable from matlab to ansys?.

9 comentarios

invoke(oProject,'SetVariableValue','$Do_new', Do_mat);
How can I write this code in ANSYS maxwell 2016.
This code didn’t work in ANSYS maxwell 2016.
Sorry, I do not have any experience with ANSYS
First of all check if your variables are project variables or design variables. If they are design variables instead of oProject in the above command, use oDesign.
Then, be sure that Do_mat is a string, in the above I convert Do value with num2str. Also, the Do_mat should inlcude the units. So, in total I use the above command like the following, where x(1) is a number:
Do = x(1);
Do_str = num2str(Do);
Do_mat = [Do_str, 'mm'];
invoke(oProject,'SetVariableValue','$Do_new', Do_mat);
Error using Interface.4E4ADE7E_0FB6_430F_9D1B_1A66C6A69953/GetPropertyValue
Error: Object returned error code: 0x80070202
Can you explain what is the meaning of this error during interfacing of matlab and ansys.
error code: 0x80070202 seems to be a custom error code, not a standardized code.
I find it used in a small number of different products; software seems to use it to mean that something was not supported (such as a request to interface an unsupported laser scanner.) I do not know if that is relevent.
I do not know.
In http://www.edatop.com/hfss/32309.html it appears that for that software the cause is a resource being used by a different program.

Iniciar sesión para comentar.

Categorías

Más información sobre Introduction to Installation and Licensing en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Preguntada:

el 26 de Feb. de 2022

Comentada:

el 29 de Jul. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by