PLUTO
tools.c
Go to the documentation of this file.
1 /* ///////////////////////////////////////////////////////////////////// */
2 /*!
3  \file
4  \brief Collection of general-purpose functions.
5 
6  This file contains some miscellaneous tools for
7 
8  - Debugging / printing / error functions such as Trace(), Show(),
9  Where(), PlutoError
10  - function for swapping/detecting endianity
11 
12  \author A. Mignone (mignone@ph.unito.it)
13  \date Sept 1, 2014
14 */
15 /* ///////////////////////////////////////////////////////////////////// */
16 #include "pluto.h"
17 
18 /* ********************************************************************* */
19 int CheckNaN (double **u, int is, int ie, int id)
20 /*!
21  * Cheeck whether the array \c u contains Not-a-Number
22  * (NaN)
23  *
24  *********************************************************************** */
25 {
26  int ii, nv, i, j;
27 
28  for (ii = is; ii <= ie; ii++) {
29  for (nv = 0; nv < NVAR; nv++) {
30  if (u[ii][nv] != u[ii][nv]) {
31  print (" > NaN found (%d), |", id);
32  Show (u, ii);
33  QUIT_PLUTO(1);
34  }
35  }}
36  return (0);
37 }
38 
39 /* ********************************************************************* */
40 int IsLittleEndian (void)
41 /*!
42  * Return 1 if the current architecture has little endian order
43  *
44  *********************************************************************** */
45 {
46  int TestEndian = 1;
47  return *(char*)&TestEndian;
48 }
49 
50 /* ********************************************************************* */
51 void MakeState (State_1D *state)
52 /*!
53  *
54  * Allocate memory areas for arrays inside the state
55  * structure.
56  *
57  *
58  *********************************************************************** */
59 {
60  state->v = ARRAY_2D(NMAX_POINT, NVAR, double);
61  state->vp = ARRAY_2D(NMAX_POINT, NVAR, double);
62  state->vm = ARRAY_2D(NMAX_POINT, NVAR, double);
63  state->up = ARRAY_2D(NMAX_POINT, NVAR, double);
64  state->um = ARRAY_2D(NMAX_POINT, NVAR, double);
65  state->flux = ARRAY_2D(NMAX_POINT, NVAR, double);
66 
67  state->src = ARRAY_2D(NMAX_POINT, NVAR, double);
68 
69  state->visc_flux = ARRAY_2D(NMAX_POINT, NVAR, double);
70  state->visc_src = ARRAY_2D(NMAX_POINT, NVAR, double);
71  state->tc_flux = ARRAY_2D(NMAX_POINT, NVAR, double);
72  state->res_flux = ARRAY_2D(NMAX_POINT, NVAR, double);
73 
74  state->rhs = ARRAY_2D(NMAX_POINT, NVAR, double);
75  state->press = ARRAY_1D(NMAX_POINT, double);
76  state->bn = ARRAY_1D(NMAX_POINT, double);
77  state->SL = ARRAY_1D(NMAX_POINT, double);
78  state->SR = ARRAY_1D(NMAX_POINT, double);
79 
80 /* -- eigenvectors -- */
81 
82  state->Lp = ARRAY_3D(NMAX_POINT, NFLX, NFLX, double);
83  state->Rp = ARRAY_3D(NMAX_POINT, NFLX, NFLX, double);
84  state->lambda = ARRAY_2D(NMAX_POINT, NFLX, double);
85  state->lmax = ARRAY_1D(NVAR, double);
86 
87  state->a2 = ARRAY_1D(NMAX_POINT, double);
88  state->h = ARRAY_1D(NMAX_POINT, double);
89 
90 /* state->dwlim = ARRAY_2D(NMAX_POINT, NVAR, double);*/
91 
92  state->flag = ARRAY_1D(NMAX_POINT, unsigned char);
93 
94 /* --------------------------------------
95  define shortcut pointers for
96  left and right values with respect
97  to the cell center
98  -------------------------------------- */
99 
100  state->vL = state->vp;
101  state->vR = state->vm + 1;
102 
103  state->uL = state->up;
104  state->uR = state->um + 1;
105 
106  #if (TIME_STEPPING == HANCOCK) || (TIME_STEPPING == CHARACTERISTIC_TRACING)
107  state->vh = ARRAY_2D(NMAX_POINT, NVAR, double);
108  #else
109  state->vh = state->v;
110  #endif
111 
112 }
113 
114 /* ********************************************************************* */
115 void PlutoError (int condition, char *str)
116 /*!
117  * If condition is true, issue an error and quit the code.
118  *
119  *********************************************************************** */
120 {
121  char *str_err="! Error: ";
122 
123  if (condition) {
124  print (str_err);
125  print (str);
126  print ("\n");
127  QUIT_PLUTO(1);
128  }
129 }
130 
131 /* ********************************************************************* */
132 void Show (double **a, int ip)
133 /*!
134  * Print the component of the array \c a at grid index \c ip
135  *
136  *********************************************************************** */
137 {
138  int nv, ix, iy, iz;
139 
140  if (g_dir == IDIR) {
141  print ("X-sweep");
142  ix = ip;
143  iy = g_j;
144  iz = g_k;
145  } else if (g_dir == JDIR) {
146  print ("Y-sweep");
147  ix = g_i;
148  iy = ip;
149  iz = g_k;
150  } else if (g_dir == KDIR) {
151  print ("Z-sweep");
152  ix = g_i;
153  iy = g_j;
154  iz = ip;
155  }
156 
157  D_SELECT( print (" (%d)> ", ix); ,
158  print (" (%d,%d)> ", ix, iy); ,
159  print (" (%d,%d,%d)> ", ix, iy, iz); )
160 
161 
162  for (nv = 0; nv < NVAR; nv++) {
163  print ("%12.6e ", a[ip][nv]);
164  }
165  print ("\n");
166 }
167 
168 /* ********************************************************************* */
169 void ShowVector (double *v, int n)
170 /*!
171  * Print the component of the array \c a at grid index \c ip
172  *
173  *********************************************************************** */
174 {
175  int k;
176 
177  for (k = 0; k < n; k++) print ("%12.6e ", v[k]);
178  print ("\n");
179 }
180 
181 /* ********************************************************************* */
182 void ShowMatrix(double **A, int n, double eps)
183 /*!
184  * Make a nice printing of a 2D matrix \c A[0..n-1][0..n-1]
185  * Entries with values below eps will display "0.0"
186  *
187  *
188  *********************************************************************** */
189 {
190  int k1,k2;
191 
192  print ("----------------------------------------------------------------\n");
193  for (k1 = 0; k1 < n; k1++){
194  for (k2 = 0; k2 < n; k2++){
195  print ("%12.3e ", fabs(A[k1][k2]) > eps ? A[k1][k2]:0.0);
196  }
197  printf ("\n");
198  }
199  print ("----------------------------------------------------------------\n");
200 }
201 
202 /* ********************************************************************* */
203 void SwapEndian (void *x, const int nbytes)
204 /*!
205  * Swap the byte order of x.
206  *
207  * \param [in] x pointer to the variable being swapped
208  * \param [in] nbytes data type size
209  * \return This function has no return value.
210  *********************************************************************** */
211 {
212  int k;
213  static char Swapped[16];
214  char *c;
215 
216  c = (char *) x;
217 
218  for (k = nbytes; k--; ) Swapped[k] = *(c + nbytes - 1 - k);
219  for (k = nbytes; k--; ) c[k] = Swapped[k];
220 }
221 
222 /* ********************************************************************* */
223 void Trace (double xx)
224 /*!
225  * Print a number xx and the number of times it has been called.
226  *
227  *********************************************************************** */
228 {
229  static int ik;
230 
231  printf ("Trace ------> %f , %d\n", xx, ++ik);
232 }
233 
234 /* ********************************************************************* */
235 void Where (int i, Grid *grid)
236 /*!
237  * Print the location of a particular zone (i,j,k)
238  * in the computational domain.
239  * \note This function must be initialized before using it
240  * to store grid information. This is done by calling
241  * Where(i, grid) the very first time.
242  * Subsequent calls can be then done by simply using
243  * Where(i,NULL).
244  *
245  *********************************************************************** */
246 {
247  int ii=0, jj=0, kk=0;
248  double x1, x2, x3;
249  static Grid *grid1, *grid2, *grid3;
250 
251 /* --------------------------------------------------
252  Keep a local copy of grid for subsequent calls
253  -------------------------------------------------- */
254 
255  if (grid != NULL){
256  grid1 = grid + IDIR;
257  grid2 = grid + JDIR;
258  grid3 = grid + KDIR;
259  return;
260  }
261 
262  #ifdef CH_SPACEDIM
263  if (g_intStage < 0) return; /* HOT FIX used by CHOMBO
264  (g_intStage = -1) when writing HDF5 file */
265  #endif
266 
267 /* -- ok, proceed normally -- */
268 
269  if (g_dir == IDIR){
270  D_EXPAND(ii = i;, jj = g_j;, kk = g_k;)
271  }else if (g_dir == JDIR){
272  D_EXPAND(ii = g_i;, jj = i;, kk = g_k;)
273  }else if (g_dir == KDIR){
274  D_EXPAND(ii = g_i;, jj = g_j;, kk = i;)
275  }
276 
277  D_EXPAND(
278  x1 = grid1->x[ii]; ,
279  x2 = grid2->x[jj]; ,
280  x3 = grid3->x[kk];
281  )
282 
283  D_SELECT(
284  print ("zone [x1(%d) = %f]",
285  ii, grid1->x[ii]); ,
286 
287  print ("zone [x1(%d) = %f, x2(%d) = %f]",
288  ii, grid1->x[ii],
289  jj, grid2->x[jj]); ,
290 
291  print ("zone [x1(%d) = %f, x2(%d) = %f, x3(%d) = %f]",
292  ii, grid1->x[ii],
293  jj, grid2->x[jj],
294  kk, grid3->x[kk]);
295  )
296 
297  #ifdef CHOMBO
298  print (", Level = %d\n", grid1->level);
299  return;
300  #endif
301  #ifdef PARALLEL
302  print (", proc %d\n", prank);
303  return;
304  #else
305  print ("\n");
306  return;
307  #endif
308 }
309 
310 /* /////////////////////////////////////////////////////////////////////
311  The next set of functions provides basic functionalities to
312 
313  - set the log file
314  - formatted output to the log file through the print() and print1()
315  functions
316  ///////////////////////////////////////////////////////////////////// */
317 
318 static char log_file_name[512];
319 
320 /* ********************************************************************* */
321 int SetLogFile(char *output_dir, Cmd_Line *cmd)
322 /*!
323  * Set the name of the log file and open in write or append mode
324  * depending on whether restart is enabled or not.
325  *
326  * \param [in] output_dir the name of the output directory
327  * \param [in] cmd pointer to cmd line option structure.
328  *
329  *********************************************************************** */
330 {
331 #if PRINT_TO_FILE == YES
332  FILE *fl;
333 
334 /* ------------------------------------------------
335  All processors set log file name
336  ------------------------------------------------ */
337 
338  sprintf (log_file_name, "%s/pluto.log",output_dir);
339 
340 /* ------------------------------------------------
341  Proc. #0 opens log file for writing if
342  -restart or -h5restart have not been given.
343  Otherwise, open in append mode.
344  ------------------------------------------------ */
345 
346  if (prank == 0){
347  if (cmd->restart == NO && cmd->h5restart == NO){
348  fl = fopen(log_file_name,"w");
349  }else{
350  fl = fopen(log_file_name,"aw");
351  }
352 
353  /* -- check that we have a valid directory name -- */
354 
355  if (fl == NULL){
356  printf ("! SetLogFile: pluto.log cannot be written.\n");
357  QUIT_PLUTO(1);
358  }
359  fprintf(fl,"\n");
360  fclose(fl);
361  }
362 #endif
363 }
364 
365 #ifndef CH_SPACEDIM
366 /* ********************************************************************* */
367 void print (const char *fmt, ...)
368 /*!
369  * Define print function for the static grid version
370  * of PLUTO. The Chombo version is defined in Chombo/amrPLUTO.cpp
371  *
372  *********************************************************************** */
373 {
374  FILE *fl;
375 
376  va_list args;
377  va_start(args, fmt);
378 
379 #if PRINT_TO_FILE == YES
380  fl = fopen(log_file_name,"a");
381  vfprintf(fl, fmt, args);
382  fclose (fl);
383 #else
384  vprintf(fmt, args);
385 #endif
386 
387  va_end(args);
388 }
389 /* ********************************************************************* */
390 void print1 (const char *fmt, ...)
391 /*!
392  *
393  * Define print1 function
394  *
395  *********************************************************************** */
396 {
397  FILE *fl;
398 
399  va_list args;
400  va_start(args, fmt);
401 
402  #if PRINT_TO_FILE == YES
403  if (prank == 0){
404  fl = fopen(log_file_name,"a");
405  vfprintf(fl,fmt, args);
406  fclose(fl);
407  }
408  #else
409  if (prank == 0) vprintf(fmt, args);
410  #endif
411  va_end(args);
412 }
413 #endif
414 
415 /* ********************************************************************* */
416 void WriteAsciiFile (char *fname, double *q, int nvar)
417 /*!
418  * Write one row a multi-column ascii file
419  *
420  *********************************************************************** */
421 {
422  int n;
423  static char old_file[64] = "\0";
424  FILE *fp;
425 
426 /* --------------------------------------------------------
427  If the old file name matches the new one, then open
428  in append mode. Otherwise, open in write mode.
429  -------------------------------------------------------- */
430 
431  if (strcmp (fname,old_file) == 0){
432  fp = fopen(fname,"a");
433  }else{
434  fp = fopen(fname,"w");
435  sprintf (old_file,"%s",fname);
436  }
437 
438  for (n = 0; n < nvar; n++) fprintf (fp,"%12.6e ",q[n]);
439  fprintf (fp,"\n");
440  fclose(fp);
441 
442 }
void MakeState(State_1D *state)
Definition: tools.c:51
static double a
Definition: init.c:135
int level
The current refinement level (chombo only).
Definition: structs.h:122
double ** v
Cell-centered primitive varables at the base time level, v[i] = .
Definition: structs.h:134
int CheckNaN(double **u, int is, int ie, int id)
Definition: tools.c:19
double * lmax
Define the maximum k-characteristic speed over the domain.
Definition: structs.h:157
double ** vh
Primitive state at n+1/2 (only for one step method)
Definition: structs.h:162
double ** flux
upwind flux computed with the Riemann solver
Definition: structs.h:149
void ShowMatrix(double **A, int n, double eps)
Definition: tools.c:182
static char log_file_name[512]
Definition: tools.c:318
static int n
Definition: analysis.c:3
void Show(double **a, int ip)
Definition: tools.c:132
double ** visc_src
Viscosity source term.
Definition: structs.h:151
double *** Lp
Definition: structs.h:155
double ** rhs
Conservative right hand side.
Definition: structs.h:163
void SwapEndian(void *x, const int nbytes)
Definition: tools.c:203
int g_intStage
Gives the current integration stage of the time stepping method (predictor = 0, 1st corrector = 1...
Definition: globals.h:98
double * SR
Rightmost velocity in the Riemann fan at i+1/2.
Definition: structs.h:167
double ** res_flux
Resistive flux (current)
Definition: structs.h:153
void PlutoError(int condition, char *str)
Definition: tools.c:115
double ** vR
Primitive variables to the right of the interface, .
Definition: structs.h:139
#define ARRAY_3D(nx, ny, nz, type)
Definition: prototypes.h:172
int h5restart
Definition: structs.h:13
int IsLittleEndian(void)
Definition: tools.c:40
double ** visc_flux
Viscosity flux.
Definition: structs.h:150
int prank
Processor rank.
Definition: globals.h:33
double * h
Enthalpy.
Definition: structs.h:159
#define KDIR
Definition: pluto.h:195
void Where(int i, Grid *grid)
Definition: tools.c:235
double ** vp
prim vars at i+1/2 edge, vp[i] = vL(i+1/2)
Definition: structs.h:142
int g_i
x1 grid index when sweeping along the x2 or x3 direction.
Definition: globals.h:82
double ** src
Definition: structs.h:160
#define eps
void Trace(double xx)
Definition: tools.c:223
void print1(const char *fmt,...)
Definition: tools.c:390
#define NFLX
Definition: mod_defs.h:32
#define IDIR
Definition: pluto.h:193
int SetLogFile(char *output_dir, Cmd_Line *cmd)
Definition: tools.c:321
int g_dir
Specifies the current sweep or direction of integration.
Definition: globals.h:86
int g_j
x2 grid index when sweeping along the x1 or x3 direction.
Definition: globals.h:83
Definition: structs.h:78
double * SL
Leftmost velocity in the Riemann fan at i+1/2.
Definition: structs.h:166
int j
Definition: analysis.c:2
double ** lambda
Characteristic speed associated to Lp and Rp.
Definition: structs.h:156
unsigned char * flag
Definition: structs.h:168
int k
Definition: analysis.c:2
double * x
Definition: structs.h:80
void ShowVector(double *v, int n)
Definition: tools.c:169
double ** tc_flux
Thermal conduction flux.
Definition: structs.h:152
tuple c
Definition: menu.py:375
int g_k
x3 grid index when sweeping along the x1 or x2 direction.
Definition: globals.h:84
double ** vm
prim vars at i-1/2 edge, vm[i] = vR(i-1/2)
Definition: structs.h:141
double ** uR
same as vR, in conservative vars
Definition: structs.h:145
PLUTO main header file.
#define ARRAY_1D(nx, type)
Definition: prototypes.h:170
D_EXPAND(tot/[n]=(double) grid[IDIR].np_int_glob;, tot/[n]=(double) grid[JDIR].np_int_glob;, tot/[n]=(double) grid[KDIR].np_int_glob;)
Definition: analysis.c:27
long int NMAX_POINT
Maximum number of points among the three directions, boundaries excluded.
Definition: globals.h:62
int i
Definition: analysis.c:2
double * bn
Face magentic field, bn = bx(i+1/2)
Definition: structs.h:165
void WriteAsciiFile(char *fname, double *q, int nvar)
Definition: tools.c:416
FILE * fp
Definition: analysis.c:7
double ** um
same as vm, in conservative vars
Definition: structs.h:146
double ** vL
Primitive variables to the left of the interface, .
Definition: structs.h:136
double * a2
Sound speed squared.
Definition: structs.h:158
double * press
Upwind pressure term computed with the Riemann solver.
Definition: structs.h:164
double ** up
same as vp, in conservative vars
Definition: structs.h:147
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171
double *** Rp
Left and right primitive eigenvectors.
Definition: structs.h:155
void print(const char *fmt,...)
Definition: tools.c:367
#define JDIR
Definition: pluto.h:194
#define NVAR
Definition: pluto.h:609
static Runtime q
int restart
Definition: structs.h:12
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
#define NO
Definition: pluto.h:26
double ** uL
same as vL, in conservative vars
Definition: structs.h:144