//Logo Image

建立程式化之ANSYS輸入檔

1. ANSYS輸入檔

1. 形態最佳化設計範例之幾何形狀與邊界條件

1. ANSYS輸入檔

 !INITIAL DEFINITION         /TITLE,2D SHAPE OPTIMIZATION EXAMPLE         /PREP7         ANTYPE,STATIC           ET,1,PLANE82         KEYOPT,1,3,3         R,1,1         MP,EX,1,210000         MP,NUXY,1,0.3         MP,DENS,1,1   !INPUT KEYPOINTS         K,1,1560.000000,320.000000,0.000000         K,2,1540.000000,300.000000,0.000000         K,3,1500.000000,300.000000,0.000000         K,4,1439.992000,279.996700,0.000000         K,5,1160.041000,280.005000,0.000000         K,6,920.000500,279.997500,0.000000         K,7,699.989700,280.015100,0.000000         K,8,600.006800,300.005700,0.000000         K,9,460.003500,299.998500,0.000000         K,10,400.000400,319.999800,0.000000         K,11,360.000000,360.000000,0.000000         K,12,0.000000,0.000000,0.000000         K,13,360.000000,720.000000,0.000000         K,14,360.000000,0.000000,0.000000         K,15,0.000000,720.000000,0.000000         K,16,1560.000000,0.000000,0.000000         K,17,1920.000000,0.000000,0.000000         K,18,1920.000000,360.000000,0.000000         K,19,1560.000000,360.000000,0.000000   !GENERATING LINES         FLST,2,11,3         FITEM,2,1           FITEM,2,2           FITEM,2,3           FITEM,2,4           FITEM,2,5           FITEM,2,6           FITEM,2,7           FITEM,2,8           FITEM,2,9           FITEM,2,10          FITEM,2,11          SPLINE,P51X         LSTR,      11,      13          LSTR,      13,      15          LSTR,      15,      12          LSTR,      12,      14          LSTR,      14,      16          LSTR,      16,      17          LSTR,      17,      18          LSTR,      18,      19          LSTR,      19,       1    !GENERATING AREA         AL,ALL   !MESH         ESIZE,15         AMESH,ALL   !APPLY BOUNDARY CONDITIONS         NSEL,S,LOC,X,0         F,ALL,FX,-5000/97         NSEL,ALL         NSEL,S,LOC,X,1920,1920         F,ALL,FX,5000/49         NSEL,ALL         NSEL,S,LOC,Y,0         D,ALL,UY         NSEL,ALL         /OUTPUT,AREA,OUT,D:\research\shape_2d\Example_01\         ASUM         /OUTPUT,TERM         FINISH   !SOLVE         /SOLU         EQSLV,PCG         SOLVE         FINISH   !POSTPROCESS         /POST1         /OUTPUT,STRESS,OUT,D:\research\shape_2d\Example_01\         NSEL,S,F,F,-100000,100000         PRNSOL,U         NSEL,ALL         PRNSOL,S,PRIN         /OUTPUT,TERM         FINISH         /CLEAR,NOSTART         /EXIT

2 設計變數的處理

(1)

(2)

3. 將ANSYS輸入檔程式化所需指令介紹

function指定程式為函式型程式

ANSYS輸入檔程式指定為函式(function)型程式，我們即可將數字資料矩陣輕易的輸入程式中，因為函式型程式可以接受輸入變數，並且也可以將計算結果輸出。指定程式為函式型程式的方法是在程式起始處使用“function”指令，如表2所示即為一形態最佳化設計程式之範例，function指令後跟著為該程式的檔案名稱，此處之檔案名稱為Example_inputfile，接著給予函式變數名稱，此處共有兩個變數輸入，分別為產生之檔案路徑(work_path)，以及控制點法線方向移動量(delta_input)，輸入函式之變數只須要是矩陣形式即可。控制點法線方向移動量輸入程式後，即可依式(1)與式(2)計算出新的控制點位置。

2. 指定程式為函式型程式

 function Example_inputfile(work_path,delta_input)

fopen開啟檔案指令

fopen指令可用於開啟已存在之檔案，或是建立一新檔案，表3所示為一使用範例，fem_inp為一指標變數，用以表示該開啟檔案，指標變數名稱可以字以給定。Fopen中包含兩個輸入變數，首先必須給予要開啟檔案的路徑、檔名與副檔名，如表3中所示表示將開啟D:\shape_2d\fem_inp.inp檔案。接著給予工作模式，亦即指定是要讀取檔案(r)，或是撰寫檔案(w)，此處是要產生一ANSYS輸入檔，因此選擇使用“w”，又由於所要寫的檔案為文字檔，因此加一“t”

