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/)
-   -   UDF "Define_On_Demand" error: "Segmentation Violation" (https://www.cfd-online.com/Forums/fluent-udf/147994-udf-define_on_demand-error-segmentation-violation.html)

dinhanh February 3, 2015 03:23

UDF "Define_On_Demand" error: "Segmentation Violation"
 
2 Attachment(s)
Hi, Fluent UDF user,

This is the same question as previous thread: Hooking On_Demand error.
I re-wrote new code, the compiler and loading UDF is OK. But when the code hooked, It showed the proplem as in the picture below (pic.12 is the compiler successful, pic.11 is the error).

Could you please help me solve this problem??

Thank you very much.

#include "udf.h"
#include "math.h"

DEFINE_ON_DEMAND(contour)
{
Domain *d;
Thread *t;
cell_t c;
Node *v;
d=Get_Domain(1);
int i,j,k,n,m;
real dx,dy,dz,delta,Cdes=0.65,Lt,Ldes;
thread_loop_c(t,d) /*loops over all cell threads in domain*/
{
begin_c_loop(c,t) /* loops over cells in a cell thread */
{
c_node_loop(c,t,n) /* loops over node in a cell thread */
{
v=C_NODE(c,t,n);
NODE_MARK(v)=0;
}
}
end_c_loop(c,t)

begin_c_loop(c,t) /* loops over cells in a cell thread */
{
k=C_NNODES(c,t);
real x[k],y[k],z[k];
real delx1=0,dely1=0,delz1=0;
for (m=0;m<=k;m++)
{
c_node_loop(c,t,n) /* loops over node in a cell thread */
{
v=C_NODE(c,t,n);
x[m]=NODE_X(v);
y[m]=NODE_Y(v);
z[m]=NODE_Z(v);
if (NODE_MARK(v)==0)
NODE_MARK(v)=1;
}
}
for (i=0;i<=k;i++)
{
for (j=0;j<=k;j++)
{
dx=MAX(delx1,fabs(x[i]-x[j]));
dy=MAX(dely1,fabs(y[i]-y[j]));
dz=MAX(delz1,fabs(z[i]-z[j]));
delta=MAX(dx,dy);
delta=MAX(delta,dz);
}
}
Lt=sqrt(C_K(c,t))/0.09/C_O(c,t);
Ldes=Cdes*delta;
if(Lt>Ldes)
{
C_UDMI(c,t,1)=1;
}
if(Lt<=Ldes)
{
C_UDMI(c,t,1)=0;
}
}
end_c_loop(c,t)
}
}

pakk February 3, 2015 03:36

I would suggest to comment out some parts, so you can determine where in the code the problem is. (Like this.)

dinhanh February 3, 2015 07:05

Thank you, Pakk,

But I could not get your answer, Please help, this one very importance for me!!!

pakk February 3, 2015 07:23

Your code has a mistake. Remove half of the code, and see if you still get an error. If you get the error, then the mistake is in the remaining code. If you don't get an error, the mistake is in the part you removed.

Repeat this until you found the error.

upeksa February 3, 2015 09:46

I don't really know what are you trying, you have a lot of useless code here.

It works now anyway.

#include "udf.h"
#include "math.h"

DEFINE_ON_DEMAND(contour)
{
Domain *d=Get_Domain(1);
Thread *t;
cell_t c;
Node *v;
int i,j,k,n,m;
real dx,dy,dz,delta,Cdes=0.65,Lt,Ldes;
real *x, *y, *z;
real delx1=0.,dely1=0.,delz1=0.;

thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
CLEAR_NODE_VISITED(v);
}
}
end_c_loop(c,t)

begin_c_loop(c,t)
{
k=C_NNODES(c,t);
x=(real *)malloc(k*sizeof(real));
y=(real *)malloc(k*sizeof(real));
z=(real *)malloc(k*sizeof(real));

c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
x[n]=NODE_X(v);
y[n]=NODE_Y(v);
z[n]=NODE_Z(v);
if (!(NODE_IS_VISITED(v)))
SET_NODE_VISITED(v);
}

for (i=0;i<=k;i++)
{
for (j=0;j<=k;j++)
{
dx=MAX(delx1,fabs(x[i]-x[j]));
dy=MAX(dely1,fabs(y[i]-y[j]));
dz=MAX(delz1,fabs(z[i]-z[j]));
delta=MAX(dx,dy);
delta=MAX(delta,dz);
}
}

