Read a single gadget file and store the data in part.
If only_keep_part_in_domain then we only keep the partiles that fall within the local domain
385 {
386
388
389
390 std::ifstream fp(
filename.c_str(), std::ios::binary);
391 if (not fp.is_open()) {
392 std::string errormessage =
"[GadgetReader::read_gadget_single] File " +
filename +
" is not open\n";
393 throw_error(errormessage);
394 }
396 if (verbose)
398
399
400 const double pos_norm = 1.0 / header.
BoxSize;
401
402
403 const double vel_norm = sqrt(header.
time);
404
405
406 size_t type_start[7];
407 type_start[0] = 0;
408 for (
int t = 0;
t < 6;
t++)
409 type_start[t + 1] = type_start[t] +
size_t(header.
npart[t]);
410 const size_t NumPartFileTot = type_start[6];
411 auto type_of = [&](
size_t i) {
413 while (i >= type_start[t + 1])
416 };
417
418
419 if (NumPartFileTot == 0)
420 return;
421
422#ifndef GADGET_ONLY_DM
423 if constexpr (FML::PARTICLE::has_set_family<T>() == false) {
424 if (NumPartFileTot !=
size_t(header.
npart[1])) {
425 std::string errormessage = "[GadgetReader::read_gadget_single] Particle type does not have "
426 "set_family, but Gadget file contains multiple species! Either use "
427 "define GADGET_ONLY_DM or add methods to particle\n";
428 throw_error(errormessage);
429 }
430 }
431#endif
432
433
434 size_t NumPartWithMass = 0;
435 for (
int t = 0;
t < 6;
t++)
436 if (header.
mass[t] == 0.0)
437 NumPartWithMass += header.
npart[
t];
438
439
440
441 if (not only_keep_part_in_domain and part.capacity() < part.size() + NumPartFileTot)
442 part.reserve(part.size() + NumPartFileTot);
443
444
445 std::vector<char> keep(NumPartFileTot, 1);
446 const size_t index_start = part.size();
447 bool pos_is_read = false;
448
449
450 auto element_size = [&](
const std::string & field,
size_t bytes,
size_t n,
size_t ncomp) {
451 for (size_t size : {size_t(4), size_t(8)})
452 if (bytes == size * n * ncomp)
454 throw_error(
"[GadgetReader::read_gadget_single] The " + field +
" block in " +
filename +
" has " +
455 std::to_string(bytes) + " bytes, expected 4 or 8 bytes times " +
456 std::to_string(n * ncomp) + "\n");
457 return size_t(0);
458 };
459
460 auto real_value = [&](
const std::vector<char> & buffer,
size_t size,
size_t i) ->
double {
461 if (size == 4)
462 return double(reinterpret_cast<const float *>(buffer.data())[i]);
463 return reinterpret_cast<const double *>(buffer.data())[i];
464 };
465 auto swap_block = [&](std::vector<char> & buffer,
size_t size) {
466 if (not endian_swap)
467 return;
468 if (size == 4)
470 else
472 };
473 auto check_pos_is_read = [&](const std::string & field) {
474 if (not pos_is_read)
475 throw_error("[GadgetReader::read_gadget_single] The " + field + " block comes before POS in " +
476 filename +
" (POS has to be read first)\n");
477 };
478
479
480 std::vector<char> buffer;
481 auto process_block = [&](const std::string & field) {
482 if (field == "POS") {
483 const size_t size = element_size(field, buffer.size(), NumPartFileTot, NDIM);
484 swap_block(buffer, size);
485 if (verbose)
486 std::cout << "Reading POS bytes = " << buffer.size() << "\n";
487 std::vector<double>
x(NDIM);
488 for (
size_t i = 0;
i < NumPartFileTot;
i++) {
489 for (int idim = 0; idim < NDIM; idim++) {
490 x[idim] = real_value(buffer, size, NDIM * i + idim) * pos_norm;
491 if (x[idim] >= 1.0)
493 if (x[idim] < 0.0)
495 }
496 assert_mpi(x[0] >= 0.0 and x[0] < 1.0,
497 "[read_gadet_single] Particle has x position outside the boxsize even after "
498 "periodic wrap");
499 const int type = type_of(i);
500#ifdef GADGET_ONLY_DM
501 if (type != 1) {
503 continue;
504 }
505#endif
506 if (only_keep_part_in_domain and
509 continue;
510 }
511 part.push_back(T{});
512 auto &
p = part.back();
513 if constexpr (FML::PARTICLE::has_get_pos<T>()) {
515 for (
int idim = 0; idim <
NDIM; idim++)
516 pos[idim] = x[idim];
517 }
518 if constexpr (FML::PARTICLE::has_set_family<T>())
520 }
521 pos_is_read = true;
522 } else if (field == "VEL") {
523 check_pos_is_read(field);
524 const size_t size = element_size(field, buffer.size(), NumPartFileTot, NDIM);
525 swap_block(buffer, size);
526 if constexpr (FML::PARTICLE::has_get_vel<T>()) {
527 size_t index = index_start;
528 for (
size_t i = 0;
i < NumPartFileTot;
i++) {
529 if (not keep[i])
530 continue;
532 for (
int idim = 0; idim <
NDIM; idim++)
533 vel[idim] = real_value(buffer, size, NDIM * i + idim) * vel_norm;
534 }
535 }
536 } else if (field == "ID") {
537 check_pos_is_read(field);
538 const size_t size = element_size(field, buffer.size(), NumPartFileTot, 1);
539 swap_block(buffer, size);
540 if constexpr (FML::PARTICLE::has_set_id<T>()) {
541 size_t index = index_start;
542 for (
size_t i = 0;
i < NumPartFileTot;
i++) {
543 if (not keep[i])
544 continue;
545 const long long int id =
546 size == 4 ? (
long long int)(
reinterpret_cast<int32_t *
>(buffer.data())[i]) :
547 (
long long int)(
reinterpret_cast<int64_t *
>(buffer.data())[i]);
549 }
550 }
551 } else if (field == "MASS") {
552 check_pos_is_read(field);
553 const size_t size = element_size(field, buffer.size(), NumPartWithMass, 1);
554 swap_block(buffer, size);
555 if constexpr (FML::PARTICLE::has_set_mass<T>()) {
556 size_t index = index_start;
557 size_t count = 0;
558 for (
size_t i = 0;
i < NumPartFileTot;
i++) {
559 const bool has_mass = header.
mass[type_of(i)] == 0.0;
560 const double mass = has_mass ? real_value(buffer, size, count++) : 0.0;
561 if (not keep[i])
562 continue;
563 if (has_mass)
566 }
567 }
568 } else {
569 if (verbose)
570 std::cout << "Skipping block [" << field << "]\n";
571 }
572 };
573
574 if (format2) {
575
577 while (read_label(fp, label)) {
579 if (label == "MASS" and NumPartWithMass == 0)
580 continue;
581 process_block(label);
582 }
583 } else {
584 auto fields = fields_in_file;
585 if (NumPartWithMass > 0 and std::find(fields.begin(), fields.end(), "MASS") == fields.end())
586 fields.push_back("MASS");
587 for (auto & field : fields) {
588 if (field == "MASS") {
589 if (NumPartWithMass == 0)
590 continue;
591
592 if (fp.peek() == std::ifstream::traits_type::eof())
593 break;
594 }
596 process_block(field);
597 }
598 }
599 if (not pos_is_read)
600 throw_error(
"[GadgetReader::read_gadget_single] No POS block in " +
filename +
"\n");
601 }
void read_section(std::ifstream &fp, std::vector< char > &buffer)
Read a section (block) of a gadget file. The buffer is resized to the size of the block.
void print_header_info(const GadgetHeader &header)
void swap_endian_vector(T *vec, size_t n)
get_vel constexpr double * GetPos(...)
constexpr void SetMass(...)
constexpr double * GetVel(...)
constexpr void SetFamily(...)
constexpr void SetID(...)
double xmax_domain
The local extent of the domain (global domain goes from 0 to 1)
double xmin_domain
The local extent of the domain (global domain goes from 0 to 1)