PLUTO
mappers.c File Reference

Convert between primitive and conservative variables. More...

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

Go to the source code of this file.

Functions

void PrimToCons (double **uprim, double **ucons, int ibeg, int iend)
 
int ConsToPrim (double **ucons, double **uprim, int ibeg, int iend, unsigned char *flag)
 

Detailed Description

Convert between primitive and conservative variables.

The PrimToCons() converts an array of primitive quantities to an array of conservative variables for the MHD equations.

The ConsToPrim() converts an array of conservative quantities to an array of primitive quantities. During the conversion, pressure is normally recovered from total energy unless zone has been tagged with FLAG_ENTROPY. In this case we recover pressure from conserved entropy:

if (FLAG_ENTROPY is TRUE)  --> p = p(S)
else                       --> p = p(E)
Author
A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
Date
June 24, 2015

Definition in file mappers.c.

Function Documentation

int ConsToPrim ( double **  ucons,
double **  uprim,
int  ibeg,
int  iend,
unsigned char *  flag 
)

Convert from conservative to primitive variables.

Parameters
[in]uconsarray of conservative variables
[out]uprimarray of primitive variables
[in]begstarting index of computation
[in]endfinal index of computation
[in,out]flagarray of flags tagging, in input, zones where entropy must be used to recover pressure and, on output, zones where conversion was not successful.
Returns
Return 0 if conversion was successful in all zones in the range [ibeg,iend]. Return 1 if one or more zones could not be converted correctly and either pressure, density or energy took non-physical values.

Definition at line 88 of file mappers.c.

