CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   CFX (https://www.cfd-online.com/Forums/cfx/)
-   -   Integrating/Interfacing cfx with Matlab (https://www.cfd-online.com/Forums/cfx/86830-integrating-interfacing-cfx-matlab.html)

Darren Leong April 3, 2011 20:27

Integrating/Interfacing cfx with Matlab
 
Hi guys,

I need help from you all on whether using Matlab to control ANSYS CFX simulation is possible. The immediate goal of my work is to setup MATLAB to write the input files for CFX and then read the output data when the simulation has finished. The work then progresses into using MATLAB to initiate the simulations. I have looked through the forum, ansys help&tutorials, & google with no clear findings on how this is achieved, with Fluent being the exception.

Highly appreciate if someone can shed more light on this matter and i'll ask more questions as we go. If it's vaguely possible, I'm happy to work on it and share my work with the forum community. I am familiar with Ansys CFX.

To give a bit more detail, the simulation involves moving 2 bodies in respect to one another.

Warmest Regards,
Darren

ghorrocks April 4, 2011 20:16

CFX can be run from the command line, so can be put in as part of a script. So if matlab can call a command line then this should be possible.

Darren Leong April 4, 2011 23:20

Thx, Glenn.

Ran some batch files from cmd and that worked, but i've yet to try it on MATLAB. Haven't used MATLAB to run external programs before so I thought to post a question here 1st for reference as the work progresses.

I'm trying to get 3D mesh deformation & remeshing to work in CFX at the moment. I'll start another post on that topic when I have some results or questions since it's still a beta feature in v13.

Best wishes,
Darren

Darren Leong April 27, 2011 23:02

Hi again,

I've got a batch of remesh simulations running in CFX through Matlab. I've only been able to use the 'edit' function in Matlab to modify the input (.CCL) files directly so far though.

I'm struggling to figure out how to get Matlab to plot the results; forces and moments from transient runs. If anyone's done it before, i'll be very grateful if some guidance can be given.

Best wishes,
Darren

ghorrocks April 28, 2011 19:27

CFX V13 has built in mesh deformation and remeshing stuff. Should be no need to couple with matlab. Have you looked at that?

CFX can output forces, moments and lots of other stuff easily by outputting it to a monitor point and then you can either manually export it from the solver manager or automatically parse it from the mon file.

Darren Leong May 1, 2011 21:52

I'm using Matlab only to execute CFX-solver as an external program, e.g. changing the input variable once the setup (remeshing config and .cfx) is stable and reading the results while being handoff as possible from ANSYS interface. The only reason i'm using CFX is that it's the most familiar package to me. Alternative solvers considered are Fluent or Openfoam once the capabilities have been developed.

It would nice if I could preset to collate/process them in cfx-pre without going into solver manager and exporting it manually from there. I was hoping there might be a suffix command i can used in the batch file for this.

I'm not sure of and can't locate the 'mon' file, do i need to setup for it to be created at the end of solution?

The monitor plot values for transient runs are stored as individual files (.trn) for each timestep. That's about 500+ files to read just get the values out for Matlab to plot. I've not parse that many files before in Matlab. I'll work on this if there's no shortcut to it.

ghorrocks May 2, 2011 08:44

The mon file is a temporary file which is generated in the CFX temporary folder when the simulation is running. It is deleted when the simulation completes. You can also use a cfx command - I forget which one, cfx5monitor or something like that - to extract monitor point info.

Darren Leong May 2, 2011 10:25

That's a relief to know ^_^!

Will post the findings when I get everything to work. Thanks, Glenn!!

ghorrocks May 2, 2011 20:13

Found the command - cfx5mondata. Should do the trick for you.

Darren Leong May 26, 2011 01:35

Thanks, Glenn =)

The following is what i used and seems to be working well.

!"C:\Program Files\ANSYS Inc\v130\CFX\bin\cfx5mondata" Spheresim_001.res -varrule "CATEGORY = USER POINT" -output Analysis_Results_001.dat

lisa July 26, 2011 12:33

could u help
 
I am new to CFX and Fortran, I am trying to read a Matlab file in a text format in CFX carry out simulation and pass the result to Matlab and do the calculation and read back it.

Is it possible in CFX. Kindly let me know.

Thanks
with regards
sesan

ghorrocks July 26, 2011 19:50

Yes, this is possible. You can either code it in fortran yourself or use MPCCI to handle the coupling. I think CFX still has MPCCI handles in it. Up to you.

lisa July 27, 2011 05:39

Hi Glenn,

Thanks for your reply. I am also new in Fortran programming and MPCCI, Could you kindly tell me on how exactly i do or give me some tips.

Thanks

ghorrocks July 27, 2011 07:01

I think some of the CFX examples use fortran. Otherwise contact CFX support for examples. MPCCI is a separate piece of commercial software designed to interface between simulation codes. Google it.

Darren Leong September 10, 2011 05:46

Hi Sessan,

Follow up to your PM, I thought i post the m files here:
http://www.2shared.com/file/3A-LkWtc/CFXMatlab.html

They're pretty basic at the moment as I haven't looked much into it since my last post. However, I'll be working on it in the coming months and it would be great if we can as much help as possible in getting this working =)

lisa September 22, 2011 07:53

hi
 
Hi Darren

Thanks for the reply and sorry i culdnt get back earlier.

The link in which u had sent the files is no longer valid, culd you kindly send the file again. In between could you also share your mail id for contact .

Thanks in adavance for the help.
Wih regards
Sesan.

Darren Leong October 4, 2011 02:45

http://www.2shared.com/file/z2WErPbu/CFXMatlab.html

lisa October 4, 2011 06:13

Thanks for sharing the files.

Darren Leong February 14, 2012 02:04

For Lisa and others,

From where the previous script left off, we have the following example

______________________________________________
!"C:\Program Files\ANSYS Inc\v130\CFX\bin\cfx5solve" -def Simulation.def -ccl Simulation.ccl

copyfile ('*.res','results\'); %Backup current result

% To maintain the filename format for looping; easiest way is to remove the
% suffix '_00#' from current result file and so on
delete ('Simulation.res'); %remove previous run result
!ren *.res Simulation.res

% Extract data from result file to csv format
!"C:\Program Files\ANSYS Inc\v130\CFX\bin\cfx5mondata" Simulation.res -varrule "CATEGORY = USER POINT" -output CFXResult.dat


% Reads the most recent CFX timestep results
ResultData = csvread ('CFXResult.dat',1,0);
[m,n]=size(ResultData);
ResultData = csvread ('CFXResult.dat',m,0);

% You can now assign names to the variables to be computed by your MATLAB code
Accumulated_Time =ResultData(1,1);
Force_X = ResultData(1,2);

% compute the new states
<Your code> % For example, it computes
Inlet_Speed = 5 % in m/s


% Opens the .ccl file and format it into cell arrays
A = fopen('Simulation.ccl','rt+');
B = textscan(A,'%s %s %s %s %s %s %s');
% number of '%s' depends on the number of space delimited columns

%combines the arrays into one matrix of cell arrays
C = horzcat(B{1,1},B{1,2},B{1,3},B{1,4},B{1,5},B{1,6}, B{1,7});

% you can now replace the variables of the .ccl file easily since their
% position should always be the same in the matrix

C{6,3} = num2str(Inlet_Speed);
% assuming 6,3 is the position of the variable.
%'num2str' is needed or else it'll introduce '[]' into your text file

%To reconstruct your .ccl text file with the new variables
[nrows,ncols]= size(C);
filename='SimulationNew.ccl';
fid=fopen(filename,'W');
for row=1:nrows;
fprintf(fid,'%s %s %s %s %s %s %s\r\n',C{row,:});
end

delete ('Simulation.ccl');
!ren SimulationNew.ccl Simulation.ccl
fclose all;

% You can now continue your simulation from the previous result
% with the new input variable
!"C:\Program Files\ANSYS Inc\v130\CFX\bin\cfx5solve" -def Simulation.def -ccl Simulation.ccl -cont-from-file Simulation.res
echo off

_________________________________________

Hope it works for you all.

Best wishes,
Leong

alikhube December 22, 2012 12:39

Hi
I'm working on optimization problem
pleas help me how to call cfx from matlab for solving and post processing?
Is this problem have specific setting at CFX and matlab code?
Pleas help me
Thank Everyone

Darren Leong January 6, 2013 23:25

Hi Ali,

Just replied your email but good to see you did post the question on forum. The example script is pretty much what you're asking for, you just need to work thru it.

Otherwise, as mentioned in the email, you can just use the system command terminal:

Have a look at the subsections in the link below on how to use cfx through your system command terminal:
https://www.sharcnet.ca/Software/Flu.../i1304872.html

If you're familiar with .bat, write the command for execution for each mesh row by row using a text editor, save it as *.bat, run it and you're done.

As for post processing of quantitative data, you can use cfxmonitor.exe to extract the outputs of *.res in a .csv format to be read by excel/matlab. If you want visualisation, the only way is using cfx-post.

_

On the hind side, try looking into the design optimisation feature in ANSYS Workbench. It works well for structural but i'm not sure if it's applicable for CFX yet.

ghorrocks January 7, 2013 17:08

ANSYS Workbench optimsation works fine for CFX.

Arash67.m September 1, 2015 03:03

call CFX from Matlab
 
Hi everyone,
I have set up a CFD problem in CFX (workbench 16) and it works with no problem and accurate results. Now I want to set input velocity and output pressure to specific values from MATLAB and then start solving with cfx5solve. I can categorize my issue in two following questions:

1- How is it possible to set specific values for velocity and pressure from MATLAB, is it possible to set them in ccl or def files (editing ccl and def) and then call cfx5solve to run the analysis.

I assume that a command like this should work:

set-> variable name->variable value
but I can not find the corresponding command in MATLAB

2- How should I define run in MATLAB? I tried the following code in command window in MATLAB:

%%%%%%%%%%%%
!"C:\Program Files\Ansys Inc\V160\CFX\bin\cfx5solve" -def FFCFX0203.def -ccl relaibilityCCL.ccl
%%%%%%%%%%%%

but I receive the following error:

An error has occurred in cfx5solve:

Command Language file relaibilityCCL.ccl does not exist

I would be truly thankful if you could help me to manage this issue.

-Maxim- September 1, 2015 03:21

Quote:

Originally Posted by Arash67.m (Post 561873)
Command Language file relaibilityCCL.ccl does not exist

Well, isn't that error message pretty clear? Maybe because of a typo? It's spelled "reliability" to my English knowledge ;)

Arash67.m September 1, 2015 03:52

Quote:

Originally Posted by -Maxim- (Post 561879)
Well, isn't that error message pretty clear? Maybe because of a typo? It's spelled "reliability" to my English knowledge ;)

