# UDF to modify gravity via M_gravity[ND_ND] or DEFINE_ADJUST

 Register Blogs Members List Search Today's Posts Mark Forums Read

 January 24, 2012, 11:26 UDF to modify gravity via M_gravity[ND_ND] or DEFINE_ADJUST #1 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 Hello, I have tried to modify the gravity vector in FLUENT. I need it to vary with altitude, but am having problems with the details how to accomplish this. Let's say that g = g(z), z is the altitude. Then it seems that one should use the DEFINE_ADJUST macro to modify M_gravity[ND_ND] I am unsure how to read M_gravity or how do I modify it. In the end, it should be altered to: M_gravity[0] = 0 M_gravity[1] = 0 M_gravity[2] = g(z) It was suggested that I could use DEFINE_SOURCE to add gravity as source I tried reading the help on this, but it is unclear how do I define the direction of gravity along z Does someone have experience with these... Thank you.

 January 24, 2012, 14:21 #2 Senior Member   duri Join Date: May 2010 Posts: 160 Rep Power: 8 M_gravity[2] takes only one value, adding in define adjust will take only the last value it won't work. Define_source should work and simple. #include "udf.h" DEFINE_SOURCE(gz_source,c,t,dS,eqn) { real x[ND_ND]; real source; C_CENTROID(x,c,t); // source = g(x)// gravity function of x cell centroid x=(x,y,z) source = source*C_R(c,t); dS[eqn] = 0.0; return source; } hook this to z-momentum source as gx and gy =0. Turn off gravity in operating conditions panel.

 January 24, 2012, 14:45 #3 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 Dear duri, thank you for your reply. You define gravity in every mesh cell... Do I need to loop over all cells? Could gravity be defined globally?

January 24, 2012, 18:59
#4
Senior Member

Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 193
Rep Power: 8
Define source should loop over all cells for you (no need to program a loop); gravity can be defined globally with the M_gravity[0,1,or 2] term -- it is not a cell-by-cell value.

ComputerGuy.

Quote:
 Originally Posted by jpo Dear duri, thank you for your reply. You define gravity in every mesh cell... Do I need to loop over all cells? Could gravity be defined globally?

 January 24, 2012, 19:11 #5 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 I would rather modify M_gravity... if I had a way to define M_gravity[2] as a function of z Is this possible?

January 24, 2012, 19:31
#6
Senior Member

Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 193
Rep Power: 8
No, I don't think so, otherwise it would not "global"

ComputerGuy.

p.s. I will have a look at your problem a bit later and let you know how to get it to converge. What does your geometry look like (simplify, if necessary).

Quote:
 Originally Posted by jpo I would rather modify M_gravity... if I had a way to define M_gravity[2] as a function of z Is this possible?

 January 24, 2012, 19:34 #7 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 just a straight pipe along z and gravity 9.81m/s2 against z mass-inlet and a pressure outlet Thank you for your time

January 25, 2012, 15:34
#8
Senior Member

Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 193
Rep Power: 8
OK, here's what I have so far

Code:
```#include "udf.h"
DEFINE_SOURCE(gravity_source,c,t,dS,eqn)
{
real gravity;
real source;
gravity = -9.81;
source = gravity*C_R(c,t);
return source;
}```
I created a 10m vertical (z-direction) pipe, with a mass inlet at the bottom (z=0) and a pressure outlet (P=0 Pa gauge) at the top (z=10). The material is water (rho=1000 kg/m3). I have used a low viscosity, so that we won't get additional pressure losses from friction, and set the mass flow to be quite low (0.01 kg/s) in a pipe with a radius of 1m (very large). I initialize the simulation close to the "exact" answer for pressure and velocities.

With gravity turned off, and the UDF above hooked to the momentum source, I get a ~1e5 Pa pressure at the bottom (z=0). This is perfect.

However the simulation isn't converging because the cells immediately adjacent to the inlet are predicted to have a very high velocity magnitude (10m/s). I suspect this has something to do with the way the boundary conditions there are handled. It's not clear to me why it's not predicting the correct behavior. I've tried a few different under-relaxation factor variants, as well as some other tricks. You might take this up with the folks at ANSYS. I would love to hear the answer.

Sorry I couldn't help more -- at least we figured out that the UDF "works"

ComputerGuy

Quote:
 Originally Posted by jpo just a straight pipe along z and gravity 9.81m/s2 against z mass-inlet and a pressure outlet Thank you for your time

 January 25, 2012, 17:49 #9 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 Dear ComputerGuy, Highlty appreciate the time and effort... I will look at the case again and will talk to the people in my group as well; our admin could send the case to ANSYS and we'll see what they say. Or perhaps it is easier to use CFX to achieve the above -jpo

January 25, 2012, 18:01
#10
Senior Member

Real Name :)
Join Date: Jan 2010
Location: United States
Posts: 193
Rep Power: 8
No prob -- wish this could've worked. I'd simply ask them about applying a user-defined function to gravity, as we've tried to do. It's a case-independent problem. Up to v13, there's no hook to gravity, so I'm a little suspicious that it may require a different treatment than the simple method I proposed.

I'm very curious about the project/requirement for g(z). I would think that unless you're dealing with huge geometries where the force of gravity does significantly change with height, you could use a fixed value (globally applied to all cells in a given time step). If it's a transient situation (a rocket going into space), and you want to see the effect of changing gravity, you could simply change the gravitational force with time...

Anyway, best of luck. I hope you can share the outcome here after you put it to rest.

ComputerGuy

Quote:
 Originally Posted by jpo Dear ComputerGuy, Highlty appreciate the time and effort... I will look at the case again and will talk to the people in my group as well; our admin could send the case to ANSYS and we'll see what they say. Or perhaps it is easier to use CFX to achieve the above -jpo

 January 25, 2012, 18:41 #11 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 Let me ask in my lab and see if we can get in touch with ANSYS... perhaps our prof can do that I'll be sure to write how we solved it Many thanks

 April 24, 2012, 16:49 #12 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 the proposed udf did work: #include "udf.h" DEFINE_SOURCE(gravity_source,c,t,dS,eqn) { real gravity; real source; gravity = -9.81; source = gravity*C_R(c,t); return source; } I have tried it with various constant gravity vectors and compared with identical cases without UDF, just enabled gravity with fluent menu. They matched in all parameters I looked at. Thus, I used the UDF to generate the variable gravity and it gave results we expected.

 June 20, 2012, 08:40 #13 New Member   Join Date: Jun 2012 Posts: 5 Rep Power: 6 Hello, I would like to know how did you charge the UDF gravity_source in the fluent graphical interface? Thank you.

 June 20, 2012, 08:54 #14 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 It would not be possible to be done via the graphical interface, rather, via a UDF

 June 20, 2012, 10:38 #15 New Member   Join Date: Jun 2012 Posts: 5 Rep Power: 6 Thanks for your reply. Can you explain me how did you do for the Fluent to recognize the value defined by gravity_source as gravity? And is it g[0] or g[1]? In my case, I need to use a UDMI to calculate the components g[0] and g[1]. After, I will use this acceleration to calculate the flow in the domain.

 June 20, 2012, 12:44 #16 Member   Join Date: Apr 2009 Posts: 93 Rep Power: 9 Previous posts above should help to explain... gravity_source in the UDF is recognized by Fluent as gravity along the z-axis

 June 21, 2012, 03:57 #17 New Member   Join Date: Jun 2012 Posts: 5 Rep Power: 6 I read almost all the topic, but I had not understood that. Thank you. In my problem, I need to set the gravity in x, y and z cell by cell. Could you give me a simple example of how I can do that? Thank you in advance

 June 21, 2012, 10:37 #18 Member   Payam D. Join Date: Aug 2011 Posts: 83 Blog Entries: 3 Rep Power: 7 You can also using the dT_CG(dt)[0]= ... for changing the CG location but you should use the 6DOF in your simulation.

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jpo Fluent UDF and Scheme Programming 3 January 24, 2012 10:10 NRD FLUENT 1 July 13, 2011 07:35 Brian FLUENT 0 November 1, 2005 16:17 jddmsh FLUENT 0 June 28, 2003 21:59 frederic FLUENT 0 February 7, 2000 08:51

All times are GMT -4. The time now is 18:30.