init.cc 21.2 KB
Newer Older
Volker Springel's avatar
Volker Springel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*******************************************************************************
 * \copyright   This file is part of the GADGET4 N-body/SPH code developed
 * \copyright   by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
 * \copyright   (vspringel@mpa-garching.mpg.de) and all contributing authors.
 *******************************************************************************/

/*! \file init.cc
 *
 *  \brief code for initialization of a simulation from initial conditions
 */

#include "gadgetconfig.h"

#include <mpi.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include "../cooling_sfr/cooling.h"
#include "../data/allvars.h"
#include "../data/dtypes.h"
#include "../data/mymalloc.h"
#include "../domain/domain.h"
#include "../fof/fof.h"
#include "../gravtree/gravtree.h"
#include "../io/io.h"
#include "../io/snap_io.h"
#include "../logs/timer.h"
#include "../main/main.h"
#include "../main/simulation.h"
#include "../mpi_utils/mpi_utils.h"
#include "../ngbtree/ngbtree.h"
#include "../ngenic/ngenic.h"
#include "../pm/pm.h"
#include "../sort/parallel_sort.h"
#include "../subfind/subfind_readid_io.h"
#include "../system/system.h"
#include "../time_integration/timestep.h"

using namespace std;

/*! \brief Prepares the loaded initial conditions for the run
 *
 *  It is only called if RestartFlag != RST_RESUME. Various counters and variables are initialized.
 *  Entries of the particle data structures not read from initial conditions are
 *  initialized or converted and a initial domain decomposition is performed.
 *  If SPH particles are present, the initial SPH smoothing lengths are determined.
 */
