|
[Sponsors] |
July 17, 2014, 18:48 |
UDF for spatially dependent momentum source
|
#1 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
Hello
I need to define a momentum source which is dependent on the location of the cells. My model is 2D only and time-independent, so all I want is to be able to define x_mom(x,y). I have data for the momentum source, which I approximnate with a 2nd degree polynomial fit. I was able to compile and hook up my UDF, but the results that I get are not correct. I was able to run the simulation with a simple UDF in which I set the source to be constant (spatially independent and about the average of the values returned by the polynomial fit), and I got reasonable results. Therefore, I think that the problem might be with C_CENTROID(x,c,t). What value does it actually return for x? Is it in meters / whatever unit is defined? Is there an easy way to verify which values are returned for x? Any other idea why this function does not work properly? I attached my code: #include "udf.h" #include <math.h> DEFINE_SOURCE(xmom_source,c,t,dS,eqn) { real source; real x0y0, x1y0, x0y1, x2y0, x1y1, x0y2; real p00, p10, p01, p20, p11, p02; real x[ND_ND]; C_CENTROID(x,c,t); x0y0 = 1; x1y0 = abs(x[0]); x0y1 = x[1]; x2y0 = pow(x[0],2.0); x1y1 = abs(x[0])*x[1]; x0y2 = pow(x[1],2.0); // define the polinomial coefficients p00 = -0.53825458; p10 = 73.53581559; p01 = 774.0253865; p20 = -1931.148778; p11 = 3762.806598; p02 = -85702.19479; // Define the momentum source source = p00*x0y0+p10*x1y0+p01*x0y1+p20*x2y0+p11*x1y1+p02*x 0y2; dS[eqn] = 0; return source; } Your help is much appreciated! Thanks |
|
July 17, 2014, 19:16 |
|
#2 | |
Senior Member
Ehsan Asgari
Join Date: Apr 2010
Posts: 473
Rep Power: 18 |
Quote:
You could write another UDF; e.g. a Define_Adjust to calculate the source terms for your particular zone and write data along with coordinates into a file. There, you may examine the value calculated for each cell and find where the problem lies. P.S.: Units are all in SI as far as I know. Goodluck |
||
July 17, 2014, 21:19 |
|
#3 |
Senior Member
|
What is the bounding-box of your computational domain?
What is your result? What is the result you expected? Your udf seems to be correct if your momentum source take the form of Code:
xmom(x, y)=pij|x|^i*|y|^j, 0<=i<=2, 0<=j<=2 |
|
July 17, 2014, 23:30 |
|
#4 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
syavash: Thanks for your reply. Could you please help me further and give me a rough outline on how to write such a function (Define_Adjust)? I think this would really help me find out where is the problem.
I defined my coefficients assuming SI units, so this should be fine. blackmask: my domain is a rectangle (-0.01 < x < 0.01, 0 < y < 0.05). This is the reason why I assume the results I get are not correct: the values of the source term in y range from 0 to 4 N (depending on the location). If I define a constant source of 4 N, I get velocities in y of roughly 0.6 m/s, which what I expect. But when I use the polynomial fit to approximate the source, it computes values several orders of magnitude higher (5000 m/s). I must add that I double checked the values of the polynomial coefficients separately, so they should be correct. My source term should indeed have the form you wrote down. Any idea on how to fix that? |
|
July 18, 2014, 04:29 |
|
#5 |
Senior Member
|
Is there a typo in your post? The range for y should be 0 < y < 0.005, right? otherwise the maximum value for the force should be two order of magnitude higher, i.e., about 400 N.
What is the output of grid/check? |
|
July 18, 2014, 07:29 |
|
#6 | |
Senior Member
Ehsan Asgari
Join Date: Apr 2010
Posts: 473
Rep Power: 18 |
Quote:
DEFINE_ADJUST(my_adjust,d) { Thread *t; cell_t c; FILE *fp; /* define a local pointer fp of type FILE */ fp = fopen("data.txt","w"); real x[ND_ND]; real source; real x0y0, x1y0, x0y1, x2y0, x1y1, x0y2; real p00, p10, p01, p20, p11, p02; thread_loop_c(t, d) { begin_c_loop(c, t) { C_CENTROID(x,c,t); x0y0 = 1; x1y0 = abs(x[0]); x0y1 = x[1]; x2y0 = pow(x[0],2.0); x1y1 = abs(x[0])*x[1]; x0y2 = pow(x[1],2.0); // define the polinomial coefficients p00 = -0.53825458; p10 = 73.53581559; p01 = 774.0253865; p20 = -1931.148778; p11 = 3762.806598; p02 = -85702.19479; // Define the momentum source source = p00*x0y0+p10*x1y0+p01*x0y1+p20*x2y0+p11*x1y1+p02*x 0y2; fprintf (fp, "% f% f% f% f", x[0],x[1],x[2],source); } end_c_loop(c, c_thread) } } Bests Last edited by syavash; July 19, 2014 at 08:45. |
||
July 19, 2014, 07:56 |
|
#7 |
Senior Member
|
I have corrected a syntax error in your code.
Code:
DEFINE_ADJUST(my_adjust,d) { Thread *t; cell_t c; FILE *fp; /* define a local pointer fp of type FILE */ fp = fopen("data.txt","w"); real x[ND_ND]; real x0y0, x1y0, x0y1, x2y0, x1y1, x0y2; real p00, p10, p01, p20, p11, p02; real source; // if it is not declared in the file scope thread_loop_c(t, d) { begin_c_loop(c, t) { C_CENTROID(x,c,t); x0y0 = 1; x1y0 = abs(x[0]); x0y1 = x[1]; x2y0 = pow(x[0],2.0); x1y1 = abs(x[0])*x[1]; x0y2 = pow(x[1],2.0); // define the polinomial coefficients p00 = -0.53825458; p10 = 73.53581559; p01 = 774.0253865; p20 = -1931.148778; p11 = 3762.806598; p02 = -85702.19479; // Define the momentum source source = p00*x0y0+p10*x1y0+p01*x0y1+p20*x2y0+p11*x1y1+p02*x0y2; fprintf (fp, "%f%f%f%f\n", x[0],x[1],x[2],source); } end_c_loop(c,t) // you have missed this line, i.e., a right curly bracket } } |
|
July 22, 2014, 10:28 |
|
#8 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
Thanks, syavash and blackmask.
As blackmask pointed out, there was a mistake regarding the domain. I fixed this, but I'd still like to be able to read the actual values computed by my UDF. I think that you did 95% of the job for me, but I was still unable to do the remaining 5% without further help 1) Where do I save the DEFINE_ADJUST function? 2) How do I link it to fluent? and, lastly, in line 5-6: FILE *fp; /* define a local pointer fp of type FILE */ fp = fopen("data.txt","w"); The pointer to the file is fully defined as it is now. There is no need for further command, right? Again, thanks for your help. |
|
July 22, 2014, 10:31 |
|
#9 | |
Member
David
Join Date: Aug 2012
Posts: 48
Rep Power: 14 |
Quote:
|
||
July 23, 2014, 10:01 |
|
#10 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
Thank you. I'm trying to incorporate all your comments and will get back to you if needed!
|
|
July 24, 2014, 15:07 |
|
#11 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
Hello,
I have tried to compile the define_adjust function, but fluent gives an error when compiling (attached). I found out that the error is due to line 8 (fp = fopen("data.txt","w") because it gets compiled without any problem if I comment that line. Am I missing something? Any header file that should be included? Thanks for the help. #include "udf.h" #include "stdio.h" DEFINE_ADJUST(my_adjust,d) { Thread *t; cell_t c; FILE *fp; /* define a local pointer fp of type FILE */ fp = fopen("data.txt","w"); real x[ND_ND]; real x0y0, x1y0, x0y1, x2y0, x1y1, x0y2; real p00, p10, p01, p20, p11, p02; real source; // if it is not declared in the file scope thread_loop_c(t, d) { begin_c_loop(c, t) { C_CENTROID(x,c,t); x0y0 = 1; x1y0 = abs(x[0]); x0y1 = x[1]; x2y0 = pow(x[0],2.0); x1y1 = abs(x[0])*x[1]; x0y2 = pow(x[1],2.0); // define the polinomial coefficients p00 = -0.53825458; p10 = 73.53581559; p01 = 774.0253865; p20 = -1931.148778; p11 = 3762.806598; p02 = -85702.19479; // Define the momentum source source = p00*x0y0 + p10*x1y0 + p01*x0y1 + p20*x2y0 + p11*x1y1 + p02*x0y2; fprintf (fp, "%f%f%f%f\n", x[0],x[1],x[2],source); } end_c_loop(c,t) } } The error looks like that: ..\..\src\define_adjust.c(36) : error C2275: 'real' : illegal use of this type as an expression c:\program files\ansys inc\v145\fluent\fluent14.5.7\src\global.h(166) : see declaration of 'real' ..\..\src\define_adjust.c(36) : error C2146: syntax error : missing ';' before identifier 'x' ..\..\src\define_adjust.c(36) : error C2065: 'x' : undeclared identifier ..\..\src\define_adjust.c(36) : error C2109: subscript requires array or pointer type ..\..\src\define_adjust.c(37) : error C2275: 'real' : illegal use of this type as an expression c:\program files\ansys inc\v145\fluent\fluent14.5.7\src\global.h(166) : see declaration of 'real' ..\..\src\define_adjust.c(37) : error C2146: syntax error : missing ';' before identifier 'x0y0' ..\..\src\define_adjust.c(37) : error C2065: 'x0y0' : undeclared identifier ..\..\src\define_adjust.c(37) : error C2065: 'x1y0' : undeclared identifier ..\..\src\define_adjust.c(37) : error C2065: 'x0y1' : undeclared identifier ..\..\src\define_adjust.c(37) : error C2065: 'x2y0' : undeclared identifier ..\..\src\define_adjust.c(37) : error C2065: 'x1y1' : undeclared identifier ..\..\src\define_adjust.c(37) : error C2065: 'x0y2' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2275: 'real' : illegal use of this type as an expression c:\program files\ansys inc\v145\fluent\fluent14.5.7\src\global.h(166) : see declaration of 'real' ..\..\src\define_adjust.c(38) : error C2146: syntax error : missing ';' before identifier 'p00' ..\..\src\define_adjust.c(38) : error C2065: 'p00' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2065: 'p10' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2065: 'p01' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2065: 'p20' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2065: 'p11' : undeclared identifier ..\..\src\define_adjust.c(38) : error C2065: 'p02' : undeclared identifier |
|
July 25, 2014, 16:17 |
|
#13 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
Thanks for the hints, but it did not work. I still get the same error when compiling. Any other hint why it would not work?
To add to my previous description,I created a data.txt file and saved it in the same folder as the one containing the UDF. Is that correct? Am I missing any headers? Again, if I comment fp = fopen("data.txt","w"); the UDF would be compiled without any error, which makes me doubt because the syntax is exactly the same as described in the UDF guide! I appreciate your help! |
|
July 27, 2014, 13:21 |
|
#15 |
New Member
Noris
Join Date: Jun 2014
Posts: 11
Rep Power: 12 |
I compiled it.
I tried to interpret it after reading your post, but the same error is displayed. |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[swak4Foam] groovyBC in openFOAM-2.0 for parabolic velocity bc | ofslcm | OpenFOAM Community Contributions | 25 | March 6, 2017 11:03 |
UDF for source term in momentum equation | Enrico | FLUENT | 9 | May 30, 2014 12:34 |
Problem in UDF time dependent vloumetric heat source | eng_yasser_2020 | Fluent UDF and Scheme Programming | 0 | March 30, 2014 09:07 |
Can Anyone help me to write a momentum source udf ? | Ozora | Fluent UDF and Scheme Programming | 0 | December 11, 2013 04:44 |
DxFoam reader update | hjasak | OpenFOAM Post-Processing | 69 | April 24, 2008 02:24 |