Commit f8be5af3 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Complete HDF5 I/O implementation and add comparison operator

parent 8b9cdadf
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -225,5 +225,14 @@ class TFRFME {
   * or "LGEACY". Default is "LEGACY").
   */
  void write_binary(std::string file_name, std::string mode="LEGACY");
  
  /*! \brief Test whether two instances of configuration are equal.
   *
   * \param other: `TFRFME &` Reference to the instance to be compared
   * with.
   * \return result: `bool` True, if the two instances are equal,
   * false otherwise.
   */
  bool operator ==(TFRFME &other);
};
#endif
+112 −4
Original line number Diff line number Diff line
@@ -75,6 +75,10 @@ TFRFME* TFRFME::from_hdf5(string file_name) {
  unsigned int flags = H5F_ACC_RDONLY;
  HDFFile *hdf_file = new HDFFile(file_name, flags);
  herr_t status = hdf_file->get_status();
  double *coord_vec, *elements;
  string str_type;
  int _nlmmt, _nrvc, num_elements;
  complex<double> value;
  if (status == 0) {
    int lmode, lm, nkv, nxv, nyv, nzv;
    double vk, exri, an, ff, tra, spd, frsh, exril;
@@ -101,7 +105,42 @@ TFRFME* TFRFME::from_hdf5(string file_name) {
    instance->set_param("spd", spd);
    instance->set_param("frsh", frsh);
    instance->set_param("exril", exril);
    // DA QUI: aggiungere lettura di vettori e matrice.
    // Vectors and matrix need to be copied element-wise and
    // subsequently deleted.
    coord_vec = new double[nxv]();
    str_type = "FLOAT64_(" + to_string(nxv) + ")";
    status = hdf_file->read("XVEC", str_type, coord_vec);
    for (int xi = 0; xi < nxv; xi++)
      instance->set_x(xi, coord_vec[xi]);
    delete[] coord_vec;
    coord_vec = new double[nyv]();
    str_type = "FLOAT64_(" + to_string(nyv) + ")";
    status = hdf_file->read("YVEC", str_type, coord_vec);
    for (int yi = 0; yi < nyv; yi++)
      instance->set_y(yi, coord_vec[yi]);
    delete[] coord_vec;
    coord_vec = new double[nzv]();
    str_type = "FLOAT64_(" + to_string(nzv) + ")";
    status = hdf_file->read("ZVEC", str_type, coord_vec);
    for (int zi = 0; zi < nzv; zi++)
      instance->set_z(zi, coord_vec[zi]);
    delete[] coord_vec;
    _nlmmt = 2 * lm * (lm + 2);
    _nrvc = nxv * nyv * nzv;
    num_elements = 2 * _nlmmt * _nrvc;
    elements = new double[num_elements]();
    str_type = "FLOAT64_(" + to_string(num_elements) + ")";
    status = hdf_file->read("WSUM", str_type, elements);
    for (int wi = 0; wi < _nlmmt; wi++) {
      for (int wj = 0; wj < _nrvc; wj++) {
	int index = (2 * _nrvc) * wi + 2 * wj;
	value = complex<double>(elements[index], elements[index + 1]);
	instance->set_matrix_element(wi, wj, value);
      } // wj loop
    } // wi loop
    delete[] elements;
    status = hdf_file->close();
    delete hdf_file;
  }
  return instance;
}
@@ -222,6 +261,7 @@ void TFRFME::write_hdf5(string file_name) {
  List<string> rec_name_list(1);
  List<string> rec_type_list(1);
  List<void *> rec_ptr_list(1);
  herr_t status;
  string str_type;
  rec_name_list.set(0, "LMODE");
  rec_type_list.set(0, "INT32_(1)");
@@ -300,8 +340,8 @@ void TFRFME::write_hdf5(string file_name) {
  FileSchema schema(rec_num, rec_types, rec_names);
  HDFFile *hdf_file = HDFFile::from_schema(schema, file_name, H5F_ACC_TRUNC);
  for (int ri = 0; ri < rec_num; ri++)
    hdf_file->write(rec_names[ri], rec_types[ri], rec_pointers[ri]);
  hdf_file->close();
    status = hdf_file->write(rec_names[ri], rec_types[ri], rec_pointers[ri]);
  status = hdf_file->close();

  delete[] ptr_elements;
  delete[] rec_names;
@@ -346,3 +386,71 @@ void TFRFME::write_legacy(string file_name) {
    printf("ERROR: could not open output file \"%s\"\n", file_name.c_str());
  }
}

bool TFRFME::operator ==(TFRFME &other) {
  if (lmode != other.lmode) {
    return false;
  }
  if (lm != other.lm) {
    return false;
  }
  if (nkv != other.nkv) {
    return false;
  }
  if (nxv != other.nxv) {
    return false;
  }
  if (nyv != other.nyv) {
    return false;
  }
  if (nzv != other.nzv) {
    return false;
  }
  if (vk != other.vk) {
    return false;
  }
  if (exri != other.exri) {
    return false;
  }
  if (an != other.an) {
    return false;
  }
  if (ff != other.ff) {
    return false;
  }
  if (tra != other.tra) {
    return false;
  }
  if (spd != other.spd) {
    return false;
  }
  if (frsh != other.frsh) {
    return false;
  }
  if (exril != other.exril) {
    return false;
  }
  for (int xi = 0; xi < nxv; xi++) {
    if (xv[xi] != other.xv[xi]) {
      return false;
    }
  }
  for (int yi = 0; yi < nyv; yi++) {
    if (yv[yi] != other.yv[yi]) {
      return false;
    }
  }
  for (int zi = 0; zi < nzv; zi++) {
    if (zv[zi] != other.zv[zi]) {
      return false;
    }
  }
  for (int wi = 0; wi < nlmmt; wi++) {
    for (int wj = 0; wj < nrvc; wj++) {
      if (wsum[wi][wj] != other.wsum[wi][wj]) {
	return false;
      }
    } // wj loop
  } // wi loop
  return true;
}