CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   (ask) how to create UDF for inlet velocity profile (https://www.cfd-online.com/Forums/fluent-udf/86275-ask-how-create-udf-inlet-velocity-profile.html)

miladspa March 18, 2017 04:50

help me
 
hello
im modeling nanofluid in two-phase mixture model,for Boundary conditions, velocity inlet,
I have defined two speeds, one for liquids, one for nanoparticles, but there's a problem, to define the fluid velocity, the Reynolds number is used, but to find out velocity nanoparticles, have problems, because it is solid nanoparticles viscosity no, it will not be used Reynolds
How do I find the velocity inlet nanoparticles?
The two-phase mixture model
Water is fluid and nanoparticles al2o3

cfd_worker99 April 3, 2018 19:31

Hi Amir,
Your replies to previous posts are quite helpful.
Currently, I am working on a project which is basically a cylindrical domain. The properties (density, thermal conductivity and . . . Alas. . . the heat capacity) of the single phase medium need to change as a function of radial distance from the center and as a function of time. Would it be possible to this through the UDF. I tried writing a UDF for the thermal conductivity but was not very successful:

#include "udf.h"

DEFINE_PROPERTY(thermal_conduct,c,t)
{
real ktc;
real t = CURRENT_TIME
float x[3]; /* Position vector*/


if (t > 3600.)
if x[1]*x[2]<2000
ktc = 2.2e-1;
else
ktc = 1.5e-1;
else
if x[1]*x[2]<2000
ktc = 6.5e-1;
else
ktc = 8.2 e-1;
return ktc;
}

pakk April 4, 2018 02:44

You forgot how to properly calculate distance. It is not the square root of x*y, but the square root of x*x+y*y.

cfd_worker99 April 4, 2018 14:44

Thanks pakk.
Yeah you are correct.
But I think there is something else wrong with the syntax as well because I keep getting this warning:
Error: The UDF library you are trying to load (libudf) is not compiled for 3ddp on the current platform (win64).

pakk April 5, 2018 08:31

Check the error messages that show up on the screen after you click 'build'.

Probably it gives an error on line 7, because you forgot a semicolon at the end of line 6.
There might be more errors, just look what you see on your screen, that makes debugging a lot easier.

cfd_worker99 April 16, 2018 18:21

Thanks Pakk.
I modified the code to the following:
/************************************************** ***********************
UDF for changing soil conductivity with radial distance and time
************************************************** ***********************/
#include "udf.h" /* must be at the beginning of every UDF you write */
#include "sg.h"
#include "sg_mphase.h"
#include "flow.h"
#include "mem.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#include <unsteady.h>
DEFINE_PROPERTY(thermal_conduct,c,t)
{
real ktc;
real time = CURRENT_TIME;
​float xa = x[0]; /* represents x-coordinate */​
float xb = x[​1​]; ​/* represents y-coordinate */​ ​
if (time > 3600)
if xa*xa+xb*xb<1000*1000
ktc = 0.22;
else
ktc = 0.15;
else
if xa*xa+xb*xb<1000*1000
ktc = 0.65;
else
ktc = 0.82;
return ktc;
}

I know get the following errors (which is longer than any grocery list I have ever seen ;-D)
The declaration of the position vectors definitely needs correction. Can you help, Pakk?:


UDF_April.c

..\..\src\UDF_April.c(17): error C3872: '0x2039': this character is not allowed in an identifier

..\..\src\UDF_April.c(17): error C2065: 'ƒ?<float': undeclared identifier

..\..\src\UDF_April.c(17): error C2146: syntax error: missing ';' before identifier 'xa'

..\..\src\UDF_April.c(17): error C2065: 'xa': undeclared identifier

..\..\src\UDF_April.c(17): error C2065: 'x': undeclared identifier

..\..\src\UDF_April.c(17): error C2109: subscript requires array or pointer type

..\..\src\UDF_April.c(18): error C2065: 'ƒ?<': undeclared identifier

..\..\src\UDF_April.c(18): error C2143: syntax error: missing ';' before 'type'

..\..\src\UDF_April.c(18): error C2065: 'x': undeclared identifier

..\..\src\UDF_April.c(18): error C3872: '0x2039': this character is not allowed in an identifier

..\..\src\UDF_April.c(18): error C2065: 'ƒ?<1ƒ?<': undeclared identifier

..\..\src\UDF_April.c(18): error C2109: subscript requires array or pointer type

..\..\src\UDF_April.c(18): error C2146: syntax error: missing ';' before identifier 'ƒ?<'

..\..\src\UDF_April.c(19): error C2065: 'ƒ?<': undeclared identifier

..\..\src\UDF_April.c(19): error C2143: syntax error: missing ';' before 'type'

..\..\src\UDF_April.c(19): error C2065: 'x': undeclared identifier

..\..\src\UDF_April.c(19): error C2109: subscript requires array or pointer type

..\..\src\UDF_April.c(19): error C3872: '0x2039': this character is not allowed in an identifier

..\..\src\UDF_April.c(19): error C2146: syntax error: missing ';' before identifier 'ƒ?<'

..\..\src\UDF_April.c(20): error C2065: 'ƒ?<': undeclared identifier

..\..\src\UDF_April.c(20): error C2143: syntax error: missing ';' before 'if'

..\..\src\UDF_April.c(21): error C2061: syntax error: identifier 'xa'

..\..\src\UDF_April.c(25): error C2181: illegal else without matching if

..\..\src\UDF_April.c(26): error C2061: syntax error: identifier 'xa'

..\..\src\UDF_April.c(28): error C2181: illegal else without matching if

pakk April 18, 2018 03:34

Which text editor are you using to edit your UDF? It looks like there are strange symbols in the code...

Additionally:
  • You don't have to include so many files. I believe only "udf.h" is sufficient, that one should include the other files.
  • You use variable "x", which should be the coordinates of the cell. However, you never tell the compiler that "x" is a variable, and the "x" should be filled with the coordinates of the cell.
  • For style: use brackets () around if-tests.
Code:

if xa*xa+xb*xb<1000*1000  /* not this */
if (xa*xa+xb*xb<1000*1000)  /* but this */

  • Use curly brackets around what the if should do
Code:

if (time > 3600)  /*not this */
if (time > 3600) { ... }  /* but this */

  • Use indentation in your code
Code:

if (time > 3600) {
 if (xa*xa+xb*xb<1000*1000) {
  ktc = 0.22;
 } else {
  ktc = 0.15;
 }
} else {
 if  (xa*xa+xb*xb<1000*1000) {
  ktc = 0.65;
 } else {
  ktc = 0.82;
 }
 return ktc;
}

This will make it much easier to visually understand your code, and see errors. Especially with nested if's that you have.

Pom Sittisak April 19, 2018 00:46

Error: received a fatal signal (Segmentation fault) in UDF
 
Hello All CFD member,

I try to find the cause of this error when in try to put below UDF in my problem.

Error: received a fatal signal (Segmentation fault).

Error: received a fatal signal (Segmentation fault).
Error Object: #f

Here my UDF, aim to bring the CO (species mass fraction) variable data from back to front of the long cylinder tube.

#include "udf.h"

//Global variable declaration
real co;

//Execute every timestep
DEFINE_EXECUTE_AT_END(Fanback)
{
//Specific the domain ** 1 = mixture domain
Domain *mix_domain = Get_Domain(1);
Thread *mix_thread;
Thread **pt;

//Initialize the variable
co = 0;

#if !RP_HOST
mp_thread_loop_f(mix_thread, mix_domain, pt)
{
face_t c;
// pt[0] ** number [0] is co phase index (co in this case)
begin_f_loop(c, pt[0])
{
// Check if it is a outlet thread (thread id = 5) (Lookup from region list)
if (THREAD_ID(mix_thread)==5){
// Loop and sum up the flux for each face
co =F_YI(c,pt[0],1);

}


}
end_f_loop(c, pt[0])


}

#endif /* !RP_HOST */
node_to_host_real_1(co);

#if RP_HOST
Message("CO Concentration at FAN BACK = %f\n",co);
#endif /* !RP_NODE */


}

// Creating profile for returning inlet
DEFINE_PROFILE(co_outlet,t,i)
{
face_t f;
begin_f_loop(f,t)
{
//Apply flowout variable to the face
F_PROFILE(f,t,i) = co;
}
end_f_loop(f,t)
}

I can pass the complied and Hook the run the simulation using these UDF but the error is shown when first the time step is finished.

AlexanderZ April 19, 2018 01:48

Code:

DEFINE_PROFILE(co_outlet,t,i)
{
face_t f;
begin_f_loop(f,t)
{
//Apply flowout variable to the face
F_PROFILE(f,t,i) = co;
}
end_f_loop(f,t)
}

co is not defined on the first timestep

best regards

Pom Sittisak April 19, 2018 05:47

Dear AlexanderZ

Thank you very much ..for your reply.

//Global variable declaration
real co;

I wrote before DEFINE_EXECUTE_AT_END(FANBACK)

However, this UDF was passed Complied process and load libudf.dll for Hook process. I can run the simulation but only 1 timestep..It seems like the variable can not transfer to second step.

AlexanderZ April 19, 2018 22:40

co has nothing inside (or any random value) on the first time step
so you apply random as a boundary condition

best regards

Pom Sittisak April 20, 2018 04:20

I have put the initial value of CO in Solution initialization as 0.00012. but still error. please advise me.

AlexanderZ April 22, 2018 20:33

I recommend you to put Message function in your code and debug line by line, which functions gives error

best regards

cfd_worker99 May 21, 2018 18:20

1 Attachment(s)
Hi Pakk,
Thanks for your help. Sorry for the delay in getting back to you.
I modified the code to the following based on your guidance. However, I am still getting a lot of errors. Could you please help.

:
/************************************************** ***********************
UDF for changing soil conductivity
************************************************** ***********************/
#include "udf.h" /* must be at the beginning of every UDF you write */
DEFINE_PROPERTY(thermal_conduct,c,t)
{
real ktc;
real time = CURRENT_TIME;
​float x[0]; /* represents x-coordinate */​
float x[​1​]; ​/* represents y-coordinate */​ ​
if (time > 3600) {
if (xa*xa+xb*xb<1000*1000) {
ktc = 0.22;
} else {
ktc = 0.15;
}
} else {
if (xa*xa+xb*xb<1000*1000) {
ktc = 0.65;
} else {
ktc = 0.82;
}
return ktc;
}



Quote:

Originally Posted by pakk (Post 689300)
Which text editor are you using to edit your UDF? It looks like there are strange symbols in the code...

Additionally:
  • You don't have to include so many files. I believe only "udf.h" is sufficient, that one should include the other files.
  • You use variable "x", which should be the coordinates of the cell. However, you never tell the compiler that "x" is a variable, and the "x" should be filled with the coordinates of the cell.
  • For style: use brackets () around if-tests.
Code:

if xa*xa+xb*xb<1000*1000  /* not this */
if (xa*xa+xb*xb<1000*1000)  /* but this */

  • Use curly brackets around what the if should do
Code:

if (time > 3600)  /*not this */
if (time > 3600) { ... }  /* but this */

  • Use indentation in your code
Code:

if (time > 3600) {
 if (xa*xa+xb*xb<1000*1000) {
  ktc = 0.22;
 } else {
  ktc = 0.15;
 }
} else {
 if  (xa*xa+xb*xb<1000*1000) {
  ktc = 0.65;
 } else {
  ktc = 0.82;
 }
 return ktc;
}

This will make it much easier to visually understand your code, and see errors. Especially with nested if's that you have.


pakk May 22, 2018 03:02

You use variable "xa", but you never told Fluent what "xa" is.
You use variable "xb", but you never told Fluent what "xb" is.

Look in the Fluent manual at C_CENTROID.

cfd_worker99 May 24, 2018 17:43

1 Attachment(s)
Hi Pakk,
Thanks for the reply.
So I looked at the manual and tried to incorporate it. I also got rid of xa and xb and just used x[0] and x[1] directly
It did remove some errors but there are others :(
Could you please tell me what I am missing? I would be most thankful for your help.
:)

Here is the modified code:
/************************************************** ***********************
UDF for changing soil conductivity
************************************************** ***********************/
#include "udf.h" /* must be at the beginning of every UDF you write */
DEFINE_PROPERTY(thermal_conduct,c,t)
{
real x[ND_ND];
real ktc;
real time = CURRENT_TIME;
​float x[0]; /* represents x-coordinate */​
float x[​1​]; ​/* represents y-coordinate */​ ​
C_CENTROID(x,c,t)
if (time > 3600) {
if (x[0]*x[0]+x[1]*x[1]<1000*1000) {
ktc = 0.22;
} else {
ktc = 0.15;
};
} else
if (x[0]*x[0]+x[1]*x[1]<1000*1000)
{
ktc = 0.65;
} else {
ktc = 0.82;
}
return ktc;
}



>>>>Here are the warnings from Fluent.


UDF_propertyvariation_May.c
..\..\src\UDF_propertyvariation_May.c(10): error C3872: '0x2039': this character is not allowed in an identifier
..\..\src\UDF_propertyvariation_May.c(10): error C2065: 'ƒ?<float': undeclared identifier
..\..\src\UDF_propertyvariation_May.c(10): error C2146: syntax error: missing ';' before identifier 'x'
..\..\src\UDF_propertyvariation_May.c(11): error C2065: 'ƒ?<': undeclared identifier
..\..\src\UDF_propertyvariation_May.c(11): error C2143: syntax error: missing ';' before 'type'
..\..\src\UDF_propertyvariation_May.c(11): error C3872: '0x2039': this character is not allowed in an identifier
..\..\src\UDF_propertyvariation_May.c(11): error C2065: 'ƒ?<1ƒ?<': undeclared identifier
..\..\src\UDF_propertyvariation_May.c(11): error C2057: expected constant expression
..\..\src\UDF_propertyvariation_May.c(11): error C2466: cannot allocate an array of constant size 0
..\..\src\UDF_propertyvariation_May.c(11): error C2371: 'x': redefinition; different basic types
..\..\src\UDF_propertyvariation_May.c(7): note: see declaration of 'x'
..\..\src\UDF_propertyvariation_May.c(11): error C2133: 'x': unknown size
..\..\src\UDF_propertyvariation_May.c(11): error C2146: syntax error: missing ';' before identifier 'ƒ?<'
..\..\src\UDF_propertyvariation_May.c(12): error C2109: subscript requires array or pointer type

Quote:

Originally Posted by pakk (Post 693127)
You use variable "xa", but you never told Fluent what "xa" is.
You use variable "xb", but you never told Fluent what "xb" is.

Look in the Fluent manual at C_CENTROID.


pakk May 27, 2018 10:19

Code:

real x[ND_ND];
​float x[0]; /* represents x-coordinate */​
float x[​1​]; ​/* represents y-coordinate */​ ​

You are giving three different definitions for x.

The latter two are not necessary (and wrong).

cfd_worker99 June 4, 2018 16:27

1 Attachment(s)
Thank you so much Pakk.
I deleted these two and that got rid of all but one of the errors. I just get this warning:
UDF_propertyvariation_4June.c
..\..\src\UDF_propertyvariation_4June.c(10): error C3872: '0x2039': this character is not allowed in an identifier

Do you know what's wrong?
The code is attached for reference.
Thanks again for being super helpful.


Quote:

Originally Posted by pakk (Post 693705)
Code:

real x[ND_ND];
​float x[0]; /* represents x-coordinate */​
float x[​1​]; ​/* represents y-coordinate */​ ​

You are giving three different definitions for x.

The latter two are not necessary (and wrong).


pakk June 5, 2018 03:11

If I open your file, the tenth line in my editor is:
Code:

​C_CENTROID(x,c,t);

Notice the strange symbols in the beginning... They should not be there, and Fluent complains about them.



As I asked on 18 April: which text editor are you using? The one you use now ignores these strange symbols, which is very strange.

cfd_worker99 June 5, 2018 17:43

2 Attachment(s)
Thanks Pakk.
I am using Notepad++ v7.5.6
Oddly enough, I don't see these symbols in the notepad file (as shown in the screen shot).
Am I using the wrong text editor? Please refer to me another suitable text editor.


Quote:

Originally Posted by pakk (Post 694606)
If I open your file, the tenth line in my editor is:
Code:

​C_CENTROID(x,c,t);

Notice the strange symbols in the beginning... They should not be there, and Fluent complains about them.



As I asked on 18 April: which text editor are you using? The one you use now ignores these strange symbols, which is very strange.



All times are GMT -4. The time now is 23:57.