void sim::init(int RestartSnapNum)
{
#ifdef NGENIC
  if(All.RestartFlag == RST_CREATEICS || All.RestartFlag == RST_BEGIN)
    {
      Ngenic.ngenic_displace_particles();

      if(All.RestartFlag == RST_CREATEICS)
        {
          double fac = 1 / sqrt(All.cf_a3inv);
          for(int i = 0; i < Sp.NumPart; i++)
            for(int k = 0; k < 3; k++)
              Sp.P[i].Vel[k] *= fac;

          strcat(All.SnapshotFileBase, "_ics");
          mpi_printf("Start writing file %s\nRestartSnapNum %d\n", All.SnapshotFileBase, 0);
          snap_io Snap(&Sp, Communicator, All.SnapFormat); /* get an I/O object */
          Snap.write_snapshot(0, NORMAL_SNAPSHOT);
          endrun();
        }
    }
#else
  if(All.RestartFlag == RST_CREATEICS)
    {
      Terminate("Compile with option NGENIC to create cosmological initial conditions");
    }
#endif

#ifdef LIGHTCONE_PARTICLES
  Lp.MaxPart = LIGHTCONE_ALLOC_FAC * Sp.MaxPart;
  Lp.NumPart = 0;
  Lp.allocate_memory();
#endif

#ifdef LIGHTCONE_MASSMAPS
  LightCone.Mp->Npix = nside2npix(All.LightConeMassMapsNside);

  subdivide_evenly(LightCone.Mp->Npix, NTask, ThisTask, &LightCone.Mp->FirstPix, &LightCone.Mp->NpixLoc);

  Mp.MaxPart = LIGHTCONE_MASSMAP_ALLOC_FAC * (Sp.TotNumPart / NTask);
  Mp.NumPart = 0;
  Mp.allocate_memory();
  LightCone.MassMap = (double *)Mem.mymalloc_movable_clear(&LightCone.MassMap, "MassMap", LightCone.Mp->NpixLoc * sizeof(double));
#endif

  /* this makes sure that masses are initialized in the case that the mass-block
     is empty for this particle type */

  for(int i = 0; i < Sp.NumPart; i++)
    if(All.MassTable[Sp.P[i].getType()] != 0)
      {
#ifndef LEAN
        Sp.P[i].setMass(All.MassTable[Sp.P[i].getType()]);
#else
        All.PartMass = All.MassTable[Sp.P[i].getType()];
#endif
      }

#if NSOFTCLASSES > 1
  for(int i = 0; i < Sp.NumPart; i++)
    Sp.P[i].setSofteningClass(All.SofteningClassOfPartType[Sp.P[i].getType()]);
#endif

#ifdef GENERATE_GAS_IN_ICS
  if(All.RestartFlag == RST_BEGIN)
    {
      /* determine maximum ID */
      MyIDType maxid = 0;
      for(int i = 0; i < Sp.NumPart; i++)
        if(Sp.P[i].ID.get() > maxid)
          maxid = Sp.P[i].ID.get();

      MyIDType *tmp = (MyIDType *)Mem.mymalloc("tmp", NTask * sizeof(MyIDType));
      MPI_Allgather(&maxid, sizeof(MyIDType), MPI_BYTE, tmp, sizeof(MyIDType), MPI_BYTE, Communicator);

      for(int i = 0; i < NTask; i++)
        if(tmp[i] > maxid)
          maxid = tmp[i];

      Mem.myfree(tmp);


      int count = 0;
      for(int i = 0; i < Sp.NumPart; i++)
#ifdef SPLIT_PARTICLE_TYPE
        if((1 << Sp.P[i].getType()) & (SPLIT_PARTICLE_TYPE))
#else
        if(Sp.P[i].getType() == 1)
#endif
          count++;

      int *numpart_list = (int *)Mem.mymalloc("numpart_list", NTask * sizeof(int));
      MPI_Allgather(&count, 1, MPI_INT, numpart_list, 1, MPI_INT, Communicator);

      maxid++;

      for(int i = 0; i < ThisTask; i++)
        maxid += numpart_list[i];

      Mem.myfree(numpart_list);

      Domain.domain_resize_storage(count + Sp.NumPart, count, 0);

      memmove(Sp.P + count, Sp.P, sizeof(particle_data) * Sp.NumPart);

      Sp.NumPart += count;
      Sp.NumGas += count;

      if(Sp.NumGas > Sp.MaxPartSph)
        Terminate("Task=%d ends up getting more SPH particles (%d) than allowed (%d)\n", ThisTask, Sp.NumGas, Sp.MaxPartSph);

      if(Sp.NumPart > Sp.MaxPart)
        Terminate("Task=%d ends up getting more particles (%d) than allowed (%d)\n", ThisTask, Sp.NumPart, Sp.MaxPart);

      double fac = All.OmegaBaryon / All.Omega0;
      double rho = All.Omega0 * 3 * All.Hubble * All.Hubble / (8 * M_PI * All.G);

      int j = 0;
      for(int i = count; i < Sp.NumPart; i++)
#ifdef SPLIT_PARTICLE_TYPE
        if((1 << Sp.P[i].getType()) & (SPLIT_PARTICLE_TYPE))
#else
        if(Sp.P[i].getType() == 1)
#endif
          {
            double d = pow(Sp.P[i].getMass() / rho, 1.0 / 3);
            double a = 0.5 * All.OmegaBaryon / All.Omega0 * d;
            double b = 0.5 * (All.Omega0 - All.OmegaBaryon) / All.Omega0 * d;

            MyIntPosType delta_a[3];
            double aa[3] = {a, a, a};
            Sp.pos_to_signedintpos(aa, (MySignedIntPosType *)delta_a);

            MyIntPosType delta_b[3];
            double bb[3] = {b, b, b};
            Sp.pos_to_signedintpos(bb, (MySignedIntPosType *)delta_b);

            Sp.P[j] = Sp.P[i];

            Sp.P[j].setMass(Sp.P[j].getMass() * fac);
            Sp.P[i].setMass(Sp.P[i].getMass() * (1 - fac));

            Sp.P[j].setType(0);
#if NSOFTCLASSES > 1
            Sp.P[j].setSofteningClass(All.SofteningClassOfPartType[0]);
#endif
            Sp.P[j].ID.set(maxid++);
            Sp.P[i].IntPos[0] += delta_a[0];
            Sp.P[i].IntPos[1] += delta_a[1];
            Sp.P[i].IntPos[2] += delta_a[2];
            Sp.P[j].IntPos[0] -= delta_b[0];
            Sp.P[j].IntPos[1] -= delta_b[1];
            Sp.P[j].IntPos[2] -= delta_b[2];

            j++;
          }

      All.MassTable[0] = 0;

#ifdef SPLIT_PARTICLE_TYPE
      for(int i = 1; i < NTYPES; i++)
        if((1 << i) & (SPLIT_PARTICLE_TYPE))
          All.MassTable[i] *= (1 - fac);
#else
      All.MassTable[1] *= (1 - fac);
#endif

      mpi_printf("\nGENERATE_GAS_IN_ICS: Generated gas particles from DM particle distribution.  TotNumGas=%lld\n\n", Sp.TotNumGas);
    }
#endif

#ifdef STARFORMATION
  if(All.RestartFlag == RST_BEGIN)
    {
      if(All.MassTable[STAR_TYPE] == 0 && All.MassTable[0] > 0)
        {
          All.MassTable[0] = 0;
        }
    }
#endif

  double u_init = (1.0 / GAMMA_MINUS1) * (BOLTZMANN / PROTONMASS) * All.InitGasTemp;
  u_init *= All.UnitMass_in_g / All.UnitEnergy_in_cgs; /* unit conversion */

  double molecular_weight;
  if(All.InitGasTemp > 1.0e4) /* assuming FULL ionization */
    molecular_weight = 4 / (8 - 5 * (1 - HYDROGEN_MASSFRAC));
  else /* assuming NEUTRAL GAS */
    molecular_weight = 4 / (1 + 3 * HYDROGEN_MASSFRAC);

  u_init /= molecular_weight;

  All.InitGasU = u_init;

  if(All.RestartFlag == RST_BEGIN)
    {
      if(All.InitGasTemp > 0)
        {
          for(int i = 0; i < Sp.NumGas; i++)
            {
              if(ThisTask == 0 && i == 0 && Sp.SphP[i].Entropy == 0)
                mpi_printf("READIC: Initializing u from InitGasTemp !\n");

              if(Sp.SphP[i].Entropy == 0)
                Sp.SphP[i].Entropy = All.InitGasU;
              /* Note: the coversion to entropy will be done in the function init(),
                 after the densities have been computed */
            }
        }
    }

  for(int i = 0; i < Sp.NumGas; i++)
    Sp.SphP[i].Entropy = std::max<double>(All.MinEgySpec, Sp.SphP[i].Entropy);

#ifdef COOLING
  CoolSfr.IonizeParams();
#endif

  if(All.ComovingIntegrationOn)
    {
      All.Timebase_interval = (log(All.TimeMax) - log(All.TimeBegin)) / TIMEBASE;
      All.Ti_Current        = 0;
    }
  else
    {
      All.Timebase_interval = (All.TimeMax - All.TimeBegin) / TIMEBASE;
      All.Ti_Current        = 0;
    }

  All.set_cosmo_factors_for_current_time();

  All.NumCurrentTiStep  = 0; /* setup some counters */
  All.SnapshotFileCount = 0;

  if(All.RestartFlag == RST_STARTFROMSNAP)
    {
      if(RestartSnapNum < 0)
        All.SnapshotFileCount = atoi(All.InitCondFile + strlen(All.InitCondFile) - 3) + 1;
      else
        All.SnapshotFileCount = RestartSnapNum + 1;
    }

  All.TotNumOfForces     = 0;
  All.TotNumDirectForces = 0;
  All.TotNumDensity      = 0;
  All.TotNumHydro        = 0;

  All.TopNodeAllocFactor = 0.08;
  All.TreeAllocFactor    = 0.3;
  All.NgbTreeAllocFactor = 0.7;

  All.TimeLastStatistics = All.TimeBegin - All.TimeBetStatistics;

#if defined(EVALPOTENTIAL) && defined(PMGRID) && defined(PERIODIC)
  double mass_sum = 0;

  for(int i = 0; i < Sp.NumPart; i++)
    mass_sum += Sp.P[i].getMass();

  MPI_Allreduce(&mass_sum, &All.TotalMass, 1, MPI_DOUBLE, MPI_SUM, Communicator);
#endif

  if(All.ComovingIntegrationOn) /*  change to new velocity variable */
    {
      double afac = sqrt(All.Time) * All.Time;

      for(int i = 0; i < Sp.NumPart; i++)
        {
          for(int j = 0; j < 3; j++)
            Sp.P[i].Vel[j] *= afac; /* for dm/gas particles, p = a^2 xdot */
        }
    }

  for(int i = 0; i < TIMEBINS; i++)
    All.Ti_begstep[i] = 0;

#if defined(PMGRID) && !defined(TREEPM_NOTIMESPLIT)
  All.PM_Ti_endstep = All.PM_Ti_begstep = 0;
#endif

  for(int i = 0; i < Sp.NumPart; i++) /*  start-up initialization with non-zero values where required */
    {
#ifndef LEAN
      Sp.P[i].access.clear();
#endif
#ifdef MERGERTREE
      Sp.P[i].PrevSubhaloNr.set(HALONR_MAX);
#endif
    }

  for(int i = 0; i < TIMEBINS; i++)
    Sp.TimeBinSynchronized[i] = 1;

  Sp.reconstruct_timebins();

  for(int i = 0; i < Sp.NumGas; i++) /* initialize sph_properties with non-zero values where required */
    {
      Sp.SphP[i].EntropyPred = Sp.SphP[i].Entropy;

      for(int j = 0; j < 3; j++)
        Sp.SphP[i].VelPred[j] = Sp.P[i].Vel[j];

      if(All.RestartFlag == RST_BEGIN)
        {
#ifdef COOLING
          Sp.SphP[i].Ne = 1.0;
#endif
        }

#ifdef PRESSURE_ENTROPY_SPH
      Sp.SphP[i].EntropyToInvGammaPred = pow(Sp.SphP[i].EntropyPred, 1.0 / GAMMA);
#endif

#ifdef TIMEDEP_ART_VISC
#ifdef HIGH_ART_VISC_START
      Sp.SphP[i].Alpha = All.ArtBulkViscConst;
#else
      Sp.SphP[i].Alpha = All.AlphaMin;
#endif
#endif
    }

#ifdef RECREATE_UNIQUE_IDS
  recreate_unique_ids();
#endif

  test_id_uniqueness();

  Domain.domain_decomposition(STANDARD); /* do initial domain decomposition (gives equal numbers of particles) */

  GravTree.set_softenings();

#ifdef ADAPTIVE_HYDRO_SOFTENING
  mpi_printf("INIT: Adaptive hydro softening, minimum gravitational softening for SPH particles: %g\n",
             All.MinimumComovingHydroSoftening);
  mpi_printf("INIT: Adaptive hydro softening, maximum gravitational softening for SPH particles: %g\n",
             All.MinimumComovingHydroSoftening * pow(All.AdaptiveHydroSofteningSpacing, NSOFTCLASSES_HYDRO - 1));
  mpi_printf("INIT: Adaptive hydro softening, number of softening values: %d\n", NSOFTCLASSES_HYDRO);
#endif

#ifdef INDIVIDUAL_GRAVITY_SOFTENING
  Sp.init_individual_softenings();
#endif

  if(All.RestartFlag == RST_FOF)
    {
#ifdef FOF

#if defined(SUBFIND) && defined(MERGERTREE)
      // we are reading the previous subhalo catalogue, if available, to assign the previous subhalo length to particles
      MergerTree.get_previous_size_of_subhalo_for_each_particle(RestartSnapNum - 1);
#endif

      Sp.PS = (subfind_data *)Mem.mymalloc_movable(&Sp.PS, "PS", Sp.MaxPart * sizeof(subfind_data));
      memset(Sp.PS, 0, Sp.MaxPart * sizeof(subfind_data));

407
408
409
410
411
412
413
414
      for(int i = 0; i < Sp.NumGas; i++)
        {
          if(ThisTask == 0 && i == 0)
            printf("INIT: Converting u -> entropy   All.cf_a3inv=%g\n", All.cf_a3inv);

          Sp.SphP[i].Entropy = GAMMA_MINUS1 * Sp.SphP[i].Entropy / pow(Sp.SphP[i].Density * All.cf_a3inv, GAMMA_MINUS1);
        }

Volker Springel's avatar
Volker Springel committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
      /* First, we save the original location of the particles, in order to be able to revert to this layout later on */
      for(int i = 0; i < Sp.NumPart; i++)
        {
          Sp.PS[i].OriginTask  = ThisTask;
          Sp.PS[i].OriginIndex = i;
        }
      fof<simparticles> FoF{Communicator, &Sp, &Domain};
      FoF.fof_fof(RestartSnapNum, "fof", "groups", 0);

      {
        All.DumpFlag_nextoutput = 1;
        snap_io Snap(&Sp, Communicator, All.SnapFormat); /* get an I/O object */
        Snap.write_snapshot(RestartSnapNum, NORMAL_SNAPSHOT);
      }

#ifdef SUBFIND_ORPHAN_TREATMENT
      {
        /* now read the IDs of the most bound particles of a previously existing special dump, with the idea being to
         * be able also on postprocessing to construct a cumulative list of all particles that used to be a most particles
         * in any previous dump. This can be accomplished by computing the group catalogues consecutively from 000 to the last
         * snapshoyt number
         */

        if(RestartSnapNum > 0)
          {
            subreadid_io SnapIDread(&Sp.IdStore, Communicator, All.SnapFormat);
            SnapIDread.previously_bound_read_snap_ids(RestartSnapNum - 1);

            FoF.subfind_match_ids_of_previously_most_bound_ids(&Sp);
          }

        snap_io Snap(&Sp, Communicator, All.SnapFormat);
        Snap.write_snapshot(RestartSnapNum, MOST_BOUND_PARTICLE_SNAPHOT); /* write special snapshot file */
      }
#endif

#endif
      endrun();
    }

  /* build neighbor tree */
  NgbTree.treeallocate(Sp.NumGas, &Sp, &Domain);
  NgbTree.treebuild(Sp.NumGas, NULL);

  if(All.RestartFlag == RST_POWERSPEC)
    {
#if defined(PMGRID) && defined(PERIODIC)

      PM.calculate_power_spectra(RestartSnapNum);
#else
      mpi_printf("\nThis option (Power Spectrum) only works for PERIODIC and enabled PMGRID.\n\n");
#endif
      endrun();
    }

  All.Ti_Current = 0;

  setup_smoothinglengths();

  /* at this point, the entropy variable actually contains the
   * internal energy, read in from the initial conditions file.
   * Once the density has been computed, we can convert to entropy.
   */
#ifdef PRESSURE_ENTROPY_SPH
479
#ifndef INITIAL_CONDITIONS_CONTAIN_ENTROPY
Volker Springel's avatar
Volker Springel committed
480
    NgbTree.setup_entropy_to_invgamma();
481
#endif
Volker Springel's avatar
Volker Springel committed
482
483
484
485
486
#endif

  double mass = 0;
  for(int i = 0; i < Sp.NumGas; i++)
    {
487
488
#ifndef INITIAL_CONDITIONS_CONTAIN_ENTROPY
        
Volker Springel's avatar
Volker Springel committed
489
490
491
492
493
494
495
          if(ThisTask == 0 && i == 0)
            printf("INIT: Converting u -> entropy\n");

#if !defined(PRESSURE_ENTROPY_SPH) && !defined(ISOTHERM_EQS)
          Sp.SphP[i].Entropy = GAMMA_MINUS1 * Sp.SphP[i].Entropy / pow(Sp.SphP[i].Density * All.cf_a3inv, GAMMA_MINUS1);
#endif
          Sp.SphP[i].EntropyPred = Sp.SphP[i].Entropy;
496
497
        
#endif
Volker Springel's avatar
Volker Springel committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
      /* The predicted entropy values have been already set for all SPH formulation */
      /* so it should be ok computing pressure and csound now */
      Sp.SphP[i].set_thermodynamic_variables();

      mass += Sp.P[i].getMass();
    }

  if(All.ComovingIntegrationOn)
    {
#ifdef PERIODIC
      if(All.RestartFlag == RST_BEGIN || All.RestartFlag == RST_RESUME || All.RestartFlag == RST_STARTFROMSNAP ||
         All.RestartFlag == RST_CREATEICS)
        {
          /* can't do this check when not all particles are loaded */
          check_omega();
        }
      else
        {
          mpi_printf("Skipping Omega check since not all particles are loaded\n");
        }
#endif
    }

#ifdef STARFORMATION
  /* initialize absolute masses in materials */
  for(int i = 0; i < Sp.NumGas; i++)
    {
      Sp.SphP[i].Metallicity = Sp.P[i].Metallicity;  // set above

      Sp.SphP[i].MassMetallicity = Sp.SphP[i].Metallicity * Sp.P[i].getMass();
    }
#endif

    // tree_based_timesteps_set_initialmaxtistep();

#ifdef DEBUG_MD5
  Logs.log_debug_md5("AFTER-INIT");
#endif

  return;
}

#ifdef PERIODIC
/*! \brief This routine computes the mass content of the box and compares it to the
 * specified value of Omega-matter.
 *
 * If discrepant, the run is terminated.
 */
void sim::check_omega(void)
{
  double mass = 0;

  for(int i = 0; i < Sp.NumPart; i++)
    mass += Sp.P[i].getMass();

  double masstot;
  MPI_Allreduce(&mass, &masstot, 1, MPI_DOUBLE, MPI_SUM, Communicator);

  double omega = masstot * (LONG_Z * LONG_Y * LONG_Z) / (All.BoxSize * All.BoxSize * All.BoxSize) /
                 (3 * All.Hubble * All.Hubble / (8 * M_PI * All.G));
  if(fabs(omega - All.Omega0) > 1.0e-2)
    {
      mpi_printf(
          "\n\nI've found something odd!\nThe mass content accounts only for Omega=%g,\nbut you specified Omega=%g in the "
          "parameterfile.\n\nI better stop.\n",
          omega, All.Omega0);
      endrun();
    }
}
#endif

/*! \brief This function is used to find an initial smoothing length for each SPH
 *  particle.
 *
 *  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.
 */
void sim::setup_smoothinglengths(void)
{
  Sp.TimeBinsGravity.NActiveParticles = 0;

  for(int i = 0; i < Sp.NumGas; i++)
    Sp.TimeBinsGravity.ActiveParticleList[Sp.TimeBinsGravity.NActiveParticles++] = i;

  sumup_large_ints(1, &Sp.TimeBinsGravity.NActiveParticles, &Sp.TimeBinsGravity.GlobalNActiveParticles, Communicator);

  if(Sp.TimeBinsGravity.GlobalNActiveParticles > 0)
    {
      mpi_printf("INIT: Setup smoothing lengths.\n");

      GravTree.treeallocate(Sp.NumPart, &Sp, &Domain);
      GravTree.treebuild(Sp.TimeBinsGravity.NActiveParticles, Sp.TimeBinsGravity.ActiveParticleList);

      for(int i = 0; i < Sp.NumGas; i++)
        {
          int no = GravTree.Father[i];

          while(10 * All.DesNumNgb * Sp.P[i].getMass() > GravTree.get_nodep(no)->mass)
            {
              int p = GravTree.get_nodep(no)->father;

              if(p < 0)
                break;

              no = p;
            }

          double len;
          if(GravTree.get_nodep(no)->level > 0)
            len = (((MyIntPosType)1) << (BITS_FOR_POSITIONS - GravTree.get_nodep(no)->level)) * Sp.FacIntToCoord;
          else
            len = Sp.RegionLen;

          Sp.SphP[i].Hsml = pow(3.0 / (4 * M_PI) * All.DesNumNgb * Sp.P[i].getMass() / GravTree.get_nodep(no)->mass, 1.0 / 3) * len;
        }

      Sp.TimeBinsHydro.NActiveParticles = 0;
      for(int i = 0; i < Sp.NumGas; i++)
        Sp.TimeBinsGravity.ActiveParticleList[Sp.TimeBinsHydro.NActiveParticles++] = i;

      NgbTree.density(Sp.TimeBinsGravity.ActiveParticleList, Sp.TimeBinsHydro.NActiveParticles);

#ifdef PRESSURE_ENTROPY_SPH
      for(int i = 0; i < Sp.NumGas; i++)
        Sp.SphP[i].PressureSphDensity = Sp.SphP[i].Density;
#endif

      GravTree.treefree();
    }
}

