12#include "gadgetconfig.h"
23#include "../cooling_sfr/cooling.h"
24#include "../data/allvars.h"
25#include "../data/dtypes.h"
26#include "../data/intposconvert.h"
27#include "../data/mymalloc.h"
28#include "../fof/fof.h"
29#include "../gitversion/version.h"
30#include "../gravtree/gravtree.h"
31#include "../io/hdf5_util.h"
33#include "../io/snap_io.h"
34#include "../lightcone/lightcone.h"
35#include "../logs/logs.h"
36#include "../logs/timer.h"
37#include "../main/main.h"
38#include "../main/simulation.h"
39#include "../mpi_utils/mpi_utils.h"
40#include "../sort/peano.h"
41#include "../src/pm/pm.h"
42#include "../system/system.h"
59 sprintf(this->
info,
"SNAPSHOT: writing snapshot");
61#ifdef OUTPUT_COORDINATES_AS_INTEGERS
65 init_field(
"POS ",
"Coordinates",
MEM_MY_DOUBLE,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 3,
A_P, NULL, io_func_pos,
ALL_TYPES, 1, 1., -1.,
69#ifdef OUTPUT_VELOCITIES_IN_HALF_PRECISION
70 init_field(
"VEL ",
"Velocities",
MEM_MY_FLOAT,
FILE_HALF,
READ_IF_PRESENT, 3,
A_NONE, NULL, io_func_vel,
ALL_TYPES, 1, 0.5, 0., 0.,
73 init_field(
"VEL ",
"Velocities",
MEM_MY_FLOAT,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 3,
A_NONE, NULL, io_func_vel,
ALL_TYPES, 1, 0.5,
77 init_field(
"ID ",
"ParticleIDs",
MEM_MY_ID_TYPE,
FILE_MY_ID_TYPE,
READ_IF_PRESENT, 1,
A_P, NULL, io_func_id,
ALL_TYPES, 0, 0, 0, 0,
81 init_field(
"MASS",
"Masses",
MEM_MY_DOUBLE,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 1,
A_P, NULL, io_func_mass,
MASS_BLOCK, 1, 0., -1.,
85#ifdef SECOND_ORDER_LPT_ICS
86 init_field(
"LPTM",
"SecondOrderICMasses",
MEM_FLOAT,
FILE_NONE,
READ_IF_PRESENT, 1,
A_P, &Sp->
P[0].
OldAcc, NULL,
ALL_TYPES, 0, 0, 0,
90 init_field(
"U ",
"InternalEnergy",
MEM_MY_FLOAT,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 1,
A_NONE, NULL, io_func_u,
GAS_ONLY, 1, 0.,
93 init_field(
"RHO ",
"Density",
MEM_MY_FLOAT,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 1,
A_SPHP, &Sp->
SphP[0].
Density, NULL,
GAS_ONLY, 1,
96 init_field(
"HSML",
"SmoothingLength",
MEM_MY_FLOAT,
FILE_MY_IO_FLOAT,
READ_IF_PRESENT, 1,
A_SPHP, &Sp->
SphP[0].
Hsml, NULL,
GAS_ONLY,
99#ifdef OUTPUT_ACCELERATION
100#ifdef OUTPUT_ACCELERATIONS_IN_HALF_PRECISION
103 init_field(
"ACCE",
"Acceleration",
MEM_MY_FLOAT,
FILE_HALF,
SKIP_ON_READ, 3,
A_NONE, 0, io_func_accel,
ALL_TYPES, 1, -2.0, 1, -1, 0,
108 init_field(
"ACCE",
"Acceleration",
MEM_MY_FLOAT,
FILE_MY_IO_FLOAT,
SKIP_ON_READ, 3,
A_NONE, 0, io_func_accel,
ALL_TYPES, 1, -2.0, 1,
120 1,
A_NONE, 0, io_func_sfr,
GAS_ONLY, 1, 0., 0., -1., 1., 1.,
SOLAR_MASS /
SEC_PER_YEAR);
124 0, 0, 0, 0, 0, 0, 0);
128 0, 0, 0, 0, 0, 0, 0);
131#if defined(PRESSURE_ENTROPY_SPH) && defined(OUTPUT_PRESSURE_SPH_DENSITY)
137#ifdef OUTPUT_PRESSURE
143#if defined(TIMEDEP_ART_VISC) && defined(OUTPUT_VISCOSITY_PARAMETER)
145 NULL,
GAS_ONLY, 1, -3., 2., -3., 1., 0., 1);
151 0, 0, 0, 0, 0, 0, 0);
157 0, 0, 0, 0, 0, 0, 0);
160#ifdef OUTPUT_POTENTIAL
166#ifdef OUTPUT_CHANGEOFENTROPY
169 0, 0, 0, 0, 0, 0, 0);
172#ifdef OUTPUT_TIMESTEP
175 0, 0, 0, 0, 0, 0, 0);
179 0, 0, 0, 0, 0, 0, 0);
185 0, 0, 0, 0, 0, 0, 0);
191 0, 0, 0, 0, 0, 0, 0);
194#ifdef OUTPUT_VELOCITY_GRADIENT
195#ifndef IMPROVED_VELOCITY_GRADIENTS
196#error "The option OUTPUT_VELOCITY_GRADIENT requires IMPROVED_VELOCITY_GRADIENTS"
202#ifdef OUTPUT_COOLHEAT
207#if defined(SUBFIND) && defined(SUBFIND_STORE_LOCAL_DENSITY)
222#if defined(GADGET2_HEADER) && defined(REARRANGE_OPTION) && defined(MERGERTREE)
223 Terminate(
"GADGET2_HEADER does not work together with REARRANGE_OPTION\n");
227#if defined(REARRANGE_OPTION) && defined(MERGERTREE)
228void snap_io::init_extra(
simparticles *Sp_ptr, mergertree *MergerTree_ptr)
231 MergerTree = MergerTree_ptr;
233 init_field(
"MTRI",
"TreeID",
MEM_INT64,
FILE_INT64,
READ_IF_PRESENT, 1,
A_P, &Sp->
P[0].TreeID, NULL,
ALL_TYPES, 0, 0, 0, 0, 0, 0, 0);
239 init_field(
"MTRI",
"TreeID",
MEM_INT64,
FILE_INT64,
READ_IF_PRESENT, 1,
A_TT, &MergerTree->TreeTable[0].TreeID, NULL,
TREETABLE, 0,
246 snap_type = loc_snap_type;
308 for(
int rep = 0; rep < 2; rep++)
318 int max_load, max_sphload;
323 max_load *= TILING * TILING * TILING;
324 max_sphload *= TILING * TILING * TILING;
332#ifndef OUTPUT_COORDINATES_AS_INTEGERS
333 Ptmp = (ptmp_data *)
Mem.mymalloc_movable(&Ptmp,
"Ptmp", Sp->
NumPart *
sizeof(ptmp_data));
345 mpi_printf(
"READIC: reading done. Took %g sec, total size %g MB, corresponds to effective I/O rate of %g MB/sec\n",
346 Logs.
timediff(t0, t1), byte_count_all / (1024.0 * 1024.0), byte_count_all / (1024.0 * 1024.0) /
Logs.
timediff(t0, t1));
350 snap_init_domain_mapping();
352#ifndef OUTPUT_COORDINATES_AS_INTEGERS
360 MyIntPosType len = (halfboxlen / TILING) + (halfboxlen / TILING);
362 Sp->
TotNumGas *= TILING * TILING * TILING;
365 int add_numgas = Sp->
NumGas * (TILING * TILING * TILING - 1);
367 memmove(
static_cast<void *
>(Sp->
P + Sp->
NumGas + add_numgas),
static_cast<void *
>(Sp->
P + Sp->
NumGas),
372 for(
int i = 0; i < TILING; i++)
373 for(
int j = 0; j < TILING; j++)
374 for(
int k = 0; k < TILING; k++)
375 if(i != 0 || j != 0 || k != 0)
376 for(
int n = 0; n < Sp->
NumGas; n++)
387 if(off != add_numgas)
393 int add_numpart = (Sp->
NumPart - Sp->
NumGas) * (TILING * TILING * TILING - 1);
397 for(
int i = 0; i < TILING; i++)
398 for(
int j = 0; j < TILING; j++)
399 for(
int k = 0; k < TILING; k++)
400 if(i != 0 || j != 0 || k != 0)
401 for(
int n = Sp->
NumGas; n < Sp->NumPart; n++)
412 if(off != add_numpart)
419#ifndef INITIAL_CONDITIONS_CONTAIN_ENTROPY
420 if(
header.flag_entropy_instead_u)
421 Terminate(
"Initial condition file contains entropy, but INITIAL_CONDITIONS_CONTAIN_ENTROPY is not set\n");
423 if(!
header.flag_entropy_instead_u)
424 Terminate(
"Initial condition file contains uthermal, but INITIAL_CONDITIONS_CONTAIN_ENTROPY is set\n");
434void snap_io::snap_init_domain_mapping(
void)
444 double posmin[3], posmax[3];
445 for(
int k = 0; k < 3; k++)
451 for(
int i = 0; i < Sp->
NumPart; i++)
452 for(
int k = 0; k < 3; k++)
454 if(Ptmp[i].Pos[k] < posmin[k])
455 posmin[k] = Ptmp[i].Pos[k];
457 if(Ptmp[i].Pos[k] > posmax[k])
458 posmax[k] = Ptmp[i].Pos[k];
461 double xyz[6] = {posmin[0], posmin[1], posmin[2], -posmax[0], -posmax[1], -posmax[2]};
464 MPI_Allreduce(xyz, xyz_glob, 6, MPI_DOUBLE, MPI_MIN,
Communicator);
466 mpi_printf(
"READIC: Region covered with particles: (%g %g %g) -> (%g %g %g)\n", xyz_glob[0], xyz_glob[1], xyz_glob[2], -xyz_glob[3],
467 -xyz_glob[4], -xyz_glob[5]);
470 for(
int j = 0; j < 3; j++)
471 if(-xyz_glob[j + 3] - xyz_glob[j] > Sp->
RegionLen)
472 Sp->
RegionLen = -xyz_glob[j + 3] - xyz_glob[j];
478 for(
int j = 0; j < 3; j++)
480 Sp->
RegionCenter[j] = 0.5 * (xyz_glob[j] - xyz_glob[j + 3]);
489#ifndef OUTPUT_COORDINATES_AS_INTEGERS
490 for(
int i = 0; i < Sp->
NumPart; i++)
527 return (
void *)(Sp->
SphP + index);
529 return (
void *)(Sp->
P + index);
531 return (
void *)(Sp->
PS + index);
532#ifdef LIGHTCONE_PARTICLES
536#ifdef LIGHTCONE_MASSMAPS
540#if defined(REARRANGE_OPTION) && defined(MERGERTREE)
542 return (
void *)(MergerTree->TreeTable + index);
545 Terminate(
"we don't expect to get here");
561#ifdef DO_NOT_PRODUCE_BIG_OUTPUT
564 mpi_printf(
"\nSNAPSHOT: We skip writing snapshot file #%d @ time %g\n", num,
All.
Time);
569 snap_type = loc_snap_type;
573 mpi_printf(
"\nSNAPSHOT: writing snapshot file #%d @ time %g ... \n", num,
All.
Time);
583 for(
int n = 0; n <
NTYPES; n++)
586 for(
int n = 0; n < Sp->
NumPart; n++)
635 mpi_printf(
"SNAPSHOT: done with writing snapshot. Took %g sec, total size %g MB, corresponds to effective I/O rate of %g MB/sec\n",
636 Logs.
timediff(t0, t1), byte_count_all / (1024.0 * 1024.0), byte_count_all / (1024.0 * 1024.0) /
Logs.
timediff(t0, t1));
646 for(
int n = 0; n <
NTYPES + 1; n++)
649 for(
int n = 0; n < Sp->
NumPart; n++)
656#if defined(REARRANGE_OPTION) && defined(MERGERTREE)
658 n_type[
NTYPES] = MergerTree->Ntrees;
664 for(
int n = 0; n <
NTYPES + 1; n++)
665 ntot_type[n] = n_type[n];
667 for(
int task = writeTask + 1; task <= lastTask; task++)
671 for(
int n = 0; n <
NTYPES + 1; n++)
672 ntot_type[n] += nn[n];
675 for(
int task = writeTask + 1; task <= lastTask; task++)
686 for(
int n = 0; n <
NTYPES; n++)
692#if defined(MERGERTREE) && !defined(GADGET2_HEADER)
696#if defined(REARRANGE_OPTION) && defined(MERGERTREE)
707 for(
int n = 0; n <
NTYPES; n++)
721 for(
int n = 0; n <
NTYPES; n++)
722 header.npartTotalLowWord[n] = ntot_type_all[n];
739#
if !(defined(REARRANGE_OPTION) && defined(MERGERTREE))
744#ifdef OUTPUT_IN_DOUBLEPRECISION
745 header.flag_doubleprecision = 1;
747 header.flag_doubleprecision = 0;
760 if(
header.npartTotalLowWord[i] > 0)
767 for(
int i = 0; i <
NTYPES; i++)
773 for(
int i = 0; i <
NTYPES; i++)
777#if defined(SECOND_ORDER_LPT_ICS)
780 mpi_printf(
"READIC: Second order ICs detected. Will complete them first before starting run.\n");
781 All.LptScalingfactor =
header.lpt_scalingfactor;
784 Terminate(
"READIC: No second order ICs detected even though you activated SECOND_ORDER_LPT_ICS.\n");
787 Terminate(
"Detected second order ICs but SECOND_ORDER_LPT_ICS is not enabled.\n");
791#ifdef GENERATE_GAS_IN_ICS
794#ifdef SPLIT_PARTICLE_TYPE
795 for(
int i = 0; i <
NTYPES; i++)
796 if((1 << i) & (SPLIT_PARTICLE_TYPE))
808 for(
int i = 0; i <
NTYPES; i++)
821 if(filenr == 0 && nstart == NULL)
824 "\nREADIC: filenr=%d, '%s' contains:\n"
825 "READIC: Type 0 (gas): %8lld (tot=%15lld) masstab= %g\n",
828 for(
int type = 1; type <
NTYPES; type++)
842 for(
int type = 0; type <
NTYPES; type++)
847 int ntask = lastTask - readTask + 1;
848 int n_for_this_task = n_in_file / ntask;
849 if((
ThisTask - readTask) < (n_in_file % ntask))
852 n_type[type] = n_for_this_task;
854 nall += n_for_this_task;
857#if defined(MERGERTREE) && !defined(GADGET2_HEADER)
864 memmove(
static_cast<void *
>(&Sp->
P[Sp->
NumGas + nall]),
static_cast<void *
>(&Sp->
P[Sp->
NumGas]),
866#ifndef OUTPUT_COORDINATES_AS_INTEGERS
896#if defined(REARRANGE_OPTION) && defined(MERGERTREE)
911 for(
int i = 0; i <
NTYPES; i++)
920 hid_t hdf5_file =
my_H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
921 hid_t handle =
my_H5Gopen(hdf5_file,
"/Header");
925 hid_t space = H5Aget_space(hdf5_attribute);
927 H5Sget_simple_extent_dims(space, &dims, &len);
929 if(len != (
size_t)ntypes)
930 Terminate(
"Length of NumPart_ThisFile attribute (%d) does not match NTYPES(ICS) (%d)", (
int)len, ntypes);
943#if defined(MERGERTREE) && !defined(GADGET2_HEADER)
957#if defined(GADGET2_HEADER) && defined(SECOND_ORDER_LPT_ICS)
972 Sp->
NumPart += n_for_this_task;
975 Sp->
NumGas += n_for_this_task;
981 sprintf(buf,
"/PartType%d", type);
983 sprintf(buf,
"/TreeTable");
global_data_all_processes All
void write_multiple_files(char *fname, int numfilesperdump, int append_flag=0, int chunk_size=0)
void init_field(const char *label, const char *datasetname, enum types_in_memory type_in_memory, enum types_in_file type_in_file_output, enum read_flags read_flag, int values_per_block, enum arrays array, void *pointer_to_field, void(*io_func)(IO_Def *, int, int, void *, int), int typelist_bitmask, int hasunits, double a, double h, double L, double M, double V, double c, bool compression_on=false)
enum file_contents type_of_file
void read_files_driver(const char *fname, int rep, int numfiles)
int find_files(const char *fname, const char *fname_multiple)
This function determines on how many files a given snapshot or group/desc catalogue is distributed.
bool is_previously_most_bound(void)
void pos_to_intpos(T *posdiff, MyIntPosType *intpos)
void reset_io_byte_count(void)
long long get_io_byte_count(void)
double timediff(double t0, double t1)
void mpi_printf(const char *fmt,...)
void allocate_memory(void)
int get_filenr_from_header(void)
void read_header_fields(const char *fname)
This function reads the snapshot header in case of hdf5 files (i.e. format 3)
void * get_base_address_of_structure(enum arrays array, int index)
void read_increase_numbers(int type, int n_for_this_task)
void read_snapshot(int num, mysnaptype snap_type)
void fill_file_header(int writeTask, int lastTask, long long *nloc_part, long long *npart)
void read_file_header(const char *fname, int filenr, int readTask, int lastTask, long long *nloc_part, long long *npart, int *nstart)
void set_type_of_element(int index, int type)
void write_header_fields(hid_t)
Write the fields contained in the header group of the HDF5 snapshot file.
void get_datagroup_name(int grnr, char *gname)
void read_ic(const char *fname)
This function reads initial conditions that are in on of the default file formats of Gadget.
void set_filenr_in_header(int)
void init_basic(simparticles *Sp_ptr)
Function for field registering.
int get_type_of_element(int index)
void write_snapshot(int num, mysnaptype snap_type)
Save snapshot to disk.
#define MAXLEN_PATH_EXTRA
@ MOST_BOUND_PARTICLE_SNAPHOT
@ MOST_BOUND_PARTICLE_SNAPHOT_REORDERED
#define BITS_FOR_POSITIONS
void write_vector_attribute(hid_t handle, const char *attr_name, const void *buf, hid_t mem_type_id, int length)
hid_t my_H5Gopen(hid_t loc_id, const char *groupname)
hid_t my_H5Fopen(const char *fname, unsigned int flags, hid_t fapl_id)
herr_t my_H5Gclose(hid_t group_id, const char *groupname)
void read_vector_attribute(hid_t handle, const char *attr_name, void *buf, hid_t mem_type_id, int length)
herr_t my_H5Fclose(hid_t file_id, const char *fname)
hid_t my_H5Aopen_name(hid_t loc_id, const char *attr_name)
herr_t my_H5Aclose(hid_t attr_id, const char *attr_name)
void write_scalar_attribute(hid_t handle, const char *attr_name, const void *buf, hid_t mem_type_id)
void read_scalar_attribute(hid_t handle, const char *attr_name, void *buf, hid_t mem_type_id)
void write_string_attribute(hid_t handle, const char *attr_name, const char *buf)
#define FLAG_SECOND_ORDER_ICS
#define TIMER_START(counter)
Starts the timer counter.
#define TIMER_STOP(counter)
Stops the timer counter.
void sumup_large_ints(int n, int *src, long long *res, MPI_Comm comm)
void sumup_longs(int n, long long *src, long long *res, MPI_Comm comm)
expr pow(half base, half exp)
double UnitVelocity_in_cm_per_s
enum restart_options RestartFlag
double UnitDensity_in_cgs
int ComovingIntegrationOn
char OutputDir[MAXLEN_PATH]
integertime Ti_lastoutput
double accel_normalize_fac
char SnapshotFileBase[MAXLEN_PATH]
void set_cosmo_factors_for_current_time(void)
unsigned char getType(void)
void setType(unsigned char type)