Commit 5764bf1d authored by gbalduzz's avatar gbalduzz
Browse files

Implemented polymorphic reader.

minor writer fixup.
parent 9b1ddbd3
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public:
  typedef JSON_context JsonDataType;

public:
  JSONReader();
  JSONReader(bool verbose = true);

  bool is_reader() {
    return true;
@@ -132,6 +132,7 @@ private:
  }

private:
  bool verbose_;
  std::string current_file_name;

  JSON_parser<JsonDataType> parser;
@@ -175,6 +176,7 @@ void JSONReader::execute(std::string name, scalartype& value, const JsonAccessor

template <typename scalartype, typename domain_type>
void JSONReader::execute(func::function<scalartype, domain_type>& f) {
  if (verbose_)
    std::cout << "\t starts reading function : " << f.get_name() << "\n";
  execute(f.get_name(), f, parse_result, 0);
}
@@ -321,7 +323,7 @@ void JSONReader::execute(std::string name,
  }
}

}  // io
}  // dca
}  // namespace io
}  // namespace dca

#endif  // DCA_IO_JSON_JSON_READER_HPP
+73 −0
Original line number Diff line number Diff line
// Copyright (C) 2020 ETH Zurich
// Copyright (C) 2020 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE for terms of usage.
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch)
//
// Wrapper to an instance of HDF5Reader of JSONReader.

#ifndef DCA_IO_READER_HPP
#define DCA_IO_READER_HPP

#include <mutex>
#include <string>
#include <variant>

#include "dca/io/hdf5/hdf5_reader.hpp"
#include "dca/io/json/json_reader.hpp"

namespace dca::io {

class Reader {
public:
  // In: format. output format, HDF5 or JSON.
  // In: verbose. If true, the reader outputs a short log whenever it is executed.
  Reader(const std::string& format, bool verbose = true) {
    if (format == "HDF5") {
      reader_.emplace<io::HDF5Reader>(verbose);
    }
    else if (format == "JSON") {
      reader_.emplace<io::JSONReader>(verbose);
    }
    else {
      throw(std::logic_error("Invalid input format"));
    }
  }

  constexpr bool is_reader() const noexcept {
    return true;
  }
  constexpr bool is_writer() const noexcept {
    return false;
  }

  void open_file(const std::string& file_name) {
    std::visit([&](auto& var) { var.open_file(file_name); }, reader_);
  }

  void close_file() {
    std::visit([&](auto& var) { var.close_file(); }, reader_);
  }

  void open_group(const std::string& new_path) {
    std::visit([&](auto& var) { var.open_group(new_path); }, reader_);
  }
  void close_group() {
    std::visit([&](auto& var) { var.close_group(); }, reader_);
  }

  template <class... Args>
  void execute(Args&... args) {
    std::visit([&](auto& var) { var.execute(args...); }, reader_);
  }

private:
  std::variant<io::HDF5Reader, io::JSONReader> reader_;
};

}  // namespace dca::io

#endif  // DCA_IO_READER_HPP
+2 −2
Original line number Diff line number Diff line
@@ -44,8 +44,8 @@ public:
    return true;
  }

  void open_file(std::string file_name_ref, bool overwrite = true) {
    std::visit([&](auto& var) { var.open_file(file_name_ref, overwrite); }, writer_);
  void open_file(const std::string& file_name, bool overwrite = true) {
    std::visit([&](auto& var) { var.open_file(file_name, overwrite); }, writer_);
  }

  void close_file() {
+20 −48
Original line number Diff line number Diff line
@@ -29,10 +29,8 @@
#include "dca/function/domains.hpp"
#include "dca/function/function.hpp"
#include "dca/function/util/real_complex_conversion.hpp"
#include "dca/io/hdf5/hdf5_reader.hpp"
#include "dca/io/hdf5/hdf5_writer.hpp"
#include "dca/io/json/json_reader.hpp"
#include "dca/io/json/json_writer.hpp"
#include "dca/io/reader.hpp"
#include "dca/io/writer.hpp"
#include "dca/linalg/linalg.hpp"
#include "dca/math/function_transform/function_transform.hpp"
#include "dca/math/util/vector_operations.hpp"
@@ -105,8 +103,7 @@ public:
  DcaData(Parameters& parameters_ref);

  void read(std::string filename);
  template <typename Reader>
  void read(Reader& reader);
  void read(io::Reader& reader);

  template <typename Writer>
  void write(Writer& writer);
@@ -323,29 +320,13 @@ void DcaData<Parameters>::read(std::string filename) {
  if (concurrency_.id() == concurrency_.first())
    std::cout << "\n\n\t starts reading \n\n";

  if (concurrency_.id() == concurrency_.first()) {
    const std::string& output_format = parameters_.get_output_format();

    if (output_format == static_cast<const std::string>("JSON")) {
      dca::io::JSONReader reader;
      reader.open_file(filename);
      this->read(reader);
      reader.close_file();
    }
  dca::io::Reader reader(parameters_.get_output_format());

    else if (output_format == static_cast<const std::string>("HDF5")) {
      dca::io::HDF5Reader reader;
  reader.open_file(filename);
      this->read(reader);
  read(reader);
  reader.close_file();
    }

    else
      throw std::logic_error(__FUNCTION__);
  }

  concurrency_.broadcast(parameters_.get_chemical_potential());

  concurrency_.broadcast_object(Sigma);

  if (parameters_.isAccumulatingG4()) {
@@ -357,8 +338,7 @@ void DcaData<Parameters>::read(std::string filename) {
}

template <class Parameters>
template <typename Reader>
void DcaData<Parameters>::read(Reader& reader) {
void DcaData<Parameters>::read(io::Reader& reader) {
  reader.open_group("parameters");

  reader.open_group("physics");
@@ -541,7 +521,7 @@ void DcaData<Parameters>::initialize_G0() {
template <class Parameters>
void DcaData<Parameters>::initializeSigma(const std::string& filename) {
  if (concurrency_.id() == concurrency_.first()) {
    auto read_all = [&](auto&& reader) {
    io::Reader reader(parameters_.get_output_format());
    reader.open_file(filename);

    if (parameters_.adjust_chemical_potential()) {
@@ -555,14 +535,6 @@ void DcaData<Parameters>::initializeSigma(const std::string& filename) {
    reader.open_group("functions");
    reader.execute(Sigma);
    reader.close_group();
    };

    if (parameters_.get_output_format() == "HDF5")
      read_all(io::HDF5Reader());
    else if (parameters_.get_output_format() == "JSON")
      read_all(io::JSONReader());
    else
      throw(std::logic_error("Invalid format"));
  }

  concurrency_.broadcast(parameters_.get_chemical_potential());
+19 −26
Original line number Diff line number Diff line
@@ -143,7 +143,8 @@ template <typename ParametersType>
int DcaLoopData<ParametersType>::tryToRead(const std::string& filename, const std::string& format,
                                           const Concurrency& concurrency) {
  if (concurrency.id() == concurrency.first() && filesystem::exists(filename)) {
    auto read_all = [&](auto&& reader) {
    io::Reader reader(format);

    reader.open_file(filename);
    reader.open_group("DCA-loop-functions");

@@ -170,14 +171,6 @@ int DcaLoopData<ParametersType>::tryToRead(const std::string& filename, const st

    reader.open_group("DCA-loop-functions");
    reader.close_file();
    };

    if (format == "HDF5")
      read_all(io::HDF5Reader());
    else if (format == "JSON")
      read_all(io::JSONReader());
    else
      throw(std::logic_error("Invalid format"));
  }

  concurrency.broadcast(last_completed_iteration);
Loading