MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
Functions
init.h File Reference
#include "domain.h"
#include "utils/paramset.h"
#include "timebinmgr.h"
#include "petaio.h"
Include dependency graph for init.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

inttime_t init (int RestartSnapNum, const char *OutputDir, struct header_data *header, Cosmology *CP)
 
void setup_smoothinglengths (int RestartSnapNum, DomainDecomp *ddecomp, Cosmology *CP, int BlackHoleOn, double MinEgySpec, double uu_in_cgs, const inttime_t Ti_Current, const double atime, const int64_t NTotGasInit)
 
void check_density_entropy (Cosmology *CP, const double MinEgySpec, const double atime)
 
void set_init_params (ParameterSet *ps)
 
void init_timeline (int RestartSnapNum, double TimeMax, const struct header_data *header, const int SnapshotWithFOF)
 

Function Documentation

◆ check_density_entropy()

void check_density_entropy ( Cosmology CP,
const double  MinEgySpec,
const double  atime 
)

Definition at line 344 of file init.c.

345 {
346  const double a3 = pow(atime, 3);
347  int i;
348  int bad = 0;
349  double meanbar = CP->OmegaBaryon * 3 * HUBBLE * CP->HubbleParam * HUBBLE * CP->HubbleParam/ (8 * M_PI * GRAVITY);
350  #pragma omp parallel for reduction(+: bad)
351  for(i = 0; i < SlotsManager->info[0].size; i++) {
352  /* This allows us to continue gracefully if
353  * there was some kind of bug in the run that output the snapshot.
354  * density() below will fix this up.*/
355  if(SphP[i].Density <= 0 || !isfinite(SphP[i].Density)) {
356  SphP[i].Density = meanbar;
357  bad++;
358  }
359  if(SphP[i].EgyWtDensity <= 0 || !isfinite(SphP[i].EgyWtDensity)) {
360  SphP[i].EgyWtDensity = SphP[i].Density;
361  }
362  double minent = GAMMA_MINUS1 * MinEgySpec / pow(SphP[i].Density / a3 , GAMMA_MINUS1);
363  if(SphP[i].Entropy < minent)
364  SphP[i].Entropy = minent;
365  }
366  MPI_Allreduce(MPI_IN_PLACE, &bad, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
367  if(bad > 0)
368  message(0, "Detected bad densities in %d particles on disc\n",bad);
369 }
void message(int where, const char *fmt,...)
Definition: endrun.c:175
#define HUBBLE
Definition: physconst.h:25
#define GRAVITY
Definition: physconst.h:5
#define GAMMA_MINUS1
Definition: physconst.h:35
static Cosmology * CP
Definition: power.c:27
struct slots_manager_type SlotsManager[1]
Definition: slotsmanager.c:7
#define SphP
Definition: slotsmanager.h:119
double HubbleParam
Definition: cosmology.h:20
double OmegaBaryon
Definition: cosmology.h:19
int64_t size
Definition: slotsmanager.h:12
struct slot_info info[6]
Definition: slotsmanager.h:112

References CP, GAMMA_MINUS1, GRAVITY, HUBBLE, Cosmology::HubbleParam, slots_manager_type::info, message(), Cosmology::OmegaBaryon, slot_info::size, SlotsManager, and SphP.

Referenced by begrun().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

inttime_t init ( int  RestartSnapNum,
const char *  OutputDir,
struct header_data header,
Cosmology CP 
)

This function reads the initial conditions, allocates storage for the particle data, validates and initialises the particle data.

Definition at line 86 of file init.c.

87 {
88  int i;
89 
91 
92  /*Read the snapshot*/
93  petaio_read_snapshot(RestartSnapNum, OutputDir, CP, header, PartManager, SlotsManager, MPI_COMM_WORLD);
94 
96 
98 
100 
101  double MeanSeparation[6] = {0};
102 
103  get_mean_separation(MeanSeparation, PartManager->BoxSize, header->NTotalInit);
104 
105  if(RestartSnapNum == -1)
106  check_smoothing_length(PartManager, MeanSeparation);
107 
108  /* As the above will mostly take place
109  * on Task 0, there will be a lot of imbalance*/
110  MPIU_Barrier(MPI_COMM_WORLD);
111 
112  gravshort_set_softenings(MeanSeparation[1]);
113  fof_init(MeanSeparation[1]);
114 
115  inttime_t Ti_Current = init_timebins(header->TimeSnapshot);
116 
117  #pragma omp parallel for
118  for(i = 0; i < PartManager->NumPart; i++) /* initialize sph_properties */
119  {
120  int j;
121  P[i].Ti_drift = Ti_Current;
122 
123  if(RestartSnapNum == -1 && P[i].Type == 5 )
124  {
125  /* Note: Gadget-3 sets this to the seed black hole mass.*/
126  BHP(i).Mass = P[i].Mass;
127  }
128 
129  if(P[i].Type == 4 )
130  {
131  /* Touch up zero star smoothing lengths, not saved in the snapshots.*/
132  if(P[i].Hsml == 0)
133  P[i].Hsml = 0.1 * MeanSeparation[0];
134  }
135 
136  if(P[i].Type == 5)
137  {
138  for(j = 0; j < 3; j++) {
139  BHP(i).DFAccel[j] = 0;
140  BHP(i).DragAccel[j] = 0;
141  }
142  }
143 
144  P[i].Key = PEANO(P[i].Pos, PartManager->BoxSize);
145 
146  if(P[i].Type != 0) continue;
147 
148  for(j = 0; j < 3; j++)
149  {
150  SPHP(i).HydroAccel[j] = 0;
151  }
152 
153  if(!isfinite(SPHP(i).DelayTime ))
154  endrun(6, "Bad DelayTime %g for part %d id %ld\n", SPHP(i).DelayTime, i, P[i].ID);
155  SPHP(i).DtEntropy = 0;
156 
157  if(RestartSnapNum == -1)
158  {
159  SPHP(i).Density = -1;
160  SPHP(i).EgyWtDensity = -1;
161  SPHP(i).DhsmlEgyDensityFactor = -1;
162  SPHP(i).Entropy = -1;
163  SPHP(i).Ne = 1.0;
164  SPHP(i).DivVel = 0;
165  SPHP(i).CurlVel = 0;
166  SPHP(i).DelayTime = 0;
167  SPHP(i).Metallicity = 0;
168  memset(SPHP(i).Metals, 0, NMETALS*sizeof(float));
169  /* Initialise to primordial abundances for H and He*/
170  SPHP(i).Metals[0] = HYDROGEN_MASSFRAC;
171  SPHP(i).Metals[1] = 1- HYDROGEN_MASSFRAC;
172  SPHP(i).Sfr = 0;
173  SPHP(i).MaxSignalVel = 0;
174  }
175  }
176  walltime_measure("/Init");
177  return Ti_Current;
178 }
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
void domain_test_id_uniqueness(struct part_manager_type *pman)
Definition: exchange.c:570
void fof_init(double DMMeanSeparation)
Definition: fof.c:66
void gravshort_set_softenings(double MeanDMSeparation)
static void check_omega(struct part_manager_type *PartManager, Cosmology *CP, int generations, double G, double *MassTable)
Definition: init.c:184
static struct init_params InitParams
static void check_smoothing_length(struct part_manager_type *PartManager, double *MeanSpacing)
Definition: init.c:323
static void check_positions(struct part_manager_type *PartManager)
Definition: init.c:294
static void init_alloc_particle_slot_memory(struct part_manager_type *PartManager, struct slots_manager_type *SlotsManager, const double PartAllocFactor, struct header_data *header, MPI_Comm Comm)
Definition: init.c:222
static void get_mean_separation(double *MeanSeparation, const double BoxSize, const int64_t *NTotalInit)
Definition: init.c:371
struct part_manager_type PartManager[1]
Definition: partmanager.c:11
#define P
Definition: partmanager.h:88
static peano_t PEANO(double *Pos, double BoxSize)
Definition: peano.h:14
void petaio_read_snapshot(int num, const char *OutputDir, Cosmology *CP, struct header_data *header, struct part_manager_type *PartManager, struct slots_manager_type *SlotsManager, MPI_Comm Comm)
Definition: petaio.c:255
#define HYDROGEN_MASSFRAC
Definition: physconst.h:37
int get_generations(void)
Definition: sfr_eff.c:87
#define BHP(i)
Definition: slotsmanager.h:125
#define SPHP(i)
Definition: slotsmanager.h:124
#define NMETALS
Definition: slotsmanager.h:60
double GravInternal
Definition: cosmology.h:32
int64_t NTotalInit[6]
Definition: petaio.h:20
double TimeSnapshot
Definition: petaio.h:26
double MassTable[6]
Definition: petaio.h:22
double PartAllocFactor
Definition: init.c:40
#define MPIU_Barrier(comm)
Definition: system.h:103
inttime_t init_timebins(double TimeInit)
Definition: timestep.c:123
int32_t inttime_t
Definition: types.h:8
#define walltime_measure(name)
Definition: walltime.h:8

References BHP, part_manager_type::BoxSize, check_omega(), check_positions(), check_smoothing_length(), CP, domain_test_id_uniqueness(), endrun(), fof_init(), get_generations(), get_mean_separation(), Cosmology::GravInternal, gravshort_set_softenings(), HYDROGEN_MASSFRAC, init_alloc_particle_slot_memory(), init_timebins(), InitParams, header_data::MassTable, MPIU_Barrier, NMETALS, header_data::NTotalInit, part_manager_type::NumPart, P, init_params::PartAllocFactor, PartManager, PEANO(), petaio_read_snapshot(), SlotsManager, SPHP, header_data::TimeSnapshot, and walltime_measure.

Referenced by begrun().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_timeline()

void init_timeline ( int  RestartSnapNum,
double  TimeMax,
const struct header_data header,
const int  SnapshotWithFOF 
)

Definition at line 56 of file init.c.

57 {
58  /*Add TimeInit and TimeMax to the output list*/
59  if (RestartSnapNum < 0) {
60  /* allow a first snapshot at IC time; */
61  setup_sync_points(header->TimeIC, TimeMax, 0.0, SnapshotWithFOF);
62  } else {
63  /* skip dumping the exactly same snapshot */
64  setup_sync_points(header->TimeIC, TimeMax, header->TimeSnapshot, SnapshotWithFOF);
65  /* If TimeInit is not in a sensible place on the integer timeline
66  * (can happen if the outputs changed since it was written)
67  * start the integer timeline anew from TimeInit */
68  inttime_t ti_init = ti_from_loga(log(header->TimeSnapshot)) % TIMEBASE;
69  if(round_down_power_of_two(ti_init) != ti_init) {
70  message(0,"Resetting integer timeline (as %x != %x) to current snapshot\n",ti_init, round_down_power_of_two(ti_init));
71  setup_sync_points(header->TimeSnapshot, TimeMax, header->TimeSnapshot, SnapshotWithFOF);
72  }
73  }
74 }
double TimeIC
Definition: petaio.h:24
inttime_t ti_from_loga(double loga)
Definition: timebinmgr.c:236
inttime_t round_down_power_of_two(inttime_t dti)
Definition: timebinmgr.c:286
void setup_sync_points(double TimeIC, double TimeMax, double no_snapshot_until_time, int SnapshotWithFOF)
Definition: timebinmgr.c:97
#define TIMEBASE
Definition: timebinmgr.h:14

References message(), round_down_power_of_two(), setup_sync_points(), ti_from_loga(), TIMEBASE, header_data::TimeIC, and header_data::TimeSnapshot.

Referenced by begrun().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_init_params()

void set_init_params ( ParameterSet ps)

Definition at line 45 of file init.c.

46 {
47  int ThisTask;
48  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
49  if(ThisTask == 0) {
50  InitParams.InitGasTemp = param_get_double(ps, "InitGasTemp");
51  InitParams.PartAllocFactor = param_get_double(ps, "PartAllocFactor");
52  }
53  MPI_Bcast(&InitParams, sizeof(InitParams), MPI_BYTE, 0, MPI_COMM_WORLD);
54 }
double param_get_double(ParameterSet *ps, const char *name)
Definition: paramset.c:336
double InitGasTemp
Definition: init.c:36
int ThisTask
Definition: test_exchange.c:23

References init_params::InitGasTemp, InitParams, param_get_double(), init_params::PartAllocFactor, and ThisTask.

Referenced by read_parameter_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_smoothinglengths()

void setup_smoothinglengths ( int  RestartSnapNum,
DomainDecomp ddecomp,
Cosmology CP,
int  BlackHoleOn,
double  MinEgySpec,
double  uu_in_cgs,
const inttime_t  Ti_Current,
const double  atime,
const int64_t  NTotGasInit 
)

This function is used to find an initial smoothing length and initial entropy for each SPH particle. Entropies are set using the initial gas temperature. It guarantees that the number of neighbours will be between desired_ngb-MAXDEV and desired_ngb+MAXDEV. For simplicity, a first guess of the smoothing length is provided to the function density(), which will then iterate if needed to find the right smoothing length.

Definition at line 439 of file init.c.

440 {
441  int i;
442  const double a3 = pow(atime, 3);
443 
444  if(RestartSnapNum >= 0)
445  return;
446 
447  if(InitParams.InitGasTemp < 0)
449 
450  const double MeanGasSeparation = PartManager->BoxSize / pow(NTotGasInit, 1.0 / 3);
451 
452  int64_t tot_sph, tot_bh;
453  MPI_Allreduce(&SlotsManager->info[0].size, &tot_sph, 1, MPI_INT64, MPI_SUM, MPI_COMM_WORLD);
454  MPI_Allreduce(&SlotsManager->info[5].size, &tot_bh, 1, MPI_INT64, MPI_SUM, MPI_COMM_WORLD);
455 
456  /* Do nothing if we are a pure DM run*/
457  if(tot_sph + tot_bh == 0)
458  return;
459 //
460 
461  ForceTree Tree = {0};
462  /* Need moments because we use them to set Hsml*/
463  force_tree_rebuild(&Tree, ddecomp, 0, 1, NULL);
464 
465  /* quick hack to adjust for the baryon fraction
466  * only this fraction of mass is of that type.
467  * this won't work for non-dm non baryon;
468  * ideally each node shall have separate count of
469  * ptypes of each type.
470  *
471  * Eventually the iteration will fix this. */
472  const double massfactor = CP->OmegaBaryon / CP->Omega0;
473  const double DesNumNgb = GetNumNgb(GetDensityKernelType());
474 
475  #pragma omp parallel for
476  for(i = 0; i < PartManager->NumPart; i++)
477  {
478  /* These initial smoothing lengths are only used for SPH-like particles.*/
479  if(P[i].Type != 0 && P[i].Type != 5)
480  continue;
481 
482  int no = force_get_father(i, &Tree);
483 
484  while(10 * DesNumNgb * P[i].Mass > massfactor * Tree.Nodes[no].mom.mass)
485  {
486  int p = force_get_father(no, &Tree);
487 
488  if(p < 0)
489  break;
490 
491  no = p;
492  }
493 
494  P[i].Hsml =
495  pow(3.0 / (4 * M_PI) * DesNumNgb * P[i].Mass / (massfactor * Tree.Nodes[no].mom.mass),
496  1.0 / 3) * Tree.Nodes[no].len;
497 
498  /* recover from a poor initial guess */
499  if(P[i].Hsml > 500.0 * MeanGasSeparation)
500  P[i].Hsml = MeanGasSeparation;
501 
502  if(P[i].Hsml <= 0)
503  endrun(5, "Bad hsml guess: i=%d, mass = %g type %d hsml %g no %d len %d treemass %g\n",
504  i, P[i].Mass, P[i].Type, P[i].Hsml, no, Tree.Nodes[no].len, Tree.Nodes[no].mom.mass);
505  }
506 
507  /* for clean IC with U input only, we need to iterate to find entropy */
508  double u_init = (1.0 / GAMMA_MINUS1) * (BOLTZMANN / PROTONMASS) * InitParams.InitGasTemp;
509  u_init /= uu_in_cgs; /* unit conversion */
510 
511  double molecular_weight;
512  if(InitParams.InitGasTemp > 1.0e4) /* assuming FULL ionization */
513  molecular_weight = 4 / (8 - 5 * (1 - HYDROGEN_MASSFRAC));
514  else /* assuming NEUTRAL GAS */
515  molecular_weight = 4 / (1 + 3 * HYDROGEN_MASSFRAC);
516  u_init /= molecular_weight;
517 
518  if(u_init < MinEgySpec)
519  u_init = MinEgySpec;
520  /* snapshot already has EgyWtDensity; hope it is read in correctly.
521  * (need a test on this!) */
522  /*Allocate the extra SPH data for transient SPH particle properties.*/
524 
525  /*At the first time step all particles should be active*/
526  ActiveParticles act = {0};
527  act.ActiveParticle = NULL;
529 
530  /* Empty kick factors as we do not move*/
531  DriftKickTimes times = init_driftkicktime(Ti_Current);
532  density(&act, 1, 0, BlackHoleOn, 0, times, CP, &sph_pred, NULL, &Tree);
533 
535  setup_density_indep_entropy(&act, &Tree, CP, &sph_pred, u_init, a3, Ti_Current, BlackHoleOn);
536  }
537  else {
538  /*Initialize to initial energy*/
539  #pragma omp parallel for
540  for(i = 0; i < SlotsManager->info[0].size; i++)
541  SphP[i].Entropy = GAMMA_MINUS1 * u_init / pow(SphP[i].Density / a3 , GAMMA_MINUS1);
542  }
543  slots_free_sph_pred_data(&sph_pred);
544  force_tree_free(&Tree);
545 }
double GetNumNgb(enum DensityKernelType KernelType)
Definition: density.c:55
struct sph_pred_data slots_allocate_sph_pred_data(int nsph)
Definition: density.c:654
void slots_free_sph_pred_data(struct sph_pred_data *sph_scratch)
Definition: density.c:665
enum DensityKernelType GetDensityKernelType(void)
Definition: density.c:63
void density(const ActiveParticles *act, int update_hsml, int DoEgyDensity, int BlackHoleOn, double MinEgySpec, const DriftKickTimes times, Cosmology *CP, struct sph_pred_data *SPH_predicted, MyFloat *GradRho, const ForceTree *const tree)
Definition: density.c:203
void force_tree_rebuild(ForceTree *tree, DomainDecomp *ddecomp, const int HybridNuGrav, const int DoMoments, const char *EmergencyOutputDir)
Definition: forcetree.c:140
void force_tree_free(ForceTree *tree)
Definition: forcetree.c:1409
int force_get_father(int no, const ForceTree *tree)
Definition: forcetree.c:905
int DensityIndependentSphOn(void)
Definition: hydra.c:49
static void setup_density_indep_entropy(const ActiveParticles *act, ForceTree *Tree, Cosmology *CP, struct sph_pred_data *sph_pred, double u_init, double a3, int BlackHoleOn, const inttime_t Ti_Current)
Definition: init.c:384
#define PROTONMASS
Definition: physconst.h:21
#define BOLTZMANN
Definition: physconst.h:11
int * ActiveParticle
Definition: timestep.h:13
int64_t NumActiveParticle
Definition: timestep.h:12
double Omega0
Definition: cosmology.h:10
double CMBTemperature
Definition: cosmology.h:9
struct NODE * Nodes
Definition: forcetree.h:97
struct NODE::@9 mom
MyFloat mass
Definition: forcetree.h:56
MyFloat len
Definition: forcetree.h:42
#define MPI_INT64
Definition: system.h:12
DriftKickTimes init_driftkicktime(inttime_t Ti_Current)
Definition: timestep.c:133

References ActiveParticles::ActiveParticle, BOLTZMANN, part_manager_type::BoxSize, Cosmology::CMBTemperature, CP, density(), DensityIndependentSphOn(), endrun(), force_get_father(), force_tree_free(), force_tree_rebuild(), GAMMA_MINUS1, GetDensityKernelType(), GetNumNgb(), HYDROGEN_MASSFRAC, slots_manager_type::info, init_driftkicktime(), init_params::InitGasTemp, InitParams, NODE::len, NODE::mass, NODE::mom, MPI_INT64, ForceTree::Nodes, ActiveParticles::NumActiveParticle, part_manager_type::NumPart, Cosmology::Omega0, Cosmology::OmegaBaryon, P, PartManager, PROTONMASS, setup_density_indep_entropy(), slot_info::size, slots_allocate_sph_pred_data(), slots_free_sph_pred_data(), SlotsManager, and SphP.

Referenced by begrun().

Here is the call graph for this function:
Here is the caller graph for this function: