Ejemplo n.º 1
0
// Write a whole StateSet in one go. The optional argument sort_order can be
// given to write states in an order different from their order in the
// StateSet (for example, in order of increasing energy).
void Datafile::write_stateset(StateSet const& stateset, int step, std::list<size_t> const* sort_order) {
	hsize_t states_cur_size[2];
	hsize_t state_history_cur_size[1];
	try {
		ensure_states_data();
		ensure_state_history_data();
		// calculate current size of states_data
		states_data.getSpace().getSimpleExtentDims(states_cur_size);
		// new slot for states will be same as states_cur_size[0]
		const hsize_t new_slot = states_cur_size[0];
		state_history_pair pair;
		pair.step = step;
		pair.index = static_cast<int>(new_slot);
		// calculate current size of state_history_data and extend by one
		state_history_data.getSpace().getSimpleExtentDims(state_history_cur_size);
		hsize_t new_size = state_history_cur_size[0]+1;
		state_history_data.extend(&new_size);
		space_1d.setExtentSimple(1, &new_size);
		space_1d.selectElements(H5S_SELECT_SET, 1, state_history_cur_size);
		validate_selection(space_1d);
		// record what was the step when states were saved
		state_history_data.write(&pair, state_history_type, scalar_space, space_1d);
		// write states
		const size_t N = stateset.get_num_states();
		if (sort_order == NULL)
			for (size_t m=0; m<N; m++)
				write_state(new_slot, m, stateset[m]);
		else {
			std::list<size_t>::const_iterator it = sort_order->begin();
			for (size_t m=0; m<N; m++) {
				write_state(new_slot, m, stateset[*it]);
				it++;
			}
		}
	}
	catch(H5::Exception& e) {
		e.printError();
		throw;
	}
}