Thanks for your attention Maxim. But it is a typing mistake in both file name and command :-) so it is not the problem as I checked. :confused:
Is there any special directory for def and ccl files I have left both of them on desktop!

-Maxim- September 1, 2015 03:54

okay, but if the message is still: 'does not exist', your script cannot find the file in the folder it is looking for the file. My guess would be that the ccl file isn't in the right folder? Is it in the same directory as your def file?

Lance September 1, 2015 03:55

My guess is that the working directory is not set. CFX looks for your .ccl file somewhere else.

Arash67.m September 1, 2015 04:03

Quote:

Originally Posted by -Maxim- (Post 561887)
okay, but if the message is still: 'does not exist', your script cannot find the file in the folder it is looking for the file. My guess would be that the ccl file isn't in the right folder? Is it in the same directory as your def file?

Yes, both of them are in desktop directory.
yhe question is when I type the following command in matlab, I do not know in wich directory Matlab looks up the def and ccl files! ? I think a special directory should be specified. Am I right?

Arash67.m September 1, 2015 04:06

Quote:

Originally Posted by Lance (Post 561888)
My guess is that the working directory is not set. CFX looks for your .ccl file somewhere else.

Thanks for your help Lance.
But I think MATLAB has got a big problem in finding def and ccl files because when I wright the following command it fails to find deffile. the same error hapens for both ccl and def!

