diff --git a/src/include/dim.hxx b/src/include/dim.hxx index a98cfa1872..089097a270 100644 --- a/src/include/dim.hxx +++ b/src/include/dim.hxx @@ -3,12 +3,34 @@ #include +#include "kg/Vec3.h" + template struct Invar { using InvarX = std::integral_constant; using InvarY = std::integral_constant; using InvarZ = std::integral_constant; + + /// @brief Gets a mask of noninvariant dimensions (i.e., 0 for invariant + /// dimensions, 1 for noninvariant dimensions). This is useful for multiplying + /// by a constant to, say, get the number of ghost cells in each dimension + /// (where invariant dimensions have no ghosts). + /// @return the mask + static Int3 get_noninvariant_mask() + { + return {!InvarX::value, !InvarY::value, !InvarZ::value}; + } + + static bool is_invar(int dim) + { + switch (dim) { + case 0: return InvarX::value; + case 1: return InvarY::value; + case 2: return InvarZ::value; + default: return false; + } + } }; using dim_xyz = Invar; diff --git a/src/include/grid.hxx b/src/include/grid.hxx index e11cc9ff4c..d3e3c60e11 100644 --- a/src/include/grid.hxx +++ b/src/include/grid.hxx @@ -7,7 +7,7 @@ #include "psc_bits.h" #include "mrc_domain.hxx" #include "grid/BC.h" -#include "grid/Domain.h" +#include "grid/domain.hxx" #include #include @@ -81,10 +81,6 @@ struct Grid_ mpi_printf(MPI_COMM_WORLD, "::: dx = %g %g %g\n", domain.dx[0], domain.dx[1], domain.dx[2]); #endif - assert(domain.dx[0] > 0.); - assert(domain.dx[1] > 0.); - assert(domain.dx[2] > 0.); - for (auto off : mrc_domain_.offs()) { patches.push_back(Patch( off, Vec3(off) * domain.dx + domain.corner, diff --git a/src/include/grid/Domain.h b/src/include/grid/domain.hxx similarity index 53% rename from src/include/grid/Domain.h rename to src/include/grid/domain.hxx index a6bb98f214..ce7f20f6a2 100644 --- a/src/include/grid/Domain.h +++ b/src/include/grid/domain.hxx @@ -1,9 +1,8 @@ - -#ifndef GRID_DOMAIN_H -#define GRID_DOMAIN_H +#pragma once #include #include +#include "../libpsc/vpic/psc_vpic_bits.h" namespace psc { @@ -26,9 +25,23 @@ struct Domain : gdims(gdims), length(length), corner(corner), np(np) { for (int d = 0; d < 3; d++) { - assert(gdims[d] % np[d] == 0); - ldims[d] = gdims[d] / np[d]; + if (gdims[d] <= 0) { + LOG_ERROR("dimension %d has non-positive number of cells (%d)\n", d, + gdims[d]); + } + + if (gdims[d] % np[d] != 0) { + LOG_ERROR("in dimension %d, number of patches (%d) doesn't divide " + "number of cells (%d)\n", + d, np[d], gdims[d]); + } + + if (length[d] <= 0.0) { + LOG_ERROR("dimension %d has non-positive length (%f)\n", d, length[d]); + } } + + ldims = gdims / np; dx = length / Real3(gdims); } @@ -40,11 +53,17 @@ struct Domain bool isInvar(int d) const { return gdims[d] == 1; } - Int3 gdims; ///< Number of grid-points in each dimension - Real3 length; ///< The physical size of the simulation-box + /// @brief Total number of grid cells in each dimension. + Int3 gdims; + /// @brief Side lengths of the domain, in physical units. + Real3 length; + /// @brief Location of lower-right corner of the domain, in physical units. Real3 corner; - Int3 np; ///< Number of patches in each dimension + /// @brief Number of patches in each dimension. + Int3 np; + /// @brief Number of grid cells per patch in each dimension. Int3 ldims; + /// @brief Side lengths of each grid cell, in physical units. Real3 dx; }; @@ -63,5 +82,3 @@ inline std::ostream& operator<<(std::ostream& os, const Domain& domain) } // namespace grid } // namespace psc - -#endif diff --git a/src/include/mrc_domain.hxx b/src/include/mrc_domain.hxx index 1d7c0ea475..3a98d393c1 100644 --- a/src/include/mrc_domain.hxx +++ b/src/include/mrc_domain.hxx @@ -2,7 +2,7 @@ #pragma once #include "grid/BC.h" -#include "grid/Domain.h" +#include "grid/domain.hxx" #include diff --git a/src/include/psc.hxx b/src/include/psc.hxx index de7e386bef..0a51e11838 100644 --- a/src/include/psc.hxx +++ b/src/include/psc.hxx @@ -177,9 +177,12 @@ struct Psc { time_start_ = MPI_Wtime(); - assert(grid.isInvar(0) == Dim::InvarX::value); - assert(grid.isInvar(1) == Dim::InvarY::value); - assert(grid.isInvar(2) == Dim::InvarZ::value); + for (int d = 0; d < 3; d++) { + if (grid.isInvar(d) != Dim::is_invar(d)) { + LOG_ERROR("dimension %d is%s invariant, but gdims[%d]=%d\n", d, + Dim::is_invar(d) ? "" : " not", d, grid.domain.gdims[d]); + } + } int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); diff --git a/src/libpsc/tests/test_boundary_injector.cxx b/src/libpsc/tests/test_boundary_injector.cxx index d424c8354e..002930265d 100644 --- a/src/libpsc/tests/test_boundary_injector.cxx +++ b/src/libpsc/tests/test_boundary_injector.cxx @@ -68,16 +68,8 @@ Grid_t* setupGrid() double dt = .1; Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/libpsc/tests/test_reflective_bcs.cxx b/src/libpsc/tests/test_reflective_bcs.cxx index 18cb4206b1..e16721eb1f 100644 --- a/src/libpsc/tests/test_reflective_bcs.cxx +++ b/src/libpsc/tests/test_reflective_bcs.cxx @@ -65,16 +65,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/libpsc/tests/test_reflective_bcs_integration.cxx b/src/libpsc/tests/test_reflective_bcs_integration.cxx index 8519c5690a..b2cefb3abe 100644 --- a/src/libpsc/tests/test_reflective_bcs_integration.cxx +++ b/src/libpsc/tests/test_reflective_bcs_integration.cxx @@ -65,16 +65,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_2d_shock.cxx b/src/psc_2d_shock.cxx index e5dc8485a1..97d72c4d1c 100644 --- a/src/psc_2d_shock.cxx +++ b/src/psc_2d_shock.cxx @@ -320,16 +320,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_bgk.cxx b/src/psc_bgk.cxx index a0c50254ea..da9fdea3d8 100644 --- a/src/psc_bgk.cxx +++ b/src/psc_bgk.cxx @@ -129,16 +129,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_bubble_yz.cxx b/src/psc_bubble_yz.cxx index 0ee3089921..93c67ca7f3 100644 --- a/src/psc_bubble_yz.cxx +++ b/src/psc_bubble_yz.cxx @@ -141,16 +141,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_flatfoil_yz.cxx b/src/psc_flatfoil_yz.cxx index 37c92ce9e2..ff2910e74f 100644 --- a/src/psc_flatfoil_yz.cxx +++ b/src/psc_flatfoil_yz.cxx @@ -339,16 +339,8 @@ Grid_t* setupGrid() mpi_printf(MPI_COMM_WORLD, "dt = %g\n", dt); - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_harris_xz.cxx b/src/psc_harris_xz.cxx index 276676d19d..b8e65c11ff 100644 --- a/src/psc_harris_xz.cxx +++ b/src/psc_harris_xz.cxx @@ -368,16 +368,8 @@ Grid_t* setupGrid() #ifdef VPIC Int3 ibn = {1, 1, 1}; #else - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); #endif auto grid_ptr = new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; diff --git a/src/psc_harris_yz.cxx b/src/psc_harris_yz.cxx index 0d1f27a126..aa2df93a27 100644 --- a/src/psc_harris_yz.cxx +++ b/src/psc_harris_yz.cxx @@ -247,16 +247,8 @@ Grid_t* setupGrid() mpi_printf(MPI_COMM_WORLD, "dt = %g\n", dt); - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_radiation.cxx b/src/psc_radiation.cxx index 3097c831fb..369b76a94a 100644 --- a/src/psc_radiation.cxx +++ b/src/psc_radiation.cxx @@ -150,16 +150,8 @@ Grid_t* setupGrid() mpi_printf(MPI_COMM_WORLD, "dt = %g\n", dt); - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_shock.cxx b/src/psc_shock.cxx index 0ccc424784..85874b9e77 100644 --- a/src/psc_shock.cxx +++ b/src/psc_shock.cxx @@ -175,16 +175,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; } diff --git a/src/psc_whistler.cxx b/src/psc_whistler.cxx index 793cd2f89e..6437bdac15 100644 --- a/src/psc_whistler.cxx +++ b/src/psc_whistler.cxx @@ -163,16 +163,8 @@ Grid_t* setupGrid() double dt = psc_params.cfl * courant_length(domain); Grid_t::Normalization norm{norm_params}; - Int3 ibn = {2, 2, 2}; - if (Dim::InvarX::value) { - ibn[0] = 0; - } - if (Dim::InvarY::value) { - ibn[1] = 0; - } - if (Dim::InvarZ::value) { - ibn[2] = 0; - } + int n_ghosts = 2; + Int3 ibn = n_ghosts * Dim::get_noninvariant_mask(); return new Grid_t{domain, bc, kinds, norm, dt, -1, ibn}; }