The FARGO_THORIN code developer's guide
 All Data Structures Files Functions Variables Typedefs Macros
main.c
Go to the documentation of this file.
1 /** \file main.c
2 
3 Main file of the distribution. Manages the call to initialization
4 functions, then the main loop.
5 
6 @author THORIN modifications by
7 Ondřej Chrenko <chrenko@sirrah.troja.mff.cuni.cz>, Copyright (C) 2017;
8 original code by Frédéric Masset
9 
10 */
11 
12 #include "fargo.h"
13 
14 boolean Restart = NO, OpenInner = NO;
15 int begin_i = 0, NbRestart = 0;
17 extern real LostMass;
18 extern boolean Corotating;
20 
21 boolean DumpTorqueNow = NO, DumpTorqueDensNow = NO; /* #THORIN: output control switches */
22 
23 int
24 main(argc, argv)
25 int argc;
26 char *argv[];
27 {
28  PolarGrid *gas_density, *gas_v_rad, *gas_v_theta, *gas_label;
29  PolarGrid *gas_energy; /* #THORIN */
30  int i;
31  boolean disable = NO, TimeInfo = NO, Profiling = NO;
32  boolean TimeToWrite, verbose = NO;
33  TimeProcess t_Hydro;
34  char ParameterFile[256];
35  PlanetarySystem *sys;
36  struct reb_simulation *rsim; /* #THORIN: structure holds a rebound simulation coupled with FARGO */
37  int npl;
38  MPI_Init (&argc, &argv);
41  CPU_Master = (CPU_Rank == 0 ? 1 : 0);
42 #ifdef OPENMP /* #THORIN: control of the OpenMP threading */
43  omp_set_dynamic (0); /* disable automatic adjustment of the number of threads */
44  int numproc = omp_get_num_procs (); /* find available number of cores */
45  omp_set_num_threads (numproc); /* set it */
46 #endif /* OPENMP */ /* <--- */
47  setfpe (); /* Control behavior for floating point
48  exceptions trapping (default is not to do anything) */
49  if (argc == 1) PrintUsage (argv[0]);
50  strcpy (ParameterFile, "");
51  for (i = 1; i < argc; i++) {
52  if (*(argv[i]) == '-') {
53  if (strspn (argv[i], "-secndovtpfamzib0123456789") != strlen (argv[i]))
54  PrintUsage (argv[0]);
55  if (strchr (argv[i], 'n'))
56  disable = YES;
57  if (strchr (argv[i], 'v'))
58  verbose = YES;
59  if (strchr (argv[i], 't'))
60  TimeInfo = YES;
61  if (strchr (argv[i], 'c'))
62  SloppyCFL = YES;
63  if (strchr (argv[i], 'p'))
64  Profiling = YES;
65  if (strchr (argv[i], 'd'))
66  debug = YES;
67  if (strchr (argv[i], 'b'))
69  if (strchr (argv[i], 'm'))
70  Merge = YES;
71  if (strchr (argv[i], 'a'))
73  if (strchr (argv[i], 'z'))
75  if (strchr (argv[i], 'i'))
76  StoreSigma = YES;
77  if (EnergyEq) StoreEnergy = YES; /* #THORIN */
78  if (strchr (argv[i], '0'))
79  OnlyInit = YES;
80  if ((argv[i][1] >= '1') && (argv[i][1] <= '9')) {
82  StillWriteOneOutput = (int)(argv[i][1]-'0');
83  }
84  if (strchr (argv[i], 's')) {
85  Restart = YES;
86  i++;
87  NbRestart = atoi(argv[i]);
88  if ((NbRestart < 0)) {
89  masterprint ("Incorrect restart number\n");
90  PrintUsage (argv[0]);
91  }
92  }
93  if (strchr (argv[i], 'o')) {
95  i++;
96  sprintf (NewOutputdir, "%s", argv[i]);
97  } else {
98  if (strchr (argv[i], 'f')) {
99  i++;
100  ScalingFactor = atof(argv[i]);
101  masterprint ("Scaling factor = %g\n", ScalingFactor);
102  if ((ScalingFactor <= 0)) {
103  masterprint ("Incorrect scaling factor\n");
104  PrintUsage (argv[0]);
105  }
106  }
107  }
108  }
109  else strcpy (ParameterFile, argv[i]);
110  }
111  if ((StoreSigma || StoreEnergy) && !(Restart)) { /* #THORIN */
112  mastererr ("You cannot use tabulated surface density\n");
113  mastererr ("or surface internal energy in a non-restart run.\n");
114  mastererr ("Aborted\n");
115  prs_exit (0);
116  }
117  if (ParameterFile[0] == 0) PrintUsage (argv[0]);
118 #ifdef OPENMP /* #THORIN: print info in the case of a multithreaded run */
119  if (CPU_Number == 1) { /* 2DO this wont work with MPI, would have to send info about num of threads from each node to the master */
120  masterprint ("\n\n----------\n");
121  masterprint ("\033[1mMultithreading enabled!\033[0m\n");
122  masterprint ("Number of threads available to parallel constructs: %d\n", omp_get_max_threads());
123  masterprint ("----------\n\n");
124  }
125 #endif /* OPENMP <--- */
126  ReadVariables (ParameterFile); /* #THORIN: InitPlanetarySystem() and ListPlanets() used to be here, replaced by functions of the Rebound interface */
127  SplitDomain ();
128  if (verbose == YES)
129  TellEverything ();
130  if (disable == YES)
131  prs_exit (0);
132  MakeDir (OUTPUTDIR);
133  DumpSources (argc, argv);
134  masterprint ("Allocating arrays...");
135  fflush (stdout);
136  gas_density = CreatePolarGrid(NRAD, NSEC, "dens");
137  gas_v_rad = CreatePolarGrid(NRAD, NSEC, "vrad");
138  gas_v_theta = CreatePolarGrid(NRAD, NSEC, "vtheta");
139  gas_energy = CreatePolarGrid(NRAD, NSEC, "energy"); /* #THORIN */
140  gas_label = CreatePolarGrid(NRAD, NSEC, "label");
141  masterprint ("done.\n");
142  /* #THORIN ---> */
143  npl = FindNumberOfPlanets (PLANETCONFIG); /* see Psys.c */
144  if (CPU_Master) printf ("%d planet(s) found.\n", npl);
145  sys = AllocPlanetSystem (npl); /* see Psys.c */
146  sys->nb = npl;
147  if (Restart == YES) {
149  rsim = RestartReboundSimulation (sys, NbRestart);
150  ListPlanets (sys);
151  } else {
152  rsim = SetupReboundSimulation (sys, PLANETCONFIG);
153  ListPlanets (sys);
154  }
155  /* <--- #THORIN */
157  if (Corotating == YES) OmegaFrame = GetPsysInfo (sys, FREQUENCY);
158  Initialization (gas_density, gas_v_rad, gas_v_theta, gas_energy, gas_label); /* #THORIN */
159  InitComputeAccel ();
160  if (Restart) OmegaFrame = GetOmegaFrame (NbRestart); /* #THORIN */
163  for (i = begin_i; i <= NTOT; i++) {
165  if (InnerOutputCounter == 1) {
166  InnerOutputCounter = 0;
167  if (WriteTorque) DumpTorqueNow=YES; /* #THORIN: DumpTorqueNow will allow for the torque calculation in FillForcesArrays () */
168  }
169  if (NINTERM * (TimeStep = (i / NINTERM)) == i) /* Outputs are done here */ {
170  TimeToWrite = YES;
171  SendOutput (TimeStep, gas_density, gas_v_rad, gas_v_theta, gas_energy, gas_label); /* #THORIN: see Output.c */
172  OutputNbodySimulation (TimeStep, rsim); /* #THORIN: output binary file with the N-body part settings */
173  if (WriteTorqueMapFile) CreateTorqueMapInfile (TimeStep, gas_density); /* #THORIN */
174  if (TorqueDensity) DumpTorqueDensNow=YES; /* #THORIN */
175  DumpOmegaFrame (TimeStep); /* #THORIN: print OmegaFrame - it is needed for restart runs */
176  if ((OnlyInit) || ((GotoNextOutput) && (!StillWriteOneOutput))) {
177  MPI_Finalize();
178  return 0;
179  }
181  if (TimeInfo == YES) /* Time monitoring is done here */
183  }
184  else {
185  TimeToWrite = NO;
186  }
187  /* Algorithm loop begins here */
188 
189  /***********************/
190  /* Hydrodynamical Part */
191  /***********************/
192  InitSpecificTime (Profiling, &t_Hydro, "Eulerian Hydro algorithms");
193  AlgoGas (gas_density, gas_v_rad, gas_v_theta, gas_energy, gas_label, sys, rsim); /* #THORIN: see SourceEuler.c */
194  GiveSpecificTime (Profiling, t_Hydro);
195  if (NOUTELEMENTS * ((i+1) /NOUTELEMENTS) == (i+1)) { /* #THORIN */
196  OutputElements (rsim);
197  fflush (plout);
198  fflush (discard);
199  if (Collisions == YES) fflush (mergers);
200  }
201  if (MonitorNPL == YES) { /* #THORIN: If the target number of planets is reached, terminate the run */
202  if (sys->nb <= TARGETNPL) {
203  masterprint ("Target number of planets %d was reached. Normal termination.\n", TARGETNPL);
204  break;
205  }
206  }
207  /* <-- */
208  if (MonitorIntegral == YES) {
209  masterprint ("Gas Momentum : %.18g\n", GasMomentum (gas_density, gas_v_theta));
210  masterprint ("Gas total Mass : %.18g\n", GasTotalMass (gas_density));
211  masterprint ("Gas total Energy: %.18g\n", GasTotalEnergy (gas_density, gas_v_rad, gas_v_theta, gas_energy)); /* #THORIN: see SideEuler.c */
212  }
213  }
214  reb_free_simulation (rsim); /* #THORIN */
215  FreePlanetary (sys);
216  fclose (plout); /* #THORIN */
217  fclose (discard);
218  if (Collisions==YES) fclose (mergers);
219  MPI_Finalize ();
220  return 0;
221 }
PlanetarySystem * AllocPlanetSystem()
void PrintUsage(char *execname)
Definition: Interpret.c:238
int TARGETNPL
Definition: param_noex.h:74
double real
Definition of the type 'real' used throughout the code.
Definition: types.h:20
void TellEverything()
Definition: Interpret.c:281
void MultiplyPolarGridbyConstant(PolarGrid *arraysrc, real constant)
Definition: LowTasks.c:106
boolean OpenInner
Definition: main.c:14
real LostMass
int CPU_Number
Definition: global.h:2
#define YES
Definition: types.h:46
boolean MonitorIntegral
Definition: global.h:29
This structure is used for monitoring CPU time usage.
Definition: types.h:86
char PLANETCONFIG[512]
Definition: param_noex.h:15
boolean DumpTorqueNow
Definition: main.c:21
real GasTotalMass()
char OUTPUTDIR[512]
Definition: param_noex.h:11
boolean Collisions
Definition: global.h:26
PolarGrid * CreatePolarGrid(int Nr, int Ns, char *name)
Definition: LowTasks.c:73
int begin_i
Definition: main.c:15
int NINTERM
Definition: param_noex.h:9
boolean FakeSequential
Definition: global.h:29
real GetOmegaFrame(int TimeStep)
Finds the angular velocity of the coordinate system in 'omegaframe.dat' at a given TimeStep...
Definition: Output.c:279
boolean StoreEnergy
Definition: global.h:24
int main(int argc, argv)
Definition: main.c:24
A structure used to store any scalar fied on the computational domain.
Definition: types.h:37
boolean WriteTorqueMapFile
Definition: global.h:26
void DumpSources(int argc, argv)
Definition: LowTasks.c:123
boolean Corotating
Definition: Interpret.c:30
void FreePlanetary()
Contains all the information about a planetary system at a given instant in time. ...
Definition: types.h:96
real OMEGAFRAME
Definition: param_noex.h:30
int NbRestart
Definition: main.c:15
int nb
Number of planets.
Definition: types.h:97
boolean Restart
Definition: main.c:14
void AlgoGas()
boolean GotoNextOutput
Definition: global.h:30
boolean EnergyEq
Definition: global.h:24
real PhysicalTime
Definition: global.h:20
boolean StoreSigma
Definition: global.h:30
void CreateTorqueMapInfile(int istep, PolarGrid *Surfdens)
Writes an input file for the 'torquemap' code written by Bertram Bitsch.
void MPI_Finalize()
Definition: mpi_dummy.c:40
#define FREQUENCY
Definition: types.h:60
void MPI_Comm_size(int a, int *b)
Definition: mpi_dummy.c:20
boolean CentrifugalBalance
Definition: global.h:31
int TimeStep
Definition: global.h:23
boolean TorqueDensity
Definition: global.h:28
FILE * discard
Definition: global.h:44
int FindNumberOfPlanets()
void InitComputeAccel()
Definition: SideEuler.c:93
void MPI_Comm_rank(int a, int *b)
Definition: mpi_dummy.c:13
void SendOutput(int index, PolarGrid *dens, PolarGrid *gasvr, PolarGrid *gasvt, PolarGrid *gasenerg, PolarGrid *label)
Definition: Output.c:202
boolean SloppyCFL
Definition: global.h:31
real OmegaFrame
Definition: global.h:20
void MakeDir(char *string)
Definition: LowTasks.c:142
void GiveTimeInfo(int number)
Definition: Interpret.c:358
int NSEC
Definition: param_noex.h:19
real GasTotalEnergy()
void SplitDomain()
Definition: split.c:24
void OutputElements()
FILE * mergers
Definition: global.h:44
int NOUTELEMENTS
Definition: param_noex.h:71
static int InnerOutputCounter
Definition: main.c:16
void setfpe()
Definition: fpe.c:15
real PhysicalTimeInitial
Definition: global.h:20
#define NO
Definition: types.h:47
void mastererr(const char *template,...)
Definition: LowTasks.c:49
boolean MonitorNPL
Definition: global.h:26
struct reb_simulation * SetupReboundSimulation()
int NRAD
Definition: param_noex.h:18
boolean DumpTorqueDensNow
Definition: main.c:21
Contains all the include directives requested by the code.
void ListPlanets()
boolean Merge
Definition: global.h:29
char NewOutputdir[1024]
Definition: global.h:43
struct reb_simulation * RestartReboundSimulation()
int CPU_Rank
Definition: global.h:1
FILE * plout
Definition: global.h:44
boolean OnlyInit
Definition: global.h:29
void DumpOmegaFrame(int TimeStep)
Writes the angular velocity of the coordinate system.
Definition: Output.c:259
boolean debug
Definition: global.h:29
real GetPsysInfo()
void GiveSpecificTime(boolean profiling, TimeProcess process_name)
Definition: Interpret.c:402
boolean WriteTorque
Definition: global.h:26
void ReadVariables(char *filename)
Definition: Interpret.c:65
void OutputNbodySimulation()
boolean CPU_Master
Definition: global.h:3
void InitSpecificTime(boolean profiling, TimeProcess *process_name, char *title)
Definition: Interpret.c:389
int NTOT
Definition: param_noex.h:10
boolean OverridesOutputdir
Definition: global.h:42
#define MPI_COMM_WORLD
Definition: mpi_dummy.h:10
void prs_exit(int numb)
Definition: LowTasks.c:33
real ScalingFactor
Definition: main.c:19
static int StillWriteOneOutput
Definition: main.c:16
void masterprint(const char *template,...)
Definition: LowTasks.c:40
void Initialization(PolarGrid *gas_density, PolarGrid *gas_v_rad, PolarGrid *gas_v_theta, PolarGrid *gas_energy, PolarGrid *gas_label)
Definition: Init.c:72
void MPI_Init(int *argc, argv)
Definition: mpi_dummy.c:27
real GasMomentum()