|
[Sponsors] |
March 15, 2015, 17:48 |
Define_Source UDF for Enthalpy of Dilution
|
#1 |
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 11 |
I am trying to write a UDF to define an energy source produced by the dilution of ethanol in water. My code is as follows:
Code:
DEFINE_SOURCE(ew_enthalpy_source,cell,thread,dS,eqn) { /* VARIABLE DECLARATION */ /* FLUENT data types */ face_t face; int n; /* External constants */ real M_e, M_w; /* Molar masses */ /* Values obtained from FLUENT */ real x_e, x_w, flux, volume; /* Calculated values */ real mass_flux_e_in, mass_flux_e_out; /* Ethanol mass fluxes */ real molar_flux_e_in, molar_flux_e_out; /* Ethanol molar fluxes */ real mass_flux_w_in, mass_flux_w_out; /* Water mass fluxes */ real molar_flux_w_in, molar_flux_w_out; /* Water molar fluxes */ real y_e_in, y_e_out, enth, enth_source; /* Mole rate fractions and enthalpy */ /* Define constants */ M_e=46.07; /* Molar mass of ethanol */ M_w=18.02; /* Molar mass of water */ /* CALCULATION */ volume=C_VOLUME(cell,thread); /* Volume of fluid in the cell */ mass_flux_e_in=0; /* Initialize flux variables */ mass_flux_e_out=0; mass_flux_w_in=0; mass_flux_w_out=0; /* loop over faces of each cell and compute mass flow rates in and out */ c_face_loop(cell, thread, n) { x_e=F_YI(face, thread, 0); x_w=1-x_e; flux=F_FLUX(face, thread); if (flux>0) { mass_flux_e_out += flux*x_e; mass_flux_w_out += flux*x_w; } else { mass_flux_e_in += -1*flux*x_e; mass_flux_w_in += -1*flux*x_w; } } /* Convert to molar flow rate */ molar_flux_e_out=mass_flux_e_out/M_e; molar_flux_e_in=mass_flux_e_in/M_e; molar_flux_w_out=mass_flux_w_out/M_w; molar_flux_w_in=mass_flux_w_in/M_w; /* Compute molar flow rate fraction */ y_e_in=molar_flux_e_in/(molar_flux_e_in+molar_flux_w_in); y_e_out=molar_flux_e_out/(molar_flux_e_out+molar_flux_w_out); /* Compute enthalpy source per unit volume */ enth=trapz(y_e_in, y_e_out); enth_source=enth/volume; dS[eqn]=0; /* Set derivative term to zero */ return enth_source; } FLUENT received fatal signal (ACCESS_VIOLATION) I have a few questions for you all: 1) First and foremost, are there any obvious culprits for that error message? 2) Does FLUENT even allow for creation of functions within a UDF as I have done here with "trapz"? 3) I am unsure about the syntax/structure for the c_face_loop. Am I implementing that properly? 4) I am very concerned about the computational cost of looping over each face of each cell, as well as performing a numerical integration for each cell. Do you guys know of any shortcuts or easier ways I could accomplish my goal? Any and all help is appreciated. |
|
March 15, 2015, 22:28 |
|
#2 |
Senior Member
Join Date: Mar 2015
Posts: 892
Rep Power: 18 |
1) It's likely Fluent is trying to access a variable that doesn't currently exist. I've heard that some variables, such as gradients (maybe the flux you have there as well), are not available on the first iteration. To test this hypothesis, try running a few iterations before enabling this DEFINE_SOURCE UDF. If that's the cause, then simply apply an if statement restricting your code for iterations after the first. If that's not the problem then strip the code down step by step until you know the precise cause.
2) I'd expect Fluent to "allow" the creation of functions within compiled UDFs. I assume you've defined this 'trapz' function in another source file (that you've compiled!). Be careful that function names don't overlap with other functions; perhaps call it 'myTrapz'. 3) c_face_loop generally looks OK (I haven't read your equations / methodology in detail but it seems fine). 4) Don't be concerned, computers are very quick things! Try simulations with and without this UDF enabled and report back with your times (please, it'd be interesting). I'd approach this problem the same way you have; I don't have any suggestions on another method/shortcut. |
|
March 16, 2015, 22:11 |
|
#3 |
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 11 |
Hello e,
Thank you for your feedback. I tried waiting a few iterations to enable the UDF and that did not help. I am going to do as you suggest and move through my code section by section and try and determine what is causing the problem. One issue I am having is re-compiling my altered UDF. When I try and re-build my library, it gives me the error message: "FLUENT unable to overwrite existing library. Permissions may be not be set to allow this, or the library may already be loaded or in use." Is there a way to unload the library? I have tried exiting FLUENT, deleting the "libudf" folder from my working folder, and then re-opening FLUENT and re-compiling. I have had mixed results with this and it is time consuming. Is there an easier way to make edits to the source file and recompile for troubleshooting purposes? |
|
March 17, 2015, 05:07 |
|
#4 |
Senior Member
Join Date: Mar 2015
Posts: 892
Rep Power: 18 |
My compiling process for Fluent UDFs with an existing library loaded:
1) Define > User-Defined > Functions > Manage... 2) Select existing library and click "Unload" 3) Define > User-Defined > Functions > Compiled... 4) Click "Add..." if there are other source files required (Fluent reads in a new version of the file regardless of whether or not you delete then add the same source file) 5) Build then Load |
|
March 18, 2015, 21:10 |
|
#5 |
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 11 |
Thank you for that information e, it made my debugging process much smoother. I can now compile my UDF, load it into the solver, and run my simulation without getting the access violation error. All good news.
However, now I have a new issue. After initialization, before my first actual iteration completes I receive the error message: "Divergence detected in AMG solver: temperature" Steps I have taken to resolve this are: 1) Checked the mesh and boundary conditions 2) Reduced energy relaxation factor 3) Ran the calculation for several iterations before turning on the source term 4) Double checked my UDF code to make sure it is mathematically correct None of these have helped. Does anyone have any suggestions on how to resolve this issue? Last edited by jreiter164; March 18, 2015 at 21:39. Reason: Adding something I forgot |
|
Tags |
define_source, udf |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
WILLING TO PAY/ FREELANCER REQUIRED / small UDF coding force loads over body / 6DOF | acasas | CFD Freelancers | 1 | January 23, 2015 08:26 |
UDF parallel error: chip-exec: function not found????? | shankara.2 | Fluent UDF and Scheme Programming | 1 | January 16, 2012 23:14 |
How to add a UDF to a compiled UDF library | kim | FLUENT | 3 | October 26, 2011 22:38 |
UDF...UDF...UDF...UDF | Luc SEMINEL | FLUENT | 0 | November 25, 2002 05:03 |
UDF, UDF, UDF, UDF | Luc SEMINEL | Main CFD Forum | 0 | November 25, 2002 05:01 |