PLUTO
tools.c File Reference

Collection of general-purpose functions. More...

#include "pluto.h"
Include dependency graph for tools.c:

Go to the source code of this file.

Functions

int CheckNaN (double **u, int is, int ie, int id)
 
int IsLittleEndian (void)
 
void MakeState (State_1D *state)
 
void PlutoError (int condition, char *str)
 
void Show (double **a, int ip)
 
void ShowVector (double *v, int n)
 
void ShowMatrix (double **A, int n, double eps)
 
void SwapEndian (void *x, const int nbytes)
 
void Trace (double xx)
 
void Where (int i, Grid *grid)
 
int SetLogFile (char *output_dir, Cmd_Line *cmd)
 
void print (const char *fmt,...)
 
void print1 (const char *fmt,...)
 
void WriteAsciiFile (char *fname, double *q, int nvar)
 

Variables

static char log_file_name [512]
 

Detailed Description

Collection of general-purpose functions.

This file contains some miscellaneous tools for

  • Debugging / printing / error functions such as Trace(), Show(), Where(), PlutoError
  • function for swapping/detecting endianity
Author
A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
Date
Sept 1, 2014

Definition in file tools.c.

Function Documentation

int CheckNaN ( double **  u,
int  is,
int  ie,
int  id 
)

Cheeck whether the array u contains Not-a-Number (NaN)

Definition at line 19 of file tools.c.

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 }
void Show(double **a, int ip)
Definition: tools.c:132
int j
Definition: analysis.c:2
int i
Definition: analysis.c:2
void print(const char *fmt,...)
Definition: tools.c:367
#define NVAR
Definition: pluto.h:609
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function:

Here is the caller graph for this function:

int IsLittleEndian ( void  )

Return 1 if the current architecture has little endian order

Definition at line 40 of file tools.c.

45 {
46  int TestEndian = 1;
47  return *(char*)&TestEndian;
48 }

Here is the caller graph for this function:

void MakeState ( State_1D state)

Allocate memory areas for arrays inside the state structure.

Definition at line 51 of file tools.c.

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 }
double ** v
Cell-centered primitive varables at the base time level, v[i] = .
Definition: structs.h:134
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
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
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
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
double ** visc_flux
Viscosity flux.
Definition: structs.h:150
double * h
Enthalpy.
Definition: structs.h:159
double ** vp
prim vars at i+1/2 edge, vp[i] = vL(i+1/2)
Definition: structs.h:142
double ** src
Definition: structs.h:160
#define NFLX
Definition: mod_defs.h:32
double * SL
Leftmost velocity in the Riemann fan at i+1/2.
Definition: structs.h:166
double ** lambda
Characteristic speed associated to Lp and Rp.
Definition: structs.h:156
unsigned char * flag
Definition: structs.h:168
double ** tc_flux
Thermal conduction flux.
Definition: structs.h:152
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
#define ARRAY_1D(nx, type)
Definition: prototypes.h:170
long int NMAX_POINT
Maximum number of points among the three directions, boundaries excluded.
Definition: globals.h:62
double * bn
Face magentic field, bn = bx(i+1/2)
Definition: structs.h:165
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
#define NVAR
Definition: pluto.h:609
double ** uL
same as vL, in conservative vars
Definition: structs.h:144

Here is the caller graph for this function:

void PlutoError ( int  condition,
char *  str 
)

If condition is true, issue an error and quit the code.

Definition at line 115 of file tools.c.

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 }
void print(const char *fmt,...)
Definition: tools.c:367
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function:

Here is the caller graph for this function:

void print ( const char *  fmt,
  ... 
)

Define print function for the static grid version of PLUTO. The Chombo version is defined in Chombo/amrPLUTO.cpp

Definition at line 367 of file tools.c.

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 }
static char log_file_name[512]
Definition: tools.c:318

Here is the caller graph for this function:

void print1 ( const char *  fmt,
  ... 
)

Define print1 function

Definition at line 390 of file tools.c.

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 }
static char log_file_name[512]
Definition: tools.c:318
int prank
Processor rank.
Definition: globals.h:33
int SetLogFile ( char *  output_dir,
Cmd_Line cmd 
)