void sim::recreate_unique_ids(void)
{
  mpi_printf("INIT: Setting new unique IDs.\n");

  int *numpart_list = (int *)Mem.mymalloc("numpart_list", NTask * sizeof(int));

  MPI_Allgather(&Sp.NumPart, 1, MPI_INT, numpart_list, 1, MPI_INT, Communicator);

  MyIDType id = 1;

  for(int i = 0; i < ThisTask; i++)
    id += numpart_list[i];

  for(int i = 0; i < Sp.NumPart; i++)
    Sp.P[i].ID.set(id++);

  Mem.myfree(numpart_list);
}

/*! \brief This function checks for unique particle  IDs
 *
 *  The particle IDs are copied to an array and then sorted among all tasks.
 *  This array is then checked for duplicates. In that case the code terminates.
 */
void sim::test_id_uniqueness(void)
{
  mpi_printf("INIT: Testing ID uniqueness...\n");

  double t0 = Logs.second();

  MyIDType *ids       = (MyIDType *)Mem.mymalloc("ids", (Sp.NumPart + 1) * sizeof(MyIDType));
  MyIDType *ids_first = (MyIDType *)Mem.mymalloc("ids_first", NTask * sizeof(MyIDType));
  int *num_list       = (int *)Mem.mymalloc("num_list", NTask * sizeof(int));

  for(int i = 0; i < Sp.NumPart; i++)
    ids[i] = Sp.P[i].ID.get();

  mycxxsort_parallel(ids, ids + Sp.NumPart, Sp.compare_IDs, Communicator);

  for(int i = 1; i < Sp.NumPart; i++)
    {
      if(ids[i] == ids[i - 1])
        Terminate("non-unique ID=%lld found on task=%d (i=%d Sp.NumPart=%d type=%d)\n", (long long)ids[i], ThisTask, i, Sp.NumPart,
                  Sp.P[i].getType());
    }

  MPI_Allgather(&ids[0], sizeof(MyIDType), MPI_BYTE, ids_first, sizeof(MyIDType), MPI_BYTE, Communicator);
  MPI_Allgather(&Sp.NumPart, 1, MPI_INT, num_list, 1, MPI_INT, Communicator);

  int next_non_empty_task = ThisTask + 1;

  while(next_non_empty_task < NTask)
    if(num_list[next_non_empty_task] == 0)
      next_non_empty_task++;
    else
      break;

  if(Sp.NumPart > 0 && next_non_empty_task < NTask)
    {
      if(ids[Sp.NumPart - 1] == ids_first[next_non_empty_task])
        Terminate("non-unique ID=%lld found on task=%d\n", (long long)ids[Sp.NumPart - 1], ThisTask);
    }

  Mem.myfree(num_list);
  Mem.myfree(ids_first);
  Mem.myfree(ids);

  double t1 = Logs.second();

  mpi_printf("INIT: success.  took=%g sec\n\n", Logs.timediff(t0, t1));
}