PLUTO
fargo_velocity.c
Go to the documentation of this file.
1 /* ///////////////////////////////////////////////////////////////////// */
2 /*!
3  \file
4  \brief Functions for computing/retrieving the mean aziumthal velocity.
5 
6  Collects various functions for computing, adding, subtracting and
7  retrieving the azimuthally-averaged velocity.
8  Depending on the macro ::FARGO_AVERAGE_VELOCITY (YES/NO) this is
9  done either numerically or analytically.
10 
11  \authors A. Mignone (mignone@ph.unito.it)\n
12  G. Muscianisi (g.muscianisi@cineca.it)
13 
14  \date Sept 12, 2012
15 */
16 /* ///////////////////////////////////////////////////////////////////// */
17 #include "pluto.h"
18 
19 /*! Defines a 2D array containing the azimuthally-averaged velocity.
20  When the average is done along the X2 direction (Cartesian and polar
21  geometries) the array should have size (NX3_TOT, NX1_TOT).
22  When the average is performed along the X3 direction (spherical geometry)
23  the array has dimensions (NX2_TOT, NX1_TOT) */
24 static double **wA;
25 /*static double **wAx1, **wAx2, **wAx3;*/ /* -- average orbital speed at
26  x1, x2 and x3 faces -- */
27 
28 /* ********************************************************************* */
29 void FARGO_ComputeVelocity(const Data *d, Grid *grid)
30 /*!
31  * Compute the mean orbital velocity as a 2D array by averaging
32  * for each pair (x,z)/(r,z)/(r,theta) (in Cartesian/polar/spherical)
33  * along the orbital coordinate y/phi/phi.
34  *
35  *********************************************************************** */
36 #if GEOMETRY != SPHERICAL
37  #define s j /* -- orbital direction index -- */
38 #else
39  #define s k
40 #endif
41 {
42  int i,j,k;
43  static int first_call = 1;
44  double *x1, *x2, *x3, th, w;
45  double ***vphi;
46 
47  x1 = grid[IDIR].x;
48  x2 = grid[JDIR].x;
49  x3 = grid[KDIR].x;
50 
51  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
52  if (wA == NULL){
53  wA = ARRAY_2D(NX3_TOT, NX1_TOT, double);
54 /*
55  wAx1 = ARRAY_2D(NX3_TOT, NX1_TOT, double);
56  wAx3 = ARRAY_2D(NX3_TOT, NX1_TOT, double);
57 */
58  }
59  #else
60  if (wA == NULL){
61  wA = ARRAY_2D(NX2_TOT, NX1_TOT, double);
62 /*
63  wAx1 = ARRAY_2D(NX2_TOT, NX1_TOT, double);
64  wAx2 = ARRAY_2D(NX2_TOT, NX1_TOT, double);
65 */
66  }
67  #endif
68 
69  #if GEOMETRY == CARTESIAN
70  vphi = d->Vc[VX2];
71  #else
72  vphi = d->Vc[iVPHI];
73  #endif
74 
75  #if FARGO_AVERAGE_VELOCITY == YES
76 
77 /* ---------------------------------------------------------------------
78  average velocity along the transport direction every 10 steps
79  --------------------------------------------------------------------- */
80 
81  if (g_stepNumber%FARGO_NSTEP_AVERAGE == 0 || first_call){
82  #ifdef PARALLEL
83  double w_recv=0.0, w_sum=0.0;
84  int count, coords[3], src, dst;
85  MPI_Comm cartcomm;
86  MPI_Status status;
87  #endif
88 
89  /* -- fill ghost zones -- */
90 
91  Boundary(d, ALL_DIR, grid);
92 
93  /* ---- get ranks of the upper and lower processsors ---- */
94 
95  #ifdef PARALLEL
96  AL_Get_cart_comm(SZ, &cartcomm);
97  for (i = 0; i < DIMENSIONS; i++) coords[i] = grid[i].rank_coord;
98  coords[SDIR] += 1;
99  MPI_Cart_rank(cartcomm, coords, &dst);
100 
101  for (i = 0; i < DIMENSIONS; i++) coords[i] = grid[i].rank_coord;
102  coords[SDIR] -= 1;
103  MPI_Cart_rank(cartcomm, coords, &src);
104  #endif
105 
106  /* ---- compute average orbital speed ---- */
107 
108  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
109  KTOT_LOOP(k) ITOT_LOOP(i) {
110  #else
111  JTOT_LOOP(j) ITOT_LOOP(i) {
112  #endif
113  w = 0.0;
114  SDOM_LOOP(s) w += vphi[k][j][i];
115  if (grid[SDIR].nproc > 1){
116  #ifdef PARALLEL
117  w_sum = w;
118  for (count=1; count < grid[SDIR].nproc; count++ ){
119  MPI_Sendrecv(&w, 1, MPI_DOUBLE, dst, 0, &w_recv, 1,
120  MPI_DOUBLE, src, 0, cartcomm, &status);
121  w = w_recv;
122  w_sum += w;
123  }
124  w = w_sum/(double)(grid[SDIR].np_int_glob);
125  #endif
126  }else{
127  w = w/(double)NS;
128  }
129  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
130  wA[k][i] = w;
131  #elif GEOMETRY == SPHERICAL
132  wA[j][i] = w;
133  #endif
134  }
135  }
136 
137  /* -- compute interface velocity -- */
138 /*
139  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
140 
141  KTOT_LOOP(k) for (i = 0; i < NX1_TOT-1; i++){
142  wAx1[k][i] = 0.5*(wA[k][i] + wA[k][i+1]);
143  }
144  for (k = 0; k < NX3_TOT-1; k++) ITOT_LOOP(i){
145  wAx3[k][i] = 0.5*(wA[k][i] + wA[k+1][i]);
146  }
147 
148  #elif GEOMETRY == SPHERICAL
149 
150  JTOT_LOOP(j) for (i = 0; i < NX1_TOT-1; i++){
151  wAx1[j][i] = 0.5*(wA[j][i] + wA[j][i+1]);
152  }
153  for (j = 0; j < NX2_TOT-1; j++) ITOT_LOOP(i){
154  wAx2[j][i] = 0.5*(wA[j][i] + wA[j+1][i]);
155  }
156  #endif
157 */
158 
159  #else
160 
161 /* ---------------------------------------------------------------------
162  define velocity analytically
163  --------------------------------------------------------------------- */
164 
165  if (first_call){
166  #if GEOMETRY == CARTESIAN
167  KTOT_LOOP(k) ITOT_LOOP(i) wA[k][i] = FARGO_SetVelocity(x1[i], x3[k]);
168  #elif GEOMETRY == POLAR
169  KTOT_LOOP(k) ITOT_LOOP(i) wA[k][i] = x1[i]*FARGO_SetVelocity(x1[i], x3[k]);
170  #elif GEOMETRY == SPHERICAL
171  JTOT_LOOP(j) ITOT_LOOP(i) {
172  th = x2[j];
173  wA[j][i] = x1[i]*sin(th)*FARGO_SetVelocity(x1[i], x2[j]);
174  }
175  #else
176  print1 ("! FARGO not supported in this geometry\n");
177  QUIT_PLUTO(1);
178  #endif
179  }
180  #endif /* FARGO_AVERAGE_VELOCITY */
181 
182  first_call = 0;
183 }
184 
185 static int vphi_is_total_velocity = YES; /* Used to make sure either operation
186  is not repeated twice in a row */
187 /* ********************************************************************* */
188 void FARGO_SubtractVelocity(const Data *d, Grid *grid)
189 /*!
190  * Subtract the mean background contribution from the total
191  * velocity. In other words, compute the residual velocity.
192  *
193  * \param [in,out] d pointer to PLUTO Data structure
194  * \param [in] grid pointer to array of Grid structures
195  *
196  * \return This function has no return value.
197  *********************************************************************** */
198 {
199  int i,j,k;
200  double ***vphi;
201 
202  #if GEOMETRY == CARTESIAN
203  vphi = d->Vc[VX2];
204  #else
205  vphi = d->Vc[iVPHI];
206  #endif
207 
208 /* ----------------------------------------------------------------------
209  subtract velocity
210  ---------------------------------------------------------------------- */
211 
212  TOT_LOOP(k,j,i){
213  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
214  vphi[k][j][i] -= wA[k][i];
215  #elif GEOMETRY == SPHERICAL
216  vphi[k][j][i] -= wA[j][i];
217  #else
218  print1 ("! FARGO not supported in this geometry\n");
219  QUIT_PLUTO(1);
220  #endif
221  }
222 
223  vphi_is_total_velocity = NO;
224 }
225 /* ************************************************************** */
226 void FARGO_AddVelocity(const Data *d, Grid *grid)
227 /*!
228  * Add the mean backgroun contribution to the residual
229 * velocity in order to obtain the total velocity.
230  *
231  * \param [in,out] d pointer to PLUTO Data structure
232  * \param [in] grid pointer to array of Grid structures
233  *
234  * \return This function has no return value.
235  *********************************************************************** */
236 {
237  int i,j,k;
238  double ***vphi;
239 
240  #if GEOMETRY == CARTESIAN
241  vphi = d->Vc[VX2];
242  #else
243  vphi = d->Vc[iVPHI];
244  #endif
245 
246  TOT_LOOP(k,j,i){
247  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
248  vphi[k][j][i] += wA[k][i];
249  #elif GEOMETRY == SPHERICAL
250  vphi[k][j][i] += wA[j][i];
251  #else
252  print1 ("! FARGO not supported in this geometry\n");
253  QUIT_PLUTO(1);
254  #endif
255  }
256 
257  vphi_is_total_velocity = YES;
258 }
259 
260 /* ********************************************************************* */
262 /*!
263  * Return 1 if the velocity array contains the total velocity.
264  * Return 0 otherwise.
265  *********************************************************************** */
266 {
267  return vphi_is_total_velocity;
268 }
269 /* ********************************************************************* */
270 double **FARGO_GetVelocity(void)
271 /*!
272  * Return a pointer to the background orbital velocity ::wA.
273  *
274  *********************************************************************** */
275 {
276  return wA;
277 /*
278  int i, j, k;
279 
280  if (where == CENTER){
281 
282  return wA;
283 
284  }else if (where == X1FACE){
285 
286  return wAx1;
287 
288  }else if (where == X2FACE){
289 
290  #if GEOMETRY == SPHERICAL
291  return wAx2;
292  #else
293  print1 ("! FARGO_GetVelocity: incorrect direction\n");
294  QUIT_PLUTO(1);
295  #endif
296 
297  }else if (where == X3FACE){
298 
299  #if GEOMETRY == CARTESIAN || GEOMETRY == POLAR
300  return wAx3;
301  #else
302  print1 ("! FARGO_GetVelocity: incorrect direction\n");
303  QUIT_PLUTO(1);
304  #endif
305 
306  }
307 
308 */
309 }
void Boundary(const Data *d, int idim, Grid *grid)
Definition: boundary.c:36
#define VX2
Definition: mod_defs.h:29
#define SDIR
Definition: fargo.h:55
#define SDOM_LOOP(s)
Definition: fargo.h:66
void print1(const char *fmt,...)
Definition: amrPluto.cpp:511
double ** FARGO_GetVelocity(void)
#define YES
Definition: pluto.h:25
long int NX2_TOT
Total number of zones in the X2 direction (boundaries included) for the local processor.
Definition: globals.h:57
static double ** wA
double **** Vc
The main four-index data array used for cell-centered primitive variables.
Definition: structs.h:31
#define KTOT_LOOP(k)
Definition: macros.h:40
#define iVPHI
Definition: mod_defs.h:67
#define KDIR
Definition: pluto.h:195
static int vphi_is_total_velocity
int AL_Get_cart_comm(int, MPI_Comm *)
Definition: al_sz_get.c:117
#define FARGO_NSTEP_AVERAGE
Definition: fargo.h:25
long int g_stepNumber
Gives the current integration step number.
Definition: globals.h:97
#define TOT_LOOP(k, j, i)
Definition: macros.h:44
#define IDIR
Definition: pluto.h:193
Definition: structs.h:78
long int NX3_TOT
Total number of zones in the X3 direction (boundaries included) for the local processor.
Definition: globals.h:59
int j
Definition: analysis.c:2
int k
Definition: analysis.c:2
double * x
Definition: structs.h:80
PLUTO main header file.
#define ITOT_LOOP(i)
Definition: macros.h:38
void FARGO_ComputeVelocity(const Data *d, Grid *grid)
Definition: structs.h:30
#define JTOT_LOOP(j)
Definition: macros.h:39
int i
Definition: analysis.c:2
void FARGO_AddVelocity(const Data *d, Grid *grid)
#define s
int FARGO_HasTotalVelocity()
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171
Definition: al_hidden.h:38
#define JDIR
Definition: pluto.h:194
#define NS
Definition: fargo.h:58
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
double FARGO_SetVelocity(double, double)
Definition: init.c:502
long int NX1_TOT
Total number of zones in the X1 direction (boundaries included) for the local processor.
Definition: globals.h:55
int nproc
number of processors for this grid.
Definition: structs.h:120
#define DIMENSIONS
Definition: definitions_01.h:2
#define NO
Definition: pluto.h:26
void FARGO_SubtractVelocity(const Data *d, Grid *grid)
#define ALL_DIR
Definition: pluto.h:196