Lt=sqrt(C_K(c,t))/0.09/C_O(c,t);
Ldes=Cdes*delta;

if(Lt>Ldes)
C_UDMI(c,t,1)=1;

if(Lt<=Ldes)
C_UDMI(c,t,1)=0;
free(x);
free(y);
free(z);
}
end_c_loop(c,t)
}
}

dinhanh February 3, 2015 10:09

Thank you so much, upeksa,

I am trying to plot the contour in which the region of LES will be 0 and the region of RAN will be 1. I am using DES turbulence model. So in order to do thay, I need to calculate the max grid size of each cell, and compear that one to lenght scale of k-omega model. That is what I am doing now.

upeksa February 3, 2015 10:28

Quote:

Originally Posted by dinhanh (Post 530175)
Thank you so much, upeksa,

I am trying to plot the contour in which the region of LES will be 0 and the region of RAN will be 1. I am using DES turbulence model. So in order to do thay, I need to calculate the max grid size of each cell, and compear that one to lenght scale of k-omega model. That is what I am doing now.

Did you know that you can use the macro C_VOLUME(c,t) to check the cell volume of each cell?

dinhanh February 3, 2015 10:32

Quote:

Originally Posted by upeksa (Post 530180)
Did you know that you can use the macro C_VOLUME(c,t) to check the cell volume of each cell?

Yes, I known. But what I need here is the max grid size, not the the vollume of cell. I mean, at each cell, I need to find delta=max(dx,dy,dz)

dinhanh February 3, 2015 22:01

Quote:

Originally Posted by upeksa (Post 530170)
I don't really know what are you trying, you have a lot of useless code here.

It works now anyway.

#include "udf.h"
#include "math.h"

DEFINE_ON_DEMAND(contour)
{
Domain *d=Get_Domain(1);
Thread *t;
cell_t c;
Node *v;
int i,j,k,n,m;
real dx,dy,dz,delta,Cdes=0.65,Lt,Ldes;
real *x, *y, *z;
real delx1=0.,dely1=0.,delz1=0.;

thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
CLEAR_NODE_VISITED(v);
}
}
end_c_loop(c,t)

begin_c_loop(c,t)
{
k=C_NNODES(c,t);
x=(real *)malloc(k*sizeof(real));
y=(real *)malloc(k*sizeof(real));
z=(real *)malloc(k*sizeof(real));

c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
x[n]=NODE_X(v);
y[n]=NODE_Y(v);
z[n]=NODE_Z(v);
if (!(NODE_IS_VISITED(v)))
SET_NODE_VISITED(v);
}

for (i=0;i<=k;i++)
{
for (j=0;j<=k;j++)
{
dx=MAX(delx1,fabs(x[i]-x[j]));
dy=MAX(dely1,fabs(y[i]-y[j]));
dz=MAX(delz1,fabs(z[i]-z[j]));
delta=MAX(dx,dy);
delta=MAX(delta,dz);
}
}

Lt=sqrt(C_K(c,t))/0.09/C_O(c,t);
Ldes=Cdes*delta;

if(Lt>Ldes)
C_UDMI(c,t,1)=1;

if(Lt<=Ldes)
C_UDMI(c,t,1)=0;
free(x);
free(y);
free(z);
}
end_c_loop(c,t)
}
}

Dear upeksa,
I Tried to use your code and modified a little. But It still did not work. The same error appeared. I dont know why It have no error in your case

#include "udf.h"
#include "math.h"

DEFINE_ON_DEMAND(contour)
{
Domain *d=Get_Domain(1);
Thread *t;
cell_t c;
Node *v;
int i,j,k,n;
real delta,Cdes=0.65,Lt,Ldes;
real *x, *y, *z;
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
CLEAR_NODE_VISITED(v);
}
}
end_c_loop(c,t)

