Skip to content

Commit

Permalink
amrex::Parser
Browse files Browse the repository at this point in the history
Replace WarpXParser with amrex::Parser. Roundoff errors are expected because
of additional optimization in amrex::Parser.
  • Loading branch information
WeiqunZhang committed Jul 7, 2021
1 parent 64702b8 commit 3ceb145
Show file tree
Hide file tree
Showing 55 changed files with 203 additions and 7,407 deletions.
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ endif()
option(WarpX_MPI_THREAD_MULTIPLE "MPI thread-multiple support, i.e. for async_io" ON)
mark_as_advanced(WarpX_MPI_THREAD_MULTIPLE)

set(WarpX_PARSER_DEPTH 24 CACHE STRING
"Maximum parser depth for input file functions")
mark_as_advanced(WarpX_PARSER_DEPTH)

option(WarpX_amrex_internal "Download & build AMReX" ON)

# change the default build type to RelWithDebInfo (or Release) instead of Debug
Expand Down Expand Up @@ -192,7 +188,6 @@ add_subdirectory(Source/Filter)
add_subdirectory(Source/Initialization)
add_subdirectory(Source/Laser)
add_subdirectory(Source/Parallelization)
add_subdirectory(Source/Parser)
add_subdirectory(Source/Particles)
add_subdirectory(Source/Python)
add_subdirectory(Source/Utils)
Expand Down Expand Up @@ -291,14 +286,9 @@ if(WarpX_PSATD)
target_compile_definitions(WarpX PUBLIC WARPX_USE_PSATD)
endif()

target_compile_definitions(WarpX PUBLIC
WARPX_PARSER_DEPTH=${WarpX_PARSER_DEPTH})

# <cmath>: M_PI
# <unistd.h>: parser
if(WIN32)
target_compile_definitions(WarpX PRIVATE _USE_MATH_DEFINES)
target_compile_definitions(WarpX PRIVATE YY_NO_UNISTD_H)
endif()


Expand Down
1 change: 0 additions & 1 deletion Docs/source/install/cmake.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ CMake Option Default & Values Descr
``WarpX_MPI`` **ON**/OFF Multi-node support (message-passing)
``WarpX_MPI_THREAD_MULTIPLE`` **ON**/OFF MPI thread-multiple support, i.e. for ``async_io``
``WarpX_OPENPMD`` ON/**OFF** openPMD I/O (HDF5, ADIOS)
``WarpX_PARSER_DEPTH`` **24** Maximum parser depth for input file functions
``WarpX_PRECISION`` SINGLE/**DOUBLE** Floating point precision (single/double)
``WarpX_PSATD`` ON/**OFF** Spectral solver
``WarpX_QED`` **ON**/OFF QED support (requires PICSAR)
Expand Down
7 changes: 2 additions & 5 deletions Docs/source/usage/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Input Parameters
================

.. note::
The WarpXParser (see :ref:`running-cpp-parameters-parser`) is used for the right-hand-side of all input parameters that consist of one or more floats, so expressions like ``<species_name>.density_max = "2.+1."`` and/or using user-defined constants are accepted. See below for more detail.
:cpp:`amrex::Parser` (see :ref:`running-cpp-parameters-parser`) is used for the right-hand-side of all input parameters that consist of one or more floats, so expressions like ``<species_name>.density_max = "2.+1."`` and/or using user-defined constants are accepted. See below for more detail.

.. _running-cpp-parameters-overall:

Expand Down Expand Up @@ -353,7 +353,7 @@ Distribution across MPI ranks and parallelization
Math parser and user-defined constants
--------------------------------------

WarpX provides a math parser that reads expressions in the input file.
WarpX uses AMReX's math parser that reads expressions in the input file.
It can be used in all input parameters that consist of one or more floats.
Note that when multiple floats are expected, the expressions are space delimited.

Expand Down Expand Up @@ -402,9 +402,6 @@ user-defined constant (see below) and ``x`` and ``y`` are spatial coordinates. T
``(x>0)`` is ``1`` where ``x>0`` and ``0`` where ``x<=0``. It allows the user to
define functions by intervals.
Alternatively the expression above can be written as ``if(x>0, a0*x**2 * (1-y*1.e2), 0)``.
The parser reads mathematical functions into an `abstract syntax tree (AST) <https://en.wikipedia.org/wiki/Abstract_syntax_tree>`_, which supports a maximum depth (see :ref:`build options <building-cmake>`).
Additional terms in a function can create a level of depth in the AST, e.g. ``a+b+c+d`` is parsed in groups of ``[+ a [+ b [+ c [+ d]]]]`` (depth: 4).
A trick to reduce this depth for the parser, e.g. when reaching the limit, is to group explicitly, e.g. via ``(a+b)+(c+d)``, which is parsed in groups of ``[+ [+ a b] [+ c d]]`` (depth: 2).

