CFD Online Logo CFD Online URL
Home > Forums > Fluent UDF and Scheme Programming

Define_Source UDF for Enthalpy of Dilution

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   March 15, 2015, 17:48
Default Define_Source UDF for Enthalpy of Dilution
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 2
jreiter164 is on a distinguished road
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:

	/* 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 */
    volume=C_VOLUME(cell,thread);                                /* Volume of fluid in the cell */
    mass_flux_e_in=0;											 /* Initialize flux variables */
    /* 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);
        flux=F_FLUX(face, thread);
		if (flux>0)
            mass_flux_e_out += flux*x_e;
            mass_flux_w_out += flux*x_w;
			mass_flux_e_in += -1*flux*x_e;
			mass_flux_w_in += -1*flux*x_w;
	/* Convert to molar flow rate */
	/* Compute molar flow rate fraction */
	/* Compute enthalpy source per unit volume */
	enth=trapz(y_e_in, y_e_out);
    dS[eqn]=0;                                                   /* Set derivative term to zero */
	return enth_source;
The main idea is to determine what the molar flow rate fraction of ethanol moving in and out of each cell is. I am attempting to do this by looping over each face of each cell. At each face, I evaluate the mass flow rate of ethanol and water across the face and determine whether it is in or out based on the sign. A function I found in a research paper then is integrated from the "in" flow rate fraction to the "out" flow rate fraction to determine the enthalpy of dilution. trapz is a numerical integration routine that I have inside my UDF. It numerically integrates the function from the research paper. My UDF compiles fine, but when I try and run my simulation I get the error:

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.
jreiter164 is offline   Reply With Quote

Old   March 15, 2015, 22:28
Senior Member
Join Date: Mar 2015
Posts: 468
Rep Power: 5
`e` is on a distinguished road
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.
`e` is offline   Reply With Quote

Old   March 16, 2015, 22:11
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 2
jreiter164 is on a distinguished road
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?
jreiter164 is offline   Reply With Quote

Old   March 17, 2015, 05:07
Senior Member
Join Date: Mar 2015
Posts: 468
Rep Power: 5
`e` is on a distinguished road
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
`e` is offline   Reply With Quote

Old   March 18, 2015, 21:10
New Member
Justin Reiter
Join Date: Feb 2015
Posts: 9
Rep Power: 2
jreiter164 is on a distinguished road
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
jreiter164 is offline   Reply With Quote


define_source, udf

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 21: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

All times are GMT -4. The time now is 16:44.