diff options
Diffstat (limited to 'Source')
54 files changed, 8247 insertions, 2852 deletions
diff --git a/Source/BoundaryConditions/PML.cpp b/Source/BoundaryConditions/PML.cpp index 03373995d..c3449cecd 100644 --- a/Source/BoundaryConditions/PML.cpp +++ b/Source/BoundaryConditions/PML.cpp @@ -546,8 +546,8 @@ void PML::ExchangeE (const std::array<amrex::MultiFab*,3>& E_fp, const std::array<amrex::MultiFab*,3>& E_cp) { - ExchangeB(PatchType::fine, E_fp); - ExchangeB(PatchType::coarse, E_cp); + ExchangeE(PatchType::fine, E_fp); + ExchangeE(PatchType::coarse, E_cp); } void diff --git a/Source/Diagnostics/BoostedFrameDiagnostic.H b/Source/Diagnostics/BoostedFrameDiagnostic.H index 17f2b161c..e35d307a6 100644 --- a/Source/Diagnostics/BoostedFrameDiagnostic.H +++ b/Source/Diagnostics/BoostedFrameDiagnostic.H @@ -6,6 +6,7 @@ #include <AMReX_VisMF.H> #include <AMReX_PlotFileUtil.H> #include <AMReX_ParallelDescriptor.H> +#include <AMReX_Geometry.H> #include "MultiParticleContainer.H" #include "WarpXConst.H" @@ -36,9 +37,13 @@ class BoostedFrameDiagnostic { amrex::Real current_z_lab; amrex::Real current_z_boost; int file_num; + int initial_i; + const BoostedFrameDiagnostic& my_bfd; - LabSnapShot(amrex::Real t_lab_in, amrex::Real zmin_lab_in, - amrex::Real zmax_lab_in, int file_num_in); + LabSnapShot(amrex::Real t_lab_in, amrex::Real t_boost, + amrex::Real zmin_lab_in, + amrex::Real zmax_lab_in, int file_num_in, + const BoostedFrameDiagnostic& bfd); /// /// This snapshot is at time t_lab, and the simulation is at time t_boost. @@ -64,11 +69,13 @@ class BoostedFrameDiagnostic { amrex::Real dt_snapshots_lab_; amrex::Real dt_boost_; int N_snapshots_; - int Nz_lab_; + unsigned Nx_lab_; + unsigned Ny_lab_; + unsigned Nz_lab_; int boost_direction_; amrex::Vector<std::unique_ptr<amrex::MultiFab> > data_buffer_; - amrex::Vector<amrex::Vector<WarpXParticleContainer::DiagnosticParticleData> > particles_buffer_; + amrex::Vector<amrex::Vector<WarpXParticleContainer::DiagnosticParticleData> > particles_buffer_; int num_buffer_ = 256; int max_box_size_ = 256; amrex::Vector<int> buff_counter_; @@ -77,13 +84,18 @@ class BoostedFrameDiagnostic { void writeParticleData(const WarpXParticleContainer::DiagnosticParticleData& pdata, const std::string& name, const int i_lab); - -public: +#ifdef WARPX_USE_HDF5 + void writeParticleDataHDF5(const WarpXParticleContainer::DiagnosticParticleData& pdata, + const std::string& name, const std::string& species_name); +#endif +public: + BoostedFrameDiagnostic(amrex::Real zmin_lab, amrex::Real zmax_lab, amrex::Real v_window_lab, amrex::Real dt_snapshots_lab, int N_snapshots, amrex::Real gamma_boost, - amrex::Real t_boost, amrex::Real dt_boost, int boost_direction); + amrex::Real t_boost, amrex::Real dt_boost, int boost_direction, + const amrex::Geometry& geom); void Flush(const amrex::Geometry& geom); diff --git a/Source/Diagnostics/BoostedFrameDiagnostic.cpp b/Source/Diagnostics/BoostedFrameDiagnostic.cpp index 6c3e3c58a..13972075d 100644 --- a/Source/Diagnostics/BoostedFrameDiagnostic.cpp +++ b/Source/Diagnostics/BoostedFrameDiagnostic.cpp @@ -1,15 +1,456 @@ -#include "BoostedFrameDiagnostic.H" #include <AMReX_MultiFabUtil.H> + +#include "BoostedFrameDiagnostic.H" #include "WarpX_f.H" #include "WarpX.H" using namespace amrex; +#ifdef WARPX_USE_HDF5 + +#include <hdf5.h> + +/* + Helper functions for doing the HDF5 IO. + + */ +namespace +{ + const std::vector<std::string> particle_field_names = {"w", "x", "y", "z", "ux", "uy", "uz"}; + const std::vector<std::string> mesh_field_names = + {"Ex", "Ey", "Ez", "Bx", "By", "Bz", "jx", "jy", "jz", "rho"}; + + /* + Creates the HDF5 file in truncate mode and closes it. + Should be run only by the root process. + */ + void output_create(const std::string& file_path) { + BL_PROFILE("output_create"); + hid_t file = H5Fcreate(file_path.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + if (file < 0) { + amrex::Abort("Error: could not create file at " + file_path); + } + H5Fclose(file); + } + + /* + Writes a single string attribute to the given group. + Should only be called by the root process. + */ + void write_string_attribute(hid_t& group, const std::string& key, const std::string& val) + { + hid_t str_type = H5Tcopy(H5T_C_S1); + hid_t scalar_space = H5Screate(H5S_SCALAR); + + // Fix the str_type length for the format string. + H5Tset_size(str_type, strlen(val.c_str())); + + hid_t attr = H5Acreate(group, key.c_str(), str_type, scalar_space, H5P_DEFAULT, H5P_DEFAULT); + H5Awrite(attr, str_type, val.c_str()); + + H5Aclose(attr); + H5Sclose(scalar_space); + H5Tclose(str_type); + } + + /* + Writes a single double attribute to the given group. + Should only be called by the root process. + */ + void write_double_attribute(hid_t& group, const std::string& key, const double val) + { + hid_t scalar_space = H5Screate(H5S_SCALAR); + + hid_t attr = H5Acreate(group, key.c_str(), H5T_IEEE_F32LE, scalar_space, + H5P_DEFAULT, H5P_DEFAULT); + H5Awrite(attr, H5T_NATIVE_DOUBLE, &val); + + H5Aclose(attr); + H5Sclose(scalar_space); + } + + /* + Opens the output file and writes all of metadata attributes. + Should be run only by the root process. + */ + void output_write_metadata(const std::string& file_path, + const int istep, const Real time, const Real dt) + { + BL_PROFILE("output_write_metadata"); + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + + write_string_attribute(file, "software", "warpx"); + write_string_attribute(file, "softwareVersion", "0.0.0"); + write_string_attribute(file, "meshesPath", "fields/"); + write_string_attribute(file, "iterationEncoding", "fileBased"); + write_string_attribute(file, "iterationFormat", "data%T.h5"); + write_string_attribute(file, "openPMD", "1.1.0"); + write_string_attribute(file, "basePath", "/data/%T/"); + + hid_t group = H5Gcreate(file, "data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + group = H5Gcreate(group, std::to_string(istep).c_str(), + H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + + write_double_attribute(group, "time", time); + write_double_attribute(group, "timeUnitSI", 1.0); + write_double_attribute(group, "dt", dt); + + // Field groups + group = H5Gcreate(group, "fields", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + + // Close all resources. + H5Gclose(group); + H5Fclose(file); + H5close(); + } + + /* + Creates a dataset with the given cell dimensions, at the path + "/native_fields/(field_name)". + Should be run only by the master rank. + */ + void output_create_field(const std::string& file_path, const std::string& field_path, + const unsigned nx, const unsigned ny, const unsigned nz) + { + BL_PROFILE("output_create_field"); + + // Open the output. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + // Create a 3D, nx x ny x nz dataspace. +#if (AMREX_SPACEDIM == 3) + hsize_t dims[3] = {nx, ny, nz}; +#else + hsize_t dims[3] = {nx, nz}; +#endif + hid_t grid_space = H5Screate_simple(AMREX_SPACEDIM, dims, NULL); + + // Create the dataset. + hid_t dataset = H5Dcreate(file, field_path.c_str(), H5T_IEEE_F64LE, + grid_space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + + if (dataset < 0) + { + amrex::Abort("Error: could not create dataset. H5 returned " + + std::to_string(dataset) + "\n"); + } + + // Close resources. + H5Dclose(dataset); + H5Sclose(grid_space); + H5Fclose(file); + } + + /* + Creates a group associated with a single particle species. + Should be run by all processes collectively. + */ + void output_create_species_group(const std::string& file_path, const std::string& species_name) + { + MPI_Comm comm = MPI_COMM_WORLD; + MPI_Info info = MPI_INFO_NULL; + int mpi_rank; + MPI_Comm_rank(comm, &mpi_rank); + + // Create the file access prop list. + hid_t pa_plist = H5Pcreate(H5P_FILE_ACCESS); + H5Pset_fapl_mpio(pa_plist, comm, info); + + // Open the output. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, pa_plist); + + hid_t group = H5Gcreate(file, species_name.c_str(), + H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + H5Gclose(group); + H5Fclose(file); + + } + + /* + Resize an extendible dataset, suitable for storing particle data. + Should be run only by the master rank. + */ + long output_resize_particle_field(const std::string& file_path, const std::string& field_path, + const long num_to_add) + { + BL_PROFILE("output_resize_particle_field"); + + // Open the output. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + + int rank; + hsize_t dims[1]; + + hid_t dataset = H5Dopen2 (file, field_path.c_str(), H5P_DEFAULT); + hid_t filespace = H5Dget_space (dataset); + rank = H5Sget_simple_extent_ndims (filespace); + herr_t status = H5Sget_simple_extent_dims (filespace, dims, NULL); + + // set new size + hsize_t new_size[1]; + new_size[0] = dims[0] + num_to_add; + status = H5Dset_extent (dataset, new_size); + + if (status < 0) + { + amrex::Abort("Error: set extent filed on dataset " + + std::to_string(dataset) + "\n"); + } + + // Close resources. + H5Sclose(filespace); + H5Dclose(dataset); + H5Fclose(file); + + return dims[0]; + } + + /* + Writes to a dataset that has been extended to the proper size. Suitable for writing particle data. + Should be run on all ranks collectively. + */ + void output_write_particle_field(const std::string& file_path, const std::string& field_path, + const Real* data_ptr, const long count, const long index) + { + BL_PROFILE("output_write_particle_field"); + + MPI_Comm comm = MPI_COMM_WORLD; + MPI_Info info = MPI_INFO_NULL; + int mpi_rank; + MPI_Comm_rank(comm, &mpi_rank); + + // Create the file access prop list. + hid_t pa_plist = H5Pcreate(H5P_FILE_ACCESS); + H5Pset_fapl_mpio(pa_plist, comm, info); + + // Open the output. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, pa_plist); + + int RANK = 1; + hsize_t offset[1]; + hsize_t dims[1]; + herr_t status; + + hid_t dataset = H5Dopen (file, field_path.c_str(), H5P_DEFAULT); + + // Make sure the dataset is there. + if (dataset < 0) + { + amrex::Abort("Error on rank " + std::to_string(mpi_rank) + + ". Count not find dataset " + field_path + "\n"); + } + + hid_t filespace = H5Dget_space (dataset); + + offset[0] = index; + dims[0] = count; + + // Create collective io prop list. + hid_t collective_plist = H5Pcreate(H5P_DATASET_XFER); + H5Pset_dxpl_mpio(collective_plist, H5FD_MPIO_INDEPENDENT); + + if (count > 0) { + + /* Define memory space */ + hid_t memspace = H5Screate_simple (RANK, dims, NULL); + + status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, + dims, NULL); + + if (status < 0) + { + amrex::Abort("Error on rank " + std::to_string(ParallelDescriptor::MyProc()) + + " could not select hyperslab.\n"); + } + + /* Write the data to the extended portion of dataset */ + status = H5Dwrite(dataset, H5T_NATIVE_DOUBLE, memspace, + filespace, collective_plist, data_ptr); + + if (status < 0) + { + amrex::Abort("Error on rank " + std::to_string(ParallelDescriptor::MyProc()) + + " could not write hyperslab.\n"); + } + + status = H5Sclose (memspace); + } + + ParallelDescriptor::Barrier(); + + // Close resources. + H5Pclose(collective_plist); + H5Sclose(filespace); + H5Dclose(dataset); + H5Fclose(file); + H5Pclose(pa_plist); + } + + /* + Creates an extendible dataset, suitable for storing particle data. + Should be run on all ranks collectively. + */ + void output_create_particle_field(const std::string& file_path, const std::string& field_path) + { + BL_PROFILE("output_create_particle_field"); + + MPI_Comm comm = MPI_COMM_WORLD; + MPI_Info info = MPI_INFO_NULL; + int mpi_rank; + MPI_Comm_rank(comm, &mpi_rank); + + // Create the file access prop list. + hid_t pa_plist = H5Pcreate(H5P_FILE_ACCESS); + H5Pset_fapl_mpio(pa_plist, comm, info); + + // Open the output. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, pa_plist); + + constexpr int RANK = 1; + hsize_t dims[1] = {0}; + hsize_t maxdims[1] = {H5S_UNLIMITED}; + hsize_t chunk_dims[2] = {4}; + + hid_t dataspace = H5Screate_simple (RANK, dims, maxdims); + + // Enable chunking + hid_t prop = H5Pcreate (H5P_DATASET_CREATE); + herr_t status = H5Pset_chunk (prop, RANK, chunk_dims); + + hid_t dataset = H5Dcreate2 (file, field_path.c_str(), H5T_NATIVE_DOUBLE, dataspace, + H5P_DEFAULT, prop, H5P_DEFAULT); + + if (dataset < 0) + { + amrex::Abort("Error: could not create dataset. H5 returned " + + std::to_string(dataset) + "\n"); + } + + // Close resources. + H5Dclose(dataset); + H5Pclose(prop); + H5Sclose(dataspace); + H5Fclose(file); + } + + /* + Write the only component in the multifab to the dataset given by field_name. + Uses hdf5-parallel. + */ + void output_write_field(const std::string& file_path, + const std::string& field_path, + const MultiFab& mf, const int comp) + { + + BL_PROFILE("output_write_field"); + + MPI_Comm comm = MPI_COMM_WORLD; + MPI_Info info = MPI_INFO_NULL; + int mpi_rank; + MPI_Comm_rank(comm, &mpi_rank); + + // Create the file access prop list. + hid_t pa_plist = H5Pcreate(H5P_FILE_ACCESS); + H5Pset_fapl_mpio(pa_plist, comm, info); + + // Open the file, and the group. + hid_t file = H5Fopen(file_path.c_str(), H5F_ACC_RDWR, pa_plist); + // Open the field dataset. + hid_t dataset = H5Dopen(file, field_path.c_str(), H5P_DEFAULT); + + // Make sure the dataset is there. + if (dataset < 0) + { + amrex::Abort("Error on rank " + std::to_string(mpi_rank) + + ". Count not find dataset " + field_path + "\n"); + } + + // Grab the dataspace of the field dataset from file. + hid_t file_dataspace = H5Dget_space(dataset); + + // Create collective io prop list. + hid_t collective_plist = H5Pcreate(H5P_DATASET_XFER); + H5Pset_dxpl_mpio(collective_plist, H5FD_MPIO_INDEPENDENT); + + // Iterate over Fabs, select matching hyperslab and write. + hid_t status; + // slab lo index and shape. +#if (AMREX_SPACEDIM == 3) + hsize_t slab_offsets[3], slab_dims[3]; +#else + hsize_t slab_offsets[2], slab_dims[2]; +#endif + hid_t slab_dataspace; + + int write_count = 0; + + std::vector<Real> transposed_data; + + for (MFIter mfi(mf); mfi.isValid(); ++mfi) + { + const Box& box = mfi.validbox(); + const int *lo_vec = box.loVect(); + const int *hi_vec = box.hiVect(); + + transposed_data.resize(box.numPts(), 0.0); + + // Set slab offset and shape. + for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) + { + AMREX_ASSERT(lo_vec[idim] >= 0); + AMREX_ASSERT(hi_vec[idim] > lo_vec[idim]); + slab_offsets[idim] = lo_vec[idim]; + slab_dims[idim] = hi_vec[idim] - lo_vec[idim] + 1; + } + + int cnt = 0; + AMREX_D_TERM( + for (int i = lo_vec[0]; i <= hi_vec[0]; ++i), + for (int j = lo_vec[1]; j <= hi_vec[1]; ++j), + for (int k = lo_vec[2]; k <= hi_vec[2]; ++k)) + transposed_data[cnt++] = mf[mfi](IntVect(AMREX_D_DECL(i, j, k)), comp); + + // Create the slab space. + slab_dataspace = H5Screate_simple(AMREX_SPACEDIM, slab_dims, NULL); + + // Select the hyperslab matching this fab. + status = H5Sselect_hyperslab(file_dataspace, H5S_SELECT_SET, + slab_offsets, NULL, slab_dims, NULL); + if (status < 0) + { + amrex::Abort("Error on rank " + std::to_string(mpi_rank) + + " could not select hyperslab.\n"); + } + + // Write this pencil. + status = H5Dwrite(dataset, H5T_NATIVE_DOUBLE, slab_dataspace, + file_dataspace, collective_plist, transposed_data.data()); + if (status < 0) + { + amrex::Abort("Error on rank " + std::to_string(mpi_rank) + + " could not write hyperslab.\n"); + } + + H5Sclose(slab_dataspace); + write_count++; + } + + ParallelDescriptor::Barrier(); + + // Close HDF5 resources. + H5Pclose(collective_plist); + H5Sclose(file_dataspace); + H5Dclose(dataset); + H5Fclose(file); + H5Pclose(pa_plist); + } +} +#endif + BoostedFrameDiagnostic:: BoostedFrameDiagnostic(Real zmin_lab, Real zmax_lab, Real v_window_lab, Real dt_snapshots_lab, int N_snapshots, Real gamma_boost, Real t_boost, Real dt_boost, - int boost_direction) + int boost_direction, const Geometry& geom) : gamma_boost_(gamma_boost), dt_snapshots_lab_(dt_snapshots_lab), dt_boost_(dt_boost), @@ -28,17 +469,23 @@ BoostedFrameDiagnostic(Real zmin_lab, Real zmax_lab, Real v_window_lab, dz_lab_ = PhysConst::c * dt_boost_ * inv_beta_boost_ * inv_gamma_boost_; inv_dz_lab_ = 1.0 / dz_lab_; - Nz_lab_ = static_cast<int>((zmax_lab - zmin_lab) * inv_dz_lab_); - + Nx_lab_ = geom.Domain().length(0); +#if (AMREX_SPACEDIM == 3) + Ny_lab_ = geom.Domain().length(1); +#else + Ny_lab_ = 1; +#endif + Nz_lab_ = static_cast<unsigned>((zmax_lab - zmin_lab) * inv_dz_lab_); + writeMetaData(); if (WarpX::do_boosted_frame_fields) data_buffer_.resize(N_snapshots); if (WarpX::do_boosted_frame_particles) particles_buffer_.resize(N_snapshots); for (int i = 0; i < N_snapshots; ++i) { Real t_lab = i * dt_snapshots_lab_; - LabSnapShot snapshot(t_lab, zmin_lab + v_window_lab * t_lab, - zmax_lab + v_window_lab * t_lab, i); - snapshot.updateCurrentZPositions(t_boost, inv_gamma_boost_, inv_beta_boost_); + LabSnapShot snapshot(t_lab, t_boost, + zmin_lab + v_window_lab * t_lab, + zmax_lab + v_window_lab * t_lab, i, *this); snapshots_.push_back(snapshot); buff_counter_.push_back(0); if (WarpX::do_boosted_frame_fields) data_buffer_[i].reset( nullptr ); @@ -80,17 +527,28 @@ void BoostedFrameDiagnostic::Flush(const Geometry& geom) MultiFab tmp(buff_ba, buff_dm, ncomp, 0); tmp.copy(*data_buffer_[i], 0, 0, ncomp); - + +#ifdef WARPX_USE_HDF5 + for (int comp = 0; comp < ncomp; ++comp) + output_write_field(snapshots_[i].file_name, mesh_field_names[comp], tmp, comp); +#else std::stringstream ss; ss << snapshots_[i].file_name << "/Level_0/" << Concatenate("buffer", i_lab, 5); VisMF::Write(tmp, ss.str()); +#endif } if (WarpX::do_boosted_frame_particles) { for (int j = 0; j < mypc.nSpecies(); ++j) { +#ifdef WARPX_USE_HDF5 + writeParticleDataHDF5(particles_buffer_[i][j], + snapshots_[i].file_name, + species_names[j]); +#else std::stringstream part_ss; part_ss << snapshots_[i].file_name + "/" + species_names[j] + "/"; writeParticleData(particles_buffer_[i][j], part_ss.str(), i_lab); +#endif } particles_buffer_[i].clear(); } @@ -196,16 +654,28 @@ writeLabFrameData(const MultiFab* cell_centered_data, if (buff_counter_[i] == num_buffer_) { if (WarpX::do_boosted_frame_fields) { +#ifdef WARPX_USE_HDF5 + for (int comp = 0; comp < data_buffer_[i]->nComp(); ++comp) + output_write_field(snapshots_[i].file_name, mesh_field_names[comp], + *data_buffer_[i], comp); +#else std::stringstream mesh_ss; mesh_ss << snapshots_[i].file_name << "/Level_0/" << Concatenate("buffer", i_lab, 5); VisMF::Write(*data_buffer_[i], mesh_ss.str()); +#endif } if (WarpX::do_boosted_frame_particles) { for (int j = 0; j < mypc.nSpecies(); ++j) { +#ifdef WARPX_USE_HDF5 + writeParticleDataHDF5(particles_buffer_[i][j], + snapshots_[i].file_name, + species_names[j]); +#else std::stringstream part_ss; part_ss << snapshots_[i].file_name + "/" + species_names[j] + "/"; writeParticleData(particles_buffer_[i][j], part_ss.str(), i_lab); +#endif } particles_buffer_[i].clear(); } @@ -216,10 +686,57 @@ writeLabFrameData(const MultiFab* cell_centered_data, VisMF::SetHeaderVersion(current_version); } +#ifdef WARPX_USE_HDF5 void BoostedFrameDiagnostic:: -writeParticleData(const WarpXParticleContainer::DiagnosticParticleData& pdata, const std::string& name, - const int i_lab) +writeParticleDataHDF5(const WarpXParticleContainer::DiagnosticParticleData& pdata, + const std::string& name, const std::string& species_name) +{ + auto np = pdata.GetRealData(DiagIdx::w).size(); + + Vector<long> particle_counts(ParallelDescriptor::NProcs(), 0); + Vector<long> particle_offsets(ParallelDescriptor::NProcs(), 0); + + ParallelAllGather::AllGather(np, particle_counts.data(), ParallelContext::CommunicatorAll()); + + long total_np = 0; + for (int i = 0; i < ParallelDescriptor::NProcs(); ++i) { + particle_offsets[i] = total_np; + total_np += particle_counts[i]; + } + + if (total_np == 0) return; + + long old_np = 0; + if (ParallelDescriptor::IOProcessor()) + { + for (int k = 0; k < static_cast<int>(particle_field_names.size()); ++k) + { + std::string field_path = species_name + "/" + particle_field_names[k]; + old_np = output_resize_particle_field(name, field_path, total_np); + } + } + + // Note, this has the effect of an MPI Barrier between the above resize operation + // and the below write. + ParallelDescriptor::ReduceLongMax(old_np); + + // Write data here + for (int k = 0; k < static_cast<int>(particle_field_names.size()); ++k) + { + std::string field_path = species_name + "/" + particle_field_names[k]; + output_write_particle_field(name, field_path, + pdata.GetRealData(k).data(), + particle_counts[ParallelDescriptor::MyProc()], + particle_offsets[ParallelDescriptor::MyProc()] + old_np); + } +} +#endif + +void +BoostedFrameDiagnostic:: +writeParticleData(const WarpXParticleContainer::DiagnosticParticleData& pdata, + const std::string& name, const int i_lab) { BL_PROFILE("BoostedFrameDiagnostic::writeParticleData"); @@ -230,7 +747,7 @@ writeParticleData(const WarpXParticleContainer::DiagnosticParticleData& pdata, c auto np = pdata.GetRealData(DiagIdx::w).size(); if (np == 0) return; - + field_name = name + Concatenate("w_", i_lab, 5) + "_" + std::to_string(MyProc); ofs.open(field_name.c_str(), std::ios::out|std::ios::binary); writeRealData(pdata.GetRealData(DiagIdx::w).data(), np, ofs); @@ -264,12 +781,12 @@ writeParticleData(const WarpXParticleContainer::DiagnosticParticleData& pdata, c field_name = name + Concatenate("uz_", i_lab, 5) + "_" + std::to_string(MyProc); ofs.open(field_name.c_str(), std::ios::out|std::ios::binary); writeRealData(pdata.GetRealData(DiagIdx::uz).data(), np, ofs); - ofs.close(); + ofs.close(); } void BoostedFrameDiagnostic:: -writeMetaData() +writeMetaData () { BL_PROFILE("BoostedFrameDiagnostic::writeMetaData"); @@ -301,17 +818,58 @@ writeMetaData() } BoostedFrameDiagnostic::LabSnapShot:: -LabSnapShot(Real t_lab_in, Real zmin_lab_in, - Real zmax_lab_in, int file_num_in) +LabSnapShot(Real t_lab_in, Real t_boost, Real zmin_lab_in, + Real zmax_lab_in, int file_num_in, const BoostedFrameDiagnostic& bfd) : t_lab(t_lab_in), zmin_lab(zmin_lab_in), zmax_lab(zmax_lab_in), - file_num(file_num_in) + file_num(file_num_in), + my_bfd(bfd) { current_z_lab = 0.0; current_z_boost = 0.0; + updateCurrentZPositions(t_boost, my_bfd.inv_gamma_boost_, my_bfd.inv_beta_boost_); + initial_i = (current_z_lab - zmin_lab) / my_bfd.dz_lab_; file_name = Concatenate("lab_frame_data/snapshot", file_num, 5); + +#ifdef WARPX_USE_HDF5 + if (ParallelDescriptor::IOProcessor()) + { + output_create(file_name); + } + + ParallelDescriptor::Barrier(); + if (ParallelDescriptor::IOProcessor()) + { + if (WarpX::do_boosted_frame_fields) + { + for (int comp = 0; comp < static_cast<int>(mesh_field_names.size()); ++comp) { + output_create_field(file_name, mesh_field_names[comp], + my_bfd.Nx_lab_, + my_bfd.Ny_lab_, + my_bfd.Nz_lab_+1); + } + } + } + + ParallelDescriptor::Barrier(); + + if (WarpX::do_boosted_frame_particles) + { + auto & mypc = WarpX::GetInstance().GetPartContainer(); + const std::vector<std::string> species_names = mypc.GetSpeciesNames(); + for (int j = 0; j < mypc.nSpecies(); ++j) + { + output_create_species_group(file_name, species_names[j]); + for (int k = 0; k < static_cast<int>(particle_field_names.size()); ++k) + { + std::string field_path = species_names[j] + "/" + particle_field_names[k]; + output_create_particle_field(file_name, field_path); + } + } + } +#else if (ParallelDescriptor::IOProcessor()) { if (!UtilCreateDirectory(file_name, 0755)) @@ -335,10 +893,10 @@ LabSnapShot(Real t_lab_in, Real zmin_lab_in, CreateDirectoryFailed(fullpath); } } - +#endif ParallelDescriptor::Barrier(); - if (ParallelDescriptor::IOProcessor()) writeSnapShotHeader(); + writeSnapShotHeader(); } void @@ -351,6 +909,7 @@ updateCurrentZPositions(Real t_boost, Real inv_gamma, Real inv_beta) { void BoostedFrameDiagnostic::LabSnapShot:: writeSnapShotHeader() { +#ifndef WARPX_USE_HDF5 if (ParallelDescriptor::IOProcessor()) { VisMF::IO_Buffer io_buffer(VisMF::IO_Buffer_Size); std::ofstream HeaderFile; @@ -368,4 +927,5 @@ writeSnapShotHeader() { HeaderFile << zmin_lab << "\n"; HeaderFile << zmax_lab << "\n"; } +#endif } diff --git a/Source/Diagnostics/ElectrostaticIO.cpp b/Source/Diagnostics/ElectrostaticIO.cpp index 8ed205b1a..a8dffac2b 100644 --- a/Source/Diagnostics/ElectrostaticIO.cpp +++ b/Source/Diagnostics/ElectrostaticIO.cpp @@ -113,7 +113,9 @@ WritePlotFileES (const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& rho, particle_varnames.push_back("By"); particle_varnames.push_back("Bz"); - mypc->Checkpoint(plotfilename, true, particle_varnames); + Vector<std::string> int_names; + + mypc->Checkpoint(plotfilename, particle_varnames, int_names); WriteJobInfo(plotfilename); diff --git a/Source/Diagnostics/FieldIO.H b/Source/Diagnostics/FieldIO.H new file mode 100644 index 000000000..1a3b45580 --- /dev/null +++ b/Source/Diagnostics/FieldIO.H @@ -0,0 +1,103 @@ +#ifndef WARPX_FielIO_H_ +#define WARPX_FielIO_H_ + +#include <WarpX.H> +#ifdef WARPX_USE_OPENPMD +#include <openPMD/openPMD.hpp> +#endif + +using namespace amrex; + +void +PackPlotDataPtrs (Vector<const MultiFab*>& pmf, + const std::array<std::unique_ptr<MultiFab>,3>& data); + +void +AverageAndPackVectorField( MultiFab& mf_avg, + const std::array< std::unique_ptr<MultiFab>, 3 >& vector_field, + const int dcomp, const int ngrow ); + +void +AverageAndPackScalarField( MultiFab& mf_avg, + const MultiFab & scalar_field, + const int dcomp, const int ngrow ); + +void +WriteRawField( const MultiFab& F, const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const std::string& field_name, + const int lev, const bool plot_guards ); + +void +WriteZeroRawField( const MultiFab& F, const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const std::string& field_name, + const int lev, const int ng ); + +void +WriteCoarseScalar( const std::string field_name, + const std::unique_ptr<MultiFab>& F_cp, + const std::unique_ptr<MultiFab>& F_fp, + const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const int lev, const bool plot_guards, + const int icomp=0 ); + +void +WriteCoarseVector( const std::string field_name, + const std::unique_ptr<MultiFab>& Fx_cp, + const std::unique_ptr<MultiFab>& Fy_cp, + const std::unique_ptr<MultiFab>& Fz_cp, + const std::unique_ptr<MultiFab>& Fx_fp, + const std::unique_ptr<MultiFab>& Fy_fp, + const std::unique_ptr<MultiFab>& Fz_fp, + const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const int lev, const bool plot_guards ); + +std::unique_ptr<MultiFab> +getInterpolatedScalar( + const MultiFab& F_cp, const MultiFab& F_fp, + const DistributionMapping& dm, const int r_ratio, + const Real* dx, const int ngrow ); + +std::array<std::unique_ptr<MultiFab>, 3> +getInterpolatedVector( + const std::unique_ptr<MultiFab>& Fx_cp, + const std::unique_ptr<MultiFab>& Fy_cp, + const std::unique_ptr<MultiFab>& Fz_cp, + const std::unique_ptr<MultiFab>& Fx_fp, + const std::unique_ptr<MultiFab>& Fy_fp, + const std::unique_ptr<MultiFab>& Fz_fp, + const DistributionMapping& dm, + const int r_ratio, const Real* dx, + const int ngrow ); + +void +coarsenCellCenteredFields( + Vector<MultiFab>& coarse_mf, Vector<Geometry>& coarse_geom, + const Vector<MultiFab>& source_mf, const Vector<Geometry>& source_geom, + int coarse_ratio, int finest_level ); + +#ifdef WARPX_USE_OPENPMD +void +setOpenPMDUnit( openPMD::Mesh mesh, const std::string field_name ); + +std::vector<std::uint64_t> +getReversedVec( const IntVect& v ); + +std::vector<double> +getReversedVec( const Real* v ); + +void +WriteOpenPMDFields( const std::string& filename, + const std::vector<std::string>& varnames, + const MultiFab& mf, const Geometry& geom, + const int iteration, const double time ); +#endif // WARPX_USE_OPENPMD + +#endif // WARPX_FielIO_H_ diff --git a/Source/Diagnostics/FieldIO.cpp b/Source/Diagnostics/FieldIO.cpp new file mode 100644 index 000000000..209d8e9b4 --- /dev/null +++ b/Source/Diagnostics/FieldIO.cpp @@ -0,0 +1,812 @@ + +#include <WarpX.H> +#include <FieldIO.H> +#ifdef WARPX_USE_OPENPMD +#include <openPMD/openPMD.hpp> +#endif + +#include <AMReX_FillPatchUtil_F.H> +#include <AMReX_Interpolater.H> + +using namespace amrex; + +#ifdef WARPX_USE_OPENPMD +/** \brief For a given field that is to be written to an openPMD file, + * set the metadata that indicates the physical unit. + */ +void +setOpenPMDUnit( openPMD::Mesh mesh, const std::string field_name ) +{ + if (field_name[0] == 'E'){ // Electric field + mesh.setUnitDimension({ + {openPMD::UnitDimension::L, 1}, + {openPMD::UnitDimension::M, 1}, + {openPMD::UnitDimension::T, -3}, + {openPMD::UnitDimension::I, -1}, + }); + } else if (field_name[0] == 'B'){ // Magnetic field + mesh.setUnitDimension({ + {openPMD::UnitDimension::M, 1}, + {openPMD::UnitDimension::I, -1}, + {openPMD::UnitDimension::T, -2} + }); + } else if (field_name[0] == 'j'){ // current + mesh.setUnitDimension({ + {openPMD::UnitDimension::L, -2}, + {openPMD::UnitDimension::I, 1}, + }); + } else if (field_name.substr(0,3) == "rho"){ // charge density + mesh.setUnitDimension({ + {openPMD::UnitDimension::L, -3}, + {openPMD::UnitDimension::I, 1}, + {openPMD::UnitDimension::T, 1}, + }); + } +} + + +/** \brief + * Convert an IntVect to a std::vector<std::uint64_t> + * and reverse the order of the elements + * (used for compatibility with the openPMD API) + */ +std::vector<std::uint64_t> +getReversedVec( const IntVect& v ) +{ + // Convert the IntVect v to and std::vector u + std::vector<std::uint64_t> u = { + AMREX_D_DECL( + static_cast<std::uint64_t>(v[0]), + static_cast<std::uint64_t>(v[1]), + static_cast<std::uint64_t>(v[2]) + ) + }; + // Reverse the order of elements, if v corresponds to the indices of a + // Fortran-order array (like an AMReX FArrayBox) + // but u is intended to be used with a C-order API (like openPMD) + std::reverse( u.begin(), u.end() ); + + return u; +} + +/** \brief + * Convert Real* pointer to a std::vector<double>, + * and reverse the order of the elements + * (used for compatibility with the openPMD API) + */ +std::vector<double> +getReversedVec( const Real* v ) +{ + // Convert Real* v to and std::vector u + std::vector<double> u = { + AMREX_D_DECL( + static_cast<double>(v[0]), + static_cast<double>(v[1]), + static_cast<double>(v[2]) + ) + }; + // Reverse the order of elements, if v corresponds to the indices of a + // Fortran-order array (like an AMReX FArrayBox) + // but u is intended to be used with a C-order API (like openPMD) + std::reverse( u.begin(), u.end() ); + + return u; +} + +/** \brief Write the `ncomp` components of `mf` (with names `varnames`) + * into a file `filename` in openPMD format. + **/ +void +WriteOpenPMDFields( const std::string& filename, + const std::vector<std::string>& varnames, + const MultiFab& mf, const Geometry& geom, + const int iteration, const double time ) +{ + BL_PROFILE("WriteOpenPMDFields()"); + + const int ncomp = mf.nComp(); + + // Create a few vectors that store info on the global domain + // Swap the indices for each of them, since AMReX data is Fortran order + // and since the openPMD API assumes contiguous C order + // - Size of the box, in integer number of cells + const Box& global_box = geom.Domain(); + auto global_size = getReversedVec(global_box.size()); + // - Grid spacing + std::vector<double> grid_spacing = getReversedVec(geom.CellSize()); + // - Global offset + std::vector<double> global_offset = getReversedVec(geom.ProbLo()); + // - AxisLabels +#if AMREX_SPACEDIM==3 + std::vector<std::string> axis_labels{"x", "y", "z"}; +#else + std::vector<std::string> axis_labels{"x", "z"}; +#endif + + // Prepare the type of dataset that will be written + openPMD::Datatype datatype = openPMD::determineDatatype<Real>(); + auto dataset = openPMD::Dataset(datatype, global_size); + + // Create new file and store the time/iteration info + auto series = openPMD::Series( filename, + openPMD::AccessType::CREATE, + MPI_COMM_WORLD ); + auto series_iteration = series.iterations[iteration]; + series_iteration.setTime( time ); + + // Loop through the different components, i.e. different fields stored in mf + for (int icomp=0; icomp<ncomp; icomp++){ + + // Check if this field is a vector or a scalar, and extract the field name + const std::string& varname = varnames[icomp]; + std::string field_name = varname; + std::string comp_name = openPMD::MeshRecordComponent::SCALAR; + bool is_vector = false; + for (const char* vector_field: {"E", "B", "j"}){ + for (const char* comp: {"x", "y", "z"}){ + if (varname[0] == *vector_field && varname[1] == *comp ){ + is_vector = true; + field_name = varname[0] + varname.substr(2); // Strip component + comp_name = varname[1]; + } + } + } + + // Setup the mesh accordingly + auto mesh = series_iteration.meshes[field_name]; + mesh.setDataOrder(openPMD::Mesh::DataOrder::F); // MultiFab: Fortran order + mesh.setAxisLabels( axis_labels ); + mesh.setGridSpacing( grid_spacing ); + mesh.setGridGlobalOffset( global_offset ); + setOpenPMDUnit( mesh, field_name ); + + // Create a new mesh record, and store the associated metadata + auto mesh_record = mesh[comp_name]; + mesh_record.resetDataset( dataset ); + // Cell-centered data: position is at 0.5 of a cell size. + mesh_record.setPosition(std::vector<double>{AMREX_D_DECL(0.5, 0.5, 0.5)}); + + // Loop through the multifab, and store each box as a chunk, + // in the openPMD file. + for (MFIter mfi(mf); mfi.isValid(); ++mfi) { + + const FArrayBox& fab = mf[mfi]; + const Box& local_box = fab.box(); + + // Determine the offset and size of this chunk + IntVect box_offset = local_box.smallEnd() - global_box.smallEnd(); + auto chunk_offset = getReversedVec(box_offset); + auto chunk_size = getReversedVec(local_box.size()); + + // Write local data + const double* local_data = fab.dataPtr(icomp); + mesh_record.storeChunk(openPMD::shareRaw(local_data), + chunk_offset, chunk_size); + } + } + // Flush data to disk after looping over all components + series.flush(); +} +#endif // WARPX_USE_OPENPMD + + +void +PackPlotDataPtrs (Vector<const MultiFab*>& pmf, + const std::array<std::unique_ptr<MultiFab>,3>& data) +{ + BL_ASSERT(pmf.size() == AMREX_SPACEDIM); +#if (AMREX_SPACEDIM == 3) + pmf[0] = data[0].get(); + pmf[1] = data[1].get(); + pmf[2] = data[2].get(); +#elif (AMREX_SPACEDIM == 2) + pmf[0] = data[0].get(); + pmf[1] = data[2].get(); +#endif +} + +/** \brief Takes an array of 3 MultiFab `vector_field` + * (representing the x, y, z components of a vector), + * averages it to the cell center, and stores the + * resulting MultiFab in mf_avg (in the components dcomp to dcomp+2) + */ +void +AverageAndPackVectorField( MultiFab& mf_avg, + const std::array< std::unique_ptr<MultiFab>, 3 >& vector_field, + const int dcomp, const int ngrow ) +{ + // The object below is temporary, and is needed because + // `average_edge_to_cellcenter` requires fields to be passed as Vector + Vector<const MultiFab*> srcmf(AMREX_SPACEDIM); + + // Check the type of staggering of the 3-component `vector_field` + // and average accordingly: + // - Fully cell-centered field (no average needed; simply copy) + if ( vector_field[0]->is_cell_centered() ){ + + MultiFab::Copy( mf_avg, *vector_field[0], 0, dcomp , 1, ngrow); + MultiFab::Copy( mf_avg, *vector_field[1], 0, dcomp+1, 1, ngrow); + MultiFab::Copy( mf_avg, *vector_field[2], 0, dcomp+2, 1, ngrow); + + // - Fully nodal + } else if ( vector_field[0]->is_nodal() ){ + + amrex::average_node_to_cellcenter( mf_avg, dcomp , + *vector_field[0], 0, 1, ngrow); + amrex::average_node_to_cellcenter( mf_avg, dcomp+1, + *vector_field[1], 0, 1, ngrow); + amrex::average_node_to_cellcenter( mf_avg, dcomp+2, + *vector_field[2], 0, 1, ngrow); + + // - Face centered, in the same way as B on a Yee grid + } else if ( vector_field[0]->is_nodal(0) ){ + + PackPlotDataPtrs(srcmf, vector_field); + amrex::average_face_to_cellcenter( mf_avg, dcomp, srcmf, ngrow); +#if (AMREX_SPACEDIM == 2) + MultiFab::Copy( mf_avg, mf_avg, dcomp+1, dcomp+2, 1, ngrow); + MultiFab::Copy( mf_avg, *vector_field[1], 0, dcomp+1, 1, ngrow); +#endif + + // - Edge centered, in the same way as E on a Yee grid + } else if ( !vector_field[0]->is_nodal(0) ){ + + PackPlotDataPtrs(srcmf, vector_field); + amrex::average_edge_to_cellcenter( mf_avg, dcomp, srcmf, ngrow); +#if (AMREX_SPACEDIM == 2) + MultiFab::Copy( mf_avg, mf_avg, dcomp+1, dcomp+2, 1, ngrow); + amrex::average_node_to_cellcenter( mf_avg, dcomp+1, + *vector_field[1], 0, 1, ngrow); +#endif + + } else { + amrex::Abort("Unknown staggering."); + } +} + +/** \brief Take a MultiFab `scalar_field` + * averages it to the cell center, and stores the + * resulting MultiFab in mf_avg (in the components dcomp) + */ +void +AverageAndPackScalarField( MultiFab& mf_avg, + const MultiFab & scalar_field, + const int dcomp, const int ngrow ) +{ + // Check the type of staggering of the 3-component `vector_field` + // and average accordingly: + // - Fully cell-centered field (no average needed; simply copy) + if ( scalar_field.is_cell_centered() ){ + + MultiFab::Copy( mf_avg, scalar_field, 0, dcomp, 1, ngrow); + + // - Fully nodal + } else if ( scalar_field.is_nodal() ){ + + amrex::average_node_to_cellcenter( mf_avg, dcomp, scalar_field, 0, 1, ngrow); + + } else { + amrex::Abort("Unknown staggering."); + } +} + +/** \brief Write the different fields that are meant for output, + * into the vector of MultiFab `mf_avg` (one MultiFab per level) + * after averaging them to the cell centers. + */ +void +WarpX::AverageAndPackFields ( Vector<std::string>& varnames, + amrex::Vector<MultiFab>& mf_avg, const int ngrow) const +{ + // Count how many different fields should be written (ncomp) + const int ncomp = 3*3 + + static_cast<int>(plot_part_per_cell) + + static_cast<int>(plot_part_per_grid) + + static_cast<int>(plot_part_per_proc) + + static_cast<int>(plot_proc_number) + + static_cast<int>(plot_divb) + + static_cast<int>(plot_dive) + + static_cast<int>(plot_rho) + + static_cast<int>(plot_F) + + static_cast<int>(plot_finepatch)*6 + + static_cast<int>(plot_crsepatch)*6 + + static_cast<int>(costs[0] != nullptr); + + // Loop over levels of refinement + for (int lev = 0; lev <= finest_level; ++lev) + { + // Allocate pointers to the `ncomp` fields that will be added + mf_avg.push_back( MultiFab(grids[lev], dmap[lev], ncomp, ngrow)); + + // Go through the different fields, pack them into mf_avg[lev], + // add the corresponding names to `varnames` and increment dcomp + int dcomp = 0; + AverageAndPackVectorField(mf_avg[lev], current_fp[lev], dcomp, ngrow); + if(lev==0) for(auto name:{"jx","jy","jz"}) varnames.push_back(name); + dcomp += 3; + AverageAndPackVectorField(mf_avg[lev], Efield_aux[lev], dcomp, ngrow); + if(lev==0) for(auto name:{"Ex","Ey","Ez"}) varnames.push_back(name); + dcomp += 3; + AverageAndPackVectorField(mf_avg[lev], Bfield_aux[lev], dcomp, ngrow); + if(lev==0) for(auto name:{"Bx","By","Bz"}) varnames.push_back(name); + dcomp += 3; + + if (plot_part_per_cell) + { + MultiFab temp_dat(grids[lev],mf_avg[lev].DistributionMap(),1,0); + temp_dat.setVal(0); + + // MultiFab containing number of particles in each cell + mypc->Increment(temp_dat, lev); + AverageAndPackScalarField( mf_avg[lev], temp_dat, dcomp, ngrow ); + if(lev==0) varnames.push_back("part_per_cell"); + dcomp += 1; + } + + if (plot_part_per_grid) + { + const Vector<long>& npart_in_grid = mypc->NumberOfParticlesInGrid(lev); + // MultiFab containing number of particles per grid + // (stored as constant for all cells in each grid) +#ifdef _OPENMP +#pragma omp parallel +#endif + for (MFIter mfi(mf_avg[lev]); mfi.isValid(); ++mfi) { + (mf_avg[lev])[mfi].setVal(static_cast<Real>(npart_in_grid[mfi.index()]), + dcomp); + } + if(lev==0) varnames.push_back("part_per_grid"); + dcomp += 1; + } + + if (plot_part_per_proc) + { + const Vector<long>& npart_in_grid = mypc->NumberOfParticlesInGrid(lev); + // MultiFab containing number of particles per process + // (stored as constant for all cells in each grid) + long n_per_proc = 0; +#ifdef _OPENMP +#pragma omp parallel reduction(+:n_per_proc) +#endif + for (MFIter mfi(mf_avg[lev]); mfi.isValid(); ++mfi) { + n_per_proc += npart_in_grid[mfi.index()]; + } + mf_avg[lev].setVal(static_cast<Real>(n_per_proc), dcomp,1); + if(lev==0) varnames.push_back("part_per_proc"); + dcomp += 1; + } + + if (plot_proc_number) + { + // MultiFab containing the Processor ID +#ifdef _OPENMP +#pragma omp parallel +#endif + for (MFIter mfi(mf_avg[lev]); mfi.isValid(); ++mfi) { + (mf_avg[lev])[mfi].setVal(static_cast<Real>(ParallelDescriptor::MyProc()), + dcomp); + } + if(lev==0) varnames.push_back("proc_number"); + dcomp += 1; + } + + if (plot_divb) + { + if (do_nodal) amrex::Abort("TODO: do_nodal && plot_divb"); + ComputeDivB(mf_avg[lev], dcomp, + {Bfield_aux[lev][0].get(), + Bfield_aux[lev][1].get(), + Bfield_aux[lev][2].get()}, + WarpX::CellSize(lev) + ); + if(lev == 0) varnames.push_back("divB"); + dcomp += 1; + } + + if (plot_dive) + { + if (do_nodal) amrex::Abort("TODO: do_nodal && plot_dive"); + const BoxArray& ba = amrex::convert(boxArray(lev),IntVect::TheUnitVector()); + MultiFab dive(ba,DistributionMap(lev),1,0); + ComputeDivE( dive, 0, + {Efield_aux[lev][0].get(), + Efield_aux[lev][1].get(), + Efield_aux[lev][2].get()}, + WarpX::CellSize(lev) + ); + AverageAndPackScalarField( mf_avg[lev], dive, dcomp, ngrow ); + if(lev == 0) varnames.push_back("divE"); + dcomp += 1; + } + + if (plot_rho) + { + AverageAndPackScalarField( mf_avg[lev], *rho_fp[lev], dcomp, ngrow ); + if(lev == 0) varnames.push_back("rho"); + dcomp += 1; + } + + if (plot_F) + { + AverageAndPackScalarField( mf_avg[lev], *F_fp[lev], dcomp, ngrow); + if(lev == 0) varnames.push_back("F"); + dcomp += 1; + } + + if (plot_finepatch) + { + AverageAndPackVectorField( mf_avg[lev], Efield_fp[lev], dcomp, ngrow ); + if(lev==0) for(auto name:{"Ex_fp","Ey_fp","Ez_fp"}) varnames.push_back(name); + dcomp += 3; + AverageAndPackVectorField( mf_avg[lev], Bfield_fp[lev], dcomp, ngrow ); + if(lev==0) for(auto name:{"Bx_fp","By_fp","Bz_fp"}) varnames.push_back(name); + dcomp += 3; + } + + if (plot_crsepatch) + { + if (lev == 0) + { + mf_avg[lev].setVal(0.0, dcomp, 3, ngrow); + } + else + { + if (do_nodal) amrex::Abort("TODO: do_nodal && plot_crsepatch"); + std::array<std::unique_ptr<MultiFab>, 3> E = getInterpolatedE(lev); + AverageAndPackVectorField( mf_avg[lev], E, dcomp, ngrow ); + + } + if(lev==0) for(auto name:{"Ex_cp","Ey_cp","Ez_cp"}) varnames.push_back(name); + dcomp += 3; + + // now the magnetic field + if (lev == 0) + { + mf_avg[lev].setVal(0.0, dcomp, 3, ngrow); + } + else + { + if (do_nodal) amrex::Abort("TODO: do_nodal && plot_crsepatch"); + std::array<std::unique_ptr<MultiFab>, 3> B = getInterpolatedB(lev); + AverageAndPackVectorField( mf_avg[lev], B, dcomp, ngrow ); + } + if(lev==0) for(auto name:{"Bx_cp","By_cp","Bz_cp"}) varnames.push_back(name); + dcomp += 3; + } + + if (costs[0] != nullptr) + { + AverageAndPackScalarField( mf_avg[lev], *costs[lev], dcomp, ngrow ); + if(lev==0) varnames.push_back("costs"); + dcomp += 1; + } + + BL_ASSERT(dcomp == ncomp); + + } // end loop over levels of refinement + +}; + +/** \brief Reduce the size of all the fields in `source_mf` + * by `coarse_ratio` and store the results in `coarse_mf`. + * Calculate the corresponding coarse Geometry from `source_geom` + * and store the results in `coarse_geom` */ +void +coarsenCellCenteredFields( + Vector<MultiFab>& coarse_mf, Vector<Geometry>& coarse_geom, + const Vector<MultiFab>& source_mf, const Vector<Geometry>& source_geom, + int coarse_ratio, int finest_level ) +{ + // Check that the Vectors to be filled have an initial size of 0 + AMREX_ALWAYS_ASSERT( coarse_mf.size()==0 ); + AMREX_ALWAYS_ASSERT( coarse_geom.size()==0 ); + + // Fill the vectors with one element per level + int ncomp = source_mf[0].nComp(); + for (int lev=0; lev<=finest_level; lev++) { + AMREX_ALWAYS_ASSERT( source_mf[lev].is_cell_centered() ); + + coarse_geom.push_back(amrex::coarsen( source_geom[lev], IntVect(coarse_ratio))); + + BoxArray small_ba = amrex::coarsen(source_mf[lev].boxArray(), coarse_ratio); + coarse_mf.push_back( MultiFab(small_ba, source_mf[lev].DistributionMap(), ncomp, 0) ); + average_down(source_mf[lev], coarse_mf[lev], 0, ncomp, IntVect(coarse_ratio)); + } +}; + + +/** \brief Write the data from MultiFab `F` into the file `filename` + * as a raw field (i.e. no interpolation to cell centers). + * Write guard cells if `plot_guards` is True. + */ +void +WriteRawField( const MultiFab& F, const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const std::string& field_name, + const int lev, const bool plot_guards ) +{ + std::string prefix = amrex::MultiFabFileFullPrefix(lev, + filename, level_prefix, field_name); + + if (plot_guards) { + // Dump original MultiFab F + VisMF::Write(F, prefix); + } else { + // Copy original MultiFab into one that does not have guard cells + MultiFab tmpF( F.boxArray(), dm, 1, 0); + MultiFab::Copy(tmpF, F, 0, 0, 1, 0); + VisMF::Write(tmpF, prefix); + } + +} + +/** \brief Write a multifab of the same shape as `F` but filled with 0. + * (The shape includes guard cells if `plot_guards` is True.) + * This is mainly needed because the yt reader requires all levels of the + * coarse/fine patch to be written, but WarpX does not have data for + * the coarse patch of level 0 (meaningless). + */ +void +WriteZeroRawField( const MultiFab& F, const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const std::string& field_name, + const int lev, const int ng ) +{ + std::string prefix = amrex::MultiFabFileFullPrefix(lev, + filename, level_prefix, field_name); + + MultiFab tmpF(F.boxArray(), dm, 1, ng); + tmpF.setVal(0.); + VisMF::Write(tmpF, prefix); +} + +/** \brief Write the coarse scalar multifab `F_cp` to the file `filename` + * *after* sampling/interpolating its value on the fine grid corresponding + * to `F_fp`. This is mainly needed because the yt reader requires the + * coarse and fine patch to have the same shape. + */ +void +WriteCoarseScalar( const std::string field_name, + const std::unique_ptr<MultiFab>& F_cp, + const std::unique_ptr<MultiFab>& F_fp, + const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const int lev, const bool plot_guards, + const int icomp ) +{ + int ng = 0; + if (plot_guards) ng = F_fp->nGrow(); + + if (lev == 0) { + // No coarse field for level 0: instead write a MultiFab + // filled with 0, with the same number of cells as the _fp field + WriteZeroRawField( *F_fp, dm, filename, level_prefix, field_name+"_cp", lev, ng ); + } else { + // Create an alias to the component `icomp` of F_cp + MultiFab F_comp(*F_cp, amrex::make_alias, icomp, 1); + // Interpolate coarse data onto fine grid + const int r_ratio = WarpX::GetInstance().refRatio(lev-1)[0]; + const Real* dx = WarpX::GetInstance().Geom(lev-1).CellSize(); + auto F = getInterpolatedScalar( F_comp, *F_fp, dm, r_ratio, dx, ng ); + // Write interpolated raw data + WriteRawField( *F, dm, filename, level_prefix, field_name+"_cp", lev, plot_guards ); + } +} + +/** \brief Write the coarse vector multifab `F*_cp` to the file `filename` + * *after* sampling/interpolating its value on the fine grid corresponding + * to `F*_fp`. This is mainly needed because the yt reader requires the + * coarse and fine patch to have the same shape. + */ +void +WriteCoarseVector( const std::string field_name, + const std::unique_ptr<MultiFab>& Fx_cp, + const std::unique_ptr<MultiFab>& Fy_cp, + const std::unique_ptr<MultiFab>& Fz_cp, + const std::unique_ptr<MultiFab>& Fx_fp, + const std::unique_ptr<MultiFab>& Fy_fp, + const std::unique_ptr<MultiFab>& Fz_fp, + const DistributionMapping& dm, + const std::string& filename, + const std::string& level_prefix, + const int lev, const bool plot_guards ) +{ + int ng = 0; + if (plot_guards) ng = Fx_fp->nGrow(); + + if (lev == 0) { + // No coarse field for level 0: instead write a MultiFab + // filled with 0, with the same number of cells as the _fp field + WriteZeroRawField( *Fx_fp, dm, filename, level_prefix, field_name+"x_cp", lev, ng ); + WriteZeroRawField( *Fy_fp, dm, filename, level_prefix, field_name+"y_cp", lev, ng ); + WriteZeroRawField( *Fz_fp, dm, filename, level_prefix, field_name+"z_cp", lev, ng ); + } else { + // Interpolate coarse data onto fine grid + const int r_ratio = WarpX::GetInstance().refRatio(lev-1)[0]; + const Real* dx = WarpX::GetInstance().Geom(lev-1).CellSize(); + auto F = getInterpolatedVector( Fx_cp, Fy_cp, Fz_cp, Fx_fp, Fy_fp, Fz_fp, + dm, r_ratio, dx, ng ); + // Write interpolated raw data + WriteRawField( *F[0], dm, filename, level_prefix, field_name+"x_cp", lev, plot_guards ); + WriteRawField( *F[1], dm, filename, level_prefix, field_name+"y_cp", lev, plot_guards ); + WriteRawField( *F[2], dm, filename, level_prefix, field_name+"z_cp", lev, plot_guards ); + } +} + +/** \brief Samples/Interpolates the coarse scalar multifab `F_cp` on the + * fine grid associated with the fine multifab `F_fp`. + */ +std::unique_ptr<MultiFab> +getInterpolatedScalar( + const MultiFab& F_cp, const MultiFab& F_fp, + const DistributionMapping& dm, const int r_ratio, + const Real* dx, const int ngrow ) +{ + // Prepare the structure that will contain the returned fields + std::unique_ptr<MultiFab> interpolated_F; + interpolated_F.reset( new MultiFab(F_fp.boxArray(), dm, 1, ngrow) ); + interpolated_F->setVal(0.); + + // Loop through the boxes and interpolate the values from the _cp data + const int use_limiter = 0; +#ifdef _OPEMP +#pragma omp parallel +#endif + { + FArrayBox ffab; // Temporary array ; contains interpolated fields + for (MFIter mfi(*interpolated_F); mfi.isValid(); ++mfi) + { + Box finebx = mfi.fabbox(); + finebx.coarsen(r_ratio).refine(r_ratio); // so that finebx is coarsenable + + const FArrayBox& cfab = (F_cp)[mfi]; + ffab.resize(finebx); + + // - Fully nodal + if ( F_fp.is_nodal() ){ + IntVect refinement_vector{AMREX_D_DECL(r_ratio, r_ratio, r_ratio)}; + node_bilinear_interp.interp(cfab, 0, ffab, 0, 1, + finebx, refinement_vector, {}, {}, {}, 0, 0); + } else { + amrex::Abort("Unknown field staggering."); + } + + // Add temporary array to the returned structure + const Box& bx = (*interpolated_F)[mfi].box(); + (*interpolated_F)[mfi].plus(ffab, bx, bx, 0, 0, 1); + } + } + return interpolated_F; +} + +/** \brief Samples/Interpolates the coarse vector multifab `F*_cp` on the + * fine grid associated with the fine multifab `F*_fp`. + */ +std::array<std::unique_ptr<MultiFab>, 3> +getInterpolatedVector( + const std::unique_ptr<MultiFab>& Fx_cp, + const std::unique_ptr<MultiFab>& Fy_cp, + const std::unique_ptr<MultiFab>& Fz_cp, + const std::unique_ptr<MultiFab>& Fx_fp, + const std::unique_ptr<MultiFab>& Fy_fp, + const std::unique_ptr<MultiFab>& Fz_fp, + const DistributionMapping& dm, const int r_ratio, + const Real* dx, const int ngrow ) +{ + + // Prepare the structure that will contain the returned fields + std::array<std::unique_ptr<MultiFab>, 3> interpolated_F; + interpolated_F[0].reset( new MultiFab(Fx_fp->boxArray(), dm, 1, ngrow) ); + interpolated_F[1].reset( new MultiFab(Fy_fp->boxArray(), dm, 1, ngrow) ); + interpolated_F[2].reset( new MultiFab(Fz_fp->boxArray(), dm, 1, ngrow) ); + for (int i=0; i<3; i++) interpolated_F[i]->setVal(0.); + + // Loop through the boxes and interpolate the values from the _cp data + const int use_limiter = 0; +#ifdef _OPEMP +#pragma omp parallel +#endif + { + std::array<FArrayBox,3> ffab; // Temporary array ; contains interpolated fields + for (MFIter mfi(*interpolated_F[0]); mfi.isValid(); ++mfi) + { + Box ccbx = mfi.fabbox(); + ccbx.enclosedCells(); + ccbx.coarsen(r_ratio).refine(r_ratio); // so that ccbx is coarsenable + + const FArrayBox& cxfab = (*Fx_cp)[mfi]; + const FArrayBox& cyfab = (*Fy_cp)[mfi]; + const FArrayBox& czfab = (*Fz_cp)[mfi]; + ffab[0].resize(amrex::convert(ccbx,(*Fx_fp)[mfi].box().type())); + ffab[1].resize(amrex::convert(ccbx,(*Fy_fp)[mfi].box().type())); + ffab[2].resize(amrex::convert(ccbx,(*Fz_fp)[mfi].box().type())); + + // - Face centered, in the same way as B on a Yee grid + if ( (*Fx_fp)[mfi].box().type() == IntVect{AMREX_D_DECL(1,0,0)} ){ +#if (AMREX_SPACEDIM == 3) + amrex_interp_div_free_bfield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[0]), + BL_TO_FORTRAN_ANYD(ffab[1]), + BL_TO_FORTRAN_ANYD(ffab[2]), + BL_TO_FORTRAN_ANYD(cxfab), + BL_TO_FORTRAN_ANYD(cyfab), + BL_TO_FORTRAN_ANYD(czfab), + dx, &r_ratio, &use_limiter); +#else + amrex_interp_div_free_bfield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[0]), + BL_TO_FORTRAN_ANYD(ffab[2]), + BL_TO_FORTRAN_ANYD(cxfab), + BL_TO_FORTRAN_ANYD(czfab), + dx, &r_ratio, &use_limiter); + amrex_interp_cc_bfield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[1]), + BL_TO_FORTRAN_ANYD(cyfab), + &r_ratio, &use_limiter); +#endif + // - Edge centered, in the same way as E on a Yee grid + } else if ( (*Fx_fp)[mfi].box().type() == IntVect{AMREX_D_DECL(0,1,1)} ){ +#if (AMREX_SPACEDIM == 3) + amrex_interp_efield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[0]), + BL_TO_FORTRAN_ANYD(ffab[1]), + BL_TO_FORTRAN_ANYD(ffab[2]), + BL_TO_FORTRAN_ANYD(cxfab), + BL_TO_FORTRAN_ANYD(cyfab), + BL_TO_FORTRAN_ANYD(czfab), + &r_ratio, &use_limiter); +#else + amrex_interp_efield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[0]), + BL_TO_FORTRAN_ANYD(ffab[2]), + BL_TO_FORTRAN_ANYD(cxfab), + BL_TO_FORTRAN_ANYD(czfab), + &r_ratio,&use_limiter); + amrex_interp_nd_efield(ccbx.loVect(), ccbx.hiVect(), + BL_TO_FORTRAN_ANYD(ffab[1]), + BL_TO_FORTRAN_ANYD(cyfab), + &r_ratio); +#endif + } else { + amrex::Abort("Unknown field staggering."); + } + + // Add temporary array to the returned structure + for (int i = 0; i < 3; ++i) { + const Box& bx = (*interpolated_F[i])[mfi].box(); + (*interpolated_F[i])[mfi].plus(ffab[i], bx, bx, 0, 0, 1); + } + } + } + return interpolated_F; +} + +std::array<std::unique_ptr<MultiFab>, 3> WarpX::getInterpolatedE(int lev) const +{ + + const int ngrow = 0; + const DistributionMapping& dm = DistributionMap(lev); + const Real* dx = Geom(lev-1).CellSize(); + const int r_ratio = refRatio(lev-1)[0]; + + return getInterpolatedVector( + Efield_cp[lev][0], Efield_cp[lev][1], Efield_cp[lev][2], + Efield_fp[lev][0], Efield_fp[lev][1], Efield_fp[lev][2], + dm, r_ratio, dx, ngrow ); +} + +std::array<std::unique_ptr<MultiFab>, 3> WarpX::getInterpolatedB(int lev) const +{ + const int ngrow = 0; + const DistributionMapping& dm = DistributionMap(lev); + const Real* dx = Geom(lev-1).CellSize(); + const int r_ratio = refRatio(lev-1)[0]; + + return getInterpolatedVector( + Bfield_cp[lev][0], Bfield_cp[lev][1], Bfield_cp[lev][2], + Bfield_fp[lev][0], Bfield_fp[lev][1], Bfield_fp[lev][2], + dm, r_ratio, dx, ngrow ); +} diff --git a/Source/Diagnostics/Make.package b/Source/Diagnostics/Make.package index b569666ff..1c602ee33 100644 --- a/Source/Diagnostics/Make.package +++ b/Source/Diagnostics/Make.package @@ -1,6 +1,8 @@ CEXE_sources += WarpXIO.cpp CEXE_sources += BoostedFrameDiagnostic.cpp CEXE_sources += ParticleIO.cpp +CEXE_sources += FieldIO.cpp +CEXE_headers += FieldIO.H CEXE_headers += BoostedFrameDiagnostic.H CEXE_headers += ElectrostaticIO.cpp F90EXE_sources += BoostedFrame_module.F90 diff --git a/Source/Diagnostics/ParticleIO.cpp b/Source/Diagnostics/ParticleIO.cpp index be9809bac..f15c084a0 100644 --- a/Source/Diagnostics/ParticleIO.cpp +++ b/Source/Diagnostics/ParticleIO.cpp @@ -19,12 +19,25 @@ WarpXParticleContainer::WriteHeader (std::ostream& os) const } void -MultiParticleContainer::Checkpoint (const std::string& dir, - bool is_checkpoint, - const Vector<std::string>& varnames) const +MultiParticleContainer::Checkpoint (const std::string& dir) const { for (unsigned i = 0, n = species_names.size(); i < n; ++i) { - allcontainers[i]->Checkpoint(dir, species_names[i], is_checkpoint, varnames); + allcontainers[i]->Checkpoint(dir, species_names[i]); + } +} + +void +MultiParticleContainer::WritePlotFile (const std::string& dir, + const Vector<int>& real_flags, + const Vector<std::string>& real_names) const +{ + Vector<std::string> int_names; + Vector<int> int_flags; + + for (unsigned i = 0, n = species_names.size(); i < n; ++i) { + allcontainers[i]->WritePlotFile(dir, species_names[i], + real_flags, int_flags, + real_names, int_names); } } diff --git a/Source/Diagnostics/WarpXIO.cpp b/Source/Diagnostics/WarpXIO.cpp index d43d74968..ccbdd280c 100644 --- a/Source/Diagnostics/WarpXIO.cpp +++ b/Source/Diagnostics/WarpXIO.cpp @@ -1,9 +1,9 @@ - #include <AMReX_MultiFabUtil.H> #include <AMReX_PlotFileUtil.H> #include <AMReX_FillPatchUtil_F.H> #include <WarpX.H> +#include <FieldIO.H> #include "AMReX_buildInfo.H" @@ -11,6 +11,11 @@ #include <AMReX_AmrMeshInSituBridge.H> #endif +#ifdef AMREX_USE_ASCENT +#include <ascent.hpp> +#include <AMReX_Conduit_Blueprint.H> +#endif + using namespace amrex; namespace @@ -175,12 +180,11 @@ WarpX::WriteCheckPointFile() const } } - mypc->Checkpoint(checkpointname, true); + mypc->Checkpoint(checkpointname); VisMF::SetHeaderVersion(current_version); } - void WarpX::InitFromCheckpoint () { @@ -366,7 +370,7 @@ WarpX::InitFromCheckpoint () } if (costs[lev]) { - const auto& cost_mf_name = + const auto& cost_mf_name = amrex::MultiFabFileFullPrefix(lev, restart_chkfile, level_prefix, "costs"); if (VisMF::Exist(cost_mf_name)) { VisMF::Read(*costs[lev], cost_mf_name); @@ -408,335 +412,52 @@ WarpX::GetCellCenteredData() { const int nc = 10; Vector<std::unique_ptr<MultiFab> > cc(finest_level+1); - + for (int lev = 0; lev <= finest_level; ++lev) { cc[lev].reset( new MultiFab(grids[lev], dmap[lev], nc, ng) ); - - Vector<const MultiFab*> srcmf(AMREX_SPACEDIM); + int dcomp = 0; - // first the electric field - PackPlotDataPtrs(srcmf, Efield_aux[lev]); - amrex::average_edge_to_cellcenter(*cc[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*cc[lev], *cc[lev], dcomp+1, dcomp+2, 1, ng); - amrex::average_node_to_cellcenter(*cc[lev], dcomp+1, *Efield_aux[lev][1], 0, 1); -#endif + AverageAndPackVectorField( *cc[lev], Efield_aux[lev], dcomp, ng ); dcomp += 3; - // then the magnetic field - PackPlotDataPtrs(srcmf, Bfield_aux[lev]); - amrex::average_face_to_cellcenter(*cc[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*cc[lev], *cc[lev], dcomp+1, dcomp+2, 1, ng); - MultiFab::Copy(*cc[lev], *Bfield_aux[lev][1], 0, dcomp+1, 1, ng); -#endif + AverageAndPackVectorField( *cc[lev], Efield_aux[lev], dcomp, ng ); dcomp += 3; - // then the current density - PackPlotDataPtrs(srcmf, current_fp[lev]); - amrex::average_edge_to_cellcenter(*cc[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*cc[lev], *cc[lev], dcomp+1, dcomp+2, 1, ng); - amrex::average_node_to_cellcenter(*cc[lev], dcomp+1, *current_fp[lev][1], 0, 1); -#endif + AverageAndPackVectorField( *cc[lev], current_fp[lev], dcomp, ng ); dcomp += 3; - const std::unique_ptr<MultiFab>& charge_density = mypc->GetChargeDensity(lev); - amrex::average_node_to_cellcenter(*cc[lev], dcomp, *charge_density, 0, 1); - + AverageAndPackScalarField( *cc[lev], *charge_density, dcomp, ng ); + cc[lev]->FillBoundary(geom[lev].periodicity()); } - + for (int lev = finest_level; lev > 0; --lev) { amrex::average_down(*cc[lev], *cc[lev-1], 0, nc, refRatio(lev-1)); } - + return std::move(cc[0]); } void WarpX::UpdateInSitu () const { -#ifdef BL_USE_SENSEI_INSITU +#if defined(BL_USE_SENSEI_INSITU) || defined(AMREX_USE_ASCENT) BL_PROFILE("WarpX::UpdateInSitu()"); - int numLevels = finest_level + 1; - Vector<std::string> varnames; - Vector<MultiFab> mf; - - const int ncomp = 3*3 - + static_cast<int>(plot_part_per_cell) - + static_cast<int>(plot_part_per_grid) - + static_cast<int>(plot_part_per_proc) - + static_cast<int>(plot_proc_number) - + static_cast<int>(plot_divb) - + static_cast<int>(plot_dive) - + static_cast<int>(plot_rho) - + static_cast<int>(plot_F) - + static_cast<int>(plot_finepatch)*6 - + static_cast<int>(plot_crsepatch)*6 - + static_cast<int>(costs[0] != nullptr); - - for (int lev = 0; lev <= finest_level; ++lev) - { - const int ngrow = 1; - mf.push_back(MultiFab(grids[lev], dmap[lev], ncomp, ngrow)); - - Vector<const MultiFab*> srcmf(AMREX_SPACEDIM); - PackPlotDataPtrs(srcmf, current_fp[lev]); - int dcomp = 0; - amrex::average_edge_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); - -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(mf[lev], dcomp+1, *current_fp[lev][1], 0, 1, ngrow); -#endif - if (lev == 0) - { - varnames.push_back("jx"); - varnames.push_back("jy"); - varnames.push_back("jz"); - } - dcomp += 3; - - PackPlotDataPtrs(srcmf, Efield_aux[lev]); - amrex::average_edge_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(mf[lev], dcomp+1, *Efield_aux[lev][1], 0, 1, ngrow); -#endif - if (lev == 0) - { - varnames.push_back("Ex"); - varnames.push_back("Ey"); - varnames.push_back("Ez"); - } - dcomp += 3; - - PackPlotDataPtrs(srcmf, Bfield_aux[lev]); - amrex::average_face_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(mf[lev], *Bfield_aux[lev][1], 0, dcomp+1, 1, ngrow); -#endif - if (lev == 0) - { - varnames.push_back("Bx"); - varnames.push_back("By"); - varnames.push_back("Bz"); - } - dcomp += 3; - - if (plot_part_per_cell) - { - MultiFab temp_dat(grids[lev], mf[lev].DistributionMap(), 1, ngrow); - temp_dat.setVal(0, ngrow); - - // MultiFab containing number of particles in each cell - mypc->Increment(temp_dat, lev); - MultiFab::Copy(mf[lev], temp_dat, 0, dcomp, 1, ngrow); - if (lev == 0) - varnames.push_back("part_per_cell"); - dcomp += 1; - } - - if (plot_part_per_grid || plot_part_per_proc) - { - const Vector<long>& npart_in_grid = mypc->NumberOfParticlesInGrid(lev); - - if (plot_part_per_grid) - { -#ifdef _OPENMP -#pragma omp parallel -#endif - for (MFIter mfi(mf[lev]); mfi.isValid(); ++mfi) - (mf[lev])[mfi].setVal(static_cast<Real>(npart_in_grid[mfi.index()]), dcomp); - - if (lev == 0) - varnames.push_back("part_per_grid"); - - dcomp += 1; - } - - if (plot_part_per_proc) - { - long n_per_proc = 0; -#ifdef _OPENMP -#pragma omp parallel reduction(+:n_per_proc) -#endif - for (MFIter mfi(mf[lev]); mfi.isValid(); ++mfi) - n_per_proc += npart_in_grid[mfi.index()]; - - mf[lev].setVal(static_cast<Real>(n_per_proc), dcomp, ngrow); - - if (lev == 0) - varnames.push_back("part_per_proc"); - - dcomp += 1; - } - } - - if (plot_proc_number) - { - Real procid = static_cast<Real>(ParallelDescriptor::MyProc()); -#ifdef _OPENMP -#pragma omp parallel -#endif - for (MFIter mfi(mf[lev]); mfi.isValid(); ++mfi) - (mf[lev])[mfi].setVal(procid, dcomp); - - if (lev == 0) - varnames.push_back("proc_number"); - - dcomp += 1; - } - - if (plot_divb) - { - ComputeDivB(mf[lev], dcomp, - {Bfield_aux[lev][0].get(),Bfield_aux[lev][1].get(),Bfield_aux[lev][2].get()}, - WarpX::CellSize(lev), ngrow); - if (lev == 0) - varnames.push_back("divB"); - - dcomp += 1; - } - - if (plot_dive) - { - const BoxArray& ba = amrex::convert(boxArray(lev),IntVect::TheUnitVector()); - MultiFab dive(ba, DistributionMap(lev), 1, ngrow); - - ComputeDivE(dive, 0, - {Efield_aux[lev][0].get(), Efield_aux[lev][1].get(), Efield_aux[lev][2].get()}, - WarpX::CellSize(lev), ngrow); - - amrex::average_node_to_cellcenter(mf[lev], dcomp, dive, 0, 1, ngrow); - - if (lev == 0) - varnames.push_back("divE"); - - dcomp += 1; - } - - if (plot_rho) - { - amrex::average_node_to_cellcenter(mf[lev], dcomp, *rho_fp[lev], 0, 1, ngrow); - if (lev == 0) - varnames.push_back("rho"); - - dcomp += 1; - } - - if (plot_F) - { - amrex::average_node_to_cellcenter(mf[lev], dcomp, *F_fp[lev], 0, 1, ngrow); - - if (lev == 0) - varnames.push_back("F"); - - dcomp += 1; - } - - if (plot_finepatch) - { - PackPlotDataPtrs(srcmf, Efield_fp[lev]); - amrex::average_edge_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(mf[lev], dcomp+1, *Efield_fp[lev][1], 0, 1, ngrow); -#endif - if (lev == 0) - { - varnames.push_back("Ex_fp"); - varnames.push_back("Ey_fp"); - varnames.push_back("Ez_fp"); - } - dcomp += 3; - - PackPlotDataPtrs(srcmf, Bfield_fp[lev]); - amrex::average_face_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(mf[lev], *Bfield_fp[lev][1], 0, dcomp+1, 1, ngrow); -#endif - if (lev == 0) - { - varnames.push_back("Bx_fp"); - varnames.push_back("By_fp"); - varnames.push_back("Bz_fp"); - } - dcomp += 3; - } - - if (plot_crsepatch) - { - // First the electric field - if (lev == 0) - { - mf[lev].setVal(0.0, dcomp, 3, ngrow); - } - else - { - std::array<std::unique_ptr<MultiFab>, 3> E = getInterpolatedE(lev); - PackPlotDataPtrs(srcmf, E); - amrex::average_edge_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(mf[lev], dcomp+1, *E[1], 0, 1, ngrow); -#endif - } - if (lev == 0) - { - varnames.push_back("Ex_cp"); - varnames.push_back("Ey_cp"); - varnames.push_back("Ez_cp"); - } - dcomp += 3; - - // now the magnetic field - if (lev == 0) - { - mf[lev].setVal(0.0, dcomp, 3, ngrow); - } - else - { - std::array<std::unique_ptr<MultiFab>, 3> B = getInterpolatedB(lev); - PackPlotDataPtrs(srcmf, B); - amrex::average_face_to_cellcenter(mf[lev], dcomp, srcmf, ngrow); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(mf[lev], mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(mf[lev], *B[1], 0, dcomp+1, 1, ngrow); -#endif - } - if (lev == 0) - { - varnames.push_back("Bx_cp"); - varnames.push_back("By_cp"); - varnames.push_back("Bz_cp"); - } - dcomp += 3; - } - - if (costs[0] != nullptr) - { - MultiFab::Copy(mf[lev], *costs[lev], 0, dcomp, 1, ngrow); - if (lev == 0) - { - varnames.push_back("costs"); - } - dcomp += 1; - } - - BL_ASSERT(dcomp == ncomp); - } + // Average the fields from the simulation to the cell centers + const int ngrow = 1; + Vector<std::string> varnames; // Name of the written fields + // mf_avg will contain the averaged, cell-centered fields + Vector<MultiFab> mf_avg; + WarpX::AverageAndPackFields( varnames, mf_avg, ngrow ); +#ifdef BL_USE_SENSEI_INSITU if (insitu_bridge->update(istep[0], t_new[0], dynamic_cast<amrex::AmrMesh*>(const_cast<WarpX*>(this)), - {&mf}, {varnames})) + {&mf_avg}, {varnames})) { amrex::ErrorStream() << "WarpXIO::UpdateInSitu : Failed to update the in situ bridge." @@ -745,6 +466,30 @@ WarpX::UpdateInSitu () const amrex::Abort(); } #endif + +#ifdef AMREX_USE_ASCENT + conduit::Node bp_mesh; + MultiLevelToBlueprint(finest_level+1, + amrex::GetVecOfConstPtrs(mf_avg), + varnames, + Geom(), + t_new[0], + istep, + refRatio(), + bp_mesh); + + ascent::Ascent ascent; + conduit::Node opts; + opts["exceptions"] = "catch"; + opts["mpi_comm"] = MPI_Comm_c2f(ParallelDescriptor::Communicator()); + ascent.open(opts); + ascent.publish(bp_mesh); + conduit::Node actions; + ascent.execute(actions); + ascent.close(); +#endif + +#endif } void @@ -752,489 +497,118 @@ WarpX::WritePlotFile () const { BL_PROFILE("WarpX::WritePlotFile()"); - VisMF::Header::Version current_version = VisMF::GetHeaderVersion(); - VisMF::SetHeaderVersion(plotfile_headerversion); - const std::string& plotfilename = amrex::Concatenate(plot_file,istep[0]); - amrex::Print() << " Writing plotfile " << plotfilename << "\n"; - { - Vector<std::string> varnames; - Vector<std::unique_ptr<MultiFab> > mf(finest_level+1); - - const int ncomp = 3*3 - + static_cast<int>(plot_part_per_cell) - + static_cast<int>(plot_part_per_grid) - + static_cast<int>(plot_part_per_proc) - + static_cast<int>(plot_proc_number) - + static_cast<int>(plot_divb) - + static_cast<int>(plot_dive) - + static_cast<int>(plot_rho) - + static_cast<int>(plot_F) - + static_cast<int>(plot_finepatch)*6 - + static_cast<int>(plot_crsepatch)*6 - + static_cast<int>(costs[0] != nullptr); - - for (int lev = 0; lev <= finest_level; ++lev) - { - const int ngrow = 0; - mf[lev].reset(new MultiFab(grids[lev], dmap[lev], ncomp, ngrow)); - - Vector<const MultiFab*> srcmf(AMREX_SPACEDIM); - int dcomp = 0; - - // j - if (do_nodal) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp , *current_fp[lev][0], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *current_fp[lev][1], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+2, *current_fp[lev][2], 0, 1); - } - else - { - PackPlotDataPtrs(srcmf, current_fp[lev]); - amrex::average_edge_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *current_fp[lev][1], 0, 1); -#endif - } - if (lev == 0) - { - varnames.push_back("jx"); - varnames.push_back("jy"); - varnames.push_back("jz"); - } - dcomp += 3; - - // E - if (do_nodal) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp , *Efield_aux[lev][0], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Efield_aux[lev][1], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+2, *Efield_aux[lev][2], 0, 1); - } - else - { - PackPlotDataPtrs(srcmf, Efield_aux[lev]); - amrex::average_edge_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Efield_aux[lev][1], 0, 1); -#endif - } - if (lev == 0) - { - varnames.push_back("Ex"); - varnames.push_back("Ey"); - varnames.push_back("Ez"); - } - dcomp += 3; - - // B - if (do_nodal) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp , *Bfield_aux[lev][0], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Bfield_aux[lev][1], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+2, *Bfield_aux[lev][2], 0, 1); - } - else - { - PackPlotDataPtrs(srcmf, Bfield_aux[lev]); - amrex::average_face_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(*mf[lev], *Bfield_aux[lev][1], 0, dcomp+1, 1, ngrow); -#endif - } - if (lev == 0) - { - varnames.push_back("Bx"); - varnames.push_back("By"); - varnames.push_back("Bz"); - } - dcomp += 3; - - if (plot_part_per_cell) - { - MultiFab temp_dat(grids[lev],mf[lev]->DistributionMap(),1,0); - temp_dat.setVal(0); - - // MultiFab containing number of particles in each cell - mypc->Increment(temp_dat, lev); - MultiFab::Copy(*mf[lev], temp_dat, 0, dcomp, 1, 0); - if (lev == 0) - { - varnames.push_back("part_per_cell"); - } - dcomp += 1; - } - - if (plot_part_per_grid || plot_part_per_proc) - { - const Vector<long>& npart_in_grid = mypc->NumberOfParticlesInGrid(lev); - - if (plot_part_per_grid) - { - // MultiFab containing number of particles per grid (stored as constant for all cells in each grid) -#ifdef _OPENMP -#pragma omp parallel -#endif - for (MFIter mfi(*mf[lev]); mfi.isValid(); ++mfi) { - (*mf[lev])[mfi].setVal(static_cast<Real>(npart_in_grid[mfi.index()]), dcomp); - } - if (lev == 0) - { - varnames.push_back("part_per_grid"); - } - dcomp += 1; - } - - if (plot_part_per_proc) - { - // MultiFab containing number of particles per process (stored as constant for all cells in each grid) - long n_per_proc = 0; -#ifdef _OPENMP -#pragma omp parallel reduction(+:n_per_proc) -#endif - for (MFIter mfi(*mf[lev]); mfi.isValid(); ++mfi) { - n_per_proc += npart_in_grid[mfi.index()]; - } - mf[lev]->setVal(static_cast<Real>(n_per_proc), dcomp,1); - if (lev == 0) - { - varnames.push_back("part_per_proc"); - } - dcomp += 1; - } - } - - if (plot_proc_number) - { - // MultiFab containing the Processor ID -#ifdef _OPENMP -#pragma omp parallel -#endif - for (MFIter mfi(*mf[lev]); mfi.isValid(); ++mfi) { - (*mf[lev])[mfi].setVal(static_cast<Real>(ParallelDescriptor::MyProc()), dcomp); - } - if (lev == 0) - { - varnames.push_back("proc_number"); - } - dcomp += 1; - } - - if (plot_divb) - { - if (do_nodal) amrex::Abort("TODO: do_nodal && plot_divb"); - ComputeDivB(*mf[lev], dcomp, - {Bfield_aux[lev][0].get(),Bfield_aux[lev][1].get(),Bfield_aux[lev][2].get()}, - WarpX::CellSize(lev)); - if (lev == 0) - { - varnames.push_back("divB"); - } - dcomp += 1; - } - - if (plot_dive) - { - if (do_nodal) amrex::Abort("TODO: do_nodal && plot_dive"); - const BoxArray& ba = amrex::convert(boxArray(lev),IntVect::TheUnitVector()); - MultiFab dive(ba,DistributionMap(lev),1,0); - ComputeDivE(dive, 0, - {Efield_aux[lev][0].get(), Efield_aux[lev][1].get(), Efield_aux[lev][2].get()}, - WarpX::CellSize(lev)); - amrex::average_node_to_cellcenter(*mf[lev], dcomp, dive, 0, 1); - if (lev == 0) - { - varnames.push_back("divE"); - } - dcomp += 1; - } - - if (plot_rho) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp, *rho_fp[lev], 0, 1); - if (lev == 0) - { - varnames.push_back("rho"); - } - dcomp += 1; - } - - if (plot_F) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp, *F_fp[lev], 0, 1); - if (lev == 0) - { - varnames.push_back("F"); - } - dcomp += 1; - } - - if (plot_finepatch) - { - if (do_nodal) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp , *Efield_fp[lev][0], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Efield_fp[lev][1], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+2, *Efield_fp[lev][2], 0, 1); - } - else - { - PackPlotDataPtrs(srcmf, Efield_fp[lev]); - amrex::average_edge_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Efield_fp[lev][1], 0, 1); -#endif - } - if (lev == 0) - { - varnames.push_back("Ex_fp"); - varnames.push_back("Ey_fp"); - varnames.push_back("Ez_fp"); - } - dcomp += 3; - - if (do_nodal) - { - amrex::average_node_to_cellcenter(*mf[lev], dcomp , *Bfield_fp[lev][0], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *Bfield_fp[lev][1], 0, 1); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+2, *Bfield_fp[lev][2], 0, 1); - } - else - { - PackPlotDataPtrs(srcmf, Bfield_fp[lev]); - amrex::average_face_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(*mf[lev], *Bfield_fp[lev][1], 0, dcomp+1, 1, ngrow); -#endif - } - if (lev == 0) - { - varnames.push_back("Bx_fp"); - varnames.push_back("By_fp"); - varnames.push_back("Bz_fp"); - } - dcomp += 3; - } - - if (plot_crsepatch) - { - // First the electric field - if (lev == 0) - { - mf[lev]->setVal(0.0, dcomp, 3, ngrow); - } - else - { - if (do_nodal) amrex::Abort("TODO: do_nodal && plot_crsepatch"); - std::array<std::unique_ptr<MultiFab>, 3> E = getInterpolatedE(lev); - PackPlotDataPtrs(srcmf, E); - amrex::average_edge_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - amrex::average_node_to_cellcenter(*mf[lev], dcomp+1, *E[1], 0, 1); -#endif - } - if (lev == 0) - { - varnames.push_back("Ex_cp"); - varnames.push_back("Ey_cp"); - varnames.push_back("Ez_cp"); - } - dcomp += 3; + // Average the fields from the simulation grid to the cell centers + const int ngrow = 0; + Vector<std::string> varnames; // Name of the written fields + // mf_avg will contain the averaged, cell-centered fields + Vector<MultiFab> mf_avg; + WarpX::AverageAndPackFields( varnames, mf_avg, ngrow ); + + // Coarsen the fields, if requested by the user + Vector<const MultiFab*> output_mf; // will point to the data to be written + Vector<MultiFab> coarse_mf; // will remain empty if there is no coarsening + Vector<Geometry> output_geom; + if (plot_coarsening_ratio != 1) { + coarsenCellCenteredFields( coarse_mf, output_geom, mf_avg, Geom(), + plot_coarsening_ratio, finest_level ); + output_mf = amrex::GetVecOfConstPtrs(coarse_mf); + } else { // No averaging necessary, simply point to mf_avg + output_mf = amrex::GetVecOfConstPtrs(mf_avg); + output_geom = Geom(); + } - // now the magnetic field - if (lev == 0) - { - mf[lev]->setVal(0.0, dcomp, 3, ngrow); - } - else - { - if (do_nodal) amrex::Abort("TODO: do_nodal && plot_crsepatch"); - std::array<std::unique_ptr<MultiFab>, 3> B = getInterpolatedB(lev); - PackPlotDataPtrs(srcmf, B); - amrex::average_face_to_cellcenter(*mf[lev], dcomp, srcmf); -#if (AMREX_SPACEDIM == 2) - MultiFab::Copy(*mf[lev], *mf[lev], dcomp+1, dcomp+2, 1, ngrow); - MultiFab::Copy(*mf[lev], *B[1], 0, dcomp+1, 1, ngrow); +#ifdef WARPX_USE_OPENPMD + if (dump_openpmd){ + // Write openPMD format: only for level 0 + std::string filename = amrex::Concatenate("diags/hdf5/data", istep[0]); + filename += ".h5"; + WriteOpenPMDFields( filename, varnames, + *output_mf[0], output_geom[0], istep[0], t_new[0] ); + } #endif - } - if (lev == 0) - { - varnames.push_back("Bx_cp"); - varnames.push_back("By_cp"); - varnames.push_back("Bz_cp"); - } - dcomp += 3; - } - - if (costs[0] != nullptr) - { - MultiFab::Copy(*mf[lev], *costs[lev], 0, dcomp, 1, 0); - if (lev == 0) - { - varnames.push_back("costs"); - } - dcomp += 1; - } - BL_ASSERT(dcomp == ncomp); - } + if (dump_plotfiles){ -#if 0 - for (int lev = finest_level; lev > 0; --lev) - { - amrex::average_down(*mf[lev], *mf[lev-1], 0, ncomp, refRatio(lev-1)); - } -#endif + // Write the fields contained in `mf_avg`, and corresponding to the + // names `varnames`, into a plotfile. + // Prepare extra directory (filled later), for the raw fields + Vector<std::string> rfs; + VisMF::Header::Version current_version = VisMF::GetHeaderVersion(); + VisMF::SetHeaderVersion(plotfile_headerversion); + if (plot_raw_fields) rfs.emplace_back("raw_fields"); + amrex::WriteMultiLevelPlotfile(plotfilename, finest_level+1, + output_mf, varnames, output_geom, + t_new[0], istep, refRatio(), + "HyperCLaw-V1.1", + "Level_", + "Cell", + rfs + ); - Vector<std::string> rfs; - if (plot_raw_fields) rfs.emplace_back("raw_fields"); // pre-build raw_fields/ - amrex::WriteMultiLevelPlotfile(plotfilename, finest_level+1, - amrex::GetVecOfConstPtrs(mf), - varnames, Geom(), t_new[0], istep, refRatio(), - "HyperCLaw-V1.1", - "Level_", - "Cell", - rfs); - } if (plot_raw_fields) { const int nlevels = finestLevel()+1; for (int lev = 0; lev < nlevels; ++lev) { - const std::string raw_plotfilename = plotfilename + "/raw_fields"; - // Plot auxilary patch - if (plot_raw_fields_guards) { - VisMF::Write(*Efield_aux[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_aux")); - VisMF::Write(*Efield_aux[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_aux")); - VisMF::Write(*Efield_aux[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_aux")); - VisMF::Write(*Bfield_aux[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_aux")); - VisMF::Write(*Bfield_aux[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_aux")); - VisMF::Write(*Bfield_aux[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_aux")); - } else { - const DistributionMapping& dm = DistributionMap(lev); - MultiFab Ex(Efield_aux[lev][0]->boxArray(), dm, 1, 0); - MultiFab Ey(Efield_aux[lev][1]->boxArray(), dm, 1, 0); - MultiFab Ez(Efield_aux[lev][2]->boxArray(), dm, 1, 0); - MultiFab Bx(Bfield_aux[lev][0]->boxArray(), dm, 1, 0); - MultiFab By(Bfield_aux[lev][1]->boxArray(), dm, 1, 0); - MultiFab Bz(Bfield_aux[lev][2]->boxArray(), dm, 1, 0); - MultiFab::Copy(Ex, *Efield_aux[lev][0], 0, 0, 1, 0); - MultiFab::Copy(Ey, *Efield_aux[lev][1], 0, 0, 1, 0); - MultiFab::Copy(Ez, *Efield_aux[lev][2], 0, 0, 1, 0); - MultiFab::Copy(Bx, *Bfield_aux[lev][0], 0, 0, 1, 0); - MultiFab::Copy(By, *Bfield_aux[lev][1], 0, 0, 1, 0); - MultiFab::Copy(Bz, *Bfield_aux[lev][2], 0, 0, 1, 0); - VisMF::Write(Ex, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_aux")); - VisMF::Write(Ey, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_aux")); - VisMF::Write(Ez, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_aux")); - VisMF::Write(Bx, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_aux")); - VisMF::Write(By, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_aux")); - VisMF::Write(Bz, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_aux")); - } - - // Plot fine patch + const std::unique_ptr<MultiFab> empty_ptr; + const std::string raw_pltname = plotfilename + "/raw_fields"; + const DistributionMapping& dm = DistributionMap(lev); + + // Auxiliary patch + WriteRawField( *Efield_aux[lev][0], dm, raw_pltname, level_prefix, "Ex_aux", lev, plot_raw_fields_guards); + WriteRawField( *Efield_aux[lev][1], dm, raw_pltname, level_prefix, "Ey_aux", lev, plot_raw_fields_guards); + WriteRawField( *Efield_aux[lev][2], dm, raw_pltname, level_prefix, "Ez_aux", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_aux[lev][0], dm, raw_pltname, level_prefix, "Bx_aux", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_aux[lev][1], dm, raw_pltname, level_prefix, "By_aux", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_aux[lev][2], dm, raw_pltname, level_prefix, "Bz_aux", lev, plot_raw_fields_guards); + + // Fine patch if (plot_finepatch) { - if (plot_raw_fields_guards) { - VisMF::Write(*Efield_fp[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_fp")); - VisMF::Write(*Efield_fp[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_fp")); - VisMF::Write(*Efield_fp[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_fp")); - VisMF::Write(*Bfield_fp[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_fp")); - VisMF::Write(*Bfield_fp[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_fp")); - VisMF::Write(*Bfield_fp[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_fp")); - VisMF::Write(*current_fp[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jx_fp")); - VisMF::Write(*current_fp[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jy_fp")); - VisMF::Write(*current_fp[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jz_fp")); - } else { - const DistributionMapping& dm = DistributionMap(lev); - MultiFab Ex(Efield_fp[lev][0]->boxArray(), dm, 1, 0); - MultiFab Ey(Efield_fp[lev][1]->boxArray(), dm, 1, 0); - MultiFab Ez(Efield_fp[lev][2]->boxArray(), dm, 1, 0); - MultiFab Bx(Bfield_fp[lev][0]->boxArray(), dm, 1, 0); - MultiFab By(Bfield_fp[lev][1]->boxArray(), dm, 1, 0); - MultiFab Bz(Bfield_fp[lev][2]->boxArray(), dm, 1, 0); - MultiFab jx(current_fp[lev][0]->boxArray(), dm, 1, 0); - MultiFab jy(current_fp[lev][1]->boxArray(), dm, 1, 0); - MultiFab jz(current_fp[lev][2]->boxArray(), dm, 1, 0); - MultiFab::Copy(Ex, *Efield_fp[lev][0], 0, 0, 1, 0); - MultiFab::Copy(Ey, *Efield_fp[lev][1], 0, 0, 1, 0); - MultiFab::Copy(Ez, *Efield_fp[lev][2], 0, 0, 1, 0); - MultiFab::Copy(Bx, *Bfield_fp[lev][0], 0, 0, 1, 0); - MultiFab::Copy(By, *Bfield_fp[lev][1], 0, 0, 1, 0); - MultiFab::Copy(Bz, *Bfield_fp[lev][2], 0, 0, 1, 0); - MultiFab::Copy(jx, *current_fp[lev][0], 0, 0, 1, 0); - MultiFab::Copy(jy, *current_fp[lev][1], 0, 0, 1, 0); - MultiFab::Copy(jz, *current_fp[lev][2], 0, 0, 1, 0); - VisMF::Write(Ex, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_fp")); - VisMF::Write(Ey, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_fp")); - VisMF::Write(Ez, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_fp")); - VisMF::Write(Bx, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_fp")); - VisMF::Write(By, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_fp")); - VisMF::Write(Bz, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_fp")); - VisMF::Write(jx, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jx_fp")); - VisMF::Write(jy, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jy_fp")); - VisMF::Write(jz, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "jz_fp")); + WriteRawField( *Efield_fp[lev][0], dm, raw_pltname, level_prefix, "Ex_fp", lev, plot_raw_fields_guards); + WriteRawField( *Efield_fp[lev][1], dm, raw_pltname, level_prefix, "Ey_fp", lev, plot_raw_fields_guards); + WriteRawField( *Efield_fp[lev][2], dm, raw_pltname, level_prefix, "Ez_fp", lev, plot_raw_fields_guards); + WriteRawField( *current_fp[lev][0], dm, raw_pltname, level_prefix, "jx_fp", lev, plot_raw_fields_guards); + WriteRawField( *current_fp[lev][1], dm, raw_pltname, level_prefix, "jy_fp", lev, plot_raw_fields_guards); + WriteRawField( *current_fp[lev][2], dm, raw_pltname, level_prefix, "jz_fp", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_fp[lev][0], dm, raw_pltname, level_prefix, "Bx_fp", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_fp[lev][1], dm, raw_pltname, level_prefix, "By_fp", lev, plot_raw_fields_guards); + WriteRawField( *Bfield_fp[lev][2], dm, raw_pltname, level_prefix, "Bz_fp", lev, plot_raw_fields_guards); + if (F_fp[lev]) WriteRawField( *F_fp[lev], dm, raw_pltname, level_prefix, "F_fp", lev, plot_raw_fields_guards); + if (plot_rho) { + // Use the component 1 of `rho_fp`, i.e. rho_new for time synchronization + MultiFab rho_new(*rho_fp[lev], amrex::make_alias, 1, 1); + WriteRawField( rho_new, dm, raw_pltname, level_prefix, "rho_fp", lev, plot_raw_fields_guards); } } - // Plot coarse patch - if (plot_crsepatch) - { - if (lev == 0) - { - const DistributionMapping& dm = DistributionMap(lev); - MultiFab Ex(Efield_aux[lev][0]->boxArray(), dm, 1, 0); - MultiFab Ey(Efield_aux[lev][1]->boxArray(), dm, 1, 0); - MultiFab Ez(Efield_aux[lev][2]->boxArray(), dm, 1, 0); - MultiFab Bx(Bfield_aux[lev][0]->boxArray(), dm, 1, 0); - MultiFab By(Bfield_aux[lev][1]->boxArray(), dm, 1, 0); - MultiFab Bz(Bfield_aux[lev][2]->boxArray(), dm, 1, 0); - - Ex.setVal(0.0); Ey.setVal(0.0); Ez.setVal(0.0); - Bx.setVal(0.0); By.setVal(0.0); Bz.setVal(0.0); - - VisMF::Write(Ex, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_cp")); - VisMF::Write(Ey, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_cp")); - VisMF::Write(Ez, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_cp")); - VisMF::Write(Bx, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_cp")); - VisMF::Write(By, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_cp")); - VisMF::Write(Bz, amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_cp")); - } else { - - if (plot_raw_fields_guards) { - VisMF::Write(*Efield_cp[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_cp")); - VisMF::Write(*Efield_cp[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_cp")); - VisMF::Write(*Efield_cp[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_cp")); - VisMF::Write(*Bfield_cp[lev][0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_cp")); - VisMF::Write(*Bfield_cp[lev][1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_cp")); - VisMF::Write(*Bfield_cp[lev][2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_cp")); - } else { - std::array<std::unique_ptr<MultiFab>, 3> E = getInterpolatedE(lev); - std::array<std::unique_ptr<MultiFab>, 3> B = getInterpolatedB(lev); - - VisMF::Write(*E[0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ex_cp")); - VisMF::Write(*E[1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ey_cp")); - VisMF::Write(*E[2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Ez_cp")); - VisMF::Write(*B[0], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bx_cp")); - VisMF::Write(*B[1], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "By_cp")); - VisMF::Write(*B[2], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "Bz_cp")); - } - } + // Coarse path + if (plot_crsepatch) { + WriteCoarseVector( "E", + Efield_cp[lev][0], Efield_cp[lev][1], Efield_cp[lev][2], + Efield_fp[lev][0], Efield_fp[lev][1], Efield_fp[lev][2], + dm, raw_pltname, level_prefix, lev, plot_raw_fields_guards); + WriteCoarseVector( "B", + Bfield_cp[lev][0], Bfield_cp[lev][1], Bfield_cp[lev][2], + Bfield_fp[lev][0], Bfield_fp[lev][1], Bfield_fp[lev][2], + dm, raw_pltname, level_prefix, lev, plot_raw_fields_guards); + WriteCoarseVector( "j", + current_cp[lev][0], current_cp[lev][1], current_cp[lev][2], + current_fp[lev][0], current_fp[lev][1], current_fp[lev][2], + dm, raw_pltname, level_prefix, lev, plot_raw_fields_guards); + if (F_cp[lev]) WriteCoarseScalar( + "F", F_cp[lev], F_fp[lev], + dm, raw_pltname, level_prefix, lev, + plot_raw_fields_guards); + if (plot_rho) WriteCoarseScalar( + "rho", rho_cp[lev], rho_fp[lev], + dm, raw_pltname, level_prefix, lev, + plot_raw_fields_guards, 1); + // Use the component 1 of `rho_cp`, i.e. rho_new for time synchronization } - - if (F_fp[lev]) { - VisMF::Write(*F_fp[lev], amrex::MultiFabFileFullPrefix(lev, raw_plotfilename, level_prefix, "F_fp")); - } - } } @@ -1264,16 +638,18 @@ WarpX::WritePlotFile () const particle_varnames.push_back("uxold"); particle_varnames.push_back("uyold"); - particle_varnames.push_back("uzold"); + particle_varnames.push_back("uzold"); #endif - - mypc->Checkpoint(plotfilename, true, particle_varnames); + + mypc->WritePlotFile(plotfilename, particle_plot_flags, particle_varnames); WriteJobInfo(plotfilename); WriteWarpXHeader(plotfilename); VisMF::SetHeaderVersion(current_version); + } // endif: dump_plotfiles + } void @@ -1394,145 +770,3 @@ WarpX::WriteJobInfo (const std::string& dir) const jobInfoFile.close(); } } - -std::array<std::unique_ptr<MultiFab>, 3> WarpX::getInterpolatedE(int lev) const -{ - - const int ngrow = 0; - - std::array<std::unique_ptr<MultiFab>, 3> interpolated_E; - for (int i = 0; i < 3; ++i) { - interpolated_E[i].reset( new MultiFab(Efield_aux[lev][i]->boxArray(), dmap[lev], 1, ngrow) ); - interpolated_E[i]->setVal(0.0); - } - - const int r_ratio = refRatio(lev-1)[0]; - const int use_limiter = 0; -#ifdef _OPEMP -#pragma omp parallel -#endif - { - std::array<FArrayBox,3> efab; - for (MFIter mfi(*interpolated_E[0]); mfi.isValid(); ++mfi) - { - Box ccbx = mfi.fabbox(); - ccbx.enclosedCells(); - ccbx.coarsen(r_ratio).refine(r_ratio); // so that ccbx is coarsenable - - const FArrayBox& cxfab = (*Efield_cp[lev][0])[mfi]; - const FArrayBox& cyfab = (*Efield_cp[lev][1])[mfi]; - const FArrayBox& czfab = (*Efield_cp[lev][2])[mfi]; - - efab[0].resize(amrex::convert(ccbx,Ex_nodal_flag)); - efab[1].resize(amrex::convert(ccbx,Ey_nodal_flag)); - efab[2].resize(amrex::convert(ccbx,Ez_nodal_flag)); - -#if (AMREX_SPACEDIM == 3) - amrex_interp_efield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(efab[0]), - BL_TO_FORTRAN_ANYD(efab[1]), - BL_TO_FORTRAN_ANYD(efab[2]), - BL_TO_FORTRAN_ANYD(cxfab), - BL_TO_FORTRAN_ANYD(cyfab), - BL_TO_FORTRAN_ANYD(czfab), - &r_ratio,&use_limiter); -#else - amrex_interp_efield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(efab[0]), - BL_TO_FORTRAN_ANYD(efab[2]), - BL_TO_FORTRAN_ANYD(cxfab), - BL_TO_FORTRAN_ANYD(czfab), - &r_ratio,&use_limiter); - amrex_interp_nd_efield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(efab[1]), - BL_TO_FORTRAN_ANYD(cyfab), - &r_ratio); -#endif - - for (int i = 0; i < 3; ++i) { - const Box& bx = (*interpolated_E[i])[mfi].box(); - (*interpolated_E[i])[mfi].plus(efab[i], bx, bx, 0, 0, 1); - } - } - } - - return interpolated_E; -} - -std::array<std::unique_ptr<MultiFab>, 3> WarpX::getInterpolatedB(int lev) const -{ - const int ngrow = 0; - - std::array<std::unique_ptr<MultiFab>, 3> interpolated_B; - for (int i = 0; i < 3; ++i) { - interpolated_B[i].reset( new MultiFab(Bfield_aux[lev][i]->boxArray(), dmap[lev], 1, ngrow) ); - interpolated_B[i]->setVal(0.0); - } - - const Real* dx = Geom(lev-1).CellSize(); - const int r_ratio = refRatio(lev-1)[0]; - const int use_limiter = 0; -#ifdef _OPEMP -#pragma omp parallel -#endif - { - std::array<FArrayBox,3> bfab; - for (MFIter mfi(*interpolated_B[0]); mfi.isValid(); ++mfi) - { - Box ccbx = mfi.fabbox(); - ccbx.enclosedCells(); - ccbx.coarsen(r_ratio).refine(r_ratio); // so that ccbx is coarsenable - - const FArrayBox& cxfab = (*Bfield_cp[lev][0])[mfi]; - const FArrayBox& cyfab = (*Bfield_cp[lev][1])[mfi]; - const FArrayBox& czfab = (*Bfield_cp[lev][2])[mfi]; - - bfab[0].resize(amrex::convert(ccbx,Bx_nodal_flag)); - bfab[1].resize(amrex::convert(ccbx,By_nodal_flag)); - bfab[2].resize(amrex::convert(ccbx,Bz_nodal_flag)); - -#if (AMREX_SPACEDIM == 3) - amrex_interp_div_free_bfield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(bfab[0]), - BL_TO_FORTRAN_ANYD(bfab[1]), - BL_TO_FORTRAN_ANYD(bfab[2]), - BL_TO_FORTRAN_ANYD(cxfab), - BL_TO_FORTRAN_ANYD(cyfab), - BL_TO_FORTRAN_ANYD(czfab), - dx, &r_ratio, &use_limiter); -#else - amrex_interp_div_free_bfield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(bfab[0]), - BL_TO_FORTRAN_ANYD(bfab[2]), - BL_TO_FORTRAN_ANYD(cxfab), - BL_TO_FORTRAN_ANYD(czfab), - dx, &r_ratio, &use_limiter); - amrex_interp_cc_bfield(ccbx.loVect(), ccbx.hiVect(), - BL_TO_FORTRAN_ANYD(bfab[1]), - BL_TO_FORTRAN_ANYD(cyfab), - &r_ratio, &use_limiter); -#endif - - for (int i = 0; i < 3; ++i) { - const Box& bx = (*interpolated_B[i])[mfi].box(); - (*interpolated_B[i])[mfi].plus(bfab[i], bx, bx, 0, 0, 1); - } - } - } - return interpolated_B; -} - -void -WarpX::PackPlotDataPtrs (Vector<const MultiFab*>& pmf, - const std::array<std::unique_ptr<MultiFab>,3>& data) -{ - BL_ASSERT(pmf.size() == AMREX_SPACEDIM); -#if (AMREX_SPACEDIM == 3) - pmf[0] = data[0].get(); - pmf[1] = data[1].get(); - pmf[2] = data[2].get(); -#elif (AMREX_SPACEDIM == 2) - pmf[0] = data[0].get(); - pmf[1] = data[2].get(); -#endif -} diff --git a/Source/Filter/BilinearFilter.H b/Source/Filter/BilinearFilter.H new file mode 100644 index 000000000..8a1b87e97 --- /dev/null +++ b/Source/Filter/BilinearFilter.H @@ -0,0 +1,26 @@ +#include <AMReX_MultiFab.H> +#include <AMReX_CudaContainers.H> + +#ifndef WARPX_FILTER_H_ +#define WARPX_FILTER_H_ + +class BilinearFilter +{ +public: + BilinearFilter () = default; + + void ComputeStencils(); + void ApplyStencil(amrex::MultiFab& dstmf, const amrex::MultiFab& srcmf, int scomp=0, int dcomp=0, int ncomp=10000); + + amrex::IntVect npass_each_dir; + amrex::IntVect stencil_length_each_dir; + + void Filter(const amrex::Box& tbx, amrex::FArrayBox const& tmpfab, amrex::FArrayBox &dstfab, int scomp, int dcomp, int ncomp); + +private: + + amrex::Gpu::ManagedVector<amrex::Real> stencil_x, stencil_y, stencil_z; + + amrex::Dim3 slen; +}; +#endif diff --git a/Source/Filter/BilinearFilter.cpp b/Source/Filter/BilinearFilter.cpp new file mode 100644 index 000000000..4017d3f73 --- /dev/null +++ b/Source/Filter/BilinearFilter.cpp @@ -0,0 +1,202 @@ +#include <WarpX.H> +#include <BilinearFilter.H> +#include <WarpX_f.H> + +#ifdef _OPENMP +#include <omp.h> +#endif + +using namespace amrex; + +namespace { + void compute_stencil(Gpu::ManagedVector<Real> &stencil, int npass) + { + Gpu::ManagedVector<Real> old_s(1+npass,0.); + Gpu::ManagedVector<Real> new_s(1+npass,0.); + + old_s[0] = 1.; + int jmax = 1; + amrex::Real loc; + // Convolve the filter with itself npass times + for(int ipass=1; ipass<npass+1; ipass++){ + // element 0 has to be treated in its own way + new_s[0] = 0.5 * old_s[0]; + if (1<jmax) new_s[0] += 0.5 * old_s[1]; + loc = 0.; + // For each element j, apply the filter to + // old_s to get new_s[j]. loc stores the tmp + // filtered value. + for(int j=1; j<jmax+1; j++){ + loc = 0.5 * old_s[j]; + loc += 0.25 * old_s[j-1]; + if (j<jmax) loc += 0.25 * old_s[j+1]; + new_s[j] = loc; + } + // copy new_s into old_s + old_s = new_s; + // extend the stencil length for next iteration + jmax += 1; + } + // we use old_s here to make sure the stencil + // is corrent even when npass = 0 + stencil = old_s; + stencil[0] *= 0.5; // because we will use it twice + } +} + +void BilinearFilter::ComputeStencils(){ + BL_PROFILE("BilinearFilter::ComputeStencils()"); + stencil_length_each_dir = npass_each_dir; + stencil_length_each_dir += 1.; +#if (AMREX_SPACEDIM == 3) + // npass_each_dir = npass_x npass_y npass_z + stencil_x.resize( 1 + npass_each_dir[0] ); + stencil_y.resize( 1 + npass_each_dir[1] ); + stencil_z.resize( 1 + npass_each_dir[2] ); + compute_stencil(stencil_x, npass_each_dir[0]); + compute_stencil(stencil_y, npass_each_dir[1]); + compute_stencil(stencil_z, npass_each_dir[2]); +#elif (AMREX_SPACEDIM == 2) + // npass_each_dir = npass_x npass_z + stencil_x.resize( 1 + npass_each_dir[0] ); + stencil_z.resize( 1 + npass_each_dir[1] ); + compute_stencil(stencil_x, npass_each_dir[0]); + compute_stencil(stencil_z, npass_each_dir[1]); +#endif + slen = stencil_length_each_dir.dim3(); +#if (AMREX_SPACEDIM == 2) + slen.z = 1; +#endif +} + + +void +BilinearFilter::ApplyStencil (MultiFab& dstmf, const MultiFab& srcmf, int scomp, int dcomp, int ncomp) +{ + BL_PROFILE("BilinearFilter::ApplyStencil()"); + ncomp = std::min(ncomp, srcmf.nComp()); +#ifdef _OPENMP +#pragma omp parallel if (Gpu::notInLaunchRegion()) +#endif + { + FArrayBox tmpfab; + for (MFIter mfi(dstmf,TilingIfNotGPU()); mfi.isValid(); ++mfi){ + const auto& srcfab = srcmf[mfi]; + auto& dstfab = dstmf[mfi]; + const Box& tbx = mfi.growntilebox(); + const Box& gbx = amrex::grow(tbx,stencil_length_each_dir-1); + // tmpfab has enough ghost cells for the stencil + AsyncFab tmp_async_fab(tmpfab,gbx,ncomp); + FArrayBox* tmpfab_ptr = tmp_async_fab.fabPtr(); + const FArrayBox* srcfab_ptr = srcmf.fabPtr(mfi); + // Copy values in srcfab into tmpfab + const Box& ibx = gbx & srcfab.box(); + AMREX_LAUNCH_HOST_DEVICE_LAMBDA(gbx, tgbx, + { + tmpfab_ptr->setVal(0.0, tgbx, 0, ncomp); + }); + + AMREX_LAUNCH_HOST_DEVICE_LAMBDA(ibx, tibx, + { + tmpfab_ptr->copy(*srcfab_ptr, tibx, scomp, tibx, 0, ncomp); + }); + + // Apply filter + Filter(tbx, tmp_async_fab.hostFab(), dstfab, 0, dcomp, ncomp); + } + } +} + +void BilinearFilter::Filter (const Box& tbx, FArrayBox const& tmpfab, FArrayBox &dstfab, + int scomp, int dcomp, int ncomp) +{ + const auto lo = amrex::lbound(tbx); + const auto hi = amrex::ubound(tbx); + const auto tmp = tmpfab.array(); + const auto dst = dstfab.array(); + // tmp and dst are of type Array4 (Fortran ordering) + amrex::Real const* AMREX_RESTRICT sx = stencil_x.dataPtr(); + amrex::Real const* AMREX_RESTRICT sy = stencil_y.dataPtr(); + amrex::Real const* AMREX_RESTRICT sz = stencil_z.dataPtr(); +#ifdef AMREX_USE_CUDA + Dim3 slen_local = slen; + amrex::ParallelFor(tbx, ncomp, + [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept + { + dst(i,j,k,dcomp+n) = 0.0; + + for (int iz=0; iz < slen_local.z; ++iz){ + for (int iy=0; iy < slen_local.y; ++iy){ + for (int ix=0; ix < slen_local.x; ++ix){ +#if (AMREX_SPACEDIM == 3) + Real sss = sx[ix]*sy[iy]*sz[iz]; +#else + Real sss = sx[ix]*sz[iy]; +#endif +#if (AMREX_SPACEDIM == 3) + dst(i,j,k,dcomp+n) += sss*(tmp(i-ix,j-iy,k-iz,scomp+n) + +tmp(i+ix,j-iy,k-iz,scomp+n) + +tmp(i-ix,j+iy,k-iz,scomp+n) + +tmp(i+ix,j+iy,k-iz,scomp+n) + +tmp(i-ix,j-iy,k+iz,scomp+n) + +tmp(i+ix,j-iy,k+iz,scomp+n) + +tmp(i-ix,j+iy,k+iz,scomp+n) + +tmp(i+ix,j+iy,k+iz,scomp+n)); +#else + dst(i,j,k,dcomp+n) += sss*(tmp(i-ix,j-iy,k,scomp+n) + +tmp(i+ix,j-iy,k,scomp+n) + +tmp(i-ix,j+iy,k,scomp+n) + +tmp(i+ix,j+iy,k,scomp+n)); +#endif + } + } + } + }); +#else // if not USE_CUDA + for (int n = 0; n < ncomp; ++n) { + // Set dst value to 0. + for (int k = lo.z; k <= hi.z; ++k) { + for (int j = lo.y; j <= hi.y; ++j) { + for (int i = lo.x; i <= hi.x; ++i) { + dst(i,j,k,dcomp+n) = 0.0; + } + } + } + // 3 nested loop on 3D stencil + for (int iz=0; iz < slen.z; ++iz){ + for (int iy=0; iy < slen.y; ++iy){ + for (int ix=0; ix < slen.x; ++ix){ +#if (AMREX_SPACEDIM == 3) + Real sss = sx[ix]*sy[iy]*sz[iz]; +#else + Real sss = sx[ix]*sz[iy]; +#endif + // 3 nested loop on 3D array + for (int k = lo.z; k <= hi.z; ++k) { + for (int j = lo.y; j <= hi.y; ++j) { + AMREX_PRAGMA_SIMD + for (int i = lo.x; i <= hi.x; ++i) { +#if (AMREX_SPACEDIM == 3) + dst(i,j,k,dcomp+n) += sss*(tmp(i-ix,j-iy,k-iz,scomp+n) + +tmp(i+ix,j-iy,k-iz,scomp+n) + +tmp(i-ix,j+iy,k-iz,scomp+n) + +tmp(i+ix,j+iy,k-iz,scomp+n) + +tmp(i-ix,j-iy,k+iz,scomp+n) + +tmp(i+ix,j-iy,k+iz,scomp+n) + +tmp(i-ix,j+iy,k+iz,scomp+n) + +tmp(i+ix,j+iy,k+iz,scomp+n)); +#else + dst(i,j,k,dcomp+n) += sss*(tmp(i-ix,j-iy,k,scomp+n) + +tmp(i+ix,j-iy,k,scomp+n) + +tmp(i-ix,j+iy,k,scomp+n) + +tmp(i+ix,j+iy,k,scomp+n)); +#endif + } + } + } + } + } + } + } +#endif // USE_CUDA +} diff --git a/Source/Filter/Make.package b/Source/Filter/Make.package index 56c0e7852..36e74bfb0 100644 --- a/Source/Filter/Make.package +++ b/Source/Filter/Make.package @@ -1,4 +1,5 @@ -F90EXE_sources += filter_module.F90 +CEXE_sources += BilinearFilter.cpp +CEXE_headers += BilinearFilter.H INCLUDE_LOCATIONS += $(WARPX_HOME)/Source/Filter VPATH_LOCATIONS += $(WARPX_HOME)/Source/Filter diff --git a/Source/Filter/filter_module.F90 b/Source/Filter/filter_module.F90 deleted file mode 100644 index ea87be7ca..000000000 --- a/Source/Filter/filter_module.F90 +++ /dev/null @@ -1,87 +0,0 @@ -module warpx_filter_module - - use iso_c_binding - use amrex_fort_module, only : rt => amrex_real - use amrex_constants_module, only : fourth, eighth - - implicit none - -contains - - subroutine warpx_filter_3d(lo, hi, src, slo, shi, dst, dlo, dhi, nc) & - bind(c, name='warpx_filter_3d') - integer, intent(in), value :: nc - integer, dimension(3), intent(in) :: lo, hi, dlo, dhi, slo, shi - real(rt), intent(inout) :: dst(dlo(1):dhi(1), dlo(2):dhi(2), dlo(3):dhi(3),nc) - real(rt), intent(in ) :: src(slo(1):shi(1), slo(2):shi(2), slo(3):shi(3),nc) - - real(rt), parameter :: sixteenth = 1.d0/16.d0 - real(rt), parameter :: thirtysecond = 1.d0/32.d0 - real(rt), parameter :: sixtyfourth = 1.d0/64.d0 - integer :: i,j,k,c - - do c = 1, nc - do k = lo(3), hi(3) - do j = lo(2), hi(2) - do i = lo(1), hi(1) - dst(i,j,k,c) = sixtyfourth*(src(i-1,j-1,k-1,c) & - & +src(i+1,j-1,k-1,c) & - & +src(i-1,j+1,k-1,c) & - & +src(i+1,j+1,k-1,c) & - & +src(i-1,j-1,k+1,c) & - & +src(i+1,j-1,k+1,c) & - & +src(i-1,j+1,k+1,c) & - & +src(i+1,j+1,k+1,c)) & - + thirtysecond*(src(i-1,j-1,k ,c) & - & +src(i+1,j-1,k ,c) & - & +src(i-1,j+1,k ,c) & - & +src(i+1,j+1,k ,c) & - & +src(i-1,j ,k-1,c) & - & +src(i+1,j ,k-1,c) & - & +src(i-1,j ,k+1,c) & - & +src(i+1,j ,k+1,c) & - & +src(i ,j-1,k-1,c) & - & +src(i ,j+1,k-1,c) & - & +src(i ,j-1,k+1,c) & - & +src(i ,j+1,k+1,c)) & - + sixteenth *(src(i-1,j ,k ,c) & - & +src(i+1,j ,k ,c) & - & +src(i ,j-1,k ,c) & - & +src(i ,j+1,k ,c) & - & +src(i ,j ,k-1,c) & - & +src(i ,j ,k+1,c)) & - + eighth * src(i ,j ,k ,c) - end do - end do - end do - end do - end subroutine warpx_filter_3d - - subroutine warpx_filter_2d(lo, hi, src, slo, shi, dst, dlo, dhi, nc) & - bind(c, name='warpx_filter_2d') - integer, intent(in), value :: nc - integer, dimension(2), intent(in) :: lo, hi, dlo, dhi, slo, shi - real(rt), intent(inout) :: dst(dlo(1):dhi(1), dlo(2):dhi(2), nc) - real(rt), intent(in ) :: src(slo(1):shi(1), slo(2):shi(2), nc) - - real(rt), parameter :: sixteenth = 1.d0/16.d0 - integer :: i,j,comp - - do comp = 1, nc - do j = lo(2), hi(2) - do i = lo(1), hi(1) - dst(i, j, comp) = sixteenth*(src(i-1,j-1,comp) & - & +src(i+1,j-1,comp) & - & +src(i-1,j+1,comp) & - & +src(i+1,j+1,comp)) & - + eighth *(src(i ,j-1,comp) & - & +src(i ,j+1,comp) & - & +src(i-1,j ,comp) & - & +src(i+1,j ,comp)) & - + fourth * src(i ,j ,comp) - end do - end do - end do - end subroutine warpx_filter_2d - -end module warpx_filter_module diff --git a/Source/FortranInterface/WarpX_f.H b/Source/FortranInterface/WarpX_f.H index 33f7f9639..d0fa712f8 100644 --- a/Source/FortranInterface/WarpX_f.H +++ b/Source/FortranInterface/WarpX_f.H @@ -40,7 +40,6 @@ #define WRPX_PUSH_LEAPFROG_POSITIONS warpx_push_leapfrog_positions_3d #define WRPX_LORENTZ_TRANSFORM_Z warpx_lorentz_transform_z -#define WRPX_FILTER warpx_filter_3d #define WRPX_COPY_SLICE warpx_copy_slice_3d #define WRPX_PXR_NCI_CORR_INIT init_godfrey_filter_coeffs #define WRPX_PXR_GODFREY_FILTER apply_godfrey_filter_z_3d @@ -71,7 +70,6 @@ #define WRPX_PUSH_LEAPFROG_POSITIONS warpx_push_leapfrog_positions_2d #define WRPX_LORENTZ_TRANSFORM_Z warpx_lorentz_transform_z -#define WRPX_FILTER warpx_filter_2d #define WRPX_COPY_SLICE warpx_copy_slice_2d #define WRPX_PXR_NCI_CORR_INIT init_godfrey_filter_coeffs #define WRPX_PXR_GODFREY_FILTER apply_godfrey_filter_z_2d @@ -187,8 +185,6 @@ extern "C" amrex::Real* Xp, amrex::Real* Yp, amrex::Real* t, amrex::Real* wavelength, amrex::Real* e_max, amrex::Real* waist, amrex::Real* duration, amrex::Real* f, amrex::Real* amplitude ); - void parse_function_laser( const long* np, amrex::Real* Xp, amrex::Real* Yp, amrex::Real* t, amrex::Real* amplitude, const int parser_instance_number ); - void calculate_laser_plane_coordinates( const long* np, amrex::Real* xp, amrex::Real* yp, amrex::Real* zp, amrex::Real* plane_Xp, amrex::Real* plane_Yp, @@ -465,10 +461,6 @@ extern "C" const BL_FORT_FAB_ARG_ANYD(fine), const int* ncomp); - void WRPX_FILTER (const int* lo, const int* hi, - const amrex_real*, const int*, const int*, - amrex_real*, const int*, const int*, int); - void WRPX_PXR_NCI_CORR_INIT(amrex::Real*, amrex::Real*, const int, const amrex::Real, const int); @@ -477,11 +469,6 @@ extern "C" const amrex_real* fin, const int* ilo, const int* ihi, const amrex_real* stencil, const int* nsten); - int parser_initialize_function(const char *str_func, const int len_func, - const char *str_var, const int len_var); - - amrex::Real parser_evaluate_function(const amrex::Real*, const int, const int); - #ifdef WARPX_USE_PSATD void warpx_fft_mpi_init (int fcomm); void warpx_fft_domain_decomp (int* warpx_local_nz, int* warpx_local_z0, diff --git a/Source/Initialization/Make.package b/Source/Initialization/Make.package index 829b9a91c..b825924c6 100644 --- a/Source/Initialization/Make.package +++ b/Source/Initialization/Make.package @@ -3,7 +3,6 @@ CEXE_sources += WarpXInitData.cpp CEXE_sources += CustomMomentumProb.cpp CEXE_sources += PlasmaInjector.cpp CEXE_headers += PlasmaInjector.H -F90EXE_sources += WarpX_parser.F90 INCLUDE_LOCATIONS += $(WARPX_HOME)/Source/Initialization VPATH_LOCATIONS += $(WARPX_HOME)/Source/Initialization diff --git a/Source/Initialization/PlasmaInjector.H b/Source/Initialization/PlasmaInjector.H index bf5f10bba..f6b76e8b6 100644 --- a/Source/Initialization/PlasmaInjector.H +++ b/Source/Initialization/PlasmaInjector.H @@ -6,6 +6,7 @@ #include "AMReX_REAL.H" #include <AMReX_Vector.H> #include <WarpXConst.H> +#include <WarpXParser.H> #include "AMReX_ParmParse.H" #include "AMReX_Utility.H" @@ -67,10 +68,9 @@ public: virtual amrex::Real getDensity(amrex::Real x, amrex::Real y, amrex::Real z) const override; - UserConstants my_constants; private: std::string _parse_density_function; - int parser_instance_number=0; + WarpXParser parser_density; }; /// @@ -172,14 +172,13 @@ public: amrex::Real x, amrex::Real y, amrex::Real z) override; - UserConstants my_constants; private: std::string _parse_momentum_function_ux; std::string _parse_momentum_function_uy; std::string _parse_momentum_function_uz; - int parser_instance_number_ux=0; - int parser_instance_number_uy=0; - int parser_instance_number_uz=0; + WarpXParser parser_ux; + WarpXParser parser_uy; + WarpXParser parser_uz; }; diff --git a/Source/Initialization/PlasmaInjector.cpp b/Source/Initialization/PlasmaInjector.cpp index 2adfcbad9..0da9318de 100644 --- a/Source/Initialization/PlasmaInjector.cpp +++ b/Source/Initialization/PlasmaInjector.cpp @@ -1,6 +1,7 @@ #include "PlasmaInjector.H" #include <sstream> +#include <functional> #include <WarpXConst.H> #include <WarpX_f.H> @@ -72,19 +73,31 @@ CustomDensityProfile::CustomDensityProfile(const std::string& species_name) ParseDensityProfile::ParseDensityProfile(std::string parse_density_function) : _parse_density_function(parse_density_function) { - my_constants.ReadParameters(); - parse_density_function = my_constants.replaceStringValue(parse_density_function); - const std::string s_var = "x,y,z"; - parser_instance_number = parser_initialize_function(parse_density_function.c_str(), - parse_density_function.length(), - s_var.c_str(), - s_var.length()); + parser_density.define(parse_density_function); + parser_density.registerVariables({"x","y","z"}); + + ParmParse pp("my_constants"); + std::set<std::string> symbols = parser_density.symbols(); + symbols.erase("x"); + symbols.erase("y"); + symbols.erase("z"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser_density.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("ParseDensityProfile: Unknown symbol "+s); + } } Real ParseDensityProfile::getDensity(Real x, Real y, Real z) const { - std::array<amrex::Real, 3> list_var = {x,y,z}; - return parser_evaluate_function(list_var.data(), 3, parser_instance_number); + return parser_density.eval(x,y,z); } ConstantMomentumDistribution::ConstantMomentumDistribution(Real ux, @@ -141,31 +154,39 @@ ParseMomentumFunction::ParseMomentumFunction(std::string parse_momentum_function _parse_momentum_function_uy(parse_momentum_function_uy), _parse_momentum_function_uz(parse_momentum_function_uz) { - const std::string s_var = "x,y,z"; - my_constants.ReadParameters(); - parse_momentum_function_ux = my_constants.replaceStringValue(parse_momentum_function_ux); - parse_momentum_function_uy = my_constants.replaceStringValue(parse_momentum_function_uy); - parse_momentum_function_uz = my_constants.replaceStringValue(parse_momentum_function_uz); - parser_instance_number_ux = parser_initialize_function(parse_momentum_function_ux.c_str(), - parse_momentum_function_ux.length(), - s_var.c_str(), - s_var.length()); - parser_instance_number_uy = parser_initialize_function(parse_momentum_function_uy.c_str(), - parse_momentum_function_uy.length(), - s_var.c_str(), - s_var.length()); - parser_instance_number_uz = parser_initialize_function(parse_momentum_function_uz.c_str(), - parse_momentum_function_uz.length(), - s_var.c_str(), - s_var.length()); + parser_ux.define(parse_momentum_function_ux); + parser_uy.define(parse_momentum_function_uy); + parser_uz.define(parse_momentum_function_uz); + + amrex::Array<std::reference_wrapper<WarpXParser>,3> parsers{parser_ux, parser_uy, parser_uz}; + ParmParse pp("my_constants"); + for (auto& p : parsers) { + auto& parser = p.get(); + parser.registerVariables({"x","y","z"}); + std::set<std::string> symbols = parser.symbols(); + symbols.erase("x"); + symbols.erase("y"); + symbols.erase("z"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("ParseMomentumFunction: Unknown symbol "+s); + } + } } void ParseMomentumFunction::getMomentum(vec3& u, Real x, Real y, Real z) { - std::array<amrex::Real, 3> list_var = {x,y,z}; - u[0] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_ux); - u[1] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_uy); - u[2] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_uz); + u[0] = parser_ux.eval(x,y,z); + u[1] = parser_uy.eval(x,y,z); + u[2] = parser_uz.eval(x,y,z); } RandomPosition::RandomPosition(int num_particles_per_cell): @@ -313,8 +334,6 @@ void PlasmaInjector::parseDensity(ParmParse pp){ } else if (rho_prof_s == "custom") { rho_prof.reset(new CustomDensityProfile(species_name)); } else if (rho_prof_s == "parse_density_function") { - // Serialize particle initialization - WarpX::serialize_ics = true; pp.get("density_function(x,y,z)", str_density_function); rho_prof.reset(new ParseDensityProfile(str_density_function)); } else { @@ -360,8 +379,6 @@ void PlasmaInjector::parseMomentum(ParmParse pp){ pp.query("u_over_r", u_over_r); mom_dist.reset(new RadialExpansionMomentumDistribution(u_over_r)); } else if (mom_dist_s == "parse_momentum_function") { - // Serialize particle initialization - WarpX::serialize_ics = true; pp.get("momentum_function_ux(x,y,z)", str_momentum_function_ux); pp.get("momentum_function_uy(x,y,z)", str_momentum_function_uy); pp.get("momentum_function_uz(x,y,z)", str_momentum_function_uz); diff --git a/Source/Initialization/WarpXInitData.cpp b/Source/Initialization/WarpXInitData.cpp index ff5442b00..23637ec97 100644 --- a/Source/Initialization/WarpXInitData.cpp +++ b/Source/Initialization/WarpXInitData.cpp @@ -6,6 +6,7 @@ #include <WarpX.H> #include <WarpX_f.H> +#include <BilinearFilter.H> #ifdef BL_USE_SENSEI_INSITU #include <AMReX_AmrMeshInSituBridge.H> @@ -21,7 +22,7 @@ WarpX::InitData () if (restart_chkfile.empty()) { ComputeDt(); - InitFromScratch(); + InitFromScratch(); } else { @@ -38,6 +39,10 @@ WarpX::InitData () WarpX::InitNCICorrector(); } + if (WarpX::use_filter) { + WarpX::InitFilter(); + } + BuildBufferMasks(); InitDiagnostics(); @@ -92,7 +97,7 @@ WarpX::InitDiagnostics () { moving_window_v, dt_snapshots_lab, num_snapshots_lab, gamma_boost, t_new[0], dt_boost, - moving_window_dir)); + moving_window_dir, geom[0])); } } @@ -178,6 +183,14 @@ WarpX::InitNCICorrector () } void +WarpX::InitFilter (){ + if (WarpX::use_filter){ + WarpX::bilinear_filter.npass_each_dir = WarpX::filter_npass_each_dir; + WarpX::bilinear_filter.ComputeStencils(); + } +} + +void WarpX::PostRestart () { #ifdef WARPX_USE_PSATD diff --git a/Source/Initialization/WarpX_parser.F90 b/Source/Initialization/WarpX_parser.F90 deleted file mode 100644 index 4b95c5ac7..000000000 --- a/Source/Initialization/WarpX_parser.F90 +++ /dev/null @@ -1,1365 +0,0 @@ -! MODULE mod_interpret -! Parser to read and evaluate math expressions - -module mod_interpret -use iso_c_binding -use amrex_fort_module, only : amrex_real -use amrex_error_module, only : amrex_error -implicit none -INTEGER, parameter :: plus =1, & - minus =2, & - multiply =3, & - divide =4, & - power =5, & - greaterthan=6, & - lessthan =7, & - exponential=8, & - logarithm =9, & - sine =10, & - cosine =11, & - tangent =12, & - square_root=13, & - arccos =14, & - arcsin =15, & - arctan =16, & - sinhyp =17, & - coshyp =18, & - tanhyp =19, & - logten =20 - -CHARACTER(5), DIMENSION(0:20) :: coper -INTEGER, parameter :: w_parenthesis = 1, & - w_number = 2, & - w_operator = 3, & - w_intrinsic = 4, & - w_variable = 5 -TYPE :: operation_type - INTEGER :: a,b,op -END TYPE operation_type - -TYPE :: res_type - INTEGER :: nb_op - REAL(amrex_real) :: value - TYPE(res_type), DIMENSION(:), POINTER :: res - LOGICAL, DIMENSION(:), POINTER :: l_res, l_res_do - TYPE(operation_type), DIMENSION(:), POINTER :: operation - LOGICAL :: a_res,a_l_res,a_l_res_do,a_operation -END TYPE res_type - -TYPE :: res_type_array - INTEGER :: nb_op - REAL(amrex_real), DIMENSION(:), POINTER :: value - TYPE(res_type_array), DIMENSION(:), POINTER :: res - LOGICAL, DIMENSION(:), POINTER :: l_res, l_res_do - TYPE(operation_type), DIMENSION(:), POINTER :: operation - LOGICAL :: a_res,a_l_res,a_l_res_do,a_operation -END TYPE res_type_array - -TYPE(res_type_array) :: res_array - -INTEGER :: nb_res=0 -TYPE(res_type), DIMENSION(10) :: table_of_res - -contains - -RECURSIVE subroutine del_res(resin) -implicit none -TYPE(res_type), INTENT(INOUT) :: resin -INTEGER :: j - IF(resin%a_res) then - do j = LBOUND(resin%res,1),UBOUND(resin%res,1) - call del_res(resin%res(j)) - end do - DEALLOCATE(resin%res) - resin%a_res=.FALSE. - END if - IF(resin%a_l_res) then - DEALLOCATE(resin%l_res) - resin%a_l_res=.FALSE. - END if - IF(resin%a_l_res_do) then - DEALLOCATE(resin%l_res_do) - resin%a_l_res_do=.FALSE. - END if - IF(resin%a_operation) then - DEALLOCATE(resin%operation) - resin%a_operation=.FALSE. - END if -return -end subroutine del_res - -RECURSIVE subroutine del_res_array(resin) -implicit none -TYPE(res_type_array), INTENT(INOUT) :: resin -INTEGER :: j - IF(resin%a_res) then - do j = LBOUND(resin%res,1),UBOUND(resin%res,1) - call del_res_array(resin%res(j)) - end do - DEALLOCATE(resin%res) - resin%a_res=.FALSE. - END if - IF(resin%a_l_res) then - DEALLOCATE(resin%l_res) - resin%a_l_res=.FALSE. - END if - IF(resin%a_l_res_do) then - DEALLOCATE(resin%l_res_do) - resin%a_l_res_do=.FALSE. - END if - IF(resin%a_operation) then - DEALLOCATE(resin%operation) - resin%a_operation=.FALSE. - END if - if (associated(resin%value)) then - nullify(resin%value) - end if -return -end subroutine del_res_array - -subroutine init_interpret() -implicit none - -coper(0 )='###' -coper(plus )='+' -coper(minus )='-' -coper(multiply )='*' -coper(divide )='/' -coper(power )='**' -coper(square_root)='sqrt' -coper(exponential)='exp' -coper(logarithm )='log' -coper(sine )='sin' -coper(cosine )='cos' -coper(tangent )='tan' -coper(arccos )='acos' -coper(arcsin )='asin' -coper(arctan )='atan' -coper(sinhyp )='sinh' -coper(coshyp )='cosh' -coper(tanhyp )='tanh' -coper(logten )='log10' -coper(greaterthan)='>' -coper(lessthan )='<' - -return -end subroutine init_interpret - -recursive function calc_res(res,list_var,root) RESULT(calc_res_r) -implicit none -REAL(amrex_real) :: calc_res_r -TYPE(res_type), INTENT(IN OUT) :: res -REAL(amrex_real), DIMENSION(:), OPTIONAL :: list_var -LOGICAL, INTENT(IN), OPTIONAL :: root - -INTEGER :: j,ia,ib,op -REAL(amrex_real) :: a,b - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op<0) then - IF(op>-100) res%res(ia)%value=list_var(-op) - else - IF(res%l_res(ia).and.res%l_res_do(ia)) then - IF(PRESENT(list_var)) then - a = calc_res(res=res%res(ia),list_var=list_var,root=.false.) - else - a = calc_res(res=res%res(ia),root=.false.) - END if - res%l_res_do(ia)=.false. - else - a = res%res(ia)%value - END if - IF(res%l_res(ib).and.res%l_res_do(ib)) then - IF(PRESENT(list_var)) then - b = calc_res(res=res%res(ib),list_var=list_var,root=.false.) - else - b = calc_res(res=res%res(ib),root=.false.) - END if - ! This might prevent OMP threading - res%l_res_do(ib)=.false. - else - b = res%res(ib)%value - END if - ! This might prevent OMP threading - res%res(ia)%value=eval(a,b,op) - END if -end do -IF(res%nb_op==0) then - ia=1 - calc_res_r = res%res(ia)%value -else - calc_res_r = res%res(ia)%value -END if - -IF(.not.PRESENT(root)) call calc_res_init(res) - -end function calc_res - -recursive subroutine calc_res_init(res) -implicit none -TYPE(res_type), INTENT(IN OUT) :: res - -INTEGER :: j,ia,ib,op - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op>=0) then - IF(res%l_res(ia).and..not.res%l_res_do(ia)) then - call calc_res_init(res%res(ia)) - res%l_res_do(ia)=.true. - END if - IF(res%l_res(ib).and..not.res%l_res_do(ib)) then - call calc_res_init(res%res(ib)) - res%l_res_do(ib)=.true. - END if - END if -end do - -end subroutine calc_res_init - -recursive function calc_res_array(res,list_values,root) RESULT(calc_res_array_r) -implicit none -REAL(amrex_real), dimension(:), pointer :: calc_res_array_r -TYPE(res_type_array), INTENT(IN OUT) :: res -REAL(amrex_real), DIMENSION(:,:), OPTIONAL :: list_values -LOGICAL, INTENT(IN), OPTIONAL :: root - -INTEGER :: j,ia,ib,op,n -REAL(amrex_real),dimension(:),pointer :: a,b - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op<0) then - IF(op>-100) then - n = size(list_values,2) - allocate(res%res(ia)%value(n)) - res%res(ia)%value=list_values(-op,:) - end if - else - IF(res%l_res(ia).and.res%l_res_do(ia)) then - IF(PRESENT(list_values)) then - a => calc_res_array(res=res%res(ia),list_values=list_values,root=.false.) - else - a => calc_res_array(res=res%res(ia),root=.false.) - END if - res%l_res_do(ia)=.false. - else - a => res%res(ia)%value - END if - IF(res%l_res(ib).and.res%l_res_do(ib)) then - IF(PRESENT(list_values)) then - b => calc_res_array(res=res%res(ib),list_values=list_values,root=.false.) - else - b => calc_res_array(res=res%res(ib),root=.false.) - END if - res%l_res_do(ib)=.false. - else - b => res%res(ib)%value - END if - res%res(ia)%value=>eval_array(a,b,op) - END if -end do - -IF(res%nb_op==0) then - ia=1 - calc_res_array_r => res%res(ia)%value -else - calc_res_array_r => res%res(ia)%value -END if - -IF(.not.PRESENT(root)) call calc_res_array_init(res) - -end function calc_res_array - -recursive subroutine calc_res_array_init(res) -implicit none -TYPE(res_type_array), INTENT(IN OUT) :: res - -INTEGER :: j,ia,ib,op - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op>=0) then - IF(res%l_res(ia).and..not.res%l_res_do(ia)) then - call calc_res_array_init(res%res(ia)) - res%l_res_do(ia)=.true. - END if - IF(res%l_res(ib).and..not.res%l_res_do(ib)) then - call calc_res_array_init(res%res(ib)) - res%l_res_do(ib)=.true. - END if - END if -end do - -end subroutine calc_res_array_init - -recursive subroutine eval_res(res,exprin,int_op,list_var,l_root) -implicit none -TYPE(res_type) :: res -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, OPTIONAL, INTENT(IN) :: int_op -CHARACTER(*), DIMENSION(:),INTENT(IN), OPTIONAL :: list_var -LOGICAL, OPTIONAL :: l_root - -CHARACTER(LEN=LEN(exprin)) :: expr_old -CHARACTER(LEN=LEN(exprin)+2) :: expr -INTEGER, parameter :: nop_tot=25 -INTEGER :: ln, i, j,ja -INTEGER :: jtot,jres,jop -INTEGER, DIMENSION(0:nop_tot) :: next_l,next_r,op,what -LOGICAL, ALLOCATABLE, DIMENSION(:) :: l_res - -IF(.not.PRESENT(l_root)) call del_res(res) - -res%a_res=.false. -res%a_l_res=.false. -res%a_l_res_do=.false. -res%a_operation=.false. - -op=0 -what=0 - -expr_old = TRIM(ADJUSTL(C_replace(exprin,' ',''))) -IF(expr_old(1:1)=='-') then - expr='0'//expr_old -else - expr='0+'//expr_old -END if -ln = LEN(TRIM(ADJUSTL(expr))) -next_r(0) = 0 -i = 1 -j = 1 -jres = 0 -do WHILE(i<ln+1) - IF(PRESENT(list_var)) then - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j),list_var) - else - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j)) - END if - next_r(j) = next_r(j)+next_r(j-1) - next_l(j) = next_r(j-1)+1+next_l(j) - i = next_r(j)+1 - IF(what(j)/=w_operator) jres=jres+1 - j = j+1 -end do -jtot = j-1 -IF(PRESENT(int_op)) then - ALLOCATE(res%res(0:jres),res%operation(jres),res%l_res(0:jres),res%l_res_do(0:jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(0:jres)%a_res=.false. - res%res(0:jres)%a_l_res=.false. - res%res(0:jres)%a_l_res_do=.false. - res%res(0:jres)%a_operation=.false. - res%l_res(0)=.false. - res%l_res_do=.true. - res%res(0)%value=0. - res%nb_op=jres - res%operation(jres)%op=int_op - res%operation(jres)%a=1 - res%operation(jres)%b=0 - res%l_res(jres)=.false. - nullify(res%res(0)%res) - nullify(res%res(0)%l_res) - nullify(res%res(0)%l_res_do) - nullify(res%res(0)%operation) -else - ALLOCATE(res%res(jres),res%operation(jres-1),res%l_res(jres),res%l_res_do(jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(1:jres)%a_res=.false. - res%res(1:jres)%a_l_res=.false. - res%res(1:jres)%a_l_res_do=.false. - res%res(1:jres)%a_operation=.false. - res%nb_op=jres-1 - res%l_res_do=.true. -END if -ALLOCATE(l_res(jres)) -l_res(:) = .true. - -res%res(:)%value=0. - -jres = 0 -do j=1, jtot - select case (what(j)) - case (w_number) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -100-op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - res%res(jres)%res(1)%value = char2real(expr(next_l(j):next_r(j))) - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_parenthesis) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),list_var=list_var,l_root=.false.) - else - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),l_root=.false.) - endif - res%l_res(jres)=.true. - case (w_intrinsic) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),list_var,l_root=.false.) - else - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),l_root=.false.) - END if - res%l_res(jres)=.true. - case (w_variable) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_operator) - case default - end select -end do -jop = 1 -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==power) then - res%operation(jop)%op = power - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END IF - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==divide) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==multiply) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==plus.or.op(j)==minus) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==greaterthan.or.op(j)==lessthan) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do - -DEALLOCATE(l_res) - -return -end subroutine eval_res - -recursive subroutine eval_res_array(res,exprin,int_op,list_var,l_root) -implicit none -TYPE(res_type_array) :: res -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, OPTIONAL, INTENT(IN) :: int_op -CHARACTER(*), DIMENSION(:),INTENT(IN), OPTIONAL :: list_var -LOGICAL, OPTIONAL :: l_root - -CHARACTER(LEN=LEN(exprin)) :: expr_old -CHARACTER(LEN=LEN(exprin)+2) :: expr -INTEGER, parameter :: nop_tot=25 -INTEGER :: ln, i, j,ja -INTEGER :: jtot,jres,jop -INTEGER, DIMENSION(0:nop_tot) :: next_l,next_r,op,what -LOGICAL, ALLOCATABLE, DIMENSION(:) :: l_res - -IF(.not.PRESENT(l_root)) call del_res_array(res) - -res%a_res=.false. -res%a_l_res=.false. -res%a_l_res_do=.false. -res%a_operation=.false. - -op=0 -what=0 - -expr_old = TRIM(ADJUSTL(C_replace(exprin,' ',''))) -IF(expr_old(1:1)=='-') then - expr='0'//expr_old -else - expr='0+'//expr_old -END if -ln = LEN(TRIM(ADJUSTL(expr))) -next_r(0) = 0 -i = 1 -j = 1 -jres = 0 -do WHILE(i<ln+1) - IF(PRESENT(list_var)) then - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j),list_var) - else - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j)) - END if - next_r(j) = next_r(j)+next_r(j-1) - next_l(j) = next_r(j-1)+1+next_l(j) - i = next_r(j)+1 - IF(what(j)/=w_operator) jres=jres+1 - j = j+1 -end do -jtot = j-1 -IF(PRESENT(int_op)) then - ALLOCATE(res%res(0:jres),res%operation(jres),res%l_res(0:jres),res%l_res_do(0:jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(0:jres)%a_res=.false. - res%res(0:jres)%a_l_res=.false. - res%res(0:jres)%a_l_res_do=.false. - res%res(0:jres)%a_operation=.false. - res%l_res(0)=.false. - res%l_res_do=.true. - nullify(res%res(0)%value) - res%nb_op=jres - res%operation(jres)%op=int_op - res%operation(jres)%a=1 - res%operation(jres)%b=0 - res%l_res(jres)=.false. - nullify(res%res(0)%res) - nullify(res%res(0)%l_res) - nullify(res%res(0)%l_res_do) - nullify(res%res(0)%operation) -else - ALLOCATE(res%res(jres),res%operation(jres-1),res%l_res(jres),res%l_res_do(jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(1:jres)%a_res=.false. - res%res(1:jres)%a_l_res=.false. - res%res(1:jres)%a_l_res_do=.false. - res%res(1:jres)%a_operation=.false. - res%nb_op=jres-1 - res%l_res_do=.true. -END if -ALLOCATE(l_res(jres)) -l_res(:) = .true. - -jres = 0 -do j=1, jtot - select case (what(j)) - case (w_number) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -100-op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - allocate(res%res(jres)%res(1)%value(1)) - res%res(jres)%res(1)%value = char2real(expr(next_l(j):next_r(j))) - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_parenthesis) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),list_var=list_var,l_root=.false.) - else - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),l_root=.false.) - endif - res%l_res(jres)=.true. - case (w_intrinsic) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),list_var,l_root=.false.) - else - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),l_root=.false.) - END if - res%l_res(jres)=.true. - case (w_variable) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_operator) - case default - end select -end do -jop = 1 -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==power) then - res%operation(jop)%op = power - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END IF - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==divide) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==multiply) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==plus.or.op(j)==minus) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==greaterthan.or.op(j)==lessthan) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do - -DEALLOCATE(l_res) - -return -end subroutine eval_res_array - -subroutine what_is_next(exprin,what,next_l,next_r,op,list_var) -implicit none -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, INTENT(OUT) :: what,next_l,next_r,op -CHARACTER(*), DIMENSION(:),INTENT(IN), optional :: list_var - -CHARACTER(LEN=LEN(exprin)) :: expr - -INTEGER :: ln, i, asc, istart,ln2,ierror,jv - -ln = LEN_TRIM(ADJUSTL(exprin)) -expr = TRIM(ADJUSTL(exprin)) -istart = LEN(exprin)-LEN(ADJUSTL(exprin)) -next_l = istart - -i = 1 -asc = IACHAR(expr(1:1)) -select case (asc) - ! parenthesis ( - case (40) - what = w_parenthesis - next_r = find_close_bracket(expr(i:ln)) - ! alphabet - case (65:90,97:122) - ln2 = ln-i+1 - op = find_intrinsic(expr(i:ln),ln2,ierror) - IF(ierror==0) then - what = w_intrinsic - IF( expr(i+ln2:i+ln2) .ne.'(' ) then - WRITE(*,*) 'Error in intrinsic ',exprin,': missing parenthesis.' - stop - END if - next_l = next_l+ln2 - next_r = ln2+find_close_bracket(expr(i+ln2:ln)) - ELSEIF(PRESENT(list_var)) then - what = w_variable - jv = 0 - op = 0 - do WHILE(jv+1<=SIZE(list_var)) - jv=jv+1 - IF(expr(i:i+ln2-1)==TRIM(list_var(jv)(:))) then - op=jv - END if - end do - IF(op==0) then - WRITE(*,*) 'Error: variable ',exprin(i:i+ln2-1),' not allowed.' - stop - END if - next_l = next_l+ln2 - next_r = ln2 - else - WRITE(*,*) 'Error: intrinsic ',exprin(i:i+ln2-1),' does not exists.' - stop - END if - ! number - case (48:57) - what = w_number - next_r = next_l+find_number_end(expr(i:ln)) - ! operator - case (42,43,45,47,60,62) - what = w_operator - op = find_operator(expr(i:i+1)) - IF(op==power) THEN - next_r = next_l+2 - else - next_r = next_l+1 - END if - case default -end select - -return -end subroutine what_is_next - -function find_close_bracket(a) -implicit none -INTEGER :: find_close_bracket -CHARACTER(*), INTENT(IN) :: a -INTEGER :: level, i, ln -level = 0 -ln=LEN(a) -do i = 1, ln - select case (a(i:i)) - case ('(') - level=level+1 - case (')') - level=level-1 - case default - end select - IF(level==0) exit -end do -IF(i>ln) then - WRITE(*,*) 'Error, no closing bracket in expression ',a - stop -END if - -find_close_bracket = i - -return -end function find_close_bracket - -function find_intrinsic(a,ln,ierror) -implicit none -INTEGER :: find_intrinsic -CHARACTER(*), INTENT(IN) :: a -CHARACTER(LEN=LEN(a)) :: ac -INTEGER, INTENT(IN OUT) :: ln -INTEGER, INTENT(OUT) :: ierror -INTEGER :: i - -ierror=0 -do i = 1, ln - select case (IACHAR(a(i:i))) - case (65:90,97:122) ! alphabet - case default - exit - end select -end do -IF(i>ln+1) then - WRITE(*,*) 'Error in expression ',a - stop -END if - -ln = i-1 -ac(1:ln) = a(1:ln) - -do i = 1, ln - IF(IACHAR(a(i:i))<97) ac(i:i)=ACHAR(IACHAR(a(i:i))+32) -END do - -select case (ac(1:ln)) - case ('sqrt') - find_intrinsic = square_root - case ('sin') - find_intrinsic = sine - case ('cos') - find_intrinsic = cosine - case ('tan') - find_intrinsic = tangent - case ('exp') - find_intrinsic = exponential - case ('log','ln') - find_intrinsic = logarithm - case ('log10') - find_intrinsic = logten - case ('asin') - find_intrinsic = arcsin - case ('acos') - find_intrinsic = arccos - case ('atan') - find_intrinsic = arctan - case ('sinh') - find_intrinsic = sinhyp - case ('cosh') - find_intrinsic = coshyp - case ('tanh') - find_intrinsic = tanhyp - case default - ierror=1 -end select -return -end function find_intrinsic - -function find_operator(a) -implicit none -INTEGER :: find_operator -CHARACTER(*), INTENT(IN) :: a - - select case (IACHAR(a(1:1))) - case (42) ! '*' - IF(IACHAR(a(2:2))==42) then - find_operator = power - else - find_operator = multiply - END if - case (43) ! '+' - find_operator = plus - case (45) ! '-' - find_operator = minus - case (47) ! '/' - find_operator = divide - case (60) ! '<' - find_operator = lessthan - case (62) ! '>' - find_operator = greaterthan - case default - WRITE(*,*) 'Error, operator ',a(1:1),' does not exist' - stop - end select - - return -end function find_operator - -function find_number_end(a) -implicit none -INTEGER :: find_number_end -CHARACTER(*), INTENT(IN) :: a -INTEGER :: i -LOGICAL :: l_point,l_minus,l_e - -l_point=.FALSE. -l_minus=.FALSE. -l_e=.FALSE. - -do i = 1, len(a) - select case (IACHAR(a(i:i))) - case (48:57) - l_minus = .true. - case (46) ! . - IF(l_point) exit - l_point = .true. - l_minus = .true. - case (45) ! - - IF(l_minus) exit - l_minus = .true. - case (69,101) ! e - IF(l_e) exit - l_e = .true. - l_minus = .false. - case default - exit - end select -end do - -find_number_end = i-1 - -return -end function find_number_end - -function char2real(a) -implicit none -REAL(amrex_real) :: char2real -CHARACTER(*), INTENT(IN) :: a - -READ(a(:),*) char2real - -return -end function char2real - -FUNCTION eval(a,b,op) -implicit none -REAL(amrex_real) :: eval -REAL(amrex_real), INTENT(IN) :: a, b -INTEGER, INTENT(IN) :: op - -select case (op) - case (plus) - eval=a+b - case (minus) - eval=a-b - case (multiply) - eval=a*b - case (divide) - eval=a/b - case (power) - eval=a**b - case (greaterthan) - eval=0. - if (a > b) eval=1. - case (lessthan) - eval=0. - if (a < b) eval=1. - case (square_root) - eval=SQRT(a) - case (exponential) - eval=EXP(a) - case (logarithm) - eval=LOG(a) - case (sine) - eval=SIN(a) - case (cosine) - eval=COS(a) - case (tangent) - eval=TAN(a) - case (logten) - eval=log10(a) - case (arcsin) - eval=asin(a) - case (arccos) - eval=acos(a) - case (arctan) - eval=atan(a) - case (sinhyp) - eval=sinh(a) - case (coshyp) - eval=cosh(a) - case (tanhyp) - eval=tanh(a) - case default - WRITE(*,*) 'Error in eval, wrong operator ',op - stop -end select - -return -END function eval - - -FUNCTION eval_array(ai,bi,op) -implicit none -REAL(amrex_real), dimension(:), pointer :: eval_array -REAL(amrex_real), INTENT(IN), dimension(:), pointer :: ai, bi -REAL(amrex_real), dimension(:), pointer :: a, b -INTEGER, INTENT(IN) :: op -integer::i1,i2,n - -i1 = size(ai) -i2 = size(bi) -n = maxval((/i1,i2/)) - -select case (op) - case (plus,minus,multiply,divide,power, greaterthan, lessthan) - - if (i1<n) then - allocate(a(n)) - a(:) = ai(1) - else - a => ai - end if - if (i2<n) then - allocate(b(n)) - b(:) = bi(1) - else - b => bi - end if - - case default - a => ai - -end select - -allocate(eval_array(n)) -eval_array=0. - -select case (op) - case (plus) - eval_array=a+b - case (minus) - eval_array=a-b - case (multiply) - eval_array=a*b - case (divide) - eval_array=a/b - case (power) - eval_array=a**b - case (square_root) - eval_array=SQRT(a) - case (exponential) - eval_array=EXP(a) - case (logarithm) - eval_array=LOG(a) - case (sine) - eval_array=SIN(a) - case (cosine) - eval_array=COS(a) - case (tangent) - eval_array=TAN(a) - case (logten) - eval_array=log10(a) - case (arcsin) - eval_array=asin(a) - case (arccos) - eval_array=acos(a) - case (arctan) - eval_array=atan(a) - case (sinhyp) - eval_array=sinh(a) - case (coshyp) - eval_array=cosh(a) - case (tanhyp) - eval_array=tanh(a) - case default - WRITE(*,*) 'Error in eval_array, wrong operator ',op - stop -end select - -return -END function eval_array - -function C_up2low(cline) -CHARACTER(*) :: cline -CHARACTER(LEN=LEN(cline)) :: C_up2low -INTEGER :: ln, i, j - ln = LEN(cline) - do i = 1, ln - j = IACHAR(cline(i:i)) - IF(j<91.and.j>64) then - C_up2low(i:i)=ACHAR(j+32) - else - C_up2low(i:i) = cline(i:i) - END if - END do -end function C_up2low - -RECURSIVE FUNCTION C_REPLACE(ST,R1,R2) RESULT(C_RES) -IMPLICIT NONE -CHARACTER(LEN=*), INTENT(IN) :: ST -CHARACTER(LEN=LEN(st)) :: c_res -CHARACTER(LEN=*), INTENT(IN) :: R1 -CHARACTER(LEN=*), INTENT(IN) :: R2 - -INTEGER :: i,ll - - c_res = ST - i=INDEX(TRIM(st),r1) - IF(i==0) return - ll = LEN(r2) - IF(ll>0) c_res(i:i+ll-1) = r2 - c_res(i+ll:) = c_replace(c_res(i+LEN(r1):),r1,r2) - - -RETURN -END FUNCTION C_REPLACE - -FUNCTION C_nboccur(ST,R1) -IMPLICIT NONE -CHARACTER(LEN=*), INTENT(IN) :: ST -integer :: c_nboccur -CHARACTER(LEN=*), INTENT(IN) :: R1 - -INTEGER :: i,is - - C_nboccur=0 - is=0 -10 i = INDEX(st(is+1:),r1) - IF(i/=0) then - C_nboccur = C_nboccur+1 - is=is+i+LEN(r1) - GO TO 10 - END if - -RETURN -END FUNCTION C_nboccur - -function stringlist(strin) RESULT(res) -CHARACTER(*), INTENT(IN) :: strin -INTEGER :: i1, nwords, i, i2 - -CHARACTER(LEN(strin)+1) :: str -CHARACTER(80), DIMENSION(:), pointer :: res - -str=ADJUSTL(strin)//' ' -i1=1 -nwords = 0 -do WHILE(TRIM(str(i1:))/='') - i2 = 1 - i2 = INDEX(str(i1:),' ') - do WHILE(i2==1) - i1 = i1 + 1 - i2 = INDEX(str(i1:),' ') - end do - i1=i1+i2 - nwords = nwords + 1 -end do -ALLOCATE(res(nwords)) -res(:) = '' -i1=1 -do i = 1, nwords - i2 = INDEX(str(i1:),' ') - do WHILE(i2==1) - i1 = i1 + 1 - i2 = INDEX(str(i1:),' ') - end do - res(i)(1:i2-1) = str(i1:i1+i2-1) - i1=i1+i2 -end do - -return -end function stringlist - -SUBROUTINE csv2list(strin, strinlen, lofstr) -CHARACTER(*), INTENT(IN) :: strin -INTEGER, INTENT(IN) :: strinlen -INTEGER :: isep, isep_rel, nwords, i, j -INTEGER, DIMENSION(strinlen) :: word_start, word_end -CHARACTER(strinlen) :: str -CHARACTER(len=10), DIMENSION(:), allocatable, INTENT(inout) :: lofstr -nwords = 1 -str = strin -isep_rel = INDEX(str, ',') -isep = isep_rel -word_start(nwords) = 1 -DO WHILE(isep_rel > 0) - word_end(nwords) = isep-1 - nwords = nwords + 1 - word_start(nwords) = isep+1 - str = strin(isep+1:strinlen) - isep_rel = INDEX(str, ',') - isep = isep_rel + isep -ENDDO -word_end(nwords) = strinlen - -ALLOCATE( lofstr(nwords) ) - -DO i=1, nwords - lofstr(i) = "" - DO j = 1, word_end(i)-word_start(i)+1 - lofstr(i)(j:j) = strin(word_start(i)+j-1:word_start(i)+j-1) - end DO -ENDDO -END SUBROUTINE csv2list -end module mod_interpret - -! --------------------- -! MODULE parser_wrapper -! Wrappers to use the parser defined in MODULE mod_interpret. -MODULE parser_wrapper -USE iso_c_binding -USE amrex_fort_module, only : amrex_real -USE amrex_error_module, only : amrex_error -USE mod_interpret, only : nb_res, table_of_res, calc_res, eval_res, csv2list - -IMPLICIT NONE - -CONTAINS - -! FUNCTION parser_initialize_function RESULT my_index_res -! Initialize a res_type and assign it a unique identifier. -! INPUTS: -!> instr_func : CHAR* for a mathematical expression -!> e.g. instr_func = "3*cos(x)+y". -!> instr_var : CHAR* for a comma-separated list of variables in instr_func -!> e.g. instr_var = "x,y". -! OUTPUT: -!> my_index_res : INT parser index. Necessary if this module is used for several -!> parsers (e.g. one for the electron density, one for the ion -!> density, one for the laser profile etc.). -FUNCTION parser_initialize_function(instr_func, length_func, instr_var, length_var) RESULT(my_index_res) & - bind(c,name='parser_initialize_function') - INTEGER(c_int), INTENT(IN), VALUE :: length_func, length_var - CHARACTER(kind=c_char), INTENT(IN) :: instr_func(length_func) - CHARACTER(kind=c_char), INTENT(IN) :: instr_var(length_var) - INTEGER :: i - CHARACTER(len=10), DIMENSION(:), allocatable :: lofstr - CHARACTER(len=length_func) :: str_func - CHARACTER(len=length_var) :: str_var - REAL(amrex_real), DIMENSION(1) :: list_var - INTEGER :: my_index_res - - nb_res = nb_res + 1 - my_index_res = nb_res - IF (nb_res>10) THEN - call amrex_error('Parser error: cannot have more than 10 parsers.') - ENDIF - - str_func = "" - DO i=1, length_func - str_func(i:i) = instr_func(i) - ENDDO - - str_var = "" - DO i=1, length_var - str_var(i:i) = instr_var(i) - ENDDO - - ! Convert variable list from csv string to list of strings. - CALL csv2list(str_var, len_trim(str_var), lofstr) - - ! Initialize the res object - CALL eval_res(table_of_res(my_index_res), str_func, list_var=lofstr) - -END FUNCTION parser_initialize_function - -! FUNCTION parser_evaluate_function RESULT out -! Evaluate parsed function -! INPUTS: -!> list_var : REAL* array of values for variables -!> e.g. list_var = (/3.14_8,2._8/). -!> nvar : INT number of variables -!> e.g. nvar = 2. -!> my_index_res : INT index of the res_type object in table table_of_res. -! OUTPUT: -!> out : REAL Result. -FUNCTION parser_evaluate_function(list_var, nvar, my_index_res) result(out) & - bind(c,name='parser_evaluate_function') - INTEGER, VALUE, INTENT(IN) :: nvar, my_index_res - REAL(amrex_real), INTENT(IN) :: list_var(1:nvar) - REAL(amrex_real) :: out - ! Evaluate parsed function in table_of_res(my_index_res), of type res_type - out = calc_res(table_of_res(my_index_res),list_var) - -END FUNCTION parser_evaluate_function - -END MODULE parser_wrapper diff --git a/Source/Laser/LaserParticleContainer.H b/Source/Laser/LaserParticleContainer.H index 77a52dc26..624e795e5 100644 --- a/Source/Laser/LaserParticleContainer.H +++ b/Source/Laser/LaserParticleContainer.H @@ -5,6 +5,7 @@ #include <WarpXParticleContainer.H> #include <WarpXConst.H> +#include <WarpXParser.H> enum class laser_t { Null, Gaussian, Harris, parse_field_function }; @@ -43,8 +44,6 @@ public: virtual void PostRestart () final; - UserConstants my_constants; - private: // runtime paramters @@ -77,7 +76,7 @@ private: amrex::Real theta_stc = 0.; // parse_field_function profile - int parser_instance_number = 0; + WarpXParser parser; std::string field_function; // laser particle domain diff --git a/Source/Laser/LaserParticleContainer.cpp b/Source/Laser/LaserParticleContainer.cpp index 08d0ae861..db5499b8e 100644 --- a/Source/Laser/LaserParticleContainer.cpp +++ b/Source/Laser/LaserParticleContainer.cpp @@ -64,46 +64,57 @@ LaserParticleContainer::LaserParticleContainer (AmrCore* amr_core, int ispecies) pp.query("phi2", phi2); } - if ( profile == laser_t::Harris ) { - // Parse the properties of the Harris profile - pp.get("profile_waist", profile_waist); - pp.get("profile_duration", profile_duration); - pp.get("profile_focal_distance", profile_focal_distance); - } - - if ( profile == laser_t::parse_field_function ) { - // Parse the properties of the parse_field_function profile - pp.get("field_function(X,Y,t)", field_function); - // User-defined constants: replace names by value - my_constants.ReadParameters(); - field_function = my_constants.replaceStringValue(field_function); - // Pass math expression and list of variables to Fortran as char* - const std::string s_var = "X,Y,t"; - parser_instance_number = parser_initialize_function(field_function.c_str(), - field_function.length(), - s_var.c_str(), - s_var.length()); - } + if ( profile == laser_t::Harris ) { + // Parse the properties of the Harris profile + pp.get("profile_waist", profile_waist); + pp.get("profile_duration", profile_duration); + pp.get("profile_focal_distance", profile_focal_distance); + } + + if ( profile == laser_t::parse_field_function ) { + // Parse the properties of the parse_field_function profile + pp.get("field_function(X,Y,t)", field_function); + parser.define(field_function); + parser.registerVariables({"X","Y","t"}); + + ParmParse pp("my_constants"); + std::set<std::string> symbols = parser.symbols(); + symbols.erase("X"); + symbols.erase("Y"); + symbols.erase("t"); // after removing variables, we are left with constants + for (auto it = symbols.begin(); it != symbols.end(); ) { + Real v; + if (pp.query(it->c_str(), v)) { + parser.setConstant(*it, v); + it = symbols.erase(it); + } else { + ++it; + } + } + for (auto const& s : symbols) { // make sure there no unknown symbols + amrex::Abort("Laser Profile: Unknown symbol "+s); + } + } // Plane normal Real s = 1.0/std::sqrt(nvec[0]*nvec[0] + nvec[1]*nvec[1] + nvec[2]*nvec[2]); nvec = { nvec[0]*s, nvec[1]*s, nvec[2]*s }; - if (WarpX::gamma_boost > 1.) { - // Check that the laser direction is equal to the boost direction - AMREX_ALWAYS_ASSERT_WITH_MESSAGE( - nvec[0]*WarpX::boost_direction[0] - + nvec[1]*WarpX::boost_direction[1] - + nvec[2]*WarpX::boost_direction[2] - 1. < 1.e-12, - "The Lorentz boost should be in the same direction as the laser propagation"); - // Get the position of the plane, along the boost direction, in the lab frame - // and convert the position of the antenna to the boosted frame - Z0_lab = nvec[0]*position[0] + nvec[1]*position[1] + nvec[2]*position[2]; - Real Z0_boost = Z0_lab/WarpX::gamma_boost; - position[0] += (Z0_boost-Z0_lab)*nvec[0]; - position[1] += (Z0_boost-Z0_lab)*nvec[1]; - position[2] += (Z0_boost-Z0_lab)*nvec[2]; - } + if (WarpX::gamma_boost > 1.) { + // Check that the laser direction is equal to the boost direction + AMREX_ALWAYS_ASSERT_WITH_MESSAGE( + nvec[0]*WarpX::boost_direction[0] + + nvec[1]*WarpX::boost_direction[1] + + nvec[2]*WarpX::boost_direction[2] - 1. < 1.e-12, + "The Lorentz boost should be in the same direction as the laser propagation"); + // Get the position of the plane, along the boost direction, in the lab frame + // and convert the position of the antenna to the boosted frame + Z0_lab = nvec[0]*position[0] + nvec[1]*position[1] + nvec[2]*position[2]; + Real Z0_boost = Z0_lab/WarpX::gamma_boost; + position[0] += (Z0_boost-Z0_lab)*nvec[0]; + position[1] += (Z0_boost-Z0_lab)*nvec[1]; + position[2] += (Z0_boost-Z0_lab)*nvec[2]; + } // The first polarization vector s = 1.0/std::sqrt(p_X[0]*p_X[0] + p_X[1]*p_X[1] + p_X[2]*p_X[2]); @@ -331,12 +342,7 @@ LaserParticleContainer::Evolve (int lev, int thread_num = 0; #endif - if (local_rho[thread_num] == nullptr) local_rho[thread_num].reset( new amrex::FArrayBox()); - if (local_jx[thread_num] == nullptr) local_jx[thread_num].reset( new amrex::FArrayBox()); - if (local_jy[thread_num] == nullptr) local_jy[thread_num].reset( new amrex::FArrayBox()); - if (local_jz[thread_num] == nullptr) local_jz[thread_num].reset( new amrex::FArrayBox()); - - Cuda::DeviceVector<Real> plane_Xp, plane_Yp, amplitude_E; + Cuda::ManagedDeviceVector<Real> plane_Xp, plane_Yp, amplitude_E; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) { @@ -398,8 +404,9 @@ LaserParticleContainer::Evolve (int lev, } if (profile == laser_t::parse_field_function) { - parse_function_laser( &np, plane_Xp.dataPtr(), plane_Yp.dataPtr(), &t, - amplitude_E.dataPtr(), parser_instance_number ); + for (int i = 0; i < np; ++i) { + amplitude_E[i] = parser.eval(plane_Xp[i], plane_Yp[i], t); + } } // Calculate the corresponding momentum and position for the particles update_laser_particle( diff --git a/Source/Laser/WarpX_laser.F90 b/Source/Laser/WarpX_laser.F90 index b08413019..e7a2736e1 100644 --- a/Source/Laser/WarpX_laser.F90 +++ b/Source/Laser/WarpX_laser.F90 @@ -11,7 +11,6 @@ module warpx_laser_module use iso_c_binding use amrex_fort_module, only : amrex_real use constants, only : clight, pi - use parser_wrapper, only : parser_evaluate_function implicit none @@ -186,24 +185,6 @@ contains end subroutine warpx_harris_laser #endif - ! Parse function from the input script for the laser temporal profile - subroutine parse_function_laser( np, Xp, Yp, t, amplitude, parser_instance_number ) bind(C, name="parse_function_laser") - integer(c_long), intent(in) :: np - real(amrex_real), intent(in) :: Xp(np),Yp(np) - real(amrex_real), intent(in) :: t - real(amrex_real), intent(inout) :: amplitude(np) - INTEGER, value, INTENT(IN) :: parser_instance_number - integer(c_long) :: i - INTEGER, PARAMETER :: nvar_parser = 3 - REAL(amrex_real) :: list_var(1:nvar_parser) - ! Loop through the macroparticle to calculate the proper amplitude - do i = 1, np - list_var = [Xp(i), Yp(i), t] - amplitude(i) = parser_evaluate_function(list_var, nvar_parser, parser_instance_number) - enddo - end subroutine parse_function_laser - - subroutine calculate_laser_plane_coordinates(np, xp, yp, zp, & plane_Xp, plane_Yp, u_Xx, u_Xy, u_Xz, u_Yx, u_Yy, u_Yz, & positionx, positiony, positionz ) & diff --git a/Source/Make.WarpX b/Source/Make.WarpX index 2b8319409..567dde5d1 100644 --- a/Source/Make.WarpX +++ b/Source/Make.WarpX @@ -12,6 +12,15 @@ ifeq ($(USE_GPU),TRUE) NVCC_HOST_COMP = gcc endif +ifeq ($(USE_ASCENT_INSITU),TRUE) + ASCENT_HOME ?= NOT_SET + ifneq ($(ASCENT_HOME),NOT_SET) + include $(ASCENT_HOME)/share/ascent/ascent_config.mk + endif + USE_CONDUIT = TRUE + USE_ASCENT = TRUE +endif + include $(AMREX_HOME)/Tools/GNUMake/Make.defs ifndef USE_PYTHON_MAIN @@ -40,6 +49,7 @@ include $(WARPX_HOME)/Source/FortranInterface/Make.package include $(WARPX_HOME)/Source/Initialization/Make.package include $(WARPX_HOME)/Source/Laser/Make.package include $(WARPX_HOME)/Source/Parallelization/Make.package +include $(WARPX_HOME)/Source/Parser/Make.package include $(WARPX_HOME)/Source/Particles/Make.package include $(WARPX_HOME)/Source/Python/Make.package include $(WARPX_HOME)/Source/Utils/Make.package @@ -57,6 +67,11 @@ endif include $(PICSAR_HOME)/src/Make.package +WARPX_GIT_VERSION := $(shell cd $(WARPX_HOME); git describe --abbrev=12 --dirty --always --tags) +PICSAR_GIT_VERSION := $(shell cd $(PICSAR_HOME); git describe --abbrev=12 --dirty --always --tags) +DEFINES += -DWARPX_GIT_VERSION=\"$(WARPX_GIT_VERSION)\" +DEFINES += -DPICSAR_GIT_VERSION=\"$(PICSAR_GIT_VERSION)\" + DEFINES += -DPICSAR_NO_ASSUMED_ALIGNMENT DEFINES += -DWARPX @@ -65,6 +80,20 @@ ifeq ($(USE_OPENBC_POISSON),TRUE) DEFINES += -DFFT_FFTW -DMPIPARALLEL -DUSE_OPENBC_POISSON endif +ifeq ($(USE_OPENPMD), TRUE) + OPENPMD_LIB_PATH ?= NOT_SET + ifneq ($(OPENPMD_LIB_PATH),NOT_SET) + LIBRARY_LOCATIONS += $(OPENPMD_LIB_PATH) + endif + OPENPMD_INCLUDE_PATH ?= NOT_SET + ifneq ($(OPENPMD_INCLUDE_PATH),NOT_SET) + INCLUDE_LOCATIONS += $(OPENPMD_INCLUDE_PATH) + endif + DEFINES += -DWARPX_USE_OPENPMD -DopenPMD_HAVE_MPI=1 + LIBRARIES += -lopenPMD -lhdf5 +endif + + ifeq ($(USE_PSATD),TRUE) FFTW_HOME ?= NOT_SET ifneq ($(FFTW_HOME),NOT_SET) @@ -100,6 +129,17 @@ ifeq ($(DO_ELECTROSTATIC),TRUE) DEFINES += -DWARPX_DO_ELECTROSTATIC endif +ifeq ($(USE_HDF5),TRUE) + HDF5_HOME ?= NOT_SET + ifneq ($(HDF5_HOME),NOT_SET) + VPATH_LOCATIONS += $(HDF5_HOME)/include + INCLUDE_LOCATIONS += $(HDF5_HOME)/include + LIBRARY_LOCATIONS += $(HDF5_HOME)/lib + endif + DEFINES += -DWARPX_USE_HDF5 + LIBRARIES += -lhdf5 -lz +endif + # job_info support CEXE_sources += AMReX_buildInfo.cpp CEXE_headers += $(AMREX_HOME)/Tools/C_scripts/AMReX_buildInfo.H diff --git a/Source/Parallelization/WarpXComm.cpp b/Source/Parallelization/WarpXComm.cpp index 09767c265..28971eb0c 100644 --- a/Source/Parallelization/WarpXComm.cpp +++ b/Source/Parallelization/WarpXComm.cpp @@ -1,4 +1,3 @@ - #include <WarpX.H> #include <WarpX_f.H> @@ -381,35 +380,42 @@ WarpX::SyncCurrent () if (WarpX::use_filter) { for (int lev = 0; lev <= finest_level; ++lev) { IntVect ng = current_fp[lev][0]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; for (int idim = 0; idim < 3; ++idim) { + // Create new MultiFab j_jp with enough guard cells for the + // (potentially large) stencil of the multi-pass bilinear filter. j_fp[lev][idim].reset(new MultiFab(current_fp[lev][idim]->boxArray(), current_fp[lev][idim]->DistributionMap(), 1, ng)); - applyFilter(*j_fp[lev][idim], *current_fp[lev][idim]); + // Apply the filter to current_fp, store the result in j_fp. + bilinear_filter.ApplyStencil(*j_fp[lev][idim], *current_fp[lev][idim]); + // Then swap j_fp and current_fp std::swap(j_fp[lev][idim], current_fp[lev][idim]); + // At this point, current_fp may have false values close to the + // edges of each FAB. This will be solved with a SumBoundary later. + // j_fp contains the exact MultiFab current_fp before this step. } } for (int lev = 1; lev <= finest_level; ++lev) { IntVect ng = current_cp[lev][0]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; for (int idim = 0; idim < 3; ++idim) { j_cp[lev][idim].reset(new MultiFab(current_cp[lev][idim]->boxArray(), current_cp[lev][idim]->DistributionMap(), 1, ng)); - applyFilter(*j_cp[lev][idim], *current_cp[lev][idim]); + bilinear_filter.ApplyStencil(*j_cp[lev][idim], *current_cp[lev][idim]); std::swap(j_cp[lev][idim], current_cp[lev][idim]); } } for (int lev = 1; lev <= finest_level; ++lev) { if (current_buf[lev][0]) { IntVect ng = current_buf[lev][0]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; for (int idim = 0; idim < 3; ++idim) { j_buf[lev][idim].reset(new MultiFab(current_buf[lev][idim]->boxArray(), current_buf[lev][idim]->DistributionMap(), 1, ng)); - applyFilter(*j_buf[lev][idim], *current_buf[lev][idim]); + bilinear_filter.ApplyStencil(*j_buf[lev][idim], *current_buf[lev][idim]); std::swap(*j_buf[lev][idim], *current_buf[lev][idim]); } } @@ -420,6 +426,9 @@ WarpX::SyncCurrent () for (int lev = 0; lev <= finest_level; ++lev) { const auto& period = Geom(lev).periodicity(); + // When using a bilinear filter with many passes, current_fp has + // temporarily more ghost cells here, so that its value inside + // the domain is correct at the end of this stage. current_fp[lev][0]->SumBoundary(period); current_fp[lev][1]->SumBoundary(period); current_fp[lev][2]->SumBoundary(period); @@ -461,8 +470,14 @@ WarpX::SyncCurrent () for (int lev = 0; lev <= finest_level; ++lev) { for (int idim = 0; idim < 3; ++idim) { + // swap j_fp and current_fp so that j_fp has correct values inside + // the domain and wrong number of ghost cells. + // current_fp has right number of ghost cells. std::swap(j_fp[lev][idim], current_fp[lev][idim]); + // Then copy the interior of j_fp to current_fp. MultiFab::Copy(*current_fp[lev][idim], *j_fp[lev][idim], 0, 0, 1, 0); + // current_fp has right number of ghost cells and + // correct filtered values here. } } for (int lev = 1; lev <= finest_level; ++lev) @@ -557,32 +572,32 @@ WarpX::SyncRho (amrex::Vector<std::unique_ptr<amrex::MultiFab> >& rhof, for (int lev = 0; lev <= finest_level; ++lev) { const int ncomp = rhof[lev]->nComp(); IntVect ng = rhof[lev]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; rho_f_g[lev].reset(new MultiFab(rhof[lev]->boxArray(), rhof[lev]->DistributionMap(), ncomp, ng)); - applyFilter(*rho_f_g[lev], *rhof[lev]); + bilinear_filter.ApplyStencil(*rho_f_g[lev], *rhof[lev]); std::swap(rho_f_g[lev], rhof[lev]); } for (int lev = 1; lev <= finest_level; ++lev) { const int ncomp = rhoc[lev]->nComp(); IntVect ng = rhoc[lev]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; rho_c_g[lev].reset(new MultiFab(rhoc[lev]->boxArray(), rhoc[lev]->DistributionMap(), ncomp, ng)); - applyFilter(*rho_c_g[lev], *rhoc[lev]); + bilinear_filter.ApplyStencil(*rho_c_g[lev], *rhoc[lev]); std::swap(rho_c_g[lev], rhoc[lev]); } for (int lev = 1; lev <= finest_level; ++lev) { if (charge_buf[lev]) { const int ncomp = charge_buf[lev]->nComp(); IntVect ng = charge_buf[lev]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; rho_buf_g[lev].reset(new MultiFab(charge_buf[lev]->boxArray(), charge_buf[lev]->DistributionMap(), ncomp, ng)); - applyFilter(*rho_buf_g[lev], *charge_buf[lev]); + bilinear_filter.ApplyStencil(*rho_buf_g[lev], *charge_buf[lev]); std::swap(*rho_buf_g[lev], *charge_buf[lev]); } } @@ -711,9 +726,9 @@ WarpX::ApplyFilterandSumBoundaryJ (int lev, PatchType patch_type) for (int idim = 0; idim < 3; ++idim) { if (use_filter) { IntVect ng = j[idim]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab jf(j[idim]->boxArray(), j[idim]->DistributionMap(), 1, ng); - applyFilter(jf, *j[idim]); + bilinear_filter.ApplyStencil(jf, *j[idim]); jf.SumBoundary(period); MultiFab::Copy(*j[idim], jf, 0, 0, 1, 0); } else { @@ -752,15 +767,15 @@ WarpX::AddCurrentFromFineLevelandSumBoundary (int lev) { // coarse patch of fine level IntVect ng = current_cp[lev+1][idim]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab jfc(current_cp[lev+1][idim]->boxArray(), current_cp[lev+1][idim]->DistributionMap(), 1, ng); - applyFilter(jfc, *current_cp[lev+1][idim]); + bilinear_filter.ApplyStencil(jfc, *current_cp[lev+1][idim]); // buffer patch of fine level MultiFab jfb(current_buf[lev+1][idim]->boxArray(), current_buf[lev+1][idim]->DistributionMap(), 1, ng); - applyFilter(jfb, *current_buf[lev+1][idim]); + bilinear_filter.ApplyStencil(jfb, *current_buf[lev+1][idim]); MultiFab::Add(jfb, jfc, 0, 0, 1, ng); mf.ParallelAdd(jfb, 0, 0, 1, ng, IntVect::TheZeroVector(), period); @@ -772,10 +787,10 @@ WarpX::AddCurrentFromFineLevelandSumBoundary (int lev) { // coarse patch of fine level IntVect ng = current_cp[lev+1][idim]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab jf(current_cp[lev+1][idim]->boxArray(), current_cp[lev+1][idim]->DistributionMap(), 1, ng); - applyFilter(jf, *current_cp[lev+1][idim]); + bilinear_filter.ApplyStencil(jf, *current_cp[lev+1][idim]); mf.ParallelAdd(jf, 0, 0, 1, ng, IntVect::TheZeroVector(), period); jf.SumBoundary(period); MultiFab::Copy(*current_cp[lev+1][idim], jf, 0, 0, 1, 0); @@ -822,9 +837,9 @@ WarpX::ApplyFilterandSumBoundaryRho (int lev, PatchType patch_type, int icomp, i if (r == nullptr) return; if (use_filter) { IntVect ng = r->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab rf(r->boxArray(), r->DistributionMap(), ncomp, ng); - applyFilter(rf, *r, icomp, 0, ncomp); + bilinear_filter.ApplyStencil(rf, *r, icomp, 0, ncomp); rf.SumBoundary(period); MultiFab::Copy(*r, rf, 0, icomp, ncomp, 0); } else { @@ -858,15 +873,15 @@ WarpX::AddRhoFromFineLevelandSumBoundary(int lev, int icomp, int ncomp) { // coarse patch of fine level IntVect ng = rho_cp[lev+1]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab rhofc(rho_cp[lev+1]->boxArray(), rho_cp[lev+1]->DistributionMap(), ncomp, ng); - applyFilter(rhofc, *rho_cp[lev+1], icomp, 0, ncomp); + bilinear_filter.ApplyStencil(rhofc, *rho_cp[lev+1], icomp, 0, ncomp); // buffer patch of fine level MultiFab rhofb(charge_buf[lev+1]->boxArray(), charge_buf[lev+1]->DistributionMap(), ncomp, ng); - applyFilter(rhofb, *charge_buf[lev+1], icomp, 0, ncomp); + bilinear_filter.ApplyStencil(rhofb, *charge_buf[lev+1], icomp, 0, ncomp); MultiFab::Add(rhofb, rhofc, 0, 0, ncomp, ng); mf.ParallelAdd(rhofb, 0, 0, ncomp, ng, IntVect::TheZeroVector(), period); @@ -877,9 +892,9 @@ WarpX::AddRhoFromFineLevelandSumBoundary(int lev, int icomp, int ncomp) else if (use_filter) // but no buffer { IntVect ng = rho_cp[lev+1]->nGrowVect(); - ng += 1; + ng += bilinear_filter.stencil_length_each_dir-1; MultiFab rf(rho_cp[lev+1]->boxArray(), rho_cp[lev+1]->DistributionMap(), ncomp, ng); - applyFilter(rf, *rho_cp[lev+1], icomp, 0, ncomp); + bilinear_filter.ApplyStencil(rf, *rho_cp[lev+1], icomp, 0, ncomp); mf.ParallelAdd(rf, 0, 0, ncomp, ng, IntVect::TheZeroVector(), period); rf.SumBoundary(0, ncomp, period); MultiFab::Copy(*rho_cp[lev+1], rf, 0, icomp, ncomp, 0); diff --git a/Source/Parser/GNUmakefile b/Source/Parser/GNUmakefile new file mode 100644 index 000000000..dfc70ff23 --- /dev/null +++ b/Source/Parser/GNUmakefile @@ -0,0 +1,4 @@ + +default: + bison -d wp_parser.y + flex -o wp_parser.lex.c --header-file=wp_parser.lex.h wp_parser.l diff --git a/Source/Parser/Make.package b/Source/Parser/Make.package new file mode 100644 index 000000000..26ef4fb43 --- /dev/null +++ b/Source/Parser/Make.package @@ -0,0 +1,9 @@ + +cEXE_sources += wp_parser_y.c wp_parser.tab.c wp_parser.lex.c wp_parser_c.c +cEXE_headers += wp_parser_y.h wp_parser.tab.h wp_parser.lex.h wp_parser_c.h +CEXE_sources += WarpXParser.cpp +CEXE_headers += WarpXParser.H + +INCLUDE_LOCATIONS += $(WARPX_HOME)/Source/Parser +VPATH_LOCATIONS += $(WARPX_HOME)/Source/Parser + diff --git a/Source/Parser/README b/Source/Parser/README new file mode 100644 index 000000000..9784d58c8 --- /dev/null +++ b/Source/Parser/README @@ -0,0 +1,44 @@ + +* List of files + +** GNUmakefile + + This is not needed by WarpX. This is used to invoke bison and flex + to generate wp_parser.lex.c wp_parser.lex.c, wp_parser.tab.c, and + wp_parser.tab.h. + +** Make.package + + This is used by WarpX make system. + +** WarpXParser.H & WarpXParser.cpp + + WarpXParser class is the interface for the parser. + +** wp_parser.c & wp_parser_c.h + + This is an intermediate layer between WarpXParser class and the C + codes of the parser. + +** wp_parser.l + + This is a flex file. Note that this file is not needed to compile + WarpX, because the files have already been generated. + +** wp_parser.lex.c & wp_parser.lex.h + + They are generated by flex. + +** wp_parser.y + + This is a bison file. Note that this file is not needed to compile + WarpX, because the files have already been generated. + +** wp_parser.tab.c & wp_parser.tab.h + + They are generated by bison. + +** wp_parser_y.c & wp_parser_y.h + + These contain C codes that are used to evaluate a mathematical + expression given in string format. diff --git a/Source/Parser/WarpXParser.H b/Source/Parser/WarpXParser.H new file mode 100644 index 000000000..046491e29 --- /dev/null +++ b/Source/Parser/WarpXParser.H @@ -0,0 +1,109 @@ +#ifndef WARPX_PARSER_H_ +#define WARPX_PARSER_H_ + +#include <array> +#include <vector> +#include <string> +#include <set> + +#include "wp_parser_c.h" +#include "wp_parser_y.h" + +#ifdef _OPENMP +#include <omp.h> +#endif + +class WarpXParser +{ +public: + WarpXParser (std::string const& func_body); + WarpXParser () = default; + ~WarpXParser (); + void define (std::string const& func_body); + + void setConstant (std::string const& name, double c); + + // + // Option 1: Register every variable to an address provided. + // Assign values to external variables. + // Call eval(). + void registerVariable (std::string const& name, double& var); + // + inline double eval () const noexcept; + + // + // Option 2: Register all variables at once. Parser will create + // variables internally. + // Call eval(...) with variable values. + void registerVariables (std::vector<std::string> const& names); + // + template <typename T, typename... Ts> inline + double eval (T x, Ts... yz) const noexcept; + + void print () const; + + std::string const& expr () const; + + std::set<std::string> symbols () const; + +private: + void clear (); + + template <typename T> inline + void unpack (double* p, T x) const noexcept; + + template <typename T, typename... Ts> inline + void unpack (double* p, T x, Ts... yz) const noexcept; + + std::string m_expression; +#ifdef _OPENMP + std::vector<struct wp_parser*> m_parser; + mutable std::vector<std::array<double,16> > m_variables; +#else + struct wp_parser* m_parser = nullptr; + mutable std::array<double,16> m_variables; +#endif +}; + +inline +double +WarpXParser::eval () const noexcept +{ +#ifdef _OPENMP + return wp_ast_eval(m_parser[omp_get_thread_num()]->ast); +#else + return wp_ast_eval(m_parser->ast); +#endif +} + +template <typename T, typename... Ts> +inline +double +WarpXParser::eval (T x, Ts... yz) const noexcept +{ +#ifdef _OPENMP + unpack(m_variables[omp_get_thread_num()].data(), x, yz...); +#else + unpack(m_variables.data(), x, yz...); +#endif + return eval(); +} + +template <typename T> +inline +void +WarpXParser::unpack (double* p, T x) const noexcept +{ + *p = x; +} + +template <typename T, typename... Ts> +inline +void +WarpXParser::unpack (double* p, T x, Ts... yz) const noexcept +{ + *p++ = x; + unpack(p, yz...); +} + +#endif diff --git a/Source/Parser/WarpXParser.cpp b/Source/Parser/WarpXParser.cpp new file mode 100644 index 000000000..8c800215f --- /dev/null +++ b/Source/Parser/WarpXParser.cpp @@ -0,0 +1,150 @@ + +#include "WarpXParser.H" + +WarpXParser::WarpXParser (std::string const& func_body) +{ + define(func_body); +} + +void +WarpXParser::define (std::string const& func_body) +{ + clear(); + + m_expression = func_body; + std::string f = m_expression + "\n"; + +#ifdef _OPENMP + + int nthreads = omp_get_max_threads(); + m_variables.resize(nthreads); + m_parser.resize(nthreads); + m_parser[0] = wp_c_parser_new(f.c_str()); +#pragma omp parallel + { + int tid = omp_get_thread_num(); + if (tid > 0) { + m_parser[tid] = wp_parser_dup(m_parser[0]); + } + } + +#else + + m_parser = wp_c_parser_new(f.c_str()); + +#endif +} + +WarpXParser::~WarpXParser () +{ + clear(); +} + +void +WarpXParser::clear () +{ + m_expression.clear(); + +#ifdef _OPENMP + + if (!m_parser.empty()) + { +#pragma omp parallel + { + wp_parser_delete(m_parser[omp_get_thread_num()]); + } + } + m_parser.clear(); + m_variables.clear(); + +#else + + if (m_parser) wp_parser_delete(m_parser); + m_parser = nullptr; + +#endif +} + +void +WarpXParser::registerVariable (std::string const& name, double& var) +{ + // We assume this is called inside OMP parallel region +#ifdef _OPENMP + wp_parser_regvar(m_parser[omp_get_thread_num()], name.c_str(), &var); +#else + wp_parser_regvar(m_parser, name.c_str(), &var); +#endif +} + +void +WarpXParser::registerVariables (std::vector<std::string> const& names) +{ +#ifdef _OPENMP + +// This must be called outside OpenMP parallel region. +#pragma omp parallel + { + const int tid = omp_get_thread_num(); + struct wp_parser* p = m_parser[tid]; + auto& v = m_variables[tid]; + for (int j = 0; j < names.size(); ++j) { + wp_parser_regvar(p, names[j].c_str(), &(v[j])); + } + } + +#else + + for (int j = 0; j < names.size(); ++j) { + wp_parser_regvar(m_parser, names[j].c_str(), &(m_variables[j])); + } + +#endif +} + +void +WarpXParser::setConstant (std::string const& name, double c) +{ +#ifdef _OPENMP + + bool in_parallel = omp_in_parallel(); + // We don't know if this is inside OMP parallel region or not +#pragma omp parallel if (!in_parallel) + { + wp_parser_setconst(m_parser[omp_get_thread_num()], name.c_str(), c); + } + +#else + + wp_parser_setconst(m_parser, name.c_str(), c); + +#endif +} + +void +WarpXParser::print () const +{ +#ifdef _OPENMP +#pragma omp critical(warpx_parser_pint) + wp_ast_print(m_parser[omp_get_thread_num()]->ast); +#else + wp_ast_print(m_parser->ast); +#endif +} + +std::string const& +WarpXParser::expr () const +{ + return m_expression; +} + +std::set<std::string> +WarpXParser::symbols () const +{ + std::set<std::string> results; +#ifdef _OPENMP + wp_ast_get_symbols(m_parser[omp_get_thread_num()]->ast, results); +#else + wp_ast_get_symbols(m_parser->ast, results); +#endif + return results; +} diff --git a/Source/Parser/wp_parser.l b/Source/Parser/wp_parser.l new file mode 100644 index 000000000..9c54ab06c --- /dev/null +++ b/Source/Parser/wp_parser.l @@ -0,0 +1,68 @@ +%option noyywrap nodefault +%{ +#include "wp_parser_y.h" +#include "wp_parser.tab.h" +%} + + /* Tokens NUMBER, SYMBOL, F1, POW, F2, etc. are defined in wp_parser.y. */ + /* Types WP_SQRT, WP_SQRT, etc. are defined in wp_parser_y.h. */ + + /* Used leater to define NUMBER */ +EXP ([Ee][-+]?[0-9]+) + +%% + +"+" | +"-" | +"*" | +"/" | +"=" | +"|" | +"," | +"<" | +">" | +"(" | +")" { return yytext[0]; } /* simply pass through */ + + /* yylval is union type defined in wp_parser.tab.h that is generated + * by bison with wp_parser.y */ + +"sqrt" { yylval.f1 = WP_SQRT; return F1; } +"exp" { yylval.f1 = WP_EXP; return F1; } +"log" { yylval.f1 = WP_LOG; return F1; } +"log10" { yylval.f1 = WP_LOG10; return F1; } +"sin" { yylval.f1 = WP_SIN; return F1; } +"cos" { yylval.f1 = WP_COS; return F1; } +"tan" { yylval.f1 = WP_TAN; return F1; } +"asin" { yylval.f1 = WP_ASIN; return F1; } +"acos" { yylval.f1 = WP_ACOS; return F1; } +"atan" { yylval.f1 = WP_ATAN; return F1; } +"sinh" { yylval.f1 = WP_SINH; return F1; } +"cosh" { yylval.f1 = WP_COSH; return F1; } +"tanh" { yylval.f1 = WP_TANH; return F1; } +"abs" { yylval.f1 = WP_ABS; return F1; } +"fabs" { yylval.f1 = WP_ABS; return F1; } +"**" { yylval.f2 = WP_POW; return POW;} +"^" { yylval.f2 = WP_POW; return POW;} +"pow" { yylval.f2 = WP_POW; return F2; } +"heaviside" { yylval.f2 = WP_HEAVISIDE; return F2; } +"min" { yylval.f2 = WP_MIN; return F2; } +"max" { yylval.f2 = WP_MAX; return F2; } + + /* We use SYMBOL to hold variables and constants */ +[a-zA-Z_][a-zA-Z0-9_]* { yylval.s = wp_makesymbol(yytext); return SYMBOL; } + + /* Number */ +[0-9]+"."[0-9]*{EXP}? | +"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; } + + /* Special characters */ +"//".* +[ \t] /* ignore white space */ +\\\n /* ignore line continuation */ +"\n" { return EOL; } + + /* everything else */ +. { yyerror("Unknow character %c\n", *yytext); } + +%% diff --git a/Source/Parser/wp_parser.lex.c b/Source/Parser/wp_parser.lex.c new file mode 100644 index 000000000..d47d2b247 --- /dev/null +++ b/Source/Parser/wp_parser.lex.c @@ -0,0 +1,2012 @@ +#line 2 "wp_parser.lex.c" + +#line 4 "wp_parser.lex.c" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 0 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart(yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +#define YY_BUF_SIZE 32768 +#else +#define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + #define YY_LESS_LINENO(n) + #define YY_LINENO_REWIND_TO(ptr) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +yy_size_t yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +static void yyensure_buffer_stack (void ); +static void yy_load_buffer_state (void ); +static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); + +#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/1) +#define YY_SKIP_YYWRAP + +typedef unsigned char YY_CHAR; + +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; + +typedef int yy_state_type; + +extern int yylineno; + +int yylineno = 1; + +extern char *yytext; +#ifdef yytext_ptr +#undef yytext_ptr +#endif +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state (void ); +static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); +static int yy_get_next_buffer (void ); +#if defined(__GNUC__) && __GNUC__ >= 3 +__attribute__((__noreturn__)) +#endif +static void yy_fatal_error (yyconst char msg[] ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 41 +#define YY_END_OF_BUFFER 42 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[95] = + { 0, + 0, 0, 42, 40, 37, 39, 10, 11, 3, 1, + 7, 2, 40, 4, 35, 8, 5, 9, 33, 40, + 28, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 6, 27, 35, 36, 34, 35, 0, 33, 38, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 36, 34, 0, 0, 35, + 25, 33, 33, 33, 17, 13, 33, 33, 14, 32, + 31, 29, 16, 33, 18, 0, 34, 20, 19, 21, + 23, 26, 33, 33, 22, 12, 24, 33, 15, 33, + 33, 33, 30, 0 + + } ; + +static yyconst YY_CHAR yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 14, 14, 14, 14, 14, 14, 14, 1, 1, 15, + 16, 17, 1, 1, 18, 18, 18, 18, 19, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 1, 20, 1, 21, 18, 1, 22, 23, 24, 25, + + 26, 27, 28, 29, 30, 18, 18, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 18, 40, 41, 42, + 18, 18, 1, 43, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst YY_CHAR yy_meta[44] = + { 0, + 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 3, 3, 3, 1, 1, 1, 3, 3, 1, + 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 1 + } ; + +static yyconst flex_uint16_t yy_base[97] = + { 0, + 0, 0, 148, 149, 149, 149, 149, 149, 141, 149, + 149, 149, 32, 135, 37, 149, 149, 149, 0, 142, + 149, 29, 110, 101, 120, 115, 106, 32, 105, 25, + 116, 149, 149, 45, 0, 60, 68, 76, 0, 149, + 99, 102, 105, 112, 95, 97, 108, 108, 101, 86, + 94, 85, 92, 87, 90, 0, 79, 88, 63, 94, + 0, 84, 88, 87, 90, 0, 80, 76, 91, 0, + 0, 0, 74, 60, 67, 97, 100, 0, 0, 0, + 0, 0, 54, 58, 0, 0, 0, 31, 0, 36, + 40, 34, 0, 149, 45, 114 + + } ; + +static yyconst flex_int16_t yy_def[97] = + { 0, + 94, 1, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 95, 94, + 94, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 94, 94, 94, 96, 94, 94, 94, 95, 94, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 96, 94, 94, 94, 94, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 94, 94, 95, 95, 95, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 0, 94, 94 + + } ; + +static yyconst flex_uint16_t yy_nxt[193] = + { 0, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 15, 15, 16, 17, 18, 19, 19, 20, + 21, 22, 19, 23, 19, 24, 25, 19, 26, 19, + 27, 28, 19, 19, 29, 19, 19, 30, 31, 19, + 19, 19, 32, 34, 34, 34, 36, 39, 37, 37, + 37, 41, 42, 50, 53, 38, 34, 34, 34, 93, + 54, 51, 38, 38, 92, 91, 43, 44, 90, 89, + 38, 57, 57, 57, 60, 60, 60, 36, 58, 37, + 37, 37, 59, 88, 59, 58, 38, 60, 60, 60, + 57, 57, 57, 38, 76, 87, 76, 58, 86, 77, + + 77, 77, 85, 84, 58, 60, 60, 60, 77, 77, + 77, 77, 77, 77, 56, 83, 56, 82, 81, 80, + 79, 78, 75, 74, 73, 72, 71, 70, 69, 68, + 67, 66, 65, 64, 63, 62, 61, 55, 52, 49, + 48, 47, 46, 45, 40, 35, 33, 94, 3, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94 + + } ; + +static yyconst flex_int16_t yy_chk[193] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 13, 13, 13, 15, 95, 15, 15, + 15, 22, 22, 28, 30, 15, 34, 34, 34, 92, + 30, 28, 15, 34, 91, 90, 22, 22, 88, 84, + 34, 36, 36, 36, 59, 59, 59, 37, 36, 37, + 37, 37, 38, 83, 38, 36, 37, 38, 38, 38, + 57, 57, 57, 37, 58, 75, 58, 57, 74, 58, + + 58, 58, 73, 69, 57, 60, 60, 60, 76, 76, + 76, 77, 77, 77, 96, 68, 96, 67, 65, 64, + 63, 62, 55, 54, 53, 52, 51, 50, 49, 48, + 47, 46, 45, 44, 43, 42, 41, 31, 29, 27, + 26, 25, 24, 23, 20, 14, 9, 3, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94 + + } ; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "wp_parser.l" +#line 3 "wp_parser.l" +#include "wp_parser_y.h" +#include "wp_parser.tab.h" +/* Tokens NUMBER, SYMBOL, F1, POW, F2, etc. are defined in wp_parser.y. */ +/* Types WP_SQRT, WP_SQRT, etc. are defined in wp_parser_y.h. */ +/* Used leater to define NUMBER */ +#line 551 "wp_parser.lex.c" + +#define INITIAL 0 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include <unistd.h> +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +static int yy_init_globals (void ); + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy (void ); + +int yyget_debug (void ); + +void yyset_debug (int debug_flag ); + +YY_EXTRA_TYPE yyget_extra (void ); + +void yyset_extra (YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in (void ); + +void yyset_in (FILE * _in_str ); + +FILE *yyget_out (void ); + +void yyset_out (FILE * _out_str ); + +yy_size_t yyget_leng (void ); + +char *yyget_text (void ); + +int yyget_lineno (void ); + +void yyset_lineno (int _line_number ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + +#ifndef YY_NO_UNPUT + + static void yyunput (int c,char *buf_ptr ); + +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#ifdef __cplusplus +static int yyinput (void ); +#else +static int input (void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else +#define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + size_t n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(yyin); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK /*LINTED*/break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + yy_state_type yy_current_state; + char *yy_cp, *yy_bp; + int yy_act; + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + { +#line 13 "wp_parser.l" + + +#line 772 "wp_parser.lex.c" + + while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + do + { + YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 149 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +#line 16 "wp_parser.l" +case 2: +#line 17 "wp_parser.l" +case 3: +#line 18 "wp_parser.l" +case 4: +#line 19 "wp_parser.l" +case 5: +#line 20 "wp_parser.l" +case 6: +#line 21 "wp_parser.l" +case 7: +#line 22 "wp_parser.l" +case 8: +#line 23 "wp_parser.l" +case 9: +#line 24 "wp_parser.l" +case 10: +#line 25 "wp_parser.l" +case 11: +YY_RULE_SETUP +#line 25 "wp_parser.l" +{ return yytext[0]; } /* simply pass through */ + YY_BREAK +/* yylval is union type defined in wp_parser.tab.h that is generated + * by bison with wp_parser.y */ +case 12: +YY_RULE_SETUP +#line 30 "wp_parser.l" +{ yylval.f1 = WP_SQRT; return F1; } + YY_BREAK +case 13: +YY_RULE_SETUP +#line 31 "wp_parser.l" +{ yylval.f1 = WP_EXP; return F1; } + YY_BREAK +case 14: +YY_RULE_SETUP +#line 32 "wp_parser.l" +{ yylval.f1 = WP_LOG; return F1; } + YY_BREAK +case 15: +YY_RULE_SETUP +#line 33 "wp_parser.l" +{ yylval.f1 = WP_LOG10; return F1; } + YY_BREAK +case 16: +YY_RULE_SETUP +#line 34 "wp_parser.l" +{ yylval.f1 = WP_SIN; return F1; } + YY_BREAK +case 17: +YY_RULE_SETUP +#line 35 "wp_parser.l" +{ yylval.f1 = WP_COS; return F1; } + YY_BREAK +case 18: +YY_RULE_SETUP +#line 36 "wp_parser.l" +{ yylval.f1 = WP_TAN; return F1; } + YY_BREAK +case 19: +YY_RULE_SETUP +#line 37 "wp_parser.l" +{ yylval.f1 = WP_ASIN; return F1; } + YY_BREAK +case 20: +YY_RULE_SETUP +#line 38 "wp_parser.l" +{ yylval.f1 = WP_ACOS; return F1; } + YY_BREAK +case 21: +YY_RULE_SETUP +#line 39 "wp_parser.l" +{ yylval.f1 = WP_ATAN; return F1; } + YY_BREAK +case 22: +YY_RULE_SETUP +#line 40 "wp_parser.l" +{ yylval.f1 = WP_SINH; return F1; } + YY_BREAK +case 23: +YY_RULE_SETUP +#line 41 "wp_parser.l" +{ yylval.f1 = WP_COSH; return F1; } + YY_BREAK +case 24: +YY_RULE_SETUP +#line 42 "wp_parser.l" +{ yylval.f1 = WP_TANH; return F1; } + YY_BREAK +case 25: +YY_RULE_SETUP +#line 43 "wp_parser.l" +{ yylval.f1 = WP_ABS; return F1; } + YY_BREAK +case 26: +YY_RULE_SETUP +#line 44 "wp_parser.l" +{ yylval.f1 = WP_ABS; return F1; } + YY_BREAK +case 27: +YY_RULE_SETUP +#line 45 "wp_parser.l" +{ yylval.f2 = WP_POW; return POW;} + YY_BREAK +case 28: +YY_RULE_SETUP +#line 46 "wp_parser.l" +{ yylval.f2 = WP_POW; return POW;} + YY_BREAK +case 29: +YY_RULE_SETUP +#line 47 "wp_parser.l" +{ yylval.f2 = WP_POW; return F2; } + YY_BREAK +case 30: +YY_RULE_SETUP +#line 48 "wp_parser.l" +{ yylval.f2 = WP_HEAVISIDE; return F2; } + YY_BREAK +case 31: +YY_RULE_SETUP +#line 49 "wp_parser.l" +{ yylval.f2 = WP_MIN; return F2; } + YY_BREAK +case 32: +YY_RULE_SETUP +#line 50 "wp_parser.l" +{ yylval.f2 = WP_MAX; return F2; } + YY_BREAK +/* We use SYMBOL to hold variables and constants */ +case 33: +YY_RULE_SETUP +#line 53 "wp_parser.l" +{ yylval.s = wp_makesymbol(yytext); return SYMBOL; } + YY_BREAK +/* Number */ +case 34: +#line 57 "wp_parser.l" +case 35: +YY_RULE_SETUP +#line 57 "wp_parser.l" +{ yylval.d = atof(yytext); return NUMBER; } + YY_BREAK +/* Special characters */ +case 36: +YY_RULE_SETUP +#line 60 "wp_parser.l" + + YY_BREAK +case 37: +YY_RULE_SETUP +#line 61 "wp_parser.l" +/* ignore white space */ + YY_BREAK +case 38: +/* rule 38 can match eol */ +YY_RULE_SETUP +#line 62 "wp_parser.l" +/* ignore line continuation */ + YY_BREAK +case 39: +/* rule 39 can match eol */ +YY_RULE_SETUP +#line 63 "wp_parser.l" +{ return EOL; } + YY_BREAK +/* everything else */ +case 40: +YY_RULE_SETUP +#line 66 "wp_parser.l" +{ yyerror("Unknow character %c\n", *yytext); } + YY_BREAK +case 41: +YY_RULE_SETUP +#line 68 "wp_parser.l" +YY_FATAL_ERROR( "flex scanner jammed" ); + YY_BREAK +#line 1009 "wp_parser.lex.c" +case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of user's declarations */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + char *source = (yytext_ptr); + yy_size_t number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (yy_size_t) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + yy_size_t num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + yy_size_t new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart(yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if ((int) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { + /* Extend the array by 50%, plus the number we really need. */ + int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); + if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + yy_state_type yy_current_state; + char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + int yy_is_jam; + char *yy_cp = (yy_c_buf_p); + + YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 94); + + return yy_is_jam ? 0 : yy_current_state; +} + +#ifndef YY_NO_UNPUT + + static void yyunput (int c, char * yy_bp ) +{ + char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + yy_size_t number_to_move = (yy_n_chars) + 2; + char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart(yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyrestart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_init_buffer(YY_CURRENT_BUFFER,input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void yy_load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = (yy_size_t)size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer(b,file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yy_delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree((void *) b->yy_ch_buf ); + + yyfree((void *) b ); +} + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + yy_flush_buffer(b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yy_flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void yyensure_buffer_stack (void) +{ + yy_size_t num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + yy_size_t grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer(b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param yystr a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) +{ + + return yy_scan_bytes(yystr,strlen(yystr) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param yybytes the byte buffer to scan + * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + yy_size_t i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = _yybytes_len + 2; + buf = (char *) yyalloc(n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < _yybytes_len; ++i ) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer(buf,n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yy_fatal_error (yyconst char* msg ) +{ + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int yyget_lineno (void) +{ + + return yylineno; +} + +/** Get the input stream. + * + */ +FILE *yyget_in (void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE *yyget_out (void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +yy_size_t yyget_leng (void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char *yyget_text (void) +{ + return yytext; +} + +/** Set the current line number. + * @param _line_number line number + * + */ +void yyset_lineno (int _line_number ) +{ + + yylineno = _line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param _in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void yyset_in (FILE * _in_str ) +{ + yyin = _in_str ; +} + +void yyset_out (FILE * _out_str ) +{ + yyout = _out_str ; +} + +int yyget_debug (void) +{ + return yy_flex_debug; +} + +void yyset_debug (int _bdebug ) +{ + yy_flex_debug = _bdebug ; +} + +static int yy_init_globals (void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + (yy_buffer_stack) = 0; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = (char *) 0; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = (FILE *) 0; + yyout = (FILE *) 0; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int yylex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals( ); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + + int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 68 "wp_parser.l" + + + diff --git a/Source/Parser/wp_parser.lex.h b/Source/Parser/wp_parser.lex.h new file mode 100644 index 000000000..13e286b05 --- /dev/null +++ b/Source/Parser/wp_parser.lex.h @@ -0,0 +1,338 @@ +#ifndef yyHEADER_H +#define yyHEADER_H 1 +#define yyIN_HEADER 1 + +#line 6 "wp_parser.lex.h" + +#line 8 "wp_parser.lex.h" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 0 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +#define YY_BUF_SIZE 32768 +#else +#define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ +#endif + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; + +extern FILE *yyin, *yyout; + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/1) +#define YY_SKIP_YYWRAP + +extern int yylineno; + +extern char *yytext; +#ifdef yytext_ptr +#undef yytext_ptr +#endif +#define yytext_ptr yytext + +#ifdef YY_HEADER_EXPORT_START_CONDITIONS +#define INITIAL 0 + +#endif + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include <unistd.h> +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy (void ); + +int yyget_debug (void ); + +void yyset_debug (int debug_flag ); + +YY_EXTRA_TYPE yyget_extra (void ); + +void yyset_extra (YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in (void ); + +void yyset_in (FILE * _in_str ); + +FILE *yyget_out (void ); + +void yyset_out (FILE * _out_str ); + +yy_size_t yyget_leng (void ); + +char *yyget_text (void ); + +int yyget_lineno (void ); + +void yyset_lineno (int _line_number ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else +#define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + +#undef YY_NEW_FILE +#undef YY_FLUSH_BUFFER +#undef yy_set_bol +#undef yy_new_buffer +#undef yy_set_interactive +#undef YY_DO_BEFORE_ACTION + +#ifdef YY_DECL_IS_OURS +#undef YY_DECL_IS_OURS +#undef YY_DECL +#endif + +#line 68 "wp_parser.l" + + +#line 337 "wp_parser.lex.h" +#undef yyIN_HEADER +#endif /* yyHEADER_H */ diff --git a/Source/Parser/wp_parser.tab.c b/Source/Parser/wp_parser.tab.c new file mode 100644 index 000000000..c0cf2fad8 --- /dev/null +++ b/Source/Parser/wp_parser.tab.c @@ -0,0 +1,1568 @@ +/* A Bison parser, made by GNU Bison 3.0.4. */ + +/* Bison implementation for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* C LALR(1) parser skeleton written by Richard Stallman, by + simplifying the original so-called "semantic" parser. */ + +/* All symbols defined below should begin with yy or YY, to avoid + infringing on user name space. This should be done even for local + variables, as they might otherwise be expanded by user macros. + There are some unavoidable exceptions within include files to + define necessary library symbols; they are noted "INFRINGES ON + USER NAME SPACE" below. */ + +/* Identify Bison output. */ +#define YYBISON 1 + +/* Bison version. */ +#define YYBISON_VERSION "3.0.4" + +/* Skeleton name. */ +#define YYSKELETON_NAME "yacc.c" + +/* Pure parsers. */ +#define YYPURE 0 + +/* Push parsers. */ +#define YYPUSH 0 + +/* Pull parsers. */ +#define YYPULL 1 + + + + +/* Copy the first part of user declarations. */ +#line 2 "wp_parser.y" /* yacc.c:339 */ + + #include <stdio.h> + #include <stdlib.h> + #include <math.h> + #include "wp_parser_y.h" + int yylex (void); + +#line 74 "wp_parser.tab.c" /* yacc.c:339 */ + +# ifndef YY_NULLPTR +# if defined __cplusplus && 201103L <= __cplusplus +# define YY_NULLPTR nullptr +# else +# define YY_NULLPTR 0 +# endif +# endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE 0 +#endif + +/* In a future release of Bison, this section will be replaced + by #include "wp_parser.tab.h". */ +#ifndef YY_YY_WP_PARSER_TAB_H_INCLUDED +# define YY_YY_WP_PARSER_TAB_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + NODE = 258, + NUMBER = 259, + SYMBOL = 260, + F1 = 261, + F2 = 262, + EOL = 263, + POW = 264, + NEG = 265, + UPLUS = 266 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED + +union YYSTYPE +{ +#line 19 "wp_parser.y" /* yacc.c:355 */ + + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; + +#line 134 "wp_parser.tab.c" /* yacc.c:355 */ +}; + +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + + +extern YYSTYPE yylval; + +int yyparse (void); + +#endif /* !YY_YY_WP_PARSER_TAB_H_INCLUDED */ + +/* Copy the second part of user declarations. */ + +#line 151 "wp_parser.tab.c" /* yacc.c:358 */ + +#ifdef short +# undef short +#endif + +#ifdef YYTYPE_UINT8 +typedef YYTYPE_UINT8 yytype_uint8; +#else +typedef unsigned char yytype_uint8; +#endif + +#ifdef YYTYPE_INT8 +typedef YYTYPE_INT8 yytype_int8; +#else +typedef signed char yytype_int8; +#endif + +#ifdef YYTYPE_UINT16 +typedef YYTYPE_UINT16 yytype_uint16; +#else +typedef unsigned short int yytype_uint16; +#endif + +#ifdef YYTYPE_INT16 +typedef YYTYPE_INT16 yytype_int16; +#else +typedef short int yytype_int16; +#endif + +#ifndef YYSIZE_T +# ifdef __SIZE_TYPE__ +# define YYSIZE_T __SIZE_TYPE__ +# elif defined size_t +# define YYSIZE_T size_t +# elif ! defined YYSIZE_T +# include <stddef.h> /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# else +# define YYSIZE_T unsigned int +# endif +#endif + +#define YYSIZE_MAXIMUM ((YYSIZE_T) -1) + +#ifndef YY_ +# if defined YYENABLE_NLS && YYENABLE_NLS +# if ENABLE_NLS +# include <libintl.h> /* INFRINGES ON USER NAME SPACE */ +# define YY_(Msgid) dgettext ("bison-runtime", Msgid) +# endif +# endif +# ifndef YY_ +# define YY_(Msgid) Msgid +# endif +#endif + +#ifndef YY_ATTRIBUTE +# if (defined __GNUC__ \ + && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ + || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C +# define YY_ATTRIBUTE(Spec) __attribute__(Spec) +# else +# define YY_ATTRIBUTE(Spec) /* empty */ +# endif +#endif + +#ifndef YY_ATTRIBUTE_PURE +# define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) +#endif + +#ifndef YY_ATTRIBUTE_UNUSED +# define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) +#endif + +#if !defined _Noreturn \ + && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) +# if defined _MSC_VER && 1200 <= _MSC_VER +# define _Noreturn __declspec (noreturn) +# else +# define _Noreturn YY_ATTRIBUTE ((__noreturn__)) +# endif +#endif + +/* Suppress unused-variable warnings by "using" E. */ +#if ! defined lint || defined __GNUC__ +# define YYUSE(E) ((void) (E)) +#else +# define YYUSE(E) /* empty */ +#endif + +#if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ +/* Suppress an incorrect diagnostic about yylval being uninitialized. */ +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ + _Pragma ("GCC diagnostic push") \ + _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ + _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") +# define YY_IGNORE_MAYBE_UNINITIALIZED_END \ + _Pragma ("GCC diagnostic pop") +#else +# define YY_INITIAL_VALUE(Value) Value +#endif +#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_END +#endif +#ifndef YY_INITIAL_VALUE +# define YY_INITIAL_VALUE(Value) /* Nothing. */ +#endif + + +#if ! defined yyoverflow || YYERROR_VERBOSE + +/* The parser invokes alloca or malloc; define the necessary symbols. */ + +# ifdef YYSTACK_USE_ALLOCA +# if YYSTACK_USE_ALLOCA +# ifdef __GNUC__ +# define YYSTACK_ALLOC __builtin_alloca +# elif defined __BUILTIN_VA_ARG_INCR +# include <alloca.h> /* INFRINGES ON USER NAME SPACE */ +# elif defined _AIX +# define YYSTACK_ALLOC __alloca +# elif defined _MSC_VER +# include <malloc.h> /* INFRINGES ON USER NAME SPACE */ +# define alloca _alloca +# else +# define YYSTACK_ALLOC alloca +# if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS +# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ + /* Use EXIT_SUCCESS as a witness for stdlib.h. */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# endif +# endif +# endif + +# ifdef YYSTACK_ALLOC + /* Pacify GCC's 'empty if-body' warning. */ +# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) +# ifndef YYSTACK_ALLOC_MAXIMUM + /* The OS might guarantee only one guard page at the bottom of the stack, + and a page size can be as small as 4096 bytes. So we cannot safely + invoke alloca (N) if N exceeds 4096. Use a slightly smaller number + to allow for a few compiler-allocated temporary stack slots. */ +# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ +# endif +# else +# define YYSTACK_ALLOC YYMALLOC +# define YYSTACK_FREE YYFREE +# ifndef YYSTACK_ALLOC_MAXIMUM +# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM +# endif +# if (defined __cplusplus && ! defined EXIT_SUCCESS \ + && ! ((defined YYMALLOC || defined malloc) \ + && (defined YYFREE || defined free))) +# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# ifndef YYMALLOC +# define YYMALLOC malloc +# if ! defined malloc && ! defined EXIT_SUCCESS +void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# ifndef YYFREE +# define YYFREE free +# if ! defined free && ! defined EXIT_SUCCESS +void free (void *); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# endif +#endif /* ! defined yyoverflow || YYERROR_VERBOSE */ + + +#if (! defined yyoverflow \ + && (! defined __cplusplus \ + || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) + +/* A type that is properly aligned for any stack member. */ +union yyalloc +{ + yytype_int16 yyss_alloc; + YYSTYPE yyvs_alloc; +}; + +/* The size of the maximum gap between one aligned stack and the next. */ +# define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) + +/* The size of an array large to enough to hold all stacks, each with + N elements. */ +# define YYSTACK_BYTES(N) \ + ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \ + + YYSTACK_GAP_MAXIMUM) + +# define YYCOPY_NEEDED 1 + +/* Relocate STACK from its old location to the new one. The + local variables YYSIZE and YYSTACKSIZE give the old and new number of + elements in the stack, and YYPTR gives the new location of the + stack. Advance YYPTR to a properly aligned location for the next + stack. */ +# define YYSTACK_RELOCATE(Stack_alloc, Stack) \ + do \ + { \ + YYSIZE_T yynewbytes; \ + YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ + Stack = &yyptr->Stack_alloc; \ + yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ + yyptr += yynewbytes / sizeof (*yyptr); \ + } \ + while (0) + +#endif + +#if defined YYCOPY_NEEDED && YYCOPY_NEEDED +/* Copy COUNT objects from SRC to DST. The source and destination do + not overlap. */ +# ifndef YYCOPY +# if defined __GNUC__ && 1 < __GNUC__ +# define YYCOPY(Dst, Src, Count) \ + __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src))) +# else +# define YYCOPY(Dst, Src, Count) \ + do \ + { \ + YYSIZE_T yyi; \ + for (yyi = 0; yyi < (Count); yyi++) \ + (Dst)[yyi] = (Src)[yyi]; \ + } \ + while (0) +# endif +# endif +#endif /* !YYCOPY_NEEDED */ + +/* YYFINAL -- State number of the termination state. */ +#define YYFINAL 2 +/* YYLAST -- Last index in YYTABLE. */ +#define YYLAST 96 + +/* YYNTOKENS -- Number of terminals. */ +#define YYNTOKENS 23 +/* YYNNTS -- Number of nonterminals. */ +#define YYNNTS 3 +/* YYNRULES -- Number of rules. */ +#define YYNRULES 17 +/* YYNSTATES -- Number of states. */ +#define YYNSTATES 38 + +/* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned + by yylex, with out-of-bounds checking. */ +#define YYUNDEFTOK 2 +#define YYMAXUTOK 266 + +#define YYTRANSLATE(YYX) \ + ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) + +/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM + as returned by yylex, without out-of-bounds checking. */ +static const yytype_uint8 yytranslate[] = +{ + 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 20, 21, 14, 12, 22, 13, 2, 15, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 16, 11, 17, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 10, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 18, 19 +}; + +#if YYDEBUG + /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ +static const yytype_uint8 yyrline[] = +{ + 0, 58, 58, 59, 68, 69, 70, 71, 72, 73, + 74, 75, 76, 77, 78, 79, 80, 81 +}; +#endif + +#if YYDEBUG || YYERROR_VERBOSE || 0 +/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at YYNTOKENS, nonterminals. */ +static const char *const yytname[] = +{ + "$end", "error", "$undefined", "NODE", "NUMBER", "SYMBOL", "F1", "F2", + "EOL", "\"**\"", "'^'", "'='", "'+'", "'-'", "'*'", "'/'", "'<'", "'>'", + "NEG", "UPLUS", "'('", "')'", "','", "$accept", "input", "exp", YY_NULLPTR +}; +#endif + +# ifdef YYPRINT +/* YYTOKNUM[NUM] -- (External) token number corresponding to the + (internal) symbol number NUM (which must be that of a token). */ +static const yytype_uint16 yytoknum[] = +{ + 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, + 94, 61, 43, 45, 42, 47, 60, 62, 265, 266, + 40, 41, 44 +}; +# endif + +#define YYPACT_NINF -18 + +#define yypact_value_is_default(Yystate) \ + (!!((Yystate) == (-18))) + +#define YYTABLE_NINF -1 + +#define yytable_value_is_error(Yytable_value) \ + 0 + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ +static const yytype_int8 yypact[] = +{ + -18, 17, -18, -18, -18, -17, -14, 27, 27, 27, + 70, 27, 27, -2, -2, 40, -18, 27, 27, 27, + 27, 27, 27, 27, 50, 29, -18, -2, 79, 79, + 9, 9, -2, -2, -18, 27, 60, -18 +}; + + /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. + Performed when YYTABLE does not specify something else to do. Zero + means the default is an error. */ +static const yytype_uint8 yydefact[] = +{ + 2, 0, 1, 4, 5, 0, 0, 0, 0, 0, + 0, 0, 0, 14, 13, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 10, 15, 6, 7, + 8, 9, 11, 12, 16, 0, 0, 17 +}; + + /* YYPGOTO[NTERM-NUM]. */ +static const yytype_int8 yypgoto[] = +{ + -18, -18, -7 +}; + + /* YYDEFGOTO[NTERM-NUM]. */ +static const yytype_int8 yydefgoto[] = +{ + -1, 1, 10 +}; + + /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule whose + number is the opposite. If YYTABLE_NINF, syntax error. */ +static const yytype_uint8 yytable[] = +{ + 13, 14, 15, 11, 24, 25, 12, 17, 0, 0, + 27, 28, 29, 30, 31, 32, 33, 2, 17, 0, + 0, 3, 4, 5, 6, 22, 23, 0, 36, 7, + 8, 3, 4, 5, 6, 0, 0, 9, 17, 7, + 8, 18, 19, 20, 21, 22, 23, 9, 0, 17, + 0, 35, 18, 19, 20, 21, 22, 23, 0, 17, + 0, 26, 18, 19, 20, 21, 22, 23, 0, 17, + 0, 34, 18, 19, 20, 21, 22, 23, 16, 17, + 0, 37, 18, 19, 20, 21, 22, 23, 17, 0, + 0, 0, 0, 20, 21, 22, 23 +}; + +static const yytype_int8 yycheck[] = +{ + 7, 8, 9, 20, 11, 12, 20, 9, -1, -1, + 17, 18, 19, 20, 21, 22, 23, 0, 9, -1, + -1, 4, 5, 6, 7, 16, 17, -1, 35, 12, + 13, 4, 5, 6, 7, -1, -1, 20, 9, 12, + 13, 12, 13, 14, 15, 16, 17, 20, -1, 9, + -1, 22, 12, 13, 14, 15, 16, 17, -1, 9, + -1, 21, 12, 13, 14, 15, 16, 17, -1, 9, + -1, 21, 12, 13, 14, 15, 16, 17, 8, 9, + -1, 21, 12, 13, 14, 15, 16, 17, 9, -1, + -1, -1, -1, 14, 15, 16, 17 +}; + + /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const yytype_uint8 yystos[] = +{ + 0, 24, 0, 4, 5, 6, 7, 12, 13, 20, + 25, 20, 20, 25, 25, 25, 8, 9, 12, 13, + 14, 15, 16, 17, 25, 25, 21, 25, 25, 25, + 25, 25, 25, 25, 21, 22, 25, 21 +}; + + /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ +static const yytype_uint8 yyr1[] = +{ + 0, 23, 24, 24, 25, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25, 25, 25 +}; + + /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ +static const yytype_uint8 yyr2[] = +{ + 0, 2, 0, 3, 1, 1, 3, 3, 3, 3, + 3, 3, 3, 2, 2, 3, 4, 6 +}; + + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY (-2) +#define YYEOF 0 + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab + + +#define YYRECOVERING() (!!yyerrstatus) + +#define YYBACKUP(Token, Value) \ +do \ + if (yychar == YYEMPTY) \ + { \ + yychar = (Token); \ + yylval = (Value); \ + YYPOPSTACK (yylen); \ + yystate = *yyssp; \ + goto yybackup; \ + } \ + else \ + { \ + yyerror (YY_("syntax error: cannot back up")); \ + YYERROR; \ + } \ +while (0) + +/* Error token number */ +#define YYTERROR 1 +#define YYERRCODE 256 + + + +/* Enable debugging if requested. */ +#if YYDEBUG + +# ifndef YYFPRINTF +# include <stdio.h> /* INFRINGES ON USER NAME SPACE */ +# define YYFPRINTF fprintf +# endif + +# define YYDPRINTF(Args) \ +do { \ + if (yydebug) \ + YYFPRINTF Args; \ +} while (0) + +/* This macro is provided for backward compatibility. */ +#ifndef YY_LOCATION_PRINT +# define YY_LOCATION_PRINT(File, Loc) ((void) 0) +#endif + + +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yy_symbol_print (stderr, \ + Type, Value); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (0) + + +/*----------------------------------------. +| Print this symbol's value on YYOUTPUT. | +`----------------------------------------*/ + +static void +yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) +{ + FILE *yyo = yyoutput; + YYUSE (yyo); + if (!yyvaluep) + return; +# ifdef YYPRINT + if (yytype < YYNTOKENS) + YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); +# endif + YYUSE (yytype); +} + + +/*--------------------------------. +| Print this symbol on YYOUTPUT. | +`--------------------------------*/ + +static void +yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) +{ + YYFPRINTF (yyoutput, "%s %s (", + yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); + + yy_symbol_value_print (yyoutput, yytype, yyvaluep); + YYFPRINTF (yyoutput, ")"); +} + +/*------------------------------------------------------------------. +| yy_stack_print -- Print the state stack from its BOTTOM up to its | +| TOP (included). | +`------------------------------------------------------------------*/ + +static void +yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop) +{ + YYFPRINTF (stderr, "Stack now"); + for (; yybottom <= yytop; yybottom++) + { + int yybot = *yybottom; + YYFPRINTF (stderr, " %d", yybot); + } + YYFPRINTF (stderr, "\n"); +} + +# define YY_STACK_PRINT(Bottom, Top) \ +do { \ + if (yydebug) \ + yy_stack_print ((Bottom), (Top)); \ +} while (0) + + +/*------------------------------------------------. +| Report that the YYRULE is going to be reduced. | +`------------------------------------------------*/ + +static void +yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule) +{ + unsigned long int yylno = yyrline[yyrule]; + int yynrhs = yyr2[yyrule]; + int yyi; + YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n", + yyrule - 1, yylno); + /* The symbols being reduced. */ + for (yyi = 0; yyi < yynrhs; yyi++) + { + YYFPRINTF (stderr, " $%d = ", yyi + 1); + yy_symbol_print (stderr, + yystos[yyssp[yyi + 1 - yynrhs]], + &(yyvsp[(yyi + 1) - (yynrhs)]) + ); + YYFPRINTF (stderr, "\n"); + } +} + +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (yydebug) \ + yy_reduce_print (yyssp, yyvsp, Rule); \ +} while (0) + +/* Nonzero means print parse trace. It is left uninitialized so that + multiple parsers can coexist. */ +int yydebug; +#else /* !YYDEBUG */ +# define YYDPRINTF(Args) +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) +# define YY_STACK_PRINT(Bottom, Top) +# define YY_REDUCE_PRINT(Rule) +#endif /* !YYDEBUG */ + + +/* YYINITDEPTH -- initial size of the parser's stacks. */ +#ifndef YYINITDEPTH +# define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only + if the built-in stack extension method is used). + + Do not make this value too large; the results are undefined if + YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) + evaluated with infinite-precision integer arithmetic. */ + +#ifndef YYMAXDEPTH +# define YYMAXDEPTH 10000 +#endif + + +#if YYERROR_VERBOSE + +# ifndef yystrlen +# if defined __GLIBC__ && defined _STRING_H +# define yystrlen strlen +# else +/* Return the length of YYSTR. */ +static YYSIZE_T +yystrlen (const char *yystr) +{ + YYSIZE_T yylen; + for (yylen = 0; yystr[yylen]; yylen++) + continue; + return yylen; +} +# endif +# endif + +# ifndef yystpcpy +# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE +# define yystpcpy stpcpy +# else +/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in + YYDEST. */ +static char * +yystpcpy (char *yydest, const char *yysrc) +{ + char *yyd = yydest; + const char *yys = yysrc; + + while ((*yyd++ = *yys++) != '\0') + continue; + + return yyd - 1; +} +# endif +# endif + +# ifndef yytnamerr +/* Copy to YYRES the contents of YYSTR after stripping away unnecessary + quotes and backslashes, so that it's suitable for yyerror. The + heuristic is that double-quoting is unnecessary unless the string + contains an apostrophe, a comma, or backslash (other than + backslash-backslash). YYSTR is taken from yytname. If YYRES is + null, do not copy; instead, return the length of what the result + would have been. */ +static YYSIZE_T +yytnamerr (char *yyres, const char *yystr) +{ + if (*yystr == '"') + { + YYSIZE_T yyn = 0; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + if (yyres) + yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + if (yyres) + yyres[yyn] = '\0'; + return yyn; + } + do_not_strip_quotes: ; + } + + if (! yyres) + return yystrlen (yystr); + + return yystpcpy (yyres, yystr) - yyres; +} +# endif + +/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message + about the unexpected token YYTOKEN for the state stack whose top is + YYSSP. + + Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is + not large enough to hold the message. In that case, also set + *YYMSG_ALLOC to the required number of bytes. Return 2 if the + required number of bytes is too large to store. */ +static int +yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, + yytype_int16 *yyssp, int yytoken) +{ + YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); + YYSIZE_T yysize = yysize0; + enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; + /* Internationalized format string. */ + const char *yyformat = YY_NULLPTR; + /* Arguments of yyformat. */ + char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + /* Number of reported tokens (one for the "unexpected", one per + "expected"). */ + int yycount = 0; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yychar) is if + this state is a consistent state with a default action. Thus, + detecting the absence of a lookahead is sufficient to determine + that there is no unexpected or expected token to report. In that + case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is a + consistent state with a default action. There might have been a + previous inconsistent state, consistent state with a non-default + action, or user semantic action that manipulated yychar. + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state merging + (from LALR or IELR) and default reductions corrupt the expected + token list. However, the list is correct for canonical LR with + one exception: it will still contain any token that will not be + accepted due to an error action in a later state. + */ + if (yytoken != YYEMPTY) + { + int yyn = yypact[*yyssp]; + yyarg[yycount++] = yytname[yytoken]; + if (!yypact_value_is_default (yyn)) + { + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. In other words, skip the first -YYN actions for + this state because they are default actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn + 1; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; + int yyx; + + for (yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR + && !yytable_value_is_error (yytable[yyx + yyn])) + { + if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) + { + yycount = 1; + yysize = yysize0; + break; + } + yyarg[yycount++] = yytname[yyx]; + { + YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); + if (! (yysize <= yysize1 + && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) + return 2; + yysize = yysize1; + } + } + } + } + + switch (yycount) + { +# define YYCASE_(N, S) \ + case N: \ + yyformat = S; \ + break + YYCASE_(0, YY_("syntax error")); + YYCASE_(1, YY_("syntax error, unexpected %s")); + YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); + YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); + YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); + YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); +# undef YYCASE_ + } + + { + YYSIZE_T yysize1 = yysize + yystrlen (yyformat); + if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) + return 2; + yysize = yysize1; + } + + if (*yymsg_alloc < yysize) + { + *yymsg_alloc = 2 * yysize; + if (! (yysize <= *yymsg_alloc + && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) + *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; + return 1; + } + + /* Avoid sprintf, as that infringes on the user's name space. + Don't have undefined behavior even if the translation + produced a string with the wrong number of "%s"s. */ + { + char *yyp = *yymsg; + int yyi = 0; + while ((*yyp = *yyformat) != '\0') + if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) + { + yyp += yytnamerr (yyp, yyarg[yyi++]); + yyformat += 2; + } + else + { + yyp++; + yyformat++; + } + } + return 0; +} +#endif /* YYERROR_VERBOSE */ + +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ + +static void +yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep) +{ + YYUSE (yyvaluep); + if (!yymsg) + yymsg = "Deleting"; + YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE (yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + + + + +/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; +/* Number of syntax errors so far. */ +int yynerrs; + + +/*----------. +| yyparse. | +`----------*/ + +int +yyparse (void) +{ + int yystate; + /* Number of tokens to shift before error messages enabled. */ + int yyerrstatus; + + /* The stacks and their tools: + 'yyss': related to states. + 'yyvs': related to semantic values. + + Refer to the stacks through separate pointers, to allow yyoverflow + to reallocate them elsewhere. */ + + /* The state stack. */ + yytype_int16 yyssa[YYINITDEPTH]; + yytype_int16 *yyss; + yytype_int16 *yyssp; + + /* The semantic value stack. */ + YYSTYPE yyvsa[YYINITDEPTH]; + YYSTYPE *yyvs; + YYSTYPE *yyvsp; + + YYSIZE_T yystacksize; + + int yyn; + int yyresult; + /* Lookahead token as an internal (translated) token number. */ + int yytoken = 0; + /* The variables used to return semantic value and location from the + action routines. */ + YYSTYPE yyval; + +#if YYERROR_VERBOSE + /* Buffer for error messages, and its allocated size. */ + char yymsgbuf[128]; + char *yymsg = yymsgbuf; + YYSIZE_T yymsg_alloc = sizeof yymsgbuf; +#endif + +#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) + + /* The number of symbols on the RHS of the reduced rule. + Keep to zero when no symbol should be popped. */ + int yylen = 0; + + yyssp = yyss = yyssa; + yyvsp = yyvs = yyvsa; + yystacksize = YYINITDEPTH; + + YYDPRINTF ((stderr, "Starting parse\n")); + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + goto yysetstate; + +/*------------------------------------------------------------. +| yynewstate -- Push a new state, which is found in yystate. | +`------------------------------------------------------------*/ + yynewstate: + /* In all cases, when you get here, the value and location stacks + have just been pushed. So pushing a state here evens the stacks. */ + yyssp++; + + yysetstate: + *yyssp = yystate; + + if (yyss + yystacksize - 1 <= yyssp) + { + /* Get the current used size of the three stacks, in elements. */ + YYSIZE_T yysize = yyssp - yyss + 1; + +#ifdef yyoverflow + { + /* Give user a chance to reallocate the stack. Use copies of + these so that the &'s don't force the real ones into + memory. */ + YYSTYPE *yyvs1 = yyvs; + yytype_int16 *yyss1 = yyss; + + /* Each stack pointer address is followed by the size of the + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ + yyoverflow (YY_("memory exhausted"), + &yyss1, yysize * sizeof (*yyssp), + &yyvs1, yysize * sizeof (*yyvsp), + &yystacksize); + + yyss = yyss1; + yyvs = yyvs1; + } +#else /* no yyoverflow */ +# ifndef YYSTACK_RELOCATE + goto yyexhaustedlab; +# else + /* Extend the stack our own way. */ + if (YYMAXDEPTH <= yystacksize) + goto yyexhaustedlab; + yystacksize *= 2; + if (YYMAXDEPTH < yystacksize) + yystacksize = YYMAXDEPTH; + + { + yytype_int16 *yyss1 = yyss; + union yyalloc *yyptr = + (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); + if (! yyptr) + goto yyexhaustedlab; + YYSTACK_RELOCATE (yyss_alloc, yyss); + YYSTACK_RELOCATE (yyvs_alloc, yyvs); +# undef YYSTACK_RELOCATE + if (yyss1 != yyssa) + YYSTACK_FREE (yyss1); + } +# endif +#endif /* no yyoverflow */ + + yyssp = yyss + yysize - 1; + yyvsp = yyvs + yysize - 1; + + YYDPRINTF ((stderr, "Stack size increased to %lu\n", + (unsigned long int) yystacksize)); + + if (yyss + yystacksize - 1 <= yyssp) + YYABORT; + } + + YYDPRINTF ((stderr, "Entering state %d\n", yystate)); + + if (yystate == YYFINAL) + YYACCEPT; + + goto yybackup; + +/*-----------. +| yybackup. | +`-----------*/ +yybackup: + + /* Do appropriate processing given the current state. Read a + lookahead token if we need one and don't already have one. */ + + /* First try to decide what to do without reference to lookahead token. */ + yyn = yypact[yystate]; + if (yypact_value_is_default (yyn)) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ + if (yychar == YYEMPTY) + { + YYDPRINTF ((stderr, "Reading a token: ")); + yychar = yylex (); + } + + if (yychar <= YYEOF) + { + yychar = yytoken = YYEOF; + YYDPRINTF ((stderr, "Now at end of input.\n")); + } + else + { + yytoken = YYTRANSLATE (yychar); + YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) + goto yydefault; + yyn = yytable[yyn]; + if (yyn <= 0) + { + if (yytable_value_is_error (yyn)) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + /* Count tokens shifted since error; after three, turn off error + status. */ + if (yyerrstatus) + yyerrstatus--; + + /* Shift the lookahead token. */ + YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); + + /* Discard the shifted token. */ + yychar = YYEMPTY; + + yystate = yyn; + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + goto yynewstate; + + +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ +yydefault: + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + goto yyreduce; + + +/*-----------------------------. +| yyreduce -- Do a reduction. | +`-----------------------------*/ +yyreduce: + /* yyn is the number of a rule to reduce with. */ + yylen = yyr2[yyn]; + + /* If YYLEN is nonzero, implement the default value of the action: + '$$ = $1'. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. Assigning to YYVAL + unconditionally makes the parser a bit smaller, and it avoids a + GCC warning that YYVAL may be used uninitialized. */ + yyval = yyvsp[1-yylen]; + + + YY_REDUCE_PRINT (yyn); + switch (yyn) + { + case 3: +#line 59 "wp_parser.y" /* yacc.c:1646 */ + { + wp_defexpr((yyvsp[-1].n)); + } +#line 1252 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 4: +#line 68 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnumber((yyvsp[0].d)); } +#line 1258 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 5: +#line 69 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newsymbol((yyvsp[0].s)); } +#line 1264 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 6: +#line 70 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_ADD, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1270 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 7: +#line 71 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_SUB, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1276 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 8: +#line 72 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_MUL, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1282 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 9: +#line 73 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_DIV, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1288 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 10: +#line 74 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = (yyvsp[-1].n); } +#line 1294 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 11: +#line 75 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_LT, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1300 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 12: +#line 76 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_GT, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1306 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 13: +#line 77 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_NEG, (yyvsp[0].n), NULL); } +#line 1312 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 14: +#line 78 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = (yyvsp[0].n); } +#line 1318 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 15: +#line 79 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_POW, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1324 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 16: +#line 80 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf1((yyvsp[-3].f1), (yyvsp[-1].n)); } +#line 1330 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 17: +#line 81 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2((yyvsp[-5].f2), (yyvsp[-3].n), (yyvsp[-1].n)); } +#line 1336 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + +#line 1340 "wp_parser.tab.c" /* yacc.c:1646 */ + default: break; + } + /* User semantic actions sometimes alter yychar, and that requires + that yytoken be updated with the new translation. We take the + approach of translating immediately before every use of yytoken. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action invokes + YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or + if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an + incorrect destructor might then be invoked immediately. In the + case of YYERROR or YYBACKUP, subsequent parser actions might lead + to an incorrect destructor call or verbose syntax error message + before the lookahead is translated. */ + YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); + + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + + *++yyvsp = yyval; + + /* Now 'shift' the result of the reduction. Determine what state + that goes to, based on the state we popped back to and the rule + number reduced by. */ + + yyn = yyr1[yyn]; + + yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; + if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) + yystate = yytable[yystate]; + else + yystate = yydefgoto[yyn - YYNTOKENS]; + + goto yynewstate; + + +/*--------------------------------------. +| yyerrlab -- here on detecting error. | +`--------------------------------------*/ +yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); + + /* If not already recovering from an error, report this error. */ + if (!yyerrstatus) + { + ++yynerrs; +#if ! YYERROR_VERBOSE + yyerror (YY_("syntax error")); +#else +# define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ + yyssp, yytoken) + { + char const *yymsgp = YY_("syntax error"); + int yysyntax_error_status; + yysyntax_error_status = YYSYNTAX_ERROR; + if (yysyntax_error_status == 0) + yymsgp = yymsg; + else if (yysyntax_error_status == 1) + { + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); + yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc); + if (!yymsg) + { + yymsg = yymsgbuf; + yymsg_alloc = sizeof yymsgbuf; + yysyntax_error_status = 2; + } + else + { + yysyntax_error_status = YYSYNTAX_ERROR; + yymsgp = yymsg; + } + } + yyerror (yymsgp); + if (yysyntax_error_status == 2) + goto yyexhaustedlab; + } +# undef YYSYNTAX_ERROR +#endif + } + + + + if (yyerrstatus == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + + if (yychar <= YYEOF) + { + /* Return failure if at end of input. */ + if (yychar == YYEOF) + YYABORT; + } + else + { + yydestruct ("Error: discarding", + yytoken, &yylval); + yychar = YYEMPTY; + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + + /* Pacify compilers like GCC when the user code never invokes + YYERROR and the label yyerrorlab therefore never appears in user + code. */ + if (/*CONSTCOND*/ 0) + goto yyerrorlab; + + /* Do not reclaim the symbols of the rule whose action triggered + this YYERROR. */ + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + yystate = *yyssp; + goto yyerrlab1; + + +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + yyerrstatus = 3; /* Each real token shifted decrements this. */ + + for (;;) + { + yyn = yypact[yystate]; + if (!yypact_value_is_default (yyn)) + { + yyn += YYTERROR; + if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) + { + yyn = yytable[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yyssp == yyss) + YYABORT; + + + yydestruct ("Error: popping", + yystos[yystate], yyvsp); + YYPOPSTACK (1); + yystate = *yyssp; + YY_STACK_PRINT (yyss, yyssp); + } + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + + /* Shift the error token. */ + YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp); + + yystate = yyn; + goto yynewstate; + + +/*-------------------------------------. +| yyacceptlab -- YYACCEPT comes here. | +`-------------------------------------*/ +yyacceptlab: + yyresult = 0; + goto yyreturn; + +/*-----------------------------------. +| yyabortlab -- YYABORT comes here. | +`-----------------------------------*/ +yyabortlab: + yyresult = 1; + goto yyreturn; + +#if !defined yyoverflow || YYERROR_VERBOSE +/*-------------------------------------------------. +| yyexhaustedlab -- memory exhaustion comes here. | +`-------------------------------------------------*/ +yyexhaustedlab: + yyerror (YY_("memory exhausted")); + yyresult = 2; + /* Fall through. */ +#endif + +yyreturn: + if (yychar != YYEMPTY) + { + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = YYTRANSLATE (yychar); + yydestruct ("Cleanup: discarding lookahead", + yytoken, &yylval); + } + /* Do not reclaim the symbols of the rule whose action triggered + this YYABORT or YYACCEPT. */ + YYPOPSTACK (yylen); + YY_STACK_PRINT (yyss, yyssp); + while (yyssp != yyss) + { + yydestruct ("Cleanup: popping", + yystos[*yyssp], yyvsp); + YYPOPSTACK (1); + } +#ifndef yyoverflow + if (yyss != yyssa) + YYSTACK_FREE (yyss); +#endif +#if YYERROR_VERBOSE + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); +#endif + return yyresult; +} +#line 84 "wp_parser.y" /* yacc.c:1906 */ + diff --git a/Source/Parser/wp_parser.tab.h b/Source/Parser/wp_parser.tab.h new file mode 100644 index 000000000..f36b4b611 --- /dev/null +++ b/Source/Parser/wp_parser.tab.h @@ -0,0 +1,86 @@ +/* A Bison parser, made by GNU Bison 3.0.4. */ + +/* Bison interface for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +#ifndef YY_YY_WP_PARSER_TAB_H_INCLUDED +# define YY_YY_WP_PARSER_TAB_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + NODE = 258, + NUMBER = 259, + SYMBOL = 260, + F1 = 261, + F2 = 262, + EOL = 263, + POW = 264, + NEG = 265, + UPLUS = 266 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED + +union YYSTYPE +{ +#line 19 "wp_parser.y" /* yacc.c:1909 */ + + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; + +#line 74 "wp_parser.tab.h" /* yacc.c:1909 */ +}; + +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + + +extern YYSTYPE yylval; + +int yyparse (void); + +#endif /* !YY_YY_WP_PARSER_TAB_H_INCLUDED */ diff --git a/Source/Parser/wp_parser.y b/Source/Parser/wp_parser.y new file mode 100644 index 000000000..3081125cc --- /dev/null +++ b/Source/Parser/wp_parser.y @@ -0,0 +1,84 @@ + +%{ + #include <stdio.h> + #include <stdlib.h> + #include <math.h> + #include "wp_parser_y.h" + int yylex (void); +%} + +/* We do not need to make this reentrant safe, because we use flex and + bison for generating AST only and this part doesn't need to be + thread safe. +*/ +/*%define api.pure full */ + +/* This is the type returned by functions wp_new* declared in + wp_parser_y.h. See also bison rules at the end of this file. +*/ +%union { + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; +} + +/* Define tokens. They are used by flex too. */ +%token <n> NODE +%token <d> NUMBER +%token <s> SYMBOL +%token <f1> F1 +%token <f2> F2 +%token EOL +%token POW "**" '^' + +%nonassoc F1 F2 +%right '=' +%left '+' '-' +%left '*' '/' +%left '<' '>' +%nonassoc NEG UPLUS +%right POW + +/* This specifies the type of `exp` (i.e., struct wp_node*). Rules + specified later pass `exp` to wp_new* functions declared in + wp_parser_y.h. +*/ +%type <n> exp + +%start input + +%% + +/* Given `\n` terminated input, a tree is generated and passed to + * function wp_defexpr defined in wp_parser_y.c. + */ +input: + %empty +| input exp EOL { + wp_defexpr($2); + } +; + +/* Enum types WP_ADD, WP_SUB, etc. are defined in wp_parser_y.h. + * Functions wp_new* are also declared in that file. + */ +exp: + NUMBER { $$ = wp_newnumber($1); } +| SYMBOL { $$ = wp_newsymbol($1); } +| exp '+' exp { $$ = wp_newnode(WP_ADD, $1, $3); } +| exp '-' exp { $$ = wp_newnode(WP_SUB, $1, $3); } +| exp '*' exp { $$ = wp_newnode(WP_MUL, $1, $3); } +| exp '/' exp { $$ = wp_newnode(WP_DIV, $1, $3); } +| '(' exp ')' { $$ = $2; } +| exp '<' exp { $$ = wp_newf2(WP_LT, $1, $3); } +| exp '>' exp { $$ = wp_newf2(WP_GT, $1, $3); } +| '-'exp %prec NEG { $$ = wp_newnode(WP_NEG, $2, NULL); } +| '+'exp %prec UPLUS { $$ = $2; } +| exp POW exp { $$ = wp_newf2(WP_POW, $1, $3); } +| F1 '(' exp ')' { $$ = wp_newf1($1, $3); } +| F2 '(' exp ',' exp ')' { $$ = wp_newf2($1, $3, $5); } +; + +%% diff --git a/Source/Parser/wp_parser_c.c b/Source/Parser/wp_parser_c.c new file mode 100644 index 000000000..ad7d5ec51 --- /dev/null +++ b/Source/Parser/wp_parser_c.c @@ -0,0 +1,13 @@ +#include "wp_parser_c.h" +#include "wp_parser.lex.h" +#include "wp_parser.tab.h" + +struct wp_parser* +wp_c_parser_new (char const* body) +{ + YY_BUFFER_STATE buffer = yy_scan_string(body); + yyparse(); + struct wp_parser* parser = wp_parser_new(); + yy_delete_buffer(buffer); + return parser; +} diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h new file mode 100644 index 000000000..d810bd685 --- /dev/null +++ b/Source/Parser/wp_parser_c.h @@ -0,0 +1,139 @@ +#ifndef WP_PARSER_C_H_ +#define WP_PARSER_C_H_ + +#include "wp_parser_y.h" + +#ifdef __cplusplus +extern "C" { +#endif + + struct wp_parser* wp_c_parser_new (char const* function_body); + +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus + +#include <set> +#include <string> + +inline +double +wp_ast_eval (struct wp_node* node) +{ + double result; + + switch (node->type) + { + case WP_NUMBER: + result = ((struct wp_number*)node)->value; + break; + case WP_SYMBOL: + result = *(((struct wp_symbol*)node)->pointer); + break; + case WP_ADD: + result = wp_ast_eval(node->l) + wp_ast_eval(node->r); + break; + case WP_SUB: + result = wp_ast_eval(node->l) - wp_ast_eval(node->r); + break; + case WP_MUL: + result = wp_ast_eval(node->l) * wp_ast_eval(node->r); + break; + case WP_DIV: + result = wp_ast_eval(node->l) / wp_ast_eval(node->r); + break; + case WP_NEG: + result = -wp_ast_eval(node->l); + break; + case WP_F1: + result = wp_call_f1(((struct wp_f1*)node)->ftype, + wp_ast_eval(((struct wp_f1*)node)->l)); + break; + case WP_F2: + result = wp_call_f2(((struct wp_f2*)node)->ftype, + wp_ast_eval(((struct wp_f2*)node)->l), + wp_ast_eval(((struct wp_f2*)node)->r)); + break; + case WP_ADD_VP: + result = node->lvp.v + *(node->rp); + break; + case WP_ADD_PP: + result = *(node->lvp.p) + *(node->rp); + break; + case WP_SUB_VP: + result = node->lvp.v - *(node->rp); + break; + case WP_SUB_PP: + result = *(node->lvp.p) - *(node->rp); + break; + case WP_MUL_VP: + result = node->lvp.v * *(node->rp); + break; + case WP_MUL_PP: + result = *(node->lvp.p) * *(node->rp); + break; + case WP_DIV_VP: + result = node->lvp.v / *(node->rp); + break; + case WP_DIV_PP: + result = *(node->lvp.p) / *(node->rp); + break; + case WP_NEG_P: + result = -*(node->lvp.p); + break; + default: + yyerror("wp_ast_eval: unknown node type %d\n", node->type); + } + + return result; +} + +inline +void +wp_ast_get_symbols (struct wp_node* node, std::set<std::string>& symbols) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + symbols.emplace(((struct wp_symbol*)node)->name); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_get_symbols(node->l, symbols); + wp_ast_get_symbols(node->r, symbols); + break; + case WP_NEG: + case WP_NEG_P: + wp_ast_get_symbols(node->l, symbols); + break; + case WP_F1: + wp_ast_get_symbols(((struct wp_f1*)node)->l, symbols); + break; + case WP_F2: + wp_ast_get_symbols(((struct wp_f2*)node)->l, symbols); + wp_ast_get_symbols(((struct wp_f2*)node)->r, symbols); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_get_symbols(node->r, symbols); + break; + default: + yyerror("wp_ast_get_symbols: unknown node type %d\n", node->type); + } +} + +#endif + +#endif diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c new file mode 100644 index 000000000..402c4e67b --- /dev/null +++ b/Source/Parser/wp_parser_y.c @@ -0,0 +1,1018 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <math.h> +#include "wp_parser_y.h" +#include "wp_parser.tab.h" + +static struct wp_node* wp_root = NULL; + +/* This is called by a bison rule to store the original AST in a + * static variable. Accessing this directly is not thread safe. So + * this will be duplicated later for each thread. + */ +void +wp_defexpr (struct wp_node* body) +{ + wp_root = body; +} + +struct wp_node* +wp_newnumber (double d) +{ + struct wp_number* r = (struct wp_number*) malloc(sizeof(struct wp_number)); + r->type = WP_NUMBER; + r->value = d; + return (struct wp_node*) r; +} + +struct wp_symbol* +wp_makesymbol (char* name) +{ + struct wp_symbol* symbol = (struct wp_symbol*) malloc(sizeof(struct wp_symbol)); + symbol->type = WP_SYMBOL; + symbol->name = strdup(name); + symbol->pointer = NULL; + return symbol; +} + +struct wp_node* +wp_newsymbol (struct wp_symbol* symbol) +{ + return (struct wp_node*) symbol; +} + +struct wp_node* +wp_newnode (enum wp_node_t type, struct wp_node* l, struct wp_node* r) +{ + struct wp_node* tmp = (struct wp_node*) malloc(sizeof(struct wp_node)); + tmp->type = type; + tmp->l = l; + tmp->r = r; + return tmp; +} + +struct wp_node* +wp_newf1 (enum wp_f1_t ftype, struct wp_node* l) +{ + struct wp_f1* tmp = (struct wp_f1*) malloc(sizeof(struct wp_f1)); + tmp->type = WP_F1; + tmp->l = l; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +struct wp_node* +wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, struct wp_node* r) +{ + struct wp_f2* tmp = (struct wp_f2*) malloc(sizeof(struct wp_f2)); + tmp->type = WP_F2; + tmp->l = l; + tmp->r = r; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +void +yyerror (char const *s, ...) +{ + va_list vl; + va_start(vl, s); + vfprintf(stderr, s, vl); + fprintf(stderr, "\n"); + va_end(vl); +} + +/*******************************************************************/ + +struct wp_parser* +wp_parser_new (void) +{ + struct wp_parser* my_parser = (struct wp_parser*) malloc(sizeof(struct wp_parser)); + + my_parser->sz_mempool = wp_ast_size(wp_root); + my_parser->p_root = malloc(my_parser->sz_mempool); + my_parser->p_free = my_parser->p_root; + + my_parser->ast = wp_parser_ast_dup(my_parser, wp_root,1); /* 1: free the source wp_root */ + + if (my_parser->p_root + my_parser->sz_mempool != my_parser->p_free) { + yyerror("wp_parser_new: error in memory size"); + exit(1); + } + + wp_ast_optimize(my_parser->ast); + + return my_parser; +} + +void +wp_parser_delete (struct wp_parser* parser) +{ + free(parser->p_root); + free(parser); +} + +static size_t +wp_aligned_size (size_t N) +{ + const unsigned int align_size = 16; + size_t x = N + (align_size-1); + x -= x & (align_size-1); + return x; +} + +static +void* +wp_parser_allocate (struct wp_parser* my_parser, size_t N) +{ + void* r = my_parser->p_free; + my_parser->p_free = (char*)r + wp_aligned_size(N); + return r; +} + +struct wp_parser* +wp_parser_dup (struct wp_parser* source) +{ + struct wp_parser* dest = (struct wp_parser*) malloc(sizeof(struct wp_parser)); + dest->sz_mempool = source->sz_mempool; + dest->p_root = malloc(dest->sz_mempool); + dest->p_free = dest->p_root; + + dest->ast = wp_parser_ast_dup(dest, source->ast, 0); /* 0: don't free the source */ + + return dest; +} + +double +wp_call_f1 (enum wp_f1_t type, double a) +{ + switch (type) { + case WP_SQRT: return sqrt(a); + case WP_EXP: return exp(a); + case WP_LOG: return log(a); + case WP_LOG10: return log10(a); + case WP_SIN: return sin(a); + case WP_COS: return cos(a); + case WP_TAN: return tan(a); + case WP_ASIN: return asin(a); + case WP_ACOS: return acos(a); + case WP_ATAN: return atan(a); + case WP_SINH: return sinh(a); + case WP_COSH: return cosh(a); + case WP_TANH: return tanh(a); + case WP_ABS: return fabs(a); + case WP_POW_M3: return 1.0/(a*a*a); + case WP_POW_M2: return 1.0/(a*a); + case WP_POW_M1: return 1.0/a; + case WP_POW_P1: return a; + case WP_POW_P2: return a*a; + case WP_POW_P3: return a*a*a; + default: + yyerror("wp_call_f1: Unknow function %d", type); + return 0.0; + } +} + +double +wp_call_f2 (enum wp_f2_t type, double a, double b) +{ + switch (type) { + case WP_POW: + return pow(a,b); + case WP_GT: + return (a > b) ? 1.0 : 0.0; + case WP_LT: + return (a < b) ? 1.0 : 0.0; + case WP_HEAVISIDE: + return (a < 0.0) ? 0.0 : ((a > 0.0) ? 1.0 : b); + case WP_MIN: + return (a < b) ? a : b; + case WP_MAX: + return (a > b) ? a : b; + default: + yyerror("wp_call_f2: Unknow function %d", type); + return 0.0; + } +} + +size_t +wp_ast_size (struct wp_node* node) +{ + size_t result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_aligned_size(sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_aligned_size(sizeof(struct wp_symbol)) + + wp_aligned_size(strlen(((struct wp_symbol*)node)->name)+1); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_NEG: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + case WP_F1: + result = wp_aligned_size(sizeof(struct wp_f1)) + + wp_ast_size(node->l); + break; + case WP_F2: + result = wp_aligned_size(sizeof(struct wp_f2)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->r); + break; + case WP_NEG_P: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + default: + yyerror("wp_ast_size: unknown node type %d\n", node->type); + exit(1); + } + + return result; +} + +struct wp_node* +wp_parser_ast_dup (struct wp_parser* my_parser, struct wp_node* node, int move) +{ + void* result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_parser_allocate(my_parser, sizeof(struct wp_number)); + memcpy(result, node , sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_parser_allocate(my_parser, sizeof(struct wp_symbol)); + memcpy(result, node , sizeof(struct wp_symbol)); + ((struct wp_symbol*)result)->name = (char*) wp_parser_allocate + (my_parser, strlen(((struct wp_symbol*)node)->name)+1); + strcpy(((struct wp_symbol*)result)->name, + ((struct wp_symbol*)node )->name); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = NULL; + break; + case WP_F1: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f1)); + memcpy(result, node , sizeof(struct wp_f1)); + ((struct wp_f1*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f1*)node)->l, move); + break; + case WP_F2: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f2)); + memcpy(result, node , sizeof(struct wp_f2)); + ((struct wp_f2*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->l, move); + ((struct wp_f2*)result)->r = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->r, move); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG_P: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + break; + default: + yyerror("wp_ast_dup: unknown node type %d\n", node->type); + exit(1); + } + if (move) { + /* Note that we only do this on the original AST. We do not + * need to call free for AST stored in wp_parser because the + * memory is not allocated with malloc directly. + */ + if (node->type == WP_SYMBOL) { + free(((struct wp_symbol*)node)->name); + } + free((void*)node); + } + return (struct wp_node*)result; +} + +#define WP_MOVEUP_R(node, v) \ + struct wp_node* n = node->r->r; \ + double* p = node->r->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_MOVEUP_L(node, v) \ + struct wp_node* n = node->l->r; \ + double* p = node->l->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_EVAL_R(node) node->r->lvp.v +#define WP_EVAL_L(node) node->l->lvp.v + +#define WP_NEG_MOVEUP(node) \ + node->r = node->l->r; \ + node->lvp.v = -node->l->lvp.v; \ + node->rp = node->l->rp; + +void +wp_ast_optimize (struct wp_node* node) +{ + /* No need to free memory because we only call this on ASTs in + * wp_parser that are allocated from the memory pool. + */ + switch (node->type) + { + case WP_NUMBER: + case WP_SYMBOL: + break; + case WP_ADD: + case WP_ADD_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = ((struct wp_number*)(node->l))->value + + ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = ((struct wp_number*)(node->l))->value; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = ((struct wp_number*)(node->r))->value; + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_SUB: + case WP_SUB_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = ((struct wp_number*)(node->l))->value + - ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = ((struct wp_number*)(node->l))->value; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = -((struct wp_number*)(node->r))->value; + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_MUL: + case WP_MUL_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = ((struct wp_number*)(node->l))->value + * ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = ((struct wp_number*)(node->l))->value; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = ((struct wp_number*)(node->r))->value; + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_DIV: + case WP_DIV_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = ((struct wp_number*)(node->l))->value + / ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = ((struct wp_number*)(node->l))->value; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = 1./((struct wp_number*)(node->r))->value; + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_NEG: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = -((struct wp_number*)(node->l))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->type = WP_NEG_P; + } + else if (node->l->type == WP_ADD_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SUB_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_MUL_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_DIV_VP; + } + break; + case WP_F1: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = wp_call_f1 + (((struct wp_f1*)node)->ftype, + ((struct wp_number*)(((struct wp_f1*)node)->l))->value); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_F2: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_call_f2 + (((struct wp_f2*)node)->ftype, + ((struct wp_number*)(((struct wp_f2*)node)->l))->value, + ((struct wp_number*)(((struct wp_f2*)node)->r))->value); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->r->type == WP_NUMBER && ((struct wp_f2*)node)->ftype == WP_POW) + { + struct wp_node* n = node->l; + double v = ((struct wp_number*)(node->r))->value; + if (-3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M3; + } else if (-2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M2; + } else if (-1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M1; + } else if (0.0 == v) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = 1.0; + } else if (1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P1; + } else if (2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P2; + } else if (3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P3; + } + } + break; + case WP_ADD_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v + ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_SUB_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v - ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_MUL_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v * ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_DIV_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v / ((struct wp_number*)(node->r))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_NEG_P: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = -((struct wp_number*)(node->l))->value; + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + default: + yyerror("wp_ast_optimize: unknown node type %d\n", node->type); + exit(1); + } +} + +static +void +wp_ast_print_f1 (struct wp_f1* f1) +{ + wp_ast_print(f1->l); + switch (f1->ftype) { + case WP_SQRT: printf("SQRT\n"); break; + case WP_EXP: printf("EXP\n"); break; + case WP_LOG: printf("LOG\n"); break; + case WP_LOG10: printf("LOG10\n"); break; + case WP_SIN: printf("SIN\n"); break; + case WP_COS: printf("COS\n"); break; + case WP_TAN: printf("TAN\n"); break; + case WP_ASIN: printf("ASIN\n"); break; + case WP_ACOS: printf("ACOS\n"); break; + case WP_ATAN: printf("ATAN\n"); break; + case WP_SINH: printf("SINH\n"); break; + case WP_COSH: printf("COSH\n"); break; + case WP_TANH: printf("TANH\n"); break; + case WP_ABS: printf("ABS\n"); break; + case WP_POW_M3: printf("POW(,-3)\n"); break; + case WP_POW_M2: printf("POW(,-2)\n"); break; + case WP_POW_M1: printf("POW(,-1)\n"); break; + case WP_POW_P1: printf("POW(,1)\n"); break; + case WP_POW_P2: printf("POW(,2)\n"); break; + case WP_POW_P3: printf("POW(,3)\n"); break; + default: + yyerror("wp_ast+print_f1: Unknow function %d", f1->ftype); + } +} + +static +void +wp_ast_print_f2 (struct wp_f2* f2) +{ + wp_ast_print(f2->l); + wp_ast_print(f2->r); + switch (f2->ftype) { + case WP_POW: + printf("POW\n"); + break; + case WP_GT: + printf("GT\n"); + break; + case WP_LT: + printf("LT\n"); + break; + case WP_HEAVISIDE: + printf("HEAVISIDE\n"); + break; + case WP_MIN: + printf("MIN\n"); + break; + case WP_MAX: + printf("MAX\n"); + break; + default: + yyerror("wp_ast_print_f2: Unknow function %d", f2->ftype); + } +} + +void +wp_ast_print (struct wp_node* node) +{ + switch (node->type) + { + case WP_NUMBER: + printf("NUMBER: %.17g\n", ((struct wp_number*)node)->value); + break; + case WP_SYMBOL: + printf("VARIABLE: %s\n", ((struct wp_symbol*)node)->name); + break; + case WP_ADD: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("ADD\n"); + break; + case WP_SUB: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("SUB\n"); + break; + case WP_MUL: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("MUL\n"); + break; + case WP_DIV: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("DIV\n"); + break; + case WP_NEG: + wp_ast_print(node->l); + printf("NEG\n"); + break; + case WP_F1: + wp_ast_print_f1((struct wp_f1*)node); + break; + case WP_F2: + wp_ast_print_f2((struct wp_f2*)node); + break; + case WP_ADD_VP: + printf("ADD: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_VP: + printf("SUM: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_VP: + printf("MUL: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_VP: + printf("DIV: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_NEG_P: + printf("NEG: %s\n", ((struct wp_symbol*)(node->l))->name); + break; + case WP_ADD_PP: + printf("ADD: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_PP: + printf("SUB: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_PP: + printf("MUL: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_PP: + printf("DIV: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + default: + yyerror("wp_ast_print: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_ast_regvar (struct wp_node* node, char const* name, double* p) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_symbol*)node)->pointer = p; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_NEG: + wp_ast_regvar(node->l, name, p); + break; + case WP_F1: + wp_ast_regvar(node->l, name, p); + break; + case WP_F2: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_regvar(node->r, name, p); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + case WP_NEG_P: + wp_ast_regvar(node->l, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + default: + yyerror("wp_ast_regvar: unknown node type %d\n", node->type); + exit(1); + } +} + +void wp_ast_setconst (struct wp_node* node, char const* name, double c) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = c; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG: + wp_ast_setconst(node->l, name, c); + break; + case WP_F1: + wp_ast_setconst(node->l, name, c); + break; + case WP_F2: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG_P: + wp_ast_setconst(node->l, name, c); + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + default: + yyerror("wp_ast_setconst: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_parser_regvar (struct wp_parser* parser, char const* name, double* p) +{ + wp_ast_regvar(parser->ast, name, p); +} + +void +wp_parser_setconst (struct wp_parser* parser, char const* name, double c) +{ + wp_ast_setconst(parser->ast, name, c); + wp_ast_optimize(parser->ast); +} + diff --git a/Source/Parser/wp_parser_y.h b/Source/Parser/wp_parser_y.h new file mode 100644 index 000000000..c583d1a33 --- /dev/null +++ b/Source/Parser/wp_parser_y.h @@ -0,0 +1,159 @@ +#ifndef WP_PARSER_Y_H_ +#define WP_PARSER_Y_H_ + +#ifdef __cplusplus +#include <cstdlib> +extern "C" { +#else +#include <stdlib.h> +#endif + +enum wp_f1_t { // Bulit-in functions with one argument + WP_SQRT = 1, + WP_EXP, + WP_LOG, + WP_LOG10, + WP_SIN, + WP_COS, + WP_TAN, + WP_ASIN, + WP_ACOS, + WP_ATAN, + WP_SINH, + WP_COSH, + WP_TANH, + WP_ABS, + WP_POW_M3, + WP_POW_M2, + WP_POW_M1, + WP_POW_P1, + WP_POW_P2, + WP_POW_P3 +}; + +enum wp_f2_t { // Built-in functions with two arguments + WP_POW = 1, + WP_GT, + WP_LT, + WP_HEAVISIDE, + WP_MIN, + WP_MAX +}; + +enum wp_node_t { + WP_NUMBER = 1, + WP_SYMBOL, + WP_ADD, + WP_SUB, + WP_MUL, + WP_DIV, + WP_NEG, + WP_F1, + WP_F2, + WP_ADD_VP, /* types below are generated by optimization */ + WP_ADD_PP, + WP_SUB_VP, + WP_SUB_PP, + WP_MUL_VP, + WP_MUL_PP, + WP_DIV_VP, + WP_DIV_PP, + WP_NEG_P +}; + +/* In C, the address of the first member of a struct is the same as + * the address of the struct itself. Because of this, all struct wp_* + * pointers can be passed around as struct wp_node pointer and enum + * wp_node_t type can be safely checked to determine their real type. + */ + +union wp_vp { + double v; + double* p; +}; + +struct wp_node { + enum wp_node_t type; + struct wp_node* l; + struct wp_node* r; + union wp_vp lvp; // After optimization, this may store left value/pointer. + double* rp; // this may store right pointer. +}; + +struct wp_number { + enum wp_node_t type; + double value; +}; + +struct wp_symbol { + enum wp_node_t type; + char* name; + double* pointer; +}; + +struct wp_f1 { /* Builtin functions with one argument */ + enum wp_node_t type; + struct wp_node* l; + enum wp_f1_t ftype; +}; + +struct wp_f2 { /* Builtin functions with two arguments */ + enum wp_node_t type; + struct wp_node* l; + struct wp_node* r; + enum wp_f2_t ftype; +}; + +/*******************************************************************/ + +/* These functions are used in bison rules to generate the original + * AST. */ +void wp_defexpr (struct wp_node* body); +struct wp_node* wp_newnumber (double d); +struct wp_symbol* wp_makesymbol (char* name); +struct wp_node* wp_newsymbol (struct wp_symbol* sym); +struct wp_node* wp_newnode (enum wp_node_t type, struct wp_node* l, + struct wp_node* r); +struct wp_node* wp_newf1 (enum wp_f1_t ftype, struct wp_node* l); +struct wp_node* wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, + struct wp_node* r); + +void yyerror (char const *s, ...); + +/*******************************************************************/ + +/* This is our struct for storing AST in a more packed way. The whole + * tree is stored in a contiguous chunk of memory starting from void* + * p_root with a size of sz_mempool. + */ +struct wp_parser { + void* p_root; + void* p_free; + struct wp_node* ast; + size_t sz_mempool; +}; + +struct wp_parser* wp_parser_new (void); +void wp_parser_delete (struct wp_parser* parser); + +struct wp_parser* wp_parser_dup (struct wp_parser* source); +struct wp_node* wp_parser_ast_dup (struct wp_parser* parser, struct wp_node* src, int move); + +void wp_parser_regvar (struct wp_parser* parser, char const* name, double* p); +void wp_parser_setconst (struct wp_parser* parser, char const* name, double c); + +/* We need to walk the tree in these functions */ +void wp_ast_optimize (struct wp_node* node); +size_t wp_ast_size (struct wp_node* node); +void wp_ast_print (struct wp_node* node); +void wp_ast_regvar (struct wp_node* node, char const* name, double* p); +void wp_ast_setconst (struct wp_node* node, char const* name, double c); + +double wp_call_f1 (enum wp_f1_t type, double a); +double wp_call_f2 (enum wp_f2_t type, double a, double b); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Source/Particles/MultiParticleContainer.H b/Source/Particles/MultiParticleContainer.H index b21247ff6..b0968b0cc 100644 --- a/Source/Particles/MultiParticleContainer.H +++ b/Source/Particles/MultiParticleContainer.H @@ -127,10 +127,12 @@ public: /// std::unique_ptr<amrex::MultiFab> GetChargeDensity(int lev, bool local = false); - void Checkpoint (const std::string& dir, - bool is_checkpoint, - const amrex::Vector<std::string>& varnames = amrex::Vector<std::string>()) const; + void Checkpoint (const std::string& dir) const; + void WritePlotFile( const std::string& dir, + const amrex::Vector<int>& real_flags, + const amrex::Vector<std::string>& real_names) const; + void Restart (const std::string& dir); void PostRestart (); diff --git a/Source/Particles/PhysicalParticleContainer.H b/Source/Particles/PhysicalParticleContainer.H index 5847a6359..362683879 100644 --- a/Source/Particles/PhysicalParticleContainer.H +++ b/Source/Particles/PhysicalParticleContainer.H @@ -64,10 +64,10 @@ public: amrex::Real dt) override; virtual void PushPX(WarpXParIter& pti, - amrex::Cuda::DeviceVector<amrex::Real>& xp, - amrex::Cuda::DeviceVector<amrex::Real>& yp, - amrex::Cuda::DeviceVector<amrex::Real>& zp, - amrex::Cuda::DeviceVector<amrex::Real>& giv, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& xp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& yp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& zp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& giv, amrex::Real dt); virtual void PushP (int lev, amrex::Real dt, diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index 07468a85d..e31d43204 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -984,7 +984,7 @@ PhysicalParticleContainer::FieldGather (int lev, #pragma omp parallel #endif { - Cuda::DeviceVector<Real> xp, yp, zp; + Cuda::ManagedDeviceVector<Real> xp, yp, zp; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) { @@ -1105,10 +1105,6 @@ PhysicalParticleContainer::Evolve (int lev, #else int thread_num = 0; #endif - if (local_rho[thread_num] == nullptr) local_rho[thread_num].reset( new amrex::FArrayBox()); - if (local_jx[thread_num] == nullptr) local_jx[thread_num].reset( new amrex::FArrayBox()); - if (local_jy[thread_num] == nullptr) local_jy[thread_num].reset( new amrex::FArrayBox()); - if (local_jz[thread_num] == nullptr) local_jz[thread_num].reset( new amrex::FArrayBox()); FArrayBox filtered_Ex, filtered_Ey, filtered_Ez; FArrayBox filtered_Bx, filtered_By, filtered_Bz; @@ -1501,7 +1497,7 @@ PhysicalParticleContainer::SplitParticles(int lev) { auto& mypc = WarpX::GetInstance().GetPartContainer(); auto& pctmp_split = mypc.GetPCtmp(); - Cuda::DeviceVector<Real> xp, yp, zp; + Cuda::ManagedDeviceVector<Real> xp, yp, zp; RealVector psplit_x, psplit_y, psplit_z, psplit_w; RealVector psplit_ux, psplit_uy, psplit_uz; long np_split_to_add = 0; @@ -1650,10 +1646,10 @@ PhysicalParticleContainer::SplitParticles(int lev) void PhysicalParticleContainer::PushPX(WarpXParIter& pti, - Cuda::DeviceVector<Real>& xp, - Cuda::DeviceVector<Real>& yp, - Cuda::DeviceVector<Real>& zp, - Cuda::DeviceVector<Real>& giv, + Cuda::ManagedDeviceVector<Real>& xp, + Cuda::ManagedDeviceVector<Real>& yp, + Cuda::ManagedDeviceVector<Real>& zp, + Cuda::ManagedDeviceVector<Real>& giv, Real dt) { diff --git a/Source/Particles/RigidInjectedParticleContainer.H b/Source/Particles/RigidInjectedParticleContainer.H index 8a9ac9e6e..d3a69f5b0 100644 --- a/Source/Particles/RigidInjectedParticleContainer.H +++ b/Source/Particles/RigidInjectedParticleContainer.H @@ -43,10 +43,10 @@ public: amrex::Real dt) override; virtual void PushPX(WarpXParIter& pti, - amrex::Cuda::DeviceVector<amrex::Real>& xp, - amrex::Cuda::DeviceVector<amrex::Real>& yp, - amrex::Cuda::DeviceVector<amrex::Real>& zp, - amrex::Cuda::DeviceVector<amrex::Real>& giv, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& xp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& yp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& zp, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& giv, amrex::Real dt) override; virtual void PushP (int lev, amrex::Real dt, diff --git a/Source/Particles/RigidInjectedParticleContainer.cpp b/Source/Particles/RigidInjectedParticleContainer.cpp index db3623705..3ee4d87e5 100644 --- a/Source/Particles/RigidInjectedParticleContainer.cpp +++ b/Source/Particles/RigidInjectedParticleContainer.cpp @@ -73,7 +73,7 @@ RigidInjectedParticleContainer::RemapParticles() // Note that the particles are already in the boosted frame. // This value is saved to advance the particles not injected yet - Cuda::DeviceVector<Real> xp, yp, zp; + Cuda::ManagedDeviceVector<Real> xp, yp, zp; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) { @@ -134,7 +134,7 @@ RigidInjectedParticleContainer::BoostandRemapParticles() #pragma omp parallel #endif { - Cuda::DeviceVector<Real> xp, yp, zp; + Cuda::ManagedDeviceVector<Real> xp, yp, zp; for (WarpXParIter pti(*this, 0); pti.isValid(); ++pti) { @@ -205,10 +205,10 @@ RigidInjectedParticleContainer::BoostandRemapParticles() void RigidInjectedParticleContainer::PushPX(WarpXParIter& pti, - Cuda::DeviceVector<Real>& xp, - Cuda::DeviceVector<Real>& yp, - Cuda::DeviceVector<Real>& zp, - Cuda::DeviceVector<Real>& giv, + Cuda::ManagedDeviceVector<Real>& xp, + Cuda::ManagedDeviceVector<Real>& yp, + Cuda::ManagedDeviceVector<Real>& zp, + Cuda::ManagedDeviceVector<Real>& giv, Real dt) { @@ -241,7 +241,7 @@ RigidInjectedParticleContainer::PushPX(WarpXParIter& pti, #endif // Save the position and momenta, making copies - Cuda::DeviceVector<Real> xp_save, yp_save, zp_save; + Cuda::ManagedDeviceVector<Real> xp_save, yp_save, zp_save; RealVector uxp_save, uyp_save, uzp_save; if (!done_injecting_lev) { @@ -362,7 +362,7 @@ RigidInjectedParticleContainer::PushP (int lev, Real dt, #pragma omp parallel #endif { - Cuda::DeviceVector<Real> xp, yp, zp, giv; + Cuda::ManagedDeviceVector<Real> xp, yp, zp, giv; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) { diff --git a/Source/Particles/WarpXParticleContainer.H b/Source/Particles/WarpXParticleContainer.H index 924980f81..050060b47 100644 --- a/Source/Particles/WarpXParticleContainer.H +++ b/Source/Particles/WarpXParticleContainer.H @@ -30,6 +30,30 @@ struct DiagIdx }; }; +namespace ParticleStringNames +{ + const std::map<std::string, int> to_index = { + {"w", PIdx::w }, + {"ux", PIdx::ux }, + {"uy", PIdx::uy }, + {"uz", PIdx::uz }, + {"Ex", PIdx::Ex }, + {"Ey", PIdx::Ey }, + {"Ez", PIdx::Ez }, + {"Bx", PIdx::Bx }, + {"By", PIdx::By }, + {"Bz", PIdx::Bz }, +#ifdef WARPX_STORE_OLD_PARTICLE_ATTRIBS + {"xold", PIdx::xold }, + {"yold", PIdx::yold }, + {"zold", PIdx::zold }, + {"uxold", PIdx::uxold}, + {"uyold", PIdx::uyold}, + {"uzold", PIdx::uzold}, +#endif + }; +} + class WarpXParIter : public amrex::ParIter<0,0,PIdx::nattribs> { @@ -39,12 +63,12 @@ public: WarpXParIter (ContainerType& pc, int level); #if (AMREX_SPACEDIM == 2) - void GetPosition (amrex::Cuda::DeviceVector<amrex::Real>& x, - amrex::Cuda::DeviceVector<amrex::Real>& y, - amrex::Cuda::DeviceVector<amrex::Real>& z) const; - void SetPosition (const amrex::Cuda::DeviceVector<amrex::Real>& x, - const amrex::Cuda::DeviceVector<amrex::Real>& y, - const amrex::Cuda::DeviceVector<amrex::Real>& z); + void GetPosition (amrex::Cuda::ManagedDeviceVector<amrex::Real>& x, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& y, + amrex::Cuda::ManagedDeviceVector<amrex::Real>& z) const; + void SetPosition (const amrex::Cuda::ManagedDeviceVector<amrex::Real>& x, + const amrex::Cuda::ManagedDeviceVector<amrex::Real>& y, + const amrex::Cuda::ManagedDeviceVector<amrex::Real>& z); #endif const std::array<RealVector, PIdx::nattribs>& GetAttribs () const { @@ -218,12 +242,12 @@ protected: static int do_not_push; - amrex::Vector<std::unique_ptr<amrex::FArrayBox> > local_rho; - amrex::Vector<std::unique_ptr<amrex::FArrayBox> > local_jx; - amrex::Vector<std::unique_ptr<amrex::FArrayBox> > local_jy; - amrex::Vector<std::unique_ptr<amrex::FArrayBox> > local_jz; + amrex::Vector<amrex::FArrayBox> local_rho; + amrex::Vector<amrex::FArrayBox> local_jx; + amrex::Vector<amrex::FArrayBox> local_jy; + amrex::Vector<amrex::FArrayBox> local_jz; - amrex::Vector<amrex::Cuda::DeviceVector<amrex::Real> > m_xp, m_yp, m_zp, m_giv; + amrex::Vector<amrex::Cuda::ManagedDeviceVector<amrex::Real> > m_xp, m_yp, m_zp, m_giv; private: virtual void particlePostLocate(ParticleType& p, const amrex::ParticleLocData& pld, diff --git a/Source/Particles/WarpXParticleContainer.cpp b/Source/Particles/WarpXParticleContainer.cpp index 53470753e..7d331104e 100644 --- a/Source/Particles/WarpXParticleContainer.cpp +++ b/Source/Particles/WarpXParticleContainer.cpp @@ -18,7 +18,7 @@ WarpXParIter::WarpXParIter (ContainerType& pc, int level) #if (AMREX_SPACEDIM == 2) void -WarpXParIter::GetPosition (Cuda::DeviceVector<Real>& x, Cuda::DeviceVector<Real>& y, Cuda::DeviceVector<Real>& z) const +WarpXParIter::GetPosition (Cuda::ManagedDeviceVector<Real>& x, Cuda::ManagedDeviceVector<Real>& y, Cuda::ManagedDeviceVector<Real>& z) const { amrex::ParIter<0,0,PIdx::nattribs>::GetPosition(x, z); #ifdef WARPX_RZ @@ -36,7 +36,7 @@ WarpXParIter::GetPosition (Cuda::DeviceVector<Real>& x, Cuda::DeviceVector<Real> } void -WarpXParIter::SetPosition (const Cuda::DeviceVector<Real>& x, const Cuda::DeviceVector<Real>& y, const Cuda::DeviceVector<Real>& z) +WarpXParIter::SetPosition (const Cuda::ManagedDeviceVector<Real>& x, const Cuda::ManagedDeviceVector<Real>& y, const Cuda::ManagedDeviceVector<Real>& z) { #ifdef WARPX_RZ auto& attribs = GetAttribs(); @@ -78,14 +78,6 @@ WarpXParticleContainer::WarpXParticleContainer (AmrCore* amr_core, int ispecies) m_yp.resize(num_threads); m_zp.resize(num_threads); m_giv.resize(num_threads); - for (int i = 0; i < num_threads; ++i) - { - local_rho[i].reset(nullptr); - local_jx[i].reset(nullptr); - local_jy[i].reset(nullptr); - local_jz[i].reset(nullptr); - } - } void @@ -276,35 +268,38 @@ WarpXParticleContainer::DepositCurrent(WarpXParIter& pti, tby.grow(ngJ); tbz.grow(ngJ); - local_jx[thread_num]->resize(tbx); - local_jy[thread_num]->resize(tby); - local_jz[thread_num]->resize(tbz); + local_jx[thread_num].resize(tbx); + local_jy[thread_num].resize(tby); + local_jz[thread_num].resize(tbz); - jx_ptr = local_jx[thread_num]->dataPtr(); - jy_ptr = local_jy[thread_num]->dataPtr(); - jz_ptr = local_jz[thread_num]->dataPtr(); + jx_ptr = local_jx[thread_num].dataPtr(); + jy_ptr = local_jy[thread_num].dataPtr(); + jz_ptr = local_jz[thread_num].dataPtr(); - FArrayBox* local_jx_ptr = local_jx[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbx, b, + auto jxarr = local_jx[thread_num].array(); + amrex::ParallelFor(tbx, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jx_ptr->setVal(0.0, b, 0, 1); + jxarr(i,j,k) = 0.0; }); - FArrayBox* local_jy_ptr = local_jy[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tby, b, + auto jyarr = local_jy[thread_num].array(); + amrex::ParallelFor(tby, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jy_ptr->setVal(0.0, b, 0, 1); + jyarr(i,j,k) = 0.0; }); - FArrayBox* local_jz_ptr = local_jz[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbz, b, + auto jzarr = local_jz[thread_num].array(); + amrex::ParallelFor(tbz, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jz_ptr->setVal(0.0, b, 0, 1); + jzarr(i,j,k) = 0.0; }); - auto jxntot = local_jx[thread_num]->length(); - auto jyntot = local_jy[thread_num]->length(); - auto jzntot = local_jz[thread_num]->length(); + auto jxntot = local_jx[thread_num].length(); + auto jyntot = local_jy[thread_num].length(); + auto jzntot = local_jz[thread_num].length(); BL_PROFILE_VAR_START(blp_pxr_cd); if (j_is_nodal) { @@ -396,28 +391,31 @@ WarpXParticleContainer::DepositCurrent(WarpXParIter& pti, } BL_PROFILE_VAR_STOP(blp_pxr_cd); - + BL_PROFILE_VAR_START(blp_accumulate); - - FArrayBox const* local_jx_const_ptr = local_jx[thread_num].get(); - FArrayBox* global_jx_ptr = jx.fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbx, thread_bx, + + const auto local_jx_arr = local_jx[thread_num].array(); + auto global_jx_arr = jx.array(pti); + amrex::ParallelFor(tbx, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jx_ptr->atomicAdd(*local_jx_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jx_arr(i, j, k), local_jx_arr(i, j, k)); }); - FArrayBox const* local_jy_const_ptr = local_jy[thread_num].get(); - FArrayBox* global_jy_ptr = jy.fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tby, thread_bx, + const auto local_jy_arr = local_jy[thread_num].array(); + auto global_jy_arr = jy.array(pti); + amrex::ParallelFor(tby, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jy_ptr->atomicAdd(*local_jy_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jy_arr(i, j, k), local_jy_arr(i, j, k)); }); - FArrayBox const* local_jz_const_ptr = local_jz[thread_num].get(); - FArrayBox* global_jz_ptr = jz.fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbz, thread_bx, + const auto local_jz_arr = local_jz[thread_num].array(); + auto global_jz_arr = jz.array(pti); + amrex::ParallelFor(tbz, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jz_ptr->atomicAdd(*local_jz_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jz_arr(i, j, k), local_jz_arr(i, j, k)); }); BL_PROFILE_VAR_STOP(blp_accumulate); @@ -437,34 +435,38 @@ WarpXParticleContainer::DepositCurrent(WarpXParIter& pti, tby.grow(ngJ); tbz.grow(ngJ); - local_jx[thread_num]->resize(tbx); - local_jy[thread_num]->resize(tby); - local_jz[thread_num]->resize(tbz); + local_jx[thread_num].resize(tbx); + local_jy[thread_num].resize(tby); + local_jz[thread_num].resize(tbz); - jx_ptr = local_jx[thread_num]->dataPtr(); - jy_ptr = local_jy[thread_num]->dataPtr(); - jz_ptr = local_jz[thread_num]->dataPtr(); + jx_ptr = local_jx[thread_num].dataPtr(); + jy_ptr = local_jy[thread_num].dataPtr(); + jz_ptr = local_jz[thread_num].dataPtr(); - FArrayBox* local_jx_ptr = local_jx[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbx, b, + auto jxarr = local_jx[thread_num].array(); + amrex::ParallelFor(tbx, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jx_ptr->setVal(0.0, b, 0, 1); + jxarr(i,j,k) = 0.0; }); - FArrayBox* local_jy_ptr = local_jy[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tby, b, + auto jyarr = local_jy[thread_num].array(); + amrex::ParallelFor(tby, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jy_ptr->setVal(0.0, b, 0, 1); + jyarr(i,j,k) = 0.0; }); - FArrayBox* local_jz_ptr = local_jz[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbz, b, + auto jzarr = local_jz[thread_num].array(); + amrex::ParallelFor(tbz, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_jz_ptr->setVal(0.0, b, 0, 1); + jzarr(i,j,k) = 0.0; }); - auto jxntot = local_jx[thread_num]->length(); - auto jyntot = local_jy[thread_num]->length(); - auto jzntot = local_jz[thread_num]->length(); + + auto jxntot = local_jx[thread_num].length(); + auto jyntot = local_jy[thread_num].length(); + auto jzntot = local_jz[thread_num].length(); long ncrse = np - np_current; BL_PROFILE_VAR_START(blp_pxr_cd); @@ -561,25 +563,28 @@ WarpXParticleContainer::DepositCurrent(WarpXParIter& pti, BL_PROFILE_VAR_START(blp_accumulate); - FArrayBox const* local_jx_const_ptr = local_jx[thread_num].get(); - FArrayBox* global_jx_ptr = cjx->fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbx, thread_bx, + const auto local_jx_arr = local_jx[thread_num].array(); + auto global_jx_arr = cjx->array(pti); + amrex::ParallelFor(tbx, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jx_ptr->atomicAdd(*local_jx_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jx_arr(i, j, k), local_jx_arr(i, j, k)); }); - FArrayBox const* local_jy_const_ptr = local_jy[thread_num].get(); - FArrayBox* global_jy_ptr = cjy->fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tby, thread_bx, + const auto local_jy_arr = local_jy[thread_num].array(); + auto global_jy_arr = cjy->array(pti); + amrex::ParallelFor(tby, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jy_ptr->atomicAdd(*local_jy_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jy_arr(i, j, k), local_jy_arr(i, j, k)); }); - FArrayBox const* local_jz_const_ptr = local_jz[thread_num].get(); - FArrayBox* global_jz_ptr = cjz->fabPtr(pti); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tbz, thread_bx, + const auto local_jz_arr = local_jz[thread_num].array(); + auto global_jz_arr = cjz->array(pti); + amrex::ParallelFor(tbz, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_jz_ptr->atomicAdd(*local_jz_const_ptr, thread_bx, thread_bx, 0, 0, 1); + Gpu::Atomic::Add(&global_jz_arr(i, j, k), local_jz_arr(i, j, k)); }); BL_PROFILE_VAR_STOP(blp_accumulate); @@ -589,9 +594,9 @@ WarpXParticleContainer::DepositCurrent(WarpXParIter& pti, void WarpXParticleContainer::DepositCharge ( WarpXParIter& pti, RealVector& wp, - MultiFab* rhomf, MultiFab* crhomf, int icomp, - const long np_current, - const long np, int thread_num, int lev ) + MultiFab* rhomf, MultiFab* crhomf, int icomp, + const long np_current, + const long np, int thread_num, int lev ) { BL_PROFILE_VAR_NS("PICSAR::ChargeDeposition", blp_pxr_chd); @@ -612,15 +617,17 @@ WarpXParticleContainer::DepositCharge ( WarpXParIter& pti, RealVector& wp, { const std::array<Real, 3>& xyzmin = xyzmin_tile; tile_box.grow(ngRho); - local_rho[thread_num]->resize(tile_box); - FArrayBox* local_rho_ptr = local_rho[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tile_box, b, + local_rho[thread_num].resize(tile_box); + + auto rhoarr = local_rho[thread_num].array(); + amrex::ParallelFor(tile_box, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_rho_ptr->setVal(0.0, b, 0, 1); + rhoarr(i,j,k) = 0.0; }); - data_ptr = local_rho[thread_num]->dataPtr(); - auto rholen = local_rho[thread_num]->length(); + data_ptr = local_rho[thread_num].dataPtr(); + auto rholen = local_rho[thread_num].length(); #if (AMREX_SPACEDIM == 3) const long nx = rholen[0]-1-2*ngRho; const long ny = rholen[1]-1-2*ngRho; @@ -644,14 +651,16 @@ WarpXParticleContainer::DepositCharge ( WarpXParIter& pti, RealVector& wp, &lvect, &WarpX::charge_deposition_algo); BL_PROFILE_VAR_STOP(blp_pxr_chd); - const int ncomp = 1; - FArrayBox const* local_fab = local_rho[thread_num].get(); - FArrayBox* global_fab = rhomf->fabPtr(pti); BL_PROFILE_VAR_START(blp_accumulate); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tile_box, tbx, + + const auto local_rho_arr = local_rho[thread_num].array(); + auto global_rho_arr = rhomf->array(pti); + amrex::ParallelFor(tile_box, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_fab->atomicAdd(*local_fab, tbx, tbx, 0, icomp, ncomp); + Gpu::Atomic::Add(&global_rho_arr(i, j, k, icomp), local_rho_arr(i, j, k)); }); + BL_PROFILE_VAR_STOP(blp_accumulate); } @@ -664,15 +673,17 @@ WarpXParticleContainer::DepositCharge ( WarpXParIter& pti, RealVector& wp, tile_box = amrex::convert(ctilebox, IntVect::TheUnitVector()); tile_box.grow(ngRho); - local_rho[thread_num]->resize(tile_box); - FArrayBox* local_rho_ptr = local_rho[thread_num].get(); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tile_box, b, + local_rho[thread_num].resize(tile_box); + + auto rhoarr = local_rho[thread_num].array(); + amrex::ParallelFor(tile_box, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - local_rho_ptr->setVal(0.0, b, 0, 1); + rhoarr(i,j,k) = 0.0; }); - data_ptr = local_rho[thread_num]->dataPtr(); - auto rholen = local_rho[thread_num]->length(); + data_ptr = local_rho[thread_num].dataPtr(); + auto rholen = local_rho[thread_num].length(); #if (AMREX_SPACEDIM == 3) const long nx = rholen[0]-1-2*ngRho; const long ny = rholen[1]-1-2*ngRho; @@ -698,14 +709,16 @@ WarpXParticleContainer::DepositCharge ( WarpXParIter& pti, RealVector& wp, &lvect, &WarpX::charge_deposition_algo); BL_PROFILE_VAR_STOP(blp_pxr_chd); - const int ncomp = 1; - FArrayBox const* local_fab = local_rho[thread_num].get(); - FArrayBox* global_fab = crhomf->fabPtr(pti); BL_PROFILE_VAR_START(blp_accumulate); - AMREX_LAUNCH_HOST_DEVICE_LAMBDA(tile_box, tbx, + + const auto local_rho_arr = local_rho[thread_num].array(); + auto global_rho_arr = crhomf->array(pti); + amrex::ParallelFor(tile_box, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - global_fab->atomicAdd(*local_fab, tbx, tbx, 0, icomp, ncomp); + Gpu::Atomic::Add(&global_rho_arr(i, j, k, icomp), local_rho_arr(i, j, k)); }); + BL_PROFILE_VAR_STOP(blp_accumulate); } }; @@ -797,7 +810,7 @@ WarpXParticleContainer::GetChargeDensity (int lev, bool local) #pragma omp parallel #endif { - Cuda::DeviceVector<Real> xp, yp, zp; + Cuda::ManagedDeviceVector<Real> xp, yp, zp; FArrayBox local_rho; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) @@ -1020,7 +1033,7 @@ WarpXParticleContainer::PushX (int lev, Real dt) #pragma omp parallel #endif { - Cuda::DeviceVector<Real> xp, yp, zp, giv; + Cuda::ManagedDeviceVector<Real> xp, yp, zp, giv; for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti) { diff --git a/Source/Python/WarpXWrappers.cpp b/Source/Python/WarpXWrappers.cpp index 6ee06a7d5..16d7cd841 100644 --- a/Source/Python/WarpXWrappers.cpp +++ b/Source/Python/WarpXWrappers.cpp @@ -5,6 +5,7 @@ #include <WarpXWrappers.h> #include <WarpXParticleContainer.H> #include <WarpX.H> +#include <WarpXUtil.H> #include <WarpX_py.H> namespace @@ -170,6 +171,11 @@ extern "C" myspc.AddNParticles(lev, lenx, x, y, z, vx, vy, vz, nattr, attr, uniqueparticles); } + void warpx_ConvertLabParamsToBoost() + { + ConvertLabParamsToBoost(); + } + double warpx_getProbLo(int dir) { WarpX& warpx = WarpX::GetInstance(); diff --git a/Source/Python/WarpXWrappers.h b/Source/Python/WarpXWrappers.h index 07d6f80f7..94fbb0d30 100644 --- a/Source/Python/WarpXWrappers.h +++ b/Source/Python/WarpXWrappers.h @@ -52,7 +52,9 @@ extern "C" { double* x, double* y, double* z, double* vx, double* vy, double* vz, int nattr, double* attr, int uniqueparticles); - + + void warpx_ConvertLabParamsToBoost(); + double warpx_getProbLo(int dir); double warpx_getProbHi(int dir); diff --git a/Source/Utils/Make.package b/Source/Utils/Make.package index 8cd124ea3..d8e2d2dab 100644 --- a/Source/Utils/Make.package +++ b/Source/Utils/Make.package @@ -1,6 +1,5 @@ F90EXE_sources += utils_ES.F90 CEXE_sources += WarpXMovingWindow.cpp -CEXE_sources += WarpXConst.cpp CEXE_sources += WarpXTagging.cpp CEXE_sources += WarpXUtil.cpp CEXE_headers += WarpXConst.H diff --git a/Source/Utils/WarpXConst.H b/Source/Utils/WarpXConst.H index fdb92f666..4e226e55f 100644 --- a/Source/Utils/WarpXConst.H +++ b/Source/Utils/WarpXConst.H @@ -19,17 +19,4 @@ namespace MathConst static constexpr amrex::Real pi = 3.14159265358979323846; } -class UserConstants{ -public: - std::vector<std::string> constant_names; - std::vector<amrex::Real> constant_values; - - int nb_constants=0; - std::string replaceStringValue(std::string); - void ReadParameters(); -private: - bool initialized = false; - bool use_my_constants = false; -}; - #endif diff --git a/Source/Utils/WarpXConst.cpp b/Source/Utils/WarpXConst.cpp deleted file mode 100644 index bd3ebc3ef..000000000 --- a/Source/Utils/WarpXConst.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include <limits> -#include <cmath> -#include <algorithm> -#include <numeric> -#include <sstream> - -#include <AMReX_ParmParse.H> -#include <WarpX.H> -#include <WarpXConst.H> -#include <WarpX_f.H> -#include <MultiParticleContainer.H> - -std::string UserConstants::replaceStringValue(std::string math_expr){ - std::string pattern, value_str; - std::string patternexp = "e+"; - std::size_t found; - amrex::Real value; - for (int i=0; i<nb_constants; i++){ - pattern = constant_names[i]; - value = constant_values[i]; - - // Convert value to string, with scientific notation - std::ostringstream streamObj; - streamObj << value; - value_str = streamObj.str(); - - // Replace "e+" by "e" in scientific notation for Fortran compatibility - found = value_str.find(patternexp); - if (found != std::string::npos){ - value_str.replace(found,patternexp.length(),"e"); - } - - // Replace all occurences of variable pattern by a string with its value value_str - found = math_expr.find(pattern); - while (found != std::string::npos){ - if ((found==0 - && !isalnum(math_expr[pattern.length() ])) || - (!isalnum(math_expr[found-1]) - && !isalnum(math_expr[found+pattern.length()]))){ - math_expr.replace(found,pattern.length(),value_str); - } - found = math_expr.find(pattern, found + pattern.length()); - } - } - return math_expr; -} - -void UserConstants::ReadParameters() -{ - if (!initialized){ - amrex::ParmParse pp("constants"); - pp.query("use_my_constants", use_my_constants); - if (use_my_constants){ - pp.getarr("constant_names", constant_names); - nb_constants = constant_names.size(); - pp.getarr("constant_values", constant_values); - BL_ASSERT(constant_values.size() == nb_constants); - } - initialized = true; - } -} diff --git a/Source/WarpX.H b/Source/WarpX.H index 2462182ed..cc01a042e 100644 --- a/Source/WarpX.H +++ b/Source/WarpX.H @@ -22,6 +22,7 @@ #include <MultiParticleContainer.H> #include <PML.H> #include <BoostedFrameDiagnostic.H> +#include <BilinearFilter.H> #ifdef WARPX_USE_PSATD #include <fftw3.h> @@ -59,6 +60,9 @@ public: WarpX (); ~WarpX (); + static std::string Version (); + static std::string PicsarVersion (); + int Verbose () const { return verbose; } void InitData (); @@ -88,7 +92,7 @@ public: static bool use_fdtd_nci_corr; static int l_lower_order_in_v; - + static bool use_laser; static bool use_filter; static bool serialize_ics; @@ -137,6 +141,9 @@ public: } } + static amrex::IntVect filter_npass_each_dir; + BilinearFilter bilinear_filter; + void ComputeDt (); int MoveWindow (bool move_j); void UpdatePlasmaInjectionPosition (amrex::Real dt); @@ -189,6 +196,8 @@ public: void WriteCheckPointFile () const; void WritePlotFile () const; void UpdateInSitu () const; + void AverageAndPackFields( amrex::Vector<std::string>& varnames, + amrex::Vector<amrex::MultiFab>& mf_avg, const int ngrow) const; void WritePlotFileES(const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& rho, const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& phi, @@ -337,6 +346,8 @@ private: void InitPML (); void ComputePMLFactors (); + void InitFilter (); + void InitDiagnostics (); void InitNCICorrector (); @@ -350,9 +361,6 @@ private: std::array<std::unique_ptr<amrex::MultiFab>, 3> getInterpolatedB(int lev) const; - static void PackPlotDataPtrs (amrex::Vector<const amrex::MultiFab*>& pmf, - const std::array<std::unique_ptr<amrex::MultiFab>,3>& data); - static void ComputeDivB (amrex::MultiFab& divB, int dcomp, const std::array<const amrex::MultiFab*, 3>& B, const std::array<amrex::Real,3>& dx); @@ -381,9 +389,6 @@ private: void LoadBalance (); - static void applyFilter (amrex::MultiFab& dstmf, const amrex::MultiFab& srcmf, - int scomp = 0, int dcomp = 0, int ncomp = 10000); - void BuildBufferMasks (); const amrex::iMultiFab* getCurrentBufferMasks (int lev) const { return current_buffer_masks[lev].get(); @@ -446,7 +451,7 @@ private: amrex::Vector<std::array<std::unique_ptr<amrex::iMultiFab>, 3 > > current_fp_owner_masks; amrex::Vector<std::array<std::unique_ptr<amrex::iMultiFab>, 3 > > current_cp_owner_masks; - + amrex::Vector<std::unique_ptr<amrex::iMultiFab> > rho_fp_owner_masks; amrex::Vector<std::unique_ptr<amrex::iMultiFab> > rho_cp_owner_masks; @@ -492,10 +497,17 @@ private: std::string restart_chkfile; std::string check_file {"checkpoints/chk"}; - std::string plot_file {"plotfiles/plt"}; + std::string plot_file {"diags/plotfiles/plt"}; int check_int = -1; int plot_int = -1; +#ifdef WARPX_USE_OPENPMD + bool dump_plotfiles = false; + bool dump_openpmd = true; +#else + bool dump_plotfiles = true; + bool dump_openpmd = false; +#endif bool plot_part_per_cell = true; bool plot_part_per_grid = false; bool plot_part_per_proc = false; @@ -508,6 +520,7 @@ private: bool plot_crsepatch = false; bool plot_raw_fields = false; bool plot_raw_fields_guards = false; + int plot_coarsening_ratio = 1; amrex::VisMF::Header::Version checkpoint_headerversion = amrex::VisMF::Header::NoFabHeader_v1; // amrex::VisMF::Header::Version plotfile_headerversion = amrex::VisMF::Header::NoFabHeader_v1; @@ -523,6 +536,9 @@ private: bool is_synchronized = true; + amrex::Vector<std::string> particle_plot_vars; + amrex::Vector<int> particle_plot_flags; + #ifdef WARPX_USE_PSATD // Store fields in real space on the dual grid (i.e. the grid for the FFT push of the fields) // This includes data for the FFT guard cells (between FFT groups) @@ -564,6 +580,7 @@ public: void operator= (FFTData const&) = delete; void operator= (FFTData&&) = delete; }; + private: amrex::Vector<std::unique_ptr<amrex::LayoutData<FFTData> > > dataptr_fp_fft; diff --git a/Source/WarpX.cpp b/Source/WarpX.cpp index bf0d9b255..51d9133be 100644 --- a/Source/WarpX.cpp +++ b/Source/WarpX.cpp @@ -91,6 +91,8 @@ IntVect WarpX::jy_nodal_flag(1,1); // y is the missing dimension to 2D AMReX IntVect WarpX::jz_nodal_flag(1,0); // z is the second dimension to 2D AMReX #endif +IntVect WarpX::filter_npass_each_dir(1); + int WarpX::n_field_gather_buffer = 0; int WarpX::n_current_deposition_buffer = -1; @@ -98,8 +100,6 @@ int WarpX::do_nodal = false; WarpX* WarpX::m_instance = nullptr; - - WarpX& WarpX::GetInstance () { @@ -260,14 +260,14 @@ WarpX::ReadParameters () pp.query("cfl", cfl); pp.query("verbose", verbose); pp.query("regrid_int", regrid_int); - pp.query("do_subcycling", do_subcycling); - - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(do_subcycling != 1 || max_level <= 1, - "Subcycling method 1 only works for 2 levels."); - - ReadBoostedFrameParameters(gamma_boost, beta_boost, boost_direction); - - pp.queryarr("B_external", B_external); + pp.query("do_subcycling", do_subcycling); + + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(do_subcycling != 1 || max_level <= 1, + "Subcycling method 1 only works for 2 levels."); + + ReadBoostedFrameParameters(gamma_boost, beta_boost, boost_direction); + + pp.queryarr("B_external", B_external); pp.query("do_moving_window", do_moving_window); if (do_moving_window) @@ -298,52 +298,62 @@ WarpX::ReadParameters () pp.query("do_plasma_injection", do_plasma_injection); if (do_plasma_injection) { - pp.get("num_injected_species", num_injected_species); - injected_plasma_species.resize(num_injected_species); - pp.getarr("injected_plasma_species", injected_plasma_species, - 0, num_injected_species); - if (moving_window_v >= 0){ - // Inject particles continuously from the right end of the box - current_injection_position = geom[0].ProbHi(moving_window_dir); - } else { - // Inject particles continuously from the left end of the box - current_injection_position = geom[0].ProbLo(moving_window_dir); - } + pp.get("num_injected_species", num_injected_species); + injected_plasma_species.resize(num_injected_species); + pp.getarr("injected_plasma_species", injected_plasma_species, + 0, num_injected_species); + if (moving_window_v >= 0){ + // Inject particles continuously from the right end of the box + current_injection_position = geom[0].ProbHi(moving_window_dir); + } else { + // Inject particles continuously from the left end of the box + current_injection_position = geom[0].ProbLo(moving_window_dir); + } } - pp.query("do_boosted_frame_diagnostic", do_boosted_frame_diagnostic); - if (do_boosted_frame_diagnostic) { - - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(gamma_boost > 1.0, - "gamma_boost must be > 1 to use the boosted frame diagnostic."); - - std::string s; - pp.get("boost_direction", s); - AMREX_ALWAYS_ASSERT_WITH_MESSAGE( (s == "z" || s == "Z"), - "The boosted frame diagnostic currently only works if the boost is in the z direction."); - - pp.get("num_snapshots_lab", num_snapshots_lab); - pp.get("dt_snapshots_lab", dt_snapshots_lab); - pp.get("gamma_boost", gamma_boost); - - pp.query("do_boosted_frame_fields", do_boosted_frame_fields); - pp.query("do_boosted_frame_particles", do_boosted_frame_particles); - - - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(do_moving_window, - "The moving window should be on if using the boosted frame diagnostic."); - - pp.get("moving_window_dir", s); - AMREX_ALWAYS_ASSERT_WITH_MESSAGE( (s == "z" || s == "Z"), - "The boosted frame diagnostic currently only works if the moving window is in the z direction."); - } + pp.query("do_boosted_frame_diagnostic", do_boosted_frame_diagnostic); + if (do_boosted_frame_diagnostic) { + + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(gamma_boost > 1.0, + "gamma_boost must be > 1 to use the boosted frame diagnostic."); + + std::string s; + pp.get("boost_direction", s); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE( (s == "z" || s == "Z"), + "The boosted frame diagnostic currently only works if the boost is in the z direction."); + + pp.get("num_snapshots_lab", num_snapshots_lab); + pp.get("dt_snapshots_lab", dt_snapshots_lab); + pp.get("gamma_boost", gamma_boost); + + pp.query("do_boosted_frame_fields", do_boosted_frame_fields); + pp.query("do_boosted_frame_particles", do_boosted_frame_particles); + + + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(do_moving_window, + "The moving window should be on if using the boosted frame diagnostic."); + + pp.get("moving_window_dir", s); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE( (s == "z" || s == "Z"), + "The boosted frame diagnostic currently only works if the moving window is in the z direction."); + } - pp.query("do_electrostatic", do_electrostatic); - pp.query("n_buffer", n_buffer); - pp.query("const_dt", const_dt); + pp.query("do_electrostatic", do_electrostatic); + pp.query("n_buffer", n_buffer); + pp.query("const_dt", const_dt); pp.query("use_laser", use_laser); + // Read filter and fill IntVect filter_npass_each_dir with + // proper size for AMREX_SPACEDIM pp.query("use_filter", use_filter); + Vector<int> parse_filter_npass_each_dir(AMREX_SPACEDIM,1); + pp.queryarr("filter_npass_each_dir", parse_filter_npass_each_dir); + filter_npass_each_dir[0] = parse_filter_npass_each_dir[0]; + filter_npass_each_dir[1] = parse_filter_npass_each_dir[1]; +#if (AMREX_SPACEDIM == 3) + filter_npass_each_dir[2] = parse_filter_npass_each_dir[2]; +#endif + pp.query("serialize_ics", serialize_ics); pp.query("refine_plasma", refine_plasma); pp.query("do_dive_cleaning", do_dive_cleaning); @@ -355,6 +365,8 @@ WarpX::ReadParameters () pp.query("pml_ncell", pml_ncell); pp.query("pml_delta", pml_delta); + pp.query("dump_openpmd", dump_openpmd); + pp.query("dump_plotfiles", dump_openpmd); pp.query("plot_raw_fields", plot_raw_fields); pp.query("plot_raw_fields_guards", plot_raw_fields_guards); if (ParallelDescriptor::NProcs() == 1) { @@ -368,6 +380,16 @@ WarpX::ReadParameters () pp.query("plot_divb" , plot_divb); pp.query("plot_rho" , plot_rho); pp.query("plot_F" , plot_F); + pp.query("plot_coarsening_ratio", plot_coarsening_ratio); + // Check that the coarsening_ratio can divide the blocking factor + for (int lev=0; lev<maxLevel(); lev++){ + for (int comp=0; comp<AMREX_SPACEDIM; comp++){ + if ( blockingFactor(lev)[comp] % plot_coarsening_ratio != 0 ){ + amrex::Abort("plot_coarsening_ratio should be an integer divisor of the blocking factor."); + } + } + } + if (plot_F){ AMREX_ALWAYS_ASSERT_WITH_MESSAGE(do_dive_cleaning, "plot_F only works if warpx.do_dive_cleaning = 1"); @@ -407,10 +429,29 @@ WarpX::ReadParameters () fine_tag_hi = RealVect{hi}; } + // select which particle comps to write + { + pp.queryarr("particle_plot_vars", particle_plot_vars); + + if (particle_plot_vars.size() == 0) + { + particle_plot_flags.resize(PIdx::nattribs, 1); + } + else + { + particle_plot_flags.resize(PIdx::nattribs, 0); + + for (const auto& var : particle_plot_vars) + { + particle_plot_flags[ParticleStringNames::to_index.at(var)] = 1; + } + } + } + pp.query("load_balance_int", load_balance_int); pp.query("load_balance_with_sfc", load_balance_with_sfc); pp.query("load_balance_knapsack_factor", load_balance_knapsack_factor); - + pp.query("do_dynamic_scheduling", do_dynamic_scheduling); pp.query("do_nodal", do_nodal); @@ -665,10 +706,10 @@ WarpX::AllocLevelMFs (int lev, const BoxArray& ba, const DistributionMapping& dm if (do_dive_cleaning || plot_rho) { - rho_fp[lev].reset(new MultiFab(amrex::convert(ba,IntVect::TheUnitVector()),dm,2,ngRho)); + rho_fp[lev].reset(new MultiFab(amrex::convert(ba,IntVect::TheUnitVector()),dm,2,ngRho)); rho_fp_owner_masks[lev] = std::move(rho_fp[lev]->OwnerMask(period)); } - + if (do_subcycling == 1 && lev == 0) { current_store[lev][0].reset( new MultiFab(amrex::convert(ba,jx_nodal_flag),dm,1,ngJ)); @@ -782,7 +823,7 @@ WarpX::AllocLevelMFs (int lev, const BoxArray& ba, const DistributionMapping& dm current_buf[lev][0].reset( new MultiFab(amrex::convert(cba,jx_nodal_flag),dm,1,ngJ)); current_buf[lev][1].reset( new MultiFab(amrex::convert(cba,jy_nodal_flag),dm,1,ngJ)); current_buf[lev][2].reset( new MultiFab(amrex::convert(cba,jz_nodal_flag),dm,1,ngJ)); - if (do_dive_cleaning) { + if (do_dive_cleaning || plot_rho) { charge_buf[lev].reset( new MultiFab(amrex::convert(cba,IntVect::TheUnitVector()),dm,2,ngRho)); } current_buffer_masks[lev].reset( new iMultiFab(ba, dm, 1, 1) ); @@ -946,33 +987,6 @@ WarpX::ComputeDivE (MultiFab& divE, int dcomp, } void -WarpX::applyFilter (MultiFab& dstmf, const MultiFab& srcmf, int scomp, int dcomp, int ncomp) -{ - ncomp = std::min(ncomp, srcmf.nComp()); -#ifdef _OPENMP -#pragma omp parallel -#endif - { - FArrayBox tmpfab; - for (MFIter mfi(dstmf,true); mfi.isValid(); ++mfi) - { - const auto& srcfab = srcmf[mfi]; - auto& dstfab = dstmf[mfi]; - const Box& tbx = mfi.growntilebox(); - const Box& gbx = amrex::grow(tbx,1); - tmpfab.resize(gbx,ncomp); - tmpfab.setVal(0.0, gbx, 0, ncomp); - const Box& ibx = gbx & srcfab.box(); - tmpfab.copy(srcfab, ibx, scomp, ibx, 0, ncomp); - WRPX_FILTER(BL_TO_FORTRAN_BOX(tbx), - BL_TO_FORTRAN_ANYD(tmpfab), - BL_TO_FORTRAN_N_ANYD(dstfab,dcomp), - ncomp); - } - } -} - -void WarpX::BuildBufferMasks () { int ngbuffer = std::max(n_field_gather_buffer, n_current_deposition_buffer); @@ -1041,3 +1055,24 @@ WarpX::RestoreCurrent (int lev) } } } + +std::string +WarpX::Version () +{ +#ifdef WARPX_GIT_VERSION + return std::string(WARPX_GIT_VERSION); +#else + return std::string("Unknown"); +#endif +} + +std::string +WarpX::PicsarVersion () +{ +#ifdef WARPX_GIT_VERSION + return std::string(PICSAR_GIT_VERSION); +#else + return std::string("Unknown"); +#endif +} + diff --git a/Source/main.cpp b/Source/main.cpp index 4bf17b7bd..d89e89e47 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -41,6 +41,8 @@ int main(int argc, char* argv[]) ParallelDescriptor::ReduceRealMax(end_total ,ParallelDescriptor::IOProcessorNumber()); if (warpx.Verbose()) { amrex::Print() << "Total Time : " << end_total << '\n'; + amrex::Print() << "WarpX Version: " << WarpX::Version() << '\n'; + amrex::Print() << "PICSAR Version: " << WarpX::PicsarVersion() << '\n'; } } |