The FARGO_THORIN code developer's guide
 All Data Structures Files Functions Variables Typedefs Macros
Init.c
Go to the documentation of this file.
1 /** \file Init.c
2 
3 Contains the functions needed to initialize the hydrodynamics arrays.
4 These can be initialized by reading a given output (in the case of a
5 restart) or by calling a function, InitEuler (), which contains
6 analytic prescription for the different hydrodynamics fields. Note
7 that this function InitEuler() is located in SourceEuler.c, which
8 itself calls InitGas(), in the file Pframeforce.c.
9 Also, note that the present file contains InitLabel(), which sets
10 the initial value of a passive scalar.
11 
12 @author THORIN modifications by
13 Ondřej Chrenko <chrenko@sirrah.troja.mff.cuni.cz>, Copyright (C) 2017;
14 original code by Frédéric Masset
15 
16 */
17 
18 #include "fargo.h"
19 
20 extern boolean Restart;
21 extern int NbRestart;
22 
23 void ReadfromFile (array, fileprefix, filenumber)
24 PolarGrid *array;
25 char *fileprefix;
26 int filenumber;
27 {
28  int nr,ns,c, foo=0;
29  real *field;
30  char name[256];
31  FILE *input;
32 /* Simultaneous read access to the same file have been observed to give wrong results. */
33 /* A sequential reading is imposed below. */
34  /* If current CPU has a predecessor, wait for a message from him */
35  if (CPU_Rank > 0) MPI_Recv (&foo, 1, MPI_INT, CPU_Rank-1, 10, MPI_COMM_WORLD, &fargostat);
36  sprintf (name, "%s%s%d.dat", OUTPUTDIR, fileprefix, filenumber);
37  input = fopen (name, "r");
38  if (input == NULL) {
39  fprintf (stderr, "WARNING ! Can't read %s. Restarting with t=0 settings.\n", name);
40  if (CPU_Rank < CPU_Number-1) MPI_Send (&foo, 1, MPI_INT, CPU_Rank+1, 10, MPI_COMM_WORLD);
41  return;
42  }
43  field = array->Field;
44  nr = array->Nrad;
45  ns = array->Nsec;
46  for (c = 0; c < IMIN; c++) {
47  fread (field, sizeof(real), ns, input); /* Can't read at once in order not to overflow 'field' */
48  }
49  fread (field, sizeof(real), nr*ns, input);
50  fclose (input);
51  /* Next CPU is waiting. Tell it to start now by sending the message that it expects */
52  if (CPU_Rank < CPU_Number-1) MPI_Send (&foo, 1, MPI_INT, CPU_Rank+1, 10, MPI_COMM_WORLD);
53  MPI_Barrier (MPI_COMM_WORLD); /* previous CPUs do not touch anything meanwhile */
54 }
55 
56 void InitLabel (array)
57 PolarGrid *array;
58 {
59  int nr,ns,i,j,l;
60  real *field;
61  field = array->Field;
62  nr = array->Nrad;
63  ns = array->Nsec;
64  for (i = 0; i <= nr; i++) {
65  for (j = 0; j < ns; j++) {
66  l = j+i*ns;
67  field[l] = (Rmed[i]-RMIN)/(RMAX-RMIN);
68  }
69  }
70 }
71 
72 void Initialization (gas_density, gas_v_rad, gas_v_theta, gas_energy, gas_label) /* #THORIN */
73 PolarGrid *gas_density, *gas_v_rad, *gas_v_theta, *gas_energy, *gas_label;
74 {
75  int i, j, l, nr, ns;
76  real *rho, *energy;
77  /* ----- */
78  ReadPrevDim ();
79  InitEuler (gas_density, gas_v_rad, gas_v_theta, gas_energy);
80  InitLabel (gas_label);
81  if (Restart || InitFromFile) {
82  nr = gas_density->Nrad;
83  ns = gas_density->Nsec;
84  rho = gas_density->Field;
85  energy = gas_energy->Field;
86  if (InitFromFile) {
87  if (!Restart) {
88  mastererr ("Initializing from files...\n");
89  } else {
90  mastererr ("Reading initial files to refill boundary zones for the damping condition (if used)...\n");
91  }
92  fflush (stderr);
93  ReadfromAsciiFile (gas_density, DENSINFILE);
94  ReadfromAsciiFile (gas_v_rad, VRADINFILE);
95  ReadfromAsciiFile (gas_v_theta, VTHETAINFILE);
96  if (EnergyEq) {
97  ReadfromAsciiFile (gas_energy, TEMPERINFILE); // 2DO option for gas label should be included
98  for (i=0; i<nr; i++) { /* !At this point, we have temperature values in the energy HD field */
99  for (j=0; j<ns; j++) {
100  l = j + i*ns;
101  energy[l] = energy[l]*rho[l]/(ADIABIND-1.0); /* E=(T*\rho)/(\gamma-1) */
102  }
103  }
104  }
105  }
106  RefillSigma (gas_density); /* this is needed for BC only - even when restarting, these field reflect the INITIAL conditions */
107  if (EnergyEq) RefillEnergy (gas_energy); /* see Theo.c */
108  if (DampInit) FillVtheta (gas_v_theta); /* needed for damping BC */
109  if (Restart) { /* if Restart==YES, it overrides InitFromFile option */
111  MPI_Barrier (MPI_COMM_WORLD); /* Don't start reading before master has finished rebining... */
112  /* It shouldn't be a problem though since a sequential read is */
113  /* imposed in the ReadfromFile function below */
114  mastererr ("Reading restart files...\n");
115  fflush (stderr);
116  ReadfromFile (gas_density, "gasdens", NbRestart);
117  ReadfromFile (gas_v_rad, "gasvrad", NbRestart);
118  ReadfromFile (gas_v_theta, "gasvtheta", NbRestart);
119  if (EnergyEq) { /* !At this point, we have temperature values in the energy HD field */
120  ReadfromFile (gas_energy, "gastemper", NbRestart);
121  for (i=0; i<nr; i++) {
122  for (j=0; j<ns; j++) {
123  l = j + i*ns;
124  energy[l] = energy[l]*rho[l]/(ADIABIND-1.0); /* E=(T*\rho)/(\gamma-1) */
125  }
126  }
127  }
128  ReadfromFile (gas_label, "gaslabel", NbRestart);
129  }
130  /* #THORIN: following lines (in similar order to InitEuler()) are new - we need to update cs,P,T using rho and energy read at restart/or from files */
131  ComputeSoundSpeed (gas_density, gas_energy);
132  ComputePressureField (gas_density, gas_energy);
133  ComputeTemperatureField (gas_density, gas_energy);
134  fprintf (stderr, "done\n");
135  fflush (stderr);
136  }
137  WriteDim ();
138  if (Pebbles) {
139  if (Restart) {
140  RestartPebbleDisk (gas_density, NbRestart); // done in case of restart
141  } else {
142  EquilPebbleDisk (gas_density, gas_v_rad, gas_v_theta); // done in standard runs or if the HD fields are initialized from files
143  }
144  }
145 }
146 
147 /** Enables to read a polar grid array from an ascii file */
148 void ReadfromAsciiFile (array, path) /* #THORIN */
149 PolarGrid *array;
150 char *path;
151 {
152  char ignore[512], *err;
153  int foo=0, nr, ns, i, j, l, chck;
154  real *field, tmp;
155  FILE *input;
156  /* If current CPU has a predecessor, wait for a message from him */
157  if (CPU_Rank > 0) MPI_Recv (&foo, 1, MPI_INT, CPU_Rank-1, 10, MPI_COMM_WORLD, &fargostat);
158  input = fopen (path, "r");
159  if (input == NULL) {
160  fprintf (stderr, "\nError! Can't find the initialization file %s \n", path);
161  exit (1);
162  }
163  field = array->Field;
164  nr = array->Nrad;
165  ns = array->Nsec;
166  for (i = 0; i < IMIN; i++) {
167  for (j = 0; j < ns; j++) {
168  err = fgets (ignore, sizeof(ignore), input);
169  if (err == NULL) {
170  fprintf (stderr, "\nError! Number of values in the initialization file %s\n", path);
171  fprintf (stderr, "is smaller than the size of hydrodynamic arrays.\n");
172  exit(1);
173  }
174  }
175  }
176  for (i = 0; i < nr; i++) {
177  for (j = 0; j < ns; j++) {
178  l = j + i*ns;
179  chck = fscanf (input, "%lf", &field[l]);
180  if (chck == EOF) {
181  fprintf (stderr, "\nError! Number of values in the initialization file %s\n", path);
182  fprintf (stderr, "is smaller than the size of hydrodynamic arrays.\n");
183  exit(1);
184  }
185  }
186  }
187  if (CPU_Rank == CPU_Number-1) {
188  chck = fscanf (input, "%lf", &tmp);
189  if (chck != EOF) {
190  fprintf (stderr, "\nError! Number of values in the initialization file %s\n", path);
191  fprintf (stderr, "is larger than the size of hydrodynamic arrays.\n");
192  exit(1);
193  }
194  }
195  fclose (input);
196  /* Next CPU is waiting. Tell it to start now by sending the message that it expects */
197  if (CPU_Rank < CPU_Number-1) MPI_Send (&foo, 1, MPI_INT, CPU_Rank+1, 10, MPI_COMM_WORLD);
198  MPI_Barrier (MPI_COMM_WORLD); /* previous CPUs do not touch anything meanwhile */
199 }
double real
Definition of the type 'real' used throughout the code.
Definition: types.h:20
MPI_Status fargostat
Definition: global.h:32
void MPI_Send()
Definition: mpi_dummy.c:56
int CPU_Number
Definition: global.h:2
boolean Restart
Definition: main.c:14
char OUTPUTDIR[512]
Definition: param_noex.h:11
void EquilPebbleDisk(PolarGrid *Rho, PolarGrid *Vrad, PolarGrid *Vtheta)
Sets up a pebble disk in a coagulation-drift equilibrium.
Definition: Pebbles.c:58
void ComputePressureField()
void RestartPebbleDisk(PolarGrid *Rho, int index)
Reads arrays necessary to restart the pebble disk.
Definition: Pebbles.c:66
A structure used to store any scalar fied on the computational domain.
Definition: types.h:37
boolean DampInit
Definition: global.h:24
void MPI_Barrier()
Definition: mpi_dummy.c:64
real * Field
Definition: types.h:40
void RefillEnergy()
real Rmed[MAX1D]
Definition: global.h:11
char VTHETAINFILE[512]
Definition: param_noex.h:65
char DENSINFILE[512]
Definition: param_noex.h:63
void RefillSigma()
boolean EnergyEq
Definition: global.h:24
int IMIN
Definition: global.h:4
char TEMPERINFILE[512]
Definition: param_noex.h:66
void WriteDim()
Definition: Output.c:191
int NbRestart
Definition: main.c:15
void CheckRebin()
#define MPI_INT
Definition: mpi_dummy.h:14
real RMIN
Definition: param_noex.h:20
void ReadPrevDim()
Definition: rebin.c:16
real RMAX
Definition: param_noex.h:21
void mastererr(const char *template,...)
Definition: LowTasks.c:49
void MPI_Recv()
Definition: mpi_dummy.c:60
real ADIABIND
Definition: param_noex.h:54
boolean InitFromFile
Definition: global.h:25
void InitEuler()
void FillVtheta()
Contains all the include directives requested by the code.
void ReadfromFile(PolarGrid *array, char *fileprefix, int filenumber)
Definition: Init.c:23
int CPU_Rank
Definition: global.h:1
void ComputeTemperatureField()
void ReadfromAsciiFile(PolarGrid *array, char *path)
Enables to read a polar grid array from an ascii file.
Definition: Init.c:148
void ComputeSoundSpeed()
char VRADINFILE[512]
Definition: param_noex.h:64
#define MPI_COMM_WORLD
Definition: mpi_dummy.h:10
void Initialization(PolarGrid *gas_density, PolarGrid *gas_v_rad, PolarGrid *gas_v_theta, PolarGrid *gas_energy, PolarGrid *gas_label)
Definition: Init.c:72
boolean Pebbles
Definition: global.h:27
void InitLabel(PolarGrid *array)
Definition: Init.c:56