@@ -1572,6 +1572,8 @@ void Component::read_bodies_and_distribute_hdf5(void)
15721572 // Phase-space data
15731573 //
15741574 FloatData m, x, y, z, u, v, w;
1575+ std::vector<unsigned long > index;
1576+ bool has_index = particles_group.exist (" index" );
15751577 std::vector<std::vector<int >> aux_ints;
15761578 std::vector<FloatData> aux_floats;
15771579
@@ -1589,11 +1591,9 @@ void Component::read_bodies_and_distribute_hdf5(void)
15891591 throw std::runtime_error (" Component ERROR: dataset <" + name + " > shorter than expected" );
15901592 }
15911593
1592- // Sanity check to ensure we don't read beyond the dataset
15931594 size_t current = std::min (batch, total - offset);
1594- // Wrap returned vector in FloatData
1595- if (precision == 4 ) {
1596- return FloatData (dataset.select ({offset}, {current}).read <std::vector<float >>());
1595+ if (current != batch) {
1596+ throw std::runtime_error (" Component ERROR: dataset <" + name + " > shorter than expected" );
15971597 }
15981598 else if (precision == 8 ) {
15991599 return FloatData (dataset.select ({offset}, {current}).read <std::vector<double >>());
@@ -1627,6 +1627,10 @@ void Component::read_bodies_and_distribute_hdf5(void)
16271627 size_t offset = 0 ;
16281628
16291629 // Read Node 0's PSP fields
1630+ if (has_index) {
1631+ index = particles_group.getDataSet (" index" )
1632+ .select ({offset}, {nbodies_table[0 ]}).read <std::vector<unsigned long >>();
1633+ }
16301634 m = read_dataset (" m" , offset, nbodies_table[0 ]);
16311635
16321636 x = read_dataset (" x" , offset, nbodies_table[0 ]);
@@ -1675,8 +1679,8 @@ void Component::read_bodies_and_distribute_hdf5(void)
16751679 part->dattrib [j] = aux_floats[j][i];
16761680 }
16771681
1678- // Make a sequential index for the particle
1679- part->indx = i + 1 ;
1682+ // Set particle index from optional dataset, otherwise sequential
1683+ part->indx = has_index ? index[i] : i + 1 ;
16801684
16811685 // Get the radius
16821686 r2 = 0.0 ;
@@ -1699,6 +1703,10 @@ void Component::read_bodies_and_distribute_hdf5(void)
16991703 for (int n=1 ; n<numprocs; n++) {
17001704
17011705 // Read core PSP fields
1706+ if (has_index) {
1707+ index = particles_group.getDataSet (" index" )
1708+ .select ({offset}, {nbodies_table[n]}).read <std::vector<unsigned long >>();
1709+ }
17021710 m = read_dataset (" m" , offset, nbodies_table[n]);
17031711
17041712 x = read_dataset (" x" , offset, nbodies_table[n]);
@@ -1732,8 +1740,8 @@ void Component::read_bodies_and_distribute_hdf5(void)
17321740
17331741 PartPtr part = std::make_shared<Particle>(niattrib, ndattrib);
17341742
1735- // Make a sequential index for the particle
1736- part->indx = nbodies_index[n-1 ] + 1 + icount;
1743+ // Set particle index from optional dataset, otherwise sequential
1744+ part->indx = has_index ? index[icount] : nbodies_index[n-1 ] + 1 + icount;
17371745
17381746 part->mass = m[icount];
17391747
0 commit comments