3. fopen指令使用範例

 fem_inp=fopen('D:\shape_2d\fem_inp.inp','wt');

fprintf將指定的文字、數字印於檔案上

K,1,10.000000,10.000000

4. fprintf指令使用範例

 fprintf(fem_inp,'%s\n','!INITIAL DEFINITION');     fprintf(fem_inp,'%s\n','/TITLE, 2D SHAPE OPTIMIZATION EXAMPLE '); fprintf(fem_inp,'%s\n','      /PREP7');   fprintf(fem_inp,'%s%.0f%s%f%s%f\n','K,',keypoint_matrix(kpoint_i,1),',',x_coor,',',y_coor);

fclose關閉檔案指令

5. fclose指令使用範例

 fclose(fem_inp) fclose('all')

4. 程式範例

6所示為由表1中之ANSYS輸入檔所改寫而來的副程式，此副程式之輸入變數有work_path，表示工作路徑，所產生之ANSYS輸入檔，以及ANSYS分析後的輸出檔都將存放於此路徑下。另一輸入變數為delta_input，此變數即為各個控制點於法線方線的移動量。若是令work_path='D:\research\shape_2d\Example_01\'，而delta_input=[0,0,0,0,0,0,0,0,0,0,0]，則所得之ANSYS輸入檔就是表1中所示的輸入檔。