108 {
109  int i, nv, err, ifail;
110  int use_entropy, use_energy=1;
111  double tau, rho, gmm1, rhoe, T;
112  double b2, m2, kinb2, rhog1;
113  double *u, *v;
114 
115 #if EOS == IDEAL
116  gmm1 = g_gamma - 1.0;
117 #endif
118 
119  ifail = 0;
120  for (i = ibeg; i <= iend; i++) {
121 
122  u = ucons[i];
123  v = uprim[i];
124 
125  m2 = EXPAND(u[MX1]*u[MX1], + u[MX2]*u[MX2], + u[MX3]*u[MX3]);
126  b2 = EXPAND(u[BX1]*u[BX1], + u[BX2]*u[BX2], + u[BX3]*u[BX3]);
127 
128  /* -- Check density positivity -- */
129 
130  if (u[RHO] < 0.0) {
131  print("! ConsToPrim: negative density (%8.2e), ", u[RHO]);
132  Where (i, NULL);
133  u[RHO] = g_smallDensity;
134  flag[i] |= FLAG_CONS2PRIM_FAIL;
135  ifail = 1;
136  }
137 
138  /* -- Compute density, velocity, mag. field and scalars -- */
139 
140  v[RHO] = rho = u[RHO];
141  tau = 1.0/u[RHO];
142  EXPAND(v[VX1] = u[MX1]*tau; ,
143  v[VX2] = u[MX2]*tau; ,
144  v[VX3] = u[MX3]*tau;)
145 
146  EXPAND(v[BX1] = u[BX1]; ,
147  v[BX2] = u[BX2]; ,
148  v[BX3] = u[BX3];)
149 
150  kinb2 = 0.5*(m2*tau + b2);
151 
152  /* -- Check energy positivity -- */
153 
154 #if HAVE_ENERGY
155  if (u[ENG] < 0.0) {
156  WARNING(
157  print("! ConsToPrim: negative energy (%8.2e), ", u[ENG]);
158  Where (i, NULL);
159  )
160  u[ENG] = g_smallPressure/gmm1 + kinb2;
161  flag[i] |= FLAG_CONS2PRIM_FAIL;
162  ifail = 1;
163  }
164 #endif
165 
166  /* -- Compute pressure from total energy or entropy -- */
167 
168 #if EOS == IDEAL
169  #if ENTROPY_SWITCH
170  use_entropy = (flag[i] & FLAG_ENTROPY);
171  use_energy = !use_entropy;
172  if (use_entropy){
173  rhog1 = pow(rho, gmm1);
174  v[PRS] = u[ENTR]*rhog1;
175  if (v[PRS] < 0.0){
176  WARNING(
177  print("! ConsToPrim: negative p(S) (%8.2e, %8.2e), ", v[PRS], u[ENTR]);
178  Where (i, NULL);
179  )
180  v[PRS] = g_smallPressure;
181  flag[i] |= FLAG_CONS2PRIM_FAIL;
182  ifail = 1;
183  }
184  u[ENG] = v[PRS]/gmm1 + kinb2; /* -- recompute energy -- */
185  }
186  #endif /* ENTROPY_SWITCH */
187 
188  if (use_energy){
189  v[PRS] = gmm1*(u[ENG] - kinb2);
190  if (v[PRS] < 0.0){
191  WARNING(
192  print("! ConsToPrim: negative p(E) (%8.2e), ", v[PRS]);
193  Where (i, NULL);
194  )
195  v[PRS] = g_smallPressure;
196  u[ENG] = v[PRS]/gmm1 + kinb2; /* -- recompute energy -- */
197  flag[i] |= FLAG_CONS2PRIM_FAIL;
198  ifail = 1;
199  }
200  #if ENTROPY_SWITCH
201  u[ENTR] = v[PRS]/pow(rho,gmm1); /* -- Recompute entropy -- */
202  #endif
203  }
204 
205  #if NSCL > 0
206  NSCL_LOOP(nv) v[nv] = u[nv]*tau;
207  #endif
208 
209 #elif EOS == PVTE_LAW
210 
211  /* -- Convert scalars here since EoS may need ion fractions -- */
212 
213  #if NSCL > 0
214  NSCL_LOOP(nv) v[nv] = u[nv]*tau;
215  #endif
216 
217  if (u[ENG] != u[ENG]){
218  print("! ConsToPrim: NaN found\n");
219  Show(ucons,i);
220  QUIT_PLUTO(1);
221  }
222  rhoe = u[ENG] - kinb2;
223 
224  err = GetEV_Temperature (rhoe, v, &T);
225  if (err){ /* If something went wrong while retrieving the */
226  /* temperature, we floor \c T to \c T_CUT_RHOE, */
227  /* recompute internal and total energies. */
228  T = T_CUT_RHOE;
229  WARNING(
230  print ("! ConsToPrim: rhoe < 0 or T < T_CUT_RHOE; ");
231  Where(i,NULL);
232  )
233  rhoe = InternalEnergy(v, T);
234  u[ENG] = rhoe + kinb2; /* -- redefine total energy -- */
235  flag[i] |= FLAG_CONS2PRIM_FAIL;
236  ifail = 1;
237  }
238  v[PRS] = Pressure(v, T);
239 
240 #endif /* EOS */
241 
242 #if DUST == YES
243  u[RHO_D] = MAX(u[RHO_D], 1.e-20);
244  v[RHO_D] = u[RHO_D];
245  EXPAND(v[MX1_D] = u[MX1_D]/v[RHO_D]; ,
246  v[MX2_D] = u[MX2_D]/v[RHO_D]; ,
247  v[MX3_D] = u[MX3_D]/v[RHO_D];)
248 #endif
249 
250 #ifdef GLM_MHD
251  v[PSI_GLM] = u[PSI_GLM];
252 #endif
253  }
254  return ifail;
255 }
#define MX3
Definition: mod_defs.h:22
#define MAX(a, b)
Definition: macros.h:101
tuple T
Definition: Sph_disk.py:33
#define NSCL_LOOP(n)
Definition: pluto.h:616
double g_gamma
Definition: globals.h:112
#define MX1
Definition: mod_defs.h:20
#define VX2
Definition: mod_defs.h:29
#define T_CUT_RHOE
Sets the lowest cut-off temperature used in the PVTE_LAW equation of state.
Definition: eos.h:69
double g_smallDensity
Small value for density fix.
Definition: globals.h:109
double InternalEnergy(double *v, double T)
#define RHO
Definition: mod_defs.h:19
#define WARNING(a)
Definition: macros.h:217
double rhoe
Definition: eos.h:107
#define PSI_GLM
Definition: mod_defs.h:34
double v[NVAR]
Definition: eos.h:106
double g_smallPressure
Small value for pressure fix.
Definition: globals.h:110
#define VX1
Definition: mod_defs.h:28
#define FLAG_CONS2PRIM_FAIL
Definition: pluto.h:189
#define FLAG_ENTROPY
Update pressure using entropy equation.
Definition: pluto.h:182
int GetEV_Temperature(double rhoe, double *v, double *T)
#define MX2
Definition: mod_defs.h:21
void Where(int, Grid *)
Definition: tools.c:235
void print(const char *fmt,...)
Definition: amrPluto.cpp:497
#define BX3
Definition: mod_defs.h:27
void Show(double **, int)
Definition: tools.c:132
int i
Definition: analysis.c:2
double Pressure(double *v, double T)
Definition: thermal_eos.c:179
#define BX1
Definition: mod_defs.h:25
#define VX3
Definition: mod_defs.h:30
#define BX2
Definition: mod_defs.h:26
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
#define GLM_MHD
Definition: glm.h:25

