SIRIUS 7.5.0
Electronic structure library and applications
serialize_mdarray.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2021 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file serialize_mdarray.hpp
21 *
22 * \brief Serialize madarray to json.
23 */
24
25#ifndef __SERIALIZE_MDARRAY_HPP__
26#define __SERIALIZE_MDARRAY_HPP__
27
28#include "SDDK/memory.hpp"
29#include "core/json.hpp"
30
31namespace sirius {
32
33template <typename T, int N>
34inline nlohmann::json
35serialize(sddk::mdarray<T, N> const& a__)
36{
37 nlohmann::json dict;
38 std::array<sddk::mdarray_index_descriptor::index_type, N> begin;
39 std::array<sddk::mdarray_index_descriptor::index_type, N> end;
40 for (int i = 0; i < N; i++) {
41 begin[i] = a__.dim(i).begin();
42 end[i] = a__.dim(i).begin();
43 }
44 dict["begin"] = begin;
45 dict["end"] = end;
46 dict["data"] = std::vector<T>(a__.size());
47 for (size_t i = 0; i < a__.size(); i++) {
48 dict["data"][i] = a__[i];
49 }
50 return dict;
51}
52
53template <typename T, int N>
54inline nlohmann::json
55serialize(sddk::mdarray<std::complex<T>, N> const& a__)
56{
57 nlohmann::json dict;
58 std::array<sddk::mdarray_index_descriptor::index_type, N> begin;
59 std::array<sddk::mdarray_index_descriptor::index_type, N> end;
60 for (int i = 0; i < N; i++) {
61 begin[i] = a__.dim(i).begin();
62 end[i] = a__.dim(i).begin();
63 }
64 dict["begin"] = begin;
65 dict["end"] = end;
66 dict["data"] = std::vector<T>(2 * a__.size());
67 for (size_t i = 0; i < a__.size(); i++) {
68 dict["data"][2 * i] = a__[i].real();
69 dict["data"][2 * i + 1] = a__[i].imag();
70 }
71 return dict;
72}
73
74template <typename T, int N>
75void
76write_to_json_file(sddk::mdarray<T, N> const& a__, std::string const& fname__)
77{
78 try {
79 auto dict = serialize(a__);
80 std::ofstream ofs(fname__, std::ofstream::out | std::ofstream::trunc);
81 ofs << dict.dump(4);
82 } catch(...) {
83 std::stringstream s;
84 s << "Error writing mdarray to file " << fname__;
85 printf("%s\n", s.str().c_str());
86 }
87}
88
89}
90
91#endif
92
Interface to nlohmann::json library and helper functions.
Memory management functions and classes.
Namespace of the SIRIUS library.
Definition: sirius.f90:5