Skip to content

Commit 87a0d35

Browse files
author
Martin D. Weinberg
committed
Merge branch 'InitFromHDF5' into slabUpdate
2 parents 9d2865a + c1d573d commit 87a0d35

3 files changed

Lines changed: 224 additions & 28 deletions

File tree

src/Component.H

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ struct loadb_datum
184184
specifying alternative parameters, using an old-style PSP file, or
185185
starting a new job with a previous output from another simulation)
186186
</ol>
187+
188+
<br> <b>Input data format</b> Component now supports reading from
189+
HDF5 files on input. The scheme for HDF5 input files is:
190+
@verbatim
191+
/
192+
├── Attributes
193+
│ ├── num_particles (int)
194+
│ ├── num_aux_ints (int)
195+
│ └── num_aux_floats (int)
196+
└── particles/ (Group)
197+
├── m (Dataset)
198+
├── x, y, z (Datasets)
199+
├── u, v, w (Datasets)
200+
├── index (optional Dataset)
201+
├── aux_int_0, aux_int_1, ... (Datasets)
202+
└── aux_float_0, aux_float_1, ... (Datasets)
203+
@endverbatim
204+
205+
@note
206+
<ol>
207+
208+
<li> The <code>index</code> dataset is optional. If it is not
209+
present, the particles are assigned indices in the order they are read.
210+
211+
<li> The <code>aux_int_*</code> and <code>aux_float_*</code>
212+
datasets are optional. If they are not present, the corresponding
213+
vectors are empty. The number of auxiliary integer and float
214+
datasets is determined by the <code>num_aux_ints</code> and
215+
<code>num_aux_floats</code> attributes, respectively.
216+
217+
<li> The datatype for the <code>m</code>, <code>x</code>,
218+
<code>y</code>, <code>z</code>, <code>u</code>, <code>v</code>,
219+
<code>w</code> and <code>aux_float_*</code> datasets maybe float or
220+
double (i.e. float32 or float64) but they all must are assumed to
221+
have the same datatype as <code>m</code>.
222+
223+
</ol>
187224
*/
188225
class Component
189226
{

src/Component.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)