%%%%%%%%%%%%%%%
!"C:\Program Files\Ansys Inc\V160\CFX\bin\cfx5solve" -def FFCFX0203.def
%%%%%%%%%%%%%%

error:

An error has occurred in cfx5solve:

ANSYS CFX Solver Input File C:/Users/NP/Documents/MATLAB/FFCFX0203.def does
not exist

-Maxim- September 1, 2015 05:09

then either put your def and ccl file in C:/Users/NP/Documents/MATLAB/

or change your command to
-def C:/Users/NP/Desktop/FFCFX0203.def

this is standard batch/console/programming syntax.
Or look up what a 'working directory' is and how it is set.
I don't want to be rude but I think you should go back a step and teach yourself some basics first...

Arash67.m September 1, 2015 06:24

Quote:

Originally Posted by -Maxim- (Post 561898)
then either put your def and ccl file in C:/Users/NP/Documents/MATLAB/

or change your command to
-def C:/Users/NP/Desktop/FFCFX0203.def

this is standard batch/console/programming syntax.
Or look up what a 'working directory' is and how it is set.
I don't want to be rude but I think you should go back a step and teach yourself some basics first...

Thanks a Million Maxim,
It worked:
!"C:\Program Files\Ansys Inc\V160\CFX\bin\cfx5solve" -def C:/Users/NP/Desktop/FFCFX0203.def

