Matlab调用ANSYS的三种方法
采用matlab作为主控程序设置好所有的参数与选项传递给ansys通过文件并调用ansys计算。ansys计算结束后默认情况下matlab会自己等着这一点很方便再用matlab处理ansys的输出文件。由于所有的设置可以在matlab中统一完成省去很多参数同步的工作也顺便实现了计算的自动化。核心提示1.如何使用matlab调用ansys下面是个例子*1. “!法* ” !D:Program FilesAnsys Incv100ANSYSbinintelansys100.exe -b -i d:inpvm1.mac -o d:out put1.txt “! ” 是由matlab提供的用以执行shell命令的操作符也可以用system或dos命令** *2. “system法*” system(D:\ANSYS\v80\ANSYS\bin\intel\ansys80 -b -p ane3fl -i C:\sibian.dat -o C:\vm5.out) *3. “dos法”* dos( C:\Program Files\ANSYS Inc\v162\ansys\bin\winx64\ANSYS -b -i C:\New folder\matlabex\ansyscampbell\in.txt -o C:\New folder\matlabex\ansyscampbell\out.txt ); That is, every path should have around it, so that if it happens to have a space in it, dos will not break it up into two arguments.参数 -b指定使用batch方式运行ansys-i 指定输入文件-o指定输出文件。这里有一点值得注意的是这里的输出文件是指在gui方式运行是output windows中的输出内容通常不是我们想要的结果文件。2.我们想要的ansys结果可以通过*vwrite,*mwrite等命令写入文件以供matlab使用。3.想要matlab传递给ansys的参数也通过文件方式传递。1、调用ANSYS!“D:\ANSYS Inc\v180\ANSYS\bin\winx64\ansys180.exe” -b -i “E:\ansys2matlab\work.txt” -o “E:\ansys2matlab\process.out”;以上为在matlab中调用ANSYS内核的代码。“D:\ANSYS Inc\v180\ANSYS\bin\winx64\ansys180.exe” 是ANSYS的绝对路径依据ANSYS的安装位置改写即可-b 是使用batch方式运行ANSYS-i 是输入文件“E:\ansys2matlab\work.txt” 是用ANSYS APDL语言编制的计算模型-o 是输出文件“E:\ansys2matlab\process.out” 是ANSYS计算过程的输出文件两种方式当matlab调用的时候可以采用的方式和system函数两种方式。注意第一个ansys80这个可执行文件目录中不能有空格否则matlab不能识别。即可以采用如下的调用方式system(‘D:\ANSYS\v80\ANSYS\bin\intel\ansys80 -b -p ane3fl -i C:\sibian.dat -o C:\vm5.out’)这部分是关于batch的使用的应尽量避免目录中的空格减小不必要的麻烦。下边的batch模式是正确的。“D:\ANSYS\v80\ANSYS\bin\intel\ansys80” -b -p ane3fl -i C:\sibian.dat -o C:\vm1.out我 的经验是输入文件和输出文件的目录不能太长且最好不带空格。刚刚开始的时候我的输入文件的目录是C:\Documents and Settings\Administrator\sibian.dat结果batch不能执行。改为C:\sibian.dat 即可执行了。遇到的问题MATLAB调用ANSYS停止工作的解决办法根据国外论坛的解答发现是堆栈溢出的问题错误代码c00000fd这是一个系统层级的错误表示堆栈溢出这个错误是Matlab调用第三方软件时触发的因为在执行系统命令时堆栈内存被名为KMP_STACKSIZE的环境变量默认指定为512k不足以调用ANSYS。解决办法人为放大堆栈内存matlab中的调用命令改为system(‘SET KMP_STACKSIZE2048k “C:\path_to_ansys\ansys[version].exe” [input arguments…]’)the problem is related to a particular piece of 3rd party software (OpenMP) used by MATLAB. As of 2014b, when running system commands, the stack size is limited to 512k through an environment variable called KMP_STACKSIZE. It is possible to verify the existence and value of this environment variable by issuing the following command listing all the variables (in Windows):System(‘SET’)(produces a list of all environment variables currently set…)For ANSYS to be able to run, you need to increase the stack size before calling the ANSYS executable. You can do this by wrapping the ANSYS call in a batch script or you can do the following:System(‘SET KMP_STACKSIZE2048k “C:\path_to_ansys\ansys[version].exe” [input arguments…]’)例子%% matlab调用ANSYS进行分析 %% % ansys 版本中的可执行文件,path中有空格要加ansys_pathstrcat(E:\ANSYS16_install\ANSYS Inc\v160\ansys\bin\winx64\ANSYS160.exe);% jobname不需要后缀jobnametianqiao1;% 是命令流文件也就是用ansys写的apdl语言matlab调用时他将以批处理方式运行需要后缀skriptFileNameF:\cuichanghui_here_all\mll\C92_movingload_all_use.txt;% 输出文件所在位置输出文件保存了程序运行的相关信息需要后缀outputFilenameF:\cuichanghui_here_all\cch_test.out;% 最终总的调用字符串,其中32代表空格的字符串ASCII码sys_charstrcat(SET KMP_STACKSIZE2048k ,32,ansys_path,32,...-b -p ane3fl -i,32,skriptFileName,32,...-j,32,jobname,32,...-o,32,outputFilename), % 调用ANSYSans1system(sys_char);SET ANS_CONSECYES !设定连续运行忽略警告SET ANSYS_LOCKOFFSET KMP_STACKSIZE2048k“C:\Program Files\ANSYS Inc\v181\ansys\bin\winx64\ANSYS181” -b -i ansysinputreader.txt -o out.dat