1©2015ANSYS,Inc.June7,201516.0ReleaseLecture3:ComposingUDFsUserDefinedFunctionsinANSYSFluent2©2015ANSYS,Inc.June7,2015UsingtheDEFINEMacrosAllUDFsaredefinedusingamacrostartingwith“DEFINE_”.ThegeneralformatofaDEFINE_macrois:udf_nameisthenameofthefunctionyoudefine•Itiscase-sensitive•ThisisthenameseeninthepicklistsintheGUIpanel.Example:•Definesboundaryprofilename“inlet_x_velocity”•threadistheboundarythreadwhichwillbechanged•indexidentifieswhichboundaryconditionvalueistobesetDEFINE_UDF_TYPE(udf_name,0ormorevariables...)DEFINE_PROFILE(inlet_x_velocity,thread,index)3©2015ANSYS,Inc.June7,2015DEFINE_PROFILE–BoundaryConditions•DEFINE_PROFILEcanbeusedonanyboundaryzonetosetconditionssuchastemperature,velocityetc.•Itispossibletospecifyaconstantvalue,position-dependentortime-dependentvalues•NotethattheBCsareappliedatthefacesofboundary,nottheadjacentcells.4©2015ANSYS,Inc.June7,2015AccesstherequiredinletzoneGotoeachofthefacesthatmaketheinletNotethecurrentflowtimeCalculatethevelocitycorrespondingtothetimeApplythevelocitytothefaceGothenextfaceTimeDependentVelocityProfileatInlet𝑽𝒙=𝑽𝟎+𝑨𝒔𝒊𝒏𝝎𝒕;𝑽𝟎=𝟐𝟎m/s,𝑨=𝟓m/s,𝝎=𝟏𝟎rad/s5©2015ANSYS,Inc.June7,2015Example1–TransientInletVelocityThisUDFsetsthevalueofx-velocityatthefaceoftheinletboundaryzoneaccordingtotheequation𝑽𝒙=𝑽𝟎+𝑨𝒔𝒊𝒏(𝝎𝒕)where𝑽𝟎=𝟐𝟎m/s,𝑨=𝟓m/s,𝝎=𝟏𝟎rad/s•Loopthroughthefacesbelongingtotheinletzone.•Specifythex-velocitycom-ponentoneachfaceusingF_PROFILE(f,ft,var)#includeudf.h”#defineOMEGA10.0/*SetUserConstants*/#defineV020.0#defineV_AMP5.0DEFINE_PROFILE(inlet_x_velocity,ft,var){realflow_time;face_tf;/*Faceindexhasitsowntype*/flow_time=CURRENT_TIME;/*SpecialFluentmacro*/begin_f_loop(f,ft)/*SpecialFluentfaceloopmacro*/{F_PROFILE(f,ft,var)=V0+V_AMP*sin(OMEGA*flow_time);}end_f_loop(f,ft)}6©2015ANSYS,Inc.June7,2015HookingaProfileUDFtoFluentSolutionSetup→BoundaryCondition→Zonename(here:inlet)1.Whichboundaryisthisappliedto?2.Whichflowvariableisbeingset?7©2015ANSYS,Inc.June7,2015ResultsEx.1:TransientInletVelocityTimehistoryoftheaveragevelocityatthepipeexitshowssinusoidaloscillationwithameanof20m/sandanamplitudeof5m/s.AverageVelocityMagnitudeFlowTime8©2015ANSYS,Inc.June7,2015Example2–SinusoidalWallTemperatureThisUDFsetsthetemperatureonthefacesofawallaccordingtotheequation𝑻𝒙=𝟑𝟎𝟎+𝟏𝟎𝟎𝒔𝒊𝒏(𝝅𝒙/𝑳)•ND_NDreturns2ina2Dand3in3Dcases•F_CENTROID(pos,f,ft)usedtoobtainthecentroidofaface.Coordinatesarestoredinpos.#includeudf.h#defineLENGTH0.005#defineT_MIN300.0#defineT_AMP100.0DEFINE_PROFILE(wall_temp,ft,var){realtemperature;realpos[ND_ND];/*How2Dand3Dvectorsaredefined*/realomega;face_tf;omega=M_PI/LENGTH;/*M_PIisFluentdefinedvalueforPi*/begin_f_loop(f,ft)/*SpecialFluentfaceloopmacro*/{F_CENTROID(pos,f,ft);/*Facecentroidsetinpos*//*x=pos[0]*/temperature=T_MIN+T_AMP*sin(omega*pos[0]);F_PROFILE(f,ft,var)=temperature;}end_f_loop(f,ft)}9©2015ANSYS,Inc.June7,2015ResultsEx.2:SinusoidalWallTemperatureVariationT(x)=𝟑𝟎𝟎+𝟏𝟎𝟎𝐬𝐢𝐧𝝅𝒙𝟎.𝟎𝟎𝟓HeatedwallT=T(x)AdiabaticwallColdInflowoutletStaticTemperature(K)10©2015ANSYS,Inc.June7,2015DEFINE_SOURCE–CustomSourceTerms•Addsourcetermstotheflowequationusing:•Sourcetermscanbeaddedtoequationsfor–Continuity–Energy–Multiphaseandothers•EnergysourcetermUDFsmayalsobedefinedforsolidzones.•Thesourcetermmacroiscalledforeachcellseparatelyunliketheprofilemacro.•Thereisnoneedtoperformacellloop!•Theunitsofallsourcetermsareexpressedintermsofthevolumetricgenerationrate.(Totalcellsource/m3)realDEFINE_SOURCE(udf_name,cell,thread,dS,eqn)–Momentum–k,11©2015ANSYS,Inc.June7,2015SourceTermEquationNote:sourcetermsareaddedtoeachcellinthecontinuumzone,hencenoloopovercellsareneeded.Asourcetermcanbelinearizedasfollowsandaresuppliedbytheuser.isthereturnvalue.Thesolverwillusethelinearizationifitenhancesnumericalstability;otherwiseisusedexplicitly,i.e..IfBisunknown,use.realDEFINE_SOURCE(udf_name,cell,thread,dS,eqn)𝑩=dS[eqn]𝑺∗𝑺∗𝑺∗𝑺𝝓=𝑺∗where“*”standsforthevaluesbasedonthepreviousiteration.𝑩=012©2015ANSYS,Inc.June7,2015Example3:PositionDependentPorousMedia#includeudf.h#defineC2100.0#defineHEIGHT0.01DEFINE_SOURCE(mom_source_x,c,ct,dS,eqn){realNV_VEC(pos);/*Anotherwaytodefine2Dand3Dvectors*/realdS_dU;C_CENTROID(pos,c,ct);/*Cellcentroidsetinpos*//*C_R(c,ct)givesdensityofmaterialinthecell*/dS_dU=-pos[1]*C2*C_R(c,ct)*fabs(C_U(c,ct))/HEIGHT;source=dS_dU*C_U(c,ct)/2.0;dS[eqn]=dS_dU;/*Couldbesetto0.0ifunknownornotrequired*/returnsource;}xxxxvChydvdSBvvChyS22)/(2/)/(ThisUDFaddsanegativemomentumsourceintoeachcellwhichisdependentonthelocalflowspeedandyposition.13©2015ANSYS,Inc.June7,2015HookingaSourceUDFtoFluentSolutionSetup→CellZoneConditions→ZoneName(here:fluid)(1)Activatesourceterms(2)ClickEdit(3)AttachthesourceUDFtothematchingequation.14©2015ANSYS,Inc.June7,2015ResultsEx.3:PositionDependentPorousMedia•X-momentumlossislinearinthey-position,startingfromzeroatthesymmetryplane.•Thefluidflowsfasternearthemidlineofthechannelwheretheresistanceisleast.•Pressuredropinaporousmediaismodeledassinktermsinthemomentumequation.VelocityVectors(m/s)PorousPlugIn