Skip to content

Commit

Permalink
Set time averaging options in ParmParse
Browse files Browse the repository at this point in the history
  • Loading branch information
n01r committed Sep 14, 2024
1 parent a5803f2 commit 48cc53e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 38 deletions.
10 changes: 5 additions & 5 deletions Source/Diagnostics/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,21 @@ Diagnostics::ComputeAndPack ()
// a diagnostics and writes in one or more components of the output
// multifab m_mf_output[lev].
m_all_field_functors[lev][icomp]->operator()(m_mf_output[i_buffer][lev], icomp_dst, i_buffer);
// update the index of the next component to fill
icomp_dst += m_all_field_functors[lev][icomp]->nComp();
}
// Check that the proper number of components of mf_avg were updated.
AMREX_ALWAYS_ASSERT( icomp_dst == m_varnames.size() );

if (m_do_timeaverage) {
// update the index of the next component to fill
if (m_diag_type == DiagTypes::TimeAveraged) {

amrex::Real real_a = 1.0;
// call amrex sax operation to do the following
amrex::MultiFab::Saxpy(
m_sum_mf_output[i_buffer][lev], real_a, m_mf_output[i_buffer][lev], 0, 0, m_mf_output[i_buffer][lev].nComp(), m_mf_output[i_buffer][lev].nGrowVect());
m_sum_mf_output[i_buffer][lev], real_a, m_mf_output[i_buffer][lev],
0, 0, m_mf_output[i_buffer][lev].nComp(), m_mf_output[i_buffer][lev].nGrowVect());
}

m_sum_mf_output[i_buffer][lev] += scalar_a * m_mf_output[i_buffer][lev]