.. _running-cpp-parameters-particle:

Expand Down
3 changes: 1 addition & 2 deletions Source/Diagnostics/FlushFormats/FlushFormatPlotfile.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "FlushFormatPlotfile.H"

#include "Diagnostics/ParticleDiag/ParticleDiag.H"
#include "Parser/WarpXParserWrapper.H"
#include "Particles/Filter/FilterFunctors.H"
#include "Particles/WarpXParticleContainer.H"
#include "Utils/Interpolate.H"
Expand Down Expand Up @@ -353,7 +352,7 @@ FlushFormatPlotfile::WriteParticles(const std::string& dir,
UniformFilter const uniform_filter(particle_diags[i].m_do_uniform_filter,
particle_diags[i].m_uniform_stride);
ParserFilter parser_filter(particle_diags[i].m_do_parser_filter,
getParser(particle_diags[i].m_particle_filter_parser),
particle_diags[i].m_particle_filter_parser->compile<7>(),
pc->getMass());
parser_filter.m_units = InputUnits::SI;
GeometryFilter const geometry_filter(particle_diags[i].m_do_geom_filter,
Expand Down
4 changes: 2 additions & 2 deletions Source/Diagnostics/ParticleDiag/ParticleDiag.H
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "ParticleDiag_fwd.H"

#include "Particles/WarpXParticleContainer_fwd.H"
#include "Parser/WarpXParserWrapper_fwd.H"

#include <AMReX_Parser.H>
#include <AMReX_REAL.H>
#include <AMReX_RealBox.H>
#include <AMReX_Vector.H>
Expand All @@ -27,7 +27,7 @@ public:
bool m_do_geom_filter = false;
amrex::Real m_random_fraction = 1.0;
int m_uniform_stride = 1;
std::unique_ptr<ParserWrapper<7>> m_particle_filter_parser;
std::unique_ptr<amrex::Parser> m_particle_filter_parser;
amrex::RealBox m_diag_domain;

private:
Expand Down
3 changes: 1 addition & 2 deletions Source/Diagnostics/ParticleDiag/ParticleDiag.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ParticleDiag.H"

#include "Diagnostics/ParticleDiag/ParticleDiag.H"
#include "Parser/WarpXParserWrapper.H"
#include "Particles/WarpXParticleContainer.H"
#include "Utils/WarpXUtil.H"
#include "WarpX.H"
Expand Down Expand Up @@ -82,7 +81,7 @@ ParticleDiag::ParticleDiag(std::string diag_name, std::string name, WarpXParticl
std::string function_string = "";
Store_parserString(pp,"plot_filter_function(t,x,y,z,ux,uy,uz)",
function_string);
m_particle_filter_parser = std::make_unique<ParserWrapper<7>>(
m_particle_filter_parser = std::make_unique<amrex::Parser>(
makeParser(function_string,{"t","x","y","z","ux","uy","uz"}));
}
}
6 changes: 3 additions & 3 deletions Source/Diagnostics/ReducedDiags/FieldReduction.H
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#ifndef WARPX_DIAGNOSTICS_REDUCEDDIAGS_FIELDREDUCTION_H_
#define WARPX_DIAGNOSTICS_REDUCEDDIAGS_FIELDREDUCTION_H_

#include "Parser/WarpXParserWrapper.H"
#include "ReducedDiags.H"
#include "Utils/CoarsenIO.H"
#include "WarpX.H"
Expand All @@ -25,6 +24,7 @@
#include <AMReX_MFIter.H>
#include <AMReX_MultiFab.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Parser.H>
#include <AMReX_REAL.H>
#include <AMReX_RealBox.H>
#include <AMReX_Reduce.H>
Expand Down Expand Up @@ -63,7 +63,7 @@ private:
/// Parser to read expression to be reduced from the input file.
/// 9 elements are x, y, z, Ex, Ey, Ez, Bx, By, Bz
static constexpr int m_nvars = 9;
std::unique_ptr<ParserWrapper<m_nvars>> m_parser;
std::unique_ptr<amrex::Parser> m_parser;

// Type of reduction (e.g. Maximum, Minimum or Sum)
int m_reduction_type;
Expand Down Expand Up @@ -127,7 +127,7 @@ public:
}

// get parser
HostDeviceParser<m_nvars> reduction_function_parser = getParser(m_parser);
auto reduction_function_parser = m_parser->compile<m_nvars>();

// MFIter loop to interpolate fields to cell center and perform reduction
#ifdef AMREX_USE_OMP
Expand Down
2 changes: 1 addition & 1 deletion Source/Diagnostics/ReducedDiags/FieldReduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ FieldReduction::FieldReduction (std::string rd_name)
std::string parser_string = "";
Store_parserString(pp_rd_name,"reduced_function(x,y,z,Ex,Ey,Ez,Bx,By,Bz)",
parser_string);
m_parser = std::make_unique<ParserWrapper<m_nvars>>(
m_parser = std::make_unique<amrex::Parser>(
makeParser(parser_string,{"x","y","z","Ex","Ey","Ez","Bx","By","Bz"}));

// Replace all newlines and possible following whitespaces with a single whitespace. This
Expand Down
6 changes: 3 additions & 3 deletions Source/Diagnostics/ReducedDiags/ParticleHistogram.H
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#ifndef WARPX_DIAGNOSTICS_REDUCEDDIAGS_PARTICLEHISTOGRAM_H_
#define WARPX_DIAGNOSTICS_REDUCEDDIAGS_PARTICLEHISTOGRAM_H_

#include "Parser/WarpXParserWrapper.H"
#include "ReducedDiags.H"

#include <AMReX_Parser.H>
#include <AMReX_REAL.H>

#include <memory>
Expand Down Expand Up @@ -49,10 +49,10 @@ public:
/// Parser to read expression for particle quantity from the input file.
/// 7 elements are t, x, y, z, ux, uy, uz
static constexpr int m_nvars = 7;
std::unique_ptr<ParserWrapper<m_nvars>> m_parser;
std::unique_ptr<amrex::Parser> m_parser;

/// Optional parser to filter particles before doing the histogram
std::unique_ptr<ParserWrapper<m_nvars>> m_parser_filter;
std::unique_ptr<amrex::Parser> m_parser_filter;

/// Whether the filter is activated
bool m_do_parser_filter = false;
Expand Down
8 changes: 4 additions & 4 deletions Source/Diagnostics/ReducedDiags/ParticleHistogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ParticleHistogram::ParticleHistogram (std::string rd_name)
std::string function_string = "";
Store_parserString(pp_rd_name,"histogram_function(t,x,y,z,ux,uy,uz)",
function_string);
m_parser = std::make_unique<ParserWrapper<m_nvars>>(
m_parser = std::make_unique<amrex::Parser>(
makeParser(function_string,{"t","x","y","z","ux","uy","uz"}));

// read normalization type
Expand Down Expand Up @@ -111,7 +111,7 @@ ParticleHistogram::ParticleHistogram (std::string rd_name)
if (m_do_parser_filter) {
std::string filter_string = "";
Store_parserString(pp_rd_name,"filter_function(t,x,y,z,ux,uy,uz)", filter_string);
m_parser_filter = std::make_unique<ParserWrapper<m_nvars>>(
m_parser_filter = std::make_unique<amrex::Parser>(
makeParser(filter_string,{"t","x","y","z","ux","uy","uz"}));
}

Expand Down Expand Up @@ -165,10 +165,10 @@ void ParticleHistogram::ComputeDiags (int step)
auto & myspc = mypc.GetParticleContainer(m_selected_species_id);

// get parser
HostDeviceParser<m_nvars> fun_partparser = getParser(m_parser);
auto fun_partparser = m_parser->compile<m_nvars>();

// get filter parser
HostDeviceParser<m_nvars> fun_filterparser = getParser(m_parser_filter);
auto fun_filterparser = m_parser_filter->compile<m_nvars>();

// declare local variables
auto const num_bins = m_bin_num;
Expand Down
3 changes: 1 addition & 2 deletions Source/Diagnostics/WarpXOpenPMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "Diagnostics/ParticleDiag/ParticleDiag.H"
#include "FieldIO.H"
#include "Parser/WarpXParserWrapper.H"
#include "Particles/Filter/FilterFunctors.H"
#include "Utils/RelativeCellPosition.H"
#include "Utils/WarpXAlgorithmSelection.H"
Expand Down Expand Up @@ -459,7 +458,7 @@ WarpXOpenPMDPlot::WriteOpenPMDParticles (const amrex::Vector<ParticleDiag>& part
UniformFilter const uniform_filter(particle_diags[i].m_do_uniform_filter,
particle_diags[i].m_uniform_stride);
ParserFilter parser_filter(particle_diags[i].m_do_parser_filter,
getParser(particle_diags[i].m_particle_filter_parser),
particle_diags[i].m_particle_filter_parser->compile<7>(),
pc->getMass());
parser_filter.m_units = InputUnits::SI;
GeometryFilter const geometry_filter(particle_diags[i].m_do_geom_filter,
Expand Down
79 changes: 39 additions & 40 deletions Source/EmbeddedBoundary/WarpXInitEB.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#include "Parser/WarpXParser.H"
#include "Parser/WarpXParserWrapper.H"
#include "Utils/WarpXUtil.H"
#include "WarpX.H"

#include <AMReX.H>
#include <AMReX_Array.H>
#include <AMReX_Array4.H>
#include <AMReX_BLProfiler.H>
#include <AMReX_Box.H>
#include <AMReX_BoxArray.H>
#include <AMReX_BoxList.H>
#include <AMReX_Config.H>
#ifdef AMREX_USE_EB
# include <AMReX_EB2.H>
#endif
#include <AMReX_FabArray.H>
#include <AMReX_FabFactory.H>
#include <AMReX_GpuControl.H>
#include <AMReX_GpuDevice.H>
#include <AMReX_GpuQualifiers.H>
#include <AMReX_IntVect.H>
#include <AMReX_Loop.H>
#include <AMReX_MFIter.H>
#include <AMReX_MultiFab.H>
#ifdef AMREX_USE_EB
# include <AMReX_ParmParse.H>
#endif
#include <AMReX_REAL.H>
#include <AMReX_SPACE.H>
#include <AMReX_Vector.H>

#include <array>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
# include "Utils/WarpXUtil.H"

# include <AMReX.H>
# include <AMReX_Array.H>
# include <AMReX_Array4.H>
# include <AMReX_BLProfiler.H>
# include <AMReX_Box.H>
# include <AMReX_BoxArray.H>
# include <AMReX_BoxList.H>
# include <AMReX_Config.H>
# include <AMReX_EB2.H>
# include <AMReX_FabArray.H>
# include <AMReX_FabFactory.H>
# include <AMReX_GpuControl.H>
# include <AMReX_GpuDevice.H>
# include <AMReX_GpuQualifiers.H>
# include <AMReX_IntVect.H>
# include <AMReX_Loop.H>
# include <AMReX_MFIter.H>
# include <AMReX_MultiFab.H>
# include <AMReX_ParmParse.H>
# include <AMReX_Parser.H>
# include <AMReX_REAL.H>
# include <AMReX_SPACE.H>
# include <AMReX_Vector.H>

# include <array>
# include <cstdlib>
# include <memory>
# include <string>
# include <vector>

#endif

#ifdef AMREX_USE_EB
namespace {
class ParserIF
: public amrex::GPUable
{
public:
ParserIF (const ParserWrapper<3>& a_parser)
: m_parser(a_parser.getParser())
ParserIF (const ParserExecutor<3>& a_parser)
: m_parser(a_parser)
{}

ParserIF (const ParserIF& rhs) noexcept = default;
Expand All @@ -66,7 +66,7 @@ namespace {
}

private:
HostDeviceParser<3> m_parser;
ParserExecutor<3> m_parser;
};
}
#endif
Expand All @@ -81,10 +81,9 @@ WarpX::InitEB ()
std::string impf;
pp_warpx.query("eb_implicit_function", impf);
if (! impf.empty()) {
WarpXParser wp = makeParser(impf, {"x", "y", "z"});
m_eb_if_parser = std::make_unique<ParserWrapper<3> >(wp);
ParserIF pif(*m_eb_if_parser);
auto gshop = amrex::EB2::makeShop(pif);
auto eb_if_parser = makeParser(impf, {"x", "y", "z"});
ParserIF pif(m_eb_if_parser->compile<3>());
auto gshop = amrex::EB2::makeShop(pif, eb_if_parser);
amrex::EB2::Build(gshop, Geom(maxLevel()), maxLevel(), maxLevel());
} else {
amrex::ParmParse pp_eb2("eb2");
Expand Down
8 changes: 5 additions & 3 deletions Source/FieldSolver/ElectrostaticSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "WarpX.H"

#include "Parallelization/GuardCellManager.H"
#include "Parser/WarpXParser.H"
#include "Particles/MultiParticleContainer.H"
#include "Particles/WarpXParticleContainer.H"
#include "Utils/WarpXAlgorithmSelection.H"
Expand Down Expand Up @@ -39,6 +38,7 @@
#endif
#include <AMReX_MultiFab.H>
#include <AMReX_ParmParse.H>
#include <AMReX_Parser.H>
#include <AMReX_REAL.H>
#include <AMReX_SPACE.H>
#include <AMReX_Vector.H>
Expand Down Expand Up @@ -548,9 +548,11 @@ WarpX::getPhiBC( const int idim, amrex::Real &pot_lo, amrex::Real &pot_hi ) cons
#endif

auto parser_lo = makeParser(potential_lo_str, {"t"});
pot_lo = parser_lo.eval(gett_new(0));
auto parser_lo_exe = parser_lo.compileHost<1>();
pot_lo = parser_lo_exe(gett_new(0));
auto parser_hi = makeParser(potential_hi_str, {"t"});
pot_hi = parser_hi.eval(gett_new(0));
auto parser_hi_exe = parser_hi.compileHost<1>();
pot_hi = parser_hi_exe(gett_new(0));
}

/* \bried Compute the electric field that corresponds to `phi`, and
Expand Down
Loading

0 comments on commit 3ceb145

Please sign in to comment.