©2012ANSYS,Inc.June17,20131Release14.514.5ReleaseChapter1IntroductiontoFluentfromtheInsideUserDefinedFunctionsinANSYSFluent©2012ANSYS,Inc.June17,20132Release14.5IntroductiontoUDFProgramming•WhyuseUDFs?−Fluentisageneral-purposetoolandcannotanticipateallusersneeds.−Additionofnewphysicalmodelsforacademicresearchorcustomerproprietaryphysics.−Customizationofboundaryconditions.©2012ANSYS,Inc.June17,20133Release14.5WhatisaUserDefinedFunction(UDF)?•AUDFisafunctionorsetoffunctionsprogrammedbytheuserinClanguageandlinkedtoFluenttoperformawidevarietyofoperationsincluding:–Initialization–Customboundaryconditions(bothspaceortimedependent)–Materialproperties–Sourcetermsintoanyoftheflowequations–Chemicalreactionrates–Postprocessingandreportingofmodifieddata–Addingextraflowequations–Meshmotion–Discretephasemodelmodification–Andmanymore...©2012ANSYS,Inc.June17,20134Release14.5UserAccesstotheFluentSolverUser-DefinedPropertiesUser-DefinedBCsUserDefinedINITIALIZESegregatedPBCSExitLoopRepeatCheckConvergenceUpdatePropertiesSolveTurbulenceEquation(s)SolveSpeciesSolveEnergyInitializeBeginLoopDBCSSolveOtherTransportEquationsasrequiredSolver?SolveMassContinuity;UpdateVelocitySolveU-MomentumSolveV-MomentumSolveW-MomentumSolveMass&MomentumSolveMass,Momentum,Energy,SpeciesUser-definedADJUSTSourcetermsSourcetermsSourcetermsSourcetermsTherearemanyplaceswithinthesolutioncyclewhereaUDFcanbeadded:©2012ANSYS,Inc.June17,20135Release14.5DataStructures•EachtypeofUDFispasseddifferentvaluesanddatastructuresdependingonitsfunction.•MostUDFswillneedgeometricaldata.Thisisstoredinahierarchy:–Domainsarecollectionsofthreadsthatmakethewholemesh–Threads(orzones)arecollectionsofcellsorfaces–CellsandfacesaremadeofNodes©2012ANSYS,Inc.June17,20136Release14.5GeometricalEntities•Thepartsofameshare:©2012ANSYS,Inc.June17,20137Release14.5DataStructures•Theterms‘Thread’and‘Zone’areinterchangeable.TheboundaryandcellzonesdefinedintheUserInterfacearestoredinternallyasthreads.–ZoneshaveanassociatedintegerIDseeninthesettingspanel–InaUDF,thecorrespondingthreadcanbeidentifiedusingthisID–Threadsareaninternaldatatypewhichstoresinformationaboutthemesh,models,properties,BCsallinoneplace•Acellhasinformationaboutitsboundingfacesandafacehasaccesstoitsneighbouringcells.©2012ANSYS,Inc.June17,20138Release14.514.5ReleaseIntroductiontotheCProgrammingLanguageUserDefinedFunctionsInANSYSFluent©2012ANSYS,Inc.June17,20139Release14.5CProgramming(1)–Basicsyntaxrules:•Eachstatementmustbeterminatedwithasemicolon;•Commentscanbeinsertedanywherebetween/*and*/•Allvariablesmustbeexplicitlydeclared(unlikeinFORTRAN)•Compoundstatementscanbecreatedbyenclosingmultiplestatementsbybraces:{}•Functiondefinitionshavethefollowingformat:return-value-typefunction-name(parameter-list){functionbody}–Built-indatatypes:int,float,double,enum,boolean:intniter,a;/*Declare‘niter’and‘a’asintegers*/floatdx[10];/*dxisanarrayof10numbersindexedfrom0-9:dx[0],dx[1]…dx[8],dx[9]*/enum{X,Y,Z};/*X,Y,Zareenumerationconstants=0,1,2*/©2012ANSYS,Inc.June17,201310Release14.5CProgramming(2)•APointerisaspecialkindofvariablethatcontainsanaddressinmemory,notcontent,ofanothervariable•Pointersaredeclaredusingthe*notation:int*ip;/*ipisdeclaredasapointertointeger*/•WecanmakeaPointerpointtotheaddressofpredefinedvariableasfollows:inta=1;int*ip;/*(*ip)containsaninteger*/ip=&a;/*&areturnstheaddressofvariablea*/printf(“ipisapointerwithvalue%p\n”,ip);printf(“Theintegerstoredatip=%d\n”,*ip);ipisapointerwithvalue0x40b2Theintegerstoredatip=1©2012ANSYS,Inc.June17,201311Release14.5CProgramming(3)•Pointersandarraysarecloselyrelated:intar[10];/*arisanarrayof10integers*/int*ip;/*(*ip)containsaninteger,*/Thenameofanarraywithoutanindexcanbeusedasapointertothestartofthearrayip=ar;/*aristheaddressofitsfirstintegersothisisequivalenttoip=&(ar[0]);*/BUT:Anarraynamecannotbesettoanewvalue.Itisfixedtothevaluesetwhenitisdefinedasanarray.ar=&i;/*Thiswillcauseacompilationerror*/©2012ANSYS,Inc.June17,201312Release14.5CProgramming(4)–Operators=(assignment)+,-,*,/,%(modulus),,=,=,==,!=++increment;i++is“post-increment”:usethecurrentvalueofiintheexpression,thenincrementiby1(i=i+1)--decrement:j--post-decrement,usethecurrentvalueofj,thendecrementjby1(j=j-1)+=additionassignment:agg+=single;/*means:agg=agg+single;*/*=multiplicationassignment,-=subtractionassignment,/=divisionassignment©2012ANSYS,Inc.June17,201313Release14.5CProgramming(5)–Basiccontrolstructuresif(…)statement;if(…)statement;elsestatement;if(…)statement;elseif(…)statement;forloops:for(k=0;kNUM;k++)statement;whileloops:while(…)statement;ConditionalOperator(?:)(condition?operanda:operandb)example:realAt=(rp_axi?At*2*M_PI:At);©2012ANSYS,Inc.June17,201314Release14.5CProgramming(6)–Compoundstatementsmaybeplacedwhereverasimplestatementisallowed:if(k2){statement;statement;…}else{statement;statement;…}andinloops:for(k=0;kNUM;k++){statement;statement;…}while(kNUM){statement;k++;…}©2012ANSYS,Inc.June17,201315Release14.5CProgramming(7)–Structures•Structuresarecollectionsofdatathatareofdif