


{"id":450754,"date":"2026-01-20T12:56:30","date_gmt":"2026-01-20T12:56:30","guid":{"rendered":"https:\/\/innovationspace.ansys.com\/forum\/forums\/topic\/compile-the-udf-code-and-apply-it-to-the-dpm\/"},"modified":"2026-01-20T12:56:30","modified_gmt":"2026-01-20T12:56:30","slug":"compile-the-udf-code-and-apply-it-to-the-dpm","status":"publish","type":"topic","link":"https:\/\/innovationspace.ansys.com\/forum\/forums\/topic\/compile-the-udf-code-and-apply-it-to-the-dpm\/","title":{"rendered":"Compile the UDF code and apply it to the DPM."},"content":{"rendered":"<p>&lt;p&gt;I plan to compile the UDF code based on the content of the image and apply it to DPM. Could you please check if there are any errors in the provided code? The references are as follows.&lt;\/p&gt;&lt;p&gt;Xu W, Dong CJ, Li DS. Numerical simulation on influences of wind speed to leaking diffusion of liquid ammonia. Advanced Materials Research. 2015 Jan 27;1073:383-7.&lt;\/p&gt;&lt;p&gt;&nbsp;&lt;\/p&gt;&lt;p&gt;<img decoding=\"async\" src=\"https:\/\/innovationspace.ansys.com\/forum\/wp-content\/uploads\/sites\/2\/2026\/01\/20-01-2026-1768913476-mceclip0.png\" \/><img decoding=\"async\" src=\"https:\/\/innovationspace.ansys.com\/forum\/wp-content\/uploads\/sites\/2\/2026\/01\/20-01-2026-1768913483-mceclip1.png\" \/>&lt;\/p&gt;&lt;div&gt;&lt;div&gt;#include &#8220;udf.h&#8221;&lt;\/div&gt;&lt;div&gt;#include &#8220;dpm.h&#8221;&lt;\/div&gt;&lt;div&gt;#include &#8220;dpm_mem.h&#8221;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;\/* \u5b9a\u4e49\u5e38\u91cf *\/&lt;\/div&gt;&lt;div&gt;#define T_SAT 240.15 &nbsp; &nbsp; &nbsp; &nbsp;\/* \u84b8\u53d1\u6e29\u5ea6 -33&deg;C = 240.15 K *\/&lt;\/div&gt;&lt;div&gt;[cite_start]#define LATENT_HEAT 1.37e6 &nbsp;\/* \u6c28\u6c14\u5316\u6f5c\u70ed J\/kg [cite: 66] *\/&lt;\/div&gt;&lt;br&gt;&lt;div&gt;DEFINE_DPM_HEAT_MASS(multivap, tp, Cp, hgas, hvap, cvap_surf, Z, dydt, dzdt)&lt;\/div&gt;&lt;div&gt;{&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; int ns;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; Material *sp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u53d8\u91cf\u521d\u59cb\u5316 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; int nc = TP_N_COMPONENTS(tp);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; Thread *t0 = TP_CELL_THREAD(tp);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; Material *gas_mix = THREAD_MATERIAL(DPM_THREAD(t0, tp));&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; cphase_state_t *c = &amp;(tp-&gt;cphase[0]);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real molwt[MAX_SPE_EQNS];&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real Tp = TP_T(tp); &nbsp; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real mp = TP_MASS(tp); &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real molwt_bulk = 0.; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u8ba1\u7b97\u7c92\u5b50\u51e0\u4f55\u5c5e\u6027 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real Dp = DPM_DIAM_FROM_VOL(mp \/ TP_RHO(tp));&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real Ap = DPM_AREA(Dp); &nbsp; [cite_start]\/* [cite: 63] *\/&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/* &#8212; 1. \u70ed\u4f20\u5bfc\u8ba1\u7b97 (\u4fdd\u6301\u539f\u903b\u8f91) &#8212; *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real Pr = c-&gt;sHeat * c-&gt;mu \/ c-&gt;tCond;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real Nu = 2.0 + 0.6 * sqrt(tp-&gt;Re) * pow(Pr, 1.\/3.);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real h = Nu * c-&gt;tCond \/ Dp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/* Q = h * Ap * (T_gas &#8211; T_p) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real heat_transfer = h * (c-&gt;temp &#8211; Tp) * Ap;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u8f85\u52a9\uff1a\u8ba1\u7b97\u6c14\u76f8\u5206\u5b50\u91cf *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; mixture_species_loop(gas_mix, sp, ns)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; molwt[ns] = MATERIAL_PROP(sp, PROP_mwi);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; molwt_bulk += c-&gt;yi[ns] \/ molwt[ns];&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; molwt_bulk = MAX(molwt_bulk, DPM_SMALL);&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/* &#8212; 2. \u6838\u5fc3\u903b\u8f91 (\u4fee\u590d\u4e86\u7269\u7406\u9519\u8bef) &#8212; *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real evaporation_rate = 0.0;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u60c5\u51b5 A: \u4f4e\u6e29\u663e\u70ed\u52a0\u70ed (Tp &lt; T_SAT) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; if (Tp &lt; T_SAT)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u4ec5\u53d1\u751f\u70ed\u4f20\u5bfc\u5347\u6e29\uff0c\u65e0\u663e\u8457\u84b8\u53d1 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* Eq (9): m * Cp * dT\/dt = Q *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; dydt[0] += heat_transfer \/ (mp * Cp);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; evaporation_rate = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u60c5\u51b5 B: \u6cb8\u817e\/\u6c14\u5316 (Tp &gt;= T_SAT) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; else&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u5047\u8bbe\u70ed\u91cf\u5168\u90e8\u8f6c\u5316\u4e3a\u6f5c\u70ed (dm\/dt * L = Q) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; if (heat_transfer &gt; 0)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;evaporation_rate = heat_transfer \/ LATENT_HEAT;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u80fd\u91cf\u5b88\u6052\u5904\u7406\uff1a\u663e\u70ed\u53d8\u5316 = (\u603b\u70ed\u91cf &#8211; \u6f5c\u70ed\u6d88\u8017) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u7406\u8bba\u4e0a\u6b64\u5904 dydt[0] \u7ed3\u679c\u4e3a 0\uff0c\u5373\u6e29\u5ea6\u4fdd\u6301\u5728\u6cb8\u70b9 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real latent_heat_term = -evaporation_rate * LATENT_HEAT;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; dydt[0] += (heat_transfer + latent_heat_term) \/ (mp * Cp);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/* &#8212; 3. \u5173\u952e\u4fee\u590d\uff1a\u6e90\u9879\u66f4\u65b0 &#8212; *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/* \u83b7\u53d6\u6c28\u6c14\u5728\u6c14\u76f8\u4e2d\u7684\u7ec4\u5206\u7d22\u5f15 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; int gas_index = TP_COMPONENT_INDEX_I(tp, 0);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; if (gas_index &gt;= 0)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* [\u4fee\u590d1] \u66f4\u65b0\u7c92\u5b50\u8d28\u91cf\u53d8\u5316\u7387 dydt *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u7c92\u5b50\u8d28\u91cf\u51cf\u5c11\uff0c\u5bfc\u6570\u4e3a\u8d1f\u503c *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u6ce8\u610f\uff1a\u5bf9\u4e8e\u591a\u7ec4\u5206\u7c92\u5b50\uff0c\u8d28\u91cf\u5b58\u50a8\u5728 dydt[1 + component_index] *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u5bf9\u4e8e\u5355\u7ec4\u5206 Droplet\uff0c\u901a\u5e38 dydt[1] \u5373\u4e3a\u8d28\u91cf\u53d8\u5316 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; dydt[1] -= evaporation_rate;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* [\u4fee\u590d2] \u66f4\u65b0\u6c14\u76f8\u6e90\u9879 dzdt *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u6c14\u4f53\u83b7\u5f97\u8d28\u91cf\uff0c\u6e90\u9879\u4e3a\u6b63\u503c (+) *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; dzdt-&gt;species[gas_index] += evaporation_rate;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u66f4\u65b0\u6c14\u76f8\u603b\u8d28\u91cf\u6e90\u9879 *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; dzdt-&gt;gas_mass += evaporation_rate;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* \u80fd\u91cf\u6e90\u9879\u901a\u5e38\u7531 Fluent \u6839\u636e mass source \u81ea\u52a8\u5904\u7406\u7113\u6d41\uff0c&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u82e5\u9700\u663e\u5f0f\u6dfb\u52a0\u663e\u70ed\u4f20\u9012\u7ed9\u6c14\u76f8\uff0c\u53ef\u53d6\u6d88\u4e0b\u884c\u6ce8\u91ca *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/* dzdt-&gt;energy += heat_transfer; *\/&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;}&lt;\/div&gt;&lt;\/div&gt;<\/p>\n","protected":false},"template":"","class_list":["post-450754","topic","type-topic","status-publish","hentry"],"aioseo_notices":[],"acf":[],"custom_fields":[{"0":{"_bbp_forum_id":["291848"],"_bbp_topic_id":["450754"],"_bbp_subscription":["641985","199"],"_bbp_author_ip":["141.11.127.67"],"_bbp_last_reply_id":["450767"],"_bbp_last_active_id":["450767"],"_bbp_last_active_time":["2026-01-20 14:01:43"],"_bbp_reply_count":["1"],"_bbp_reply_count_hidden":["0"],"_bbp_voice_count":["2"],"_bbp_engagement":["641985","199"],"_btv_view_count":["154"],"_bbp_topic_status":["unanswered"]},"test":"24212040751stumail-xsyu-edu-cn"}],"_links":{"self":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics\/450754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics"}],"about":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics\/450754\/revisions"}],"wp:attachment":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/media?parent=450754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}