Anyway, you are right. I am not familiar with programming.

Arash67.m September 1, 2015 14:03

Quote:

Originally Posted by Arash67.m (Post 561873)
Hi everyone,
I have set up a CFD problem in CFX (workbench 16) and it works with no problem and accurate results. Now I want to set input velocity and output pressure to specific values from MATLAB and then start solving with cfx5solve. I can categorize my issue in two following questions:

1- How is it possible to set specific values for velocity and pressure from MATLAB, is it possible to set them in ccl or def files (editing ccl and def) and then call cfx5solve to run the analysis.

I assume that a command like this should work:

set-> variable name->variable value
but I can not find the corresponding command in MATLAB

2- How should I define run in MATLAB? I tried the following code in command window in MATLAB:

%%%%%%%%%%%%
!"C:\Program Files\Ansys Inc\V160\CFX\bin\cfx5solve" -def FFCFX0203.def -ccl relaibilityCCL.ccl
%%%%%%%%%%%%

but I receive the following error:

An error has occurred in cfx5solve:

Command Language file relaibilityCCL.ccl does not exist

I would be truly thankful if you could help me to manage this issue.

taking the advantage of your guidance I fixed the second issue I mentioned above, but my first question remains a disaster!

I need to edit CFX definition file (.def) in MATLAB and set some boundary conditions to new values and restart the analysis for updated boundary conditions.

I think it is a common issue in optimization problems but I can not find related topics in forum. I would be truly thankful if you could help me to fix this issue.

-Maxim- September 2, 2015 03:15

Darren Leong posted some Matlab code a page back. I would go from there and try to implement your ideas in the same syntax.
I don't have Matlab installed right now, so I cannot test things.
But what about you post your Matlab code that you have written so far?

Lance September 2, 2015 03:40

Write a new .ccl in Matlab and use the -ccl option? You can get the syntax for the .ccl file by right clicking on the boundary condition in cfx-pre and 'edit in command editor'.

amsys February 9, 2019 08:33

Quote:

Originally Posted by Darren Leong (Post 326574)




This seems perfect for what I am currently trying to achieve. However the link is now invalid, is there anyway you could please re-upload the link to the files?


Thanks!


All times are GMT -4. The time now is 07:07.