Set the name of the log file and open in write or append mode depending on whether restart is enabled or not.

Parameters
[in]output_dirthe name of the output directory
[in]cmdpointer to cmd line option structure.

Definition at line 321 of file tools.c.

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 }
static char log_file_name[512]
Definition: tools.c:318
int h5restart
Definition: structs.h:13
int prank
Processor rank.
Definition: globals.h:33
int restart
Definition: structs.h:12
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
#define NO
Definition: pluto.h:26

Here is the caller graph for this function:

void Show ( double **  a,
int  ip 
)

Print the component of the array a at grid index ip

Definition at line 132 of file tools.c.

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 }
static double a
Definition: init.c:135
#define KDIR
Definition: pluto.h:195
int g_i
x1 grid index when sweeping along the x2 or x3 direction.
Definition: globals.h:82
#define IDIR
Definition: pluto.h:193
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
int g_k
x3 grid index when sweeping along the x1 or x2 direction.
Definition: globals.h:84
void print(const char *fmt,...)
Definition: tools.c:367
#define JDIR
Definition: pluto.h:194
#define NVAR
Definition: pluto.h:609

Here is the call graph for this function:

Here is the caller graph for this function:

void ShowMatrix ( double **  A,
int  n,
double  eps 
)

Make a nice printing of a 2D matrix A[0..n-1][0..n-1] Entries with values below eps will display "0.0"

Definition at line 182 of file tools.c.

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 }
static int n
Definition: analysis.c:3
#define eps
void print(const char *fmt,...)
Definition: tools.c:367
double * A
Right interface area, A[i] = .
Definition: structs.h:87

Here is the call graph for this function:

Here is the caller graph for this function:

void ShowVector ( double *  v,
int  n 
)

Print the component of the array a at grid index ip

Definition at line 169 of file tools.c.

174 {
175  int k;
176 
177  for (k = 0; k < n; k++) print ("%12.6e ", v[k]);
178  print ("\n");
179 }
static int n
Definition: analysis.c:3
int k
Definition: analysis.c:2
void print(const char *fmt,...)
Definition: tools.c:367

Here is the call graph for this function:

void SwapEndian ( void *  x,
const int  nbytes 
)

Swap the byte order of x.

Parameters
[in]xpointer to the variable being swapped
[in]nbytesdata type size
Returns
This function has no return value.

Definition at line 203 of file tools.c.

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 }
int k
Definition: analysis.c:2
double * x
Definition: structs.h:80
tuple c
Definition: menu.py:375
void Trace ( double  xx)

Print a number xx and the number of times it has been called.

Definition at line 223 of file tools.c.

228 {
229  static int ik;
230 
231  printf ("Trace ------> %f , %d\n", xx, ++ik);
232 }
void Where ( int  i,
Grid grid 
)

Print the location of a particular zone (i,j,k) in the computational domain.

Note
This function must be initialized before using it to store grid information. This is done by calling Where(i, grid) the very first time. Subsequent calls can be then done by simply using Where(i,NULL).

Definition at line 235 of file tools.c.

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 }
int level
The current refinement level (chombo only).
Definition: structs.h:122
int g_intStage
Gives the current integration stage of the time stepping method (predictor = 0, 1st corrector = 1...
Definition: globals.h:98
int prank
Processor rank.
Definition: globals.h:33
#define KDIR
Definition: pluto.h:195
int g_i
x1 grid index when sweeping along the x2 or x3 direction.
Definition: globals.h:82
#define IDIR
Definition: pluto.h:193
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 * x
Definition: structs.h:80
int g_k
x3 grid index when sweeping along the x1 or x2 direction.
Definition: globals.h:84
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
int i
Definition: analysis.c:2
void print(const char *fmt,...)
Definition: tools.c:367
#define JDIR
Definition: pluto.h:194

Here is the call graph for this function:

Here is the caller graph for this function:

void WriteAsciiFile ( char *  fname,
double *  q,
int  nvar 
)

Write one row a multi-column ascii file

Definition at line 416 of file tools.c.

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 }
static int n
Definition: analysis.c:3
FILE * fp
Definition: analysis.c:7
static Runtime q

Variable Documentation

char log_file_name[512]
static

Definition at line 318 of file tools.c.