CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

UDF for reading file and assigning temperature values to boundary faces

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By Sherlock_1812
  • 1 Post By pakk
  • 1 Post By Sherlock_1812
  • 1 Post By pakk
  • 1 Post By pakk
  • 1 Post By nsaeidi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 25, 2014, 02:19
Default UDF for reading file and assigning temperature values to boundary faces
  #1
Senior Member
 
Srivathsan N
Join Date: Jan 2013
Location: India
Posts: 101
Rep Power: 13
Sherlock_1812 is on a distinguished road
Hi !

I would like to apply a varying temperature boundary condition at one of the boundary faces in my geometry by reading the temperature values from a data file (.txt) present in the case folder.

I read the data into a global variable (as an array), but am confused as to how I would assign each of those temperatures to the face centroids while looping over the faces of the boundary. (The current way assigned in the C file attached, obviously throws an error while interpreting- incorrect type conversion)

This is my first time writing a udf, so any help is welcome. pls find the C file attached.
Attached Files
File Type: c tempProfile.C (774 Bytes, 105 views)
luonghungtruyen likes this.
__________________
Regards,

Srivaths
Sherlock_1812 is offline   Reply With Quote

Old   November 25, 2014, 04:42
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
I think you made a good start. Here is a hint for the rest:
Code:
DEFINE_PROFILE(dropSurfaceTemp,thread,position)
{
  real coord[ND_ND];	/* this will hold the position vector */
  real x;
  face_t f;

  begin_f_loop(f,thread)
  {
    F_CENTROID(coord,f,thread);
    x=coord[0];
      /*Here you need to find i such that x1[i] is close to x */
    F_PROFILE(f,thread,position)= Temp[i];   /*Notice the [i] added */
  }
  
  end_f_loop(f,thread)
}
How to find that i is not so trivial, otherwise I would have added it.
luonghungtruyen likes this.
pakk is offline   Reply With Quote

Old   November 25, 2014, 08:01
Default
  #3
Senior Member
 
Srivathsan N
Join Date: Jan 2013
Location: India
Posts: 101
Rep Power: 13
Sherlock_1812 is on a distinguished road
How silly of me!

Thank you!
__________________
Regards,

Srivaths
Sherlock_1812 is offline   Reply With Quote

Old   March 11, 2015, 09:09
Default Error while fitting polynomial temperature distribution
  #4
Senior Member
 
Srivathsan N
Join Date: Jan 2013
Location: India
Posts: 101
Rep Power: 13
Sherlock_1812 is on a distinguished road
Hi!

I am attempting to assign a temperature variation on a face using a polynomial, with coefficients defined in the UDF.

However I'm getting a strange error where the temperatures vary from 265-275 K on the face when the actual variation according to the polynomial should've been between 289-294 K. I don't seem to find a reason for the mismatch.

Could it be because of the data type used, or the extremely large coefficients? Should I increase precision for them?

Open to suggestions. Attached are the UDF and the plot.
Attached Files
File Type: c dropSurfaceTemp.c (771 Bytes, 44 views)
luonghungtruyen likes this.
__________________
Regards,

Srivaths
Sherlock_1812 is offline   Reply With Quote

Old   March 11, 2015, 09:29
Default
  #5
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
A solution that might work:
change your evaluation scheme to something numerically more stable. What I mean in practice: replace
Code:
(p1*x1*x1*x1*x1) + (p2*x1*x1*x1) + (p3*x1*x1) + (p4*x1) + p5;
by the mathematically equivalent but numerically better
Code:
p5+(x1*(p4+x1*(p3+(x1*(p2+x1*p1)))));
A better solution: rethink your model. Your coefficients are far too large, it seems like you are overfitting. Why do you fit with a sixth-order polynomial? Because you have six data points? If you would use a linear fit, which characteristics do you lose? Or a piece-wise linear fit?
luonghungtruyen likes this.
pakk is offline   Reply With Quote

Old   March 11, 2015, 11:22
Default
  #6
Senior Member
 
Srivathsan N
Join Date: Jan 2013
Location: India
Posts: 101
Rep Power: 13
Sherlock_1812 is on a distinguished road
So you think that it could be because of the extremely large polynomial coefficients? I'm afraid I can't use a piecewise linear or a linear fit. I shall try the 6th order polynomial with the expression suggested.

Thank you.
__________________
Regards,

Srivaths
Sherlock_1812 is offline   Reply With Quote

Old   March 11, 2015, 11:41
Default
  #7
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
What do you mean, you can't use a piecewise linear fit? Computationally it is no problem, it will be easier to implement, and easier to understand. And the results will be virtually indistinguishable from the sixth order polynomial.

Where does your sixth order polynomial come from? If it is really from a fit, I would strongly suggest to use a piecewise linear fit. What makes you say you have to use the sixth order fit???
luonghungtruyen likes this.
pakk is offline   Reply With Quote

Old   June 13, 2016, 10:20
Default
  #8
New Member
 
Negar Saeidi
Join Date: Dec 2015
Location: Sudbury, ON, Canada
Posts: 18
Rep Power: 10
nsaeidi is on a distinguished road
Hello Srivaths!

Did you manage to make your code work?
I am trying to apply a UDF to the inlet boundary of my porous zone. My C file is at the end of this text.
After I compile the UDF and Hook the read_data function, this message shows up in Console: “temperature limited to 1.000000e+00 in 141630 cells on zone 4”
Zone 4 is my porous zone. My temperature limit setting is from 1K to 5000K.
I don’t face this problem when I set constant inlet BC or when I don’t have a read_data function in the UDF.
Do you have any idea what could be the problem?

Here is my Code:

/* ************************************************** ********* */
// UDF for time-varying temperature at the inlet
/* ************************************************** ********* */

#include "udf.h"


double Temp[15];
FILE *fp;

DEFINE_INIT(read_data, d)
{
int i;
fp=fopen("Env-Temp.txt","r");
for (i=1;i<15;i++)
{
fscanf(fp,"%ld", &Temp[i]);
}
fclose(fp);
}


DEFINE_PROFILE(Tinlet, thread, position)
{
face_t f;
real t = CURRENT_TIME*pow(1,-1);
int i = t;

begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = Temp[i];
}
end_f_loop(f, thread)
}

Here is the text file that I read from:

273
278
283
288
293
298
293
288
283
278
273
268
263
258
253
luonghungtruyen likes this.
nsaeidi is offline   Reply With Quote

Reply


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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting cell variable values in a fluid zone using UDF eromon84 Fluent UDF and Scheme Programming 6 March 28, 2021 12:59
timeVaryingMappedFixedValue: large data mapping on complex inlet boundaries vkrastev OpenFOAM Pre-Processing 7 June 2, 2016 16:50
Assigning values to a cartesian grid from a binary image (or stl) Cyp OpenFOAM Pre-Processing 1 August 5, 2014 19:02


All times are GMT -4. The time now is 20:58.