6. 程式範例

 function Example_inputfile(work_path,delta_input) % %     Shape optimization example % % %                     Ming-Hsiu Hsu %                     Aug.,12,2002           if work_path(length(work_path))~='\'                 work_path=[work_path,'\'];         end;             keypoint_matrix=[                         1,1560.000,320.0000,0.000000                         2,1540.000,300.0000,0.000000                          3,1500.000,300.0000,0.000000                         4,1439.992,279.9967,0.000000                         5,1160.041,280.0050,0.000000                         6,920.0005,279.9975,0.000000                          7,699.9897,280.0151,0.000000                         8,600.0068,300.0057,0.000000                         9,460.0035,299.9985,0.000000                         10,400.0004,319.9998,0.000000                         11,360.0000,360.0000,0.000000                         12,0.000000,0.000000,0.000000                         13,360.0000,720.0000,0.000000                         14,360.0000,0.000000,0.000000                         15,0.000000,720.0000,0.000000                         16,1560.000,0.000000,0.000000                         17,1920.000,0.000000,0.000000                         18,1920.000,360.0000,0.000000                         19,1560.000,360.0000,0.000000                         ];           angle_matrix=[                         1,180                         2,67.5                         3,80.7823                         4,80.7831                         5,90                         6,90                         7,84.3444                         8,84.3482                         9,80.7839                         10,58.2825                         11,0                         ];               eval(['fem_inp=fopen(''',work_path,'fem_inp.inp'',''wt'');']);             fprintf(fem_inp,'%s\n','!INITIAL DEFINITION');             fprintf(fem_inp,'%s\n','/TITLE, 2D SHAPE OPTIMIZATION EXAMPLE ');         fprintf(fem_inp,'%s\n','      /PREP7');         fprintf(fem_inp,'%s\n','      ANTYPE,STATIC');         fprintf(fem_inp,'\n');         fprintf(fem_inp,'%s\n','      ET,1,PLANE82');         fprintf(fem_inp,'%s\n','      KEYOPT,1,3,3');         fprintf(fem_inp,'%s\n','      R,1,1');         fprintf(fem_inp,'%s\n','      MP,EX,1,210000');         fprintf(fem_inp,'%s\n','      MP,NUXY,1,0.3');         fprintf(fem_inp,'%s\n','      MP,DENS,1,1');         fprintf(fem_inp,'\n');           fprintf(fem_inp,'%s\n','!INPUT KEYPOINT');                 [kpoint_n,kpoint_m]=size(keypoint_matrix);         [angle_n,angle_m]=size(angle_matrix)           for kpoint_i=1:kpoint_n                 if kpoint_i<=angle_n                 x_coor=keypoint_matrix(kpoint_i,2)                         +delta_input(kpoint_i)*cos(angle_matrix(kpoint_i,2)*pi/180);                 y_coor=keypoint_matrix(kpoint_i,3)                         +delta_input(kpoint_i)*sin(angle_matrix(kpoint_i,2)*pi/180);                 z_coor=keypoint_matrix(kpoint_i,4);                 else                 x_coor=keypoint_matrix(kpoint_i,2);                 y_coor=keypoint_matrix(kpoint_i,3);                 z_coor=keypoint_matrix(kpoint_i,4);                                        end;                   fprintf(fem_inp,'%s%.0f%s%f%s%f%s%f\n','    K,', …                         keypoint_matrix(kpoint_i,1),',',x_coor,',',y_coor,',',z_coor);         end;           fprintf(fem_inp,'\n%s\n','!CREATING LINES');         fprintf(fem_inp,'%s\n','      FLST,2,11,3 ');         fprintf(fem_inp,'%s\n','      FITEM,2,1   ');         fprintf(fem_inp,'%s\n','      FITEM,2,2   ');         fprintf(fem_inp,'%s\n','      FITEM,2,3   ');         fprintf(fem_inp,'%s\n','      FITEM,2,4   ');         fprintf(fem_inp,'%s\n','      FITEM,2,5   ');         fprintf(fem_inp,'%s\n','      FITEM,2,6   ');         fprintf(fem_inp,'%s\n','      FITEM,2,7   ');         fprintf(fem_inp,'%s\n','      FITEM,2,8   ');         fprintf(fem_inp,'%s\n','      FITEM,2,9   ');         fprintf(fem_inp,'%s\n','      FITEM,2,10  ');         fprintf(fem_inp,'%s\n','      FITEM,2,11  ');         fprintf(fem_inp,'%s\n','      SPLINE,P51X ');         fprintf(fem_inp,'%s\n','      LSTR,      11,      13  ');         fprintf(fem_inp,'%s\n','      LSTR,      13,      15  ');         fprintf(fem_inp,'%s\n','      LSTR,      15,      12  ');         fprintf(fem_inp,'%s\n','      LSTR,      12,      14  ');         fprintf(fem_inp,'%s\n','      LSTR,      14,      16  ');         fprintf(fem_inp,'%s\n','      LSTR,      16,      17  ');         fprintf(fem_inp,'%s\n','      LSTR,      17,      18  ');         fprintf(fem_inp,'%s\n','      LSTR,      18,      19  ');         fprintf(fem_inp,'%s\n','      LSTR,      19,       1  ');           fprintf(fem_inp,'\n%s\n','!CREATING AREA');         fprintf(fem_inp,'%s\n','      AL,ALL');           fprintf(fem_inp,'\n%s\n','!MESH');         fprintf(fem_inp,'%s\n','      ESIZE,15');         fprintf(fem_inp,'%s\n','      AMESH,ALL');           fprintf(fem_inp,'\n%s\n','!APPLY BOUNDARY CONDITIONS');         fprintf(fem_inp,'%s\n','      NSEL,S,LOC,X,0');         fprintf(fem_inp,'%s\n','      F,ALL,FX,-5000/97');         fprintf(fem_inp,'%s\n','      NSEL,ALL');         fprintf(fem_inp,'%s\n','      NSEL,S,LOC,X,1920,1920');         fprintf(fem_inp,'%s\n','      F,ALL,FX,5000/49');         fprintf(fem_inp,'%s\n','      NSEL,ALL');         fprintf(fem_inp,'%s\n','      NSEL,S,LOC,Y,0');         fprintf(fem_inp,'%s\n','      D,ALL,UY');         fprintf(fem_inp,'%s\n','      NSEL,ALL');         eval(['fprintf(fem_inp,''%s\n'',''/OUTPUT,AREA,OUT,',work_path,''');']);         fprintf(fem_inp,'%s\n','      ASUM');         fprintf(fem_inp,'%s\n','      /OUTPUT,TERM');         fprintf(fem_inp,'%s\n','      FINISH');           fprintf(fem_inp,'\n%s\n','!SOLVE');         fprintf(fem_inp,'%s\n','      /SOLU');         fprintf(fem_inp,'%s\n','      EQSLV,PCG');         fprintf(fem_inp,'%s\n','      SOLVE');         fprintf(fem_inp,'%s\n','      FINISH');           fprintf(fem_inp,'\n%s\n','!POSTPROCESS');         fprintf(fem_inp,'%s\n','      /POST1');         eval(['fprintf(fem_inp,''%s\n'',''/OUTPUT,STRESS,OUT,',work_path,''');']);         fprintf(fem_inp,'%s\n','      NSEL,S,F,F,-100000,100000');         fprintf(fem_inp,'%s\n','      PRNSOL,U');         fprintf(fem_inp,'%s\n','      NSEL,ALL');         fprintf(fem_inp,'%s\n','      PRNSOL,S,PRIN');         fprintf(fem_inp,'%s\n','      /OUTPUT,TERM');          fprintf(fem_inp,'%s\n','      FINISH');         fprintf(fem_inp,'%s\n','      /CLEAR,NOSTART');         fprintf(fem_inp,'%s\n','      /EXIT');           fclose('all');