GADGET-4
lightcone_massmap_io.cc
Go to the documentation of this file.
1 /*******************************************************************************
2  * \copyright This file is part of the GADGET4 N-body/SPH code developed
3  * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4  * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5  *******************************************************************************/
6 
12 #include "gadgetconfig.h"
13 
14 #if defined(LIGHTCONE) && defined(LIGHTCONE_MASSMAPS)
15 
16 #include <gsl/gsl_rng.h>
17 #include <hdf5.h>
18 #include <math.h>
19 #include <mpi.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include "../data/allvars.h"
28 #include "../data/dtypes.h"
29 #include "../data/mymalloc.h"
30 #include "../io/hdf5_util.h"
31 #include "../io/io.h"
32 #include "../lightcone/lightcone.h"
33 #include "../lightcone/lightcone_massmap_io.h"
34 #include "../main/simulation.h"
35 #include "../mpi_utils/mpi_utils.h"
36 #include "../system/system.h"
37 
45 lightcone_massmap_io::lightcone_massmap_io(mmparticles *Mp_ptr, lightcone *LightCone_ptr, MPI_Comm comm, int format)
46  : IO_Def(comm, format)
47 {
48  Mp = Mp_ptr;
49  LightCone = LightCone_ptr;
50 
51  this->N_IO_Fields = 0;
52  this->N_DataGroups = 1;
53  this->header_size = sizeof(header);
54  this->header_buf = &header;
55  this->type_of_file = FILE_IS_MASSMAP;
56  sprintf(this->info, "LIGHTCONE: writing mass map data");
57 
58  init_field("MAMP", "Mass", MEM_DOUBLE, FILE_MY_IO_FLOAT, SKIP_ON_READ, 1, A_MM, &LightCone->MassMap[0], NULL, MASSMAPS, 1, 0., -1.,
59  0., 1., 0., All.UnitMass_in_g, true);
60 }
61 
62 void lightcone_massmap_io::lightcone_massmap_save(int num)
63 {
64  char buf[2 * MAXLEN_PATH];
65 
66  selected_bnd = num;
67 
68  long long NumLP_tot = LightCone->Mp->NpixLoc;
69  MPI_Allreduce(MPI_IN_PLACE, &NumLP_tot, 1, MPI_LONG_LONG, MPI_SUM, Communicator);
70  mpi_printf("\nLIGHTCONE: writing lightcone massmap files #%d ... (Npix_tot = %lld, ascale %g to %g)\n", num, NumLP_tot,
71  LightCone->MassMapBoundariesAscale[num], LightCone->MassMapBoundariesAscale[num + 1]);
72 
73  if(All.NumFilesPerSnapshot > 1)
74  {
75  if(ThisTask == 0)
76  {
77  char buf[2 * MAXLEN_PATH];
78  sprintf(buf, "%s/mapsdir_%03d", All.OutputDir, num);
79  mkdir(buf, 02755);
80  }
81  MPI_Barrier(Communicator);
82  }
83 
84  if(All.NumFilesPerSnapshot > 1)
85  sprintf(buf, "%s/mapsdir_%03d/%s_%03d", All.OutputDir, num, "maps", num);
86  else
87  sprintf(buf, "%s/%s_%03d", All.OutputDir, "maps", num);
88 
89  write_multiple_files(buf, All.NumFilesPerSnapshot);
90 
91  mpi_printf("LIGHTCONE: done with writing mass map.\n");
92 
93  /* clear the massmap again */
94  memset(LightCone->MassMap, 0, LightCone->Mp->NpixLoc * sizeof(double));
95 }
96 
97 void lightcone_massmap_io::fill_file_header(int writeTask, int lastTask, long long *n_type, long long *ntot_type)
98 {
99  /* determine global and local pixel numbers */
100 
101  n_type[0] = LightCone->Mp->NpixLoc;
102 
103  /* determine particle numbers of each type in file */
104  if(ThisTask == writeTask)
105  {
106  ntot_type[0] = n_type[0];
107 
108  for(int task = writeTask + 1; task <= lastTask; task++)
109  {
110  long long nn;
111  MPI_Recv(&nn, 1, MPI_LONG_LONG, task, TAG_LOCALN, Communicator, MPI_STATUS_IGNORE);
112  ntot_type[0] += nn;
113  }
114 
115  for(int task = writeTask + 1; task <= lastTask; task++)
116  MPI_Send(&ntot_type[0], 1, MPI_LONG_LONG, task, TAG_N, Communicator);
117  }
118  else
119  {
120  MPI_Send(&n_type[0], 1, MPI_LONG_LONG, writeTask, TAG_LOCALN, Communicator);
121  MPI_Recv(&ntot_type[0], 1, MPI_LONG_LONG, writeTask, TAG_N, Communicator, MPI_STATUS_IGNORE);
122  }
123 
124  /* fill file header */
125  header.nside = All.LightConeMassMapsNside; /* healpix nside */
126  header.npix_local = ntot_type[0];
127  header.npix_total = LightCone->Mp->Npix;
128  header.num_files = All.NumFilesPerSnapshot;
129 
130  header.AscaleStart = LightCone->MassMapBoundariesAscale[selected_bnd];
131  header.AscaleEnd = LightCone->MassMapBoundariesAscale[selected_bnd + 1];
132  header.ComDistStart = LightCone->MassMapBoundariesComDist[selected_bnd];
133  header.ComDistEnd = LightCone->MassMapBoundariesComDist[selected_bnd + 1];
134 }
135 
136 void lightcone_massmap_io::write_header_fields(hid_t handle)
137 {
138  write_scalar_attribute(handle, "Nside", &header.nside, H5T_NATIVE_INT);
139 
140  write_scalar_attribute(handle, "NpixLocal", &header.npix_local, H5T_NATIVE_INT);
141 
142  write_scalar_attribute(handle, "NpixTotal", &header.npix_total, H5T_NATIVE_INT);
143 
144  write_scalar_attribute(handle, "NumFiles", &header.num_files, H5T_NATIVE_INT);
145 
146  write_scalar_attribute(handle, "AscaleStart", &header.AscaleStart, H5T_NATIVE_DOUBLE);
147 
148  write_scalar_attribute(handle, "AscaleEnd", &header.AscaleEnd, H5T_NATIVE_DOUBLE);
149 
150  write_scalar_attribute(handle, "ComDistStart", &header.ComDistStart, H5T_NATIVE_DOUBLE);
151 
152  write_scalar_attribute(handle, "ComDistEnd", &header.ComDistEnd, H5T_NATIVE_DOUBLE);
153 }
154 
155 void lightcone_massmap_io::set_filenr_in_header(int numfiles) { header.num_files = numfiles; }
156 
157 void lightcone_massmap_io::get_datagroup_name(int type, char *buf)
158 {
159  if(type == 0)
160  sprintf(buf, "/Maps");
161  else
162  Terminate("should not get here");
163 }
164 
165 void *lightcone_massmap_io::get_base_address_of_structure(enum arrays array, int index)
166 {
167  switch(array)
168  {
169  case A_MM:
170  return (void *)(LightCone->MassMap + index);
171 
172  default:
173  Terminate("we don't expect to get here");
174  }
175 
176  return NULL;
177 }
178 
179 void lightcone_massmap_io::read_file_header(const char *fname, int filenr, int readTask, int lastTask, long long *nloc_part,
180  long long *npart, int *nstart)
181 {
182  /* empty */
183 }
184 
185 void lightcone_massmap_io::read_header_fields(const char *fname)
186 { /* empty */
187 }
188 
189 void lightcone_massmap_io::read_increase_numbers(int type, int n_for_this_task)
190 { /* empty */
191 }
192 
193 int lightcone_massmap_io::get_filenr_from_header(void)
194 {
195  /* empty */
196  return 0;
197 }
198 
199 int lightcone_massmap_io::get_type_of_element(int index)
200 {
201  /* empty */
202  return 0;
203 }
204 
205 void lightcone_massmap_io::set_type_of_element(int index, int type)
206 { /* empty */
207 }
208 
209 #endif
global_data_all_processes All
Definition: main.cc:40
Definition: io.h:129
#define MAXLEN_PATH
Definition: constants.h:300
void write_scalar_attribute(hid_t handle, const char *attr_name, const void *buf, hid_t mem_type_id)
Definition: hdf5_util.cc:621
@ SKIP_ON_READ
Definition: io.h:125
@ MASSMAPS
Definition: io.h:115
arrays
Definition: io.h:30
@ A_MM
Definition: io.h:46
@ FILE_MY_IO_FLOAT
Definition: io.h:69
@ MEM_DOUBLE
Definition: io.h:84
@ FILE_IS_MASSMAP
Definition: io.h:60
#define Terminate(...)
Definition: macros.h:19
#define TAG_LOCALN
Definition: mpi_utils.h:52
#define TAG_N
Definition: mpi_utils.h:25
char OutputDir[MAXLEN_PATH]
Definition: allvars.h:272