aboutsummaryrefslogtreecommitdiff
path: root/Source/Diagnostics/ReducedDiags/ReducedDiags.cpp
blob: 758b48b7262b59d2eae9e3061461ea7932edd84b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright 2019-2020 Maxence Thevenet, Yinjian Zhao
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */

#include "ReducedDiags.H"

#include "WarpX.H"
#include "Utils/Parser/IntervalsParser.H"
#include "Utils/Parser/ParserUtils.H"
#include "Utils/TextMsg.H"

#include <AMReX.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_ParmParse.H>
#include <AMReX_Utility.H>

#include <fstream>
#include <iomanip>

using namespace amrex;

// constructor
ReducedDiags::ReducedDiags (std::string rd_name)
    : m_rd_name(std::move(rd_name))
{
    BackwardCompatibility();

    const ParmParse pp_rd_name(m_rd_name);

    // read path
    pp_rd_name.query("path", m_path);

    // read extension
    pp_rd_name.query("extension", m_extension);

    // check if it is a restart run
    std::string restart_chkfile;
    const ParmParse pp_amr("amr");
    pp_amr.query("restart", restart_chkfile);
    bool IsNotRestart = restart_chkfile.empty();

    if (ParallelDescriptor::IOProcessor())
    {
        // create folder
        constexpr int permission_flag_rwxrxrx = 0755;
        if (!amrex::UtilCreateDirectory(m_path, permission_flag_rwxrxrx))
        { amrex::CreateDirectoryFailed(m_path); }

        // replace / create output file
        std::string rd_full_file_name = m_path + m_rd_name + "." + m_extension;
        m_write_header = IsNotRestart || !amrex::FileExists(rd_full_file_name); // not a restart or file doesn't exist
        if (m_write_header)
        {
            std::ofstream ofs{rd_full_file_name, std::ios::trunc};
            ofs.close();
        }
    }

    // read reduced diags intervals
    std::vector<std::string> intervals_string_vec = {"1"};
    pp_rd_name.getarr("intervals", intervals_string_vec);
    m_intervals = utils::parser::IntervalsParser(intervals_string_vec);

    // read separator
    pp_rd_name.query("separator", m_sep);

    // precision of data in the output file
    utils::parser::queryWithParser(pp_rd_name, "precision", m_precision);
}
// end constructor

void ReducedDiags::InitData ()
{
    // Defines an empty function InitData() to be overwritten if needed.
    // Function used to initialize data of the diagnostics after the WarpX
    // data structures are all set up
}

void ReducedDiags::LoadBalance ()
{
    // Defines an empty function LoadBalance() to be overwritten if needed.
    // Function used to redistribute parallel data of the diagnostics in
    // load balancing operations
}

void ReducedDiags::BackwardCompatibility ()
{
    const amrex::ParmParse pp_rd_name(m_rd_name);
    std::vector<std::string> backward_strings;
    WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
        !pp_rd_name.queryarr("frequency", backward_strings),
        "<reduced_diag_name>.frequency is no longer a valid option. "
        "Please use the renamed option <reduced_diag_name>.intervals instead."
    );
}

// write to file function
void ReducedDiags::WriteToFile (int step) const
{
    // open file
    std::ofstream ofs{m_path + m_rd_name + "." + m_extension,
        std::ofstream::out | std::ofstream::app};

    // write step
    ofs << step+1;

    ofs << m_sep;

    // set precision
    ofs << std::fixed << std::setprecision(m_precision) << std::scientific;

    // write time
    ofs << WarpX::GetInstance().gett_new(0);

    // loop over data size and write
    for (const auto& item : m_data) ofs << m_sep << item;

    // end loop over data size

    // end line
    ofs << std::endl;

    // close file
    ofs.close();
}
// end ReducedDiags::WriteToFile