|
|
发表于 2009-10-15 12:59:18
|
显示全部楼层
网上一个例子(在指定单元坐标加入源项的UDF)
- /***************** UDF ***********************/
- /*
- * FLUENT User Defined Function
- *
- * version 4.0
- *
- *
- * Purpose
- * -------
- * Marks cells that contain coordinates in the a specified input file.
- *
- * Author
- * ------
- * William Wangard, Ph.D.
- * Fluent, Inc.
- * Feb 2003
- *
- * Note: Fluent, Inc. does not guarantee the rigor of this User-defined function
- *
- *
- * Usage
- * -----
- *
- * Data input format
- * -----------------
- * The format for the UDF input file (interp-input.dat) is the following:
- *
- * x0, y0, z0
- * x1, y1, z1
- * ...
- *
- *
- * Note, if you are using 2D, then you only enter the x- and y-coordinates.
- *
- *
- * Execution
- * ---------
- * There are 2 functions in this packages: mark, source.
- *
- *
- * Define on Demand function "mark" is used to read the data points from the inpute file
- * and locate the points in the domain. Upon finding each point, a UDM is set
- * to positive value in that cell.
- *
- * In FLUENT, you will separate the cell thread by UDM value. Then, you apply
- * the Mass source to the source-contatining cell thread.
- *
- *
- */
- #include "udf.h"
- #include "surf.h"
- #define MAXPOINTS 20000
- #define UDM_SOURCE 0
- boolean check(void);
- static int np = 0;
- real coordinates[MAXPOINTS][ND_ND] = {{0.}};
- struct interpolation_point{
- cell_t c;
- Thread* t;
- };
- struct interpolation_point point_list[MAXPOINTS];
- boolean check(void)
- {
- boolean ret = TRUE;
- if (sg_udm < 1)
- {
- ret = FALSE;
- Message("Error: You must define at least 1 UDM\n");
- }
- if (!Data_Valid_P())
- {
- ret = FALSE;
- Message("Error: You must initialize the data field\n");
- }
- return ret;
- }
- DEFINE_ON_DEMAND(mark_reset)
- {
- Domain*d = Get_Domain(1);
- Thread*t;
- cell_t c;
- if (!check()) return;
- thread_loop_c(t,d)
- {
- begin_c_loop(c,t)
- {
- C_UDMI(c,t,UDM_SOURCE) = 0.0;
- }
- end_c_loop(c,t);
- }
- }
- DEFINE_ON_DEMAND(mark)
- {
- #if !RP_HOST
- Domain *d = Get_Domain(1);
- cell_t c;
- CX_Cell_Id cx_cell;
- Thread* t;
- int points_found = 0, total_points_found=0;
- #endif
- #if !RP_NODE
- FILE* input;
- #endif
- int n;
- /* Check for UDM */
- if (!check()) return;
- /* READ COORDINATES */
- #if !RP_NODE
- /* Open input file */
- if (!(input = fopen("interp-input.dat","r")))
- {
- Message0("\nWarning: Could not open interpolation input file...\n");
- return;
- }
- /* Initialize */
- for(n=0; n<MAXPOINTS; n++)
- {
- point_list[n].c = 0;
- point_list[n].t = NULL;
- }
- /* Read points from input file */
- n = -1;
- while (!feof(input))
- {
- n++;
- #if RP_DOUBLE
- #if RP_3D
- fscanf(input,"%lg %lg %lg", &coordinates[n][0], &coordinates[n][1], &coordinates[n][2])
- ;
- #else
- fscanf(input,"%lg %lg", &coordinates[n][0], &coordinates[n][1]);
- #endif
- #else
- #if RP_3D
- fscanf(input,"%g %g %g", &coordinates[n][0], &coordinates[n][1], &coordinates[n][2])
- ;
- #else
- fscanf(input,"%g %g", &coordinates[n][0], &coordinates[n][1]);
- #endif
- #endif
- }
- np = n;
- /* Check np with MAXPOINTS */
- if (np > MAXPOINTS)
- {
- Message0("ERROR: You must recompile interpolate UDF with MAXPOINTS at least %i\n", np)
- ;
- return;
- }
- /* Close input file */
- fclose(input);
- #endif
- /* FIND COORDINATES IN CELLS */
- /* Pass coordinates and np to the compute nodes */
- host_to_node_real(&coordinates[0][0],ND_ND*MAXPOINTS);
- host_to_node_int_1(np);
- #if !RP_HOST
- /* Do computations */
- for(n=0;n<np;n++)
- {
- thread_loop_c(t,d)
- {
- begin_c_loop_int(c,t)
- {
- if (SV_is_point_in_cell(&cx_cell, c, t, coordinates[n]))
- {
- point_list[n].c = RP_CELL(&cx_cell);
- point_list[n].t = RP_THREAD(&cx_cell);
- /* SET UDM HERE */
- C_UDMI(point_list[n].c, point_list[n].t, UDM_SOURCE) = 1.0;
- points_found++;
- goto label;
- }
- }
- end_c_loop_int(c,t);
- }
- label: continue;
- }
- total_points_found += points_found;
- #if PARALLEL
- total_points_found = PRF_GISUM1(total_points_found);
- #endif
- /* PRINT MESSAGE */
- if (np != total_points_found)
- Message0("\n\n Warning.... %i points found in domain out of %i points in input file\n",
- total_points_found, np);
- else
- Message0("\n\n Interpolate... all %i points located!\n", np);
- #endif
- }
- DEFINE_SOURCE(mass_source,c,t,dS,eqn)
- {
- real source = 0.0;
- real mdot = 1.0;
- source = (C_UDMI(c,t,UDM_SOURCE) == 1.0) ? (mdot/C_VOLUME(c,t)) : 0.0;
- dS[eqn] = 0.0;
- return source;
- }
- /************************ END OF UDF *****************************/
复制代码 |
|