Here is the call graph for this function:

void PrimToCons ( double **  uprim,
double **  ucons,
int  ibeg,
int  iend 
)

Convert primitive variables in conservative variables.

Parameters
[in]uprimarray of primitive variables
[out]uconsarray of conservative variables
[in]begstarting index of computation
[in]endfinal index of computation

Definition at line 25 of file mappers.c.

35 {
36  int i, nv, status;
37  double *v, *u;
38  double rhoe, kinb2, T, gmm1;
39 
40 #if EOS == IDEAL
41  gmm1 = g_gamma - 1.0;
42 #endif
43  for (i = ibeg; i <= iend; i++) {
44 
45  v = uprim[i];
46  u = ucons[i];
47 
48  u[RHO] = v[RHO];
49 
50  EXPAND (u[MX1] = v[RHO]*v[VX1]; ,
51  u[MX2] = v[RHO]*v[VX2]; ,
52  u[MX3] = v[RHO]*v[VX3];)
53 
54  EXPAND (u[BX1] = v[BX1]; ,
55  u[BX2] = v[BX2]; ,
56  u[BX3] = v[BX3];)
57 
58  kinb2 = EXPAND(v[VX1]*v[VX1], + v[VX2]*v[VX2], + v[VX3]*v[VX3]);
59  kinb2 = v[RHO]*kinb2 + EXPAND(v[BX1]*v[BX1], + v[BX2]*v[BX2], + v[BX3]*v[BX3]);
60  kinb2 *= 0.5;
61 
62 #if EOS == IDEAL
63  u[ENG] = kinb2 + v[PRS]/gmm1;
64 #elif EOS == PVTE_LAW
65  status = GetPV_Temperature(v, &T);
66  if (status != 0){
67  T = T_CUT_RHOE;
68  v[PRS] = Pressure(v, T);
69  }
70  rhoe = InternalEnergy(v, T);
71  u[ENG] = rhoe + kinb2;
72 
73  if (u[ENG] != u[ENG]){
74  print("! PrimToCons: KE:%12.6e uRHO : %12.6e, m2 : %12.6e \n",rhoe,v[RHO],u[ENG]);
75  QUIT_PLUTO(1);
76  }
77 #endif
78 
79 #ifdef GLM_MHD
80  u[PSI_GLM] = v[PSI_GLM];
81 #endif
82 #if NSCL > 0
83  NSCL_LOOP(nv) u[nv] = v[RHO]*v[nv];
84 #endif
85  }
86 }
#define MX3
Definition: mod_defs.h:22
tuple T
Definition: Sph_disk.py:33
#define NSCL_LOOP(n)
Definition: pluto.h:616
double g_gamma
Definition: globals.h:112
#define MX1
Definition: mod_defs.h:20
#define VX2
Definition: mod_defs.h:29
#define T_CUT_RHOE
Sets the lowest cut-off temperature used in the PVTE_LAW equation of state.
Definition: eos.h:69
double InternalEnergy(double *v, double T)
#define RHO
Definition: mod_defs.h:19
double rhoe
Definition: eos.h:107
#define PSI_GLM
Definition: mod_defs.h:34
double v[NVAR]
Definition: eos.h:106
#define VX1
Definition: mod_defs.h:28
#define MX2
Definition: mod_defs.h:21
void print(const char *fmt,...)
Definition: amrPluto.cpp:497
#define BX3
Definition: mod_defs.h:27
int i
Definition: analysis.c:2
double Pressure(double *v, double T)
Definition: thermal_eos.c:179
#define BX1
Definition: mod_defs.h:25
#define VX3
Definition: mod_defs.h:30
int GetPV_Temperature(double *v, double *T)
Definition: thermal_eos.c:221
#define BX2
Definition: mod_defs.h:26
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function: