From 4e40aef22c675bbea8195789570c872b9c8878a1 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Thu, 2 May 2024 14:28:54 -0400 Subject: [PATCH] add support for calling Particles3D::initialize without gravity --- src/gravity/grav3D.cpp | 35 ++++++++--------- src/gravity/grav3D.h | 6 +-- src/gravity/gravity_functions.cpp | 5 +-- src/grid/spatial_domain_props.cpp | 64 +++++++++++++++++++++++++++++++ src/grid/spatial_domain_props.h | 32 ++++++++++++++++ src/particles/particles_3D.cpp | 52 ++++++++++++++++--------- src/particles/particles_3D.h | 3 +- 7 files changed, 153 insertions(+), 44 deletions(-) create mode 100644 src/grid/spatial_domain_props.cpp create mode 100644 src/grid/spatial_domain_props.h diff --git a/src/gravity/grav3D.cpp b/src/gravity/grav3D.cpp index 866663589..9234b2199 100644 --- a/src/gravity/grav3D.cpp +++ b/src/gravity/grav3D.cpp @@ -16,9 +16,8 @@ Grav3D::Grav3D(void) {} -void Grav3D::Initialize(Real x_min, Real y_min, Real z_min, Real x_max, Real y_max, Real z_max, Real Lx, Real Ly, - Real Lz, int nx, int ny, int nz, int nx_real, int ny_real, int nz_real, Real dx_real, - Real dy_real, Real dz_real, int n_ghost_pot_offset, struct Parameters *P) +void Grav3D::Initialize(const SpatialDomainProps& spatial_props, Real Lx, Real Ly, Real Lz, + int n_ghost_pot_offset, Parameters *P) { // Set Box Size Lbox_x = Lx; @@ -26,29 +25,29 @@ void Grav3D::Initialize(Real x_min, Real y_min, Real z_min, Real x_max, Real y_m Lbox_z = Lz; // Set Box Left boundary positions - xMin = x_min; - yMin = y_min; - zMin = z_min; + xMin = spatial_props.xMin; + yMin = spatial_props.yMin; + zMin = spatial_props.zMin; // Set Box Right boundary positions - xMax = x_max; - yMax = y_max; - zMax = z_max; + xMax = spatial_props.xMax; + yMax = spatial_props.yMax; + zMax = spatial_props.zMax; // Set uniform ( dx, dy, dz ) - dx = dx_real; - dy = dy_real; - dz = dz_real; + dx = spatial_props.dx; + dy = spatial_props.dy; + dz = spatial_props.dz; // Set Box Total number of cells - nx_total = nx; - ny_total = ny; - nz_total = nz; + nx_total = spatial_props.nx_total; + ny_total = spatial_props.ny_total; + nz_total = spatial_props.nz_total; // Set Box local domain number of cells - nx_local = nx_real; - ny_local = ny_real; - nz_local = nz_real; + nx_local = spatial_props.nx_local; + ny_local = spatial_props.ny_local; + nz_local = spatial_props.nz_local; // Local n_cells without ghost cells n_cells = nx_local * ny_local * nz_local; diff --git a/src/gravity/grav3D.h b/src/gravity/grav3D.h index 69cf8308a..bbb3cf733 100644 --- a/src/gravity/grav3D.h +++ b/src/gravity/grav3D.h @@ -4,6 +4,7 @@ #include #include "../global/global.h" +#include "../grid/spatial_domain_props.h" #ifdef SOR #include "../gravity/potential_SOR_3D.h" @@ -191,9 +192,8 @@ class Grav3D /*! \fn void Initialize(int nx_in, int ny_in, int nz_in) * \brief Initialize the grid. */ - void Initialize(Real x_min, Real y_min, Real z_min, Real x_max, Real y_max, Real z_max, Real Lx, Real Ly, Real Lz, - int nx_total, int ny_total, int nz_total, int nx_real, int ny_real, int nz_real, Real dx_real, - Real dy_real, Real dz_real, int n_ghost_pot_offset, struct Parameters *P); + void Initialize(const SpatialDomainProps& spatial_props, Real Lx, Real Ly, Real Lz, + int n_ghost_pot_offset, struct Parameters *P); void AllocateMemory_CPU(void); void Initialize_values_CPU(); diff --git a/src/gravity/gravity_functions.cpp b/src/gravity/gravity_functions.cpp index 7f1f9cc28..f8e3abf92 100644 --- a/src/gravity/gravity_functions.cpp +++ b/src/gravity/gravity_functions.cpp @@ -358,9 +358,8 @@ static void printDiff(const Real *p, const Real *q, const int nx, const int ny, void Grid3D::Initialize_Gravity(struct Parameters *P) { chprintf("\nInitializing Gravity... \n"); - Grav.Initialize(H.xblocal, H.yblocal, H.zblocal, H.xblocal_max, H.yblocal_max, H.zblocal_max, H.xdglobal, H.ydglobal, - H.zdglobal, P->nx, P->ny, P->nz, H.nx_real, H.ny_real, H.nz_real, H.dx, H.dy, H.dz, - H.n_ghost_potential_offset, P); + SpatialDomainProps spatial_props = SpatialDomainProps::From_Grid3D(*this, P); + Grav.Initialize(spatial_props, H.xdglobal, H.ydglobal, H.zdglobal, H.n_ghost_potential_offset, P); chprintf("Gravity Successfully Initialized. \n\n"); if (P->bc_potential_type == 1) { diff --git a/src/grid/spatial_domain_props.cpp b/src/grid/spatial_domain_props.cpp new file mode 100644 index 000000000..7f64875fd --- /dev/null +++ b/src/grid/spatial_domain_props.cpp @@ -0,0 +1,64 @@ +#include "../grid/spatial_domain_props.h" +#include "../gravity/grav3D.h" +#include "../grid/grid3D.h" + +SpatialDomainProps SpatialDomainProps::From_Grav3D(Grav3D& grav) +{ + SpatialDomainProps out; + + out.nx_local = grav.nx_local; + out.ny_local = grav.ny_local; + out.nz_local = grav.nz_local; + + out.nx_total = grav.nx_total; + out.ny_total = grav.ny_total; + out.nz_total = grav.nz_total; + + out.dx = grav.dx; + out.dy = grav.dy; + out.dz = grav.dz; + + // Left boundaries of the local domain + out.xMin = grav.xMin; + out.yMin = grav.yMin; + out.zMin = grav.zMin; + + // Right boundaries of the local domain + out.xMax = grav.xMax; + out.yMax = grav.yMax; + out.zMax = grav.zMax; + + return out; +} + +SpatialDomainProps SpatialDomainProps::From_Grid3D(Grid3D& grid, struct Parameters * P) +{ + + SpatialDomainProps out; + + // Set Box Left boundary positions + out.xMin = grid.H.xblocal; // x_min + out.yMin = grid.H.yblocal; // y_min + out.zMin = grid.H.zblocal; // z_min + + // Set Box Right boundary positions + out.xMax = grid.H.xblocal_max; //x_max; + out.yMax = grid.H.yblocal_max; //y_max; + out.zMax = grid.H.zblocal_max; //z_max; + + // Set uniform ( dx, dy, dz ) + out.dx = grid.H.dx; // dx_real; + out.dy = grid.H.dy; // dy_real; + out.dz = grid.H.dz; // dz_real; + + // Set Box Total number of cells + out.nx_total = P->nx; // nx; + out.ny_total = P->ny; // ny; + out.nz_total = P->nz; // nz; + + // Set Box local domain number of cells + out.nx_local = grid.H.nx_real; // nx_real; + out.ny_local = grid.H.ny_real; // ny_real; + out.nz_local = grid.H.nz_real; // nz_real; + return out; +}; \ No newline at end of file diff --git a/src/grid/spatial_domain_props.h b/src/grid/spatial_domain_props.h new file mode 100644 index 000000000..55e4c6d95 --- /dev/null +++ b/src/grid/spatial_domain_props.h @@ -0,0 +1,32 @@ +#pragma once + +#include "../global/global.h" + +class Grav3D; +class Grid3D; +struct Parameters; + +/* This is a collection of 15 quantities that appear in 3 other locations throughout the codebase. + * + * The struct primarily exists to simplify the process of copying these values from one place to + * another. (But it may make sense to refactor other parts of the code in terms of this object) + */ +struct SpatialDomainProps { + // number of cells in the local domain + int nx_local, ny_local, nz_local; + + // total number of cells in the entire (global) domain + int nx_total, ny_total, nz_total; + + // Left boundaries of the local domain + Real xMin, yMin, zMin; + + // Right boundaries of the local domain + Real xMax, yMax, zMax; + + // cell widths + Real dx, dy, dz; + + static SpatialDomainProps From_Grav3D(Grav3D&); + static SpatialDomainProps From_Grid3D(Grid3D&, Parameters* P); +}; \ No newline at end of file diff --git a/src/particles/particles_3D.cpp b/src/particles/particles_3D.cpp index 9f8d207de..d9bfbd421 100644 --- a/src/particles/particles_3D.cpp +++ b/src/particles/particles_3D.cpp @@ -29,7 +29,13 @@ void Grid3D::Initialize_Particles(struct Parameters *P) { chprintf("\nInitializing Particles...\n"); - Particles.Initialize(P, Grav, H.xbound, H.ybound, H.zbound, H.xdglobal, H.ydglobal, H.zdglobal); +#ifdef GRAVITY + SpatialDomainProps spatial_props = SpatialDomainProps::From_Grav3D(Grav); +#else + SpatialDomainProps spatial_props = SpatialDomainProps::From_Grid3D(*this, P); +#endif + + Particles.Initialize(P, spatial_props, H.xbound, H.ybound, H.zbound, H.xdglobal, H.ydglobal, H.zdglobal); #if defined(PARTICLES_GPU) && defined(GRAVITY_GPU) // Set the GPU array for the particles potential equal to the Gravity GPU @@ -47,7 +53,7 @@ void Grid3D::Initialize_Particles(struct Parameters *P) chprintf("Particles Initialized Successfully. \n\n"); } -void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Real ybound, Real zbound, Real xdglobal, +void Particles3D::Initialize(struct Parameters *P, const SpatialDomainProps& spatial_props, Real xbound, Real ybound, Real zbound, Real xdglobal, Real ydglobal, Real zdglobal) { // Initialize local and total number of particles to 0 @@ -108,27 +114,27 @@ void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Re // Initialize Grid Values // Local and total number of cells - G.nx_local = Grav.nx_local; - G.ny_local = Grav.ny_local; - G.nz_local = Grav.nz_local; - G.nx_total = Grav.nx_total; - G.ny_total = Grav.ny_total; - G.nz_total = Grav.nz_total; + G.nx_local = spatial_props.nx_local; + G.ny_local = spatial_props.ny_local; + G.nz_local = spatial_props.nz_local; + G.nx_total = spatial_props.nx_total; + G.ny_total = spatial_props.ny_total; + G.nz_total = spatial_props.nz_total; // Uniform (dx, dy, dz) - G.dx = Grav.dx; - G.dy = Grav.dy; - G.dz = Grav.dz; + G.dx = spatial_props.dx; + G.dy = spatial_props.dy; + G.dz = spatial_props.dz; // Left boundaries of the local domain - G.xMin = Grav.xMin; - G.yMin = Grav.yMin; - G.zMin = Grav.zMin; + G.xMin = spatial_props.xMin; + G.yMin = spatial_props.yMin; + G.zMin = spatial_props.zMin; // Right boundaries of the local domain - G.xMax = Grav.xMax; - G.yMax = Grav.yMax; - G.zMax = Grav.zMax; + G.xMax = spatial_props.xMax; + G.yMax = spatial_props.yMax; + G.zMax = spatial_props.zMax; // Left boundaries of the global domain G.domainMin_x = xbound; @@ -176,8 +182,16 @@ void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Re #endif G.size_blocks_array = 1024 * 128; - G.n_cells_potential = (G.nx_local + 2 * N_GHOST_POTENTIAL) * (G.ny_local + 2 * N_GHOST_POTENTIAL) * - (G.nz_local + 2 * N_GHOST_POTENTIAL); + { + #ifdef N_GHOST_POTENTIAL + int n_ghost_pot = N_GHOST_POTENTIAL; + #else + int n_ghost_pot = 0; + #endif + + G.n_cells_potential = (G.nx_local + 2 * n_ghost_pot) * (G.ny_local + 2 * n_ghost_pot) * + (G.nz_local + 2 * n_ghost_pot); + } #ifdef SINGLE_PARTICLE_MASS mass_dev = NULL; // This array won't be used diff --git a/src/particles/particles_3D.h b/src/particles/particles_3D.h index 2ddd146e7..dbda79be5 100644 --- a/src/particles/particles_3D.h +++ b/src/particles/particles_3D.h @@ -14,6 +14,7 @@ #include "../global/global.h" #include "../gravity/grav3D.h" + #include "../grid/spatial_domain_props.h" #ifdef PARTICLES_GPU #define TPB_PARTICLES 1024 @@ -218,7 +219,7 @@ class Particles3D Particles3D(void); - void Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Real ybound, Real zbound, Real xdglobal, + void Initialize(struct Parameters *P, const SpatialDomainProps& spatial_props, Real xbound, Real ybound, Real zbound, Real xdglobal, Real ydglobal, Real zdglobal); void Allocate_Particles_Grid_Field_Real(Real **array_dev, int size);