// needed for contour plots of rho, i.e. ascent/sensei
if (m_format == "sensei" || m_format == "ascent") {
ablastr::utils::communication::FillBoundary(m_mf_output[i_buffer][lev], WarpX::do_single_precision_comms,
Expand Down
22 changes: 13 additions & 9 deletions Source/Diagnostics/FullDiagnostics.H
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ class FullDiagnostics final : public Diagnostics
{
public:
FullDiagnostics (int i, const std::string& name, DiagTypes diag_type);
// bool m_do_timeaverage = false;
// enum TimeAverageType {None, Static, Dynamic};
/** Type of time averaging for diagnostics (fields only)
* None corresponds to instantaneous diags
* Static corresponds to a fixed starting step for averaging,
* will average until the end, and dump out intermediate average results
* Dynamic corresponds to a moving period for averaging where the start step
* is as many steps before the output interval as the averaging period is long.
*/
enum TimeAverageType {None, Static, Dynamic};
private:
/** Read user-requested parameters for full diagnostics */
void ReadParameters ();
Expand All @@ -29,16 +35,14 @@ private:
* before writing the diagnostic.
*/
bool m_solver_deposits_current = true;
/** Whether the diagnostics are averaging data over time or not
* Valid options are "fixed_start" and "dynamic_start".
*/
std::string m_time_averaging = "none";
/** Whether the diagnostics are averaging data over time or not */
TimeAverageType m_time_average_type = TimeAverageType::None;
/** Period to average fields over: in steps */
unsigned int m_average_period_steps = 0;
int m_average_period_steps = 0;
/** Period to average fields over: in seconds */
amrex::Real m_average_period_seconds = 0;
amrex::Real m_average_period_time = 0;
/** Time step to start averaging */
unsigned int m_average_start_step = 0;
int m_average_start_step = 0;
/** Flush m_mf_output and particles to file for the i^th buffer */
void Flush (int i_buffer, bool /* force_flush */) override;
/** Flush raw data */
Expand Down
102 changes: 78 additions & 24 deletions Source/Diagnostics/FullDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,47 @@ FullDiagnostics::ReadParameters ()
const bool plot_raw_fields_guards_specified = pp_diag_name.query("plot_raw_fields_guards", m_plot_raw_fields_guards);
const bool raw_specified = plot_raw_fields_specified || plot_raw_fields_guards_specified;

if (m_diag_type == DiagTypes::TimeAveraged) {
std::string m_time_average_mode_str = "none";
/** Whether the diagnostics are averaging data over time or not
* Valid options are "fixed_start" and "dynamic_start".
*/
pp_diag_name.get("time_average_mode", m_time_average_mode_str);

if (m_time_average_mode_str == "fixed_start") {
m_time_average_type = TimeAverageType::Static;
} else if (m_time_average_mode_str == "dynamic_start") {
m_time_average_type = TimeAverageType::Dynamic;
} else if (m_time_average_mode_str == "none") {
m_time_average_type = TimeAverageType::None;
} else {
WARPX_ABORT_WITH_MESSAGE(
"Unknown time averaging mode. Valid entries are: none, fixed_start, dynamic_start"
);
}

if (m_time_average_type == TimeAverageType::Static) {
pp_diag_name.get("average_start_step", m_average_start_step);
}

if (m_time_average_type == TimeAverageType::Dynamic) {
const bool averaging_period_steps_specified = pp_diag_name.query(
"average_period_steps", m_average_period_steps
);
const bool averaging_period_time_specified = pp_diag_name.queryWithParser(
"average_period_time", m_average_period_time
);
// one of the two averaging period options must be set but neither none nor both
if (
(averaging_period_steps_specified && averaging_period_time_specified)
|| !(averaging_period_steps_specified || averaging_period_time_specified)
) {
WARPX_ABORT_WITH_MESSAGE("Please specify either 'average_period_steps' or 'average_period_time', not both.");
}
}
}


#ifdef WARPX_DIM_RZ
pp_diag_name.query("dump_rz_modes", m_dump_rz_modes);
#else
Expand Down Expand Up @@ -142,20 +183,19 @@ FullDiagnostics::Flush ( int i_buffer, bool /* force_flush */ )
m_output_species.at(i_buffer), nlev_output, m_file_prefix,
m_file_min_digits, m_plot_raw_fields, m_plot_raw_fields_guards);

// if (m_time_averaging == "fixed_start" || m_time_averaging == "dynamic_start") {
// // Loop over the output levels and divide by the number of steps in the averaging period
// for (int lev = 0; lev < nlev_output; ++lev) {
// m_sum_mf_output.at(i_buffer).at(lev).mult(1./m_average_period_steps);
// }
//
// m_flush_format->WriteToFile(
// m_varnames, m_sum_mf_output.at(i_buffer), m_geom_output.at(i_buffer), warpx.getistep(),
// warpx.gett_new(0),
// m_output_species.at(i_buffer), nlev_output, m_file_prefix,
// m_file_min_digits, m_plot_raw_fields, m_plot_raw_fields_guards);
//
//
// }
if (m_time_average_type == TimeAverageType::Static || m_time_average_type == TimeAverageType::Dynamic) {
// Loop over the output levels and divide by the number of steps in the averaging period
for (int lev = 0; lev < nlev_output; ++lev) {
m_sum_mf_output.at(i_buffer).at(lev).mult(1./m_average_period_steps);
}

m_flush_format->WriteToFile(
m_varnames, m_sum_mf_output.at(i_buffer), m_geom_output.at(i_buffer), warpx.getistep(),
warpx.gett_new(0),
m_output_species.at(i_buffer), nlev_output, m_file_prefix,
m_file_min_digits, m_plot_raw_fields, m_plot_raw_fields_guards);

}

FlushRaw();
}
Expand All @@ -178,18 +218,20 @@ FullDiagnostics::DoDump (int step, int /*i_buffer*/, bool force_flush)
bool
FullDiagnostics::DoComputeAndPack (int step, bool force_flush)
{
// if (m_time_averaging == "dynamic_start")
// m_average_start_step = m_intervals.nextContains(step) - m_average_period_steps;
//
// // add logic here to do compute and pack if m_intervals.contains (step+1-time_average_period) or if (cur_step>time_average_startstep)
// bool in_averaging_period = false;
// if (step > m_intervals.nextContains(step) - m_average_start_step && step <= m_intervals.nextContains(step))
// in_averaging_period = true;
//
if (m_time_averaging == "dynamic_start") {
m_average_start_step = m_intervals.nextContains(step) - m_average_period_steps;
}

// add logic here to do compute and pack if m_intervals.contains (step+1-time_average_period) or if (cur_step>time_average_startstep)
bool in_averaging_period = false;
if (step > m_intervals.nextContains(step) - m_average_start_step && step <= m_intervals.nextContains(step)) {
in_averaging_period = true;
}

// Data must be computed and packed for full diagnostics
// whenever the data needs to be flushed.
//return (force_flush || m_intervals.contains(step+1) || in_averaging_period);
return (force_flush || m_intervals.contains(step+1) );
return (force_flush || m_intervals.contains(step+1) || in_averaging_period);
//return (force_flush || m_intervals.contains(step+1) );

}

Expand Down Expand Up @@ -621,6 +663,18 @@ FullDiagnostics::InitializeBufferData (int i_buffer, int lev, bool restart ) {
diag_dom.setHi( idim, warpx.Geom(lev).ProbLo(idim) +
(ba.getCellCenteredBox( static_cast<int>(ba.size())-1 ).bigEnd(idim) + 1) * warpx.Geom(lev).CellSize(idim));
}

if (m_time_average_type == TimeAverageType::Dynamic) {

// already checked in ReadParameters that only one of them is set
if (m_average_period_steps > 0) {
m_average_period_time = m_average_period_steps * warpx.getdt(0);
} else if (m_average_period_time > 0) {
m_average_period_steps = static_cast<int> (std::round(m_average_period_time / warpx.getdt(0)));
}

// CONTINUE HERE
}
}

WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
Expand Down

0 comments on commit 48cc53e

Please sign in to comment.