begin_c_loop(c,t)
{
k=C_NNODES(c,t);
x=(real *)malloc(k*sizeof(real));
y=(real *)malloc(k*sizeof(real));
z=(real *)malloc(k*sizeof(real));
real dx=0.,dy=0.,dz=0.;
c_node_loop(c,t,n)
{
v=C_NODE(c,t,n);
x[n]=NODE_X(v);
y[n]=NODE_Y(v);
z[n]=NODE_Z(v);
if (!(NODE_IS_VISITED(v)))
SET_NODE_VISITED(v);
}

for (i=0;i<=k;i++)
{
for (j=0;j<=k;j++)
{
dx=MAX(dx,fabs(x[i]-x[j]));
dy=MAX(dy,fabs(y[i]-y[j]));
dz=MAX(dz,fabs(z[i]-z[j]));
delta=MAX(dx,dy);
delta=MAX(delta,dz);
}
}

Lt=sqrt(C_K(c,t))/0.09/C_O(c,t);
Ldes=Cdes*delta;

if(Lt>Ldes)
C_UDMI(c,t,1)=1;
if(Lt<=Ldes)
C_UDMI(c,t,1)=0;
free(x);
free(y);
free(z);
}
end_c_loop(c,t)
}
}

dinhanh February 4, 2015 02:13

I also tried with the more simple code, But still got same error.
So I dont know what happened:(

#include "udf.h"
#include "math.h"
#include "stdlib.h"
#include "malloc.h"

DEFINE_ON_DEMAND(contour)
{
Domain *d=Get_Domain(1);
Thread *t;
cell_t c;
real delta,Cdes=0.65,Lt,Ldes;
thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
delta=pow(C_VOLUME(c,t),1./3);
Lt=sqrt(C_K(c,t))/0.09/C_O(c,t);
Ldes=Cdes*delta;

if(Lt>Ldes)
C_UDMI(c,t,1)=1;
if(Lt<=Ldes)
C_UDMI(c,t,1)=0;
}
end_c_loop(c,t)
}
}

pakk February 4, 2015 04:24

Do you have two two user defined memories (UDMS) defined in Fluent?

dinhanh February 4, 2015 04:49

1 Attachment(s)
Quote:

Originally Posted by pakk (Post 530307)
Do you have two two user defined memories (UDMS) defined in Fluent?

Thank, pakk,

What do you mean?

upeksa February 4, 2015 05:28

I think something is wrong with C_O(c,t).

I have change it for C_T(c,t) for example and it worked.

dinhanh February 4, 2015 06:01

Quote:

Originally Posted by upeksa (Post 530324)
I think something is wrong with C_O(c,t).

I have change it for C_T(c,t) for example and it worked.

For me, even, I changed C_O(c,t) by 5, for example, still got same error.
And in the simple code upper, the same error appeared. But when I compiler UDF for vapor pressure, It ran well, no error.:(
So it seem to be have problem in my PC. I am using super-comp at University.
If you dont mind, Could you use example Fluent file, and use this UDF code to plot the data?
http://www.filedropper.com/desktop_61

Thank you

upeksa February 4, 2015 07:31

You haven't reserved UDF memories:

Define -> User-Defined -> Memory... Number of User-Defined Memory Locations =2

It will work

dinhanh February 4, 2015 09:34

Quote:

Originally Posted by upeksa (Post 530357)
You haven't reserved UDF memories:

Define -> User-Defined -> Memory... Number of User-Defined Memory Locations =2

It will work

Thank you very much, upeksa,

It work now. So the problem is the number of memory location. How can we determine how many memory location will be needed?

upeksa February 4, 2015 09:37

Quote:

Originally Posted by dinhanh (Post 530378)
Thank you very much, upeksa,

It work now. So the problem is the number of memory location. How can we determine how many memory location will be needed?

You used C_UDMI(c,t,0) and C_UDMI(c,t,1). 2 memory locations.

dinhanh February 4, 2015 09:41

From the UDF code, I used only one memory user define location with If loops
If (a<b) C_UDMI(c,t,1)=1
If (a>b) C_UDMI(c,t,1)=0

So I think only 1 location is required?

upeksa February 4, 2015 09:55

Quote:

Originally Posted by dinhanh (Post 530381)
From the UDF code, I used only one memory user define location with If loops
If (a<b) C_UDMI(c,t,1)=1
If (a>b) C_UDMI(c,t,1)=0

So I think only 1 location is required?

No, you still need 2.

if you only use 1 memory location whose number is, for example 11 (C_UDMI(c,t,11)), you will need in that case 12 memory locations.

If you are going to use only one memory location, use C_UDMI(c,t,0) instead.


All times are GMT -4. The time now is 09:39.