The FARGO_THORIN code developer's guide
 All Data Structures Files Functions Variables Typedefs Macros
rebin.c
Go to the documentation of this file.
1 /** \file rebin.c
2 
3 Resample the hydrodynamical fields at a restart with a different resolution.
4 Note that at the restart, even if NRAD and NSEC coincide with previous values,
5 the data is resampled if the radii do not coincide (for instance, if we switch
6 from ARITHMETIC to LOGARITHMIC spacing).
7 
8 */
9 
10 
11 #include "fargo.h"
12 
14 static int OldNRAD, OldNSEC;
15 
16 void ReadPrevDim () {
17  FILE *DIM, *RAD;
18  char name_dim[1024], name_rad[1024];
19  int i, foo;
20  float Foo;
21  double value;
22  if (!CPU_Master) return;
23  OldNRAD = 0;
24  sprintf (name_dim, "%sdims.dat", OUTPUTDIR);
25  sprintf (name_rad, "%sused_rad.dat", OUTPUTDIR);
26  DIM = fopen (name_dim, "r");
27  if (DIM == NULL) return;
28  RAD = fopen (name_rad, "r");
29  if (RAD == NULL) return;
30  fscanf (DIM,"%d %d %d %d %f %d %d %d\n",&foo,&foo,&foo,&foo,&Foo,&foo,&OldNRAD,&OldNSEC);
31  for (i = 0; i <= OldNRAD; i++) {
32  fscanf (RAD, "%lf", &value);
33  OldRadii[i] = (real)value;
34  }
35  fclose (DIM);
36  fclose (RAD);
37  for (i = 0; i < OldNRAD; i++) {
38  OldRmed[i] = 2.0/3.0*(OldRadii[i+1]*OldRadii[i+1]*OldRadii[i+1]-OldRadii[i]*OldRadii[i]*OldRadii[i]);
39  OldRmed[i] = OldRmed[i] / (OldRadii[i+1]*OldRadii[i+1]-OldRadii[i]*OldRadii[i]);
40  }
41 }
42 
43 void CheckRebin (nb)
44 int nb;
45 {
46  boolean RebinNeeded = NO, found;
47  char radix[1024], filename[1024];
48  int i, iold, jold, l, j, lo, loip, lojp, loipjp, type;
49  real ifrac, jfrac, jreal, r, angle, dangle;
50  FILE *ARR;
51  real *Old_r, *OldArray, *NewArray;
52  if (!CPU_Master) return;
53  if (NSEC != OldNSEC) RebinNeeded = YES;
54  if (GLOBALNRAD != OldNRAD) RebinNeeded = YES;
55  for (i = 0; i <= NRAD; i++) {
56  if (fabs((Radii[i]-OldRadii[i])/Radii[i]) > 1e-9) RebinNeeded = YES;
57  }
58  if (!RebinNeeded) return;
59  printf ("Restart/Old mesh mismatch. Rebin needed.\n");
60  OldArray = (real *)malloc(OldNRAD*OldNSEC*sizeof(real));
61  NewArray = (real *)malloc(GLOBALNRAD*NSEC*sizeof(real));
62  if ((OldArray == NULL) || (NewArray == NULL)) {
63  mastererr ("Not enough memory left for rebining.\n");
64  mastererr ("Aborted.\n");
65  prs_exit (1);
66  }
67  for (type = 0; type < 4; type++) {
68  Old_r = OldRmed;
69  memcpy (New_r, GlobalRmed, (GLOBALNRAD+1)*sizeof(double));
70  dangle = 0.0;
71  switch (type) {
72  case 0: sprintf (radix, "dens"); break;
73  case 1: sprintf (radix, "vrad");
74  Old_r = OldRadii;
75  memcpy (New_r, Radii, (GLOBALNRAD+1)*sizeof(double));
76  break;
77  case 2: sprintf (radix, "vtheta"); dangle = 0.5; break;
78  case 3: sprintf (radix, "label"); break;
79  }
80  for (i = 0; i < GLOBALNRAD; i++) {
81  if (New_r[i] < Old_r[0]) New_r[i] = Old_r[0];
82  if (New_r[i] > Old_r[OldNRAD-1]) New_r[i] = Old_r[OldNRAD-1];
83  }
84  sprintf (filename, "%sgas%s%d.dat", OUTPUTDIR, radix, nb);
85  ARR = fopen (filename, "r");
86  if (ARR != NULL) {
87  fread (OldArray, sizeof(real), OldNRAD*OldNSEC, ARR);
88  fclose (ARR);
89  for (i = 0; i < GLOBALNRAD; i++) {
90  r = New_r[i];
91  iold = 0;
92  found = NO;
93  while ((iold < OldNRAD) && (!found)) {
94  if (Old_r[iold+1] <= r) iold++;
95  else found = YES;
96  }
97  if (r <= Old_r[0]) {
98  iold = 0;
99  ifrac = 0.0;
100  } else if (r >= Old_r[OldNRAD-1]) {
101  iold = OldNRAD-2;
102  ifrac = 1.0;
103  } else
104  ifrac = (r-Old_r[iold])/(Old_r[iold+1]-Old_r[iold]);
105  printf ("%d\t%d\t%g\n", i, iold, ifrac);
106  for (j = 0; j < NSEC; j++) {
107  l = j+i*NSEC;
108  angle = ((real)j-dangle)/(real)NSEC*2.0*M_PI;
109  jreal = angle/2.0/M_PI*(real)OldNSEC+dangle;
110  while (jreal < 0.0) jreal += (real)OldNSEC;
111  jold = (int)jreal;
112  jold = jold % OldNSEC;
113  jfrac = jreal-(real)jold;
114  lo = jold+iold*OldNSEC;
115  loip = lo+OldNSEC;
116  lojp = (jold == OldNSEC-1 ? lo-OldNSEC+1 : lo+1);
117  loipjp = lojp+OldNSEC;
118  NewArray[l] = OldArray[lo]*(1.0-ifrac)*(1.-jfrac)+\
119  OldArray[lojp]*(1.0-ifrac)*jfrac+\
120  OldArray[loip]*ifrac*(1.0-jfrac)+\
121  OldArray[loipjp]*ifrac*jfrac;
122  }
123  }
124  ARR = fopenp (filename, "w");
125  fwrite (NewArray, sizeof(real), NRAD*NSEC, ARR);
126  fclose (ARR);
127  }
128  else {
129  mastererr("Could not rebin %s. File not found\n", filename);
130  }
131  }
132 }
double real
Definition of the type 'real' used throughout the code.
Definition: types.h:20
#define YES
Definition: types.h:46
char OUTPUTDIR[512]
Definition: param_noex.h:11
real GlobalRmed[MAX1D]
Definition: global.h:13
FILE * fopenp(char *string, char *mode)
Definition: LowTasks.c:163
static real New_r[MAX1D]
Definition: rebin.c:13
real Radii[MAX1D]
Definition: global.h:13
void ReadPrevDim()
Definition: rebin.c:16
static int OldNRAD
Definition: rebin.c:14
int GLOBALNRAD
Definition: global.h:10
static int OldNSEC
Definition: rebin.c:14
static real OldRmed[MAX1D]
Definition: rebin.c:13
int NSEC
Definition: param_noex.h:19
void CheckRebin(int nb)
Definition: rebin.c:43
#define NO
Definition: types.h:47
void mastererr(const char *template,...)
Definition: LowTasks.c:49
int NRAD
Definition: param_noex.h:18
Contains all the include directives requested by the code.
static real OldRadii[MAX1D]
Definition: rebin.c:13
boolean CPU_Master
Definition: global.h:3
void prs_exit(int numb)
Definition: LowTasks.c:33
#define MAX1D
Definition: types.h:65