
[Sponsors] 
May 17, 2020, 08:26 
[UDF] error C2106: '=': left operand ...

#1 
Member
mCiFlDk
Join Date: Feb 2020
Posts: 34
Rep Power: 2 
Hi everyone,
I have a simple rectangular domain, where I'm adding a force in X direction using a UDF. The UDF depends on X and Y but is straight forward in the rest of elements (Fig. 1). The code is just below and everything is calculated inside a DEFINE_SOURCE macros, where f_b is the body force exerted in the X component and what I'm trying to do is running it inside the limits (that's why I included an ifelse). Code:
#include "udf.h" #include "math.h" #include "sg_udms.h" #define thrust 0.009549 #define xRef 0.5 #define yRef 0 #define pi 3.141592 enum { l, h, Fx }; DEFINE_SOURCE(body_force, c, t, dS, eqn) { real x; real f_b; l = 0.005*pow(thrust/0.015, 1/3); h = 0.0013*pow(thrust/0.015, 1/3); begin_f_loop(f, t) { if (x[0]<0.5  x[0]>l  x[1]>h) f_b = 0; C_UDMI(c, t, Fx) = f_b; else { F_CENTROID(x, f, t); f_b = pi*thrust/(4*l*h) * sin(pi*(x[0]xRef)/l) * cos(pi*(x[1]yRef)/(2*h)); C_UDMI(c, t, Fx) = f_b; } } end_f_loop(f, t) dS[eqn] = pi*thrust/(4*l*h); return f_b; } I've been a couple of hours solving several errors that appeared in the UDF, but I'm not able to figure out what is this one. When I compile it in Fluent I obtain the following errors: Code:
..\..\src\UDF_Yoon.c(23): error C2106: '=': left operand must be lvalue ..\..\src\UDF_Yoon.c(24): error C2106: '=': left operand must be lvalue ..\..\src\UDF_Yoon.c(26): error C2065: 'f': undeclared identifier ..\..\src\UDF_Yoon.c(28): error C2109: subscript requires array or pointer type ..\..\src\UDF_Yoon.c(31): error C2181: illegal else without matching if ..\..\src\UDF_Yoon.c(33): error C2109: subscript requires array or pointer type ..\..\src\UDF_Yoon.c(33): error C2065: 'f': undeclared identifier ..\..\src\UDF_Yoon.c(34): error C2109: subscript requires array or pointer type ..\..\src\UDF_Yoon.c(34): error C2198: 'sin': too few arguments for call ..\..\src\UDF_Yoon.c(34): error C2198: 'cos': too few arguments for call I'd appreciate a lot your help. Regards Last edited by mCiFlDk; May 17, 2020 at 11:28. 

May 17, 2020, 13:30 
Bugs

#2 
Senior Member

The code is riddled with syntactical as well as logical bugs.
1. You are trying to save output of an equation that will return a real value to an enumeration variable. That's not doable. So, either remove l and h from enum and define those as real or define two new variable as real and assign values of first two equations to those variables. 2. DEFINE_SOURCE is automatically executed in a cell loop, i.e., function is executed at each cell. There could be a need to loop over faces of each cell to determine values of some function but apparently not in your case. So, remove face loop. 3. x is a real variable but it needs to have some value assigned before you can use it for comparison. So, you need to use C_CENTROID before comparing values of coordinates in conditional statement. 4. C_UDMI needs initialization but you can do that using Patch option as well. If you are not doing that, then you need to use a loop, NOT within DEFINE_SOURCE, to initialize UDMs. Also note that as per your code, you need 3 UDMs since value of Fx is 2. If this is the complete code, then you don't need 3 UDMs. So, use l in place of Fx or simply use 0 as third argument of C_UDMI. 5. dS[eqn] is supposed to be firstorder derivative of source term with respect to the field variable to which source is being applied. Of course, this UDF can be hooked for any field, but most likely you want to apply this to one of the velocity component or may be to all of the components. That means, dS[eqn] should be derivative of the source term with respect to that component. But, if you look at the source term, it does not appear to be a function of any velocity. That means, dS[eqn] should be 0. Lastly, not a bug but the usage of the UDF. You don't need this UDF. Since the source value is not changing from iteration to iteration or with time, you can precalculate the values and apply a profile for the source.
__________________
Regards, Vinerm PM to be used if and only if you do not want something to be shared on the Forum 

May 18, 2020, 07:38 

#3  
Member
mCiFlDk
Join Date: Feb 2020
Posts: 34
Rep Power: 2 
You were completely right. However, thanks to your detailed explanation you made me understand every of the steps and I fixed it, so first of all: thank you SO much, I appreciate a lot your time.
I looked for every step you told me, changed other several tiny things and I'm starting to understand how ANSYS UDFs work thanks to explaining it, and not only saying: this is bad or that is wrong. You made one CFD lover very happy Quote:
Quote:
Thank you so much again. Regards Code:
FINAL 2D DEFINE_SOURCE: DEFINE_SOURCE(body_force, c, t, dS, eqn) { real x[ND_ND]; real f_b; C_CENTROID(x, c, t); if (x[0]<xRef  x[0]>l  x[1]>h) f_b = 0; else { f_b = pi*thrust/(4*l*h) * sin(pi*(x[0]xRef)/l) * cos(pi*(x[1]yRef)/(2*h)); } C_UDMI(c, t, Fx) = f_b; dS[eqn] = 0; return f_b; } 

May 18, 2020, 08:06 
Patch and Version

#4 
Senior Member

Patch is initialization but localized to a particular region or for a particular variable, in this case for UDM over whole domain.
If you want the code to work for 2D as well as 3D, you will have to modify it further since you can't compare zcoordinates in 2D. In any case, profile would be faster and you won't have to worry about serial or parallel.
__________________
Regards, Vinerm PM to be used if and only if you do not want something to be shared on the Forum 

May 18, 2020, 11:34 

#5 
Member
mCiFlDk
Join Date: Feb 2020
Posts: 34
Rep Power: 2 

May 18, 2020, 11:40 
Profile

#6 
Senior Member

It is a static, text file and has no loop or anything. Refer
https://www.afs.enea.it/project/nept...ug/node263.htm
__________________
Regards, Vinerm PM to be used if and only if you do not want something to be shared on the Forum 

May 18, 2020, 12:17 

#7  
Member
mCiFlDk
Join Date: Feb 2020
Posts: 34
Rep Power: 2 
Quote:


Thread Tools  Search this Thread 
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
[swak4Foam] GroovyBC the dynamic cousin of funkySetFields that lives on the suburb of the mesh  gschaider  OpenFOAM Community Contributions  300  October 29, 2014 18:00 
Undeclared Identifier Errof UDF  SteveGoat  Fluent UDF and Scheme Programming  7  October 15, 2014 07:11 
checking the system setup and Qt version  vivek070176  OpenFOAM Installation  22  June 1, 2010 12:34 
[swak4Foam] groovyBC: problems compiling: "flex: not found" and "undefined reference to ..."  sega  OpenFOAM Community Contributions  12  February 17, 2010 09:30 
error while compiling the USER Sub routine  CFD user  CFX  3  November 25, 2002 15:16 