PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/fluent_udf/hylab/src/myudf.c

http://hy-fluent.googlecode.com/
C | 318 lines | 198 code | 54 blank | 66 comment | 6 complexity | fcd3d702ad2fd645a2d4b7f219267e3d MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: myudf.c
  5. *
  6. * Description: Implement for my UDF wrappers
  7. *
  8. * Version: 1.0
  9. * Created: 2010-2-23 12:01:23
  10. * Revision: none
  11. * Compiler: gcc/msc
  12. *
  13. * Author: huys (hys), huys03@gmail.com
  14. * Company: hu
  15. *
  16. * =====================================================================================
  17. */
  18. #include "udf.h"
  19. #include "mydialog.h"
  20. /************************************************************************/
  21. /* EOD functions */
  22. /************************************************************************/
  23. DEFINE_ON_DEMAND(showMsg)
  24. {
  25. /*Message("message\n");*/
  26. showMyMessage("Hello!");
  27. }
  28. DEFINE_ON_DEMAND(hy_show_info_dialog)
  29. {
  30. showInfoDialog();
  31. }
  32. DEFINE_ON_DEMAND(myAdd)
  33. {
  34. Message("1+1");
  35. }
  36. DEFINE_ON_DEMAND(runSchemeProc)
  37. {
  38. CX_Interpret_String("(%udf-on-demand \"showMsg::libhylab\")");
  39. }
  40. /************************************************************************/
  41. /* 2009.12.26: Sigh!!!! I do not know how to use it! */
  42. /* 2009.12.26: Aha! Use (%run-udf-apply mode)! */
  43. /************************************************************************/
  44. DEFINE_EXECUTE_FROM_GUI(sayHello, libhylab, mode)
  45. {
  46. if(0 == mode)
  47. {
  48. showMyMessage("Hello!");
  49. }
  50. Message("Hello From the C Function\n");
  51. }
  52. /************************************************************************/
  53. /* UDF for integrating turbulent dissipation */
  54. /* and printing it to console window */
  55. /************************************************************************/
  56. DEFINE_ADJUST(my_adjust, d)
  57. {
  58. Thread *t;
  59. /* Integrate dissipation. */
  60. real sum_diss=0.;
  61. cell_t c;
  62. thread_loop_c(t,d)
  63. {
  64. begin_c_loop(c,t)
  65. {
  66. sum_diss += C_D(c,t) * C_VOLUME(c,t);
  67. }
  68. end_c_loop(c,t)
  69. }
  70. printf("Volume integral of turbulent dissipation: %g\n", sum_diss);
  71. }
  72. /***********************************************************************
  73. UDFs that increment a variable, write it to a data file
  74. and read it back in
  75. ************************************************************************/
  76. int kount = 0; /* define global variable kount */
  77. DEFINE_ADJUST(demo_calc, d)
  78. {
  79. kount++;
  80. printf("kount = %d\n",kount);
  81. }
  82. DEFINE_RW_FILE(writer,fp)
  83. {
  84. printf("Writing UDF data to data file...\n");
  85. fprintf(fp, "%d", kount); /* write out kount to data file */
  86. }
  87. DEFINE_RW_FILE(reader,fp)
  88. {
  89. printf("Reading UDF data from data file...\n");
  90. fscanf(fp, "%d", &kount); /* read kount from data file */
  91. }
  92. /***********************************************************************
  93. UDFs that operating the grid
  94. ************************************************************************/
  95. int * g_sgrid = 0;
  96. int g_ssize = 0;
  97. DEFINE_ON_DEMAND(INIT)
  98. {
  99. int count;
  100. int thread_ID;
  101. Thread *t;
  102. int i;
  103. face_t f; /* index of face of wall */
  104. cell_t c0; /* index of cell which is adjacent to wall. */
  105. Domain *domain;
  106. thread_ID = 8; /* */
  107. count=0;
  108. i=0;
  109. domain = Get_Domain(1); /* returns fluid domain pointer */
  110. t = Lookup_Thread(domain, thread_ID);
  111. /* */
  112. begin_f_loop(f,t)
  113. {
  114. c0 = F_C0(f,t);
  115. count++;
  116. }
  117. end_f_loop(f,t)
  118. g_sgrid = (int *) malloc(count * sizeof(int));
  119. g_ssize = count;
  120. /* */
  121. begin_f_loop(f,t)
  122. {
  123. c0 = F_C0(f,t);
  124. g_sgrid[i] = c0;
  125. i++;
  126. }
  127. end_f_loop(f,t)
  128. }
  129. /* */
  130. int cellTest(int d)
  131. {
  132. int iResult;
  133. int i;
  134. /* if d is within array g_sgrid return 1, otherwise return 0. */
  135. iResult = 0;
  136. for (i=0; i<g_ssize; ++i)
  137. {
  138. if (d == g_sgrid[i])
  139. {
  140. iResult = 1;
  141. break;
  142. }
  143. }
  144. return iResult;
  145. }
  146. real g_hy_face_t_avg = 0.0;
  147. /* Fetch the average temperature of a specific face */
  148. DEFINE_ON_DEMAND(hy_face_temp_avg)
  149. {
  150. real temperature = 0.0f;
  151. real area = 0.0f;
  152. int num = 0;
  153. int thread_ID = 8;
  154. /* */
  155. face_t f;
  156. Thread *t;
  157. int n;
  158. Node *node;
  159. Domain *domain;
  160. /* */
  161. real NV_VEC(A);
  162. real x[ND_ND];
  163. domain = Get_Domain(1); /* returns fluid domain pointer */
  164. t = Lookup_Thread(domain, thread_ID);
  165. /*
  166. CX_Message("There are %d nodes in Face.\n", F_NNODES(f,t));
  167. */
  168. /* area = A[0]; */
  169. begin_f_loop(f, t) /* loops over faces in a face thread */
  170. {
  171. F_CENTROID(x,f,t);
  172. F_AREA(A, f, t);
  173. #ifdef DEBUG
  174. CX_Message("area : %f\n", -A[1]);
  175. CX_Message("temperature : %f %f\n", x[0], F_T(f, t));
  176. #endif /* DEBUG */
  177. temperature -= F_T(f,t) * A[1];
  178. area -= A[1];
  179. }
  180. end_f_loop(f, t)
  181. g_hy_face_t_avg = temperature / area;
  182. /* */
  183. CX_Message("Total area of Face %d : %f\n", THREAD_ID(t), area);
  184. CX_Message("The average temperature of the Face : %f\n", g_hy_face_t_avg);
  185. }
  186. FILE *fout;
  187. void Print_Thread_Face_Centroids(Domain *domain, int id)
  188. {
  189. real FC[3] = {0., 0.};
  190. face_t f;
  191. Thread *t = Lookup_Thread(domain, id);
  192. fprintf(fout,"thread id %d\n", id);
  193. begin_f_loop(f,t)
  194. {
  195. F_CENTROID(FC,f,t);
  196. fprintf(fout, "f%d %g %g %g\n", f, FC[0], FC[1], FC[2]);
  197. }
  198. end_f_loop(f,t)
  199. fprintf(fout, "\n");
  200. }
  201. DEFINE_ON_DEMAND(get_coords)
  202. {
  203. Domain *domain;
  204. domain = Get_Domain(1);
  205. fout = fopen("faces.out", "w");
  206. Print_Thread_Face_Centroids(domain, 2);
  207. Print_Thread_Face_Centroids(domain, 4);
  208. fclose(fout);
  209. }
  210. /* Source Term */
  211. DEFINE_SOURCE(MySourceTerm, c, t, dS, eqn)
  212. {
  213. real source;
  214. if(cellTest(c))
  215. {
  216. source = 1.0;
  217. dS[eqn] = 0.0;
  218. return source;
  219. }
  220. return 0;
  221. }
  222. /*******************************************************************/
  223. /* UDF for specifying an x-momentum source term in a spatially */
  224. /* dependent porous media */
  225. /*******************************************************************/
  226. /*
  227. * source = -0.5 C_2 \rho y |v_x| v_x
  228. */
  229. #define C2 100.0
  230. DEFINE_SOURCE(xmom_source, c, t, dS, eqn)
  231. {
  232. real x[ND_ND];
  233. real con, source;
  234. C_CENTROID(x, c, t);
  235. con = C2*0.5*C_R(c, t)*x[1];
  236. source = -con*fabs(C_U(c, t))*C_U(c, t);
  237. dS[eqn] = -2.*con*fabs(C_U(c, t));
  238. return source;
  239. }
  240. /****************************************************/
  241. /* Simple UDF that uses compiler directives */
  242. /****************************************************/
  243. DEFINE_ADJUST(where_am_i, domain)
  244. {
  245. #if RP_HOST
  246. CX_Message("I am in the host process\n");
  247. #endif /* RP_HOST */
  248. #if RP_NODE
  249. CX_Message("I am in the node process with ID %d\n",myid);
  250. /* myid is a global variable which is set to the multiport ID for
  251. each node */
  252. #endif /* RP_NODE */
  253. #if !PARALLEL
  254. CX_Message("I am in the serial process\n");
  255. #endif /* !PARALLEL */
  256. }
  257. /**********************************************************************
  258. UDF that computes diffusivity for mean age using a user-defined
  259. scalar.
  260. ***********************************************************************/
  261. DEFINE_DIFFUSIVITY(mean_age_diff,c,t,i)
  262. {
  263. return C_R(c,t) * 2.88e-05 + C_MU_EFF(c,t) / 0.7;
  264. }